diff options
| author | George Rimar <grimar@accesssoftek.com> | 2019-06-27 11:08:42 +0000 |
|---|---|---|
| committer | George Rimar <grimar@accesssoftek.com> | 2019-06-27 11:08:42 +0000 |
| commit | 687d47c2b0cebf17773def27291fcb8524f4313b (patch) | |
| tree | 1500ea930d13892414e8728a27deb2b6b36c5944 /llvm | |
| parent | 7eeeb5947ec0920460b2e32025c4cc8ccffc7719 (diff) | |
| download | bcm5719-llvm-687d47c2b0cebf17773def27291fcb8524f4313b.tar.gz bcm5719-llvm-687d47c2b0cebf17773def27291fcb8524f4313b.zip | |
[yaml2obj] - Allow overriding e_shentsize, e_shoff, e_shnum and e_shstrndx fields in the YAML.
This allows setting different values for e_shentsize, e_shoff, e_shnum
and e_shstrndx fields and is useful for producing broken inputs for various
test cases.
Differential revision: https://reviews.llvm.org/D63771
llvm-svn: 364517
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/ObjectYAML/ELFYAML.h | 5 | ||||
| -rw-r--r-- | llvm/lib/ObjectYAML/ELFYAML.cpp | 5 | ||||
| -rw-r--r-- | llvm/test/tools/yaml2obj/elf-header-sh-fields.yaml | 61 | ||||
| -rw-r--r-- | llvm/tools/obj2yaml/elf2yaml.cpp | 4 | ||||
| -rw-r--r-- | llvm/tools/yaml2obj/yaml2elf.cpp | 14 |
5 files changed, 84 insertions, 5 deletions
diff --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h index 38e19d28faf..b12c2936692 100644 --- a/llvm/include/llvm/ObjectYAML/ELFYAML.h +++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -75,6 +75,11 @@ struct FileHeader { ELF_EM Machine; ELF_EF Flags; llvm::yaml::Hex64 Entry; + + Optional<llvm::yaml::Hex16> SHEntSize; + Optional<llvm::yaml::Hex16> SHOffset; + Optional<llvm::yaml::Hex16> SHNum; + Optional<llvm::yaml::Hex16> SHStrNdx; }; struct SectionName { diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp index ef46ca73ec6..49161623cd7 100644 --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -842,6 +842,11 @@ void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO, IO.mapRequired("Machine", FileHdr.Machine); IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); IO.mapOptional("Entry", FileHdr.Entry, Hex64(0)); + + IO.mapOptional("SHEntSize", FileHdr.SHEntSize); + IO.mapOptional("SHOffset", FileHdr.SHOffset); + IO.mapOptional("SHNum", FileHdr.SHNum); + IO.mapOptional("SHStrNdx", FileHdr.SHStrNdx); } void MappingTraits<ELFYAML::ProgramHeader>::mapping( diff --git a/llvm/test/tools/yaml2obj/elf-header-sh-fields.yaml b/llvm/test/tools/yaml2obj/elf-header-sh-fields.yaml new file mode 100644 index 00000000000..65a503c3d3d --- /dev/null +++ b/llvm/test/tools/yaml2obj/elf-header-sh-fields.yaml @@ -0,0 +1,61 @@ +## In this test case we check that we can override the default values for +## e_shentsize, e_shoff, e_shnum and e_shstrndx fields in the YAML. + +## First we check the default values. + +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readelf --file-headers %t1 | FileCheck %s --check-prefix=DEFAULT + +# DEFAULT: Start of section headers: 64 (bytes into file) +# DEFAULT: Size of section headers: 64 (bytes) +# DEFAULT: Number of section headers: 4 +# DEFAULT: Section header string table index: 3 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + +## Override 3 fields: e_shoff, e_shnum and e_shstrndx. Check the output. + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: llvm-readelf --file-headers %t2 | FileCheck %s --check-prefix=CUSTOM + +# CUSTOM: Start of section headers: 2 (bytes into file) +# CUSTOM: Size of section headers: 64 (bytes) +# CUSTOM: Number of section headers: 3 +# CUSTOM: Section header string table index: 4 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SHEntSize: 64 + SHOffset: 2 + SHNum: 3 + SHStrNdx: 4 + +## Finally, we use the same YAML as above, but set e_shentsize to 1. +## Check the result using raw output from 'od' because llvm-readelf +## is unable to dump such headers. + +# RUN: yaml2obj --docnum=3 %s -o %t3 +# RUN: od -t x1 -v -j 0x3a -N 1 %t3 | FileCheck %s --check-prefix=NEWSIZE +# RUN: od -t x1 -v -j 0x3a -N 1 %t2 | FileCheck %s --check-prefix=OLDSIZE +# NEWSIZE: 0000072 01 +# OLDSIZE: 0000072 40 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SHEntSize: 1 + SHOffset: 2 + SHNum: 3 + SHStrNdx: 4 diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp index a47d5537a87..804101f2dee 100644 --- a/llvm/tools/obj2yaml/elf2yaml.cpp +++ b/llvm/tools/obj2yaml/elf2yaml.cpp @@ -137,7 +137,9 @@ ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable, template <class ELFT> ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() { auto Y = make_unique<ELFYAML::Object>(); - // Dump header + // Dump header. We do not dump SHEntSize, SHOffset, SHNum and SHStrNdx field. + // When not explicitly set, the values are set by yaml2obj automatically + // and there is no need to dump them here. Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass()); Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding()); Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI]; diff --git a/llvm/tools/yaml2obj/yaml2elf.cpp b/llvm/tools/yaml2obj/yaml2elf.cpp index f3f78005043..e093ed8d72f 100644 --- a/llvm/tools/yaml2obj/yaml2elf.cpp +++ b/llvm/tools/yaml2obj/yaml2elf.cpp @@ -208,12 +208,18 @@ void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) { Header.e_ehsize = sizeof(Elf_Ehdr); Header.e_phentsize = sizeof(Elf_Phdr); Header.e_phnum = Doc.ProgramHeaders.size(); - Header.e_shentsize = sizeof(Elf_Shdr); + + Header.e_shentsize = + Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr); // Immediately following the ELF header and program headers. Header.e_shoff = - sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size(); - Header.e_shnum = SN2I.size() + 1; - Header.e_shstrndx = SN2I.get(".shstrtab"); + Doc.Header.SHOffset + ? (uint16_t)*Doc.Header.SHOffset + : sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size(); + Header.e_shnum = + Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : SN2I.size() + 1; + Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx + : SN2I.get(".shstrtab"); } template <class ELFT> |

