So I was trying to wrap the server executable to freepascal program. The program was not supposed to do much. Just print hello world and call main from minetest. A basic experiment before adding hooks and api glue everywhere.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
{$L minetestserver.o} {L as link in an object file}
procedure main(argc:integer; argv:ppchar); cdecl; external name 'main';
BEGIN
write('Hello, world'#10);
main(argc,argv)
END.
Note: I am not really asking for help. I am just reporting my experimentation results.
The minetestserver.o was produced by adding -r to CMake linker flags. The first issues were related to duplicate symbols main and _start (both in c and pascal runtime), i solved that by removing crt0 from link and renaming main to cmain. This produce an executable, but it crashed in dynamic linker initialization.
As have no knowledge in dynamic linking I decided to link statically. So I linked all minetest object files to my minetestserver.o, which repeatedly produced tons of unresolved symbols until I added -lc -lm -lgcc_s -lz -lsqlite3 -lluajit-5.1 -lrt -lpthread -ldl -lcurl -lstdc++ and gmp and jsoncpp libraries to the link(age). Then a last error about dso_handle was solved by few lines in linker script.
This again produced useless executable which told me it was not found, despite being clearly present and rwx in the directory. (gdb was able to open the file and read symbols, but failed on 'r' commnd with NoSuchFileOrDir)
Why I was trying to do that? I tought about kind of binary modding interface.
I like about FPC that you just USES your units and they get pulled in with all their dependencies recompiled as needed, and it does that real fast. The minetest.o file could be distributed as binary and on server startup mods will get linked linked in. Saving mod developer from editing (c)Makefiles and user from resolving dependencies.
Next time I will probably try it the other way, with dl_open. Will keep you updated :)
Ps: do not relate my lack of knowledge in dynamic linking with the rest of compiler toolchain. I was able to assemble an glue and link a barebone OS.