Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

c - Override weak symbols in static library

I want to make a static .a library for my project from multiple sources, some of them define weak functions and others implements them. Let's say as example I have :

lib1.c :

void defaultHandler()
{
    for(;;);
}
void myHandler() __attribute__((weak, alias ("defaultHandler")));

lib2.c :

void myHandler()
{
    /* do my stuff here */
}

Then I want to put them into one single library, so that it seems transparent for the end application

$ ar -r libhandlers.a lib1.o lib2.o

But there is now 2 symbols myHandler in libhandlers :

$ nm libhandlers.a | grep "myHandler"
00000001 W myHandler
00000581 T myHandler

And then when using the lib, the weak reference is linked. The only solution I have for the moment is to not include in the library lib2.c but to add it as source in the application's Makefile… that's not satisfying since I would like to provide only a few libraries to use and not a whole bunch of files.

The --whole-archive option is also not satisfying since I work on embedded system and I don't want to include all things I don't need.

Is there a way to compile the library so that the weak symbol disappear if a strong one is provided?

NOTE?: I'm using arm-none-eabi-gcc v4.8

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a byproduct of the way that .a libraries work - they're simply a collection of .o files.

What happens at compile link time is that the first reference to the name gets resolved to the weak reference and the strong name never gets a look in.

You can test this yourself by actually making both the identical name and strong and you'll see exactly the same behaviour.

If you want the strong references resolved first, then put them earlier in the archive, or create a separate strong archive and link that first in the link-line.

While not directly applicable to your case, as you're using an embedded environment, weak vs. strong references come into force properly when creating/consuming .so dynamic libraries rather than .a archives. When you create a .so, all the weak references that make up the library will not generate an error, and only one of them will be used for the final product; and if there is a strong definition anywhere then it gets used rather than any of the weak ones (this only works properly if, when you're creating a .so, you link all the .o files that make it up separately, or use the --whole-archive when creating the .so if linking to a .a).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...