authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-29 18:00:57+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-29 18:55:58+01:00
log71dfea1f174b0531139e33e8aa4fe86ccd9f23dd
tree1981e3e6b7e0706abb6ce910c51ae87afd20a391
parenta7a95ce9c4e11e8b3ab887481a7f0e7e8294a87c

coff: implement exporting anon decls


2 files changed, 53 insertions(+), 39 deletions(-)

src/link/Coff.zig+49-37
......@@ -54,7 +54,7 @@ entry_addr: ?u32 = null,
5454lazy_syms: LazySymbolTable = .{},
5555
5656/// Table of tracked Decls.
57decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
57decls: DeclTable = .{},
5858
5959/// List of atoms that are either synthetic or map directly to the Zig source program.
6060atoms: std.ArrayListUnmanaged(Atom) = .{},
......@@ -108,7 +108,8 @@ const HotUpdateState = struct {
108108 loaded_base_address: ?std.os.windows.HMODULE = null,
109109};
110110
111const AnonDeclTable = std.AutoHashMapUnmanaged(InternPool.Index, Atom.Index);
111const DeclTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, DeclMetadata);
112const AnonDeclTable = std.AutoHashMapUnmanaged(InternPool.Index, DeclMetadata);
112113const RelocTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));
113114const BaseRelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));
114115const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index));
......@@ -325,7 +326,14 @@ pub fn deinit(self: *Coff) void {
325326 atoms.deinit(gpa);
326327 }
327328 self.unnamed_const_atoms.deinit(gpa);
328 self.anon_decls.deinit(gpa);
329
330 {
331 var it = self.anon_decls.iterator();
332 while (it.next()) |entry| {
333 entry.value_ptr.exports.deinit(gpa);
334 }
335 self.anon_decls.deinit(gpa);
336 }
329337
330338 for (self.relocs.values()) |*relocs| {
331339 relocs.deinit(gpa);
......@@ -1462,62 +1470,66 @@ pub fn updateExports(
14621470
14631471 const gpa = self.base.allocator;
14641472
1465 const decl_index = switch (exported) {
1466 .decl_index => |i| i,
1467 .value => |val| {
1468 _ = val;
1469 @panic("TODO: implement COFF linker code for exporting a constant value");
1473 const metadata = switch (exported) {
1474 .decl_index => |decl_index| blk: {
1475 _ = try self.getOrCreateAtomForDecl(decl_index);
1476 break :blk self.decls.getPtr(decl_index).?;
1477 },
1478 .value => |value| self.anon_decls.getPtr(value) orelse blk: {
1479 const first_exp = exports[0];
1480 const res = try self.lowerAnonDecl(value, .none, first_exp.getSrcLoc(mod));
1481 switch (res) {
1482 .ok => {},
1483 .fail => |em| {
1484 // TODO maybe it's enough to return an error here and let Module.processExportsInner
1485 // handle the error?
1486 try mod.failed_exports.ensureUnusedCapacity(mod.gpa, 1);
1487 mod.failed_exports.putAssumeCapacityNoClobber(first_exp, em);
1488 return;
1489 },
1490 }
1491 break :blk self.anon_decls.getPtr(value).?;
14701492 },
14711493 };
1472 const decl = mod.declPtr(decl_index);
1473 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
1494 const atom_index = metadata.atom;
14741495 const atom = self.getAtom(atom_index);
1475 const decl_metadata = self.decls.getPtr(decl_index).?;
14761496
14771497 for (exports) |exp| {
14781498 log.debug("adding new export '{}'", .{exp.opts.name.fmt(&mod.intern_pool)});
14791499
14801500 if (mod.intern_pool.stringToSliceUnwrap(exp.opts.section)) |section_name| {
14811501 if (!mem.eql(u8, section_name, ".text")) {
1482 try mod.failed_exports.putNoClobber(
1502 try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
14831503 gpa,
1484 exp,
1485 try Module.ErrorMsg.create(
1486 gpa,
1487 decl.srcLoc(mod),
1488 "Unimplemented: ExportOptions.section",
1489 .{},
1490 ),
1491 );
1504 exp.getSrcLoc(mod),
1505 "Unimplemented: ExportOptions.section",
1506 .{},
1507 ));
14921508 continue;
14931509 }
14941510 }
14951511
14961512 if (exp.opts.linkage == .LinkOnce) {
1497 try mod.failed_exports.putNoClobber(
1513 try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
14981514 gpa,
1499 exp,
1500 try Module.ErrorMsg.create(
1501 gpa,
1502 decl.srcLoc(mod),
1503 "Unimplemented: GlobalLinkage.LinkOnce",
1504 .{},
1505 ),
1506 );
1515 exp.getSrcLoc(mod),
1516 "Unimplemented: GlobalLinkage.LinkOnce",
1517 .{},
1518 ));
15071519 continue;
15081520 }
15091521
1510 const sym_index = decl_metadata.getExport(self, mod.intern_pool.stringToSlice(exp.opts.name)) orelse blk: {
1522 const sym_index = metadata.getExport(self, mod.intern_pool.stringToSlice(exp.opts.name)) orelse blk: {
15111523 const sym_index = try self.allocateSymbol();
1512 try decl_metadata.exports.append(gpa, sym_index);
1524 try metadata.exports.append(gpa, sym_index);
15131525 break :blk sym_index;
15141526 };
15151527 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
15161528 const sym = self.getSymbolPtr(sym_loc);
15171529 try self.setSymbolName(sym, mod.intern_pool.stringToSlice(exp.opts.name));
15181530 sym.value = atom.getSymbol(self).value;
1519 sym.section_number = @as(coff.SectionNumber, @enumFromInt(self.text_section_index.? + 1));
1520 sym.type = .{ .complex_type = .FUNCTION, .base_type = .NULL };
1531 sym.section_number = @as(coff.SectionNumber, @enumFromInt(metadata.section + 1));
1532 sym.type = atom.getSymbol(self).type;
15211533
15221534 switch (exp.opts.linkage) {
15231535 .Strong => {
......@@ -1761,8 +1773,8 @@ pub fn lowerAnonDecl(
17611773 .none => ty.abiAlignment(mod),
17621774 else => explicit_alignment,
17631775 };
1764 if (self.anon_decls.get(decl_val)) |atom_index| {
1765 const existing_addr = self.getAtom(atom_index).getSymbol(self).value;
1776 if (self.anon_decls.get(decl_val)) |metadata| {
1777 const existing_addr = self.getAtom(metadata.atom).getSymbol(self).value;
17661778 if (decl_alignment.check(existing_addr))
17671779 return .ok;
17681780 }
......@@ -1792,14 +1804,14 @@ pub fn lowerAnonDecl(
17921804 .ok => |atom_index| atom_index,
17931805 .fail => |em| return .{ .fail = em },
17941806 };
1795 try self.anon_decls.put(gpa, decl_val, atom_index);
1807 try self.anon_decls.put(gpa, decl_val, .{ .atom = atom_index, .section = self.rdata_section_index.? });
17961808 return .ok;
17971809}
17981810
17991811pub fn getAnonDeclVAddr(self: *Coff, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 {
18001812 assert(self.llvm_object == null);
18011813
1802 const this_atom_index = self.anon_decls.get(decl_val).?;
1814 const this_atom_index = self.anon_decls.get(decl_val).?.atom;
18031815 const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
18041816 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
18051817 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
test/behavior/export_builtin.zig+4-2
......@@ -56,7 +56,8 @@ test "exporting comptime-known value" {
5656 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
5757 if (builtin.zig_backend == .stage2_x86_64 and
5858 (builtin.target.ofmt != .elf and
59 builtin.target.ofmt != .macho)) return error.SkipZigTest;
59 builtin.target.ofmt != .macho and
60 builtin.target.ofmt != .coff)) return error.SkipZigTest;
6061 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
6162 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
6263 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
......@@ -74,7 +75,8 @@ test "exporting comptime var" {
7475 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
7576 if (builtin.zig_backend == .stage2_x86_64 and
7677 (builtin.target.ofmt != .elf and
77 builtin.target.ofmt != .macho)) return error.SkipZigTest;
78 builtin.target.ofmt != .macho and
79 builtin.target.ofmt != .coff)) return error.SkipZigTest;
7880 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
7981 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
8082 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;