ld: Ignore .note.gnu.build-id when placing orphaned notes

Message ID CAMe9rOoDTJCYV=6MBRgrT2OdExpvHD+peVoqUNZXfrF3bO0MiQ@mail.gmail.com
State New
Headers
Series ld: Ignore .note.gnu.build-id when placing orphaned notes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

H.J. Lu Sept. 20, 2024, 9:25 p.m. UTC
  The commits:

e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code'
from generating two read-only segments.
bf6d7087de0 ld: Move the .note.build-id section to near the start of the memory
map

place .note.gnu.build-id before text sections when --rosegment is used.
Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
-z separate-code are used together to avoid putting any note sections
between .note.gnu.build-id and text sections in the same PT_LOAD segment.

PR ld/32191
* ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
placing orphaned notes.
* testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
* testsuite/ld-elf/pr23658-1c.d: Likewise.
* testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
* testsuite/ld-i386/pr32191.d: New file.
* testsuite/ld-x86-64/lam-u48.rd: Updated.
* testsuite/ld-x86-64/lam-u57.rd: Likewise.
* testsuite/ld-x86-64/pr32191-x32.d: New file.
* testsuite/ld-x86-64/pr32191.d: Likewise.
* testsuite/ld-x86-64/pr32191.s: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.
  

Comments

H.J. Lu Sept. 26, 2024, 12:35 a.m. UTC | #1
On Sat, Sep 21, 2024, 5:25 AM H.J. Lu <hjl.tools@gmail.com> wrote:

> The commits:
>
> e8e10743f7b Add --rosegment option to BFD linker to stop the '-z
> separate-code'
> from generating two read-only segments.
> bf6d7087de0 ld: Move the .note.build-id section to near the start of the
> memory
> map
>
> place .note.gnu.build-id before text sections when --rosegment is used.
> Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
> -z separate-code are used together to avoid putting any note sections
> between .note.gnu.build-id and text sections in the same PT_LOAD segment.
>
> PR ld/32191
> * ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
> placing orphaned notes.
> * testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
> * testsuite/ld-elf/pr23658-1c.d: Likewise.
> * testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
> * testsuite/ld-i386/pr32191.d: New file.
> * testsuite/ld-x86-64/lam-u48.rd: Updated.
> * testsuite/ld-x86-64/lam-u57.rd: Likewise.
> * testsuite/ld-x86-64/pr32191-x32.d: New file.
> * testsuite/ld-x86-64/pr32191.d: Likewise.
> * testsuite/ld-x86-64/pr32191.s: Likewise.
> * testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.
>

PING.


>
> --
> H.J.
>
  
Nick Clifton Sept. 26, 2024, 11:17 a.m. UTC | #2
On 9/20/24 22:25, H.J. Lu wrote:
> The commits:
> 
> e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code'
> from generating two read-only segments.
> bf6d7087de0 ld: Move the .note.build-id section to near the start of the memory
> map
> 
> place .note.gnu.build-id before text sections when --rosegment is used.
> Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
> -z separate-code are used together to avoid putting any note sections
> between .note.gnu.build-id and text sections in the same PT_LOAD segment.
> 
> PR ld/32191
> * ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
> placing orphaned notes.
> * testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
> * testsuite/ld-elf/pr23658-1c.d: Likewise.
> * testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
> * testsuite/ld-i386/pr32191.d: New file.
> * testsuite/ld-x86-64/lam-u48.rd: Updated.
> * testsuite/ld-x86-64/lam-u57.rd: Likewise.
> * testsuite/ld-x86-64/pr32191-x32.d: New file.
> * testsuite/ld-x86-64/pr32191.d: Likewise.
> * testsuite/ld-x86-64/pr32191.s: Likewise.
> * testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.

Patch approved - please commit.

Cheers
   Nick
  
