History

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.

Michael Matz committed 6 years ago

Commit 51f15d91 parent 1ca209d4 files · +9 −3Browse files

Unified · Split

4 files
  1. libtcc.c +1 −1
  2. tccelf.c +6 −0
  3. tccmacho.c +1 −1
  4. tccrun.c +1 −1
libtcc.c+1−1
@@ -777,7 +777,7 @@ LIBTCCAPI TCCState *tcc_new(void) · 776 unmodified lines
777777 s->seg_size = 32;
778778 #endif
779779 /* 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 */
781781 s->leading_underscore = 1;
782782 #endif
783783 s->ppfp = stdout;
tccelf.c+6−0
@@ -932,7 +932,13 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) · 931 unmodified lines
932932 /* Use ld.so to resolve symbol for us (for tcc -run) */
933933 if (do_resolve) {
934934 #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
935940 void *addr = dlsym(RTLD_DEFAULT, name);
941+#endif
936942 if (addr) {
937943 sym->st_value = (addr_t) addr;
938944 #ifdef DEBUG_RELOC
tccmacho.c+1−1
@@ -810,7 +810,7 @@ ST_FUNC int macho_output_file(TCCState *s1, const char *filename) · 809 unmodified lines
810810 Section *s;
811811 collect_sections(s1, &mo);
812812 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;
814814 if (s1->nb_errors)
815815 goto do_ret;
816816
tccrun.c+1−1
@@ -140,7 +140,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 139 unmodified lines
140140 rt_context *rc = &g_rtctxt;
141141 #endif
142142
143− s1->runtime_main = s1->nostdlib ? "_start" : "main";
143+ s1->runtime_main = s1->nostdlib ? "_start" : s1->leading_underscore ? "_main" : "main";
144144 if ((s1->dflag & 16) && !find_elf_sym(s1->symtab, s1->runtime_main))
145145 return 0;
146146 #ifdef CONFIG_TCC_BACKTRACE

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