Linking removes unnused code i still need

Programming, for all ages and all languages.
Post Reply
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Linking removes unnused code i still need

Post by Jezze »

Hi,

When my kernel is linked together from it's source object files I've noticed that if I create functions in a seperate c file that no other function will call the resulting object file will later not be included when I build my kernel. If I call just one of the functions in the file all of them will be included and not just the one I called. This behaviour is probably a good idea in the normal case because it removes unnused code but when you have a kernel and you have modules trying to hook into these functions it is bad when they dont exist. I assume this is one of the reasons you export symbols using macros in the linux kernel among others. Is there any other way that I could tell my linker not to remove code or do you have any other recommendation?
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: Linking removes unnused code i still need

Post by Gigasoft »

If your code is not exposed in any way, it will be removed. How were you planning to call it? Don't call the kernel just by embedding fixed offsets in driver code. You should have a table of well defined entry points which is used to resolve imports in loaded executables. What I've done is to reserve space for a jump table in the bss section of every executable. This is initialized by the kernel when the executable is loaded.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Linking removes unnused code i still need

Post by jnc100 »

I note from your repository that you link your kernel by linking together a set of static library files. In this case, ld by default chooses to only include those files within each archive whose members are actually used. If you wish to include every file in each archive in the link, put the option '--whole-archive' somewhere before the first library on the linker command line.

Regards,
John.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Linking removes unnused code i still need

Post by Jezze »

Thanks for your help!
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
Post Reply