Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc functions with constructor attribute are not being linked

I have a bunch of static libraries and they are interdependent. I faced problems while linking those libraries for my target because of dependencies. As a workaround I created one single archive file from all the libraries.

One of the static library has constructor and destructor functions so does the combined archive (examined the archive using nm and objdump) But when I used the combined archive for my target the final binary does not contain the constructor and destructor functions.

I also tried with --whole-archive but this option doesn't seem to work for me (the binary size didn't increase).

Any ideas what might have gone wrong. Thank you

like image 240
kanna Avatar asked Jul 05 '11 22:07

kanna


2 Answers

The linker only loads the modules it must (i.e. what you actually reference implicitly or explicitly), unless you force it.

That is both a good thing, and unfortunate. It makes the link process faster and prevents code bloat. It normally does not cause problems either, because normally you reference all modules that are needed in some way, and what you don't reference isn't needed. Normally.
But it is also the cause for this silent failure: The linker never loads the module containing your constructor/destructor functions and never even looks at it. That is because you never actually call these functions.

You can force the linker to include the module by explicitly linking with the object file that corresponds to the source containing the constructor/destructor functions.

In other words, if the constructor/destructor functions are in foo.c, add -l/path/to/foo.o to your linker's options, or simply pass foo.o on the commandline to the linker (as an extra argument).

In either case, by doing so, you explicitly tell the linker to consider this module, and doing so will make it find the constructor functions and properly call them.

An alternative (which does not require you to play with the commandline) may be putting a variable or a function (which does not necessarily do anything) into the same source file as your constructor/destructor functions and calling that one from any source file in the main program.
This will also force the linker to load the containing module (where it will find the constructor functions).

Update:
I tested that alternative, it works fine:

/* libcode.c */
void enable_constructors() { /* do nothing */ }
void __attribute__ ((constructor)) con() { __builtin_puts("construct"); }
void __attribute__ ((destructor))  des() { __builtin_puts("destruct"); }


/* main.c */
extern void enable_constructors();

int main()
{
    enable_constructors();
    __builtin_puts("main");
    return 0;
}

Output is:

construct
main
destruct

It also works with a global variable which you touch from a source file in the main program:

/* libcode.c */
int enable_constructors; /* not used for anything */

void __attribute__ ((constructor)) cons() { __builtin_puts("construct"); }
void __attribute__ ((destructor)) des() { __builtin_puts("destruct"); }


/* main.c */
extern int enable_constructors;

int main()
{
    ++enable_constructors; /* touch the other module */
    __builtin_puts("main");
    return 0;
}
like image 113
Damon Avatar answered Mar 23 '23 13:03

Damon


From the other answer:

You can force the linker to include the module by explicitly linking with the object file that corresponds to the source containing the constructor/destructor functions.

With modern CMake, this is as easy as an INTERFACE source on the library. Just use a dedicated source file for the constructor.

cmake_minimum_required(VERSION 3.13)

add_library(mylib STATIC libsource.c)
target_sources(mylib INTERFACE lib_ctor.c)
# CMake prior 3.13 requires an absolute path
# target_sources(mylib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/lib_ctor.c)

add_executable(prog app.c)
target_link_libraries(prog mylib)

Here, lib_ctor.c would contain the constructor. INTERFACE sources are added to the list of source files of every target linking against mylib. So they get explicitly linked and the constructor function is read by the linker.

I wasted an hour or two on --whole-archive and other hacks before realizing my build system can take care of this in a much cleaner way. As CMake is pretty popular nowadays and this is the top search result for terms like "gcc force constructor" and "gcc constructor not linked", I'd like to share it.

like image 22
WolleTD Avatar answered Mar 23 '23 15:03

WolleTD