linker

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
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

linker

Post 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
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Post 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
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post 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
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

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