Multiple definition error when using STARTUP ld expression

Programming, for all ages and all languages.
Post Reply
schuirl
Posts: 6
Joined: Sat May 11, 2013 4:19 pm

Multiple definition error when using STARTUP ld expression

Post by schuirl »

Hello,

as I read the OSDEV Linker Scripts Wiki article while I rewrote my linker script (in fact I am rewriting my whole kernel - wich isn't so much since I just began with this all a few weeks ago) I wanted to try out the STARTUP expression in ld. - Sounded right to ensure that the assembly loader is right at the beginning of the binary. If I insert the STARTUP (loader.o) expression to my linker.ld file I get the following errors by the compiler:

Code: Select all

nasm -f elf -o loader.o loader.s
gcc -m32 -Wall -Wextra -nostdlib -nostdinc -fno-builtin -nostartfiles -nodefaultlibs -ffreestanding -fno-stack-protector -I ./include/ -I ./HAL/  -o init.o -c init.c
ld -melf_i386 -T linker.ld -o kernel.img loader.o init.o
loader.o: In function `loader':
loader.s:(.text+0x0): multiple definition of `loader'
loader.o:loader.s:(.text+0x0): first defined here
loader.o: In function `hltLoop':
loader.s:(.text+0xc): multiple definition of `hltLoop'
loader.o:loader.s:(.text+0xc): first defined here
Here is the code where the error occurs (I shortened it a little):

Code: Select all

global loader
global hltLoop

; some expressions for multiboot standard compliance

section .text
loader:
	; load C kernel

hltLoop:
	hlt
	jmp hltLoop
The hltLoop function is declared in 'init.c' as 'extern void hltLoop();' - the loader function is just named in the linker file. ('ENTRY (loader)'

The interesting thing is that the info utility of my archlinux says the following when I invoke "info ld" and search for "STARTUP":
'STARTUP(FILENAME)'
(...)
FILENAME will become the first input file to be linked, as though
it were specified first on the command line.
(...)
As you see in the invocation of the linker, the loader.o file is already specified first on the command line. The STARTUP expression shouldn't make any difference - but if I remove the STARTUP expression everything works fine.

I feel betrayed by the linker.
I feel betrayed by the info utility.
I feel betrayed by google.

Maybe someone can bring a little light into my dark.
If you need more information just ask. Thanks for reading until down here ;)

Have a nice day, weekend, evening, night. (Choose appropriate.)

Lasse
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Multiple definition error when using STARTUP ld expressi

Post by xenos »

Actually the documentation is quite correct: loader.o will become the first input file to be linked, as though it were specified first on the command line. So this means that if you use STARTUP, you don't need to include loader.o in the fie list on the command line, but ld will automatically prepend it to this list. Since you additionally list it on the command line, ld includes it twice - and this causes the multiple definition errors. Simply remove it from the command line and the problem should be gone.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
schuirl
Posts: 6
Joined: Sat May 11, 2013 4:19 pm

Re: Multiple definition error when using STARTUP ld expressi

Post by schuirl »

Thank you for the quick reply. I (obviously) haven't thought of that.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Multiple definition error when using STARTUP ld expressi

Post by sortie »

Hi,

You are not using a cross-compiler and this will lead you into certain trouble soon! Please stop what you are doing and build a proper cross-compiler and use libgcc along with it. Otherwise, you will pass a lot of option to gcc that is completely not needed and will cause trouble, you will get a lot of completely imaginary problems. The main problem is that gcc thinks it is compiling Linux programs when it is actually building a custom operating system. If you use a cross-compiler, then it knows it isn't Linux and things will work much better.

Please read this wiki article I wrote because of you on the subject: http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F
schuirl
Posts: 6
Joined: Sat May 11, 2013 4:19 pm

Re: Multiple definition error when using STARTUP ld expressi

Post by schuirl »

Thanks for your advise. I'm working on it right now.

Greetings,

Lasse
Post Reply