0

Here's my predicament: I am using PhysFS, which allows me to treat multiple directories and archives as one virtual directory. This is for a video game in the future, which I intend to make modder-friendly. PhysFS is the best way to make it modding-friendly.

At the same time, I also intend to use a script-extender of a sort that dnyamically loads libraries and registers them in Lua. I already created a crude, makeshift, largerly proof-of-concept Lua script extender.

So what is my issue? My issue is that dlopen / LoadLibrary only works with files on a real filesystem. And I want to load via PhysFS. I load the file via PHYSFS_openRead, then use PHYSFS_read to load the file's full content into a memory buffer.

Some people suggested loading the .so / .dll file into the memory from PhysFS, and then writing it out to /tmp in Linux, or C:\temp in Windows, and then dlopen-ing it. But I don't think that's a very elegant way to do it.

So... any other ideas? I did look into mmap and thought maybe I could manually load the ELF file (on Linux) and somehow manually extract the functions, and finally register them for Lua, but so far, all I could produce was a program that gave me information about the elf.

People said that I should look into LibJIT, but I'm not exactly sure how that would help me.

So what should I do? How do I load a library into memory via PhysFS, and use an alternative to dlopen to... dlsym the functions out of it?

Please don't suggest dlopen, unless you genuinely suggest that I write out the file temporarily.

My question is largerly: how do I link? How do I get the functions out of the library which I already have in memory? And why do some people suggest LibJIT to me?

  • 1
    Possible duplicate of [dlopen from memory?](https://stackoverflow.com/questions/5053664/dlopen-from-memory) – jotik Sep 13 '17 at 08:47
  • Only halfway. I only require loading form memory, due to the usage of PhysFS. And because I need to somehow dlsym the functions. – Stephanus Tavilrond Sep 13 '17 at 08:50
  • @StephanusTavilrond Don't use it then? – user0042 Sep 13 '17 at 08:50
  • Not-using PhysFS is not an option. It is the necessity of making it modder-friendly. – Stephanus Tavilrond Sep 13 '17 at 08:51
  • C++ or C - pick one. – Robert Andrzejuk Sep 13 '17 at 08:55
  • I'm not sure if that's very relevant, but if I must choose, I say C. PhysFS is written in C, the Lua library is written in C. All the functions I register to Lua must have a C linkage (no C++ name mangling). – Stephanus Tavilrond Sep 13 '17 at 08:57
  • Silly me... I didn't realize that I can just create the temporary file in /tmp, fill it with zeroes, then mmap it, use PHYSFS_read write onto it, flush, and then load it with dlopen. Somewhat more elegant (and possibly less roundabout) than just using fopen, PHYSFS_read into a malloc-d memory (or C++ char vector), and then fwrite, fclose and dlopen. – Stephanus Tavilrond Sep 13 '17 at 09:26

0 Answers0