[PATCH] D60785: [ELF] Align file offset for .bss if first section in a PT_LOAD
James Clarke via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 16 10:46:50 PDT 2019
jrtc27 created this revision.
jrtc27 added reviewers: ruiu, espindola.
Herald added subscribers: llvm-commits, MaskRay, arichardson, emaste.
Herald added a project: LLVM.
If a linker script requests additional alignment for .bss, and it is the
first section in a PT_LOAD, we must ensure to apply this alignment to
the file offset as well, as with other sections, to ensure the file
offset and virtual address are congruent modulo the page size.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D60785
Files:
lld/ELF/Writer.cpp
lld/test/ELF/linkerscript/Inputs/bss-first-align.s
lld/test/ELF/linkerscript/bss-first-align.test
Index: lld/test/ELF/linkerscript/bss-first-align.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/bss-first-align.test
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/bss-first-align.s -o %t.o
+# RUN: ld.lld -o %t.exe --script %s %t.o
+# RUN: llvm-readelf -program-headers %t.exe | FileCheck %s
+
+SECTIONS {
+ .text : {
+ *(.text)
+ }
+ .bss ALIGN(0x20): {
+ *(.bss)
+ }
+}
+
+# CHECK: Program Headers:
+# CHECK-NEXT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
+# CHECK-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000001 0x000001 R E 0x1000
+# CHECK-NEXT: LOAD 0x001020 0x0000000000000020 0x0000000000000020 0x000000 0x000002 RW 0x1000
+# CHECK-NOT: LOAD
Index: lld/test/ELF/linkerscript/Inputs/bss-first-align.s
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/Inputs/bss-first-align.s
@@ -0,0 +1,7 @@
+.text
+.globl _start
+_start:
+ nop
+
+.bss
+.short 0
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -2216,14 +2216,16 @@
// same with its virtual address modulo the page size, so that the loader can
// load executables without any address adjustment.
static uint64_t computeFileOffset(OutputSection *OS, uint64_t Off) {
- // File offsets are not significant for .bss sections. By convention, we keep
- // section offsets monotonically increasing rather than setting to zero.
- if (OS->Type == SHT_NOBITS)
- return Off;
-
// If the section is not in a PT_LOAD, we just have to align it.
- if (!OS->PtLoad)
+ if (!OS->PtLoad) {
+ // File offsets are not significant for .bss sections when not the first
+ // section in a PT_LOAD. By convention, we keep section offsets
+ // monotonically increasing rather than setting to zero.
+ if (OS->Type == SHT_NOBITS)
+ return Off;
+
return alignTo(Off, OS->Alignment);
+ }
// The first section in a PT_LOAD has to have congruent offset and address
// module the page size.
@@ -2233,6 +2235,10 @@
return alignTo(Off, Alignment, OS->Addr);
}
+ // This is a .bss section that isn't first in its PT_LOAD; see above.
+ if (OS->Type == SHT_NOBITS)
+ return Off;
+
// If two sections share the same PT_LOAD the file offset is calculated
// using this formula: Off2 = Off1 + (VA2 - VA1).
return First->Offset + OS->Addr - First->Addr;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60785.195414.patch
Type: text/x-patch
Size: 2590 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190416/bcab8e0a/attachment.bin>
More information about the llvm-commits
mailing list