I've written a C library for AmigaOS4. Everything is working correctly except when I try to use the libc.so and libstdc++.so.
Basically the problem is that libstdc++ calls stream Init (https://en.cppreference.com/w/cpp/io/ios_base/Init) too early. Before libc constructors and so stdin, stdout and stderr are not yet created.
So, when i try to use even a simple example that is using cout I get a DSI (NPE).
I didn't found any way to set the c++ Init constructor priority. I've tried also to lower the C lib constructors priority but with no luck.
Any hint? Maybe some gcc switch or flag?
Asked
Active
Viewed 66 times
0

Andrea Palmate'
- 1
- 1
- 2
-
This sounds like a variant of the "normal" [static initialization order fiasco](https://en.cppreference.com/w/cpp/language/siof). – Some programmer dude Sep 26 '22 at 13:20
-
Well, yes it is. But how to fix it? I don't see anything on c++ side to late constructors intializations – Andrea Palmate' Sep 28 '22 at 08:37
-
Assuming that you use GCC, it uses *sections* to know about things like global objects and their initialization. If you have your own "construction" code that needs to be run in a specific order, you should make sure its put in a section that is processed before the libstdc++ global object constructors are executed. What does the GCC documentation say about it? – Some programmer dude Sep 28 '22 at 08:43
-
Yes I'm using gcc with my own C constructors and I've set a priority but I didn't find anything about sections. Or better. They are put where all ctors/dtors are. I've search also for switches can be useful but I haven't find nothing useful – Andrea Palmate' Sep 28 '22 at 17:24
-
It seems you might need to create the actual start-up code that does the global initialization, calls the constructors and calls the `main` function. See e.g. [this C library wiki](https://wiki.osdev.org/Creating_a_C_Library). – Some programmer dude Sep 28 '22 at 17:42
-
I have it already. https://github.com/afxgroup/clib2 – Andrea Palmate' Sep 28 '22 at 18:56
-
Your `crt0.S` needs to first do its own initialization, before invoking the "initialization" sections. Like for example create and initialize the C standard I/O streams. – Some programmer dude Sep 29 '22 at 03:55
-
I'm looking to some C libraries (like musl for example) but don't see any kind of specific initialization in crt.* files. The only define _init and _fini that I have in crtbegin.c and that call constructors. Keep in mind that if I don't use shared objects with c++ everything works correctly. The problem occurs with libc.so and libstdc++.so – Andrea Palmate' Sep 29 '22 at 08:09