[lld] r251998 - [elf2] Implement R_X86_64_TPOFF32.

Michael J. Spencer via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 3 14:39:09 PST 2015


Author: mspencer
Date: Tue Nov  3 16:39:09 2015
New Revision: 251998

URL: http://llvm.org/viewvc/llvm-project?rev=251998&view=rev
Log:
[elf2] Implement R_X86_64_TPOFF32.

This does not support TPOFF32 relocations to local symbols as the address calculations are separate. Support for this will be a separate patch.

Modified:
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Target.cpp
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/elf2/tls.s

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=251998&r1=251997&r2=251998&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Tue Nov  3 16:39:09 2015
@@ -644,6 +644,9 @@ typename ELFFile<ELFT>::uintX_t lld::elf
   case SymbolBody::DefinedRegularKind: {
     const auto &DR = cast<DefinedRegular<ELFT>>(S);
     InputSectionBase<ELFT> &SC = DR.Section;
+    if (DR.Sym.getType() == STT_TLS)
+      return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
+             Out<ELFT>::TlsInitImageVA;
     return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
   }
   case SymbolBody::DefinedCommonKind:

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=251998&r1=251997&r2=251998&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Tue Nov  3 16:39:09 2015
@@ -353,6 +353,7 @@ private:
 // globally accessible. Writer initializes them, so don't use them
 // until Writer is initialized.
 template <class ELFT> struct Out {
+  typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
   static DynamicSection<ELFT> *Dynamic;
   static GnuHashTableSection<ELFT> *GnuHashTab;
   static GotPltSection<ELFT> *GotPlt;
@@ -370,6 +371,7 @@ template <class ELFT> struct Out {
   static StringTableSection<ELFT> *StrTab;
   static SymbolTableSection<ELFT> *DynSymTab;
   static SymbolTableSection<ELFT> *SymTab;
+  static uintX_t TlsInitImageVA;
 };
 
 template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
@@ -389,6 +391,7 @@ template <class ELFT> StringTableSection
 template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
 template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
 template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
+template <class ELFT> typename Out<ELFT>::uintX_t Out<ELFT>::TlsInitImageVA;
 }
 }
 #endif

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=251998&r1=251997&r2=251998&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Tue Nov  3 16:39:09 2015
@@ -339,6 +339,9 @@ void X86_64TargetInfo::relocateOne(uint8
       error("R_X86_64_32S out of range");
     write32le(Loc, SA);
     break;
+  case R_X86_64_TPOFF32:
+    write32le(Loc, SA);
+    break;
   default:
     error("unrecognized reloc " + Twine(Type));
   }

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=251998&r1=251997&r2=251998&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Nov  3 16:39:09 2015
@@ -716,8 +716,10 @@ template <class ELFT> void Writer<ELFT>:
     }
 
     if ((Sec->getFlags() & SHF_ALLOC) && (Sec->getFlags() & SHF_TLS)) {
-      if (!TlsPhdr.p_vaddr)
+      if (!TlsPhdr.p_vaddr) {
         setPhdr(&TlsPhdr, PT_TLS, PF_R, FileOff, VA, 0, Sec->getAlign());
+        Out<ELFT>::TlsInitImageVA = VA;
+      }
       if (Sec->getType() != SHT_NOBITS)
         VA = RoundUpToAlignment(VA, Sec->getAlign());
       uintX_t TVA = RoundUpToAlignment(VA + ThreadBSSOffset, Sec->getAlign());

Modified: lld/trunk/test/elf2/tls.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/tls.s?rev=251998&r1=251997&r2=251998&view=diff
==============================================================================
--- lld/trunk/test/elf2/tls.s (original)
+++ lld/trunk/test/elf2/tls.s Tue Nov  3 16:39:09 2015
@@ -1,21 +1,34 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld2 %t -o %tout
-// RUN: llvm-readobj -sections -program-headers %tout | FileCheck %s
+// RUN: llvm-readobj -symbols -sections -program-headers %tout | FileCheck %s
+// RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DIS
 
 .global _start
 _start:
+  movl %fs:a at tpoff, %eax
+  movl %fs:b at tpoff, %eax
+  movl %fs:c at tpoff, %eax
+  movl %fs:d at tpoff, %eax
 
+  .global a
 	.section	.tbss,"awT", at nobits
+a:
 	.long	0
 
+  .global b
 	.section	.tdata,"awT", at progbits
+b:
 	.long	1
 
+  .global c
 	.section	.thread_bss,"awT", at nobits
+c:
 	.long	0
 
+  .global d
 	.section	.thread_data,"awT", at progbits
+d:
 	.long	2
 
 // CHECK:          Name: .tdata
@@ -77,9 +90,9 @@ _start:
 // CHECK-NEXT:       SHF_WRITE
 // CHECK-NEXT:     ]
 
-// 0x1100C = TBSS_ADDR + 4
+// 0x1200C = TBSS_ADDR + 4
 
-// CHECK-NEXT:     Address: 0x1100C
+// CHECK-NEXT:     Address: 0x1200C
 // CHECK-NEXT:     Offset:
 // CHECK-NEXT:     Size: 4
 // CHECK-NEXT:     Link:
@@ -88,11 +101,49 @@ _start:
 // CHECK-NEXT:     EntrySize:
 // CHECK-NEXT:   }
 
+// CHECK:      Symbols [
+// CHECK:          Name: a
+// CHECK-NEXT:     Value: 0x8
+// CHECK-NEXT:     Size:
+// CHECK-NEXT:     Binding: Global
+// CHECK-NEXT:     Type: TLS
+// CHECK-NEXT:     Other: 0
+// CHECK-NEXT:     Section: .tbss
+// CHECK-NEXT:   }
+// CHECK-NEXT:   Symbol {
+// CHECK-NEXT:     Name: b
+// CHECK-NEXT:     Value: 0x0
+// CHECK-NEXT:     Size:
+// CHECK-NEXT:     Binding: Global
+// CHECK-NEXT:     Type: TLS
+// CHECK-NEXT:     Other: 0
+// CHECK-NEXT:     Section: .tdata
+// CHECK-NEXT:   }
+// CHECK-NEXT:   Symbol {
+// CHECK-NEXT:     Name: c
+// CHECK-NEXT:     Value: 0xC
+// CHECK-NEXT:     Size:
+// CHECK-NEXT:     Binding: Global
+// CHECK-NEXT:     Type: TLS
+// CHECK-NEXT:     Other: 0
+// CHECK-NEXT:     Section: .thread_bss
+// CHECK-NEXT:   }
+// CHECK-NEXT:   Symbol {
+// CHECK-NEXT:     Name: d
+// CHECK-NEXT:     Value: 0x4
+// CHECK-NEXT:     Size:
+// CHECK-NEXT:     Binding: Global
+// CHECK-NEXT:     Type: TLS
+// CHECK-NEXT:     Other: 0
+// CHECK-NEXT:     Section: .thread_data
+// CHECK-NEXT:   }
+
 // Check that the TLS NOBITS sections weren't added to the R/W PT_LOAD's size.
 
 // CHECK:      ProgramHeaders [
 // CHECK:          Type: PT_LOAD
 // CHECK:          Type: PT_LOAD
+// CHECK:          Type: PT_LOAD
 // CHECK:          FileSize: 8
 // CHECK-NEXT:     MemSize: 8
 // CHECK-NEXT:     Flags [
@@ -110,3 +161,10 @@ _start:
 // CHECK-NEXT:     ]
 // CHECK-NEXT:     Alignment:
 // CHECK-NEXT:   }
+
+// DIS:      Disassembly of section .text:
+// DIS-NEXT: _start:
+// DIS-NEXT:    11000: {{.+}} movl    %fs:8, %eax
+// DIS-NEXT:    11008: {{.+}} movl    %fs:0, %eax
+// DIS-NEXT:    11010: {{.+}} movl    %fs:12, %eax
+// DIS-NEXT:    11018: {{.+}} movl    %fs:4, %eax




More information about the llvm-commits mailing list