From ec70b5d16b0d32b54001bc40b35f301f4ac44938 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 19 Feb 2019 12:15:04 +0000 Subject: [PATCH] [yaml2obj] - Do not ignore explicit addresses for .dynsym and .dynstr This fixes https://bugs.llvm.org/show_bug.cgi?id=40339 Previously if the addresses were set in YAML they were ignored for .dynsym and .dynstr sections. The patch fixes that. Differential revision: https://reviews.llvm.org/D58168 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354318 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/yaml2obj/dynsym-dynstr-addr.yaml | 40 +++++++++++++++++++++ tools/yaml2obj/yaml2elf.cpp | 18 ++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/tools/yaml2obj/dynsym-dynstr-addr.yaml diff --git a/test/tools/yaml2obj/dynsym-dynstr-addr.yaml b/test/tools/yaml2obj/dynsym-dynstr-addr.yaml new file mode 100644 index 00000000000..c54787cf30e --- /dev/null +++ b/test/tools/yaml2obj/dynsym-dynstr-addr.yaml @@ -0,0 +1,40 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-readobj -sections %t | FileCheck %s + +## Check yaml2obj does not ignore the address of the +## explicitly listed .dynstr and .dynsym sections. + +# CHECK: Name: .dynstr +# CHECK-NEXT: Type: SHT_STRTAB +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x1000 + +# CHECK: Name: .dynsym +# CHECK-NEXT: Type: SHT_DYNSYM +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x2000 + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1000 + EntSize: 0x1 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x2000 + EntSize: 0x18 +DynamicSymbols: + Global: + - Name: foo diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp index 5198a751cfb..ac1626aa1a3 100644 --- a/tools/yaml2obj/yaml2elf.cpp +++ b/tools/yaml2obj/yaml2elf.cpp @@ -321,6 +321,15 @@ void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader, SHeader.sh_entsize = sizeof(Elf_Sym); SHeader.sh_addralign = 8; + // If .dynsym section is explicitly described in the YAML + // then we want to use its section address. + if (!IsStatic) { + // Take section index and ignore the SHT_NULL section. + unsigned SecNdx = getDotDynSymSecNo() - 1; + if (SecNdx < Doc.Sections.size()) + SHeader.sh_addr = Doc.Sections[SecNdx]->Address; + } + std::vector Syms; { // Ensure STN_UNDEF is present @@ -358,6 +367,15 @@ void ELFState::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); SHeader.sh_size = STB.getSize(); SHeader.sh_addralign = 1; + + // If .dynstr section is explicitly described in the YAML + // then we want to use its section address. + if (Name == ".dynstr") { + // Take section index and ignore the SHT_NULL section. + unsigned SecNdx = getDotDynStrSecNo() - 1; + if (SecNdx < Doc.Sections.size()) + SHeader.sh_addr = Doc.Sections[SecNdx]->Address; + } } template -- 2.40.0