diff --git a/test/tools/llvm-objcopy/symtab-link.test b/test/tools/llvm-objcopy/symtab-link.test new file mode 100644 index 00000000000..7ed895e1c62 --- /dev/null +++ b/test/tools/llvm-objcopy/symtab-link.test @@ -0,0 +1,27 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj -sections %t2 | FileCheck %s +# RUN: cp %t %t3 +# RUN: llvm-strip --strip-debug %t3 +# RUN: llvm-readobj -sections %t3 | 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 diff --git a/tools/llvm-objcopy/Object.cpp b/tools/llvm-objcopy/Object.cpp index c00477a1980..6b656c6b2f7 100644 --- a/tools/llvm-objcopy/Object.cpp +++ b/tools/llvm-objcopy/Object.cpp @@ -401,15 +401,17 @@ void GroupSection::markSymbols() { } 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; + this->Link = LinkSection ? LinkSection->Index : 0; } void GnuDebugLinkSection::init(StringRef File, StringRef Data) {