Discussion:
OS X: Leading underscores and symbol name redirection
Miles
2012-08-22 05:34:36 UTC
Permalink
In Mac OS X, C compilers generate symbols that are prefixed with
underscores, such that these two declarations are functionally
identical:

int close(int);
int close(int) __asm("_close");

Meanwhile, dlsym() automatically prepends an underscore to its argument:

The symbol name passed to dlsym() is the name used in C source
code. For example to find the address
of function foo(), you would pass "foo" as the symbol name. [1]

So the second declaration above, when used with the LuaJIT FFI, will
result in LJ attempting to resolve a non-existent symbol named
"__close" (since it passes the name obtained from __asm directly to
dlsym).

The main impact for me is that OS X's system header files generally
can't be read directly with ffi.cdef (caveats[2] aside), as a
significant number of functions are declared with __asm.

-Miles

[1] https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/dlsym.3.html
[2] http://www.freelists.org/post/luajit/gccpythonplugin-LuaJIT-FFI-defs-collectingcoordination,2
Justin Cormack
2012-08-22 08:19:30 UTC
Permalink
Post by Miles
In Mac OS X, C compilers generate symbols that are prefixed with
underscores, such that these two declarations are functionally
int close(int);
int close(int) __asm("_close");
The symbol name passed to dlsym() is the name used in C source
code. For example to find the address
of function foo(), you would pass "foo" as the symbol name. [1]
So the second declaration above, when used with the LuaJIT FFI, will
result in LJ attempting to resolve a non-existent symbol named
"__close" (since it passes the name obtained from __asm directly to
dlsym).
The main impact for me is that OS X's system header files generally
can't be read directly with ffi.cdef (caveats[2] aside), as a
significant number of functions are declared with __asm.
-Miles
[1]
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/dlsym.3.html
Post by Miles
[2]
http://www.freelists.org/post/luajit/gccpythonplugin-LuaJIT-FFI-defs-collectingcoordination,2
There is no general guarantee that you can use header files unmunged. So
just remove those bits...

Justin
Miles
2012-08-24 06:30:20 UTC
Permalink
On Wed, Aug 22, 2012 at 1:19 AM, Justin Cormack
Post by Justin Cormack
There is no general guarantee that you can use header files unmunged. So
just remove those bits...
Sure, but I wasn't sure if LuaJIT's difference here from how compilers
behave on OS X was known or considered acceptable, as I couldn't find
it referenced anywhere.

-Miles
Mike Pall
2012-08-25 11:07:12 UTC
Permalink
Post by Miles
Sure, but I wasn't sure if LuaJIT's difference here from how compilers
behave on OS X was known or considered acceptable, as I couldn't find
it referenced anywhere.
Well, __asm__("...") is defined to hold the raw symbol name, so
that's what LuaJIT is doing. The real problem is that dlsym() on
OSX prepends an underscore, which is Apple's workaround for their
unsound design decisions wrt. the executable format. Sadly, the
damage cannot be undone and IMHO adding yet another workaround on
top of it isn't going to improve matters.

--Mike

Loading...