History

macOS 11: link using dyld shared cache

Supports linking using .tbd definitions found in the default installed command-line tools SDK or Xcode.app SDK. Only for creating executables (not yet for `tcc -run`).

Sushant Pandurangi committed 5 years ago

Commit f6fb4d01 parent e3a0eb53 files · +103 −20Browse files

Unified · Split

3 files
  1. libtcc.c +5 −3
  2. tcc.h +17 −1
  3. tccmacho.c +81 −16
libtcc.c+5−3
@@ -1006,7 +1006,9 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 1005 unmodified lines
10061006 lseek(fd, 0, SEEK_SET);
10071007
10081008 #ifdef TCC_TARGET_MACHO
1009− if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1009+ if (0 == obj_type
1010+ && (0 == strcmp(tcc_fileextension(filename), ".dylib")
1011+ || 0 == strcmp(tcc_fileextension(filename), ".tbd")))
10101012 obj_type = AFF_BINTYPE_DYN;
10111013 #endif
10121014
@@ -1142,8 +1144,8 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 129 unmodified lines
11421144 static const char * const libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
11431145 const char * const *pp = s->static_link ? libs + 4 : libs;
11441146 #elif defined TCC_TARGET_MACHO
1145− static const char * const libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1146− const char * const *pp = s->static_link ? libs + 1 : libs;
1147+ static const char * const libs[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL };
1148+ const char * const *pp = s->static_link ? libs + 2 : libs;
11471149 #elif defined TARGETOS_OpenBSD
11481150 static const char * const libs[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL };
11491151 const char * const *pp = s->static_link ? libs + 1 : libs;
tcc.h+17−1
@@ -42,6 +42,7 @@ · 41 unmodified lines
4242 #ifndef _WIN32
4343 # include <unistd.h>
4444 # include <sys/time.h>
45+# include <sys/stat.h>
4546 # ifndef CONFIG_TCC_STATIC
4647 # include <dlfcn.h>
4748 # endif
@@ -246,6 +247,12 @@ extern long double strtold (const char *__nptr, char **__endptr); · 198 unmodified lines
246247 # define ALSO_TRIPLET(s) s
247248 #endif
248249
250+// FIXME: do this at runtime instead; check output of `xcode-select -p`. libxcselect provides this stuff
251+#ifndef CONFIG_OSX_SDK1
252+# define CONFIG_OSX_SDK1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
253+# define CONFIG_OSX_SDK2 "/Applications/Xcode.app/Developer/SDKs/MacOSX.sdk"
254+#endif
255+
249256 /* path to find crt1.o, crti.o and crtn.o */
250257 #ifndef CONFIG_TCC_CRTPREFIX
251258 # define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
@@ -274,10 +281,19 @@ extern long double strtold (const char *__nptr, char **__endptr); · 22 unmodified lines
274281 # ifdef TCC_TARGET_PE
275282 # define CONFIG_TCC_LIBPATHS "{B}/lib"
276283 # else
277−# define CONFIG_TCC_LIBPATHS \
284+# ifdef TCC_TARGET_MACHO
285+# define CONFIG_TCC_LIBPATHS \
286+ ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
287+ ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
288+ ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)\
289+ ":" ALSO_TRIPLET(CONFIG_OSX_SDK1 "/usr/" CONFIG_LDDIR) \
290+ ":" ALSO_TRIPLET(CONFIG_OSX_SDK2 "/usr/" CONFIG_LDDIR)
291+# else
292+# define CONFIG_TCC_LIBPATHS \
278293 ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
279294 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
280295 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
296+# endif
281297 # endif
282298 #endif
283299
tccmacho.c+81−16
@@ -837,7 +837,78 @@ static uint32_t macho_swap32(uint32_t x) · 836 unmodified lines
837837 }
838838 #define SWAP(x) (swap ? macho_swap32(x) : (x))
839839
840−ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev)
840+ST_FUNC int macho_add_dllref(TCCState* s1, int lev, const char* soname)
841+{
842+ /* if the dll is already loaded, do not load it */
843+ DLLReference *dllref;
844+ for(int i = 0; i < s1->nb_loaded_dlls; i++) {
845+ dllref = s1->loaded_dlls[i];
846+ if (!strcmp(soname, dllref->name)) {
847+ /* but update level if needed */
848+ if (lev < dllref->level)
849+ dllref->level = lev;
850+ return -1;
851+ }
852+ }
853+ dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
854+ dllref->level = lev;
855+ strcpy(dllref->name, soname);
856+ dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
857+ return 0;
858+}
859+
860+#define tbd_parse_movepast(s) \
861+ (pos = (pos = strstr(pos, s)) ? pos + strlen(s) : NULL)
862+#define tbd_parse_movetoany(cs) (pos = strpbrk(pos, cs))
863+#define tbd_parse_skipws while (*pos && (*pos==' '||*pos=='\n')) ++pos
864+#define tbd_parse_tramplequote if(*pos=='\''||*pos=='"') tbd_parse_trample
865+#define tbd_parse_tramplespace if(*pos==' ') tbd_parse_trample
866+#define tbd_parse_trample *pos++=0
867+
868+ST_FUNC int macho_load_tbd(TCCState* s1, int fd, const char* filename, int lev)
869+{
870+ char* soname;
871+
872+ struct stat sb;
873+ fstat(fd,&sb);
874+ char* data = load_data(fd, 0, sb.st_size+1);
875+ data[sb.st_size]=0;
876+ char* pos = data;
877+
878+ if (!tbd_parse_movepast("install-name: ")) return -1;
879+ tbd_parse_skipws;
880+ tbd_parse_tramplequote;
881+ soname = pos;
882+ if (!tbd_parse_movetoany("\n \"'")) return -1;
883+ tbd_parse_trample;
884+ if (macho_add_dllref(s1, lev, soname) != 0) goto the_end;
885+
886+ while(pos) {
887+ char* sym = NULL;
888+ if (!tbd_parse_movepast("symbols: ")) break;
889+ if (!tbd_parse_movepast("[")) break;
890+ int cont = 1;
891+ while (cont) {
892+ tbd_parse_skipws;
893+ tbd_parse_tramplequote;
894+ sym = pos;
895+ if (!tbd_parse_movetoany(",] \"'")) break;
896+ tbd_parse_tramplequote;
897+ tbd_parse_tramplespace;
898+ tbd_parse_skipws;
899+ if (*pos==0||*pos==']') cont=0;
900+ tbd_parse_trample;
901+ set_elf_sym(s1->dynsymtab_section, 0, 0,
902+ ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, SHN_UNDEF, sym);
903+ }
904+ }
905+
906+the_end:
907+ tcc_free(data);
908+ return 0;
909+}
910+
911+ST_FUNC int macho_load_dylib(TCCState * s1, int fd, const char* filename, int lev)
841912 {
842913 unsigned char buf[sizeof(struct mach_header_64)];
843914 void *buf2;
@@ -853,7 +924,6 @@ ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev) · 9 unmodified lines
853924 uint32_t strsize = 0;
854925 uint32_t iextdef = 0;
855926 uint32_t nextdef = 0;
856− DLLReference *dllref;
857927
858928 again:
859929 if (full_read(fd, buf, sizeof(buf)) != sizeof(buf))
@@ -934,20 +1004,8 @@ ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev) · 74 unmodified lines
9341004 lc = (struct load_command*) ((char*)lc + lc->cmdsize);
9351005 }
9361006
937− /* if the dll is already loaded, do not load it */
938− for(i = 0; i < s1->nb_loaded_dlls; i++) {
939− dllref = s1->loaded_dlls[i];
940− if (!strcmp(soname, dllref->name)) {
941− /* but update level if needed */
942− if (lev < dllref->level)
943− dllref->level = lev;
944− goto the_end;
945− }
946− }
947− dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
948− dllref->level = lev;
949− strcpy(dllref->name, soname);
950− dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1007+ if (0 != macho_add_dllref(s1, lev, soname))
1008+ goto the_end;
9511009
9521010 if (!nsyms || !nextdef)
9531011 tcc_warning("%s doesn't export any symbols?", filename);
@@ -972,3 +1030,10 @@ ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev) · 18 unmodified lines
9721030 tcc_free(buf2);
9731031 return 0;
9741032 }
1033+
1034+ST_FUNC int macho_load_dll(TCCState * s1, int fd, const char* filename, int lev)
1035+{
1036+ return strcmp(tcc_fileextension(filename), ".tbd") == 0
1037+ ? macho_load_tbd(s1, fd, filename, lev)
1038+ : macho_load_dylib(s1, fd, filename, lev);
1039+}

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