[PATCH] D31751: [LLD][ELF] Tidy up handleARMTlsRelocation [NFC]

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 7 03:02:17 PDT 2017


peter.smith updated this revision to Diff 94500.
peter.smith added a comment.

Yes, we can use Config->Shared instead, thanks for pointing that out. I've updated to use Config->Shared and have put back the use of TlsOffsetRel in the Got, it could be possible to add another field to make it use a more regular relocation code, but I'm not sure that is a net win in readability. I'm happy to make the change if it is important to others though.


https://reviews.llvm.org/D31751

Files:
  ELF/Relocations.cpp
  ELF/Target.cpp


Index: ELF/Target.cpp
===================================================================
--- ELF/Target.cpp
+++ ELF/Target.cpp
@@ -1839,6 +1839,7 @@
   case R_ARM_TLS_LDO32:
   case R_ARM_TLS_LE32:
   case R_ARM_TLS_TPOFF32:
+  case R_ARM_TLS_DTPOFF32:
     write32le(Loc, Val);
     break;
   case R_ARM_TLS_DTPMOD32:
Index: ELF/Relocations.cpp
===================================================================
--- ELF/Relocations.cpp
+++ ELF/Relocations.cpp
@@ -148,25 +148,27 @@
                                        SymbolBody &Body, InputSectionBase &C,
                                        uint64_t Offset, int64_t Addend,
                                        RelExpr Expr) {
-  auto addModuleReloc = [&](uint64_t Off, bool LD) {
-    // The Dynamic TLS Module Index Relocation can be statically resolved to 1
-    // if we know that the TLS Symbol is in an executable.
-    if (!Body.isPreemptible() && !Config->Pic)
-      Got->Relocations.push_back(
-          {R_ABS, Target->TlsModuleIndexRel, Off, 0, &Body});
-    else {
-      SymbolBody *Dest = LD ? nullptr : &Body;
-      In<ELFT>::RelaDyn->addReloc(
-          {Target->TlsModuleIndexRel, Got, Off, false, Dest, 0});
-    }
+  // The Dynamic TLS Module Index Relocation for a symbol defined in an
+  // executable is always 1. If the target Symbol is not preemtible then
+  // we know the offset into the TLS block at static link time.
+  bool NeedDynId = Body.isPreemptible() || Config->Shared;
+  bool NeedDynOff = Body.isPreemptible();
+
+  auto AddTlsReloc = [&](uint64_t Off, uint32_t Type, SymbolBody *Dest,
+                         bool Dyn) {
+    if (Dyn)
+      In<ELFT>::RelaDyn->addReloc({Type, Got, Off, false, Dest, 0});
+    else
+      Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
   };
 
   // Local Dynamic is for access to module local TLS variables, while still
   // being suitable for being dynamically loaded via dlopen.
   // GOT[e0] is the module index, with a special value of 0 for the current
   // module. GOT[e1] is unused. There only needs to be one module index entry.
   if (Expr == R_TLSLD_PC && Got->addTlsIndex()) {
-    addModuleReloc(Got->getTlsIndexOff(), true);
+    AddTlsReloc(Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
+                NeedDynId ? nullptr : &Body, NeedDynId);
     C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
     return 1;
   }
@@ -177,13 +179,9 @@
   if (Expr == R_TLSGD_PC) {
     if (Got->addDynTlsEntry(Body)) {
       uint64_t Off = Got->getGlobalDynOffset(Body);
-      addModuleReloc(Off, false);
-      if (Body.isPreemptible())
-        In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Got,
-                                     Off + Config->Wordsize, false, &Body, 0});
-      else
-        Got->Relocations.push_back(
-            {R_ABS, R_ARM_ABS32, Off + Config->Wordsize, 0, &Body});
+      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Body, NeedDynId);
+      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Body,
+                  NeedDynOff);
     }
     C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
     return 1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31751.94500.patch
Type: text/x-patch
Size: 3143 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170407/385eb5e7/attachment.bin>


More information about the llvm-commits mailing list