bebbo / amiga-gcc

The GNU C-Compiler with Binutils and other useful tools for cross development for Amiga
GNU General Public License v2.0
312 stars 66 forks source link

Is it possible to compile Amiga Shared Libraries with this wonderfull compiler ? #379

Closed krabobmkd closed 3 months ago

krabobmkd commented 8 months ago

Hello, not necessarily a bug, but maybe still an issue: Is there a way to compile a .library file containing C++ , with this compiler ? I can't find any information on this, neither for the old GCC 2.5. ...The closest I can find is year 2000 StormC4 with their patched GCC 2.5 and there is a shared library example which links a special startup and uses a .fd file to build a library. Very nice but still GCC 2.5. Did I missed something ? Is such feature planned ?

Thanks for your efforts, and happy new year by the way.

bebbo commented 8 months ago

Sorry, no plans. But for public interest: you should link/provide that example!

krabobmkd commented 8 months ago

Hello again, According to official documentation, a .library file is same format as an exe, but just does moveq ¸-1,d0 / rts , then there is a constant sized structure with flags, and then the vector of function pointers in which the 4 firsts are the Open() / Close() / Expunge() functions that are used when doing OpenLibrary() /CloseLibrary() and Expunge() is used when Library is flushed, ... then a null pointer, and then only: the library public exported API as function pointers, corresponding to what a fd file describes.

This is well explained in this short asm example that does compile a valid .library: https://amigadev.elowar.com/read/ADCD_2.1/Libraries_Manual_guide/node058C.html

I may be wrong, but enclosing static-linked C++ inside an Amiga library with C Api would "just" implies the static inits usually done before entering main() in an exe, would be done in that Open() call, and then closed in the expunge() call. There is something about data allocated for each caller in the documentation, but for a pure "re-entrant" api, which is the case of what I want to port, and most "well done" codes, this is not even needed.

... StormC4 used to patch GCC2.5 but used their own format for makefiles, and I bet they never published their linker sources. there is something called "stormlink" that allows to link a library. in that case the first .o linked is "gcclibrary_startup.o" , which I inspected, and it is exactly looking like the structure described in the link, with names to be replaced... The whole AmigaOS 3.9 must have been compiled with that, This was done by the same team if i'm correct.

I may investigate further myself If there's no plan for generating libraries... I noted that doing gcc -shared would generate a legit "68k elf .so file" ... maybe coding a "dlopen() using 68k elf" on amiga would be a solution.

For information, I have a pistorm here and I've ported a static version of "libheif" , something to read the new .heic image format of the phones. The example "convert-heif" is already 90% working , yet a few of the iOS images I tested have glitchs, but the exe looks stable. it embbeds also libde265, the h265 codec (!). libheif is done in C11 with shared_ptr<> and all, but then the API is very C-looking and would fit a .library and fd file.

My plan is to have a heif.library , and then doing a "datatype class" to allow Amiga Apps to read the images directly, that would use the library. As their is a USB MTP driver with the Amiga Poseidon USB stack, this would allow to read images from modern androids directly.

... If I have a way to generate shared libraries with gcc6, then I would also try to do a python3 port.

michalsc commented 4 months ago

I am building many shared libraries with this gcc and there are no issues with that. As you have found out yourself shared libraries are executables, just built differently. The issue reported here is actually irrelevant to the gcc project itself, so it could be closed here.

You can find examples of my libraries (build with gcc-6.5 and cmake) can be found here: https://github.com/AmigaPorts/UUID https://github.com/michalsc/Emu68-tools

krabobmkd commented 4 months ago

Thanks a lot mister Schulz. I'll just looked at that.

The issue reported here is actually irrelevant to the gcc project itself,

The thing is, newcomers will endlessly try to build shared libraries with the common gcc options. using -nostartfiles and using a init.c to make the executable header is not that intuitive nor documented... Plus, I will look further into your examples, but ideally the .library should also manage the init/close of the C++ runtime.

All thatshould be documented somewhere.

( and also thanks because I am a very happy piStorm user.)

michalsc commented 4 months ago

Yeah, unfortunately AmigaOS shared libraries are completely different from what you know in case of other operating systems, so making a shared .library cannot be done just by adding a -shared compiler flag. Amiga libraries will always need more work and more boilerplate code.

But you're right, a topic like how to create shared library or .device file in pure C or C++ should be documented somewhere as it would probably help many :)

bebbo commented 3 months ago

Yeah, unfortunately AmigaOS shared libraries are completely different from what you know in case of other operating systems, so making a shared .library cannot be done just by adding a -shared compiler flag. Amiga libraries will always need more work and more boilerplate code.

But you're right, a topic like how to create shared library or .device file in pure C or C++ should be documented somewhere as it would probably help many :)

Wrong: Now shared libraries can be done just by adding a -shared compiler flag!

bebbo commented 3 months ago

Read more here: https://franke.ms/amiga/so.wiki