⬆️ ⬇️

We use 32-bit libraries in 64-bit programs

Can I use 32bit libraries in 64bit programs? Probably not. And if you really need? Then, rather, yes!



For one project, I need to work with 32bit proprietary libraries. There are no problems under Windows - we compile everything into 32 bits and more. But under Linux things are worse. Collecting all in 32 bits and hooking 32-bit versions of free libraries into RPM is ugly, moreover, I have an API for plug-ins. I don’t want to force users to install a 32bit compiler for the sake of the unfortunate plugin. So I decided to make an adapter from cat to mouse , which will call functions from the 32bit library in a separate process. At first I wanted to make it on a python, but I failed to compile a python into a binary. Then the kind people on the PyPy IRC channel told me that you can use libffi directly from C! Next thing is small.



The result was the runso32 program. It works in two modes. In the first mode, a library, a function, a string describing the parameters and the parameters themselves are specified in the command line arguments. Currently, parameters of the type int, unsigned int, double, pointer and byte sequence (buffer) are supported. The program calls the function, prints the return value and exits. The second option is interactive. The program reads commands from standard input or file. First, all the interesting functions are recorded. Then each entry that starts with “CALL” calls the function with the specified number. In interactive mode, before sending a buffer, you must specify its length. In interactive mode, all all values ​​are sent in Little Endian binary format.



Once we have decided what functions we need, you can make a 64bit library. Calling each function will send the identifier of the desired function and parameters to runso32 and get the result from runso32. Thus, we call functions from a 32bit library into a 64bit application. This happens, albeit with a delay, but almost imperceptibly for the rest of the program.

')

It seems that everything is ready, but the software interface is too cumbersome. Then I wrote a 64-bit library that registers functions and allows them to be called a little easier. Now we need only one function - versatile_call. However, before using it, it is necessary to fill in the global structure, the name of the library, the names of the functions and the parameter description lines. This structure must be populated during compilation. Then you just need to call the function versatile_call (n, ...), where n is the number of the necessary function from 32bit and further the list of parameters, do not forget about the buffer length, and the last argument is a pointer to the variable in which the return value will be written.



Source and RPM are here: sourceforge.net/projects/runso32

Source: https://habr.com/ru/post/202060/



All Articles