[uClibc]new `dummy' main reference

Erik Andersen andersen at codepoet.org
Mon Apr 15 04:42:37 UTC 2002


On Mon Apr 15, 2002 at 10:37:22AM +0900, Miles Bader wrote:
> I see in crt0.s the new code:
> 
>    /* Stick in a dummy reference to main(), so that if an application
>     * is linking when the main() function is in a static library (.a)
>     * we can be sure that main() actually gets linked in */
>    L_dummy_main_reference:
>            .long	main
> 
> Can you explain in more detail when this is required?
> 
> Since crt0 calls __uClibc_main, and __uClibc_main calls main, isn't
> main always pulled in anyway?

That was my theory as well -- until proven wrong.  Take as an
example busybox (development tree).  I recently reworked the
busybox build system such that everything (including the .o
file containing main) was packed into static libraries.  For
the link line I then have something like:
    gcc applets.a archival.a <etc> libbb.a  -o busybox

Well, with uClibc's wrapper this becomes something like:
    gcc <stuff> /usr/i386-linux-uclibc/lib/crt0.o applets.a \
	archival.a <etc> libbb.a  -o busybox -lgcc -lc

Since GNU ld is a single pass linker, it collects symbols as it
traverses the list.  If crt0.o does not reference main directly,
then main never gets pulled out of the various .a archives, and
then when ld gets to -lc, where __uClibc_main() calls main(), the
linker realizes that there is no main() to be found and spews an
error.  This same problem was what prevented XFree86 from
linking, since it does the same thing (putting main into a static
library).

So when Manuel and I realized the problem, the simplest way to
fix it was to stick a dummy reference to main in each and every
crt0.o file.  Admittedly, the way I added it is a bit lame (might
be beter to do a 'mov r0, main' right before r0 is used for
something useful).  But the idea is that unless crt0.o refers to
main, things may not link properly.

 -Erik

--
Erik B. Andersen             http://codepoet-consulting.com/
--This message was written using 73% post-consumer electrons--



More information about the uClibc mailing list