Index: lib/ReaderWriter/ELF/DefaultLayout.h =================================================================== --- lib/ReaderWriter/ELF/DefaultLayout.h +++ lib/ReaderWriter/ELF/DefaultLayout.h @@ -581,8 +581,13 @@ int64_t lookupSectionFlag = msi->flags(); if (!(lookupSectionFlag & llvm::ELF::SHF_WRITE)) lookupSectionFlag &= ~llvm::ELF::SHF_EXECINSTR; + + // Merge string sections into Data segment itself lookupSectionFlag &= ~(llvm::ELF::SHF_STRINGS | llvm::ELF::SHF_MERGE); + // Merge the TLS section into the DATA segment itself + lookupSectionFlag &= ~(llvm::ELF::SHF_TLS); + Segment *segment; // We need a seperate segment for sections that dont have // the segment type to be PT_LOAD Index: lib/ReaderWriter/ELF/SegmentChunks.h =================================================================== --- lib/ReaderWriter/ELF/SegmentChunks.h +++ lib/ReaderWriter/ELF/SegmentChunks.h @@ -30,6 +30,9 @@ namespace lld { namespace elf { + +template class DefaultLayout; + /// \brief A segment can be divided into segment slices /// depending on how the segments can be split template @@ -496,8 +499,13 @@ } if (isTLSSegment) tlsStartAddr += section->memSize(); - addr += section->memSize(); - section->setMemSize(addr - section->virtualAddr()); + section->setMemSize(addr + section->memSize() - section->virtualAddr()); + // TBSS section is special that it doesnot contribute to memory of any + // segment, If we see a tbss section, dont add memory size to addr + // The fileOffset is automatically taken care of since TBSS section does + // not endup using file size + if (section->order() != DefaultLayout::ORDER_TBSS) + addr += section->memSize(); } slice->setMemSize(addr - slice->virtualAddr()); } Index: test/elf/Inputs/tlsaddr.c =================================================================== --- /dev/null +++ test/elf/Inputs/tlsaddr.c @@ -0,0 +1,8 @@ +__thread int tls0 = 0; +__thread int tls1 = 0; +__thread int tls2 = 1; +__thread int tls3 = 2; + +int main() { + return tls0 + tls1 + tls2; +} Index: test/elf/dynamic-segorder.test =================================================================== --- test/elf/dynamic-segorder.test +++ test/elf/dynamic-segorder.test @@ -10,8 +10,6 @@ CHECK: flags r-x CHECK: LOAD CHECK: flags rw- -CHECK: LOAD -CHECK: flags rw- CHECK: DYNAMIC CHECK: flags r-- CHECK: TLS Index: test/elf/tlsAddr.test =================================================================== --- /dev/null +++ test/elf/tlsAddr.test @@ -0,0 +1,7 @@ +# This tests verifies that TLS variables have correct offsets +# and that TBSS doesnot occupy memory +RUN: lld -flavor gnu -target x86_64-linux %p/Inputs/tlsAddr.x86-64 -static \ +RUN: -o %t --noinhibit-exec +RUN: llvm-objdump -section-headers %t | FileCheck -check-prefix=CHECKADDR %s + +CHECKADDR: 8 .data 00000000 0000000000401008 DATA