Index: llvm/test/tools/llvm-objcopy/symtab-link.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objcopy/symtab-link.test @@ -0,0 +1,24 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj -sections %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Link: .symtab + Type: SHT_PROGBITS + Flags: [ ] + +# CHECK: Name: .foo +# CHECK-NEXT: Type: +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Address: +# CHECK-NEXT: Offset: +# CHECK-NEXT: Size: +# CHECK-NEXT: Link: 0 Index: llvm/tools/llvm-objcopy/Object.cpp =================================================================== --- llvm/tools/llvm-objcopy/Object.cpp +++ llvm/tools/llvm-objcopy/Object.cpp @@ -385,15 +385,20 @@ } void Section::initialize(SectionTableRef SecTable) { - if (Link != ELF::SHN_UNDEF) + if (Link != ELF::SHN_UNDEF) { LinkSection = SecTable.getSection(Link, "Link field value " + Twine(Link) + " in section " + Name + " is invalid"); + if (LinkSection->Type == ELF::SHT_SYMTAB) + LinkSection = nullptr; + } } void Section::finalize() { if (LinkSection) this->Link = LinkSection->Index; + else + this->Link = 0; } void GnuDebugLinkSection::init(StringRef File, StringRef Data) {