From 05108a3b0a8eff70739b253b8995999b1861f9f2 Mon Sep 17 00:00:00 2001 From: grischka Date: Tue, 12 Feb 2013 19:13:28 +0100 Subject: libtcc: new LIBTCCAPI tcc_set_options(TCCState*, const char*str) This replaces -> use instead: ----------------------------------- - tcc_set_linker -> tcc_set_options(s, "-Wl,..."); - tcc_set_warning -> tcc_set_options(s, "-W..."); - tcc_enable_debug -> tcc_set_options(s, "-g"); parse_args is moved to libtcc.c (now tcc_parse_args). Also some cleanups: - reorder TCCState members - add some comments here and there - do not use argv's directly, make string copies - use const char* in tcc_set_linker - tccpe: use fd instead of fp tested with -D MEM_DEBUG: 0 bytes left --- tccpe.c | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) (limited to 'tccpe.c') diff --git a/tccpe.c b/tccpe.c index 120dc4e..79be6f3 100644 --- a/tccpe.c +++ b/tccpe.c @@ -1505,10 +1505,10 @@ static int add_dllref(TCCState *s1, const char *dllname) /* ------------------------------------------------------------- */ -static int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len) +static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) { - fseek(fp, offset, 0); - return len == fread(buffer, 1, len, fp); + lseek(fd, offset, SEEK_SET); + return len == read(fd, buffer, len); } /* ------------------------------------------------------------- @@ -1516,7 +1516,7 @@ static int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len) * as generated by 'windres.exe -O coff ...'. */ -static int pe_load_res(TCCState *s1, FILE *fp) +static int pe_load_res(TCCState *s1, int fd) { struct pe_rsrc_header hdr; Section *rsrc_section; @@ -1524,7 +1524,7 @@ static int pe_load_res(TCCState *s1, FILE *fp) BYTE *ptr; unsigned offs; - if (!read_mem(fp, 0, &hdr, sizeof hdr)) + if (!read_mem(fd, 0, &hdr, sizeof hdr)) goto quit; if (hdr.filehdr.Machine != 0x014C @@ -1535,13 +1535,13 @@ static int pe_load_res(TCCState *s1, FILE *fp) rsrc_section = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC); ptr = section_ptr_add(rsrc_section, hdr.sectionhdr.SizeOfRawData); offs = hdr.sectionhdr.PointerToRawData; - if (!read_mem(fp, offs, ptr, hdr.sectionhdr.SizeOfRawData)) + if (!read_mem(fd, offs, ptr, hdr.sectionhdr.SizeOfRawData)) goto quit; offs = hdr.sectionhdr.PointerToRelocations; for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i) { struct pe_rsrc_reloc rel; - if (!read_mem(fp, offs, &rel, sizeof rel)) + if (!read_mem(fd, offs, &rel, sizeof rel)) goto quit; // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type); if (rel.type != 7) /* DIR32NB */ @@ -1571,22 +1571,26 @@ static char *trimback(char *a, char *e) return a; } -static char *get_line(char *line, int size, FILE *fp) +static char *get_line(char *line, int size, int fd) { - if (NULL == fgets(line, size, fp)) + int n; + for (n = 0; n < size - 1; ) + if (read(fd, line + n, 1) < 1 || line[n++] == '\n') + break; + if (0 == n) return NULL; - trimback(line, strchr(line, 0)); + trimback(line, line + n); return trimfront(line); } /* ------------------------------------------------------------- */ -static int pe_load_def(TCCState *s1, FILE *fp) +static int pe_load_def(TCCState *s1, int fd) { int state = 0, ret = -1, dllindex = 0; char line[400], dllname[80], *p; for (;;) { - p = get_line(line, sizeof line, fp); + p = get_line(line, sizeof line, fd); if (NULL == p) break; if (0 == *p || ';' == *p) @@ -1623,11 +1627,11 @@ quit: #define TINY_IMPDEF_GET_EXPORT_NAMES_ONLY #include "win32/tools/tiny_impdef.c" -static int pe_load_dll(TCCState *s1, const char *dllname, FILE *fp) +static int pe_load_dll(TCCState *s1, const char *dllname, int fd) { char *p, *q; int index; - p = get_export_names(fp); + p = get_export_names(fd); if (!p) return -1; index = add_dllref(s1, dllname); @@ -1640,18 +1644,14 @@ static int pe_load_dll(TCCState *s1, const char *dllname, FILE *fp) /* ------------------------------------------------------------- */ ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd) { - FILE *fp = fdopen(dup(fd), "rb"); int ret = -1; char buf[10]; - if (fp) { - if (0 == strcmp(tcc_fileextension(filename), ".def")) - ret = pe_load_def(s1, fp); - else if (pe_load_res(s1, fp) == 0) - ret = 0; - else if (read_mem(fp, 0, buf, sizeof buf) && 0 == strncmp(buf, "MZ", 2)) - ret = pe_load_dll(s1, tcc_basename(filename), fp); - fclose(fp); - } + if (0 == strcmp(tcc_fileextension(filename), ".def")) + ret = pe_load_def(s1, fd); + else if (pe_load_res(s1, fd) == 0) + ret = 0; + else if (read_mem(fd, 0, buf, sizeof buf) && 0 == strncmp(buf, "MZ", 2)) + ret = pe_load_dll(s1, tcc_basename(filename), fd); return ret; } @@ -1723,7 +1723,7 @@ ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack) #define PE_STDSYM(n,s) "_" n s #endif -static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) +static void pe_add_runtime(TCCState *s1, struct pe_info *pe) { const char *start_symbol; ADDR3264 addr = 0; @@ -1762,15 +1762,17 @@ static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) if (0 == s1->nostdlib) { static const char *libs[] = { - "tcc1", "msvcrt", "kernel32", "", "user32", "gdi32", NULL + "libtcc1.a", "msvcrt", "kernel32", "", "user32", "gdi32", NULL }; const char **pp, *p; for (pp = libs; 0 != (p = *pp); ++pp) { if (0 == *p) { if (PE_DLL != pe_type && PE_GUI != pe_type) break; - } else if (tcc_add_library(s1, p) < 0) + } else if (pp == libs ? tcc_add_dll(s1, p, 0) : tcc_add_library(s1, p)) { tcc_error_noabort("cannot find library: %s", p); + break; + } } } @@ -1802,7 +1804,7 @@ ST_FUNC int pe_output_file(TCCState * s1, const char *filename) pe.s1 = s1; tcc_add_bcheck(s1); - pe_add_runtime_ex(s1, &pe); + pe_add_runtime(s1, &pe); relocate_common_syms(); /* assign bss adresses */ tcc_add_linker_symbols(s1); -- cgit v1.3.1