-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 <SIZEOF>): 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.
This commit is contained in:
Alan Modra
2022-06-24 13:25:45 +09:30
parent b4eb841afe
commit 648f6099d4
8 changed files with 33 additions and 26 deletions

View File

@ -864,9 +864,17 @@ fold_name (etree_type *tree)
bfd_vma val; bfd_vma val;
if (tree->type.node_code == SIZEOF) if (tree->type.node_code == SIZEOF)
val = (os->bfd_section->size {
/ bfd_octets_per_byte (link_info.output_bfd, if (os->processed_vma)
os->bfd_section)); 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 else
val = (bfd_vma)1 << os->bfd_section->alignment_power; val = (bfd_vma)1 << os->bfd_section->alignment_power;

View File

@ -5778,7 +5778,6 @@ lang_size_sections_1
os->addr_tree = exp_intop (0); os->addr_tree = exp_intop (0);
if (os->addr_tree != NULL) if (os->addr_tree != NULL)
{ {
os->processed_vma = false;
exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
if (expld.result.valid_p) if (expld.result.valid_p)

View File

@ -6,11 +6,11 @@
SYMBOL TABLE: 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: Disassembly of section .text:
.* <_start>: .* <_start>:
.*: 8d 80 00 e0 ff ff lea -0x2000\(%eax\),%eax .*: 8d 80 0c e0 ff ff lea -0x1ff4\(%eax\),%eax
#pass #pass

View File

@ -50,19 +50,19 @@ Contents of the .eh_frame section:
Disassembly of section .plt: Disassembly of section .plt:
0+1b0 <.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 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_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 25 3c fe 3f 00 jmp \*0x3ffe3c\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10>
+[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\) +[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\)
Disassembly of section .plt.got: Disassembly of section .plt.got:
0+1c0 <func@plt>: 0+1c0 <func@plt>:
+[a-f0-9]+: ff 25 32 fe 3f 00 jmp \*0x3ffe32\(%rip\) # 3ffff8 <func> +[a-f0-9]+: ff 25 1a fe 3f 00 jmp \*0x3ffe1a\(%rip\) # 3fffe0 <func>
+[a-f0-9]+: 66 90 xchg %ax,%ax +[a-f0-9]+: 66 90 xchg %ax,%ax
Disassembly of section .text: Disassembly of section .text:
0+1c8 <foo>: 0+1c8 <foo>:
+[a-f0-9]+: e8 f3 ff ff ff call 1c0 <func@plt> +[a-f0-9]+: e8 f3 ff ff ff call 1c0 <func@plt>
+[a-f0-9]+: 48 8b 05 24 fe 3f 00 mov 0x3ffe24\(%rip\),%rax # 3ffff8 <func> +[a-f0-9]+: 48 8b 05 0c fe 3f 00 mov 0x3ffe0c\(%rip\),%rax # 3fffe0 <func>
#pass #pass

View File

@ -42,19 +42,19 @@ Contents of the .eh_frame section:
Disassembly of section .plt: Disassembly of section .plt:
0+120 <.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 35 ca fe 3f 00 push 0x3ffeca\(%rip\) # 3ffff0 <_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 25 cc fe 3f 00 jmp \*0x3ffecc\(%rip\) # 3ffff8 <_GLOBAL_OFFSET_TABLE_\+0x10>
+[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\) +[a-f0-9]+: 0f 1f 40 00 nopl 0x0\(%rax\)
Disassembly of section .plt.got: Disassembly of section .plt.got:
0+130 <func@plt>: 0+130 <func@plt>:
+[a-f0-9]+: ff 25 c2 fe 3f 00 jmp \*0x3ffec2\(%rip\) # 3ffff8 <func> +[a-f0-9]+: ff 25 aa fe 3f 00 jmp \*0x3ffeaa\(%rip\) # 3fffe0 <func>
+[a-f0-9]+: 66 90 xchg %ax,%ax +[a-f0-9]+: 66 90 xchg %ax,%ax
Disassembly of section .text: Disassembly of section .text:
0+138 <foo>: 0+138 <foo>:
+[a-f0-9]+: e8 f3 ff ff ff call 130 <func@plt> +[a-f0-9]+: e8 f3 ff ff ff call 130 <func@plt>
+[a-f0-9]+: 48 8b 05 b4 fe 3f 00 mov 0x3ffeb4\(%rip\),%rax # 3ffff8 <func> +[a-f0-9]+: 48 8b 05 9c fe 3f 00 mov 0x3ffe9c\(%rip\),%rax # 3fffe0 <func>
#pass #pass

View File

@ -49,19 +49,19 @@ Contents of the .eh_frame section:
Disassembly of section .plt: Disassembly of section .plt:
0+1b0 <.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 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_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]+: 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]+: 0f 1f 00 nopl \(%rax\)
Disassembly of section .plt.got: Disassembly of section .plt.got:
0+1c0 <func@plt>: 0+1c0 <func@plt>:
+[a-f0-9]+: f2 ff 25 31 fe 3f 00 bnd jmp \*0x3ffe31\(%rip\) # 3ffff8 <func> +[a-f0-9]+: f2 ff 25 19 fe 3f 00 bnd jmp \*0x3ffe19\(%rip\) # 3fffe0 <func>
+[a-f0-9]+: 90 nop +[a-f0-9]+: 90 nop
Disassembly of section .text: Disassembly of section .text:
0+1c8 <foo>: 0+1c8 <foo>:
+[a-f0-9]+: e8 f3 ff ff ff call 1c0 <func@plt> +[a-f0-9]+: e8 f3 ff ff ff call 1c0 <func@plt>
+[a-f0-9]+: 48 8b 05 24 fe 3f 00 mov 0x3ffe24\(%rip\),%rax # 3ffff8 <func> +[a-f0-9]+: 48 8b 05 0c fe 3f 00 mov 0x3ffe0c\(%rip\),%rax # 3fffe0 <func>
#pass #pass

View File

@ -49,8 +49,8 @@ Contents of the .eh_frame section:
Disassembly of section .plt: Disassembly of section .plt:
0+1b0 <.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 35 3a fe 3f 00 push 0x3ffe3a\(%rip\) # 3ffff0 <_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]+: 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]+: 0f 1f 00 nopl \(%rax\)
+[a-f0-9]+: 68 00 00 00 00 push \$0x0 +[a-f0-9]+: 68 00 00 00 00 push \$0x0
+[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1b0 <func@plt-0x20> +[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1b0 <func@plt-0x20>
@ -59,7 +59,7 @@ Disassembly of section .plt:
Disassembly of section .plt.sec: Disassembly of section .plt.sec:
0+1d0 <func@plt>: 0+1d0 <func@plt>:
+[a-f0-9]+: f2 ff 25 41 fe 3f 00 bnd jmp \*0x3ffe41\(%rip\) # 400018 <func> +[a-f0-9]+: f2 ff 25 29 fe 3f 00 bnd jmp \*0x3ffe29\(%rip\) # 400000 <func>
+[a-f0-9]+: 90 nop +[a-f0-9]+: 90 nop
Disassembly of section .text: Disassembly of section .text:

View File

@ -58,8 +58,8 @@ Contents of the .eh_frame section:
Disassembly of section .plt: Disassembly of section .plt:
0+1f0 <.plt>: 0+1f0 <.plt>:
+[a-f0-9]+: ff 35 12 fe 3f 00 push 0x3ffe12\(%rip\) # 400008 <_GLOBAL_OFFSET_TABLE_\+0x8> +[a-f0-9]+: ff 35 fa fd 3f 00 push 0x3ffdfa\(%rip\) # 3ffff0 <_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]+: 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]+: 0f 1f 00 nopl \(%rax\)
+[a-f0-9]+: 68 00 00 00 00 push \$0x0 +[a-f0-9]+: 68 00 00 00 00 push \$0x0
+[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1f0 <func1@plt-0x20> +[a-f0-9]+: f2 e9 e5 ff ff ff bnd jmp 1f0 <func1@plt-0x20>
@ -68,13 +68,13 @@ Disassembly of section .plt:
Disassembly of section .plt.got: Disassembly of section .plt.got:
0+210 <func1@plt>: 0+210 <func1@plt>:
+[a-f0-9]+: f2 ff 25 e1 fd 3f 00 bnd jmp \*0x3ffde1\(%rip\) # 3ffff8 <func1> +[a-f0-9]+: f2 ff 25 c9 fd 3f 00 bnd jmp \*0x3ffdc9\(%rip\) # 3fffe0 <func1>
+[a-f0-9]+: 90 nop +[a-f0-9]+: 90 nop
Disassembly of section .plt.sec: Disassembly of section .plt.sec:
0+218 <func2@plt>: 0+218 <func2@plt>:
+[a-f0-9]+: f2 ff 25 f9 fd 3f 00 bnd jmp \*0x3ffdf9\(%rip\) # 400018 <func2> +[a-f0-9]+: f2 ff 25 e1 fd 3f 00 bnd jmp \*0x3ffde1\(%rip\) # 400000 <func2>
+[a-f0-9]+: 90 nop +[a-f0-9]+: 90 nop
Disassembly of section .text: Disassembly of section .text:
@ -82,5 +82,5 @@ Disassembly of section .text:
0+220 <foo>: 0+220 <foo>:
+[a-f0-9]+: e8 eb ff ff ff call 210 <func1@plt> +[a-f0-9]+: e8 eb ff ff ff call 210 <func1@plt>
+[a-f0-9]+: e8 ee ff ff ff call 218 <func2@plt> +[a-f0-9]+: e8 ee ff ff ff call 218 <func2@plt>
+[a-f0-9]+: 48 8b 05 c7 fd 3f 00 mov 0x3ffdc7\(%rip\),%rax # 3ffff8 <func1> +[a-f0-9]+: 48 8b 05 af fd 3f 00 mov 0x3ffdaf\(%rip\),%rax # 3fffe0 <func1>
#pass #pass