[PATCH] D67152: Align output segments correctly

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 04:10:49 PDT 2019


ruiu created this revision.
ruiu added reviewers: MaskRay, peter.smith, grimar.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

Previously, segments were aligned according to their first section's
alignment requirements. That was not correct, but segments are also
aligned to a page boundary, and a page boundary is usually much larger
than a section alignment requirement, so no one noticed this bug before.

Now, lld has --nmagic option which sets maxPageSize to 1 to effectively
disable page alignment, which reveals the issue.

Fixes https://bugs.llvm.org/show_bug.cgi?id=43212


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67152

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/nmagic.s


Index: lld/test/ELF/nmagic.s
===================================================================
--- /dev/null
+++ lld/test/ELF/nmagic.s
@@ -0,0 +1,23 @@
+# REQUIRES: x86
+# Verify that .rodata is aligned to a 8 byte boundary.
+
+# RUN: llvm-mc -filetype=obj -triple=i386-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t.exe -N -Ttext 0
+# RUN: llvm-readelf -section-headers %t.exe | FileCheck %s
+
+# CHECK: [ 0]           NULL     00000000 000000 000000 00      0   0  0
+# CHECK: [ 1] .text     PROGBITS 00000000 000098 000001 00  AX  0   0  4
+# CHECK: [ 2] .rodata   PROGBITS 00000008 0000a0 000008 00   A  0   0  8
+# CHECK: [ 3] .comment  PROGBITS 00000000 0000a8 000008 01  MS  0   0  1
+# CHECK: [ 4] .symtab   SYMTAB   00000000 0000b0 000020 10      6   1  4
+# CHECK: [ 5] .shstrtab STRTAB   00000000 0000d0 000032 00      0   0  1
+# CHECK: [ 6] .strtab   STRTAB   00000000 000102 000008 00      0   0  1
+
+.globl _start
+.text
+_start:
+  ret
+
+.rodata
+.align 8
+.quad 42
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -2272,7 +2272,8 @@
   // The first section in a PT_LOAD has to have congruent offset and address
   // module the page size.
   if (os->ptLoad && os->ptLoad->firstSec == os) {
-    uint64_t alignment = std::max<uint64_t>(os->alignment, config->maxPageSize);
+    uint64_t alignment =
+        std::max<uint64_t>(os->ptLoad->p_align, config->maxPageSize);
     return alignTo(off, alignment, os->addr);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67152.218640.patch
Type: text/x-patch
Size: 1542 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190904/9b2a0f39/attachment.bin>


More information about the llvm-commits mailing list