[PATCH] [lld][ELF] TBSS section has to be seperately considered so that it doesnot occupy memory space

Shankar Kalpathi Easwaran shankarke at gmail.com
Mon Jun 10 20:37:33 PDT 2013


Hi Bigcheese,

This adds functionality to the ELF writer to not add memory size to the segment and checks to make sure that DATA starts at the same address where TBSS starts with a test.

http://llvm-reviews.chandlerc.com/D954

Files:
  lib/ReaderWriter/ELF/DefaultLayout.h
  lib/ReaderWriter/ELF/SegmentChunks.h
  test/elf/Inputs/tlsAddr.x86-64
  test/elf/Inputs/tlsaddr.c
  test/elf/dynamic-segorder.test
  test/elf/tlsAddr.test

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<ELFT> *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 <typename ELFT> class DefaultLayout;
+
 /// \brief A segment can be divided into segment slices
 ///        depending on how the segments can be split
 template<class ELFT>
@@ -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<ELFT>::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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D954.1.patch
Type: text/x-patch
Size: 3060 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130610/7300e80e/attachment.bin>


More information about the llvm-commits mailing list