Thursday, February 19, 2009

A note about libdl functions on NetBSD and the "Service unavailable" error

On NetBSD, dlopen(), dlsym(), dlerror() and other such functions are implemented as stubs in libc, and the stubs just return an error, "Service unavailable". The ELF loader patches references to these symbols inside binaries to point at the real definitions. Trouble is, if you look up one of these symbols at runtime using dlsym() itself, it will return the address of the stub function and not the correct definition.

So amusingly enough, this won't work in Factor on NetBSD:
FUNCTION: void* dlopen ( char* path, int flags ) ;
"libX11.so" dlopen .

Of course you'd never do the above, but it can come up legitimately if you try calling dlerror() via FFI and expect to get a useful error message for the most recent failed dynamic linker operation. Instead, you'll always get the undescriptive "Service unavailable".

No comments: