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 {
10241024 0b10 => .imports,
10251025 else => unreachable,
10261026 },
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 },
10281032 .offset = @intCast(u32, end_offset - 4),
10291033 .addend = 0,
10301034 .pcrel = true,
......@@ -1142,12 +1146,10 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11421146 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
11431147 // Add relocation to the decl.
11441148 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
1149 const target = macho_file.getGlobalByIndex(relocation.sym_index);
11451150 try atom.relocs.append(emit.bin_file.allocator, .{
11461151 .offset = offset,
1147 .target = .{
1148 .sym_index = relocation.sym_index,
1149 .file = null,
1150 },
1152 .target = target,
11511153 .addend = 0,
11521154 .subtractor = null,
11531155 .pcrel = true,
......@@ -1157,16 +1159,17 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11571159 } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
11581160 // Add relocation to the decl.
11591161 const atom = coff_file.atom_by_index_table.get(relocation.atom_index).?;
1162 const target = coff_file.getGlobalByIndex(relocation.sym_index);
11601163 try atom.addRelocation(coff_file, .{
11611164 .@"type" = .direct,
1162 .target = .{ .sym_index = relocation.sym_index, .file = null },
1165 .target = target,
11631166 .offset = offset,
11641167 .addend = 0,
11651168 .pcrel = true,
11661169 .length = 2,
11671170 });
11681171 } 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", .{});
11701173 }
11711174}
11721175
src/link/Coff.zig+10-3
......@@ -1544,9 +1544,10 @@ pub fn getDeclVAddr(
15441544
15451545pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 {
15461546 const gop = try self.getOrPutGlobalPtr(name);
1547 const global_index = self.getGlobalIndex(name).?;
15471548
15481549 if (gop.found_existing) {
1549 return gop.value_ptr.sym_index;
1550 return global_index;
15501551 }
15511552
15521553 const sym_index = try self.allocateSymbol();
......@@ -1559,9 +1560,9 @@ pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 {
15591560 try self.setSymbolName(sym, sym_name);
15601561 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;
15651566}
15661567
15671568pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {
......@@ -2077,6 +2078,12 @@ pub fn getGlobalIndex(self: *const Coff, name: []const u8) ?u32 {
20772078 return self.resolver.get(name);
20782079}
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
20802087const GetOrPutGlobalPtrResult = struct {
20812088 found_existing: bool,
20822089 value_ptr: *SymbolWithLoc,
src/link/MachO.zig+10-3
......@@ -4890,9 +4890,10 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
48904890 const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
48914891 defer gpa.free(sym_name);
48924892 const gop = try self.getOrPutGlobalPtr(sym_name);
4893 const global_index = self.getGlobalIndex(sym_name).?;
48934894
48944895 if (gop.found_existing) {
4895 return gop.value_ptr.sym_index;
4896 return global_index;
48964897 }
48974898
48984899 const sym_index = try self.allocateSymbol();
......@@ -4902,9 +4903,9 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
49024903 const sym = self.getSymbolPtr(sym_loc);
49034904 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;
49084909}
49094910
49104911fn 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 {
58305831 return self.resolver.get(name);
58315832}
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
58335840const GetOrPutGlobalPtrResult = struct {
58345841 found_existing: bool,
58355842 value_ptr: *SymbolWithLoc,