I noticed that constructors from shared objects linked into my applications will always run before my application constructors, even if my applications has lower priority number (ie: higher priority); for example, let's say this program:
#include <stdio.h>
static void __attribute__ ((constructor (101))) test() {
printf("test\n");
}
int main(int argc, char *argv[]) {
return 0;
}
is linking the following shared object:
#include <stdio.h>
static void __attribute__ ((constructor (102))) test_so() {
printf("test so\n");
}
I expected the output to be:
test
test so
Instead, the output is the opposite.
Is there any reason why? I could not find any documentation.
Facts:
__attribute__((__constructor__))
basically adds a pointer to the function to some ELF section__attribute__((__constructor__(this_number)))
is limited to one ELF file, as compiler can reorder them there.It is documented in https://refspecs.linuxfoundation.org/elf/elf.pdf :