History

Update to run on apple 13.0 (Ventura)

Add --config-codesign option to run codesign on apple to sign executables. See configure and Makefile In tccmacho.c use codesign option to call codesign application. Add build_version/source_version Sort sections in __LINKEDIT the same way as llvm does. Add simple support for trie code. Need some more attention. Fix rebase/bind error.

herman ten brugge committed 3 years ago

Commit f48efee1 parent b86d82c3 files · +151 −20Browse files

Unified · Split

3 files
  1. Makefile +1 −0
  2. configure +1 −0
  3. tccmacho.c +149 −20
Makefile+1−0
@@ -94,6 +94,7 @@ NATIVE_DEFINES_$(CONFIG_Android) += -DTARGETOS_ANDROID · 93 unmodified lines
9494 NATIVE_DEFINES_$(CONFIG_pie) += -DCONFIG_TCC_PIE
9595 NATIVE_DEFINES_$(CONFIG_pic) += -DCONFIG_TCC_PIC
9696 NATIVE_DEFINES_$(CONFIG_new_macho) += -DCONFIG_NEW_MACHO
97+NATIVE_DEFINES_$(CONFIG_codesign) += -DCONFIG_CODESIGN
9798 NATIVE_DEFINES_$(CONFIG_new-dtags) += -DCONFIG_NEW_DTAGS
9899 NATIVE_DEFINES_no_$(CONFIG_bcheck) += -DCONFIG_TCC_BCHECK=0
99100 NATIVE_DEFINES_no_$(CONFIG_backtrace) += -DCONFIG_TCC_BACKTRACE=0
configure+1−0
@@ -351,6 +351,7 @@ Advanced options (experts only): · 350 unmodified lines
351351 --config-bcheck=no disable bounds checker (-b)
352352 --config-predefs=no do not compile tccdefs.h, instead just include
353353 --config-new_macho Use apple new macho object format
354+ --config-codesign Use codesign on apple to sign executables
354355 --dwarf=x Use dwarf debug info instead of stabs (x=2..5)
355356 EOF
356357 exit 1
tccmacho.c+149−20
@@ -107,6 +107,8 @@ struct load_command { · 106 unmodified lines
107107 #define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD)
108108 #define LC_DYLD_INFO_ONLY (0x22|LC_REQ_DYLD)
109109 #define LC_MAIN (0x28|LC_REQ_DYLD)
110+#define LC_SOURCE_VERSION 0x2A
111+#define LC_BUILD_VERSION 0x32
110112 #define LC_DYLD_EXPORTS_TRIE (0x33 | LC_REQ_DYLD)
111113 #define LC_DYLD_CHAINED_FIXUPS (0x34 | LC_REQ_DYLD)
112114
@@ -256,6 +258,24 @@ struct linkedit_data_command { · 143 unmodified lines
256258 uint32_t datasize; /* file size of data in __LINKEDIT segment */
257259 };
258260
261+#define PLATFORM_MACOS 1
262+
263+struct build_version_command {
264+ uint32_t cmd; /* LC_BUILD_VERSION */
265+ uint32_t cmdsize; /* sizeof(struct build_version_command) plus */
266+ /* ntools * sizeof(struct build_tool_version) */
267+ uint32_t platform; /* platform */
268+ uint32_t minos; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
269+ uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
270+ uint32_t ntools; /* number of tool entries following this */
271+};
272+
273+struct source_version_command {
274+ uint32_t cmd; /* LC_SOURCE_VERSION */
275+ uint32_t cmdsize; /* 16 */
276+ uint64_t version; /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
277+};
278+
259279 struct symtab_command {
260280 uint32_t cmd; /* LC_SYMTAB */
261281 uint32_t cmdsize; /* sizeof(struct symtab_command) */
@@ -492,7 +512,17 @@ static void * add_dylib(struct macho *mo, char *name) · 230 unmodified lines
492512 return lc;
493513 }
494514
495−#ifndef CONFIG_NEW_MACHO
515+static int uleb128_size (unsigned long long value)
516+{
517+ int size = 0;
518+
519+ do {
520+ value >>= 7;
521+ size++;
522+ } while (value != 0);
523+ return size;
524+}
525+
496526 static void write_uleb128(Section *section, uint64_t value)
497527 {
498528 do {
@@ -503,7 +533,6 @@ static void write_uleb128(Section *section, uint64_t value) · 4 unmodified lines
503533 *ptr = byte | (value ? 0x80 : 0);
504534 } while (value != 0);
505535 }
506−#endif
507536
508537 static void tcc_macho_add_destructor(TCCState *s1)
509538 {
@@ -512,15 +541,15 @@ static void tcc_macho_add_destructor(TCCState *s1) · 2 unmodified lines
512541 ElfW_Rel *rel;
513542 uint8_t *ptr;
514543
544+ mh_execute_header = put_elf_sym(s1->symtab, -4096, 0,
545+ ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0,
546+ text_section->sh_num, "__mh_execute_header");
515547 s = find_section(s1, ".fini_array");
516548 if (s->data_offset == 0)
517549 return;
518550 init_sym = put_elf_sym(s1->symtab, text_section->data_offset, 0,
519551 ELFW(ST_INFO)(STB_LOCAL, STT_FUNC), 0,
520552 text_section->sh_num, "___GLOBAL_init_65535");
521− mh_execute_header = put_elf_sym(s1->symtab, 0x100000000ll, 0,
522− ELFW(ST_INFO)(STB_LOCAL, STT_OBJECT), 0,
523− SHN_ABS, "__mh_execute_header");
524553 at_exit_sym = put_elf_sym(s1->symtab, 0, 0,
525554 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0,
526555 SHN_UNDEF, "___cxa_atexit");
@@ -674,11 +703,11 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 147 unmodified lines
674703 attr->plt_offset = 0; // ignore next bind
675704 s1->got->reloc->data_offset -= sizeof (ElfW_Rel);
676705 }
677− if (for_code)
706+ if (for_code && sym->st_shndx == SHN_UNDEF)
678707 s1->got->reloc->data_offset -= sizeof (ElfW_Rel);
679708 }
680709 }
681− if (for_code) {
710+ if (for_code && sym->st_shndx == SHN_UNDEF) {
682711 if (attr->plt_offset == -1) {
683712 uint8_t *jmp;
684713
@@ -1312,27 +1341,103 @@ static void bind_rebase(TCCState *s1, struct macho *mo) · 627 unmodified lines
13121341 }
13131342 #endif
13141343
1315−/* FIXME */
1344+struct trie {
1345+ const char *name;
1346+ int flag;
1347+ addr_t addr;
1348+ int offset_size;
1349+ int str_size;
1350+ int term_size;
1351+ int term_offset;
1352+};
1353+
1354+static int triecmp(const void *_a, const void *_b, void *arg)
1355+{
1356+ struct trie *a = (struct trie *) _a;
1357+ struct trie *b = (struct trie *) _b;
1358+
1359+ return strcmp(a->name, b->name);
1360+}
1361+
13161362 static void export_trie(TCCState *s1, struct macho *mo)
13171363 {
1364+ int i, j, n, m, offset;
1365+ uint8_t *ptr;
13181366 int sym_index;
1319− int sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));;
1367+ int sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
1368+ int n_trie = 0;
1369+ struct trie *trie = NULL;
1370+ addr_t vm_addr = get_segment(mo, 1)->vmaddr;
13201371
13211372 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
13221373 ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
13231374 const char *name = (char*)symtab_section->link->data + sym->st_name;
13241375
1325− if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE &&
1326− ELFW(ST_BIND)(sym->st_info) == STB_GLOBAL) {
1376+ if (sym->st_shndx == text_section->sh_num &&
1377+ (ELFW(ST_BIND)(sym->st_info) == STB_GLOBAL ||
1378+ ELFW(ST_BIND)(sym->st_info) == STB_WEAK)) {
13271379 int flag = EXPORT_SYMBOL_FLAGS_KIND_REGULAR;
1380+ addr_t addr =
1381+ sym->st_value + s1->sections[sym->st_shndx]->sh_addr - vm_addr;
13281382
1329− if (sym->st_shndx == SHN_ABS)
1330− flag = EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE;
13311383 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
13321384 flag |= EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
1333− dprintf ("%s %d %llx\n", name, flag, sym->st_value + s1->sections[sym->st_shndx]->sh_addr);
1385+ dprintf ("%s %d %llx\n", name, flag, addr + vm_addr);
1386+ trie = tcc_realloc(trie, (n_trie + 1) * sizeof(struct trie));
1387+ trie[n_trie].name = name;
1388+ trie[n_trie].flag = flag;
1389+ trie[n_trie].addr = addr;
1390+ trie[n_trie].offset_size = 1;
1391+ trie[n_trie].str_size = strlen(name) + 1;
1392+ trie[n_trie].term_size = uleb128_size(flag) + uleb128_size(addr);
1393+ trie[n_trie].term_offset = 0;
1394+ n_trie++;
13341395 }
13351396 }
1397+ /* FIXME: generate tree */
1398+ if (n_trie > 255) {
1399+ tcc_warning("Fix trie code. n_trie(%d) > 255", n_trie);
1400+ n_trie = 255;
1401+ }
1402+ if (n_trie) {
1403+ tcc_qsort(trie, n_trie, sizeof(struct trie), triecmp, NULL);
1404+ offset = 1 + 1;
1405+ for (i = 0; i < n_trie; i++)
1406+ offset += trie[i].str_size + trie[i].offset_size;
1407+ for (i = 0; i < n_trie; i++) {
1408+ n = uleb128_size(offset);
1409+ trie[i].term_offset = offset + n - 1;
1410+ trie[i].offset_size = n;
1411+ offset += 1 + trie[i].term_size + 1 + n - 1;
1412+ if (n > 1)
1413+ for (j = i - 1; j >= 0; j--) {
1414+ trie[j].term_offset += n - 1;
1415+ m = uleb128_size(trie[j].term_offset);
1416+ if (m != trie[j].offset_size) {
1417+ n += m - trie[j].offset_size;
1418+ offset += m - trie[j].offset_size;
1419+ trie[j].offset_size = m;
1420+ }
1421+ }
1422+ }
1423+ ptr = section_ptr_add(mo->exports, 2);
1424+ *ptr++ = 0;
1425+ *ptr = n_trie;
1426+ for (i = 0; i < n_trie; i++) {
1427+ ptr = section_ptr_add(mo->exports, trie[i].str_size);
1428+ memcpy(ptr, trie[i].name, trie[i].str_size);
1429+ write_uleb128(mo->exports, trie[i].term_offset);
1430+ }
1431+ for (i = 0; i < n_trie; i++) {
1432+ write_uleb128(mo->exports, trie[i].term_size);
1433+ write_uleb128(mo->exports, trie[i].flag);
1434+ write_uleb128(mo->exports, trie[i].addr);
1435+ ptr = section_ptr_add(mo->exports, 1);
1436+ *ptr = 0;
1437+ }
1438+ section_ptr_add(mo->exports, -mo->exports->data_offset & 7);
1439+ }
1440+ tcc_free(trie);
13361441 }
13371442
13381443 static void collect_sections(TCCState *s1, struct macho *mo)
@@ -1345,6 +1450,8 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 6 unmodified lines
13451450 struct linkedit_data_command *chained_fixups_lc;
13461451 struct linkedit_data_command *export_trie_lc;
13471452 #endif
1453+ struct build_version_command *dyldbv;
1454+ struct source_version_command *dyldsv;
13481455 struct dylinker_command *dyldlc;
13491456 struct symtab_command *symlc;
13501457 struct dysymtab_command *dysymlc;
@@ -1377,8 +1484,8 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 26 unmodified lines
13771484 mo->dyldinfo = add_lc(mo, LC_DYLD_INFO_ONLY, sizeof(*mo->dyldinfo));
13781485 #endif
13791486
1380− mo->ep = add_lc(mo, LC_MAIN, sizeof(*mo->ep));
1381− mo->ep->entryoff = 4096;
1487+ symlc = add_lc(mo, LC_SYMTAB, sizeof(*symlc));
1488+ dysymlc = add_lc(mo, LC_DYSYMTAB, sizeof(*dysymlc));
13821489
13831490 i = (sizeof(*dyldlc) + strlen("/usr/lib/dyld") + 1 + 7) &-8;
13841491 dyldlc = add_lc(mo, LC_LOAD_DYLINKER, i);
@@ -1386,8 +1493,17 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 1 unmodified line
13861493 str = (char*)dyldlc + dyldlc->name;
13871494 strcpy(str, "/usr/lib/dyld");
13881495
1389− symlc = add_lc(mo, LC_SYMTAB, sizeof(*symlc));
1390− dysymlc = add_lc(mo, LC_DYSYMTAB, sizeof(*dysymlc));
1496+ dyldbv = add_lc(mo, LC_BUILD_VERSION, sizeof(*dyldbv));
1497+ dyldbv->platform = PLATFORM_MACOS;
1498+ dyldbv->minos = (10 << 16) + (6 << 8);
1499+ dyldbv->sdk = (10 << 16) + (6 << 8);
1500+ dyldbv->ntools = 0;
1501+
1502+ dyldsv = add_lc(mo, LC_SOURCE_VERSION, sizeof(*dyldsv));
1503+ dyldsv->version = 0;
1504+
1505+ mo->ep = add_lc(mo, LC_MAIN, sizeof(*mo->ep));
1506+ mo->ep->entryoff = 4096;
13911507
13921508 for(i = 0; i < s1->nb_loaded_dlls; i++) {
13931509 DLLReference *dllref = s1->loaded_dlls[i];
@@ -1710,7 +1826,9 @@ ST_FUNC void bind_rebase_import(TCCState *s1, struct macho *mo) · 316 unmodified lines
17101826 sym_index = ELFW(R_SYM)(mo->bind_rebase[i].rel.r_info);
17111827 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
17121828 name = (char *) symtab_section->link->data + sym->st_name;
1713− tcc_error("Overlap bind/rebase %s:%s",
1829+ tcc_error("Overlap %s/%s %s:%s",
1830+ mo->bind_rebase[i].bind ? "bind" : "rebase",
1831+ mo->bind_rebase[i + 1].bind ? "bind" : "rebase",
17141832 s1->sections[mo->bind_rebase[i].section]->name, name);
17151833 }
17161834 header = (struct dyld_chained_fixups_header *) data;
@@ -1832,7 +1950,7 @@ ST_FUNC void bind_rebase_import(TCCState *s1, struct macho *mo) · 115 unmodified lines
18321950 import[bind_index].weak_import =
18331951 ELFW(ST_BIND)(sym->st_info) == STB_WEAK;
18341952 name = (char *) symtab_section->link->data + sym->st_name;
1835− strcpy(data, name);
1953+ strcpy((char *) data, name);
18361954 data += strlen(name) + 1;
18371955 bind_index++;
18381956 }
@@ -1893,6 +2011,17 @@ ST_FUNC int macho_output_file(TCCState *s1, const char *filename) · 54 unmodified lines
18932011 tcc_free(mo.e2msym);
18942012
18952013 fclose(fp);
2014+#ifdef CONFIG_CODESIGN
2015+ {
2016+ char command[1024];
2017+ int retval;
2018+
2019+ snprintf(command, sizeof(command), "codesign -f -s - %s", filename);
2020+ retval = system (command);
2021+ if (retval == -1 || !(WIFEXITED(retval) && WEXITSTATUS(retval) == 0))
2022+ tcc_error ("command failed '%s'", command);
2023+ }
2024+#endif
18962025 return ret;
18972026 }
18982027

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