dlltool.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,503 行 · 第 1/5 页
C
2,503 行
scan_open_obj_file (f);#ifdef DLLTOOL_MCORE_ELF if (mcore_elf_out_file) mcore_elf_cache_filename ((char *) filename);#endif } bfd_close (f);}/**********************************************************************/static voiddump_def_info (f) FILE *f;{ int i; export_type *exp; fprintf (f, "%s ", ASM_C); for (i = 0; oav[i]; i++) fprintf (f, "%s ", oav[i]); fprintf (f, "\n"); for (i = 0, exp = d_exports; exp; i++, exp = exp->next) { fprintf (f, "%s %d = %s %s @ %d %s%s%s\n", ASM_C, i, exp->name, exp->internal_name, exp->ordinal, exp->noname ? "NONAME " : "", exp->constant ? "CONSTANT" : "", exp->data ? "DATA" : ""); }}/* Generate the .exp file */static intsfunc (a, b) const void *a; const void *b;{ return *(const long *) a - *(const long *) b;}static voidflush_page (f, need, page_addr, on_page) FILE *f; long *need; int page_addr; int on_page;{ int i; /* Flush this page */ fprintf (f, "\t%s\t0x%08x\t%s Starting RVA for chunk\n", ASM_LONG, page_addr, ASM_C); fprintf (f, "\t%s\t0x%x\t%s Size of block\n", ASM_LONG, (on_page * 2) + (on_page & 1) * 2 + 8, ASM_C); for (i = 0; i < on_page; i++) { long needed = need[i]; if (needed) needed = ((needed - page_addr) | 0x3000) & 0xffff; fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, needed); } /* And padding */ if (on_page & 1) fprintf (f, "\t%s\t0x%x\n", ASM_SHORT, 0 | 0x0000);}static voidgen_def_file (){ int i; export_type *exp; inform (_("Adding exports to output file")); fprintf (output_def, ";"); for (i = 0; oav[i]; i++) fprintf (output_def, " %s", oav[i]); fprintf (output_def, "\nEXPORTS\n"); for (i = 0, exp = d_exports; exp; i++, exp = exp->next) { char *quote = strchr (exp->name, '.') ? "\"" : ""; char *res = cplus_demangle (exp->internal_name, DMGL_ANSI | DMGL_PARAMS); if (strcmp (exp->name, exp->internal_name) == 0) { fprintf (output_def, "\t%s%s%s @ %d%s%s ; %s\n", quote, exp->name, quote, exp->ordinal, exp->noname ? " NONAME" : "", exp->data ? " DATA" : "", res ? res : ""); } else { char *quote1 = strchr (exp->internal_name, '.') ? "\"" : ""; /* char *alias = */ fprintf (output_def, "\t%s%s%s = %s%s%s @ %d%s%s ; %s\n", quote, exp->name, quote, quote1, exp->internal_name, quote1, exp->ordinal, exp->noname ? " NONAME" : "", exp->data ? " DATA" : "", res ? res : ""); } if (res) free (res); } inform (_("Added exports to output file"));}/* generate_idata_ofile generates the portable assembly source code for the idata sections. It appends the source code to the end of the file. */static voidgenerate_idata_ofile (filvar) FILE *filvar;{ iheadtype *headptr; ifunctype *funcptr; int headindex; int funcindex; int nheads; if (import_list == NULL) return; fprintf (filvar, "%s Import data sections\n", ASM_C); fprintf (filvar, "\n\t.section\t.idata$2\n"); fprintf (filvar, "\t%s\tdoi_idata\n", ASM_GLOBAL); fprintf (filvar, "doi_idata:\n"); nheads = 0; for (headptr = import_list; headptr != NULL; headptr = headptr->next) { fprintf (filvar, "\t%slistone%d%s\t%s %s\n", ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER, ASM_C, headptr->dllname); fprintf (filvar, "\t%s\t0\n", ASM_LONG); fprintf (filvar, "\t%s\t0\n", ASM_LONG); fprintf (filvar, "\t%sdllname%d%s\n", ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER); fprintf (filvar, "\t%slisttwo%d%s\n\n", ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER); nheads++; } fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL record at */ fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* end of idata$2 */ fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* section */ fprintf (filvar, "\t%s\t0\n", ASM_LONG); fprintf (filvar, "\t%s\t0\n", ASM_LONG); fprintf (filvar, "\n\t.section\t.idata$4\n"); headindex = 0; for (headptr = import_list; headptr != NULL; headptr = headptr->next) { fprintf (filvar, "listone%d:\n", headindex); for ( funcindex = 0; funcindex < headptr->nfuncs; funcindex++ ) fprintf (filvar, "\t%sfuncptr%d_%d%s\n", ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER); fprintf (filvar,"\t%s\t0\n", ASM_LONG); /* NULL terminating list */ headindex++; } fprintf (filvar, "\n\t.section\t.idata$5\n"); headindex = 0; for (headptr = import_list; headptr != NULL; headptr = headptr->next) { fprintf (filvar, "listtwo%d:\n", headindex); for ( funcindex = 0; funcindex < headptr->nfuncs; funcindex++ ) fprintf (filvar, "\t%sfuncptr%d_%d%s\n", ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER); fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list */ headindex++; } fprintf (filvar, "\n\t.section\t.idata$6\n"); headindex = 0; for (headptr = import_list; headptr != NULL; headptr = headptr->next) { funcindex = 0; for (funcptr = headptr->funchead; funcptr != NULL; funcptr = funcptr->next) { fprintf (filvar,"funcptr%d_%d:\n", headindex, funcindex); fprintf (filvar,"\t%s\t%d\n", ASM_SHORT, ((funcptr->ord) & 0xFFFF)); fprintf (filvar,"\t%s\t\"%s\"\n", ASM_TEXT, funcptr->name); fprintf (filvar,"\t%s\t0\n", ASM_BYTE); funcindex++; } headindex++; } fprintf (filvar, "\n\t.section\t.idata$7\n"); headindex = 0; for (headptr = import_list; headptr != NULL; headptr = headptr->next) { fprintf (filvar,"dllname%d:\n", headindex); fprintf (filvar,"\t%s\t\"%s\"\n", ASM_TEXT, headptr->dllname); fprintf (filvar,"\t%s\t0\n", ASM_BYTE); headindex++; }}/* Assemble the specified file. */static voidassemble_file (source, dest) const char * source; const char * dest;{ char * cmd; cmd = (char *) alloca (strlen (ASM_SWITCHES) + strlen (as_flags) + strlen (source) + strlen (dest) + 50); sprintf (cmd, "%s %s -o %s %s", ASM_SWITCHES, as_flags, dest, source); run (as_name, cmd);}static voidgen_exp_file (){ FILE *f; int i; export_type *exp; dlist_type *dl; /* xgettext:c-format */ inform (_("Generating export file: %s"), exp_name); f = fopen (TMP_ASM, FOPEN_WT); if (!f) /* xgettext:c-format */ fatal (_("Unable to open temporary assembler file: %s"), TMP_ASM); /* xgettext:c-format */ inform (_("Opened temporary file: %s"), TMP_ASM); dump_def_info (f); if (d_exports) { fprintf (f, "\t.section .edata\n\n"); fprintf (f, "\t%s 0 %s Allways 0\n", ASM_LONG, ASM_C); fprintf (f, "\t%s 0x%lx %s Time and date\n", ASM_LONG, (long) time(0), ASM_C); fprintf (f, "\t%s 0 %s Major and Minor version\n", ASM_LONG, ASM_C); fprintf (f, "\t%sname%s %s Ptr to name of dll\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); fprintf (f, "\t%s %d %s Starting ordinal of exports\n", ASM_LONG, d_low_ord, ASM_C); fprintf (f, "\t%s %d %s Number of functions\n", ASM_LONG, d_high_ord - d_low_ord + 1, ASM_C); fprintf(f,"\t%s named funcs %d, low ord %d, high ord %d\n", ASM_C, d_named_nfuncs, d_low_ord, d_high_ord); fprintf (f, "\t%s %d %s Number of names\n", ASM_LONG, show_allnames ? d_high_ord - d_low_ord + 1 : d_named_nfuncs, ASM_C); fprintf (f, "\t%safuncs%s %s Address of functions\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); fprintf (f, "\t%sanames%s %s Address of Name Pointer Table\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); fprintf (f, "\t%sanords%s %s Address of ordinals\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); fprintf (f, "name: %s \"%s\"\n", ASM_TEXT, dll_name); fprintf(f,"%s Export address Table\n", ASM_C); fprintf(f,"\t%s\n", ASM_ALIGN_LONG); fprintf (f, "afuncs:\n"); i = d_low_ord; for (exp = d_exports; exp; exp = exp->next) { if (exp->ordinal != i) {#if 0 fprintf (f, "\t%s\t%d\t%s %d..%d missing\n", ASM_SPACE, (exp->ordinal - i) * 4, ASM_C, i, exp->ordinal - 1); i = exp->ordinal;#endif while (i < exp->ordinal) { fprintf(f,"\t%s\t0\n", ASM_LONG); i++; } } fprintf (f, "\t%s%s%s%s\t%s %d\n", ASM_RVA_BEFORE, ASM_PREFIX, exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal); i++; } fprintf (f,"%s Export Name Pointer Table\n", ASM_C); fprintf (f, "anames:\n"); for (i = 0; (exp = d_exports_lexically[i]); i++) { if (!exp->noname || show_allnames) fprintf (f, "\t%sn%d%s\n", ASM_RVA_BEFORE, exp->ordinal, ASM_RVA_AFTER); } fprintf (f,"%s Export Oridinal Table\n", ASM_C); fprintf (f, "anords:\n"); for (i = 0; (exp = d_exports_lexically[i]); i++) { if (!exp->noname || show_allnames) fprintf (f, "\t%s %d\n", ASM_SHORT, exp->ordinal - d_low_ord); } fprintf(f,"%s Export Name Table\n", ASM_C); for (i = 0; (exp = d_exports_lexically[i]); i++) if (!exp->noname || show_allnames) fprintf (f, "n%d: %s \"%s\"\n", exp->ordinal, ASM_TEXT, exp->name); if (a_list) { fprintf (f, "\t.section %s\n", DRECTVE_SECTION_NAME); for (dl = a_list; dl; dl = dl->next) { fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, dl->text); } } if (d_list) { fprintf (f, "\t.section .rdata\n"); for (dl = d_list; dl; dl = dl->next) { char *p; int l; /* We don't output as ascii because there can be quote characters in the string. */ l = 0; for (p = dl->text; *p; p++) { if (l == 0) fprintf (f, "\t%s\t", ASM_BYTE); else fprintf (f, ","); fprintf (f, "%d", *p); if (p[1] == 0) { fprintf (f, ",0\n"); break; } if (++l == 10) { fprintf (f, "\n"); l = 0; } } } } } /* Add to the output file a way of getting to the exported names without using the import library. */ if (add_indirect) { fprintf (f, "\t.section\t.rdata\n"); for (i = 0, exp = d_exports; exp; i++, exp = exp->next) if (!exp->noname || show_allnames) { /* We use a single underscore for MS compatibility, and a double underscore for backward compatibility with old cygwin releases. */ if (create_compat_implib) fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name); fprintf (f, "\t%s\t_imp__%s\n", ASM_GLOBAL, exp->name); if (create_compat_implib) fprintf (f, "__imp_%s:\n", exp->name); fprintf (f, "_imp__%s:\n", exp->name); fprintf (f, "\t%s\t%s\n", ASM_LONG, exp->name); } } /* Dump the reloc section if a base file is provided */ if (base_file) { int addr; long need[PAGE_SIZE]; long page_addr; int numbytes; int num_entries; long *copy; int j; int on_page; fprintf (f, "\t.section\t.init\n"); fprintf (f, "lab:\n"); fseek (base_file, 0, SEEK_END); numbytes = ftell (base_file); fseek (base_file, 0, SEEK_SET); copy = xmalloc (numbytes); fread (copy, 1, numbytes, base_file); num_entries = numbytes / sizeof (long); fprintf (f, "\t.section\t.reloc\n"); if (num_entries) { int src; int dst = 0; int last = -1; qsort (copy, num_entries, sizeof (long), sfunc); /* Delete duplcates */ for (src = 0; src < num_entries; src++) { if (last != copy[src]) last = copy[dst++] = copy[src]; } num_entries = dst; addr = copy[0]; page_addr = addr & PAGE_MASK; /* work out the page addr */ on_page = 0; for (j = 0; j < num_entries; j++) { addr = copy[j]; if ((addr & PAGE_MASK) != page_addr) { flush_page (f, need, page_addr, on_page); on_page = 0; page_addr = addr & PAGE_MASK; } need[on_page++] = addr; } flush_page (f, need, page_addr, on_page);/* fprintf (f, "\t%s\t0,0\t%s End\n", ASM_LONG, ASM_C);*/ } } generate_idata_ofile (f); fclose (f); /* assemble the file */ assemble_file (TMP_ASM, exp_name); if (dontdeltemps == 0) unlink (TMP_ASM); inform (_("Generated exports file"));}static const char *xlate (name) const char *name;{ if (add_underscore) { char *copy = xmalloc (strlen (name) + 2); copy[0] = '_'; strcpy (copy + 1, name); name = copy; } if (killat) { char *p; p = strchr (name, '@'); if (p) *p = 0; } return name;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?