History

Fixed trie code generation for apple

I removed the FIXME for generating trie code with working code. I also fixed a problem where: cc -g -c a.c; cc -o a a.o created a bind/rebase error because the second cc did not use -g and .debug sections where not checked correctly. I also removed the elfinterp printout in tcc.c when TCC_TARGET_MACHO is set.

herman ten brugge committed 3 years ago

Commit 6b9fb931 parent d8329c22 files · +168 −68Browse files

Unified · Split

2 files
  1. tcc.c +1 −1
  2. tccmacho.c +167 −67
tcc.c+1−1
@@ -207,7 +207,7 @@ static void print_search_dirs(TCCState *s) · 206 unmodified lines
207207 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
208208 print_dirs("libraries", s->library_paths, s->nb_library_paths);
209209 printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
210−#ifndef TCC_TARGET_PE
210+#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
211211 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
212212 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
213213 #endif
tccmacho.c+167−67
@@ -650,7 +650,6 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 649 unmodified lines
650650 ElfW_Rel *rel;
651651 ElfW(Sym) *sym;
652652 int i, type, gotplt_entry, sym_index, for_code;
653− int sh_num, debug;
654653 uint32_t *pi, *goti;
655654 struct sym_attr *attr;
656655
@@ -659,12 +658,9 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 2 unmodified lines
659658 mo->nr_plt = mo->n_got = 0;
660659 for (i = 1; i < s1->nb_sections; i++) {
661660 s = s1->sections[i];
662− if (s->sh_type != SHT_RELX)
661+ if (s->sh_type != SHT_RELX ||
662+ !strncmp(s1->sections[s->sh_info]->name, ".debug_", 7))
663663 continue;
664− sh_num = s1->sections[s->sh_info]->sh_num;
665− debug = sh_num >= s1->dwlo && sh_num < s1->dwhi;
666− if (debug)
667− continue;
668664 for_each_elem(s, 0, rel, ElfW_Rel) {
669665 type = ELFW(R_TYPE)(rel->r_info);
670666 gotplt_entry = gotplt_entry_type(type);
@@ -765,7 +761,7 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 94 unmodified lines
765761 ElfW_Rel *rel;
766762 ElfW(Sym) *sym;
767763 int i, type, gotplt_entry, sym_index, for_code;
768− int sh_num, debug, bind_offset, la_symbol_offset;
764+ int bind_offset, la_symbol_offset;
769765 uint32_t *pi, *goti;
770766 struct sym_attr *attr;
771767
@@ -807,10 +803,9 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 35 unmodified lines
807803 mo->nr_plt = mo->n_got = 0;
808804 for (i = 1; i < s1->nb_sections; i++) {
809805 s = s1->sections[i];
810− if (s->sh_type != SHT_RELX)
806+ if (s->sh_type != SHT_RELX ||
807+ !strncmp(s1->sections[s->sh_info]->name, ".debug_", 7))
811808 continue;
812− sh_num = s1->sections[s->sh_info]->sh_num;
813− debug = sh_num >= s1->dwlo && sh_num < s1->dwhi;
814809 for_each_elem(s, 0, rel, ElfW_Rel) {
815810 type = ELFW(R_TYPE)(rel->r_info);
816811 gotplt_entry = gotplt_entry_type(type);
@@ -820,9 +815,8 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 3 unmodified lines
820815 address due to codegen (i.e. a reloc requiring a got slot). */
821816 sym_index = ELFW(R_SYM)(rel->r_info);
822817 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
823− if (!debug &&
824− (sym->st_shndx == SHN_UNDEF
825− || gotplt_entry == ALWAYS_GOTPLT_ENTRY)) {
818+ if (sym->st_shndx == SHN_UNDEF
819+ || gotplt_entry == ALWAYS_GOTPLT_ENTRY) {
826820 attr = get_sym_attr(s1, sym_index, 1);
827821 if (!attr->dyn_index) {
828822 attr->got_offset = s1->got->data_offset;
@@ -1041,8 +1035,7 @@ static void convert_symbol(TCCState *s1, struct macho *mo, struct nlist_64 *pn) · 212 unmodified lines
10411035 else if (sym->st_shndx >= SHN_LORESERVE)
10421036 tcc_error("unhandled ELF symbol section %d %s", sym->st_shndx, name);
10431037 else if (!mo->elfsectomacho[sym->st_shndx]) {
1044− int sh_num = s1->sections[sym->st_shndx]->sh_num;
1045− if (sh_num < s1->dwlo || sh_num >= s1->dwhi)
1038+ if (strncmp(s1->sections[sym->st_shndx]->name, ".debug_", 7))
10461039 tcc_error("ELF section %d(%s) not mapped into Mach-O for symbol %s",
10471040 sym->st_shndx, s1->sections[sym->st_shndx]->name, name);
10481041 }
@@ -1341,32 +1334,139 @@ static void bind_rebase(TCCState *s1, struct macho *mo) · 292 unmodified lines
13411334 }
13421335 #endif
13431336
1344−struct trie {
1337+struct trie_info {
13451338 const char *name;
13461339 int flag;
13471340 addr_t addr;
1348− int offset_size;
13491341 int str_size;
13501342 int term_size;
1351− int term_offset;
13521343 };
13531344
1345+struct trie_node {
1346+ int start;
1347+ int end;
1348+ int index_start;
1349+ int index_end;
1350+ int n_child;
1351+ struct trie_node *child;
1352+};
1353+
1354+struct trie_seq {
1355+ int n_child;
1356+ struct trie_node *node;
1357+ int offset;
1358+ int nest_offset;
1359+};
1360+
1361+static void create_trie(struct trie_node *node,
1362+ int from, int to, int index_start,
1363+ int n_trie, struct trie_info *trie)
1364+{
1365+ int i;
1366+ int start, end, index_end;
1367+ char cur;
1368+ struct trie_node *child;
1369+
1370+ for (i = from; i < to; i = end) {
1371+ cur = trie[i].name[index_start];
1372+ start = i++;
1373+ for (; i < to; i++)
1374+ if (cur != trie[i].name[index_start])
1375+ break;
1376+ end = i;
1377+ if (start == end - 1 ||
1378+ (trie[start].name[index_start] &&
1379+ trie[start].name[index_start + 1] == 0))
1380+ index_end = trie[start].str_size - 1;
1381+ else {
1382+ index_end = index_start + 1;
1383+ for (;;) {
1384+ cur = trie[start].name[index_end];
1385+ for (i = start + 1; i < end; i++)
1386+ if (cur != trie[i].name[index_end])
1387+ break;
1388+ if (trie[start].name[index_end] &&
1389+ trie[start].name[index_end + 1] == 0) {
1390+ end = start + 1;
1391+ index_end = trie[start].str_size - 1;
1392+ break;
1393+ }
1394+ if (i != end)
1395+ break;
1396+ index_end++;
1397+ }
1398+ }
1399+ node->child = tcc_realloc(node->child,
1400+ (node->n_child + 1) *
1401+ sizeof(struct trie_node));
1402+ child = &node->child[node->n_child];
1403+ child->start = start;
1404+ child->end = end;
1405+ child->index_start = index_start;
1406+ child->index_end = index_end;
1407+ child->n_child = 0;
1408+ child->child = NULL;
1409+ node->n_child++;
1410+ if (start != end - 1)
1411+ create_trie(child, start, end, index_end, n_trie, trie);
1412+ }
1413+}
1414+
1415+static int create_seq(int *offset, int *n_head, struct trie_seq **seq,
1416+ struct trie_node *node,
1417+ int n_trie, struct trie_info *trie)
1418+{
1419+ int i, nest_offset, last_head = *n_head, retval = *offset;
1420+ struct trie_seq *p_head;
1421+ struct trie_node *p_nest;
1422+
1423+ for (i = 0; i < node->n_child; i++) {
1424+ p_nest = &node->child[i];
1425+ *seq = tcc_realloc(*seq, (*n_head + 1) * sizeof(struct trie_seq));
1426+ p_head = &(*seq)[(*n_head)++];
1427+ p_head->n_child = i == 0 ? node->n_child : -1;
1428+ p_head->node = p_nest;
1429+ p_head->offset = *offset;
1430+ p_head->nest_offset = 0;
1431+ *offset += (i == 0 ? 1 + 1 : 0) +
1432+ p_nest->index_end - p_nest->index_start + 1 + 3;
1433+ }
1434+ for (i = 0; i < node->n_child; i++) {
1435+ nest_offset =
1436+ create_seq(offset, n_head, seq, &node->child[i], n_trie, trie);
1437+ p_head = &(*seq)[last_head + i];
1438+ p_head->nest_offset = nest_offset;
1439+ }
1440+ return retval;
1441+}
1442+
1443+static void node_free(struct trie_node *node)
1444+{
1445+ int i;
1446+
1447+ for (i = 0; i < node->n_child; i++)
1448+ node_free(&node->child[i]);
1449+ tcc_free(node->child);
1450+}
1451+
13541452 static int triecmp(const void *_a, const void *_b, void *arg)
13551453 {
1356− struct trie *a = (struct trie *) _a;
1357− struct trie *b = (struct trie *) _b;
1454+ struct trie_info *a = (struct trie_info *) _a;
1455+ struct trie_info *b = (struct trie_info *) _b;
13581456
13591457 return strcmp(a->name, b->name);
13601458 }
13611459
13621460 static void export_trie(TCCState *s1, struct macho *mo)
13631461 {
1364− int i, j, n, m, offset;
1462+ int i, size, offset = 0, save_offset;
13651463 uint8_t *ptr;
13661464 int sym_index;
13671465 int sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
1368− int n_trie = 0;
1369− struct trie *trie = NULL;
1466+ int n_trie = 0, n_head = 0;
1467+ struct trie_info *trie = NULL, *p_trie;
1468+ struct trie_node node, *p_node;
1469+ struct trie_seq *seq = NULL;
13701470 addr_t vm_addr = get_segment(mo, 1)->vmaddr;
13711471
13721472 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
@@ -1383,59 +1483,59 @@ static void export_trie(TCCState *s1, struct macho *mo) · 10 unmodified lines
13831483 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
13841484 flag |= EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
13851485 dprintf ("%s %d %llx\n", name, flag, addr + vm_addr);
1386− trie = tcc_realloc(trie, (n_trie + 1) * sizeof(struct trie));
1486+ trie = tcc_realloc(trie, (n_trie + 1) * sizeof(struct trie_info));
13871487 trie[n_trie].name = name;
13881488 trie[n_trie].flag = flag;
13891489 trie[n_trie].addr = addr;
1390− trie[n_trie].offset_size = 1;
13911490 trie[n_trie].str_size = strlen(name) + 1;
13921491 trie[n_trie].term_size = uleb128_size(flag) + uleb128_size(addr);
1393− trie[n_trie].term_offset = 0;
13941492 n_trie++;
13951493 }
13961494 }
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− }
14021495 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);
1496+ tcc_qsort(trie, n_trie, sizeof(struct trie_info), triecmp, NULL);
1497+ memset(&node, 0, sizeof(node));
1498+ create_trie(&node, 0, n_trie, 0, n_trie, trie);
1499+ create_seq(&offset, &n_head, &seq, &node, n_trie, trie);
1500+ save_offset = offset;
1501+ for (i = 0; i < n_head; i++) {
1502+ p_node = seq[i].node;
1503+ if (p_node->n_child == 0) {
1504+ p_trie = &trie[p_node->start];
1505+ seq[i].nest_offset = offset;
1506+ offset += 1 + p_trie->term_size + 1;
1507+ }
1508+ }
1509+ for (i = 0; i < n_head; i++) {
1510+ p_node = seq[i].node;
1511+ p_trie = &trie[p_node->start];
1512+ if (seq[i].n_child >= 0) {
1513+ section_ptr_add(mo->exports, seq[i].offset - mo->exports->data_offset);
1514+ ptr = section_ptr_add(mo->exports, 2);
1515+ *ptr++ = 0;
1516+ *ptr = seq[i].n_child;
1517+ }
1518+ size = p_node->index_end - p_node->index_start;
1519+ ptr = section_ptr_add(mo->exports, size + 1);
1520+ memcpy(ptr, &p_trie->name[p_node->index_start], size);
1521+ ptr[size] = 0;
1522+ write_uleb128(mo->exports, seq[i].nest_offset);
1523+ }
1524+ section_ptr_add(mo->exports, save_offset - mo->exports->data_offset);
1525+ for (i = 0; i < n_head; i++) {
1526+ p_node = seq[i].node;
1527+ if (p_node->n_child == 0) {
1528+ p_trie = &trie[p_node->start];
1529+ write_uleb128(mo->exports, p_trie->term_size);
1530+ write_uleb128(mo->exports, p_trie->flag);
1531+ write_uleb128(mo->exports, p_trie->addr);
1532+ ptr = section_ptr_add(mo->exports, 1);
1533+ *ptr = 0;
1534+ }
1535+ }
1536+ section_ptr_add(mo->exports, -mo->exports->data_offset & 7);
1537+ node_free(&node);
1538+ tcc_free(seq);
14391539 }
14401540 tcc_free(trie);
14411541 }

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