Skip to content

Commit bdcabc4

Browse files
authored
[ELF] writeTrapInstr: Don't decrease p_memsz
When the last PT_LOAD segment is executable and includes BSS sections, its p_memsz may exceed the aligned p_filesz. This change ensures p_memsz is not reduced in such cases (e.g. --omagic). In addition, disable this behavior when a SECTIONS command is specified. Refined behavior introduced in https://reviews.llvm.org/D37369 (2017). The -z separate-loadable-segments --omagic test adds coverage for the option combination, even if it might be practical. Pull Request: #139207
1 parent 8893d40 commit bdcabc4

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

lld/ELF/Writer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,9 +2960,12 @@ template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
29602960
if (p->p_type == PT_LOAD)
29612961
last = p.get();
29622962

2963-
if (last && (last->p_flags & PF_X))
2964-
last->p_memsz = last->p_filesz =
2965-
alignToPowerOf2(last->p_filesz, ctx.arg.maxPageSize);
2963+
if (last && (last->p_flags & PF_X)) {
2964+
last->p_filesz = alignToPowerOf2(last->p_filesz, ctx.arg.maxPageSize);
2965+
// p_memsz might be larger than the aligned p_filesz due to trailing BSS
2966+
// sections. Don't decrease it.
2967+
last->p_memsz = std::max(last->p_memsz, last->p_filesz);
2968+
}
29662969
}
29672970
}
29682971

lld/test/ELF/fill-trap.s

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
# REQUIRES: x86
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
2+
# RUN: rm -rf %t && split-file %s %t && cd %t
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64 bss.s -o bss.o
35

46
## -z noseparate-code is the default: text segment is not tail padded.
5-
# RUN: ld.lld %t.o -o %t
6-
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD
7-
# RUN: ld.lld %t.o -z noseparate-code -z common-page-size=512 -o %t
8-
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD
7+
# RUN: ld.lld a.o -o out
8+
# RUN: llvm-readobj -l out | FileCheck %s --check-prefixes=CHECK,NOPAD
9+
# RUN: ld.lld a.o -z noseparate-code -z common-page-size=512 -o out
10+
# RUN: llvm-readobj -l out | FileCheck %s --check-prefixes=CHECK,NOPAD
911

1012
## -z separate-code pads the tail of text segment with traps.
1113
## Make common-page-size smaller than max-page-size.
1214
## Check that we use max-page-size instead of common-page-size for padding.
13-
# RUN: ld.lld %t.o -z separate-code -z common-page-size=512 -o %t
14-
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,PAD
15-
# RUN: od -Ax -x -N16 -j0x1ff0 %t | FileCheck %s --check-prefix=FILL
15+
# RUN: ld.lld a.o -z separate-code -z common-page-size=512 -o out
16+
# RUN: llvm-readobj -l out | FileCheck %s --check-prefixes=CHECK,PAD
17+
# RUN: od -Ax -x -N16 -j0x1ff0 out | FileCheck %s --check-prefix=FILL
1618

1719
## -z separate-loadable-segments pads all segments, including the text segment.
18-
# RUN: ld.lld %t.o -z separate-loadable-segments -z common-page-size=512 -o %t
19-
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,PAD
20-
# RUN: od -Ax -x -N16 -j0x1ff0 %t | FileCheck %s --check-prefix=FILL
20+
# RUN: ld.lld a.o -z separate-loadable-segments -z common-page-size=512 -o out
21+
# RUN: llvm-readobj -l out | FileCheck %s --check-prefixes=CHECK,PAD
22+
# RUN: od -Ax -x -N16 -j0x1ff0 out | FileCheck %s --check-prefix=FILL
2123

22-
# RUN: ld.lld %t.o -z separate-code -z noseparate-code -z common-page-size=512 -o %t
23-
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD
24+
# RUN: ld.lld a.o -z separate-code -z noseparate-code -z common-page-size=512 -o out
25+
# RUN: llvm-readobj -l out | FileCheck %s --check-prefixes=CHECK,NOPAD
2426

2527
# CHECK: ProgramHeader {
2628
# CHECK: Type: PT_LOAD
@@ -39,6 +41,31 @@
3941
## Check that executable page is filled with traps at its end.
4042
# FILL: 001ff0 cccc cccc cccc cccc cccc cccc cccc cccc
4143

44+
## There is a single RWX segment. Test that p_memsz is not truncated to p_filesz.
45+
# RUN: ld.lld a.o bss.o -z separate-loadable-segments -T rwx.lds -z max-page-size=64 -o rwx
46+
# RUN: llvm-readelf -l rwx | FileCheck %s --check-prefix=RWX
47+
48+
# RWX: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
49+
# RWX-NEXT: LOAD 0x000080 0x0000000000000000 0x0000000000000000 0x000040 0x000404 RWE 0x40
50+
51+
# RUN: ld.lld a.o bss.o -z separate-loadable-segments --omagic -o omagic
52+
# RUN: llvm-readelf -l omagic | FileCheck %s --check-prefix=OMAGIC
53+
54+
# OMAGIC: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
55+
# OMAGIC-NEXT:LOAD 0x0000b0 0x00000000002000b0 0x00000000002000b0 0x000004 0x000404 RWE 0x4
56+
57+
#--- a.s
4258
.globl _start
4359
_start:
4460
nop
61+
62+
#--- bss.s
63+
.bss
64+
.space 1024
65+
66+
#--- rwx.lds
67+
PHDRS { all PT_LOAD; }
68+
SECTIONS {
69+
.text : {*(.text*)} :all
70+
.bss : {*(.bss*)} :all
71+
}

0 commit comments

Comments
 (0)