lua-users home
lua-l archive

Re: lua to native linux binary

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thats the Lua script I'm using. I'm waving any copyright claims of that:
------------------------------
if #arg < 3 then
 error("Usage: "..arg[0].." [infile] [varname] [outfile]")
end
fin, err = io.open(arg[1], "rb")
if fin == nil then
 error("Cannot open '"..arg[1].."' for reading: "..err)
end
fout, err = io.open(arg[3], "w")
if fout == nil then
 error("Cannot open '"..arg[3].."'for writing: "..err)
end
fout:write("/* created by "..arg[0].." from file "..arg[1].." */\n")
fout:write("#include <stddef.h>\n")
fout:write("const char "..arg[2].."_out[] = {\n")
while true do
 local block = fin:read(16)
 if block == nil then
 break
 end
 for i = 1, #block do
 local val = string.format("%x", block:byte(i))
 if #val < 2 then
 val = "0" ..val
 end
 fout:write("0x",val,",")
 end
 fout:write("\n")
end
fout:write("};\n\nsize_t "..arg[2].."_size = sizeof("..arg[2].."_out);\n");
fin:close();
fout:close();
------------------------------
In my case with following Makefile rules:
------------------------------
luac.o: luac.c
luac.c: luac.out bin2carray.lua
	lua ./bin2carray.lua luac.out luac luac.c
luac.out: lsyncd.lua
	luac $<
------------------------------
In the past I've been using "objcopy", its theoretically quite an
effective way to store any binary data into an executable or library.
However, I've moved back to standard c-file-array-generation since a)
its really Linux only b) had some complains about some stack bits not
correctly set or something like that, so if you arent into
rocketscience thats binary formats and linking better stay away from
it - like I eventually decided to. I just wished the people within
this craft would make the tools simpler to directly link in arbitrary
data. Thats what I had before:
------------------------------
objarch: | lsyncd.o
	objdump -f lsyncd.o | grep architecture | \
		sed -e "s/,.*$$//" -e "s/[^ ]* \(.*\)/1円/" > $@ || rm $@
objtarget: | lsyncd.o
	objdump -f lsyncd.o | grep "file format" | \
		sed -e "s/.* \(.*\)/1円/" > $@ || rm $@
luac.o: luac.out objarch objtarget
	objcopy --input-target=binary \
		--output-target=`cat objtarget` \
		--binary-architecture=`cat objarch` $< $@
luac.out: lsyncd.lua
	luac $<

AltStyle によって変換されたページ (->オリジナル) /