* [PATCH] elf: Discard input .note.gnu.build-id sections
@ 2021-12-01 4:46 H.J. Lu
2021-12-01 7:23 ` Alan Modra
0 siblings, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2021-12-01 4:46 UTC (permalink / raw)
To: binutils
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] elf: Discard input .note.gnu.build-id sections
2021-12-01 4:46 [PATCH] elf: Discard input .note.gnu.build-id sections H.J. Lu
@ 2021-12-01 7:23 ` Alan Modra
2021-12-01 7:56 ` Fangrui Song
0 siblings, 1 reply; 7+ messages in thread
From: Alan Modra @ 2021-12-01 7:23 UTC (permalink / raw)
To: H.J. Lu; +Cc: binutils
On Tue, Nov 30, 2021 at 08:46:04PM -0800, H.J. Lu via Binutils wrote:
> 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.
It's simpler to mark all input .note.gnu.build-id sections with
SEC_EXCLUDE then use bfd_make_section_anyway_with_flags to create a
new section. That way you'll avoid any problems that might occur when
trying to use an existing section. For example, the size might change
with a change in build-id style, leading to section contents buffer
overflow.
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] elf: Discard input .note.gnu.build-id sections
2021-12-01 7:23 ` Alan Modra
@ 2021-12-01 7:56 ` Fangrui Song
2021-12-01 11:55 ` Alan Modra
0 siblings, 1 reply; 7+ messages in thread
From: Fangrui Song @ 2021-12-01 7:56 UTC (permalink / raw)
To: H.J. Lu; +Cc: binutils, Alan Modra
On 2021-12-01, Alan Modra via Binutils wrote:
>On Tue, Nov 30, 2021 at 08:46:04PM -0800, H.J. Lu via Binutils wrote:
>> 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.
>
>It's simpler to mark all input .note.gnu.build-id sections with
>SEC_EXCLUDE then use bfd_make_section_anyway_with_flags to create a
>new section. That way you'll avoid any problems that might occur when
>trying to use an existing section. For example, the size might change
>with a change in build-id style, leading to section contents buffer
>overflow.
For --build-id=none final link, should .note.gnu.build-id input sections
be combined? This is the currently ld.bfd (and ld.lld)'s behavior.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] elf: Discard input .note.gnu.build-id sections
2021-12-01 7:56 ` Fangrui Song
@ 2021-12-01 11:55 ` Alan Modra
2021-12-01 11:58 ` [PATCH v2] " H.J. Lu
0 siblings, 1 reply; 7+ messages in thread
From: Alan Modra @ 2021-12-01 11:55 UTC (permalink / raw)
To: Fangrui Song; +Cc: H.J. Lu, binutils
On Tue, Nov 30, 2021 at 11:56:10PM -0800, Fangrui Song wrote:
> On 2021-12-01, Alan Modra via Binutils wrote:
> > On Tue, Nov 30, 2021 at 08:46:04PM -0800, H.J. Lu via Binutils wrote:
> > > 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.
> >
> > It's simpler to mark all input .note.gnu.build-id sections with
> > SEC_EXCLUDE then use bfd_make_section_anyway_with_flags to create a
> > new section. That way you'll avoid any problems that might occur when
> > trying to use an existing section. For example, the size might change
> > with a change in build-id style, leading to section contents buffer
> > overflow.
>
> For --build-id=none final link, should .note.gnu.build-id input sections
> be combined? This is the currently ld.bfd (and ld.lld)'s behavior.
That's an interesting question. We often supply an option such as
--build-id=none in order to turn off a project-wide default for
particular files. So --build-id=none ought to behave the same as not
supplying any --build-id option. What I'm saying is that the option
--build-id=none shouldn't necessarily mean "no build id", but we could
go that way if we also dropped .note.gnu.build-id sections when no
--build-id option was given. I'm in favour of doing that.
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2] elf: Discard input .note.gnu.build-id sections
2021-12-01 11:55 ` Alan Modra
@ 2021-12-01 11:58 ` H.J. Lu
2021-12-01 12:17 ` [PATCH v3] " H.J. Lu
0 siblings, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2021-12-01 11:58 UTC (permalink / raw)
To: Alan Modra; +Cc: Fangrui Song, Binutils
[-- Attachment #1: Type: text/plain, Size: 1676 bytes --]
On Wed, Dec 1, 2021 at 3:55 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Tue, Nov 30, 2021 at 11:56:10PM -0800, Fangrui Song wrote:
> > On 2021-12-01, Alan Modra via Binutils wrote:
> > > On Tue, Nov 30, 2021 at 08:46:04PM -0800, H.J. Lu via Binutils wrote:
> > > > 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.
> > >
> > > It's simpler to mark all input .note.gnu.build-id sections with
> > > SEC_EXCLUDE then use bfd_make_section_anyway_with_flags to create a
> > > new section. That way you'll avoid any problems that might occur when
> > > trying to use an existing section. For example, the size might change
> > > with a change in build-id style, leading to section contents buffer
> > > overflow.
> >
> > For --build-id=none final link, should .note.gnu.build-id input sections
> > be combined? This is the currently ld.bfd (and ld.lld)'s behavior.
>
> That's an interesting question. We often supply an option such as
> --build-id=none in order to turn off a project-wide default for
> particular files. So --build-id=none ought to behave the same as not
> supplying any --build-id option. What I'm saying is that the option
> --build-id=none shouldn't necessarily mean "no build id", but we could
> go that way if we also dropped .note.gnu.build-id sections when no
> --build-id option was given. I'm in favour of doing that.
>
Here is the v2 patch:
1. Use bfd_make_section_anyway_with_flags to create the output
.note.gnu.build-id section.
2. Discard input .note.gnu.build-id sections.
OK for master?
Thanks.
--
H.J.
[-- Attachment #2: v2-0001-elf-Discard-input-.note.gnu.build-id-sections.patch --]
[-- Type: text/x-patch, Size: 6777 bytes --]
From 9e65503ef1511124f4e1d3517e2b7ae60629a511 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 30 Nov 2021 20:40:38 -0800
Subject: [PATCH v2] elf: Discard input .note.gnu.build-id sections
1. Discard input .note.gnu.build-id sections.
2. Clear the build ID field before writing.
3. Use bfd_make_section_anyway_with_flags to create the output
.note.gnu.build-id section.
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 bfd_make_section_anyway_with_flags
to create the output .note.gnu.build-id section.
* testsuite/ld-elf/build-id.exp: New file.
* testsuite/ld-elf/pr28639a.rd: Likewise.
* testsuite/ld-elf/pr28639b.rd: Likewise.
* testsuite/ld-elf/pr28639c.rd: Likewise.
* testsuite/ld-elf/pr28639d.rd: Likewise.
---
ld/ldelf.c | 12 ++++-
ld/testsuite/ld-elf/build-id.exp | 77 ++++++++++++++++++++++++++++++++
ld/testsuite/ld-elf/pr28639a.rd | 6 +++
ld/testsuite/ld-elf/pr28639b.rd | 6 +++
ld/testsuite/ld-elf/pr28639c.rd | 10 +++++
ld/testsuite/ld-elf/pr28639d.rd | 4 ++
6 files changed, 114 insertions(+), 1 deletion(-)
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
create mode 100644 ld/testsuite/ld-elf/pr28639c.rd
create mode 100644 ld/testsuite/ld-elf/pr28639d.rd
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 0c39eb6024f..7d90af6d477 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1043,6 +1043,12 @@ 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. */
+ s = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
+ if (s != NULL
+ && s != elf_tdata (link_info.output_bfd)->o->build_id.sec)
+ s->flags |= SEC_EXCLUDE;
+
if (abfd->xvec->flavour == bfd_target_elf_flavour
&& !bfd_input_just_syms (abfd)
&& elf_tdata (abfd) != NULL
@@ -1386,6 +1392,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);
@@ -1417,7 +1426,8 @@ ldelf_setup_build_id (bfd *ibfd)
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_make_section_anyway_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..19c22a75c4d
--- /dev/null
+++ b/ld/testsuite/ld-elf/build-id.exp
@@ -0,0 +1,77 @@
+# 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 \
+ "pr28639a" \
+ "--build-id tmpdir/pr28639a.o tmpdir/pr28639b.o" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639b.rd} \
+ {readelf {--notes} pr28639c.rd}} \
+ "pr28639a" \
+ ] \
+ [list \
+ "pr28639b" \
+ "--build-id=none tmpdir/pr28639a.o tmpdir/pr28639b.o" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639d.rd}} \
+ "pr28639b" \
+ ] \
+]
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
diff --git a/ld/testsuite/ld-elf/pr28639c.rd b/ld/testsuite/ld-elf/pr28639c.rd
new file mode 100644
index 00000000000..64221e5fa51
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639c.rd
@@ -0,0 +1,10 @@
+#failif
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+ Owner Data size Description
+ GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+ Owner Data size Description
+ GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+#...
diff --git a/ld/testsuite/ld-elf/pr28639d.rd b/ld/testsuite/ld-elf/pr28639d.rd
new file mode 100644
index 00000000000..897c8493efa
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639d.rd
@@ -0,0 +1,4 @@
+#failif
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+#...
--
2.33.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3] elf: Discard input .note.gnu.build-id sections
2021-12-01 11:58 ` [PATCH v2] " H.J. Lu
@ 2021-12-01 12:17 ` H.J. Lu
2021-12-02 0:02 ` Alan Modra
0 siblings, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2021-12-01 12:17 UTC (permalink / raw)
To: Alan Modra; +Cc: Fangrui Song, Binutils
[-- Attachment #1: Type: text/plain, Size: 1926 bytes --]
On Wed, Dec 1, 2021 at 3:58 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Dec 1, 2021 at 3:55 AM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Tue, Nov 30, 2021 at 11:56:10PM -0800, Fangrui Song wrote:
> > > On 2021-12-01, Alan Modra via Binutils wrote:
> > > > On Tue, Nov 30, 2021 at 08:46:04PM -0800, H.J. Lu via Binutils wrote:
> > > > > 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.
> > > >
> > > > It's simpler to mark all input .note.gnu.build-id sections with
> > > > SEC_EXCLUDE then use bfd_make_section_anyway_with_flags to create a
> > > > new section. That way you'll avoid any problems that might occur when
> > > > trying to use an existing section. For example, the size might change
> > > > with a change in build-id style, leading to section contents buffer
> > > > overflow.
> > >
> > > For --build-id=none final link, should .note.gnu.build-id input sections
> > > be combined? This is the currently ld.bfd (and ld.lld)'s behavior.
> >
> > That's an interesting question. We often supply an option such as
> > --build-id=none in order to turn off a project-wide default for
> > particular files. So --build-id=none ought to behave the same as not
> > supplying any --build-id option. What I'm saying is that the option
> > --build-id=none shouldn't necessarily mean "no build id", but we could
> > go that way if we also dropped .note.gnu.build-id sections when no
> > --build-id option was given. I'm in favour of doing that.
> >
>
> Here is the v2 patch:
>
> 1. Use bfd_make_section_anyway_with_flags to create the output
> .note.gnu.build-id section.
> 2. Discard input .note.gnu.build-id sections.
>
> OK for master?
>
Here is the v3 patch:
1. Use bfd_get_next_section_by_name to find input
.note.gnu.build-id sections.
OK for master?
--
H.J.
[-- Attachment #2: v3-0001-elf-Discard-input-.note.gnu.build-id-sections.patch --]
[-- Type: text/x-patch, Size: 6844 bytes --]
From bc0691b4bd7894e257ce9a6b9294aa70a830c088 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 30 Nov 2021 20:40:38 -0800
Subject: [PATCH v3] elf: Discard input .note.gnu.build-id sections
1. Discard input .note.gnu.build-id sections.
2. Clear the build ID field before writing.
3. Use bfd_make_section_anyway_with_flags to create the output
.note.gnu.build-id section.
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 bfd_make_section_anyway_with_flags
to create the output .note.gnu.build-id section.
* testsuite/ld-elf/build-id.exp: New file.
* testsuite/ld-elf/pr28639a.rd: Likewise.
* testsuite/ld-elf/pr28639b.rd: Likewise.
* testsuite/ld-elf/pr28639c.rd: Likewise.
* testsuite/ld-elf/pr28639d.rd: Likewise.
---
ld/ldelf.c | 15 ++++++-
ld/testsuite/ld-elf/build-id.exp | 77 ++++++++++++++++++++++++++++++++
ld/testsuite/ld-elf/pr28639a.rd | 6 +++
ld/testsuite/ld-elf/pr28639b.rd | 6 +++
ld/testsuite/ld-elf/pr28639c.rd | 10 +++++
ld/testsuite/ld-elf/pr28639d.rd | 4 ++
6 files changed, 117 insertions(+), 1 deletion(-)
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
create mode 100644 ld/testsuite/ld-elf/pr28639c.rd
create mode 100644 ld/testsuite/ld-elf/pr28639d.rd
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 0c39eb6024f..529992b02ae 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1043,6 +1043,15 @@ 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. */
+ s = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
+ while (s != NULL)
+ {
+ if (s != elf_tdata (link_info.output_bfd)->o->build_id.sec)
+ s->flags |= SEC_EXCLUDE;
+ s = bfd_get_next_section_by_name (NULL, s);
+ }
+
if (abfd->xvec->flavour == bfd_target_elf_flavour
&& !bfd_input_just_syms (abfd)
&& elf_tdata (abfd) != NULL
@@ -1386,6 +1395,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);
@@ -1417,7 +1429,8 @@ ldelf_setup_build_id (bfd *ibfd)
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_make_section_anyway_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..19c22a75c4d
--- /dev/null
+++ b/ld/testsuite/ld-elf/build-id.exp
@@ -0,0 +1,77 @@
+# 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 \
+ "pr28639a" \
+ "--build-id tmpdir/pr28639a.o tmpdir/pr28639b.o" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639b.rd} \
+ {readelf {--notes} pr28639c.rd}} \
+ "pr28639a" \
+ ] \
+ [list \
+ "pr28639b" \
+ "--build-id=none tmpdir/pr28639a.o tmpdir/pr28639b.o" \
+ "" \
+ "" \
+ {dummy.s} \
+ {{readelf {--notes} pr28639d.rd}} \
+ "pr28639b" \
+ ] \
+]
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
diff --git a/ld/testsuite/ld-elf/pr28639c.rd b/ld/testsuite/ld-elf/pr28639c.rd
new file mode 100644
index 00000000000..64221e5fa51
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639c.rd
@@ -0,0 +1,10 @@
+#failif
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+ Owner Data size Description
+ GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+ Owner Data size Description
+ GNU 0x[0-9a-f]+ NT_GNU_BUILD_ID \(unique build ID bitstring\)
+ Build ID: [0-9a-f]+
+#...
diff --git a/ld/testsuite/ld-elf/pr28639d.rd b/ld/testsuite/ld-elf/pr28639d.rd
new file mode 100644
index 00000000000..897c8493efa
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr28639d.rd
@@ -0,0 +1,4 @@
+#failif
+#...
+Displaying notes found in: \.note\.gnu\.build-id
+#...
--
2.33.1
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2021-12-02 0:02 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-01 4:46 [PATCH] elf: Discard input .note.gnu.build-id sections H.J. Lu
2021-12-01 7:23 ` Alan Modra
2021-12-01 7:56 ` Fangrui Song
2021-12-01 11:55 ` Alan Modra
2021-12-01 11:58 ` [PATCH v2] " H.J. Lu
2021-12-01 12:17 ` [PATCH v3] " H.J. Lu
2021-12-02 0:02 ` Alan Modra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).