├── examples ├── no_dep │ ├── main.lua │ └── lw_dependencies.c ├── both_dep │ ├── main.lua │ ├── foo.lua │ ├── lw_dependencies.c │ └── bar.c ├── lua_dep │ ├── main.lua │ ├── foo.lua │ └── lw_dependencies.c ├── examples.mk └── Makefile ├── src ├── lua │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lfunc.h │ ├── ldebug.h │ ├── lualib.h │ ├── ltm.h │ ├── ltable.h │ ├── lstring.h │ ├── lvm.h │ ├── ldo.h │ ├── lzio.h │ ├── lzio.c │ ├── linit.c │ ├── lmem.h │ ├── ltm.c │ ├── lctype.h │ ├── llex.h │ ├── lctype.c │ ├── lmem.c │ ├── lcode.h │ ├── lopcodes.c │ ├── lparser.h │ ├── ldump.c │ ├── lcorolib.c │ └── lfunc.c └── libelf │ ├── common │ └── native-elf-format │ ├── elf_fill.c │ ├── gelf_getclass.c │ ├── elf_errno.c │ ├── elf.c │ ├── elf_kind.c │ ├── elf_getarhdr.c │ ├── elf_memory.c │ ├── elf_getbase.c │ ├── elf_version.c │ ├── elf_rawfile.c │ ├── elf_hash.c │ ├── libelf_shdr.c │ ├── elf_cntl.c │ ├── gelf_checksum.c │ ├── elf_rand.c │ ├── elf_getarsym.c │ ├── gelf_fsize.c │ ├── elf_open.c │ ├── elf_getident.c │ ├── elf_next.c │ ├── elf_phnum.c │ ├── elf_shnum.c │ ├── _libelf_ar.h │ ├── elf_shstrndx.c │ ├── gelf_xlate.c │ ├── elf_begin.c │ ├── libelf_data.c │ ├── elf_end.c │ ├── libelf_memory.c │ ├── elf_errmsg.c │ ├── libelf_checksum.c │ ├── libelf_msize.m4 │ ├── libelf_extended.c │ ├── gelf_shdr.c │ ├── gelf_symshndx.c │ ├── elf_strptr.c │ ├── gelf_dyn.c │ ├── gelf_cap.c │ ├── gelf_syminfo.c │ ├── libelf_align.c │ ├── gelf_move.c │ ├── gelf_rel.c │ ├── gelf_sym.c │ ├── libelf_phdr.c │ ├── gelf_rela.c │ ├── libelf_xlate.c │ ├── gelf_ehdr.c │ └── libelf_fsize.m4 ├── .gitignore ├── include └── lw_dependencies.h ├── README.md └── Makefile /examples/no_dep/main.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | print "Hello luawrapper world !" -------------------------------------------------------------------------------- /examples/both_dep/main.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | foo = require "foo" 4 | 5 | foo.foo() 6 | -------------------------------------------------------------------------------- /examples/lua_dep/main.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | plop = require "plop" 4 | 5 | plop.foo() 6 | -------------------------------------------------------------------------------- /examples/lua_dep/foo.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | function M.foo() 4 | print("Hello World!") 5 | end 6 | 7 | return M -------------------------------------------------------------------------------- /examples/both_dep/foo.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | local bar = require "doe" 4 | 5 | function M.foo() 6 | bar.baz("Hello World!") 7 | end 8 | 9 | return M -------------------------------------------------------------------------------- /src/lua/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .cproject 2 | .project 3 | *.o 4 | *.d 5 | *.tar.gz 6 | *.a 7 | native-elf-format.h 8 | src/libelf/libelf_convert.c 9 | src/libelf/libelf_fsize.c 10 | src/libelf/libelf_msize.c 11 | examples/both_dep/both_dep 12 | examples/lua_dep/lua_dep 13 | examples/no_dep/no_dep 14 | -------------------------------------------------------------------------------- /examples/lua_dep/lw_dependencies.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file lw_dependencies.c 3 | * @brief This file must be updated each time a new dependency to a C lua 4 | * library is added. For this test, no C dependency is needed 5 | * 6 | * @date 27 july 2014 7 | * @author carrier.nicolas0@gmail.com 8 | */ 9 | #include 10 | 11 | #include "lw_dependencies.h" 12 | 13 | const struct luaL_Reg lw_dependencies[] = { 14 | {.name = NULL, .func = NULL} /* NULL guard */ 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /examples/examples.mk: -------------------------------------------------------------------------------- 1 | # list of the examples to build 2 | examples := both_dep \ 3 | lua_dep \ 4 | no_dep 5 | 6 | # dependencies for each example 7 | both_dep_OBJS := both_dep/bar.o 8 | both_dep_LUA_DEPS := both_dep/foo.lua \ 9 | both_dep/main.lua 10 | both_dep_CHECK_RESULT := "Hello World!" 11 | 12 | lua_dep_LUA_DEPS := plop:lua_dep/foo.lua \ 13 | lua_dep/main.lua 14 | lua_dep_CHECK_RESULT := "Hello World!" 15 | 16 | no_dep_LUA_DEPS := no_dep/main.lua 17 | no_dep_CHECK_RESULT := "Hello luawrapper world !" 18 | -------------------------------------------------------------------------------- /examples/both_dep/lw_dependencies.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file lw_dependencies.c 3 | * @brief This file must be updated each time a new dependency to a C lua 4 | * library is added. For this test, no C dependency is needed 5 | * 6 | * @date 27 july 2014 7 | * @author carrier.nicolas0@gmail.com 8 | */ 9 | #include 10 | 11 | #include "lw_dependencies.h" 12 | 13 | extern int luaopen_bar(lua_State *L); 14 | 15 | const struct luaL_Reg lw_dependencies[] = { 16 | {.name = "doe", .func = luaopen_bar}, 17 | 18 | {.name = NULL, .func = NULL} /* NULL guard */ 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /include/lw_dependencies.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file lw_dependencies.h 3 | * @brief This header declares the list of dependencies the lua wrapper will 4 | * load at startup, before running the main script. lw_dependencies.c must be 5 | * updated to list them all. 6 | * 7 | * @date 24 juin 2014 8 | * @author carrier.nicolas0@gmail.com 9 | */ 10 | 11 | #ifndef LW_DEPENDENCIES_H_ 12 | #define LW_DEPENDENCIES_H_ 13 | 14 | #include 15 | 16 | /** 17 | * @var lw_dependencies 18 | * @brief list of the C lua modules which will be loaded at startup 19 | */ 20 | extern const struct luaL_Reg lw_dependencies[]; 21 | 22 | #endif /* LW_DEPENDENCIES_H_ */ 23 | -------------------------------------------------------------------------------- /src/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /examples/both_dep/bar.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file bar.c 3 | * @brief 4 | * 5 | * @date 6 août 2014 6 | * @author carrier.nicolas0@gmail.com 7 | */ 8 | #include 9 | #include 10 | 11 | static int l_baz(lua_State *L) 12 | { 13 | int ret; 14 | const char *msg; 15 | 16 | msg = luaL_checkstring(L, 1); 17 | 18 | ret = puts(msg); 19 | if (ret == EOF) 20 | return luaL_error(L, "puts returned %d", ret); 21 | 22 | lua_pushinteger(L, ret); 23 | 24 | return 1; 25 | } 26 | 27 | static const struct luaL_Reg bar_reg[] = { 28 | {"baz", l_baz}, 29 | 30 | { NULL, NULL, } /* sentinel */ 31 | }; 32 | 33 | int luaopen_bar(lua_State *L) 34 | { 35 | luaL_newlib(L, bar_reg); 36 | 37 | return 1; 38 | } 39 | -------------------------------------------------------------------------------- /src/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39 2012/05/08 13:53:33 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | CFLAGS ?= -Os 3 | CPPFLAGS ?= 4 | CPPFLAGS += \ 5 | -I../include/ \ 6 | -I../src/lua/ 7 | Q ?= @ 8 | 9 | include examples.mk 10 | 11 | bin := $(foreach t,$(examples),$(t)/$(t)) 12 | checks := $(foreach t,$(examples),$(t)_CHECK) 13 | 14 | all: $(bin) 15 | 16 | check: all $(checks) 17 | 18 | .SECONDEXPANSION: 19 | $(bin): $$($$(notdir $$@)_OBJS) $$(notdir $$@)/lw_dependencies.o ../luawrapper.a 20 | @echo linking $@ 21 | $(Q) ($(CC) -o $@.$$$$ -static -Os $^ -lm; \ 22 | TARGET_OBJCOPY=$(TARGET_OBJCOPY) ../build_wrapper.sh \ 23 | -o $@ $@.$$$$ $($(notdir $@)_LUA_DEPS); \ 24 | rm $@.$$$$) 25 | 26 | $(checks): $$(patsubst %_CHECK,%,$$@)/$$(patsubst %_CHECK,%,$$@) 27 | $(Q) ./$< | grep $($(@)_RESULT) > /dev/null 28 | @echo "*** $@ TEST PASSED" 29 | 30 | clean: 31 | $(Q) -rm -f $(bin) 32 | $(Q) -(rm -f $$(find -name '*.o')) 33 | 34 | .PHONY: all $(checks) clean check 35 | -------------------------------------------------------------------------------- /src/lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8 2012/05/08 13:53:33 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /src/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/libelf/common/native-elf-format: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # $Id: native-elf-format 2064 2011-10-26 15:12:32Z jkoshy $ 4 | # 5 | # Find the native ELF format for a host platform by compiling a 6 | # test object and examining the resulting object. 7 | # 8 | # This script is used if there is no easy way to determine this 9 | # information statically at compile time. 10 | 11 | program=`basename $0` 12 | tmp_c=`mktemp -u nefXXXXXX`.c 13 | tmp_o=`echo ${tmp_c} | sed -e 's/.c$/.o/'` 14 | 15 | trap "rm -f ${tmp_c} ${tmp_o}" 0 1 2 3 15 16 | 17 | touch ${tmp_c} 18 | 19 | echo "/* Generated by ${program} on `date` */" 20 | 21 | cc -c ${tmp_c} -o ${tmp_o} 22 | readelf -h ${tmp_o} | awk ' 23 | $1 ~ "Class:" { 24 | sub("ELF","",$2); elfclass = $2; 25 | } 26 | $1 ~ "Data:" { 27 | if (match($0, "little")) { 28 | elfdata = "LSB"; 29 | } else { 30 | elfdata = "MSB"; 31 | } 32 | } 33 | $1 ~ "Machine:" { 34 | if (match($0, "Intel.*386")) { 35 | elfarch = "EM_386"; 36 | } else if (match($0, ".*X86-64")) { 37 | elfarch = "EM_X86_64"; 38 | } else { 39 | elfarch = "unknown"; 40 | } 41 | } 42 | END { 43 | printf("#define ELFTC_CLASS ELFCLASS%s\n", elfclass); 44 | printf("#define ELFTC_ARCH %s\n", elfarch); 45 | printf("#define ELFTC_BYTEORDER ELFDATA2%s\n", elfdata); 46 | }' 47 | 48 | -------------------------------------------------------------------------------- /src/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16 2011/08/17 20:26:47 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 22 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 25 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L); 28 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 29 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 30 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 31 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 32 | LUAI_FUNC int luaH_getn (Table *t); 33 | 34 | 35 | #if defined(LUA_DEBUG) 36 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 37 | LUAI_FUNC int luaH_isdummy (Node *n); 38 | #endif 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /src/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.18 2013/01/08 14:06:55 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) 23 | 24 | 25 | /* not to called directly */ 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 27 | 28 | 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_finishOp (lua_State *L); 38 | LUAI_FUNC void luaV_execute (lua_State *L); 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 40 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 41 | const TValue *rc, TMS op); 42 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20 2011/11/29 15:55:08 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /src/libelf/elf_fill.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_fill.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | void 36 | elf_fill(int fill) 37 | { 38 | LIBELF_PRIVATE(fillchar) = fill; 39 | } 40 | -------------------------------------------------------------------------------- /src/libelf/gelf_getclass.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: gelf_getclass.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | int 36 | gelf_getclass(Elf *e) 37 | { 38 | return (e != NULL ? e->e_class : ELFCLASSNONE); 39 | } 40 | -------------------------------------------------------------------------------- /src/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/libelf/elf_errno.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008,2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_errno.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | int 36 | elf_errno(void) 37 | { 38 | int old; 39 | 40 | old = LIBELF_PRIVATE(error); 41 | LIBELF_PRIVATE(error) = 0; 42 | return (old & LIBELF_ELF_ERROR_MASK); 43 | } 44 | -------------------------------------------------------------------------------- /examples/no_dep/lw_dependencies.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file lw_dependencies.c 3 | * @brief This file must be updated each time a new dependency to a C lua 4 | * library is added. For this test, no C dependency is needed 5 | * 6 | * @date 27 july 2014 7 | * @author carrier.nicolas0@gmail.com 8 | */ 9 | 10 | /****************************************************************************** 11 | * Copyright (C) 2015 Nicolas Carrier 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining 14 | * a copy of this software and associated documentation files (the 15 | * "Software"), to deal in the Software without restriction, including 16 | * without limitation the rights to use, copy, modify, merge, publish, 17 | * distribute, sublicense, and/or sell copies of the Software, and to 18 | * permit persons to whom the Software is furnished to do so, subject to 19 | * the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be 22 | * included in all copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 27 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 28 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 29 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 30 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 | ******************************************************************************/ 32 | 33 | #include 34 | 35 | #include "lw_dependencies.h" 36 | 37 | const struct luaL_Reg lw_dependencies[] = { 38 | {.name = NULL, .func = NULL} /* NULL guard */ 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /src/libelf/elf.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008,2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id: elf.c 2225 2011-11-26 18:55:54Z jkoshy $"); 32 | 33 | struct _libelf_globals _libelf = { 34 | .libelf_arch = LIBELF_ARCH, 35 | .libelf_byteorder = LIBELF_BYTEORDER, 36 | .libelf_class = LIBELF_CLASS, 37 | .libelf_error = 0, 38 | .libelf_fillchar = 0, 39 | .libelf_version = EV_NONE 40 | }; 41 | -------------------------------------------------------------------------------- /src/libelf/elf_kind.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_kind.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | Elf_Kind 36 | elf_kind(Elf *e) 37 | { 38 | if (e == NULL) 39 | return (ELF_K_NONE); 40 | if (e->e_kind == ELF_K_AR || 41 | e->e_kind == ELF_K_ELF) 42 | return (e->e_kind); 43 | return (ELF_K_NONE); 44 | } 45 | -------------------------------------------------------------------------------- /src/libelf/elf_getarhdr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008,2010 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_getarhdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | Elf_Arhdr * 36 | elf_getarhdr(Elf *e) 37 | { 38 | if (e == NULL) { 39 | LIBELF_SET_ERROR(ARGUMENT, 0); 40 | return (NULL); 41 | } 42 | 43 | if (e->e_flags & LIBELF_F_AR_HEADER) 44 | return (e->e_hdr.e_arhdr); 45 | 46 | return (_libelf_ar_gethdr(e)); 47 | } 48 | -------------------------------------------------------------------------------- /src/libelf/elf_memory.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id: elf_memory.c 2368 2011-12-29 06:34:28Z jkoshy $"); 32 | 33 | Elf * 34 | elf_memory(char *image, size_t sz) 35 | { 36 | if (LIBELF_PRIVATE(version) == EV_NONE) { 37 | LIBELF_SET_ERROR(SEQUENCE, 0); 38 | return (NULL); 39 | } 40 | 41 | if (image == NULL || sz == 0) { 42 | LIBELF_SET_ERROR(ARGUMENT, 0); 43 | return (NULL); 44 | } 45 | 46 | return (_libelf_memory(image, sz, 1)); 47 | } 48 | -------------------------------------------------------------------------------- /src/libelf/elf_getbase.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_getbase.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | off_t 36 | elf_getbase(Elf *e) 37 | { 38 | if (e == NULL) { 39 | LIBELF_SET_ERROR(ARGUMENT, 0); 40 | return ((off_t) -1); 41 | } 42 | 43 | if (e->e_parent == NULL) 44 | return ((off_t) 0); 45 | 46 | return ((off_t) ((uintptr_t) e->e_rawfile - 47 | (uintptr_t) e->e_parent->e_rawfile)); 48 | } 49 | -------------------------------------------------------------------------------- /src/lua/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/lua/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /src/libelf/elf_version.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_version.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | unsigned int 36 | elf_version(unsigned int v) 37 | { 38 | unsigned int old; 39 | 40 | if ((old = LIBELF_PRIVATE(version)) == EV_NONE) 41 | old = EV_CURRENT; 42 | 43 | if (v == EV_NONE) 44 | return old; 45 | if (v > EV_CURRENT) { 46 | LIBELF_SET_ERROR(VERSION, 0); 47 | return EV_NONE; 48 | } 49 | 50 | LIBELF_PRIVATE(version) = v; 51 | return (old); 52 | } 53 | -------------------------------------------------------------------------------- /src/libelf/elf_rawfile.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_rawfile.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | char * 36 | elf_rawfile(Elf *e, size_t *sz) 37 | { 38 | char *ptr; 39 | size_t size; 40 | 41 | size = e ? e->e_rawsize : 0; 42 | ptr = NULL; 43 | 44 | if (e == NULL) 45 | LIBELF_SET_ERROR(ARGUMENT, 0); 46 | else if ((ptr = e->e_rawfile) == NULL && e->e_cmd == ELF_C_WRITE) 47 | LIBELF_SET_ERROR(SEQUENCE, 0); 48 | 49 | if (sz) 50 | *sz = size; 51 | 52 | return (ptr); 53 | } 54 | -------------------------------------------------------------------------------- /src/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.40 2013/02/20 14:08:21 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is 19 | ** always constant. 20 | ** The macro is somewhat complex to avoid warnings: 21 | ** +1 avoids warnings of "comparison has constant result"; 22 | ** cast to 'void' avoids warnings of "value unused". 23 | */ 24 | #define luaM_reallocv(L,b,on,n,e) \ 25 | (cast(void, \ 26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ 27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 28 | 29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 32 | 33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 35 | #define luaM_newvector(L,n,t) \ 36 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 37 | 38 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 39 | 40 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41 | if ((nelems)+1 > (size)) \ 42 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 43 | 44 | #define luaM_reallocvector(L, v,oldn,n,t) \ 45 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 46 | 47 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 48 | 49 | /* not to be called directly */ 50 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 51 | size_t size); 52 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 53 | size_t size_elem, int limit, 54 | const char *what); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /src/libelf/elf_hash.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_hash.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | /* 36 | * This elf_hash function is defined by the System V ABI. 37 | */ 38 | 39 | unsigned long 40 | elf_hash(const char *name) 41 | { 42 | unsigned long h, t; 43 | const unsigned char *s; 44 | 45 | s = (const unsigned char *) name; 46 | h = t = 0; 47 | 48 | for (; *s != '\0'; h = h & ~t) { 49 | h = (h << 4) + *s++; 50 | t = h & 0xF0000000UL; 51 | if (t) 52 | h ^= t >> 24; 53 | } 54 | 55 | return (h); 56 | } 57 | -------------------------------------------------------------------------------- /src/libelf/libelf_shdr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: libelf_shdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | void * 37 | _libelf_getshdr(Elf_Scn *s, int ec) 38 | { 39 | Elf *e; 40 | 41 | if (s == NULL || (e = s->s_elf) == NULL || 42 | e->e_kind != ELF_K_ELF) { 43 | LIBELF_SET_ERROR(ARGUMENT, 0); 44 | return (NULL); 45 | } 46 | 47 | if (ec == ELFCLASSNONE) 48 | ec = e->e_class; 49 | 50 | if (ec != e->e_class) { 51 | LIBELF_SET_ERROR(CLASS, 0); 52 | return (NULL); 53 | } 54 | 55 | return ((void *) &s->s_shdr); 56 | } 57 | -------------------------------------------------------------------------------- /src/libelf/elf_cntl.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id: elf_cntl.c 2225 2011-11-26 18:55:54Z jkoshy $"); 32 | 33 | int 34 | elf_cntl(Elf *e, Elf_Cmd c) 35 | { 36 | if (e == NULL || 37 | (c != ELF_C_FDDONE && c != ELF_C_FDREAD)) { 38 | LIBELF_SET_ERROR(ARGUMENT, 0); 39 | return (-1); 40 | } 41 | 42 | if (e->e_parent) { 43 | LIBELF_SET_ERROR(ARCHIVE, 0); 44 | return (-1); 45 | } 46 | 47 | if (c == ELF_C_FDREAD) { 48 | if (e->e_cmd == ELF_C_WRITE) { 49 | LIBELF_SET_ERROR(MODE, 0); 50 | return (-1); 51 | } 52 | else 53 | return (0); 54 | } 55 | 56 | e->e_fd = -1; 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /src/libelf/gelf_checksum.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: gelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | long 37 | elf32_checksum(Elf *e) 38 | { 39 | return (_libelf_checksum(e, ELFCLASS32)); 40 | } 41 | 42 | long 43 | elf64_checksum(Elf *e) 44 | { 45 | return (_libelf_checksum(e, ELFCLASS64)); 46 | } 47 | 48 | long 49 | gelf_checksum(Elf *e) 50 | { 51 | int ec; 52 | if (e == NULL || 53 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (0L); 56 | } 57 | return (_libelf_checksum(e, ec)); 58 | } 59 | -------------------------------------------------------------------------------- /src/lua/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/libelf/elf_rand.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: elf_rand.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | off_t 37 | elf_rand(Elf *ar, off_t offset) 38 | { 39 | struct ar_hdr *arh; 40 | 41 | if (ar == NULL || ar->e_kind != ELF_K_AR || 42 | (offset & 1) || offset < SARMAG || 43 | offset + sizeof(struct ar_hdr) >= ar->e_rawsize) { 44 | LIBELF_SET_ERROR(ARGUMENT, 0); 45 | return 0; 46 | } 47 | 48 | arh = (struct ar_hdr *) (ar->e_rawfile + offset); 49 | 50 | /* a too simple sanity check */ 51 | if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') { 52 | LIBELF_SET_ERROR(ARCHIVE, 0); 53 | return 0; 54 | } 55 | 56 | ar->e_u.e_ar.e_next = offset; 57 | 58 | return (offset); 59 | } 60 | -------------------------------------------------------------------------------- /src/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /src/libelf/elf_getarsym.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: elf_getarsym.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | Elf_Arsym * 36 | elf_getarsym(Elf *ar, size_t *ptr) 37 | { 38 | size_t n; 39 | Elf_Arsym *symtab; 40 | 41 | n = 0; 42 | symtab = NULL; 43 | 44 | if (ar == NULL || ar->e_kind != ELF_K_AR) 45 | LIBELF_SET_ERROR(ARGUMENT, 0); 46 | else if ((symtab = ar->e_u.e_ar.e_symtab) != NULL) 47 | n = ar->e_u.e_ar.e_symtabsz; 48 | else if (ar->e_u.e_ar.e_rawsymtab) 49 | symtab = (ar->e_flags & LIBELF_F_AR_VARIANT_SVR4) ? 50 | _libelf_ar_process_svr4_symtab(ar, &n) : 51 | _libelf_ar_process_bsd_symtab(ar, &n); 52 | else 53 | LIBELF_SET_ERROR(ARCHIVE, 0); 54 | 55 | if (ptr) 56 | *ptr = n; 57 | return (symtab); 58 | } 59 | -------------------------------------------------------------------------------- /src/libelf/gelf_fsize.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: gelf_fsize.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | size_t 37 | elf32_fsize(Elf_Type t, size_t c, unsigned int v) 38 | { 39 | return (_libelf_fsize(t, ELFCLASS32, v, c)); 40 | } 41 | 42 | size_t 43 | elf64_fsize(Elf_Type t, size_t c, unsigned int v) 44 | { 45 | return (_libelf_fsize(t, ELFCLASS64, v, c)); 46 | } 47 | 48 | size_t 49 | gelf_fsize(Elf *e, Elf_Type t, size_t c, unsigned int v) 50 | { 51 | 52 | if (e == NULL) { 53 | LIBELF_SET_ERROR(ARGUMENT, 0); 54 | return (0); 55 | } 56 | 57 | if (e->e_class == ELFCLASS32 || e->e_class == ELFCLASS64) 58 | return (_libelf_fsize(t, e->e_class, v, c)); 59 | 60 | LIBELF_SET_ERROR(ARGUMENT, 0); 61 | return (0); 62 | } 63 | -------------------------------------------------------------------------------- /src/libelf/elf_open.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id$"); 32 | 33 | /* 34 | * Extension API: open a file for reading, ignoring parse errors. 35 | */ 36 | 37 | Elf * 38 | elf_open(int fd) 39 | { 40 | if (LIBELF_PRIVATE(version) == EV_NONE) { 41 | LIBELF_SET_ERROR(SEQUENCE, 0); 42 | return (NULL); 43 | } 44 | 45 | return (_libelf_open_object(fd, ELF_C_READ, 0)); 46 | } 47 | 48 | /* 49 | * Extension API: create an ELF descriptor for an in-memory object, 50 | * ignoring parse errors. 51 | */ 52 | 53 | Elf * 54 | elf_openmemory(char *image, size_t sz) 55 | { 56 | if (LIBELF_PRIVATE(version) == EV_NONE) { 57 | LIBELF_SET_ERROR(SEQUENCE, 0); 58 | return (NULL); 59 | } 60 | 61 | if (image == NULL || sz == 0) { 62 | LIBELF_SET_ERROR(ARGUMENT, 0); 63 | return (NULL); 64 | } 65 | 66 | return (_libelf_memory(image, sz, 0)); 67 | } 68 | -------------------------------------------------------------------------------- /src/libelf/elf_getident.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: elf_getident.c 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | char * 38 | elf_getident(Elf *e, size_t *sz) 39 | { 40 | 41 | if (e == NULL) { 42 | LIBELF_SET_ERROR(ARGUMENT, 0); 43 | goto error; 44 | } 45 | 46 | if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) { 47 | LIBELF_SET_ERROR(SEQUENCE, 0); 48 | goto error; 49 | } 50 | 51 | assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ); 52 | 53 | if (sz) { 54 | if (e->e_kind == ELF_K_AR) 55 | *sz = SARMAG; 56 | else if (e->e_kind == ELF_K_ELF) 57 | *sz = EI_NIDENT; 58 | else 59 | *sz = e->e_rawsize; 60 | } 61 | 62 | return ((char *) e->e_rawfile); 63 | 64 | error: 65 | if (sz) 66 | *sz = 0; 67 | return (NULL); 68 | } 69 | -------------------------------------------------------------------------------- /src/libelf/elf_next.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: elf_next.c 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | Elf_Cmd 38 | elf_next(Elf *e) 39 | { 40 | off_t next; 41 | Elf *parent; 42 | 43 | if (e == NULL) 44 | return (ELF_C_NULL); 45 | 46 | if ((parent = e->e_parent) == NULL) { 47 | LIBELF_SET_ERROR(ARGUMENT, 0); 48 | return (ELF_C_NULL); 49 | } 50 | 51 | assert (parent->e_kind == ELF_K_AR); 52 | assert (parent->e_cmd == ELF_C_READ); 53 | assert(e->e_rawfile > parent->e_rawfile); 54 | 55 | next = e->e_rawfile - parent->e_rawfile + e->e_rawsize; 56 | next = (next + 1) & ~1; /* round up to an even boundary */ 57 | 58 | parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ? 59 | (off_t) 0 : next; 60 | 61 | return (ELF_C_READ); 62 | } 63 | -------------------------------------------------------------------------------- /src/libelf/elf_phnum.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: elf_phnum.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | static int 37 | _libelf_getphdrnum(Elf *e, size_t *phnum) 38 | { 39 | void *eh; 40 | int ec; 41 | 42 | if (e == NULL || e->e_kind != ELF_K_ELF || 43 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 44 | LIBELF_SET_ERROR(ARGUMENT, 0); 45 | return (-1); 46 | } 47 | 48 | if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) 49 | return (-1); 50 | 51 | *phnum = e->e_u.e_elf.e_nphdr; 52 | 53 | return (0); 54 | } 55 | 56 | int 57 | elf_getphdrnum(Elf *e, size_t *phnum) 58 | { 59 | return (_libelf_getphdrnum(e, phnum)); 60 | } 61 | 62 | /* Deprecated API */ 63 | int 64 | elf_getphnum(Elf *e, size_t *phnum) 65 | { 66 | return (_libelf_getphdrnum(e, phnum) >= 0); 67 | } 68 | -------------------------------------------------------------------------------- /src/libelf/elf_shnum.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: elf_shnum.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | static int 37 | _libelf_getshdrnum(Elf *e, size_t *shnum) 38 | { 39 | void *eh; 40 | int ec; 41 | 42 | if (e == NULL || e->e_kind != ELF_K_ELF || 43 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 44 | LIBELF_SET_ERROR(ARGUMENT, 0); 45 | return (-1); 46 | } 47 | 48 | if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) 49 | return (-1); 50 | 51 | *shnum = e->e_u.e_elf.e_nscn; 52 | 53 | return (0); 54 | } 55 | 56 | int 57 | elf_getshdrnum(Elf *e, size_t *shnum) 58 | { 59 | return (_libelf_getshdrnum(e, shnum)); 60 | } 61 | 62 | /* Deprecated API. */ 63 | int 64 | elf_getshnum(Elf *e, size_t *shnum) 65 | { 66 | return (_libelf_getshdrnum(e, shnum) >= 0); 67 | } 68 | -------------------------------------------------------------------------------- /src/libelf/_libelf_ar.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2010 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | * 26 | * $Id: _libelf_ar.h 2032 2011-10-23 09:07:00Z jkoshy $ 27 | */ 28 | 29 | #ifndef __LIBELF_AR_H_ 30 | #define __LIBELF_AR_H_ 31 | 32 | /* 33 | * Prototypes and declarations needed by libelf's ar(1) archive 34 | * handling code. 35 | */ 36 | 37 | #include 38 | 39 | #define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX "#1/" 40 | #define LIBELF_AR_BSD_SYMTAB_NAME "__.SYMDEF" 41 | #define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE \ 42 | (sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1) 43 | 44 | #define IS_EXTENDED_BSD_NAME(NAME) \ 45 | (strncmp((NAME), LIBELF_AR_BSD_EXTENDED_NAME_PREFIX, \ 46 | LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0) 47 | 48 | 49 | char *_libelf_ar_get_string(const char *_buf, size_t _sz, int _rawname, 50 | int _svr4names); 51 | char *_libelf_ar_get_raw_name(const struct ar_hdr *_arh); 52 | char *_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar); 53 | int _libelf_ar_get_number(const char *_buf, size_t _sz, int _base, 54 | size_t *_ret); 55 | 56 | #endif /* __LIBELF_AR_H_ */ 57 | -------------------------------------------------------------------------------- /src/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/lua/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /src/libelf/elf_shstrndx.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: elf_shstrndx.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | static int 37 | _libelf_getshdrstrndx(Elf *e, size_t *strndx) 38 | { 39 | void *eh; 40 | int ec; 41 | 42 | if (e == NULL || e->e_kind != ELF_K_ELF || 43 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 44 | LIBELF_SET_ERROR(ARGUMENT, 0); 45 | return (-1); 46 | } 47 | 48 | if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) 49 | return (-1); 50 | 51 | *strndx = e->e_u.e_elf.e_strndx; 52 | 53 | return (0); 54 | } 55 | 56 | int 57 | elf_getshdrstrndx(Elf *e, size_t *strndx) 58 | { 59 | return (_libelf_getshdrstrndx(e, strndx)); 60 | } 61 | 62 | int 63 | elf_getshstrndx(Elf *e, size_t *strndx) /* Deprecated API. */ 64 | { 65 | return (_libelf_getshdrstrndx(e, strndx) >= 0); 66 | } 67 | 68 | int 69 | elf_setshstrndx(Elf *e, size_t strndx) 70 | { 71 | void *eh; 72 | int ec; 73 | 74 | if (e == NULL || e->e_kind != ELF_K_ELF || 75 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || 76 | ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) { 77 | LIBELF_SET_ERROR(ARGUMENT, 0); 78 | return (0); 79 | } 80 | 81 | return (_libelf_setshstrndx(e, eh, ec, strndx)); 82 | } 83 | -------------------------------------------------------------------------------- /src/libelf/gelf_xlate.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_xlate.c 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | Elf_Data * 38 | elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) 39 | { 40 | return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE); 41 | } 42 | 43 | Elf_Data * 44 | elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) 45 | { 46 | return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE); 47 | } 48 | 49 | Elf_Data * 50 | elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) 51 | { 52 | return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY); 53 | } 54 | 55 | Elf_Data * 56 | elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) 57 | { 58 | return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY); 59 | } 60 | 61 | Elf_Data * 62 | gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src, 63 | unsigned int encoding) 64 | { 65 | if (e != NULL) 66 | return (_libelf_xlate(dst, src, encoding, e->e_class, 67 | ELF_TOMEMORY)); 68 | LIBELF_SET_ERROR(ARGUMENT, 0); 69 | return (NULL); 70 | } 71 | 72 | Elf_Data * 73 | gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src, 74 | unsigned int encoding) 75 | { 76 | if (e != NULL) 77 | return (_libelf_xlate(dst, src, encoding, e->e_class, 78 | ELF_TOFILE)); 79 | LIBELF_SET_ERROR(ARGUMENT, 0); 80 | return (NULL); 81 | } 82 | -------------------------------------------------------------------------------- /src/libelf/elf_begin.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008-2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id: elf_begin.c 2364 2011-12-28 17:55:25Z jkoshy $"); 32 | 33 | Elf * 34 | elf_begin(int fd, Elf_Cmd c, Elf *a) 35 | { 36 | Elf *e; 37 | 38 | e = NULL; 39 | 40 | if (LIBELF_PRIVATE(version) == EV_NONE) { 41 | LIBELF_SET_ERROR(SEQUENCE, 0); 42 | return (NULL); 43 | } 44 | 45 | switch (c) { 46 | case ELF_C_NULL: 47 | return (NULL); 48 | 49 | case ELF_C_WRITE: 50 | /* 51 | * The ELF_C_WRITE command is required to ignore the 52 | * descriptor passed in. 53 | */ 54 | a = NULL; 55 | break; 56 | 57 | case ELF_C_RDWR: 58 | if (a != NULL) { /* not allowed for ar(1) archives. */ 59 | LIBELF_SET_ERROR(ARGUMENT, 0); 60 | return (NULL); 61 | } 62 | /*FALLTHROUGH*/ 63 | case ELF_C_READ: 64 | /* 65 | * Descriptor `a' could be for a regular ELF file, or 66 | * for an ar(1) archive. If descriptor `a' was opened 67 | * using a valid file descriptor, we need to check if 68 | * the passed in `fd' value matches the original one. 69 | */ 70 | if (a && 71 | ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) { 72 | LIBELF_SET_ERROR(ARGUMENT, 0); 73 | return (NULL); 74 | } 75 | break; 76 | 77 | default: 78 | LIBELF_SET_ERROR(ARGUMENT, 0); 79 | return (NULL); 80 | 81 | } 82 | 83 | if (a == NULL) 84 | e = _libelf_open_object(fd, c, 1); 85 | else if (a->e_kind == ELF_K_AR) 86 | e = _libelf_ar_open_member(a->e_fd, c, a); 87 | else 88 | (e = a)->e_activations++; 89 | 90 | return (e); 91 | } 92 | -------------------------------------------------------------------------------- /src/libelf/libelf_data.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: libelf_data.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | int 36 | _libelf_xlate_shtype(uint32_t sht) 37 | { 38 | switch (sht) { 39 | case SHT_DYNAMIC: 40 | return (ELF_T_DYN); 41 | case SHT_DYNSYM: 42 | return (ELF_T_SYM); 43 | case SHT_FINI_ARRAY: 44 | return (ELF_T_ADDR); 45 | case SHT_GNU_HASH: 46 | return (ELF_T_GNUHASH); 47 | case SHT_GNU_LIBLIST: 48 | return (ELF_T_WORD); 49 | case SHT_GROUP: 50 | return (ELF_T_WORD); 51 | case SHT_HASH: 52 | return (ELF_T_WORD); 53 | case SHT_INIT_ARRAY: 54 | return (ELF_T_ADDR); 55 | case SHT_NOBITS: 56 | return (ELF_T_BYTE); 57 | case SHT_NOTE: 58 | return (ELF_T_NOTE); 59 | case SHT_PREINIT_ARRAY: 60 | return (ELF_T_ADDR); 61 | case SHT_PROGBITS: 62 | return (ELF_T_BYTE); 63 | case SHT_REL: 64 | return (ELF_T_REL); 65 | case SHT_RELA: 66 | return (ELF_T_RELA); 67 | case SHT_STRTAB: 68 | return (ELF_T_BYTE); 69 | case SHT_SYMTAB: 70 | return (ELF_T_SYM); 71 | case SHT_SYMTAB_SHNDX: 72 | return (ELF_T_WORD); 73 | case SHT_SUNW_dof: 74 | return (ELF_T_BYTE); 75 | case SHT_SUNW_move: 76 | return (ELF_T_MOVE); 77 | case SHT_SUNW_syminfo: 78 | return (ELF_T_SYMINFO); 79 | case SHT_SUNW_verdef: /* == SHT_GNU_verdef */ 80 | return (ELF_T_VDEF); 81 | case SHT_SUNW_verneed: /* == SHT_GNU_verneed */ 82 | return (ELF_T_VNEED); 83 | case SHT_SUNW_versym: /* == SHT_GNU_versym */ 84 | return (ELF_T_HALF); 85 | default: 86 | return (-1); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/libelf/elf_end.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008-2009,2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | #if ELFTC_HAVE_MMAP 36 | #include 37 | #endif 38 | 39 | ELFTC_VCSID("$Id: elf_end.c 2240 2011-11-28 06:36:48Z jkoshy $"); 40 | 41 | int 42 | elf_end(Elf *e) 43 | { 44 | Elf *sv; 45 | Elf_Scn *scn, *tscn; 46 | 47 | if (e == NULL || e->e_activations == 0) 48 | return (0); 49 | 50 | if (--e->e_activations > 0) 51 | return (e->e_activations); 52 | 53 | assert(e->e_activations == 0); 54 | 55 | while (e && e->e_activations == 0) { 56 | switch (e->e_kind) { 57 | case ELF_K_AR: 58 | /* 59 | * If we still have open child descriptors, we 60 | * need to defer reclaiming resources till all 61 | * the child descriptors for the archive are 62 | * closed. 63 | */ 64 | if (e->e_u.e_ar.e_nchildren > 0) 65 | return (0); 66 | break; 67 | case ELF_K_ELF: 68 | /* 69 | * Reclaim all section descriptors. 70 | */ 71 | STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, 72 | tscn) 73 | scn = _libelf_release_scn(scn); 74 | break; 75 | case ELF_K_NUM: 76 | assert(0); 77 | default: 78 | break; 79 | } 80 | 81 | if (e->e_rawfile) { 82 | if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) 83 | free(e->e_rawfile); 84 | #if ELFTC_HAVE_MMAP 85 | else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) 86 | (void) munmap(e->e_rawfile, e->e_rawsize); 87 | #endif 88 | } 89 | 90 | sv = e; 91 | if ((e = e->e_parent) != NULL) 92 | e->e_u.e_ar.e_nchildren--; 93 | sv = _libelf_release_elf(sv); 94 | } 95 | 96 | return (0); 97 | } 98 | -------------------------------------------------------------------------------- /src/lua/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Luawrapper 2 | 3 | ## Overview 4 | 5 | Luawrapper is a way to build a fully autonomous statically precompiled binary 6 | from a lua script. To do so, it must contain both the C and lua libraries the 7 | lua script depends on. 8 | 9 | ## Description 10 | 11 | Luawrapper implements a small lua interpreter. The needed lua scripts, 12 | dependencies and the main lua script, are embedded in the final executable, as 13 | elf sections. The C dependencies are compiled into the main executable so that 14 | their corresponding module initialization functions are accessible 15 | (luaopen\_XXX). 16 | 17 | Both lua and C dependencies are preloaded with the package.preload lua facility. 18 | So, when the main lua executable is interpreted, the needed dependencies, be 19 | they C, or lua, are loaded on demand, i.e. when they are "require"-d. 20 | 21 | ## Usage 22 | 23 | ### With the provided build system 24 | 25 | The **examples** directory contains some use cases which can be used as 26 | examples. These can be modified or a new example can be created easily by 27 | tweaking examples.mk and adding the corresponding directory and files in the 28 | examples folder. 29 | 30 | The process to do this by hand is explained in the following sections. 31 | 32 | ### Building the executable 33 | 34 | C libraries must be compiled into the main executable. They must also be 35 | declared in a lw\_dependencies array. Each entry must list the module name, as 36 | when referenced by a call to require and the luaopen\_ function, responsible of 37 | loading the module. 38 | 39 | To build the executable, just : 40 | 41 | 1. build the luawrapper static library: 42 | `$ make` 43 | this produces luawrapper.a at the root 44 | 2. build all your C dependencies as object files or static libraries, e.g.: 45 | `$ gcc foo.c -c -o foo.o # plus relevant flags` 46 | `$ gcc bar.c -c -o bar.o # plus relevant flags ...` 47 | 3. build your C dependencies declaration module (a C file declaring a 48 | `struct luaL_Reg lw_dependencies` NULL-terminated array) : 49 | `$ gcc lw_dependencies.c -o lw_dependencies.o -I src/lua/ -I include` 50 | 4. link all: 51 | `$ gcc -o my_program -static luawrapper.a foo.o bar.o lw_dependencies.o -lm` 52 | 53 | ### Wrapping the lua scripts 54 | 55 | Once the wrapper has been compiled, the lua script and it's lua dependencies 56 | must be wrapped into it. For this purpose, one must use the build\_wrapper.sh 57 | script provided. It will store the lua dependencies in elf sections in the 58 | wrapper, in a way suitable for it to retrieve them. It's usage is: 59 | 60 | `./build_wrapper.sh -o outfile my_program dep1.lua dep2.lua [...] main.lua` 61 | 62 | Then one can use *outfile* like he would use main.lua, but it doesn't rely on 63 | external dependencies anymore. The resulting executable can then be used on any 64 | similar platform. Cross-compilation is also possible by specifying the compiler 65 | to the Makefile and the objcopy utility to the build\_wrapper.sh script via the 66 | TARGET\_OBJCOPY environment variable. 67 | 68 | *Note*: lw\_dependency is needed because we can't load luaopen\_XXX functions 69 | with dlopen, when the executable is built statically. 70 | -------------------------------------------------------------------------------- /src/libelf/libelf_memory.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: libelf_memory.c 2368 2011-12-29 06:34:28Z jkoshy $"); 35 | 36 | /* 37 | * Create an ELF descriptor for a memory image, optionally reporting 38 | * parse errors. 39 | */ 40 | 41 | Elf * 42 | _libelf_memory(char *image, size_t sz, int reporterror) 43 | { 44 | Elf *e; 45 | int e_class; 46 | enum Elf_Error error; 47 | unsigned int e_byteorder, e_version; 48 | 49 | assert(image != NULL); 50 | assert(sz > 0); 51 | 52 | if ((e = _libelf_allocate_elf()) == NULL) 53 | return (NULL); 54 | 55 | e->e_cmd = ELF_C_READ; 56 | e->e_rawfile = image; 57 | e->e_rawsize = sz; 58 | 59 | #undef LIBELF_IS_ELF 60 | #define LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && \ 61 | (P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 && \ 62 | (P)[EI_MAG3] == ELFMAG3) 63 | 64 | if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) { 65 | e_byteorder = image[EI_DATA]; 66 | e_class = image[EI_CLASS]; 67 | e_version = image[EI_VERSION]; 68 | 69 | error = ELF_E_NONE; 70 | 71 | if (e_version > EV_CURRENT) 72 | error = ELF_E_VERSION; 73 | else if ((e_byteorder != ELFDATA2LSB && e_byteorder != 74 | ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class != 75 | ELFCLASS64)) 76 | error = ELF_E_HEADER; 77 | 78 | if (error != ELF_E_NONE) { 79 | if (reporterror) { 80 | LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0); 81 | (void) _libelf_release_elf(e); 82 | return (NULL); 83 | } 84 | } else { 85 | _libelf_init_elf(e, ELF_K_ELF); 86 | 87 | e->e_byteorder = e_byteorder; 88 | e->e_class = e_class; 89 | e->e_version = e_version; 90 | } 91 | } else if (sz >= SARMAG && 92 | strncmp(image, ARMAG, (size_t) SARMAG) == 0) 93 | return (_libelf_ar_open(e, reporterror)); 94 | 95 | return (e); 96 | } 97 | -------------------------------------------------------------------------------- /src/libelf/elf_errmsg.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008,2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: elf_errmsg.c 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | /* 38 | * Retrieve a human readable translation for an error message. 39 | */ 40 | 41 | const char *_libelf_errors[] = { 42 | #define DEFINE_ERROR(N,S) [ELF_E_##N] = S 43 | DEFINE_ERROR(NONE, "No Error"), 44 | DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"), 45 | DEFINE_ERROR(ARGUMENT, "Invalid argument"), 46 | DEFINE_ERROR(CLASS, "ELF class mismatch"), 47 | DEFINE_ERROR(DATA, "Invalid data buffer descriptor"), 48 | DEFINE_ERROR(HEADER, "Missing or malformed ELF header"), 49 | DEFINE_ERROR(IO, "I/O error"), 50 | DEFINE_ERROR(LAYOUT, "Layout constraint violation"), 51 | DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"), 52 | DEFINE_ERROR(RANGE, "Value out of range of target"), 53 | DEFINE_ERROR(RESOURCE, "Resource exhaustion"), 54 | DEFINE_ERROR(SECTION, "Invalid section descriptor"), 55 | DEFINE_ERROR(SEQUENCE, "API calls out of sequence"), 56 | DEFINE_ERROR(UNIMPL, "Unimplemented feature"), 57 | DEFINE_ERROR(VERSION, "Unknown ELF API version"), 58 | DEFINE_ERROR(NUM, "Unknown error") 59 | #undef DEFINE_ERROR 60 | }; 61 | 62 | const char * 63 | elf_errmsg(int error) 64 | { 65 | int oserr; 66 | 67 | if (error == ELF_E_NONE && 68 | (error = LIBELF_PRIVATE(error)) == 0) 69 | return NULL; 70 | else if (error == -1) 71 | error = LIBELF_PRIVATE(error); 72 | 73 | oserr = error >> LIBELF_OS_ERROR_SHIFT; 74 | error &= LIBELF_ELF_ERROR_MASK; 75 | 76 | if (error < ELF_E_NONE || error >= ELF_E_NUM) 77 | return _libelf_errors[ELF_E_NUM]; 78 | if (oserr) { 79 | (void) snprintf(LIBELF_PRIVATE(msg), 80 | sizeof(LIBELF_PRIVATE(msg)), "%s: %s", 81 | _libelf_errors[error], strerror(oserr)); 82 | return (const char *)&LIBELF_PRIVATE(msg); 83 | } 84 | return _libelf_errors[error]; 85 | } 86 | -------------------------------------------------------------------------------- /src/libelf/libelf_checksum.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include "_libelf.h" 32 | 33 | ELFTC_VCSID("$Id: libelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy $"); 34 | 35 | static unsigned long 36 | _libelf_sum(unsigned long c, const unsigned char *s, size_t size) 37 | { 38 | if (s == NULL || size == 0) 39 | return (c); 40 | 41 | while (size--) 42 | c += *s++; 43 | 44 | return (c); 45 | } 46 | 47 | unsigned long 48 | _libelf_checksum(Elf *e, int elfclass) 49 | { 50 | size_t shn; 51 | Elf_Scn *scn; 52 | Elf_Data *d; 53 | unsigned long checksum; 54 | GElf_Ehdr eh; 55 | GElf_Shdr shdr; 56 | 57 | if (e == NULL) { 58 | LIBELF_SET_ERROR(ARGUMENT, 0); 59 | return (0L); 60 | } 61 | 62 | if (e->e_class != elfclass) { 63 | LIBELF_SET_ERROR(CLASS, 0); 64 | return (0L); 65 | } 66 | 67 | if (gelf_getehdr(e, &eh) == NULL) 68 | return (0); 69 | 70 | /* 71 | * Iterate over all sections in the ELF file, computing the 72 | * checksum along the way. 73 | * 74 | * The first section is always SHN_UNDEF and can be skipped. 75 | * Non-allocatable sections are skipped, as are sections that 76 | * could be affected by utilities such as strip(1). 77 | */ 78 | 79 | checksum = 0; 80 | for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) { 81 | if ((scn = elf_getscn(e, shn)) == NULL) 82 | return (0); 83 | if (gelf_getshdr(scn, &shdr) == NULL) 84 | return (0); 85 | if ((shdr.sh_flags & SHF_ALLOC) == 0 || 86 | shdr.sh_type == SHT_DYNAMIC || 87 | shdr.sh_type == SHT_DYNSYM) 88 | continue; 89 | 90 | d = NULL; 91 | while ((d = elf_rawdata(scn, d)) != NULL) 92 | checksum = _libelf_sum(checksum, 93 | (unsigned char *) d->d_buf, d->d_size); 94 | } 95 | 96 | /* 97 | * Return a 16-bit checksum compatible with Solaris. 98 | */ 99 | return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL)); 100 | } 101 | -------------------------------------------------------------------------------- /src/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58 2011/08/30 16:26:41 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /src/libelf/libelf_msize.m4: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008-2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: libelf_msize.m4 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | /* WARNING: GENERATED FROM __file__. */ 38 | 39 | struct msize { 40 | size_t msz32; 41 | size_t msz64; 42 | }; 43 | 44 | divert(-1) 45 | include(SRCDIR`/elf_types.m4') 46 | 47 | /* 48 | * ELF types whose memory representations have a variable size. 49 | */ 50 | define(BYTE_SIZE, 1) 51 | define(GNUHASH_SIZE, 1) 52 | define(NOTE_SIZE, 1) 53 | define(VDEF_SIZE, 1) 54 | define(VNEED_SIZE, 1) 55 | 56 | /* 57 | * Unimplemented types. 58 | */ 59 | define(MOVEP_SIZE, 0) 60 | define(SXWORD_SIZE32, 0) 61 | define(XWORD_SIZE32, 0) 62 | 63 | define(`DEFINE_ELF_MSIZE', 64 | `ifdef($1`_SIZE', 65 | `define($1_SIZE32,$1_SIZE) 66 | define($1_SIZE64,$1_SIZE)', 67 | `ifdef($1`_SIZE32',`', 68 | `define($1_SIZE32,sizeof(Elf32_$2))') 69 | ifdef($1`_SIZE64',`', 70 | `define($1_SIZE64,sizeof(Elf64_$2))')')') 71 | define(`DEFINE_ELF_MSIZES', 72 | `ifelse($#,1,`', 73 | `DEFINE_ELF_MSIZE($1) 74 | DEFINE_ELF_MSIZES(shift($@))')') 75 | 76 | DEFINE_ELF_MSIZES(ELF_TYPE_LIST) 77 | 78 | define(`MSIZE', 79 | `[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 }, 80 | ') 81 | define(`MSIZES', 82 | `ifelse($#,1,`', 83 | `MSIZE($1) 84 | MSIZES(shift($@))')') 85 | 86 | divert(0) 87 | 88 | static struct msize msize[ELF_T_NUM] = { 89 | MSIZES(ELF_TYPE_LIST) 90 | }; 91 | 92 | size_t 93 | _libelf_msize(Elf_Type t, int elfclass, unsigned int version) 94 | { 95 | size_t sz; 96 | 97 | assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); 98 | assert((signed) t >= ELF_T_FIRST && t <= ELF_T_LAST); 99 | 100 | if (version != EV_CURRENT) { 101 | LIBELF_SET_ERROR(VERSION, 0); 102 | return (0); 103 | } 104 | 105 | sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64; 106 | 107 | return (sz); 108 | } 109 | -------------------------------------------------------------------------------- /src/lua/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49 2012/05/14 13:34:18 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /src/lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /src/libelf/libelf_extended.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: libelf_extended.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | /* 37 | * Retrieve section #0, allocating a new section if needed. 38 | */ 39 | static Elf_Scn * 40 | _libelf_getscn0(Elf *e) 41 | { 42 | Elf_Scn *s; 43 | 44 | if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL) 45 | return (s); 46 | 47 | return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF)); 48 | } 49 | 50 | int 51 | _libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum) 52 | { 53 | Elf_Scn *scn; 54 | 55 | if (shnum >= SHN_LORESERVE) { 56 | if ((scn = _libelf_getscn0(e)) == NULL) 57 | return (0); 58 | 59 | assert(scn->s_ndx == SHN_UNDEF); 60 | 61 | if (ec == ELFCLASS32) 62 | scn->s_shdr.s_shdr32.sh_size = shnum; 63 | else 64 | scn->s_shdr.s_shdr64.sh_size = shnum; 65 | 66 | (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); 67 | 68 | shnum = 0; 69 | } 70 | 71 | if (ec == ELFCLASS32) 72 | ((Elf32_Ehdr *) eh)->e_shnum = shnum; 73 | else 74 | ((Elf64_Ehdr *) eh)->e_shnum = shnum; 75 | 76 | 77 | return (1); 78 | } 79 | 80 | int 81 | _libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx) 82 | { 83 | Elf_Scn *scn; 84 | 85 | if (shstrndx >= SHN_LORESERVE) { 86 | if ((scn = _libelf_getscn0(e)) == NULL) 87 | return (0); 88 | 89 | assert(scn->s_ndx == SHN_UNDEF); 90 | 91 | if (ec == ELFCLASS32) 92 | scn->s_shdr.s_shdr32.sh_link = shstrndx; 93 | else 94 | scn->s_shdr.s_shdr64.sh_link = shstrndx; 95 | 96 | (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); 97 | 98 | shstrndx = SHN_XINDEX; 99 | } 100 | 101 | if (ec == ELFCLASS32) 102 | ((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx; 103 | else 104 | ((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx; 105 | 106 | return (1); 107 | } 108 | 109 | int 110 | _libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum) 111 | { 112 | Elf_Scn *scn; 113 | 114 | if (phnum >= PN_XNUM) { 115 | if ((scn = _libelf_getscn0(e)) == NULL) 116 | return (0); 117 | 118 | assert(scn->s_ndx == SHN_UNDEF); 119 | 120 | if (ec == ELFCLASS32) 121 | scn->s_shdr.s_shdr32.sh_info = phnum; 122 | else 123 | scn->s_shdr.s_shdr64.sh_info = phnum; 124 | 125 | (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); 126 | 127 | phnum = PN_XNUM; 128 | } 129 | 130 | if (ec == ELFCLASS32) 131 | ((Elf32_Ehdr *) eh)->e_phnum = phnum; 132 | else 133 | ((Elf64_Ehdr *) eh)->e_phnum = phnum; 134 | 135 | return (1); 136 | } 137 | -------------------------------------------------------------------------------- /src/lua/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Package-related substitution variables 2 | export package = luawrapper 3 | export version = 0.1 4 | export tarname = $(package) 5 | export distdir = $(tarname)-$(version) 6 | 7 | # Prefix-related substitution variables 8 | export prefix = /usr/local 9 | export exec_prefix = $(prefix) 10 | export libdir = $(exec_prefix)/lib 11 | 12 | # related substitution variables 13 | export CC ?= gcc 14 | export CFLAGS ?= -Os 15 | export CPPFLAGS ?= 16 | export AR ?= ar 17 | 18 | export Q ?= @ 19 | 20 | CPPFLAGS += \ 21 | -I. \ 22 | -Iinclude/ \ 23 | -Isrc/libelf/common/ \ 24 | -Isrc/libelf/ \ 25 | -Isrc/lua/ 26 | 27 | luawrapper_gen_src := \ 28 | src/libelf/libelf_msize.c \ 29 | src/libelf/libelf_fsize.c \ 30 | src/libelf/libelf_convert.c 31 | 32 | .SECONDARY: $(luawrapper_gen_src) 33 | 34 | luawrapper_src := \ 35 | $(shell find src -name *.c) \ 36 | $(luawrapper_gen_src) 37 | 38 | luawrapper_objects := $(luawrapper_src:.c=.o) 39 | 40 | luawrapper_clean_files := \ 41 | $(luawrapper_gen_src) \ 42 | native-elf-format.h \ 43 | luawrapper.a \ 44 | $(luawrapper_objects) \ 45 | $(luawrapper_src:.c=.d) 46 | 47 | all: luawrapper.a examples 48 | 49 | native-elf-format.h: 50 | @echo generate platform dependant header $@ 51 | $(Q) LANG=C src/libelf/common/native-elf-format > $@ 52 | 53 | -include $(luawrapper_src:.c=.d) 54 | 55 | %.d: %.c 56 | $(Q) set -e; rm -f $@; \ 57 | $(CC) -MM -MG $(CPPFLAGS) $< | \ 58 | sed 's,\(^[^:]*\):,$(dir $@)\1 $@ : ,g' > $@; \ 59 | 60 | # TODO tester $? 61 | luawrapper.a: $(luawrapper_objects) 62 | @echo creation of $@ 63 | $(Q) $(AR) cr $@ $^ 64 | 65 | src/libelf/libelf_%.c: src/libelf/libelf_%.m4 66 | @echo "generate m4 generated $@ for luawrapper" 67 | $(Q) (cd src/libelf/; m4 -D SRCDIR=. $(notdir $^) > $(notdir $@)) 68 | 69 | examples: luawrapper.a 70 | $(MAKE) -C examples 71 | 72 | clean: 73 | $(Q) -rm -f $(luawrapper_clean_files) &>/dev/null 74 | $(MAKE) -C examples clean 75 | 76 | check: all 77 | $(MAKE) -C examples check 78 | 79 | install: 80 | install -d $(DESTDIR)$(libdir) 81 | install -m 0755 luawrapper.a $(DESTDIR)$(libdir) 82 | 83 | uninstall: 84 | -rm $(DESTDIR)$(libdir)/luawrapper.a &>/dev/null 85 | 86 | dist: $(distdir).tar.gz 87 | 88 | $(distdir).tar.gz: FORCE $(distdir) 89 | tar chof - $(distdir) | gzip -9 -c >$(distdir).tar.gz 90 | rm -rf $(distdir) 91 | 92 | $(distdir): 93 | mkdir -p $(distdir)/src/libelf/common 94 | mkdir -p $(distdir)/src/lua 95 | mkdir -p $(distdir)/include 96 | mkdir -p $(distdir)/examples/both_dep 97 | mkdir -p $(distdir)/examples/lua_dep 98 | mkdir -p $(distdir)/examples/no_dep 99 | cp Makefile $(distdir) 100 | cp build_wrapper.sh $(distdir) 101 | cp include/lw_dependencies.h $(distdir)/include 102 | cp src/luawrapper.c $(distdir)/src 103 | cp src/lua/*.c $(distdir)/src/lua 104 | cp src/lua/*.h $(distdir)/src/lua 105 | cp src/libelf/*.c $(distdir)/src/libelf 106 | cp src/libelf/*.h $(distdir)/src/libelf 107 | cp src/libelf/*.m4 $(distdir)/src/libelf 108 | cp src/libelf/common/*.h $(distdir)/src/libelf/common 109 | cp src/libelf/common/native-elf-format $(distdir)/src/libelf/common 110 | cp examples/Makefile $(distdir)/examples 111 | cp examples/examples.mk $(distdir)/examples 112 | cp examples/both_dep/*.c $(distdir)/examples/both_dep 113 | cp examples/both_dep/*.lua $(distdir)/examples/both_dep 114 | cp examples/no_dep/*.c $(distdir)/examples/no_dep 115 | cp examples/no_dep/*.lua $(distdir)/examples/no_dep 116 | cp examples/lua_dep/*.c $(distdir)/examples/lua_dep 117 | cp examples/lua_dep/*.lua $(distdir)/examples/lua_dep 118 | 119 | distcheck: $(distdir).tar.gz 120 | gzip -cd $+ | tar xvf - 121 | $(MAKE) -C $(distdir) all check 122 | $(MAKE) -C $(distdir) DESTDIR=$${PWD}/$(distdir)/_inst install uninstall 123 | $(MAKE) -C $(distdir) clean 124 | rm -rf $(distdir) 125 | @echo "*** Package $(distdir).tar.gz is ready for distribution." 126 | 127 | FORCE: 128 | -rm -rf $(distdir) &>/dev/null 129 | -rm $(distdir).tar.gz &>/dev/null 130 | 131 | .PHONY: FORCE all examples clean check dist distcheck install uninstall 132 | -------------------------------------------------------------------------------- /src/libelf/gelf_shdr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "_libelf.h" 35 | 36 | ELFTC_VCSID("$Id: gelf_shdr.c 2268 2011-12-03 17:05:11Z jkoshy $"); 37 | 38 | Elf32_Shdr * 39 | elf32_getshdr(Elf_Scn *s) 40 | { 41 | return (_libelf_getshdr(s, ELFCLASS32)); 42 | } 43 | 44 | Elf64_Shdr * 45 | elf64_getshdr(Elf_Scn *s) 46 | { 47 | return (_libelf_getshdr(s, ELFCLASS64)); 48 | } 49 | 50 | GElf_Shdr * 51 | gelf_getshdr(Elf_Scn *s, GElf_Shdr *d) 52 | { 53 | int ec; 54 | void *sh; 55 | Elf32_Shdr *sh32; 56 | Elf64_Shdr *sh64; 57 | 58 | if (d == NULL) { 59 | LIBELF_SET_ERROR(ARGUMENT, 0); 60 | return (NULL); 61 | } 62 | 63 | if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL) 64 | return (NULL); 65 | 66 | ec = s->s_elf->e_class; 67 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 68 | 69 | if (ec == ELFCLASS32) { 70 | sh32 = (Elf32_Shdr *) sh; 71 | 72 | d->sh_name = sh32->sh_name; 73 | d->sh_type = sh32->sh_type; 74 | d->sh_flags = (Elf64_Xword) sh32->sh_flags; 75 | d->sh_addr = (Elf64_Addr) sh32->sh_addr; 76 | d->sh_offset = (Elf64_Off) sh32->sh_offset; 77 | d->sh_size = (Elf64_Xword) sh32->sh_size; 78 | d->sh_link = sh32->sh_link; 79 | d->sh_info = sh32->sh_info; 80 | d->sh_addralign = (Elf64_Xword) sh32->sh_addralign; 81 | d->sh_entsize = (Elf64_Xword) sh32->sh_entsize; 82 | } else { 83 | sh64 = (Elf64_Shdr *) sh; 84 | *d = *sh64; 85 | } 86 | 87 | return (d); 88 | } 89 | 90 | int 91 | gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s) 92 | { 93 | int ec; 94 | Elf *e; 95 | Elf32_Shdr *sh32; 96 | 97 | 98 | if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL || 99 | e->e_kind != ELF_K_ELF || 100 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 101 | LIBELF_SET_ERROR(ARGUMENT, 0); 102 | return (0); 103 | } 104 | 105 | if (e->e_cmd == ELF_C_READ) { 106 | LIBELF_SET_ERROR(MODE, 0); 107 | return (0); 108 | } 109 | 110 | (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY); 111 | 112 | if (ec == ELFCLASS64) { 113 | scn->s_shdr.s_shdr64 = *s; 114 | return (1); 115 | } 116 | 117 | sh32 = &scn->s_shdr.s_shdr32; 118 | 119 | sh32->sh_name = s->sh_name; 120 | sh32->sh_type = s->sh_type; 121 | LIBELF_COPY_U32(sh32, s, sh_flags); 122 | LIBELF_COPY_U32(sh32, s, sh_addr); 123 | LIBELF_COPY_U32(sh32, s, sh_offset); 124 | LIBELF_COPY_U32(sh32, s, sh_size); 125 | sh32->sh_link = s->sh_link; 126 | sh32->sh_info = s->sh_info; 127 | LIBELF_COPY_U32(sh32, s, sh_addralign); 128 | LIBELF_COPY_U32(sh32, s, sh_entsize); 129 | 130 | return (1); 131 | } 132 | -------------------------------------------------------------------------------- /src/libelf/gelf_symshndx.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: gelf_symshndx.c 2283 2011-12-04 04:07:24Z jkoshy $"); 35 | 36 | GElf_Sym * 37 | gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, 38 | Elf32_Word *shindex) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | uint32_t sh_type; 45 | struct _Libelf_Data *ld, *lid; 46 | 47 | ld = (struct _Libelf_Data *) d; 48 | lid = (struct _Libelf_Data *) id; 49 | 50 | if (gelf_getsym(d, ndx, dst) == 0) 51 | return (NULL); 52 | 53 | if (lid == NULL || (scn = lid->d_scn) == NULL || 54 | (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf) || 55 | shindex == NULL) { 56 | LIBELF_SET_ERROR(ARGUMENT, 0); 57 | return (NULL); 58 | } 59 | 60 | ec = e->e_class; 61 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 62 | 63 | if (ec == ELFCLASS32) 64 | sh_type = scn->s_shdr.s_shdr32.sh_type; 65 | else 66 | sh_type = scn->s_shdr.s_shdr64.sh_type; 67 | 68 | if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || 69 | id->d_type != ELF_T_WORD) { 70 | LIBELF_SET_ERROR(ARGUMENT, 0); 71 | return (NULL); 72 | } 73 | 74 | msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); 75 | 76 | assert(msz > 0); 77 | 78 | if (msz * ndx >= id->d_size) { 79 | LIBELF_SET_ERROR(ARGUMENT, 0); 80 | return (NULL); 81 | } 82 | 83 | *shindex = ((Elf32_Word *) id->d_buf)[ndx]; 84 | 85 | return (dst); 86 | } 87 | 88 | int 89 | gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs, 90 | Elf32_Word xindex) 91 | { 92 | int ec; 93 | Elf *e; 94 | size_t msz; 95 | Elf_Scn *scn; 96 | uint32_t sh_type; 97 | struct _Libelf_Data *ld, *lid; 98 | 99 | ld = (struct _Libelf_Data *) d; 100 | lid = (struct _Libelf_Data *) id; 101 | 102 | if (gelf_update_sym(d, ndx, gs) == 0) 103 | return (0); 104 | 105 | if (lid == NULL || (scn = lid->d_scn) == NULL || 106 | (e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) { 107 | LIBELF_SET_ERROR(ARGUMENT, 0); 108 | return (0); 109 | } 110 | 111 | ec = e->e_class; 112 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 113 | 114 | if (ec == ELFCLASS32) 115 | sh_type = scn->s_shdr.s_shdr32.sh_type; 116 | else 117 | sh_type = scn->s_shdr.s_shdr64.sh_type; 118 | 119 | if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || 120 | d->d_type != ELF_T_WORD) { 121 | LIBELF_SET_ERROR(ARGUMENT, 0); 122 | return (0); 123 | } 124 | 125 | msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); 126 | assert(msz > 0); 127 | 128 | if (msz * ndx >= id->d_size) { 129 | LIBELF_SET_ERROR(ARGUMENT, 0); 130 | return (0); 131 | } 132 | 133 | *(((Elf32_Word *) id->d_buf) + ndx) = xindex; 134 | 135 | return (1); 136 | } 137 | -------------------------------------------------------------------------------- /src/libelf/elf_strptr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: elf_strptr.c 2271 2011-12-03 17:06:35Z jkoshy $"); 35 | 36 | /* 37 | * Convert an ELF section#,offset pair to a string pointer. 38 | */ 39 | 40 | char * 41 | elf_strptr(Elf *e, size_t scndx, size_t offset) 42 | { 43 | Elf_Scn *s; 44 | Elf_Data *d; 45 | size_t alignment, count; 46 | GElf_Shdr shdr; 47 | 48 | if (e == NULL || e->e_kind != ELF_K_ELF) { 49 | LIBELF_SET_ERROR(ARGUMENT, 0); 50 | return (NULL); 51 | } 52 | 53 | if ((s = elf_getscn(e, scndx)) == NULL || 54 | gelf_getshdr(s, &shdr) == NULL) 55 | return (NULL); 56 | 57 | if (shdr.sh_type != SHT_STRTAB || 58 | offset >= shdr.sh_size) { 59 | LIBELF_SET_ERROR(ARGUMENT, 0); 60 | return (NULL); 61 | } 62 | 63 | d = NULL; 64 | if (e->e_flags & ELF_F_LAYOUT) { 65 | 66 | /* 67 | * The application is taking responsibility for the 68 | * ELF object's layout, so we can directly translate 69 | * an offset to a `char *' address using the `d_off' 70 | * members of Elf_Data descriptors. 71 | */ 72 | while ((d = elf_getdata(s, d)) != NULL) { 73 | 74 | if (d->d_buf == 0 || d->d_size == 0) 75 | continue; 76 | 77 | if (d->d_type != ELF_T_BYTE) { 78 | LIBELF_SET_ERROR(DATA, 0); 79 | return (NULL); 80 | } 81 | 82 | if (offset >= d->d_off && 83 | offset < d->d_off + d->d_size) 84 | return ((char *) d->d_buf + offset - d->d_off); 85 | } 86 | } else { 87 | /* 88 | * Otherwise, the `d_off' members are not useable and 89 | * we need to compute offsets ourselves, taking into 90 | * account 'holes' in coverage of the section introduced 91 | * by alignment requirements. 92 | */ 93 | count = (size_t) 0; /* cumulative count of bytes seen */ 94 | while ((d = elf_getdata(s, d)) != NULL && count <= offset) { 95 | 96 | if (d->d_buf == NULL || d->d_size == 0) 97 | continue; 98 | 99 | if (d->d_type != ELF_T_BYTE) { 100 | LIBELF_SET_ERROR(DATA, 0); 101 | return (NULL); 102 | } 103 | 104 | if ((alignment = d->d_align) > 1) { 105 | if ((alignment & (alignment - 1)) != 0) { 106 | LIBELF_SET_ERROR(DATA, 0); 107 | return (NULL); 108 | } 109 | count = roundup2(count, alignment); 110 | } 111 | 112 | if (offset < count) { 113 | /* offset starts in the 'hole' */ 114 | LIBELF_SET_ERROR(ARGUMENT, 0); 115 | return (NULL); 116 | } 117 | 118 | if (offset < count + d->d_size) { 119 | if (d->d_buf != NULL) 120 | return ((char *) d->d_buf + 121 | offset - count); 122 | LIBELF_SET_ERROR(DATA, 0); 123 | return (NULL); 124 | } 125 | 126 | count += d->d_size; 127 | } 128 | } 129 | 130 | LIBELF_SET_ERROR(ARGUMENT, 0); 131 | return (NULL); 132 | } 133 | -------------------------------------------------------------------------------- /src/lua/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | return lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /src/libelf/gelf_dyn.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_dyn.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Dyn * 38 | gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | Elf32_Dyn *dyn32; 45 | Elf64_Dyn *dyn64; 46 | uint32_t sh_type; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | dyn32 = (Elf32_Dyn *) d->d_data.d_buf + ndx; 82 | 83 | dst->d_tag = dyn32->d_tag; 84 | dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val; 85 | 86 | } else { 87 | 88 | dyn64 = (Elf64_Dyn *) d->d_data.d_buf + ndx; 89 | 90 | *dst = *dyn64; 91 | } 92 | 93 | return (dst); 94 | } 95 | 96 | int 97 | gelf_update_dyn(Elf_Data *ed, int ndx, GElf_Dyn *ds) 98 | { 99 | int ec; 100 | Elf *e; 101 | size_t msz; 102 | Elf_Scn *scn; 103 | Elf32_Dyn *dyn32; 104 | Elf64_Dyn *dyn64; 105 | uint32_t sh_type; 106 | struct _Libelf_Data *d; 107 | 108 | d = (struct _Libelf_Data *) ed; 109 | 110 | if (d == NULL || ndx < 0 || ds == NULL || 111 | (scn = d->d_scn) == NULL || 112 | (e = scn->s_elf) == NULL) { 113 | LIBELF_SET_ERROR(ARGUMENT, 0); 114 | return (0); 115 | } 116 | 117 | ec = e->e_class; 118 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 119 | 120 | if (ec == ELFCLASS32) 121 | sh_type = scn->s_shdr.s_shdr32.sh_type; 122 | else 123 | sh_type = scn->s_shdr.s_shdr64.sh_type; 124 | 125 | if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { 126 | LIBELF_SET_ERROR(ARGUMENT, 0); 127 | return (0); 128 | } 129 | 130 | msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); 131 | assert(msz > 0); 132 | 133 | if (msz * ndx >= d->d_data.d_size) { 134 | LIBELF_SET_ERROR(ARGUMENT, 0); 135 | return (0); 136 | } 137 | 138 | if (ec == ELFCLASS32) { 139 | dyn32 = (Elf32_Dyn *) d->d_data.d_buf + ndx; 140 | 141 | LIBELF_COPY_S32(dyn32, ds, d_tag); 142 | LIBELF_COPY_U32(dyn32, ds, d_un.d_val); 143 | } else { 144 | dyn64 = (Elf64_Dyn *) d->d_data.d_buf + ndx; 145 | 146 | *dyn64 = *ds; 147 | } 148 | 149 | return (1); 150 | } 151 | -------------------------------------------------------------------------------- /src/libelf/gelf_cap.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_cap.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Cap * 38 | gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | Elf32_Cap *cap32; 45 | Elf64_Cap *cap64; 46 | uint32_t sh_type; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | 82 | cap32 = (Elf32_Cap *) d->d_data.d_buf + ndx; 83 | 84 | dst->c_tag = cap32->c_tag; 85 | dst->c_un.c_val = (Elf64_Xword) cap32->c_un.c_val; 86 | 87 | } else { 88 | 89 | cap64 = (Elf64_Cap *) d->d_data.d_buf + ndx; 90 | 91 | *dst = *cap64; 92 | } 93 | 94 | return (dst); 95 | } 96 | 97 | int 98 | gelf_update_cap(Elf_Data *ed, int ndx, GElf_Cap *gc) 99 | { 100 | int ec; 101 | Elf *e; 102 | size_t msz; 103 | Elf_Scn *scn; 104 | Elf32_Cap *cap32; 105 | Elf64_Cap *cap64; 106 | uint32_t sh_type; 107 | struct _Libelf_Data *d; 108 | 109 | d = (struct _Libelf_Data *) ed; 110 | 111 | if (d == NULL || ndx < 0 || gc == NULL || 112 | (scn = d->d_scn) == NULL || 113 | (e = scn->s_elf) == NULL) { 114 | LIBELF_SET_ERROR(ARGUMENT, 0); 115 | return (0); 116 | } 117 | 118 | ec = e->e_class; 119 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 120 | 121 | if (ec == ELFCLASS32) 122 | sh_type = scn->s_shdr.s_shdr32.sh_type; 123 | else 124 | sh_type = scn->s_shdr.s_shdr64.sh_type; 125 | 126 | if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) { 127 | LIBELF_SET_ERROR(ARGUMENT, 0); 128 | return (0); 129 | } 130 | 131 | msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); 132 | assert(msz > 0); 133 | 134 | if (msz * ndx >= d->d_data.d_size) { 135 | LIBELF_SET_ERROR(ARGUMENT, 0); 136 | return (0); 137 | } 138 | 139 | if (ec == ELFCLASS32) { 140 | cap32 = (Elf32_Cap *) d->d_data.d_buf + ndx; 141 | 142 | LIBELF_COPY_U32(cap32, gc, c_tag); 143 | LIBELF_COPY_U32(cap32, gc, c_un.c_val); 144 | } else { 145 | cap64 = (Elf64_Cap *) d->d_data.d_buf + ndx; 146 | 147 | *cap64 = *gc; 148 | } 149 | 150 | return (1); 151 | } 152 | -------------------------------------------------------------------------------- /src/libelf/gelf_syminfo.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: gelf_syminfo.c 2272 2011-12-03 17:07:31Z jkoshy $"); 35 | 36 | GElf_Syminfo * 37 | gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst) 38 | { 39 | int ec; 40 | Elf *e; 41 | size_t msz; 42 | Elf_Scn *scn; 43 | uint32_t sh_type; 44 | struct _Libelf_Data *d; 45 | Elf32_Syminfo *syminfo32; 46 | Elf64_Syminfo *syminfo64; 47 | 48 | d = (struct _Libelf_Data *) ed; 49 | 50 | if (d == NULL || ndx < 0 || dst == NULL || 51 | (scn = d->d_scn) == NULL || 52 | (e = scn->s_elf) == NULL) { 53 | LIBELF_SET_ERROR(ARGUMENT, 0); 54 | return (NULL); 55 | } 56 | 57 | ec = e->e_class; 58 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 59 | 60 | if (ec == ELFCLASS32) 61 | sh_type = scn->s_shdr.s_shdr32.sh_type; 62 | else 63 | sh_type = scn->s_shdr.s_shdr64.sh_type; 64 | 65 | if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) { 66 | LIBELF_SET_ERROR(ARGUMENT, 0); 67 | return (NULL); 68 | } 69 | 70 | msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); 71 | 72 | assert(msz > 0); 73 | 74 | if (msz * ndx >= d->d_data.d_size) { 75 | LIBELF_SET_ERROR(ARGUMENT, 0); 76 | return (NULL); 77 | } 78 | 79 | if (ec == ELFCLASS32) { 80 | 81 | syminfo32 = (Elf32_Syminfo *) d->d_data.d_buf + ndx; 82 | 83 | dst->si_boundto = syminfo32->si_boundto; 84 | dst->si_flags = syminfo32->si_flags; 85 | 86 | } else { 87 | 88 | syminfo64 = (Elf64_Syminfo *) d->d_data.d_buf + ndx; 89 | 90 | *dst = *syminfo64; 91 | } 92 | 93 | return (dst); 94 | } 95 | 96 | int 97 | gelf_update_syminfo(Elf_Data *ed, int ndx, GElf_Syminfo *gs) 98 | { 99 | int ec; 100 | Elf *e; 101 | size_t msz; 102 | Elf_Scn *scn; 103 | uint32_t sh_type; 104 | struct _Libelf_Data *d; 105 | Elf32_Syminfo *syminfo32; 106 | Elf64_Syminfo *syminfo64; 107 | 108 | d = (struct _Libelf_Data *) ed; 109 | 110 | if (d == NULL || ndx < 0 || gs == NULL || 111 | (scn = d->d_scn) == NULL || 112 | (e = scn->s_elf) == NULL) { 113 | LIBELF_SET_ERROR(ARGUMENT, 0); 114 | return (0); 115 | } 116 | 117 | ec = e->e_class; 118 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 119 | 120 | if (ec == ELFCLASS32) 121 | sh_type = scn->s_shdr.s_shdr32.sh_type; 122 | else 123 | sh_type = scn->s_shdr.s_shdr64.sh_type; 124 | 125 | if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) { 126 | LIBELF_SET_ERROR(ARGUMENT, 0); 127 | return (0); 128 | } 129 | 130 | msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); 131 | assert(msz > 0); 132 | 133 | if (msz * ndx >= d->d_data.d_size) { 134 | LIBELF_SET_ERROR(ARGUMENT, 0); 135 | return (0); 136 | } 137 | 138 | if (ec == ELFCLASS32) { 139 | syminfo32 = (Elf32_Syminfo *) d->d_data.d_buf + ndx; 140 | 141 | syminfo32->si_boundto = gs->si_boundto; 142 | syminfo32->si_flags = gs->si_flags; 143 | 144 | } else { 145 | syminfo64 = (Elf64_Syminfo *) d->d_data.d_buf + ndx; 146 | 147 | *syminfo64 = *gs; 148 | } 149 | 150 | return (1); 151 | } 152 | -------------------------------------------------------------------------------- /src/libelf/libelf_align.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: libelf_align.c 2225 2011-11-26 18:55:54Z jkoshy $"); 36 | 37 | struct align { 38 | int a32; 39 | int a64; 40 | }; 41 | 42 | #ifdef __GNUC__ 43 | #define MALIGN(N) { \ 44 | .a32 = __alignof__(Elf32_##N), \ 45 | .a64 = __alignof__(Elf64_##N) \ 46 | } 47 | #define MALIGN64(V) { \ 48 | .a32 = 0, \ 49 | .a64 = __alignof__(Elf64_##V) \ 50 | } 51 | #define MALIGN_WORD() { \ 52 | .a32 = __alignof__(int32_t), \ 53 | .a64 = __alignof__(int64_t) \ 54 | } 55 | #else 56 | #error Need the __alignof__ builtin. 57 | #endif 58 | #define UNSUPPORTED() { \ 59 | .a32 = 0, \ 60 | .a64 = 0 \ 61 | } 62 | 63 | static struct align malign[ELF_T_NUM] = { 64 | [ELF_T_ADDR] = MALIGN(Addr), 65 | [ELF_T_BYTE] = { .a32 = 1, .a64 = 1 }, 66 | [ELF_T_CAP] = MALIGN(Cap), 67 | [ELF_T_DYN] = MALIGN(Dyn), 68 | [ELF_T_EHDR] = MALIGN(Ehdr), 69 | [ELF_T_HALF] = MALIGN(Half), 70 | [ELF_T_LWORD] = MALIGN(Lword), 71 | [ELF_T_MOVE] = MALIGN(Move), 72 | [ELF_T_MOVEP] = UNSUPPORTED(), 73 | [ELF_T_NOTE] = MALIGN(Nhdr), 74 | [ELF_T_OFF] = MALIGN(Off), 75 | [ELF_T_PHDR] = MALIGN(Phdr), 76 | [ELF_T_REL] = MALIGN(Rel), 77 | [ELF_T_RELA] = MALIGN(Rela), 78 | [ELF_T_SHDR] = MALIGN(Shdr), 79 | [ELF_T_SWORD] = MALIGN(Sword), 80 | [ELF_T_SXWORD] = MALIGN64(Sxword), 81 | [ELF_T_SYM] = MALIGN(Sym), 82 | [ELF_T_SYMINFO] = MALIGN(Syminfo), 83 | [ELF_T_VDEF] = MALIGN(Verdef), 84 | [ELF_T_VNEED] = MALIGN(Verneed), 85 | [ELF_T_WORD] = MALIGN(Word), 86 | [ELF_T_XWORD] = MALIGN64(Xword), 87 | [ELF_T_GNUHASH] = MALIGN_WORD() 88 | }; 89 | 90 | int 91 | _libelf_malign(Elf_Type t, int elfclass) 92 | { 93 | if (t >= ELF_T_NUM || (int) t < 0) 94 | return (0); 95 | 96 | return (elfclass == ELFCLASS32 ? malign[t].a32 : 97 | malign[t].a64); 98 | } 99 | 100 | #define FALIGN(A32,A64) { .a32 = (A32), .a64 = (A64) } 101 | 102 | static struct align falign[ELF_T_NUM] = { 103 | [ELF_T_ADDR] = FALIGN(4,8), 104 | [ELF_T_BYTE] = FALIGN(1,1), 105 | [ELF_T_CAP] = FALIGN(4,8), 106 | [ELF_T_DYN] = FALIGN(4,8), 107 | [ELF_T_EHDR] = FALIGN(4,8), 108 | [ELF_T_HALF] = FALIGN(2,2), 109 | [ELF_T_LWORD] = FALIGN(8,8), 110 | [ELF_T_MOVE] = FALIGN(8,8), 111 | [ELF_T_MOVEP] = UNSUPPORTED(), 112 | [ELF_T_NOTE] = FALIGN(4,4), 113 | [ELF_T_OFF] = FALIGN(4,8), 114 | [ELF_T_PHDR] = FALIGN(4,8), 115 | [ELF_T_REL] = FALIGN(4,8), 116 | [ELF_T_RELA] = FALIGN(4,8), 117 | [ELF_T_SHDR] = FALIGN(4,8), 118 | [ELF_T_SWORD] = FALIGN(4,4), 119 | [ELF_T_SXWORD] = FALIGN(0,8), 120 | [ELF_T_SYM] = FALIGN(4,8), 121 | [ELF_T_SYMINFO] = FALIGN(2,2), 122 | [ELF_T_VDEF] = FALIGN(4,4), 123 | [ELF_T_VNEED] = FALIGN(4,4), 124 | [ELF_T_WORD] = FALIGN(4,4), 125 | [ELF_T_XWORD] = FALIGN(0,8), 126 | [ELF_T_GNUHASH] = FALIGN(4,8) 127 | }; 128 | 129 | int 130 | _libelf_falign(Elf_Type t, int elfclass) 131 | { 132 | if (t >= ELF_T_NUM || (int) t < 0) 133 | return (0); 134 | 135 | return (elfclass == ELFCLASS32 ? falign[t].a32 : 136 | falign[t].a64); 137 | } 138 | -------------------------------------------------------------------------------- /src/libelf/gelf_move.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_move.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Move * 38 | gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | uint32_t sh_type; 45 | Elf32_Move *move32; 46 | Elf64_Move *move64; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | 82 | move32 = (Elf32_Move *) d->d_data.d_buf + ndx; 83 | 84 | dst->m_value = move32->m_value; 85 | dst->m_info = (Elf64_Xword) move32->m_info; 86 | dst->m_poffset = (Elf64_Xword) move32->m_poffset; 87 | dst->m_repeat = move32->m_repeat; 88 | dst->m_stride = move32->m_stride; 89 | } else { 90 | 91 | move64 = (Elf64_Move *) d->d_data.d_buf + ndx; 92 | 93 | *dst = *move64; 94 | } 95 | 96 | return (dst); 97 | } 98 | 99 | int 100 | gelf_update_move(Elf_Data *ed, int ndx, GElf_Move *gm) 101 | { 102 | int ec; 103 | Elf *e; 104 | size_t msz; 105 | Elf_Scn *scn; 106 | uint32_t sh_type; 107 | Elf32_Move *move32; 108 | Elf64_Move *move64; 109 | struct _Libelf_Data *d; 110 | 111 | d = (struct _Libelf_Data *) ed; 112 | 113 | if (d == NULL || ndx < 0 || gm == NULL || 114 | (scn = d->d_scn) == NULL || 115 | (e = scn->s_elf) == NULL) { 116 | LIBELF_SET_ERROR(ARGUMENT, 0); 117 | return (0); 118 | } 119 | 120 | ec = e->e_class; 121 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 122 | 123 | if (ec == ELFCLASS32) 124 | sh_type = scn->s_shdr.s_shdr32.sh_type; 125 | else 126 | sh_type = scn->s_shdr.s_shdr64.sh_type; 127 | 128 | if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) { 129 | LIBELF_SET_ERROR(ARGUMENT, 0); 130 | return (0); 131 | } 132 | 133 | msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); 134 | assert(msz > 0); 135 | 136 | if (msz * ndx >= d->d_data.d_size) { 137 | LIBELF_SET_ERROR(ARGUMENT, 0); 138 | return (0); 139 | } 140 | 141 | if (ec == ELFCLASS32) { 142 | move32 = (Elf32_Move *) d->d_data.d_buf + ndx; 143 | 144 | move32->m_value = gm->m_value; 145 | LIBELF_COPY_U32(move32, gm, m_info); 146 | LIBELF_COPY_U32(move32, gm, m_poffset); 147 | move32->m_repeat = gm->m_repeat; 148 | move32->m_stride = gm->m_stride; 149 | 150 | } else { 151 | move64 = (Elf64_Move *) d->d_data.d_buf + ndx; 152 | 153 | *move64 = *gm; 154 | } 155 | 156 | return (1); 157 | } 158 | -------------------------------------------------------------------------------- /src/libelf/gelf_rel.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_rel.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Rel * 38 | gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | uint32_t sh_type; 45 | Elf32_Rel *rel32; 46 | Elf64_Rel *rel64; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_REL, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx; 82 | 83 | dst->r_offset = (Elf64_Addr) rel32->r_offset; 84 | dst->r_info = ELF64_R_INFO( 85 | (Elf64_Xword) ELF32_R_SYM(rel32->r_info), 86 | ELF32_R_TYPE(rel32->r_info)); 87 | 88 | } else { 89 | 90 | rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; 91 | 92 | *dst = *rel64; 93 | } 94 | 95 | return (dst); 96 | } 97 | 98 | int 99 | gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr) 100 | { 101 | int ec; 102 | Elf *e; 103 | size_t msz; 104 | Elf_Scn *scn; 105 | uint32_t sh_type; 106 | Elf32_Rel *rel32; 107 | Elf64_Rel *rel64; 108 | struct _Libelf_Data *d; 109 | 110 | d = (struct _Libelf_Data *) ed; 111 | 112 | if (d == NULL || ndx < 0 || dr == NULL || 113 | (scn = d->d_scn) == NULL || 114 | (e = scn->s_elf) == NULL) { 115 | LIBELF_SET_ERROR(ARGUMENT, 0); 116 | return (0); 117 | } 118 | 119 | ec = e->e_class; 120 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 121 | 122 | if (ec == ELFCLASS32) 123 | sh_type = scn->s_shdr.s_shdr32.sh_type; 124 | else 125 | sh_type = scn->s_shdr.s_shdr64.sh_type; 126 | 127 | if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) { 128 | LIBELF_SET_ERROR(ARGUMENT, 0); 129 | return (0); 130 | } 131 | 132 | msz = _libelf_msize(ELF_T_REL, ec, e->e_version); 133 | assert(msz > 0); 134 | 135 | if (msz * ndx >= d->d_data.d_size) { 136 | LIBELF_SET_ERROR(ARGUMENT, 0); 137 | return (0); 138 | } 139 | 140 | if (ec == ELFCLASS32) { 141 | rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx; 142 | 143 | LIBELF_COPY_U32(rel32, dr, r_offset); 144 | 145 | if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || 146 | ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { 147 | LIBELF_SET_ERROR(RANGE, 0); 148 | return (0); 149 | } 150 | rel32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), 151 | ELF64_R_TYPE(dr->r_info)); 152 | } else { 153 | rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; 154 | 155 | *rel64 = *dr; 156 | } 157 | 158 | return (1); 159 | } 160 | -------------------------------------------------------------------------------- /src/libelf/gelf_sym.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_sym.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Sym * 38 | gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | uint32_t sh_type; 45 | Elf32_Sym *sym32; 46 | Elf64_Sym *sym64; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | 82 | sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx; 83 | 84 | dst->st_name = sym32->st_name; 85 | dst->st_value = (Elf64_Addr) sym32->st_value; 86 | dst->st_size = (Elf64_Xword) sym32->st_size; 87 | dst->st_info = ELF64_ST_INFO(ELF32_ST_BIND(sym32->st_info), 88 | ELF32_ST_TYPE(sym32->st_info)); 89 | dst->st_other = sym32->st_other; 90 | dst->st_shndx = sym32->st_shndx; 91 | } else { 92 | 93 | sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx; 94 | 95 | *dst = *sym64; 96 | } 97 | 98 | return (dst); 99 | } 100 | 101 | int 102 | gelf_update_sym(Elf_Data *ed, int ndx, GElf_Sym *gs) 103 | { 104 | int ec; 105 | Elf *e; 106 | size_t msz; 107 | Elf_Scn *scn; 108 | uint32_t sh_type; 109 | Elf32_Sym *sym32; 110 | Elf64_Sym *sym64; 111 | struct _Libelf_Data *d; 112 | 113 | d = (struct _Libelf_Data *) ed; 114 | 115 | if (d == NULL || ndx < 0 || gs == NULL || 116 | (scn = d->d_scn) == NULL || 117 | (e = scn->s_elf) == NULL) { 118 | LIBELF_SET_ERROR(ARGUMENT, 0); 119 | return (0); 120 | } 121 | 122 | ec = e->e_class; 123 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 124 | 125 | if (ec == ELFCLASS32) 126 | sh_type = scn->s_shdr.s_shdr32.sh_type; 127 | else 128 | sh_type = scn->s_shdr.s_shdr64.sh_type; 129 | 130 | if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) { 131 | LIBELF_SET_ERROR(ARGUMENT, 0); 132 | return (0); 133 | } 134 | 135 | msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); 136 | assert(msz > 0); 137 | 138 | if (msz * ndx >= d->d_data.d_size) { 139 | LIBELF_SET_ERROR(ARGUMENT, 0); 140 | return (0); 141 | } 142 | 143 | if (ec == ELFCLASS32) { 144 | sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx; 145 | 146 | sym32->st_name = gs->st_name; 147 | sym32->st_info = gs->st_info; 148 | sym32->st_other = gs->st_other; 149 | sym32->st_shndx = gs->st_shndx; 150 | 151 | LIBELF_COPY_U32(sym32, gs, st_value); 152 | LIBELF_COPY_U32(sym32, gs, st_size); 153 | } else { 154 | sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx; 155 | 156 | *sym64 = *gs; 157 | } 158 | 159 | return (1); 160 | } 161 | -------------------------------------------------------------------------------- /src/libelf/libelf_phdr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "_libelf.h" 35 | 36 | ELFTC_VCSID("$Id: libelf_phdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); 37 | 38 | void * 39 | _libelf_getphdr(Elf *e, int ec) 40 | { 41 | size_t phnum, phentsize; 42 | size_t fsz, msz; 43 | uint64_t phoff; 44 | Elf32_Ehdr *eh32; 45 | Elf64_Ehdr *eh64; 46 | void *ehdr, *phdr; 47 | int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); 48 | 49 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 50 | 51 | if (e == NULL) { 52 | LIBELF_SET_ERROR(ARGUMENT, 0); 53 | return (NULL); 54 | } 55 | 56 | if ((phdr = (ec == ELFCLASS32 ? 57 | (void *) e->e_u.e_elf.e_phdr.e_phdr32 : 58 | (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL) 59 | return (phdr); 60 | 61 | /* 62 | * Check the PHDR related fields in the EHDR for sanity. 63 | */ 64 | 65 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 66 | return (NULL); 67 | 68 | phnum = e->e_u.e_elf.e_nphdr; 69 | 70 | if (ec == ELFCLASS32) { 71 | eh32 = (Elf32_Ehdr *) ehdr; 72 | phentsize = eh32->e_phentsize; 73 | phoff = (uint64_t) eh32->e_phoff; 74 | } else { 75 | eh64 = (Elf64_Ehdr *) ehdr; 76 | phentsize = eh64->e_phentsize; 77 | phoff = (uint64_t) eh64->e_phoff; 78 | } 79 | 80 | fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version); 81 | 82 | assert(fsz > 0); 83 | 84 | if ((uint64_t) e->e_rawsize < (phoff + fsz)) { 85 | LIBELF_SET_ERROR(HEADER, 0); 86 | return (NULL); 87 | } 88 | 89 | msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT); 90 | 91 | assert(msz > 0); 92 | 93 | if ((phdr = calloc(phnum, msz)) == NULL) { 94 | LIBELF_SET_ERROR(RESOURCE, 0); 95 | return (NULL); 96 | } 97 | 98 | if (ec == ELFCLASS32) 99 | e->e_u.e_elf.e_phdr.e_phdr32 = phdr; 100 | else 101 | e->e_u.e_elf.e_phdr.e_phdr64 = phdr; 102 | 103 | 104 | xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec); 105 | (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, 106 | e->e_byteorder != LIBELF_PRIVATE(byteorder)); 107 | 108 | return (phdr); 109 | } 110 | 111 | void * 112 | _libelf_newphdr(Elf *e, int ec, size_t count) 113 | { 114 | void *ehdr, *newphdr, *oldphdr; 115 | size_t msz; 116 | 117 | if (e == NULL) { 118 | LIBELF_SET_ERROR(ARGUMENT, 0); 119 | return (NULL); 120 | } 121 | 122 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) { 123 | LIBELF_SET_ERROR(SEQUENCE, 0); 124 | return (NULL); 125 | } 126 | 127 | assert(e->e_class == ec); 128 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 129 | assert(e->e_version == EV_CURRENT); 130 | 131 | msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version); 132 | 133 | assert(msz > 0); 134 | 135 | newphdr = NULL; 136 | if (count > 0 && (newphdr = calloc(count, msz)) == NULL) { 137 | LIBELF_SET_ERROR(RESOURCE, 0); 138 | return (NULL); 139 | } 140 | 141 | if (ec == ELFCLASS32) { 142 | if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL) 143 | free(oldphdr); 144 | e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr; 145 | } else { 146 | if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL) 147 | free(oldphdr); 148 | e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr; 149 | } 150 | 151 | e->e_u.e_elf.e_nphdr = count; 152 | 153 | elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); 154 | 155 | return (newphdr); 156 | } 157 | -------------------------------------------------------------------------------- /src/libelf/gelf_rela.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "_libelf.h" 34 | 35 | ELFTC_VCSID("$Id: gelf_rela.c 2272 2011-12-03 17:07:31Z jkoshy $"); 36 | 37 | GElf_Rela * 38 | gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst) 39 | { 40 | int ec; 41 | Elf *e; 42 | size_t msz; 43 | Elf_Scn *scn; 44 | uint32_t sh_type; 45 | Elf32_Rela *rela32; 46 | Elf64_Rela *rela64; 47 | struct _Libelf_Data *d; 48 | 49 | d = (struct _Libelf_Data *) ed; 50 | 51 | if (d == NULL || ndx < 0 || dst == NULL || 52 | (scn = d->d_scn) == NULL || 53 | (e = scn->s_elf) == NULL) { 54 | LIBELF_SET_ERROR(ARGUMENT, 0); 55 | return (NULL); 56 | } 57 | 58 | ec = e->e_class; 59 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 60 | 61 | if (ec == ELFCLASS32) 62 | sh_type = scn->s_shdr.s_shdr32.sh_type; 63 | else 64 | sh_type = scn->s_shdr.s_shdr64.sh_type; 65 | 66 | if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) { 67 | LIBELF_SET_ERROR(ARGUMENT, 0); 68 | return (NULL); 69 | } 70 | 71 | msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); 72 | 73 | assert(msz > 0); 74 | 75 | if (msz * ndx >= d->d_data.d_size) { 76 | LIBELF_SET_ERROR(ARGUMENT, 0); 77 | return (NULL); 78 | } 79 | 80 | if (ec == ELFCLASS32) { 81 | rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx; 82 | 83 | dst->r_offset = (Elf64_Addr) rela32->r_offset; 84 | dst->r_info = ELF64_R_INFO( 85 | (Elf64_Xword) ELF32_R_SYM(rela32->r_info), 86 | ELF32_R_TYPE(rela32->r_info)); 87 | dst->r_addend = (Elf64_Sxword) rela32->r_addend; 88 | 89 | } else { 90 | 91 | rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx; 92 | 93 | *dst = *rela64; 94 | } 95 | 96 | return (dst); 97 | } 98 | 99 | int 100 | gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr) 101 | { 102 | int ec; 103 | Elf *e; 104 | size_t msz; 105 | Elf_Scn *scn; 106 | uint32_t sh_type; 107 | Elf32_Rela *rela32; 108 | Elf64_Rela *rela64; 109 | struct _Libelf_Data *d; 110 | 111 | d = (struct _Libelf_Data *) ed; 112 | 113 | if (d == NULL || ndx < 0 || dr == NULL || 114 | (scn = d->d_scn) == NULL || 115 | (e = scn->s_elf) == NULL) { 116 | LIBELF_SET_ERROR(ARGUMENT, 0); 117 | return (0); 118 | } 119 | 120 | ec = e->e_class; 121 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); 122 | 123 | if (ec == ELFCLASS32) 124 | sh_type = scn->s_shdr.s_shdr32.sh_type; 125 | else 126 | sh_type = scn->s_shdr.s_shdr64.sh_type; 127 | 128 | if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) { 129 | LIBELF_SET_ERROR(ARGUMENT, 0); 130 | return (0); 131 | } 132 | 133 | msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); 134 | assert(msz > 0); 135 | 136 | if (msz * ndx >= d->d_data.d_size) { 137 | LIBELF_SET_ERROR(ARGUMENT, 0); 138 | return (0); 139 | } 140 | 141 | if (ec == ELFCLASS32) { 142 | rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx; 143 | 144 | LIBELF_COPY_U32(rela32, dr, r_offset); 145 | 146 | if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || 147 | ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { 148 | LIBELF_SET_ERROR(RANGE, 0); 149 | return (0); 150 | } 151 | rela32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), 152 | ELF64_R_TYPE(dr->r_info)); 153 | 154 | LIBELF_COPY_S32(rela32, dr, r_addend); 155 | } else { 156 | rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx; 157 | 158 | *rela64 = *dr; 159 | } 160 | 161 | return (1); 162 | } 163 | -------------------------------------------------------------------------------- /src/libelf/libelf_xlate.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "_libelf.h" 33 | 34 | ELFTC_VCSID("$Id: libelf_xlate.c 2225 2011-11-26 18:55:54Z jkoshy $"); 35 | 36 | /* 37 | * Translate to/from the file representation of ELF objects. 38 | * 39 | * Translation could potentially involve the following 40 | * transformations: 41 | * 42 | * - an endianness conversion, 43 | * - a change of layout, as the file representation of ELF objects 44 | * can differ from their in-memory representation. 45 | * - a change in representation due to a layout version change. 46 | */ 47 | 48 | Elf_Data * 49 | _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, 50 | int elfclass, int direction) 51 | { 52 | int byteswap; 53 | size_t cnt, dsz, fsz, msz; 54 | uintptr_t sb, se, db, de; 55 | 56 | if (encoding == ELFDATANONE) 57 | encoding = LIBELF_PRIVATE(byteorder); 58 | 59 | if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) || 60 | dst == NULL || src == NULL || dst == src) { 61 | LIBELF_SET_ERROR(ARGUMENT, 0); 62 | return (NULL); 63 | } 64 | 65 | assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); 66 | assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY); 67 | 68 | if (dst->d_version != src->d_version) { 69 | LIBELF_SET_ERROR(UNIMPL, 0); 70 | return (NULL); 71 | } 72 | 73 | if (src->d_buf == NULL || dst->d_buf == NULL) { 74 | LIBELF_SET_ERROR(DATA, 0); 75 | return (NULL); 76 | } 77 | 78 | if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) { 79 | LIBELF_SET_ERROR(DATA, 0); 80 | return (NULL); 81 | } 82 | 83 | if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize) 84 | (src->d_type, (size_t) 1, src->d_version)) == 0) 85 | return (NULL); 86 | 87 | msz = _libelf_msize(src->d_type, elfclass, src->d_version); 88 | 89 | assert(msz > 0); 90 | 91 | if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) { 92 | LIBELF_SET_ERROR(DATA, 0); 93 | return (NULL); 94 | } 95 | 96 | /* 97 | * Determine the number of objects that need to be converted, and 98 | * the space required for the converted objects in the destination 99 | * buffer. 100 | */ 101 | if (direction == ELF_TOMEMORY) { 102 | cnt = src->d_size / fsz; 103 | dsz = cnt * msz; 104 | } else { 105 | cnt = src->d_size / msz; 106 | dsz = cnt * fsz; 107 | } 108 | 109 | if (dst->d_size < dsz) { 110 | LIBELF_SET_ERROR(DATA, 0); 111 | return (NULL); 112 | } 113 | 114 | sb = (uintptr_t) src->d_buf; 115 | se = sb + src->d_size; 116 | db = (uintptr_t) dst->d_buf; 117 | de = db + dst->d_size; 118 | 119 | /* 120 | * Check for overlapping buffers. Note that db == sb is 121 | * allowed. 122 | */ 123 | if (db != sb && de > sb && se > db) { 124 | LIBELF_SET_ERROR(DATA, 0); 125 | return (NULL); 126 | } 127 | 128 | if ((direction == ELF_TOMEMORY ? db : sb) % 129 | _libelf_malign(src->d_type, elfclass)) { 130 | LIBELF_SET_ERROR(DATA, 0); 131 | return (NULL); 132 | } 133 | 134 | dst->d_type = src->d_type; 135 | dst->d_size = dsz; 136 | 137 | byteswap = encoding != LIBELF_PRIVATE(byteorder); 138 | 139 | if (src->d_size == 0 || 140 | (db == sb && !byteswap && fsz == msz)) 141 | return (dst); /* nothing more to do */ 142 | 143 | if (!(_libelf_get_translator(src->d_type, direction, elfclass)) 144 | (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) { 145 | LIBELF_SET_ERROR(DATA, 0); 146 | return (NULL); 147 | } 148 | 149 | return (dst); 150 | } 151 | -------------------------------------------------------------------------------- /src/libelf/gelf_ehdr.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "_libelf.h" 36 | 37 | ELFTC_VCSID("$Id: gelf_ehdr.c 2268 2011-12-03 17:05:11Z jkoshy $"); 38 | 39 | Elf32_Ehdr * 40 | elf32_getehdr(Elf *e) 41 | { 42 | return (_libelf_ehdr(e, ELFCLASS32, 0)); 43 | } 44 | 45 | Elf64_Ehdr * 46 | elf64_getehdr(Elf *e) 47 | { 48 | return (_libelf_ehdr(e, ELFCLASS64, 0)); 49 | } 50 | 51 | GElf_Ehdr * 52 | gelf_getehdr(Elf *e, GElf_Ehdr *d) 53 | { 54 | int ec; 55 | Elf32_Ehdr *eh32; 56 | Elf64_Ehdr *eh64; 57 | 58 | if (d == NULL || e == NULL || 59 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 60 | LIBELF_SET_ERROR(ARGUMENT, 0); 61 | return (NULL); 62 | } 63 | 64 | if (ec == ELFCLASS32) { 65 | if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL) 66 | return (NULL); 67 | 68 | (void) memcpy(d->e_ident, eh32->e_ident, 69 | sizeof(eh32->e_ident)); 70 | d->e_type = eh32->e_type; 71 | d->e_machine = eh32->e_machine; 72 | d->e_version = eh32->e_version; 73 | d->e_entry = eh32->e_entry; 74 | d->e_phoff = eh32->e_phoff; 75 | d->e_shoff = eh32->e_shoff; 76 | d->e_flags = eh32->e_flags; 77 | d->e_ehsize = eh32->e_ehsize; 78 | d->e_phentsize = eh32->e_phentsize; 79 | d->e_phnum = eh32->e_phnum; 80 | d->e_shentsize = eh32->e_shentsize; 81 | d->e_shnum = eh32->e_shnum; 82 | d->e_shstrndx = eh32->e_shstrndx; 83 | 84 | return (d); 85 | } 86 | 87 | assert(ec == ELFCLASS64); 88 | 89 | if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL) 90 | return (NULL); 91 | *d = *eh64; 92 | 93 | return (d); 94 | } 95 | 96 | Elf32_Ehdr * 97 | elf32_newehdr(Elf *e) 98 | { 99 | return (_libelf_ehdr(e, ELFCLASS32, 1)); 100 | } 101 | 102 | Elf64_Ehdr * 103 | elf64_newehdr(Elf *e) 104 | { 105 | return (_libelf_ehdr(e, ELFCLASS64, 1)); 106 | } 107 | 108 | void * 109 | gelf_newehdr(Elf *e, int ec) 110 | { 111 | if (e != NULL && 112 | (ec == ELFCLASS32 || ec == ELFCLASS64)) 113 | return (_libelf_ehdr(e, ec, 1)); 114 | 115 | LIBELF_SET_ERROR(ARGUMENT, 0); 116 | return (NULL); 117 | } 118 | 119 | int 120 | gelf_update_ehdr(Elf *e, GElf_Ehdr *s) 121 | { 122 | int ec; 123 | void *ehdr; 124 | Elf32_Ehdr *eh32; 125 | Elf64_Ehdr *eh64; 126 | 127 | if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF || 128 | ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 129 | LIBELF_SET_ERROR(ARGUMENT, 0); 130 | return (0); 131 | } 132 | 133 | if (e->e_cmd == ELF_C_READ) { 134 | LIBELF_SET_ERROR(MODE, 0); 135 | return (0); 136 | } 137 | 138 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 139 | return (0); 140 | 141 | (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 142 | 143 | if (ec == ELFCLASS64) { 144 | eh64 = (Elf64_Ehdr *) ehdr; 145 | *eh64 = *s; 146 | return (1); 147 | } 148 | 149 | eh32 = (Elf32_Ehdr *) ehdr; 150 | 151 | (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident)); 152 | 153 | eh32->e_type = s->e_type; 154 | eh32->e_machine = s->e_machine; 155 | eh32->e_version = s->e_version; 156 | LIBELF_COPY_U32(eh32, s, e_entry); 157 | LIBELF_COPY_U32(eh32, s, e_phoff); 158 | LIBELF_COPY_U32(eh32, s, e_shoff); 159 | eh32->e_flags = s->e_flags; 160 | eh32->e_ehsize = s->e_ehsize; 161 | eh32->e_phentsize = s->e_phentsize; 162 | eh32->e_phnum = s->e_phnum; 163 | eh32->e_shentsize = s->e_shentsize; 164 | eh32->e_shnum = s->e_shnum; 165 | eh32->e_shstrndx = s->e_shstrndx; 166 | 167 | return (1); 168 | } 169 | -------------------------------------------------------------------------------- /src/lua/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.30 2012/10/03 12:36:46 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int n) { 24 | Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.nupvalues = cast_byte(n); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int n) { 31 | Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; 32 | c->l.p = NULL; 33 | c->l.nupvalues = cast_byte(n); 34 | while (n--) c->l.upvals[n] = NULL; 35 | return c; 36 | } 37 | 38 | 39 | UpVal *luaF_newupval (lua_State *L) { 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 41 | uv->v = &uv->u.value; 42 | setnilvalue(uv->v); 43 | return uv; 44 | } 45 | 46 | 47 | UpVal *luaF_findupval (lua_State *L, StkId level) { 48 | global_State *g = G(L); 49 | GCObject **pp = &L->openupval; 50 | UpVal *p; 51 | UpVal *uv; 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 53 | GCObject *o = obj2gco(p); 54 | lua_assert(p->v != &p->u.value); 55 | lua_assert(!isold(o) || isold(obj2gco(L))); 56 | if (p->v == level) { /* found a corresponding upvalue? */ 57 | if (isdead(g, o)) /* is it dead? */ 58 | changewhite(o); /* resurrect it */ 59 | return p; 60 | } 61 | pp = &p->next; 62 | } 63 | /* not found: create a new one */ 64 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 65 | uv->v = level; /* current value lives in the stack */ 66 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 67 | uv->u.l.next = g->uvhead.u.l.next; 68 | uv->u.l.next->u.l.prev = uv; 69 | g->uvhead.u.l.next = uv; 70 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 71 | return uv; 72 | } 73 | 74 | 75 | static void unlinkupval (UpVal *uv) { 76 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 77 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 78 | uv->u.l.prev->u.l.next = uv->u.l.next; 79 | } 80 | 81 | 82 | void luaF_freeupval (lua_State *L, UpVal *uv) { 83 | if (uv->v != &uv->u.value) /* is it open? */ 84 | unlinkupval(uv); /* remove from open list */ 85 | luaM_free(L, uv); /* free upvalue */ 86 | } 87 | 88 | 89 | void luaF_close (lua_State *L, StkId level) { 90 | UpVal *uv; 91 | global_State *g = G(L); 92 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 93 | GCObject *o = obj2gco(uv); 94 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 95 | L->openupval = uv->next; /* remove from `open' list */ 96 | if (isdead(g, o)) 97 | luaF_freeupval(L, uv); /* free upvalue */ 98 | else { 99 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 100 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 101 | uv->v = &uv->u.value; /* now current value lives here */ 102 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 103 | g->allgc = o; 104 | luaC_checkupvalcolor(g, uv); 105 | } 106 | } 107 | } 108 | 109 | 110 | Proto *luaF_newproto (lua_State *L) { 111 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 112 | f->k = NULL; 113 | f->sizek = 0; 114 | f->p = NULL; 115 | f->sizep = 0; 116 | f->code = NULL; 117 | f->cache = NULL; 118 | f->sizecode = 0; 119 | f->lineinfo = NULL; 120 | f->sizelineinfo = 0; 121 | f->upvalues = NULL; 122 | f->sizeupvalues = 0; 123 | f->numparams = 0; 124 | f->is_vararg = 0; 125 | f->maxstacksize = 0; 126 | f->locvars = NULL; 127 | f->sizelocvars = 0; 128 | f->linedefined = 0; 129 | f->lastlinedefined = 0; 130 | f->source = NULL; 131 | return f; 132 | } 133 | 134 | 135 | void luaF_freeproto (lua_State *L, Proto *f) { 136 | luaM_freearray(L, f->code, f->sizecode); 137 | luaM_freearray(L, f->p, f->sizep); 138 | luaM_freearray(L, f->k, f->sizek); 139 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 140 | luaM_freearray(L, f->locvars, f->sizelocvars); 141 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 142 | luaM_free(L, f); 143 | } 144 | 145 | 146 | /* 147 | ** Look for n-th local variable at line `line' in function `func'. 148 | ** Returns NULL if not found. 149 | */ 150 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 151 | int i; 152 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 153 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 154 | local_number--; 155 | if (local_number == 0) 156 | return getstr(f->locvars[i].varname); 157 | } 158 | } 159 | return NULL; /* not found */ 160 | } 161 | 162 | -------------------------------------------------------------------------------- /src/libelf/libelf_fsize.m4: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2006,2008-2011 Joseph Koshy 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "_libelf.h" 30 | 31 | ELFTC_VCSID("$Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy $"); 32 | 33 | /* WARNING: GENERATED FROM __file__. */ 34 | 35 | /* 36 | * Create an array of file sizes from the elf_type definitions 37 | */ 38 | 39 | divert(-1) 40 | include(SRCDIR`/elf_types.m4') 41 | 42 | /* 43 | * Translations from structure definitions to the size of their file 44 | * representations. 45 | */ 46 | 47 | /* `Basic' types. */ 48 | define(`BYTE_SIZE', 1) 49 | define(`IDENT_SIZE', `EI_NIDENT') 50 | 51 | /* Types that have variable length. */ 52 | define(`GNUHASH_SIZE', 1) 53 | define(`NOTE_SIZE', 1) 54 | define(`VDEF_SIZE', 1) 55 | define(`VNEED_SIZE', 1) 56 | 57 | /* Currently unimplemented types. */ 58 | define(`MOVEP_SIZE', 0) 59 | 60 | /* Overrides for 32 bit types that do not exist. */ 61 | define(`XWORD_SIZE32', 0) 62 | define(`SXWORD_SIZE32', 0) 63 | 64 | /* 65 | * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively. 66 | */ 67 | 68 | define(`FSZ32',`_FSZ32($1_DEF)') 69 | define(`_FSZ32', 70 | `ifelse($#,1,0, 71 | `_BSZ32($1)+_FSZ32(shift($@))')') 72 | define(`_BSZ32',`$2_SIZE32') 73 | 74 | define(`FSZ64',`_FSZ64($1_DEF)') 75 | define(`_FSZ64', 76 | `ifelse($#,1,0, 77 | `_BSZ64($1)+_FSZ64(shift($@))')') 78 | define(`_BSZ64',`$2_SIZE64') 79 | 80 | /* 81 | * DEFINE_ELF_FSIZES(TYPE,NAME) 82 | * 83 | * Shorthand for defining for 32 and 64 versions 84 | * of elf type TYPE. 85 | * 86 | * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit 87 | * sizes. 88 | * 89 | * Otherwise, look for a explicit 32/64 bit size definition for TYPE, 90 | * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there 91 | * is nothing further to do. 92 | * 93 | * Otherwise, if an Elf{32,64}_`'NAME structure definition is known, 94 | * compute an expression that adds up the sizes of the structure's 95 | * constituents. 96 | * 97 | * If such a structure definition is not known, treat TYPE as a primitive 98 | * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its 99 | * file representation size. 100 | */ 101 | 102 | define(`DEFINE_ELF_FSIZE', 103 | `ifdef($1`_SIZE', 104 | `define($1_SIZE32,$1_SIZE) 105 | define($1_SIZE64,$1_SIZE)', 106 | `ifdef($1`_SIZE32',`', 107 | `ifdef(`Elf32_'$2`_DEF', 108 | `define($1_SIZE32,FSZ32(Elf32_$2))', 109 | `define($1_SIZE32,`sizeof(Elf32_'$2`)')')') 110 | ifdef($1`_SIZE64',`', 111 | `ifdef(`Elf64_'$2`_DEF', 112 | `define($1_SIZE64,FSZ64(Elf64_$2))', 113 | `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')') 114 | 115 | define(`DEFINE_ELF_FSIZES', 116 | `ifelse($#,1,`', 117 | `DEFINE_ELF_FSIZE($1) 118 | DEFINE_ELF_FSIZES(shift($@))')') 119 | 120 | DEFINE_ELF_FSIZES(ELF_TYPE_LIST) 121 | DEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type 122 | 123 | define(`FSIZE', 124 | `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 }, 125 | ') 126 | define(`FSIZES', 127 | `ifelse($#,1,`', 128 | `FSIZE($1) 129 | FSIZES(shift($@))')') 130 | 131 | divert(0) 132 | 133 | struct fsize { 134 | size_t fsz32; 135 | size_t fsz64; 136 | }; 137 | 138 | static struct fsize fsize[ELF_T_NUM] = { 139 | FSIZES(ELF_TYPE_LIST) 140 | }; 141 | 142 | size_t 143 | _libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c) 144 | { 145 | size_t sz; 146 | 147 | sz = 0; 148 | if (v != EV_CURRENT) 149 | LIBELF_SET_ERROR(VERSION, 0); 150 | else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST) 151 | LIBELF_SET_ERROR(ARGUMENT, 0); 152 | else { 153 | sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32; 154 | if (sz == 0) 155 | LIBELF_SET_ERROR(UNIMPL, 0); 156 | } 157 | 158 | return (sz*c); 159 | } 160 | --------------------------------------------------------------------------------