Init calls and unused symbol removal

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
JacobL
Posts: 5
Joined: Thu Aug 08, 2013 1:04 pm

Init calls and unused symbol removal

Post by JacobL »

I have been trying to implement my own version of the Linux init call tables (__define_initcall in linux/init.h + associated sections in the linker script). It works fine with the default parameters to ld, but if I add --gc-sections in order to cleanup unreferenced symbols, these init function symbols also get removed. Which of course makes sense, since the symbols would not be directly referenced, only indirectly via variables from the linker script that marks the array borders.

My goal is to be able to register initialization functions in a decentralized manner in order to get a sharper separation between code modules.

I looked into the EXTERN declaration in the linker script, but from what I can tell, it can only be used on symbols, not sections. Is there a way to exclude entire input sections from being removed via --gc-sections?

Alternatively, is it possible to set something in the __define_initcall() macro that would cause the symbol it defines to be excluded from being removed?

I can of course set up a table somewhere that references these symbols, but that defeats the entire purpose of this exercise.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Init calls and unused symbol removal

Post by sortie »

You could use a global variable with a constructor that registers the system call, or if you use C, you can use the GCC C extension that uses the same mechanism:

Code: Select all

__attribute__ ((constructor)) void initialize_foo_system_calls(void)
{
    syscall_table[FOO] = sys_foo;
}
Though, you'll need to calls the constructors by calling the _init function during early boot as described in Calling_Global_Constructors. I'm not sure how fitting this mechanism is for you, but it allows to you decentralized run code in 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: Init calls and unused symbol removal

Post by pcmattman »

The 'used' attribute is probably more useful:

Code: Select all

__attribute__((used))
JacobL
Posts: 5
Joined: Thu Aug 08, 2013 1:04 pm

Re: Init calls and unused symbol removal

Post by JacobL »

sortie:
The constructor concept is basically what I want to accomplish, without using C++. So it looks like this might be the way to go. I will give it a try in the weekend.

pcmattman:
I am already using the 'used' attribute, as the functions are declared as statics, and the compiler would warn + remove the code without it. But I cannot make it have any effect on which symbols are removed by --gc-sections in the linker.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Init calls and unused symbol removal

Post by Combuster »

JacobL wrote:(...) as the functions are declared as statics (...)
Static means inaccessible from anything outside the same source file. That's not what you want, now is it? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Init calls and unused symbol removal

Post by Kevin »

Inaccessible as in not making the name visible externally, but still accessible through pointers. This is probably what he wants.

Anyway, are the __attribute__((used)) on the functions or on the function pointers in the array? As you refer to the functions being static, it sounds like the former, but I think it should actually be the latter.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Init calls and unused symbol removal

Post by Combuster »

Given that the compiler needs to be explicitly told not to remove a static function already implies there's no reference to it at all - including any of the expected nonstatic pointers.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
JacobL
Posts: 5
Joined: Thu Aug 08, 2013 1:04 pm

Re: Init calls and unused symbol removal

Post by JacobL »

Thanks, I got it working. Turns out that all I needed was to add KEEP() around the sections containing the function pointers in my linker script. The example in http://wiki.osdev.org/Calling_Global_Constructors showed this part.
Post Reply