History

Switch to newer tccpe.c (includes support for resources)

grischka committed 18 years ago

Commit 5342b321 parent adb14566 files · +1342 −987Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. i386-gen.c +3 −3
  3. tcc.c +174 −135
  4. tccelf.c +79 −82
  5. tccpe.c +1007 −700
  6. win32/readme.txt +78 −67
Changelog+1−0
@@ -1,5 +1,6 @@
1version 0.9.24:1version 0.9.24:
2 2
3- Switch to newer tccpe.c (includes support for resources)
3- Handle backslashes within #include, #error, #warning4- Handle backslashes within #include, #error, #warning
4- Import changesets (part 4) 428,457,460,467: defines for openbsd etc.5- Import changesets (part 4) 428,457,460,467: defines for openbsd etc.
5- Use _WIN32 for a windows hosted tcc and define it for the PE target,6- Use _WIN32 for a windows hosted tcc and define it for the PE target,
i386-gen.c+3−3
@@ -380,7 +380,7 @@ void gfunc_call(int nb_args) · 379 unmodified lines
380 }380 }
381 save_regs(0); /* save used temporary registers */381 save_regs(0); /* save used temporary registers */
382 func_sym = vtop->type.ref;382 func_sym = vtop->type.ref;
383 func_call = func_sym->r;383 func_call = FUNC_CALL(func_sym->r);
384 /* fast call case */384 /* fast call case */
385 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||385 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
386 func_call == FUNC_FASTCALLW) {386 func_call == FUNC_FASTCALLW) {
@@ -402,7 +402,7 @@ void gfunc_call(int nb_args) · 15 unmodified lines
402 }402 }
403 }403 }
404 gcall_or_jmp(0);404 gcall_or_jmp(0);
405 if (args_size && func_sym->r != FUNC_STDCALL)405 if (args_size && func_call != FUNC_STDCALL)
406 gadd_sp(args_size);406 gadd_sp(args_size);
407 vtop--;407 vtop--;
408}408}
@@ -423,7 +423,7 @@ void gfunc_prolog(CType *func_type) · 14 unmodified lines
423 CType *type;423 CType *type;
424 424
425 sym = func_type->ref;425 sym = func_type->ref;
426 func_call = sym->r;426 func_call = FUNC_CALL(sym->r);
427 addr = 8;427 addr = 8;
428 loc = 0;428 loc = 0;
429 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {429 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
tcc.c+174−135
@@ -40,7 +40,7 @@ · 39 unmodified lines
40#include <time.h>40#include <time.h>
41#ifdef _WIN3241#ifdef _WIN32
42#include <sys/timeb.h>42#include <sys/timeb.h>
43// #include <windows.h>43#include <windows.h>
44#endif44#endif
45#ifndef _WIN3245#ifndef _WIN32
46#include <sys/time.h>46#include <sys/time.h>
@@ -230,10 +230,24 @@ typedef struct AttributeDef { · 183 unmodified lines
230 int aligned;230 int aligned;
231 int packed; 231 int packed;
232 Section *section;232 Section *section;
233 unsigned char func_call; /* FUNC_CDECL, FUNC_STDCALL, FUNC_FASTCALLx */233 int func_attr; /* calling convention, exports, ... */
234 unsigned char dllexport;
235} AttributeDef;234} AttributeDef;
236 235
236/* -------------------------------------------------- */
237/* gr: wrappers for casting sym->r for other purposes */
238typedef struct {
239 unsigned
240 func_call : 8,
241 func_args : 8,
242 func_export : 1;
243} func_attr_t;
244
245#define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
246#define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
247#define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
248#define INLINE_DEF(r) (*(int **)&(r))
249/* -------------------------------------------------- */
250
237#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */251#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
238#define SYM_FIELD 0x20000000 /* struct/union field symbol space */252#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
239#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */253#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
@@ -383,6 +397,9 @@ static CType char_pointer_type, func_old_type, int_type; · 143 unmodified lines
383/* true if isid(c) || isnum(c) */397/* true if isid(c) || isnum(c) */
384static unsigned char isidnum_table[256];398static unsigned char isidnum_table[256];
385 399
400/* display some information during compilation */
401static int verbose = 0;
402
386/* compile with debug symbol (and use them if error during execution) */403/* compile with debug symbol (and use them if error during execution) */
387static int do_debug = 0;404static int do_debug = 0;
388 405
@@ -723,14 +740,6 @@ static const char tcc_keywords[] = · 334 unmodified lines
723#define TOK_UIDENT TOK_DEFINE740#define TOK_UIDENT TOK_DEFINE
724 741
725#ifdef _WIN32742#ifdef _WIN32
726int __stdcall GetModuleFileNameA(void *, char *, int);
727void *__stdcall GetProcAddress(void *, const char *);
728void *__stdcall GetModuleHandleA(const char *);
729void *__stdcall LoadLibraryA(const char *);
730int __stdcall FreeConsole(void);
731int __stdcall VirtualProtect(void*,unsigned long,unsigned long,unsigned long*);
732#define PAGE_EXECUTE_READWRITE 0x0040
733
734#define snprintf _snprintf743#define snprintf _snprintf
735#define vsnprintf _vsnprintf744#define vsnprintf _vsnprintf
736#ifndef __GNUC__745#ifndef __GNUC__
@@ -758,6 +767,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 21 unmodified lines
758static char *pstrcpy(char *buf, int buf_size, const char *s);767static char *pstrcpy(char *buf, int buf_size, const char *s);
759static char *pstrcat(char *buf, int buf_size, const char *s);768static char *pstrcat(char *buf, int buf_size, const char *s);
760static char *tcc_basename(const char *name);769static char *tcc_basename(const char *name);
770static char *tcc_fileextension (const char *p);
761 771
762static void next(void);772static void next(void);
763static void next_nomacro(void);773static void next_nomacro(void);
@@ -858,10 +868,12 @@ int tcc_output_coff(TCCState *s1, FILE *f); · 94 unmodified lines
858 868
859/* tccpe.c */869/* tccpe.c */
860void *resolve_sym(TCCState *s1, const char *sym, int type);870void *resolve_sym(TCCState *s1, const char *sym, int type);
861int pe_load_def_file(struct TCCState *s1, FILE *fp);871int pe_load_def_file(struct TCCState *s1, int fd);
862void pe_setup_paths(struct TCCState *s1, int *p_output_type, const char **p_outfile, char *first_file);872int pe_test_res_file(void *v, int size);
863unsigned long pe_add_runtime(struct TCCState *s1);873int pe_load_res_file(struct TCCState *s1, int fd);
864int tcc_output_pe(struct TCCState *s1, const char *filename);874void pe_add_runtime(struct TCCState *s1);
875void pe_guess_outfile(char *objfilename, int output_type);
876int pe_output_file(struct TCCState *s1, const char *filename);
865 877
866/* tccasm.c */878/* tccasm.c */
867 879
@@ -1036,6 +1048,27 @@ static int strstart(const char *str, const char *val, const char **ptr) · 168 unmodified lines
1036 return 1;1048 return 1;
1037}1049}
1038 1050
1051/* extract the basename of a file */
1052static char *tcc_basename(const char *name)
1053{
1054 char *p = strchr(name, 0);
1055 while (p > name
1056 && p[-1] != '/'
1057#ifdef _WIN32
1058 && p[-1] != '\\'
1059#endif
1060 )
1061 --p;
1062 return p;
1063}
1064
1065static char *tcc_fileextension (const char *name)
1066{
1067 char *b = tcc_basename(name);
1068 char *e = strrchr(b, '.');
1069 return e ? e : strchr(b, 0);
1070}
1071
1039#ifdef _WIN321072#ifdef _WIN32
1040char *normalize_slashes(char *path)1073char *normalize_slashes(char *path)
1041{1074{
@@ -1162,6 +1195,16 @@ static void dynarray_add(void ***ptab, int *nb_ptr, void *data) · 120 unmodified lines
1162 *nb_ptr = nb;1195 *nb_ptr = nb;
1163}1196}
1164 1197
1198static void dynarray_reset(void *pp, int *n)
1199{
1200 void **p;
1201 for (p = *(void***)pp; *n; ++p, --*n)
1202 if (*p)
1203 tcc_free(*p);
1204 tcc_free(*(void**)pp);
1205 *(void**)pp = NULL;
1206}
1207
1165/* symbol allocator */1208/* symbol allocator */
1166static Sym *__sym_malloc(void)1209static Sym *__sym_malloc(void)
1167{1210{
@@ -1291,7 +1334,7 @@ static void put_extern_sym2(Sym *sym, Section *section, · 123 unmodified lines
1291 unsigned long value, unsigned long size,1334 unsigned long value, unsigned long size,
1292 int can_add_underscore)1335 int can_add_underscore)
1293{1336{
1294 int sym_type, sym_bind, sh_num, info;1337 int sym_type, sym_bind, sh_num, info, other, attr;
1295 Elf32_Sym *esym;1338 Elf32_Sym *esym;
1296 const char *name;1339 const char *name;
1297 char buf1[256];1340 char buf1[256];
@@ -1302,16 +1345,29 @@ static void put_extern_sym2(Sym *sym, Section *section, · 4 unmodified lines
1302 sh_num = SHN_ABS;1345 sh_num = SHN_ABS;
1303 else1346 else
1304 sh_num = section->sh_num;1347 sh_num = section->sh_num;
1348
1349 other = attr = 0;
1350
1351 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1352 sym_type = STT_FUNC;
1353#ifdef TCC_TARGET_PE
1354 if (sym->type.ref)
1355 attr = sym->type.ref->r;
1356 if (FUNC_EXPORT(attr))
1357 other |= 1;
1358 if (FUNC_CALL(attr) == FUNC_STDCALL)
1359 other |= 2;
1360#endif
1361 } else {
1362 sym_type = STT_OBJECT;
1363 }
1364
1365 if (sym->type.t & VT_STATIC)
1366 sym_bind = STB_LOCAL;
1367 else
1368 sym_bind = STB_GLOBAL;
1369
1305 if (!sym->c) {1370 if (!sym->c) {
1306 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1307 sym_type = STT_FUNC;
1308 else
1309 sym_type = STT_OBJECT;
1310 if (sym->type.t & VT_STATIC)
1311 sym_bind = STB_LOCAL;
1312 else
1313 sym_bind = STB_GLOBAL;
1314
1315 name = get_tok_str(sym->v, NULL);1371 name = get_tok_str(sym->v, NULL);
1316#ifdef CONFIG_TCC_BCHECK1372#ifdef CONFIG_TCC_BCHECK
1317 if (do_bounds_check) {1373 if (do_bounds_check) {
@@ -1342,18 +1398,26 @@ static void put_extern_sym2(Sym *sym, Section *section, · 24 unmodified lines
1342 }1398 }
1343 }1399 }
1344#endif1400#endif
1401
1402#ifdef TCC_TARGET_PE
1403 if ((other & 2) && can_add_underscore) {
1404 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1405 name = buf1;
1406 } else
1407#endif
1345 if (tcc_state->leading_underscore && can_add_underscore) {1408 if (tcc_state->leading_underscore && can_add_underscore) {
1346 buf1[0] = '_';1409 buf1[0] = '_';
1347 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);1410 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1348 name = buf1;1411 name = buf1;
1349 }1412 }
1350 info = ELF32_ST_INFO(sym_bind, sym_type);1413 info = ELF32_ST_INFO(sym_bind, sym_type);
1351 sym->c = add_elf_sym(symtab_section, value, size, info, 0, sh_num, name);1414 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1352 } else {1415 } else {
1353 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];1416 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1354 esym->st_value = value;1417 esym->st_value = value;
1355 esym->st_size = size;1418 esym->st_size = size;
1356 esym->st_shndx = sh_num;1419 esym->st_shndx = sh_num;
1420 esym->st_other |= other;
1357 }1421 }
1358}1422}
1359 1423
@@ -2868,7 +2932,7 @@ static void preprocess(int is_bof) · 1508 unmodified lines
2868{2932{
2869 TCCState *s1 = tcc_state;2933 TCCState *s1 = tcc_state;
2870 int size, i, c, n, saved_parse_flags;2934 int size, i, c, n, saved_parse_flags;
2871 char buf[1024], *q, *p;2935 char buf[1024], *q;
2872 char buf1[1024];2936 char buf1[1024];
2873 BufferedFile *f;2937 BufferedFile *f;
2874 Sym *s;2938 Sym *s;
@@ -2962,10 +3026,7 @@ static void preprocess(int is_bof) · 87 unmodified lines
2962 } else {3026 } else {
2963 if (c == '\"') {3027 if (c == '\"') {
2964 /* first search in current dir if "header.h" */3028 /* first search in current dir if "header.h" */
2965 size = 0;3029 size = tcc_basename(file->filename) - file->filename;
2966 p = strrchr(file->filename, '/');
2967 if (p)
2968 size = p + 1 - file->filename;
2969 if (size > sizeof(buf1) - 1)3030 if (size > sizeof(buf1) - 1)
2970 size = sizeof(buf1) - 1;3031 size = sizeof(buf1) - 1;
2971 memcpy(buf1, file->filename, size);3032 memcpy(buf1, file->filename, size);
@@ -4026,7 +4087,9 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 1054 unmodified lines
4026 cstr_ccat(&cstr, ' ');4087 cstr_ccat(&cstr, ' ');
4027 TOK_GET(t, st, cval);4088 TOK_GET(t, st, cval);
4028 cstr_cat(&cstr, get_tok_str(t, &cval));4089 cstr_cat(&cstr, get_tok_str(t, &cval));
4090#ifndef PP_NOSPACES
4029 notfirst = 1;4091 notfirst = 1;
4092#endif
4030 }4093 }
4031 cstr_ccat(&cstr, '\0');4094 cstr_ccat(&cstr, '\0');
4032#ifdef PP_DEBUG4095#ifdef PP_DEBUG
@@ -6112,7 +6175,7 @@ static int is_compatible_func(CType *type1, CType *type2) · 2079 unmodified lines
6112 if (!is_compatible_types(&s1->type, &s2->type))6175 if (!is_compatible_types(&s1->type, &s2->type))
6113 return 0;6176 return 0;
6114 /* check func_call */6177 /* check func_call */
6115 if (s1->r != s2->r)6178 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6116 return 0;6179 return 0;
6117 /* XXX: not complete */6180 /* XXX: not complete */
6118 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)6181 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
@@ -6569,12 +6632,12 @@ static void parse_attribute(AttributeDef *ad) · 450 unmodified lines
6569 case TOK_CDECL1:6632 case TOK_CDECL1:
6570 case TOK_CDECL2:6633 case TOK_CDECL2:
6571 case TOK_CDECL3:6634 case TOK_CDECL3:
6572 ad->func_call = FUNC_CDECL;6635 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6573 break;6636 break;
6574 case TOK_STDCALL1:6637 case TOK_STDCALL1:
6575 case TOK_STDCALL2:6638 case TOK_STDCALL2:
6576 case TOK_STDCALL3:6639 case TOK_STDCALL3:
6577 ad->func_call = FUNC_STDCALL;6640 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6578 break;6641 break;
6579#ifdef TCC_TARGET_I3866642#ifdef TCC_TARGET_I386
6580 case TOK_REGPARM1:6643 case TOK_REGPARM1:
@@ -6586,17 +6649,17 @@ static void parse_attribute(AttributeDef *ad) · 5 unmodified lines
6586 else if (n < 0)6649 else if (n < 0)
6587 n = 0;6650 n = 0;
6588 if (n > 0)6651 if (n > 0)
6589 ad->func_call = FUNC_FASTCALL1 + n - 1;6652 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6590 skip(')');6653 skip(')');
6591 break;6654 break;
6592 case TOK_FASTCALL1:6655 case TOK_FASTCALL1:
6593 case TOK_FASTCALL2:6656 case TOK_FASTCALL2:
6594 case TOK_FASTCALL3:6657 case TOK_FASTCALL3:
6595 ad->func_call = FUNC_FASTCALLW;6658 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6596 break; 6659 break;
6597#endif6660#endif
6598 case TOK_DLLEXPORT:6661 case TOK_DLLEXPORT:
6599 ad->dllexport = 1;6662 FUNC_EXPORT(ad->func_attr) = 1;
6600 break;6663 break;
6601 default:6664 default:
6602 if (tcc_state->warn_unsupported)6665 if (tcc_state->warn_unsupported)
@@ -6979,7 +7042,7 @@ static int parse_btype(CType *type, AttributeDef *ad) · 376 unmodified lines
6979 }7042 }
6980the_end:7043the_end:
6981 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))7044 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
6982 error("signed and unsigned modifier");7045 error("signed and unsigned modifier");
6983 if (tcc_state->char_is_unsigned) {7046 if (tcc_state->char_is_unsigned) {
6984 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)7047 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
6985 t |= VT_UNSIGNED;7048 t |= VT_UNSIGNED;
@@ -7009,7 +7072,7 @@ static inline void convert_parameter_type(CType *pt) · 23 unmodified lines
7009 7072
7010static void post_type(CType *type, AttributeDef *ad)7073static void post_type(CType *type, AttributeDef *ad)
7011{7074{
7012 int n, l, t1;7075 int n, l, t1, arg_size, align;
7013 Sym **plast, *s, *first;7076 Sym **plast, *s, *first;
7014 AttributeDef ad1;7077 AttributeDef ad1;
7015 CType pt;7078 CType pt;
@@ -7020,6 +7083,7 @@ static void post_type(CType *type, AttributeDef *ad) · 4 unmodified lines
7020 l = 0;7083 l = 0;
7021 first = NULL;7084 first = NULL;
7022 plast = &first;7085 plast = &first;
7086 arg_size = 0;
7023 if (tok != ')') {7087 if (tok != ')') {
7024 for(;;) {7088 for(;;) {
7025 /* read param name and compute offset */7089 /* read param name and compute offset */
@@ -7038,6 +7102,7 @@ static void post_type(CType *type, AttributeDef *ad) · 12 unmodified lines
7038 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);7102 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7039 if ((pt.t & VT_BTYPE) == VT_VOID)7103 if ((pt.t & VT_BTYPE) == VT_VOID)
7040 error("parameter declared as void");7104 error("parameter declared as void");
7105 arg_size += (type_size(&pt, &align) + 3) & ~3;
7041 } else {7106 } else {
7042 old_proto:7107 old_proto:
7043 n = tok;7108 n = tok;
@@ -7070,7 +7135,8 @@ static void post_type(CType *type, AttributeDef *ad) · 26 unmodified lines
7070 type->t &= ~(VT_STORAGE | VT_CONSTANT); 7135 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7071 post_type(type, ad);7136 post_type(type, ad);
7072 /* we push a anonymous symbol which will contain the function prototype */7137 /* we push a anonymous symbol which will contain the function prototype */
7073 s = sym_push(SYM_FIELD, type, ad->func_call, l);7138 FUNC_ARGS(ad->func_attr) = arg_size;
7139 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7074 s->next = first;7140 s->next = first;
7075 type->t = t1 | VT_FUNC;7141 type->t = t1 | VT_FUNC;
7076 type->ref = s;7142 type->ref = s;
@@ -7629,6 +7695,7 @@ static void unary(void) · 552 unmodified lines
7629 next();7695 next();
7630 sa = s->next; /* first parameter */7696 sa = s->next; /* first parameter */
7631 nb_args = 0;7697 nb_args = 0;
7698 ret.r2 = VT_CONST;
7632 /* compute first implicit argument if a structure is returned */7699 /* compute first implicit argument if a structure is returned */
7633 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {7700 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7634 /* get some space for the returned structure */7701 /* get some space for the returned structure */
@@ -7643,7 +7710,6 @@ static void unary(void) · 8 unmodified lines
7643 nb_args++;7710 nb_args++;
7644 } else {7711 } else {
7645 ret.type = s->type; 7712 ret.type = s->type;
7646 ret.r2 = VT_CONST;
7647 /* return in register */7713 /* return in register */
7648 if (is_float(ret.type.t)) {7714 if (is_float(ret.type.t)) {
7649 ret.r = REG_FRET; 7715 ret.r = REG_FRET;
@@ -8660,12 +8726,11 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 1010 unmodified lines
8660 them as ((w)char *) expressions */8726 them as ((w)char *) expressions */
8661 if ((tok == TOK_LSTR && 8727 if ((tok == TOK_LSTR &&
8662#ifdef TCC_TARGET_PE8728#ifdef TCC_TARGET_PE
8663 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)) ||8729 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8664#else8730#else
8665 (t1->t & VT_BTYPE) == VT_INT) ||8731 (t1->t & VT_BTYPE) == VT_INT
8666#endif8732#endif
8667 (tok == TOK_STR &&8733 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8668 (t1->t & VT_BTYPE) == VT_BYTE)) {
8669 while (tok == TOK_STR || tok == TOK_LSTR) {8734 while (tok == TOK_STR || tok == TOK_LSTR) {
8670 int cstr_len, ch;8735 int cstr_len, ch;
8671 CString *cstr;8736 CString *cstr;
@@ -9166,9 +9231,9 @@ static void gen_inline_functions(void) · 494 unmodified lines
9166 sym->c != 0) {9231 sym->c != 0) {
9167 /* the function was used: generate its code and9232 /* the function was used: generate its code and
9168 convert it to a normal function */9233 convert it to a normal function */
9169 str = (int *)sym->r;9234 str = INLINE_DEF(sym->r);
9170 sym->r = VT_SYM | VT_CONST;9235 sym->r = VT_SYM | VT_CONST;
9171 type->t &= ~VT_INLINE;9236 sym->type.t &= ~VT_INLINE;
9172 9237
9173 macro_ptr = str;9238 macro_ptr = str;
9174 next();9239 next();
@@ -9190,7 +9255,10 @@ static void gen_inline_functions(void) · 15 unmodified lines
9190 if (((type->t & VT_BTYPE) == VT_FUNC) &&9255 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9191 (type->t & (VT_STATIC | VT_INLINE)) == 9256 (type->t & (VT_STATIC | VT_INLINE)) ==
9192 (VT_STATIC | VT_INLINE)) {9257 (VT_STATIC | VT_INLINE)) {
9193 str = (int *)sym->r;9258 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9259 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9260 continue;
9261 str = INLINE_DEF(sym->r);
9194 tok_str_free(str);9262 tok_str_free(str);
9195 sym->r = 0; /* fail safe */9263 sym->r = 0; /* fail safe */
9196 }9264 }
@@ -9273,9 +9341,13 @@ static void decl(int l) · 76 unmodified lines
9273 /* specific case: if not func_call defined, we put9341 /* specific case: if not func_call defined, we put
9274 the one of the prototype */9342 the one of the prototype */
9275 /* XXX: should have default value */9343 /* XXX: should have default value */
9276 if (sym->type.ref->r != FUNC_CDECL &&9344 r = sym->type.ref->r;
9277 type.ref->r == FUNC_CDECL)9345 if (FUNC_CALL(r) != FUNC_CDECL
9278 type.ref->r = sym->type.ref->r;9346 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9347 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9348 if (FUNC_EXPORT(r))
9349 FUNC_EXPORT(type.ref->r) = 1;
9350
9279 if (!is_compatible_types(&sym->type, &type)) {9351 if (!is_compatible_types(&sym->type, &type)) {
9280 func_error1:9352 func_error1:
9281 error("incompatible types for redefinition of '%s'", 9353 error("incompatible types for redefinition of '%s'",
@@ -9317,7 +9389,7 @@ static void decl(int l) · 35 unmodified lines
9317 }9389 }
9318 tok_str_add(&func_str, -1);9390 tok_str_add(&func_str, -1);
9319 tok_str_add(&func_str, 0);9391 tok_str_add(&func_str, 0);
9320 sym->r = (int)func_str.str;9392 INLINE_DEF(sym->r) = func_str.str;
9321 } else {9393 } else {
9322 /* compute text section */9394 /* compute text section */
9323 cur_text_section = ad.section;9395 cur_text_section = ad.section;
@@ -9325,11 +9397,6 @@ static void decl(int l) · 1 unmodified line
9325 cur_text_section = text_section;9397 cur_text_section = text_section;
9326 sym->r = VT_SYM | VT_CONST;9398 sym->r = VT_SYM | VT_CONST;
9327 gen_function(sym);9399 gen_function(sym);
9328#ifdef TCC_TARGET_PE
9329 if (ad.dllexport) {
9330 ((Elf32_Sym *)symtab_section->data)[sym->c].st_other |= 1;
9331 }
9332#endif
9333 }9400 }
9334 break;9401 break;
9335 } else {9402 } else {
@@ -9341,8 +9408,8 @@ static void decl(int l) · 5 unmodified lines
9341 } else if ((type.t & VT_BTYPE) == VT_FUNC) {9408 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9342 /* external function definition */9409 /* external function definition */
9343 /* specific case for func_call attribute */9410 /* specific case for func_call attribute */
9344 if (ad.func_call)9411 if (ad.func_attr)
9345 type.ref->r = ad.func_call;9412 type.ref->r = ad.func_attr;
9346 external_sym(v, &type, 0);9413 external_sym(v, &type, 0);
9347 } else {9414 } else {
9348 /* not lvalue if array */9415 /* not lvalue if array */
@@ -9891,9 +9958,9 @@ int tcc_relocate(TCCState *s1) · 542 unmodified lines
9891 relocate_common_syms();9958 relocate_common_syms();
9892 9959
9893 tcc_add_linker_symbols(s1);9960 tcc_add_linker_symbols(s1);
9894 9961#ifndef TCC_TARGET_PE
9895 build_got_entries(s1);9962 build_got_entries(s1);
9896 9963#endif
9897 /* compute relocation address : section are relocated in place. We9964 /* compute relocation address : section are relocated in place. We
9898 also alloc the bss space */9965 also alloc the bss space */
9899 for(i = 1; i < s1->nb_sections; i++) {9966 for(i = 1; i < s1->nb_sections; i++) {
@@ -10118,27 +10185,15 @@ void tcc_delete(TCCState *s1) · 218 unmodified lines
10118 tcc_free(s1->sections);10185 tcc_free(s1->sections);
10119 10186
10120 /* free loaded dlls array */10187 /* free loaded dlls array */
10121 for(i = 0; i < s1->nb_loaded_dlls; i++)10188 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10122 tcc_free(s1->loaded_dlls[i]);
10123 tcc_free(s1->loaded_dlls);
10124
10125 /* library paths */
10126 for(i = 0; i < s1->nb_library_paths; i++)
10127 tcc_free(s1->library_paths[i]);
10128 tcc_free(s1->library_paths);
10129 10189
10130 /* cached includes */10190 /* free library paths */
10131 for(i = 0; i < s1->nb_cached_includes; i++)10191 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10132 tcc_free(s1->cached_includes[i]);
10133 tcc_free(s1->cached_includes);
10134 10192
10135 for(i = 0; i < s1->nb_include_paths; i++)10193 /* free include paths */
10136 tcc_free(s1->include_paths[i]);10194 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10137 tcc_free(s1->include_paths);10195 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10138 10196 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10139 for(i = 0; i < s1->nb_sysinclude_paths; i++)
10140 tcc_free(s1->sysinclude_paths[i]);
10141 tcc_free(s1->sysinclude_paths);
10142 10197
10143 tcc_free(s1);10198 tcc_free(s1);
10144}10199}
@@ -10163,19 +10218,14 @@ int tcc_add_sysinclude_path(TCCState *s1, const char *pathname) · 18 unmodified lines
10163 10218
10164static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)10219static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10165{10220{
10166 const char *ext, *filename1;10221 const char *ext;
10167 Elf32_Ehdr ehdr;10222 Elf32_Ehdr ehdr;
10168 int fd, ret;10223 int fd, ret;
10169 BufferedFile *saved_file;10224 BufferedFile *saved_file;
10170 10225
10171 /* find source file type with extension */10226 /* find source file type with extension */
10172 filename1 = strrchr(filename, '/');10227 ext = tcc_fileextension(filename);
10173 if (filename1)10228 if (ext[0])
10174 filename1++;
10175 else
10176 filename1 = filename;
10177 ext = strrchr(filename1, '.');
10178 if (ext)
10179 ext++;10229 ext++;
10180 10230
10181 /* open the file */10231 /* open the file */
@@ -10191,7 +10241,7 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 9 unmodified lines
10191 10241
10192 if (flags & AFF_PREPROCESS) {10242 if (flags & AFF_PREPROCESS) {
10193 ret = tcc_preprocess(s1);10243 ret = tcc_preprocess(s1);
10194 } else if (!ext || !strcmp(ext, "c")) {10244 } else if (!ext[0] || !strcmp(ext, "c")) {
10195 /* C file assumed */10245 /* C file assumed */
10196 ret = tcc_compile(s1);10246 ret = tcc_compile(s1);
10197 } else 10247 } else
@@ -10206,7 +10256,7 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 8 unmodified lines
10206#endif10256#endif
10207#ifdef TCC_TARGET_PE10257#ifdef TCC_TARGET_PE
10208 if (!strcmp(ext, "def")) {10258 if (!strcmp(ext, "def")) {
10209 ret = pe_load_def_file(s1, fdopen(file->fd, "rb"));10259 ret = pe_load_def_file(s1, file->fd);
10210 } else10260 } else
10211#endif10261#endif
10212 {10262 {
@@ -10256,6 +10306,11 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 43 unmodified lines
10256 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {10306 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10257 ret = tcc_load_coff(s1, fd);10307 ret = tcc_load_coff(s1, fd);
10258 } else10308 } else
10309#endif
10310#ifdef TCC_TARGET_PE
10311 if (pe_test_res_file(&ehdr, ret)) {
10312 ret = pe_load_res_file(s1, fd);
10313 } else
10259#endif10314#endif
10260 {10315 {
10261 /* as GNU ld, consider it is an ld script if not recognized */10316 /* as GNU ld, consider it is an ld script if not recognized */
@@ -10478,20 +10533,6 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value) · 216 unmodified lines
10478 flag_name, value);10533 flag_name, value);
10479}10534}
10480 10535
10481/* extract the basename of a file */
10482static char *tcc_basename(const char *name)
10483{
10484 char *p = strchr(name, 0);
10485 while (p > name
10486 && p[-1] != '/'
10487#ifdef _WIN32
10488 && p[-1] != '\\'
10489#endif
10490 )
10491 --p;
10492 return p;
10493}
10494
10495#if !defined(LIBTCC)10536#if !defined(LIBTCC)
10496 10537
10497static int64_t getclock_us(void)10538static int64_t getclock_us(void)
@@ -10535,7 +10576,7 @@ void help(void) · 37 unmodified lines
10535 " -shared generate a shared library\n"10576 " -shared generate a shared library\n"
10536 " -static static linking\n"10577 " -static static linking\n"
10537 " -rdynamic export all global symbols to dynamic linker\n"10578 " -rdynamic export all global symbols to dynamic linker\n"
10538 " -r relocatable output\n"10579 " -r generate (relocatable) object file\n"
10539 "Debugger options:\n"10580 "Debugger options:\n"
10540 " -g generate runtime debug info\n"10581 " -g generate runtime debug info\n"
10541#ifdef CONFIG_TCC_BCHECK10582#ifdef CONFIG_TCC_BCHECK
@@ -10885,6 +10926,7 @@ int main(int argc, char **argv) · 343 unmodified lines
10885 nb_libraries = 0;10926 nb_libraries = 0;
10886 reloc_output = 0;10927 reloc_output = 0;
10887 print_search_dirs = 0;10928 print_search_dirs = 0;
10929 ret = 0;
10888 10930
10889 optind = parse_args(s, argc - 1, argv + 1) + 1;10931 optind = parse_args(s, argc - 1, argv + 1) + 1;
10890 10932
@@ -10915,30 +10957,28 @@ int main(int argc, char **argv) · 24 unmodified lines
10915 if (!outfile) {10957 if (!outfile) {
10916 s->outfile = stdout;10958 s->outfile = stdout;
10917 } else {10959 } else {
10918 s->outfile = fopen(outfile, "wb");10960 s->outfile = fopen(outfile, "w");
10919 if (!s->outfile)10961 if (!s->outfile)
10920 error("could not open '%s", outfile);10962 error("could not open '%s", outfile);
10921 }10963 }
10922 } else if (output_type != TCC_OUTPUT_MEMORY) {10964 } else if (output_type != TCC_OUTPUT_MEMORY) {
10923 if (!outfile) {10965 if (!outfile) {
10924 /* compute default outfile name */10966 /* compute default outfile name */
10925 pstrcpy(objfilename, sizeof(objfilename) - 1, 10967 char *ext;
10926 /* strip path */10968 pstrcpy(objfilename, sizeof(objfilename), tcc_basename(files[0]));
10927 tcc_basename(files[0]));10969 ext = tcc_fileextension(objfilename);
10928#ifdef TCC_TARGET_PE10970#ifdef TCC_TARGET_PE
10929 pe_guess_outfile(objfilename, output_type);10971 if (output_type == TCC_OUTPUT_DLL)
10930#else10972 strcpy(ext, ".dll");
10931 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {10973 else
10932 char *ext = strrchr(objfilename, '.');10974 if (output_type == TCC_OUTPUT_EXE)
10933 if (!ext)10975 strcpy(ext, ".exe");
10934 goto default_outfile;10976 else
10935 /* add .o extension */
10936 strcpy(ext + 1, "o");
10937 } else {
10938 default_outfile:
10939 pstrcpy(objfilename, sizeof(objfilename), "a.out");
10940 }
10941#endif10977#endif
10978 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
10979 strcpy(ext, ".o");
10980 else
10981 pstrcpy(objfilename, sizeof(objfilename), "a.out");
10942 outfile = objfilename;10982 outfile = objfilename;
10943 }10983 }
10944 }10984 }
@@ -10950,29 +10990,29 @@ int main(int argc, char **argv) · 5 unmodified lines
10950 tcc_set_output_type(s, output_type);10990 tcc_set_output_type(s, output_type);
10951 10991
10952 /* compile or add each files or library */10992 /* compile or add each files or library */
10953 for(i = 0;i < nb_files; i++) {10993 for(i = 0; i < nb_files && ret == 0; i++) {
10954 const char *filename;10994 const char *filename;
10955 10995
10956 filename = files[i];10996 filename = files[i];
10957 if (output_type == TCC_OUTPUT_PREPROCESS) {10997 if (output_type == TCC_OUTPUT_PREPROCESS) {
10958 tcc_add_file_internal(s, filename, 10998 if (tcc_add_file_internal(s, filename,
10959 AFF_PRINT_ERROR | AFF_PREPROCESS);10999 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11000 ret = 1;
11001 } else if (filename[0] == '-') {
11002 if (tcc_add_library(s, filename + 2) < 0)
11003 error("cannot find %s", filename);
10960 } else {11004 } else {
10961 if (filename[0] == '-') {11005 if (tcc_add_file(s, filename) < 0)
10962 if (tcc_add_library(s, filename + 2) < 0)11006 ret = 1;
10963 error("cannot find %s", filename);
10964 } else {
10965 if (tcc_add_file(s, filename) < 0) {
10966 ret = 1;
10967 goto the_end;
10968 }
10969 }
10970 }11007 }
10971 }11008 }
10972 11009
10973 /* free all files */11010 /* free all files */
10974 tcc_free(files);11011 tcc_free(files);
10975 11012
11013 if (ret)
11014 goto the_end;
11015
10976 if (do_bench) {11016 if (do_bench) {
10977 double total_time;11017 double total_time;
10978 total_time = (double)(getclock_us() - start_time) / 1000000.0;11018 total_time = (double)(getclock_us() - start_time) / 1000000.0;
@@ -10989,13 +11029,12 @@ int main(int argc, char **argv) · 10 unmodified lines
10989 if (s->output_type == TCC_OUTPUT_PREPROCESS) {11029 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
10990 if (outfile)11030 if (outfile)
10991 fclose(s->outfile);11031 fclose(s->outfile);
10992 ret = 0;
10993 } else if (s->output_type == TCC_OUTPUT_MEMORY) {11032 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
10994 ret = tcc_run(s, argc - optind, argv + optind);11033 ret = tcc_run(s, argc - optind, argv + optind);
10995 } else11034 } else
10996#ifdef TCC_TARGET_PE11035#ifdef TCC_TARGET_PE
10997 if (s->output_type != TCC_OUTPUT_OBJ) {11036 if (s->output_type != TCC_OUTPUT_OBJ) {
10998 ret = tcc_output_pe(s, outfile);11037 ret = pe_output_file(s, outfile);
10999 } else11038 } else
11000#endif11039#endif
11001 {11040 {
tccelf.c+79−82
@@ -531,11 +531,11 @@ static void relocate_section(TCCState *s1, Section *s) · 530 unmodified lines
531 *(int *)ptr += s1->got_offsets[sym_index];531 *(int *)ptr += s1->got_offsets[sym_index];
532 break;532 break;
533#elif defined(TCC_TARGET_ARM)533#elif defined(TCC_TARGET_ARM)
534 case R_ARM_PC24:534 case R_ARM_PC24:
535 case R_ARM_CALL:535 case R_ARM_CALL:
536 case R_ARM_JUMP24:536 case R_ARM_JUMP24:
537 case R_ARM_PLT32:537 case R_ARM_PLT32:
538 {538 {
539 int x;539 int x;
540 x = (*(int *)ptr)&0xffffff;540 x = (*(int *)ptr)&0xffffff;
541 (*(int *)ptr) &= 0xff000000;541 (*(int *)ptr) &= 0xff000000;
@@ -548,42 +548,42 @@ static void relocate_section(TCCState *s1, Section *s) · 6 unmodified lines
548 x >>= 2;548 x >>= 2;
549 x &= 0xffffff;549 x &= 0xffffff;
550 (*(int *)ptr) |= x;550 (*(int *)ptr) |= x;
551 }551 }
552 break;552 break;
553 case R_ARM_PREL31:553 case R_ARM_PREL31:
554 {554 {
555 int x;555 int x;
556 x = (*(int *)ptr) & 0x7fffffff;556 x = (*(int *)ptr) & 0x7fffffff;
557 (*(int *)ptr) &= 0x80000000;557 (*(int *)ptr) &= 0x80000000;
558 x = (x * 2) / 2;558 x = (x * 2) / 2;
559 x += val - addr;559 x += val - addr;
560 if((x^(x>>1))&0x40000000)560 if((x^(x>>1))&0x40000000)
561 error("can't relocate value at %x",addr);561 error("can't relocate value at %x",addr);
562 (*(int *)ptr) |= x & 0x7fffffff;562 (*(int *)ptr) |= x & 0x7fffffff;
563 }563 }
564 case R_ARM_ABS32:564 case R_ARM_ABS32:
565 *(int *)ptr += val;565 *(int *)ptr += val;
566 break;566 break;
567 case R_ARM_BASE_PREL:567 case R_ARM_BASE_PREL:
568 *(int *)ptr += s1->got->sh_addr - addr;568 *(int *)ptr += s1->got->sh_addr - addr;
569 break;569 break;
570 case R_ARM_GOTOFF32:570 case R_ARM_GOTOFF32:
571 *(int *)ptr += val - s1->got->sh_addr;571 *(int *)ptr += val - s1->got->sh_addr;
572 break;572 break;
573 case R_ARM_GOT_BREL:573 case R_ARM_GOT_BREL:
574 /* we load the got offset */574 /* we load the got offset */
575 *(int *)ptr += s1->got_offsets[sym_index];575 *(int *)ptr += s1->got_offsets[sym_index];
576 break;576 break;
577 case R_ARM_COPY:577 case R_ARM_COPY:
578 break;578 break;
579 default:579 default:
580 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",580 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
581 type,addr,(unsigned int )ptr,val);581 type,addr,(unsigned int )ptr,val);
582 break;582 break;
583#elif defined(TCC_TARGET_C67)583#elif defined(TCC_TARGET_C67)
584 case R_C60_32:584 case R_C60_32:
585 *(int *)ptr += val;585 *(int *)ptr += val;
586 break;586 break;
587 case R_C60LO16:587 case R_C60LO16:
588 {588 {
589 uint32_t orig;589 uint32_t orig;
@@ -603,7 +603,7 @@ static void relocate_section(TCCState *s1, Section *s) · 13 unmodified lines
603 case R_C60HI16:603 case R_C60HI16:
604 break;604 break;
605 default:605 default:
606 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",606 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
607 type,addr,(unsigned int )ptr,val);607 type,addr,(unsigned int )ptr,val);
608 break;608 break;
609#else609#else
@@ -784,7 +784,7 @@ static void put_got_entry(TCCState *s1, · 174 unmodified lines
784 offset = plt->data_offset - 16;784 offset = plt->data_offset - 16;
785 }785 }
786#elif defined(TCC_TARGET_ARM)786#elif defined(TCC_TARGET_ARM)
787 if (reloc_type == R_ARM_JUMP_SLOT) {787 if (reloc_type == R_ARM_JUMP_SLOT) {
788 Section *plt;788 Section *plt;
789 uint8_t *p;789 uint8_t *p;
790 790
@@ -797,17 +797,17 @@ static void put_got_entry(TCCState *s1, · 6 unmodified lines
797 if (plt->data_offset == 0) {797 if (plt->data_offset == 0) {
798 /* first plt entry */798 /* first plt entry */
799 p = section_ptr_add(plt, 16);799 p = section_ptr_add(plt, 16);
800 put32(p , 0xe52de004);800 put32(p , 0xe52de004);
801 put32(p + 4, 0xe59fe010);801 put32(p + 4, 0xe59fe010);
802 put32(p + 8, 0xe08fe00e);802 put32(p + 8, 0xe08fe00e);
803 put32(p + 12, 0xe5bef008);803 put32(p + 12, 0xe5bef008);
804 }804 }
805 805
806 p = section_ptr_add(plt, 16);806 p = section_ptr_add(plt, 16);
807 put32(p , 0xe59fc004);807 put32(p , 0xe59fc004);
808 put32(p+4, 0xe08fc00c);808 put32(p+4, 0xe08fc00c);
809 put32(p+8, 0xe59cf000);809 put32(p+8, 0xe59cf000);
810 put32(p+12, s1->got->data_offset);810 put32(p+12, s1->got->data_offset);
811 811
812 /* the symbol is modified so that it will be relocated to812 /* the symbol is modified so that it will be relocated to
813 the PLT */813 the PLT */
@@ -872,7 +872,7 @@ static void build_got_entries(TCCState *s1) · 58 unmodified lines
872 }872 }
873 break;873 break;
874#elif defined(TCC_TARGET_ARM)874#elif defined(TCC_TARGET_ARM)
875 case R_ARM_GOT_BREL:875 case R_ARM_GOT_BREL:
876 case R_ARM_GOTOFF32:876 case R_ARM_GOTOFF32:
877 case R_ARM_BASE_PREL:877 case R_ARM_BASE_PREL:
878 case R_ARM_PLT32:878 case R_ARM_PLT32:
@@ -891,7 +891,7 @@ static void build_got_entries(TCCState *s1) · 12 unmodified lines
891 }891 }
892 break;892 break;
893#elif defined(TCC_TARGET_C67)893#elif defined(TCC_TARGET_C67)
894 case R_C60_GOT32:894 case R_C60_GOT32:
895 case R_C60_GOTOFF:895 case R_C60_GOTOFF:
896 case R_C60_GOTPC:896 case R_C60_GOTPC:
897 case R_C60_PLT32:897 case R_C60_PLT32:
@@ -1547,13 +1547,13 @@ int tcc_output_file(TCCState *s1, const char *filename) · 649 unmodified lines
1547 p += 16;1547 p += 16;
1548 }1548 }
1549#elif defined(TCC_TARGET_ARM)1549#elif defined(TCC_TARGET_ARM)
1550 int x;1550 int x;
1551 x=s1->got->sh_addr - s1->plt->sh_addr - 12;1551 x=s1->got->sh_addr - s1->plt->sh_addr - 12;
1552 p +=16;1552 p +=16;
1553 while (p < p_end) {1553 while (p < p_end) {
1554 put32(p + 12, x + get32(p + 12) + s1->plt->data - p);1554 put32(p + 12, x + get32(p + 12) + s1->plt->data - p);
1555 p += 16;1555 p += 16;
1556 }1556 }
1557#elif defined(TCC_TARGET_C67)1557#elif defined(TCC_TARGET_C67)
1558 /* XXX: TODO */1558 /* XXX: TODO */
1559#else1559#else
@@ -1682,8 +1682,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 122 unmodified lines
1682#endif1682#endif
1683#ifdef TCC_TARGET_ARM1683#ifdef TCC_TARGET_ARM
1684#ifdef TCC_ARM_EABI1684#ifdef TCC_ARM_EABI
1685 ehdr.e_ident[EI_OSABI] = 0;1685 ehdr.e_ident[EI_OSABI] = 0;
1686 ehdr.e_flags = 4 << 24;1686 ehdr.e_flags = 4 << 24;
1687#else1687#else
1688 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;1688 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1689#endif1689#endif
@@ -1859,7 +1859,7 @@ static int tcc_load_object_file(TCCState *s1, · 169 unmodified lines
1859 if (sh->sh_type != SHT_PROGBITS &&1859 if (sh->sh_type != SHT_PROGBITS &&
1860 sh->sh_type != SHT_REL && 1860 sh->sh_type != SHT_REL &&
1861#ifdef TCC_ARM_EABI1861#ifdef TCC_ARM_EABI
1862 sh->sh_type != SHT_ARM_EXIDX &&1862 sh->sh_type != SHT_ARM_EXIDX &&
1863#endif1863#endif
1864 sh->sh_type != SHT_NOBITS)1864 sh->sh_type != SHT_NOBITS)
1865 continue;1865 continue;
@@ -2022,16 +2022,16 @@ static int tcc_load_object_file(TCCState *s1, · 156 unmodified lines
2022 return ret;2022 return ret;
2023}2023}
2024 2024
2025#define ARMAG "!<arch>\012" /* For COFF and a.out archives */2025#define ARMAG "!<arch>\012" /* For COFF and a.out archives */
2026 2026
2027typedef struct ArchiveHeader {2027typedef struct ArchiveHeader {
2028 char ar_name[16]; /* name of this member */2028 char ar_name[16]; /* name of this member */
2029 char ar_date[12]; /* file mtime */2029 char ar_date[12]; /* file mtime */
2030 char ar_uid[6]; /* owner uid; printed as decimal */2030 char ar_uid[6]; /* owner uid; printed as decimal */
2031 char ar_gid[6]; /* owner gid; printed as decimal */2031 char ar_gid[6]; /* owner gid; printed as decimal */
2032 char ar_mode[8]; /* file mode, printed as octal */2032 char ar_mode[8]; /* file mode, printed as octal */
2033 char ar_size[10]; /* file size, printed as decimal */2033 char ar_size[10]; /* file size, printed as decimal */
2034 char ar_fmag[2]; /* should contain ARFMAG */2034 char ar_fmag[2]; /* should contain ARFMAG */
2035} ArchiveHeader;2035} ArchiveHeader;
2036 2036
2037static int get_be32(const uint8_t *b)2037static int get_be32(const uint8_t *b)
@@ -2056,26 +2056,26 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) · 18 unmodified lines
2056 ar_names = ar_index + nsyms * 4;2056 ar_names = ar_index + nsyms * 4;
2057 2057
2058 do {2058 do {
2059 bound = 0;2059 bound = 0;
2060 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {2060 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2061 sym_index = find_elf_sym(symtab_section, p);2061 sym_index = find_elf_sym(symtab_section, p);
2062 if(sym_index) {2062 if(sym_index) {
2063 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];2063 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
2064 if(sym->st_shndx == SHN_UNDEF) {2064 if(sym->st_shndx == SHN_UNDEF) {
2065 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);2065 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
2066#if 02066#if 0
2067 printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);2067 printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
2068#endif2068#endif
2069 ++bound;2069 ++bound;
2070 lseek(fd, off, SEEK_SET);2070 lseek(fd, off, SEEK_SET);
2071 if(tcc_load_object_file(s1, fd, off) < 0) {2071 if(tcc_load_object_file(s1, fd, off) < 0) {
2072 fail:2072 fail:
2073 ret = -1;2073 ret = -1;
2074 goto the_end;2074 goto the_end;
2075 }2075 }
2076 }2076 }
2077 }2077 }
2078 }2078 }
2079 } while(bound);2079 } while(bound);
2080 ret = 0;2080 ret = 0;
2081 the_end:2081 the_end:
@@ -2119,8 +2119,8 @@ static int tcc_load_archive(TCCState *s1, int fd) · 37 unmodified lines
2119 size = (size + 1) & ~1;2119 size = (size + 1) & ~1;
2120 if (!strcmp(ar_name, "/")) {2120 if (!strcmp(ar_name, "/")) {
2121 /* coff symbol table : we handle it */2121 /* coff symbol table : we handle it */
2122 if(s1->alacarte_link)2122 if(s1->alacarte_link)
2123 return tcc_load_alacarte(s1, fd, size);2123 return tcc_load_alacarte(s1, fd, size);
2124 } else if (!strcmp(ar_name, "//") ||2124 } else if (!strcmp(ar_name, "//") ||
2125 !strcmp(ar_name, "__.SYMDEF") ||2125 !strcmp(ar_name, "__.SYMDEF") ||
2126 !strcmp(ar_name, "__.SYMDEF/") ||2126 !strcmp(ar_name, "__.SYMDEF/") ||
@@ -2146,7 +2146,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 19 unmodified lines
2146 Elf32_Sym *sym, *dynsym;2146 Elf32_Sym *sym, *dynsym;
2147 Elf32_Dyn *dt, *dynamic;2147 Elf32_Dyn *dt, *dynamic;
2148 unsigned char *dynstr;2148 unsigned char *dynstr;
2149 const char *name, *soname, *p;2149 const char *name, *soname;
2150 DLLReference *dllref;2150 DLLReference *dllref;
2151 2151
2152 read(fd, &ehdr, sizeof(ehdr));2152 read(fd, &ehdr, sizeof(ehdr));
@@ -2185,10 +2185,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 32 unmodified lines
2185 }2185 }
2186 2186
2187 /* compute the real library name */2187 /* compute the real library name */
2188 soname = filename;2188 soname = tcc_basename(filename);
2189 p = strrchr(soname, '/');
2190 if (p)
2191 soname = p + 1;
2192 2189
2193 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {2190 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2194 if (dt->d_tag == DT_SONAME) {2191 if (dt->d_tag == DT_SONAME) {
tccpe.c+1007−700
@@ -1,7 +1,7 @@
1/*1/*
2 * TCCPE.C - PE file output for the TinyC Compiler2 * TCCPE.C - PE file output for the Tiny C Compiler
3 * 3 *
4 * Copyright (c) 2005 grischka4 * Copyright (c) 2005-2007 grischka
5 *5 *
6 * This library is free software; you can redistribute it and/or6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public7 * modify it under the terms of the GNU Lesser General Public
@@ -18,251 +18,189 @@ · 10 unmodified lines
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */19 */
20 20
21typedef unsigned char BYTE;21#ifdef TCC_TARGET_PE
22typedef unsigned short WORD;
23typedef unsigned long DWORD;
24#define ST static
25 22
26/* XXX: move that to TCC ? */23#define ST_FN static
27int verbose = 0;24#define ST_DATA static
25#define PUB_FN
28 26
29/* definitions below are from winnt.h */27#ifndef _WIN32
28#define stricmp strcasecmp
29#define strnicmp strncasecmp
30#endif
31
32#ifndef MAX_PATH
33#define MAX_PATH 260
34#endif
30 35
31typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */36#define PE_MERGE_DATA
32 WORD e_magic; /* Magic number */37// #define PE_PRINT_SECTIONS
33 WORD e_cblp; /* Bytes on last page of file */
34 WORD e_cp; /* Pages in file */
35 WORD e_crlc; /* Relocations */
36 WORD e_cparhdr; /* Size of header in paragraphs */
37 WORD e_minalloc; /* Minimum extra paragraphs needed */
38 WORD e_maxalloc; /* Maximum extra paragraphs needed */
39 WORD e_ss; /* Initial (relative) SS value */
40 WORD e_sp; /* Initial SP value */
41 WORD e_csum; /* Checksum */
42 WORD e_ip; /* Initial IP value */
43 WORD e_cs; /* Initial (relative) CS value */
44 WORD e_lfarlc; /* File address of relocation table */
45 WORD e_ovno; /* Overlay number */
46 WORD e_res[4]; /* Reserved words */
47 WORD e_oemid; /* OEM identifier (for e_oeminfo) */
48 WORD e_oeminfo; /* OEM information; e_oemid specific */
49 WORD e_res2[10]; /* Reserved words */
50 DWORD e_lfanew; /* File address of new exe header */
51 BYTE e_code[0x40];
52 38
39/* ----------------------------------------------------------- */
40#ifndef IMAGE_NT_SIGNATURE
41/* ----------------------------------------------------------- */
42/* definitions below are from winnt.h */
43
44typedef unsigned char BYTE;
45typedef unsigned short WORD;
46typedef unsigned long DWORD;
47#pragma pack(push, 1)
48
49typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
50 WORD e_magic; /* Magic number */
51 WORD e_cblp; /* Bytes on last page of file */
52 WORD e_cp; /* Pages in file */
53 WORD e_crlc; /* Relocations */
54 WORD e_cparhdr; /* Size of header in paragraphs */
55 WORD e_minalloc; /* Minimum extra paragraphs needed */
56 WORD e_maxalloc; /* Maximum extra paragraphs needed */
57 WORD e_ss; /* Initial (relative) SS value */
58 WORD e_sp; /* Initial SP value */
59 WORD e_csum; /* Checksum */
60 WORD e_ip; /* Initial IP value */
61 WORD e_cs; /* Initial (relative) CS value */
62 WORD e_lfarlc; /* File address of relocation table */
63 WORD e_ovno; /* Overlay number */
64 WORD e_res[4]; /* Reserved words */
65 WORD e_oemid; /* OEM identifier (for e_oeminfo) */
66 WORD e_oeminfo; /* OEM information; e_oemid specific */
67 WORD e_res2[10]; /* Reserved words */
68 DWORD e_lfanew; /* File address of new exe header */
53} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;69} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
54 70
55#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */71#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
56#define SIZE_OF_NT_SIGNATURE 472#define SIZE_OF_NT_SIGNATURE 4
57 73
58typedef struct _IMAGE_FILE_HEADER {74typedef struct _IMAGE_FILE_HEADER {
59 WORD Machine;75 WORD Machine;
60 WORD NumberOfSections;76 WORD NumberOfSections;
61 DWORD TimeDateStamp;77 DWORD TimeDateStamp;
62 DWORD PointerToSymbolTable;78 DWORD PointerToSymbolTable;
63 DWORD NumberOfSymbols;79 DWORD NumberOfSymbols;
64 WORD SizeOfOptionalHeader;80 WORD SizeOfOptionalHeader;
65 WORD Characteristics;81 WORD Characteristics;
66} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;82} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
67 83
68 84
69#define IMAGE_SIZEOF_FILE_HEADER 2085#define IMAGE_SIZEOF_FILE_HEADER 20
70 86
71typedef struct _IMAGE_DATA_DIRECTORY {87typedef struct _IMAGE_DATA_DIRECTORY {
72 DWORD VirtualAddress;88 DWORD VirtualAddress;
73 DWORD Size;89 DWORD Size;
74} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;90} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
75 91
76 92
77typedef struct _IMAGE_OPTIONAL_HEADER {93typedef struct _IMAGE_OPTIONAL_HEADER {
78 /* Standard fields. */94 /* Standard fields. */
79 WORD Magic;95 WORD Magic;
80 BYTE MajorLinkerVersion;96 BYTE MajorLinkerVersion;
81 BYTE MinorLinkerVersion;97 BYTE MinorLinkerVersion;
82 DWORD SizeOfCode;98 DWORD SizeOfCode;
83 DWORD SizeOfInitializedData;99 DWORD SizeOfInitializedData;
84 DWORD SizeOfUninitializedData;100 DWORD SizeOfUninitializedData;
85 DWORD AddressOfEntryPoint;101 DWORD AddressOfEntryPoint;
86 DWORD BaseOfCode;102 DWORD BaseOfCode;
87 DWORD BaseOfData;103 DWORD BaseOfData;
88 104
89 /* NT additional fields. */105 /* NT additional fields. */
90 DWORD ImageBase;106 DWORD ImageBase;
91 DWORD SectionAlignment;107 DWORD SectionAlignment;
92 DWORD FileAlignment;108 DWORD FileAlignment;
93 WORD MajorOperatingSystemVersion;109 WORD MajorOperatingSystemVersion;
94 WORD MinorOperatingSystemVersion;110 WORD MinorOperatingSystemVersion;
95 WORD MajorImageVersion;111 WORD MajorImageVersion;
96 WORD MinorImageVersion;112 WORD MinorImageVersion;
97 WORD MajorSubsystemVersion;113 WORD MajorSubsystemVersion;
98 WORD MinorSubsystemVersion;114 WORD MinorSubsystemVersion;
99 DWORD Win32VersionValue;115 DWORD Win32VersionValue;
100 DWORD SizeOfImage;116 DWORD SizeOfImage;
101 DWORD SizeOfHeaders;117 DWORD SizeOfHeaders;
102 DWORD CheckSum;118 DWORD CheckSum;
103 WORD Subsystem;119 WORD Subsystem;
104 WORD DllCharacteristics;120 WORD DllCharacteristics;
105 DWORD SizeOfStackReserve;121 DWORD SizeOfStackReserve;
106 DWORD SizeOfStackCommit;122 DWORD SizeOfStackCommit;
107 DWORD SizeOfHeapReserve;123 DWORD SizeOfHeapReserve;
108 DWORD SizeOfHeapCommit;124 DWORD SizeOfHeapCommit;
109 DWORD LoaderFlags;125 DWORD LoaderFlags;
110 DWORD NumberOfRvaAndSizes;126 DWORD NumberOfRvaAndSizes;
111 IMAGE_DATA_DIRECTORY DataDirectory[16];127 IMAGE_DATA_DIRECTORY DataDirectory[16];
112 128
113} IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;129} IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;
114 130
115 131
116#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */132#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
117#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */133#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
118#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */134#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
119#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */135#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
120#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */136#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
121#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */137#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
122#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */138#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
123/* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */139/* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */
124#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */140#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */
125#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */141#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */
126#define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */142#define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
127#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */143#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
128#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */144#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
129#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */145#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
130#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */146#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */
131#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */147#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */
132 148
133/* Section header format. */149/* Section header format. */
134#define IMAGE_SIZEOF_SHORT_NAME 8150#define IMAGE_SIZEOF_SHORT_NAME 8
135 151
136typedef struct _IMAGE_SECTION_HEADER {152typedef struct _IMAGE_SECTION_HEADER {
137 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];153 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
138 union {154 union {
139 DWORD PhysicalAddress;155 DWORD PhysicalAddress;
140 DWORD VirtualSize;156 DWORD VirtualSize;
141 } Misc;157 } Misc;
142 DWORD VirtualAddress;158 DWORD VirtualAddress;
143 DWORD SizeOfRawData;159 DWORD SizeOfRawData;
144 DWORD PointerToRawData;160 DWORD PointerToRawData;
145 DWORD PointerToRelocations;161 DWORD PointerToRelocations;
146 DWORD PointerToLinenumbers;162 DWORD PointerToLinenumbers;
147 WORD NumberOfRelocations;163 WORD NumberOfRelocations;
148 WORD NumberOfLinenumbers;164 WORD NumberOfLinenumbers;
149 DWORD Characteristics;165 DWORD Characteristics;
150} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;166} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
151 167
152#define IMAGE_SIZEOF_SECTION_HEADER 40168#define IMAGE_SIZEOF_SECTION_HEADER 40
153 169
154/* ----------------------------------------------------------- */
155typedef struct _IMAGE_BASE_RELOCATION {170typedef struct _IMAGE_BASE_RELOCATION {
156 DWORD VirtualAddress;171 DWORD VirtualAddress;
157 DWORD SizeOfBlock;172 DWORD SizeOfBlock;
158// WORD TypeOffset[1];173// WORD TypeOffset[1];
159} IMAGE_BASE_RELOCATION;174} IMAGE_BASE_RELOCATION;
160 175
161#define IMAGE_SIZEOF_BASE_RELOCATION 8176#define IMAGE_SIZEOF_BASE_RELOCATION 8
162 177
163#define IMAGE_REL_BASED_ABSOLUTE 0178#define IMAGE_REL_BASED_ABSOLUTE 0
164#define IMAGE_REL_BASED_HIGH 1179#define IMAGE_REL_BASED_HIGH 1
165#define IMAGE_REL_BASED_LOW 2180#define IMAGE_REL_BASED_LOW 2
166#define IMAGE_REL_BASED_HIGHLOW 3181#define IMAGE_REL_BASED_HIGHLOW 3
167#define IMAGE_REL_BASED_HIGHADJ 4182#define IMAGE_REL_BASED_HIGHADJ 4
168#define IMAGE_REL_BASED_MIPS_JMPADDR 5183#define IMAGE_REL_BASED_MIPS_JMPADDR 5
169#define IMAGE_REL_BASED_SECTION 6184#define IMAGE_REL_BASED_SECTION 6
170#define IMAGE_REL_BASED_REL32 7185#define IMAGE_REL_BASED_REL32 7
171 186
172/* ----------------------------------------------------------- */187#pragma pack(pop)
173 188
174/* ----------------------------------------------------------- */189/* ----------------------------------------------------------- */
175IMAGE_DOS_HEADER pe_dos_hdr = {190#endif /* ndef IMAGE_NT_SIGNATURE */
176 0x5A4D, /*WORD e_magic; Magic number */191/* ----------------------------------------------------------- */
177 0x0090, /*WORD e_cblp; Bytes on last page of file */
178 0x0003, /*WORD e_cp; Pages in file */
179 0x0000, /*WORD e_crlc; Relocations */
180
181 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
182 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
183 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
184 0x0000, /*WORD e_ss; Initial (relative) SS value */
185
186 0x00B8, /*WORD e_sp; Initial SP value */
187 0x0000, /*WORD e_csum; Checksum */
188 0x0000, /*WORD e_ip; Initial IP value */
189 0x0000, /*WORD e_cs; Initial (relative) CS value */
190 0x0040, /*WORD e_lfarlc; File address of relocation table */
191 0x0000, /*WORD e_ovno; Overlay number */
192 {0, 0, 0, 0}, /*WORD e_res[4]; Reserved words */
193 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
194 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
195 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /*WORD e_res2[10]; Reserved words */
196 0x00000080, /*DWORD e_lfanew; File address of new exe header */
197 { /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
198 /*0040 */ 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8,
199 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68,
200 /*0050 */ 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d,
201 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f,
202 /*0060 */ 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69,
203 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
204 /*0070 */ 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, 0x24, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 /*0080 */
207 }
208};
209
210DWORD pe_magic = IMAGE_NT_SIGNATURE;
211
212IMAGE_FILE_HEADER pe_file_hdr = {
213 0x014C, /*WORD Machine; */
214 0x0003, /*WORD NumberOfSections; */
215 0x00000000, /*DWORD TimeDateStamp; */
216 0x00000000, /*DWORD PointerToSymbolTable; */
217 0x00000000, /*DWORD NumberOfSymbols; */
218 0x00E0, /*WORD SizeOfOptionalHeader; */
219 0x030F /*WORD Characteristics; */
220};
221 192
222IMAGE_OPTIONAL_HEADER32 pe_opt_hdr = {193#pragma pack(push, 1)
223 /* Standard fields. */
224 0x010B, /*WORD Magic; */
225 0x06, /*BYTE MajorLinkerVersion; */
226 0x00, /*BYTE MinorLinkerVersion; */
227 0x00000000, /*DWORD SizeOfCode; */
228 0x00000000, /*DWORD SizeOfInitializedData; */
229 0x00000000, /*DWORD SizeOfUninitializedData; */
230 0x00000000, /*DWORD AddressOfEntryPoint; */
231 0x00000000, /*DWORD BaseOfCode; */
232 0x00000000, /*DWORD BaseOfData; */
233 194
234 /* NT additional fields. */195struct pe_header
235 0x00400000, /*DWORD ImageBase; */196{
236 0x00001000, /*DWORD SectionAlignment; */197 IMAGE_DOS_HEADER doshdr;
237 0x00000200, /*DWORD FileAlignment; */198 BYTE dosstub[0x40];
238 0x0004, /*WORD MajorOperatingSystemVersion; */199 DWORD nt_sig;
239 0x0000, /*WORD MinorOperatingSystemVersion; */200 IMAGE_FILE_HEADER filehdr;
240 0x0000, /*WORD MajorImageVersion; */201 IMAGE_OPTIONAL_HEADER opthdr;
241 0x0000, /*WORD MinorImageVersion; */
242 0x0004, /*WORD MajorSubsystemVersion; */
243 0x0000, /*WORD MinorSubsystemVersion; */
244 0x00000000, /*DWORD Win32VersionValue; */
245 0x00000000, /*DWORD SizeOfImage; */
246 0x00000200, /*DWORD SizeOfHeaders; */
247 0x00000000, /*DWORD CheckSum; */
248 0x0002, /*WORD Subsystem; */
249 0x0000, /*WORD DllCharacteristics; */
250 0x00100000, /*DWORD SizeOfStackReserve; */
251 0x00001000, /*DWORD SizeOfStackCommit; */
252 0x00100000, /*DWORD SizeOfHeapReserve; */
253 0x00001000, /*DWORD SizeOfHeapCommit; */
254 0x00000000, /*DWORD LoaderFlags; */
255 0x00000010, /*DWORD NumberOfRvaAndSizes; */
256
257 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
258 {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
259 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}
260};202};
261 203
262/*----------------------------------------------------------------------------*/
263
264/*----------------------------------------------------------------------------*/
265
266struct pe_import_header {204struct pe_import_header {
267 DWORD first_entry;205 DWORD first_entry;
268 DWORD time_date;206 DWORD time_date;
@@ -289,52 +227,154 @@ struct pe_reloc_header { · 20 unmodified lines
289 DWORD size;227 DWORD size;
290};228};
291 229
230struct pe_rsrc_header {
231 struct _IMAGE_FILE_HEADER filehdr;
232 struct _IMAGE_SECTION_HEADER sectionhdr;
233};
234
235struct pe_rsrc_reloc {
236 DWORD offset;
237 DWORD size;
238 WORD type;
239};
240
241#pragma pack(pop)
242
243/* ----------------------------------------------------------- */
244ST_DATA struct pe_header pe_header = {
245{
246 /* IMAGE_DOS_HEADER doshdr */
247 0x5A4D, /*WORD e_magic; Magic number */
248 0x0090, /*WORD e_cblp; Bytes on last page of file */
249 0x0003, /*WORD e_cp; Pages in file */
250 0x0000, /*WORD e_crlc; Relocations */
251
252 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
253 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
254 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
255 0x0000, /*WORD e_ss; Initial (relative) SS value */
256
257 0x00B8, /*WORD e_sp; Initial SP value */
258 0x0000, /*WORD e_csum; Checksum */
259 0x0000, /*WORD e_ip; Initial IP value */
260 0x0000, /*WORD e_cs; Initial (relative) CS value */
261 0x0040, /*WORD e_lfarlc; File address of relocation table */
262 0x0000, /*WORD e_ovno; Overlay number */
263 {0,0,0,0}, /*WORD e_res[4]; Reserved words */
264 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
265 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
266 {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */
267 0x00000080 /*DWORD e_lfanew; File address of new exe header */
268},{
269 /* BYTE dosstub[0x40] */
270 /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
271 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68,
272 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f,
273 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,
274 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
275},
276 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
277{
278 /* IMAGE_FILE_HEADER filehdr */
279 0x014C, /*WORD Machine; */
280 0x0003, /*WORD NumberOfSections; */
281 0x00000000, /*DWORD TimeDateStamp; */
282 0x00000000, /*DWORD PointerToSymbolTable; */
283 0x00000000, /*DWORD NumberOfSymbols; */
284 0x00E0, /*WORD SizeOfOptionalHeader; */
285 0x030F /*WORD Characteristics; */
286},{
287 /* IMAGE_OPTIONAL_HEADER opthdr */
288 /* Standard fields. */
289 0x010B, /*WORD Magic; */
290 0x06, /*BYTE MajorLinkerVersion; */
291 0x00, /*BYTE MinorLinkerVersion; */
292 0x00000000, /*DWORD SizeOfCode; */
293 0x00000000, /*DWORD SizeOfInitializedData; */
294 0x00000000, /*DWORD SizeOfUninitializedData; */
295 0x00000000, /*DWORD AddressOfEntryPoint; */
296 0x00000000, /*DWORD BaseOfCode; */
297 0x00000000, /*DWORD BaseOfData; */
298
299 /* NT additional fields. */
300 0x00400000, /*DWORD ImageBase; */
301 0x00001000, /*DWORD SectionAlignment; */
302 0x00000200, /*DWORD FileAlignment; */
303 0x0004, /*WORD MajorOperatingSystemVersion; */
304 0x0000, /*WORD MinorOperatingSystemVersion; */
305 0x0000, /*WORD MajorImageVersion; */
306 0x0000, /*WORD MinorImageVersion; */
307 0x0004, /*WORD MajorSubsystemVersion; */
308 0x0000, /*WORD MinorSubsystemVersion; */
309 0x00000000, /*DWORD Win32VersionValue; */
310 0x00000000, /*DWORD SizeOfImage; */
311 0x00000200, /*DWORD SizeOfHeaders; */
312 0x00000000, /*DWORD CheckSum; */
313 0x0002, /*WORD Subsystem; */
314 0x0000, /*WORD DllCharacteristics; */
315 0x00100000, /*DWORD SizeOfStackReserve; */
316 0x00001000, /*DWORD SizeOfStackCommit; */
317 0x00100000, /*DWORD SizeOfHeapReserve; */
318 0x00001000, /*DWORD SizeOfHeapCommit; */
319 0x00000000, /*DWORD LoaderFlags; */
320 0x00000010, /*DWORD NumberOfRvaAndSizes; */
321
322 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
323 {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
324 {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}
325}};
326
292/* ------------------------------------------------------------- */327/* ------------------------------------------------------------- */
293/* internal temporary structures */328/* internal temporary structures */
294 329
295ST const char *pe_sec_names[] = {330/*
296 ".text",331#define IMAGE_SCN_CNT_CODE 0x00000020
297 ".data",332#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
298 ".bss",333#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
299 ".rsrc",334#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
300 ".reloc",335#define IMAGE_SCN_MEM_SHARED 0x10000000
301 ".stab",336#define IMAGE_SCN_MEM_EXECUTE 0x20000000
302 ".stabstr"337#define IMAGE_SCN_MEM_READ 0x40000000
303};338#define IMAGE_SCN_MEM_WRITE 0x80000000
339*/
304 340
305enum {341enum {
306 sec_text = 0,342 sec_text = 0,
307 sec_data,343 sec_data ,
308 sec_bss,344 sec_bss ,
309 sec_rsrc,345 sec_idata ,
310 sec_reloc,346 sec_rsrc ,
311 sec_stab,347 sec_stab ,
312 sec_stabstr,348 sec_reloc ,
313 pe_sec_number349
350 sec_last
314};351};
315 352
316ST DWORD pe_flags[] = {353ST_DATA DWORD pe_sec_flags[] = {
317 0x60000020, /* ".text", */354 0x60000020, /* ".text" , */
318 0xC0000040, /* ".data", */355 0xC0000040, /* ".data" , */
319 0xC0000080, /* ".bss", */356 0xC0000080, /* ".bss" , */
320 0x40000040, /* ".rsrc", */357 0x40000040, /* ".idata" , */
321 0x42000040, /* ".reloc", */358 0x40000040, /* ".rsrc" , */
322 0x42000802, /* ".stab", */359 0x42000802, /* ".stab" , */
323 0x42000802 /* ".stabstr", */360 0x42000040, /* ".reloc" , */
324};361};
325 362
326struct section_info {363struct section_info {
327 struct section_info *next;364 int cls, ord;
328 int id;365 char name[32];
329 DWORD sh_addr;366 DWORD sh_addr;
330 DWORD sh_size;367 DWORD sh_size;
368 DWORD sh_flags;
331 unsigned char *data;369 unsigned char *data;
332 DWORD data_size;370 DWORD data_size;
371 IMAGE_SECTION_HEADER ish;
333};372};
334 373
335struct import_symbol {374struct import_symbol {
336 int sym_index;375 int sym_index;
337 int offset;376 int iat_index;
377 int thk_offset;
338};378};
339 379
340struct pe_import_info {380struct pe_import_info {
@@ -344,7 +384,11 @@ struct pe_import_info { · 3 unmodified lines
344};384};
345 385
346struct pe_info {386struct pe_info {
387 TCCState *s1;
388 Section *reloc;
389 Section *thunk;
347 const char *filename;390 const char *filename;
391 int type;
348 DWORD sizeofheaders;392 DWORD sizeofheaders;
349 DWORD imagebase;393 DWORD imagebase;
350 DWORD start_addr;394 DWORD start_addr;
@@ -354,173 +398,172 @@ struct pe_info { · 3 unmodified lines
354 DWORD iat_size;398 DWORD iat_size;
355 DWORD exp_offs;399 DWORD exp_offs;
356 DWORD exp_size;400 DWORD exp_size;
357 struct section_info sh_info[pe_sec_number];401 struct section_info *sec_info;
358 int sec_count;402 int sec_count;
359 struct pe_import_info **imp_info;403 struct pe_import_info **imp_info;
360 int imp_count;404 int imp_count;
361 Section *reloc;
362 Section *thunk;
363 TCCState *s1;
364};405};
365 406
366/* ------------------------------------------------------------- */407/* ------------------------------------------------------------- */
367#define PE_MERGE_DATA
368// #define PE_PRINT_SECTIONS
369
370#ifndef MAX_PATH
371#define MAX_PATH 260
372#endif
373
374void error_noabort(const char *, ...);
375
376ST char pe_type;
377 408
378#define PE_NUL 0409#define PE_NUL 0
379#define PE_DLL 1410#define PE_DLL 1
380#define PE_GUI 2411#define PE_GUI 2
381#define PE_EXE 3412#define PE_EXE 3
382 413
383ST int pe_find_import(TCCState * s1, const char *symbol, char *ret)414void error_noabort(const char *, ...);
415
416#ifdef _WIN32
417void dbg_printf (const char *fmt, ...)
384{418{
385 int sym_index = find_elf_sym(s1->dynsymtab_section, symbol);419 char buffer[4000];
386 if (0 == sym_index) {420 va_list arg;
387 /* Hm, maybe it's '_symbol' instead of 'symbol' or '__imp__symbol' */421 int x;
388 char buffer[100];422 va_start(arg, fmt);
389 if (0 == memcmp(symbol, "__imp__", 7))423 x = vsprintf (buffer, fmt, arg);
390 symbol += 6;424 strcpy(buffer+x, "\n");
391 else425 OutputDebugString(buffer);
392 buffer[0] = '_', strcpy(buffer + 1, symbol), symbol = buffer;426}
393 sym_index = find_elf_sym(s1->dynsymtab_section, symbol);427#endif
428
429/* --------------------------------------------*/
430
431ST_FN const char* get_alt_symbol(char *buffer, const char *symbol)
432{
433 const char *p;
434 p = strrchr(symbol, '@');
435 if (p && isnum(p[1]) && symbol[0] == '_') { /* stdcall decor */
436 strcpy(buffer, symbol+1)[p-symbol-1] = 0;
437 } else if (symbol[0] != '_') { /* try non-ansi function */
438 buffer[0] = '_', strcpy(buffer + 1, symbol);
439 } else if (0 == memcmp(symbol, "__imp__", 7)) { /* mingw 2.0 */
440 strcpy(buffer, symbol + 6);
441 } else if (0 == memcmp(symbol, "_imp___", 7)) { /* mingw 3.7 */
442 strcpy(buffer, symbol + 6);
443 } else {
444 return symbol;
394 }445 }
395 if (ret)446 return buffer;
396 strcpy(ret, symbol);447}
448
449ST_FN int pe_find_import(TCCState * s1, const char *symbol)
450{
451 char buffer[200];
452 const char *s;
453 int sym_index, n = 0;
454 do {
455 s = n ? get_alt_symbol(buffer, symbol) : symbol;
456 sym_index = find_elf_sym(s1->dynsymtab_section, s);
457 // printf("find %d %s\n", sym_index, s);
458 } while (0 == sym_index && ++n < 2);
397 return sym_index;459 return sym_index;
398}460}
399 461
400#ifdef _WIN32462#if defined _WIN32 || defined __CYGWIN__
401ST void **pe_imp;
402ST int nb_pe_imp;
403 463
464#ifdef __CYGWIN__
465# include <dlfcn.h>
466# define LoadLibrary(s) dlopen(s, RTLD_NOW)
467# define GetProcAddress(h,s) dlsym(h, s)
468#endif
469
470/* for the -run option: dynamically load symbol from dll */
404void *resolve_sym(struct TCCState *s1, const char *symbol, int type)471void *resolve_sym(struct TCCState *s1, const char *symbol, int type)
405{472{
406 char buffer[100], *p = buffer;473 char buffer[100];
407 void *a = NULL;474 int sym_index, dll_index;
408 int sym_index = pe_find_import(s1, symbol, p);475 void *hModule, *addr, **m;
409 int dll_index;476
410 const char *dll_name;477 sym_index = pe_find_import(s1, symbol);
411 void *hm;478 if (0 == sym_index)
412 479 return NULL;
413 if (sym_index) {480 dll_index = ((Elf32_Sym *)s1->dynsymtab_section->data + sym_index)->st_value;
414 dll_index = ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].481 hModule = LoadLibrary(s1->loaded_dlls[dll_index-1]->name);
415 st_other;482 addr = GetProcAddress(hModule, symbol);
416 dll_name = s1->loaded_dlls[dll_index]->name;483 if (NULL == addr)
417 hm = GetModuleHandleA(dll_name);484 addr = GetProcAddress(hModule, get_alt_symbol(buffer, symbol));
418 if (NULL == hm)485
419 hm = LoadLibraryA(dll_name);486 if (addr && STT_OBJECT == type) {
420 if (hm) {487 /* need to return a pointer to the address for data objects */
421 a = GetProcAddress(hm, buffer);488 m = (void**)tcc_malloc(sizeof addr), *m = addr, addr = m;
422 if (a && STT_OBJECT == type) {489#ifdef MEM_DEBUG
423 // need to return a pointer to the address for data objects490 /* yep, we don't free it */
424 dynarray_add(&pe_imp, &nb_pe_imp, a);491 mem_cur_size -= sizeof (void*);
425 a = &pe_imp[nb_pe_imp - 1];492#endif
426 }
427 }
428 }493 }
429 return a;494 return addr;
430}495}
431#endif496#endif
432 497
433#define for_sym_in_symtab(sym) \
434for (sym = (Elf32_Sym *)symtab_section->data + 1; \
435 sym < (Elf32_Sym *)(symtab_section->data + \
436 symtab_section->data_offset); \
437 ++sym)
438
439#define pe_set_datadir(dir,addr,size) \
440 pe_opt_hdr.DataDirectory[dir].VirtualAddress = addr, \
441 pe_opt_hdr.DataDirectory[dir].Size = size
442
443/*----------------------------------------------------------------------------*/498/*----------------------------------------------------------------------------*/
444ST void dynarray_reset(void ***pp, int *n)
445{
446 int i;
447 for (i = 0; i < *n; ++i)
448 tcc_free((*pp)[i]);
449 tcc_free(*pp);
450 *pp = NULL;
451 *n = 0;
452}
453 499
454ST int dynarray_assoc(void **pp, int n, int key)500ST_FN int dynarray_assoc(void **pp, int n, int key)
455{501{
456 int i;502 int i;
457 for (i = 0; i < n; ++i, ++pp)503 for (i = 0; i < n; ++i, ++pp)
458 if (key == **(int **) pp)504 if (key == **(int **) pp)
459 return i;505 return i;
460 return -1;506 return -1;
461}507}
462 508
463#if 0509#if 0
464ST DWORD umin(DWORD a, DWORD b)510ST_FN DWORD umin(DWORD a, DWORD b)
465{511{
466 return a < b ? a : b;512 return a < b ? a : b;
467}513}
468#endif514#endif
469 515
470ST DWORD umax(DWORD a, DWORD b)516ST_FN DWORD umax(DWORD a, DWORD b)
471{517{
472 return a < b ? b : a;518 return a < b ? b : a;
473}519}
474 520
475ST void pe_fpad(FILE * fp, DWORD new_pos)521ST_FN void pe_fpad(FILE *fp, DWORD new_pos)
476{522{
477 DWORD pos = ftell(fp);523 DWORD pos = ftell(fp);
478 while (++pos <= new_pos)524 while (++pos <= new_pos)
479 fputc(0, fp);525 fputc(0, fp);
480}526}
481 527
482ST DWORD pe_file_align(DWORD n)528ST_FN DWORD pe_file_align(DWORD n)
483{529{
484 return (n + (0x200 - 1)) & ~(0x200 - 1);530 return (n + (0x200 - 1)) & ~(0x200 - 1);
485}531}
486 532
487ST DWORD pe_virtual_align(DWORD n)533ST_FN DWORD pe_virtual_align(DWORD n)
488{534{
489 return (n + (0x1000 - 1)) & ~(0x1000 - 1);535 return (n + (0x1000 - 1)) & ~(0x1000 - 1);
490}536}
491 537
492ST void pe_align_section(Section * s, int a)538ST_FN void pe_align_section(Section *s, int a)
493{539{
494 int i = s->data_offset & (a - 1);540 int i = s->data_offset & (a-1);
495 if (i)541 if (i)
496 section_ptr_add(s, a - i);542 section_ptr_add(s, a - i);
497}543}
498 544
545ST_FN void pe_set_datadir(int dir, DWORD addr, DWORD size)
546{
547 pe_header.opthdr.DataDirectory[dir].VirtualAddress = addr;
548 pe_header.opthdr.DataDirectory[dir].Size = size;
549}
499 550
500/*----------------------------------------------------------------------------*/551/*----------------------------------------------------------------------------*/
501ST int pe_write_pe(struct pe_info *pe)552ST_FN int pe_write(struct pe_info *pe)
502{553{
503 int i;554 int i;
504 FILE *op;555 FILE *op;
505 DWORD file_offset;556 DWORD file_offset, r;
506 IMAGE_SECTION_HEADER ish[pe_sec_number], *psh;
507 int sec_index = 0;
508 557
509 op = fopen(pe->filename, "wb");558 op = fopen(pe->filename, "wb");
510 if (NULL == op) {559 if (NULL == op) {
511 error_noabort("could not create file: %s", pe->filename);560 error_noabort("could not write '%s': %s", pe->filename, strerror(errno));
512 return 1;561 return 1;
513 }562 }
514 563
515 memset(&ish, 0, sizeof ish);564 pe->sizeofheaders = pe_file_align(
516 565 sizeof pe_header
517 pe->sizeofheaders = pe_file_align(sizeof pe_dos_hdr566 + pe->sec_count * sizeof (IMAGE_SECTION_HEADER)
518 + sizeof pe_magic
519 + sizeof pe_file_hdr
520 + sizeof pe_opt_hdr
521 +
522 pe->sec_count *
523 sizeof(IMAGE_SECTION_HEADER)
524 );567 );
525 568
526 file_offset = pe->sizeofheaders;569 file_offset = pe->sizeofheaders;
@@ -531,137 +574,132 @@ ST int pe_write_pe(struct pe_info *pe) · 4 unmodified lines
531 "\n virt file size section" "\n");574 "\n virt file size section" "\n");
532 575
533 for (i = 0; i < pe->sec_count; ++i) {576 for (i = 0; i < pe->sec_count; ++i) {
534 struct section_info *si = pe->sh_info + i;577 struct section_info *si = pe->sec_info + i;
535 const char *sh_name = pe_sec_names[si->id];578 const char *sh_name = si->name;
536 unsigned long addr = si->sh_addr - pe->imagebase;579 unsigned long addr = si->sh_addr - pe->imagebase;
537 unsigned long size = si->sh_size;580 unsigned long size = si->sh_size;
581 IMAGE_SECTION_HEADER *psh = &si->ish;
538 582
539 if (2 == verbose)583 if (2 == verbose)
540 printf("%6lx %6lx %6lx %s\n",584 printf("%6lx %6lx %6lx %s\n",
541 addr, file_offset, size, sh_name);585 addr, file_offset, size, sh_name);
542 586
543 switch (si->id) {587 switch (si->cls) {
544 case sec_text:588 case sec_text:
545 pe_opt_hdr.BaseOfCode = addr;589 pe_header.opthdr.BaseOfCode = addr;
546 pe_opt_hdr.AddressOfEntryPoint = addr + pe->start_addr;590 pe_header.opthdr.AddressOfEntryPoint = addr + pe->start_addr;
547 break;591 break;
592
593 case sec_data:
594 pe_header.opthdr.BaseOfData = addr;
595 break;
596
597 case sec_bss:
598 break;
599
600 case sec_reloc:
601 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size);
602 break;
603
604 case sec_rsrc:
605 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size);
606 break;
607
608 case sec_stab:
609 break;
610 }
548 611
549 case sec_data:612 if (pe->thunk == pe->s1->sections[si->ord]) {
550 pe_opt_hdr.BaseOfData = addr;
551 if (pe->imp_size) {613 if (pe->imp_size) {
552 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IMPORT,614 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IMPORT,
553 pe->imp_offs + addr, pe->imp_size);615 pe->imp_offs + addr, pe->imp_size);
554 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IAT,616 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IAT,
555 pe->iat_offs + addr, pe->iat_size);617 pe->iat_offs + addr, pe->iat_size);
556 }618 }
557 if (pe->exp_size) {619 if (pe->exp_size) {
558 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_EXPORT,620 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_EXPORT,
559 pe->exp_offs + addr, pe->exp_size);621 pe->exp_offs + addr, pe->exp_size);
560 }622 }
561 break;
562
563 case sec_bss:
564 break;
565
566 case sec_reloc:
567 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size);
568 break;
569
570 case sec_rsrc:
571 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size);
572 break;
573
574 case sec_stab:
575 break;
576
577 case sec_stabstr:
578 break;
579 }623 }
580 624
581 psh = &ish[sec_index++];625 strcpy((char*)psh->Name, sh_name);
582 strcpy((char *) psh->Name, sh_name);
583 626
584 psh->Characteristics = pe_flags[si->id];627 psh->Characteristics = pe_sec_flags[si->cls];
585 psh->VirtualAddress = addr;628 psh->VirtualAddress = addr;
586 psh->Misc.VirtualSize = size;629 psh->Misc.VirtualSize = size;
587 pe_opt_hdr.SizeOfImage =630 pe_header.opthdr.SizeOfImage =
588 umax(psh->VirtualAddress + psh->Misc.VirtualSize,631 umax(pe_virtual_align(size + addr), pe_header.opthdr.SizeOfImage);
589 pe_opt_hdr.SizeOfImage);
590 632
591 if (si->data_size) {633 if (si->data_size) {
592 psh->PointerToRawData = file_offset;634 psh->PointerToRawData = r = file_offset;
593 fwrite(si->data, 1, si->data_size, op);635 fwrite(si->data, 1, si->data_size, op);
594 file_offset = pe_file_align(file_offset + si->data_size);636 file_offset = pe_file_align(file_offset + si->data_size);
595 psh->SizeOfRawData = file_offset - psh->PointerToRawData;637 psh->SizeOfRawData = file_offset - r;
596 pe_fpad(op, file_offset);638 pe_fpad(op, file_offset);
597 }639 }
598 }640 }
599 641
600 /*----------------------------------------------------- */642 // pe_header.filehdr.TimeDateStamp = time(NULL);
601 643 pe_header.filehdr.NumberOfSections = pe->sec_count;
602 pe_file_hdr.NumberOfSections = sec_index;644 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
603 pe_opt_hdr.SizeOfHeaders = pe->sizeofheaders;645 pe_header.opthdr.ImageBase = pe->imagebase;
604 pe_opt_hdr.ImageBase = pe->imagebase;646 if (PE_DLL == pe->type)
605 if (PE_DLL == pe_type)647 pe_header.filehdr.Characteristics = 0x230E;
606 pe_file_hdr.Characteristics = 0x230E;648 else if (PE_GUI != pe->type)
607 else if (PE_GUI != pe_type)649 pe_header.opthdr.Subsystem = 3;
608 pe_opt_hdr.Subsystem = 3;
609 650
610 fseek(op, SEEK_SET, 0);651 fseek(op, SEEK_SET, 0);
611 fwrite(&pe_dos_hdr, 1, sizeof pe_dos_hdr, op);652 fwrite(&pe_header, 1, sizeof pe_header, op);
612 fwrite(&pe_magic, 1, sizeof pe_magic, op);653 for (i = 0; i < pe->sec_count; ++i)
613 fwrite(&pe_file_hdr, 1, sizeof pe_file_hdr, op);654 fwrite(&pe->sec_info[i].ish, 1, sizeof(IMAGE_SECTION_HEADER), op);
614 fwrite(&pe_opt_hdr, 1, sizeof pe_opt_hdr, op);655 fclose (op);
615 for (i = 0; i < sec_index; ++i)
616 fwrite(&ish[i], 1, sizeof(IMAGE_SECTION_HEADER), op);
617 fclose(op);
618 656
619 if (2 == verbose)657 if (2 == verbose)
620 printf("-------------------------------\n");658 printf("-------------------------------\n");
621 if (verbose)659 if (verbose)
622 printf("<-- %s (%lu bytes)\n", pe->filename, file_offset);660 printf("<- %s (%lu bytes)\n", pe->filename, file_offset);
623 661
624 return 0;662 return 0;
625}663}
626 664
627/*----------------------------------------------------------------------------*/665/*----------------------------------------------------------------------------*/
628ST int pe_add_import(struct pe_info *pe, int sym_index, DWORD offset)666
667ST_FN struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)
629{668{
630 int i;669 int i;
631 int dll_index;670 int dll_index;
632 struct pe_import_info *p;671 struct pe_import_info *p;
633 struct import_symbol *s;672 struct import_symbol *s;
634 673
635 dll_index =674 dll_index = ((Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index)->st_value;
636 ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index].675 if (0 == dll_index)
637 st_other;676 return NULL;
638 i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index);677
678 i = dynarray_assoc ((void**)pe->imp_info, pe->imp_count, dll_index);
639 if (-1 != i) {679 if (-1 != i) {
640 p = pe->imp_info[i];680 p = pe->imp_info[i];
641 goto found_dll;681 goto found_dll;
642 }682 }
643 p = tcc_mallocz(sizeof *p);683 p = tcc_mallocz(sizeof *p);
644 p->dll_index = dll_index;684 p->dll_index = dll_index;
645 dynarray_add((void ***) &pe->imp_info, &pe->imp_count, p);685 dynarray_add((void***)&pe->imp_info, &pe->imp_count, p);
646 686
647 found_dll:687found_dll:
648 i = dynarray_assoc((void **) p->symbols, p->sym_count, sym_index);688 i = dynarray_assoc ((void**)p->symbols, p->sym_count, sym_index);
649 if (-1 != i)689 if (-1 != i)
650 goto found_sym;690 return p->symbols[i];
691
651 s = tcc_mallocz(sizeof *s);692 s = tcc_mallocz(sizeof *s);
693 dynarray_add((void***)&p->symbols, &p->sym_count, s);
652 s->sym_index = sym_index;694 s->sym_index = sym_index;
653 s->offset = offset;695 return s;
654 dynarray_add((void ***) &p->symbols, &p->sym_count, s);
655
656 found_sym:
657 return 1;
658}696}
659 697
660/*----------------------------------------------------------------------------*/698/*----------------------------------------------------------------------------*/
661ST void pe_build_imports(struct pe_info *pe)699ST_FN void pe_build_imports(struct pe_info *pe)
662{700{
663 int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i;701 int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i;
664 DWORD voffset = pe->thunk->sh_addr - pe->imagebase;702 DWORD rva_base = pe->thunk->sh_addr - pe->imagebase;
665 int ndlls = pe->imp_count;703 int ndlls = pe->imp_count;
666 704
667 for (sym_cnt = i = 0; i < ndlls; ++i)705 for (sym_cnt = i = 0; i < ndlls; ++i)
@@ -676,293 +714,447 @@ ST void pe_build_imports(struct pe_info *pe) · 8 unmodified lines
676 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);714 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);
677 pe->iat_offs = dll_ptr + pe->imp_size;715 pe->iat_offs = dll_ptr + pe->imp_size;
678 pe->iat_size = (sym_cnt + ndlls) * sizeof(DWORD);716 pe->iat_size = (sym_cnt + ndlls) * sizeof(DWORD);
679 section_ptr_add(pe->thunk, pe->imp_size + 2 * pe->iat_size);717 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);
680 718
681 thk_ptr = pe->iat_offs;719 thk_ptr = pe->iat_offs;
682 ent_ptr = pe->iat_offs + pe->iat_size;720 ent_ptr = pe->iat_offs + pe->iat_size;
721
683 for (i = 0; i < pe->imp_count; ++i) {722 for (i = 0; i < pe->imp_count; ++i) {
684 struct pe_import_header *hdr;723 struct pe_import_header *hdr;
685 int k, n, v;724 int k, n, v;
686 struct pe_import_info *p = pe->imp_info[i];725 struct pe_import_info *p = pe->imp_info[i];
687 const char *name = pe->s1->loaded_dlls[p->dll_index]->name;726 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;
688 727
689 /* put the dll name into the import header */728 /* put the dll name into the import header */
690 if (0 == strncmp(name, "lib", 3))
691 name += 3;
692 v = put_elf_str(pe->thunk, name);729 v = put_elf_str(pe->thunk, name);
693 730
694 hdr = (struct pe_import_header *) (pe->thunk->data + dll_ptr);731 hdr = (struct pe_import_header*)(pe->thunk->data + dll_ptr);
695 hdr->first_thunk = thk_ptr + voffset;732 hdr->first_thunk = thk_ptr + rva_base;
696 hdr->first_entry = ent_ptr + voffset;733 hdr->first_entry = ent_ptr + rva_base;
697 hdr->lib_name_offset = v + voffset;734 hdr->lib_name_offset = v + rva_base;
698 735
699 for (k = 0, n = p->sym_count; k <= n; ++k) {736 for (k = 0, n = p->sym_count; k <= n; ++k) {
700 if (k < n) {737 if (k < n) {
701 DWORD offset = p->symbols[k]->offset;738 DWORD iat_index = p->symbols[k]->iat_index;
702 int sym_index = p->symbols[k]->sym_index;739 int sym_index = p->symbols[k]->sym_index;
703 Elf32_Sym *sym =740 Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;
704 (Elf32_Sym *) pe->s1->dynsymtab_section->data +741 Elf32_Sym *org_sym = (Elf32_Sym *)symtab_section->data + iat_index;
705 sym_index;742 const char *name = pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
706 const char *name =743
707 pe->s1->dynsymtab_section->link->data + sym->st_name;744 org_sym->st_value = thk_ptr;
708 745 org_sym->st_shndx = pe->thunk->sh_num;
709 if (offset & 0x80000000) { /* ref to data */746 v = pe->thunk->data_offset + rva_base;
710 Elf32_Sym *sym =747 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
711 &((Elf32_Sym *) symtab_section->
712 data)[offset & 0x7FFFFFFF];
713 sym->st_value = thk_ptr;
714 sym->st_shndx = pe->thunk->sh_num;
715 } else { /* ref to function */
716
717 char buffer[100];
718 sprintf(buffer, "IAT.%s", name);
719 sym_index =
720 put_elf_sym(symtab_section, thk_ptr, sizeof(DWORD),
721 ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
722 0, pe->thunk->sh_num, buffer);
723
724 put_elf_reloc(symtab_section, text_section, offset, R_386_32, /*R_JMP_SLOT, */
725 sym_index);
726 }
727 v = pe->thunk->data_offset + voffset;
728 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
729 put_elf_str(pe->thunk, name);748 put_elf_str(pe->thunk, name);
749
730 } else {750 } else {
731 v = 0; // last entry is zero751 v = 0; /* last entry is zero */
732 }752 }
733 *(DWORD *) (pe->thunk->data + thk_ptr) =753 *(DWORD*)(pe->thunk->data+thk_ptr) =
734 *(DWORD *) (pe->thunk->data + ent_ptr) = v;754 *(DWORD*)(pe->thunk->data+ent_ptr) = v;
735 thk_ptr += sizeof(DWORD);755 thk_ptr += sizeof (DWORD);
736 ent_ptr += sizeof(DWORD);756 ent_ptr += sizeof (DWORD);
737 }757 }
738 dll_ptr += sizeof(struct pe_import_header);758 dll_ptr += sizeof(struct pe_import_header);
739 dynarray_reset((void ***) &p->symbols, &p->sym_count);759 dynarray_reset(&p->symbols, &p->sym_count);
740 }760 }
741 dynarray_reset((void ***) &pe->imp_info, &pe->imp_count);761 dynarray_reset(&pe->imp_info, &pe->imp_count);
742}762}
743 763
744/* ------------------------------------------------------------- */764/* ------------------------------------------------------------- */
745ST int sym_cmp(const void *va, const void *vb)765/*
766 For now only functions are exported. Export of data
767 would work, but import requires compiler support to
768 do an additional indirection.
769
770 For instance:
771 __declspec(dllimport) extern int something;
772
773 needs to be translated to:
774
775 *(int*)something
776*/
777
778ST_FN int sym_cmp(const void *va, const void *vb)
746{779{
747 Elf32_Sym *sa = (Elf32_Sym *)symtab_section->data + *(int*)va;780 const char *ca = ((const char **)va)[1];
748 Elf32_Sym *sb = (Elf32_Sym *)symtab_section->data + *(int*)vb;781 const char *cb = ((const char **)vb)[1];
749 const char *ca = symtab_section->link->data + sa->st_name;
750 const char *cb = symtab_section->link->data + sb->st_name;
751 return strcmp(ca, cb);782 return strcmp(ca, cb);
752}783}
753 784
754ST void pe_build_exports(struct pe_info *pe)785ST_FN void pe_build_exports(struct pe_info *pe)
755{786{
756 Elf32_Sym *sym;787 Elf32_Sym *sym;
757 DWORD func_offset, voffset;788 int sym_index, sym_end;
789 DWORD rva_base, func_o, name_o, ord_o, str_o;
758 struct pe_export_header *hdr;790 struct pe_export_header *hdr;
759 int sym_count, n, ord, *sorted;791 int sym_count, n, ord, *sorted, *sp;
760 792
761 voffset = pe->thunk->sh_addr - pe->imagebase;793 FILE *op;
762 sym_count = 0, n = 1, sorted = NULL;794 char buf[MAX_PATH];
795 const char *dllname;
796 const char *name;
763 797
764 // for simplicity only functions are exported798 rva_base = pe->thunk->sh_addr - pe->imagebase;
765 for_sym_in_symtab(sym)799 sym_count = 0, n = 1, sorted = NULL, op = NULL;
766 {800
801 sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);
802 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
803 sym = (Elf32_Sym*)symtab_section->data + sym_index;
804 name = symtab_section->link->data + sym->st_name;
767 if ((sym->st_other & 1)805 if ((sym->st_other & 1)
768 && sym->st_shndx == text_section->sh_num)806 /* export only symbols from actually written sections */
807 && pe->s1->sections[sym->st_shndx]->sh_addr) {
769 dynarray_add((void***)&sorted, &sym_count, (void*)n);808 dynarray_add((void***)&sorted, &sym_count, (void*)n);
809 dynarray_add((void***)&sorted, &sym_count, (void*)name);
810 }
770 ++n;811 ++n;
812#if 0
813 if (sym->st_other & 1)
814 printf("export: %s\n", name);
815 if (sym->st_other & 2)
816 printf("stdcall: %s\n", name);
817#endif
771 }818 }
772 819
773 if (0 == sym_count)820 if (0 == sym_count)
774 return;821 return;
822 sym_count /= 2;
775 823
776 qsort (sorted, sym_count, sizeof sorted[0], sym_cmp);824 qsort (sorted, sym_count, 2 * sizeof sorted[0], sym_cmp);
777 pe_align_section(pe->thunk, 16);825 pe_align_section(pe->thunk, 16);
826 dllname = tcc_basename(pe->filename);
778 827
779 pe->exp_offs = pe->thunk->data_offset;828 pe->exp_offs = pe->thunk->data_offset;
780 hdr = section_ptr_add(pe->thunk,829 func_o = pe->exp_offs + sizeof(struct pe_export_header);
781 sizeof(struct pe_export_header) +830 name_o = func_o + sym_count * sizeof (DWORD);
782 sym_count * (2 * sizeof(DWORD) + sizeof(WORD)));831 ord_o = name_o + sym_count * sizeof (DWORD);
783 832 str_o = ord_o + sym_count * sizeof(WORD);
784 func_offset = pe->exp_offs + sizeof(struct pe_export_header);833
785 834 hdr = section_ptr_add(pe->thunk, str_o - pe->exp_offs);
786 hdr->Characteristics = 0;835 hdr->Characteristics = 0;
787 hdr->Base = 1;836 hdr->Base = 1;
788 hdr->NumberOfFunctions = sym_count;837 hdr->NumberOfFunctions = sym_count;
789 hdr->NumberOfNames = sym_count;838 hdr->NumberOfNames = sym_count;
790 hdr->AddressOfFunctions = func_offset + voffset;839 hdr->AddressOfFunctions = func_o + rva_base;
791 hdr->AddressOfNames = hdr->AddressOfFunctions + sym_count * sizeof(DWORD);840 hdr->AddressOfNames = name_o + rva_base;
792 hdr->AddressOfNameOrdinals = hdr->AddressOfNames + sym_count * sizeof(DWORD);841 hdr->AddressOfNameOrdinals = ord_o + rva_base;
793 hdr->Name = pe->thunk->data_offset + voffset;842 hdr->Name = str_o + rva_base;
794 put_elf_str(pe->thunk, tcc_basename(pe->filename));843 put_elf_str(pe->thunk, dllname);
795 844
796 for (ord = 0; ord < sym_count; ++ord)845#if 1
846 /* automatically write exports to <output-filename>.def */
847 strcpy(buf, pe->filename);
848 strcpy(tcc_fileextension(buf), ".def");
849 op = fopen(buf, "w");
850 if (NULL == op) {
851 error_noabort("could not create '%s': %s", buf, strerror(errno));
852 } else {
853 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", dllname);
854 if (verbose)
855 printf("<- %s (%d symbols)\n", buf, sym_count);
856 }
857#endif
858
859 for (sp = sorted, ord = 0; ord < sym_count; ++ord, sp += 2)
797 {860 {
798 char *name; DWORD *p, *pfunc, *pname; WORD *pord;861 sym_index = sp[0], name = (const char *)sp[1];
799 sym = (Elf32_Sym *)symtab_section->data + sorted[ord];862 /* insert actual address later in pe_relocate_rva */
800 name = symtab_section->link->data + sym->st_name;863 put_elf_reloc(symtab_section, pe->thunk,
801 p = (DWORD*)(pe->thunk->data + func_offset);864 func_o, R_386_RELATIVE, sym_index);
802 pfunc = p + ord;865 *(DWORD*)(pe->thunk->data + name_o)
803 pname = p + sym_count + ord;866 = pe->thunk->data_offset + rva_base;
804 pord = (WORD *)(p + 2*sym_count) + ord;867 *(WORD*)(pe->thunk->data + ord_o)
805 *pfunc = sym->st_value + pe->s1->sections[sym->st_shndx]->sh_addr - pe->imagebase;868 = ord;
806 *pname = pe->thunk->data_offset + voffset;
807 *pord = ord;
808 put_elf_str(pe->thunk, name);869 put_elf_str(pe->thunk, name);
809 /* printf("export: %s\n", name); */870 func_o += sizeof (DWORD);
871 name_o += sizeof (DWORD);
872 ord_o += sizeof (WORD);
873
874 if (op)
875 fprintf(op, "%s\n", name);
810 }876 }
811 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;877 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;
812 tcc_free(sorted);878 tcc_free(sorted);
813}879}
814 880
815/* ------------------------------------------------------------- */881/* ------------------------------------------------------------- */
816ST void pe_build_reloc(struct pe_info *pe, int *section_order,882ST_FN void pe_build_reloc (struct pe_info *pe)
817 int section_count)
818{883{
819 DWORD offset, block_ptr, addr;884 DWORD offset, block_ptr, addr;
820 int count, i;885 int count, i;
821 Elf32_Rel *rel, *rel_end;886 Elf32_Rel *rel, *rel_end;
822 Section *s = NULL, *sr;887 Section *s = NULL, *sr;
888
823 offset = addr = block_ptr = count = i = 0;889 offset = addr = block_ptr = count = i = 0;
824 rel = rel_end = NULL;890 rel = rel_end = NULL;
825 for (;;) {891
892 for(;;) {
826 if (rel < rel_end) {893 if (rel < rel_end) {
827 int type = ELF32_R_TYPE(rel->r_info);894 int type = ELF32_R_TYPE(rel->r_info);
828 addr = rel->r_offset + s->sh_addr;895 addr = rel->r_offset + s->sh_addr;
829 ++rel;896 ++ rel;
830 if (type != R_386_32)897 if (type != R_386_32)
831 continue;898 continue;
832 if (count == 0) { /* new block */899 if (count == 0) { /* new block */
833 block_ptr = pe->reloc->data_offset;900 block_ptr = pe->reloc->data_offset;
834 section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header));901 section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header));
835 offset = addr & 0xFFFFFFFF << 12;902 offset = addr & 0xFFFFFFFF<<12;
836 }903 }
837 if ((addr -= offset) < (1 << 12)) { /* one block spans 4k addresses */904 if ((addr -= offset) < (1<<12)) { /* one block spans 4k addresses */
838 WORD *wp = section_ptr_add(pe->reloc, sizeof(WORD));905 WORD *wp = section_ptr_add(pe->reloc, sizeof (WORD));
839 *wp = addr | IMAGE_REL_BASED_HIGHLOW << 12;906 *wp = addr | IMAGE_REL_BASED_HIGHLOW<<12;
840 ++count;907 ++count;
841 continue;908 continue;
842 }909 }
843 --rel;910 -- rel;
844 } else if (i < section_count) {911
845 sr = (s = pe->s1->sections[section_order[i++]])->reloc;912 } else if (i < pe->sec_count) {
913 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;
846 if (sr) {914 if (sr) {
847 rel = (Elf32_Rel *) sr->data;915 rel = (Elf32_Rel *)sr->data;
848 rel_end = (Elf32_Rel *) (sr->data + sr->data_offset);916 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
849 }917 }
850 continue;918 continue;
851 }919 }
852 920
853 if (count) { /* store the last block and ready for a new one */921 if (count) {
922 /* store the last block and ready for a new one */
854 struct pe_reloc_header *hdr;923 struct pe_reloc_header *hdr;
855 if (count & 1)924 if (count & 1) /* align for DWORDS */
856 section_ptr_add(pe->reloc, 2), ++count;925 section_ptr_add(pe->reloc, sizeof(WORD)), ++count;
857 hdr = (struct pe_reloc_header *) (pe->reloc->data + block_ptr);926 hdr = (struct pe_reloc_header *)(pe->reloc->data + block_ptr);
858 hdr->offset = offset - pe->imagebase;927 hdr -> offset = offset - pe->imagebase;
859 hdr->size =928 hdr -> size = count * sizeof(WORD) + sizeof(struct pe_reloc_header);
860 count * sizeof(WORD) + sizeof(struct pe_reloc_header);
861 count = 0;929 count = 0;
862 }930 }
931
863 if (rel >= rel_end)932 if (rel >= rel_end)
864 break;933 break;
865 }934 }
866}935}
867 936
868/* ------------------------------------------------------------- */937/* ------------------------------------------------------------- */
869ST int pe_assign_addresses(struct pe_info *pe)938ST_FN int pe_section_class(Section *s)
870{939{
871 int i, k, n;940 int type, flags;
941 const char *name;
942
943 type = s->sh_type;
944 flags = s->sh_flags;
945 name = s->name;
946 if (flags & SHF_ALLOC) {
947 if (type == SHT_PROGBITS) {
948 if (flags & SHF_EXECINSTR)
949 return sec_text;
950 if (flags & SHF_WRITE)
951 return sec_data;
952 if (0 == strcmp(name, ".rsrc"))
953 return sec_rsrc;
954 if (0 == strcmp(name, ".iedat"))
955 return sec_idata;
956 } else if (type == SHT_NOBITS) {
957 if (flags & SHF_WRITE)
958 return sec_bss;
959 }
960 } else {
961 if (0 == strcmp(name, ".reloc"))
962 return sec_reloc;
963 if (0 == strncmp(name, ".stab", 5)) /* .stab and .stabstr */
964 return sec_stab;
965 }
966 return -1;
967}
968
969ST_FN int pe_assign_addresses (struct pe_info *pe)
970{
971 int i, k, o, c;
872 DWORD addr;972 DWORD addr;
873 int section_order[pe_sec_number];973 int *section_order;
874 struct section_info *si_data = NULL;974 struct section_info *si;
975 Section *s;
976
977 // pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
978
979 section_order = tcc_malloc(pe->s1->nb_sections * sizeof (int));
980 for (o = k = 0 ; k < sec_last; ++k) {
981 for (i = 1; i < pe->s1->nb_sections; ++i) {
982 s = pe->s1->sections[i];
983 if (k == pe_section_class(s)) {
984 // printf("%s %d\n", s->name, k);
985 s->sh_addr = pe->imagebase;
986 section_order[o++] = i;
987 }
988 }
989 }
875 990
876 pe->imagebase = PE_DLL == pe_type ? 0x10000000 : 0x00400000;991 pe->sec_info = tcc_mallocz(o * sizeof (struct section_info));
877 addr = pe->imagebase + 1;992 addr = pe->imagebase + 1;
878 993
879 if (PE_DLL == pe_type)994 for (i = 0; i < o; ++i)
880 pe->reloc = new_section(pe->s1, ".reloc", SHT_DYNAMIC, SHF_ALLOC);995 {
996 k = section_order[i];
997 s = pe->s1->sections[k];
998 c = pe_section_class(s);
999 si = &pe->sec_info[pe->sec_count];
881 1000
882 for (n = k = 0; n < pe_sec_number; ++n) {
883 for (i = 1; i < pe->s1->nb_sections; ++i) {
884 Section *s = pe->s1->sections[i];
885 if (0 == strcmp(s->name, pe_sec_names[n])) {
886 struct section_info *si = &pe->sh_info[pe->sec_count];
887#ifdef PE_MERGE_DATA1001#ifdef PE_MERGE_DATA
888 if (n == sec_bss && si_data) {1002 if (c == sec_bss && pe->sec_count && si[-1].cls == sec_data) {
889 /* append .bss to .data */1003 /* append .bss to .data */
890 s->sh_addr = addr = ((addr - 1) | 15) + 1;1004 s->sh_addr = addr = ((addr-1) | 15) + 1;
891 addr += s->data_offset;1005 addr += s->data_offset;
892 si_data->sh_size = addr - si_data->sh_addr;1006 si[-1].sh_size = addr - si[-1].sh_addr;
893 } else1007 continue;
1008 }
894#endif1009#endif
895 {1010 strcpy(si->name, s->name);
896 si->sh_addr = s->sh_addr = addr =1011 si->cls = c;
897 pe_virtual_align(addr);1012 si->ord = k;
898 si->id = n;1013 si->sh_addr = s->sh_addr = addr = pe_virtual_align(addr);
899 1014 si->sh_flags = s->sh_flags;
900 if (n == sec_data) {1015
901 pe->thunk = s;1016 if (c == sec_data && NULL == pe->thunk)
902 si_data = si;1017 pe->thunk = s;
903 pe_build_imports(pe);1018
904 pe_build_exports(pe);1019 if (s == pe->thunk) {
905 } else if (n == sec_reloc) {1020 pe_build_imports(pe);
906 pe_build_reloc(pe, section_order, k);1021 pe_build_exports(pe);
907 }1022 }
908 1023
909 if (s->data_offset) {1024 if (c == sec_reloc)
910 if (n != sec_bss) {1025 pe_build_reloc (pe);
911 si->data = s->data;1026
912 si->data_size = s->data_offset;1027 if (s->data_offset)
913 }1028 {
914 1029 if (s->sh_type != SHT_NOBITS) {
915 addr += s->data_offset;1030 si->data = s->data;
916 si->sh_size = s->data_offset;1031 si->data_size = s->data_offset;
917 ++pe->sec_count;
918 }
919 //printf("Section %08X %04X %s\n", si->sh_addr, si->data_size, s->name);
920 }
921 section_order[k] = i, ++k;
922 }1032 }
1033
1034 addr += s->data_offset;
1035 si->sh_size = s->data_offset;
1036 ++pe->sec_count;
923 }1037 }
1038 // printf("%08x %05x %s\n", si->sh_addr, si->sh_size, si->name);
924 }1039 }
1040
1041#if 0
1042 for (i = 1; i < pe->s1->nb_sections; ++i) {
1043 Section *s = pe->s1->sections[i];
1044 int type = s->sh_type;
1045 int flags = s->sh_flags;
1046 printf("section %-16s %-10s %5x %s,%s,%s\n",
1047 s->name,
1048 type == SHT_PROGBITS ? "progbits" :
1049 type == SHT_NOBITS ? "nobits" :
1050 type == SHT_SYMTAB ? "symtab" :
1051 type == SHT_STRTAB ? "strtab" :
1052 type == SHT_REL ? "rel" : "???",
1053 s->data_offset,
1054 flags & SHF_ALLOC ? "alloc" : "",
1055 flags & SHF_WRITE ? "write" : "",
1056 flags & SHF_EXECINSTR ? "exec" : ""
1057 );
1058 }
1059 verbose = 2;
1060#endif
1061
1062 tcc_free(section_order);
925 return 0;1063 return 0;
926}1064}
927 1065
1066/* ------------------------------------------------------------- */
1067ST_FN void pe_relocate_rva (struct pe_info *pe, Section *s)
1068{
1069 Section *sr = s->reloc;
1070 Elf32_Rel *rel, *rel_end;
1071 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
1072 for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++)
1073 if (ELF32_R_TYPE(rel->r_info) == R_386_RELATIVE) {
1074 int sym_index = ELF32_R_SYM(rel->r_info);
1075 DWORD addr = s->sh_addr;
1076 if (sym_index) {
1077 Elf32_Sym *sym = (Elf32_Sym *)symtab_section->data + sym_index;
1078 addr = sym->st_value;
1079 }
1080 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;
1081 }
1082}
1083
928/*----------------------------------------------------------------------------*/1084/*----------------------------------------------------------------------------*/
929ST int pe_check_symbols(struct pe_info *pe)1085ST_FN int pe_check_symbols(struct pe_info *pe)
930{1086{
931 Elf32_Sym *sym;1087 Elf32_Sym *sym;
1088 int sym_index, sym_end;
932 int ret = 0;1089 int ret = 0;
933 1090
934 pe_align_section(text_section, 8);1091 pe_align_section(text_section, 8);
935 1092
936 for_sym_in_symtab(sym) {1093 sym_end = symtab_section->data_offset / sizeof(Elf32_Sym);
1094 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
1095
1096 sym = (Elf32_Sym*)symtab_section->data + sym_index;
937 if (sym->st_shndx == SHN_UNDEF) {1097 if (sym->st_shndx == SHN_UNDEF) {
938 const char *symbol = symtab_section->link->data + sym->st_name;1098
1099 const char *name = symtab_section->link->data + sym->st_name;
939 unsigned type = ELF32_ST_TYPE(sym->st_info);1100 unsigned type = ELF32_ST_TYPE(sym->st_info);
940 int sym_index = pe_find_import(pe->s1, symbol, NULL);1101 int imp_sym = pe_find_import(pe->s1, name);
941 if (sym_index) {1102 struct import_symbol *is;
942 if (type == STT_FUNC) {1103
943 unsigned long offset = text_section->data_offset;1104 if (0 == imp_sym)
944 if (pe_add_import(pe, sym_index, offset + 2)) {1105 goto not_found;
945 /* add the 'jmp IAT[x]' instruction */1106 is = pe_add_import(pe, imp_sym);
946 *(WORD *) section_ptr_add(text_section, 8) =1107 if (!is)
947 0x25FF;1108 goto not_found;
948 /* patch the symbol */1109
949 sym->st_shndx = text_section->sh_num;1110 if (type == STT_FUNC) {
950 sym->st_value = offset;1111 unsigned long offset = is->thk_offset;
951 continue;1112 if (offset) {
952 }1113 /* got aliased symbol, like stricmp and _stricmp */
953 } else if (type == STT_OBJECT) { /* data, ptr to that should be */1114
954 if (pe_add_import(pe, sym_index,1115 } else {
955 (sym -1116 char buffer[100];
956 (Elf32_Sym *) symtab_section->data) | 1117
957 0x80000000))1118 offset = text_section->data_offset;
958 continue;1119 /* add the 'jmp IAT[x]' instruction */
1120 *(WORD*)section_ptr_add(text_section, 8) = 0x25FF;
1121
1122 /* add a helper symbol, will be patched later in
1123 pe_build_imports */
1124 sprintf(buffer, "IAT.%s", name);
1125 is->iat_index = put_elf_sym(
1126 symtab_section, 0, sizeof(DWORD),
1127 ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
1128 0, SHN_UNDEF, buffer);
1129 put_elf_reloc(symtab_section, text_section,
1130 offset + 2, R_386_32, is->iat_index);
1131 is->thk_offset = offset;
959 }1132 }
1133
1134 /* tcc_realloc might have altered sym's address */
1135 sym = (Elf32_Sym*)symtab_section->data + sym_index;
1136
1137 /* patch the original symbol */
1138 sym->st_value = offset;
1139 sym->st_shndx = text_section->sh_num;
1140 sym->st_other &= ~1; /* do not export */
1141 continue;
960 }1142 }
961 error_noabort("undefined symbol '%s'", symbol);1143
1144 if (type == STT_OBJECT) { /* data, ptr to that should be */
1145 if (0 == is->iat_index) {
1146 /* original symbol will be patched later in pe_build_imports */
1147 is->iat_index = sym_index;
1148 continue;
1149 }
1150 }
1151
1152 not_found:
1153 error_noabort("undefined symbol '%s'", name);
962 ret = 1;1154 ret = 1;
963 } else1155
964 if (pe->s1->rdynamic1156 } else if (pe->s1->rdynamic
965 && ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {1157 && ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
966 /* if -rdynamic option, then export all non local symbols */1158 /* if -rdynamic option, then export all non local symbols */
967 sym->st_other |= 1;1159 sym->st_other |= 1;
968 }1160 }
@@ -972,8 +1164,9 @@ ST int pe_check_symbols(struct pe_info *pe) · 3 unmodified lines
972 1164
973/*----------------------------------------------------------------------------*/1165/*----------------------------------------------------------------------------*/
974#ifdef PE_PRINT_SECTIONS1166#ifdef PE_PRINT_SECTIONS
975ST void pe_print_section(FILE * f, Section * s)1167ST_FN void pe_print_section(FILE * f, Section * s)
976{ /* just if you'r curious */1168{
1169 /* just if you'r curious */
977 BYTE *p, *e, b;1170 BYTE *p, *e, b;
978 int i, n, l, m;1171 int i, n, l, m;
979 p = s->data;1172 p = s->data;
@@ -992,6 +1185,9 @@ ST void pe_print_section(FILE * f, Section * s) · 12 unmodified lines
992 if (s->sh_type == SHT_NOBITS)1185 if (s->sh_type == SHT_NOBITS)
993 return;1186 return;
994 1187
1188 if (0 == l)
1189 return;
1190
995 if (s->sh_type == SHT_SYMTAB)1191 if (s->sh_type == SHT_SYMTAB)
996 m = sizeof(Elf32_Sym);1192 m = sizeof(Elf32_Sym);
997 if (s->sh_type == SHT_REL)1193 if (s->sh_type == SHT_REL)
@@ -999,7 +1195,49 @@ ST void pe_print_section(FILE * f, Section * s) · 1 unmodified line
999 else1195 else
1000 m = 16;1196 m = 16;
1001 1197
1002 for (i = 0; i < l;) {1198 fprintf(f, "%-8s", "offset");
1199 for (i = 0; i < m; ++i)
1200 fprintf(f, " %02x", i);
1201 n = 56;
1202
1203 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_REL) {
1204 const char *fields1[] = {
1205 "name",
1206 "value",
1207 "size",
1208 "bind",
1209 "type",
1210 "other",
1211 "shndx",
1212 NULL
1213 };
1214
1215 const char *fields2[] = {
1216 "offs",
1217 "type",
1218 "symb",
1219 NULL
1220 };
1221
1222 const char **p;
1223
1224 if (s->sh_type == SHT_SYMTAB)
1225 p = fields1, n = 106;
1226 else
1227 p = fields2, n = 58;
1228
1229 for (i = 0; p[i]; ++i)
1230 fprintf(f, "%6s", p[i]);
1231 fprintf(f, " symbol");
1232 }
1233
1234 fprintf(f, "\n");
1235 for (i = 0; i < n; ++i)
1236 fprintf(f, "-");
1237 fprintf(f, "\n");
1238
1239 for (i = 0; i < l;)
1240 {
1003 fprintf(f, "%08X", i);1241 fprintf(f, "%08X", i);
1004 for (n = 0; n < m; ++n) {1242 for (n = 0; n < m; ++n) {
1005 if (n + i < l)1243 if (n + i < l)
@@ -1011,31 +1249,20 @@ ST void pe_print_section(FILE * f, Section * s) · 5 unmodified lines
1011 if (s->sh_type == SHT_SYMTAB) {1249 if (s->sh_type == SHT_SYMTAB) {
1012 Elf32_Sym *sym = (Elf32_Sym *) (p + i);1250 Elf32_Sym *sym = (Elf32_Sym *) (p + i);
1013 const char *name = s->link->data + sym->st_name;1251 const char *name = s->link->data + sym->st_name;
1014 fprintf(f,1252 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
1015 " name:%04X"
1016 " value:%04X"
1017 " size:%04X"
1018 " bind:%02X"
1019 " type:%02X"
1020 " other:%02X"
1021 " shndx:%04X"
1022 " \"%s\"",
1023 sym->st_name,1253 sym->st_name,
1024 sym->st_value,1254 sym->st_value,
1025 sym->st_size,1255 sym->st_size,
1026 ELF32_ST_BIND(sym->st_info),1256 ELF32_ST_BIND(sym->st_info),
1027 ELF32_ST_TYPE(sym->st_info),1257 ELF32_ST_TYPE(sym->st_info),
1028 sym->st_other, sym->st_shndx, name);1258 sym->st_other, sym->st_shndx, name);
1259
1029 } else if (s->sh_type == SHT_REL) {1260 } else if (s->sh_type == SHT_REL) {
1030 Elf32_Rel *rel = (Elf32_Rel *) (p + i);1261 Elf32_Rel *rel = (Elf32_Rel *) (p + i);
1031 Elf32_Sym *sym =1262 Elf32_Sym *sym =
1032 (Elf32_Sym *) s->link->data + ELF32_R_SYM(rel->r_info);1263 (Elf32_Sym *) s->link->data + ELF32_R_SYM(rel->r_info);
1033 const char *name = s->link->link->data + sym->st_name;1264 const char *name = s->link->link->data + sym->st_name;
1034 fprintf(f,1265 fprintf(f, " %04X %02X %04X \"%s\"",
1035 " offset:%04X"
1036 " type:%02X"
1037 " symbol:%04X"
1038 " \"%s\"",
1039 rel->r_offset,1266 rel->r_offset,
1040 ELF32_R_TYPE(rel->r_info),1267 ELF32_R_TYPE(rel->r_info),
1041 ELF32_R_SYM(rel->r_info), name);1268 ELF32_R_SYM(rel->r_info), name);
@@ -1055,190 +1282,270 @@ ST void pe_print_section(FILE * f, Section * s) · 13 unmodified lines
1055 }1282 }
1056 fprintf(f, "\n\n");1283 fprintf(f, "\n\n");
1057}1284}
1285
1286ST_FN void pe_print_sections(TCCState *s1, const char *fname)
1287{
1288 Section *s;
1289 FILE *f;
1290 int i;
1291 f = fopen(fname, "wt");
1292 for (i = 1; i < s1->nb_sections; ++i) {
1293 s = s1->sections[i];
1294 pe_print_section(f, s);
1295 }
1296 pe_print_section(f, s1->dynsymtab_section);
1297 fclose(f);
1298}
1058#endif1299#endif
1059 1300
1060static int pe_test_cmd(const char **pp, const char *cmd)1301/* -------------------------------------------------------------
1302 * This is for compiled windows resources in 'coff' format
1303 * as generated by 'windres.exe -O coff ...'.
1304 */
1305
1306PUB_FN int pe_test_res_file(void *v, int size)
1061{1307{
1062 const char *p;1308 struct pe_rsrc_header *p = (struct pe_rsrc_header *)v;
1063 char *q, buf[16];1309 return
1064 int ret;1310 size >= IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_SHORT_NAME /* = 28 */
1311 && p->filehdr.Machine == 0x014C
1312 && 1 == p->filehdr.NumberOfSections
1313 && 0 == strcmp(p->sectionhdr.Name, ".rsrc")
1314 ;
1315}
1065 1316
1066 p = *pp;1317ST_FN int read_n(int fd, void *ptr, unsigned size)
1067 q = buf;1318{
1068 while (*p != '\0' && !is_space(*p)) {1319 return size == read(fd, ptr, size);
1069 if ((q - buf) < sizeof(buf) - 1)1320}
1070 *q++ = toup(*p);1321
1071 p++;1322PUB_FN int pe_load_res_file(TCCState *s1, int fd)
1323{
1324 struct pe_rsrc_header hdr;
1325 Section *rsrc_section;
1326 int i, ret = -1;
1327 BYTE *ptr;
1328
1329 lseek (fd, 0, SEEK_SET);
1330 if (!read_n(fd, &hdr, sizeof hdr))
1331 goto quit;
1332 if (!pe_test_res_file(&hdr, sizeof hdr))
1333 goto quit;
1334
1335 rsrc_section = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC);
1336 ptr = section_ptr_add(rsrc_section, hdr.sectionhdr.SizeOfRawData);
1337 lseek (fd, hdr.sectionhdr.PointerToRawData, SEEK_SET);
1338 if (!read_n(fd, ptr, hdr.sectionhdr.SizeOfRawData))
1339 goto quit;
1340
1341 lseek (fd, hdr.sectionhdr.PointerToRelocations, SEEK_SET);
1342 for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i)
1343 {
1344 struct pe_rsrc_reloc rel;
1345 if (!read_n(fd, &rel, sizeof rel))
1346 goto quit;
1347 // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type);
1348 if (rel.type != 7) /* DIR32NB */
1349 goto quit;
1350 put_elf_reloc(symtab_section, rsrc_section,
1351 rel.offset, R_386_RELATIVE, 0);
1072 }1352 }
1073 *q = '\0';1353 ret = 0;
1074 ret = !strcmp(buf, cmd);1354quit:
1075 *pp = p;1355 if (ret)
1356 error_noabort("unrecognized resource file format");
1076 return ret;1357 return ret;
1077}1358}
1078 1359
1079/* ------------------------------------------------------------- */1360/* ------------------------------------------------------------- */
1080int pe_load_def_file(TCCState * s1, FILE * fp)1361ST_FN char *trimfront(char *p)
1081{1362{
1082 DLLReference *dllref;1363 while (*p && (unsigned char)*p <= ' ')
1083 int f = 0, sym_index;1364 ++p;
1084 char *p, line[120], dllname[40];1365 return p;
1085 while (fgets(line, sizeof line, fp)) {1366}
1086 p = strchr(line, 0);
1087 while (p > line && p[-1] <= ' ')
1088 --p;
1089 *p = 0;
1090 p = line;
1091 while (*p && *p <= ' ')
1092 ++p;
1093
1094 if (*p && ';' != *p)
1095 switch (f) {
1096 case 0:
1097 if (!pe_test_cmd((const char **)&p, "LIBRARY"))
1098 return -1;
1099 while (is_space(*p))
1100 p++;
1101 pstrcpy(dllname, sizeof(dllname), p);
1102 ++f;
1103 continue;
1104 1367
1105 case 1:1368ST_FN char *trimback(char *a, char *e)
1106 if (!pe_test_cmd((const char **)&p, "EXPORTS"))1369{
1107 return -1;1370 while (e > a && (unsigned char)e[-1] <= ' ')
1108 ++f;1371 --e;
1109 continue;1372 *e = 0;;
1373 return a;
1374}
1110 1375
1111 case 2:1376ST_FN char *get_line(char *line, int size, FILE *fp)
1112 dllref =1377{
1113 tcc_malloc(sizeof(DLLReference) + strlen(dllname));1378 if (NULL == fgets(line, size, fp))
1114 strcpy(dllref->name, dllname);1379 return NULL;
1115 dllref->level = 0;1380 trimback(line, strchr(line, 0));
1116 dynarray_add((void ***) &s1->loaded_dlls,1381 return trimfront(line);
1117 &s1->nb_loaded_dlls, dllref);
1118 ++f;
1119
1120 default:
1121 /* tccpe needs to know from what dll it should import
1122 the sym */
1123 sym_index = add_elf_sym(s1->dynsymtab_section,
1124 0, 0, ELF32_ST_INFO(STB_GLOBAL,
1125 STT_FUNC),
1126 s1->nb_loaded_dlls - 1,
1127 text_section->sh_num, p);
1128 continue;
1129 }
1130 }
1131 return 0;
1132}1382}
1133 1383
1134/* ------------------------------------------------------------- */1384/* ------------------------------------------------------------- */
1135void pe_guess_outfile(char *objfilename, int output_type)1385PUB_FN int pe_load_def_file(TCCState *s1, int fd)
1136{1386{
1137 char *ext = strrchr(objfilename, '.');1387 DLLReference *dllref;
1138 if (NULL == ext)1388 int state = 0, ret = -1;
1139 ext = strchr(objfilename, 0);1389 char line[400], dllname[80], *p;
1140 if (output_type == TCC_OUTPUT_DLL)1390 FILE *fp = fdopen(dup(fd), "rb");
1141 strcpy(ext, ".dll");1391
1142 else1392 if (NULL == fp)
1143 if (output_type == TCC_OUTPUT_EXE)1393 goto quit;
1144 strcpy(ext, ".exe");1394
1145 else1395 for (;;) {
1146 if (output_type == TCC_OUTPUT_OBJ && strcmp(ext, ".o"))1396 p = get_line(line, sizeof line, fp);
1147 strcpy(ext, ".o");1397 if (NULL == p)
1148 else1398 break;
1149 error("no outputfile given");1399 if (0 == *p || ';' == *p)
1400 continue;
1401 switch (state) {
1402 case 0:
1403 if (0 != strnicmp(p, "LIBRARY", 7))
1404 goto quit;
1405 strcpy(dllname, trimfront(p+7));
1406 ++state;
1407 continue;
1408
1409 case 1:
1410 if (0 != stricmp(p, "EXPORTS"))
1411 goto quit;
1412 ++state;
1413 continue;
1414
1415 case 2:
1416 dllref = tcc_malloc(sizeof(DLLReference) + strlen(dllname));
1417 strcpy(dllref->name, dllname);
1418 dllref->level = 0;
1419 dynarray_add((void ***) &s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1420 ++state;
1421
1422 default:
1423 add_elf_sym(s1->dynsymtab_section,
1424 s1->nb_loaded_dlls, 0,
1425 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), 0,
1426 text_section->sh_num, p);
1427 continue;
1428 }
1429 }
1430 ret = 0;
1431quit:
1432 if (fp)
1433 fclose(fp);
1434 if (ret)
1435 error_noabort("unrecognized export definition file format");
1436 return ret;
1150}1437}
1151 1438
1152/* ------------------------------------------------------------- */1439/* ------------------------------------------------------------- */
1153unsigned long pe_add_runtime(TCCState * s1)1440
1441ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
1154{1442{
1155 const char *start_symbol;1443 const char *start_symbol;
1156 unsigned long addr;1444 unsigned long addr = 0;
1445 int pe_type = 0;
1157 1446
1158 if (find_elf_sym(symtab_section, "WinMain"))1447 if (find_elf_sym(symtab_section, "_WinMain@16"))
1159 pe_type = PE_GUI;1448 pe_type = PE_GUI;
1160 else1449 else
1161 if (TCC_OUTPUT_DLL == s1->output_type)1450 if (TCC_OUTPUT_DLL == s1->output_type) {
1162 {
1163 pe_type = PE_DLL;1451 pe_type = PE_DLL;
1164 // need this for 'tccelf.c:relocate_section()'1452 /* need this for 'tccelf.c:relocate_section()' */
1165 s1->output_type = TCC_OUTPUT_EXE;1453 s1->output_type = TCC_OUTPUT_EXE;
1166 }1454 }
1167 1455
1168 start_symbol =1456 start_symbol =
1169 TCC_OUTPUT_MEMORY == s1->output_type1457 TCC_OUTPUT_MEMORY == s1->output_type
1170 ? PE_GUI == pe_type ? "_runwinmain" : NULL1458 ? PE_GUI == pe_type ? "_runwinmain" : NULL
1171 : PE_DLL == pe_type ? "_dllstart"1459 : PE_DLL == pe_type ? "__dllstart@12"
1172 : PE_GUI == pe_type ? "_winstart" : "_start";1460 : PE_GUI == pe_type ? "_winstart" : "_start"
1461 ;
1173 1462
1174 /* grab the startup code from libtcc1 */1463 /* grab the startup code from libtcc1 */
1175 if (start_symbol)1464 if (start_symbol)
1176 add_elf_sym(symtab_section,1465 add_elf_sym(symtab_section,
1177 0, 0,1466 0, 0,
1178 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1467 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1179 SHN_UNDEF, start_symbol);1468 SHN_UNDEF, start_symbol);
1180 1469
1181 if (0 == s1->nostdlib) {1470 if (0 == s1->nostdlib) {
1182 tcc_add_library(s1, "tcc1");1471 tcc_add_library(s1, "tcc1");
1472#ifdef __CYGWIN__
1473 tcc_add_library(s1, "cygwin1");
1474#else
1183 tcc_add_library(s1, "msvcrt");1475 tcc_add_library(s1, "msvcrt");
1476#endif
1477 tcc_add_library(s1, "kernel32");
1184 if (PE_DLL == pe_type || PE_GUI == pe_type) {1478 if (PE_DLL == pe_type || PE_GUI == pe_type) {
1185 tcc_add_library(s1, "kernel32");
1186 tcc_add_library(s1, "user32");1479 tcc_add_library(s1, "user32");
1187 tcc_add_library(s1, "gdi32");1480 tcc_add_library(s1, "gdi32");
1188 }1481 }
1189 }1482 }
1190 1483
1191 addr = start_symbol ?1484 if (start_symbol) {
1192 (unsigned long) tcc_get_symbol_err(s1, start_symbol) : 0;1485 addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);
1193 1486 if (s1->output_type == TCC_OUTPUT_MEMORY && addr)
1194 if (s1->output_type == TCC_OUTPUT_MEMORY && addr) {1487 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1195 /* for -run GUI's, put '_runwinmain' instead of 'main' */1488 add_elf_sym(symtab_section,
1196 add_elf_sym(symtab_section,
1197 addr, 0,1489 addr, 0,
1198 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1490 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1199 text_section->sh_num, "main");1491 text_section->sh_num, "main");
1492 }
1200 1493
1201 /* FreeConsole(); */1494 if (pe) {
1495 pe->type = pe_type;
1496 pe->start_addr = addr;
1202 }1497 }
1203 return addr;
1204}1498}
1205 1499
1206int tcc_output_pe(TCCState * s1, const char *filename)1500PUB_FN void pe_add_runtime(TCCState *s1)
1501{
1502 pe_add_runtime_ex(s1, NULL);
1503}
1504
1505PUB_FN int pe_output_file(TCCState * s1, const char *filename)
1207{1506{
1208 int ret;1507 int ret;
1209 struct pe_info pe;1508 struct pe_info pe;
1210 int i;1509 int i;
1510
1211 memset(&pe, 0, sizeof pe);1511 memset(&pe, 0, sizeof pe);
1212 pe.filename = filename;1512 pe.filename = filename;
1213 pe.s1 = s1;1513 pe.s1 = s1;
1214 pe.start_addr = pe_add_runtime(s1);
1215 1514
1216 relocate_common_syms(); /* assign bss adresses */1515 pe_add_runtime_ex(s1, &pe);
1516 relocate_common_syms(); /* assign bss adresses */
1517 tcc_add_linker_symbols(s1);
1518
1217 ret = pe_check_symbols(&pe);1519 ret = pe_check_symbols(&pe);
1218 if (0 == ret) {1520 if (0 == ret) {
1521 if (PE_DLL == pe.type) {
1522 pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);
1523 pe.imagebase = 0x10000000;
1524 } else {
1525 pe.imagebase = 0x00400000;
1526 }
1219 pe_assign_addresses(&pe);1527 pe_assign_addresses(&pe);
1220 relocate_syms(s1, 0);1528 relocate_syms(s1, 0);
1221 for (i = 1; i < s1->nb_sections; ++i) {1529 for (i = 1; i < s1->nb_sections; ++i) {
1222 Section *s = s1->sections[i];1530 Section *s = s1->sections[i];
1223 if (s->reloc)1531 if (s->reloc) {
1224 relocate_section(s1, s);1532 relocate_section(s1, s);
1533 pe_relocate_rva(&pe, s);
1534 }
1225 }1535 }
1226 ret = pe_write_pe(&pe);1536 if (s1->nb_errors)
1537 ret = 1;
1538 else
1539 ret = pe_write(&pe);
1540 tcc_free(pe.sec_info);
1227 }1541 }
1542
1228#ifdef PE_PRINT_SECTIONS1543#ifdef PE_PRINT_SECTIONS
1229 {1544 pe_print_sections(s1, "tcc.log");
1230 Section *s;
1231 FILE *f;
1232 f = fopen("tccpe.log", "wt");
1233 for (i = 1; i < s1->nb_sections; ++i) {
1234 s = s1->sections[i];
1235 pe_print_section(f, s);
1236 }
1237 pe_print_section(f, s1->dynsymtab_section);
1238 fclose(f);
1239 }
1240#endif1545#endif
1241 return ret;1546 return ret;
1242}1547}
1243 1548
1244/*----------------------------------------------------------------------------*/1549/* ------------------------------------------------------------- */
1550#endif /* def TCC_TARGET_PE */
1551/* ------------------------------------------------------------- */
win32/readme.txt+78−67
@@ -1,115 +1,126 @@
1 1
2 TinyCC-PE2 TinyCC-PE
3 ---------3 ---------
4 4
5 TinyCC (aka TCC) is a small but hyperfast C compiler,5 TinyCC (aka TCC) is a small but hyperfast C compiler,
6 written by Fabrice Bellard,6 written by Fabrice Bellard,
7 7
8 8
9 TinyCC-PE is the TinyCC compiler with an extension to9 TinyCC-PE is the TinyCC compiler with an extension to
10 write PE executables for MS-Windows.10 write PE executables for MS-Windows.
11 11
12 12
13 Features:13 Features:
14 ---------14 ---------
15 15
16 TinyCC-PE can produce console applications, native windows16 TinyCC-PE can produce console applications, native windows
17 GUI programs and DLL's.17 GUI programs and DLL's.
18 18
19 Most of the features pointed out by Fabrice Bellard for the19 Most of the features pointed out by Fabrice Bellard for the
20 original version are still valid, i.e:20 original version are still valid, i.e:
21 21
22 - SMALL! The package with ~400kb includes a complete C-compiler22 - SMALL! The package with ~400kb includes a complete C-compiler
23 with header files for console and GUI applications.23 with header files for console and GUI applications.
24 24
25 - With the -run switch you can run C-sources without any25 - With the -run switch you can run C-sources without any
26 linking directly from the command line.26 linking directly from the command line.
27 27
28 - TCC can of course compile itself.28 - TCC can of course compile itself.
29 29
30 30
31 Compilation: (omit that if you use the binary ZIP package)31 Compilation: (omit that if you use the binary ZIP package)
32 ------------32 ------------
33 33
34 You must use the MinGW and MSYS tools available at34 You must use the MinGW and MSYS tools available at
35 http://www.mingw.org to compile TCC for Windows. Untar the TCC35 http://www.mingw.org to compile TCC for Windows. Untar the TCC
36 archive and type in the MSYS shell:36 archive and type in the MSYS shell:
37 37
38 ./configure38 ./configure
39 make39 make
40 make install 40 make install
41 41
42 TCC is installed in c:\Program Files\tcc42 TCC is installed in c:\Program Files\tcc
43 43
44 Installation: (from the binary ZIP package)44 Alternatively you can use win32\build-tcc.bat to compile TCC
45 -------------45 with just gcc and ar from MINGW. To install, copy the entire
46 contents of the win32 directory to where you want.
46 47
47 Just unzip the package to a directory anywhere on your computer.
48 48
49 Installation: (from the binary ZIP package)
50 -------------
49 51
50 Examples:52 Just unzip the package to a directory anywhere on your computer.
51 ---------
52 53
53 For the 'Fibonacci' console example type from the command line:
54 54
55 tcc examples\fib.c55 Examples:
56 ---------
56 57
58 For the 'Fibonacci' console example type from the command line:
57 59
58 For the 'Hello Windows' GUI example:60 tcc examples\fib.c
59 61
60 tcc examples\hello_win.c62 For the 'Hello Windows' GUI example:
61 63
64 tcc examples\hello_win.c
62 65
63 For the 'Hello DLL' example:66 For the 'Hello DLL' example:
64 67
65 tcc -shared examples\dll.c68 tcc -shared examples\dll.c
66 tcc examples\hello_dll.c examples\dll.def69 tcc examples\hello_dll.c examples\dll.def
67 70
68 71
69 Import Definitions:72 Import Definitions:
70 -------------------73 -------------------
71 74
72 TinyCC-PE searches and reads import definition files similar75 TinyCC-PE searches and reads import definition files similar
73 to libraries.76 to libraries.
74 77
75 The included 'tiny_impdef' program may be used to make .def files78 The included 'tiny_impdef' program may be used to make .def files
76 for any DLL, e.g for an 'opengl32.def':79 for any DLL, e.g for an 'opengl32.def':
77 80
78 tiny_impdef.exe opengl32.dll81 tiny_impdef.exe opengl32.dll
79 82
80 or to the same effect:83 or to the same effect:
81 84
82 tcc -lkernel32 -run tiny_impdef.c opengl32.dll85 tcc -run tiny_impdef.c opengl32.dll
83 86
84 87
85 Header Files:88 Resource Files:
86 -------------89 ---------------
87 90
88 The system header files, except '_mingw.h', are from the91 TinyCC-PE can now link windows resources in coff format as generated
89 2.0 mingw distribution. See also: http://www.mingw.org/92 by MINGW's windres.exe. For example:
90 93
94 windres -O coff app.rc -o appres.o
95 tcc app.c appres.o -o app.exe
91 96
92 Compile TCC:
93 ------------
94 97
95 With TCC itself just say:98 Header Files:
99 -------------
96 100
97 tcc src\tcc.c -lkernel32 -o tcc.new.exe101 The system header files, except '_mingw.h', are from the
102 3.7 mingw distribution. See also: http://www.mingw.org/
98 103
99 Other compilers like mingw-gcc or msvc work as well.
100 To make libtcc1.a, you need 'ar' from the mingw binutils.
101 104
105 Compile TCC:
106 ------------
102 107
103 Documentation and License:108 With TCC itself just say:
104 --------------------------
105 109
106 TCC is distributed under the GNU Lesser General Public License110 tcc src\tcc.c -o tcc.new.exe
107 (see COPYING file).
108 111
109 Please read the original tcc-doc.html to have all the features112 Other compilers like mingw-gcc or msvc work as well.
110 of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/113 To make libtcc1.a, you need 'ar' from the mingw binutils.
111 114
112 115
113 --------------------------------------------116 Documentation and License:
114 09.Apr.2005 - grischka@users.sourceforge.net117 --------------------------
115 118
119 TCC is distributed under the GNU Lesser General Public License
120 (see COPYING file).
121
122 Please read the original tcc-doc.html to have all the features
123 of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/
124
125 --
126 grischka@users.sourceforge.net

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t