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

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 25 18:33:44 PDT 2018


pcc created this revision.
pcc added reviewers: phosek, jakehehrlich, alexshap.

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.


https://reviews.llvm.org/D47404

Files:
  llvm/test/tools/llvm-objcopy/symtab-link.test
  llvm/tools/llvm-objcopy/Object.cpp


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) {
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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47404.148698.patch
Type: text/x-patch
Size: 1536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180526/9f487df8/attachment-0001.bin>


More information about the llvm-commits mailing list