diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2021-12-13 19:46:04 -0800 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2022-01-05 05:06:18 -0800 |
commit | 74e315dbfe5200c473b226e937935fb8ce391489 (patch) | |
tree | 0dd599fc7c02c929e81fdce2fb518c4e0aef296c /ld | |
parent | 10371501814d6944dc625477e75f5d7b89288ddf (diff) | |
download | binutils-74e315dbfe5200c473b226e937935fb8ce391489.zip binutils-74e315dbfe5200c473b226e937935fb8ce391489.tar.gz binutils-74e315dbfe5200c473b226e937935fb8ce391489.tar.bz2 |
elf: Set p_align to the minimum page size if possible
Currently, on 32-bit and 64-bit ARM, it seems that ld generates p_align
values of 0x10000 even if no section alignment is greater than 0x1000.
The issue is more general and probably affects other targets with multiple
page sizes.
While file layout absolutely must take 64K page size into account, that
does not have to be reflected in the p_align value. If running on a 64K
kernel, the file will be loaded at a 64K page boundary by necessity. On
a 4K kernel, 64K alignment is not needed.
The glibc loader has been fixed to honor p_align:
https://sourceware.org/bugzilla/show_bug.cgi?id=28676
similar to kernel:
commit ce81bb256a224259ab686742a6284930cbe4f1fa
Author: Chris Kennelly <ckennelly@google.com>
Date: Thu Oct 15 20:12:32 2020 -0700
fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
This means that on 4K kernels, we will start to do extra work for 64K
p_align, but this pointless for pretty much all binaries (whose section
alignment rarely exceeds 16).
The minimum page size is used, instead of the maximum section alignment
due to this glibc bug:
https://sourceware.org/bugzilla/show_bug.cgi?id=28688
It has been fixed in glibc 2.35. But linker output must work on existing
glibc binaries.
1. Set p_align to the minimum page size while laying out segments aligning
to the maximum page size or section alignment. The run-time loader can
align segments to the minimum page size or above, depending on system page
size.
2. If -z max-page-size=NNN is used, p_align will be set to the maximum
page size or the largest section alignment.
3. If a section requires alignment higher than the minimum page size,
don't set p_align to the minimum page size.
4. If a section requires alignment higher than the maximum page size,
set p_align to the section alignment.
5. For objcopy, when the minimum page size != the maximum page size,
p_align may be set to the minimum page size while segments are aligned
to the maximum page size. In this case, the input p_align will be
ignored and the maximum page size will be used to align the ouput
segments.
6. Update linker to disallow the common page size > the maximum page size.
7. Update linker to avoid the common page size > the maximum page size.
8. Adjust pru_irq_map-1.d to expect p_align == sh_addralign:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 20000000 00007c 000004 00 AX 0 0 4
...
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000074 0x00000000 0x00000000 0x00008 0x00008 RW 0x1
LOAD 0x00007c 0x20000000 0x20000000 0x00004 0x00004 R E 0x4
vs.
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 20000000 00007c 000004 00 AX 0 0 4
...
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000074 0x00000000 0x00000000 0x00008 0x00008 RW 0x1
LOAD 0x00007c 0x20000000 0x20000000 0x00004 0x00004 R E 0x1
To enable this linker optimization, the backend should define ELF_P_ALIGN
to ELF_MINPAGESIZE.
bfd/
PR ld/28689
PR ld/28695
* elf-bfd.h (elf_backend_data): Add p_align.
* elf.c (assign_file_positions_for_load_sections): Set p_align
to the default p_align value while laying out segments aligning
to maximum page size or section alignment.
(elf_is_p_align_valid): New function.
(copy_elf_program_header): Call elf_is_p_align_valid to determine
if p_align is valid.
* elfxx-target.h (ELF_P_ALIGN): New. Default to 0.
(elfNN_bed): Add ELF_P_ALIGN.
* elfxx-x86.h (ELF_P_ALIGN): New. Set to ELF_MINPAGESIZE.
include/
PR ld/28689
PR ld/28695
* bfdlink.h (bfd_link_info): Add maxpagesize_is_set.
ld/
PR ld/28689
PR ld/28695
* emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Set
link_info.maxpagesize_is_set for -z max-page-size=NNN.
* ldelf.c (ldelf_after_parse): Disallow link_info.commonpagesize
> link_info.maxpagesize.
* testsuite/ld-elf/elf.exp: Pass -z max-page-size=0x4000 to
linker to build mbind2a and mbind2b.
* testsuite/ld-elf/header.d: Add -z common-page-size=0x100.
* testsuite/ld-elf/linux-x86.exp: Add PR ld/28689 tests.
* testsuite/ld-elf/p_align-1.c: New file.
* testsuite/ld-elf/page-size-1.d: New test.
* testsuite/ld-elf/pr26936.d: Add -z common-page-size=0x1000.
* testsuite/ld-elf/seg.d: Likewise.
* testsuite/ld-scripts/rgn-at5.d: Likewise.
* testsuite/ld-pru/pru_irq_map-1.d: Append 1 to name. Adjust
expected PT_LOAD segment alignment.
* testsuite/ld-pru/pru_irq_map-2.d: Append 2 to name.
* testsuite/ld-scripts/pr23571.d: Add -z max-page-size=0x1000.
Diffstat (limited to 'ld')
-rw-r--r-- | ld/emultempl/elf.em | 1 | ||||
-rw-r--r-- | ld/ldelf.c | 3 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/elf.exp | 4 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/header.d | 2 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/linux-x86.exp | 36 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/p_align-1.c | 25 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/page-size-1.d | 4 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/pr26936.d | 2 | ||||
-rw-r--r-- | ld/testsuite/ld-elf/seg.d | 2 | ||||
-rw-r--r-- | ld/testsuite/ld-pru/pru_irq_map-1.d | 4 | ||||
-rw-r--r-- | ld/testsuite/ld-pru/pru_irq_map-2.d | 2 | ||||
-rw-r--r-- | ld/testsuite/ld-scripts/pr23571.d | 2 | ||||
-rw-r--r-- | ld/testsuite/ld-scripts/rgn-at5.d | 2 |
13 files changed, 79 insertions, 10 deletions
diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index 9c8e792..5977526 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -721,6 +721,7 @@ fragment <<EOF || (link_info.maxpagesize & (link_info.maxpagesize - 1)) != 0) einfo (_("%F%P: invalid maximum page size \`%s'\n"), optarg + 14); + link_info.maxpagesize_is_set = true; } else if (startswith (optarg, "common-page-size=")) { @@ -72,6 +72,9 @@ ldelf_after_parse (void) link_info.dynamic_undefined_weak = 0; } after_parse_default (); + if (link_info.commonpagesize > link_info.maxpagesize) + einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"), + link_info.commonpagesize, link_info.maxpagesize); } /* Handle the generation of DT_NEEDED tags. */ diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp index 119908c..16128c2 100644 --- a/ld/testsuite/ld-elf/elf.exp +++ b/ld/testsuite/ld-elf/elf.exp @@ -365,7 +365,7 @@ if { [istarget *-*-linux*] run_ld_link_exec_tests [list \ [list \ "Run mbind2a" \ - "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \ + "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \ "" \ { mbind2a.s mbind2b.c } \ "mbind2a" \ @@ -374,7 +374,7 @@ if { [istarget *-*-linux*] ] \ [list \ "Run mbind2b" \ - "-static -Wl,-z,common-page-size=0x4000" \ + "-static -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \ "" \ { mbind2a.s mbind2b.c } \ "mbind2b" \ diff --git a/ld/testsuite/ld-elf/header.d b/ld/testsuite/ld-elf/header.d index c4d174a..67f0c98 100644 --- a/ld/testsuite/ld-elf/header.d +++ b/ld/testsuite/ld-elf/header.d @@ -1,5 +1,5 @@ # target: *-*-linux* *-*-gnu* *-*-vxworks arm*-*-uclinuxfdpiceabi -# ld: -T header.t -z max-page-size=0x100 +# ld: -T header.t -z max-page-size=0x100 -z common-page-size=0x100 # objdump: -hpw #... diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp index f48c196..2e0cbd3 100644 --- a/ld/testsuite/ld-elf/linux-x86.exp +++ b/ld/testsuite/ld-elf/linux-x86.exp @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \ "" \ "tmpdir/indirect-extern-access-2.so" \ ] \ + [list \ + "Run p_align-1a without PIE" \ + "$NOPIE_LDFLAGS" \ + "" \ + { p_align-1.c } \ + "p_align-1a" \ + "pass.out" \ + "$NOPIE_CFLAGS" \ + ] \ + [list \ + "Run p_align-1b with PIE" \ + "-pie" \ + "" \ + { p_align-1.c } \ + "p_align-1b" \ + "pass.out" \ + "-fpie" \ + ] \ + [list \ + "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \ + "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \ + "" \ + { p_align-1.c } \ + "p_align-1c" \ + "pass.out" \ + "$NOPIE_CFLAGS" \ + ] \ + [list \ + "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \ + "-pie -Wl,-z,max-page-size=0x1000" \ + "" \ + { p_align-1.c } \ + "p_align-1d" \ + "pass.out" \ + "-fpie" \ + ] \ ] proc elfedit_test { options test output } { diff --git a/ld/testsuite/ld-elf/p_align-1.c b/ld/testsuite/ld-elf/p_align-1.c new file mode 100644 index 0000000..6579cd7 --- /dev/null +++ b/ld/testsuite/ld-elf/p_align-1.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> + +#ifndef ALIGN +# define ALIGN 0x800000 +#endif + +int +__attribute__ ((weak)) +is_aligned (void *p, int align) +{ + return (((uintptr_t) p) & (align - 1)) == 0; +} + +int foo __attribute__ ((aligned (ALIGN))) = 1; + +int +main (void) +{ + if (!is_aligned (&foo, ALIGN)) + abort (); + printf ("PASS\n"); + return 0; +} diff --git a/ld/testsuite/ld-elf/page-size-1.d b/ld/testsuite/ld-elf/page-size-1.d new file mode 100644 index 0000000..04d2153 --- /dev/null +++ b/ld/testsuite/ld-elf/page-size-1.d @@ -0,0 +1,4 @@ +#source: dummy.s +#ld: -z common-page-size=0x4000 -z max-page-size=0x1000 +#error: common page size \(0x4000\) > maximum page size \(0x1000\) +#target: *-*-linux-gnu *-*-gnu* arm*-*-uclinuxfdpiceabi diff --git a/ld/testsuite/ld-elf/pr26936.d b/ld/testsuite/ld-elf/pr26936.d index 0a2831d..c479f47 100644 --- a/ld/testsuite/ld-elf/pr26936.d +++ b/ld/testsuite/ld-elf/pr26936.d @@ -2,7 +2,7 @@ #source: pr26936b.s #source: pr26936c.s #as: --gen-debug -#ld: -z noseparate-code -Ttext-segment 0x10000 -z max-page-size=0x1000 +#ld: -z noseparate-code -Ttext-segment 0x10000 -z max-page-size=0x1000 -z common-page-size=0x1000 #readelf: -wL -W #target: [check_shared_lib_support] # Assembly source file for the HPPA assembler is renamed and modifed by diff --git a/ld/testsuite/ld-elf/seg.d b/ld/testsuite/ld-elf/seg.d index 3ff7aba..9dce11e 100644 --- a/ld/testsuite/ld-elf/seg.d +++ b/ld/testsuite/ld-elf/seg.d @@ -1,6 +1,6 @@ #target: *-*-linux* *-*-gnu* *-*-vxworks arm*-*-uclinuxfdpiceabi #source: seg.s -#ld: -T seg.t -z max-page-size=0x1000 +#ld: -T seg.t -z max-page-size=0x1000 -z common-page-size=0x1000 #readelf: -l --wide #... diff --git a/ld/testsuite/ld-pru/pru_irq_map-1.d b/ld/testsuite/ld-pru/pru_irq_map-1.d index bbdf5b6..2538f4c 100644 --- a/ld/testsuite/ld-pru/pru_irq_map-1.d +++ b/ld/testsuite/ld-pru/pru_irq_map-1.d @@ -1,4 +1,4 @@ -#name: pru_irq_map special section for host +#name: pru_irq_map special section for host 1 #source: pru_irq_map.s #ld: --defsym=__HEAP_SIZE=0 --defsym=__STACK_SIZE=0 #readelf: -l --wide @@ -9,7 +9,7 @@ Program Headers: +Type +Offset +VirtAddr +PhysAddr +FileSiz +MemSiz +Flg +Align +LOAD +0x[0-9a-f]+ 0x0+ 0x0+ 0x0+8 0x0+8 RW 0x1 - +LOAD +0x[0-9a-f]+ 0x20+ 0x20+ 0x0+4 0x0+4 R E 0x1 + +LOAD +0x[0-9a-f]+ 0x20+ 0x20+ 0x0+4 0x0+4 R E 0x4 Section to Segment mapping: +Segment Sections... diff --git a/ld/testsuite/ld-pru/pru_irq_map-2.d b/ld/testsuite/ld-pru/pru_irq_map-2.d index 3166595..d6c583f 100644 --- a/ld/testsuite/ld-pru/pru_irq_map-2.d +++ b/ld/testsuite/ld-pru/pru_irq_map-2.d @@ -1,4 +1,4 @@ -#name: pru_irq_map special section for host +#name: pru_irq_map special section for host 2 #source: pru_irq_map.s #ld: --defsym=__HEAP_SIZE=0 --defsym=__STACK_SIZE=0 #readelf: -S --wide diff --git a/ld/testsuite/ld-scripts/pr23571.d b/ld/testsuite/ld-scripts/pr23571.d index adf4796..45b4059 100644 --- a/ld/testsuite/ld-scripts/pr23571.d +++ b/ld/testsuite/ld-scripts/pr23571.d @@ -1,5 +1,5 @@ #source: align2a.s -#ld: -T pr23571.t -z common-page-size=0x1000 +#ld: -T pr23571.t -z common-page-size=0x1000 -z max-page-size=0x1000 #objdump: -h -w .*: +file format .* diff --git a/ld/testsuite/ld-scripts/rgn-at5.d b/ld/testsuite/ld-scripts/rgn-at5.d index 767285c..e9ab081 100644 --- a/ld/testsuite/ld-scripts/rgn-at5.d +++ b/ld/testsuite/ld-scripts/rgn-at5.d @@ -1,6 +1,6 @@ # name: rgn-at5 # source: rgn-at5.s -# ld: -T rgn-at5.t -z max-page-size=0x1000 +# ld: -T rgn-at5.t -z max-page-size=0x1000 -z common-page-size=0x1000 # objdump: -w -h # target: *-*-linux* *-*-gnu* arm*-*-uclinuxfdpiceabi # xfail: rx-*-* |