Linking in libgcc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
uglyoldbob
Member
Member
Posts: 62
Joined: Tue Feb 13, 2007 10:46 am

Linking in libgcc

Post by uglyoldbob »

I am using the autotools to compile my kernel and OS.
I have successfully compiled a cross-compiler. I call configure so that it uses my cross-compiler to compile everything. I even setup distcheck so that it call configure and use the cross-compiler.
I can call
./configure --host=i386-elf-doors
make
and it works fine
but make distcheck does not work.

Here are the relevant makefile.am lines. This is an issue of linking in libgcc.
Here is the hard way to do it (which doesn't work right with make distcheck)
kernel_LDADD = libgcc.a
libgcc.a: $(shell $(CC) -print-libgcc-file-name)
cp $(shell $(CC) -print-libgcc-file-name) $(srcdir)/libgcc.a

The easy way to do it would be like this.
kernel_LDFLAGS = -lgcc -T $(srcdir)/x86/link.ld
But I still get undefined references to the functions defined in libgcc.a when I do it that way (__divdi3 and others).
I have an 80386SX 20MHz 2MB RAM.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Linking in libgcc

Post by JamesM »

uglyoldbob wrote:I am using the autotools to compile my kernel and OS.
I have successfully compiled a cross-compiler. I call configure so that it uses my cross-compiler to compile everything. I even setup distcheck so that it call configure and use the cross-compiler.
I can call
./configure --host=i386-elf-doors
make
and it works fine
but make distcheck does not work.

Here are the relevant makefile.am lines. This is an issue of linking in libgcc.
Here is the hard way to do it (which doesn't work right with make distcheck)
kernel_LDADD = libgcc.a
libgcc.a: $(shell $(CC) -print-libgcc-file-name)
cp $(shell $(CC) -print-libgcc-file-name) $(srcdir)/libgcc.a

The easy way to do it would be like this.
kernel_LDFLAGS = -lgcc -T $(srcdir)/x86/link.ld
But I still get undefined references to the functions defined in libgcc.a when I do it that way (__divdi3 and others).
Get rid of -no-stdlib from your CFLAGS.
uglyoldbob
Member
Member
Posts: 62
Joined: Tue Feb 13, 2007 10:46 am

Re: Linking in libgcc

Post by uglyoldbob »

Removing -nostdlib from the compile options works, but it also causes libraries I don't want to be brought in (-lstdc++ -lm -lg -lc). I can add -nostartfiles to get rid of the crt stuff, but some of the crt files (crtend.o and crtbegin.o) might be usable in the kernel. I am trying to find a "prettier" method of doing the C/C++ startup and exit routines. Would creating a kernel-level crt0.o object and linking with that work? Or am I just wasting my time?
The gcc docs seems to say that nostdlib and lgcc can be specified at the same time and it '"should"' work, but it doesn't. I have a feeling it may be the order in which the arguments are passed to the linker (lgcc should probably be passed after nostdlib but it not). Slight tickling of my automake configuration might be in order? I can provide output of the link phase with the -### flag if it would help any. I would post it now but it is a lot and would just add more clutter.
I was under the impression that you were only supposed to use libraries from the "system independent" directory in the kernel and the rest of it was fair game for "user" programs.
For my setup that directory is /usr/local/lib/gcc/i386-elf-doors/4.3.2.
The "system dependent" directory should be for user programs right (/usr/local/i386-elf-doors/lib on my system).

Also, I am trying to setup the build system and everything else so that it can be ported relatively easily to another architecture.
I have an 80386SX 20MHz 2MB RAM.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Linking in libgcc

Post by quok »

uglyoldbob wrote:Removing -nostdlib from the compile options works, but it also causes libraries I don't want to be brought in (-lstdc++ -lm -lg -lc). I can add -nostartfiles to get rid of the crt stuff, but some of the crt files (crtend.o and crtbegin.o) might be usable in the kernel. I am trying to find a "prettier" method of doing the C/C++ startup and exit routines. Would creating a kernel-level crt0.o object and linking with that work? Or am I just wasting my time?
The gcc docs seems to say that nostdlib and lgcc can be specified at the same time and it '"should"' work, but it doesn't. I have a feeling it may be the order in which the arguments are passed to the linker (lgcc should probably be passed after nostdlib but it not). Slight tickling of my automake configuration might be in order? I can provide output of the link phase with the -### flag if it would help any. I would post it now but it is a lot and would just add more clutter.
I was under the impression that you were only supposed to use libraries from the "system independent" directory in the kernel and the rest of it was fair game for "user" programs.
For my setup that directory is /usr/local/lib/gcc/i386-elf-doors/4.3.2.
The "system dependent" directory should be for user programs right (/usr/local/i386-elf-doors/lib on my system).

Also, I am trying to setup the build system and everything else so that it can be ported relatively easily to another architecture.
If you use -nostdlib and -lgcc, make sure that -nostdlib is listed before -lgcc on the command line. I've seen some versions of binutils that would wipe -lgcc if it was specified before -nostdlib. Also remember that by default symbols in libraries are only resolved if they have already occurred when the library is linked. That is to say, if you have:

Code: Select all

ld ... file1.o file2.o libgcc.a file3.o ...
then only file1.o and file2.o will get their external symbols resolved, and anything in file3.o that calls something in libgcc.a will still be undefined. To fix this, specify -lgcc and other libraries as the last things on the command line, or use --start-group and --end-group.
uglyoldbob
Member
Member
Posts: 62
Joined: Tue Feb 13, 2007 10:46 am

Re: Linking in libgcc

Post by uglyoldbob »

Here is what I am trying in my makefile.am.

kernel_LDFLAGS = -nostdlib -lgcc -T $(srcdir)/x86/link.ld

I posted on the autotools mailing list. Hopefully someone over there has seen this on a cross compile configuration before. I think it is definitely an ordering problem with the way automake does it and the way I have it configured. I just have to figure out how to get automake to do it the "right" way.
I have an 80386SX 20MHz 2MB RAM.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
Post Reply