Alan Modra Sept. 26, 2024, 12:52 p.m. UTC | #3
arc-elf  +FAIL: ld-elf/pr23658-1e
arc-linux-uclibc  +FAIL: ld-elf/pr23658-1e
arm-nacl  +FAIL: ld-elf/pr23658-1e
avr-elf  +FAIL: ld-elf/pr23658-1e
cris-elf  +FAIL: ld-elf/pr23658-1e
csky-elf  +FAIL: ld-elf/pr23658-1e
d10v-elf  +FAIL: ld-elf/pr23658-1e
epiphany-elf  +FAIL: ld-elf/pr23658-1e
h8300-elf  +FAIL: ld-elf/pr23658-1e
h8300-linux-gnu  +FAIL: ld-elf/pr23658-1e
hppa64-hp-hpux11.23  +FAIL: ld-elf/pr23658-1e
ip2k-elf  +FAIL: ld-elf/pr23658-1e
lm32-elf  +FAIL: ld-elf/pr23658-1e
loongarch32-elf  +FAIL: ld-elf/pr23658-1e
m32c-elf  +FAIL: ld-elf/pr23658-1e
m32r-elf  +FAIL: ld-elf/pr23658-1e
m68hc11-elf  +FAIL: ld-elf/pr23658-1e
mcore-elf  +FAIL: ld-elf/pr23658-1e
mep-elf  +FAIL: ld-elf/pr23658-1e
microblaze-elf  +FAIL: ld-elf/pr23658-1e
moxie-elf  +FAIL: ld-elf/pr23658-1e
nds32be-elf  +FAIL: ld-elf/pr23658-1e
nds32le-linux-gnu  +FAIL: ld-elf/pr23658-1e
or1k-elf  +FAIL: ld-elf/pr23658-1e
riscv32-elf  +FAIL: ld-elf/pr23658-1e
rl78-elf  +FAIL: ld-elf/pr23658-1e
rx-elf  +FAIL: ld-elf/pr23658-1e
spu-elf  +FAIL: ld-elf/pr23658-1e
v850-elf  +FAIL: ld-elf/pr23658-1e
xstormy16-elf  +FAIL: ld-elf/pr23658-1e
xtensa-lx106-elf  +FAIL: ld-elf/pr23658-1e
z80-elf  +FAIL: ld-elf/pr23658-1e
  
H.J. Lu Sept. 26, 2024, 11:06 p.m. UTC | #4
On Thu, Sep 26, 2024 at 8:52 PM Alan Modra <amodra@gmail.com> wrote:
>
> arc-elf  +FAIL: ld-elf/pr23658-1e
> arc-linux-uclibc  +FAIL: ld-elf/pr23658-1e
> arm-nacl  +FAIL: ld-elf/pr23658-1e
> avr-elf  +FAIL: ld-elf/pr23658-1e
> cris-elf  +FAIL: ld-elf/pr23658-1e
> csky-elf  +FAIL: ld-elf/pr23658-1e
> d10v-elf  +FAIL: ld-elf/pr23658-1e
> epiphany-elf  +FAIL: ld-elf/pr23658-1e
> h8300-elf  +FAIL: ld-elf/pr23658-1e
> h8300-linux-gnu  +FAIL: ld-elf/pr23658-1e
> hppa64-hp-hpux11.23  +FAIL: ld-elf/pr23658-1e
> ip2k-elf  +FAIL: ld-elf/pr23658-1e
> lm32-elf  +FAIL: ld-elf/pr23658-1e
> loongarch32-elf  +FAIL: ld-elf/pr23658-1e
> m32c-elf  +FAIL: ld-elf/pr23658-1e
> m32r-elf  +FAIL: ld-elf/pr23658-1e
> m68hc11-elf  +FAIL: ld-elf/pr23658-1e
> mcore-elf  +FAIL: ld-elf/pr23658-1e
> mep-elf  +FAIL: ld-elf/pr23658-1e
> microblaze-elf  +FAIL: ld-elf/pr23658-1e
> moxie-elf  +FAIL: ld-elf/pr23658-1e
> nds32be-elf  +FAIL: ld-elf/pr23658-1e
> nds32le-linux-gnu  +FAIL: ld-elf/pr23658-1e
> or1k-elf  +FAIL: ld-elf/pr23658-1e
> riscv32-elf  +FAIL: ld-elf/pr23658-1e
> rl78-elf  +FAIL: ld-elf/pr23658-1e
> rx-elf  +FAIL: ld-elf/pr23658-1e
> spu-elf  +FAIL: ld-elf/pr23658-1e
> v850-elf  +FAIL: ld-elf/pr23658-1e
> xstormy16-elf  +FAIL: ld-elf/pr23658-1e
> xtensa-lx106-elf  +FAIL: ld-elf/pr23658-1e
> z80-elf  +FAIL: ld-elf/pr23658-1e
>
> --
> Alan Modra

