Running luajit object file from C - c
From the documentation: http://luajit.org/running.html
luajit -b test.lua test.obj # Generate object file
# Link test.obj with your application and load it with require("test")
But doesn't explain how to do these things. I guess they're assuming anyone using Lua is also a C programmer, not the case with me! Can I get some help? GCC as an example.
I would also like to do the same thing except from the C byte array header. I can't find documentation on this either.
luajit -bt h -n test test.lua test.h
This creates the header file but I don't know how to run it from C. Thanks.
main.lua
print("Hello from main.lua")
app.c
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char **argv)
{
int status;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_getglobal(L, "require");
lua_pushliteral(L, "main");
status = lua_pcall(L, 1, 0, 0);
if (status) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
return 1;
}
return 0;
}
Shell commands:
luajit -b main.lua main.o
gcc -O2 -Wall -Wl,-E -o app app.c main.o -Ixx -Lxx -lluajit-5.1 -lm -ldl
Replace -Ixx and -Lxx by the LuaJIT include and library directories. If you've installed it in /usr/local (the default), then most GCC installations will find it without these two options.
The first command compiles the Lua source code to bytecode and embeds it into the object file main.o.
The second command compiles and links the minimal C application code. Note that it links in the embedded bytecode, too. The -Wl,-E is mandatory (on Linux) to export all symbols from the executable.
Now move the original main.lua away (to ensure it's really running the embedded bytecode and not the Lua source code file) and then run your app:
mv main.lua main.lua.orig
./app
# Output: Hello from main.lua
The basic usage is as follows:
Generate the header file using luajit
#include that header in the source file(s) that's going to be referencing its symbols
Compile the source into a runnable executable or shared binary module for lua depending on your use-case.
Here's a minimal example to illustrate:
test.lua
return
{
fooprint = function (s) return print("from foo: "..s) end,
barprint = function (s) return print("from bar: "..s) end
}
test.h
// luajit -b test.lua test.h
#define luaJIT_BC_test_SIZE 155
static const char luaJIT_BC_test[] = {
27,76,74,1,2,44,0,1,4,0,2,0,5,52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,
102,114,111,109,32,102,111,111,58,32,10,112,114,105,110,116,44,0,1,4,0,2,0,5,
52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,102,114,111,109,32,98,97,114,
58,32,10,112,114,105,110,116,58,3,0,2,0,5,0,7,51,0,1,0,49,1,0,0,58,1,2,0,49,1,
3,0,58,1,4,0,48,0,0,128,72,0,2,0,13,98,97,114,112,114,105,110,116,0,13,102,
111,111,112,114,105,110,116,1,0,0,0,0
};
runtest.cpp
// g++ -Wall -pedantic -g runtest.cpp -o runtest.exe -llua51
#include <stdio.h>
#include <assert.h>
#include "lua.hpp"
#include "test.h"
static const char *runtest =
"test = require 'test'\n"
"test.fooprint('it works!')\n"
"test.barprint('it works!')\n";
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
// package, preload, luaJIT_BC_test
bool err = luaL_loadbuffer(L, luaJIT_BC_test, luaJIT_BC_test_SIZE, NULL);
assert(!err);
// package.preload.test = luaJIT_BC_test
lua_setfield(L, -2, "test");
// check that 'test' lib is now available; run the embedded test script
lua_settop(L, 0);
err = luaL_dostring(L, runtest);
assert(!err);
lua_close(L);
}
This is pretty straight-forward. This example takes the byte-code and places it into the package.preload table for this program's lua environment. Other lua scripts can then use this by doing require 'test'. The embedded lua source in runtest does exactly this and outputs:
from foo: it works!
from bar: it works!
Related
Is it possible to wrap a dynamic object in an ELF dynamically linked binary?
I am attempting to wrap a glibc symbol with my own definition of that symbol. For functions that glibc exposes this has so far been as easy as defining a __wrap_function_name in my source code and then adding Wl,--wrap=external_function_name to the linker flags of my build system's linking steps. In practice this then would look something like: extern "C" void __wrap_external_function_name(void) { my_internal function(); } However I have recently attempted the same on a variable that glibc exposes, in this case __signgam. I again defined the linker flags for its wrapper, but I am not sure how and if it is even possible to define the wrapper for a variable. I attempted __wrap__signgam = signgam, but that had no effect. In fact the symbol table when exposed with objdump -T binary | grep signgam had the following content, showing that even though the wrap function is defined, the original symbol remains untouched: 0000000000000000 DO *UND* 0000000000000000 GLIBC_2.23 __signgam 0000000000000000 DO *UND* 0000000000000000 GLIBC_2.2.5 signgam 0000000001509d24 g DO .bss 0000000000000004 Base __wrap___signgam Is there a canonical way to wrap these dynamic objects?
You can use the dynamic load library (e.g. -ldl) and use some of its functions, notably, dlsym. There are three steps: First create your source file. Creat a shared library (.so file) from that source file Invoke target program (set environment variable LD_PRELOAD to point to your .so file Note that to intercept a given function, your function has to be defined with the same name. You can have as many intercept functions as you want in your source file. Here is some sample source code for intercepting (e.g.) read ... // NOTE: need _GNU_SOURCE above for RTLD_NEXT in dlfcn.h #define _GNU_SOURCE #include <unistd.h> #include <dlfcn.h> #include <string.h> static void msg(const char *buf) { int len; len = strlen(buf); write(1,buf,len); } // read -- read a file ssize_t read(int unit,void *buf,size_t buflen) { static ssize_t (*proc)(int,void *,size_t) = NULL; ssize_t rlen; // get pointer to real function (only needs to be done once) if (proc == NULL) proc = dlsym(RTLD_NEXT,"read"); // do [whatever] stuff before real read ... msg("PHONY: before\n"); // invoke the real function rlen = proc(unit,buf,buflen); // do [whatever] stuff after real read ... msg("PHONY: after\n"); return rlen; } Here's a sample target program: #include <stdio.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd; int rlen; char buf[100]; fd = open("/etc/passwd",O_RDONLY); rlen = read(fd,buf,sizeof(buf)); close(fd); printf("main: read %d bytes\n",rlen); return 0; } Now create a shared library (.so). Note that below, there's only one source file. But, you can create the library from as many separate source files as you wish (e.g. you could put one intercept function per source file or put all in one source file, as you choose). Here's a [crude] Makefile (that creates the shared library and the sample target program): all: mylib.so target read.o: read.c cc -c -fPIC read.c mylib.so: read.o cc -shared -o mylib.so read.o -ldl target: target.c cc -o target target.c test: env LD_PRELOAD=./mylib.so ./target clean: rm -f mylib.so *.o target Now, to invoke the target program (e.g.): make test Here's the generated output of the test: env LD_PRELOAD=./mylib.so ./target PHONY: before PHONY: after main: read 100 bytes
How can I have the symbols in a shared library override existing symbols?
I want to load a shared library with dlopen and have the symbols in it available without having to individually grab function pointers to them with dlsym. The man page says that the RTLD_DEEPBIND flag will place lookup of symbols in the library ahead of global scope, but evidently this does not mean it overrides existing symbols because this does not work. Consider this example: main.c: #define _GNU_SOURCE #include <stdio.h> #include <dlfcn.h> int is_loaded(){return 0;} int main(){ void *h = dlopen("./libimplementation.so", RTLD_NOW | RTLD_DEEPBIND); if(!h){ printf("Could not load implementation: %s\n", dlerror()); return 1; } puts(is_loaded() ? "Implementation loaded" : "Implementation not loaded"); dlclose(h); } implementation.c: int is_loaded(){return 1;} Makefile: all: main libimplementation.so main: main.c gcc -Wall -std=c99 -o $# $^ -ldl lib%.so: %.c gcc -Wall -std=c99 -o $# $^ -shared clean: -rm main *.so When I build and run with make and ./main, I expect the test() function from libimplementation.so to override the test() function from main but it doesn't. I know I could also move all of the code in main() into another shared library run and then have main() dlopen libimplementation.so with RTLD_GLOBAL and then have librun.so refer to the symbols from libimplementation.so without having them defined so it loads them: modified main.c: #define _GNU_SOURCE #include <stdio.h> #include <dlfcn.h> int main(){ void *impl_h = dlopen("./libimplementation.so", RTLD_LAZY | RTLD_GLOBAL); if(!impl_h){ printf("Could not load implementation: %s\n", dlerror()); return 1; } void *run_h = dlopen("./librun.so", RTLD_LAZY); if(!run_h){ printf("Could not load run: %s\n", dlerror()); dlclose(impl_h); return 1; } void (*run)(void); *(void**)&run = dlsym(run_h, "run"); if(!*(void**)&run){ printf("Could not find entry point in run: %s\n", dlerror()); dlclose(impl_h); dlclose(run_h); return 1; } run(); dlclose(impl_h); dlclose(run_h); } run.c: #include <stdio.h> int is_loaded(void); void run(void){ puts(is_loaded() ? "Implementation loaded" : "Implementation not loaded"); } and the Makefile gets librun.so added as a prerequisite for all. Is there a way to get the symbols from the shared library available all at once without dlsym or putting the actual code in another shared library like with librun.so?
There is fundamentally no way to do what you're asking for. Imagine the main program had something like: static char *myptr = array_in_lib1; Later, at the time you dlopen, myptr has some other value. Has the program just changed the variable to point to a different object? Or has it been incremented to point to some element later in the array - in which case, would you want it adjusted to account for the redefinition of array_in_lib1 with a new definition from the newly-opened library? Or is it just a random integer cast to char *? Deciding how to treat it is impossible without understanding programmer intent and full process history of how it arrived in the current state. The above is a particulrly egregious sort of example I've constructed, but the idea of symbols changing definition at runtime is fundamentally inconsistent in all sorts of ways. Even RTLD_DEEPBIND, in what it already does, is arguably inconsitent and buggy. Whatever you're trying to do, you should find another way to do it.
Lua: compiling C module gives "multiple Lua VMs detected"
I am using Lua 5.3.4 on Mac OS X 10.12 (Sierra). I have build Lua using "make macosx install". I am trying to extend Lua, writing a C module that I can call from Lua. To do that I wrote this C code: #include <math.h> #include <lua/lua.h> #include <lua/lauxlib.h> #include <lua/lualib.h> static int l_sin (lua_State *L) { double d = lua_tonumber (L, 1); lua_pushnumber (L, sin (d)); return 1; } static const struct luaL_Reg mylib[] = { {"l_sin", l_sin}, {NULL, NULL} }; int luaopen_mylib (lua_State *L) { luaL_newlib (L, mylib); return 1; } If I try to compile it with gcc -Wall -shared -fPIC -o mylib.so -llua mylib.c (or in every other way I found online), when I do l = require "mylib" on the Lua interpreter, I get multiple Lua VMs detected. (of course I have copied the shared library in the right path) I can't find a solution, can you help me? Thanks
gcc shared library with header in the same library
I'm trying to compile a shared library (.so) with the following code: libreceive.h: #include <stddef.h> int receive(int sockfd, void *buf, size_t len, int flags); libreceive.c #include <stddef.h> #include <libreceive/libreceive.h> int receive(int sockfd, void *buf, size_t len, int flags){ return recv(sockfd, buf, len, flags); } the problem here is that I'm trying to include the .h in the library that I'm building and using it in the same time from the same library in the .c . I know that what I'm trying to do is possible, but I can't manage to do it. How can I do that please. the code I'm trying is: gcc -o libreceive.o -c -include libreceive.h libreceive.c I get the following error: fatal error: libreceive/libreceive.h: No such file or directory compilation terminated. the problem here is that I'm trying to include the .h in the library that I'm building and using it in the same time from the same library in the .c . I know that what I'm trying to do is possible, but I can't manage to do it. How can I do that please.
Since libreceive.h and libreceive.c appear to be in the same directory (judging from your compiler call), the normal way is #include "libreceive.h" In order to use #include <libreceive/libreceive.h> libreceive.h would have to lie in a directory called libreceive, and that directory would have to be part of the include path. It is possible to achieve this, but I believe it is neither necessary nor useful here.
You are missing out a few steps here. Consider the following setup. File: add.c #include "header.h" int add(int a, int b) { printf("SIZE: %d\n", SIZE); return a+b; } File: sub.c #include "header.h" int sub(int a, int b) { printf("SIZE: %d\n", SIZE); return a-b; } File: header.h, located in directory called include. #include <stdio.h> #define SIZE 100 int add(int a, int b); int sub(int a, int b); So to step by step build a .so file. /* Build `.o` files first */ $ gcc -fPIC -c sub.c -I path/to/include/ $ gcc -fPIC -c add.c -I path/to/include/ /* Build shared library called libsample.so */ $ gcc -shared -o libsample.so add.o sub.o The above command will build a .so by name libsample.so. Where all definition from .c(like functions) and .h(like #defines) will get included in your library. How to use this in your code: Consider the file File: main.c #include <stdio.h> int main() { int a = 3, b = 4; printf("Return : %d\n", add(a, b)); return 0; } To make use of your library libsample.so. $ export LD_LIBRARY_PATH=/path/to/direc/containing/.so/file $ gcc -o exe main.c -lsample -L/path/to/direc/containing/.so/file The above command should create a binary called exe. $./exe SIZE : 100 /* SIZE Defined in .h file */ Return : 7 /* Defined in add.c */ You can refer this guide : http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
Finaly I decided to use #include "libreceive.h" as suggested by the guys. the probleme I had is that the compiler was looking for my so in /usr/lib wich is the default when id do sudo gcc and my usr had the $LD_LIBRARY_PATH at /usr/local/lib and therefore gcc coudn't find my library at compile time another problem was that the program that call thos .so was looking fro the .h in some folder that doesn't exist and I had to add it. thanks guys for you answers
How can a shared library (.so) call a function that is implemented in its loader code?
I have a shared library that I implemented and want the .so to call a function that's implemented in the main program which loads the library. Let's say I have main.c (executable) which contains: void inmain_function(void*); dlopen("libmy.so"); In the my.c (the code for the libmy.so) I want to call inmain_function: inmain_function(NULL); How can the shared library call inmain_function regardless the fact inmain_function is defined in the main program. Note: I want to call a symbol in main.c from my.c not vice versa which is the common usage.
You have two options, from which you can choose: Option 1: export all symbols from your executable. This is simple option, just when building executable, add a flag -Wl,--export-dynamic. This would make all functions available to library calls. Option 2: create an export symbol file with list of functions, and use -Wl,--dynamic-list=exported.txt. This requires some maintenance, but more accurate. To demonstrate: simple executable and dynamically loaded library. #include <stdio.h> #include <dlfcn.h> void exported_callback() /*< Function we want to export */ { printf("Hello from callback!\n"); } void unexported_callback() /*< Function we don't want to export */ { printf("Hello from unexported callback!\n"); } typedef void (*lib_func)(); int call_library() { void *handle = NULL; lib_func func = NULL; handle = dlopen("./libprog.so", RTLD_NOW | RTLD_GLOBAL); if (handle == NULL) { fprintf(stderr, "Unable to open lib: %s\n", dlerror()); return -1; } func = dlsym(handle, "library_function"); if (func == NULL) { fprintf(stderr, "Unable to get symbol\n"); return -1; } func(); return 0; } int main(int argc, const char *argv[]) { printf("Hello from main!\n"); call_library(); return 0; } Library code (lib.c): #include <stdio.h> int exported_callback(); int library_function() { printf("Hello from library!\n"); exported_callback(); /* unexported_callback(); */ /*< This one will not be exported in the second case */ return 0; } So, first build the library (this step doesn't differ): gcc -shared -fPIC lib.c -o libprog.so Now build executable with all symbols exported: gcc -Wl,--export-dynamic main.c -o prog.exe -ldl Run example: $ ./prog.exe Hello from main! Hello from library! Hello from callback! Symbols exported: $ objdump -e prog.exe -T | grep callback 00000000004009f4 g DF .text 0000000000000015 Base exported_callback 0000000000400a09 g DF .text 0000000000000015 Base unexported_callback Now with the exported list (exported.txt): { extern "C" { exported_callback; }; }; Build & check visible symbols: $ gcc -Wl,--dynamic-list=./exported.txt main.c -o prog.exe -ldl $ objdump -e prog.exe -T | grep callback 0000000000400774 g DF .text 0000000000000015 Base exported_callback
You'll need make a register function in your .so so that the executable can give a function pointer to your .so for it's later used. Like this: void in_main_func () { // this is the function that need to be called from a .so } void (*register_function)(void(*)()); void *handle = dlopen("libmylib.so"); register_function = dlsym(handle, "register_function"); register_function(in_main_func); the register_function needs to store the function pointer in a variable in the .so where the other function in the .so can find it. Your mylib.c would the need to look something like this: void (*callback)() = NULL; void register_function( void (*in_main_func)()) { callback = in_main_func; } void function_needing_callback() { callback(); }
Put your main function's prototype in a .h file and include it in both your main and dynamic library code. With GCC, simply compile your main program with the -rdynamic flag. Once loaded, your library will be able to call the function from the main program. A little further explanation is that once compiled, your dynamic library will have an undefined symbol in it for the function that is in the main code. Upon having your main app load the library, the symbol will be resolved by the main program's symbol table. I've used the above pattern numerous times and it works like a charm.
The following can be used to load a dynamic library and call it from the loading call (in case somebody came here after looking for how to load and call a function in an .so library): void* func_handle = dlopen ("my.so", RTLD_LAZY); /* open a handle to your library */ void (*ptr)() = dlsym (func_handle, "my_function"); /* get the address of the function you want to call */ ptr(); /* call it */ dlclose (func_handle); /* close the handle */ Don't forget to put #include <dlfcn.h> and link with the –ldl option. You might also want to add some logic that checks if NULL is returned. If it is the case you can call dlerror and it should give you some meaningful messages describing the problem. Other posters have however provided more suitable answers for your problem.