| ... | ... | @@ -3,6 +3,7 @@ pub const Cie = struct { |
| 3 | 3 | offset: u32, |
| 4 | 4 | out_offset: u32 = 0, |
| 5 | 5 | size: u32, |
| 6 | address_ptr_size: enum { p32, p64 } = .p64, |
| 6 | 7 | lsda_size: ?enum { p32, p64 } = null, |
| 7 | 8 | personality: ?Personality = null, |
| 8 | 9 | file: File.Index = 0, |
| ... | ... | @@ -27,9 +28,15 @@ pub const Cie = struct { |
| 27 | 28 | for (aug[1..]) |ch| switch (ch) { |
| 28 | 29 | 'R' => { |
| 29 | 30 | const enc: DW.EH.PE = @bitCast(try reader.takeByte()); |
| 30 | | if (enc != @as(DW.EH.PE, .{ .type = .absptr, .rel = .pcrel })) { |
| 31 | if (enc.rel != .pcrel) { |
| 31 | 32 | @panic("unexpected pointer encoding"); // TODO error |
| 32 | 33 | } |
| 34 | |
| 35 | switch (enc.type) { |
| 36 | .sdata4 => cie.address_ptr_size = .p32, |
| 37 | .absptr => cie.address_ptr_size = .p64, |
| 38 | else => @panic("unexpected pointer encoding"), // TODO error |
| 39 | } |
| 33 | 40 | }, |
| 34 | 41 | 'P' => { |
| 35 | 42 | const enc: DW.EH.PE = @bitCast(try reader.takeByte()); |
| ... | ... | @@ -131,21 +138,6 @@ pub const Fde = struct { |
| 131 | 138 | const object = fde.getObject(macho_file); |
| 132 | 139 | const sect = object.sections.items(.header)[object.eh_frame_sect_index.?]; |
| 133 | 140 | |
| 134 | | // Parse target atom index |
| 135 | | const pc_begin = std.mem.readInt(i64, data[8..][0..8], .little); |
| 136 | | const taddr: u64 = @intCast(@as(i64, @intCast(sect.addr + fde.offset + 8)) + pc_begin); |
| 137 | | fde.atom = object.findAtom(taddr) orelse { |
| 138 | | try macho_file.reportParseError2(object.index, "{s},{s}: 0x{x}: invalid function reference in FDE", .{ |
| 139 | | sect.segName(), sect.sectName(), fde.offset + 8, |
| 140 | | }); |
| 141 | | return error.MalformedObject; |
| 142 | | }; |
| 143 | | const atom = fde.getAtom(macho_file); |
| 144 | | fde.atom_offset = @intCast(taddr - atom.getInputAddress(macho_file)); |
| 145 | | |
| 146 | | // Parse pc_range (function size) |
| 147 | | fde.pc_range = std.mem.readInt(u64, data[16..][0..8], .little); |
| 148 | | |
| 149 | 141 | // Associate with a CIE |
| 150 | 142 | const cie_ptr = std.mem.readInt(u32, data[4..8], .little); |
| 151 | 143 | const cie_offset = fde.offset + 4 - cie_ptr; |
| ... | ... | @@ -163,10 +155,34 @@ pub const Fde = struct { |
| 163 | 155 | |
| 164 | 156 | const cie = fde.getCie(macho_file); |
| 165 | 157 | |
| 158 | // Parse target atom index |
| 159 | const pc_begin = switch (cie.address_ptr_size) { |
| 160 | .p32 => std.mem.readInt(i32, data[8..][0..4], .little), |
| 161 | .p64 => std.mem.readInt(i64, data[8..][0..8], .little), |
| 162 | }; |
| 163 | const taddr: u64 = @intCast(@as(i64, @intCast(sect.addr + fde.offset + 8)) + pc_begin); |
| 164 | fde.atom = object.findAtom(taddr) orelse { |
| 165 | try macho_file.reportParseError2(object.index, "{s},{s}: 0x{x}: invalid function reference in FDE", .{ |
| 166 | sect.segName(), sect.sectName(), fde.offset + 8, |
| 167 | }); |
| 168 | return error.MalformedObject; |
| 169 | }; |
| 170 | const atom = fde.getAtom(macho_file); |
| 171 | fde.atom_offset = @intCast(taddr - atom.getInputAddress(macho_file)); |
| 172 | |
| 173 | // Parse pc_range (function size) |
| 174 | fde.pc_range = switch (cie.address_ptr_size) { |
| 175 | .p32 => std.mem.readInt(u32, data[12..][0..4], .little), |
| 176 | .p64 => std.mem.readInt(u64, data[16..][0..8], .little), |
| 177 | }; |
| 178 | |
| 166 | 179 | // Parse LSDA atom index if any |
| 167 | 180 | if (cie.lsda_size) |lsda_size| { |
| 168 | 181 | var reader: std.Io.Reader = .fixed(data); |
| 169 | | reader.seek = 24; |
| 182 | reader.seek = switch (cie.address_ptr_size) { |
| 183 | .p32 => 16, |
| 184 | .p64 => 24, |
| 185 | }; |
| 170 | 186 | _ = try reader.takeLeb128(u64); // augmentation length |
| 171 | 187 | fde.lsda_ptr_offset = @intCast(reader.seek); |
| 172 | 188 | const lsda_ptr = switch (lsda_size) { |
| ... | ... | @@ -378,12 +394,21 @@ pub fn write(macho_file: *MachO, buffer: []u8) void { |
| 378 | 394 | const offset = fde.out_offset + 8; |
| 379 | 395 | const saddr = sect.addr + offset; |
| 380 | 396 | const taddr = fde.getAtom(macho_file).getAddress(macho_file) + fde.atom_offset; |
| 381 | | std.mem.writeInt( |
| 382 | | i64, |
| 383 | | buffer[offset..][0..8], |
| 384 | | @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)), |
| 385 | | .little, |
| 386 | | ); |
| 397 | |
| 398 | switch (fde.getCie(macho_file).address_ptr_size) { |
| 399 | .p32 => std.mem.writeInt( |
| 400 | i32, |
| 401 | buffer[offset..][0..4], |
| 402 | @intCast(@as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr))), |
| 403 | .little, |
| 404 | ), |
| 405 | .p64 => std.mem.writeInt( |
| 406 | i64, |
| 407 | buffer[offset..][0..8], |
| 408 | @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)), |
| 409 | .little, |
| 410 | ), |
| 411 | } |
| 387 | 412 | } |
| 388 | 413 | |
| 389 | 414 | if (fde.getLsdaAtom(macho_file)) |atom| { |
| ... | ... | @@ -465,12 +490,21 @@ pub fn writeRelocs(macho_file: *MachO, code: []u8, relocs: []macho.relocation_in |
| 465 | 490 | const offset = fde.out_offset + 8; |
| 466 | 491 | const saddr = sect.addr + offset; |
| 467 | 492 | const taddr = fde.getAtom(macho_file).getAddress(macho_file) + fde.atom_offset; |
| 468 | | std.mem.writeInt( |
| 469 | | i64, |
| 470 | | code[offset..][0..8], |
| 471 | | @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)), |
| 472 | | .little, |
| 473 | | ); |
| 493 | |
| 494 | switch (fde.getCie(macho_file).address_ptr_size) { |
| 495 | .p32 => std.mem.writeInt( |
| 496 | i32, |
| 497 | code[offset..][0..4], |
| 498 | @intCast(@as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr))), |
| 499 | .little, |
| 500 | ), |
| 501 | .p64 => std.mem.writeInt( |
| 502 | i64, |
| 503 | code[offset..][0..8], |
| 504 | @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)), |
| 505 | .little, |
| 506 | ), |
| 507 | } |
| 474 | 508 | } |
| 475 | 509 | |
| 476 | 510 | if (fde.getLsdaAtom(macho_file)) |atom| { |