From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id B684C3857806 for ; Mon, 27 Jun 2022 00:23:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B684C3857806 Received: by mail-pg1-x52a.google.com with SMTP id 68so7597972pgb.10 for ; Sun, 26 Jun 2022 17:23:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=mFugpqbwq9OKt79Fk4GbY32Mr91a5Dz5bnt+6h653hY=; b=4aUoq2EdBwDPyuMmVlXT5yzDt0XlvGQJQrDEfmV6CzKmOjdHlqJxbZN2IMJu2BPHx6 tEyXQihJqru+zVAg7pkbiDEQRW1tAzhj45VhV77PS8rpQFxTr00n+GsaHO99laNa8Sul Osm7TSKDaFYnSqihKvyfepLVxjMQja5gPGoQn/zDe8tfOpTspQSgW86Cg6mN0o5aTXMJ /cjN0h8w09NV+eUaE/LxELPssL4YvJYoedhreNTYjTMQjaYUcEucXXGIwqbPP2Ct4YKf wg+00EGP7bpW85NudRaXOj0wiw8i2IvHbzj9O5WtnoB7uwbkcmwv/QyD3nbGRXbJ6aGY Xf+g== X-Gm-Message-State: AJIora/Do0aE7nMCtKR3fRmzuBkfdIJDpTuAbngUtjs935fAjws+wywO 9wMkclQkl9itKWz4sEMQS2YtQdxigAQ= X-Google-Smtp-Source: AGRyM1tJLAD9XgOAa4lh180q8mHSU4cJVtSRwDDzMd2jJe2nInV/YMu+fX38QazQuuSyLZAb6MD1Vg== X-Received: by 2002:a05:6a00:16c7:b0:520:6ede:2539 with SMTP id l7-20020a056a0016c700b005206ede2539mr11883055pfc.46.1656289401217; Sun, 26 Jun 2022 17:23:21 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id 125-20020a630483000000b0040d2717473fsm5855487pge.38.2022.06.26.17.23.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Jun 2022 17:23:20 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 6C21411402C1; Mon, 27 Jun 2022 09:42:56 +0930 (ACST) Date: Mon, 27 Jun 2022 09:42:56 +0930 From: Alan Modra To: binutils@sourceware.org Subject: -z relro relaxation and ld script SIZEOF Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3037.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Jun 2022 00:23:25 -0000 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. 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 -- Alan Modra Australia Development Lab, IBM