[llvm] r333581 - llvm-objcopy: Set sh_link to 0 on unrecognized symtab-linked sections.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Wed May 30 12:30:39 PDT 2018


Author: pcc
Date: Wed May 30 12:30:39 2018
New Revision: 333581

URL: http://llvm.org/viewvc/llvm-project?rev=333581&view=rev
Log:
llvm-objcopy: Set sh_link to 0 on unrecognized symtab-linked sections.

Per discussion on the generic-abi mailing list:
https://groups.google.com/forum/#!topic/generic-abi/MPr8TVtnVn4

An object file manipulation tool must either write out a symbol
table with the same number of entries as the original symbol table
and in the same order, or if this is impossible, refuse to operate
on the object file if it has unrecognized sections that are linked
to the symtab section. However, existing tools (namely GNU strip,
GNU objcopy and ld.{bfd,gold,lld} -r) do not comply with this at
present: they change symbol table indexes and set sh_link to 0 on
the unrecognized symtab-linked sections.

We intend to use the latter as a (temporary) signal that a tool has
operated on a proposed new symtab-linked section and invalidated the
symbol table indexes. However, llvm-objcopy currently keeps sh_link
pointing to the new symtab section. This patch changes llvm-objcopy
to set sh_link to 0 to match the behaviour of the other tools.

Differential Revision: https://reviews.llvm.org/D47404

Added:
    llvm/trunk/test/tools/llvm-objcopy/symtab-link.test
Modified:
    llvm/trunk/tools/llvm-objcopy/Object.cpp

Added: llvm/trunk/test/tools/llvm-objcopy/symtab-link.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/symtab-link.test?rev=333581&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/symtab-link.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/symtab-link.test Wed May 30 12:30:39 2018
@@ -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

Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=333581&r1=333580&r2=333581&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Wed May 30 12:30:39 2018
@@ -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) {




More information about the llvm-commits mailing list