Need help with ld

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.
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Need help with ld

Post by meh »

After I switched to Xubuntu Linux I am getting this error when linking with ld.
start.o:start.o:(.text+0x1): undefined reference to `__main'
start.o:start.o:(.text+0xb): undefined reference to `__atexit'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x129): undefined reference to `__dso_handle'
main.cpp:(.text+0x13d): undefined reference to `__cxa_atexit'
support.o: In function `_main':
support.c:(.text+0x9): undefined reference to `_CTOR_LIST__'
support.o: In function `_atexit':
support.c:(.text+0x3b): undefined reference to `_DTOR_LIST__'
make[1]: *** [all] Error 1
make[1]: Leaving directory `/home/auszero/Programming/ChihuahuaOS/v0.01/Source'
make: *** [all] Error 2
These are my makefiles.

Code: Select all

BUILDDIR	= Build

all:
	make -C Source

Code: Select all

ROOT=../
BUILDDIR=${ROOT}/Build

all:
	gcc -c support.c -ffreestanding -nostdlib -fno-builtin -fno-exceptions
	g++ -c print.cpp -nostdlib -fno-builtin -fno-rtti -fno-exceptions
	g++ -c main.cpp -nostdlib -fno-builtin -fno-rtti -fno-exceptions
	nasm -f aout start.asm -o start.o
	nasm -f aout int.asm -o int.o
	ld -T ${ROOT}/link.ld -o ${BUILDDIR}/KERNEL.BIN start.o int.o main.o print.o support.o
There not to much, but all I'm trying to do is compile and link.

I'm kind of new to Linux.
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

1. -fno-leading-underscore as described in at least tens of threads here, or make a cross compiler (!) since you are not developing for linux.
2. Create _dso_handle and _cxa_atexit as described on iirc the wiki

Okay, I take that first back. Why in gods name are you creating _main and using __main in assembly? What about using the same name in both if you want them to match up?
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

I'm using _main in the main C++ file of the kernel and __main in the C support code.
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

AusZero wrote:I'm using _main in the main C++ file of the kernel and __main in the C support code.
Why?
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

Because if I use main in both the C and C++ code it gives an error that says multiple definitions of main.
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

I fixed one problem, but I still get these errors:
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x129): undefined reference to `__dso_handle'
main.cpp:(.text+0x13d): undefined reference to `__cxa_atexit'
support.o: In function `_main':
support.c:(.text+0x9): undefined reference to `_CTOR_LIST__'
support.o: In function `atexit':
support.c:(.text+0x3b): undefined reference to `_DTOR_LIST__'
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

Code: Select all

ROOT=../
BUILDDIR=${ROOT}/Build

all:
   gcc -c support.c -ffreestanding -nostdlib -fno-builtin -fno-exceptions
   g++ -c print.cpp -nostdlib -fno-builtin -fno-rtti -fno-exceptions
   g++ -c main.cpp -nostdlib -fno-builtin -fno-rtti -fno-exceptions
   nasm -f aout start.asm -o start.o
   nasm -f aout int.asm -o int.o
   ld -T ${ROOT}/link.ld -o ${BUILDDIR}/KERNEL.BIN start.o int.o main.o print.o support.o
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Do you reference __dso_handle, tatic_initialization_and_destruction_0(int, int), __cxa_atexit, _CTOR_LIST__, or _DTOR_LIST__ anywhere in your code or is LD or GCC doing that?
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

I have _CTOR_LIST__ and _DTOR_LIST__ in the C support code.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Try using this to link the files and if that doesn't work try adding -nostartfiles to the compiling command lines

Code: Select all

gcc -Xlinker -T -Xlinker ${ROOT}/link.ld -ffreestanding -nostdlib -fno-builtin -nostartfiles -o ${BUILDDIR}/KERNEL.BIN start.o int.o main.o print.o support.o 
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

It gives the same error.
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

shouldn't you be using g++ to link it. This is in c++ right?
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

It is in C++.
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

yes so use

Code: Select all

g++ -Xlinker -T -Xlinker ${ROOT}/link.ld -ffreestanding -nostdlib -fno-builtin -nostartfiles -o ${BUILDDIR}/KERNEL.BIN start.o int.o main.o print.o support.o
because this is c++ not c and gcc is a C compiler.
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Post by meh »

It still doesn't work. I think it could be something wrong with my C support file.
Post Reply