static link problems with alias

Carmelo Amoroso carmelo73@gmail.com
Tue Jan 9 10:59:00 GMT 2007


On 1/9/07, Nick Clifton <nickc@redhat.com> wrote:
> Hi Carmelo,
>
> >>    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?
>
> No.  (Or at least it should not).
>
> >>    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?
>
> Are you sure ?  I tried it and I still got the error message.
>
> Here is what I did:
>
>    1. Edit bar.c to be:
>
> int foo_alias();
> int foo();
>
> int  bar() {
> //return foo_alias();
> return foo();
> }
>
>    2. Rebuild the library:
>
> % ar crv libfoo_alias.a foo_alias.o bar.o
>
>    3. Relink the executable:
>
> % gcc -static -L. main.c -lfoo_alias -lfoo
> ./libfoo.a(foo.o): In function `foo':
> foo.c:(.text+0x0): multiple definition of `foo'
> ./libfoo_alias.a(foo_alias.o):foo_alias.c:(.text+0x0): first defined here
>
>    Can you show me what you did please ?
>
I switched the link order of the archives
gcc -static -L. main.c -lfoo -lfoo_alias
and this works fine.

so does the link take the foo symbol from libfoo:foo.o without further
searching?

I know the order is important, so I'd like to understand if the
following statements are correct:
1) when using alias (strong or weak) the symbol's searching is
perfomed through all the
libraries (failing if multiple definition will be found)
2) while referring to a 'real' symbol the linker will stop at the
first occurrence

or I am really wrong.

Regards,
Carmelo

> Cheers
>    Nick
>
>
>
>



More information about the Binutils mailing list