tags:

views:

40

answers:

2

I'm building an executable using GCC 3.4.0 . The target is an embedded system. I've been using a way of modularly defining "console command handlers" by defining a function pointer to a handler in any compilation unit to be in a certain linker section. At runtime when a command is entered on the console I can loop through all the handlers in the "console handler data section" without having to have a central table with references to each handler.

Clever clever right, well now it's biting me. When I do this in a c file that has no other externally referenced symbols (my handler is the only function for example), the linker throws all of it away. My handler isn't included in the final executable, neither is anything else in the compilation unit.

  • A hack is to define a dummy global variable in the c file and reference it elsewhere, then my handler in it's special data section, is included.
  • I can also use -u to the linker and it works, but blows the goal of modularity.
  • I've tried using attribute ((used)) on my callback with no luck -- seems to get ignored.
  • My special section has the KEEP specification, but that doesn't help.

Any ideas?

Thanks, Kurt

A: 

Besides writing a linker script, in which you have all the control you'll ever need to decide what gets included/discared etc. try create a .a out of all your object file and specify -whole-archive to the linker, or you'll need to figure out all the symbols you want and specify them with --retain-symbols-file

leeeroy
Thanks for the reply. Unfortunately I believe that all of those options require explicitly naming each and every function pointer in order to keep it in the binary. That defeats the purpose of the linker section which is modularity, otherwise I'd just make a central table and be done with it. I really need some way to telling it to never throw away symbols linked to a certain section.
Kurt Schwemmer
A: 

You should be able to do this with an explicit linker script. Something like

.mysection : {
       PROVIDE(_mysection = .);
       KEEP (callbacks.o(.text*))
}

would put the .text section from callbacks.o in section .mysection in the output file. However I would expect the compiler to do this with the "used" attribute but maybe the linker doesn't get it. If you look at the ld invocation (such as with the -v flag to gcc), does your callback object file get included there (that is, is it the compiler or the linker that discards it)?

Per Ekman