A minimal loader of Windows x64 DLLs (PE32+) into a Linux x86-64 process.
It runs the exports of a DLL directly, in the caller's thread: no Wine, no separate process, no IPC. It is meant for computational DLLs with a statically linked CRT (codecs, signal processing, crypto cores) that have to be called from a Linux program at full speed.
The loader is two C files and a header (LoadLibrary.c and Kernel32.c),
with no dependencies beyond glibc. Two optional files on top of it,
FrameBridge.c and SymbolBridge.c, make DLL frames visible to backtraces,
dladdr and gdb; they need nothing more (libgcc and libunwind are used when
the process has them).
- Wine runs a DLL in its own process: the caller needs a second process, a pipe or shared memory to talk to it, and Wine packages on every host.
- taviso/loadlibrary loads 32-bit PE only, so it cannot be used in a 64-bit process, and it emulates a large part of Win32 for one specific DLL.
This loader takes the opposite, minimal approach: it does what a DLL needs to be mapped and bound, gives every thread a TEB, and adds Win32 functions only when a DLL actually calls them, one at a time, through providers. An import nobody implements is bound to a thunk that reports its name and aborts, so an unexpected call is never silent.
- relocations, imports by name and by ordinal, delayed imports, exports,
forwards (
OTHER.Function,OTHER.#n), images without relocations at theirImageBase; - dependencies loaded from the directory of the importing DLL, shared by
identity, cycles collected on
FreeLibrary; - per-section protections,
PatchLibraryfor a byte pattern found exactly once; - a TEB and a PEB per thread on
gs:(stack bounds, last error, TLS slots), so__chkstkand friends work as on Windows; - static TLS (
__declspec(thread)), TLS callbacks,DllMainwith process and thread attach/detach (AttachLibrary), the startup of a static MSVC CRT; - providers: tables of Win32 functions per DLL name, registered by a
constructor or passed per load, overlaying real DLLs or replacing them;
Loader/Kernel32.cimplements the part of KERNEL32 a static CRT and plain computational code need (errors, time, critical sections, heap, TLS and FLS, code pages, a few stubs); - error codes for failures, a list of unresolved imports, names for addresses
inside DLLs (
A.dll+0x1003 (Twice+0x3)) that are safe in a fault handler; - messages to stderr, or through one process-wide function with the signature
of
syslog(3)set bySetLibraryReport(); - observers of loads and unloads, and two of them on top:
FrameBridge.ctranslates the.pdataof DLLs into DWARF CFI for libgcc and libunwind, so backtraces of fault handlers pass through DLL frames, epilogues included;SymbolBridge.cputs each image inside an ELF object of ld.so, sodladdr,dlsym,dl_iterate_phdrand gdb see its functions (with FrameBridge the object carries the CFI too, and gdb unwinds through DLL frames).
Everything past that is Windows itself, which is what Wine is for: SEH and
C++ exceptions thrown inside the DLL, the dynamic CRT (vcruntime140,
msvcp140, ucrtbase), files, consoles, the registry, real locales, 32-bit
images and images that are not large address aware. A DLL that needs those
belongs in Wine; the tools below tell on which side a DLL is.
make checkbuilds the loader, the tools and the example, generates small test DLLs with a Python script (no Windows toolchain is needed) and runs the tests on them.
Using it in a program:
#include <syslog.h>
#include "LoadLibrary.h"
typedef int (LIBRARY_ABI *Function)(int value1, int value2);
SetLibraryReport(syslog); // optional: stderr by default, NULL turns messages off
struct Module* image = LoadLibrary("Library.dll", NULL);
if (image == NULL)
fprintf(stderr, "%s\n", GetLibraryErrorText(GetLibraryLastError()));
PrepareLibraryThread(); // in every thread that calls into a DLL
AttachLibrary(image); // only if the DLL needs DllMain and its CRT startup
Function function = (Function)GetProcAddress(image, "Add");
int result = function(2, 3);
FreeLibrary(image);Compile Loader/LoadLibrary.c and Loader/Kernel32.c into the program and
link the objects directly: Kernel32.c registers itself in a constructor and
nothing refers to it, so a static archive would lose it. Functions given to a
DLL and pointers to its exports use LIBRARY_ABI (__attribute__((ms_abi))).
The full example is Examples/Call.c.
Tools/loadlibrary-check [--attach] library.dllloads the DLL with the real loader and tells why it fails, what stays unresolved (grouped by the side of the line), and with--attachwhetherDllMainpasses.Loader/FrameBridge.candLoader/SymbolBridge.care optional: link their objects likeKernel32.o, and the usualbacktrace(),_Unwind_Backtrace, libunwind anddladdrof a fault handler go through DLL frames and name them.Tools/LibraryCheck.py library.dll [export ...]walks the call graph from the exports you need (withobjdump) and lists the imports and TEB fields they reach, so error paths can be told from normal work.
- Documentation/Design.md: how it works, memory
layout, TEB, TLS,
AttachLibrary, providers, assessing a new DLL. - Documentation/TODO.md: known gaps and how to close them.
MIT, see LICENSE.