Linker Script

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
Skute
Posts: 2
Joined: Sun Apr 03, 2005 11:00 pm

Linker Script

Post 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,
Arthur
Posts: 6
Joined: Fri Dec 08, 2006 8:41 am

Post 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.
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Post 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. :-)
Skute
Posts: 2
Joined: Sun Apr 03, 2005 11:00 pm

Post 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 :)
Post Reply