authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-05 01:55:34-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-23 00:22:41-04:00
log84142ad56bb22105f23a357201973a91a90e21b9
tree2578de012171be445a7609912aca581fc902db35
parent0bfa6e41e9654d35d79ce4b617180a86eb5a2c3d

Coff: support build-obj

- Fix MappedFile.nodeResize causing USER_MAPPED_FILE when resizing the root node when the requested size is smaller than the previous resize + growth factor.

1 files changed, 15 insertions(+), 11 deletions(-)

src/link/Coff.zig+15-11
...@@ -149,7 +149,6 @@ pub const Node = union(enum) {...@@ -149,7 +149,6 @@ pub const Node = union(enum) {
149 header,149 header,
150 /// Images and archives only.150 /// Images and archives only.
151 signature,151 signature,
152
153 /// Archives only.152 /// Archives only.
154 archive_member_header: Member.Index,153 archive_member_header: Member.Index,
155 archive_member: Member.Index,154 archive_member: Member.Index,
...@@ -159,6 +158,7 @@ pub const Node = union(enum) {...@@ -159,6 +158,7 @@ pub const Node = union(enum) {
159 optional_header,158 optional_header,
160 /// Image only159 /// Image only
161 data_directories,160 data_directories,
161
162 section_table,162 section_table,
163 // Archives and objects only163 // Archives and objects only
164 symbol_table,164 symbol_table,
...@@ -1201,6 +1201,15 @@ fn isArchive(coff: *const Coff) bool {...@@ -1201,6 +1201,15 @@ fn isArchive(coff: *const Coff) bool {
1201 };1201 };
1202}1202}
12031203
1204fn isObj(coff: *const Coff) bool {
1205 return coff.base.comp.config.output_mode == .Obj;
1206}
1207
1208fn zcuSectionParent(coff: *Coff) MappedFile.Node.Index {
1209 assert(coff.base.comp.zcu != null);
1210 return if (coff.isArchive()) Node.known.zcu_member else Node.known.file;
1211}
1212
1204fn initHeaders(1213fn initHeaders(
1205 coff: *Coff,1214 coff: *Coff,
1206 machine: std.coff.IMAGE.FILE.MACHINE,1215 machine: std.coff.IMAGE.FILE.MACHINE,
...@@ -1261,7 +1270,7 @@ fn initHeaders(...@@ -1261,7 +1270,7 @@ fn initHeaders(
1261 const archive_signature = "!<arch>\n";1270 const archive_signature = "!<arch>\n";
12621271
1263 const signature_ni = Node.known.signature;1272 const signature_ni = Node.known.signature;
1264 assert(signature_ni == try coff.mf.addLastChildNode(gpa, if (is_image) header_ni else Node.known.file, .{1273 assert(signature_ni == try coff.mf.addLastChildNode(gpa, if (is_image or !is_archive) header_ni else Node.known.file, .{
1265 .size = if (is_image)1274 .size = if (is_image)
1266 msdos_stub.len + pe_signature.len1275 msdos_stub.len + pe_signature.len
1267 else if (is_archive)1276 else if (is_archive)
...@@ -2448,12 +2457,7 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags...@@ -2448,12 +2457,7 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags
2448 @sizeOf(std.coff.SectionHeader) * section_table_len,2457 @sizeOf(std.coff.SectionHeader) * section_table_len,
2449 );2458 );
24502459
2451 const parent_ni = if (coff.isArchive())2460 const ni = try coff.mf.addLastChildNode(gpa, coff.zcuSectionParent(), .{
2452 Node.known.zcu_member
2453 else
2454 Node.known.file;
2455
2456 const ni = try coff.mf.addLastChildNode(gpa, parent_ni, .{
2457 .alignment = coff.mf.flags.block_size,2461 .alignment = coff.mf.flags.block_size,
2458 .moved = true,2462 .moved = true,
2459 .bubbles_moved = false,2463 .bubbles_moved = false,
...@@ -2652,7 +2656,7 @@ pub fn addReloc(...@@ -2652,7 +2656,7 @@ pub fn addReloc(
2652 const new_size = new_num_relocations * std.coff.Relocation.sizeOf();2656 const new_size = new_num_relocations * std.coff.Relocation.sizeOf();
2653 if (section.relocation_table_ni == .none) {2657 if (section.relocation_table_ni == .none) {
2654 try coff.nodes.ensureUnusedCapacity(gpa, 1);2658 try coff.nodes.ensureUnusedCapacity(gpa, 1);
2655 section.relocation_table_ni = try coff.mf.addLastChildNode(gpa, Node.known.zcu_member, .{2659 section.relocation_table_ni = try coff.mf.addLastChildNode(gpa, coff.zcuSectionParent(), .{
2656 .size = new_size,2660 .size = new_size,
2657 .alignment = .@"2",2661 .alignment = .@"2",
2658 .moved = true,2662 .moved = true,
...@@ -3308,7 +3312,7 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void {...@@ -3308,7 +3312,7 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void {
3308 const si = gmi.symbol(coff);3312 const si = gmi.symbol(coff);
3309 try coff.pendingSymbolTableEntry(si);3313 try coff.pendingSymbolTableEntry(si);
33103314
3311 if (si.get(coff).ni != .none)3315 if (coff.isArchive() and si.get(coff).ni != .none)
3312 try coff.ensureMemberSymbol(3316 try coff.ensureMemberSymbol(
3313 gn.name,3317 gn.name,
3314 coff.getNode(Node.known.zcu_member).archive_member,3318 coff.getNode(Node.known.zcu_member).archive_member,
...@@ -3542,7 +3546,7 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void {...@@ -3542,7 +3546,7 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void {
3542 .data_directories,3546 .data_directories,
3543 .section_table,3547 .section_table,
3544 .placeholder,3548 .placeholder,
3545 => if (coff.isImage()) unreachable,3549 => assert(!coff.isImage()),
3546 .symbol_table => {3550 .symbol_table => {
3547 coff.targetStore(3551 coff.targetStore(
3548 &coff.headerPtr().pointer_to_symbol_table,3552 &coff.headerPtr().pointer_to_symbol_table,