| ... | ... | @@ -300,7 +300,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void { |
| 300 | 300 | self.* = .{}; |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | | pub fn relocs(self: Atom, elf_file: *Elf) []align(1) const elf.Elf64_Rela { |
| 303 | pub fn relocs(self: Atom, elf_file: *Elf) []const elf.Elf64_Rela { |
| 304 | 304 | const shndx = self.relocsShndx() orelse return &[0]elf.Elf64_Rela{}; |
| 305 | 305 | return switch (self.file(elf_file).?) { |
| 306 | 306 | .zig_object => |x| x.relocs.items[shndx].items, |
| ... | ... | @@ -394,11 +394,62 @@ pub fn scanRelocsRequiresCode(self: Atom, elf_file: *Elf) bool { |
| 394 | 394 | return false; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | | pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype) !void { |
| 398 | | switch (elf_file.getTarget().cpu.arch) { |
| 399 | | .x86_64 => try x86_64.scanRelocs(self, elf_file, code, undefs), |
| 400 | | else => return error.UnsupportedCpuArch, |
| 397 | pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype) RelocError!void { |
| 398 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 399 | const file_ptr = self.file(elf_file).?; |
| 400 | const rels = self.relocs(elf_file); |
| 401 | |
| 402 | var has_reloc_errors = false; |
| 403 | var it = RelocsIterator{ .relocs = rels }; |
| 404 | while (it.next()) |rel| { |
| 405 | const r_kind = relocation.decode(rel.r_type(), cpu_arch); |
| 406 | if (r_kind == .none) continue; |
| 407 | |
| 408 | const symbol_index = switch (file_ptr) { |
| 409 | .zig_object => |x| x.symbol(rel.r_sym()), |
| 410 | .object => |x| x.symbols.items[rel.r_sym()], |
| 411 | else => unreachable, |
| 412 | }; |
| 413 | const symbol = elf_file.symbol(symbol_index); |
| 414 | |
| 415 | // Check for violation of One Definition Rule for COMDATs. |
| 416 | if (symbol.file(elf_file) == null) { |
| 417 | // TODO convert into an error |
| 418 | log.debug("{}: {s}: {s} refers to a discarded COMDAT section", .{ |
| 419 | file_ptr.fmtPath(), |
| 420 | self.name(elf_file), |
| 421 | symbol.name(elf_file), |
| 422 | }); |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | // Report an undefined symbol. |
| 427 | if (try self.reportUndefined(elf_file, symbol, symbol_index, rel, undefs)) continue; |
| 428 | |
| 429 | if (symbol.isIFunc(elf_file)) { |
| 430 | symbol.flags.needs_got = true; |
| 431 | symbol.flags.needs_plt = true; |
| 432 | } |
| 433 | |
| 434 | // While traversing relocations, mark symbols that require special handling such as |
| 435 | // pointer indirection via GOT, or a stub trampoline via PLT. |
| 436 | switch (cpu_arch) { |
| 437 | .x86_64 => x86_64.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) { |
| 438 | error.RelocFailure => has_reloc_errors = true, |
| 439 | else => |e| return e, |
| 440 | }, |
| 441 | .aarch64 => aarch64.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) { |
| 442 | error.RelocFailure => has_reloc_errors = true, |
| 443 | else => |e| return e, |
| 444 | }, |
| 445 | .riscv64 => riscv.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) { |
| 446 | error.RelocFailure => has_reloc_errors = true, |
| 447 | else => |e| return e, |
| 448 | }, |
| 449 | else => return error.UnsupportedCpuArch, |
| 450 | } |
| 401 | 451 | } |
| 452 | if (has_reloc_errors) return error.RelocFailure; |
| 402 | 453 | } |
| 403 | 454 | |
| 404 | 455 | fn scanReloc( |
| ... | ... | @@ -407,7 +458,7 @@ fn scanReloc( |
| 407 | 458 | rel: elf.Elf64_Rela, |
| 408 | 459 | action: RelocAction, |
| 409 | 460 | elf_file: *Elf, |
| 410 | | ) error{OutOfMemory}!void { |
| 461 | ) RelocError!void { |
| 411 | 462 | const is_writeable = self.inputShdr(elf_file).sh_flags & elf.SHF_WRITE != 0; |
| 412 | 463 | const num_dynrelocs = switch (self.file(elf_file).?) { |
| 413 | 464 | .linker_defined => unreachable, |
| ... | ... | @@ -554,7 +605,7 @@ fn dataType(symbol: *const Symbol, elf_file: *Elf) u2 { |
| 554 | 605 | return 3; |
| 555 | 606 | } |
| 556 | 607 | |
| 557 | | fn reportUnhandledRelocError(self: Atom, rel: elf.Elf64_Rela, elf_file: *Elf) error{OutOfMemory}!void { |
| 608 | fn reportUnhandledRelocError(self: Atom, rel: elf.Elf64_Rela, elf_file: *Elf) RelocError!void { |
| 558 | 609 | var err = try elf_file.addErrorWithNotes(1); |
| 559 | 610 | try err.addMsg(elf_file, "fatal linker error: unhandled relocation type {} at offset 0x{x}", .{ |
| 560 | 611 | relocation.fmtRelocType(rel.r_type(), elf_file.getTarget().cpu.arch), |
| ... | ... | @@ -564,6 +615,7 @@ fn reportUnhandledRelocError(self: Atom, rel: elf.Elf64_Rela, elf_file: *Elf) er |
| 564 | 615 | self.file(elf_file).?.fmtPath(), |
| 565 | 616 | self.name(elf_file), |
| 566 | 617 | }); |
| 618 | return error.RelocFailure; |
| 567 | 619 | } |
| 568 | 620 | |
| 569 | 621 | fn reportTextRelocError( |
| ... | ... | @@ -571,7 +623,7 @@ fn reportTextRelocError( |
| 571 | 623 | symbol: *const Symbol, |
| 572 | 624 | rel: elf.Elf64_Rela, |
| 573 | 625 | elf_file: *Elf, |
| 574 | | ) error{OutOfMemory}!void { |
| 626 | ) RelocError!void { |
| 575 | 627 | var err = try elf_file.addErrorWithNotes(1); |
| 576 | 628 | try err.addMsg(elf_file, "relocation at offset 0x{x} against symbol '{s}' cannot be used", .{ |
| 577 | 629 | rel.r_offset, |
| ... | ... | @@ -581,6 +633,7 @@ fn reportTextRelocError( |
| 581 | 633 | self.file(elf_file).?.fmtPath(), |
| 582 | 634 | self.name(elf_file), |
| 583 | 635 | }); |
| 636 | return error.RelocFailure; |
| 584 | 637 | } |
| 585 | 638 | |
| 586 | 639 | fn reportPicError( |
| ... | ... | @@ -588,7 +641,7 @@ fn reportPicError( |
| 588 | 641 | symbol: *const Symbol, |
| 589 | 642 | rel: elf.Elf64_Rela, |
| 590 | 643 | elf_file: *Elf, |
| 591 | | ) error{OutOfMemory}!void { |
| 644 | ) RelocError!void { |
| 592 | 645 | var err = try elf_file.addErrorWithNotes(2); |
| 593 | 646 | try err.addMsg(elf_file, "relocation at offset 0x{x} against symbol '{s}' cannot be used", .{ |
| 594 | 647 | rel.r_offset, |
| ... | ... | @@ -599,6 +652,7 @@ fn reportPicError( |
| 599 | 652 | self.name(elf_file), |
| 600 | 653 | }); |
| 601 | 654 | try err.addNote(elf_file, "recompile with -fPIC", .{}); |
| 655 | return error.RelocFailure; |
| 602 | 656 | } |
| 603 | 657 | |
| 604 | 658 | fn reportNoPicError( |
| ... | ... | @@ -606,7 +660,7 @@ fn reportNoPicError( |
| 606 | 660 | symbol: *const Symbol, |
| 607 | 661 | rel: elf.Elf64_Rela, |
| 608 | 662 | elf_file: *Elf, |
| 609 | | ) error{OutOfMemory}!void { |
| 663 | ) RelocError!void { |
| 610 | 664 | var err = try elf_file.addErrorWithNotes(2); |
| 611 | 665 | try err.addMsg(elf_file, "relocation at offset 0x{x} against symbol '{s}' cannot be used", .{ |
| 612 | 666 | rel.r_offset, |
| ... | ... | @@ -617,6 +671,7 @@ fn reportNoPicError( |
| 617 | 671 | self.name(elf_file), |
| 618 | 672 | }); |
| 619 | 673 | try err.addNote(elf_file, "recompile with -fno-PIC", .{}); |
| 674 | return error.RelocFailure; |
| 620 | 675 | } |
| 621 | 676 | |
| 622 | 677 | // This function will report any undefined non-weak symbols that are not imports. |
| ... | ... | @@ -627,7 +682,7 @@ fn reportUndefined( |
| 627 | 682 | sym_index: Symbol.Index, |
| 628 | 683 | rel: elf.Elf64_Rela, |
| 629 | 684 | undefs: anytype, |
| 630 | | ) !void { |
| 685 | ) !bool { |
| 631 | 686 | const comp = elf_file.base.comp; |
| 632 | 687 | const gpa = comp.gpa; |
| 633 | 688 | const rel_esym = switch (self.file(elf_file).?) { |
| ... | ... | @@ -647,16 +702,95 @@ fn reportUndefined( |
| 647 | 702 | gop.value_ptr.* = std.ArrayList(Atom.Index).init(gpa); |
| 648 | 703 | } |
| 649 | 704 | try gop.value_ptr.append(self.atom_index); |
| 705 | return true; |
| 650 | 706 | } |
| 707 | |
| 708 | return false; |
| 651 | 709 | } |
| 652 | 710 | |
| 653 | | pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 711 | pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!void { |
| 654 | 712 | relocs_log.debug("0x{x}: {s}", .{ self.address(elf_file), self.name(elf_file) }); |
| 655 | 713 | |
| 656 | | switch (elf_file.getTarget().cpu.arch) { |
| 657 | | .x86_64 => try x86_64.resolveRelocsAlloc(self, elf_file, code), |
| 658 | | else => return error.UnsupportedCpuArch, |
| 714 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 715 | const file_ptr = self.file(elf_file).?; |
| 716 | var stream = std.io.fixedBufferStream(code); |
| 717 | |
| 718 | const rels = self.relocs(elf_file); |
| 719 | var it = RelocsIterator{ .relocs = rels }; |
| 720 | var has_reloc_errors = false; |
| 721 | while (it.next()) |rel| { |
| 722 | const r_kind = relocation.decode(rel.r_type(), cpu_arch); |
| 723 | if (r_kind == .none) continue; |
| 724 | |
| 725 | const target = switch (file_ptr) { |
| 726 | .zig_object => |x| elf_file.symbol(x.symbol(rel.r_sym())), |
| 727 | .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]), |
| 728 | else => unreachable, |
| 729 | }; |
| 730 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 731 | |
| 732 | // We will use equation format to resolve relocations: |
| 733 | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ |
| 734 | // |
| 735 | // Address of the source atom. |
| 736 | const P = @as(i64, @intCast(self.address(elf_file) + rel.r_offset)); |
| 737 | // Addend from the relocation. |
| 738 | const A = rel.r_addend; |
| 739 | // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub. |
| 740 | const S = @as(i64, @intCast(target.address(.{}, elf_file))); |
| 741 | // Address of the global offset table. |
| 742 | const GOT = @as(i64, @intCast(elf_file.gotAddress())); |
| 743 | // Address of the .zig.got table entry if any. |
| 744 | const ZIG_GOT = @as(i64, @intCast(target.zigGotAddress(elf_file))); |
| 745 | // Relative offset to the start of the global offset table. |
| 746 | const G = @as(i64, @intCast(target.gotAddress(elf_file))) - GOT; |
| 747 | // // Address of the thread pointer. |
| 748 | const TP = @as(i64, @intCast(elf_file.tpAddress())); |
| 749 | // Address of the dynamic thread pointer. |
| 750 | const DTP = @as(i64, @intCast(elf_file.dtpAddress())); |
| 751 | |
| 752 | relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{ |
| 753 | relocation.fmtRelocType(rel.r_type(), cpu_arch), |
| 754 | r_offset, |
| 755 | P, |
| 756 | S + A, |
| 757 | G + GOT + A, |
| 758 | ZIG_GOT + A, |
| 759 | target.name(elf_file), |
| 760 | }); |
| 761 | |
| 762 | try stream.seekTo(r_offset); |
| 763 | |
| 764 | const args = ResolveArgs{ P, A, S, GOT, G, TP, DTP, ZIG_GOT }; |
| 765 | |
| 766 | switch (cpu_arch) { |
| 767 | .x86_64 => x86_64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 768 | error.RelocFailure, |
| 769 | error.RelaxFailure, |
| 770 | error.InvalidInstruction, |
| 771 | error.CannotEncode, |
| 772 | => has_reloc_errors = true, |
| 773 | else => |e| return e, |
| 774 | }, |
| 775 | .aarch64 => aarch64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 776 | error.RelocFailure, |
| 777 | error.RelaxFailure, |
| 778 | error.UnexpectedRemainder, |
| 779 | error.DivisionByZero, |
| 780 | => has_reloc_errors = true, |
| 781 | else => |e| return e, |
| 782 | }, |
| 783 | .riscv64 => riscv.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 784 | error.RelocFailure, |
| 785 | error.RelaxFailure, |
| 786 | => has_reloc_errors = true, |
| 787 | else => |e| return e, |
| 788 | }, |
| 789 | else => return error.UnsupportedCpuArch, |
| 790 | } |
| 659 | 791 | } |
| 792 | |
| 793 | if (has_reloc_errors) return error.RelaxFailure; |
| 660 | 794 | } |
| 661 | 795 | |
| 662 | 796 | fn resolveDynAbsReloc( |
| ... | ... | @@ -761,10 +895,83 @@ fn applyDynamicReloc(value: i64, elf_file: *Elf, writer: anytype) !void { |
| 761 | 895 | pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: anytype) !void { |
| 762 | 896 | relocs_log.debug("0x{x}: {s}", .{ self.address(elf_file), self.name(elf_file) }); |
| 763 | 897 | |
| 764 | | switch (elf_file.getTarget().cpu.arch) { |
| 765 | | .x86_64 => try x86_64.resolveRelocsNonAlloc(self, elf_file, code, undefs), |
| 766 | | else => return error.UnsupportedCpuArch, |
| 898 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 899 | const file_ptr = self.file(elf_file).?; |
| 900 | var stream = std.io.fixedBufferStream(code); |
| 901 | |
| 902 | const rels = self.relocs(elf_file); |
| 903 | var has_reloc_errors = false; |
| 904 | var it = RelocsIterator{ .relocs = rels }; |
| 905 | while (it.next()) |rel| { |
| 906 | const r_kind = relocation.decode(rel.r_type(), cpu_arch); |
| 907 | if (r_kind == .none) continue; |
| 908 | |
| 909 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 910 | |
| 911 | const target_index = switch (file_ptr) { |
| 912 | .zig_object => |x| x.symbol(rel.r_sym()), |
| 913 | .object => |x| x.symbols.items[rel.r_sym()], |
| 914 | else => unreachable, |
| 915 | }; |
| 916 | const target = elf_file.symbol(target_index); |
| 917 | |
| 918 | // Check for violation of One Definition Rule for COMDATs. |
| 919 | if (target.file(elf_file) == null) { |
| 920 | // TODO convert into an error |
| 921 | log.debug("{}: {s}: {s} refers to a discarded COMDAT section", .{ |
| 922 | file_ptr.fmtPath(), |
| 923 | self.name(elf_file), |
| 924 | target.name(elf_file), |
| 925 | }); |
| 926 | continue; |
| 927 | } |
| 928 | |
| 929 | // Report an undefined symbol. |
| 930 | if (try self.reportUndefined(elf_file, target, target_index, rel, undefs)) continue; |
| 931 | |
| 932 | // We will use equation format to resolve relocations: |
| 933 | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ |
| 934 | // |
| 935 | const P = @as(i64, @intCast(self.address(elf_file) + rel.r_offset)); |
| 936 | // Addend from the relocation. |
| 937 | const A = rel.r_addend; |
| 938 | // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub. |
| 939 | const S = @as(i64, @intCast(target.address(.{}, elf_file))); |
| 940 | // Address of the global offset table. |
| 941 | const GOT = @as(i64, @intCast(elf_file.gotAddress())); |
| 942 | // Address of the dynamic thread pointer. |
| 943 | const DTP = @as(i64, @intCast(elf_file.dtpAddress())); |
| 944 | |
| 945 | const args = ResolveArgs{ P, A, S, GOT, 0, 0, DTP, 0 }; |
| 946 | |
| 947 | relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{ |
| 948 | relocation.fmtRelocType(rel.r_type(), cpu_arch), |
| 949 | rel.r_offset, |
| 950 | P, |
| 951 | S + A, |
| 952 | target.name(elf_file), |
| 953 | }); |
| 954 | |
| 955 | try stream.seekTo(r_offset); |
| 956 | |
| 957 | switch (cpu_arch) { |
| 958 | .x86_64 => x86_64.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 959 | error.RelocFailure => has_reloc_errors = true, |
| 960 | else => |e| return e, |
| 961 | }, |
| 962 | .aarch64 => aarch64.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 963 | error.RelocFailure => has_reloc_errors = true, |
| 964 | else => |e| return e, |
| 965 | }, |
| 966 | .riscv64 => riscv.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) { |
| 967 | error.RelocFailure => has_reloc_errors = true, |
| 968 | else => |e| return e, |
| 969 | }, |
| 970 | else => return error.UnsupportedCpuArch, |
| 971 | } |
| 767 | 972 | } |
| 973 | |
| 974 | if (has_reloc_errors) return error.RelocFailure; |
| 768 | 975 | } |
| 769 | 976 | |
| 770 | 977 | pub fn format( |
| ... | ... | @@ -831,429 +1038,313 @@ pub const Flags = packed struct { |
| 831 | 1038 | }; |
| 832 | 1039 | |
| 833 | 1040 | const x86_64 = struct { |
| 834 | | fn scanRelocs(atom: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype) !void { |
| 1041 | fn scanReloc( |
| 1042 | atom: Atom, |
| 1043 | elf_file: *Elf, |
| 1044 | rel: elf.Elf64_Rela, |
| 1045 | symbol: *Symbol, |
| 1046 | code: ?[]const u8, |
| 1047 | it: *RelocsIterator, |
| 1048 | ) !void { |
| 835 | 1049 | const is_static = elf_file.base.isStatic(); |
| 836 | 1050 | const is_dyn_lib = elf_file.base.isDynLib(); |
| 837 | | const file_ptr = atom.file(elf_file).?; |
| 838 | | const rels = atom.relocs(elf_file); |
| 839 | | var i: usize = 0; |
| 840 | | while (i < rels.len) : (i += 1) { |
| 841 | | const rel = rels[i]; |
| 842 | | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 843 | | |
| 844 | | if (r_type == .NONE) continue; |
| 845 | | |
| 846 | | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 847 | | |
| 848 | | const symbol_index = switch (file_ptr) { |
| 849 | | .zig_object => |x| x.symbol(rel.r_sym()), |
| 850 | | .object => |x| x.symbols.items[rel.r_sym()], |
| 851 | | else => unreachable, |
| 852 | | }; |
| 853 | | const symbol = elf_file.symbol(symbol_index); |
| 854 | | |
| 855 | | // Check for violation of One Definition Rule for COMDATs. |
| 856 | | if (symbol.file(elf_file) == null) { |
| 857 | | // TODO convert into an error |
| 858 | | log.debug("{}: {s}: {s} refers to a discarded COMDAT section", .{ |
| 859 | | file_ptr.fmtPath(), |
| 860 | | atom.name(elf_file), |
| 861 | | symbol.name(elf_file), |
| 862 | | }); |
| 863 | | continue; |
| 864 | | } |
| 865 | 1051 | |
| 866 | | // Report an undefined symbol. |
| 867 | | try atom.reportUndefined(elf_file, symbol, symbol_index, rel, undefs); |
| 1052 | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 1053 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 868 | 1054 | |
| 869 | | if (symbol.isIFunc(elf_file)) { |
| 870 | | symbol.flags.needs_got = true; |
| 871 | | symbol.flags.needs_plt = true; |
| 872 | | } |
| 1055 | switch (r_type) { |
| 1056 | .@"64" => { |
| 1057 | try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file); |
| 1058 | }, |
| 873 | 1059 | |
| 874 | | // While traversing relocations, mark symbols that require special handling such as |
| 875 | | // pointer indirection via GOT, or a stub trampoline via PLT. |
| 876 | | switch (r_type) { |
| 877 | | .@"64" => { |
| 878 | | try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file); |
| 879 | | }, |
| 1060 | .@"32", |
| 1061 | .@"32S", |
| 1062 | => { |
| 1063 | try atom.scanReloc(symbol, rel, absRelocAction(symbol, elf_file), elf_file); |
| 1064 | }, |
| 880 | 1065 | |
| 881 | | .@"32", |
| 882 | | .@"32S", |
| 883 | | => { |
| 884 | | try atom.scanReloc(symbol, rel, absRelocAction(symbol, elf_file), elf_file); |
| 885 | | }, |
| 1066 | .GOT32, |
| 1067 | .GOTPC32, |
| 1068 | .GOTPC64, |
| 1069 | .GOTPCREL, |
| 1070 | .GOTPCREL64, |
| 1071 | .GOTPCRELX, |
| 1072 | .REX_GOTPCRELX, |
| 1073 | => { |
| 1074 | symbol.flags.needs_got = true; |
| 1075 | }, |
| 886 | 1076 | |
| 887 | | .GOT32, |
| 888 | | .GOTPC32, |
| 889 | | .GOTPC64, |
| 890 | | .GOTPCREL, |
| 891 | | .GOTPCREL64, |
| 892 | | .GOTPCRELX, |
| 893 | | .REX_GOTPCRELX, |
| 894 | | => { |
| 895 | | symbol.flags.needs_got = true; |
| 896 | | }, |
| 1077 | .PLT32, |
| 1078 | .PLTOFF64, |
| 1079 | => { |
| 1080 | if (symbol.flags.import) { |
| 1081 | symbol.flags.needs_plt = true; |
| 1082 | } |
| 1083 | }, |
| 897 | 1084 | |
| 898 | | .PLT32, |
| 899 | | .PLTOFF64, |
| 900 | | => { |
| 901 | | if (symbol.flags.import) { |
| 902 | | symbol.flags.needs_plt = true; |
| 903 | | } |
| 904 | | }, |
| 1085 | .PC32 => { |
| 1086 | try atom.scanReloc(symbol, rel, pcRelocAction(symbol, elf_file), elf_file); |
| 1087 | }, |
| 905 | 1088 | |
| 906 | | .PC32 => { |
| 907 | | try atom.scanReloc(symbol, rel, pcRelocAction(symbol, elf_file), elf_file); |
| 908 | | }, |
| 1089 | .TLSGD => { |
| 1090 | // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr |
| 909 | 1091 | |
| 910 | | .TLSGD => { |
| 911 | | // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr |
| 1092 | if (is_static or (!symbol.flags.import and !is_dyn_lib)) { |
| 1093 | // Relax if building with -static flag as __tls_get_addr() will not be present in libc.a |
| 1094 | // We skip the next relocation. |
| 1095 | it.skip(1); |
| 1096 | } else if (!symbol.flags.import and is_dyn_lib) { |
| 1097 | symbol.flags.needs_gottp = true; |
| 1098 | it.skip(1); |
| 1099 | } else { |
| 1100 | symbol.flags.needs_tlsgd = true; |
| 1101 | } |
| 1102 | }, |
| 912 | 1103 | |
| 913 | | if (is_static or (!symbol.flags.import and !is_dyn_lib)) { |
| 914 | | // Relax if building with -static flag as __tls_get_addr() will not be present in libc.a |
| 915 | | // We skip the next relocation. |
| 916 | | i += 1; |
| 917 | | } else if (!symbol.flags.import and is_dyn_lib) { |
| 918 | | symbol.flags.needs_gottp = true; |
| 919 | | i += 1; |
| 920 | | } else { |
| 921 | | symbol.flags.needs_tlsgd = true; |
| 922 | | } |
| 923 | | }, |
| 1104 | .TLSLD => { |
| 1105 | // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr |
| 924 | 1106 | |
| 925 | | .TLSLD => { |
| 926 | | // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr |
| 1107 | if (is_static or !is_dyn_lib) { |
| 1108 | // Relax if building with -static flag as __tls_get_addr() will not be present in libc.a |
| 1109 | // We skip the next relocation. |
| 1110 | it.skip(1); |
| 1111 | } else { |
| 1112 | elf_file.got.flags.needs_tlsld = true; |
| 1113 | } |
| 1114 | }, |
| 927 | 1115 | |
| 928 | | if (is_static or !is_dyn_lib) { |
| 929 | | // Relax if building with -static flag as __tls_get_addr() will not be present in libc.a |
| 930 | | // We skip the next relocation. |
| 931 | | i += 1; |
| 932 | | } else { |
| 933 | | elf_file.got.flags.needs_tlsld = true; |
| 934 | | } |
| 935 | | }, |
| 1116 | .GOTTPOFF => { |
| 1117 | const should_relax = blk: { |
| 1118 | if (is_dyn_lib or symbol.flags.import) break :blk false; |
| 1119 | if (!x86_64.canRelaxGotTpOff(code.?[r_offset - 3 ..])) break :blk false; |
| 1120 | break :blk true; |
| 1121 | }; |
| 1122 | if (!should_relax) { |
| 1123 | symbol.flags.needs_gottp = true; |
| 1124 | } |
| 1125 | }, |
| 936 | 1126 | |
| 937 | | .GOTTPOFF => { |
| 938 | | const should_relax = blk: { |
| 939 | | if (is_dyn_lib or symbol.flags.import) break :blk false; |
| 940 | | if (!x86_64.canRelaxGotTpOff(code.?[r_offset - 3 ..])) break :blk false; |
| 941 | | break :blk true; |
| 942 | | }; |
| 943 | | if (!should_relax) { |
| 944 | | symbol.flags.needs_gottp = true; |
| 945 | | } |
| 946 | | }, |
| 1127 | .GOTPC32_TLSDESC => { |
| 1128 | const should_relax = is_static or (!is_dyn_lib and !symbol.flags.import); |
| 1129 | if (!should_relax) { |
| 1130 | symbol.flags.needs_tlsdesc = true; |
| 1131 | } |
| 1132 | }, |
| 947 | 1133 | |
| 948 | | .GOTPC32_TLSDESC => { |
| 949 | | const should_relax = is_static or (!is_dyn_lib and !symbol.flags.import); |
| 950 | | if (!should_relax) { |
| 951 | | symbol.flags.needs_tlsdesc = true; |
| 952 | | } |
| 953 | | }, |
| 1134 | .TPOFF32, |
| 1135 | .TPOFF64, |
| 1136 | => { |
| 1137 | if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file); |
| 1138 | }, |
| 954 | 1139 | |
| 955 | | .TPOFF32, |
| 956 | | .TPOFF64, |
| 1140 | .GOTOFF64, |
| 1141 | .DTPOFF32, |
| 1142 | .DTPOFF64, |
| 1143 | .SIZE32, |
| 1144 | .SIZE64, |
| 1145 | .TLSDESC_CALL, |
| 1146 | => {}, |
| 1147 | |
| 1148 | else => |x| switch (@intFromEnum(x)) { |
| 1149 | // Zig custom relocations |
| 1150 | Elf.R_ZIG_GOT32, |
| 1151 | Elf.R_ZIG_GOTPCREL, |
| 957 | 1152 | => { |
| 958 | | if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file); |
| 1153 | assert(symbol.flags.has_zig_got); |
| 959 | 1154 | }, |
| 960 | 1155 | |
| 961 | | .GOTOFF64, |
| 962 | | .DTPOFF32, |
| 963 | | .DTPOFF64, |
| 964 | | .SIZE32, |
| 965 | | .SIZE64, |
| 966 | | .TLSDESC_CALL, |
| 967 | | => {}, |
| 968 | | |
| 969 | | else => |x| switch (@intFromEnum(x)) { |
| 970 | | // Zig custom relocations |
| 971 | | Elf.R_ZIG_GOT32, |
| 972 | | Elf.R_ZIG_GOTPCREL, |
| 973 | | => { |
| 974 | | assert(symbol.flags.has_zig_got); |
| 975 | | }, |
| 976 | | |
| 977 | | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 978 | | }, |
| 979 | | } |
| 1156 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1157 | }, |
| 980 | 1158 | } |
| 981 | 1159 | } |
| 982 | 1160 | |
| 983 | | fn resolveRelocsAlloc(atom: Atom, elf_file: *Elf, code: []u8) !void { |
| 984 | | const file_ptr = atom.file(elf_file).?; |
| 985 | | var stream = std.io.fixedBufferStream(code); |
| 986 | | const cwriter = stream.writer(); |
| 1161 | fn resolveRelocAlloc( |
| 1162 | atom: Atom, |
| 1163 | elf_file: *Elf, |
| 1164 | rel: elf.Elf64_Rela, |
| 1165 | target: *const Symbol, |
| 1166 | args: ResolveArgs, |
| 1167 | it: *RelocsIterator, |
| 1168 | code: []u8, |
| 1169 | stream: anytype, |
| 1170 | ) (error{ InvalidInstruction, CannotEncode } || RelocError)!void { |
| 1171 | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 1172 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 987 | 1173 | |
| 988 | | const rels = atom.relocs(elf_file); |
| 989 | | var i: usize = 0; |
| 990 | | while (i < rels.len) : (i += 1) { |
| 991 | | const rel = rels[i]; |
| 992 | | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 993 | | if (r_type == .NONE) continue; |
| 994 | | |
| 995 | | const target = switch (file_ptr) { |
| 996 | | .zig_object => |x| elf_file.symbol(x.symbol(rel.r_sym())), |
| 997 | | .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]), |
| 998 | | else => unreachable, |
| 999 | | }; |
| 1000 | | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 1001 | | |
| 1002 | | // We will use equation format to resolve relocations: |
| 1003 | | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ |
| 1004 | | // |
| 1005 | | // Address of the source atom. |
| 1006 | | const P = @as(i64, @intCast(atom.address(elf_file) + rel.r_offset)); |
| 1007 | | // Addend from the relocation. |
| 1008 | | const A = rel.r_addend; |
| 1009 | | // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub. |
| 1010 | | const S = @as(i64, @intCast(target.address(.{}, elf_file))); |
| 1011 | | // Address of the global offset table. |
| 1012 | | const GOT = blk: { |
| 1013 | | const shndx = if (elf_file.got_plt_section_index) |shndx| |
| 1014 | | shndx |
| 1015 | | else if (elf_file.got_section_index) |shndx| |
| 1016 | | shndx |
| 1017 | | else |
| 1018 | | null; |
| 1019 | | break :blk if (shndx) |index| @as(i64, @intCast(elf_file.shdrs.items[index].sh_addr)) else 0; |
| 1020 | | }; |
| 1021 | | // Address of the .zig.got table entry if any. |
| 1022 | | const ZIG_GOT = @as(i64, @intCast(target.zigGotAddress(elf_file))); |
| 1023 | | // Relative offset to the start of the global offset table. |
| 1024 | | const G = @as(i64, @intCast(target.gotAddress(elf_file))) - GOT; |
| 1025 | | // // Address of the thread pointer. |
| 1026 | | const TP = @as(i64, @intCast(elf_file.tpAddress())); |
| 1027 | | // Address of the dynamic thread pointer. |
| 1028 | | const DTP = @as(i64, @intCast(elf_file.dtpAddress())); |
| 1029 | | |
| 1030 | | relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{ |
| 1031 | | relocation.fmtRelocType(rel.r_type(), .x86_64), |
| 1032 | | r_offset, |
| 1033 | | P, |
| 1034 | | S + A, |
| 1035 | | G + GOT + A, |
| 1036 | | ZIG_GOT + A, |
| 1037 | | target.name(elf_file), |
| 1038 | | }); |
| 1174 | const cwriter = stream.writer(); |
| 1039 | 1175 | |
| 1040 | | try stream.seekTo(r_offset); |
| 1176 | const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args; |
| 1041 | 1177 | |
| 1042 | | switch (r_type) { |
| 1043 | | .NONE => unreachable, |
| 1178 | switch (r_type) { |
| 1179 | .NONE => unreachable, |
| 1044 | 1180 | |
| 1045 | | .@"64" => { |
| 1046 | | try atom.resolveDynAbsReloc( |
| 1047 | | target, |
| 1048 | | rel, |
| 1049 | | dynAbsRelocAction(target, elf_file), |
| 1050 | | elf_file, |
| 1051 | | cwriter, |
| 1052 | | ); |
| 1053 | | }, |
| 1181 | .@"64" => { |
| 1182 | try atom.resolveDynAbsReloc( |
| 1183 | target, |
| 1184 | rel, |
| 1185 | dynAbsRelocAction(target, elf_file), |
| 1186 | elf_file, |
| 1187 | cwriter, |
| 1188 | ); |
| 1189 | }, |
| 1054 | 1190 | |
| 1055 | | .PLT32, |
| 1056 | | .PC32, |
| 1057 | | => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little), |
| 1191 | .PLT32, |
| 1192 | .PC32, |
| 1193 | => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little), |
| 1058 | 1194 | |
| 1059 | | .GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little), |
| 1060 | | .GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little), |
| 1061 | | .GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little), |
| 1195 | .GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little), |
| 1196 | .GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little), |
| 1197 | .GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little), |
| 1062 | 1198 | |
| 1063 | | .GOTPCRELX => { |
| 1064 | | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 1065 | | x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk; |
| 1066 | | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 1067 | | continue; |
| 1068 | | } |
| 1069 | | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 1070 | | }, |
| 1199 | .GOTPCRELX => { |
| 1200 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 1201 | x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk; |
| 1202 | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 1203 | return; |
| 1204 | } |
| 1205 | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 1206 | }, |
| 1071 | 1207 | |
| 1072 | | .REX_GOTPCRELX => { |
| 1073 | | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 1074 | | x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk; |
| 1075 | | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 1076 | | continue; |
| 1077 | | } |
| 1078 | | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 1079 | | }, |
| 1208 | .REX_GOTPCRELX => { |
| 1209 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 1210 | x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk; |
| 1211 | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 1212 | return; |
| 1213 | } |
| 1214 | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 1215 | }, |
| 1080 | 1216 | |
| 1081 | | .@"32" => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little), |
| 1082 | | .@"32S" => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little), |
| 1217 | .@"32" => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little), |
| 1218 | .@"32S" => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little), |
| 1083 | 1219 | |
| 1084 | | .TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little), |
| 1085 | | .TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little), |
| 1220 | .TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little), |
| 1221 | .TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little), |
| 1086 | 1222 | |
| 1087 | | .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little), |
| 1088 | | .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 1223 | .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little), |
| 1224 | .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 1089 | 1225 | |
| 1090 | | .TLSGD => { |
| 1091 | | if (target.flags.has_tlsgd) { |
| 1092 | | const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file))); |
| 1093 | | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1094 | | } else if (target.flags.has_gottp) { |
| 1095 | | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 1096 | | try x86_64.relaxTlsGdToIe(atom, rels[i .. i + 2], @intCast(S_ - P), elf_file, &stream); |
| 1097 | | i += 1; |
| 1098 | | } else { |
| 1099 | | try x86_64.relaxTlsGdToLe( |
| 1100 | | atom, |
| 1101 | | rels[i .. i + 2], |
| 1102 | | @as(i32, @intCast(S - TP)), |
| 1103 | | elf_file, |
| 1104 | | &stream, |
| 1105 | | ); |
| 1106 | | i += 1; |
| 1107 | | } |
| 1108 | | }, |
| 1226 | .TLSGD => { |
| 1227 | if (target.flags.has_tlsgd) { |
| 1228 | const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file))); |
| 1229 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1230 | } else if (target.flags.has_gottp) { |
| 1231 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 1232 | try x86_64.relaxTlsGdToIe(atom, &.{ rel, it.next().? }, @intCast(S_ - P), elf_file, stream); |
| 1233 | } else { |
| 1234 | try x86_64.relaxTlsGdToLe( |
| 1235 | atom, |
| 1236 | &.{ rel, it.next().? }, |
| 1237 | @as(i32, @intCast(S - TP)), |
| 1238 | elf_file, |
| 1239 | stream, |
| 1240 | ); |
| 1241 | } |
| 1242 | }, |
| 1109 | 1243 | |
| 1110 | | .TLSLD => { |
| 1111 | | if (elf_file.got.tlsld_index) |entry_index| { |
| 1112 | | const tlsld_entry = elf_file.got.entries.items[entry_index]; |
| 1113 | | const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file))); |
| 1114 | | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1115 | | } else { |
| 1116 | | try x86_64.relaxTlsLdToLe( |
| 1117 | | atom, |
| 1118 | | rels[i .. i + 2], |
| 1119 | | @as(i32, @intCast(TP - @as(i64, @intCast(elf_file.tlsAddress())))), |
| 1120 | | elf_file, |
| 1121 | | &stream, |
| 1122 | | ); |
| 1123 | | i += 1; |
| 1124 | | } |
| 1125 | | }, |
| 1244 | .TLSLD => { |
| 1245 | if (elf_file.got.tlsld_index) |entry_index| { |
| 1246 | const tlsld_entry = elf_file.got.entries.items[entry_index]; |
| 1247 | const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file))); |
| 1248 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1249 | } else { |
| 1250 | try x86_64.relaxTlsLdToLe( |
| 1251 | atom, |
| 1252 | &.{ rel, it.next().? }, |
| 1253 | @as(i32, @intCast(TP - @as(i64, @intCast(elf_file.tlsAddress())))), |
| 1254 | elf_file, |
| 1255 | stream, |
| 1256 | ); |
| 1257 | } |
| 1258 | }, |
| 1126 | 1259 | |
| 1127 | | .GOTPC32_TLSDESC => { |
| 1128 | | if (target.flags.has_tlsdesc) { |
| 1129 | | const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file))); |
| 1130 | | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1131 | | } else { |
| 1132 | | try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]); |
| 1133 | | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 1134 | | } |
| 1135 | | }, |
| 1260 | .GOTPC32_TLSDESC => { |
| 1261 | if (target.flags.has_tlsdesc) { |
| 1262 | const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file))); |
| 1263 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1264 | } else { |
| 1265 | x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]) catch { |
| 1266 | var err = try elf_file.addErrorWithNotes(1); |
| 1267 | try err.addMsg(elf_file, "could not relax {s}", .{@tagName(r_type)}); |
| 1268 | try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{ |
| 1269 | atom.file(elf_file).?.fmtPath(), |
| 1270 | atom.name(elf_file), |
| 1271 | rel.r_offset, |
| 1272 | }); |
| 1273 | return error.RelaxFailure; |
| 1274 | }; |
| 1275 | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 1276 | } |
| 1277 | }, |
| 1136 | 1278 | |
| 1137 | | .TLSDESC_CALL => if (!target.flags.has_tlsdesc) { |
| 1138 | | // call -> nop |
| 1139 | | try cwriter.writeAll(&.{ 0x66, 0x90 }); |
| 1140 | | }, |
| 1279 | .TLSDESC_CALL => if (!target.flags.has_tlsdesc) { |
| 1280 | // call -> nop |
| 1281 | try cwriter.writeAll(&.{ 0x66, 0x90 }); |
| 1282 | }, |
| 1141 | 1283 | |
| 1142 | | .GOTTPOFF => { |
| 1143 | | if (target.flags.has_gottp) { |
| 1144 | | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 1145 | | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1146 | | } else { |
| 1147 | | x86_64.relaxGotTpOff(code[r_offset - 3 ..]) catch unreachable; |
| 1148 | | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 1149 | | } |
| 1150 | | }, |
| 1284 | .GOTTPOFF => { |
| 1285 | if (target.flags.has_gottp) { |
| 1286 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 1287 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 1288 | } else { |
| 1289 | x86_64.relaxGotTpOff(code[r_offset - 3 ..]); |
| 1290 | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 1291 | } |
| 1292 | }, |
| 1151 | 1293 | |
| 1152 | | .GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little), |
| 1294 | .GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little), |
| 1153 | 1295 | |
| 1154 | | else => |x| switch (@intFromEnum(x)) { |
| 1155 | | // Zig custom relocations |
| 1156 | | Elf.R_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little), |
| 1157 | | Elf.R_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little), |
| 1296 | else => |x| switch (@intFromEnum(x)) { |
| 1297 | // Zig custom relocations |
| 1298 | Elf.R_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little), |
| 1299 | Elf.R_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little), |
| 1158 | 1300 | |
| 1159 | | else => {}, |
| 1160 | | }, |
| 1161 | | } |
| 1301 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1302 | }, |
| 1162 | 1303 | } |
| 1163 | 1304 | } |
| 1164 | 1305 | |
| 1165 | | fn resolveRelocsNonAlloc(atom: Atom, elf_file: *Elf, code: []u8, undefs: anytype) !void { |
| 1166 | | const file_ptr = atom.file(elf_file).?; |
| 1167 | | var stream = std.io.fixedBufferStream(code); |
| 1306 | fn resolveRelocNonAlloc( |
| 1307 | atom: Atom, |
| 1308 | elf_file: *Elf, |
| 1309 | rel: elf.Elf64_Rela, |
| 1310 | target: *const Symbol, |
| 1311 | args: ResolveArgs, |
| 1312 | it: *RelocsIterator, |
| 1313 | code: []u8, |
| 1314 | stream: anytype, |
| 1315 | ) !void { |
| 1316 | _ = code; |
| 1317 | _ = it; |
| 1318 | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 1168 | 1319 | const cwriter = stream.writer(); |
| 1169 | 1320 | |
| 1170 | | const rels = atom.relocs(elf_file); |
| 1171 | | var i: usize = 0; |
| 1172 | | while (i < rels.len) : (i += 1) { |
| 1173 | | const rel = rels[i]; |
| 1174 | | const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type()); |
| 1175 | | if (r_type == .NONE) continue; |
| 1176 | | |
| 1177 | | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 1178 | | |
| 1179 | | const target_index = switch (file_ptr) { |
| 1180 | | .zig_object => |x| x.symbol(rel.r_sym()), |
| 1181 | | .object => |x| x.symbols.items[rel.r_sym()], |
| 1182 | | else => unreachable, |
| 1183 | | }; |
| 1184 | | const target = elf_file.symbol(target_index); |
| 1185 | | |
| 1186 | | // Check for violation of One Definition Rule for COMDATs. |
| 1187 | | if (target.file(elf_file) == null) { |
| 1188 | | // TODO convert into an error |
| 1189 | | log.debug("{}: {s}: {s} refers to a discarded COMDAT section", .{ |
| 1190 | | file_ptr.fmtPath(), |
| 1191 | | atom.name(elf_file), |
| 1192 | | target.name(elf_file), |
| 1193 | | }); |
| 1194 | | continue; |
| 1195 | | } |
| 1196 | | |
| 1197 | | // Report an undefined symbol. |
| 1198 | | try atom.reportUndefined(elf_file, target, target_index, rel, undefs); |
| 1199 | | |
| 1200 | | // We will use equation format to resolve relocations: |
| 1201 | | // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/ |
| 1202 | | // |
| 1203 | | const P = @as(i64, @intCast(atom.address(elf_file) + rel.r_offset)); |
| 1204 | | // Addend from the relocation. |
| 1205 | | const A = rel.r_addend; |
| 1206 | | // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub. |
| 1207 | | const S = @as(i64, @intCast(target.address(.{}, elf_file))); |
| 1208 | | // Address of the global offset table. |
| 1209 | | const GOT = blk: { |
| 1210 | | const shndx = if (elf_file.got_plt_section_index) |shndx| |
| 1211 | | shndx |
| 1212 | | else if (elf_file.got_section_index) |shndx| |
| 1213 | | shndx |
| 1214 | | else |
| 1215 | | null; |
| 1216 | | break :blk if (shndx) |index| @as(i64, @intCast(elf_file.shdrs.items[index].sh_addr)) else 0; |
| 1217 | | }; |
| 1218 | | // Address of the dynamic thread pointer. |
| 1219 | | const DTP = @as(i64, @intCast(elf_file.dtpAddress())); |
| 1220 | | |
| 1221 | | relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{ |
| 1222 | | relocation.fmtRelocType(rel.r_type(), .x86_64), |
| 1223 | | rel.r_offset, |
| 1224 | | P, |
| 1225 | | S + A, |
| 1226 | | target.name(elf_file), |
| 1227 | | }); |
| 1228 | | |
| 1229 | | try stream.seekTo(r_offset); |
| 1230 | | |
| 1231 | | switch (r_type) { |
| 1232 | | .NONE => unreachable, |
| 1233 | | .@"8" => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little), |
| 1234 | | .@"16" => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little), |
| 1235 | | .@"32" => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little), |
| 1236 | | .@"32S" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little), |
| 1237 | | .@"64" => try cwriter.writeInt(i64, S + A, .little), |
| 1238 | | .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little), |
| 1239 | | .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 1240 | | .GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little), |
| 1241 | | .GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little), |
| 1242 | | .SIZE32 => { |
| 1243 | | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1244 | | try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .little); |
| 1245 | | }, |
| 1246 | | .SIZE64 => { |
| 1247 | | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1248 | | try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .little); |
| 1249 | | }, |
| 1250 | | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1251 | | } |
| 1321 | _, const A, const S, const GOT, _, _, const DTP, _ = args; |
| 1322 | |
| 1323 | switch (r_type) { |
| 1324 | .NONE => unreachable, |
| 1325 | .@"8" => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little), |
| 1326 | .@"16" => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little), |
| 1327 | .@"32" => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little), |
| 1328 | .@"32S" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little), |
| 1329 | .@"64" => try cwriter.writeInt(i64, S + A, .little), |
| 1330 | .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little), |
| 1331 | .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 1332 | .GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little), |
| 1333 | .GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little), |
| 1334 | .SIZE32 => { |
| 1335 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1336 | try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .little); |
| 1337 | }, |
| 1338 | .SIZE64 => { |
| 1339 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1340 | try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .little); |
| 1341 | }, |
| 1342 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1252 | 1343 | } |
| 1253 | 1344 | } |
| 1254 | 1345 | |
| 1255 | 1346 | fn relaxGotpcrelx(code: []u8) !void { |
| 1256 | | const old_inst = disassemble(code) orelse return error.RelaxFail; |
| 1347 | const old_inst = disassemble(code) orelse return error.RelaxFailure; |
| 1257 | 1348 | const inst = switch (old_inst.encoding.mnemonic) { |
| 1258 | 1349 | .call => try Instruction.new(old_inst.prefix, .call, &.{ |
| 1259 | 1350 | // TODO: hack to force imm32s in the assembler |
| ... | ... | @@ -1263,28 +1354,28 @@ const x86_64 = struct { |
| 1263 | 1354 | // TODO: hack to force imm32s in the assembler |
| 1264 | 1355 | .{ .imm = Immediate.s(-129) }, |
| 1265 | 1356 | }), |
| 1266 | | else => return error.RelaxFail, |
| 1357 | else => return error.RelaxFailure, |
| 1267 | 1358 | }; |
| 1268 | 1359 | relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding }); |
| 1269 | 1360 | const nop = try Instruction.new(.none, .nop, &.{}); |
| 1270 | | encode(&.{ nop, inst }, code) catch return error.RelaxFail; |
| 1361 | try encode(&.{ nop, inst }, code); |
| 1271 | 1362 | } |
| 1272 | 1363 | |
| 1273 | 1364 | fn relaxRexGotpcrelx(code: []u8) !void { |
| 1274 | | const old_inst = disassemble(code) orelse return error.RelaxFail; |
| 1365 | const old_inst = disassemble(code) orelse return error.RelaxFailure; |
| 1275 | 1366 | switch (old_inst.encoding.mnemonic) { |
| 1276 | 1367 | .mov => { |
| 1277 | 1368 | const inst = try Instruction.new(old_inst.prefix, .lea, &old_inst.ops); |
| 1278 | 1369 | relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding }); |
| 1279 | | encode(&.{inst}, code) catch return error.RelaxFail; |
| 1370 | try encode(&.{inst}, code); |
| 1280 | 1371 | }, |
| 1281 | | else => return error.RelaxFail, |
| 1372 | else => return error.RelaxFailure, |
| 1282 | 1373 | } |
| 1283 | 1374 | } |
| 1284 | 1375 | |
| 1285 | 1376 | fn relaxTlsGdToIe( |
| 1286 | 1377 | self: Atom, |
| 1287 | | rels: []align(1) const elf.Elf64_Rela, |
| 1378 | rels: []const elf.Elf64_Rela, |
| 1288 | 1379 | value: i32, |
| 1289 | 1380 | elf_file: *Elf, |
| 1290 | 1381 | stream: anytype, |
| ... | ... | @@ -1307,7 +1398,7 @@ const x86_64 = struct { |
| 1307 | 1398 | |
| 1308 | 1399 | else => { |
| 1309 | 1400 | var err = try elf_file.addErrorWithNotes(1); |
| 1310 | | try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{ |
| 1401 | try err.addMsg(elf_file, "TODO: rewrite {} when followed by {}", .{ |
| 1311 | 1402 | relocation.fmtRelocType(rels[0].r_type(), .x86_64), |
| 1312 | 1403 | relocation.fmtRelocType(rels[1].r_type(), .x86_64), |
| 1313 | 1404 | }); |
| ... | ... | @@ -1316,13 +1407,14 @@ const x86_64 = struct { |
| 1316 | 1407 | self.name(elf_file), |
| 1317 | 1408 | rels[0].r_offset, |
| 1318 | 1409 | }); |
| 1410 | return error.RelaxFailure; |
| 1319 | 1411 | }, |
| 1320 | 1412 | } |
| 1321 | 1413 | } |
| 1322 | 1414 | |
| 1323 | 1415 | fn relaxTlsLdToLe( |
| 1324 | 1416 | self: Atom, |
| 1325 | | rels: []align(1) const elf.Elf64_Rela, |
| 1417 | rels: []const elf.Elf64_Rela, |
| 1326 | 1418 | value: i32, |
| 1327 | 1419 | elf_file: *Elf, |
| 1328 | 1420 | stream: anytype, |
| ... | ... | @@ -1360,7 +1452,7 @@ const x86_64 = struct { |
| 1360 | 1452 | |
| 1361 | 1453 | else => { |
| 1362 | 1454 | var err = try elf_file.addErrorWithNotes(1); |
| 1363 | | try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{ |
| 1455 | try err.addMsg(elf_file, "TODO: rewrite {} when followed by {}", .{ |
| 1364 | 1456 | relocation.fmtRelocType(rels[0].r_type(), .x86_64), |
| 1365 | 1457 | relocation.fmtRelocType(rels[1].r_type(), .x86_64), |
| 1366 | 1458 | }); |
| ... | ... | @@ -1369,6 +1461,7 @@ const x86_64 = struct { |
| 1369 | 1461 | self.name(elf_file), |
| 1370 | 1462 | rels[0].r_offset, |
| 1371 | 1463 | }); |
| 1464 | return error.RelaxFailure; |
| 1372 | 1465 | }, |
| 1373 | 1466 | } |
| 1374 | 1467 | } |
| ... | ... | @@ -1388,24 +1481,24 @@ const x86_64 = struct { |
| 1388 | 1481 | } |
| 1389 | 1482 | } |
| 1390 | 1483 | |
| 1391 | | fn relaxGotTpOff(code: []u8) !void { |
| 1392 | | const old_inst = disassemble(code) orelse return error.RelaxFail; |
| 1484 | fn relaxGotTpOff(code: []u8) void { |
| 1485 | const old_inst = disassemble(code) orelse unreachable; |
| 1393 | 1486 | switch (old_inst.encoding.mnemonic) { |
| 1394 | 1487 | .mov => { |
| 1395 | | const inst = try Instruction.new(old_inst.prefix, .mov, &.{ |
| 1488 | const inst = Instruction.new(old_inst.prefix, .mov, &.{ |
| 1396 | 1489 | old_inst.ops[0], |
| 1397 | 1490 | // TODO: hack to force imm32s in the assembler |
| 1398 | 1491 | .{ .imm = Immediate.s(-129) }, |
| 1399 | | }); |
| 1492 | }) catch unreachable; |
| 1400 | 1493 | relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding }); |
| 1401 | | encode(&.{inst}, code) catch return error.RelaxFail; |
| 1494 | encode(&.{inst}, code) catch unreachable; |
| 1402 | 1495 | }, |
| 1403 | | else => return error.RelaxFail, |
| 1496 | else => unreachable, |
| 1404 | 1497 | } |
| 1405 | 1498 | } |
| 1406 | 1499 | |
| 1407 | 1500 | fn relaxGotPcTlsDesc(code: []u8) !void { |
| 1408 | | const old_inst = disassemble(code) orelse return error.RelaxFail; |
| 1501 | const old_inst = disassemble(code) orelse return error.RelaxFailure; |
| 1409 | 1502 | switch (old_inst.encoding.mnemonic) { |
| 1410 | 1503 | .lea => { |
| 1411 | 1504 | const inst = try Instruction.new(old_inst.prefix, .mov, &.{ |
| ... | ... | @@ -1414,15 +1507,15 @@ const x86_64 = struct { |
| 1414 | 1507 | .{ .imm = Immediate.s(-129) }, |
| 1415 | 1508 | }); |
| 1416 | 1509 | relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding }); |
| 1417 | | encode(&.{inst}, code) catch return error.RelaxFail; |
| 1510 | try encode(&.{inst}, code); |
| 1418 | 1511 | }, |
| 1419 | | else => return error.RelaxFail, |
| 1512 | else => return error.RelaxFailure, |
| 1420 | 1513 | } |
| 1421 | 1514 | } |
| 1422 | 1515 | |
| 1423 | 1516 | fn relaxTlsGdToLe( |
| 1424 | 1517 | self: Atom, |
| 1425 | | rels: []align(1) const elf.Elf64_Rela, |
| 1518 | rels: []const elf.Elf64_Rela, |
| 1426 | 1519 | value: i32, |
| 1427 | 1520 | elf_file: *Elf, |
| 1428 | 1521 | stream: anytype, |
| ... | ... | @@ -1460,6 +1553,7 @@ const x86_64 = struct { |
| 1460 | 1553 | self.name(elf_file), |
| 1461 | 1554 | rels[0].r_offset, |
| 1462 | 1555 | }); |
| 1556 | return error.RelaxFailure; |
| 1463 | 1557 | }, |
| 1464 | 1558 | } |
| 1465 | 1559 | } |
| ... | ... | @@ -1485,15 +1579,446 @@ const x86_64 = struct { |
| 1485 | 1579 | const Instruction = encoder.Instruction; |
| 1486 | 1580 | }; |
| 1487 | 1581 | |
| 1582 | const aarch64 = struct { |
| 1583 | fn scanReloc( |
| 1584 | atom: Atom, |
| 1585 | elf_file: *Elf, |
| 1586 | rel: elf.Elf64_Rela, |
| 1587 | symbol: *Symbol, |
| 1588 | code: ?[]const u8, |
| 1589 | it: *RelocsIterator, |
| 1590 | ) !void { |
| 1591 | _ = code; |
| 1592 | _ = it; |
| 1593 | |
| 1594 | const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type()); |
| 1595 | switch (r_type) { |
| 1596 | .ABS64 => { |
| 1597 | try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file); |
| 1598 | }, |
| 1599 | |
| 1600 | .ADR_PREL_PG_HI21 => { |
| 1601 | try atom.scanReloc(symbol, rel, pcRelocAction(symbol, elf_file), elf_file); |
| 1602 | }, |
| 1603 | |
| 1604 | .ADR_GOT_PAGE => { |
| 1605 | // TODO: relax if possible |
| 1606 | symbol.flags.needs_got = true; |
| 1607 | }, |
| 1608 | |
| 1609 | .LD64_GOT_LO12_NC, |
| 1610 | .LD64_GOTPAGE_LO15, |
| 1611 | => { |
| 1612 | symbol.flags.needs_got = true; |
| 1613 | }, |
| 1614 | |
| 1615 | .CALL26, |
| 1616 | .JUMP26, |
| 1617 | => { |
| 1618 | if (symbol.flags.import) { |
| 1619 | symbol.flags.needs_plt = true; |
| 1620 | } |
| 1621 | }, |
| 1622 | |
| 1623 | .ADD_ABS_LO12_NC, |
| 1624 | .ADR_PREL_LO21, |
| 1625 | .LDST8_ABS_LO12_NC, |
| 1626 | .LDST16_ABS_LO12_NC, |
| 1627 | .LDST32_ABS_LO12_NC, |
| 1628 | .LDST64_ABS_LO12_NC, |
| 1629 | .LDST128_ABS_LO12_NC, |
| 1630 | => {}, |
| 1631 | |
| 1632 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | fn resolveRelocAlloc( |
| 1637 | atom: Atom, |
| 1638 | elf_file: *Elf, |
| 1639 | rel: elf.Elf64_Rela, |
| 1640 | target: *const Symbol, |
| 1641 | args: ResolveArgs, |
| 1642 | it: *RelocsIterator, |
| 1643 | code: []u8, |
| 1644 | stream: anytype, |
| 1645 | ) (error{ UnexpectedRemainder, DivisionByZero } || RelocError)!void { |
| 1646 | _ = it; |
| 1647 | |
| 1648 | const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type()); |
| 1649 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 1650 | const cwriter = stream.writer(); |
| 1651 | |
| 1652 | const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args; |
| 1653 | _ = TP; |
| 1654 | _ = DTP; |
| 1655 | _ = ZIG_GOT; |
| 1656 | |
| 1657 | switch (r_type) { |
| 1658 | .NONE => unreachable, |
| 1659 | .ABS64 => { |
| 1660 | try atom.resolveDynAbsReloc( |
| 1661 | target, |
| 1662 | rel, |
| 1663 | dynAbsRelocAction(target, elf_file), |
| 1664 | elf_file, |
| 1665 | cwriter, |
| 1666 | ); |
| 1667 | }, |
| 1668 | |
| 1669 | .CALL26, |
| 1670 | .JUMP26, |
| 1671 | => { |
| 1672 | // TODO: add thunk support |
| 1673 | const disp: i28 = math.cast(i28, S + A - P) orelse { |
| 1674 | var err = try elf_file.addErrorWithNotes(1); |
| 1675 | try err.addMsg(elf_file, "TODO: branch relocation target ({s}) exceeds max jump distance", .{ |
| 1676 | target.name(elf_file), |
| 1677 | }); |
| 1678 | try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{ |
| 1679 | atom.file(elf_file).?.fmtPath(), |
| 1680 | atom.name(elf_file), |
| 1681 | r_offset, |
| 1682 | }); |
| 1683 | return; |
| 1684 | }; |
| 1685 | try aarch64_util.writeBranchImm(disp, code[r_offset..][0..4]); |
| 1686 | }, |
| 1687 | |
| 1688 | .ADR_PREL_PG_HI21 => { |
| 1689 | // TODO: check for relaxation of ADRP+ADD |
| 1690 | const saddr = @as(u64, @intCast(P)); |
| 1691 | const taddr = @as(u64, @intCast(S + A)); |
| 1692 | const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr))); |
| 1693 | try aarch64_util.writePages(pages, code[r_offset..][0..4]); |
| 1694 | }, |
| 1695 | |
| 1696 | .ADR_GOT_PAGE => if (target.flags.has_got) { |
| 1697 | const saddr = @as(u64, @intCast(P)); |
| 1698 | const taddr = @as(u64, @intCast(G + GOT + A)); |
| 1699 | const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr))); |
| 1700 | try aarch64_util.writePages(pages, code[r_offset..][0..4]); |
| 1701 | } else { |
| 1702 | // TODO: relax |
| 1703 | var err = try elf_file.addErrorWithNotes(1); |
| 1704 | try err.addMsg(elf_file, "TODO: relax ADR_GOT_PAGE", .{}); |
| 1705 | try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{ |
| 1706 | atom.file(elf_file).?.fmtPath(), |
| 1707 | atom.name(elf_file), |
| 1708 | r_offset, |
| 1709 | }); |
| 1710 | }, |
| 1711 | |
| 1712 | .LD64_GOT_LO12_NC => { |
| 1713 | assert(target.flags.has_got); |
| 1714 | const taddr = @as(u64, @intCast(G + GOT + A)); |
| 1715 | try aarch64_util.writePageOffset(.load_store_64, taddr, code[r_offset..][0..4]); |
| 1716 | }, |
| 1717 | |
| 1718 | .ADD_ABS_LO12_NC, |
| 1719 | .LDST8_ABS_LO12_NC, |
| 1720 | .LDST16_ABS_LO12_NC, |
| 1721 | .LDST32_ABS_LO12_NC, |
| 1722 | .LDST64_ABS_LO12_NC, |
| 1723 | .LDST128_ABS_LO12_NC, |
| 1724 | => { |
| 1725 | // TODO: NC means no overflow check |
| 1726 | const taddr = @as(u64, @intCast(S + A)); |
| 1727 | const kind: aarch64_util.PageOffsetInstKind = switch (r_type) { |
| 1728 | .ADD_ABS_LO12_NC => .arithmetic, |
| 1729 | .LDST8_ABS_LO12_NC => .load_store_8, |
| 1730 | .LDST16_ABS_LO12_NC => .load_store_16, |
| 1731 | .LDST32_ABS_LO12_NC => .load_store_32, |
| 1732 | .LDST64_ABS_LO12_NC => .load_store_64, |
| 1733 | .LDST128_ABS_LO12_NC => .load_store_128, |
| 1734 | else => unreachable, |
| 1735 | }; |
| 1736 | try aarch64_util.writePageOffset(kind, taddr, code[r_offset..][0..4]); |
| 1737 | }, |
| 1738 | |
| 1739 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | fn resolveRelocNonAlloc( |
| 1744 | atom: Atom, |
| 1745 | elf_file: *Elf, |
| 1746 | rel: elf.Elf64_Rela, |
| 1747 | target: *const Symbol, |
| 1748 | args: ResolveArgs, |
| 1749 | it: *RelocsIterator, |
| 1750 | code: []u8, |
| 1751 | stream: anytype, |
| 1752 | ) !void { |
| 1753 | _ = it; |
| 1754 | _ = code; |
| 1755 | _ = target; |
| 1756 | |
| 1757 | const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type()); |
| 1758 | const cwriter = stream.writer(); |
| 1759 | |
| 1760 | _, const A, const S, _, _, _, _, _ = args; |
| 1761 | |
| 1762 | switch (r_type) { |
| 1763 | .NONE => unreachable, |
| 1764 | .ABS32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little), |
| 1765 | .ABS64 => try cwriter.writeInt(i64, S + A, .little), |
| 1766 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | const aarch64_util = @import("../aarch64.zig"); |
| 1771 | }; |
| 1772 | |
| 1773 | const riscv = struct { |
| 1774 | fn scanReloc( |
| 1775 | atom: Atom, |
| 1776 | elf_file: *Elf, |
| 1777 | rel: elf.Elf64_Rela, |
| 1778 | symbol: *Symbol, |
| 1779 | code: ?[]const u8, |
| 1780 | it: *RelocsIterator, |
| 1781 | ) !void { |
| 1782 | _ = code; |
| 1783 | _ = it; |
| 1784 | |
| 1785 | const r_type: elf.R_RISCV = @enumFromInt(rel.r_type()); |
| 1786 | |
| 1787 | switch (r_type) { |
| 1788 | .@"64" => { |
| 1789 | try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file); |
| 1790 | }, |
| 1791 | |
| 1792 | .HI20 => { |
| 1793 | try atom.scanReloc(symbol, rel, absRelocAction(symbol, elf_file), elf_file); |
| 1794 | }, |
| 1795 | |
| 1796 | .CALL_PLT => if (symbol.flags.import) { |
| 1797 | symbol.flags.needs_plt = true; |
| 1798 | }, |
| 1799 | |
| 1800 | .GOT_HI20 => { |
| 1801 | symbol.flags.needs_got = true; |
| 1802 | }, |
| 1803 | |
| 1804 | .PCREL_HI20, |
| 1805 | .PCREL_LO12_I, |
| 1806 | .PCREL_LO12_S, |
| 1807 | .LO12_I, |
| 1808 | .ADD32, |
| 1809 | .SUB32, |
| 1810 | => {}, |
| 1811 | |
| 1812 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | fn resolveRelocAlloc( |
| 1817 | atom: Atom, |
| 1818 | elf_file: *Elf, |
| 1819 | rel: elf.Elf64_Rela, |
| 1820 | target: *const Symbol, |
| 1821 | args: ResolveArgs, |
| 1822 | it: *RelocsIterator, |
| 1823 | code: []u8, |
| 1824 | stream: anytype, |
| 1825 | ) !void { |
| 1826 | const r_type: elf.R_RISCV = @enumFromInt(rel.r_type()); |
| 1827 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 1828 | const cwriter = stream.writer(); |
| 1829 | |
| 1830 | const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args; |
| 1831 | _ = TP; |
| 1832 | _ = DTP; |
| 1833 | _ = ZIG_GOT; |
| 1834 | |
| 1835 | switch (r_type) { |
| 1836 | .NONE => unreachable, |
| 1837 | |
| 1838 | .@"64" => { |
| 1839 | try atom.resolveDynAbsReloc( |
| 1840 | target, |
| 1841 | rel, |
| 1842 | dynAbsRelocAction(target, elf_file), |
| 1843 | elf_file, |
| 1844 | cwriter, |
| 1845 | ); |
| 1846 | }, |
| 1847 | |
| 1848 | .ADD32 => riscv_util.writeAddend(i32, .add, code[r_offset..][0..4], S + A), |
| 1849 | .SUB32 => riscv_util.writeAddend(i32, .sub, code[r_offset..][0..4], S + A), |
| 1850 | |
| 1851 | .HI20 => { |
| 1852 | const value: u32 = @bitCast(math.cast(i32, S + A) orelse return error.Overflow); |
| 1853 | riscv_util.writeInstU(code[r_offset..][0..4], value); |
| 1854 | }, |
| 1855 | |
| 1856 | .LO12_I => { |
| 1857 | const value: u32 = @bitCast(math.cast(i32, S + A) orelse return error.Overflow); |
| 1858 | riscv_util.writeInstI(code[r_offset..][0..4], value); |
| 1859 | }, |
| 1860 | |
| 1861 | .GOT_HI20 => { |
| 1862 | assert(target.flags.has_got); |
| 1863 | const disp: u32 = @bitCast(math.cast(i32, G + GOT + A - P) orelse return error.Overflow); |
| 1864 | riscv_util.writeInstU(code[r_offset..][0..4], disp); |
| 1865 | }, |
| 1866 | |
| 1867 | .CALL_PLT => { |
| 1868 | // TODO: relax |
| 1869 | const disp: u32 = @bitCast(math.cast(i32, S + A - P) orelse return error.Overflow); |
| 1870 | riscv_util.writeInstU(code[r_offset..][0..4], disp); // auipc |
| 1871 | riscv_util.writeInstI(code[r_offset + 4 ..][0..4], disp); // jalr |
| 1872 | }, |
| 1873 | |
| 1874 | .PCREL_HI20 => { |
| 1875 | const disp: u32 = @bitCast(math.cast(i32, S + A - P) orelse return error.Overflow); |
| 1876 | riscv_util.writeInstU(code[r_offset..][0..4], disp); |
| 1877 | }, |
| 1878 | |
| 1879 | .PCREL_LO12_I, |
| 1880 | .PCREL_LO12_S, |
| 1881 | => { |
| 1882 | assert(A == 0); // according to the spec |
| 1883 | // We need to find the paired reloc for this relocation. |
| 1884 | const file_ptr = atom.file(elf_file).?; |
| 1885 | const atom_addr = atom.address(elf_file); |
| 1886 | const pos = it.pos; |
| 1887 | const pair = while (it.prev()) |pair| { |
| 1888 | if (S == atom_addr + pair.r_offset) break pair; |
| 1889 | } else { |
| 1890 | // TODO: implement searching forward |
| 1891 | var err = try elf_file.addErrorWithNotes(1); |
| 1892 | try err.addMsg(elf_file, "TODO: find HI20 paired reloc scanning forward", .{}); |
| 1893 | try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{ |
| 1894 | atom.file(elf_file).?.fmtPath(), |
| 1895 | atom.name(elf_file), |
| 1896 | rel.r_offset, |
| 1897 | }); |
| 1898 | return error.RelocFailure; |
| 1899 | }; |
| 1900 | it.pos = pos; |
| 1901 | const target_ = switch (file_ptr) { |
| 1902 | .zig_object => |x| elf_file.symbol(x.symbol(pair.r_sym())), |
| 1903 | .object => |x| elf_file.symbol(x.symbols.items[pair.r_sym()]), |
| 1904 | else => unreachable, |
| 1905 | }; |
| 1906 | const S_ = @as(i64, @intCast(target_.address(.{}, elf_file))); |
| 1907 | const A_ = pair.r_addend; |
| 1908 | const P_ = @as(i64, @intCast(atom_addr + pair.r_offset)); |
| 1909 | const G_ = @as(i64, @intCast(target_.gotAddress(elf_file))) - GOT; |
| 1910 | const disp = switch (@as(elf.R_RISCV, @enumFromInt(pair.r_type()))) { |
| 1911 | .PCREL_HI20 => math.cast(i32, S_ + A_ - P_) orelse return error.Overflow, |
| 1912 | .GOT_HI20 => math.cast(i32, G_ + GOT + A_ - P_) orelse return error.Overflow, |
| 1913 | else => unreachable, |
| 1914 | }; |
| 1915 | relocs_log.debug(" [{x} => {x}]", .{ P_, disp + P_ }); |
| 1916 | switch (r_type) { |
| 1917 | .PCREL_LO12_I => riscv_util.writeInstI(code[r_offset..][0..4], @bitCast(disp)), |
| 1918 | .PCREL_LO12_S => riscv_util.writeInstS(code[r_offset..][0..4], @bitCast(disp)), |
| 1919 | else => unreachable, |
| 1920 | } |
| 1921 | }, |
| 1922 | |
| 1923 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | fn resolveRelocNonAlloc( |
| 1928 | atom: Atom, |
| 1929 | elf_file: *Elf, |
| 1930 | rel: elf.Elf64_Rela, |
| 1931 | target: *const Symbol, |
| 1932 | args: ResolveArgs, |
| 1933 | it: *RelocsIterator, |
| 1934 | code: []u8, |
| 1935 | stream: anytype, |
| 1936 | ) !void { |
| 1937 | _ = target; |
| 1938 | _ = it; |
| 1939 | |
| 1940 | const r_type: elf.R_RISCV = @enumFromInt(rel.r_type()); |
| 1941 | const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow; |
| 1942 | const cwriter = stream.writer(); |
| 1943 | |
| 1944 | _, const A, const S, const GOT, _, _, const DTP, _ = args; |
| 1945 | _ = GOT; |
| 1946 | _ = DTP; |
| 1947 | |
| 1948 | switch (r_type) { |
| 1949 | .NONE => unreachable, |
| 1950 | |
| 1951 | .@"32" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little), |
| 1952 | .@"64" => try cwriter.writeInt(i64, S + A, .little), |
| 1953 | |
| 1954 | .ADD8 => riscv_util.writeAddend(i8, .add, code[r_offset..][0..1], S + A), |
| 1955 | .SUB8 => riscv_util.writeAddend(i8, .sub, code[r_offset..][0..1], S + A), |
| 1956 | .ADD16 => riscv_util.writeAddend(i16, .add, code[r_offset..][0..2], S + A), |
| 1957 | .SUB16 => riscv_util.writeAddend(i16, .sub, code[r_offset..][0..2], S + A), |
| 1958 | .ADD32 => riscv_util.writeAddend(i32, .add, code[r_offset..][0..4], S + A), |
| 1959 | .SUB32 => riscv_util.writeAddend(i32, .sub, code[r_offset..][0..4], S + A), |
| 1960 | .ADD64 => riscv_util.writeAddend(i64, .add, code[r_offset..][0..8], S + A), |
| 1961 | .SUB64 => riscv_util.writeAddend(i64, .sub, code[r_offset..][0..8], S + A), |
| 1962 | |
| 1963 | .SET8 => mem.writeInt(i8, code[r_offset..][0..1], @as(i8, @truncate(S + A)), .little), |
| 1964 | .SET16 => mem.writeInt(i16, code[r_offset..][0..2], @as(i16, @truncate(S + A)), .little), |
| 1965 | .SET32 => mem.writeInt(i32, code[r_offset..][0..4], @as(i32, @truncate(S + A)), .little), |
| 1966 | |
| 1967 | .SET6 => riscv_util.writeSetSub6(.set, code[r_offset..][0..1], S + A), |
| 1968 | .SUB6 => riscv_util.writeSetSub6(.sub, code[r_offset..][0..1], S + A), |
| 1969 | |
| 1970 | else => try atom.reportUnhandledRelocError(rel, elf_file), |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | const riscv_util = @import("../riscv.zig"); |
| 1975 | }; |
| 1976 | |
| 1977 | const ResolveArgs = struct { i64, i64, i64, i64, i64, i64, i64, i64 }; |
| 1978 | |
| 1979 | const RelocError = error{ |
| 1980 | Overflow, |
| 1981 | OutOfMemory, |
| 1982 | NoSpaceLeft, |
| 1983 | RelocFailure, |
| 1984 | RelaxFailure, |
| 1985 | UnsupportedCpuArch, |
| 1986 | }; |
| 1987 | |
| 1988 | const RelocsIterator = struct { |
| 1989 | relocs: []const elf.Elf64_Rela, |
| 1990 | pos: i64 = -1, |
| 1991 | |
| 1992 | fn next(it: *RelocsIterator) ?elf.Elf64_Rela { |
| 1993 | it.pos += 1; |
| 1994 | if (it.pos >= it.relocs.len) return null; |
| 1995 | return it.relocs[@intCast(it.pos)]; |
| 1996 | } |
| 1997 | |
| 1998 | fn prev(it: *RelocsIterator) ?elf.Elf64_Rela { |
| 1999 | if (it.pos == -1) return null; |
| 2000 | const rel = it.relocs[@intCast(it.pos)]; |
| 2001 | it.pos -= 1; |
| 2002 | return rel; |
| 2003 | } |
| 2004 | |
| 2005 | fn skip(it: *RelocsIterator, num: usize) void { |
| 2006 | assert(num > 0); |
| 2007 | it.pos += @intCast(num); |
| 2008 | } |
| 2009 | }; |
| 2010 | |
| 1488 | 2011 | const std = @import("std"); |
| 1489 | 2012 | const assert = std.debug.assert; |
| 1490 | 2013 | const elf = std.elf; |
| 1491 | 2014 | const eh_frame = @import("eh_frame.zig"); |
| 1492 | 2015 | const log = std.log.scoped(.link); |
| 2016 | const math = std.math; |
| 2017 | const mem = std.mem; |
| 1493 | 2018 | const relocs_log = std.log.scoped(.link_relocs); |
| 1494 | 2019 | const relocation = @import("relocation.zig"); |
| 1495 | 2020 | |
| 1496 | | const Allocator = std.mem.Allocator; |
| 2021 | const Allocator = mem.Allocator; |
| 1497 | 2022 | const Atom = @This(); |
| 1498 | 2023 | const Elf = @import("../Elf.zig"); |
| 1499 | 2024 | const Fde = eh_frame.Fde; |