Index: lld/trunk/ELF/InputFiles.h =================================================================== --- lld/trunk/ELF/InputFiles.h +++ lld/trunk/ELF/InputFiles.h @@ -194,8 +194,8 @@ void initializeSymbols(); void initializeDwarfLine(); InputSectionBase *getRelocTarget(const Elf_Shdr &Sec); - InputSectionBase *createInputSection(const Elf_Shdr &Sec, - StringRef SectionStringTable); + InputSectionBase *createInputSection(const Elf_Shdr &Sec); + StringRef getSectionName(const Elf_Shdr &Sec); bool shouldMerge(const Elf_Shdr &Sec); SymbolBody *createSymbolBody(const Elf_Sym *Sym); @@ -203,6 +203,9 @@ // List of all symbols referenced or defined by this file. std::vector SymbolBodies; + // .shstrtab contents. + StringRef SectionStringTable; + // Debugging information to retrieve source file and line for error // reporting. Linker may find reasonable number of errors in a // single object file, so we cache debugging information in order to Index: lld/trunk/ELF/InputFiles.cpp =================================================================== --- lld/trunk/ELF/InputFiles.cpp +++ lld/trunk/ELF/InputFiles.cpp @@ -205,13 +205,27 @@ StringRef elf::ObjectFile::getShtGroupSignature(ArrayRef Sections, const Elf_Shdr &Sec) { + // Group signatures are stored as symbol names in object files. + // sh_info contains a symbol index, so we fetch a symbol and read its name. if (this->Symbols.empty()) this->initSymtab( Sections, check(object::getSection(Sections, Sec.sh_link), toString(this))); + const Elf_Sym *Sym = check( object::getSymbol(this->Symbols, Sec.sh_info), toString(this)); - return check(Sym->getName(this->StringTable), toString(this)); + StringRef Signature = check(Sym->getName(this->StringTable), toString(this)); + + // As a special case, if a symbol is a section symbol and has no name, + // we use a section name as a signature. + // + // Such SHT_GROUP sections are invalid from the perspective of the ELF + // standard, but GNU gold 1.14 (the neweset version as of July 2017) or + // older produce such sections as outputs for the -r option, so we need + // a bug-compatibility. + if (Signature.empty() && Sym->getType() == STT_SECTION) + return getSectionName(Sec); + return Signature; } template @@ -287,8 +301,7 @@ check(this->getObj().sections(), toString(this)); uint64_t Size = ObjSections.size(); this->Sections.resize(Size); - - StringRef SectionStringTable = + this->SectionStringTable = check(Obj.getSectionStringTable(ObjSections), toString(this)); for (size_t I = 0, E = ObjSections.size(); I < E; I++) { @@ -318,7 +331,7 @@ // object files, we want to pass through basically everything. if (IsNew) { if (Config->Relocatable) - this->Sections[I] = createInputSection(Sec, SectionStringTable); + this->Sections[I] = createInputSection(Sec); continue; } @@ -342,7 +355,7 @@ case SHT_NULL: break; default: - this->Sections[I] = createInputSection(Sec, SectionStringTable); + this->Sections[I] = createInputSection(Sec); } // .ARM.exidx sections have a reverse dependency on the InputSection they @@ -386,10 +399,8 @@ template InputSectionBase * -elf::ObjectFile::createInputSection(const Elf_Shdr &Sec, - StringRef SectionStringTable) { - StringRef Name = check( - this->getObj().getSectionName(&Sec, SectionStringTable), toString(this)); +elf::ObjectFile::createInputSection(const Elf_Shdr &Sec) { + StringRef Name = getSectionName(Sec); switch (Sec.sh_type) { case SHT_ARM_ATTRIBUTES: @@ -521,6 +532,12 @@ return make(this, &Sec, Name); } +template +StringRef elf::ObjectFile::getSectionName(const Elf_Shdr &Sec) { + return check(this->getObj().getSectionName(&Sec, SectionStringTable), + toString(this)); +} + template void elf::ObjectFile::initializeSymbols() { SymbolBodies.reserve(this->Symbols.size()); for (const Elf_Sym &Sym : this->Symbols) Index: lld/trunk/test/ELF/Inputs/sht-group-gold-r.s =================================================================== --- lld/trunk/test/ELF/Inputs/sht-group-gold-r.s +++ lld/trunk/test/ELF/Inputs/sht-group-gold-r.s @@ -0,0 +1,14 @@ +# sht-group-gold-r.elf is produced by +# +# llvm-mc -filetype=obj -triple=x86_64-pc-linux sht-group-gold-r.s -o sht-group-gold-r.o +# ld.gold -o sht-group-gold-r.elf -r sht-group-gold-r.o + +.global foo, bar + +.section .text.foo,"aG",@progbits,group_foo,comdat +foo: + nop + +.section .text.bar,"aG",@progbits,group_bar,comdat +bar: + nop Index: lld/trunk/test/ELF/sht-group-gold-r.test =================================================================== --- lld/trunk/test/ELF/sht-group-gold-r.test +++ lld/trunk/test/ELF/sht-group-gold-r.test @@ -0,0 +1,17 @@ +# GNU gold 1.14 (the newest version as of July 2017) seems to create +# non-standard-compliant SHT_GROUP sections when the -r option is given. +# +# Such SHT_GROUP sections use section names as their signatures +# instead of symbols pointed by sh_link field. Since it is prevalent, +# we accept such nonstandard sections. + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld %p/Inputs/sht-group-gold-r.elf %t.o -o %t.exe +# RUN: llvm-objdump -t %t.exe | FileCheck %s + +# CHECK: .text 00000000 bar +# CHECK: .text 00000000 foo + +.globl _start +_start: + ret