[PATCH] D63869: [ELF] Do not produce DT_JMPREL and DT_PLTGOT if .rela.plt is empty.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 04:11:11 PDT 2019


ikudrin created this revision.
ikudrin added reviewers: ruiu, grimar.
ikudrin added a project: lld.
Herald added subscribers: MaskRay, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

If `.rela.plt` is mentioned in a linker script, it might be preserved even if it is empty. In that case, LLD created DT_JMPREL and DT_PLTGOT dynamic tags. When the tags are present, a dynamic loader writes values into reserved fields in `.got.plt` to support lazy symbol resolution. The problem is that, in fact, the linker has not reserved that space, and the writing may occur into the memory allocated for something else.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D63869

Files:
  ELF/SyntheticSections.cpp
  test/ELF/empty-relaplt-dyntags.s


Index: test/ELF/empty-relaplt-dyntags.s
===================================================================
--- /dev/null
+++ test/ELF/empty-relaplt-dyntags.s
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+
+# RUN: echo "PHDRS { \
+# RUN:  all PT_LOAD; \
+# RUN:  dyn PT_DYNAMIC; \
+# RUN: } \
+# RUN: SECTIONS { \
+# RUN:  .rela.plt : { *(.rela.plt) }: all \
+# RUN:  .dynamic : { *(.dynamic) }: all : dyn \
+# RUN: }" > %t.script
+
+# RUN: ld.lld -shared %t.o -T %t.script -o %t
+# RUN: llvm-readobj --dynamic-table %t | FileCheck %s
+
+## In spite of .rela.plt is empty, it might have been preserved because it is
+## mentioned in the linker script. However, even in that case we should not
+## produce DT_JMPREL and DT_PLTGOT tags because this can cause a dynamic loader
+## to write into memory it considers reserved. In fact, as .got.plt is also
+## empty, that memory might be allocated for something else.
+
+# CHECK: DynamicSection [
+# CHECK-NOT: JMPREL
+# CHECK-NOT: PLTGOT
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1339,7 +1339,7 @@
   // as RelaIplt have. And we still want to emit proper dynamic tags for that
   // case, so here we always use RelaPlt as marker for the begining of
   // .rel[a].plt section.
-  if (IsMain && In.RelaPlt->getParent()->isLive()) {
+  if (IsMain && (In.RelaPlt->isNeeded() || In.RelaIplt->isNeeded())) {
     addInSec(DT_JMPREL, In.RelaPlt);
     Entries.push_back({DT_PLTRELSZ, addPltRelSz});
     switch (Config->EMachine) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63869.206817.patch
Type: text/x-patch
Size: 1648 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190627/8d58fc94/attachment.bin>


More information about the llvm-commits mailing list