[PATCH] D53993: [ELF] Set sh_link to 0 in static link to make GNU strip happy

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 1 12:45:52 PDT 2018


MaskRay created this revision.
MaskRay added reviewers: ruiu, grimar, emaste.
Herald added subscribers: llvm-commits, arichardson.
Herald added a reviewer: espindola.

https://reviews.llvm.org/D52830 sets sh_link to .symtab in static link, which makes more sense
than 0, but it unfortunately breaks executable stripped by GNU strip.

We cannot set it to .symtab as otherwise GNU strip will change the
address of .rela.plt (and rename it to .rela.got.plt) and break
__rela_iplt_{start,end} used in glibc/csu/libc-start.c:apply_irel. Note,
GNU readelf will warn "Link field (0) should index a symtab section",
but we can live with that.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D53993

Files:
  ELF/SyntheticSections.cpp


Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1491,15 +1491,15 @@
 }
 
 void RelocationBaseSection::finalizeContents() {
-  // If all relocations are R_*_{,I}RELATIVE they don't refer to any dynamic
-  // symbol and we don't need a dynamic symbol table. If that is the case, use
-  // the index of the regular symbol table section (if exists) or 0.
-  if (In.DynSymTab)
-    getParent()->Link = In.DynSymTab->getParent()->SectionIndex;
-  else if (In.SymTab)
-    getParent()->Link = In.SymTab->getParent()->SectionIndex;
-  else
-    getParent()->Link = 0;
+  // When linking glibc statically, .rela?.plt contains R_*_IRELATIVE
+  // relocations due to IFUNC (e.g. strcpy). Just set sh_link to 0 as there is
+  // no .dynsym. We cannot set it to .symtab as otherwise GNU strip will change
+  // the address of .rela.plt (and rename it to .rela.got.plt) and break
+  // __rela_iplt_{start,end} used in glibc/csu/libc-start.c:apply_irel. Note,
+  // GNU readelf will warn "Link field (0) should index a symtab section", but
+  // we can live with that.
+  getParent()->Link =
+      In.DynSymTab ? In.DynSymTab->getParent()->SectionIndex : 0;
 
   if (In.RelaIplt == this || In.RelaPlt == this)
     getParent()->Info = In.GotPlt->getParent()->SectionIndex;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53993.172205.patch
Type: text/x-patch
Size: 1376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181101/504a05c7/attachment.bin>


More information about the llvm-commits mailing list