Here is the patch.
  
Alan Modra Sept. 27, 2024, 12:04 a.m. UTC | #5
On Fri, Sep 27, 2024 at 07:06:30AM +0800, H.J. Lu wrote:
> Since -z separate-code is only supported with shared library and orphaned

Is there a reason why we don't support "-z separate-code", "-z common",
and "-z text" unless targets also support shared libraries?

It seems to me that there is a misplaced "fi" in elf.em.
  
H.J. Lu Sept. 27, 2024, 12:06 a.m. UTC | #6
On Fri, Sep 27, 2024, 8:04 AM Alan Modra <amodra@gmail.com> wrote:

> On Fri, Sep 27, 2024 at 07:06:30AM +0800, H.J. Lu wrote:
> > Since -z separate-code is only supported with shared library and orphaned
>
> Is there a reason why we don't support "-z separate-code", "-z common",
> and "-z text" unless targets also support shared libraries?
>
> It seems to me that there is a misplaced "fi" in elf.em.
>

Good question.  But I don't have an answer for this.


> --
> Alan Modra
>
>
  
Alan Modra Sept. 27, 2024, 12:33 a.m. UTC | #7
On Fri, Sep 27, 2024 at 08:06:31AM +0800, H.J. Lu wrote:
> On Fri, Sep 27, 2024, 8:04 AM Alan Modra <amodra@gmail.com> wrote:
> 
> > On Fri, Sep 27, 2024 at 07:06:30AM +0800, H.J. Lu wrote:
> > > Since -z separate-code is only supported with shared library and orphaned
> >
> > Is there a reason why we don't support "-z separate-code", "-z common",
> > and "-z text" unless targets also support shared libraries?
> >
> > It seems to me that there is a misplaced "fi" in elf.em.
> >
> 
> Good question.  But I don't have an answer for this.

Well, it seems like the required scripts are generated, so..

From 428f3561bc16dfc2944ee641201acdd166315aa2 Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Fri, 27 Sep 2024 09:53:10 +0930
Subject: Enable -z separate-code, -z common and -z text for more targets

Fix a mis-placed "fi".

diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index 863657e12f5..2e865728587 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -1084,6 +1084,7 @@ fragment <<EOF
 	link_info.relro = false;
 EOF
 fi
+fi
 fragment <<EOF
       else if (strcmp (optarg, "separate-code") == 0)
 	link_info.separate_code = true;
@@ -1100,7 +1101,6 @@ fragment <<EOF
       else if (strcmp (optarg, "textoff") == 0)
 	link_info.textrel_check = textrel_check_none;
 EOF
-fi
 
 if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
 fragment <<EOF
diff --git a/ld/testsuite/ld-elf/pr22393-1e.d b/ld/testsuite/ld-elf/pr22393-1e.d
index 51d74fa1381..55f7e9a6e4e 100644
--- a/ld/testsuite/ld-elf/pr22393-1e.d
+++ b/ld/testsuite/ld-elf/pr22393-1e.d
@@ -2,7 +2,7 @@
 #ld: -z separate-code
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
-#xfail: h8300-*-* rx-*-linux*
+#xfail: rx-*-linux*
 
 #failif
 #...
