authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-29 18:15:08+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
logf0d4ce4494f910508a127aad83cfbc557ac4aef9
tree7e2c74dbf6a34c41ed1c912e9662b5c0e6857591
parentdb1a3bb0e70338dc5261adf148284c1408d3df87

coff: add basic handling of GOT PC relative indirection


4 files changed, 157 insertions(+), 64 deletions(-)

src/arch/x86_64/CodeGen.zig+47-15
......@@ -2664,6 +2664,10 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26642664 };
26652665 const mod = self.bin_file.options.module.?;
26662666 const fn_owner_decl = mod.declPtr(self.mod_fn.owner_decl);
2667 const atom_index = if (self.bin_file.tag == link.File.MachO.base_tag)
2668 fn_owner_decl.link.macho.sym_index
2669 else
2670 fn_owner_decl.link.coff.sym_index;
26672671 _ = try self.addInst(.{
26682672 .tag = .lea_pie,
26692673 .ops = Mir.Inst.Ops.encode(.{
......@@ -2672,7 +2676,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26722676 }),
26732677 .data = .{
26742678 .relocation = .{
2675 .atom_index = fn_owner_decl.link.macho.sym_index,
2679 .atom_index = atom_index,
26762680 .sym_index = sym_index,
26772681 },
26782682 },
......@@ -3961,21 +3965,17 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39613965 // Due to incremental compilation, how function calls are generated depends
39623966 // on linking.
39633967 const mod = self.bin_file.options.module.?;
3964 if (self.bin_file.tag == link.File.Elf.base_tag or self.bin_file.tag == link.File.Coff.base_tag) {
3968 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
39653969 if (self.air.value(callee)) |func_value| {
39663970 if (func_value.castTag(.function)) |func_payload| {
39673971 const func = func_payload.data;
39683972 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
39693973 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
39703974 const fn_owner_decl = mod.declPtr(func.owner_decl);
3971 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
3975 const got_addr = blk: {
39723976 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
39733977 break :blk @intCast(u32, got.p_vaddr + fn_owner_decl.link.elf.offset_table_index * ptr_bytes);
3974 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| blk: {
3975 const got_atom = coff_file.getGotAtomForSymbol(.{ .sym_index = fn_owner_decl.link.coff.sym_index, .file = null }).?;
3976 const got_sym = coff_file.getSymbol(got_atom.getSymbolWithLoc());
3977 break :blk got_sym.value;
3978 } else unreachable;
3978 };
39793979 _ = try self.addInst(.{
39803980 .tag = .call,
39813981 .ops = Mir.Inst.Ops.encode(.{ .flags = 0b01 }),
......@@ -3999,14 +3999,47 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39993999 .data = undefined,
40004000 });
40014001 }
4002 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4002 } else if (self.bin_file.cast(link.File.Coff)) |_| {
40034003 if (self.air.value(callee)) |func_value| {
40044004 if (func_value.castTag(.function)) |func_payload| {
40054005 const func = func_payload.data;
40064006 const fn_owner_decl = mod.declPtr(func.owner_decl);
4007 try self.genSetReg(Type.initTag(.usize), .rax, .{
4008 .got_load = fn_owner_decl.link.macho.sym_index,
4007 const sym_index = fn_owner_decl.link.coff.sym_index;
4008 try self.genSetReg(Type.initTag(.usize), .rax, .{ .got_load = sym_index });
4009 // callq *%rax
4010 _ = try self.addInst(.{
4011 .tag = .call,
4012 .ops = Mir.Inst.Ops.encode(.{
4013 .reg1 = .rax,
4014 .flags = 0b01,
4015 }),
4016 .data = undefined,
40094017 });
4018 } else if (func_value.castTag(.extern_fn)) |_| {
4019 return self.fail("TODO implement calling extern functions", .{});
4020 } else {
4021 return self.fail("TODO implement calling bitcasted functions", .{});
4022 }
4023 } else {
4024 assert(ty.zigTypeTag() == .Pointer);
4025 const mcv = try self.resolveInst(callee);
4026 try self.genSetReg(Type.initTag(.usize), .rax, mcv);
4027 _ = try self.addInst(.{
4028 .tag = .call,
4029 .ops = Mir.Inst.Ops.encode(.{
4030 .reg1 = .rax,
4031 .flags = 0b01,
4032 }),
4033 .data = undefined,
4034 });
4035 }
4036 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4037 if (self.air.value(callee)) |func_value| {
4038 if (func_value.castTag(.function)) |func_payload| {
4039 const func = func_payload.data;
4040 const fn_owner_decl = mod.declPtr(func.owner_decl);
4041 const sym_index = fn_owner_decl.link.macho.sym_index;
4042 try self.genSetReg(Type.initTag(.usize), .rax, .{ .got_load = sym_index });
40104043 // callq *%rax
40114044 _ = try self.addInst(.{
40124045 .tag = .call,
......@@ -6847,10 +6880,9 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
68476880 // the linker has enough info to perform relocations.
68486881 assert(decl.link.macho.sym_index != 0);
68496882 return MCValue{ .got_load = decl.link.macho.sym_index };
6850 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
6851 const got_atom = coff_file.getGotAtomForSymbol(.{ .sym_index = decl.link.coff.sym_index, .file = null }).?;
6852 const got_sym = coff_file.getSymbol(got_atom.getSymbolWithLoc());
6853 return MCValue{ .memory = got_sym.value };
6883 } else if (self.bin_file.cast(link.File.Coff)) |_| {
6884 assert(decl.link.coff.sym_index != 0);
6885 return MCValue{ .got_load = decl.link.coff.sym_index };
68546886 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
68556887 try p9.seeDecl(decl_index);
68566888 const got_addr = p9.bases.data + decl.link.plan9.got_index.? * ptr_bytes;
src/arch/x86_64/Emit.zig+20-1
......@@ -994,6 +994,7 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
994994 );
995995
996996 const end_offset = emit.code.items.len;
997 const gpa = emit.bin_file.allocator;
997998
998999 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
9991000 const reloc_type = switch (ops.flags) {
......@@ -1003,7 +1004,7 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
10031004 };
10041005 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
10051006 log.debug("adding reloc of type {} to local @{d}", .{ reloc_type, relocation.sym_index });
1006 try atom.relocs.append(emit.bin_file.allocator, .{
1007 try atom.relocs.append(gpa, .{
10071008 .offset = @intCast(u32, end_offset - 4),
10081009 .target = .{ .sym_index = relocation.sym_index, .file = null },
10091010 .addend = 0,
......@@ -1012,6 +1013,24 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
10121013 .length = 2,
10131014 .@"type" = reloc_type,
10141015 });
1016 } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
1017 const atom = coff_file.atom_by_index_table.get(relocation.atom_index).?;
1018 log.debug("adding reloc to local @{d}", .{relocation.sym_index});
1019 const gop = try coff_file.relocs.getOrPut(gpa, atom);
1020 if (!gop.found_existing) {
1021 gop.value_ptr.* = .{};
1022 }
1023 try gop.value_ptr.append(gpa, .{
1024 .@"type" = switch (ops.flags) {
1025 0b00 => .got_pcrel,
1026 0b01 => .direct,
1027 else => return emit.fail("TODO unused LEA PIE variants 0b10 and 0b11", .{}),
1028 },
1029 .target = .{ .sym_index = relocation.sym_index, .file = null },
1030 .offset = @intCast(u32, end_offset - 4),
1031 .addend = 0,
1032 .prev_vaddr = atom.getSymbol(coff_file).value,
1033 });
10151034 } else {
10161035 return emit.fail(
10171036 "TODO implement lea reg, [rip + reloc] for linking backends different than MachO",
src/link/Coff.zig+82-43
......@@ -104,6 +104,10 @@ unnamed_const_atoms: UnnamedConstTable = .{},
104104relocs: RelocTable = .{},
105105
106106const Reloc = struct {
107 @"type": enum {
108 got_pcrel,
109 direct,
110 },
107111 target: SymbolWithLoc,
108112 offset: u32,
109113 addend: u32,
......@@ -413,10 +417,11 @@ pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {
413417 try self.decls.putNoClobber(gpa, decl_index, null);
414418}
415419
416fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32, sect_id: u16) !u32 {
420fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u32 {
417421 const tracy = trace(@src());
418422 defer tracy.end();
419423
424 const sect_id = @enumToInt(atom.getSymbol(self).section_number) - 1;
420425 const header = &self.sections.items(.header)[sect_id];
421426 const free_list = &self.sections.items(.free_list)[sect_id];
422427 const maybe_last_atom = &self.sections.items(.last_atom)[sect_id];
......@@ -580,18 +585,20 @@ fn createGotAtom(self: *Coff, target: SymbolWithLoc) !*Atom {
580585
581586 try self.managed_atoms.append(gpa, atom);
582587 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
588 self.got_entries.getPtr(target).?.* = atom.sym_index;
583589
584590 const sym = atom.getSymbolPtr(self);
585 sym.value = try self.allocateAtom(atom, atom.size, atom.alignment, self.got_section_index.?);
586591 sym.section_number = @intToEnum(coff.SectionNumber, self.got_section_index.? + 1);
592 sym.value = try self.allocateAtom(atom, atom.size, atom.alignment);
587593
588 log.debug("allocated {s} atom at 0x{x}", .{ atom.getName(self), sym.value });
594 log.debug("allocated GOT atom at 0x{x}", .{sym.value});
589595
590596 const gop_relocs = try self.relocs.getOrPut(gpa, atom);
591597 if (!gop_relocs.found_existing) {
592598 gop_relocs.value_ptr.* = .{};
593599 }
594600 try gop_relocs.value_ptr.append(gpa, .{
601 .@"type" = .direct,
595602 .target = target,
596603 .offset = 0,
597604 .addend = 0,
......@@ -601,72 +608,98 @@ fn createGotAtom(self: *Coff, target: SymbolWithLoc) !*Atom {
601608 return atom;
602609}
603610
604fn growAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32, sect_id: u16) !u32 {
611fn growAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u32 {
605612 const sym = atom.getSymbol(self);
606613 const align_ok = mem.alignBackwardGeneric(u32, sym.value, alignment) == sym.value;
607614 const need_realloc = !align_ok or new_atom_size > atom.capacity(self);
608615 if (!need_realloc) return sym.value;
609 return self.allocateAtom(atom, new_atom_size, alignment, sect_id);
616 return self.allocateAtom(atom, new_atom_size, alignment);
610617}
611618
612fn shrinkAtom(self: *Coff, atom: *Atom, new_block_size: u32, sect_id: u16) void {
619fn shrinkAtom(self: *Coff, atom: *Atom, new_block_size: u32) void {
613620 _ = self;
614621 _ = atom;
615622 _ = new_block_size;
616 _ = sect_id;
617623 // TODO check the new capacity, and if it crosses the size threshold into a big enough
618624 // capacity, insert a free list node for it.
619625}
620626
621fn writeAtom(self: *Coff, atom: *Atom, code: []const u8, sect_id: u16) !void {
622 const section = self.sections.get(sect_id);
627fn writeAtom(self: *Coff, atom: *Atom, code: []const u8) !void {
623628 const sym = atom.getSymbol(self);
629 const section = self.sections.get(@enumToInt(sym.section_number) - 1);
624630 const file_offset = section.header.pointer_to_raw_data + sym.value - section.header.virtual_address;
625 const resolved = try self.resolveRelocs(atom, code);
626 defer self.base.allocator.free(resolved);
627631 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });
628 try self.base.file.?.pwriteAll(resolved, file_offset);
632 try self.base.file.?.pwriteAll(code, file_offset);
633 try self.resolveRelocs(atom);
629634}
630635
631636fn writeGotAtom(self: *Coff, atom: *Atom) !void {
632637 switch (self.ptr_width) {
633638 .p32 => {
634639 var buffer: [@sizeOf(u32)]u8 = [_]u8{0} ** @sizeOf(u32);
635 try self.writeAtom(atom, &buffer, self.got_section_index.?);
640 try self.writeAtom(atom, &buffer);
636641 },
637642 .p64 => {
638643 var buffer: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64);
639 try self.writeAtom(atom, &buffer, self.got_section_index.?);
644 try self.writeAtom(atom, &buffer);
640645 },
641646 }
642647}
643648
644fn resolveRelocs(self: *Coff, atom: *Atom, code: []const u8) ![]const u8 {
645 const gpa = self.base.allocator;
646 const resolved = try gpa.dupe(u8, code);
647 const relocs = self.relocs.get(atom) orelse return resolved;
649fn resolveRelocs(self: *Coff, atom: *Atom) !void {
650 const relocs = self.relocs.get(atom) orelse return;
651 const source_sym = atom.getSymbol(self);
652 const source_section = self.sections.get(@enumToInt(source_sym.section_number) - 1).header;
653 const file_offset = source_section.pointer_to_raw_data + source_sym.value - source_section.virtual_address;
654
655 log.debug("relocating '{s}'", .{atom.getName(self)});
648656
649657 for (relocs.items) |*reloc| {
650 const target_sym = self.getSymbol(reloc.target);
651 const target_vaddr = target_sym.value + reloc.addend;
652 if (target_vaddr == reloc.prev_vaddr) continue;
658 const target_vaddr = switch (reloc.@"type") {
659 .got_pcrel => blk: {
660 const got_atom = self.getGotAtomForSymbol(reloc.target) orelse continue;
661 break :blk got_atom.getSymbol(self).value;
662 },
663 .direct => self.getSymbol(reloc.target).value,
664 };
665 const target_vaddr_with_addend = target_vaddr + reloc.addend;
653666
654 log.debug(" ({x}: [() => 0x{x} ({s}))", .{ reloc.offset, target_vaddr, self.getSymbolName(reloc.target) });
667 if (target_vaddr_with_addend == reloc.prev_vaddr) continue;
655668
656 switch (self.ptr_width) {
657 .p32 => mem.writeIntLittle(u32, resolved[reloc.offset..][0..4], @intCast(u32, target_vaddr)),
658 .p64 => mem.writeIntLittle(u64, resolved[reloc.offset..][0..8], target_vaddr),
669 log.debug(" ({x}: [() => 0x{x} ({s})) ({s})", .{
670 reloc.offset,
671 target_vaddr_with_addend,
672 self.getSymbolName(reloc.target),
673 @tagName(reloc.@"type"),
674 });
675
676 switch (reloc.@"type") {
677 .got_pcrel => {
678 const source_vaddr = source_sym.value + reloc.offset;
679 const disp = target_vaddr_with_addend - source_vaddr - 4;
680 try self.base.file.?.pwriteAll(mem.asBytes(&@intCast(u32, disp)), file_offset + reloc.offset);
681 },
682 .direct => switch (self.ptr_width) {
683 .p32 => try self.base.file.?.pwriteAll(
684 mem.asBytes(&@intCast(u32, target_vaddr_with_addend + default_image_base_exe)),
685 file_offset + reloc.offset,
686 ),
687 .p64 => try self.base.file.?.pwriteAll(
688 mem.asBytes(&(target_vaddr_with_addend + default_image_base_exe)),
689 file_offset + reloc.offset,
690 ),
691 },
659692 }
660693
661 reloc.prev_vaddr = target_vaddr;
694 reloc.prev_vaddr = target_vaddr_with_addend;
662695 }
663
664 return resolved;
665696}
666697
667fn freeAtom(self: *Coff, atom: *Atom, sect_id: u16) void {
698fn freeAtom(self: *Coff, atom: *Atom) void {
668699 log.debug("freeAtom {*}", .{atom});
669700
701 const sym = atom.getSymbol(self);
702 const sect_id = @enumToInt(sym.section_number) - 1;
670703 const free_list = &self.sections.items(.free_list)[sect_id];
671704 var already_have_free_list_node = false;
672705 {
......@@ -858,11 +891,14 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
858891 assert(atom.sym_index != 0); // Caller forgot to allocateDeclIndexes()
859892 if (atom.size != 0) {
860893 const sym = atom.getSymbolPtr(self);
894 try self.setSymbolName(sym, decl_name);
895 sym.section_number = @intToEnum(coff.SectionNumber, sect_index + 1);
896 sym.@"type" = .{ .complex_type = complex_type, .base_type = .NULL };
897
861898 const capacity = atom.capacity(self);
862899 const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u64, sym.value, required_alignment);
863
864900 if (need_realloc) {
865 const vaddr = try self.growAtom(atom, code_len, required_alignment, sect_index);
901 const vaddr = try self.growAtom(atom, code_len, required_alignment);
866902 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, sym.value, vaddr });
867903 log.debug(" (required alignment 0x{x}", .{required_alignment});
868904
......@@ -873,24 +909,20 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
873909 try self.writeGotAtom(got_atom);
874910 }
875911 } else if (code_len < atom.size) {
876 self.shrinkAtom(atom, code_len, sect_index);
912 self.shrinkAtom(atom, code_len);
877913 }
878914 atom.size = code_len;
879 try self.setSymbolName(sym, decl_name);
880 sym.section_number = @intToEnum(coff.SectionNumber, sect_index + 1);
881 sym.@"type" = .{ .complex_type = complex_type, .base_type = .NULL };
882915 } else {
883916 const sym = atom.getSymbolPtr(self);
884917 try self.setSymbolName(sym, decl_name);
885 const vaddr = try self.allocateAtom(atom, code_len, required_alignment, sect_index);
886 errdefer self.freeAtom(atom, sect_index);
918 sym.section_number = @intToEnum(coff.SectionNumber, sect_index + 1);
919 sym.@"type" = .{ .complex_type = complex_type, .base_type = .NULL };
887920
921 const vaddr = try self.allocateAtom(atom, code_len, required_alignment);
922 errdefer self.freeAtom(atom);
888923 log.debug("allocated atom for {s} at 0x{x}", .{ decl_name, vaddr });
889
890924 atom.size = code_len;
891925 sym.value = vaddr;
892 sym.section_number = @intToEnum(coff.SectionNumber, sect_index + 1);
893 sym.@"type" = .{ .complex_type = complex_type, .base_type = .NULL };
894926
895927 const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null };
896928 _ = try self.allocateGotEntry(got_target);
......@@ -898,7 +930,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
898930 try self.writeGotAtom(got_atom);
899931 }
900932
901 try self.writeAtom(atom, code, sect_index);
933 try self.writeAtom(atom, code);
902934}
903935
904936pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {
......@@ -912,8 +944,8 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {
912944 log.debug("freeDecl {*}", .{decl});
913945
914946 const kv = self.decls.fetchRemove(decl_index);
915 if (kv.?.value) |index| {
916 self.freeAtom(&decl.link.coff, index);
947 if (kv.?.value) |_| {
948 self.freeAtom(&decl.link.coff);
917949 }
918950
919951 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
......@@ -1134,6 +1166,13 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
11341166 self.logSymtab();
11351167 }
11361168
1169 {
1170 var it = self.relocs.keyIterator();
1171 while (it.next()) |atom| {
1172 try self.resolveRelocs(atom.*);
1173 }
1174 }
1175
11371176 if (self.getEntryPoint()) |entry_sym_loc| {
11381177 self.entry_addr = self.getSymbol(entry_sym_loc).value;
11391178 }
src/link/Coff/Atom.zig+8-5
......@@ -45,8 +45,11 @@ pub fn deinit(self: *Atom, gpa: Allocator) void {
4545}
4646
4747/// Returns symbol referencing this atom.
48pub fn getSymbol(self: Atom, coff_file: *Coff) coff.Symbol {
49 return self.getSymbolPtr(coff_file).*;
48pub fn getSymbol(self: Atom, coff_file: *const Coff) *const coff.Symbol {
49 return coff_file.getSymbol(.{
50 .sym_index = self.sym_index,
51 .file = self.file,
52 });
5053}
5154
5255/// Returns pointer-to-symbol referencing this atom.
......@@ -62,7 +65,7 @@ pub fn getSymbolWithLoc(self: Atom) SymbolWithLoc {
6265}
6366
6467/// Returns the name of this atom.
65pub fn getName(self: Atom, coff_file: *Coff) []const u8 {
68pub fn getName(self: Atom, coff_file: *const Coff) []const u8 {
6669 return coff_file.getSymbolName(.{
6770 .sym_index = self.sym_index,
6871 .file = self.file,
......@@ -70,7 +73,7 @@ pub fn getName(self: Atom, coff_file: *Coff) []const u8 {
7073}
7174
7275/// Returns how much room there is to grow in virtual address space.
73pub fn capacity(self: Atom, coff_file: *Coff) u32 {
76pub fn capacity(self: Atom, coff_file: *const Coff) u32 {
7477 const self_sym = self.getSymbol(coff_file);
7578 if (self.next) |next| {
7679 const next_sym = next.getSymbol(coff_file);
......@@ -82,7 +85,7 @@ pub fn capacity(self: Atom, coff_file: *Coff) u32 {
8285 }
8386}
8487
85pub fn freeListEligible(self: Atom, coff_file: *Coff) bool {
88pub fn freeListEligible(self: Atom, coff_file: *const Coff) bool {
8689 // No need to keep a free list node for the last atom.
8790 const next = self.next orelse return false;
8891 const self_sym = self.getSymbol(coff_file);