authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-09 23:30:31+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-09 23:30:31+02:00
logd8f210354577eda1b438d692b28bb3a582913b5a
tree5855f0bc25efb2ba6617b8a6cad46f19d0958f82
parentbac065c7cfb6f3aa0fe1cdf1334adb5a46d9a2af

macho+coff: return index into global table from getGlobalSymbol


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