[lld] r267684 - [ELF] - Align sections file offsets correctly.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 27 02:16:29 PDT 2016


Author: grimar
Date: Wed Apr 27 04:16:28 2016
New Revision: 267684

URL: http://llvm.org/viewvc/llvm-project?rev=267684&view=rev
Log:
[ELF] - Align sections file offsets correctly.

System V ABI 4.1 specifies that program header's p_vaddr should equal p_offset, modulo p_align. 
(https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html).
It was possible to violate this using the linkerscript.
Patch fixes the issue.

Differential revision: http://reviews.llvm.org/D19416

Added:
    lld/trunk/test/ELF/phdr-align.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=267684&r1=267683&r2=267684&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Apr 27 04:16:28 2016
@@ -1662,6 +1662,24 @@ template <class ELFT> void Writer<ELFT>:
   }
 }
 
+// Adjusts the file alignment for a given output section and returns
+// its new file offset. The file offset must be the same with its
+// virtual address (modulo the page size) so that the loader can load
+// executables without any address adjustment.
+template <class ELFT, class uintX_t>
+static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) {
+  uintX_t Align = Sec->getAlign();
+  if (Sec->PageAlign)
+    Align = std::max<uintX_t>(Align, Target->PageSize);
+  Off = alignTo(Off, Align);
+
+  // Relocatable output does not have program headers
+  // and does not need any other offset adjusting.
+  if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC))
+    return Off;
+  return alignTo(Off, Target->PageSize, Sec->getVA());
+}
+
 // Assign file offsets to output sections.
 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
   uintX_t Off =
@@ -1672,10 +1690,8 @@ template <class ELFT> void Writer<ELFT>:
       Sec->setFileOffset(Off);
       continue;
     }
-    uintX_t Align = Sec->getAlign();
-    if (Sec->PageAlign)
-      Align = std::max<uintX_t>(Align, Target->PageSize);
-    Off = alignTo(Off, Align);
+
+    Off = getFileAlignment<ELFT>(Off, Sec);
     Sec->setFileOffset(Off);
     Off += Sec->getSize();
   }

Added: lld/trunk/test/ELF/phdr-align.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/phdr-align.s?rev=267684&view=auto
==============================================================================
--- lld/trunk/test/ELF/phdr-align.s (added)
+++ lld/trunk/test/ELF/phdr-align.s Wed Apr 27 04:16:28 2016
@@ -0,0 +1,82 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+
+# RUN: echo "SECTIONS { \
+# RUN:  .bss : { *(.bss) } \
+# RUN:  .data : { *(.data) } \
+# RUN:  .text : { *(.text) } }" > %t.script
+# RUN: ld.lld %t.o --script %t.script -o %t
+# RUN: llvm-readobj -sections -symbols %t | FileCheck %s
+
+# CHECK:      Sections [
+# CHECK-NEXT:  Section {
+# CHECK-NEXT:    Index: 0
+# CHECK-NEXT:    Name:  (0)
+# CHECK-NEXT:    Type: SHT_NULL
+# CHECK-NEXT:    Flags [
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: 0x0
+# CHECK-NEXT:    Offset: 0x0
+# CHECK-NEXT:    Size: 0
+# CHECK-NEXT:    Link: 0
+# CHECK-NEXT:    Info: 0
+# CHECK-NEXT:    AddressAlignment: 0
+# CHECK-NEXT:    EntrySize: 0
+# CHECK-NEXT:  }
+# CHECK-NEXT:  Section {
+# CHECK-NEXT:    Index: 1
+# CHECK-NEXT:    Name: .bss
+# CHECK-NEXT:    Type: SHT_NOBITS
+# CHECK-NEXT:    Flags [
+# CHECK-NEXT:      SHF_ALLOC
+# CHECK-NEXT:      SHF_WRITE
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: 0x158
+# CHECK-NEXT:    Offset: 0x158
+# CHECK-NEXT:    Size: 6
+# CHECK-NEXT:    Link: 0
+# CHECK-NEXT:    Info: 0
+# CHECK-NEXT:    AddressAlignment: 1
+# CHECK-NEXT:    EntrySize: 0
+# CHECK-NEXT:  }
+# CHECK-NEXT:  Section {
+# CHECK-NEXT:    Index: 2
+# CHECK-NEXT:    Name: .data
+# CHECK-NEXT:    Type: SHT_PROGBITS
+# CHECK-NEXT:    Flags [
+# CHECK-NEXT:      SHF_ALLOC
+# CHECK-NEXT:      SHF_WRITE
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: 0x15E
+# CHECK-NEXT:    Offset: 0x15E
+# CHECK-NEXT:    Size: 2
+# CHECK-NEXT:    Link: 0
+# CHECK-NEXT:    Info: 0
+# CHECK-NEXT:    AddressAlignment: 1
+# CHECK-NEXT:    EntrySize: 0
+# CHECK-NEXT:  }
+# CHECK-NEXT:  Section {
+# CHECK-NEXT:    Index: 3
+# CHECK-NEXT:    Name: .text
+# CHECK-NEXT:    Type: SHT_PROGBITS
+# CHECK-NEXT:    Flags [
+# CHECK-NEXT:      SHF_ALLOC
+# CHECK-NEXT:      SHF_EXECINSTR
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: 0x160
+# CHECK-NEXT:    Offset: 0x160
+# CHECK-NEXT:    Size: 1
+# CHECK-NEXT:    Link: 0
+# CHECK-NEXT:    Info: 0
+# CHECK-NEXT:    AddressAlignment: 4
+# CHECK-NEXT:    EntrySize: 0
+# CHECK-NEXT:  }
+
+.global _start
+.text
+_start:
+ nop
+.data
+ .word 1
+.bss
+ .space 6




More information about the llvm-commits mailing list