diff options
author | Julie Hockett <juliehockett@google.com> | 2018-09-12 17:56:31 +0000 |
---|---|---|
committer | Julie Hockett <juliehockett@google.com> | 2018-09-12 17:56:31 +0000 |
commit | 468722ee9f74cf4eedc4b6bf2db34e43be6b7ec7 (patch) | |
tree | 6a0dc7df03eacc16b6b68d31e27c60788ab0830f /llvm/tools/llvm-objcopy/Object.cpp | |
parent | ebd4c906d870bc0e64e63fdf0fec3169bc4fc7d0 (diff) | |
download | bcm5719-llvm-468722ee9f74cf4eedc4b6bf2db34e43be6b7ec7.tar.gz bcm5719-llvm-468722ee9f74cf4eedc4b6bf2db34e43be6b7ec7.zip |
[objcopy] make objcopy follow program header standards
Submitted on behalf of Armando Montanez (amontanez@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
llvm-svn: 342065
Diffstat (limited to 'llvm/tools/llvm-objcopy/Object.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/Object.cpp | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index aaefcf99f38..e57a626fcad 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -1121,15 +1121,13 @@ template <class ELFT> void ELFWriter<ELFT>::writeEhdr() { 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<ELFT>::writeEhdr() { else Ehdr.e_shstrndx = Obj.SectionNames->Index; } else { + Ehdr.e_shentsize = 0; Ehdr.e_shoff = 0; Ehdr.e_shnum = 0; Ehdr.e_shstrndx = 0; |