开发者

Linker throwing out undefined symbol

开发者 https://www.devze.com 2022-12-22 22:07 出处:网络
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

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开发者_如何学JAVA 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


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


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)?

0

精彩评论

暂无评论...
验证码 换一张
取 消