Linker option to include unreferenced symbols in the output

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
samueldotj
Member
Member
Posts: 32
Joined: Mon Nov 13, 2006 12:24 am

Linker option to include unreferenced symbols in the output

Post 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
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Linker option to include unreferenced symbols in the output

Post 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.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker option to include unreferenced symbols in the output

Post 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.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Linker option to include unreferenced symbols in the output

Post 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!
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker option to include unreferenced symbols in the output

Post 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?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Linker option to include unreferenced symbols in the output

Post 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).
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker option to include unreferenced symbols in the output

Post 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.
User avatar
samueldotj
Member
Member
Posts: 32
Joined: Mon Nov 13, 2006 12:24 am

Re: Linker option to include unreferenced symbols in the output

Post 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.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker option to include unreferenced symbols in the output

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