From e3a21fcc3570a79f8e9ed0c4bc26cbc6cf33804f Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Mon, 28 Oct 2019 13:47:44 +0300 Subject: [PATCH] [yaml2obj] - Make .symtab to be not mandatory section for SHT_REL[A] section. Before this change .symtab section was required for SHT_REL[A] section declarations. yaml2obj automatically defined it in case when YAML document did not have it. With this change it is now possible to produce an object that has a relocation section, but has no symbol table. It simplifies the code and also it is inline with how we handle Link fields for another special sections. Differential revision: https://reviews.llvm.org/D69260 --- lib/ObjectYAML/ELFEmitter.cpp | 14 ++++---------- test/Object/invalid.test | 1 + test/Object/objdump-sectionheaders.test | 1 + .../llvm-objcopy/ELF/no-symbol-relocation.test | 2 ++ test/tools/llvm-readobj/all.test | 1 + .../elf-reloc-negative-addend-no-sym.test | 1 - test/tools/obj2yaml/elf-ppc64-relocations.yaml | 1 - test/tools/obj2yaml/no-symbol-reloc.test | 2 -- .../obj2yaml/relocation-unsupported-machine.yaml | 1 - 9 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/ObjectYAML/ELFEmitter.cpp b/lib/ObjectYAML/ELFEmitter.cpp index 6a4e00c6285..64b5cd4df5b 100644 --- a/lib/ObjectYAML/ELFEmitter.cpp +++ b/lib/ObjectYAML/ELFEmitter.cpp @@ -205,17 +205,10 @@ template ELFState::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH) : Doc(D), ErrHandler(EH) { StringSet<> DocSections; - for (std::unique_ptr &D : Doc.Sections) { + for (std::unique_ptr &D : Doc.Sections) if (!D->Name.empty()) DocSections.insert(D->Name); - // Some sections wants to link to .symtab by default. - // That means we want to create the symbol table for them. - if (D->Type == llvm::ELF::SHT_REL || D->Type == llvm::ELF::SHT_RELA) - if (!Doc.Symbols && D->Link.empty()) - Doc.Symbols.emplace(); - } - // Insert SHT_NULL section implicitly when it is not defined in YAML. if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL) Doc.Sections.insert( @@ -742,8 +735,9 @@ void ELFState::writeSectionContent( SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); // For relocation section set link to .symtab by default. - if (Section.Link.empty()) - SHeader.sh_link = SN2I.get(".symtab"); + unsigned Link = 0; + if (Section.Link.empty() && SN2I.lookup(".symtab", Link)) + SHeader.sh_link = Link; if (!Section.RelocatableSec.empty()) SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); diff --git a/test/Object/invalid.test b/test/Object/invalid.test index 396776c5f91..2c95eea74f2 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -333,6 +333,7 @@ Sections: - Offset: 0x0 Type: R_X86_64_64 Symbol: 0xFFFFFF +Symbols: [] ## Check llvm-readobj does not crash on a truncated ELF. diff --git a/test/Object/objdump-sectionheaders.test b/test/Object/objdump-sectionheaders.test index fba4fa79980..aab3f54d7db 100644 --- a/test/Object/objdump-sectionheaders.test +++ b/test/Object/objdump-sectionheaders.test @@ -39,3 +39,4 @@ Sections: Address: 0x0000000000000038 Info: .text Relocations: +Symbols: [] diff --git a/test/tools/llvm-objcopy/ELF/no-symbol-relocation.test b/test/tools/llvm-objcopy/ELF/no-symbol-relocation.test index 4b13dda6484..91040170d73 100644 --- a/test/tools/llvm-objcopy/ELF/no-symbol-relocation.test +++ b/test/tools/llvm-objcopy/ELF/no-symbol-relocation.test @@ -21,6 +21,8 @@ Sections: Relocations: - Offset: 0x1000 Type: R_X86_64_RELATIVE +## TODO: llvm-objcopy crashes without the following line. +Symbols: [] # CHECK: Relocations [ # CHECK-NEXT: Section (2) .rel.text { diff --git a/test/tools/llvm-readobj/all.test b/test/tools/llvm-readobj/all.test index 944ad038400..5edda0af1f9 100644 --- a/test/tools/llvm-readobj/all.test +++ b/test/tools/llvm-readobj/all.test @@ -110,3 +110,4 @@ ProgramHeaders: - Type: PT_NOTE Sections: - Section: .note.gnu.build-id +Symbols: [] diff --git a/test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test b/test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test index 86ec658f19a..3a318d52175 100644 --- a/test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test +++ b/test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test @@ -29,7 +29,6 @@ Sections: Size: 0x10 - Name: .rela.text Type: SHT_RELA - Link: .symtab Info: .text Relocations: - Offset: 0 diff --git a/test/tools/obj2yaml/elf-ppc64-relocations.yaml b/test/tools/obj2yaml/elf-ppc64-relocations.yaml index 68be4276fc2..d80d9290cc8 100644 --- a/test/tools/obj2yaml/elf-ppc64-relocations.yaml +++ b/test/tools/obj2yaml/elf-ppc64-relocations.yaml @@ -12,7 +12,6 @@ # CHECK-NEXT: Sections: # CHECK-NEXT: - Name: .rela.text # CHECK-NEXT: Type: SHT_RELA -# CHECK-NEXT: Link: .symtab # CHECK-NEXT: EntSize: 0x0000000000000018 # CHECK-NEXT: Relocations: # CHECK-NEXT: - Offset: 0x0000000000000000 diff --git a/test/tools/obj2yaml/no-symbol-reloc.test b/test/tools/obj2yaml/no-symbol-reloc.test index 09299968885..56d8c1ae101 100644 --- a/test/tools/obj2yaml/no-symbol-reloc.test +++ b/test/tools/obj2yaml/no-symbol-reloc.test @@ -16,7 +16,6 @@ # CHECK-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ] # CHECK-NEXT: - Name: .rela.text # CHECK-NEXT: Type: SHT_RELA -# CHECK-NEXT: Link: .symtab # CHECK-NEXT: EntSize: 0x0000000000000018 # CHECK-NEXT: Info: .text # CHECK-NEXT: Relocations: @@ -24,7 +23,6 @@ # CHECK-NEXT: Type: R_X86_64_NONE # CHECK-NEXT: - Offset: 0x0000000000000004 # CHECK-NEXT: Type: R_X86_64_NONE -# CHECK-NEXT: Symbols: [] # CHECK-NEXT: ... --- !ELF diff --git a/test/tools/obj2yaml/relocation-unsupported-machine.yaml b/test/tools/obj2yaml/relocation-unsupported-machine.yaml index 54dd2e7b928..84e5c13b53d 100644 --- a/test/tools/obj2yaml/relocation-unsupported-machine.yaml +++ b/test/tools/obj2yaml/relocation-unsupported-machine.yaml @@ -12,7 +12,6 @@ # CHECK-NEXT: Sections: # CHECK-NEXT: - Name: .rela.text # CHECK-NEXT: Type: SHT_RELA -# CHECK-NEXT: Link: .symtab # CHECK-NEXT: EntSize: 0x0000000000000018 # CHECK-NEXT: Relocations: # CHECK-NEXT: - Offset: 0x0000000000000001