History

tccpe: support leading underscore for symbols

To make this the default, enable this line in libtcc.c:tcc_new: #if defined(TCC_TARGET_PE) && 0 s->leading_underscore = 1; and then recompile tcc and also libtcc1.a

grischka committed 15 years ago

Commit a3ebdd01 parent 11b2d333 files · +42 −24Browse files

Unified · Split

3 files
  1. libtcc.c +1 −1
  2. tccpe.c +39 −23
  3. win32/lib/chkstk.S +2 −0
libtcc.c+1−1
@@ -991,8 +991,8 @@ LIBTCCAPI TCCState *tcc_new(void) · 990 unmodified lines
991#ifdef CHAR_IS_UNSIGNED991#ifdef CHAR_IS_UNSIGNED
992 s->char_is_unsigned = 1;992 s->char_is_unsigned = 1;
993#endif993#endif
994 /* enable this if you want symbols with leading underscore on windows: */
994#if defined(TCC_TARGET_PE) && 0995#if defined(TCC_TARGET_PE) && 0
995 /* XXX: currently the PE linker is not ready to support that */
996 s->leading_underscore = 1;996 s->leading_underscore = 1;
997#endif997#endif
998 if (s->section_align == 0)998 if (s->section_align == 0)
tccpe.c+39−23
@@ -364,31 +364,41 @@ struct pe_info { · 363 unmodified lines
364 364
365/* --------------------------------------------*/365/* --------------------------------------------*/
366 366
367static const char* get_alt_symbol(char *buffer, const char *symbol)367static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym)
368{368{
369 const char *p;369 const char *name = symtab_section->link->data + sym->st_name;
370 p = strrchr(symbol, '@');370 if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & 2))
371 if (p && isnum(p[1]) && symbol[0] == '_') { /* stdcall decor */371 return name + 1;
372 strcpy(buffer, symbol+1)[p-symbol-1] = 0;372 return name;
373 } else if (symbol[0] != '_') { /* try non-ansi function */
374 buffer[0] = '_', strcpy(buffer + 1, symbol);
375 } else if (0 == memcmp(symbol, "__imp__", 7)) { /* mingw 2.0 */
376 strcpy(buffer, symbol + 6);
377 } else if (0 == memcmp(symbol, "_imp___", 7)) { /* mingw 3.7 */
378 strcpy(buffer, symbol + 6);
379 } else {
380 return symbol;
381 }
382 return buffer;
383}373}
384 374
385static int pe_find_import(TCCState * s1, const char *symbol)375static int pe_find_import(TCCState * s1, ElfW(Sym) *sym)
386{376{
387 char buffer[200];377 char buffer[200];
388 const char *s;378 const char *s, *p;
389 int sym_index, n = 0;379 int sym_index, n = 0;
380
390 do {381 do {
391 s = n ? get_alt_symbol(buffer, symbol) : symbol;382 s = pe_export_name(s1, sym);
383 if (n) {
384 /* second try: */
385 if (sym->st_other & 2) {
386 /* try w/0 stdcall deco (windows API convention) */
387 p = strrchr(s, '@');
388 if (!p || s[0] != '_')
389 break;
390 strcpy(buffer, s+1)[p-s-1] = 0;
391 } else if (s[0] != '_') { /* try non-ansi function */
392 buffer[0] = '_', strcpy(buffer + 1, s);
393 } else if (0 == memcmp(s, "__imp__", 7)) { /* mingw 2.0 */
394 strcpy(buffer, s + 6);
395 } else if (0 == memcmp(s, "_imp___", 7)) { /* mingw 3.7 */
396 strcpy(buffer, s + 6);
397 } else {
398 break;
399 }
400 s = buffer;
401 }
392 sym_index = find_elf_sym(s1->dynsymtab_section, s);402 sym_index = find_elf_sym(s1->dynsymtab_section, s);
393 // printf("find (%d) %d %s\n", n, sym_index, s);403 // printf("find (%d) %d %s\n", n, sym_index, s);
394 } while (0 == sym_index && ++n < 2);404 } while (0 == sym_index && ++n < 2);
@@ -899,7 +909,7 @@ static void pe_build_exports(struct pe_info *pe) · 504 unmodified lines
899 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));909 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
900 for (sym_index = 1; sym_index < sym_end; ++sym_index) {910 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
901 sym = (ElfW(Sym)*)symtab_section->data + sym_index;911 sym = (ElfW(Sym)*)symtab_section->data + sym_index;
902 name = symtab_section->link->data + sym->st_name;912 name = pe_export_name(pe->s1, sym);
903 if ((sym->st_other & 1)913 if ((sym->st_other & 1)
904 /* export only symbols from actually written sections */914 /* export only symbols from actually written sections */
905 && pe->s1->sections[sym->st_shndx]->sh_addr) {915 && pe->s1->sections[sym->st_shndx]->sh_addr) {
@@ -1219,7 +1229,7 @@ static int pe_check_symbols(struct pe_info *pe) · 313 unmodified lines
1219 1229
1220 const char *name = symtab_section->link->data + sym->st_name;1230 const char *name = symtab_section->link->data + sym->st_name;
1221 unsigned type = ELFW_ST_TYPE(sym->st_info);1231 unsigned type = ELFW_ST_TYPE(sym->st_info);
1222 int imp_sym = pe_find_import(pe->s1, name);1232 int imp_sym = pe_find_import(pe->s1, sym);
1223 struct import_symbol *is;1233 struct import_symbol *is;
1224 1234
1225 if (0 == imp_sym)1235 if (0 == imp_sym)
@@ -1756,11 +1766,17 @@ static void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 530 unmodified lines
1756 1766
1757 start_symbol =1767 start_symbol =
1758 TCC_OUTPUT_MEMORY == s1->output_type1768 TCC_OUTPUT_MEMORY == s1->output_type
1759 ? PE_GUI == pe_type ? "_runwinmain" : NULL1769 ? PE_GUI == pe_type ? "__runwinmain" : "_main"
1760 : PE_DLL == pe_type ? PE_STDSYM("_dllstart","@12")1770 : PE_DLL == pe_type ? PE_STDSYM("__dllstart","@12")
1761 : PE_GUI == pe_type ? "_winstart" : "_start"1771 : PE_GUI == pe_type ? "__winstart" : "__start"
1762 ;1772 ;
1763 1773
1774 if (!s1->leading_underscore || strchr(start_symbol, '@')) {
1775 ++start_symbol;
1776 if (start_symbol[0] != '_')
1777 start_symbol = NULL;
1778 }
1779
1764 /* grab the startup code from libtcc1 */1780 /* grab the startup code from libtcc1 */
1765 if (start_symbol)1781 if (start_symbol)
1766 add_elf_sym(symtab_section,1782 add_elf_sym(symtab_section,
win32/lib/chkstk.S+2−0
@@ -150,6 +150,8 @@ seh_scopetable: · 149 unmodified lines
150seh_handler:150seh_handler:
151 jmp _except_handler3151 jmp _except_handler3
152 152
153.globl ___try__
154___try__:
153.globl __try__155.globl __try__
154__try__:156__try__:
155 push %ebp157 push %ebp

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