Page 1 of 1

Linker Script

Posted: Mon Dec 04, 2006 3:44 pm
by Skute
Hi, can someone explain a little bit about the linker script to me. I have followed the beginners tutorials and got a 32bit bootloader which starts my C kernel, which just prints some text out on the screen.

My makefile features the following lines (amongst others):

nasmw -f bin $(SOURCE_DIRECTORY)\bootloader.asm -o $(BOOTLOADER_PATH)

gcc -ffreestanding -c $(SOURCE_DIRECTORY)\main.c -o $(INTERMEDIATE_DIRECTORY)\main.o

ld -e _main -Ttext 0x1000 -o $(INTERMEDIATE_DIRECTORY)\kernel.o $(INTERMEDIATE_DIRECTORY)\main.o $(INTERMEDIATE_DIRECTORY)\video.o $(INTERMEDIATE_DIRECTORY)\ports.o

ld -i -e _main -Ttext 0x1000 -o $(INTERMEDIATE_DIRECTORY)\kernel.o $(INTERMEDIATE_DIRECTORY)\main.o $(INTERMEDIATE_DIRECTORY)\video.o $(INTERMEDIATE_DIRECTORY)\ports.o

objcopy -R .note -R .comment -S -O binary $(INTERMEDIATE_DIRECTORY)\kernel.o $(INTERMEDIATE_DIRECTORY)\kernel.bin

I'm not entirely sure what the "freestanding" flag on gcc does, nor what the 2 calls to ld are doing. I understand the objcopy command removes sections from the binary file, but again, I don't know why.

Can someone recommend some good resources on linker scripts and possibly what I need to look at to make the build process a little easier, i.e.

1x nasmw
nx gcc
1x ld

then run bochs

Cheers,

Posted: Fri Dec 08, 2006 9:22 am
by Arthur
The following command can output some thing you interest include the default link script of ld:

Code: Select all

ld -verbose > ld.script
Note: remove useless text from the output file.

Posted: Fri Dec 08, 2006 9:42 am
by Walling
The "freestanding" parameter to GCC tells that it should not expect to find any standard C libraries, nor a main method. Something like that...

'ld' is a linker. It links your object files together and produces a binary (which itself can be a object file).

The reason why to run 'ld' two times.. I don't remember the "-i" option, but I think it means "incremental linking". Maybe it will make the final binary smaller in size. But the first linking has to be there to ensure that all input files exists. It is just a guess. I think I've used it before because the "-i" option does not expect to link all symbols, so you can create an object file which is runtime linked by your kernel.

About linker scripts, read this manual: http://www.gnu.org/software/binutils/ma ... d_toc.html If you read it you are able to create complex linker scripts that does exactly what you want to do. Also use the 'objdump' tool to analyze your object files and see their content.

The last 'objcopy' in your makefile removes dubugging information. Debugging information is great, when creating an application, but when creating a kernel it just takes up a lot of space and you can't use the debugging information anyway. Unless you create your own debugger. :-)

Posted: Fri Dec 08, 2006 11:29 am
by Skute
That's great, thanks for the info :)

The tutorial I was reading said the double ld call was to make sure input files existed and to reduce the size of the binary. Good guess :)