Page 1 of 2

Need help with ld

Posted: Fri Dec 21, 2007 2:10 pm
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.

Posted: Fri Dec 21, 2007 2:56 pm
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?

Posted: Fri Dec 21, 2007 4:05 pm
by meh
I'm using _main in the main C++ file of the kernel and __main in the C support code.

Posted: Sat Dec 22, 2007 1:42 am
by Candy
AusZero wrote:I'm using _main in the main C++ file of the kernel and __main in the C support code.
Why?

Posted: Sat Dec 22, 2007 11:15 am
by meh
Because if I use main in both the C and C++ code it gives an error that says multiple definitions of main.

Posted: Sat Dec 22, 2007 11:48 am
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__'

Posted: Sat Dec 22, 2007 11:55 am
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

Posted: Sat Dec 22, 2007 12:02 pm
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?

Posted: Sat Dec 22, 2007 12:06 pm
by meh
I have _CTOR_LIST__ and _DTOR_LIST__ in the C support code.

Posted: Sat Dec 22, 2007 12:39 pm
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 

Posted: Sat Dec 22, 2007 1:04 pm
by meh
It gives the same error.

Posted: Sun Dec 23, 2007 5:26 pm
by iammisc
shouldn't you be using g++ to link it. This is in c++ right?

Posted: Mon Dec 24, 2007 11:56 am
by meh
It is in C++.

Posted: Wed Dec 26, 2007 1:46 am
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.

Posted: Wed Dec 26, 2007 10:50 am
by meh
It still doesn't work. I think it could be something wrong with my C support file.