all C/C++/ObjC symbols in symbols tables have a leading underscore in Mach-O. Within TCC there's some confusion with tcc_add_symbol (not adding it) and tcc_get_elf_symbol (not expecting it), and resolve_syms (using dlsym, which doesn't expect it) and -run support. But this sort of works.
Unified · Split
| @@ -777,7 +777,7 @@ LIBTCCAPI TCCState *tcc_new(void) · 776 unmodified lines | |||
| 777 | s->seg_size = 32; | 777 | s->seg_size = 32; |
| 778 | #endif | 778 | #endif |
| 779 | /* enable this if you want symbols with leading underscore on windows: */ | 779 | /* enable this if you want symbols with leading underscore on windows: */ |
| 780 | #if 0 /* def TCC_TARGET_PE */ | 780 | #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */ |
| 781 | s->leading_underscore = 1; | 781 | s->leading_underscore = 1; |
| 782 | #endif | 782 | #endif |
| 783 | s->ppfp = stdout; | 783 | s->ppfp = stdout; |
| @@ -932,7 +932,13 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) · 931 unmodified lines | |||
| 932 | /* Use ld.so to resolve symbol for us (for tcc -run) */ | 932 | /* Use ld.so to resolve symbol for us (for tcc -run) */ |
| 933 | if (do_resolve) { | 933 | if (do_resolve) { |
| 934 | #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE | 934 | #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE |
| 935 | #ifdef TCC_TARGET_MACHO | ||
| 936 | /* The symbols in the symtables have a prepended '_' | ||
| 937 | but dlsym() needs the undecorated name. */ | ||
| 938 | void *addr = dlsym(RTLD_DEFAULT, name + 1); | ||
| 939 | #else | ||
| 935 | void *addr = dlsym(RTLD_DEFAULT, name); | 940 | void *addr = dlsym(RTLD_DEFAULT, name); |
| 941 | #endif | ||
| 936 | if (addr) { | 942 | if (addr) { |
| 937 | sym->st_value = (addr_t) addr; | 943 | sym->st_value = (addr_t) addr; |
| 938 | #ifdef DEBUG_RELOC | 944 | #ifdef DEBUG_RELOC |
| @@ -810,7 +810,7 @@ ST_FUNC int macho_output_file(TCCState *s1, const char *filename) · 809 unmodified lines | |||
| 810 | Section *s; | 810 | Section *s; |
| 811 | collect_sections(s1, &mo); | 811 | collect_sections(s1, &mo); |
| 812 | relocate_syms(s1, s1->symtab, 0); | 812 | relocate_syms(s1, s1->symtab, 0); |
| 813 | mo.ep.entryoff = get_elf_sym_addr(s1, "main", 1) - mo.seg[1]->vmaddr; | 813 | mo.ep.entryoff = get_elf_sym_addr(s1, "_main", 1) - mo.seg[1]->vmaddr; |
| 814 | if (s1->nb_errors) | 814 | if (s1->nb_errors) |
| 815 | goto do_ret; | 815 | goto do_ret; |
| 816 | 816 | ||
| @@ -140,7 +140,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 139 unmodified lines | |||
| 140 | rt_context *rc = &g_rtctxt; | 140 | rt_context *rc = &g_rtctxt; |
| 141 | #endif | 141 | #endif |
| 142 | 142 | ||
| 143 | s1->runtime_main = s1->nostdlib ? "_start" : "main"; | 143 | s1->runtime_main = s1->nostdlib ? "_start" : s1->leading_underscore ? "_main" : "main"; |
| 144 | if ((s1->dflag & 16) && !find_elf_sym(s1->symtab, s1->runtime_main)) | 144 | if ((s1->dflag & 16) && !find_elf_sym(s1->symtab, s1->runtime_main)) |
| 145 | return 0; | 145 | return 0; |
| 146 | #ifdef CONFIG_TCC_BACKTRACE | 146 | #ifdef CONFIG_TCC_BACKTRACE |