History

Better fix for old macho format

The last commit only worked on arm64. The problem was that the new __DWARF segment was empty. I now remove the __DWARF segment if it is not needed. This resolves the rebase problem on x86_64.

herman ten brugge committed 3 years ago

Commit 894e1661 parent 14e78d71 file · +21 −29Browse files

Unified · Split

tccmacho.c+21−29
@@ -422,6 +422,7 @@ struct macho { · 421 unmodified lines
422422 Section *symtab, *strtab, *wdata, *indirsyms, *stubs, *exports;
423423 uint32_t ilocal, iextdef, iundef;
424424 int stubsym, n_got, nr_plt;
425+ int segment[sk_last];
425426 #ifdef CONFIG_NEW_MACHO
426427 Section *chained_fixups;
427428 int n_bind;
@@ -1189,14 +1190,14 @@ static void create_symtab(TCCState *s1, struct macho *mo) · 761 unmodified lines
11891190 }
11901191
11911192 const struct {
1192− int seg;
1193+ int seg_initial;
11931194 uint32_t flags;
11941195 const char *name;
11951196 } skinfo[sk_last] = {
11961197 /*[sk_unknown] =*/ { 0 },
11971198 /*[sk_discard] =*/ { 0 },
11981199 /*[sk_text] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS
1199− | S_ATTR_SOME_INSTRUCTIONS, "__text" },
1200+ | S_ATTR_SOME_INSTRUCTIONS, "__text" },
12001201 /*[sk_stubs] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS | S_SYMBOL_STUBS
12011202 | S_ATTR_SOME_INSTRUCTIONS , "__stubs" },
12021203 /*[sk_stub_helper] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS
@@ -1570,6 +1571,9 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 367 unmodified lines
15701571 struct dysymtab_command *dysymlc;
15711572 char *str;
15721573
1574+ for (sk = sk_unknown; sk < sk_last; sk++)
1575+ mo->segment[sk] = skinfo[sk].seg_initial;
1576+
15731577 seg = add_segment(mo, "__PAGEZERO");
15741578 seg->vmsize = (uint64_t)1 << 32;
15751579
@@ -1584,10 +1588,16 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 8 unmodified lines
15841588 seg->initprot = 3; // rw-
15851589 seg->flags = SG_READ_ONLY;
15861590
1587− seg = add_segment(mo, "__DWARF");
1588− seg->vmaddr = -1;
1589− seg->maxprot = 7; // rwx
1590− seg->initprot = 3; // rw-
1591+ if (dwarf_info_section) {
1592+ seg = add_segment(mo, "__DWARF");
1593+ seg->vmaddr = -1;
1594+ seg->maxprot = 7; // rwx
1595+ seg->initprot = 3; // rw-
1596+ }
1597+ else
1598+ for (sk = sk_unknown; sk < sk_last; sk++)
1599+ if (mo->segment[sk] >= 4)
1600+ mo->segment[sk]--;
15911601
15921602 seg = add_segment(mo, "__DATA");
15931603 seg->vmaddr = -1;
@@ -1599,10 +1609,6 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 5 unmodified lines
15991609 seg->maxprot = 1; // r--
16001610 seg->initprot = 1; // r--
16011611
1602− /* trick to avoid __DWARF vmaddr = -1 */
1603− if (dwarf_info_section == NULL)
1604− dwarf_info_section = new_section(s1, ".debug_info", SHT_PROGBITS, 0);
1605−
16061612 #ifdef CONFIG_NEW_MACHO
16071613 chained_fixups_lc = add_lc(mo, LC_DYLD_CHAINED_FIXUPS,
16081614 sizeof(struct linkedit_data_command));
@@ -1714,15 +1720,8 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 105 unmodified lines
17141720 for (sk = sk_unknown; sk < sk_last; sk++) {
17151721 struct section_64 *sec = NULL;
17161722 if (seg) {
1717−#ifdef CONFIG_NEW_MACHO
17181723 seg->vmsize = curaddr - seg->vmaddr;
17191724 seg->filesize = fileofs - seg->fileoff;
1720−#else
1721− seg->vmsize = (curaddr - seg->vmaddr + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
1722− seg->filesize = (fileofs - seg->fileoff + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
1723− curaddr = seg->vmaddr + seg->vmsize;
1724− fileofs = seg->fileoff + seg->filesize;
1725−#endif
17261725 }
17271726 #ifdef CONFIG_NEW_MACHO
17281727 if (sk == sk_linkedit) {
@@ -1735,14 +1734,14 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 6 unmodified lines
17351734 export_trie(s1, mo);
17361735 }
17371736 #endif
1738− if (skinfo[sk].seg && mo->sk_to_sect[sk].s) {
1737+ if (mo->segment[sk] && mo->sk_to_sect[sk].s) {
17391738 uint64_t al = 0;
17401739 int si;
1741− seg = get_segment(mo, skinfo[sk].seg);
1740+ seg = get_segment(mo, mo->segment[sk]);
17421741 if (skinfo[sk].name) {
17431742 si = add_section(mo, &seg, skinfo[sk].name);
17441743 numsec++;
1745− mo->lc[mo->seg2lc[skinfo[sk].seg]] = (struct load_command*)seg;
1744+ mo->lc[mo->seg2lc[mo->segment[sk]]] = (struct load_command*)seg;
17461745 mo->sk_to_sect[sk].machosect = si;
17471746 sec = get_section(seg, si);
17481747 sec->flags = skinfo[sk].flags;
@@ -1760,17 +1759,10 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 11 unmodified lines
17601759 #endif
17611760 }
17621761 if (seg->vmaddr == -1) {
1763−#ifdef CONFIG_NEW_MACHO
17641762 curaddr = (curaddr + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
17651763 seg->vmaddr = curaddr;
17661764 fileofs = (fileofs + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
17671765 seg->fileoff = fileofs;
1768−#else
1769− curaddr = (curaddr + 4095) & -4096;
1770− seg->vmaddr = curaddr;
1771− fileofs = (fileofs + 4095) & -4096;
1772− seg->fileoff = fileofs;
1773−#endif
17741766 }
17751767
17761768 for (s = mo->sk_to_sect[sk].s; s; s = s->prev) {
@@ -1913,9 +1905,9 @@ static void macho_write(TCCState *s1, struct macho *mo, FILE *fp) · 136 unmodified lines
19131905
19141906 for (sk = sk_unknown; sk < sk_last; sk++) {
19151907 //struct segment_command_64 *seg;
1916− if (!skinfo[sk].seg || !mo->sk_to_sect[sk].s)
1908+ if (!mo->segment[sk] || !mo->sk_to_sect[sk].s)
19171909 continue;
1918− /*seg =*/ get_segment(mo, skinfo[sk].seg);
1910+ /*seg =*/ get_segment(mo, mo->segment[sk]);
19191911 for (s = mo->sk_to_sect[sk].s; s; s = s->prev) {
19201912 if (s->sh_type != SHT_NOBITS) {
19211913 while (fileofs < s->sh_offset)

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