macos: Deal with leading underscore on Mach-O

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.
This commit is contained in:
Michael Matz 2020-05-18 05:15:08 +02:00
parent 1ca209dad0
commit 51f15d9971
4 changed files with 9 additions and 3 deletions

View file

@ -777,7 +777,7 @@ LIBTCCAPI TCCState *tcc_new(void)
s->seg_size = 32;
#endif
/* enable this if you want symbols with leading underscore on windows: */
#if 0 /* def TCC_TARGET_PE */
#if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
s->leading_underscore = 1;
#endif
s->ppfp = stdout;

View file

@ -932,7 +932,13 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
/* Use ld.so to resolve symbol for us (for tcc -run) */
if (do_resolve) {
#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
#ifdef TCC_TARGET_MACHO
/* The symbols in the symtables have a prepended '_'
but dlsym() needs the undecorated name. */
void *addr = dlsym(RTLD_DEFAULT, name + 1);
#else
void *addr = dlsym(RTLD_DEFAULT, name);
#endif
if (addr) {
sym->st_value = (addr_t) addr;
#ifdef DEBUG_RELOC

View file

@ -810,7 +810,7 @@ ST_FUNC int macho_output_file(TCCState *s1, const char *filename)
Section *s;
collect_sections(s1, &mo);
relocate_syms(s1, s1->symtab, 0);
mo.ep.entryoff = get_elf_sym_addr(s1, "main", 1) - mo.seg[1]->vmaddr;
mo.ep.entryoff = get_elf_sym_addr(s1, "_main", 1) - mo.seg[1]->vmaddr;
if (s1->nb_errors)
goto do_ret;

View file

@ -140,7 +140,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
rt_context *rc = &g_rtctxt;
#endif
s1->runtime_main = s1->nostdlib ? "_start" : "main";
s1->runtime_main = s1->nostdlib ? "_start" : s1->leading_underscore ? "_main" : "main";
if ((s1->dflag & 16) && !find_elf_sym(s1->symtab, s1->runtime_main))
return 0;
#ifdef CONFIG_TCC_BACKTRACE