diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2022-01-10 15:26:18 -0800 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2022-01-13 05:20:51 -0800 |
commit | 2f83249c13d86065b4c7cdb198ea871017b4bba1 (patch) | |
tree | 9e1807cdf070909cb98f34c3bb25a53f33d6f36b /ld/ldlang.c | |
parent | ce2d3708bc8b1dfed6a3c69b56077ad10d2f351d (diff) | |
download | fsf-binutils-gdb-2f83249c13d86065b4c7cdb198ea871017b4bba1.zip fsf-binutils-gdb-2f83249c13d86065b4c7cdb198ea871017b4bba1.tar.gz fsf-binutils-gdb-2f83249c13d86065b4c7cdb198ea871017b4bba1.tar.bz2 |
elf: Remove the 1-page gap before the RELRO segment
The existing RELRO scheme may leave a 1-page gap before the RELRO segment
and align the end of the RELRO segment to the page size:
[18] .eh_frame PROGBITS 408fa0 008fa0 005e80 00 A 0 0 8
[19] .init_array INIT_ARRAY 410de0 00fde0 000008 08 WA 0 0 8
[20] .fini_array FINI_ARRAY 410de8 00fde8 000008 08 WA 0 0 8
[21] .dynamic DYNAMIC 410df0 00fdf0 000200 10 WA 7 0 8
[22] .got PROGBITS 410ff0 00fff0 000010 08 WA 0 0 8
[23] .got.plt PROGBITS 411000 010000 000048 08 WA 0 0 8
Instead, we can remove the 1-page gap if the maximum page size >= the
maximum section alignment:
[18] .eh_frame PROGBITS 408fa0 008fa0 005e80 00 A 0 0 8
[19] .init_array INIT_ARRAY 40fde0 00fde0 000008 08 WA 0 0 8
[20] .fini_array FINI_ARRAY 40fde8 00fde8 000008 08 WA 0 0 8
[21] .dynamic DYNAMIC 40fdf0 00fdf0 000200 10 WA 7 0 8
[22] .got PROGBITS 40fff0 00fff0 000010 08 WA 0 0 8
[23] .got.plt PROGBITS 410000 010000 000048 08 WA 0 0 8
Because the end of the RELRO segment is always aligned to the page size
and may not be moved, the RELRO segment size may be increased:
[ 3] .dynstr STRTAB 000148 000148 000001 00 A 0 0 1
[ 4] .eh_frame PROGBITS 000150 000150 000000 00 A 0 0 8
[ 5] .init_array INIT_ARRAY 200150 000150 000010 08 WA 0 0 1
[ 6] .fini_array FINI_ARRAY 200160 000160 000010 08 WA 0 0 1
[ 7] .jcr PROGBITS 200170 000170 000008 00 WA 0 0 1
[ 8] .data.rel.ro PROGBITS 200180 000180 000020 00 WA 0 0 16
[ 9] .dynamic DYNAMIC 2001a0 0001a0 0001c0 10 WA 3 0 8
[10] .got PROGBITS 200360 000360 0002a8 00 WA 0 0 8
[11] .bss NOBITS 201000 000608 000840 00 WA 0 0 1
vs the old section layout:
[ 3] .dynstr STRTAB 000148 000148 000001 00 A 0 0 1
[ 4] .eh_frame PROGBITS 000150 000150 000000 00 A 0 0 8
[ 5] .init_array INIT_ARRAY 200b48 000b48 000010 08 WA 0 0 1
[ 6] .fini_array FINI_ARRAY 200b58 000b58 000010 08 WA 0 0 1
[ 7] .jcr PROGBITS 200b68 000b68 000008 00 WA 0 0 1
[ 8] .data.rel.ro PROGBITS 200b70 000b70 000020 00 WA 0 0 16
[ 9] .dynamic DYNAMIC 200b90 000b90 0001c0 10 WA 3 0 8
[10] .got PROGBITS 200d50 000d50 0002a8 00 WA 0 0 8
[11] .bss NOBITS 201000 000ff8 000840 00 WA 0 0 1
But there is no 1-page gap.
PR ld/28743
* ldlang.c (lang_size_relro_segment_1): Remove the 1-page gap
before the RELRO segment if the maximum page size >= the maximum
section alignment.
* testsuite/ld-i386/pr20830.d: Adjusted.
* testsuite/ld-s390/gotreloc_64-relro-1.dd: Likewise.
* testsuite/ld-x86-64/pr14207.d: Likewise.
* testsuite/ld-x86-64/pr18176.d: Likewise.
* testsuite/ld-x86-64/pr20830a-now.d: Likewise.
* testsuite/ld-x86-64/pr20830a.d: Likewise.
* testsuite/ld-x86-64/pr20830b-now.d: Likewise.
* testsuite/ld-x86-64/pr20830b.d: Likewise.
* testsuite/ld-x86-64/pr21038a-now.d: Likewise.
* testsuite/ld-x86-64/pr21038a.d: Likewise.
* testsuite/ld-x86-64/pr21038b-now.d: Likewise.
* testsuite/ld-x86-64/pr21038c-now.d: Likewise.
* testsuite/ld-x86-64/pr21038c.d: Likewise.
Diffstat (limited to 'ld/ldlang.c')
-rw-r--r-- | ld/ldlang.c | 89 |
1 files changed, 71 insertions, 18 deletions
diff --git a/ld/ldlang.c b/ld/ldlang.c index 0a24fd8..48e4082 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -6371,7 +6371,9 @@ static bfd_vma lang_size_relro_segment_1 (seg_align_type *seg) { bfd_vma relro_end, desired_end; - asection *sec; + asection *sec, *prev_sec = NULL; + bool remove_page_gap = false; + unsigned int max_alignment_power = 0; /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ relro_end = ((seg->relro_end + seg->pagesize - 1) @@ -6382,28 +6384,79 @@ lang_size_relro_segment_1 (seg_align_type *seg) /* For sections in the relro segment.. */ for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) - if ((sec->flags & SEC_ALLOC) != 0 - && sec->vma >= seg->base - && sec->vma < seg->relro_end - seg->relro_offset) + if ((sec->flags & SEC_ALLOC) != 0) { - /* Where do we want to put this section so that it ends as - desired? */ - bfd_vma start, end, bump; - - end = start = sec->vma; - if (!IS_TBSS (sec)) - end += TO_ADDR (sec->size); - bump = desired_end - end; - /* We'd like to increase START by BUMP, but we must heed - alignment so the increase might be less than optimum. */ - start += bump; - start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); - /* This is now the desired end for the previous section. */ - desired_end = start; + if (sec->alignment_power > max_alignment_power) + max_alignment_power = sec->alignment_power; + + if (sec->vma >= seg->base + && sec->vma < seg->relro_end - seg->relro_offset) + { + /* Where do we want to put this section so that it ends as + desired? */ + bfd_vma start, end, bump; + + end = start = sec->vma; + if (!IS_TBSS (sec)) + end += TO_ADDR (sec->size); + bump = desired_end - end; + /* We'd like to increase START by BUMP, but we must heed + alignment so the increase might be less than optimum. */ + start += bump; + start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); + /* This is now the desired end for the previous section. */ + desired_end = start; + prev_sec = sec->prev; + } } seg->phase = exp_seg_relro_adjust; ASSERT (desired_end >= seg->base); + + for (; prev_sec; prev_sec = prev_sec->prev) + if ((prev_sec->flags & SEC_ALLOC) != 0) + { + if (prev_sec->alignment_power > max_alignment_power) + max_alignment_power = prev_sec->alignment_power; + + if (prev_sec->size != 0) + { + /* The 1-page gap before the RELRO segment may be removed. */ + remove_page_gap = ((prev_sec->vma + prev_sec->size + + seg->maxpagesize) < desired_end); + + break; + } + } + + if (remove_page_gap) + { + /* Find the maximum section alignment. */ + for (sec = prev_sec; sec; sec = sec->prev) + if ((sec->flags & SEC_ALLOC) != 0 + && sec->alignment_power > max_alignment_power) + max_alignment_power = sec->alignment_power; + + /* Remove the 1-page gap before the RELRO segment only if the + maximum page size >= the maximum section alignment. */ + if (seg->maxpagesize >= (1U << max_alignment_power)) + { + /* If the preceding section size is greater than the maximum + page size, subtract the maximum page size. Otherwise, + align the RELRO segment to the maximum page size. */ + if (prev_sec->size > seg->maxpagesize) + { + desired_end -= seg->maxpagesize; + relro_end -= seg->maxpagesize; + } + else + { + desired_end &= ~(seg->maxpagesize - 1); + relro_end &= ~(seg->maxpagesize - 1); + } + } + } + seg->base = desired_end; return relro_end; } |