| ... | @@ -1809,16 +1809,25 @@ fn resolveSymbols(self: *Zld) !void { | ... | @@ -1809,16 +1809,25 @@ fn resolveSymbols(self: *Zld) !void { |
| 1809 | if (self.symbol_resolver.getPtr("___dso_handle")) |resolv| blk: { | 1809 | if (self.symbol_resolver.getPtr("___dso_handle")) |resolv| blk: { |
| 1810 | if (resolv.where != .undef) break :blk; | 1810 | if (resolv.where != .undef) break :blk; |
| 1811 | | 1811 | |
| 1812 | const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; | | |
| 1813 | const undef = &self.undefs.items[resolv.where_index]; | 1812 | const undef = &self.undefs.items[resolv.where_index]; |
| 1814 | const global_sym_index = @intCast(u32, self.globals.items.len); | 1813 | const match: MatchingSection = .{ |
| 1815 | try self.globals.append(self.allocator, .{ | 1814 | .seg = self.text_segment_cmd_index.?, |
| | 1815 | .sect = self.text_section_index.?, |
| | 1816 | }; |
| | 1817 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| | 1818 | var nlist = macho.nlist_64{ |
| 1816 | .n_strx = undef.n_strx, | 1819 | .n_strx = undef.n_strx, |
| 1817 | .n_type = macho.N_PEXT | macho.N_EXT | macho.N_SECT, | 1820 | .n_type = macho.N_SECT, |
| 1818 | .n_sect = 0, | 1821 | .n_sect = self.sectionId(match), |
| 1819 | .n_desc = macho.N_WEAK_DEF, | 1822 | .n_desc = 0, |
| 1820 | .n_value = seg.inner.vmaddr, | 1823 | .n_value = 0, |
| 1821 | }); | 1824 | }; |
| | 1825 | try self.locals.append(self.allocator, nlist); |
| | 1826 | const global_sym_index = @intCast(u32, self.globals.items.len); |
| | 1827 | nlist.n_type |= macho.N_EXT; |
| | 1828 | nlist.n_desc = macho.N_WEAK_DEF; |
| | 1829 | try self.globals.append(self.allocator, nlist); |
| | 1830 | |
| 1822 | undef.* = .{ | 1831 | undef.* = .{ |
| 1823 | .n_strx = 0, | 1832 | .n_strx = 0, |
| 1824 | .n_type = macho.N_UNDF, | 1833 | .n_type = macho.N_UNDF, |
| ... | @@ -1829,7 +1838,28 @@ fn resolveSymbols(self: *Zld) !void { | ... | @@ -1829,7 +1838,28 @@ fn resolveSymbols(self: *Zld) !void { |
| 1829 | resolv.* = .{ | 1838 | resolv.* = .{ |
| 1830 | .where = .global, | 1839 | .where = .global, |
| 1831 | .where_index = global_sym_index, | 1840 | .where_index = global_sym_index, |
| | 1841 | .local_sym_index = local_sym_index, |
| 1832 | }; | 1842 | }; |
| | 1843 | |
| | 1844 | // We create an empty atom for this symbol. |
| | 1845 | // TODO perhaps we should special-case special symbols? Create a separate |
| | 1846 | // linked list of atoms? |
| | 1847 | const block = try self.allocator.create(TextBlock); |
| | 1848 | errdefer self.allocator.destroy(block); |
| | 1849 | |
| | 1850 | block.* = TextBlock.init(self.allocator); |
| | 1851 | block.local_sym_index = local_sym_index; |
| | 1852 | block.code = try self.allocator.alloc(u8, 0); |
| | 1853 | block.size = 0; |
| | 1854 | block.alignment = 0; |
| | 1855 | |
| | 1856 | if (self.blocks.getPtr(match)) |last| { |
| | 1857 | last.*.next = block; |
| | 1858 | block.prev = last.*; |
| | 1859 | last.* = block; |
| | 1860 | } else { |
| | 1861 | try self.blocks.putNoClobber(self.allocator, match, block); |
| | 1862 | } |
| 1833 | } | 1863 | } |
| 1834 | | 1864 | |
| 1835 | var has_undefined = false; | 1865 | var has_undefined = false; |