2015年3月26日 星期四

C Library(Module) Loader for UEFI Lua



require("mylib")    // load mylib.efi or mylib.so or mylib.dll

本篇學習如何建立在Uefi 下可供Lua載入的library

試圖讓library能由Lua C Loader 載入

關鍵檔案在src/loadlib.c


LUA_USE_DLOPEN :for linux *.so


LUA_DL_DLL :for windows *.dll




/*

119 ** {========================================================================

120 ** This is an implementation of loadlib based on the dlfcn interface.

121 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,

122 ** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least

123 ** as an emulation layer on top of native functions.

124 ** =========================================================================

125 */
126 
127 #include <dlfcn.h>
128 
129 static void ll_unloadlib (void *lib) {
130   dlclose(lib);
131 }
132 
133 
134 static void *ll_load (lua_State *L, const char *path, int seeglb) {
135   void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
136   if (lib == NULL) lua_pushstring(L, dlerror());
137   return lib;
138 }
139 
140 
141 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
142   lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
143   if (f == NULL) lua_pushstring(L, dlerror());
144   return f;
145 }
146 
147 /* }====================================================== */
148 
149 


222 #undef LIB_FAIL
223 #define LIB_FAIL    "absent"
224 
225 
226 #define DLMSG   "dynamic libraries not enabled; check your Lua installation"
227 
228 
229 static void ll_unloadlib (void *lib) {
230   (void)(lib);  /* not used */
231 }
232 
233 
234 static void *ll_load (lua_State *L, const char *path, int seeglb) {
235   (void)(path); (void)(seeglb);  /* not used */
236   lua_pushliteral(L, DLMSG);
237   return NULL;
238 }
239 
240 
241 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
242   (void)(lib); (void)(sym);  /* not used */
243   lua_pushliteral(L, DLMSG);
244   return NULL;
245 }


初步判斷整個過程是利用dlopen 及 LoadLibraryEx的加載過程.......

疑問: Uefi 有Dynamic Load Library架構嗎?好像是沒有,載入Library有重定位問題,UEFI 可由載入的Application or Driver 中,對應出所要的Function ?要將Library以UEFI Driver 常駐形式存在,且初始化時,要建立FunctionName及FunctionAddress 對應關係


static void ll_unloadlib (void *lib)

static void *ll_load (lua_State *L, const char *path, int seeglb)

static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym)


在UEFI 下實現構想如下:

1.建立 EFI_LUA_DLL_PROTOCOL,用於Function name查找
2.於ll_load時載入 Lua Module Uefi Driver
3.於ll_unloadlib時卸載 Lua Module Uefi Driver
4.於ll_sym時call method 取得Function Address


 static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
287   void *reg = ll_checkclib(L, path);  /* check loaded C libraries */
288   if (reg == NULL) {  /* must load library? */
289     reg = ll_load(L, path, *sym == '*');
290     if (reg == NULL) return ERRLIB;  /* unable to load library */
291     ll_addtoclib(L, path, reg);
292   }
293   if (*sym == '*') {  /* loading only library (no function)? */
294     lua_pushboolean(L, 1);  /* return 'true' */
295     return 0;  /* no errors */
296   }
297   else {
298     lua_CFunction f = ll_sym(L, reg, sym);
299     if (f == NULL)
300       return ERRFUNC;  /* unable to find function */
301     lua_pushcfunction(L, f);  /* else create new function */
302     return 0;  /* no errors */
303   }
304 }


print(package.path)
require("hello")

(收工)

沒有留言:

張貼留言