Compiling / linking with -nostdlib
seems to prevent static initialization, even if I add my own crti.s and crtn.s with .init
/.fini
sections.
Are there workarounds to make g++ generate static initialization code that is inserted in .init
or that I can call manually?
This is what I tried:
g++ -o test.o -c -fno-use-cxa-atexit test.cc # has _start (entry point)
# that calls _init and _main
as -o crti.o crti.s # has _init in section .init
as -o crtn.o crtn.s
g++ -o test ./crti.o test.o -nodefaultlibs -nostartfiles ./crtn.o
-nodefaultlibs
alone includes static initialization code and call, but forces use of libc-_start/_init.
-nodefaultlibs -nostartfiles
allows me to use my own _start / _init, but does not include code or call to static initialization.