Index: lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- lib/ObjectYAML/ELFEmitter.cpp +++ lib/ObjectYAML/ELFEmitter.cpp @@ -200,17 +200,10 @@ 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( @@ -735,8 +728,9 @@ 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); Index: test/Object/invalid.test =================================================================== --- test/Object/invalid.test +++ test/Object/invalid.test @@ -333,6 +333,7 @@ - Offset: 0x0 Type: R_X86_64_64 Symbol: 0xFFFFFF +Symbols: [] ## Check llvm-readobj does not crash on a truncated ELF. Index: test/Object/objdump-sectionheaders.test =================================================================== --- test/Object/objdump-sectionheaders.test +++ test/Object/objdump-sectionheaders.test @@ -39,3 +39,4 @@ Address: 0x0000000000000038 Info: .text Relocations: +Symbols: [] Index: test/tools/llvm-objcopy/ELF/no-symbol-relocation.test =================================================================== --- test/tools/llvm-objcopy/ELF/no-symbol-relocation.test +++ test/tools/llvm-objcopy/ELF/no-symbol-relocation.test @@ -21,6 +21,8 @@ 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 { Index: test/tools/llvm-readobj/all.test =================================================================== --- test/tools/llvm-readobj/all.test +++ test/tools/llvm-readobj/all.test @@ -110,3 +110,4 @@ - Type: PT_NOTE Sections: - Section: .note.gnu.build-id +Symbols: [] Index: test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test =================================================================== --- test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test +++ test/tools/llvm-readobj/elf-reloc-negative-addend-no-sym.test @@ -29,7 +29,6 @@ Size: 0x10 - Name: .rela.text Type: SHT_RELA - Link: .symtab Info: .text Relocations: - Offset: 0 Index: test/tools/obj2yaml/elf-ppc64-relocations.yaml =================================================================== --- test/tools/obj2yaml/elf-ppc64-relocations.yaml +++ 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 Index: test/tools/obj2yaml/no-symbol-reloc.test =================================================================== --- test/tools/obj2yaml/no-symbol-reloc.test +++ 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 Index: test/tools/obj2yaml/relocation-unsupported-machine.yaml =================================================================== --- test/tools/obj2yaml/relocation-unsupported-machine.yaml +++ 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 Index: test/tools/yaml2obj/reloc-sec-info-link.yaml =================================================================== --- test/tools/yaml2obj/reloc-sec-info-link.yaml +++ test/tools/yaml2obj/reloc-sec-info-link.yaml @@ -49,3 +49,46 @@ Type: SHT_RELA Info: 0xFF Relocations: + +## Check we set sh_link field of a relocation section to 0 when there is +## no symbol table. + +# RUN: yaml2obj --docnum=3 %s -o %t3 +# RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=LINK-ZERO + +# LINK-ZERO: Name: .foo +# LINK-ZERO: Link: +# LINK-ZERO-SAME: 0 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_RELA + +## Check we link a relocation section with the .symtab section when one is exist. + +# RUN: yaml2obj --docnum=4 %s -o %t4 +# RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=LINK-SYMTAB + +# LINK-SYMTAB: Name: .foo +# LINK-SYMTAB: Link: +# LINK-SYMTAB-SAME: 2 + +# LINK-SYMTAB: Index: 3 +# LINK-SYMTAB-NEXT: Name: .strtab + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_RELA +Symbols: []