diff --git a/ld/testsuite/ld-elf/pr22393-1f.d b/ld/testsuite/ld-elf/pr22393-1f.d
index 2a44955d0f1..bc61b639658 100644
--- a/ld/testsuite/ld-elf/pr22393-1f.d
+++ b/ld/testsuite/ld-elf/pr22393-1f.d
@@ -2,7 +2,7 @@
 #ld: -z separate-code
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
-#xfail: h8300-*-* rx-*-linux*
+#xfail: rx-*-linux*
 
 #failif
 #...
diff --git a/ld/testsuite/ld-elf/pr30508.d b/ld/testsuite/ld-elf/pr30508.d
index 325ff40f562..16927809030 100644
--- a/ld/testsuite/ld-elf/pr30508.d
+++ b/ld/testsuite/ld-elf/pr30508.d
@@ -2,7 +2,7 @@
 #objcopy_linked_file: -R .foo
 #readelf: -lW
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
-#xfail: h8300-*-* mips*-*-* rx-*-linux*
+#xfail: mips*-*-* rx-*-linux*
 
 #...
  Section to Segment mapping:
  
H.J. Lu Sept. 27, 2024, 4:26 a.m. UTC | #8
On Fri, Sep 27, 2024 at 8:33 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Sep 27, 2024 at 08:06:31AM +0800, H.J. Lu wrote:
> > On Fri, Sep 27, 2024, 8:04 AM Alan Modra <amodra@gmail.com> wrote:
> >
> > > On Fri, Sep 27, 2024 at 07:06:30AM +0800, H.J. Lu wrote:
> > > > Since -z separate-code is only supported with shared library and orphaned
> > >
> > > Is there a reason why we don't support "-z separate-code", "-z common",
> > > and "-z text" unless targets also support shared libraries?
> > >
> > > It seems to me that there is a misplaced "fi" in elf.em.
> > >
> >
> > Good question.  But I don't have an answer for this.
>
> Well, it seems like the required scripts are generated, so..
>
> From 428f3561bc16dfc2944ee641201acdd166315aa2 Mon Sep 17 00:00:00 2001
> From: Alan Modra <amodra@gmail.com>
> Date: Fri, 27 Sep 2024 09:53:10 +0930
> Subject: Enable -z separate-code, -z common and -z text for more targets
>
> Fix a mis-placed "fi".
>
> diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
> index 863657e12f5..2e865728587 100644
> --- a/ld/emultempl/elf.em
> +++ b/ld/emultempl/elf.em
> @@ -1084,6 +1084,7 @@ fragment <<EOF
>         link_info.relro = false;
>  EOF
>  fi
> +fi
>  fragment <<EOF
>        else if (strcmp (optarg, "separate-code") == 0)
>         link_info.separate_code = true;
> @@ -1100,7 +1101,6 @@ fragment <<EOF
>        else if (strcmp (optarg, "textoff") == 0)
>         link_info.textrel_check = textrel_check_none;
>  EOF
> -fi
>
>  if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
>  fragment <<EOF

I don't think -z separate-code works ASAS for targets without shared
library support.  On cris-elf, ld-elf/pr23658-1e passes.  But ld-elf/pr23658-1e
generates:

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.build-id .text .note.4 .note.1 .note.2 .note.3
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is wrong for -z separate-code.  This may be another
symptom for PR ld/32191.

   01     .note.gnu.build-id
   02     .note.4 .note.1
   03     .note.2 .note.3


