| ... | @@ -425,6 +425,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -425,6 +425,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 425 | .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]), | 425 | .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]), |
| 426 | else => unreachable, | 426 | else => unreachable, |
| 427 | }; | 427 | }; |
| | 428 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 428 | | 429 | |
| 429 | // We will use equation format to resolve relocations: | 430 | // We will use equation format to resolve relocations: |
| 430 | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ | 431 | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ |
| ... | @@ -454,14 +455,14 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -454,14 +455,14 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 454 | | 455 | |
| 455 | relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ({s})", .{ | 456 | relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ({s})", .{ |
| 456 | fmtRelocType(r_type), | 457 | fmtRelocType(r_type), |
| 457 | rel.r_offset, | 458 | r_offset, |
| 458 | P, | 459 | P, |
| 459 | S + A, | 460 | S + A, |
| 460 | G + GOT + A, | 461 | G + GOT + A, |
| 461 | target.name(elf_file), | 462 | target.name(elf_file), |
| 462 | }); | 463 | }); |
| 463 | | 464 | |
| 464 | try stream.seekTo(rel.r_offset); | 465 | try stream.seekTo(r_offset); |
| 465 | | 466 | |
| 466 | switch (rel.r_type()) { | 467 | switch (rel.r_type()) { |
| 467 | elf.R_X86_64_NONE => unreachable, | 468 | elf.R_X86_64_NONE => unreachable, |
| ... | @@ -481,7 +482,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -481,7 +482,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 481 | | 482 | |
| 482 | elf.R_X86_64_GOTPCRELX => { | 483 | elf.R_X86_64_GOTPCRELX => { |
| 483 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { | 484 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 484 | x86_64.relaxGotpcrelx(code[rel.r_offset - 2 ..]) catch break :blk; | 485 | x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk; |
| 485 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); | 486 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); |
| 486 | continue; | 487 | continue; |
| 487 | } | 488 | } |
| ... | @@ -490,7 +491,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -490,7 +491,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 490 | | 491 | |
| 491 | elf.R_X86_64_REX_GOTPCRELX => { | 492 | elf.R_X86_64_REX_GOTPCRELX => { |
| 492 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { | 493 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 493 | x86_64.relaxRexGotpcrelx(code[rel.r_offset - 3 ..]) catch break :blk; | 494 | x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk; |
| 494 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); | 495 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); |
| 495 | continue; | 496 | continue; |
| 496 | } | 497 | } |