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