| ... | ... | @@ -5,7 +5,7 @@ const build_options = @import("build_options"); |
| 5 | 5 | const aarch64 = @import("../../arch/aarch64/bits.zig"); |
| 6 | 6 | const assert = std.debug.assert; |
| 7 | 7 | const commands = @import("commands.zig"); |
| 8 | | const log = std.log.scoped(.text_block); |
| 8 | const log = std.log.scoped(.link); |
| 9 | 9 | const macho = std.macho; |
| 10 | 10 | const math = std.math; |
| 11 | 11 | const mem = std.mem; |
| ... | ... | @@ -54,10 +54,10 @@ rebases: std.ArrayListUnmanaged(u64) = .{}, |
| 54 | 54 | /// List of offsets contained within this atom that will be dynamically bound |
| 55 | 55 | /// by the dynamic loader and contain pointers to resolved (at load time) extern |
| 56 | 56 | /// symbols (aka proxies aka imports) |
| 57 | | bindings: std.ArrayListUnmanaged(SymbolAtOffset) = .{}, |
| 57 | bindings: std.ArrayListUnmanaged(Binding) = .{}, |
| 58 | 58 | |
| 59 | 59 | /// List of lazy bindings |
| 60 | | lazy_bindings: std.ArrayListUnmanaged(SymbolAtOffset) = .{}, |
| 60 | lazy_bindings: std.ArrayListUnmanaged(Binding) = .{}, |
| 61 | 61 | |
| 62 | 62 | /// List of data-in-code entries. This is currently specific to x86_64 only. |
| 63 | 63 | dices: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{}, |
| ... | ... | @@ -83,25 +83,15 @@ dbg_info_len: u32, |
| 83 | 83 | |
| 84 | 84 | dirty: bool = true, |
| 85 | 85 | |
| 86 | pub const Binding = struct { |
| 87 | n_strx: u32, |
| 88 | offset: u64, |
| 89 | }; |
| 90 | |
| 86 | 91 | pub const SymbolAtOffset = struct { |
| 87 | 92 | local_sym_index: u32, |
| 88 | 93 | offset: u64, |
| 89 | 94 | stab: ?Stab = null, |
| 90 | | |
| 91 | | pub fn format( |
| 92 | | self: SymbolAtOffset, |
| 93 | | comptime fmt: []const u8, |
| 94 | | options: std.fmt.FormatOptions, |
| 95 | | writer: anytype, |
| 96 | | ) !void { |
| 97 | | _ = fmt; |
| 98 | | _ = options; |
| 99 | | try std.fmt.format(writer, "{{ {d}: .offset = {d}", .{ self.local_sym_index, self.offset }); |
| 100 | | if (self.stab) |stab| { |
| 101 | | try std.fmt.format(writer, ", .stab = {any}", .{stab}); |
| 102 | | } |
| 103 | | try std.fmt.format(writer, " }}", .{}); |
| 104 | | } |
| 105 | 95 | }; |
| 106 | 96 | |
| 107 | 97 | pub const Stab = union(enum) { |
| ... | ... | @@ -171,411 +161,26 @@ pub const Stab = union(enum) { |
| 171 | 161 | }; |
| 172 | 162 | |
| 173 | 163 | pub const Relocation = struct { |
| 164 | pub const Target = union(enum) { |
| 165 | local: u32, |
| 166 | global: u32, |
| 167 | }; |
| 168 | |
| 174 | 169 | /// Offset within the atom's code buffer. |
| 175 | 170 | /// Note relocation size can be inferred by relocation's kind. |
| 176 | 171 | offset: u32, |
| 177 | 172 | |
| 178 | | where: enum { |
| 179 | | local, |
| 180 | | undef, |
| 181 | | }, |
| 182 | | |
| 183 | | where_index: u32, |
| 184 | | |
| 185 | | payload: union(enum) { |
| 186 | | unsigned: Unsigned, |
| 187 | | branch: Branch, |
| 188 | | page: Page, |
| 189 | | page_off: PageOff, |
| 190 | | pointer_to_got: PointerToGot, |
| 191 | | signed: Signed, |
| 192 | | load: Load, |
| 193 | | }, |
| 194 | | |
| 195 | | const ResolveArgs = struct { |
| 196 | | block: *Atom, |
| 197 | | offset: u32, |
| 198 | | source_addr: u64, |
| 199 | | target_addr: u64, |
| 200 | | macho_file: *MachO, |
| 201 | | }; |
| 202 | | |
| 203 | | pub const Unsigned = struct { |
| 204 | | subtractor: ?u32, |
| 205 | | |
| 206 | | /// Addend embedded directly in the relocation slot |
| 207 | | addend: i64, |
| 208 | | |
| 209 | | /// Extracted from r_length: |
| 210 | | /// => 3 implies true |
| 211 | | /// => 2 implies false |
| 212 | | /// => * is unreachable |
| 213 | | is_64bit: bool, |
| 214 | | |
| 215 | | pub fn resolve(self: Unsigned, args: ResolveArgs) !void { |
| 216 | | const result = blk: { |
| 217 | | if (self.subtractor) |subtractor| { |
| 218 | | const sym = args.macho_file.locals.items[subtractor]; |
| 219 | | break :blk @intCast(i64, args.target_addr) - @intCast(i64, sym.n_value) + self.addend; |
| 220 | | } else { |
| 221 | | break :blk @intCast(i64, args.target_addr) + self.addend; |
| 222 | | } |
| 223 | | }; |
| 224 | | |
| 225 | | if (self.is_64bit) { |
| 226 | | mem.writeIntLittle(u64, args.block.code.items[args.offset..][0..8], @bitCast(u64, result)); |
| 227 | | } else { |
| 228 | | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @truncate(u32, @bitCast(u64, result))); |
| 229 | | } |
| 230 | | } |
| 231 | | |
| 232 | | pub fn format(self: Unsigned, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 233 | | _ = fmt; |
| 234 | | _ = options; |
| 235 | | try std.fmt.format(writer, "Unsigned {{ ", .{}); |
| 236 | | if (self.subtractor) |sub| { |
| 237 | | try std.fmt.format(writer, ".subtractor = {}, ", .{sub}); |
| 238 | | } |
| 239 | | try std.fmt.format(writer, ".addend = {}, ", .{self.addend}); |
| 240 | | const length: usize = if (self.is_64bit) 8 else 4; |
| 241 | | try std.fmt.format(writer, ".length = {}, ", .{length}); |
| 242 | | try std.fmt.format(writer, "}}", .{}); |
| 243 | | } |
| 244 | | }; |
| 245 | | |
| 246 | | pub const Branch = struct { |
| 247 | | arch: Arch, |
| 248 | | |
| 249 | | pub fn resolve(self: Branch, args: ResolveArgs) !void { |
| 250 | | switch (self.arch) { |
| 251 | | .aarch64 => { |
| 252 | | const displacement = math.cast( |
| 253 | | i28, |
| 254 | | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr), |
| 255 | | ) catch |err| switch (err) { |
| 256 | | error.Overflow => { |
| 257 | | log.err("jump too big to encode as i28 displacement value", .{}); |
| 258 | | log.err(" (target - source) = displacement => 0x{x} - 0x{x} = 0x{x}", .{ |
| 259 | | args.target_addr, |
| 260 | | args.source_addr, |
| 261 | | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr), |
| 262 | | }); |
| 263 | | log.err(" TODO implement branch islands to extend jump distance for arm64", .{}); |
| 264 | | return error.TODOImplementBranchIslands; |
| 265 | | }, |
| 266 | | }; |
| 267 | | const code = args.block.code.items[args.offset..][0..4]; |
| 268 | | var inst = aarch64.Instruction{ |
| 269 | | .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload( |
| 270 | | aarch64.Instruction, |
| 271 | | aarch64.Instruction.unconditional_branch_immediate, |
| 272 | | ), code), |
| 273 | | }; |
| 274 | | inst.unconditional_branch_immediate.imm26 = @truncate(u26, @bitCast(u28, displacement >> 2)); |
| 275 | | mem.writeIntLittle(u32, code, inst.toU32()); |
| 276 | | }, |
| 277 | | .x86_64 => { |
| 278 | | const displacement = try math.cast( |
| 279 | | i32, |
| 280 | | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4, |
| 281 | | ); |
| 282 | | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 283 | | }, |
| 284 | | else => return error.UnsupportedCpuArchitecture, |
| 285 | | } |
| 286 | | } |
| 287 | | |
| 288 | | pub fn format(self: Branch, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 289 | | _ = self; |
| 290 | | _ = fmt; |
| 291 | | _ = options; |
| 292 | | try std.fmt.format(writer, "Branch {{}}", .{}); |
| 293 | | } |
| 294 | | }; |
| 173 | target: Target, |
| 295 | 174 | |
| 296 | | pub const Page = struct { |
| 297 | | kind: enum { |
| 298 | | page, |
| 299 | | got, |
| 300 | | tlvp, |
| 301 | | }, |
| 302 | | addend: u32 = 0, |
| 303 | | |
| 304 | | pub fn resolve(self: Page, args: ResolveArgs) !void { |
| 305 | | const target_addr = args.target_addr + self.addend; |
| 306 | | const source_page = @intCast(i32, args.source_addr >> 12); |
| 307 | | const target_page = @intCast(i32, target_addr >> 12); |
| 308 | | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 309 | | |
| 310 | | const code = args.block.code.items[args.offset..][0..4]; |
| 311 | | var inst = aarch64.Instruction{ |
| 312 | | .pc_relative_address = mem.bytesToValue(meta.TagPayload( |
| 313 | | aarch64.Instruction, |
| 314 | | aarch64.Instruction.pc_relative_address, |
| 315 | | ), code), |
| 316 | | }; |
| 317 | | inst.pc_relative_address.immhi = @truncate(u19, pages >> 2); |
| 318 | | inst.pc_relative_address.immlo = @truncate(u2, pages); |
| 319 | | |
| 320 | | mem.writeIntLittle(u32, code, inst.toU32()); |
| 321 | | } |
| 322 | | |
| 323 | | pub fn format(self: Page, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 324 | | _ = fmt; |
| 325 | | _ = options; |
| 326 | | try std.fmt.format(writer, "Page {{ ", .{}); |
| 327 | | switch (self.kind) { |
| 328 | | .page => {}, |
| 329 | | .got => { |
| 330 | | try std.fmt.format(writer, ".got, ", .{}); |
| 331 | | }, |
| 332 | | .tlvp => { |
| 333 | | try std.fmt.format(writer, ".tlvp", .{}); |
| 334 | | }, |
| 335 | | } |
| 336 | | try std.fmt.format(writer, ".addend = {}, ", .{self.addend}); |
| 337 | | try std.fmt.format(writer, "}}", .{}); |
| 338 | | } |
| 339 | | }; |
| 340 | | |
| 341 | | pub const PageOff = struct { |
| 342 | | kind: enum { |
| 343 | | page, |
| 344 | | got, |
| 345 | | tlvp, |
| 346 | | }, |
| 347 | | addend: u32 = 0, |
| 348 | | op_kind: ?OpKind = null, |
| 349 | | |
| 350 | | pub const OpKind = enum { |
| 351 | | arithmetic, |
| 352 | | load, |
| 353 | | }; |
| 354 | | |
| 355 | | pub fn resolve(self: PageOff, args: ResolveArgs) !void { |
| 356 | | const code = args.block.code.items[args.offset..][0..4]; |
| 357 | | |
| 358 | | switch (self.kind) { |
| 359 | | .page => { |
| 360 | | const target_addr = args.target_addr + self.addend; |
| 361 | | const narrowed = @truncate(u12, target_addr); |
| 362 | | |
| 363 | | const op_kind = self.op_kind orelse unreachable; |
| 364 | | var inst: aarch64.Instruction = blk: { |
| 365 | | switch (op_kind) { |
| 366 | | .arithmetic => { |
| 367 | | break :blk .{ |
| 368 | | .add_subtract_immediate = mem.bytesToValue(meta.TagPayload( |
| 369 | | aarch64.Instruction, |
| 370 | | aarch64.Instruction.add_subtract_immediate, |
| 371 | | ), code), |
| 372 | | }; |
| 373 | | }, |
| 374 | | .load => { |
| 375 | | break :blk .{ |
| 376 | | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| 377 | | aarch64.Instruction, |
| 378 | | aarch64.Instruction.load_store_register, |
| 379 | | ), code), |
| 380 | | }; |
| 381 | | }, |
| 382 | | } |
| 383 | | }; |
| 384 | | |
| 385 | | if (op_kind == .arithmetic) { |
| 386 | | inst.add_subtract_immediate.imm12 = narrowed; |
| 387 | | } else { |
| 388 | | const offset: u12 = blk: { |
| 389 | | if (inst.load_store_register.size == 0) { |
| 390 | | if (inst.load_store_register.v == 1) { |
| 391 | | // 128-bit SIMD is scaled by 16. |
| 392 | | break :blk try math.divExact(u12, narrowed, 16); |
| 393 | | } |
| 394 | | // Otherwise, 8-bit SIMD or ldrb. |
| 395 | | break :blk narrowed; |
| 396 | | } else { |
| 397 | | const denom: u4 = try math.powi(u4, 2, inst.load_store_register.size); |
| 398 | | break :blk try math.divExact(u12, narrowed, denom); |
| 399 | | } |
| 400 | | }; |
| 401 | | inst.load_store_register.offset = offset; |
| 402 | | } |
| 403 | | |
| 404 | | mem.writeIntLittle(u32, code, inst.toU32()); |
| 405 | | }, |
| 406 | | .got => { |
| 407 | | const narrowed = @truncate(u12, args.target_addr); |
| 408 | | var inst: aarch64.Instruction = .{ |
| 409 | | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| 410 | | aarch64.Instruction, |
| 411 | | aarch64.Instruction.load_store_register, |
| 412 | | ), code), |
| 413 | | }; |
| 414 | | const offset = try math.divExact(u12, narrowed, 8); |
| 415 | | inst.load_store_register.offset = offset; |
| 416 | | mem.writeIntLittle(u32, code, inst.toU32()); |
| 417 | | }, |
| 418 | | .tlvp => { |
| 419 | | const RegInfo = struct { |
| 420 | | rd: u5, |
| 421 | | rn: u5, |
| 422 | | size: u1, |
| 423 | | }; |
| 424 | | const reg_info: RegInfo = blk: { |
| 425 | | if (isArithmeticOp(code)) { |
| 426 | | const inst = mem.bytesToValue(meta.TagPayload( |
| 427 | | aarch64.Instruction, |
| 428 | | aarch64.Instruction.add_subtract_immediate, |
| 429 | | ), code); |
| 430 | | break :blk .{ |
| 431 | | .rd = inst.rd, |
| 432 | | .rn = inst.rn, |
| 433 | | .size = inst.sf, |
| 434 | | }; |
| 435 | | } else { |
| 436 | | const inst = mem.bytesToValue(meta.TagPayload( |
| 437 | | aarch64.Instruction, |
| 438 | | aarch64.Instruction.load_store_register, |
| 439 | | ), code); |
| 440 | | break :blk .{ |
| 441 | | .rd = inst.rt, |
| 442 | | .rn = inst.rn, |
| 443 | | .size = @truncate(u1, inst.size), |
| 444 | | }; |
| 445 | | } |
| 446 | | }; |
| 447 | | const narrowed = @truncate(u12, args.target_addr); |
| 448 | | var inst = aarch64.Instruction{ |
| 449 | | .add_subtract_immediate = .{ |
| 450 | | .rd = reg_info.rd, |
| 451 | | .rn = reg_info.rn, |
| 452 | | .imm12 = narrowed, |
| 453 | | .sh = 0, |
| 454 | | .s = 0, |
| 455 | | .op = 0, |
| 456 | | .sf = reg_info.size, |
| 457 | | }, |
| 458 | | }; |
| 459 | | mem.writeIntLittle(u32, code, inst.toU32()); |
| 460 | | }, |
| 461 | | } |
| 462 | | } |
| 463 | | |
| 464 | | pub fn format(self: PageOff, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 465 | | _ = fmt; |
| 466 | | _ = options; |
| 467 | | try std.fmt.format(writer, "PageOff {{ ", .{}); |
| 468 | | switch (self.kind) { |
| 469 | | .page => {}, |
| 470 | | .got => { |
| 471 | | try std.fmt.format(writer, ".got, ", .{}); |
| 472 | | }, |
| 473 | | .tlvp => { |
| 474 | | try std.fmt.format(writer, ".tlvp, ", .{}); |
| 475 | | }, |
| 476 | | } |
| 477 | | try std.fmt.format(writer, ".addend = {}, ", .{self.addend}); |
| 478 | | try std.fmt.format(writer, ".op_kind = {s}, ", .{self.op_kind}); |
| 479 | | try std.fmt.format(writer, "}}", .{}); |
| 480 | | } |
| 481 | | }; |
| 482 | | |
| 483 | | pub const PointerToGot = struct { |
| 484 | | pub fn resolve(_: PointerToGot, args: ResolveArgs) !void { |
| 485 | | const result = try math.cast(i32, @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr)); |
| 486 | | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, result)); |
| 487 | | } |
| 488 | | |
| 489 | | pub fn format(self: PointerToGot, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 490 | | _ = self; |
| 491 | | _ = fmt; |
| 492 | | _ = options; |
| 493 | | try std.fmt.format(writer, "PointerToGot {{}}", .{}); |
| 494 | | } |
| 495 | | }; |
| 496 | | |
| 497 | | pub const Signed = struct { |
| 498 | | addend: i64, |
| 499 | | correction: u3, |
| 500 | | |
| 501 | | pub fn resolve(self: Signed, args: ResolveArgs) !void { |
| 502 | | const target_addr = @intCast(i64, args.target_addr) + self.addend; |
| 503 | | const displacement = try math.cast( |
| 504 | | i32, |
| 505 | | target_addr - @intCast(i64, args.source_addr + self.correction + 4), |
| 506 | | ); |
| 507 | | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 508 | | } |
| 509 | | |
| 510 | | pub fn format(self: Signed, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 511 | | _ = fmt; |
| 512 | | _ = options; |
| 513 | | try std.fmt.format(writer, "Signed {{ ", .{}); |
| 514 | | try std.fmt.format(writer, ".addend = {}, ", .{self.addend}); |
| 515 | | try std.fmt.format(writer, ".correction = {}, ", .{self.correction}); |
| 516 | | try std.fmt.format(writer, "}}", .{}); |
| 517 | | } |
| 518 | | }; |
| 519 | | |
| 520 | | pub const Load = struct { |
| 521 | | kind: enum { |
| 522 | | got, |
| 523 | | tlvp, |
| 524 | | }, |
| 525 | | addend: i32 = 0, |
| 175 | addend: i64, |
| 526 | 176 | |
| 527 | | pub fn resolve(self: Load, args: ResolveArgs) !void { |
| 528 | | if (self.kind == .tlvp) { |
| 529 | | // We need to rewrite the opcode from movq to leaq. |
| 530 | | args.block.code.items[args.offset - 2] = 0x8d; |
| 531 | | } |
| 532 | | const displacement = try math.cast( |
| 533 | | i32, |
| 534 | | @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4 + self.addend, |
| 535 | | ); |
| 536 | | mem.writeIntLittle(u32, args.block.code.items[args.offset..][0..4], @bitCast(u32, displacement)); |
| 537 | | } |
| 177 | subtractor: ?u32, |
| 538 | 178 | |
| 539 | | pub fn format(self: Load, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 540 | | _ = fmt; |
| 541 | | _ = options; |
| 542 | | try std.fmt.format(writer, "Load {{ ", .{}); |
| 543 | | try std.fmt.format(writer, "{s}, ", .{self.kind}); |
| 544 | | try std.fmt.format(writer, ".addend = {}, ", .{self.addend}); |
| 545 | | try std.fmt.format(writer, "}}", .{}); |
| 546 | | } |
| 547 | | }; |
| 179 | pcrel: bool, |
| 548 | 180 | |
| 549 | | pub fn resolve(self: Relocation, args: ResolveArgs) !void { |
| 550 | | switch (self.payload) { |
| 551 | | .unsigned => |unsigned| try unsigned.resolve(args), |
| 552 | | .branch => |branch| try branch.resolve(args), |
| 553 | | .page => |page| try page.resolve(args), |
| 554 | | .page_off => |page_off| try page_off.resolve(args), |
| 555 | | .pointer_to_got => |pointer_to_got| try pointer_to_got.resolve(args), |
| 556 | | .signed => |signed| try signed.resolve(args), |
| 557 | | .load => |load| try load.resolve(args), |
| 558 | | } |
| 559 | | } |
| 181 | length: u2, |
| 560 | 182 | |
| 561 | | pub fn format(self: Relocation, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 562 | | try std.fmt.format(writer, "Relocation {{ ", .{}); |
| 563 | | try std.fmt.format(writer, ".offset = {}, ", .{self.offset}); |
| 564 | | try std.fmt.format(writer, ".where = {}, ", .{self.where}); |
| 565 | | try std.fmt.format(writer, ".where_index = {d}, ", .{self.where_index}); |
| 566 | | |
| 567 | | switch (self.payload) { |
| 568 | | .unsigned => |unsigned| try unsigned.format(fmt, options, writer), |
| 569 | | .branch => |branch| try branch.format(fmt, options, writer), |
| 570 | | .page => |page| try page.format(fmt, options, writer), |
| 571 | | .page_off => |page_off| try page_off.format(fmt, options, writer), |
| 572 | | .pointer_to_got => |pointer_to_got| try pointer_to_got.format(fmt, options, writer), |
| 573 | | .signed => |signed| try signed.format(fmt, options, writer), |
| 574 | | .load => |load| try load.format(fmt, options, writer), |
| 575 | | } |
| 576 | | |
| 577 | | try std.fmt.format(writer, "}}", .{}); |
| 578 | | } |
| 183 | @"type": u4, |
| 579 | 184 | }; |
| 580 | 185 | |
| 581 | 186 | pub const empty = Atom{ |
| ... | ... | @@ -641,526 +246,367 @@ pub fn freeListEligible(self: Atom, macho_file: MachO) bool { |
| 641 | 246 | |
| 642 | 247 | const RelocContext = struct { |
| 643 | 248 | base_addr: u64 = 0, |
| 644 | | base_offset: u64 = 0, |
| 645 | 249 | allocator: *Allocator, |
| 646 | 250 | object: *Object, |
| 647 | 251 | macho_file: *MachO, |
| 648 | 252 | }; |
| 649 | 253 | |
| 650 | | fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Relocation { |
| 651 | | var parsed_rel = Relocation{ |
| 652 | | .offset = @intCast(u32, @intCast(u64, rel.r_address) - context.base_offset), |
| 653 | | .where = undefined, |
| 654 | | .where_index = undefined, |
| 655 | | .payload = undefined, |
| 656 | | }; |
| 657 | | |
| 658 | | if (rel.r_extern == 0) { |
| 659 | | const sect_id = @intCast(u16, rel.r_symbolnum - 1); |
| 660 | | |
| 661 | | const local_sym_index = context.object.sections_as_symbols.get(sect_id) orelse blk: { |
| 662 | | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 663 | | const sect = seg.sections.items[sect_id]; |
| 664 | | const match = (try context.macho_file.getMatchingSection(sect)) orelse unreachable; |
| 665 | | const local_sym_index = @intCast(u32, context.macho_file.locals.items.len); |
| 666 | | try context.macho_file.locals.append(context.allocator, .{ |
| 667 | | .n_strx = 0, |
| 668 | | .n_type = macho.N_SECT, |
| 669 | | .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), |
| 670 | | .n_desc = 0, |
| 671 | | .n_value = 0, |
| 672 | | }); |
| 673 | | try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index); |
| 674 | | break :blk local_sym_index; |
| 675 | | }; |
| 676 | | |
| 677 | | parsed_rel.where = .local; |
| 678 | | parsed_rel.where_index = local_sym_index; |
| 679 | | } else { |
| 680 | | const sym = context.object.symtab.items[rel.r_symbolnum]; |
| 681 | | const sym_name = context.object.getString(sym.n_strx); |
| 682 | | |
| 683 | | if (MachO.symbolIsSect(sym) and !MachO.symbolIsExt(sym)) { |
| 684 | | const where_index = context.object.symbol_mapping.get(rel.r_symbolnum) orelse unreachable; |
| 685 | | parsed_rel.where = .local; |
| 686 | | parsed_rel.where_index = where_index; |
| 687 | | } else { |
| 688 | | const n_strx = context.macho_file.strtab_dir.getKeyAdapted(@as([]const u8, sym_name), StringIndexAdapter{ |
| 689 | | .bytes = &context.macho_file.strtab, |
| 690 | | }) orelse unreachable; |
| 691 | | const resolv = context.macho_file.symbol_resolver.get(n_strx) orelse unreachable; |
| 692 | | switch (resolv.where) { |
| 693 | | .global => { |
| 694 | | parsed_rel.where = .local; |
| 695 | | parsed_rel.where_index = resolv.local_sym_index; |
| 696 | | }, |
| 697 | | .undef => { |
| 698 | | parsed_rel.where = .undef; |
| 699 | | parsed_rel.where_index = resolv.where_index; |
| 700 | | }, |
| 701 | | } |
| 702 | | } |
| 703 | | } |
| 704 | | |
| 705 | | return parsed_rel; |
| 706 | | } |
| 707 | | |
| 708 | 254 | pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocContext) !void { |
| 709 | 255 | const tracy = trace(@src()); |
| 710 | 256 | defer tracy.end(); |
| 711 | 257 | |
| 712 | | const filtered_relocs = filterRelocs(relocs, context.base_offset, context.base_offset + self.size); |
| 713 | | var it = RelocIterator{ |
| 714 | | .buffer = filtered_relocs, |
| 715 | | }; |
| 716 | | |
| 717 | | var addend: u32 = 0; |
| 718 | | var subtractor: ?u32 = null; |
| 719 | 258 | const arch = context.macho_file.base.options.target.cpu.arch; |
| 259 | var addend: i64 = 0; |
| 260 | var subtractor: ?u32 = null; |
| 720 | 261 | |
| 721 | | while (it.next()) |rel| { |
| 722 | | if (isAddend(rel, arch)) { |
| 723 | | // Addend is not a relocation with effect on the TextBlock, so |
| 724 | | // parse it and carry on. |
| 725 | | assert(addend == 0); // Oh no, addend was not reset! |
| 726 | | addend = rel.r_symbolnum; |
| 727 | | |
| 728 | | // Verify ADDEND is followed by a PAGE21 or PAGEOFF12. |
| 729 | | const next = @intToEnum(macho.reloc_type_arm64, it.peek().r_type); |
| 730 | | switch (next) { |
| 731 | | .ARM64_RELOC_PAGE21, .ARM64_RELOC_PAGEOFF12 => {}, |
| 732 | | else => { |
| 733 | | log.err("unexpected relocation type: expected PAGE21 or PAGEOFF12, found {s}", .{next}); |
| 734 | | return error.UnexpectedRelocationType; |
| 262 | for (relocs) |rel, i| { |
| 263 | blk: { |
| 264 | switch (arch) { |
| 265 | .aarch64 => switch (@intToEnum(macho.reloc_type_arm64, rel.r_type)) { |
| 266 | .ARM64_RELOC_ADDEND => { |
| 267 | assert(addend == 0); |
| 268 | addend = rel.r_symbolnum; |
| 269 | // Verify that it's followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12. |
| 270 | if (relocs.len <= i + 1) { |
| 271 | log.err("no relocation after ARM64_RELOC_ADDEND", .{}); |
| 272 | return error.UnexpectedRelocationType; |
| 273 | } |
| 274 | const next = @intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type); |
| 275 | switch (next) { |
| 276 | .ARM64_RELOC_PAGE21, .ARM64_RELOC_PAGEOFF12 => {}, |
| 277 | else => { |
| 278 | log.err("unexpected relocation type after ARM64_RELOC_ADDEND", .{}); |
| 279 | log.err(" expected ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12", .{}); |
| 280 | log.err(" found {s}", .{next}); |
| 281 | return error.UnexpectedRelocationType; |
| 282 | }, |
| 283 | } |
| 284 | continue; |
| 285 | }, |
| 286 | .ARM64_RELOC_SUBTRACTOR => {}, |
| 287 | else => break :blk, |
| 288 | }, |
| 289 | .x86_64 => switch (@intToEnum(macho.reloc_type_x86_64, rel.r_type)) { |
| 290 | .X86_64_RELOC_SUBTRACTOR => {}, |
| 291 | else => break :blk, |
| 735 | 292 | }, |
| 293 | else => unreachable, |
| 736 | 294 | } |
| 737 | | continue; |
| 738 | | } |
| 739 | 295 | |
| 740 | | if (isSubtractor(rel, arch)) { |
| 741 | | // Subtractor is not a relocation with effect on the TextBlock, so |
| 742 | | // parse it and carry on. |
| 743 | | assert(subtractor == null); // Oh no, subtractor was not reset! |
| 744 | | assert(rel.r_extern == 1); |
| 296 | assert(subtractor == null); |
| 745 | 297 | const sym = context.object.symtab.items[rel.r_symbolnum]; |
| 746 | | const sym_name = context.object.getString(sym.n_strx); |
| 747 | | |
| 748 | 298 | if (MachO.symbolIsSect(sym) and !MachO.symbolIsExt(sym)) { |
| 749 | | const where_index = context.object.symbol_mapping.get(rel.r_symbolnum) orelse unreachable; |
| 750 | | subtractor = where_index; |
| 299 | subtractor = context.object.symbol_mapping.get(rel.r_symbolnum).?; |
| 751 | 300 | } else { |
| 752 | | const n_strx = context.macho_file.strtab_dir.getKeyAdapted(@as([]const u8, sym_name), StringIndexAdapter{ |
| 753 | | .bytes = &context.macho_file.strtab, |
| 754 | | }) orelse unreachable; |
| 755 | | const resolv = context.macho_file.symbol_resolver.get(n_strx) orelse unreachable; |
| 301 | const sym_name = context.object.getString(sym.n_strx); |
| 302 | const n_strx = context.macho_file.strtab_dir.getKeyAdapted( |
| 303 | @as([]const u8, sym_name), |
| 304 | StringIndexAdapter{ |
| 305 | .bytes = &context.macho_file.strtab, |
| 306 | }, |
| 307 | ).?; |
| 308 | const resolv = context.macho_file.symbol_resolver.get(n_strx).?; |
| 756 | 309 | assert(resolv.where == .global); |
| 757 | 310 | subtractor = resolv.local_sym_index; |
| 758 | 311 | } |
| 759 | | |
| 760 | | // Verify SUBTRACTOR is followed by UNSIGNED. |
| 312 | // Verify that *_SUBTRACTOR is followed by *_UNSIGNED. |
| 313 | if (relocs.len <= i + 1) { |
| 314 | log.err("no relocation after *_RELOC_SUBTRACTOR", .{}); |
| 315 | return error.UnexpectedRelocationType; |
| 316 | } |
| 761 | 317 | switch (arch) { |
| 762 | | .aarch64 => { |
| 763 | | const next = @intToEnum(macho.reloc_type_arm64, it.peek().r_type); |
| 764 | | if (next != .ARM64_RELOC_UNSIGNED) { |
| 765 | | log.err("unexpected relocation type: expected UNSIGNED, found {s}", .{next}); |
| 318 | .aarch64 => switch (@intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type)) { |
| 319 | .ARM64_RELOC_UNSIGNED => {}, |
| 320 | else => { |
| 321 | log.err("unexpected relocation type after ARM64_RELOC_ADDEND", .{}); |
| 322 | log.err(" expected ARM64_RELOC_UNSIGNED", .{}); |
| 323 | log.err(" found {s}", .{@intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type)}); |
| 766 | 324 | return error.UnexpectedRelocationType; |
| 767 | | } |
| 325 | }, |
| 768 | 326 | }, |
| 769 | | .x86_64 => { |
| 770 | | const next = @intToEnum(macho.reloc_type_x86_64, it.peek().r_type); |
| 771 | | if (next != .X86_64_RELOC_UNSIGNED) { |
| 772 | | log.err("unexpected relocation type: expected UNSIGNED, found {s}", .{next}); |
| 327 | .x86_64 => switch (@intToEnum(macho.reloc_type_x86_64, relocs[i + 1].r_type)) { |
| 328 | .X86_64_RELOC_UNSIGNED => {}, |
| 329 | else => { |
| 330 | log.err("unexpected relocation type after X86_64_RELOC_ADDEND", .{}); |
| 331 | log.err(" expected X86_64_RELOC_UNSIGNED", .{}); |
| 332 | log.err(" found {s}", .{@intToEnum(macho.reloc_type_x86_64, relocs[i + 1].r_type)}); |
| 773 | 333 | return error.UnexpectedRelocationType; |
| 774 | | } |
| 334 | }, |
| 775 | 335 | }, |
| 776 | 336 | else => unreachable, |
| 777 | 337 | } |
| 778 | 338 | continue; |
| 779 | 339 | } |
| 780 | 340 | |
| 781 | | var parsed_rel = try initRelocFromObject(rel, context); |
| 341 | const target = target: { |
| 342 | if (rel.r_extern == 0) { |
| 343 | const sect_id = @intCast(u16, rel.r_symbolnum - 1); |
| 344 | const local_sym_index = context.object.sections_as_symbols.get(sect_id) orelse blk: { |
| 345 | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 346 | const sect = seg.sections.items[sect_id]; |
| 347 | const match = (try context.macho_file.getMatchingSection(sect)) orelse unreachable; |
| 348 | const sym_name = try std.fmt.allocPrint(context.allocator, "{s}_{s}_{s}", .{ |
| 349 | context.object.name, |
| 350 | commands.segmentName(sect), |
| 351 | commands.sectionName(sect), |
| 352 | }); |
| 353 | defer context.allocator.free(sym_name); |
| 354 | const local_sym_index = @intCast(u32, context.macho_file.locals.items.len); |
| 355 | try context.macho_file.locals.append(context.allocator, .{ |
| 356 | .n_strx = try context.macho_file.makeString(sym_name), |
| 357 | .n_type = macho.N_SECT, |
| 358 | .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), |
| 359 | .n_desc = 0, |
| 360 | .n_value = 0, |
| 361 | }); |
| 362 | try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index); |
| 363 | break :blk local_sym_index; |
| 364 | }; |
| 365 | break :target Relocation.Target{ .local = local_sym_index }; |
| 366 | } |
| 367 | |
| 368 | const sym = context.object.symtab.items[rel.r_symbolnum]; |
| 369 | const sym_name = context.object.getString(sym.n_strx); |
| 370 | |
| 371 | if (MachO.symbolIsSect(sym) and !MachO.symbolIsExt(sym)) { |
| 372 | const sym_index = context.object.symbol_mapping.get(rel.r_symbolnum) orelse unreachable; |
| 373 | break :target Relocation.Target{ .local = sym_index }; |
| 374 | } |
| 375 | |
| 376 | const n_strx = context.macho_file.strtab_dir.getKeyAdapted( |
| 377 | @as([]const u8, sym_name), |
| 378 | StringIndexAdapter{ |
| 379 | .bytes = &context.macho_file.strtab, |
| 380 | }, |
| 381 | ) orelse unreachable; |
| 382 | break :target Relocation.Target{ .global = n_strx }; |
| 383 | }; |
| 384 | const offset = @intCast(u32, rel.r_address); |
| 782 | 385 | |
| 783 | 386 | switch (arch) { |
| 784 | 387 | .aarch64 => { |
| 785 | | const rel_type = @intToEnum(macho.reloc_type_arm64, rel.r_type); |
| 786 | | switch (rel_type) { |
| 787 | | .ARM64_RELOC_ADDEND => unreachable, |
| 788 | | .ARM64_RELOC_SUBTRACTOR => unreachable, |
| 388 | switch (@intToEnum(macho.reloc_type_arm64, rel.r_type)) { |
| 789 | 389 | .ARM64_RELOC_BRANCH26 => { |
| 790 | | self.parseBranch(rel, &parsed_rel, context); |
| 791 | | }, |
| 792 | | .ARM64_RELOC_UNSIGNED => { |
| 793 | | self.parseUnsigned(rel, &parsed_rel, subtractor, context); |
| 794 | | subtractor = null; |
| 390 | // TODO rewrite relocation |
| 391 | try addStub(target, context); |
| 795 | 392 | }, |
| 796 | | .ARM64_RELOC_PAGE21, |
| 797 | | .ARM64_RELOC_GOT_LOAD_PAGE21, |
| 798 | | .ARM64_RELOC_TLVP_LOAD_PAGE21, |
| 799 | | => { |
| 800 | | self.parsePage(rel, &parsed_rel, addend); |
| 801 | | if (rel_type == .ARM64_RELOC_PAGE21) |
| 802 | | addend = 0; |
| 803 | | }, |
| 804 | | .ARM64_RELOC_PAGEOFF12, |
| 805 | | .ARM64_RELOC_GOT_LOAD_PAGEOFF12, |
| 806 | | .ARM64_RELOC_TLVP_LOAD_PAGEOFF12, |
| 807 | | => { |
| 808 | | self.parsePageOff(rel, &parsed_rel, addend); |
| 809 | | if (rel_type == .ARM64_RELOC_PAGEOFF12) |
| 810 | | addend = 0; |
| 393 | .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => { |
| 394 | // TODO rewrite relocation |
| 395 | try addGotEntry(target, context); |
| 811 | 396 | }, |
| 812 | | .ARM64_RELOC_POINTER_TO_GOT => { |
| 813 | | self.parsePointerToGot(rel, &parsed_rel); |
| 397 | .ARM64_RELOC_UNSIGNED => { |
| 398 | assert(rel.r_extern == 1); |
| 399 | addend = if (rel.r_length == 3) |
| 400 | mem.readIntLittle(i64, self.code.items[offset..][0..8]) |
| 401 | else |
| 402 | mem.readIntLittle(i32, self.code.items[offset..][0..4]); |
| 403 | try self.addPtrBindingOrRebase(rel, target, context); |
| 814 | 404 | }, |
| 405 | else => {}, |
| 815 | 406 | } |
| 816 | 407 | }, |
| 817 | 408 | .x86_64 => { |
| 818 | | switch (@intToEnum(macho.reloc_type_x86_64, rel.r_type)) { |
| 819 | | .X86_64_RELOC_SUBTRACTOR => unreachable, |
| 409 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 410 | switch (rel_type) { |
| 820 | 411 | .X86_64_RELOC_BRANCH => { |
| 821 | | self.parseBranch(rel, &parsed_rel, context); |
| 412 | // TODO rewrite relocation |
| 413 | try addStub(target, context); |
| 414 | }, |
| 415 | .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => { |
| 416 | // TODO rewrite relocation |
| 417 | try addGotEntry(target, context); |
| 418 | addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]); |
| 822 | 419 | }, |
| 823 | 420 | .X86_64_RELOC_UNSIGNED => { |
| 824 | | self.parseUnsigned(rel, &parsed_rel, subtractor, context); |
| 825 | | subtractor = null; |
| 421 | addend = if (rel.r_length == 3) |
| 422 | mem.readIntLittle(i64, self.code.items[offset..][0..8]) |
| 423 | else |
| 424 | mem.readIntLittle(i32, self.code.items[offset..][0..4]); |
| 425 | if (rel.r_extern == 0) { |
| 426 | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 427 | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 428 | addend -= @intCast(i64, target_sect_base_addr); |
| 429 | } |
| 430 | try self.addPtrBindingOrRebase(rel, target, context); |
| 826 | 431 | }, |
| 827 | 432 | .X86_64_RELOC_SIGNED, |
| 828 | 433 | .X86_64_RELOC_SIGNED_1, |
| 829 | 434 | .X86_64_RELOC_SIGNED_2, |
| 830 | 435 | .X86_64_RELOC_SIGNED_4, |
| 831 | 436 | => { |
| 832 | | self.parseSigned(rel, &parsed_rel, context); |
| 833 | | }, |
| 834 | | .X86_64_RELOC_GOT_LOAD, |
| 835 | | .X86_64_RELOC_GOT, |
| 836 | | .X86_64_RELOC_TLV, |
| 837 | | => { |
| 838 | | self.parseLoad(rel, &parsed_rel); |
| 437 | const correction: u3 = switch (rel_type) { |
| 438 | .X86_64_RELOC_SIGNED => 0, |
| 439 | .X86_64_RELOC_SIGNED_1 => 1, |
| 440 | .X86_64_RELOC_SIGNED_2 => 2, |
| 441 | .X86_64_RELOC_SIGNED_4 => 4, |
| 442 | else => unreachable, |
| 443 | }; |
| 444 | addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]) + correction; |
| 445 | if (rel.r_extern == 0) { |
| 446 | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 447 | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 448 | addend += @intCast(i64, context.base_addr + offset + correction + 4) - |
| 449 | @intCast(i64, target_sect_base_addr); |
| 450 | } |
| 839 | 451 | }, |
| 452 | else => {}, |
| 840 | 453 | } |
| 841 | 454 | }, |
| 842 | 455 | else => unreachable, |
| 843 | 456 | } |
| 844 | 457 | |
| 845 | | try self.relocs.append(context.allocator, parsed_rel); |
| 846 | | |
| 847 | | const is_via_got = switch (parsed_rel.payload) { |
| 848 | | .pointer_to_got => true, |
| 849 | | .load => |load| load.kind == .got, |
| 850 | | .page => |page| page.kind == .got, |
| 851 | | .page_off => |page_off| page_off.kind == .got, |
| 852 | | else => false, |
| 853 | | }; |
| 854 | | |
| 855 | | if (is_via_got) blk: { |
| 856 | | const key = MachO.GotIndirectionKey{ |
| 857 | | .where = switch (parsed_rel.where) { |
| 858 | | .local => .local, |
| 859 | | .undef => .undef, |
| 860 | | }, |
| 861 | | .where_index = parsed_rel.where_index, |
| 862 | | }; |
| 863 | | if (context.macho_file.got_entries_map.contains(key)) break :blk; |
| 864 | | |
| 865 | | const atom = try context.macho_file.createGotAtom(key); |
| 866 | | try context.macho_file.got_entries_map.putNoClobber(context.macho_file.base.allocator, key, atom); |
| 867 | | const match = MachO.MatchingSection{ |
| 868 | | .seg = context.macho_file.data_const_segment_cmd_index.?, |
| 869 | | .sect = context.macho_file.got_section_index.?, |
| 870 | | }; |
| 871 | | |
| 872 | | if (!context.object.start_atoms.contains(match)) { |
| 873 | | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 874 | | } |
| 875 | | |
| 876 | | if (context.object.end_atoms.getPtr(match)) |last| { |
| 877 | | last.*.next = atom; |
| 878 | | atom.prev = last.*; |
| 879 | | last.* = atom; |
| 880 | | } else { |
| 881 | | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 882 | | } |
| 883 | | } else if (parsed_rel.payload == .unsigned) { |
| 884 | | switch (parsed_rel.where) { |
| 885 | | .undef => { |
| 886 | | try self.bindings.append(context.allocator, .{ |
| 887 | | .local_sym_index = parsed_rel.where_index, |
| 888 | | .offset = parsed_rel.offset, |
| 889 | | }); |
| 890 | | }, |
| 891 | | .local => { |
| 892 | | const source_sym = context.macho_file.locals.items[self.local_sym_index]; |
| 893 | | const match = context.macho_file.section_ordinals.keys()[source_sym.n_sect - 1]; |
| 894 | | const seg = context.macho_file.load_commands.items[match.seg].Segment; |
| 895 | | const sect = seg.sections.items[match.sect]; |
| 896 | | const sect_type = commands.sectionType(sect); |
| 897 | | |
| 898 | | const should_rebase = rebase: { |
| 899 | | if (!parsed_rel.payload.unsigned.is_64bit) break :rebase false; |
| 900 | | |
| 901 | | // TODO actually, a check similar to what dyld is doing, that is, verifying |
| 902 | | // that the segment is writable should be enough here. |
| 903 | | const is_right_segment = blk: { |
| 904 | | if (context.macho_file.data_segment_cmd_index) |idx| { |
| 905 | | if (match.seg == idx) { |
| 906 | | break :blk true; |
| 907 | | } |
| 908 | | } |
| 909 | | if (context.macho_file.data_const_segment_cmd_index) |idx| { |
| 910 | | if (match.seg == idx) { |
| 911 | | break :blk true; |
| 912 | | } |
| 913 | | } |
| 914 | | break :blk false; |
| 915 | | }; |
| 916 | | |
| 917 | | if (!is_right_segment) break :rebase false; |
| 918 | | if (sect_type != macho.S_LITERAL_POINTERS and |
| 919 | | sect_type != macho.S_REGULAR and |
| 920 | | sect_type != macho.S_MOD_INIT_FUNC_POINTERS and |
| 921 | | sect_type != macho.S_MOD_TERM_FUNC_POINTERS) |
| 922 | | { |
| 923 | | break :rebase false; |
| 924 | | } |
| 925 | | |
| 926 | | break :rebase true; |
| 927 | | }; |
| 458 | try self.relocs.append(context.allocator, .{ |
| 459 | .offset = offset, |
| 460 | .target = target, |
| 461 | .addend = addend, |
| 462 | .subtractor = subtractor, |
| 463 | .pcrel = rel.r_pcrel == 1, |
| 464 | .length = rel.r_length, |
| 465 | .@"type" = rel.r_type, |
| 466 | }); |
| 928 | 467 | |
| 929 | | if (should_rebase) { |
| 930 | | try self.rebases.append(context.allocator, parsed_rel.offset); |
| 931 | | } |
| 932 | | }, |
| 933 | | } |
| 934 | | } else if (parsed_rel.payload == .branch) blk: { |
| 935 | | if (parsed_rel.where != .undef) break :blk; |
| 936 | | if (context.macho_file.stubs_map.contains(parsed_rel.where_index)) break :blk; |
| 937 | | |
| 938 | | // TODO clean this up! |
| 939 | | const stub_helper_atom = atom: { |
| 940 | | const atom = try context.macho_file.createStubHelperAtom(); |
| 941 | | const match = MachO.MatchingSection{ |
| 942 | | .seg = context.macho_file.text_segment_cmd_index.?, |
| 943 | | .sect = context.macho_file.stub_helper_section_index.?, |
| 944 | | }; |
| 945 | | if (!context.object.start_atoms.contains(match)) { |
| 946 | | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 947 | | } |
| 948 | | if (context.object.end_atoms.getPtr(match)) |last| { |
| 949 | | last.*.next = atom; |
| 950 | | atom.prev = last.*; |
| 951 | | last.* = atom; |
| 952 | | } else { |
| 953 | | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 954 | | } |
| 955 | | break :atom atom; |
| 956 | | }; |
| 957 | | const laptr_atom = atom: { |
| 958 | | const atom = try context.macho_file.createLazyPointerAtom( |
| 959 | | stub_helper_atom.local_sym_index, |
| 960 | | parsed_rel.where_index, |
| 961 | | ); |
| 962 | | const match = MachO.MatchingSection{ |
| 963 | | .seg = context.macho_file.data_segment_cmd_index.?, |
| 964 | | .sect = context.macho_file.la_symbol_ptr_section_index.?, |
| 965 | | }; |
| 966 | | if (!context.object.start_atoms.contains(match)) { |
| 967 | | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 968 | | } |
| 969 | | if (context.object.end_atoms.getPtr(match)) |last| { |
| 970 | | last.*.next = atom; |
| 971 | | atom.prev = last.*; |
| 972 | | last.* = atom; |
| 973 | | } else { |
| 974 | | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 975 | | } |
| 976 | | break :atom atom; |
| 977 | | }; |
| 978 | | { |
| 979 | | const atom = try context.macho_file.createStubAtom(laptr_atom.local_sym_index); |
| 980 | | const match = MachO.MatchingSection{ |
| 981 | | .seg = context.macho_file.text_segment_cmd_index.?, |
| 982 | | .sect = context.macho_file.stubs_section_index.?, |
| 983 | | }; |
| 984 | | if (!context.object.start_atoms.contains(match)) { |
| 985 | | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 986 | | } |
| 987 | | if (context.object.end_atoms.getPtr(match)) |last| { |
| 988 | | last.*.next = atom; |
| 989 | | atom.prev = last.*; |
| 990 | | last.* = atom; |
| 991 | | } else { |
| 992 | | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 993 | | } |
| 994 | | try context.macho_file.stubs_map.putNoClobber(context.allocator, parsed_rel.where_index, atom); |
| 995 | | } |
| 996 | | } |
| 468 | addend = 0; |
| 469 | subtractor = null; |
| 997 | 470 | } |
| 998 | 471 | } |
| 999 | 472 | |
| 1000 | | fn isAddend(rel: macho.relocation_info, arch: Arch) bool { |
| 1001 | | if (arch != .aarch64) return false; |
| 1002 | | return @intToEnum(macho.reloc_type_arm64, rel.r_type) == .ARM64_RELOC_ADDEND; |
| 1003 | | } |
| 1004 | | |
| 1005 | | fn isSubtractor(rel: macho.relocation_info, arch: Arch) bool { |
| 1006 | | return switch (arch) { |
| 1007 | | .aarch64 => @intToEnum(macho.reloc_type_arm64, rel.r_type) == .ARM64_RELOC_SUBTRACTOR, |
| 1008 | | .x86_64 => @intToEnum(macho.reloc_type_x86_64, rel.r_type) == .X86_64_RELOC_SUBTRACTOR, |
| 1009 | | else => unreachable, |
| 1010 | | }; |
| 1011 | | } |
| 1012 | | |
| 1013 | | fn parseUnsigned( |
| 1014 | | self: Atom, |
| 473 | fn addPtrBindingOrRebase( |
| 474 | self: *Atom, |
| 1015 | 475 | rel: macho.relocation_info, |
| 1016 | | out: *Relocation, |
| 1017 | | subtractor: ?u32, |
| 476 | target: Relocation.Target, |
| 1018 | 477 | context: RelocContext, |
| 1019 | | ) void { |
| 1020 | | assert(rel.r_pcrel == 0); |
| 1021 | | |
| 1022 | | const is_64bit: bool = switch (rel.r_length) { |
| 1023 | | 3 => true, |
| 1024 | | 2 => false, |
| 1025 | | else => unreachable, |
| 1026 | | }; |
| 1027 | | |
| 1028 | | var addend: i64 = if (is_64bit) |
| 1029 | | mem.readIntLittle(i64, self.code.items[out.offset..][0..8]) |
| 1030 | | else |
| 1031 | | mem.readIntLittle(i32, self.code.items[out.offset..][0..4]); |
| 1032 | | |
| 1033 | | if (rel.r_extern == 0) { |
| 1034 | | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 1035 | | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 1036 | | addend -= @intCast(i64, target_sect_base_addr); |
| 1037 | | } |
| 1038 | | |
| 1039 | | out.payload = .{ |
| 1040 | | .unsigned = .{ |
| 1041 | | .subtractor = subtractor, |
| 1042 | | .is_64bit = is_64bit, |
| 1043 | | .addend = addend, |
| 1044 | | }, |
| 1045 | | }; |
| 1046 | | } |
| 1047 | | |
| 1048 | | fn parseBranch(self: Atom, rel: macho.relocation_info, out: *Relocation, context: RelocContext) void { |
| 1049 | | _ = self; |
| 1050 | | assert(rel.r_pcrel == 1); |
| 1051 | | assert(rel.r_length == 2); |
| 1052 | | |
| 1053 | | out.payload = .{ |
| 1054 | | .branch = .{ |
| 1055 | | .arch = context.macho_file.base.options.target.cpu.arch, |
| 478 | ) !void { |
| 479 | switch (target) { |
| 480 | .global => |n_strx| { |
| 481 | try self.bindings.append(context.allocator, .{ |
| 482 | .n_strx = n_strx, |
| 483 | .offset = @intCast(u32, rel.r_address), |
| 484 | }); |
| 1056 | 485 | }, |
| 1057 | | }; |
| 1058 | | } |
| 486 | .local => { |
| 487 | const source_sym = context.macho_file.locals.items[self.local_sym_index]; |
| 488 | const match = context.macho_file.section_ordinals.keys()[source_sym.n_sect - 1]; |
| 489 | const seg = context.macho_file.load_commands.items[match.seg].Segment; |
| 490 | const sect = seg.sections.items[match.sect]; |
| 491 | const sect_type = commands.sectionType(sect); |
| 492 | |
| 493 | const should_rebase = rebase: { |
| 494 | if (rel.r_length != 3) break :rebase false; |
| 495 | |
| 496 | // TODO actually, a check similar to what dyld is doing, that is, verifying |
| 497 | // that the segment is writable should be enough here. |
| 498 | const is_right_segment = blk: { |
| 499 | if (context.macho_file.data_segment_cmd_index) |idx| { |
| 500 | if (match.seg == idx) { |
| 501 | break :blk true; |
| 502 | } |
| 503 | } |
| 504 | if (context.macho_file.data_const_segment_cmd_index) |idx| { |
| 505 | if (match.seg == idx) { |
| 506 | break :blk true; |
| 507 | } |
| 508 | } |
| 509 | break :blk false; |
| 510 | }; |
| 1059 | 511 | |
| 1060 | | fn parsePage(self: Atom, rel: macho.relocation_info, out: *Relocation, addend: u32) void { |
| 1061 | | _ = self; |
| 1062 | | assert(rel.r_pcrel == 1); |
| 1063 | | assert(rel.r_length == 2); |
| 1064 | | |
| 1065 | | out.payload = .{ |
| 1066 | | .page = .{ |
| 1067 | | .kind = switch (@intToEnum(macho.reloc_type_arm64, rel.r_type)) { |
| 1068 | | .ARM64_RELOC_PAGE21 => .page, |
| 1069 | | .ARM64_RELOC_GOT_LOAD_PAGE21 => .got, |
| 1070 | | .ARM64_RELOC_TLVP_LOAD_PAGE21 => .tlvp, |
| 1071 | | else => unreachable, |
| 1072 | | }, |
| 1073 | | .addend = addend, |
| 1074 | | }, |
| 1075 | | }; |
| 1076 | | } |
| 512 | if (!is_right_segment) break :rebase false; |
| 513 | if (sect_type != macho.S_LITERAL_POINTERS and |
| 514 | sect_type != macho.S_REGULAR and |
| 515 | sect_type != macho.S_MOD_INIT_FUNC_POINTERS and |
| 516 | sect_type != macho.S_MOD_TERM_FUNC_POINTERS) |
| 517 | { |
| 518 | break :rebase false; |
| 519 | } |
| 1077 | 520 | |
| 1078 | | fn parsePageOff(self: Atom, rel: macho.relocation_info, out: *Relocation, addend: u32) void { |
| 1079 | | assert(rel.r_pcrel == 0); |
| 1080 | | assert(rel.r_length == 2); |
| 1081 | | |
| 1082 | | const rel_type = @intToEnum(macho.reloc_type_arm64, rel.r_type); |
| 1083 | | const op_kind: ?Relocation.PageOff.OpKind = blk: { |
| 1084 | | if (rel_type != .ARM64_RELOC_PAGEOFF12) break :blk null; |
| 1085 | | const op_kind: Relocation.PageOff.OpKind = if (isArithmeticOp(self.code.items[out.offset..][0..4])) |
| 1086 | | .arithmetic |
| 1087 | | else |
| 1088 | | .load; |
| 1089 | | break :blk op_kind; |
| 1090 | | }; |
| 521 | break :rebase true; |
| 522 | }; |
| 1091 | 523 | |
| 1092 | | out.payload = .{ |
| 1093 | | .page_off = .{ |
| 1094 | | .kind = switch (rel_type) { |
| 1095 | | .ARM64_RELOC_PAGEOFF12 => .page, |
| 1096 | | .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => .got, |
| 1097 | | .ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => .tlvp, |
| 1098 | | else => unreachable, |
| 1099 | | }, |
| 1100 | | .addend = addend, |
| 1101 | | .op_kind = op_kind, |
| 524 | if (should_rebase) { |
| 525 | try self.rebases.append(context.allocator, @intCast(u32, rel.r_address)); |
| 526 | } |
| 1102 | 527 | }, |
| 1103 | | }; |
| 528 | } |
| 1104 | 529 | } |
| 1105 | 530 | |
| 1106 | | fn parsePointerToGot(self: Atom, rel: macho.relocation_info, out: *Relocation) void { |
| 1107 | | _ = self; |
| 1108 | | assert(rel.r_pcrel == 1); |
| 1109 | | assert(rel.r_length == 2); |
| 1110 | | |
| 1111 | | out.payload = .{ |
| 1112 | | .pointer_to_got = .{}, |
| 531 | fn addGotEntry(target: Relocation.Target, context: RelocContext) !void { |
| 532 | if (context.macho_file.got_entries_map.contains(target)) return; |
| 533 | const atom = try context.macho_file.createGotAtom(target); |
| 534 | try context.macho_file.got_entries_map.putNoClobber(context.macho_file.base.allocator, target, atom); |
| 535 | const match = MachO.MatchingSection{ |
| 536 | .seg = context.macho_file.data_const_segment_cmd_index.?, |
| 537 | .sect = context.macho_file.got_section_index.?, |
| 1113 | 538 | }; |
| 539 | if (!context.object.start_atoms.contains(match)) { |
| 540 | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 541 | } |
| 542 | if (context.object.end_atoms.getPtr(match)) |last| { |
| 543 | last.*.next = atom; |
| 544 | atom.prev = last.*; |
| 545 | last.* = atom; |
| 546 | } else { |
| 547 | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 548 | } |
| 1114 | 549 | } |
| 1115 | 550 | |
| 1116 | | fn parseSigned(self: Atom, rel: macho.relocation_info, out: *Relocation, context: RelocContext) void { |
| 1117 | | assert(rel.r_pcrel == 1); |
| 1118 | | assert(rel.r_length == 2); |
| 1119 | | |
| 1120 | | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 1121 | | const correction: u3 = switch (rel_type) { |
| 1122 | | .X86_64_RELOC_SIGNED => 0, |
| 1123 | | .X86_64_RELOC_SIGNED_1 => 1, |
| 1124 | | .X86_64_RELOC_SIGNED_2 => 2, |
| 1125 | | .X86_64_RELOC_SIGNED_4 => 4, |
| 1126 | | else => unreachable, |
| 551 | fn addStub(target: Relocation.Target, context: RelocContext) !void { |
| 552 | if (target != .global) return; |
| 553 | if (context.macho_file.stubs_map.contains(target.global)) return; |
| 554 | // TODO clean this up! |
| 555 | const stub_helper_atom = atom: { |
| 556 | const atom = try context.macho_file.createStubHelperAtom(); |
| 557 | const match = MachO.MatchingSection{ |
| 558 | .seg = context.macho_file.text_segment_cmd_index.?, |
| 559 | .sect = context.macho_file.stub_helper_section_index.?, |
| 560 | }; |
| 561 | if (!context.object.start_atoms.contains(match)) { |
| 562 | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 563 | } |
| 564 | if (context.object.end_atoms.getPtr(match)) |last| { |
| 565 | last.*.next = atom; |
| 566 | atom.prev = last.*; |
| 567 | last.* = atom; |
| 568 | } else { |
| 569 | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 570 | } |
| 571 | break :atom atom; |
| 1127 | 572 | }; |
| 1128 | | var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction; |
| 1129 | | |
| 1130 | | if (rel.r_extern == 0) { |
| 1131 | | const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; |
| 1132 | | const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; |
| 1133 | | addend += @intCast(i64, context.base_addr + out.offset + correction + 4) - @intCast(i64, target_sect_base_addr); |
| 1134 | | } |
| 1135 | | |
| 1136 | | out.payload = .{ |
| 1137 | | .signed = .{ |
| 1138 | | .correction = correction, |
| 1139 | | .addend = addend, |
| 1140 | | }, |
| 573 | const laptr_atom = atom: { |
| 574 | const atom = try context.macho_file.createLazyPointerAtom( |
| 575 | stub_helper_atom.local_sym_index, |
| 576 | target.global, |
| 577 | ); |
| 578 | const match = MachO.MatchingSection{ |
| 579 | .seg = context.macho_file.data_segment_cmd_index.?, |
| 580 | .sect = context.macho_file.la_symbol_ptr_section_index.?, |
| 581 | }; |
| 582 | if (!context.object.start_atoms.contains(match)) { |
| 583 | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 584 | } |
| 585 | if (context.object.end_atoms.getPtr(match)) |last| { |
| 586 | last.*.next = atom; |
| 587 | atom.prev = last.*; |
| 588 | last.* = atom; |
| 589 | } else { |
| 590 | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 591 | } |
| 592 | break :atom atom; |
| 1141 | 593 | }; |
| 1142 | | } |
| 1143 | | |
| 1144 | | fn parseLoad(self: Atom, rel: macho.relocation_info, out: *Relocation) void { |
| 1145 | | assert(rel.r_pcrel == 1); |
| 1146 | | assert(rel.r_length == 2); |
| 1147 | | |
| 1148 | | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 1149 | | const addend: i32 = if (rel_type == .X86_64_RELOC_GOT) |
| 1150 | | mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) |
| 1151 | | else |
| 1152 | | 0; |
| 1153 | | |
| 1154 | | out.payload = .{ |
| 1155 | | .load = .{ |
| 1156 | | .kind = switch (rel_type) { |
| 1157 | | .X86_64_RELOC_GOT_LOAD, .X86_64_RELOC_GOT => .got, |
| 1158 | | .X86_64_RELOC_TLV => .tlvp, |
| 1159 | | else => unreachable, |
| 1160 | | }, |
| 1161 | | .addend = addend, |
| 1162 | | }, |
| 594 | const atom = try context.macho_file.createStubAtom(laptr_atom.local_sym_index); |
| 595 | const match = MachO.MatchingSection{ |
| 596 | .seg = context.macho_file.text_segment_cmd_index.?, |
| 597 | .sect = context.macho_file.stubs_section_index.?, |
| 1163 | 598 | }; |
| 599 | if (!context.object.start_atoms.contains(match)) { |
| 600 | try context.object.start_atoms.putNoClobber(context.allocator, match, atom); |
| 601 | } |
| 602 | if (context.object.end_atoms.getPtr(match)) |last| { |
| 603 | last.*.next = atom; |
| 604 | atom.prev = last.*; |
| 605 | last.* = atom; |
| 606 | } else { |
| 607 | try context.object.end_atoms.putNoClobber(context.allocator, match, atom); |
| 608 | } |
| 609 | try context.macho_file.stubs_map.putNoClobber(context.allocator, target.global, atom); |
| 1164 | 610 | } |
| 1165 | 611 | |
| 1166 | 612 | pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void { |
| ... | ... | @@ -1169,42 +615,42 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void { |
| 1169 | 615 | |
| 1170 | 616 | for (self.relocs.items) |rel| { |
| 1171 | 617 | log.debug("relocating {}", .{rel}); |
| 1172 | | |
| 618 | const arch = macho_file.base.options.target.cpu.arch; |
| 1173 | 619 | const source_addr = blk: { |
| 1174 | 620 | const sym = macho_file.locals.items[self.local_sym_index]; |
| 1175 | 621 | break :blk sym.n_value + rel.offset; |
| 1176 | 622 | }; |
| 1177 | 623 | const target_addr = blk: { |
| 1178 | | const is_via_got = switch (rel.payload) { |
| 1179 | | .pointer_to_got => true, |
| 1180 | | .page => |page| page.kind == .got, |
| 1181 | | .page_off => |page_off| page_off.kind == .got, |
| 1182 | | .load => |load| load.kind == .got, |
| 1183 | | else => false, |
| 624 | const is_via_got = got: { |
| 625 | switch (arch) { |
| 626 | .aarch64 => break :got switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) { |
| 627 | .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => true, |
| 628 | else => false, |
| 629 | }, |
| 630 | .x86_64 => break :got switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { |
| 631 | .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true, |
| 632 | else => false, |
| 633 | }, |
| 634 | else => unreachable, |
| 635 | } |
| 1184 | 636 | }; |
| 1185 | 637 | |
| 1186 | 638 | if (is_via_got) { |
| 1187 | | const atom = macho_file.got_entries_map.get(.{ |
| 1188 | | .where = switch (rel.where) { |
| 1189 | | .local => .local, |
| 1190 | | .undef => .undef, |
| 1191 | | }, |
| 1192 | | .where_index = rel.where_index, |
| 1193 | | }) orelse { |
| 1194 | | const sym = switch (rel.where) { |
| 1195 | | .local => macho_file.locals.items[rel.where_index], |
| 1196 | | .undef => macho_file.undefs.items[rel.where_index], |
| 639 | const atom = macho_file.got_entries_map.get(rel.target) orelse { |
| 640 | const n_strx = switch (rel.target) { |
| 641 | .local => |sym_index| macho_file.locals.items[sym_index].n_strx, |
| 642 | .global => |n_strx| n_strx, |
| 1197 | 643 | }; |
| 1198 | | log.err("expected GOT entry for symbol '{s}'", .{macho_file.getString(sym.n_strx)}); |
| 644 | log.err("expected GOT entry for symbol '{s}'", .{macho_file.getString(n_strx)}); |
| 1199 | 645 | log.err(" this is an internal linker error", .{}); |
| 1200 | 646 | return error.FailedToResolveRelocationTarget; |
| 1201 | 647 | }; |
| 1202 | 648 | break :blk macho_file.locals.items[atom.local_sym_index].n_value; |
| 1203 | 649 | } |
| 1204 | 650 | |
| 1205 | | switch (rel.where) { |
| 1206 | | .local => { |
| 1207 | | const sym = macho_file.locals.items[rel.where_index]; |
| 651 | switch (rel.target) { |
| 652 | .local => |sym_index| { |
| 653 | const sym = macho_file.locals.items[sym_index]; |
| 1208 | 654 | const is_tlv = is_tlv: { |
| 1209 | 655 | const source_sym = macho_file.locals.items[self.local_sym_index]; |
| 1210 | 656 | const match = macho_file.section_ordinals.keys()[source_sym.n_sect - 1]; |
| ... | ... | @@ -1233,28 +679,23 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void { |
| 1233 | 679 | }; |
| 1234 | 680 | break :blk sym.n_value - base_address; |
| 1235 | 681 | } |
| 1236 | | |
| 1237 | 682 | break :blk sym.n_value; |
| 1238 | 683 | }, |
| 1239 | | .undef => { |
| 1240 | | const atom = macho_file.stubs_map.get(rel.where_index) orelse { |
| 1241 | | // TODO this is required for incremental when we don't have every symbol |
| 1242 | | // resolved when creating relocations. In this case, we will insert a branch |
| 1243 | | // reloc to an undef symbol which may happen to be defined within the binary. |
| 1244 | | // Then, the undef we point at will be a null symbol (free symbol) which we |
| 1245 | | // should remove/repurpose. To circumvent this (for now), we check if the symbol |
| 1246 | | // we point to is garbage, and if so we fall back to symbol resolver to find by name. |
| 1247 | | const n_strx = macho_file.undefs.items[rel.where_index].n_strx; |
| 1248 | | if (macho_file.symbol_resolver.get(n_strx)) |resolv| inner: { |
| 1249 | | if (resolv.where != .global) break :inner; |
| 1250 | | break :blk macho_file.globals.items[resolv.where_index].n_value; |
| 1251 | | } |
| 1252 | | |
| 1253 | | // TODO verify in TextBlock that the symbol is indeed dynamically bound. |
| 1254 | | break :blk 0; // Dynamically bound by dyld. |
| 1255 | | }; |
| 1256 | | |
| 1257 | | break :blk macho_file.locals.items[atom.local_sym_index].n_value; |
| 684 | .global => |n_strx| { |
| 685 | // TODO Still trying to figure out how to possibly use stubs for local symbol indirection with |
| 686 | // branching instructions. If it is not possible, then the best course of action is to |
| 687 | // resurrect the former approach of defering creating synthethic atoms in __got and __la_symbol_ptr |
| 688 | // sections until we resolve the relocations. |
| 689 | const resolv = macho_file.symbol_resolver.get(n_strx).?; |
| 690 | switch (resolv.where) { |
| 691 | .global => break :blk macho_file.globals.items[resolv.where_index].n_value, |
| 692 | .undef => { |
| 693 | break :blk if (macho_file.stubs_map.get(n_strx)) |atom| |
| 694 | macho_file.locals.items[atom.local_sym_index].n_value |
| 695 | else |
| 696 | 0; |
| 697 | }, |
| 698 | } |
| 1258 | 699 | }, |
| 1259 | 700 | } |
| 1260 | 701 | }; |
| ... | ... | @@ -1262,67 +703,248 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void { |
| 1262 | 703 | log.debug(" | source_addr = 0x{x}", .{source_addr}); |
| 1263 | 704 | log.debug(" | target_addr = 0x{x}", .{target_addr}); |
| 1264 | 705 | |
| 1265 | | try rel.resolve(.{ |
| 1266 | | .block = self, |
| 1267 | | .offset = rel.offset, |
| 1268 | | .source_addr = source_addr, |
| 1269 | | .target_addr = target_addr, |
| 1270 | | .macho_file = macho_file, |
| 1271 | | }); |
| 1272 | | } |
| 1273 | | } |
| 1274 | | |
| 1275 | | pub fn format(self: Atom, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 1276 | | _ = fmt; |
| 1277 | | _ = options; |
| 1278 | | try std.fmt.format(writer, "TextBlock {{ ", .{}); |
| 1279 | | try std.fmt.format(writer, ".local_sym_index = {d}, ", .{self.local_sym_index}); |
| 1280 | | try std.fmt.format(writer, ".aliases = {any}, ", .{self.aliases.items}); |
| 1281 | | try std.fmt.format(writer, ".contained = {any}, ", .{self.contained.items}); |
| 1282 | | try std.fmt.format(writer, ".code = {*}, ", .{self.code.items}); |
| 1283 | | try std.fmt.format(writer, ".size = {d}, ", .{self.size}); |
| 1284 | | try std.fmt.format(writer, ".alignment = {d}, ", .{self.alignment}); |
| 1285 | | try std.fmt.format(writer, ".relocs = {any}, ", .{self.relocs.items}); |
| 1286 | | try std.fmt.format(writer, ".rebases = {any}, ", .{self.rebases.items}); |
| 1287 | | try std.fmt.format(writer, ".bindings = {any}, ", .{self.bindings.items}); |
| 1288 | | try std.fmt.format(writer, ".dices = {any}, ", .{self.dices.items}); |
| 1289 | | if (self.stab) |stab| { |
| 1290 | | try std.fmt.format(writer, ".stab = {any}, ", .{stab}); |
| 1291 | | } |
| 1292 | | try std.fmt.format(writer, "}}", .{}); |
| 1293 | | } |
| 706 | switch (arch) { |
| 707 | .aarch64 => { |
| 708 | switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) { |
| 709 | .ARM64_RELOC_BRANCH26 => { |
| 710 | const displacement = math.cast( |
| 711 | i28, |
| 712 | @intCast(i64, target_addr) - @intCast(i64, source_addr), |
| 713 | ) catch |err| switch (err) { |
| 714 | error.Overflow => { |
| 715 | log.err("jump too big to encode as i28 displacement value", .{}); |
| 716 | log.err(" (target - source) = displacement => 0x{x} - 0x{x} = 0x{x}", .{ |
| 717 | target_addr, |
| 718 | source_addr, |
| 719 | @intCast(i64, target_addr) - @intCast(i64, source_addr), |
| 720 | }); |
| 721 | log.err(" TODO implement branch islands to extend jump distance for arm64", .{}); |
| 722 | return error.TODOImplementBranchIslands; |
| 723 | }, |
| 724 | }; |
| 725 | const code = self.code.items[rel.offset..][0..4]; |
| 726 | var inst = aarch64.Instruction{ |
| 727 | .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload( |
| 728 | aarch64.Instruction, |
| 729 | aarch64.Instruction.unconditional_branch_immediate, |
| 730 | ), code), |
| 731 | }; |
| 732 | inst.unconditional_branch_immediate.imm26 = @truncate(u26, @bitCast(u28, displacement >> 2)); |
| 733 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 734 | }, |
| 735 | .ARM64_RELOC_PAGE21, |
| 736 | .ARM64_RELOC_GOT_LOAD_PAGE21, |
| 737 | .ARM64_RELOC_TLVP_LOAD_PAGE21, |
| 738 | => { |
| 739 | const actual_target_addr = @intCast(i64, target_addr) + rel.addend; |
| 740 | const source_page = @intCast(i32, source_addr >> 12); |
| 741 | const target_page = @intCast(i32, actual_target_addr >> 12); |
| 742 | const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); |
| 743 | const code = self.code.items[rel.offset..][0..4]; |
| 744 | var inst = aarch64.Instruction{ |
| 745 | .pc_relative_address = mem.bytesToValue(meta.TagPayload( |
| 746 | aarch64.Instruction, |
| 747 | aarch64.Instruction.pc_relative_address, |
| 748 | ), code), |
| 749 | }; |
| 750 | inst.pc_relative_address.immhi = @truncate(u19, pages >> 2); |
| 751 | inst.pc_relative_address.immlo = @truncate(u2, pages); |
| 752 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 753 | }, |
| 754 | .ARM64_RELOC_PAGEOFF12 => { |
| 755 | const code = self.code.items[rel.offset..][0..4]; |
| 756 | const actual_target_addr = @intCast(i64, target_addr) + rel.addend; |
| 757 | const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); |
| 758 | if (isArithmeticOp(self.code.items[rel.offset..][0..4])) { |
| 759 | var inst = aarch64.Instruction{ |
| 760 | .add_subtract_immediate = mem.bytesToValue(meta.TagPayload( |
| 761 | aarch64.Instruction, |
| 762 | aarch64.Instruction.add_subtract_immediate, |
| 763 | ), code), |
| 764 | }; |
| 765 | inst.add_subtract_immediate.imm12 = narrowed; |
| 766 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 767 | } else { |
| 768 | var inst = aarch64.Instruction{ |
| 769 | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| 770 | aarch64.Instruction, |
| 771 | aarch64.Instruction.load_store_register, |
| 772 | ), code), |
| 773 | }; |
| 774 | const offset: u12 = blk: { |
| 775 | if (inst.load_store_register.size == 0) { |
| 776 | if (inst.load_store_register.v == 1) { |
| 777 | // 128-bit SIMD is scaled by 16. |
| 778 | break :blk try math.divExact(u12, narrowed, 16); |
| 779 | } |
| 780 | // Otherwise, 8-bit SIMD or ldrb. |
| 781 | break :blk narrowed; |
| 782 | } else { |
| 783 | const denom: u4 = try math.powi(u4, 2, inst.load_store_register.size); |
| 784 | break :blk try math.divExact(u12, narrowed, denom); |
| 785 | } |
| 786 | }; |
| 787 | inst.load_store_register.offset = offset; |
| 788 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 789 | } |
| 790 | }, |
| 791 | .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => { |
| 792 | const code = self.code.items[rel.offset..][0..4]; |
| 793 | const actual_target_addr = @intCast(i64, target_addr) + rel.addend; |
| 794 | const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); |
| 795 | var inst: aarch64.Instruction = .{ |
| 796 | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| 797 | aarch64.Instruction, |
| 798 | aarch64.Instruction.load_store_register, |
| 799 | ), code), |
| 800 | }; |
| 801 | const offset = try math.divExact(u12, narrowed, 8); |
| 802 | inst.load_store_register.offset = offset; |
| 803 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 804 | }, |
| 805 | .ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => { |
| 806 | const code = self.code.items[rel.offset..][0..4]; |
| 807 | const RegInfo = struct { |
| 808 | rd: u5, |
| 809 | rn: u5, |
| 810 | size: u1, |
| 811 | }; |
| 812 | const reg_info: RegInfo = blk: { |
| 813 | if (isArithmeticOp(code)) { |
| 814 | const inst = mem.bytesToValue(meta.TagPayload( |
| 815 | aarch64.Instruction, |
| 816 | aarch64.Instruction.add_subtract_immediate, |
| 817 | ), code); |
| 818 | break :blk .{ |
| 819 | .rd = inst.rd, |
| 820 | .rn = inst.rn, |
| 821 | .size = inst.sf, |
| 822 | }; |
| 823 | } else { |
| 824 | const inst = mem.bytesToValue(meta.TagPayload( |
| 825 | aarch64.Instruction, |
| 826 | aarch64.Instruction.load_store_register, |
| 827 | ), code); |
| 828 | break :blk .{ |
| 829 | .rd = inst.rt, |
| 830 | .rn = inst.rn, |
| 831 | .size = @truncate(u1, inst.size), |
| 832 | }; |
| 833 | } |
| 834 | }; |
| 835 | const actual_target_addr = @intCast(i64, target_addr) + rel.addend; |
| 836 | const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); |
| 837 | var inst = aarch64.Instruction{ |
| 838 | .add_subtract_immediate = .{ |
| 839 | .rd = reg_info.rd, |
| 840 | .rn = reg_info.rn, |
| 841 | .imm12 = narrowed, |
| 842 | .sh = 0, |
| 843 | .s = 0, |
| 844 | .op = 0, |
| 845 | .sf = reg_info.size, |
| 846 | }, |
| 847 | }; |
| 848 | mem.writeIntLittle(u32, code, inst.toU32()); |
| 849 | }, |
| 850 | .ARM64_RELOC_POINTER_TO_GOT => { |
| 851 | const result = try math.cast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr)); |
| 852 | mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, result)); |
| 853 | }, |
| 854 | .ARM64_RELOC_UNSIGNED => { |
| 855 | const result = blk: { |
| 856 | if (rel.subtractor) |subtractor| { |
| 857 | const sym = macho_file.locals.items[subtractor]; |
| 858 | break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend; |
| 859 | } else { |
| 860 | break :blk @intCast(i64, target_addr) + rel.addend; |
| 861 | } |
| 862 | }; |
| 1294 | 863 | |
| 1295 | | const RelocIterator = struct { |
| 1296 | | buffer: []const macho.relocation_info, |
| 1297 | | index: i32 = -1, |
| 864 | if (rel.length == 3) { |
| 865 | mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result)); |
| 866 | } else { |
| 867 | mem.writeIntLittle( |
| 868 | u32, |
| 869 | self.code.items[rel.offset..][0..4], |
| 870 | @truncate(u32, @bitCast(u64, result)), |
| 871 | ); |
| 872 | } |
| 873 | }, |
| 874 | .ARM64_RELOC_SUBTRACTOR => unreachable, |
| 875 | .ARM64_RELOC_ADDEND => unreachable, |
| 876 | } |
| 877 | }, |
| 878 | .x86_64 => { |
| 879 | switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { |
| 880 | .X86_64_RELOC_BRANCH => { |
| 881 | const displacement = try math.cast( |
| 882 | i32, |
| 883 | @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4, |
| 884 | ); |
| 885 | mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); |
| 886 | }, |
| 887 | .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => { |
| 888 | const displacement = try math.cast( |
| 889 | i32, |
| 890 | @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend, |
| 891 | ); |
| 892 | mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); |
| 893 | }, |
| 894 | .X86_64_RELOC_TLV => { |
| 895 | // We need to rewrite the opcode from movq to leaq. |
| 896 | self.code.items[rel.offset - 2] = 0x8d; |
| 897 | const displacement = try math.cast( |
| 898 | i32, |
| 899 | @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend, |
| 900 | ); |
| 901 | mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); |
| 902 | }, |
| 903 | .X86_64_RELOC_SIGNED, |
| 904 | .X86_64_RELOC_SIGNED_1, |
| 905 | .X86_64_RELOC_SIGNED_2, |
| 906 | .X86_64_RELOC_SIGNED_4, |
| 907 | => { |
| 908 | const correction: u3 = switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { |
| 909 | .X86_64_RELOC_SIGNED => 0, |
| 910 | .X86_64_RELOC_SIGNED_1 => 1, |
| 911 | .X86_64_RELOC_SIGNED_2 => 2, |
| 912 | .X86_64_RELOC_SIGNED_4 => 4, |
| 913 | else => unreachable, |
| 914 | }; |
| 915 | const actual_target_addr = @intCast(i64, target_addr) + rel.addend; |
| 916 | const displacement = try math.cast( |
| 917 | i32, |
| 918 | actual_target_addr - @intCast(i64, source_addr + correction + 4), |
| 919 | ); |
| 920 | mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); |
| 921 | }, |
| 922 | .X86_64_RELOC_UNSIGNED => { |
| 923 | const result = blk: { |
| 924 | if (rel.subtractor) |subtractor| { |
| 925 | const sym = macho_file.locals.items[subtractor]; |
| 926 | break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend; |
| 927 | } else { |
| 928 | break :blk @intCast(i64, target_addr) + rel.addend; |
| 929 | } |
| 930 | }; |
| 1298 | 931 | |
| 1299 | | pub fn next(self: *RelocIterator) ?macho.relocation_info { |
| 1300 | | self.index += 1; |
| 1301 | | if (self.index < self.buffer.len) { |
| 1302 | | return self.buffer[@intCast(u32, self.index)]; |
| 932 | if (rel.length == 3) { |
| 933 | mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result)); |
| 934 | } else { |
| 935 | mem.writeIntLittle( |
| 936 | u32, |
| 937 | self.code.items[rel.offset..][0..4], |
| 938 | @truncate(u32, @bitCast(u64, result)), |
| 939 | ); |
| 940 | } |
| 941 | }, |
| 942 | .X86_64_RELOC_SUBTRACTOR => unreachable, |
| 943 | } |
| 944 | }, |
| 945 | else => unreachable, |
| 1303 | 946 | } |
| 1304 | | return null; |
| 1305 | 947 | } |
| 1306 | | |
| 1307 | | pub fn peek(self: RelocIterator) macho.relocation_info { |
| 1308 | | assert(self.index + 1 < self.buffer.len); |
| 1309 | | return self.buffer[@intCast(u32, self.index + 1)]; |
| 1310 | | } |
| 1311 | | }; |
| 1312 | | |
| 1313 | | fn filterRelocs(relocs: []macho.relocation_info, start_addr: u64, end_addr: u64) []macho.relocation_info { |
| 1314 | | const Predicate = struct { |
| 1315 | | addr: u64, |
| 1316 | | |
| 1317 | | pub fn predicate(self: @This(), rel: macho.relocation_info) bool { |
| 1318 | | return rel.r_address < self.addr; |
| 1319 | | } |
| 1320 | | }; |
| 1321 | | |
| 1322 | | const start = MachO.findFirst(macho.relocation_info, relocs, 0, Predicate{ .addr = end_addr }); |
| 1323 | | const end = MachO.findFirst(macho.relocation_info, relocs, start, Predicate{ .addr = start_addr }); |
| 1324 | | |
| 1325 | | return relocs[start..end]; |
| 1326 | 948 | } |
| 1327 | 949 | |
| 1328 | 950 | inline fn isArithmeticOp(inst: *const [4]u8) bool { |