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
422 Section *symtab, *strtab, *wdata, *indirsyms, *stubs, *exports;422 Section *symtab, *strtab, *wdata, *indirsyms, *stubs, *exports;
423 uint32_t ilocal, iextdef, iundef;423 uint32_t ilocal, iextdef, iundef;
424 int stubsym, n_got, nr_plt;424 int stubsym, n_got, nr_plt;
425 int segment[sk_last];
425#ifdef CONFIG_NEW_MACHO426#ifdef CONFIG_NEW_MACHO
426 Section *chained_fixups;427 Section *chained_fixups;
427 int n_bind;428 int n_bind;
@@ -1189,14 +1190,14 @@ static void create_symtab(TCCState *s1, struct macho *mo) · 761 unmodified lines
1189}1190}
1190 1191
1191const struct {1192const struct {
1192 int seg;1193 int seg_initial;
1193 uint32_t flags;1194 uint32_t flags;
1194 const char *name;1195 const char *name;
1195} skinfo[sk_last] = {1196} skinfo[sk_last] = {
1196 /*[sk_unknown] =*/ { 0 },1197 /*[sk_unknown] =*/ { 0 },
1197 /*[sk_discard] =*/ { 0 },1198 /*[sk_discard] =*/ { 0 },
1198 /*[sk_text] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS1199 /*[sk_text] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS
1199 | S_ATTR_SOME_INSTRUCTIONS, "__text" },1200 | S_ATTR_SOME_INSTRUCTIONS, "__text" },
1200 /*[sk_stubs] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS | S_SYMBOL_STUBS1201 /*[sk_stubs] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS | S_SYMBOL_STUBS
1201 | S_ATTR_SOME_INSTRUCTIONS , "__stubs" },1202 | S_ATTR_SOME_INSTRUCTIONS , "__stubs" },
1202 /*[sk_stub_helper] =*/ { 1, S_REGULAR | S_ATTR_PURE_INSTRUCTIONS1203 /*[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
1570 struct dysymtab_command *dysymlc;1571 struct dysymtab_command *dysymlc;
1571 char *str;1572 char *str;
1572 1573
1574 for (sk = sk_unknown; sk < sk_last; sk++)
1575 mo->segment[sk] = skinfo[sk].seg_initial;
1576
1573 seg = add_segment(mo, "__PAGEZERO");1577 seg = add_segment(mo, "__PAGEZERO");
1574 seg->vmsize = (uint64_t)1 << 32;1578 seg->vmsize = (uint64_t)1 << 32;
1575 1579
@@ -1584,10 +1588,16 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 8 unmodified lines
1584 seg->initprot = 3; // rw-1588 seg->initprot = 3; // rw-
1585 seg->flags = SG_READ_ONLY;1589 seg->flags = SG_READ_ONLY;
1586 1590
1587 seg = add_segment(mo, "__DWARF");1591 if (dwarf_info_section) {
1588 seg->vmaddr = -1;1592 seg = add_segment(mo, "__DWARF");
1589 seg->maxprot = 7; // rwx1593 seg->vmaddr = -1;
1590 seg->initprot = 3; // rw-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]--;
1591 1601
1592 seg = add_segment(mo, "__DATA");1602 seg = add_segment(mo, "__DATA");
1593 seg->vmaddr = -1;1603 seg->vmaddr = -1;
@@ -1599,10 +1609,6 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 5 unmodified lines
1599 seg->maxprot = 1; // r--1609 seg->maxprot = 1; // r--
1600 seg->initprot = 1; // r--1610 seg->initprot = 1; // r--
1601 1611
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
1606#ifdef CONFIG_NEW_MACHO1612#ifdef CONFIG_NEW_MACHO
1607 chained_fixups_lc = add_lc(mo, LC_DYLD_CHAINED_FIXUPS,1613 chained_fixups_lc = add_lc(mo, LC_DYLD_CHAINED_FIXUPS,
1608 sizeof(struct linkedit_data_command));1614 sizeof(struct linkedit_data_command));
@@ -1714,15 +1720,8 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 105 unmodified lines
1714 for (sk = sk_unknown; sk < sk_last; sk++) {1720 for (sk = sk_unknown; sk < sk_last; sk++) {
1715 struct section_64 *sec = NULL;1721 struct section_64 *sec = NULL;
1716 if (seg) {1722 if (seg) {
1717#ifdef CONFIG_NEW_MACHO
1718 seg->vmsize = curaddr - seg->vmaddr;1723 seg->vmsize = curaddr - seg->vmaddr;
1719 seg->filesize = fileofs - seg->fileoff;1724 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
1726 }1725 }
1727#ifdef CONFIG_NEW_MACHO1726#ifdef CONFIG_NEW_MACHO
1728 if (sk == sk_linkedit) {1727 if (sk == sk_linkedit) {
@@ -1735,14 +1734,14 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 6 unmodified lines
1735 export_trie(s1, mo);1734 export_trie(s1, mo);
1736 }1735 }
1737#endif1736#endif
1738 if (skinfo[sk].seg && mo->sk_to_sect[sk].s) {1737 if (mo->segment[sk] && mo->sk_to_sect[sk].s) {
1739 uint64_t al = 0;1738 uint64_t al = 0;
1740 int si;1739 int si;
1741 seg = get_segment(mo, skinfo[sk].seg);1740 seg = get_segment(mo, mo->segment[sk]);
1742 if (skinfo[sk].name) {1741 if (skinfo[sk].name) {
1743 si = add_section(mo, &seg, skinfo[sk].name);1742 si = add_section(mo, &seg, skinfo[sk].name);
1744 numsec++;1743 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;
1746 mo->sk_to_sect[sk].machosect = si;1745 mo->sk_to_sect[sk].machosect = si;
1747 sec = get_section(seg, si);1746 sec = get_section(seg, si);
1748 sec->flags = skinfo[sk].flags;1747 sec->flags = skinfo[sk].flags;
@@ -1760,17 +1759,10 @@ static void collect_sections(TCCState *s1, struct macho *mo) · 11 unmodified lines
1760#endif1759#endif
1761 }1760 }
1762 if (seg->vmaddr == -1) {1761 if (seg->vmaddr == -1) {
1763#ifdef CONFIG_NEW_MACHO
1764 curaddr = (curaddr + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;1762 curaddr = (curaddr + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
1765 seg->vmaddr = curaddr;1763 seg->vmaddr = curaddr;
1766 fileofs = (fileofs + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;1764 fileofs = (fileofs + SEG_PAGE_SIZE - 1) & -SEG_PAGE_SIZE;
1767 seg->fileoff = fileofs;1765 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
1774 }1766 }
1775 1767
1776 for (s = mo->sk_to_sect[sk].s; s; s = s->prev) {1768 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
1913 1905
1914 for (sk = sk_unknown; sk < sk_last; sk++) {1906 for (sk = sk_unknown; sk < sk_last; sk++) {
1915 //struct segment_command_64 *seg;1907 //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)
1917 continue;1909 continue;
1918 /*seg =*/ get_segment(mo, skinfo[sk].seg);1910 /*seg =*/ get_segment(mo, mo->segment[sk]);
1919 for (s = mo->sk_to_sect[sk].s; s; s = s->prev) {1911 for (s = mo->sk_to_sect[sk].s; s; s = s->prev) {
1920 if (s->sh_type != SHT_NOBITS) {1912 if (s->sh_type != SHT_NOBITS) {
1921 while (fileofs < s->sh_offset)1913 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