> diff --git a/ld/testsuite/ld-elf/pr22393-1e.d b/ld/testsuite/ld-elf/pr22393-1e.d
> index 51d74fa1381..55f7e9a6e4e 100644
> --- a/ld/testsuite/ld-elf/pr22393-1e.d
> +++ b/ld/testsuite/ld-elf/pr22393-1e.d
> @@ -2,7 +2,7 @@
>  #ld: -z separate-code
>  #readelf: -l --wide
>  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> -#xfail: h8300-*-* rx-*-linux*
> +#xfail: rx-*-linux*
>
>  #failif
>  #...
> diff --git a/ld/testsuite/ld-elf/pr22393-1f.d b/ld/testsuite/ld-elf/pr22393-1f.d
> index 2a44955d0f1..bc61b639658 100644
> --- a/ld/testsuite/ld-elf/pr22393-1f.d
> +++ b/ld/testsuite/ld-elf/pr22393-1f.d
> @@ -2,7 +2,7 @@
>  #ld: -z separate-code
>  #readelf: -l --wide
>  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> -#xfail: h8300-*-* rx-*-linux*
> +#xfail: rx-*-linux*
>
>  #failif
>  #...
> diff --git a/ld/testsuite/ld-elf/pr30508.d b/ld/testsuite/ld-elf/pr30508.d
> index 325ff40f562..16927809030 100644
> --- a/ld/testsuite/ld-elf/pr30508.d
> +++ b/ld/testsuite/ld-elf/pr30508.d
> @@ -2,7 +2,7 @@
>  #objcopy_linked_file: -R .foo
>  #readelf: -lW
>  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> -#xfail: h8300-*-* mips*-*-* rx-*-linux*
> +#xfail: mips*-*-* rx-*-linux*
>
>  #...
>   Section to Segment mapping:
>
>
> --
> Alan Modra
  
H.J. Lu Sept. 27, 2024, 5:33 a.m. UTC | #9
On Fri, Sep 27, 2024 at 12:26 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Fri, Sep 27, 2024 at 8:33 AM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Fri, Sep 27, 2024 at 08:06:31AM +0800, H.J. Lu wrote:
> > > On Fri, Sep 27, 2024, 8:04 AM Alan Modra <amodra@gmail.com> wrote:
> > >
> > > > On Fri, Sep 27, 2024 at 07:06:30AM +0800, H.J. Lu wrote:
> > > > > Since -z separate-code is only supported with shared library and orphaned
> > > >
> > > > Is there a reason why we don't support "-z separate-code", "-z common",
> > > > and "-z text" unless targets also support shared libraries?
> > > >
> > > > It seems to me that there is a misplaced "fi" in elf.em.
> > > >
> > >
> > > Good question.  But I don't have an answer for this.
> >
> > Well, it seems like the required scripts are generated, so..
> >
> > From 428f3561bc16dfc2944ee641201acdd166315aa2 Mon Sep 17 00:00:00 2001
> > From: Alan Modra <amodra@gmail.com>
> > Date: Fri, 27 Sep 2024 09:53:10 +0930
> > Subject: Enable -z separate-code, -z common and -z text for more targets
> >
> > Fix a mis-placed "fi".
> >
> > diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
> > index 863657e12f5..2e865728587 100644
> > --- a/ld/emultempl/elf.em
> > +++ b/ld/emultempl/elf.em
> > @@ -1084,6 +1084,7 @@ fragment <<EOF
> >         link_info.relro = false;
> >  EOF
> >  fi
> > +fi
> >  fragment <<EOF
> >        else if (strcmp (optarg, "separate-code") == 0)
> >         link_info.separate_code = true;
> > @@ -1100,7 +1101,6 @@ fragment <<EOF
> >        else if (strcmp (optarg, "textoff") == 0)
> >         link_info.textrel_check = textrel_check_none;
> >  EOF
> > -fi
> >
> >  if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
> >  fragment <<EOF
>
> I don't think -z separate-code works ASAS for targets without shared
> library support.  On cris-elf, ld-elf/pr23658-1e passes.  But ld-elf/pr23658-1e
> generates:
>
>  Section to Segment mapping:
>   Segment Sections...
>    00     .note.gnu.build-id .text .note.4 .note.1 .note.2 .note.3
>                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> This is wrong for -z separate-code.  This may be another
> symptom for PR ld/32191.
>
>    01     .note.gnu.build-id
>    02     .note.4 .note.1
>    03     .note.2 .note.3
>
>
> > diff --git a/ld/testsuite/ld-elf/pr22393-1e.d b/ld/testsuite/ld-elf/pr22393-1e.d
> > index 51d74fa1381..55f7e9a6e4e 100644
> > --- a/ld/testsuite/ld-elf/pr22393-1e.d
> > +++ b/ld/testsuite/ld-elf/pr22393-1e.d
> > @@ -2,7 +2,7 @@
> >  #ld: -z separate-code
> >  #readelf: -l --wide
> >  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> > -#xfail: h8300-*-* rx-*-linux*
> > +#xfail: rx-*-linux*
> >
> >  #failif
> >  #...
> > diff --git a/ld/testsuite/ld-elf/pr22393-1f.d b/ld/testsuite/ld-elf/pr22393-1f.d
> > index 2a44955d0f1..bc61b639658 100644
> > --- a/ld/testsuite/ld-elf/pr22393-1f.d
> > +++ b/ld/testsuite/ld-elf/pr22393-1f.d
> > @@ -2,7 +2,7 @@
> >  #ld: -z separate-code
> >  #readelf: -l --wide
> >  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> > -#xfail: h8300-*-* rx-*-linux*
> > +#xfail: rx-*-linux*
> >
> >  #failif
> >  #...
> > diff --git a/ld/testsuite/ld-elf/pr30508.d b/ld/testsuite/ld-elf/pr30508.d
> > index 325ff40f562..16927809030 100644
> > --- a/ld/testsuite/ld-elf/pr30508.d
> > +++ b/ld/testsuite/ld-elf/pr30508.d
> > @@ -2,7 +2,7 @@
> >  #objcopy_linked_file: -R .foo
> >  #readelf: -lW
> >  #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
> > -#xfail: h8300-*-* mips*-*-* rx-*-linux*
> > +#xfail: mips*-*-* rx-*-linux*
> >
> >  #...
> >   Section to Segment mapping:
> >
> >
> > --
> > Alan Modra
>

