authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-11 00:21:52-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-23 00:27:17-04:00
logf2a778ca523ebe5b84f0d5463be24ca43262eea3
treef963862dce32a60fbf2bca3c6dfe1fdcdf5441ee
parenta9b0999d5b8922580560fcb0f65f5860df78c8be

Coff: fixes for 32 bit targets


3 files changed, 22 insertions(+), 23 deletions(-)

lib/std/coff.zig+1-1
...@@ -2043,7 +2043,7 @@ pub const ArchiveMemberHeader = extern struct {...@@ -2043,7 +2043,7 @@ pub const ArchiveMemberHeader = extern struct {
20432043
2044 if (opt_longnames) |longnames| {2044 if (opt_longnames) |longnames| {
2045 if (offset >= longnames.len) return error.BadName;2045 if (offset >= longnames.len) return error.BadName;
2046 break :name std.mem.sliceTo(longnames[offset..], 0);2046 break :name std.mem.sliceTo(longnames[@intCast(offset)..], 0);
2047 } else return error.NoLongNames;2047 } else return error.NoLongNames;
2048 } else if (trim[trim.len - 1] == '/')2048 } else if (trim[trim.len - 1] == '/')
2049 trim[0 .. trim.len - 1]2049 trim[0 .. trim.len - 1]
src/link/Coff.zig+20-21
...@@ -27,7 +27,7 @@ nodes: std.MultiArrayList(Node),...@@ -27,7 +27,7 @@ nodes: std.MultiArrayList(Node),
27members: std.ArrayList(Member),27members: std.ArrayList(Member),
28pending_members: std.AutoArrayHashMapUnmanaged(Member.Index, void),28pending_members: std.AutoArrayHashMapUnmanaged(Member.Index, void),
29lib_string_table: std.ArrayList(String),29lib_string_table: std.ArrayList(String),
30lib_string_len: u64,30lib_string_len: u32,
31long_names_table: LongNamesTable,31long_names_table: LongNamesTable,
32import_table: ImportTable,32import_table: ImportTable,
33export_table: ExportTable,33export_table: ExportTable,
...@@ -528,7 +528,7 @@ pub const Member = struct {...@@ -528,7 +528,7 @@ pub const Member = struct {
528528
529 try Node.known.longnames_member.resize(&coff.mf, gpa, new_size);529 try Node.known.longnames_member.resize(&coff.mf, gpa, new_size);
530 const name_table_slice = Node.known.longnames_member.slice(&coff.mf);530 const name_table_slice = Node.known.longnames_member.slice(&coff.mf);
531 const name_slice = name_table_slice[old_size..][0 .. name.len + 1];531 const name_slice = name_table_slice[@intCast(old_size)..][0 .. name.len + 1];
532 @memcpy(name_slice[0..name.len], name);532 @memcpy(name_slice[0..name.len], name);
533 name_slice[name.len] = 0;533 name_slice[name.len] = 0;
534534
...@@ -608,7 +608,7 @@ pub const LongNamesTable = struct {...@@ -608,7 +608,7 @@ pub const LongNamesTable = struct {
608 assert(adapter.coff.isArchive());608 assert(adapter.coff.isArchive());
609 const longnames_slice = Node.known.longnames_member.slice(&adapter.coff.mf);609 const longnames_slice = Node.known.longnames_member.slice(&adapter.coff.mf);
610 const rhs = adapter.coff.long_names_table.entries.values()[rhs_index];610 const rhs = adapter.coff.long_names_table.entries.values()[rhs_index];
611 return std.mem.eql(u8, longnames_slice[rhs.offset..][0..rhs.len], lhs_key);611 return std.mem.eql(u8, longnames_slice[@intCast(rhs.offset)..][0..@intCast(rhs.len)], lhs_key);
612 }612 }
613613
614 pub fn hash(_: Adapter, key: []const u8) u32 {614 pub fn hash(_: Adapter, key: []const u8) u32 {
...@@ -2721,8 +2721,8 @@ fn getOrPutSymbolName(coff: *Coff, name: []const u8, opt_string: ?String) !Symbo...@@ -2721,8 +2721,8 @@ fn getOrPutSymbolName(coff: *Coff, name: []const u8, opt_string: ?String) !Symbo
27212721
2722 try coff.symbol_table.strings_ni.resize(&coff.mf, gpa, string_index + name.len + 1);2722 try coff.symbol_table.strings_ni.resize(&coff.mf, gpa, string_index + name.len + 1);
2723 const slice = coff.symbol_table.strings_ni.slice(&coff.mf);2723 const slice = coff.symbol_table.strings_ni.slice(&coff.mf);
2724 @memcpy(slice[string_index..][0..name.len], name);2724 @memcpy(slice[@intCast(string_index)..][0..name.len], name);
2725 slice[string_index + name.len] = 0;2725 slice[@intCast(string_index + name.len)] = 0;
2726 }2726 }
27272727
2728 break :name .{ .long = string_gop.value_ptr.* };2728 break :name .{ .long = string_gop.value_ptr.* };
...@@ -2941,7 +2941,7 @@ pub fn getVAddr(coff: *Coff, reloc_info: link.File.RelocInfo, target_si: Symbol....@@ -2941,7 +2941,7 @@ pub fn getVAddr(coff: *Coff, reloc_info: link.File.RelocInfo, target_si: Symbol.
2941}2941}
29422942
2943/// Caller guarantees there is capacity for one member and two nodes2943/// Caller guarantees there is capacity for one member and two nodes
2944fn addMemberAssumeCapacity(coff: *Coff, kind: std.coff.ArchiveMemberHeader.Kind, size: usize) !Member.Index {2944fn addMemberAssumeCapacity(coff: *Coff, kind: std.coff.ArchiveMemberHeader.Kind, size: u64) !Member.Index {
2945 const comp = coff.base.comp;2945 const comp = coff.base.comp;
2946 const gpa = comp.gpa;2946 const gpa = comp.gpa;
29472947
...@@ -2987,7 +2987,7 @@ fn addMemberAssumeCapacity(coff: *Coff, kind: std.coff.ArchiveMemberHeader.Kind,...@@ -2987,7 +2987,7 @@ fn addMemberAssumeCapacity(coff: *Coff, kind: std.coff.ArchiveMemberHeader.Kind,
29872987
2988 const old_size = Node.known.second_linker_member.location(&coff.mf).resolve(&coff.mf)[1];2988 const old_size = Node.known.second_linker_member.location(&coff.mf).resolve(&coff.mf)[1];
2989 const old_header_size = new_num_members * @sizeOf(u32);2989 const old_header_size = new_num_members * @sizeOf(u32);
2990 const trailing_size = old_size - old_header_size;2990 const trailing_size: usize = @intCast(old_size - old_header_size);
2991 try Node.known.second_linker_member.resize(&coff.mf, gpa, old_size + @sizeOf(u32));2991 try Node.known.second_linker_member.resize(&coff.mf, gpa, old_size + @sizeOf(u32));
29922992
2993 const slice = Node.known.second_linker_member.slice(&coff.mf);2993 const slice = Node.known.second_linker_member.slice(&coff.mf);
...@@ -3053,12 +3053,12 @@ fn ensureMemberSymbol(coff: *Coff, mi: Member.Index, name: String) !void {...@@ -3053,12 +3053,12 @@ fn ensureMemberSymbol(coff: *Coff, mi: Member.Index, name: String) !void {
3053 // can't guarantee that they will be tightly packed after resizing3053 // can't guarantee that they will be tightly packed after resizing
30543054
3055 const name_slice = name.toSlice(coff);3055 const name_slice = name.toSlice(coff);
3056 const new_string_table_size = coff.lib_string_len + name_slice.len + 1;3056 const new_string_table_size: u32 = @intCast(coff.lib_string_len + name_slice.len + 1);
3057 defer coff.lib_string_len = new_string_table_size;3057 defer coff.lib_string_len = new_string_table_size;
30583058
3059 {3059 {
3060 const old_header_size = @sizeOf(u32) + @intFromEnum(mfli) * @sizeOf(u32);3060 const old_header_size: usize = @intCast(@sizeOf(u32) + @intFromEnum(mfli) * @sizeOf(u32));
3061 const new_header_size = old_header_size + @sizeOf(u32);3061 const new_header_size: usize = @intCast(old_header_size + @sizeOf(u32));
3062 try Node.known.first_linker_member.resize(&coff.mf, gpa, new_header_size + new_string_table_size);3062 try Node.known.first_linker_member.resize(&coff.mf, gpa, new_header_size + new_string_table_size);
30633063
3064 const slice = Node.known.first_linker_member.slice(&coff.mf);3064 const slice = Node.known.first_linker_member.slice(&coff.mf);
...@@ -3231,7 +3231,7 @@ fn flushSymbolTableEntry(coff: *Coff, index: u32, pt: Zcu.PerThread) !void {...@@ -3231,7 +3231,7 @@ fn flushSymbolTableEntry(coff: *Coff, index: u32, pt: Zcu.PerThread) !void {
3231 .unused = @splat(0),3231 .unused = @splat(0),
3232 };3232 };
3233 if (coff.targetEndian() != native_endian)3233 if (coff.targetEndian() != native_endian)
3234 std.mem.byteSwapAllFields(std.coff.SectionDefinition, .@"2", aux_ptr);3234 std.mem.byteSwapAllFieldsAligned(std.coff.WeakExternalDefinition, .@"2", aux_ptr);
32353235
3236 break :aux_init;3236 break :aux_init;
3237 } else switch (coff.getNode(sym.ni)) {3237 } else switch (coff.getNode(sym.ni)) {
...@@ -3249,7 +3249,7 @@ fn flushSymbolTableEntry(coff: *Coff, index: u32, pt: Zcu.PerThread) !void {...@@ -3249,7 +3249,7 @@ fn flushSymbolTableEntry(coff: *Coff, index: u32, pt: Zcu.PerThread) !void {
3249 .unused = @splat(0),3249 .unused = @splat(0),
3250 };3250 };
3251 if (coff.targetEndian() != native_endian)3251 if (coff.targetEndian() != native_endian)
3252 std.mem.byteSwapAllFields(std.coff.SectionDefinition, .@"2", aux_ptr);3252 std.mem.byteSwapAllFieldsAligned(std.coff.SectionDefinition, .@"2", aux_ptr);
32533253
3254 break :aux_init;3254 break :aux_init;
3255 },3255 },
...@@ -4269,7 +4269,7 @@ fn loadObject(...@@ -4269,7 +4269,7 @@ fn loadObject(
4269 var weak_external: std.coff.WeakExternalDefinition = undefined;4269 var weak_external: std.coff.WeakExternalDefinition = undefined;
4270 @memcpy(std.mem.asBytes(&weak_external)[0..symbol_size], aux_symbols[0..symbol_size]);4270 @memcpy(std.mem.asBytes(&weak_external)[0..symbol_size], aux_symbols[0..symbol_size]);
4271 if (target_endian != native_endian)4271 if (target_endian != native_endian)
4272 std.mem.byteSwapAllFields(std.coff.SectionDefinition, &weak_external);4272 std.mem.byteSwapAllFields(std.coff.WeakExternalDefinition, &weak_external);
42734273
4274 if (weak_external.tag_index >= header.number_of_symbols)4274 if (weak_external.tag_index >= header.number_of_symbols)
4275 return diags.failParse(4275 return diags.failParse(
...@@ -5131,7 +5131,7 @@ fn loadArchive(coff: *Coff, path: std.Build.Cache.Path, fr: *Io.File.Reader) Loa...@@ -5131,7 +5131,7 @@ fn loadArchive(coff: *Coff, path: std.Build.Cache.Path, fr: *Io.File.Reader) Loa
5131 symbol_member_indices.addOneAssumeCapacity().* = (try r.takeInt(u16, target_endian)) - 1;5131 symbol_member_indices.addOneAssumeCapacity().* = (try r.takeInt(u16, target_endian)) - 1;
51325132
5133 pos = fr.logicalPos();5133 pos = fr.logicalPos();
5134 try coff.ensureManyUnusedStringCapacity(num_symbols, member_end - pos);5134 try coff.ensureManyUnusedStringCapacity(num_symbols, @intCast(member_end - pos));
5135 try coff.input_archive_members.ensureUnusedCapacity(gpa, num_members);5135 try coff.input_archive_members.ensureUnusedCapacity(gpa, num_members);
5136 try coff.input_archive_symbols.ensureUnusedCapacity(gpa, num_symbols);5136 try coff.input_archive_symbols.ensureUnusedCapacity(gpa, num_symbols);
5137 try coff.input_archive_symbol_indices.ensureUnusedCapacity(gpa, num_symbols);5137 try coff.input_archive_symbol_indices.ensureUnusedCapacity(gpa, num_symbols);
...@@ -5202,7 +5202,7 @@ fn loadArchive(coff: *Coff, path: std.Build.Cache.Path, fr: *Io.File.Reader) Loa...@@ -5202,7 +5202,7 @@ fn loadArchive(coff: *Coff, path: std.Build.Cache.Path, fr: *Io.File.Reader) Loa
5202 .longnames => {5202 .longnames => {
5203 // This member is optional5203 // This member is optional
5204 if (std.mem.eql(u8, res.name, "//"))5204 if (std.mem.eql(u8, res.name, "//"))
5205 opt_longnames = try r.readAlloc(gpa, res.size);5205 opt_longnames = try r.readAlloc(gpa, @intCast(res.size));
52065206
5207 opt_expected_kind = null;5207 opt_expected_kind = null;
5208 break;5208 break;
...@@ -5753,9 +5753,8 @@ fn reportUndefs(coff: *Coff, tid: Zcu.PerThread.Id) !void {...@@ -5753,9 +5753,8 @@ fn reportUndefs(coff: *Coff, tid: Zcu.PerThread.Id) !void {
5753 // TODO: Make this a helper for anything that needs to report "referenced by" notes5753 // TODO: Make this a helper for anything that needs to report "referenced by" notes
5754 switch (coff.getNode(loc_sym.ni)) {5754 switch (coff.getNode(loc_sym.ni)) {
5755 .data_directories => {5755 .data_directories => {
5756 const dir_align = std.mem.Alignment.of(std.coff.ImageDataDirectory);
5757 const dir: std.coff.IMAGE.DIRECTORY_ENTRY =5756 const dir: std.coff.IMAGE.DIRECTORY_ENTRY =
5758 @enumFromInt(dir_align.backward(reloc.offset) / @sizeOf(std.coff.IMAGE.DIRECTORY_ENTRY));5757 @enumFromInt(reloc.offset / @sizeOf(std.coff.ImageDataDirectory));
5759 err.addNote("referenced by data directory entry: {t}", .{dir});5758 err.addNote("referenced by data directory entry: {t}", .{dir});
5760 },5759 },
5761 .optional_header => err.addNote("referenced by optional header field", .{}),5760 .optional_header => err.addNote("referenced by optional header field", .{}),
...@@ -6566,7 +6565,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {...@@ -6566,7 +6565,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
6566 const Entry = std.coff.ImportLookupTableEntry(ct_magic);6565 const Entry = std.coff.ImportLookupTableEntry(ct_magic);
6567 const import_lookup_table: []Entry = @ptrCast(@alignCast(import_lookup_slice));6566 const import_lookup_table: []Entry = @ptrCast(@alignCast(import_lookup_slice));
6568 const import_address_table: []Entry = @ptrCast(@alignCast(import_address_slice));6567 const import_address_table: []Entry = @ptrCast(@alignCast(import_address_slice));
6569 const import_hint_name_rvas: [2]Entry = .{6568 var import_hint_name_rvas: [2]Entry = .{
6570 .{6569 .{
6571 .payload = if (import.name == .none)6570 .payload = if (import.name == .none)
6572 .{ .ordinal = .{ .ordinal = import.ordinal_hint } }6571 .{ .ordinal = .{ .ordinal = import.ordinal_hint } }
...@@ -6577,7 +6576,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {...@@ -6577,7 +6576,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
6577 @bitCast(@as(@typeInfo(Entry).@"struct".backing_integer.?, 0)),6576 @bitCast(@as(@typeInfo(Entry).@"struct".backing_integer.?, 0)),
6578 };6577 };
6579 if (native_endian != target_endian)6578 if (native_endian != target_endian)
6580 for (import_hint_name_rvas) |*v| std.mem.byteSwapAllFields(Entry, v);6579 for (&import_hint_name_rvas) |*v| std.mem.byteSwapAllFields(Entry, v);
65816580
6582 import_lookup_table[import_symbol_index..][0..2].* = import_hint_name_rvas;6581 import_lookup_table[import_symbol_index..][0..2].* = import_hint_name_rvas;
6583 import_address_table[import_symbol_index..][0..2].* = import_hint_name_rvas;6582 import_address_table[import_symbol_index..][0..2].* = import_hint_name_rvas;
...@@ -7182,7 +7181,7 @@ fn flushMember(coff: *Coff, mi: Member.Index) !void {...@@ -7182,7 +7181,7 @@ fn flushMember(coff: *Coff, mi: Member.Index) !void {
7182 .strings = coff.lib_string_table.items,7181 .strings = coff.lib_string_table.items,
7183 });7182 });
71847183
7185 var offset: u64 = 0;7184 var offset: usize = 0;
7186 var string_table = coff.secondLinkerMemberStringsSlice();7185 var string_table = coff.secondLinkerMemberStringsSlice();
7187 for (coff.lib_string_table.items) |string| {7186 for (coff.lib_string_table.items) |string| {
7188 const str = string.toSlice(coff);7187 const str = string.toSlice(coff);
...@@ -7419,7 +7418,7 @@ fn updateExportsInner(...@@ -7419,7 +7418,7 @@ fn updateExportsInner(
7419 if (export_count > std.math.maxInt(@FieldType(std.coff.ExportDirectoryTable, "number_of_entries")))7418 if (export_count > std.math.maxInt(@FieldType(std.coff.ExportDirectoryTable, "number_of_entries")))
7420 return coff.base.comp.link_diags.fail("exceeded maximum number of exports", .{});7419 return coff.base.comp.link_diags.fail("exceeded maximum number of exports", .{});
74217420
7422 const name_index: u64 = coff.export_table.name_table_ni.location(&coff.mf).resolve(&coff.mf)[1];7421 const name_index: u32 = @intCast(coff.export_table.name_table_ni.location(&coff.mf).resolve(&coff.mf)[1]);
7423 const new_name_table_size = name_index + name.len + 1;7422 const new_name_table_size = name_index + name.len + 1;
7424 if (new_name_table_size > std.math.maxInt(@FieldType(ExportTable.Entry, "name_index")))7423 if (new_name_table_size > std.math.maxInt(@FieldType(ExportTable.Entry, "name_index")))
7425 return coff.base.comp.link_diags.fail("exports name table limit reached", .{});7424 return coff.base.comp.link_diags.fail("exports name table limit reached", .{});
src/link/MappedFile.zig+1-1
...@@ -1064,7 +1064,7 @@ fn realignNode(...@@ -1064,7 +1064,7 @@ fn realignNode(
1064 };1064 };
10651065
1066 if (try_backward) {1066 if (try_backward) {
1067 const backward_offset = new_alignment.backward(old_offset);1067 const backward_offset = new_alignment.backward(@intCast(old_offset));
1068 const prev_end = if (node.prev == .none) 0 else prev: {1068 const prev_end = if (node.prev == .none) 0 else prev: {
1069 const prev_offset, const prev_size = node.prev.location(mf).resolve(mf);1069 const prev_offset, const prev_size = node.prev.location(mf).resolve(mf);
1070 break :prev prev_offset + prev_size;1070 break :prev prev_offset + prev_size;