static void init_curIAT (void) { char *h; char *r; h = (char *) strdup (cur_libname); while ((r = strchr (h, '.')) != NULL) *r = '_'; cur_dllsym = unifyStr (h); free (h); cur_IAT = unifyCat ("__IAT_", cur_dllsym); }
static int parseit (void) { int r; char *dot = NULL; if (!exports_seen) { if ((r = lexit2 ()) == -1) return 0; if (r == TK_EXPORTS) { r = expect_newline ("EXPORTS"); return (r != -1 ? 1 : 0); } if (r == TK_LIBRARY) { r = lexit2 (); if (r != TK_STRING && r != TK_NAME) { fprintf (stderr, "Expect name/string after LIBRARY keyword.\n"); return (r != -1 ? 1 : 0); } cur_libname = unifyStr (t_buf); if ((dot = strchr (t_buf, '.')) != NULL) { *dot = '\0'; cur_libbasename = unifyStr (t_buf); } fprintf (stderr, "Current library-name set to %s'\n", cur_libname); cur_outlibbasename = unifyCat (cur_libbasename, SUFFIX); r = expect_newline ("LIBRARY"); return (r != -1 ? 1 : 0); } } else { if ((r = lexit2 ()) == -1) return 0; if (r != TK_NAME) { fprintf (stderr, "Unexpected token ,%s'. Would have expected a string/name.\n", t_buf); return (expect_newline ("UNKNOWN") == -1 ? 0 : 1); } cur_symbol = unifyStr (t_buf); cur_alias = ""; cur_libsymbol = ""; cur_srcfile = ""; cur_data = 0; while ((r = lexit2 ()) != -1 && r != TK_NL) { if (r == TK_DATA) cur_data = 1; else if (r == TK_EQUAL) { r = lexit2 (); if (r != TK_NAME) { fprintf (stderr, "Expected name after = expression.\n"); if (r != -1 && r != TK_NL) r = expect_newline ("SYMBOL"); return (r == -1 ? 0 : 1); } cur_libsymbol = unifyStr (t_buf); } else if (r == TK_EQUALEQUAL) { r = lexit2 (); if (r != TK_NAME) { fprintf (stderr, "Expected name after == expression.\n"); if (r != -1 && r != TK_NL) r = expect_newline ("SYMBOL"); return (r == -1 ? 0 : 1); } cur_alias = unifyStr (t_buf); } else if (r == TK_SOURCEFILENAME) { cur_srcfile = unifyStr (t_buf); } else { fprintf (stderr, "Unknown token ,%s'.\n", t_buf); } } addSymbol (cur_symbol, cur_libsymbol, cur_alias, cur_srcfile, cur_data); } return (r != -1 ? 1 : 0); }
void outputSyms (void) { FILE *fp; sSymbol *l; const char *pth = unifyCat ("./s_", cur_libname); _mkdir (pth); init_curIAT (); pth = unifyCat (pth, "/"); fp = fopen (unifyCat (pth, "do_init.c"), "wb"); generate_stub_c (fp); fclose (fp); fp = fopen (unifyCat (pth, "sec_start.S"), "wb"); printHeader (fp); fprintf (fp, "\t.section .rdata, \"dr\"\n"); fprintf (fp, "__def_module:\n"); fprintf (fp, "\t.ascii \"%s\"\n", cur_libname); fprintf (fp,"\t.section .rdata$imp.%s, \"dr\"\n", cur_libname); fprintf (fp, "#ifdef __x86_64__\n"); fprintf (fp, "\t.align 16\n"); fprintf (fp, "\t.globl %s\n%s:\n", cur_IAT, cur_IAT); fprintf (fp, "\t.quad __def_module, 0\n"); fprintf (fp, "#elif __i386__\n"); fprintf (fp, "\t.align 8\n"); fprintf (fp, "\t.globl _%s\n_%s:\n", cur_IAT, cur_IAT); fprintf (fp, "\t.long __def_module, 0\n"); fprintf (fp, "#else\n#error \"Unknown target\"\n#endif\n"); fprintf (fp, "\n"); fprintf (fp,"\t.section .rdata$imp.xxxxxxxx, \"dr\"\n", cur_libname); fprintf (fp,"\t.linkonce\n"); fprintf (fp, "#ifdef __x86_64__\n"); fprintf (fp, "\t.align 8\n"); fprintf (fp, "\t.quad 0, 0\n"); fprintf (fp, "#elif __i386__\n"); fprintf (fp, "\t.align 4\n"); fprintf (fp, "\t.long 0, 0\n"); fprintf (fp, "#else\n#error \"Unknown target\"\n#endif\n"); fclose (fp); l = t_sym; while (l != NULL) { const char *hname = unifyCat (l->sym, ".S"); if (l->is_data == 0) { fp = fopen (unifyCat (pth, unifyCat ("stub_", hname)), "wb"); printStubCode (fp, hname, l->sym); fclose (fp); } fp = fopen (unifyCat (pth, unifyCat ("imp_", hname)), "wb"); printHeader (fp); fprintf (fp, "\t.file \"imp_%s\"\n", hname); fprintf (fp, "#ifdef __x86_64__\n"); fprintf (fp, "\t.section .data$%s.iat.%s,\"w\"\n", cur_libname, l->sym); fprintf (fp,"\t.globl __imp_%s\n", l->sym); fprintf (fp, "\t.align 8\n"); fprintf (fp, "__imp_%s:\n", l->sym); fprintf (fp, "\t.quad %s\n", (l->libsym[0] != 0 ? l->libsym : "0")); fprintf (fp, "\t.section .rdata, \"dr\"\n"); fprintf (fp, "__imp_%s_name:\n", l->sym); fprintf (fp, "\t.ascii \"%s\"\n", l->sym); fprintf (fp, "\t.section .rdata$imp.%s.%s, \"dr\"\n", cur_libname, l->sym); fprintf (fp, "\t.align 8\n"); fprintf (fp, "\t.quad __imp_%s_name, __imp_%s\n", l->sym, l->sym); fprintf (fp, "#elif __i386__\n"); fprintf (fp, "\t.section .data$%s.iat.%s, \"w\"\n", cur_libname, l->sym); fprintf (fp, "\t.align 4\n"); fprintf (fp,"\t.globl __imp__%s\n", l->sym); fprintf (fp, "__imp__%s:\n", l->sym); fprintf (fp, "\t.long %s%s\n", (l->libsym[0] != 0 ? "_" : ""), (l->libsym[0] != 0 ? l->libsym : "0")); fprintf (fp, "\t.section .rdata, \"dr\"\n"); fprintf (fp, "__imp__%s_name:\n", l->sym); fprintf (fp, "\t.ascii \"%s\"\n", l->sym); fprintf (fp, "\t.section .rdata$imp.%s.%s, \"dr\"\n", cur_libname, l->sym); fprintf (fp, "\t.align 4\n"); fprintf (fp, "\t.long __imp_%s__name, __imp__%s\n", l->sym, l->sym); fprintf (fp, "#else\n#error \"Unknown target\"\n#endif\n"); fclose (fp); if (l->subs != NULL) { sSymbol *p; p = l->subs; while (p != NULL) { const char *hn = unifyCat (p->sym, ".S"); fp = fopen (unifyCat (pth, unifyCat ("stub_", hn)), "wb"); printStubCode (fp, hn, p->sym); fclose (fp); p = p->next; } p = l->subs; while (p != NULL) { const char *hn = unifyCat (p->sym, ".S"); fp = fopen (unifyCat (pth, unifyCat ("imp_", hn)), "wb"); printHeader (fp); fprintf (fp, "\t.file \"imp_%s\"\n", hn); fprintf (fp, "#ifdef __x86_64__\n"); fprintf (fp, "\t.section .data$%s.iat.%s,\"w\"\n", cur_libname, p->sym); fprintf (fp,"\t.globl __imp_%s\n", p->sym); fprintf (fp, "\t.align 8\n"); fprintf (fp, "__imp_%s:\n", p->sym); fprintf (fp, "\t.quad %s\n", (p->libsym[0] != 0 ? p->libsym : (l->libsym[0] != 0 ? l->libsym : "0"))); fprintf (fp, "\t.section .rdata$imp.%s.%s, \"dr\"\n", cur_libname, p->sym); fprintf (fp, "\t.align 8\n"); fprintf (fp, "\t.quad __imp_%s, 1, __imp_%s, 0\n", l->sym, p->sym); fprintf (fp, "#elif __i386__\n"); fprintf (fp, "\t.section .data$%s.iat.%s, \"w\"\n", cur_libname, p->sym); fprintf (fp, "\t.align 4\n"); fprintf (fp,"\t.globl __imp__%s\n", p->sym); fprintf (fp, "__imp__%s:\n", p->sym); fprintf (fp, "\t.long %s\n", (p->libsym[0] != 0 ? p->libsym : (l->libsym[0] != 0 ? l->libsym : "0"))); fprintf (fp, "\t.section .rdata$imp.%s.%s, \"dr\"\n", cur_libname, p->sym); fprintf (fp, "\t.align 4\n"); fprintf (fp, "\t.long __imp__%s, 1, __imp__%s, 0\n", l->sym, p->sym); fprintf (fp, "#else\n#error \"Unknown target\"\n#endif\n"); fclose (fp); p = p->next; } } l = l->next; } }