15

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Thomas
  • 323
  • 1
  • 3
  • 11
  • Try this `g++ -o test ./crti.o ./crtn.o test.o -Wl, -nodefaultlibs -nostartfiles` And looking at your usage I feel you only want your startup files to be included and you seem to be OK with other libs from the standard library? – Pavan Manjunath Apr 12 '12 at 08:56
  • @Pavan: I tried `-Wl,-nodefaultlibs -Wl,-nostartfiles`, but then libc-_init is included by the compiler. The usage is for IBM Cell SPU - I need my own startup code to set everything up and don't use libc functions but still need the static constructors to get called. – Thomas Apr 12 '12 at 09:06
  • I am not well versed in C++, so I am not getting what you mean by "but still need the static constructors to get called"? Can you tell me some example. I was thinking static libs all the time – Pavan Manjunath Apr 12 '12 at 09:19
  • @Pavan: I have several template classes that have static class members which get constructed / initialized at startup automatically before `main` is called. As the compiler decides which template instantiations to use, it is not easy to call the constructors explicitly. That's why i need the static initialization code to be included. – Thomas Apr 12 '12 at 09:27
  • See, then, lets go step by step, first try ONLY WITH `-nostartfiles` so that you first solve your startup files problem. Once it starts to include your startup files then you tell us what specifically you wanna avoid including, then we'll take it from there – Pavan Manjunath Apr 12 '12 at 09:32
  • @Pavan: `-nostartfiles` alone won't let me replace libc-`_start` with my own code. – Thomas Apr 12 '12 at 09:38
  • As there seems to be no way to do what I need with command-line arguments, this is what I did instead to solve the problem: a) Use `-nodefaultlibs` (otherwise libstdc++ etc. have to be present). b) `mount` my own dir to the place where libc is. c) build custom `crti.o` and `crtn.o`, custom `_start`, empty `crt1.o` and use original `crtbegin.o` and `crtend.o` (these contain the intialization calls) and copy everything into mounted directory. – Thomas Apr 12 '12 at 10:23

1 Answers1

15

From gcc linker docs,

-nostdlib

Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.

Hence use,

-nodefaultlibs

Do not use the standard system libraries when linking. Only the libraries you specify will be passed to the linker, options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, will be ignored. The standard startup files are used normally, unless -nostartfiles is used. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

Also try,

g++ -Wl, -static

-Wl      passes the next command on to the linker
-static  On systems that support dynamic linking, this prevents linking with 
         the shared libraries. On other systems, this option has no effect.
Will Ness
  • 70,110
  • 9
  • 98
  • 181
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • With -nodefaultlibs the code for static intialization is included and called, but then I see no way to use my own .init / _start code. Is that still possible? – Thomas Apr 12 '12 at 08:37
  • Yes, I tried -nostartfiles, but then again the linker doesn't include the code and call for static intialization into my .init section. – Thomas Apr 12 '12 at 08:38