public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* The difference in behavior between ld and gold when processing "start group/end group" option
@ 2015-03-03 17:19 Nikola Ikonic
  2015-03-03 23:16 ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Nikola Ikonic @ 2015-03-03 17:19 UTC (permalink / raw)
  To: binutils; +Cc: Petar Jovanovic

Hello,

I have found a difference in behavior of gold and GNU ld when processing command line in one specific case when "start group/end group" is used in conjuction with linker script, and I am wondering if this difference is intentional or possible bug in either of linkers. In the example I have made, two libraries (libtest.a/libtest2.a) have different definitions of the same function and the command line uses linker script with a library name inside "start group/end group" option. Linker script only includes other libraries. When the same line is given to gold and GNU LD they call function from different libraries.

EXAMPLE:
main.c
extern void head_dummy();

int main(){
  head_dummy();
  return 0;
}

test.c
#include <stdio.h>

void dummy_func()
{
  printf("\nthis is library TEST\n\n");
}

test2.c
#include <stdio.h>

void dummy_func()
{
  printf("\nthis is library TEST2\n\n");
}

head_dummy.c
extern void dummy_func();    

void head_dummy()
{
  dummy_func();
}

libscript.a
GROUP ( libtest2.a libdummy.a )


$ gcc main.o -o main.exe -Wl,--start-group -Wl,-lscript -Wl,-ltest  -Wl,--end-group -L./

Executable linked with LD prints "... TEST2" while gold-linked executable prints "... TEST".

Which of these linkers behave correctly?

Thank you in advance for the explanation.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The difference in behavior between ld and gold when processing "start group/end group" option
  2015-03-03 17:19 The difference in behavior between ld and gold when processing "start group/end group" option Nikola Ikonic
@ 2015-03-03 23:16 ` Alan Modra
  2015-03-04 18:47   ` Cary Coutant
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Modra @ 2015-03-03 23:16 UTC (permalink / raw)
  To: Nikola Ikonic; +Cc: binutils, Petar Jovanovic

On Tue, Mar 03, 2015 at 06:19:45PM +0100, Nikola Ikonic wrote:
> Which of these linkers behave correctly?

I would say, GNU ld.  The innermost group ought to be repeatedly
searched until no new objects are extracted from libraries, before
looking at any other input files.  GROUP ( libtest2.a libdummy.a )
ought to cause head_dummy.o to be extracted from libdummy.a then
test2.o from libtest2.a, before the linker considers libtest.a.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The difference in behavior between ld and gold when processing "start group/end group" option
  2015-03-03 23:16 ` Alan Modra
@ 2015-03-04 18:47   ` Cary Coutant
  2015-03-05 18:21     ` Nikola Ikonic
  0 siblings, 1 reply; 4+ messages in thread
From: Cary Coutant @ 2015-03-04 18:47 UTC (permalink / raw)
  To: Nikola Ikonic, Binutils, Petar Jovanovic

Gold flattens the nested groups, by design:

// Called by the bison parser to start a group.  If we are already in
// a group, that means that this script was invoked within a
// --start-group --end-group sequence on the command line, or that
// this script was found in a GROUP of another script.  In that case,
// we simply continue the existing group, rather than starting a new
// one.  It is possible to construct a case in which this will do
// something other than what would happen if we did a recursive group,
// but it's hard to imagine why the different behaviour would be
// useful for a real program.  Avoiding recursive groups is simpler
// and more efficient.

If you try to nest a --start-group option within another, you'll get
an error, but if you use a script with a GROUP inside a --start-group,
we simply flatten it.

How much sympathy I have for builds that break because of this: none, sorry.

-cary


On Tue, Mar 3, 2015 at 3:16 PM, Alan Modra <amodra@gmail.com> wrote:
> On Tue, Mar 03, 2015 at 06:19:45PM +0100, Nikola Ikonic wrote:
>> Which of these linkers behave correctly?
>
> I would say, GNU ld.  The innermost group ought to be repeatedly
> searched until no new objects are extracted from libraries, before
> looking at any other input files.  GROUP ( libtest2.a libdummy.a )
> ought to cause head_dummy.o to be extracted from libdummy.a then
> test2.o from libtest2.a, before the linker considers libtest.a.
>
> --
> Alan Modra
> Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The difference in behavior between ld and gold when processing "start group/end group" option
  2015-03-04 18:47   ` Cary Coutant
@ 2015-03-05 18:21     ` Nikola Ikonic
  0 siblings, 0 replies; 4+ messages in thread
From: Nikola Ikonic @ 2015-03-05 18:21 UTC (permalink / raw)
  To: Cary Coutant, binutils

Your reply made things clear for me. Thank you.

Best regards,
            Nikola

-------- Original Message --------
Subject: Re: The difference in behavior between ld and gold when processing "start group/end group" option
Date: Wednesday, March 4, 2015 19:47 CET
From: Cary Coutant <ccoutant@google.com>
To: Nikola Ikonic <Nikola.Ikonic@rt-rk.com>, Binutils <binutils@sourceware.org>, Petar Jovanovic <Petar.Jovanovic@rt-rk.com>
References: <626f-54f5ed00-13-159dfd60@105572234><20150303231626.GA26435@bubble.grove.modra.org>



> Gold flattens the nested groups, by design:
>
> // Called by the bison parser to start a group.  If we are already in
> // a group, that means that this script was invoked within a
> // --start-group --end-group sequence on the command line, or that
> // this script was found in a GROUP of another script.  In that case,
> // we simply continue the existing group, rather than starting a new
> // one.  It is possible to construct a case in which this will do
> // something other than what would happen if we did a recursive group,
> // but it's hard to imagine why the different behaviour would be
> // useful for a real program.  Avoiding recursive groups is simpler
> // and more efficient.
>
> If you try to nest a --start-group option within another, you'll get
> an error, but if you use a script with a GROUP inside a --start-group,
> we simply flatten it.
>
> How much sympathy I have for builds that break because of this: none, sorry.
>
> -cary
>
>
> On Tue, Mar 3, 2015 at 3:16 PM, Alan Modra <amodra@gmail.com> wrote:
> > On Tue, Mar 03, 2015 at 06:19:45PM +0100, Nikola Ikonic wrote:
> >> Which of these linkers behave correctly?
> >
> > I would say, GNU ld.  The innermost group ought to be repeatedly
> > searched until no new objects are extracted from libraries, before
> > looking at any other input files.  GROUP ( libtest2.a libdummy.a )
> > ought to cause head_dummy.o to be extracted from libdummy.a then
> > test2.o from libtest2.a, before the linker considers libtest.a.
> >
> > --
> > Alan Modra
> > Australia Development Lab, IBM




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-05 18:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-03 17:19 The difference in behavior between ld and gold when processing "start group/end group" option Nikola Ikonic
2015-03-03 23:16 ` Alan Modra
2015-03-04 18:47   ` Cary Coutant
2015-03-05 18:21     ` Nikola Ikonic

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).