Since orphaned note section placement doesn't work for targets without
.interp in linker script:

https://sourceware.org/bugzilla/show_bug.cgi?id=32219

and -z separate-code doesn't work correctly for all ELF targets,

limit testsuite/ld-elf/pr23658-1e.d to the known working targets.
  
Alan Modra Sept. 28, 2024, 6:44 a.m. UTC | #10
On Fri, Sep 27, 2024 at 01:33:19PM +0800, H.J. Lu wrote:
> Since orphaned note section placement doesn't work for targets without
> .interp in linker script:
> 
> https://sourceware.org/bugzilla/show_bug.cgi?id=32219
> 
> and -z separate-code doesn't work correctly for all ELF targets,
> 
> limit testsuite/ld-elf/pr23658-1e.d to the known working targets.

Fine by me.
  
H.J. Lu Sept. 28, 2024, 7:46 a.m. UTC | #11
On Sat, Sep 28, 2024 at 2:44 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Sep 27, 2024 at 01:33:19PM +0800, H.J. Lu wrote:
> > Since orphaned note section placement doesn't work for targets without
> > .interp in linker script:
> >
> > https://sourceware.org/bugzilla/show_bug.cgi?id=32219
> >
> > and -z separate-code doesn't work correctly for all ELF targets,
> >
> > limit testsuite/ld-elf/pr23658-1e.d to the known working targets.
>
> Fine by me.
>

Nick is woring on a fix for PR 32219.  I will wait for his patch.
Hopefully his patch will fix most, if not all, failures.

H.J.
  

Patch

From 07ffe0601da8ef38cbf64acccaee0dd143d7d5da Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 20 Sep 2024 11:32:26 +0800
Subject: [PATCH] ld: Ignore .note.gnu.build-id when placing orphaned notes

The commits:

e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code' from generating two read-only segments.
bf6d7087de0 ld: Move the .note.build-id section to near the start of the memory map

