authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-23 00:19:44-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-23 00:27:18-04:00
log323a3edafe0ac3e3b101db87188fe9ace1d9c388
tree7956f4126433d0fb81f2de339c6b9ae33f14d4e6
parent411e5099e5fb8a8e1b39e0bcd0098e7a2c162aec

Coff: Rework global keys

The previous way of keying globals on (name, lib_name) did not allow resolving undef externals from imports to globals that were first seen with a lib_name

1 files changed, 119 insertions(+), 93 deletions(-)

src/link/Coff.zig+119-93
......@@ -64,7 +64,7 @@ object_section_table: std.array_hash_map.Auto(String, Symbol.Index),
6464section_merges: std.array_hash_map.Auto(String, String),
6565section_merge_pending_index: u32,
6666symbols: std.ArrayList(Symbol),
67globals: std.array_hash_map.Auto(GlobalName, Symbol.Index),
67globals: std.array_hash_map.Auto(String, Global),
6868global_pending_index: u32,
6969navs: std.array_hash_map.Auto(InternPool.Nav.Index, Symbol.Index),
7070uavs: std.array_hash_map.Auto(InternPool.Index, Symbol.Index),
......@@ -268,12 +268,16 @@ pub const Node = union(enum) {
268268 };
269269 }
270270
271 pub fn globalName(gmi: GlobalMapIndex, coff: *const Coff) GlobalName {
271 pub fn name(gmi: GlobalMapIndex, coff: *const Coff) String {
272272 return coff.globals.keys()[gmi.unwrap().?];
273273 }
274274
275275 pub fn symbol(gmi: GlobalMapIndex, coff: *const Coff) Symbol.Index {
276 return coff.globals.values()[gmi.unwrap().?];
276 return coff.globals.values()[gmi.unwrap().?].si;
277 }
278
279 pub fn libName(gmi: GlobalMapIndex, coff: *const Coff) String.Optional {
280 return coff.globals.values()[gmi.unwrap().?].lib_name;
277281 }
278282 };
279283
......@@ -335,6 +339,10 @@ pub const Node = union(enum) {
335339
336340 const LocalIndex = enum(u32) {
337341 _,
342
343 pub fn name(isli: LocalIndex, coff: *const Coff) String {
344 return coff.input_symbols.items[@intFromEnum(isli)].name;
345 }
338346 };
339347 };
340348
......@@ -847,12 +855,15 @@ pub const Section = struct {
847855 ) ?*align(2) std.coff.Relocation {
848856 if (sri == .none) return null;
849857 const table_slice = sn.section(coff).relocation_table_ni.slice(&coff.mf);
850 return @ptrCast(@alignCast(&table_slice[sri.unwrap().? * std.coff.Relocation.sizeOf()]));
858 return @ptrCast(@alignCast(&table_slice[@as(u32, sri.unwrap().?) * std.coff.Relocation.sizeOf()]));
851859 }
852860 };
853861};
854862
855pub const GlobalName = struct { name: String, lib_name: String.Optional };
863pub const Global = struct {
864 si: Symbol.Index,
865 lib_name: String.Optional,
866};
856867
857868pub const WeakExternalStrat = enum(u3) {
858869 none,
......@@ -1332,7 +1343,7 @@ pub const Reloc = extern struct {
13321343 // so that this function doesn't return an err
13331344 else => |kind| return coff.base.comp.link_diags.fail(
13341345 "absolute symbol '{s}' targeted by invalid relocation type: {t}",
1335 .{ target_sym.gmi.globalName(coff).name.toSlice(coff), kind },
1346 .{ target_sym.gmi.name(coff).toSlice(coff), kind },
13361347 ),
13371348 .ABSOLUTE => {},
13381349 .ADDR64 => std.mem.writeInt(
......@@ -1351,7 +1362,7 @@ pub const Reloc = extern struct {
13511362 .I386 => switch (reloc.type.I386) {
13521363 else => |kind| return coff.base.comp.link_diags.fail(
13531364 "absolute symbol '{s}' targeted by invalid relocation type: {t}",
1354 .{ target_sym.gmi.globalName(coff).name.toSlice(coff), kind },
1365 .{ target_sym.gmi.name(coff).toSlice(coff), kind },
13551366 ),
13561367 .ABSOLUTE => {},
13571368 .DIR16 => std.mem.writeInt(
......@@ -2767,12 +2778,12 @@ const GlobalOptions = struct {
27672778fn getOrPutGlobalSymbol(
27682779 coff: *Coff,
27692780 opts: GlobalOptions,
2770) !std.array_hash_map.Auto(GlobalName, Symbol.Index).GetOrPutResult {
2781) !std.array_hash_map.Auto(String, Global).GetOrPutResult {
27712782 const comp = coff.base.comp;
27722783 const gpa = comp.gpa;
27732784 try coff.symbols.ensureUnusedCapacity(gpa, 1);
27742785
2775 const lib_name = if (opts.lib_name) |lib_name| lib_name: {
2786 const lib_name: String.Optional = if (opts.lib_name) |lib_name| lib_name: {
27762787 const is_libc = std.zig.target.isLibCLibName(&comp.root_mod.resolved_target.result, lib_name);
27772788 if (is_libc) {
27782789 // This is guaranteed by Sema.handleExternLibName
......@@ -2781,23 +2792,23 @@ fn getOrPutGlobalSymbol(
27812792 // TODO: The user has requested this symbol come from libc, but this logic allows
27822793 // it to come from anywhere. We need to know what inputs are libc inputs,
27832794 // and set a flag to only search them for this symbol.
2784 break :lib_name null;
2795 break :lib_name .none;
27852796 }
27862797
2787 break :lib_name lib_name;
2788 } else null;
2798 break :lib_name (try coff.getOrPutString(lib_name)).toOptional();
2799 } else .none;
27892800
2790 const sym_gop = try coff.globals.getOrPut(gpa, .{
2791 .name = try coff.getOrPutString(opts.name),
2792 .lib_name = try coff.getOrPutOptionalString(lib_name),
2793 });
2801 const sym_gop = try coff.globals.getOrPut(gpa, try coff.getOrPutString(opts.name));
27942802 if (!sym_gop.found_existing) {
27952803 const si = coff.addSymbolAssumeCapacity();
27962804 const sym = si.get(coff);
27972805 sym.gmi = .wrap(@intCast(sym_gop.index));
27982806 sym.flags.type = opts.type;
27992807 sym.flags.dll_storage_class = opts.dll_storage_class;
2800 sym_gop.value_ptr.* = si;
2808 sym_gop.value_ptr.* = .{
2809 .si = si,
2810 .lib_name = lib_name,
2811 };
28012812 coff.synth_prog_node.increaseEstimatedTotalItems(1);
28022813
28032814 log.debug("globalSymbol({s}, {?s}) = {d}", .{ opts.name, opts.lib_name, si });
......@@ -2807,16 +2818,15 @@ fn getOrPutGlobalSymbol(
28072818}
28082819
28092820fn getDefinedGlobal(coff: *Coff, name: []const u8) Symbol.Index {
2810 if (coff.globals.get(.{
2811 .name = coff.getString(name).unwrap() orelse return .null,
2812 .lib_name = .none,
2813 })) |si| if (si.get(coff).ni != .none) return si;
2821 if (coff.globals.get(
2822 coff.getString(name).unwrap() orelse return .null,
2823 )) |global| if (global.si.get(coff).ni != .none) return global.si;
28142824 return .null;
28152825}
28162826
28172827pub fn globalSymbol(coff: *Coff, opts: GlobalOptions) !Symbol.Index {
28182828 const gop = try coff.getOrPutGlobalSymbol(opts);
2819 return gop.value_ptr.*;
2829 return gop.value_ptr.si;
28202830}
28212831
28222832pub fn pendingSymbolTableEntry(coff: *Coff, si: Symbol.Index) !void {
......@@ -3121,9 +3131,9 @@ fn flushSymbolTableEntry(coff: *Coff, index: u32, pt: Zcu.PerThread) !void {
31213131 var buf: [15]u8 = undefined;
31223132 const symbol_name, const num_aux_symbols: u8, const complex_type: std.coff.ComplexType =
31233133 if (sym.gmi != .none) blk: {
3124 const gn = sym.gmi.globalName(coff);
3134 const name = sym.gmi.name(coff);
31253135 break :blk .{
3126 try coff.getOrPutSymbolName(gn.name.toSlice(coff), gn.name),
3136 try coff.getOrPutSymbolName(name.toSlice(coff), name),
31273137 @intFromBool(sym.flags.weak_external_strat != .none),
31283138 if (Symbol.Index.text.get(coff).section_number == sym.section_number)
31293139 .FUNCTION
......@@ -3735,7 +3745,7 @@ fn addRelocAssumeCapacity(
37353745 const header = loc_sn.header(coff);
37363746 const old_num_relocations = coff.targetLoad(&header.number_of_relocations);
37373747 const new_num_relocations = old_num_relocations + 1;
3738 const new_size = new_num_relocations * std.coff.Relocation.sizeOf();
3748 const new_size = @as(u32, new_num_relocations) * std.coff.Relocation.sizeOf();
37393749
37403750 coff.targetStore(&header.number_of_relocations, new_num_relocations);
37413751 if (coff.symbolTableSectionAuxEntryPtr(loc_sn.symbol(coff).sti(coff))) |aux_ptr|
......@@ -4529,17 +4539,16 @@ fn loadObject(
45294539 .external => {
45304540 const global_gop = try coff.getOrPutGlobalSymbol(.{
45314541 .name = symbol.name.toSlice(coff),
4532 .lib_name = null,
45334542 });
45344543
45354544 // TODO: What if the same symbol is incorrectly defined twice in this obj?
45364545 // Would need to mark this global as pending, or notice it later when .ni != none
4537 if (!global_gop.found_existing or global_gop.value_ptr.get(coff).ni == .none) {
4538 symbol.si = global_gop.value_ptr.*;
4546 if (!global_gop.found_existing or global_gop.value_ptr.si.get(coff).ni == .none) {
4547 symbol.si = global_gop.value_ptr.si;
45394548 break :comdat .include;
45404549 }
45414550
4542 break :existing global_gop.value_ptr.*;
4551 break :existing global_gop.value_ptr.si;
45434552 },
45444553 };
45454554
......@@ -4736,7 +4745,7 @@ fn loadObject(
47364745 },
47374746 .weak_external => |alias_index| {
47384747 const global_gop = try coff.getOrPutGlobalSymbol(.{ .name = symbol.name.toSlice(coff) });
4739 symbol.si = global_gop.value_ptr.*;
4748 symbol.si = global_gop.value_ptr.si;
47404749 if (!global_gop.found_existing or symbol.si.get(coff).ni == .none) {
47414750 const sym = symbol.si.get(coff);
47424751 const alias = pending_symbols.getPtr(alias_index) orelse
......@@ -4775,9 +4784,16 @@ fn loadObject(
47754784 },
47764785 .external => |value| {
47774786 const global_gop = try coff.getOrPutGlobalSymbol(.{ .name = symbol.name.toSlice(coff) });
4778 symbol.si = global_gop.value_ptr.*;
4787 symbol.si = global_gop.value_ptr.si;
47794788 if (global_gop.found_existing)
4780 return coff.failMultipleDefinitions(path, member_name, symbol.name, index, global_gop.value_ptr.*, .none);
4789 return coff.failMultipleDefinitions(
4790 path,
4791 member_name,
4792 symbol.name,
4793 index,
4794 global_gop.value_ptr.si,
4795 .none,
4796 );
47814797 break :sym value;
47824798 },
47834799 else => unreachable,
......@@ -4804,11 +4820,18 @@ fn loadObject(
48044820 .external => {
48054821 assert(index != section.comdat_psi.unwrap());
48064822 const global_gop = try coff.getOrPutGlobalSymbol(.{ .name = symbol.name.toSlice(coff) });
4807 symbol.si = global_gop.value_ptr.*;
4823 symbol.si = global_gop.value_ptr.si;
48084824
48094825 const sym = symbol.si.get(coff);
48104826 if (global_gop.found_existing and sym.ni != .none)
4811 return coff.failMultipleDefinitions(path, member_name, symbol.name, index, global_gop.value_ptr.*, .none);
4827 return coff.failMultipleDefinitions(
4828 path,
4829 member_name,
4830 symbol.name,
4831 index,
4832 global_gop.value_ptr.si,
4833 .none,
4834 );
48124835 },
48134836 .weak_external,
48144837 .weak_external_aux,
......@@ -4881,7 +4904,7 @@ fn loadObject(
48814904 switch (symbol.value) {
48824905 .external => |size| {
48834906 const global_gop = try coff.getOrPutGlobalSymbol(.{ .name = symbol.name.toSlice(coff) });
4884 symbol.si = global_gop.value_ptr.*;
4907 symbol.si = global_gop.value_ptr.si;
48854908 if (!global_gop.found_existing or symbol.si.get(coff).ni == .none) {
48864909 const sym = symbol.si.get(coff);
48874910 sym.setExtra(.{ .size = @max(sym.size(), size) });
......@@ -5743,7 +5766,9 @@ fn reportUndefs(coff: *Coff, tid: Zcu.PerThread.Id) !void {
57435766 num_full_notes + @intFromBool(num_unique_references > max_notes),
57445767 );
57455768 const target_sym = target.get(coff);
5746 try err.addMsg("undefined symbol: {s}", .{target_sym.gmi.globalName(coff).name.toSlice(coff)});
5769 try err.addMsg("undefined symbol: {s}", .{target_sym.gmi.name(coff).toSlice(coff)});
5770
5771 // TODO: If lib_name is set, show the user
57475772
57485773 var prev_loc_si: Symbol.Index = .null;
57495774 for (undef_indices.items[start_i .. i + 1]) |reference_i| {
......@@ -5774,9 +5799,9 @@ fn reportUndefs(coff: *Coff, tid: Zcu.PerThread.Id) !void {
57745799 if (section.comdat_si != .null) {
57755800 const comdat_sym = section.comdat_si.get(coff);
57765801 const comdat_name = if (comdat_sym.gmi != .none)
5777 comdat_sym.gmi.globalName(coff).name.toSlice(coff)
5802 comdat_sym.gmi.name(coff).toSlice(coff)
57785803 else
5779 coff.input_symbols.items[@intFromEnum(comdat_sym.extra.isli)].name.toSlice(coff);
5804 comdat_sym.extra.isli.name(coff).toSlice(coff);
57805805
57815806 err.addNote("referenced by input COMDAT section '{s}={s}' '{f}{f}'", .{
57825807 section_name,
......@@ -5793,14 +5818,14 @@ fn reportUndefs(coff: *Coff, tid: Zcu.PerThread.Id) !void {
57935818 }
57945819 } else {
57955820 err.addNote("referenced by input symbol '{s}' from '{f}{f}'", .{
5796 loc_sym.gmi.globalName(coff).name.toSlice(coff),
5821 loc_sym.gmi.name(coff).toSlice(coff),
57975822 other_ioi.path(coff).fmtEscapeString(),
57985823 fmtMemberNameString(other_ioi.memberName(coff)),
57995824 });
58005825 }
58015826 },
58025827 .import_thunk => |gmi| err.addNote("referenced by import thunk for '{s}'", .{
5803 gmi.globalName(coff).name.toSlice(coff),
5828 gmi.name(coff).toSlice(coff),
58045829 }),
58055830 inline .nav,
58065831 .uav,
......@@ -5959,7 +5984,7 @@ fn resolve(coff: *Coff, tid: Zcu.PerThread.Id) !bool {
59595984 if (coff.exports_complete and coff.global_pending_index < coff.globals.count()) {
59605985 const gmi: Node.GlobalMapIndex = .wrap(coff.global_pending_index);
59615986 const sub_prog_node = coff.synth_prog_node.start(
5962 gmi.globalName(coff).name.toSlice(coff),
5987 gmi.name(coff).toSlice(coff),
59635988 0,
59645989 );
59655990 defer sub_prog_node.end();
......@@ -6138,7 +6163,7 @@ fn idleProgNode(
61386163 coff.getNode(isi.symbol(coff).node(coff).parent(&coff.mf)).object_section.name(coff).toSlice(coff),
61396164 }) catch &name;
61406165 },
6141 .import_thunk => |gmi| gmi.globalName(coff).name.toSlice(coff),
6166 .import_thunk => |gmi| gmi.name(coff).toSlice(coff),
61426167 .nav => |nmi| {
61436168 const ip = &coff.base.comp.zcu.?.intern_pool;
61446169 break :name ip.getNav(nmi.navIndex(coff)).fqn.toSlice(ip);
......@@ -6216,7 +6241,6 @@ fn flushUav(
62166241}
62176242
62186243fn aliasGlobal(coff: *Coff, gmi: Node.GlobalMapIndex, alias_si: Symbol.Index) !void {
6219 const gn = gmi.globalName(coff);
62206244 const si = gmi.symbol(coff);
62216245 const sym = si.get(coff);
62226246 const alias_sym = alias_si.get(coff);
......@@ -6224,11 +6248,11 @@ fn aliasGlobal(coff: *Coff, gmi: Node.GlobalMapIndex, alias_si: Symbol.Index) !v
62246248 assert(sym.loc_relocs == .none);
62256249
62266250 log.debug("aliasGlobal({s}, {?s}) {d}->{d} ({?s})", .{
6227 gn.name.toSlice(coff),
6228 gn.lib_name.toSlice(coff),
6251 gmi.name(coff).toSlice(coff),
6252 gmi.libName(coff).toSlice(coff),
62296253 si,
62306254 alias_si,
6231 if (alias_sym.gmi != .none) alias_sym.gmi.globalName(coff).name.toSlice(coff) else null,
6255 if (alias_sym.gmi != .none) alias_sym.gmi.name(coff).toSlice(coff) else null,
62326256 });
62336257
62346258 var ri = sym.target_relocs;
......@@ -6250,7 +6274,7 @@ fn aliasGlobal(coff: *Coff, gmi: Node.GlobalMapIndex, alias_si: Symbol.Index) !v
62506274 alias_sym.target_relocs = sym.target_relocs;
62516275 sym.target_relocs = .none;
62526276 sym.gmi = alias_sym.gmi;
6253 coff.globals.values()[gmi.unwrap().?] = alias_si;
6277 coff.globals.values()[gmi.unwrap().?].si = alias_si;
62546278 // Only apply the new relocs
62556279 try alias_si.applyTargetRelocs(coff, prev_target_relocs);
62566280}
......@@ -6258,12 +6282,18 @@ fn aliasGlobal(coff: *Coff, gmi: Node.GlobalMapIndex, alias_si: Symbol.Index) !v
62586282fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
62596283 const comp = coff.base.comp;
62606284 const gpa = comp.gpa;
6261 const gn = gmi.globalName(coff);
6285 const name = gmi.name(coff);
62626286 const si = gmi.symbol(coff);
62636287
62646288 log.debug(
62656289 "flushGlobal({s}, {?s}) = n{d} {d}@{d}",
6266 .{ gn.name.toSlice(coff), gn.lib_name.toSlice(coff), si.get(coff).ni, si, si.get(coff).section_number },
6290 .{
6291 name.toSlice(coff),
6292 gmi.libName(coff).toSlice(coff),
6293 si.get(coff).ni,
6294 si,
6295 si.get(coff).section_number,
6296 },
62676297 );
62686298
62696299 if (!coff.isImage()) {
......@@ -6271,7 +6301,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
62716301 if (coff.isArchive() and si.get(coff).ni != .none)
62726302 try coff.ensureMemberSymbol(
62736303 coff.getNode(Node.known.zcu_member).archive_member,
6274 gn.name,
6304 name,
62756305 );
62766306
62776307 return true;
......@@ -6292,18 +6322,18 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
62926322
62936323 const import: Import = import: {
62946324 const sym = si.get(coff);
6295 const global_name = gn.name.toSlice(coff);
6296 const imp_match = std.mem.startsWith(u8, global_name, imp_prefix);
6325 const name_slice = name.toSlice(coff);
6326 const imp_match = std.mem.startsWith(u8, name_slice, imp_prefix);
62976327
62986328 // Globals may have the __imp_ prefix already if they are undef externals from another input.
62996329 assert(sym.flags.dll_storage_class != .dllexport);
63006330 const search_name, const is_imp = if (imp_match or sym.flags.dll_storage_class != .dllimport)
6301 .{ gn.name, imp_match }
6331 .{ name, imp_match }
63026332 else name: {
6303 try coff.ensureUnusedStringCapacity(imp_prefix.len + global_name.len);
6304 const name = try std.fmt.allocPrint(gpa, imp_prefix ++ "{s}", .{global_name});
6305 defer gpa.free(name);
6306 break :name .{ coff.getOrPutStringAssumeCapacity(name), true };
6333 try coff.ensureUnusedStringCapacity(imp_prefix.len + name_slice.len);
6334 const imp_name = try std.fmt.allocPrint(gpa, imp_prefix ++ "{s}", .{name_slice});
6335 defer gpa.free(imp_name);
6336 break :name .{ coff.getOrPutStringAssumeCapacity(imp_name), true };
63076337 };
63086338
63096339 const opt_alt_search_name = coff.alternate_names.get(search_name);
......@@ -6317,7 +6347,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
63176347 .anti_dependency => return comp.link_diags.fail(
63186348 // TODO: Figure out what the purpose of this is
63196349 "TODO support anti_dependency weak external: {s}",
6320 .{gn.name.toSlice(coff)},
6350 .{name.toSlice(coff)},
63216351 ),
63226352 },
63236353 else => true,
......@@ -6336,7 +6366,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
63366366 const member = &coff.input_archive_members.items[@intFromEnum(archive_sym.iami)];
63376367 member: switch (member.content) {
63386368 .object => if (!member.flags.is_loaded) {
6339 if (gn.lib_name.unwrap()) |lib_name|
6369 if (gmi.libName(coff).unwrap()) |lib_name|
63406370 if (!std.ascii.eqlIgnoreCase(
63416371 lib_name.toSlice(coff),
63426372 member.iai.path(coff).stem(),
......@@ -6349,32 +6379,32 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
63496379 return false;
63506380 },
63516381 .import => |import| {
6352 if (gn.lib_name.unwrap()) |lib_name|
6382 if (gmi.libName(coff).unwrap()) |lib_name|
63536383 if (!std.ascii.eqlIgnoreCase(
63546384 import.lib_name.toSlice(coff),
63556385 lib_name.toSlice(coff),
63566386 )) break :member;
63576387
6358 const name: String.Optional = name: switch (import.name_type) {
6388 const imp_name: String.Optional = name: switch (import.name_type) {
63596389 .NAME,
63606390 .NAME_NOPREFIX,
63616391 .NAME_UNDECORATE,
63626392 => |tag| {
63636393 const symbol_name: []const u8 = import.symbol_name.toSlice(coff);
6364 const end_match = std.mem.endsWith(u8, global_name, symbol_name);
6365 const len_delta = global_name.len -% symbol_name.len;
6394 const end_match = std.mem.endsWith(u8, name_slice, symbol_name);
6395 const len_delta = name_slice.len -% symbol_name.len;
63666396 if (!end_match or
63676397 (!imp_match and len_delta != 0) or
63686398 (imp_match and len_delta != imp_prefix.len))
63696399 return comp.link_diags.fail(
63706400 "global '{s}' has mismatched symbol name in import header: '{s}'",
63716401 .{
6372 gn.name.toSlice(coff),
6402 name.toSlice(coff),
63736403 import.symbol_name.toSlice(coff),
63746404 },
63756405 );
63766406
6377 const name = if (tag == .NAME) import.symbol_name else undecorated: {
6407 const imp_name = if (tag == .NAME) import.symbol_name else undecorated: {
63786408 var imp_name = std.mem.trimStart(u8, symbol_name, "?@_");
63796409 if (tag == .NAME_UNDECORATE)
63806410 imp_name = std.mem.sliceTo(imp_name, '@');
......@@ -6383,7 +6413,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
63836413 break :undecorated coff.getOrPutStringAssumeCapacity(imp_name);
63846414 };
63856415
6386 break :name name.toOptional();
6416 break :name imp_name.toOptional();
63876417 },
63886418 .ORDINAL => break :name .none,
63896419 else => |t| return comp.link_diags.fail("TODO handle name_type {t}", .{t}),
......@@ -6391,7 +6421,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
63916421
63926422 break :import .{
63936423 .lib_name = import.lib_name,
6394 .name = name,
6424 .name = imp_name,
63956425 .ordinal_hint = import.import_ordinal_hint,
63966426 .kind = if (import.type == .CODE and !is_imp) .thunk else .iat_ptr,
63976427 };
......@@ -6414,7 +6444,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
64146444 const alias_gop = try coff.getOrPutGlobalSymbol(.{
64156445 .name = sym.value.weak_alias_name.toSlice(coff),
64166446 });
6417 try coff.aliasGlobal(gmi, alias_gop.value_ptr.*);
6447 try coff.aliasGlobal(gmi, alias_gop.value_ptr.si);
64186448 return true;
64196449 },
64206450 else => {},
......@@ -6422,8 +6452,8 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
64226452
64236453 // If there was an object that had the alternate name, we've attempted to load it
64246454 if (opt_alt_search_name) |alt_search_name| {
6425 if (coff.globals.get(.{ .name = alt_search_name, .lib_name = .none })) |alias_si| {
6426 try coff.aliasGlobal(gmi, alias_si);
6455 if (coff.globals.get(alt_search_name)) |alias_global| {
6456 try coff.aliasGlobal(gmi, alias_global.si);
64276457 return true;
64286458 }
64296459 }
......@@ -6432,9 +6462,9 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
64326462 // This is necessary for certain ntdll symbols, such as LdrRegisterDllNotification,
64336463 // which are not in the implib.
64346464 if (sym.flags.type != .unknown) {
6435 if (gn.lib_name.unwrap()) |lib_name| break :import .{
6465 if (gmi.libName(coff).unwrap()) |lib_name| break :import .{
64366466 .lib_name = lib_name,
6437 .name = gn.name.toOptional(),
6467 .name = name.toOptional(),
64386468 .ordinal_hint = 0,
64396469 .kind = if (sym.flags.type == .code) .thunk else .iat_ptr,
64406470 };
......@@ -6525,7 +6555,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
65256555
65266556 log.debug(
65276557 "flushGlobalImport({s}, {?s}, {d}, {s})",
6528 .{ gn.name.toSlice(coff), import.name.toSlice(coff), import.ordinal_hint, lib_name },
6558 .{ name.toSlice(coff), import.name.toSlice(coff), import.ordinal_hint, lib_name },
65296559 );
65306560
65316561 const iat_symbol_gop = try coff.import_table.iat_symbol_indices.getOrPut(gpa, .{
......@@ -6544,11 +6574,11 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
65446574 const import_address_table_ni = gop.value_ptr.import_address_table_si.node(coff);
65456575 try import_address_table_ni.resize(&coff.mf, gpa, new_symbol_table_size);
65466576
6547 const opt_name = import.name.toSlice(coff);
6548 const opt_import_hint_name_index = if (opt_name) |name| blk: {
6577 const opt_imp_name = import.name.toSlice(coff);
6578 const opt_import_hint_name_index = if (opt_imp_name) |imp_name| blk: {
65496579 const import_hint_name_index = gop.value_ptr.hint_name_len;
65506580 gop.value_ptr.hint_name_len = @intCast(
6551 import_hint_name_align.forward(import_hint_name_index + 2 + name.len + 1),
6581 import_hint_name_align.forward(import_hint_name_index + 2 + imp_name.len + 1),
65526582 );
65536583 try gop.value_ptr.import_hint_name_table_ni.resize(&coff.mf, gpa, gop.value_ptr.hint_name_len);
65546584 break :blk import_hint_name_index;
......@@ -6558,8 +6588,8 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
65586588 const import_hint_name_slice = gop.value_ptr.import_hint_name_table_ni.slice(&coff.mf);
65596589 const ordinal_hint: *u16 = @ptrCast(@alignCast(import_hint_name_slice[import_hint_name_index..][0..2]));
65606590 ordinal_hint.* = std.mem.nativeTo(u16, import.ordinal_hint, target_endian);
6561 @memcpy(import_hint_name_slice[import_hint_name_index + 2 ..][0..opt_name.?.len], opt_name.?);
6562 @memset(import_hint_name_slice[import_hint_name_index + 2 + opt_name.?.len ..], 0);
6591 @memcpy(import_hint_name_slice[import_hint_name_index + 2 ..][0..opt_imp_name.?.len], opt_imp_name.?);
6592 @memset(import_hint_name_slice[import_hint_name_index + 2 + opt_imp_name.?.len ..], 0);
65636593 break :blk coff.computeNodeRva(gop.value_ptr.import_hint_name_table_ni) + import_hint_name_index;
65646594 } else 0;
65656595
......@@ -6687,7 +6717,7 @@ fn flushSpecialSymbol(coff: *Coff, pending: SpecialSymbol) !SpecialSymbol {
66876717 if (entry_si != .null) {
66886718 log.debug(
66896719 "entry({s}, {d})",
6690 .{ entry_si.get(coff).gmi.globalName(coff).name.toSlice(coff), entry_si },
6720 .{ entry_si.get(coff).gmi.name(coff).toSlice(coff), entry_si },
66916721 );
66926722
66936723 try coff.symbols.ensureUnusedCapacity(gpa, 1);
......@@ -7379,10 +7409,7 @@ fn updateExportsInner(
73797409 const name = @"export".opts.name.toSlice(ip);
73807410
73817411 // TODO: add an errMsg if this conflicts with an existing symbol
7382 const export_si = try coff.globalSymbol(.{
7383 .name = name,
7384 .lib_name = null,
7385 });
7412 const export_si = try coff.globalSymbol(.{ .name = name });
73867413 const export_sym = export_si.get(coff);
73877414 export_sym.ni = exported_ni;
73887415 export_sym.rva = exported_sym.rva;
......@@ -7605,6 +7632,8 @@ fn printSymbol(
76057632 } else {
76067633 try w.writeAll("| ");
76077634 try coff.printNodeName(w, tid, node);
7635 if (sym.flags.extra_tag == .isli)
7636 try w.print(" | {s}", .{sym.extra.isli.name(coff).toSlice(coff)});
76087637 try w.writeByte('\n');
76097638 }
76107639}
......@@ -7617,9 +7646,8 @@ fn fmtGlobalName(coff: *Coff, gmi: Node.GlobalMapIndex) std.fmt.Alt(FmtGlobalNam
76177646
76187647fn globalNameEscape(data: FmtGlobalName, w: *std.Io.Writer) std.Io.Writer.Error!void {
76197648 if (data.gmi == .none) return;
7620 const gn = data.gmi.globalName(data.coff);
7621 try w.writeAll(gn.name.toSlice(data.coff));
7622 if (gn.lib_name.unwrap()) |lib_name|
7649 try w.writeAll(data.gmi.name(data.coff).toSlice(data.coff));
7650 if (data.gmi.libName(data.coff).unwrap()) |lib_name|
76237651 try w.print("({s})", .{lib_name.toSlice(data.coff)});
76247652}
76257653
......@@ -7645,7 +7673,7 @@ fn printNodeName(
76457673 if (is.comdat_si != .null) {
76467674 const comdat_sym = is.comdat_si.get(coff);
76477675 const comdat_name = if (comdat_sym.gmi != .none)
7648 comdat_sym.gmi.globalName(coff).name.toSlice(coff)
7676 comdat_sym.gmi.name(coff).toSlice(coff)
76497677 else
76507678 coff.input_symbols.items[@intFromEnum(comdat_sym.extra.isli)].name.toSlice(coff);
76517679
......@@ -7664,10 +7692,9 @@ fn printNodeName(
76647692 }),
76657693 .import_thunk,
76667694 => |gmi| {
7667 const gn = gmi.globalName(coff);
76687695 try w.writeByte('(');
7669 if (gn.lib_name.toSlice(coff)) |lib_name| try w.print("{s}.dll, ", .{lib_name});
7670 try w.print("{s})", .{gn.name.toSlice(coff)});
7696 if (gmi.libName(coff).toSlice(coff)) |lib_name| try w.print("{s}.dll, ", .{lib_name});
7697 try w.print("{s})", .{gmi.name(coff).toSlice(coff)});
76717698 },
76727699 .nav => |nmi| {
76737700 const zcu = coff.base.comp.zcu.?;
......@@ -7695,10 +7722,9 @@ fn printNodeName(
76957722 .builtin => |si| {
76967723 const sym = si.get(coff);
76977724 if (sym.gmi != .none) {
7698 const gn = sym.gmi.globalName(coff);
76997725 try w.writeByte('(');
7700 if (gn.lib_name.toSlice(coff)) |lib_name| try w.print("{s}.dll, ", .{lib_name});
7701 try w.print("{s})", .{gn.name.toSlice(coff)});
7726 if (sym.gmi.libName(coff).toSlice(coff)) |lib_name| try w.print("{s}.dll, ", .{lib_name});
7727 try w.print("{s})", .{sym.gmi.name(coff).toSlice(coff)});
77027728 }
77037729 },
77047730 }