[PATCH] elf: Discard input .note.gnu.build-id sections
H.J. Lu
hjl.tools@gmail.com
Wed Dec 1 04:46:04 GMT 2021
1. Discard input .note.gnu.build-id , excluding the first one.
2. Clear the build ID field before writing.
3. Use the input .note.gnu.build-id section if available.
PR ld/28639
* ldelf.c (ldelf_after_open): Discard input .note.gnu.build-id
sections, excluding the first one.
(write_build_id): Clear the build ID field before writing.
(ldelf_setup_build_id): Use the input .note.gnu.build-id section
if available.
* testsuite/ld-elf/build-id.exp: New file.
* testsuite/ld-elf/pr28639a.rd: Likewise.
* testsuite/ld-elf/pr28639b.rd: Likewise.
---
ld/ldelf.c | 23 +++++++++--
ld/testsuite/ld-elf/build-id.exp | 67 ++++++++++++++++++++++++++++++++
ld/testsuite/ld-elf/pr28639a.rd | 6 +++
ld/testsuite/ld-elf/pr28639b.rd | 6 +++
4 files changed, 99 insertions(+), 3 deletions(-)
create mode 100644 ld/testsuite/ld-elf/build-id.exp
create mode 100644 ld/testsuite/ld-elf/pr28639a.rd
create mode 100644 ld/testsuite/ld-elf/pr28639b.rd
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 0c39eb6024f..f77547b3e82 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -997,6 +997,7 @@ ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
asection *s;
bfd *abfd;
bfd **save_input_bfd_tail;
+ bfd *abfd_build_id = NULL;
after_open_default ();
@@ -1036,6 +1037,7 @@ ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
free ((char *) ldelf_emit_note_gnu_build_id);
ldelf_emit_note_gnu_build_id = NULL;
}
+ abfd_build_id = abfd;
}
get_elf_backend_data (link_info.output_bfd)->setup_gnu_properties (&link_info);
@@ -1043,6 +1045,14 @@ ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
/* Do not allow executable files to be used as inputs to the link. */
for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
{
+ /* Discard input .note.gnu.build-id sections. */
+ if (abfd != abfd_build_id)
+ {
+ s = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
+ if (s != NULL)
+ s->flags |= SEC_EXCLUDE;
+ }
+
if (abfd->xvec->flavour == bfd_target_elf_flavour
&& !bfd_input_just_syms (abfd)
&& elf_tdata (abfd) != NULL
@@ -1386,6 +1396,9 @@ write_build_id (bfd *abfd)
id_bits = contents + size;
size = asec->size - size;
+ /* Clear the build ID field. */
+ memset (id_bits, 0, size);
+
bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
bfd_h_put_32 (abfd, size, &e_note->descsz);
bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
@@ -1415,9 +1428,13 @@ ldelf_setup_build_id (bfd *ibfd)
return false;
}
- flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
- | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
- s = bfd_make_section_with_flags (ibfd, ".note.gnu.build-id", flags);
+ s = bfd_get_section_by_name (ibfd, ".note.gnu.build-id");
+ if (s == NULL)
+ {
+ flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
+ | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
+ s = bfd_make_section_with_flags (ibfd, ".note.gnu.build-id", flags);
+ }
if (s != NULL && bfd_set_section_alignment (s, 2))
{
struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
diff --git a/ld/testsuite/ld-elf/build-id.exp b/ld/testsuite/ld-elf/build-id.exp
new file mode 100644
index 00000000000..2658d36863a
--- /dev/null
+++ b/ld/testsuite/ld-elf/build-id.exp
@@ -0,0 +1,67 @@
+# Expect script for --build-id tests.
+# Copyright (C) 2021 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+# Exclude non-ELF targets.
+
+if ![is_elf_format] {
+ return
+}
+
+if { [istarget frv-*-*] || [istarget lm32-*-*] } {
+ return
+}
+
+if { !([istarget *-*-linux*]
+ || [istarget arm*-*-uclinuxfdpiceabi]
+ || [istarget *-*-nacl*]
+ || [istarget *-*-gnu*]) } then {
+ return
+}
+
+run_ld_link_tests [list \
+ [list \
+ "pr28639a.o" \
+ "-r --build-id=md5" \
+ "" \
+ "" \
+ {start.s} \
+ {{readelf {--notes} pr28639a.rd}} \
+ "pr28639a.o" \
+ ] \
+ [list \
+ "pr28639a.o" \
+ "-r --build-id" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639b.rd}} \
+ "pr28639b.o" \
+ ] \
+ [list \
+ "pr28639" \
+ "--build-id tmpdir/pr28639a.o tmpdir/pr28639b.o" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639b.rd}} \
+ "pr28639b.o" \
+ ] \
+]
diff --git a/ld/testsuite/ld-elf/pr28639a.rd b/ld/testsuite/ld-elf/pr28639a.rd
new file mode 100644
index 00000000000..e85087064d0
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639a.rd
@@ -0,0 +1,6 @@
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+ Owner Data size Description
+ GNU 0x00000010 NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+#pass
diff --git a/ld/testsuite/ld-elf/pr28639b.rd b/ld/testsuite/ld-elf/pr28639b.rd
new file mode 100644
index 00000000000..04dcb04bec2
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639b.rd
@@ -0,0 +1,6 @@
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+ Owner Data size Description
+ GNU 0x00000014 NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+#pass
--
2.33.1
More information about the Binutils
mailing list