place .note.gnu.build-id before text sections when --rosegment is used.
Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
-z separate-code are used together to avoid putting any note sections
between .note.gnu.build-id and text sections in the same PT_LOAD segment.

	PR ld/32191
	* ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
	placing orphaned notes.
	* testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
	* testsuite/ld-elf/pr23658-1c.d: Likewise.
	* testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
	* testsuite/ld-i386/pr32191.d: New file.
	* testsuite/ld-x86-64/lam-u48.rd: Updated.
	* testsuite/ld-x86-64/lam-u57.rd: Likewise.
	* testsuite/ld-x86-64/pr32191-x32.d: New file.
	* testsuite/ld-x86-64/pr32191.d: Likewise.
	* testsuite/ld-x86-64/pr32191.s: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/ldlang.c                          | 14 +++++++++++++-
 ld/testsuite/ld-elf/pr23658-1a.d     |  2 +-
 ld/testsuite/ld-elf/pr23658-1c.d     |  2 +-
 ld/testsuite/ld-i386/i386.exp        |  1 +
 ld/testsuite/ld-i386/pr32191.d       |  9 +++++++++
 ld/testsuite/ld-x86-64/lam-u48.rd    |  1 +
 ld/testsuite/ld-x86-64/lam-u57.rd    |  1 +
 ld/testsuite/ld-x86-64/pr32191-x32.d |  9 +++++++++
 ld/testsuite/ld-x86-64/pr32191.d     |  9 +++++++++
 ld/testsuite/ld-x86-64/pr32191.s     |  5 +++++
 ld/testsuite/ld-x86-64/x86-64.exp    |  2 ++
 11 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-i386/pr32191.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191-x32.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191.s

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 9e8cc224f4d..95d64fd05e2 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -2047,13 +2047,25 @@  lang_insert_orphan (asection *s,
 		 place orphan note section after non-note sections.  */
 
 	      first_orphan_note = NULL;
+
+	      /* NB: When --rosegment is used, the .note.gnu.build-id
+		 section is placed before text sections.  Ignore the
+		 .note.gnu.build-id section if -z separate-code and
+		 --rosegment are used together to avoid putting any
+		 note sections between the .note.gnu.build-id section
+		 and text sections in the same PT_LOAD segment.  */
+	      bool ignore_build_id = (link_info.separate_code
+				      && link_info.one_rosegment);
+
 	      for (sec = link_info.output_bfd->sections;
 		   (sec != NULL
 		    && !bfd_is_abs_section (sec));
 		   sec = sec->next)
 		if (sec != snew
 		    && elf_section_type (sec) == SHT_NOTE
-		    && (sec->flags & SEC_LOAD) != 0)
+		    && (sec->flags & SEC_LOAD) != 0
+		    && (!ignore_build_id
+			|| strcmp (sec->name, ".note.gnu.build-id") != 0))
 		  {
 		    if (!first_orphan_note)
 		      first_orphan_note = sec;
diff --git a/ld/testsuite/ld-elf/pr23658-1a.d b/ld/testsuite/ld-elf/pr23658-1a.d
index 10c6ef3973b..388f7a42fa7 100644
--- a/ld/testsuite/ld-elf/pr23658-1a.d
+++ b/ld/testsuite/ld-elf/pr23658-1a.d
@@ -3,7 +3,7 @@ 
 #source: pr23658-1c.s
 #source: pr23658-1d.s
 #source: start.s
-#ld: --build-id
+#ld: --build-id --no-rosegment
 #readelf: -l --wide
 # Since generic linker targets don't place SHT_NOTE sections as orphan,
 # SHT_NOTE sections aren't grouped nor sorted.
diff --git a/ld/testsuite/ld-elf/pr23658-1c.d b/ld/testsuite/ld-elf/pr23658-1c.d
index 87acecaabe1..6ab72dc60b3 100644
--- a/ld/testsuite/ld-elf/pr23658-1c.d
+++ b/ld/testsuite/ld-elf/pr23658-1c.d
@@ -3,7 +3,7 @@ 
 #source: pr23658-1c.s
 #source: pr23658-1d.s
 #source: start.s
-#ld: --build-id -shared
+#ld: --build-id --no-rosegment -shared
 #readelf: -l --wide
 #target: *-*-linux* *-*-gnu* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index a66d67a3244..90293226954 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -514,6 +514,7 @@  run_dump_test "pr28870"
 run_dump_test "pr28894"
 run_dump_test "pr30787"
 run_dump_test "pr31047"
+run_dump_test "pr32191"
 
 if { !([istarget "i?86-*-linux*"]
        || [istarget "i?86-*-gnu*"]
diff --git a/ld/testsuite/ld-i386/pr32191.d b/ld/testsuite/ld-i386/pr32191.d
new file mode 100644
index 00000000000..8d7838a1311
--- /dev/null
+++ b/ld/testsuite/ld-i386/pr32191.d
@@ -0,0 +1,9 @@ 
+#source: ../ld-x86-64/pr32191.s
+#as: --32 -mx86-used-note=yes
+#ld: -shared -m elf_i386 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.note\.gnu\.build-id \.text 
+ +[0-9]+ +\..* \.note\.gnu\.property .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/lam-u48.rd b/ld/testsuite/ld-x86-64/lam-u48.rd
index ad312627059..8fac904c064 100644
--- a/ld/testsuite/ld-x86-64/lam-u48.rd
+++ b/ld/testsuite/ld-x86-64/lam-u48.rd
@@ -1,3 +1,4 @@ 
+#...
 Displaying notes found in: .note.gnu.property
 [ 	]+Owner[ 	]+Data size[ 	]+Description
   GNU                  0x[0-9a-f]+	NT_GNU_PROPERTY_TYPE_0
diff --git a/ld/testsuite/ld-x86-64/lam-u57.rd b/ld/testsuite/ld-x86-64/lam-u57.rd
index 8b77e6311c0..dd8d1dafbeb 100644
--- a/ld/testsuite/ld-x86-64/lam-u57.rd
+++ b/ld/testsuite/ld-x86-64/lam-u57.rd
@@ -1,3 +1,4 @@ 
+#...
 Displaying notes found in: .note.gnu.property
 [ 	]+Owner[ 	]+Data size[ 	]+Description
   GNU                  0x[0-9a-f]+	NT_GNU_PROPERTY_TYPE_0
diff --git a/ld/testsuite/ld-x86-64/pr32191-x32.d b/ld/testsuite/ld-x86-64/pr32191-x32.d
new file mode 100644
index 00000000000..19e06a26c11
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191-x32.d
@@ -0,0 +1,9 @@ 
+#source: pr32191.s
+#as: --x32 -mx86-used-note=yes
+#ld: -shared -m elf32_x86_64 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.note\.gnu\.build-id \.text 
+ +[0-9]+ +\..* \.note\.gnu\.property .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr32191.d b/ld/testsuite/ld-x86-64/pr32191.d
new file mode 100644
index 00000000000..9038ccd9b35
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191.d
@@ -0,0 +1,9 @@ 
+#source: pr32191.s
+#as: --64 -mx86-used-note=yes
+#ld: -shared -m elf_x86_64 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.note\.gnu\.build-id \.text 
+ +[0-9]+ +\..* \.note\.gnu\.property .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr32191.s b/ld/testsuite/ld-x86-64/pr32191.s
new file mode 100644
index 00000000000..953f0de670f
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191.s
@@ -0,0 +1,5 @@ 
+	.text
+	.global foo
+foo:
+	.nops 4
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index cf78fffe29f..e9b50b6cf8c 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -541,6 +541,8 @@  run_dump_test "tlsie5"
 run_dump_test "tlsdesc3"
 run_dump_test "tlsdesc4"
 run_dump_test "tlsdesc5"
+run_dump_test "pr32191"
+run_dump_test "pr32191-x32"
 
 if { ![skip_sframe_tests] } {
     run_dump_test "sframe-simple-1"
-- 
2.46.1