Page 1 of 1

Linker option to include unreferenced symbols in the output

Posted: Thu Apr 09, 2009 10:07 pm
by samueldotj
Hi,

I have a library which is linked into my kernel executable. Some of the functions in the library are never used (unreferenced). So when linking the linker (ld) discards those symbols. I tried –no-gc-sections but it didn’t work. From ld man page I could nt find any other option to control this. Any other option?

I work around this problem by using EXPORT_SYMBOL inside my kernel code. This problem with this code is the library has lot of symbols and it also uses EXPORT_SYMBOL, so lot of duplication.

Sam

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 5:39 pm
by pcmattman
Why do you want unreferenced symbols in the output? It doesn't really make sense to link in unused symbols - it's just a waste of space.

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 8:48 pm
by frank
pcmattman wrote:Why do you want unreferenced symbols in the output? It doesn't really make sense to link in unused symbols - it's just a waste of space.
The only reason I could think of is that an external module could try to call those functions but can't because they aren't in the kernel. But other than that I would just leave the unreferenced symbols out to save space.

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 9:05 pm
by pcmattman
The only reason I could think of is that an external module could try to call those functions but can't because they aren't in the kernel.
That's a valid reason - but why not link the module against the library? It's probably more efficient than keeping the entire library in the kernel binary - if you want to do that you may as well not have the library!

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 9:28 pm
by frank
I'm assuming an example where the modules are loaded on boot or on demand.
pcmattman wrote:That's a valid reason - but why not link the module against the library? It's probably more efficient than keeping the entire library in the kernel binary - if you want to do that you may as well not have the library!
Agreed but that would create code duplication. On the other hand it would save the modules from crashing on a single change in the kernel library and a possibly recompilation of every module linked to the kernel. Doesn't Linux export kernel functions to modules?

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 9:34 pm
by pcmattman
Linux does export kernel functions to modules, however AFAIK they are not symbols from a library - they are symbols that are in the final kernel binary (eg, printf in the kernel). When the module is loaded its undefined references are linked to the kernel's symbols.

In this case, it looks like the OP wants the entire library in the kernel for this purpose.

However, I can't help but look at this and think shared objects would help a lot. That way modules would pick up symbols either from the kernel or the shared object - avoiding code duplication and definitely helping with the final binary size of the kernel (not to mention modules).

Re: Linker option to include unreferenced symbols in the output

Posted: Sun Apr 12, 2009 9:44 pm
by frank
I would be more inclined to make a separate kernel and driver library and only export the bare minimum of symbols from the kernel. That way if something in the kernel changed it wouldn't affect drivers. Of course this assumes that the change is simple like changing the format of a function call or the number of arguments required not an entire driver structure rewrite.

Re: Linker option to include unreferenced symbols in the output

Posted: Mon Apr 13, 2009 12:58 am
by samueldotj
frank wrote:
pcmattman wrote:Why do you want unreferenced symbols in the output? It doesn't really make sense to link in unused symbols - it's just a waste of space.
The only reason I could think of is that an external module could try to call those functions but can't because they aren't in the kernel. But other than that I would just leave the unreferenced symbols out to save space.
Yes. A driver calls a unreferenced function; so during loading of that function symbol lookup fails.

My kernel is linked with a library(libacpi); it is required for early boot processor/memory detection. The same library is used by acpi driver after boot. Since the library is statically linked into my kernel; I thought it would be better to use it rather load the same library again in the driver.

From the replies, it seems I have only two options
1) Use it as a shared library - It is not possible because during boot I cant load a library
2) Use it as a static library in both kernel and driver.

Re: Linker option to include unreferenced symbols in the output

Posted: Mon Apr 13, 2009 10:21 am
by frank
When linking against a library only the symbols that are needed are placed in the final binary, however, I think that if you were to link directly against the object files in the library instead of putting them in a library then all of the symbols would be included.

EDIT: Maybe this option could help you out some
--whole-archive
For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

Two notes when using this option from gcc: First, gcc doesn't know about this option, so you have to use -Wl,-whole-archive. Second, don't forget to use -Wl,-no-whole-archive after your list of archives, because gcc will add its own list of archives to your link and you may not want this flag to affect those as well.