[lld] cfaa5bf - [ELF] Align the first section of a PT_TLS even if its type is SHT_NOBITS

Jessica Clarke via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 29 07:14:15 PDT 2021


Author: Jessica Clarke
Date: 2021-07-29T15:14:00+01:00
New Revision: cfaa5bf4ce62266897d27c8c0f3474e555ab87e5

URL: https://github.com/llvm/llvm-project/commit/cfaa5bf4ce62266897d27c8c0f3474e555ab87e5
DIFF: https://github.com/llvm/llvm-project/commit/cfaa5bf4ce62266897d27c8c0f3474e555ab87e5.diff

LOG: [ELF] Align the first section of a PT_TLS even if its type is SHT_NOBITS

This is somewhat of a repeat of D66658 but for sections in PT_TLS
segments. Although such sections don't need to be aligned such that
address and offset are congruent modulo the page size, they do need
to be congruent modulo the segment alignment, otherwise the
whole PT_TLS will be unaligned. We therefore use the normal calculation
to determine the section's address within the PT_LOAD rather than
bailing out early due to being SHT_NOBITS.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D106987

Added: 
    

Modified: 
    lld/ELF/Writer.cpp
    lld/test/ELF/linkerscript/tls-nobits-offset.s
    lld/test/ELF/tls-nobits-offset.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 321599b0055ae..0069822d12178 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2597,9 +2597,10 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
     return alignTo(off, os->ptLoad->p_align, os->addr);
 
   // File offsets are not significant for .bss sections other than the first one
-  // in a PT_LOAD. By convention, we keep section offsets monotonically
+  // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
   // increasing rather than setting to zero.
-   if (os->type == SHT_NOBITS)
+  if (os->type == SHT_NOBITS &&
+      (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
      return off;
 
   // If the section is not in a PT_LOAD, we just have to align it.

diff  --git a/lld/test/ELF/linkerscript/tls-nobits-offset.s b/lld/test/ELF/linkerscript/tls-nobits-offset.s
index 8a774aec0d64f..9aab32317be4c 100644
--- a/lld/test/ELF/linkerscript/tls-nobits-offset.s
+++ b/lld/test/ELF/linkerscript/tls-nobits-offset.s
@@ -11,17 +11,16 @@
 ## p_offset will be set to the sh_offset field of the section. Check we align
 ## sh_offset to sh_addr modulo p_align, so that p_vaddr=p_offset (mod
 ## p_align).
-## TODO: Currently we fail to ensure this, as shown by this test
 
 # CHECK:      Name  Type     Address          Off     Size
 # CHECK-NEXT:       NULL     0000000000000000 000000  000000
 # CHECK-NEXT: .text PROGBITS 0000000000000000 000190  000000
 # CHECK-NEXT: .sec1 PROGBITS 0000000000000000 001000  000001
-# CHECK-NEXT: .tbss NOBITS   0000000000000400 001001  000001
+# CHECK-NEXT: .tbss NOBITS   0000000000000400 001400  000001
 
 # CHECK:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
 # CHECK-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000001 0x000001 R   0x1000
-# CHECK-NEXT: TLS  0x001001 0x0000000000000400 0x0000000000000400 0x000000 0x000001 R   0x400
+# CHECK-NEXT: TLS  0x001400 0x0000000000000400 0x0000000000000400 0x000000 0x000001 R   0x400
 
 # CHECK:      00 .sec1 {{$}}
 # CHECK:      01 .tbss {{$}}

diff  --git a/lld/test/ELF/tls-nobits-offset.s b/lld/test/ELF/tls-nobits-offset.s
index 9dbff0d52b4b5..9bfbedab6c78c 100644
--- a/lld/test/ELF/tls-nobits-offset.s
+++ b/lld/test/ELF/tls-nobits-offset.s
@@ -7,13 +7,12 @@
 ## p_offset will be set to the sh_offset field of the section. Check we align
 ## sh_offset to sh_addr modulo p_align, so that p_vaddr=p_offset (mod
 ## p_align).
-## TODO: Currently we fail to ensure this, as shown by this test
 
 # CHECK: Name  Type   Address          Off     Size   ES Flg Lk Inf Al
-# CHECK: .tbss NOBITS 0000000000211000 000158  000001 00 WAT  0   0 4096
+# CHECK: .tbss NOBITS 0000000000211000 001000  000001 00 WAT  0   0 4096
 
 # CHECK: Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
-# CHECK: TLS  0x000158 0x0000000000211000 0x0000000000211000 0x000000 0x000001 R   0x1000
+# CHECK: TLS  0x001000 0x0000000000211000 0x0000000000211000 0x000000 0x000001 R   0x1000
 
 # CHECK: 02 .tbss
 


        


More information about the llvm-commits mailing list