Page 1 of 1

linker

Posted: Wed Jun 28, 2006 4:12 am
by kenneth_phough
I'm having trouble with linkers. Is a linker really necessary? If I have the following sources:
- kernel.c
- video.c
would I be able to compile and link them with gcc?

Code: Select all

gcc -fwritable-strings kernel.c video.c -o kernel.bin
If I do need a linker, what linkers would you recommend?

Thanks,
Kenneth

Posted: Wed Jun 28, 2006 8:08 am
by carbonBased
Yes, you always need a linker (at the risk of generalizing).

If you have more then one object file, you will need a linker to combine the two. Even if you have only one object file, you will typically need a linker to create the resultant executable (very few compilers output executables... I can't think of any off hand, as it's simply not practical).

When you envoke GCC in this way, it envokes ld, the GNU linker.

The GCC executable is a front end to the GNU C compiler, and linker (among many other things).

--Jeff

Posted: Wed Jun 28, 2006 9:14 pm
by kenneth_phough
Thank you, that helped me clarify a couple things about compiling and linking. My next question is if I have two object files and the boot sector compiled with nasm and am going to use ld linker to link them. Is a linker script necessary and if so where might I find a good tutorial on ld scripting?

Thanks,
Kenneth

Posted: Thu Jun 29, 2006 12:48 pm
by carbonBased
A linker script isn't always necessary, no. However, in OSDev'ing, it's often very useful to know how to write one so I would still suggest reading up on them.

A linker script will allow you to directly place/align sections where you want, define symbols at specific addresses, etc.

Just searching for gnu ld scripts will probably find some useful info. I was able to find ld script basics from various online docs.

In your case, yes, you would use a linker (ie, ld) to link them. This is a perfect instance where a link script comes in handy, as you want to ensure that your boot sector is the first object in the resultant binary (although, if you know ld, you'll realize that providing it as the first object on the command line will do the same thing... but this isn't necessarily defined in the ld specs).

--Jeff