[PATCH] D43347: [ELF][MIPS] Support linking of PIE for MIPS

Simon Atanasyan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 15 11:38:20 PST 2018


atanasyan created this revision.
atanasyan added reviewers: ruiu, rafael.
atanasyan added a project: lld.
Herald added subscribers: arichardson, sdardis, emaste.

MIPS ABI requires creation of the MIPS_RLD_MAP dynamic tag for non-PIE executables only and MIPS_RLD_MAP_REL tag for both PIE and non-PIE executables. The patch skips definition of the MIPS_RLD_MAP for PIE files and defines MIPS_RLD_MAP_REL.

The MIPS_RLD_MAP_REL tag stores the offset to the .rld_map section relative to the address of the tag itself.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D43347

Files:
  ELF/SyntheticSections.cpp
  ELF/SyntheticSections.h
  test/ELF/mips-dynamic.s


Index: test/ELF/mips-dynamic.s
===================================================================
--- test/ELF/mips-dynamic.s
+++ test/ELF/mips-dynamic.s
@@ -6,7 +6,11 @@
 
 # RUN: ld.lld %t.o %td.so -o %t.exe
 # RUN: llvm-readobj -sections -dynamic-table %t.exe \
-# RUN:   | FileCheck -check-prefix=EXE %s
+# RUN:   | FileCheck -check-prefixes=EXE,NOPIE %s
+
+# RUN: ld.lld -pie %t.o %td.so -o %t.so
+# RUN: llvm-readobj -sections -dyn-symbols -dynamic-table %t.so \
+# RUN:   | FileCheck -check-prefixes=EXE,PIE %s
 
 # RUN: ld.lld %t.o --image-base=0x123000 %td.so -o %t.exe
 # RUN: llvm-readobj -sections -dynamic-table %t.exe \
@@ -49,11 +53,13 @@
 # EXE-DAG:    0x00000003 PLTGOT               [[GOTADDR]]
 # EXE-DAG:    0x70000001 MIPS_RLD_VERSION     1
 # EXE-DAG:    0x70000005 MIPS_FLAGS           NOTPOT
-# EXE-DAG:    0x70000006 MIPS_BASE_ADDRESS    0x10000
+# NOPIE-DAG:  0x70000006 MIPS_BASE_ADDRESS    0x10000
+# PIE-DAG:    0x70000006 MIPS_BASE_ADDRESS    0x0
 # EXE-DAG:    0x7000000A MIPS_LOCAL_GOTNO     2
 # EXE-DAG:    0x70000011 MIPS_SYMTABNO        2
 # EXE-DAG:    0x70000013 MIPS_GOTSYM          0x2
-# EXE-DAG:    0x70000016 MIPS_RLD_MAP         [[RLDMAPADDR]]
+# NOPIE-DAG:  0x70000016 MIPS_RLD_MAP         [[RLDMAPADDR]]
+# EXE-DAG:    0x70000035 MIPS_RLD_MAP_REL     0x{{[0-9A-F]+}}
 # EXE:      ]
 
 # IMAGE_BASE: 0x70000006 MIPS_BASE_ADDRESS    0x123000
Index: ELF/SyntheticSections.h
===================================================================
--- ELF/SyntheticSections.h
+++ ELF/SyntheticSections.h
@@ -349,7 +349,7 @@
 private:
   void add(int32_t Tag, std::function<uint64_t()> Fn);
   void addInt(int32_t Tag, uint64_t Val);
-  void addInSec(int32_t Tag, InputSection *Sec);
+  void addInSec(int32_t Tag, InputSection *Sec, bool IsRelative = false);
   void addOutSec(int32_t Tag, OutputSection *Sec);
   void addSize(int32_t Tag, OutputSection *Sec);
   void addSym(int32_t Tag, Symbol *Sym);
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1004,9 +1004,13 @@
 }
 
 template <class ELFT>
-void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
-  Entries.push_back(
-      {Tag, [=] { return Sec->getParent()->Addr + Sec->OutSecOff; }});
+void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec,
+                                    bool IsRelative) {
+  size_t TagOffset = Entries.size() * Entsize;
+  Entries.push_back({Tag, [=] {
+                       return Sec->getParent()->Addr + Sec->OutSecOff -
+                              (IsRelative ? getVA() + TagOffset : 0);
+                     }});
 }
 
 template <class ELFT>
@@ -1160,8 +1164,13 @@
     else
       addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
     addInSec(DT_PLTGOT, InX::MipsGot);
-    if (InX::MipsRldMap)
-      addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
+    if (InX::MipsRldMap) {
+      if (!Config->Pie)
+        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
+      // Store the offset to the .rld_map section
+      // relative to the address of the tag.
+      addInSec(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap, true /*relative offset*/);
+    }
   }
 
   addInt(DT_NULL, 0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43347.134476.patch
Type: text/x-patch
Size: 3257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180215/2a039903/attachment.bin>


More information about the llvm-commits mailing list