JUST SO YOU KNOW: Using GCC6.5 / C11 to make a library
This may be of some use for people that wish to compile their Amiga OS3 libraries with the modern GCCs ... so here is my story: Else working project is in https://github.com/krabobmkd/EmojiGear/tree/main/libutf8rastport , in the CMakeLists.txt gcc link options.
The Amiga GCC 6.5 recent cross-compiler and even the old GCC2.95 Amiga version will create executables easily, but the "-shared" options will not make an amiga shared library (maybe for some very recent GCC ? ... also: the old Haage&p StormC4 GGC2.95 was modified to actually create libraries ) ... for information the recent GCC6.5 creates a legitimate "68000 elf shared library" when using "-shared". I once thought about just creating a "dlopen()" using elf on OS3, that would resolve linking shared C and C++ runtimes using function names by the same way as linux,... but well: no.
1. Replace the startup
So I finally found that the pistorm guys were compiling their .library and amiga shared binaries with GCC6.5, by forcing their own startup like this:
- having a .library startup called libinit.s , which exports a "__start" label
- using link option -Wl,-e,__start , to force start label being the start of the binary.
...This replace the startup by libinit.s , which contains what is needed to point the array of functions... well it's exactly like what you had in the old SDK for SASC and all that, nothing new, but: this looks like it works and link correctly, but...
2. Have your own reentrant C Runtime code (if needed)
first big trap: it still uses the GCC C Runtimes init and close, that are really specific to exe, and absolutely can't be used in a .library , because you now there must be nothing using "globals" in a .library you must be re-entrant, the GCC C runtime manages "malloc/free" and DOS calls with static things that are not closed when released, and will consider a unique memory pool for all calling process, ... things like that.
So: if you wish to use C Runtime functions in your library code (you may not if you only need amiga OS api) ... you have to make some C Runtime that redirect C API to AmigaOS api... As I use just malloc/free/calloc , fopen/fread/fclose and some snprintf , this is not a big issue. I added lib_amiga_crt.c for this and have these option to remove the executable C Runtime to be automatically linked:
** -nostdlib -ffreestanding **
3. Trick the GCC6 linker to actually link in the correct order
Then ok, I though it was fine... and it is not !!! a library created like this will be perfectly working when OpenLibrary() / CloseLibrary() is used with it, but it is linked the wrong way: contrary to SASC and all old compilers, the order you specify the source is not respected in the link with GCC6.5 ... and** libinit.s code finish at the end of the binary when it should be at start** . I Found that recently because OS3.9 version command will work, but the** OS3.9 installer is unable to find the starting tag** (OS3.2 Installer does it). The other side effect is that if you try to run it as an executable it crashes the OS, when it should just reach the "moveq #-1,d0 + rts " code, to exit properly.
So forcing that with GCC6.5 looks like adding this link option again:
** -Wl,-T,amiga_library.ld **
... and you need that .ld file, that is meant to map how the code sections are mapped to the generated binary. This will force libinit.s section to be at the beginning, and so to reach the exit code. And ok, now, we have a 100% compliant .library linked with GCC6.5 and compiled with modern C11.