| author | |
| committer | |
| log | db44a7803fac38096526e9b8baba60d966d77ab6 |
| tree | 5bb8d077642a1d871f9e08503265d896f56b9a66 |
| parent | b667fe2c62044ec56e05edd74f8ab3f080f813b1 |
3 files changed, 209 insertions(+), 215 deletions(-)
src/link/MachO/Object.zig+3-1| ... | ... | @@ -191,11 +191,13 @@ pub fn readLoadCommands(self: *Object, reader: anytype) !void { |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | pub fn parseSections(self: *Object) !void { |
| 194 | log.warn("parsing sections in {s}", .{self.name.?}); | |
| 194 | 195 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; |
| 195 | 196 | |
| 196 | 197 | try self.sections.ensureCapacity(self.allocator, seg.sections.items.len); |
| 197 | 198 | |
| 198 | 199 | for (seg.sections.items) |sect| { |
| 200 | log.warn("parsing section '{s},{s}'", .{ parseName(&sect.segname), parseName(&sect.sectname) }); | |
| 199 | 201 | // Read sections' code |
| 200 | 202 | var code = try self.allocator.alloc(u8, sect.size); |
| 201 | 203 | _ = try self.file.?.preadAll(code, sect.offset); |
| ... | ... | @@ -215,7 +217,7 @@ pub fn parseSections(self: *Object) !void { |
| 215 | 217 | |
| 216 | 218 | break :relocs try reloc.parse( |
| 217 | 219 | self.allocator, |
| 218 | &section.code, | |
| 220 | section.code, | |
| 219 | 221 | mem.bytesAsSlice(macho.relocation_info, raw_relocs), |
| 220 | 222 | ); |
| 221 | 223 | } else null; |
src/link/MachO/Zld.zig+125-146| ... | ... | @@ -16,6 +16,7 @@ const Allocator = mem.Allocator; |
| 16 | 16 | const Archive = @import("Archive.zig"); |
| 17 | 17 | const CodeSignature = @import("CodeSignature.zig"); |
| 18 | 18 | const Object = @import("Object.zig"); |
| 19 | const Relocation = @import("reloc.zig").Relocation; | |
| 19 | 20 | const Symbol = @import("Symbol.zig"); |
| 20 | 21 | const Trie = @import("Trie.zig"); |
| 21 | 22 | |
| ... | ... | @@ -77,7 +78,7 @@ symtab: std.StringArrayHashMapUnmanaged(Symbol) = .{}, |
| 77 | 78 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 78 | 79 | |
| 79 | 80 | threadlocal_offsets: std.ArrayListUnmanaged(u64) = .{}, |
| 80 | rebases: std.ArrayListUnmanaged(Pointer) = .{}, | |
| 81 | local_rebases: std.ArrayListUnmanaged(Pointer) = .{}, | |
| 81 | 82 | stubs: std.StringArrayHashMapUnmanaged(u32) = .{}, |
| 82 | 83 | got_entries: std.StringArrayHashMapUnmanaged(GotEntry) = .{}, |
| 83 | 84 | |
| ... | ... | @@ -186,7 +187,7 @@ pub fn init(allocator: *Allocator) Zld { |
| 186 | 187 | |
| 187 | 188 | pub fn deinit(self: *Zld) void { |
| 188 | 189 | self.threadlocal_offsets.deinit(self.allocator); |
| 189 | self.rebases.deinit(self.allocator); | |
| 190 | self.local_rebases.deinit(self.allocator); | |
| 190 | 191 | |
| 191 | 192 | for (self.stubs.items()) |entry| { |
| 192 | 193 | self.allocator.free(entry.key); |
| ... | ... | @@ -280,9 +281,8 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8) !void { |
| 280 | 281 | self.allocateLinkeditSegment(); |
| 281 | 282 | try self.allocateSymbols(); |
| 282 | 283 | try self.allocateStubsAndGotEntries(); |
| 283 | self.printDebug(); | |
| 284 | // try self.writeStubHelperCommon(); | |
| 285 | // try self.resolveRelocsAndWriteSections(); | |
| 284 | try self.writeStubHelperCommon(); | |
| 285 | try self.resolveRelocsAndWriteSections(); | |
| 286 | 286 | // try self.flush(); |
| 287 | 287 | } |
| 288 | 288 | |
| ... | ... | @@ -1051,7 +1051,7 @@ fn writeStubHelperCommon(self: *Zld) !void { |
| 1051 | 1051 | code[9] = 0xff; |
| 1052 | 1052 | code[10] = 0x25; |
| 1053 | 1053 | { |
| 1054 | const dyld_stub_binder = self.nonlazy_imports.get("dyld_stub_binder").?; | |
| 1054 | const dyld_stub_binder = self.got_entries.get("dyld_stub_binder").?; | |
| 1055 | 1055 | const addr = (got.addr + dyld_stub_binder.index * @sizeOf(u64)); |
| 1056 | 1056 | const displacement = try math.cast(u32, addr - stub_helper.addr - code_size); |
| 1057 | 1057 | mem.writeIntLittle(u32, code[11..], displacement); |
| ... | ... | @@ -1095,7 +1095,7 @@ fn writeStubHelperCommon(self: *Zld) !void { |
| 1095 | 1095 | code[10] = 0xbf; |
| 1096 | 1096 | code[11] = 0xa9; |
| 1097 | 1097 | binder_blk_outer: { |
| 1098 | const dyld_stub_binder = self.nonlazy_imports.get("dyld_stub_binder").?; | |
| 1098 | const dyld_stub_binder = self.got_entries.get("dyld_stub_binder").?; | |
| 1099 | 1099 | const this_addr = stub_helper.addr + 3 * @sizeOf(u32); |
| 1100 | 1100 | const target_addr = (got.addr + dyld_stub_binder.index * @sizeOf(u64)); |
| 1101 | 1101 | binder_blk: { |
| ... | ... | @@ -1113,7 +1113,6 @@ fn writeStubHelperCommon(self: *Zld) !void { |
| 1113 | 1113 | const new_this_addr = this_addr + @sizeOf(u32); |
| 1114 | 1114 | const displacement = math.divExact(u64, target_addr - new_this_addr, 4) catch |_| break :binder_blk; |
| 1115 | 1115 | const literal = math.cast(u18, displacement) catch |_| break :binder_blk; |
| 1116 | log.debug("2: disp=0x{x}, literal=0x{x}", .{ displacement, literal }); | |
| 1117 | 1116 | // Pad with nop to please division. |
| 1118 | 1117 | // nop |
| 1119 | 1118 | mem.writeIntLittle(u32, code[12..16], aarch64.Instruction.nop().toU32()); |
| ... | ... | @@ -1149,8 +1148,8 @@ fn writeStubHelperCommon(self: *Zld) !void { |
| 1149 | 1148 | } |
| 1150 | 1149 | }; |
| 1151 | 1150 | |
| 1152 | for (self.lazy_imports.items()) |_, i| { | |
| 1153 | const index = @intCast(u32, i); | |
| 1151 | for (self.stubs.items()) |entry| { | |
| 1152 | const index = entry.value; | |
| 1154 | 1153 | try self.writeLazySymbolPointer(index); |
| 1155 | 1154 | try self.writeStub(index); |
| 1156 | 1155 | try self.writeStubInStubHelper(index); |
| ... | ... | @@ -1458,6 +1457,7 @@ fn resolveStubsAndGotEntries(self: *Zld) !void { |
| 1458 | 1457 | |
| 1459 | 1458 | if (self.got_entries.contains(sym_name)) continue; |
| 1460 | 1459 | |
| 1460 | // TODO clean this up | |
| 1461 | 1461 | const is_import = self.symtab.get(sym_name).?.tag == .Import; |
| 1462 | 1462 | var name = try self.allocator.dupe(u8, sym_name); |
| 1463 | 1463 | const index = @intCast(u32, self.got_entries.items().len); |
| ... | ... | @@ -1509,8 +1509,7 @@ fn resolveStubsAndGotEntries(self: *Zld) !void { |
| 1509 | 1509 | |
| 1510 | 1510 | fn resolveRelocsAndWriteSections(self: *Zld) !void { |
| 1511 | 1511 | for (self.objects.items) |object, object_id| { |
| 1512 | log.debug("\n\n", .{}); | |
| 1513 | log.debug("relocating object {s}", .{object.name}); | |
| 1512 | log.warn("relocating object {s}", .{object.name}); | |
| 1514 | 1513 | |
| 1515 | 1514 | for (object.sections.items) |sect, source_sect_id| { |
| 1516 | 1515 | const segname = parseName(&sect.inner.segname); |
| ... | ... | @@ -1529,78 +1528,86 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void { |
| 1529 | 1528 | const target_sect_addr = target_sect.addr + target_mapping.offset; |
| 1530 | 1529 | const target_sect_off = target_sect.offset + target_mapping.offset; |
| 1531 | 1530 | |
| 1532 | for (sect.relocs) |reloc| { | |
| 1533 | const source_addr = target_sect_addr + reloc.offset; | |
| 1531 | if (sect.relocs) |relocs| { | |
| 1532 | for (relocs) |rel| { | |
| 1533 | const source_addr = target_sect_addr + rel.offset; | |
| 1534 | 1534 | |
| 1535 | var args: Relocation.ResolveArgs = .{ | |
| 1536 | .source_addr = source_addr, | |
| 1537 | .target_addr = undefined, | |
| 1538 | }; | |
| 1535 | var args: Relocation.ResolveArgs = .{ | |
| 1536 | .source_addr = source_addr, | |
| 1537 | .target_addr = undefined, | |
| 1538 | .subtractor = null, | |
| 1539 | }; | |
| 1539 | 1540 | |
| 1540 | if (reloc.cast(Relocation.Unsigned)) |unsigned| { | |
| 1541 | // TODO resolve target addr | |
| 1541 | switch (rel.@"type") { | |
| 1542 | .unsigned => { | |
| 1543 | args.target_addr = try self.relocTargetAddr(@intCast(u16, object_id), rel.target); | |
| 1542 | 1544 | |
| 1543 | if (unsigned.subtractor) |subtractor| { | |
| 1544 | args.subtractor = undefined; // TODO resolve | |
| 1545 | } | |
| 1545 | const unsigned = rel.cast(Relocation.Unsigned) orelse unreachable; | |
| 1546 | if (unsigned.subtractor) |subtractor| { | |
| 1547 | args.subtractor = try self.relocTargetAddr(@intCast(u16, object_id), subtractor); | |
| 1548 | } | |
| 1546 | 1549 | |
| 1547 | rebases: { | |
| 1548 | var hit: bool = false; | |
| 1549 | if (target_mapping.target_seg_id == self.data_segment_cmd_index.?) { | |
| 1550 | if (self.data_section_index) |index| { | |
| 1551 | if (index == target_mapping.target_sect_id) hit = true; | |
| 1550 | rebases: { | |
| 1551 | var hit: bool = false; | |
| 1552 | if (target_mapping.target_seg_id == self.data_segment_cmd_index.?) { | |
| 1553 | if (self.data_section_index) |index| { | |
| 1554 | if (index == target_mapping.target_sect_id) hit = true; | |
| 1555 | } | |
| 1556 | } | |
| 1557 | if (target_mapping.target_seg_id == self.data_const_segment_cmd_index.?) { | |
| 1558 | if (self.data_const_section_index) |index| { | |
| 1559 | if (index == target_mapping.target_sect_id) hit = true; | |
| 1560 | } | |
| 1561 | } | |
| 1562 | ||
| 1563 | if (!hit) break :rebases; | |
| 1564 | ||
| 1565 | try self.local_rebases.append(self.allocator, .{ | |
| 1566 | .offset = source_addr - target_seg.inner.vmaddr, | |
| 1567 | .segment_id = target_mapping.target_seg_id, | |
| 1568 | }); | |
| 1552 | 1569 | } |
| 1553 | } | |
| 1554 | if (target_mapping.target_seg_id == self.data_const_segment_cmd_index.?) { | |
| 1555 | if (self.data_const_section_index) |index| { | |
| 1556 | if (index == target_mapping.target_sect_id) hit = true; | |
| 1570 | // TLV is handled via a separate offset mechanism. | |
| 1571 | // Calculate the offset to the initializer. | |
| 1572 | if (target_sect.flags == macho.S_THREAD_LOCAL_VARIABLES) tlv: { | |
| 1573 | const sym = object.symtab.items[rel.target.symbol]; | |
| 1574 | const sym_name = object.getString(sym.inner.n_strx); | |
| 1575 | ||
| 1576 | // TODO we don't want to save offset to tlv_bootstrap | |
| 1577 | if (mem.eql(u8, sym_name, "__tlv_boostrap")) break :tlv; | |
| 1578 | ||
| 1579 | const base_addr = blk: { | |
| 1580 | if (self.tlv_data_section_index) |index| { | |
| 1581 | const tlv_data = target_seg.sections.items[index]; | |
| 1582 | break :blk tlv_data.addr; | |
| 1583 | } else { | |
| 1584 | const tlv_bss = target_seg.sections.items[self.tlv_bss_section_index.?]; | |
| 1585 | break :blk tlv_bss.addr; | |
| 1586 | } | |
| 1587 | }; | |
| 1588 | // Since we require TLV data to always preceed TLV bss section, we calculate | |
| 1589 | // offsets wrt to the former if it is defined; otherwise, wrt to the latter. | |
| 1590 | try self.threadlocal_offsets.append(self.allocator, args.target_addr - base_addr); | |
| 1557 | 1591 | } |
| 1558 | } | |
| 1559 | ||
| 1560 | if (!hit) break :rebases; | |
| 1561 | ||
| 1562 | try self.local_rebases.append(self.allocator, .{ | |
| 1563 | .offset = source_addr - target_seg.inner.vmaddr, | |
| 1564 | .segment_id = target_mapping.target_seg_id, | |
| 1565 | }); | |
| 1592 | }, | |
| 1593 | .got_page, .got_page_off => { | |
| 1594 | const dc_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; | |
| 1595 | const got = dc_seg.sections.items[self.got_section_index.?]; | |
| 1596 | const sym = object.symtab.items[rel.target.symbol]; | |
| 1597 | const sym_name = object.getString(sym.inner.n_strx); | |
| 1598 | const entry = self.got_entries.get(sym_name) orelse unreachable; | |
| 1599 | args.target_addr = got.addr + entry.index * @sizeOf(u64); | |
| 1600 | }, | |
| 1601 | else => { | |
| 1602 | args.target_addr = try self.relocTargetAddr(@intCast(u16, object_id), rel.target); | |
| 1603 | }, | |
| 1566 | 1604 | } |
| 1567 | // TLV is handled via a separate offset mechanism. | |
| 1568 | // Calculate the offset to the initializer. | |
| 1569 | if (target_sect.flags == macho.S_THREAD_LOCAL_VARIABLES) tlv: { | |
| 1570 | const sym = object.symtab.items[reloc.target.symbol]; | |
| 1571 | const sym_name = object.getString(sym.inner.n_strx); | |
| 1572 | ||
| 1573 | // TODO we don't want to save offset to tlv_bootstrap | |
| 1574 | if (mem.eql(u8, sym_name, "__tlv_boostrap")) break :tlv; | |
| 1575 | 1605 | |
| 1576 | const base_addr = blk: { | |
| 1577 | if (self.tlv_data_section_index) |index| { | |
| 1578 | const tlv_data = target_seg.sections.items[index]; | |
| 1579 | break :blk tlv_data.addr; | |
| 1580 | } else { | |
| 1581 | const tlv_bss = target_seg.sections.items[self.tlv_bss_section_index.?]; | |
| 1582 | break :blk tlv_bss.addr; | |
| 1583 | } | |
| 1584 | }; | |
| 1585 | // Since we require TLV data to always preceed TLV bss section, we calculate | |
| 1586 | // offsets wrt to the former if it is defined; otherwise, wrt to the latter. | |
| 1587 | try self.threadlocal_offsets.append(self.allocator, target_addr - base_addr); | |
| 1588 | } | |
| 1589 | } else if (reloc.cast(Relocation.GotPageOff)) |page_off| { | |
| 1590 | // TODO here we need to work out the indirection to GOT. | |
| 1591 | } else { | |
| 1592 | // TODO resolve target addr. | |
| 1606 | try rel.resolve(args); | |
| 1593 | 1607 | } |
| 1594 | ||
| 1595 | log.debug("{s}", .{reloc.@"type"}); | |
| 1596 | log.debug(" | offset 0x{x}", .{reloc.offset}); | |
| 1597 | log.debug(" | source address 0x{x}", .{args.source_addr}); | |
| 1598 | log.debug(" | target address 0x{x}", .{args.target_addr}); | |
| 1599 | ||
| 1600 | try reloc.resolve(args); | |
| 1601 | 1608 | } |
| 1602 | 1609 | |
| 1603 | log.debug("writing contents of '{s},{s}' section from '{s}' from 0x{x} to 0x{x}", .{ | |
| 1610 | log.warn("writing contents of '{s},{s}' section from '{s}' from 0x{x} to 0x{x}", .{ | |
| 1604 | 1611 | segname, |
| 1605 | 1612 | sectname, |
| 1606 | 1613 | object.name, |
| ... | ... | @@ -1612,7 +1619,7 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void { |
| 1612 | 1619 | target_sect.flags == macho.S_THREAD_LOCAL_ZEROFILL or |
| 1613 | 1620 | target_sect.flags == macho.S_THREAD_LOCAL_VARIABLES) |
| 1614 | 1621 | { |
| 1615 | log.debug("zeroing out '{s},{s}' from 0x{x} to 0x{x}", .{ | |
| 1622 | log.warn("zeroing out '{s},{s}' from 0x{x} to 0x{x}", .{ | |
| 1616 | 1623 | parseName(&target_sect.segname), |
| 1617 | 1624 | parseName(&target_sect.sectname), |
| 1618 | 1625 | target_sect_off, |
| ... | ... | @@ -1630,88 +1637,60 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void { |
| 1630 | 1637 | } |
| 1631 | 1638 | } |
| 1632 | 1639 | |
| 1633 | fn relocTargetAddr(self: *Zld, object_id: u16, rel: macho.relocation_info) !u64 { | |
| 1640 | fn relocTargetAddr(self: *Zld, object_id: u16, target: Relocation.Target) !u64 { | |
| 1634 | 1641 | const object = self.objects.items[object_id]; |
| 1635 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; | |
| 1642 | const target_addr = blk: { | |
| 1643 | switch (target) { | |
| 1644 | .symbol => |sym_id| { | |
| 1645 | const sym = object.symtab.items[sym_id]; | |
| 1646 | const sym_name = object.getString(sym.inner.n_strx); | |
| 1636 | 1647 | |
| 1637 | const is_got: bool = is_got: { | |
| 1638 | switch (self.arch.?) { | |
| 1639 | .x86_64 => { | |
| 1640 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); | |
| 1641 | break :is_got = switch (rel_type) { | |
| 1642 | .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true, | |
| 1643 | else => false, | |
| 1644 | }; | |
| 1645 | }, | |
| 1646 | .aarch64 => { | |
| 1647 | const rel_type = @intToEnum(macho.reloc_type_aarch64, rel.r_type); | |
| 1648 | break :is_got = switch (rel_type) { | |
| 1649 | .ARM64_RELOC_GOT_LOAD_PAGE21, | |
| 1650 | .ARM64_RELOC_GOT_LOAD_PAGEOFF12, | |
| 1651 | .ARM64_RELOC_POINTER_TO_GOT, | |
| 1652 | => true, | |
| 1653 | else => false, | |
| 1654 | }; | |
| 1648 | switch (sym.tag) { | |
| 1649 | .Stab => unreachable, // TODO is this even allowed to happen? | |
| 1650 | .Local, .Weak, .Strong => { | |
| 1651 | // Relocate using section offsets only. | |
| 1652 | const target_mapping = self.mappings.get(.{ | |
| 1653 | .object_id = object_id, | |
| 1654 | .source_sect_id = sym.inner.n_sect - 1, | |
| 1655 | }) orelse unreachable; | |
| 1656 | const source_sect = object.sections.items[target_mapping.source_sect_id]; | |
| 1657 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; | |
| 1658 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; | |
| 1659 | const target_sect_addr = target_sect.addr + target_mapping.offset; | |
| 1660 | log.warn(" | symbol local to object", .{}); | |
| 1661 | break :blk target_sect_addr + sym.inner.n_value - source_sect.inner.addr; | |
| 1662 | }, | |
| 1663 | else => { | |
| 1664 | if (self.stubs.get(sym_name)) |index| { | |
| 1665 | log.warn(" | symbol stub {s}", .{sym_name}); | |
| 1666 | const segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; | |
| 1667 | const stubs = segment.sections.items[self.stubs_section_index.?]; | |
| 1668 | break :blk stubs.addr + index * stubs.reserved2; | |
| 1669 | } else if (mem.eql(u8, sym_name, "__tlv_bootstrap")) { | |
| 1670 | const segment = self.load_commands.items[self.data_segment_cmd_index.?].Segment; | |
| 1671 | const tlv = segment.sections.items[self.tlv_section_index.?]; | |
| 1672 | break :blk tlv.addr; | |
| 1673 | } else { | |
| 1674 | const global = self.symtab.get(sym_name) orelse { | |
| 1675 | log.err("failed to resolve symbol '{s}' as a relocation target", .{sym_name}); | |
| 1676 | return error.FailedToResolveRelocationTarget; | |
| 1677 | }; | |
| 1678 | break :blk global.inner.n_value; | |
| 1679 | } | |
| 1680 | }, | |
| 1681 | } | |
| 1655 | 1682 | }, |
| 1656 | } | |
| 1657 | }; | |
| 1658 | ||
| 1659 | const target_addr = blk: { | |
| 1660 | if (rel.r_extern == 1) { | |
| 1661 | const sym = object.symtab.items[rel.r_symbolnum]; | |
| 1662 | if (sym.isSect()) { | |
| 1663 | // Relocate using section offsets only. | |
| 1683 | .section => |sect_id| { | |
| 1664 | 1684 | const target_mapping = self.mappings.get(.{ |
| 1665 | 1685 | .object_id = object_id, |
| 1666 | .source_sect_id = sym.n_sect - 1, | |
| 1686 | .source_sect_id = sect_id, | |
| 1667 | 1687 | }) orelse unreachable; |
| 1668 | const source_sect = seg.sections.items[target_mapping.source_sect_id]; | |
| 1669 | 1688 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; |
| 1670 | 1689 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; |
| 1671 | const target_sect_addr = target_sect.addr + target_mapping.offset; | |
| 1672 | log.debug(" | symbol local to object", .{}); | |
| 1673 | break :blk target_sect_addr + sym.n_value - source_sect.addr; | |
| 1674 | } else if (sym.isUndf()) { | |
| 1675 | // Relocate to either the global symbol, or an import from | |
| 1676 | // shared library. | |
| 1677 | const sym_name = object.getString(sym.n_strx); | |
| 1678 | if (self.globals.get(sym_name)) |glob| { | |
| 1679 | break :blk glob.inner.n_value; | |
| 1680 | } else if (self.externs.getEntry(sym_name)) |ext| { | |
| 1681 | const segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; | |
| 1682 | const stubs = segment.sections.items[self.stubs_section_index.?]; | |
| 1683 | break :blk stubs.addr + ext.index * stubs.reserved2; | |
| 1684 | } else if (self.nonlazy_imports.get(sym_name)) |ext| { | |
| 1685 | const segment = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; | |
| 1686 | const got = segment.sections.items[self.got_section_index.?]; | |
| 1687 | break :blk got.addr + ext.index * @sizeOf(u64); | |
| 1688 | } else if (mem.eql(u8, sym_name, "__tlv_bootstrap")) { | |
| 1689 | const segment = self.load_commands.items[self.data_segment_cmd_index.?].Segment; | |
| 1690 | const tlv = segment.sections.items[self.tlv_section_index.?]; | |
| 1691 | break :blk tlv.addr; | |
| 1692 | } else { | |
| 1693 | log.err("failed to resolve symbol '{s}' as a relocation target", .{sym_name}); | |
| 1694 | return error.FailedToResolveRelocationTarget; | |
| 1695 | } | |
| 1696 | } else { | |
| 1697 | log.err("unexpected symbol {}, {s}", .{ sym, object.getString(sym.n_strx) }); | |
| 1698 | return error.UnexpectedSymbolWhenRelocating; | |
| 1699 | } | |
| 1700 | } else { | |
| 1701 | // TODO I think we need to reparse the relocation_info as scattered_relocation_info | |
| 1702 | // here to get the actual section plus offset into that section of the relocated | |
| 1703 | // symbol. Unless the fine-grained location is encoded within the cell in the code | |
| 1704 | // buffer? | |
| 1705 | const target_mapping = self.mappings.get(.{ | |
| 1706 | .object_id = object_id, | |
| 1707 | .source_sect_id = @intCast(u16, rel.r_symbolnum - 1), | |
| 1708 | }) orelse unreachable; | |
| 1709 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; | |
| 1710 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; | |
| 1711 | break :blk target_sect.addr + target_mapping.offset; | |
| 1690 | break :blk target_sect.addr + target_mapping.offset; | |
| 1691 | }, | |
| 1712 | 1692 | } |
| 1713 | 1693 | }; |
| 1714 | ||
| 1715 | 1694 | return target_addr; |
| 1716 | 1695 | } |
| 1717 | 1696 |
src/link/MachO/reloc.zig+81-68| ... | ... | @@ -3,6 +3,7 @@ const aarch64 = @import("../../codegen/aarch64.zig"); |
| 3 | 3 | const assert = std.debug.assert; |
| 4 | 4 | const log = std.log.scoped(.reloc); |
| 5 | 5 | const macho = std.macho; |
| 6 | const math = std.math; | |
| 6 | 7 | const mem = std.mem; |
| 7 | 8 | const meta = std.meta; |
| 8 | 9 | |
| ... | ... | @@ -10,7 +11,7 @@ const Allocator = mem.Allocator; |
| 10 | 11 | |
| 11 | 12 | pub const Relocation = struct { |
| 12 | 13 | @"type": Type, |
| 13 | code: *[]u8, | |
| 14 | code: []u8, | |
| 14 | 15 | offset: u32, |
| 15 | 16 | target: Target, |
| 16 | 17 | |
| ... | ... | @@ -24,20 +25,26 @@ pub const Relocation = struct { |
| 24 | 25 | pub const ResolveArgs = struct { |
| 25 | 26 | source_addr: u64, |
| 26 | 27 | target_addr: u64, |
| 27 | subtractor: i64 = undefined, | |
| 28 | subtractor: ?u64, | |
| 28 | 29 | }; |
| 29 | 30 | |
| 30 | 31 | pub fn resolve(base: *Relocation, args: ResolveArgs) !void { |
| 31 | switch (base.@"type") { | |
| 32 | .branch => try base.cast(Branch).?.resolve(args.source_addr, args.target_addr), | |
| 33 | .unsigned => try base.cast(Unsigned).?.resolve(args.target_addr, args.subtractor), | |
| 34 | .page => try base.cast(Page).?.resolve(args.source_addr, args.target_addr), | |
| 35 | .page_off => try base.cast(PageOff).?.resolve(args.target_addr), | |
| 36 | .got_page => try base.cast(GotPage).?.resolve(args.source_addr, args.target_addr), | |
| 37 | .got_page_off => try base.cast(GotPageOff).?.resolve(args.target_addr), | |
| 38 | .tlvp_page => try base.cast(TlvpPage).?.resolve(args.source_addr, args.target_addr), | |
| 39 | .tlvp_page_off => try base.cast(TlvpPageOff).?.resolve(args.target_addr), | |
| 40 | } | |
| 32 | log.warn("{s}", .{base.@"type"}); | |
| 33 | log.warn(" | offset 0x{x}", .{base.offset}); | |
| 34 | log.warn(" | source address 0x{x}", .{args.source_addr}); | |
| 35 | log.warn(" | target address 0x{x}", .{args.target_addr}); | |
| 36 | log.warn(" | subtractor address 0x{x}", .{args.subtractor}); | |
| 37 | ||
| 38 | return switch (base.@"type") { | |
| 39 | .branch => @fieldParentPtr(Branch, "base", base).resolve(args.source_addr, args.target_addr), | |
| 40 | .unsigned => @fieldParentPtr(Unsigned, "base", base).resolve(args.target_addr, args.subtractor), | |
| 41 | .page => @fieldParentPtr(Page, "base", base).resolve(args.source_addr, args.target_addr), | |
| 42 | .page_off => @fieldParentPtr(PageOff, "base", base).resolve(args.target_addr), | |
| 43 | .got_page => @fieldParentPtr(GotPage, "base", base).resolve(args.source_addr, args.target_addr), | |
| 44 | .got_page_off => @fieldParentPtr(GotPageOff, "base", base).resolve(args.target_addr), | |
| 45 | .tlvp_page => @fieldParentPtr(TlvpPage, "base", base).resolve(args.source_addr, args.target_addr), | |
| 46 | .tlvp_page_off => @fieldParentPtr(TlvpPageOff, "base", base).resolve(args.target_addr), | |
| 47 | }; | |
| 41 | 48 | } |
| 42 | 49 | |
| 43 | 50 | pub const Type = enum { |
| ... | ... | @@ -73,9 +80,12 @@ pub const Relocation = struct { |
| 73 | 80 | |
| 74 | 81 | pub fn resolve(branch: Branch, source_addr: u64, target_addr: u64) !void { |
| 75 | 82 | const displacement = try math.cast(i28, @intCast(i64, target_addr) - @intCast(i64, source_addr)); |
| 83 | ||
| 84 | log.warn(" | displacement 0x{x}", .{displacement}); | |
| 85 | ||
| 76 | 86 | var inst = branch.inst; |
| 77 | inst.AddSubtractImmediate.imm26 = @truncate(u26, @bitCast(u28, displacement) >> 2); | |
| 78 | mem.writeIntLittle(u32, branch.base.code.*[branch.base.offset..], inst.toU32()); | |
| 87 | inst.UnconditionalBranchImmediate.imm26 = @truncate(u26, @bitCast(u28, displacement) >> 2); | |
| 88 | mem.writeIntLittle(u32, branch.base.code[0..4], inst.toU32()); | |
| 79 | 89 | } |
| 80 | 90 | }; |
| 81 | 91 | |
| ... | ... | @@ -92,22 +102,25 @@ pub const Relocation = struct { |
| 92 | 102 | |
| 93 | 103 | pub const base_type: Relocation.Type = .unsigned; |
| 94 | 104 | |
| 95 | pub fn resolve(unsigned: Unsigned, target_addr: u64, subtractor: i64) !void { | |
| 96 | const result = @intCast(i64, target_addr) - subtractor + unsigned.addend; | |
| 105 | pub fn resolve(unsigned: Unsigned, target_addr: u64, subtractor: ?u64) !void { | |
| 106 | const result = if (subtractor) |sub| | |
| 107 | @intCast(i64, target_addr) - @intCast(i64, sub) + unsigned.addend | |
| 108 | else | |
| 109 | @intCast(i64, target_addr) + unsigned.addend; | |
| 97 | 110 | |
| 98 | log.debug(" | calculated addend 0x{x}", .{unsigned.addend}); | |
| 99 | log.debug(" | calculated unsigned value 0x{x}", .{result}); | |
| 111 | log.warn(" | calculated addend 0x{x}", .{unsigned.addend}); | |
| 112 | log.warn(" | calculated unsigned value 0x{x}", .{result}); | |
| 100 | 113 | |
| 101 | 114 | if (unsigned.is_64bit) { |
| 102 | 115 | mem.writeIntLittle( |
| 103 | 116 | u64, |
| 104 | unsigned.base.code.*[unsigned.base.offset..], | |
| 117 | unsigned.base.code[0..8], | |
| 105 | 118 | @bitCast(u64, result), |
| 106 | 119 | ); |
| 107 | 120 | } else { |
| 108 | 121 | mem.writeIntLittle( |
| 109 | 122 | u32, |
| 110 | unsigned.base.code.*[unsigned.base.offset..], | |
| 123 | unsigned.base.code[0..4], | |
| 111 | 124 | @truncate(u32, @bitCast(u64, result)), |
| 112 | 125 | ); |
| 113 | 126 | } |
| ... | ... | @@ -128,13 +141,14 @@ pub const Relocation = struct { |
| 128 | 141 | const target_page = @intCast(i32, ta >> 12); |
| 129 | 142 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 130 | 143 | |
| 131 | log.debug(" | moving by {} pages", .{pages}); | |
| 144 | log.warn(" | calculated addend 0x{x}", .{page.addend}); | |
| 145 | log.warn(" | moving by {} pages", .{pages}); | |
| 132 | 146 | |
| 133 | 147 | var inst = page.inst; |
| 134 | 148 | inst.PCRelativeAddress.immhi = @truncate(u19, pages >> 2); |
| 135 | 149 | inst.PCRelativeAddress.immlo = @truncate(u2, pages); |
| 136 | 150 | |
| 137 | mem.writeIntLittle(u32, page.base.code.*[page.base.offset..], inst.toU32()); | |
| 151 | mem.writeIntLittle(u32, page.base.code[0..4], inst.toU32()); | |
| 138 | 152 | } |
| 139 | 153 | }; |
| 140 | 154 | |
| ... | ... | @@ -155,8 +169,8 @@ pub const Relocation = struct { |
| 155 | 169 | const ta = if (page_off.addend) |a| target_addr + a else target_addr; |
| 156 | 170 | const narrowed = @truncate(u12, ta); |
| 157 | 171 | |
| 158 | log.debug(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 159 | log.debug(" | {s} opcode", .{page_off.op_kind}); | |
| 172 | log.warn(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 173 | log.warn(" | {s} opcode", .{page_off.op_kind}); | |
| 160 | 174 | |
| 161 | 175 | var inst = page_off.inst; |
| 162 | 176 | if (page_off.op_kind == .arithmetic) { |
| ... | ... | @@ -178,7 +192,7 @@ pub const Relocation = struct { |
| 178 | 192 | inst.LoadStoreRegister.offset = offset; |
| 179 | 193 | } |
| 180 | 194 | |
| 181 | mem.writeIntLittle(u32, page_off.base.code.*[page_off.base.offset..], inst.toU32()); | |
| 195 | mem.writeIntLittle(u32, page_off.base.code[0..4], inst.toU32()); | |
| 182 | 196 | } |
| 183 | 197 | }; |
| 184 | 198 | |
| ... | ... | @@ -194,13 +208,13 @@ pub const Relocation = struct { |
| 194 | 208 | const target_page = @intCast(i32, target_addr >> 12); |
| 195 | 209 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 196 | 210 | |
| 197 | log.debug(" | moving by {} pages", .{pages}); | |
| 211 | log.warn(" | moving by {} pages", .{pages}); | |
| 198 | 212 | |
| 199 | 213 | var inst = page.inst; |
| 200 | 214 | inst.PCRelativeAddress.immhi = @truncate(u19, pages >> 2); |
| 201 | 215 | inst.PCRelativeAddress.immlo = @truncate(u2, pages); |
| 202 | 216 | |
| 203 | mem.writeIntLittle(u32, page.base.code.*[page.base.offset..], inst.toU32()); | |
| 217 | mem.writeIntLittle(u32, page.base.code[0..4], inst.toU32()); | |
| 204 | 218 | } |
| 205 | 219 | }; |
| 206 | 220 | |
| ... | ... | @@ -214,13 +228,13 @@ pub const Relocation = struct { |
| 214 | 228 | pub fn resolve(page_off: GotPageOff, target_addr: u64) !void { |
| 215 | 229 | const narrowed = @truncate(u12, target_addr); |
| 216 | 230 | |
| 217 | log.debug(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 231 | log.warn(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 218 | 232 | |
| 219 | 233 | var inst = page_off.inst; |
| 220 | 234 | const offset = try math.divExact(u12, narrowed, 8); |
| 221 | 235 | inst.LoadStoreRegister.offset = offset; |
| 222 | 236 | |
| 223 | mem.writeIntLittle(u32, page_off.base.code.*[page_off.base.offset..], inst.toU32()); | |
| 237 | mem.writeIntLittle(u32, page_off.base.code[0..4], inst.toU32()); | |
| 224 | 238 | } |
| 225 | 239 | }; |
| 226 | 240 | |
| ... | ... | @@ -236,13 +250,13 @@ pub const Relocation = struct { |
| 236 | 250 | const target_page = @intCast(i32, target_addr >> 12); |
| 237 | 251 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 238 | 252 | |
| 239 | log.debug(" | moving by {} pages", .{pages}); | |
| 253 | log.warn(" | moving by {} pages", .{pages}); | |
| 240 | 254 | |
| 241 | 255 | var inst = page.inst; |
| 242 | 256 | inst.PCRelativeAddress.immhi = @truncate(u19, pages >> 2); |
| 243 | 257 | inst.PCRelativeAddress.immlo = @truncate(u2, pages); |
| 244 | 258 | |
| 245 | mem.writeIntLittle(u32, page.base.code.*[page.base.offset..], inst.toU32()); | |
| 259 | mem.writeIntLittle(u32, page.base.code[0..4], inst.toU32()); | |
| 246 | 260 | } |
| 247 | 261 | }; |
| 248 | 262 | |
| ... | ... | @@ -258,17 +272,17 @@ pub const Relocation = struct { |
| 258 | 272 | pub fn resolve(page_off: TlvpPageOff, target_addr: u64) !void { |
| 259 | 273 | const narrowed = @truncate(u12, target_addr); |
| 260 | 274 | |
| 261 | log.debug(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 275 | log.warn(" | narrowed address within the page 0x{x}", .{narrowed}); | |
| 262 | 276 | |
| 263 | 277 | var inst = page_off.inst; |
| 264 | 278 | inst.AddSubtractImmediate.imm12 = narrowed; |
| 265 | 279 | |
| 266 | mem.writeIntLittle(u32, page_off.base.code.*[page_off.base.offset..], inst.toU32()); | |
| 280 | mem.writeIntLittle(u32, page_off.base.code[0..4], inst.toU32()); | |
| 267 | 281 | } |
| 268 | 282 | }; |
| 269 | 283 | }; |
| 270 | 284 | |
| 271 | pub fn parse(allocator: *Allocator, code: *[]u8, relocs: []const macho.relocation_info) ![]*Relocation { | |
| 285 | pub fn parse(allocator: *Allocator, code: []u8, relocs: []const macho.relocation_info) ![]*Relocation { | |
| 272 | 286 | var it = RelocIterator{ |
| 273 | 287 | .buffer = relocs, |
| 274 | 288 | }; |
| ... | ... | @@ -280,8 +294,9 @@ pub fn parse(allocator: *Allocator, code: *[]u8, relocs: []const macho.relocatio |
| 280 | 294 | .parsed = std.ArrayList(*Relocation).init(allocator), |
| 281 | 295 | }; |
| 282 | 296 | defer parser.deinit(); |
| 297 | try parser.parse(); | |
| 283 | 298 | |
| 284 | return parser.parse(); | |
| 299 | return parser.parsed.toOwnedSlice(); | |
| 285 | 300 | } |
| 286 | 301 | |
| 287 | 302 | const RelocIterator = struct { |
| ... | ... | @@ -292,12 +307,12 @@ const RelocIterator = struct { |
| 292 | 307 | self.index += 1; |
| 293 | 308 | if (self.index < self.buffer.len) { |
| 294 | 309 | const reloc = self.buffer[@intCast(u64, self.index)]; |
| 295 | log.debug("{s}", .{@intToEnum(macho.reloc_type_arm64, reloc.r_type)}); | |
| 296 | log.debug(" | offset = {}", .{reloc.r_address}); | |
| 297 | log.debug(" | PC = {}", .{reloc.r_pcrel == 1}); | |
| 298 | log.debug(" | length = {}", .{reloc.r_length}); | |
| 299 | log.debug(" | symbolnum = {}", .{reloc.r_symbolnum}); | |
| 300 | log.debug(" | extern = {}", .{reloc.r_extern == 1}); | |
| 310 | log.warn("{s}", .{@intToEnum(macho.reloc_type_arm64, reloc.r_type)}); | |
| 311 | log.warn(" | offset = {}", .{reloc.r_address}); | |
| 312 | log.warn(" | PC = {}", .{reloc.r_pcrel == 1}); | |
| 313 | log.warn(" | length = {}", .{reloc.r_length}); | |
| 314 | log.warn(" | symbolnum = {}", .{reloc.r_symbolnum}); | |
| 315 | log.warn(" | extern = {}", .{reloc.r_extern == 1}); | |
| 301 | 316 | return reloc; |
| 302 | 317 | } |
| 303 | 318 | return null; |
| ... | ... | @@ -316,7 +331,7 @@ const RelocIterator = struct { |
| 316 | 331 | const Parser = struct { |
| 317 | 332 | allocator: *Allocator, |
| 318 | 333 | it: *RelocIterator, |
| 319 | code: *[]u8, | |
| 334 | code: []u8, | |
| 320 | 335 | parsed: std.ArrayList(*Relocation), |
| 321 | 336 | addend: ?u32 = null, |
| 322 | 337 | subtractor: ?Relocation.Target = null, |
| ... | ... | @@ -325,7 +340,7 @@ const Parser = struct { |
| 325 | 340 | parser.parsed.deinit(); |
| 326 | 341 | } |
| 327 | 342 | |
| 328 | fn parse(parser: *Parser) ![]*Relocation { | |
| 343 | fn parse(parser: *Parser) !void { | |
| 329 | 344 | while (parser.it.next()) |reloc| { |
| 330 | 345 | switch (@intToEnum(macho.reloc_type_arm64, reloc.r_type)) { |
| 331 | 346 | .ARM64_RELOC_BRANCH26 => { |
| ... | ... | @@ -360,8 +375,6 @@ const Parser = struct { |
| 360 | 375 | }, |
| 361 | 376 | } |
| 362 | 377 | } |
| 363 | ||
| 364 | return parser.parsed.toOwnedSlice(); | |
| 365 | 378 | } |
| 366 | 379 | |
| 367 | 380 | fn parseAddend(parser: *Parser, reloc: macho.relocation_info) !void { |
| ... | ... | @@ -395,7 +408,7 @@ const Parser = struct { |
| 395 | 408 | assert(reloc.r_length == 2); |
| 396 | 409 | |
| 397 | 410 | const offset = @intCast(u32, reloc.r_address); |
| 398 | const inst = parser.code.*[offset..][0..4]; | |
| 411 | const inst = parser.code[offset..][0..4]; | |
| 399 | 412 | const parsed_inst = aarch64.Instruction{ .UnconditionalBranchImmediate = mem.bytesToValue( |
| 400 | 413 | meta.TagPayload( |
| 401 | 414 | aarch64.Instruction, |
| ... | ... | @@ -412,14 +425,14 @@ const Parser = struct { |
| 412 | 425 | branch.* = .{ |
| 413 | 426 | .base = .{ |
| 414 | 427 | .@"type" = .branch, |
| 415 | .code = parser.code, | |
| 428 | .code = inst, | |
| 416 | 429 | .offset = @intCast(u32, reloc.r_address), |
| 417 | 430 | .target = target, |
| 418 | 431 | }, |
| 419 | 432 | .inst = parsed_inst, |
| 420 | 433 | }; |
| 421 | 434 | |
| 422 | log.debug(" | emitting {}", .{branch}); | |
| 435 | log.warn(" | emitting {}", .{branch}); | |
| 423 | 436 | try parser.parsed.append(&branch.base); |
| 424 | 437 | } |
| 425 | 438 | |
| ... | ... | @@ -431,7 +444,7 @@ const Parser = struct { |
| 431 | 444 | const target = Relocation.Target.from_reloc(reloc); |
| 432 | 445 | |
| 433 | 446 | const offset = @intCast(u32, reloc.r_address); |
| 434 | const inst = parser.code.*[offset..][0..4]; | |
| 447 | const inst = parser.code[offset..][0..4]; | |
| 435 | 448 | const parsed_inst = aarch64.Instruction{ .PCRelativeAddress = mem.bytesToValue(meta.TagPayload( |
| 436 | 449 | aarch64.Instruction, |
| 437 | 450 | aarch64.Instruction.PCRelativeAddress, |
| ... | ... | @@ -450,7 +463,7 @@ const Parser = struct { |
| 450 | 463 | page.* = .{ |
| 451 | 464 | .base = .{ |
| 452 | 465 | .@"type" = .page, |
| 453 | .code = parser.code, | |
| 466 | .code = inst, | |
| 454 | 467 | .offset = offset, |
| 455 | 468 | .target = target, |
| 456 | 469 | }, |
| ... | ... | @@ -458,7 +471,7 @@ const Parser = struct { |
| 458 | 471 | .inst = parsed_inst, |
| 459 | 472 | }; |
| 460 | 473 | |
| 461 | log.debug(" | emitting {}", .{page}); | |
| 474 | log.warn(" | emitting {}", .{page}); | |
| 462 | 475 | |
| 463 | 476 | break :ptr &page.base; |
| 464 | 477 | }, |
| ... | ... | @@ -469,14 +482,14 @@ const Parser = struct { |
| 469 | 482 | page.* = .{ |
| 470 | 483 | .base = .{ |
| 471 | 484 | .@"type" = .got_page, |
| 472 | .code = parser.code, | |
| 485 | .code = inst, | |
| 473 | 486 | .offset = offset, |
| 474 | 487 | .target = target, |
| 475 | 488 | }, |
| 476 | 489 | .inst = parsed_inst, |
| 477 | 490 | }; |
| 478 | 491 | |
| 479 | log.debug(" | emitting {}", .{page}); | |
| 492 | log.warn(" | emitting {}", .{page}); | |
| 480 | 493 | |
| 481 | 494 | break :ptr &page.base; |
| 482 | 495 | }, |
| ... | ... | @@ -487,14 +500,14 @@ const Parser = struct { |
| 487 | 500 | page.* = .{ |
| 488 | 501 | .base = .{ |
| 489 | 502 | .@"type" = .tlvp_page, |
| 490 | .code = parser.code, | |
| 503 | .code = inst, | |
| 491 | 504 | .offset = offset, |
| 492 | 505 | .target = target, |
| 493 | 506 | }, |
| 494 | 507 | .inst = parsed_inst, |
| 495 | 508 | }; |
| 496 | 509 | |
| 497 | log.debug(" | emitting {}", .{page}); | |
| 510 | log.warn(" | emitting {}", .{page}); | |
| 498 | 511 | |
| 499 | 512 | break :ptr &page.base; |
| 500 | 513 | }, |
| ... | ... | @@ -517,7 +530,7 @@ const Parser = struct { |
| 517 | 530 | assert(reloc.r_length == 2); |
| 518 | 531 | |
| 519 | 532 | const offset = @intCast(u32, reloc.r_address); |
| 520 | const inst = parser.code.*[offset..][0..4]; | |
| 533 | const inst = parser.code[offset..][0..4]; | |
| 521 | 534 | |
| 522 | 535 | var op_kind: Relocation.PageOff.OpKind = undefined; |
| 523 | 536 | var parsed_inst: aarch64.Instruction = undefined; |
| ... | ... | @@ -542,7 +555,7 @@ const Parser = struct { |
| 542 | 555 | page_off.* = .{ |
| 543 | 556 | .base = .{ |
| 544 | 557 | .@"type" = .page_off, |
| 545 | .code = parser.code, | |
| 558 | .code = inst, | |
| 546 | 559 | .offset = offset, |
| 547 | 560 | .target = target, |
| 548 | 561 | }, |
| ... | ... | @@ -551,7 +564,7 @@ const Parser = struct { |
| 551 | 564 | .addend = parser.addend, |
| 552 | 565 | }; |
| 553 | 566 | |
| 554 | log.debug(" | emitting {}", .{page_off}); | |
| 567 | log.warn(" | emitting {}", .{page_off}); | |
| 555 | 568 | try parser.parsed.append(&page_off.base); |
| 556 | 569 | } |
| 557 | 570 | |
| ... | ... | @@ -562,7 +575,7 @@ const Parser = struct { |
| 562 | 575 | assert(reloc.r_length == 2); |
| 563 | 576 | |
| 564 | 577 | const offset = @intCast(u32, reloc.r_address); |
| 565 | const inst = parser.code.*[offset..][0..4]; | |
| 578 | const inst = parser.code[offset..][0..4]; | |
| 566 | 579 | assert(!isArithmeticOp(inst)); |
| 567 | 580 | |
| 568 | 581 | const parsed_inst = mem.bytesToValue(meta.TagPayload( |
| ... | ... | @@ -579,7 +592,7 @@ const Parser = struct { |
| 579 | 592 | page_off.* = .{ |
| 580 | 593 | .base = .{ |
| 581 | 594 | .@"type" = .got_page_off, |
| 582 | .code = parser.code, | |
| 595 | .code = inst, | |
| 583 | 596 | .offset = offset, |
| 584 | 597 | .target = target, |
| 585 | 598 | }, |
| ... | ... | @@ -588,7 +601,7 @@ const Parser = struct { |
| 588 | 601 | }, |
| 589 | 602 | }; |
| 590 | 603 | |
| 591 | log.debug(" | emitting {}", .{page_off}); | |
| 604 | log.warn(" | emitting {}", .{page_off}); | |
| 592 | 605 | try parser.parsed.append(&page_off.base); |
| 593 | 606 | } |
| 594 | 607 | |
| ... | ... | @@ -605,7 +618,7 @@ const Parser = struct { |
| 605 | 618 | }; |
| 606 | 619 | |
| 607 | 620 | const offset = @intCast(u32, reloc.r_address); |
| 608 | const inst = parser.code.*[offset..][0..4]; | |
| 621 | const inst = parser.code[offset..][0..4]; | |
| 609 | 622 | const parsed: RegInfo = parsed: { |
| 610 | 623 | if (isArithmeticOp(inst)) { |
| 611 | 624 | const parsed_inst = mem.bytesAsValue(meta.TagPayload( |
| ... | ... | @@ -638,7 +651,7 @@ const Parser = struct { |
| 638 | 651 | page_off.* = .{ |
| 639 | 652 | .base = .{ |
| 640 | 653 | .@"type" = .tlvp_page_off, |
| 641 | .code = parser.code, | |
| 654 | .code = inst, | |
| 642 | 655 | .offset = @intCast(u32, reloc.r_address), |
| 643 | 656 | .target = target, |
| 644 | 657 | }, |
| ... | ... | @@ -655,7 +668,7 @@ const Parser = struct { |
| 655 | 668 | }, |
| 656 | 669 | }; |
| 657 | 670 | |
| 658 | log.debug(" | emitting {}", .{page_off}); | |
| 671 | log.warn(" | emitting {}", .{page_off}); | |
| 659 | 672 | try parser.parsed.append(&page_off.base); |
| 660 | 673 | } |
| 661 | 674 | |
| ... | ... | @@ -700,14 +713,14 @@ const Parser = struct { |
| 700 | 713 | }; |
| 701 | 714 | const offset = @intCast(u32, reloc.r_address); |
| 702 | 715 | const addend: i64 = if (is_64bit) |
| 703 | mem.readIntLittle(i64, parser.code.*[offset..][0..8]) | |
| 716 | mem.readIntLittle(i64, parser.code[offset..][0..8]) | |
| 704 | 717 | else |
| 705 | mem.readIntLittle(i32, parser.code.*[offset..][0..4]); | |
| 718 | mem.readIntLittle(i32, parser.code[offset..][0..4]); | |
| 706 | 719 | |
| 707 | 720 | unsigned.* = .{ |
| 708 | 721 | .base = .{ |
| 709 | 722 | .@"type" = .unsigned, |
| 710 | .code = parser.code, | |
| 723 | .code = if (is_64bit) parser.code[offset..][0..8] else parser.code[offset..][0..4], | |
| 711 | 724 | .offset = offset, |
| 712 | 725 | .target = target, |
| 713 | 726 | }, |
| ... | ... | @@ -716,7 +729,7 @@ const Parser = struct { |
| 716 | 729 | .addend = addend, |
| 717 | 730 | }; |
| 718 | 731 | |
| 719 | log.debug(" | emitting {}", .{unsigned}); | |
| 732 | log.warn(" | emitting {}", .{unsigned}); | |
| 720 | 733 | try parser.parsed.append(&unsigned.base); |
| 721 | 734 | } |
| 722 | 735 | }; |