From 648f6099d4dcadf446f3f00790ad4b16dd4042f6 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 24 Jun 2022 13:25:45 +0930 Subject: [PATCH] -z relro relaxation and ld script SIZEOF A number of targets use assignments like: . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 12 ? 12 : 0, .); (from i386) in linker scripts to put the end of the relro segment past the header in .got.plt. Examination of testcases like those edited by this patch instead sees the end of the relro segment being placed at the start of .got.plt. For the i386 pie1 test: [ 9] .got.plt PROGBITS 00002000 001000 00000c 04 WA 0 0 4 GNU_RELRO 0x000f90 0x00001f90 0x00001f90 0x00070 0x00070 R 0x1 A map file shows: .dynamic 0x0000000000001f90 0x70 *(.dynamic) .dynamic 0x0000000000001f90 0x70 tmpdir/pie1.o 0x0000000000001f90 _DYNAMIC .got 0x0000000000002000 0x0 *(.got) .got 0x0000000000002000 0x0 tmpdir/pie1.o *(.igot) 0x0000000000002ff4 . = DATA_SEGMENT_RELRO_END (., (SIZEOF (.got.plt) >= 0xc)?0xc:0x0) .got.plt 0x0000000000002000 0xc *(.got.plt) .got.plt 0x0000000000002000 0xc tmpdir/pie1.o 0x0000000000002000 _GLOBAL_OFFSET_TABLE_ The DATA_SEGMENT_RELRO_END value in the map file is weird too. All of this is triggered by SIZEOF (.got.plt) being evaluated wrongly as zero. Fix it by taking into account the action of lang_reset_memory_regions during relaxation. * ldexp.c (fold_name ): Use rawsize if size has been reset. * ldlang.c (lang_size_sections_1): Don't reset processed_vma here. * testsuite/ld-i386/pie1.d: Adjust to suit. * testsuite/ld-x86-64/pr20830a.d: Likewise. * testsuite/ld-x86-64/pr20830b.d: Likewise. * testsuite/ld-x86-64/pr21038a.d: Likewise. * testsuite/ld-x86-64/pr21038b.d: Likewise. * testsuite/ld-x86-64/pr21038c.d: Likewise. --- ld/ldexp.c | 14 +++++++++++--- ld/ldlang.c | 1 - ld/testsuite/ld-i386/pie1.d | 4 ++-- ld/testsuite/ld-x86-64/pr20830a.d | 8 ++++---- ld/testsuite/ld-x86-64/pr20830b.d | 8 ++++---- ld/testsuite/ld-x86-64/pr21038a.d | 8 ++++---- ld/testsuite/ld-x86-64/pr21038b.d | 6 +++--- ld/testsuite/ld-x86-64/pr21038c.d | 10 +++++----- 8 files changed, 33 insertions(+), 26 deletions(-) diff --git a/ld/ldexp.c b/ld/ldexp.c index c18b8284ca5..d4d8706968d 100644 --- a/ld/ldexp.c +++ b/ld/ldexp.c @@ -864,9 +864,17 @@ fold_name (etree_type *tree) bfd_vma val; if (tree->type.node_code == SIZEOF) - val = (os->bfd_section->size - / bfd_octets_per_byte (link_info.output_bfd, - os->bfd_section)); + { + if (os->processed_vma) + val = os->bfd_section->size; + else + /* If we've just called lang_reset_memory_regions, + size will be zero and a previous estimate of + size will be in rawsize. */ + val = os->bfd_section->rawsize; + val /= bfd_octets_per_byte (link_info.output_bfd, + os->bfd_section); + } else val = (bfd_vma)1 << os->bfd_section->alignment_power; diff --git a/ld/ldlang.c b/ld/ldlang.c index 839535bdb73..e640380e901 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -5778,7 +5778,6 @@ lang_size_sections_1 os->addr_tree = exp_intop (0); if (os->addr_tree != NULL) { - os->processed_vma = false; exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); if (expld.result.valid_p) diff --git a/ld/testsuite/ld-i386/pie1.d b/ld/testsuite/ld-i386/pie1.d index 338d05784d0..fdc65b101f6 100644 --- a/ld/testsuite/ld-i386/pie1.d +++ b/ld/testsuite/ld-i386/pie1.d @@ -6,11 +6,11 @@ SYMBOL TABLE: #... -0+2000 l O .got.plt 0+ _GLOBAL_OFFSET_TABLE_ +0+1ff4 l O .got.plt 0+ _GLOBAL_OFFSET_TABLE_ #... Disassembly of section .text: .* <_start>: -.*: 8d 80 00 e0 ff ff lea -0x2000\(%eax\),%eax +.*: 8d 80 0c e0 ff ff lea -0x1ff4\(%eax\),%eax #pass diff --git a/ld/testsuite/ld-x86-64/pr20830a.d b/ld/testsuite/ld-x86-64/pr20830a.d index 2c47a51b19d..1d92b10ba71 100644 --- a/ld/testsuite/ld-x86-64/pr20830a.d +++ b/ld/testsuite/ld-x86-64/pr20830a.d @@ -50,19 +50,19 @@ Contents of the .eh_frame section: Disassembly of section .plt: 0+1b0 <.plt>: - +[a-f0-9]+: ff 35 52 fe 3f 00 push 0x3ffe52\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> - +[a-f0-9]+: ff 25 54 fe 3f 00 jmp \*0x3ffe54\(%rip\) # 400010 <_GLOBAL_OFFSET_TABLE_\+0x10> + +[a-f0-9]+: ff 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_GLOBAL_OFFSET_TABLE_\+0x8> + +[a-f0-9]+: ff 25 3c fe 3f 00 jmp \*0x3ffe3c\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10> +[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\) Disassembly of section .plt.got: 0+1c0 : - +[a-f0-9]+: ff 25 32 fe 3f 00 jmp \*0x3ffe32\(%rip\) # 3ffff8 + +[a-f0-9]+: ff 25 1a fe 3f 00 jmp \*0x3ffe1a\(%rip\) # 3fffe0 +[a-f0-9]+: 66 90 xchg %ax,%ax Disassembly of section .text: 0+1c8 : +[a-f0-9]+: e8 f3 ff ff ff call 1c0 - +[a-f0-9]+: 48 8b 05 24 fe 3f 00 mov 0x3ffe24\(%rip\),%rax # 3ffff8 + +[a-f0-9]+: 48 8b 05 0c fe 3f 00 mov 0x3ffe0c\(%rip\),%rax # 3fffe0 #pass diff --git a/ld/testsuite/ld-x86-64/pr20830b.d b/ld/testsuite/ld-x86-64/pr20830b.d index 047adc17460..303a0242573 100644 --- a/ld/testsuite/ld-x86-64/pr20830b.d +++ b/ld/testsuite/ld-x86-64/pr20830b.d @@ -42,19 +42,19 @@ Contents of the .eh_frame section: Disassembly of section .plt: 0+120 <.plt>: - +[a-f0-9]+: ff 35 e2 fe 3f 00 push 0x3ffee2\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> - +[a-f0-9]+: ff 25 e4 fe 3f 00 jmp \*0x3ffee4\(%rip\) # 400010 <_GLOBAL_OFFSET_TABLE_\+0x10> + +[a-f0-9]+: ff 35 ca fe 3f 00 push 0x3ffeca\(%rip\) # 3ffff0 <_GLOBAL_OFFSET_TABLE_\+0x8> + +[a-f0-9]+: ff 25 cc fe 3f 00 jmp \*0x3ffecc\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10> +[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\) Disassembly of section .plt.got: 0+130 : - +[a-f0-9]+: ff 25 c2 fe 3f 00 jmp \*0x3ffec2\(%rip\) # 3ffff8 + +[a-f0-9]+: ff 25 aa fe 3f 00 jmp \*0x3ffeaa\(%rip\) # 3fffe0 +[a-f0-9]+: 66 90 xchg %ax,%ax Disassembly of section .text: 0+138 : +[a-f0-9]+: e8 f3 ff ff ff call 130 - +[a-f0-9]+: 48 8b 05 b4 fe 3f 00 mov 0x3ffeb4\(%rip\),%rax # 3ffff8 + +[a-f0-9]+: 48 8b 05 9c fe 3f 00 mov 0x3ffe9c\(%rip\),%rax # 3fffe0 #pass diff --git a/ld/testsuite/ld-x86-64/pr21038a.d b/ld/testsuite/ld-x86-64/pr21038a.d index 070d37e5bcc..06e504ec956 100644 --- a/ld/testsuite/ld-x86-64/pr21038a.d +++ b/ld/testsuite/ld-x86-64/pr21038a.d @@ -49,19 +49,19 @@ Contents of the .eh_frame section: Disassembly of section .plt: 0+1b0 <.plt>: - +[a-f0-9]+: ff 35 52 fe 3f 00 push 0x3ffe52\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> - +[a-f0-9]+: f2 ff 25 53 fe 3f 00 bnd jmp \*0x3ffe53\(%rip\) # 400010 <_GLOBAL_OFFSET_TABLE_\+0x10> + +[a-f0-9]+: ff 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_GLOBAL_OFFSET_TABLE_\+0x8> + +[a-f0-9]+: f2 ff 25 3b fe 3f 00 bnd jmp \*0x3ffe3b\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10> +[a-f0-9]+: 0f 1f 00 nopl \(%rax\) Disassembly of section .plt.got: 0+1c0 : - +[a-f0-9]+: f2 ff 25 31 fe 3f 00 bnd jmp \*0x3ffe31\(%rip\) # 3ffff8 + +[a-f0-9]+: f2 ff 25 19 fe 3f 00 bnd jmp \*0x3ffe19\(%rip\) # 3fffe0 +[a-f0-9]+: 90 nop Disassembly of section .text: 0+1c8 : +[a-f0-9]+: e8 f3 ff ff ff call 1c0 - +[a-f0-9]+: 48 8b 05 24 fe 3f 00 mov 0x3ffe24\(%rip\),%rax # 3ffff8 + +[a-f0-9]+: 48 8b 05 0c fe 3f 00 mov 0x3ffe0c\(%rip\),%rax # 3fffe0 #pass diff --git a/ld/testsuite/ld-x86-64/pr21038b.d b/ld/testsuite/ld-x86-64/pr21038b.d index d6a9567364d..0e77d2c89d7 100644 --- a/ld/testsuite/ld-x86-64/pr21038b.d +++ b/ld/testsuite/ld-x86-64/pr21038b.d @@ -49,8 +49,8 @@ Contents of the .eh_frame section: Disassembly of section .plt: 0+1b0 <.plt>: - +[a-f0-9]+: ff 35 52 fe 3f 00 push 0x3ffe52\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> - +[a-f0-9]+: f2 ff 25 53 fe 3f 00 bnd jmp \*0x3ffe53\(%rip\) # 400010 <_GLOBAL_OFFSET_TABLE_\+0x10> + +[a-f0-9]+: ff 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_GLOBAL_OFFSET_TABLE_\+0x8> + +[a-f0-9]+: f2 ff 25 3b fe 3f 00 bnd jmp \*0x3ffe3b\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10> +[a-f0-9]+: 0f 1f 00 nopl \(%rax\) +[a-f0-9]+: 68 00 00 00 00 push \$0x0 +[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1b0 @@ -59,7 +59,7 @@ Disassembly of section .plt: Disassembly of section .plt.sec: 0+1d0 : - +[a-f0-9]+: f2 ff 25 41 fe 3f 00 bnd jmp \*0x3ffe41\(%rip\) # 400018 + +[a-f0-9]+: f2 ff 25 29 fe 3f 00 bnd jmp \*0x3ffe29\(%rip\) # 400000 +[a-f0-9]+: 90 nop Disassembly of section .text: diff --git a/ld/testsuite/ld-x86-64/pr21038c.d b/ld/testsuite/ld-x86-64/pr21038c.d index 6784d336a53..afbdb56086c 100644 --- a/ld/testsuite/ld-x86-64/pr21038c.d +++ b/ld/testsuite/ld-x86-64/pr21038c.d @@ -58,8 +58,8 @@ Contents of the .eh_frame section: Disassembly of section .plt: 0+1f0 <.plt>: - +[a-f0-9]+: ff 35 12 fe 3f 00 push 0x3ffe12\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> - +[a-f0-9]+: f2 ff 25 13 fe 3f 00 bnd jmp \*0x3ffe13\(%rip\) # 400010 <_GLOBAL_OFFSET_TABLE_\+0x10> + +[a-f0-9]+: ff 35 fa fd 3f 00 push 0x3ffdfa\(%rip\) # 3ffff0 <_GLOBAL_OFFSET_TABLE_\+0x8> + +[a-f0-9]+: f2 ff 25 fb fd 3f 00 bnd jmp \*0x3ffdfb\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10> +[a-f0-9]+: 0f 1f 00 nopl \(%rax\) +[a-f0-9]+: 68 00 00 00 00 push \$0x0 +[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1f0 @@ -68,13 +68,13 @@ Disassembly of section .plt: Disassembly of section .plt.got: 0+210 : - +[a-f0-9]+: f2 ff 25 e1 fd 3f 00 bnd jmp \*0x3ffde1\(%rip\) # 3ffff8 + +[a-f0-9]+: f2 ff 25 c9 fd 3f 00 bnd jmp \*0x3ffdc9\(%rip\) # 3fffe0 +[a-f0-9]+: 90 nop Disassembly of section .plt.sec: 0+218 : - +[a-f0-9]+: f2 ff 25 f9 fd 3f 00 bnd jmp \*0x3ffdf9\(%rip\) # 400018 + +[a-f0-9]+: f2 ff 25 e1 fd 3f 00 bnd jmp \*0x3ffde1\(%rip\) # 400000 +[a-f0-9]+: 90 nop Disassembly of section .text: @@ -82,5 +82,5 @@ Disassembly of section .text: 0+220 : +[a-f0-9]+: e8 eb ff ff ff call 210 +[a-f0-9]+: e8 ee ff ff ff call 218 - +[a-f0-9]+: 48 8b 05 c7 fd 3f 00 mov 0x3ffdc7\(%rip\),%rax # 3ffff8 + +[a-f0-9]+: 48 8b 05 af fd 3f 00 mov 0x3ffdaf\(%rip\),%rax # 3fffe0 #pass -- 2.30.2