This is the mail archive of the crossgcc@sourceware.cygnus.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more infromation.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

__main undefined symbol


Stephane Dalton writes:
 > Thanks to William Gatliff and Kenneth Porter, I know a bit more about this
 > __main and I've been able to fix the problem.
 > 
 > But... I find it annoying to have this __main in my C file, and I did some
 > searching in the mail archive of this list to find that linking with
 > libgcc.a should fix my problem. 

Startup code is one of the rats nests in GCC.
You have to remember that GCC supports M different cpus and several
file formats, ALL of which have their own way of doing things and ALL
of which GCC has to do correctly for their respective environments.

For example, while a.out file formats usually have main call __main,
ELF has no need for this at all, and you won't find __main in ELF
targets [excepting of course unusual circumstances].

Heh.  Going back through the crossgcc archives I see that your target
is m68k-psos-elf.  I built a gcc-2.95.2 m68k-psos-elf compiler and compiled
main(){} and see that it is calling __main.  Generally that's not
how ELF ports work, but it looks like the psos port does that.  Oh well.

Hmmm... in another message I see Brendan Simon suggesting using -mads
and then you saying -m refers to ld emulation.
Note that Brendan was passing -mads to GCC.
You're refering to -m as passed to LD.
They don't mean the same thing.

Also, people generally do not invoke the linker directly.
Sure, go ahead if you want to.  But then you take on the
responsibility of specifying things like libgcc.a [which
if you invoke gcc to do the link, gcc will specify for you].

 > This libgcc.a file is present in the directory where I've built the actual
 > compiler but in the the resulting installation lib directory

libgcc.a contains most of the runtime support that GCC compiled programs
needs.  One canonical example is that if your cpu doesn't have a
divide instruction then libgcc.a will provide one and GCC will emit
calls to it as necessary.  Another thing it contains [for targets that
need it] is __main, and its job is to run the constructors for global
objects.

Note that you can need __main for C code too!
GCC has an extension to allow constructors to be run before main() is
called.

void __attribute__((constructor))
foo ()
{
  // blah ...
}

 > Looking at the sources I've also found that this __main should be called
 > from the crt0 or crt1, and for many architecture it is true but I did a grep
 > inside all those find inside the installation directory and the only files
 > containing the __main symbol are the tools themselve???

Whether crt0 calls __main or main calls __main is kind of up to the
person who did that port and on any compatibility requirements with
other toolchains.  It's no big deal which one does it [except of
course unnecessary differences between ports just cause no end of
confusion, sigh!].

 > Just to be clear I understand the purpose of __main but as I'm not doing c++
 > in this one I'd like to get rid of this problem in a clean way. Meanwhile
 > I've the fix provided by William (putting __main at the end of the source
 > file)

Putting a dummy __main in your code is fine [albeit a wart].

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]