| author | |
| committer | |
| log | d8f210354577eda1b438d692b28bb3a582913b5a |
| tree | 5855f0bc25efb2ba6617b8a6cad46f19d0958f82 |
| parent | bac065c7cfb6f3aa0fe1cdf1334adb5a46d9a2af |
3 files changed, 30 insertions(+), 13 deletions(-)
src/arch/x86_64/Emit.zig+10-7| ... | ... | @@ -1024,7 +1024,11 @@ fn mirLeaPic(emit: *Emit, inst: Mir.Inst.Index) InnerError!void { |
| 1024 | 1024 | 0b10 => .imports, |
| 1025 | 1025 | else => unreachable, |
| 1026 | 1026 | }, |
| 1027 | .target = .{ .sym_index = relocation.sym_index, .file = null }, | |
| 1027 | .target = switch (ops.flags) { | |
| 1028 | 0b00, 0b01 => .{ .sym_index = relocation.sym_index, .file = null }, | |
| 1029 | 0b10 => coff_file.getGlobalByIndex(relocation.sym_index), | |
| 1030 | else => unreachable, | |
| 1031 | }, | |
| 1028 | 1032 | .offset = @intCast(u32, end_offset - 4), |
| 1029 | 1033 | .addend = 0, |
| 1030 | 1034 | .pcrel = true, |
| ... | ... | @@ -1142,12 +1146,10 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void { |
| 1142 | 1146 | if (emit.bin_file.cast(link.File.MachO)) |macho_file| { |
| 1143 | 1147 | // Add relocation to the decl. |
| 1144 | 1148 | const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?; |
| 1149 | const target = macho_file.getGlobalByIndex(relocation.sym_index); | |
| 1145 | 1150 | try atom.relocs.append(emit.bin_file.allocator, .{ |
| 1146 | 1151 | .offset = offset, |
| 1147 | .target = .{ | |
| 1148 | .sym_index = relocation.sym_index, | |
| 1149 | .file = null, | |
| 1150 | }, | |
| 1152 | .target = target, | |
| 1151 | 1153 | .addend = 0, |
| 1152 | 1154 | .subtractor = null, |
| 1153 | 1155 | .pcrel = true, |
| ... | ... | @@ -1157,16 +1159,17 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void { |
| 1157 | 1159 | } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| { |
| 1158 | 1160 | // Add relocation to the decl. |
| 1159 | 1161 | const atom = coff_file.atom_by_index_table.get(relocation.atom_index).?; |
| 1162 | const target = coff_file.getGlobalByIndex(relocation.sym_index); | |
| 1160 | 1163 | try atom.addRelocation(coff_file, .{ |
| 1161 | 1164 | .@"type" = .direct, |
| 1162 | .target = .{ .sym_index = relocation.sym_index, .file = null }, | |
| 1165 | .target = target, | |
| 1163 | 1166 | .offset = offset, |
| 1164 | 1167 | .addend = 0, |
| 1165 | 1168 | .pcrel = true, |
| 1166 | 1169 | .length = 2, |
| 1167 | 1170 | }); |
| 1168 | 1171 | } else { |
| 1169 | return emit.fail("TODO implement call_extern for linking backends different than MachO", .{}); | |
| 1172 | return emit.fail("TODO implement call_extern for linking backends different than MachO and COFF", .{}); | |
| 1170 | 1173 | } |
| 1171 | 1174 | } |
| 1172 | 1175 |
src/link/Coff.zig+10-3| ... | ... | @@ -1544,9 +1544,10 @@ pub fn getDeclVAddr( |
| 1544 | 1544 | |
| 1545 | 1545 | pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 { |
| 1546 | 1546 | const gop = try self.getOrPutGlobalPtr(name); |
| 1547 | const global_index = self.getGlobalIndex(name).?; | |
| 1547 | 1548 | |
| 1548 | 1549 | if (gop.found_existing) { |
| 1549 | return gop.value_ptr.sym_index; | |
| 1550 | return global_index; | |
| 1550 | 1551 | } |
| 1551 | 1552 | |
| 1552 | 1553 | const sym_index = try self.allocateSymbol(); |
| ... | ... | @@ -1559,9 +1560,9 @@ pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 { |
| 1559 | 1560 | try self.setSymbolName(sym, sym_name); |
| 1560 | 1561 | sym.storage_class = .EXTERNAL; |
| 1561 | 1562 | |
| 1562 | try self.unresolved.putNoClobber(gpa, self.getGlobalIndex(name).?, true); | |
| 1563 | try self.unresolved.putNoClobber(gpa, global_index, true); | |
| 1563 | 1564 | |
| 1564 | return sym_index; | |
| 1565 | return global_index; | |
| 1565 | 1566 | } |
| 1566 | 1567 | |
| 1567 | 1568 | pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void { |
| ... | ... | @@ -2077,6 +2078,12 @@ pub fn getGlobalIndex(self: *const Coff, name: []const u8) ?u32 { |
| 2077 | 2078 | return self.resolver.get(name); |
| 2078 | 2079 | } |
| 2079 | 2080 | |
| 2081 | /// Returns global entry at `index`. | |
| 2082 | pub fn getGlobalByIndex(self: *const Coff, index: u32) SymbolWithLoc { | |
| 2083 | assert(index < self.globals.items.len); | |
| 2084 | return self.globals.items[index]; | |
| 2085 | } | |
| 2086 | ||
| 2080 | 2087 | const GetOrPutGlobalPtrResult = struct { |
| 2081 | 2088 | found_existing: bool, |
| 2082 | 2089 | value_ptr: *SymbolWithLoc, |
src/link/MachO.zig+10-3| ... | ... | @@ -4890,9 +4890,10 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 { |
| 4890 | 4890 | const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name}); |
| 4891 | 4891 | defer gpa.free(sym_name); |
| 4892 | 4892 | const gop = try self.getOrPutGlobalPtr(sym_name); |
| 4893 | const global_index = self.getGlobalIndex(sym_name).?; | |
| 4893 | 4894 | |
| 4894 | 4895 | if (gop.found_existing) { |
| 4895 | return gop.value_ptr.sym_index; | |
| 4896 | return global_index; | |
| 4896 | 4897 | } |
| 4897 | 4898 | |
| 4898 | 4899 | const sym_index = try self.allocateSymbol(); |
| ... | ... | @@ -4902,9 +4903,9 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 { |
| 4902 | 4903 | const sym = self.getSymbolPtr(sym_loc); |
| 4903 | 4904 | sym.n_strx = try self.strtab.insert(gpa, sym_name); |
| 4904 | 4905 | |
| 4905 | try self.unresolved.putNoClobber(gpa, self.getGlobalIndex(sym_name).?, true); | |
| 4906 | try self.unresolved.putNoClobber(gpa, global_index, true); | |
| 4906 | 4907 | |
| 4907 | return sym_index; | |
| 4908 | return global_index; | |
| 4908 | 4909 | } |
| 4909 | 4910 | |
| 4910 | 4911 | fn getSegmentAllocBase(self: MachO, indices: []const ?u8) struct { vmaddr: u64, fileoff: u64 } { |
| ... | ... | @@ -5830,6 +5831,12 @@ pub fn getGlobalIndex(self: *const MachO, name: []const u8) ?u32 { |
| 5830 | 5831 | return self.resolver.get(name); |
| 5831 | 5832 | } |
| 5832 | 5833 | |
| 5834 | /// Returns global entry at `index`. | |
| 5835 | pub fn getGlobalByIndex(self: *const MachO, index: u32) SymbolWithLoc { | |
| 5836 | assert(index < self.globals.items.len); | |
| 5837 | return self.globals.items[index]; | |
| 5838 | } | |
| 5839 | ||
| 5833 | 5840 | const GetOrPutGlobalPtrResult = struct { |
| 5834 | 5841 | found_existing: bool, |
| 5835 | 5842 | value_ptr: *SymbolWithLoc, |