avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Link question


From: Kári Davíðsson
Subject: Re: [avr-gcc-list] Link question
Date: Sun, 25 Mar 2012 15:51:35 +0000
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2

Got confused with the behavior of __attribute__((constructor)) and
__attribute__((destructor)) for shared libraries. I thought I could do
something similar through the .init8 and .fini8 sections. But as I
discovered the behavior of the linker between static and dynamic
libraries is quite different.

I read a little up on the topic since your answer did limit my search
space on google. The solution seems to be to use -whole-archive for the
linker. To prevent clash with the standard library the trick is to use
-whole-arcive in pairs with -no-whole-archive enclosing the libraries
to be linked, e.g

$ avr-g++ -g -Os -DF_CPU=16000000UL -mmcu=atmega328p myapp.o \
-L. -Wl,-whole-archive -lmylib -Wl,-no-whole-archive -o myapp

More in man ld.

Thanks for your answer which definitely pushed me in the right
direction.

brg
kd

PS: I am not to keen on your "-u symbol" solution although I am sure it
works since it breaks "information hiding" principle and I did not
have global symbols in all of the .o files. Each .o file would have to
have its own symbol to prevent symbol clashing in the linker. Also
starting to introduce global symbols to the .o files just to have to
maintain them also in the Makefile(s) seem counter productive.

On 3/23/12 8:07 AM, Joerg Wunsch wrote:
(Please suscribe to the list to not miss replies.)

Kári Davíðsson<address@hidden>  wrote:

I can compile my application and link in this library without errors,
but then .init8 and .fini8 sections are stripped out of the final
executable, e.g.

No, they are not "stripped", they simply never made it there, because
the linker had no reason to include those object modules in the first
place.

If I on the other hand link the application by specifying all of the
library object files on the command line, every thing works just fine.

Sure, object files specified on the command line are always linked.

Object modules within a library are only pulled in to satisfy global
references which are undefined at the point where the library is being
processed (that's why the position of a library on the command line is
important).

Presumably, your init/fini object modules don't define any symbol that
is being needed by the linker, so they are not considered.

What you could do is putting one global symbol into each of your
modules (perhaps you've already got some?), and then explicitly tell
the linker that just this symbol is global undefined, using the -u
option.  This will make the linker pull in that entire object module
from the library.

avr-g++ -g -Os -DF_CPU=16000000UL -mmcu=atmega328p myapp.o \
-Wl,-u,symbol1 -Wl,-u,symbol2 \
-L. -lmylib -o myapp

(The -Wl option is used to prefix a linker-only option on the compiler
command line.)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]