[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Bundling Lua code in your (ELF) executable.
- From: Tony Finch <dot@...>
- Date: Mon, 3 Mar 2008 21:04:20 +0000
On Mon, 3 Mar 2008, David Jones wrote:
>
> There are plenty of ways of doing this (for example translate the .lua files
> into .c files that have the contents in a C string), I think mostly the reason
> why I chose this way is that I happened to be reading about the Mach-O loader
> whilst I was needing this feature.
>
> Because I use the Mach-O loader interface this technique only works on OS X;
> though the general principles will be similar on other operating systems, most
> of the work is in the platform-specific details. Since the work was
> non-trivial I thought I'd jot down a few notes in case anyone else wanted to
> do something similar
Interesting. The easy linking is really nice.
On an ELF system with gcc and GNU binutils, you can turn a (source or
compiled) lua script into a linkable object as follows. The key magic is
the -b option which specifies the input file format, in this case, any old
binary data.
ld -b binary -r -o MYSCRIPT.o MYSCRIPT.lua
When you link your program you must specify -export-dynamic so that the
binary file's symbols are available to dynamic objects, including the
run-time linker.
gcc -export-dynamic -o MYPROG MYPROG.c MYSCRIPT.o
Then MYPROG can find the location of MYSCRIPT in memory using dlsym().
void *start = dlsym(NULL, "_binary_MYSCRIPT_lua_start");
void *end = dlsym(NULL, "_binary_MYSCRIPT_lua_end");
luaL_loadbuffer(L, start, end-start, "MYSCRIPT");
You can also turn MYSCRIPT into a shared object using:
ld -shared -o MYSCRIPT.so MYSCRIPT.o
Then MYPROG can use dlopen() and pass the resulting handle into the
dlsym() calls in the loadbuffer() code, or you can use the LD_PRELOAD
environment variable to link MYPROG and MYSCRIPT together at run-time.
Tony.
--
f.a.n.finch <dot@dotat.at> http://dotat.at/
FISHER: NORTHERLY 5 OR 6, INCREASING 6 TO GALE 8. ROUGH BECOMING VERY ROUGH.
WINTRY SHOWERS. MODERATE OR GOOD.