[llvm] r342065 - [objcopy] make objcopy follow program header standards
Julie Hockett via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 12 10:56:32 PDT 2018
Author: juliehockett
Date: Wed Sep 12 10:56:31 2018
New Revision: 342065
URL: http://llvm.org/viewvc/llvm-project?rev=342065&view=rev
Log:
[objcopy] make objcopy follow program header standards
Submitted on behalf of Armando Montanez (amontanez at google.com).
Objects with unused program headers copied by objcopy would always have
nonzero values for program header offset and program header entry size.
While technically valid, this atypical behavior triggers warnings in some
tools. This change sets the two fields to zero when the program header is
unused, better fitting the general expectations for unused program header
data.
Section headers behaved somewhat similarly (though only with the entry size),
and are fixed in this revision as well.
Differential Revision: https://reviews.llvm.org/D51961
Added:
llvm/trunk/test/tools/llvm-objcopy/relocatable-phdr.test
Modified:
llvm/trunk/test/tools/llvm-objcopy/binary-input-arch.test
llvm/trunk/test/tools/llvm-objcopy/many-sections.test
llvm/trunk/test/tools/llvm-objcopy/strip-sections.test
llvm/trunk/tools/llvm-objcopy/Object.cpp
Modified: llvm/trunk/test/tools/llvm-objcopy/binary-input-arch.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/binary-input-arch.test?rev=342065&r1=342064&r2=342065&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/binary-input-arch.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/binary-input-arch.test Wed Sep 12 10:56:31 2018
@@ -65,8 +65,8 @@
# CHECK-NEXT: ]
# 32-NEXT: HeaderSize: 52
# 64-NEXT: HeaderSize: 64
-# 32-NEXT: ProgramHeaderEntrySize: 32
-# 64-NEXT: ProgramHeaderEntrySize: 56
+# 32-NEXT: ProgramHeaderEntrySize: 0
+# 64-NEXT: ProgramHeaderEntrySize: 0
# CHECK-NEXT: ProgramHeaderCount: 0
# 32-NEXT: SectionHeaderEntrySize: 40
# 64-NEXT: SectionHeaderEntrySize: 64
Modified: llvm/trunk/test/tools/llvm-objcopy/many-sections.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/many-sections.test?rev=342065&r1=342064&r2=342065&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/many-sections.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/many-sections.test Wed Sep 12 10:56:31 2018
@@ -22,12 +22,12 @@ EHDR-NEXT: Type: Relocatable (0x1)
EHDR-NEXT: Machine: EM_X86_64 (0x3E)
EHDR-NEXT: Version: 1
EHDR-NEXT: Entry: 0x0
-EHDR-NEXT: ProgramHeaderOffset: 0x40
+EHDR-NEXT: ProgramHeaderOffset: 0x0
EHDR-NEXT: SectionHeaderOffset:
EHDR-NEXT: Flags [ (0x0)
EHDR-NEXT: ]
EHDR-NEXT: HeaderSize: 64
-EHDR-NEXT: ProgramHeaderEntrySize: 56
+EHDR-NEXT: ProgramHeaderEntrySize: 0
EHDR-NEXT: ProgramHeaderCount: 0
EHDR-NEXT: SectionHeaderEntrySize: 64
EHDR-NEXT: SectionHeaderCount: 0
Added: llvm/trunk/test/tools/llvm-objcopy/relocatable-phdr.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/relocatable-phdr.test?rev=342065&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/relocatable-phdr.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/relocatable-phdr.test Wed Sep 12 10:56:31 2018
@@ -0,0 +1,16 @@
+# This test ensures an object without a program header will retain zero values
+# for offset and header size when copied with llvm-objcopy.
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objcopy %t %t2
+# RUN: llvm-readobj -file-headers %t2 | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+
+# CHECK: ProgramHeaderOffset: 0x0
+# CHECK: ProgramHeaderEntrySize: 0
+# CHECK-NEXT: ProgramHeaderCount: 0
Modified: llvm/trunk/test/tools/llvm-objcopy/strip-sections.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/strip-sections.test?rev=342065&r1=342064&r2=342065&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-sections.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/strip-sections.test Wed Sep 12 10:56:31 2018
@@ -44,7 +44,7 @@ ProgramHeaders:
#CHECK-NEXT: HeaderSize: 64
#CHECK-NEXT: ProgramHeaderEntrySize: 56
#CHECK-NEXT: ProgramHeaderCount: 1
-#CHECK-NEXT: SectionHeaderEntrySize: 64
+#CHECK-NEXT: SectionHeaderEntrySize: 0
#CHECK-NEXT: SectionHeaderCount: 0
#CHECK-NEXT: StringTableSectionIndex: 0
#CHECK-NEXT: }
Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=342065&r1=342064&r2=342065&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Wed Sep 12 10:56:31 2018
@@ -1121,15 +1121,13 @@ template <class ELFT> void ELFWriter<ELF
Ehdr.e_machine = Obj.Machine;
Ehdr.e_version = Obj.Version;
Ehdr.e_entry = Obj.Entry;
- // TODO: Only set phoff when a program header exists, to avoid tools
- // thinking this is corrupt data.
- Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset;
+ Ehdr.e_phnum = size(Obj.segments());
+ Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0;
+ Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
Ehdr.e_flags = Obj.Flags;
Ehdr.e_ehsize = sizeof(Elf_Ehdr);
- Ehdr.e_phentsize = sizeof(Elf_Phdr);
- Ehdr.e_phnum = size(Obj.segments());
- Ehdr.e_shentsize = sizeof(Elf_Shdr);
- if (WriteSectionHeaders) {
+ if (WriteSectionHeaders && size(Obj.sections()) != 0) {
+ Ehdr.e_shentsize = sizeof(Elf_Shdr);
Ehdr.e_shoff = Obj.SHOffset;
// """
// If the number of sections is greater than or equal to
@@ -1153,6 +1151,7 @@ template <class ELFT> void ELFWriter<ELF
else
Ehdr.e_shstrndx = Obj.SectionNames->Index;
} else {
+ Ehdr.e_shentsize = 0;
Ehdr.e_shoff = 0;
Ehdr.e_shnum = 0;
Ehdr.e_shstrndx = 0;
More information about the llvm-commits
mailing list