[lld] r221862 - [ELF] Fix segment alignment.

Shankar Easwaran shankare at codeaurora.org
Wed Nov 12 20:33:06 PST 2014


Author: shankare
Date: Wed Nov 12 22:33:06 2014
New Revision: 221862

URL: http://llvm.org/viewvc/llvm-project?rev=221862&view=rev
Log:
[ELF] Fix segment alignment.

The segment alignment for PT_LOAD segments is set to page size by default, but
if any of the sections require an alignment more than the page size, the segment
alignment property is set to the maximum alignment of the sections that are part
of the segment.

Modified:
    lld/trunk/lib/ReaderWriter/ELF/HeaderChunks.h
    lld/trunk/test/elf/phdr.test

Modified: lld/trunk/lib/ReaderWriter/ELF/HeaderChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/HeaderChunks.h?rev=221862&r1=221861&r2=221862&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/HeaderChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/HeaderChunks.h Wed Nov 12 22:33:06 2014
@@ -226,11 +226,18 @@ bool ProgramHeader<ELFT>::addSegment(Seg
     phdr->p_filesz = slice->fileSize();
     phdr->p_memsz = slice->memSize();
     phdr->p_flags = segment->flags();
+    phdr->p_align = slice->align2();
+    uint64_t segPageSize = segment->pageSize();
+    uint64_t sliceAlign = slice->align2();
+    // Alignment of PT_LOAD segments are set to the page size, but if the
+    // alignment of the slice is greater than the page size, set the alignment
+    // of the segment appropriately.
     if (outputMagic != ELFLinkingContext::OutputMagic::NMAGIC &&
-        outputMagic != ELFLinkingContext::OutputMagic::OMAGIC)
-      phdr->p_align = (phdr->p_type == llvm::ELF::PT_LOAD) ? segment->pageSize()
-                                                           : slice->align2();
-    else
+        outputMagic != ELFLinkingContext::OutputMagic::OMAGIC) {
+      phdr->p_align = (phdr->p_type == llvm::ELF::PT_LOAD)
+          ? (segPageSize < sliceAlign) ? sliceAlign : segPageSize
+          : sliceAlign;
+    } else
       phdr->p_align = slice->align2();
   }
   this->_fsize = fileSize();

Modified: lld/trunk/test/elf/phdr.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/phdr.test?rev=221862&r1=221861&r2=221862&view=diff
==============================================================================
--- lld/trunk/test/elf/phdr.test (original)
+++ lld/trunk/test/elf/phdr.test Wed Nov 12 22:33:06 2014
@@ -68,7 +68,7 @@ I386-NEXT:     Flags [ (0x6)
 I386-NEXT:       PF_R (0x4)
 I386-NEXT:       PF_W (0x2)
 I386-NEXT:     ]
-I386-NEXT:     Alignment: 4096
+I386-NEXT:     Alignment: 16384
 I386-NEXT:   }
 I386-NEXT:   ProgramHeader {
 I386-NEXT:     Type: PT_GNU_EH_FRAME (0x6474E550)





More information about the llvm-commits mailing list