static link problems with alias

Carmelo Amoroso carmelo73@gmail.com
Mon Jan 8 16:42:00 GMT 2007


On 1/8/07, Nick Clifton <nickc@redhat.com> wrote:
> Hi Carmelo,
>
> > I've encountered a problem doing a static link due to duplicated symbols
> > error.
>
> You appear to misunderstand how aliases and libraries work...
probably...
>
> > I have the following dummy functions:
> > - int foo();
> > - foo_alias() as weak alias of foo()
> > - int bar(); (thats call foo_alias())
> > all archived into libfoo_alias.a
>
> Note that in your libfoo_alias.a you have a *strong* definition of a
> symbol called "foo" and a *weak* definition of a symbol called "foo_alias".
>
> > and the following ones:
> >
> > int foo();
> > int other();
> > archied into libfoo.a
>
> Note that in libfoo.a you have a *second* strong definition of a symbol
> called "foo".
>
yes, it's wanted... I'm trying to reproduce a more complicated scenario
> > and the main.c calling bar() and other().
> >
> > When I compile the main with the following command:
> >
> > gcc -static -L. main.c -lfoo_alias -lfoo I get the following error:
> >
> > ./libfoo.a(foo.o)(.text+0x0): In function `foo':
> > : multiple definition of `foo'
> > ./libfoo_alias.a(foo_alias.o)(.text+0x0): first defined here
>
> Which is quite correct.  It goes like this:
>
>    1) main calls bar.  bar is defined in libfoo_alias:bar.o
>
>    2) bar calls foo_alias.  foo_alias is weakly defined in
> libfoo_alias:foo.o, but there are no strong definitions provided
> elsewhere, so the weak definition is used.  Hence libfoo_alias:foo.o is
> included in the link which brings with it a strong definition of the
> "foo" symbol.
>
I tried also having foo_alias() a strong alias: does it make any changes?

>    3) main calls other.  other is defined in libfoo.a:foo.o.
> libfoo.a:foo.o also defines "foo", hence there is now a duplicate
> definition.
>
Ok, thanks for your detailed explanation.. what I don't understand if why
if libfoo_alias:bar calls foo instead of the alias, having always to strong
definitions of foo(), the link works fine.
Could you explain the path in this case?

> What you probably wanted was the following:
>
>    1) Edit foo.c and add these lines to the end:
>
> int foo_alias () {
>    return 2;
> }
>
>    This provides a strong definition of the foo_alias symbol which can
> override the weak definition in foo_alias.c.
>
No, this is not what I wanted
>    2) Change the gcc link command line to:
>
> gcc -static -L. main.c -lfoo -lfoo_alias
>
>    ie moving the linking of libfoo.a to before libfoo_alias.a so that
> the strong definition of foo_alias is available when the reference to it
> is encountered in libfoo_alias:bar.o.
>
> Cheers
>    Nick
>
Thanks,
Carmelo



More information about the Binutils mailing list