Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1 | From b2e18d3ea300f7491705b6e86a7cc3d6366e3b1f Mon Sep 17 00:00:00 2001 |
| 2 | From: "H.J. Lu" <hjl.tools@gmail.com> |
| 3 | Date: Tue, 30 Nov 2021 20:40:38 -0800 |
| 4 | Subject: [PATCH] elf: Discard input .note.gnu.build-id sections |
| 5 | |
| 6 | 1. Discard input .note.gnu.build-id sections. |
| 7 | 2. Clear the build ID field before writing. |
| 8 | 3. Use bfd_make_section_anyway_with_flags to create the output |
| 9 | .note.gnu.build-id section. |
| 10 | |
| 11 | PR ld/28639 |
| 12 | * ldelf.c (ldelf_after_open): Discard input .note.gnu.build-id |
| 13 | sections, excluding the first one. |
| 14 | (write_build_id): Clear the build ID field before writing. |
| 15 | (ldelf_setup_build_id): Use bfd_make_section_anyway_with_flags |
| 16 | to create the output .note.gnu.build-id section. |
| 17 | * testsuite/ld-elf/build-id.exp: New file. |
| 18 | * testsuite/ld-elf/pr28639a.rd: Likewise. |
| 19 | * testsuite/ld-elf/pr28639b.rd: Likewise. |
| 20 | * testsuite/ld-elf/pr28639c.rd: Likewise. |
| 21 | * testsuite/ld-elf/pr28639d.rd: Likewise. |
| 22 | |
| 23 | Upstream-Status: Backport |
| 24 | |
| 25 | Reference to upstream patch: |
| 26 | [https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=1f1d0f8888a6c944e612b416a2a6e11abcf5199f] |
| 27 | --- |
| 28 | ld/ldelf.c | 15 ++++++- |
| 29 | ld/testsuite/ld-elf/build-id.exp | 77 ++++++++++++++++++++++++++++++++ |
| 30 | ld/testsuite/ld-elf/pr28639a.rd | 6 +++ |
| 31 | ld/testsuite/ld-elf/pr28639b.rd | 6 +++ |
| 32 | ld/testsuite/ld-elf/pr28639c.rd | 10 +++++ |
| 33 | ld/testsuite/ld-elf/pr28639d.rd | 4 ++ |
| 34 | 6 files changed, 117 insertions(+), 1 deletion(-) |
| 35 | create mode 100644 ld/testsuite/ld-elf/build-id.exp |
| 36 | create mode 100644 ld/testsuite/ld-elf/pr28639a.rd |
| 37 | create mode 100644 ld/testsuite/ld-elf/pr28639b.rd |
| 38 | create mode 100644 ld/testsuite/ld-elf/pr28639c.rd |
| 39 | create mode 100644 ld/testsuite/ld-elf/pr28639d.rd |
| 40 | |
| 41 | diff --git a/ld/ldelf.c b/ld/ldelf.c |
| 42 | index 21e655bb55c..8501d98b48f 100644 |
| 43 | --- a/ld/ldelf.c |
| 44 | +++ b/ld/ldelf.c |
| 45 | @@ -1043,6 +1043,15 @@ ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd, |
| 46 | /* Do not allow executable files to be used as inputs to the link. */ |
| 47 | for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next) |
| 48 | { |
| 49 | + /* Discard input .note.gnu.build-id sections. */ |
| 50 | + s = bfd_get_section_by_name (abfd, ".note.gnu.build-id"); |
| 51 | + while (s != NULL) |
| 52 | + { |
| 53 | + if (s != elf_tdata (link_info.output_bfd)->o->build_id.sec) |
| 54 | + s->flags |= SEC_EXCLUDE; |
| 55 | + s = bfd_get_next_section_by_name (NULL, s); |
| 56 | + } |
| 57 | + |
| 58 | if (abfd->xvec->flavour == bfd_target_elf_flavour |
| 59 | && !bfd_input_just_syms (abfd) |
| 60 | && elf_tdata (abfd) != NULL |
| 61 | @@ -1387,6 +1396,9 @@ write_build_id (bfd *abfd) |
| 62 | id_bits = contents + size; |
| 63 | size = asec->size - size; |
| 64 | |
| 65 | + /* Clear the build ID field. */ |
| 66 | + memset (id_bits, 0, size); |
| 67 | + |
| 68 | bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz); |
| 69 | bfd_h_put_32 (abfd, size, &e_note->descsz); |
| 70 | bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type); |
| 71 | @@ -1418,7 +1430,8 @@ ldelf_setup_build_id (bfd *ibfd) |
| 72 | |
| 73 | flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY |
| 74 | | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA); |
| 75 | - s = bfd_make_section_with_flags (ibfd, ".note.gnu.build-id", flags); |
| 76 | + s = bfd_make_section_anyway_with_flags (ibfd, ".note.gnu.build-id", |
| 77 | + flags); |
| 78 | if (s != NULL && bfd_set_section_alignment (s, 2)) |
| 79 | { |
| 80 | struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd); |
| 81 | diff --git a/ld/testsuite/ld-elf/build-id.exp b/ld/testsuite/ld-elf/build-id.exp |
| 82 | new file mode 100644 |
| 83 | index 00000000000..19c22a75c4d |
| 84 | --- /dev/null |
| 85 | +++ b/ld/testsuite/ld-elf/build-id.exp |
| 86 | @@ -0,0 +1,77 @@ |
| 87 | +# Expect script for --build-id tests. |
| 88 | +# Copyright (C) 2021 Free Software Foundation, Inc. |
| 89 | +# |
| 90 | +# This file is part of the GNU Binutils. |
| 91 | +# |
| 92 | +# This program is free software; you can redistribute it and/or modify |
| 93 | +# it under the terms of the GNU General Public License as published by |
| 94 | +# the Free Software Foundation; either version 3 of the License, or |
| 95 | +# (at your option) any later version. |
| 96 | +# |
| 97 | +# This program is distributed in the hope that it will be useful, |
| 98 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 99 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 100 | +# GNU General Public License for more details. |
| 101 | +# |
| 102 | +# You should have received a copy of the GNU General Public License |
| 103 | +# along with this program; if not, write to the Free Software |
| 104 | +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 105 | +# MA 02110-1301, USA. |
| 106 | +# |
| 107 | + |
| 108 | +# Exclude non-ELF targets. |
| 109 | + |
| 110 | +if ![is_elf_format] { |
| 111 | + return |
| 112 | +} |
| 113 | + |
| 114 | +if { [istarget frv-*-*] || [istarget lm32-*-*] } { |
| 115 | + return |
| 116 | +} |
| 117 | + |
| 118 | +if { !([istarget *-*-linux*] |
| 119 | + || [istarget arm*-*-uclinuxfdpiceabi] |
| 120 | + || [istarget *-*-nacl*] |
| 121 | + || [istarget *-*-gnu*]) } then { |
| 122 | + return |
| 123 | +} |
| 124 | + |
| 125 | +run_ld_link_tests [list \ |
| 126 | + [list \ |
| 127 | + "pr28639a.o" \ |
| 128 | + "-r --build-id=md5" \ |
| 129 | + "" \ |
| 130 | + "" \ |
| 131 | + {start.s} \ |
| 132 | + {{readelf {--notes} pr28639a.rd}} \ |
| 133 | + "pr28639a.o" \ |
| 134 | + ] \ |
| 135 | + [list \ |
| 136 | + "pr28639a.o" \ |
| 137 | + "-r --build-id" \ |
| 138 | + "" \ |
| 139 | + "" \ |
| 140 | + {dummy.s} \ |
| 141 | + {{readelf {--notes} pr28639b.rd}} \ |
| 142 | + "pr28639b.o" \ |
| 143 | + ] \ |
| 144 | + [list \ |
| 145 | + "pr28639a" \ |
| 146 | + "--build-id tmpdir/pr28639a.o tmpdir/pr28639b.o" \ |
| 147 | + "" \ |
| 148 | + "" \ |
| 149 | + {dummy.s} \ |
| 150 | + {{readelf {--notes} pr28639b.rd} \ |
| 151 | + {readelf {--notes} pr28639c.rd}} \ |
| 152 | + "pr28639a" \ |
| 153 | + ] \ |
| 154 | + [list \ |
| 155 | + "pr28639b" \ |
| 156 | + "--build-id=none tmpdir/pr28639a.o tmpdir/pr28639b.o" \ |
| 157 | + "" \ |
| 158 | + "" \ |
| 159 | + {dummy.s} \ |
| 160 | + {{readelf {--notes} pr28639d.rd}} \ |
| 161 | + "pr28639b" \ |
| 162 | + ] \ |
| 163 | +] |
| 164 | diff --git a/ld/testsuite/ld-elf/pr28639a.rd b/ld/testsuite/ld-elf/pr28639a.rd |
| 165 | new file mode 100644 |
| 166 | index 00000000000..e85087064d0 |
| 167 | --- /dev/null |
| 168 | +++ b/ld/testsuite/ld-elf/pr28639a.rd |
| 169 | @@ -0,0 +1,6 @@ |
| 170 | +#... |
| 171 | +Displaying notes found in: \.note\.gnu\.build-id |
| 172 | + Owner Data size Description |
| 173 | + GNU 0x00000010 NT_GNU_BUILD_ID \(unique build ID bitstring\) |
| 174 | + Build ID: [0-9a-f]+ |
| 175 | +#pass |
| 176 | diff --git a/ld/testsuite/ld-elf/pr28639b.rd b/ld/testsuite/ld-elf/pr28639b.rd |
| 177 | new file mode 100644 |
| 178 | index 00000000000..04dcb04bec2 |
| 179 | --- /dev/null |
| 180 | +++ b/ld/testsuite/ld-elf/pr28639b.rd |
| 181 | @@ -0,0 +1,6 @@ |
| 182 | +#... |
| 183 | +Displaying notes found in: \.note\.gnu\.build-id |
| 184 | + Owner Data size Description |
| 185 | + GNU 0x00000014 NT_GNU_BUILD_ID \(unique build ID bitstring\) |
| 186 | + Build ID: [0-9a-f]+ |
| 187 | +#pass |
| 188 | diff --git a/ld/testsuite/ld-elf/pr28639c.rd b/ld/testsuite/ld-elf/pr28639c.rd |
| 189 | new file mode 100644 |
| 190 | index 00000000000..64221e5fa51 |
| 191 | --- /dev/null |
| 192 | +++ b/ld/testsuite/ld-elf/pr28639c.rd |
| 193 | @@ -0,0 +1,10 @@ |
| 194 | +#failif |
| 195 | +#... |
| 196 | +Displaying notes found in: \.note\.gnu\.build-id |
| 197 | + Owner Data size Description |
| 198 | + GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\) |
| 199 | + Build ID: [0-9a-f]+ |
| 200 | + Owner Data size Description |
| 201 | + GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\) |
| 202 | + Build ID: [0-9a-f]+ |
| 203 | +#... |
| 204 | diff --git a/ld/testsuite/ld-elf/pr28639d.rd b/ld/testsuite/ld-elf/pr28639d.rd |
| 205 | new file mode 100644 |
| 206 | index 00000000000..897c8493efa |
| 207 | --- /dev/null |
| 208 | +++ b/ld/testsuite/ld-elf/pr28639d.rd |
| 209 | @@ -0,0 +1,4 @@ |
| 210 | +#failif |
| 211 | +#... |
| 212 | +Displaying notes found in: \.note\.gnu\.build-id |
| 213 | +#... |
| 214 | -- |
| 215 | |