From 33fa3a4d41bb9cba9997dc030e3c8f01ccaa51a9 Mon Sep 17 00:00:00 2001 From: Sushant Pandurangi Date: Sun, 4 Jul 2021 11:07:48 +0200 Subject: [PATCH] macOS 11: `tcc -run` using dyld shared cache --- libtcc.c | 13 +++++++++++-- tccmacho.c | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/libtcc.c b/libtcc.c index b05ebf06..6786ba11 100644 --- a/libtcc.c +++ b/libtcc.c @@ -979,6 +979,10 @@ static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size) } #endif +#ifdef TCC_TARGET_MACHO +ST_FUNC const char* macho_tbd_soname(const char* filename); +#endif + ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) { int fd, ret = -1; @@ -1029,9 +1033,14 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) case AFF_BINTYPE_DYN: if (s1->output_type == TCC_OUTPUT_MEMORY) { #ifdef TCC_IS_NATIVE - void *dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); + char* soname = filename; +# ifdef TCC_TARGET_MACHO + if (!strcmp(tcc_fileextension(filename), ".tbd")) + soname = macho_tbd_soname(filename); +# endif + void* dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY); if (dl) { - tcc_add_dllref(s1, filename)->handle = dl; + tcc_add_dllref(s1, soname)->handle = dl; ret = 0; } #endif diff --git a/tccmacho.c b/tccmacho.c index ce61bdb8..fe670e8f 100644 --- a/tccmacho.c +++ b/tccmacho.c @@ -865,6 +865,29 @@ ST_FUNC int macho_add_dllref(TCCState* s1, int lev, const char* soname) #define tbd_parse_tramplespace if(*pos==' ') tbd_parse_trample #define tbd_parse_trample *pos++=0 +ST_FUNC const char* macho_tbd_soname(const char* filename) { + char* soname; + int fd = open(filename,O_RDONLY); + if (fd<0) return filename; + struct stat sb; + fstat(fd,&sb); + char* data = load_data(fd, 0, sb.st_size+1); + data[sb.st_size]=0; + char* pos = data; + + if (!tbd_parse_movepast("install-name: ")) return filename; + tbd_parse_skipws; + tbd_parse_tramplequote; + soname = pos; + if (!tbd_parse_movetoany("\n \"'")) return filename; + tbd_parse_trample; + char* ret = tcc_mallocz(strlen(soname)+1); + strcpy(ret, soname); + // soname = strdup(soname); + tcc_free(data); + return ret; +} + ST_FUNC int macho_load_tbd(TCCState* s1, int fd, const char* filename, int lev) { char* soname;