Skip to content

Commit

Permalink
[ELF] -z separate-*: Use max-page-size instead of common-page-size fo…
Browse files Browse the repository at this point in the history
…r text/non-SHF_ALLOC transition and writeTrapInstr

For -z separate-code and -z separate-loadable-segments:

When RW is present, the RX to RW transition is aligned with max-page-size.
When RW is absent, the RX to non-SHF_ALLOC transition should use max-page-size as well.
  • Loading branch information
MaskRay committed Nov 28, 2021
1 parent 6c1c231 commit f9a4d9a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lld/ELF/Writer.cpp
Expand Up @@ -2608,7 +2608,7 @@ template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
// following section to avoid loading non-segments parts of the file.
if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
lastRX->lastSec == sec)
off = alignTo(off, config->commonPageSize);
off = alignTo(off, config->maxPageSize);
}
for (OutputSection *sec : outputSections)
if (!(sec->flags & SHF_ALLOC))
Expand Down Expand Up @@ -2886,9 +2886,9 @@ template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
for (PhdrEntry *p : part.phdrs)
if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
fillTrap(Out::bufferStart + alignDown(p->firstSec->offset + p->p_filesz,
config->commonPageSize),
config->maxPageSize),
Out::bufferStart + alignTo(p->firstSec->offset + p->p_filesz,
config->commonPageSize));
config->maxPageSize));

// Round up the file size of the last segment to the page boundary iff it is
// an executable segment to ensure that other tools don't accidentally
Expand All @@ -2900,7 +2900,7 @@ template <class ELFT> void Writer<ELFT>::writeTrapInstr() {

if (last && (last->p_flags & PF_X))
last->p_memsz = last->p_filesz =
alignTo(last->p_filesz, config->commonPageSize);
alignTo(last->p_filesz, config->maxPageSize);
}
}

Expand Down
34 changes: 17 additions & 17 deletions lld/test/ELF/fill-trap-ppc.s
Expand Up @@ -2,29 +2,29 @@

# RUN: llvm-mc -filetype=obj -triple=powerpc64le-linux %s -o %t.o
# RUN: ld.lld %t.o -z separate-code -o %t.ppc64le
# RUN: llvm-readobj -l %t.ppc64le | FileCheck %s
# RUN: od -Ax -t x1 -N16 -j0x10ff0 %t.ppc64le | FileCheck %s -check-prefix=LE
# RUN: llvm-readelf -Sl %t.ppc64le | FileCheck %s
# RUN: od -Ax -t x1 -N16 -j0x1fff0 %t.ppc64le | FileCheck %s -check-prefix=LE

# RUN: llvm-mc -filetype=obj -triple=powerpc64-linux %s -o %t.o
# RUN: ld.lld %t.o -z separate-code -o %t.ppc64
# RUN: llvm-readobj -l %t.ppc64 | FileCheck %s
# RUN: od -Ax -t x1 -N16 -j0x10ff0 %t.ppc64 | FileCheck %s -check-prefix=BE
# RUN: llvm-readelf -Sl %t.ppc64 | FileCheck %s
# RUN: od -Ax -t x1 -N16 -j0x1fff0 %t.ppc64 | FileCheck %s -check-prefix=BE

# CHECK: ProgramHeader {
# CHECK: Type: PT_LOAD
# CHECK: Offset: 0x10000{{$}}
# CHECK-NEXT: VirtualAddress:
# CHECK-NEXT: PhysicalAddress:
# CHECK-NEXT: FileSize: 4096
# CHECK-NEXT: MemSize:
# CHECK-NEXT: Flags [
# CHECK-NEXT: PF_R
# CHECK-NEXT: PF_X
# CHECK-NEXT: ]
# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
# CHECK-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
# CHECK-NEXT: [ 1] .text PROGBITS 0000000010010000 010000 000004 00 AX 0 0 4
## TODO Remove empty .branch_lt
# CHECK-NEXT: [ 2] .branch_lt PROGBITS 0000000010020000 020000 000000 00 WA 0 0 8
# CHECK-NEXT: [ 3] .comment PROGBITS 0000000000000000 020000 000008 01 MS 0 0 1

# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
# CHECK-NEXT: PHDR 0x000040 0x0000000010000040 0x0000000010000040 0x000118 0x000118 R 0x8
# CHECK-NEXT: LOAD 0x000000 0x0000000010000000 0x0000000010000000 0x000158 0x000158 R 0x10000
# CHECK-NEXT: LOAD 0x010000 0x0000000010010000 0x0000000010010000 0x010000 0x010000 R E 0x10000

## Check that executable page is filled with traps at its end.
# LE: 010ff0 08 00 e0 7f 08 00 e0 7f 08 00 e0 7f 08 00 e0 7f
# BE: 010ff0 7f e0 00 08 7f e0 00 08 7f e0 00 08 7f e0 00 08
# LE: 01fff0 08 00 e0 7f 08 00 e0 7f 08 00 e0 7f 08 00 e0 7f
# BE: 01fff0 7f e0 00 08 7f e0 00 08 7f e0 00 08 7f e0 00 08

.globl _start
_start:
Expand Down
10 changes: 6 additions & 4 deletions lld/test/ELF/fill-trap.s
Expand Up @@ -4,20 +4,22 @@
## -z noseparate-code is the default: text segment is not tail padded.
# RUN: ld.lld %t.o -o %t
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD
# RUN: ld.lld %t.o -z noseparate-code -o %t
# RUN: ld.lld %t.o -z noseparate-code -z common-page-size=512 -o %t
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD

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

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

# RUN: ld.lld %t.o -z separate-code -z noseparate-code -o %t
# RUN: ld.lld %t.o -z separate-code -z noseparate-code -z common-page-size=512 -o %t
# RUN: llvm-readobj -l %t | FileCheck %s --check-prefixes=CHECK,NOPAD

# CHECK: ProgramHeader {
Expand Down

0 comments on commit f9a4d9a

Please sign in to comment.