Bug in linker script file matching after b1eecf6f66a4a642 (prefix tree)

2023-06-25 Thread Andrew Oates
Hi,

Apologies for the generic subject, I don't totally understand the
nature of the bug.

I have a basic linker script that stopped working after upgrading to
binutils 2.40.  I did a git bisect on the repository and it seems that
the breakage starts with commit
b1eecf6f66a4a642f4cb35688213e6c1c1ffdc79.

My simplified test case is as follows:

#
$ cat test1.c
int func3(void);

int func1(void) { return func3(); }

$ cat test2.c
int func1(void);

int func2(void) { return func1(); }

$ (127) cat test3.c
int func3(void) { return 0; }

$ cat linker.ld
ENTRY (func2)

SECTIONS
{
  . = 0x1000;
  .init-text ALIGN(4) :
  {
lib1.a(*)
  }

  . = 0xc000;

  .text ALIGN(4) :
  {
*(.text)
  }
}


If you then run
$ ${LD} --version
GNU ld (GNU Binutils) 2.39.50.20221130
Copyright (C) 2022 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
$ ${LD} -o out.bin -T linker.ld lib1.a lib2.a
$ objdump -x out.bin | grep func
c01e g F .text  000a func3
c00f g F .text  000f func1
c000 g F .text  000f func2

you see that func1 and func2 are in .text rather than .init-text where
they should be.

Running it compiled at 50be5d11289, you get the expected result:
$ objdump -x out.bin | grep func
c000 g F .text  000a func3
1000 g F .init-text 000f func1
100f g F .init-text 000f func2

I don't totally understand _why_ the prefix-tree change breaks this,
but it does.  If in fact my linker script is incorrect and the commit
is not introducing a bug, what is wrong with my script?  This seems
like a pretty straightforward scenario, though.

Thanks,
Andrew



Re: Bug in linker script file matching after b1eecf6f66a4a642 (prefix tree)

2023-06-26 Thread Andrew Oates
On Mon, Jun 26, 2023 at 8:40 AM Nick Clifton  wrote:
>
> Hi Andrew,
>
> > Apologies for the generic subject, I don't totally understand the
> > nature of the bug.
>
> No worries.  You have the problem narrowed down to a single commit,
> which definitely helps.
>
> > I have a basic linker script that stopped working after upgrading to
> > binutils 2.40.  I did a git bisect on the repository and it seems that
> > the breakage starts with commit
> > b1eecf6f66a4a642f4cb35688213e6c1c1ffdc79.
>
> Please could you file a bug report for this problem here:
>
>https://sourceware.org/bugzilla/enter_bug.cgi?product=binutils
>
> Having a bug report like this really helps us to track the bug
> and ensure that it can be reviewed in the future, should it ever
> come back.

Done, thanks!  https://sourceware.org/bugzilla/show_bug.cgi?id=30590
>
>
> > $ ${LD} -o out.bin -T linker.ld lib1.a lib2.a
>
> You have now shown how lib1.a and lib2.a are created.  I assumed
> that you did:
>
>$ ar cr lib1.a test1.o test2.o
>$ ar cr lib2.a test3.o

Yes, forgot that part

>
>
> > $ objdump -x out.bin | grep func
> > c01e g F .text  000a func3
> > c00f g F .text  000f func1
> > c000 g F .text  000f func2
> >
> > you see that func1 and func2 are in .text rather than .init-text where
> > they should be.
>
> Yes, this is a bug.  Not sure of the exact cause, but maybe the
> new code in commit b1eecf6f66a4a642 in scanning the sections in
> reverse order rather than forward order.
>
> Cheers
>Nick
>
>