diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 321599b0055ae3..0069822d12178b 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 8a774aec0d64f7..9aab32317be4cc 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 9dbff0d52b4b5c..9bfbedab6c78c2 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