Skip to content
Advertisement

Problem loading a library with FFI in PHP 7.4

I’m having trouble using a third party .so library in PHP with the new FFI. When I run this little piece of code:

JavaScript

PHP outputs me this error:

JavaScript

Is this a problem with the library itself, my PHP configuration or something else? It’s confusing to me, because I can use normally this same library with this C++ code:

JavaScript

I’m aware that the PHP code doesn’t execute the NFE_Nome function, but I’m getting the error before trying to call the function itself.

Advertisement

Answer

— Edit —

This problem is result of two bugs in two different programs.

  1. When linking a shared objects, fpc-3.0.0 (or newer) adds this to the dependencies (as the first dependency): /lib64/ld-linux-x86-64.so.2

  2. ld-linux-x86-64.so.2 exports a calloc variant, that doesn’t (always) clears the memory it returns (details below)

The workaround OP suggested is linking in a separate pass (using -E (or -Cn) option of fpc), but before running ./ppas.sh fixing link.res file. For that, I hacked this awk-script, but I do feel it a bit clumsy:

JavaScript

— Original answer —

Sounds like a linking problem: you might have added /lib64/ld-linux-x86-64.so.2 into the dependent shared libraries, which is neither required or useful.

Actually, it resulted a calloc version that returns non-zeroed memory. The details are described here: https://www.linuxquestions.org/questions/programming-9/debugging-dlopen-4175668676/ and here: Why does calling calloc in gdb not appear to zero out the memory?

Suggested solution: change the linkage according to the example:

JavaScript

The difference can be checked with readelf -d. Wrong:

JavaScript

Right output:

JavaScript

Also, with command ldd demodule.so the line containing /lib64/ld-linux-x86-64.so.2 should be the last one.

Edit: discussion on sourceware.org regarding this problem: https://sourceware.org/bugzilla/show_bug.cgi?id=25486

Edit: on Freepascal side: https://bugs.freepascal.org/view.php?id=36706

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement