Skip to content

Commit e99dc4b

Browse files
committed
Align output segments correctly
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 Differential Revision: https://reviews.llvm.org/D67152 llvm-svn: 371013
1 parent 281f2e2 commit e99dc4b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lld/ELF/Writer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,8 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
22722272
// The first section in a PT_LOAD has to have congruent offset and address
22732273
// module the page size.
22742274
if (os->ptLoad && os->ptLoad->firstSec == os) {
2275-
uint64_t alignment = std::max<uint64_t>(os->alignment, config->maxPageSize);
2275+
uint64_t alignment =
2276+
std::max<uint64_t>(os->ptLoad->p_align, config->maxPageSize);
22762277
return alignTo(off, alignment, os->addr);
22772278
}
22782279

lld/test/ELF/nmagic.s

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# REQUIRES: x86
2+
# Verify that .rodata is aligned to a 8 byte boundary.
3+
4+
# RUN: llvm-mc -filetype=obj -triple=i386 %s -o %t.o
5+
# RUN: ld.lld %t.o -o %t.exe -n -Ttext 0
6+
# RUN: llvm-readelf --section-headers %t.exe | FileCheck %s
7+
8+
# CHECK: [ 0] NULL 00000000 000000 000000 00 0 0 0
9+
# CHECK: [ 1] .text PROGBITS 00000000 0000d4 000001 00 AX 0 0 4
10+
# CHECK: [ 2] .rodata PROGBITS 00000008 0000d8 000008 00 A 0 0 8
11+
# CHECK: [ 3] .comment PROGBITS 00000000 0000e0 000008 01 MS 0 0 1
12+
# CHECK: [ 4] .symtab SYMTAB 00000000 0000e8 000020 10 6 1 4
13+
# CHECK: [ 5] .shstrtab STRTAB 00000000 000108 000032 00 0 0 1
14+
# CHECK: [ 6] .strtab STRTAB 00000000 00013a 000008 00 0 0 1
15+
16+
.globl _start
17+
.text
18+
_start:
19+
ret
20+
21+
.rodata
22+
.align 8
23+
.quad 42

0 commit comments

Comments
 (0)