| ... | @@ -30,7 +30,7 @@ aliases: std.ArrayListUnmanaged(u32) = .{}, | ... | @@ -30,7 +30,7 @@ aliases: std.ArrayListUnmanaged(u32) = .{}, |
| 30 | contained: std.ArrayListUnmanaged(SymbolAtOffset) = .{}, | 30 | contained: std.ArrayListUnmanaged(SymbolAtOffset) = .{}, |
| 31 | | 31 | |
| 32 | /// Code (may be non-relocated) this block represents | 32 | /// Code (may be non-relocated) this block represents |
| 33 | code: []u8, | 33 | code: std.ArrayListUnmanaged(u8) = .{}, |
| 34 | | 34 | |
| 35 | /// Size and alignment of this text block | 35 | /// Size and alignment of this text block |
| 36 | /// Unlike in Elf, we need to store the size of this symbol as part of | 36 | /// Unlike in Elf, we need to store the size of this symbol as part of |
| ... | @@ -196,9 +196,9 @@ pub const Relocation = struct { | ... | @@ -196,9 +196,9 @@ pub const Relocation = struct { |
| 196 | }; | 196 | }; |
| 197 | | 197 | |
| 198 | if (self.is_64bit) { | 198 | if (self.is_64bit) { |
| 199 | mem.writeIntLittle(u64, args.block.code[args.offset..][0..8], @bitCast(u64, result)); | 199 | mem.writeIntLittle(u64, args.block.code.items[args.offset..][0..8], @bitCast(u64, result)); |
| 200 | } else { | 200 | } else { |
| 201 | mem.writeIntLittle(u32, args.block.code[args.offset..][0..4], @truncate(u32, @bitCast(u64, result))); | 201 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @truncate(u32, @bitCast(u64, result))); |
| 202 | } | 202 | } |
| 203 | } | 203 | } |
| 204 | | 204 | |
| ... | @@ -226,7 +226,7 @@ pub const Relocation = struct { | ... | @@ -226,7 +226,7 @@ pub const Relocation = struct { |
| 226 | i28, | 226 | i28, |
| 227 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr), | 227 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr), |
| 228 | ); | 228 | ); |
| 229 | const code = args.block.code[args.offset..][0..4]; | 229 | const code = args.block.code.items[args.offset..][0..4]; |
| 230 | var inst = aarch64.Instruction{ | 230 | var inst = aarch64.Instruction{ |
| 231 | .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload( | 231 | .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload( |
| 232 | aarch64.Instruction, | 232 | aarch64.Instruction, |
| ... | @@ -241,7 +241,7 @@ pub const Relocation = struct { | ... | @@ -241,7 +241,7 @@ pub const Relocation = struct { |
| 241 | i32, | 241 | i32, |
| 242 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4, | 242 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4, |
| 243 | ); | 243 | ); |
| 244 | mem.writeIntLittle(u32, args.block.code[args.offset..][0..4], @bitCast(u32, displacement)); | 244 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 245 | }, | 245 | }, |
| 246 | else => return error.UnsupportedCpuArchitecture, | 246 | else => return error.UnsupportedCpuArchitecture, |
| 247 | } | 247 | } |
| ... | @@ -269,7 +269,7 @@ pub const Relocation = struct { | ... | @@ -269,7 +269,7 @@ pub const Relocation = struct { |
| 269 | const target_page = @intCast(i32, target_addr >> 12); | 269 | const target_page = @intCast(i32, target_addr >> 12); |
| 270 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); | 270 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 271 | | 271 | |
| 272 | const code = args.block.code[args.offset..][0..4]; | 272 | const code = args.block.code.items[args.offset..][0..4]; |
| 273 | var inst = aarch64.Instruction{ | 273 | var inst = aarch64.Instruction{ |
| 274 | .pc_relative_address = mem.bytesToValue(meta.TagPayload( | 274 | .pc_relative_address = mem.bytesToValue(meta.TagPayload( |
| 275 | aarch64.Instruction, | 275 | aarch64.Instruction, |
| ... | @@ -315,7 +315,7 @@ pub const Relocation = struct { | ... | @@ -315,7 +315,7 @@ pub const Relocation = struct { |
| 315 | }; | 315 | }; |
| 316 | | 316 | |
| 317 | pub fn resolve(self: PageOff, args: ResolveArgs) !void { | 317 | pub fn resolve(self: PageOff, args: ResolveArgs) !void { |
| 318 | const code = args.block.code[args.offset..][0..4]; | 318 | const code = args.block.code.items[args.offset..][0..4]; |
| 319 | | 319 | |
| 320 | switch (self.kind) { | 320 | switch (self.kind) { |
| 321 | .page => { | 321 | .page => { |
| ... | @@ -445,7 +445,7 @@ pub const Relocation = struct { | ... | @@ -445,7 +445,7 @@ pub const Relocation = struct { |
| 445 | pub const PointerToGot = struct { | 445 | pub const PointerToGot = struct { |
| 446 | pub fn resolve(_: PointerToGot, args: ResolveArgs) !void { | 446 | pub fn resolve(_: PointerToGot, args: ResolveArgs) !void { |
| 447 | const result = try math.cast(i32, @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr)); | 447 | const result = try math.cast(i32, @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr)); |
| 448 | mem.writeIntLittle(u32, args.block.code[args.offset..][0..4], @bitCast(u32, result)); | 448 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, result)); |
| 449 | } | 449 | } |
| 450 | | 450 | |
| 451 | pub fn format(self: PointerToGot, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { | 451 | pub fn format(self: PointerToGot, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| ... | @@ -466,7 +466,7 @@ pub const Relocation = struct { | ... | @@ -466,7 +466,7 @@ pub const Relocation = struct { |
| 466 | i32, | 466 | i32, |
| 467 | target_addr - @intCast(i64, args.source_addr) - self.correction - 4, | 467 | target_addr - @intCast(i64, args.source_addr) - self.correction - 4, |
| 468 | ); | 468 | ); |
| 469 | mem.writeIntLittle(u32, args.block.code[args.offset..][0..4], @bitCast(u32, displacement)); | 469 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 470 | } | 470 | } |
| 471 | | 471 | |
| 472 | pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { | 472 | pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| ... | @@ -489,13 +489,13 @@ pub const Relocation = struct { | ... | @@ -489,13 +489,13 @@ pub const Relocation = struct { |
| 489 | pub fn resolve(self: Load, args: ResolveArgs) !void { | 489 | pub fn resolve(self: Load, args: ResolveArgs) !void { |
| 490 | if (self.kind == .tlvp) { | 490 | if (self.kind == .tlvp) { |
| 491 | // We need to rewrite the opcode from movq to leaq. | 491 | // We need to rewrite the opcode from movq to leaq. |
| 492 | args.block.code[args.offset - 2] = 0x8d; | 492 | args.block.code.items[args.offset - 2] = 0x8d; |
| 493 | } | 493 | } |
| 494 | const displacement = try math.cast( | 494 | const displacement = try math.cast( |
| 495 | i32, | 495 | i32, |
| 496 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4 + self.addend, | 496 | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4 + self.addend, |
| 497 | ); | 497 | ); |
| 498 | mem.writeIntLittle(u32, args.block.code[args.offset..][0..4], @bitCast(u32, displacement)); | 498 | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 499 | } | 499 | } |
| 500 | | 500 | |
| 501 | pub fn format(self: Load, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { | 501 | pub fn format(self: Load, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| ... | @@ -542,7 +542,6 @@ pub const Relocation = struct { | ... | @@ -542,7 +542,6 @@ pub const Relocation = struct { |
| 542 | | 542 | |
| 543 | pub const empty = TextBlock{ | 543 | pub const empty = TextBlock{ |
| 544 | .local_sym_index = 0, | 544 | .local_sym_index = 0, |
| 545 | .code = undefined, | | |
| 546 | .size = 0, | 545 | .size = 0, |
| 547 | .alignment = 0, | 546 | .alignment = 0, |
| 548 | .prev = null, | 547 | .prev = null, |
| ... | @@ -560,7 +559,7 @@ pub fn deinit(self: *TextBlock, allocator: *Allocator) void { | ... | @@ -560,7 +559,7 @@ pub fn deinit(self: *TextBlock, allocator: *Allocator) void { |
| 560 | self.relocs.deinit(allocator); | 559 | self.relocs.deinit(allocator); |
| 561 | self.contained.deinit(allocator); | 560 | self.contained.deinit(allocator); |
| 562 | self.aliases.deinit(allocator); | 561 | self.aliases.deinit(allocator); |
| 563 | allocator.free(self.code); | 562 | self.code.deinit(allocator); |
| 564 | } | 563 | } |
| 565 | | 564 | |
| 566 | /// Returns how much room there is to grow in virtual address space. | 565 | /// Returns how much room there is to grow in virtual address space. |
| ... | @@ -914,9 +913,9 @@ fn parseUnsigned( | ... | @@ -914,9 +913,9 @@ fn parseUnsigned( |
| 914 | }; | 913 | }; |
| 915 | | 914 | |
| 916 | var addend: i64 = if (is_64bit) | 915 | var addend: i64 = if (is_64bit) |
| 917 | mem.readIntLittle(i64, self.code[out.offset..][0..8]) | 916 | mem.readIntLittle(i64, self.code.items[out.offset..][0..8]) |
| 918 | else | 917 | else |
| 919 | mem.readIntLittle(i32, self.code[out.offset..][0..4]); | 918 | mem.readIntLittle(i32, self.code.items[out.offset..][0..4]); |
| 920 | | 919 | |
| 921 | if (rel.r_extern == 0) { | 920 | if (rel.r_extern == 0) { |
| 922 | assert(out.where == .local); | 921 | assert(out.where == .local); |
| ... | @@ -970,7 +969,7 @@ fn parsePageOff(self: TextBlock, rel: macho.relocation_info, out: *Relocation, a | ... | @@ -970,7 +969,7 @@ fn parsePageOff(self: TextBlock, rel: macho.relocation_info, out: *Relocation, a |
| 970 | const rel_type = @intToEnum(macho.reloc_type_arm64, rel.r_type); | 969 | const rel_type = @intToEnum(macho.reloc_type_arm64, rel.r_type); |
| 971 | const op_kind: ?Relocation.PageOff.OpKind = blk: { | 970 | const op_kind: ?Relocation.PageOff.OpKind = blk: { |
| 972 | if (rel_type != .ARM64_RELOC_PAGEOFF12) break :blk null; | 971 | if (rel_type != .ARM64_RELOC_PAGEOFF12) break :blk null; |
| 973 | const op_kind: Relocation.PageOff.OpKind = if (isArithmeticOp(self.code[out.offset..][0..4])) | 972 | const op_kind: Relocation.PageOff.OpKind = if (isArithmeticOp(self.code.items[out.offset..][0..4])) |
| 974 | .arithmetic | 973 | .arithmetic |
| 975 | else | 974 | else |
| 976 | .load; | 975 | .load; |
| ... | @@ -1013,7 +1012,7 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, ct | ... | @@ -1013,7 +1012,7 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, ct |
| 1013 | .X86_64_RELOC_SIGNED_4 => 4, | 1012 | .X86_64_RELOC_SIGNED_4 => 4, |
| 1014 | else => unreachable, | 1013 | else => unreachable, |
| 1015 | }; | 1014 | }; |
| 1016 | var addend: i64 = mem.readIntLittle(i32, self.code[out.offset..][0..4]) + correction; | 1015 | var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction; |
| 1017 | | 1016 | |
| 1018 | if (rel.r_extern == 0) { | 1017 | if (rel.r_extern == 0) { |
| 1019 | const source_sym = ctx.macho_file.locals.items[self.local_sym_index]; | 1018 | const source_sym = ctx.macho_file.locals.items[self.local_sym_index]; |
| ... | @@ -1038,7 +1037,7 @@ fn parseLoad(self: TextBlock, rel: macho.relocation_info, out: *Relocation) void | ... | @@ -1038,7 +1037,7 @@ fn parseLoad(self: TextBlock, rel: macho.relocation_info, out: *Relocation) void |
| 1038 | | 1037 | |
| 1039 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); | 1038 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 1040 | const addend: i32 = if (rel_type == .X86_64_RELOC_GOT) | 1039 | const addend: i32 = if (rel_type == .X86_64_RELOC_GOT) |
| 1041 | mem.readIntLittle(i32, self.code[out.offset..][0..4]) | 1040 | mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) |
| 1042 | else | 1041 | else |
| 1043 | 0; | 1042 | 0; |
| 1044 | | 1043 | |
| ... | @@ -1173,7 +1172,7 @@ pub fn print_this(self: *const TextBlock, macho_file: MachO) void { | ... | @@ -1173,7 +1172,7 @@ pub fn print_this(self: *const TextBlock, macho_file: MachO) void { |
| 1173 | } | 1172 | } |
| 1174 | } | 1173 | } |
| 1175 | } | 1174 | } |
| 1176 | log.warn(" code.len = {}", .{self.code.len}); | 1175 | log.warn(" code.len = {}", .{self.code.items.len}); |
| 1177 | if (self.relocs.items.len > 0) { | 1176 | if (self.relocs.items.len > 0) { |
| 1178 | log.warn(" relocations:", .{}); | 1177 | log.warn(" relocations:", .{}); |
| 1179 | for (self.relocs.items) |rel| { | 1178 | for (self.relocs.items) |rel| { |