aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2011-11-25 11:19:59 +0100
committerMike Pall <mike>2011-11-25 11:19:59 +0100
commitc2dcf39ee3d113ce775e8e4c10f8f8ea4467f920 (patch)
tree683bac012429f5978352c91490512b821c4a657f /src
parent923738459de649ef2369ab48fcc1bfd523168e41 (diff)
downloadluajit-c2dcf39ee3d113ce775e8e4c10f8f8ea4467f920.tar.gz
luajit-c2dcf39ee3d113ce775e8e4c10f8f8ea4467f920.tar.bz2
luajit-c2dcf39ee3d113ce775e8e4c10f8f8ea4467f920.zip
FFI: Improve ld script detection in ffi.load().
Diffstat (limited to 'src')
-rw-r--r--src/lj_clib.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/lj_clib.c b/src/lj_clib.c
index 9f69b969..937b1258 100644
--- a/src/lj_clib.c
+++ b/src/lj_clib.c
@@ -60,25 +60,39 @@ static const char *clib_extname(lua_State *L, const char *name)
60 return name; 60 return name;
61} 61}
62 62
63/* Check for a recognized ld script line. */
64static const char *clib_check_lds(lua_State *L, const char *buf)
65{
66 char *p, *e;
67 if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) &&
68 (p = strchr(buf, '('))) {
69 while (*++p == ' ') ;
70 for (e = p; *e && *e != ' ' && *e != ')'; e++) ;
71 return strdata(lj_str_new(L, p, e-p));
72 }
73 return NULL;
74}
75
63/* Quick and dirty solution to resolve shared library name from ld script. */ 76/* Quick and dirty solution to resolve shared library name from ld script. */
64static const char *clib_resolve_lds(lua_State *L, const char *name) 77static const char *clib_resolve_lds(lua_State *L, const char *name)
65{ 78{
66 FILE *fp = fopen(name, "r"); 79 FILE *fp = fopen(name, "r");
80 const char *p = NULL;
67 if (fp) { 81 if (fp) {
68 char *p, *e, buf[256]; 82 char buf[256];
69 if (fgets(buf, sizeof(buf), fp) && !strncmp(buf, "/* GNU ld script", 16)) { 83 if (fgets(buf, sizeof(buf), fp)) {
70 while (fgets(buf, sizeof(buf), fp)) { 84 if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */
71 if (!strncmp(buf, "GROUP", 5) && (p = strchr(buf, '('))) { 85 while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */
72 while (*++p == ' ') ; 86 p = clib_check_lds(L, buf);
73 for (e = p; *e && *e != ' ' && *e != ')'; e++) ; 87 if (p) break;
74 fclose(fp);
75 return strdata(lj_str_new(L, p, e-p));
76 } 88 }
89 } else { /* Otherwise check only the first line. */
90 p = clib_check_lds(L, buf);
77 } 91 }
78 } 92 }
79 fclose(fp); 93 fclose(fp);
80 } 94 }
81 return NULL; 95 return p;
82} 96}
83 97
84static void *clib_loadlib(lua_State *L, const char *name, int global) 98static void *clib_loadlib(lua_State *L, const char *name, int global)