From f9a4d9aa0309ca353de909a19b648ab34f6c0c68 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 28 Nov 2021 12:47:49 -0800 Subject: [PATCH] [ELF] -z separate-*: Use max-page-size instead of common-page-size for 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. --- lld/ELF/Writer.cpp | 8 ++++---- lld/test/ELF/fill-trap-ppc.s | 34 +++++++++++++++++----------------- lld/test/ELF/fill-trap.s | 10 ++++++---- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 1906b2deccf71..dce928319b09c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2608,7 +2608,7 @@ template void Writer::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)) @@ -2886,9 +2886,9 @@ template void Writer::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 @@ -2900,7 +2900,7 @@ template void Writer::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); } } diff --git a/lld/test/ELF/fill-trap-ppc.s b/lld/test/ELF/fill-trap-ppc.s index b7e5fa86ba7ce..42ba6f53d5ee5 100644 --- a/lld/test/ELF/fill-trap-ppc.s +++ b/lld/test/ELF/fill-trap-ppc.s @@ -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: diff --git a/lld/test/ELF/fill-trap.s b/lld/test/ELF/fill-trap.s index bd26c3f8f8d7d..60276a1cc5af6 100644 --- a/lld/test/ELF/fill-trap.s +++ b/lld/test/ELF/fill-trap.s @@ -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 {