authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-05 01:55:36-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2026-06-23 00:26:56-04:00
log6057145533c877ff5682e0dd08fc11b68ee8850c
tree28bb17c90d9de203f5101de6555ed1e6f201ee20
parent5b7881cb2a1c83b8db7c6bae2ddaba8925a65a80

Coff: put import thunks into their own section

- Realign object sections if needed

1 files changed, 33 insertions(+), 12 deletions(-)

src/link/Coff.zig+33-12
...@@ -805,6 +805,7 @@ pub const String = enum(u32) {...@@ -805,6 +805,7 @@ pub const String = enum(u32) {
805 @".bss" = 75,805 @".bss" = 75,
806 @".fptable" = 80,806 @".fptable" = 80,
807 @".tls" = 89,807 @".tls" = 89,
808 @".thunks" = 94,
808 _,809 _,
809810
810 pub const Optional = enum(u32) {811 pub const Optional = enum(u32) {
...@@ -821,6 +822,7 @@ pub const String = enum(u32) {...@@ -821,6 +822,7 @@ pub const String = enum(u32) {
821 @".bss" = @intFromEnum(String.@".bss"),822 @".bss" = @intFromEnum(String.@".bss"),
822 @".fptable" = @intFromEnum(String.@".fptable"),823 @".fptable" = @intFromEnum(String.@".fptable"),
823 @".tls" = @intFromEnum(String.@".tls"),824 @".tls" = @intFromEnum(String.@".tls"),
825 @".thunks" = @intFromEnum(String.@".thunks"),
824 none = std.math.maxInt(u32),826 none = std.math.maxInt(u32),
825 _,827 _,
826828
...@@ -3401,6 +3403,15 @@ fn objectSectionMapIndex(...@@ -3401,6 +3403,15 @@ fn objectSectionMapIndex(
3401 };3403 };
3402 }3404 }
34033405
3406 const old_alignment = sym.ni.alignment(&coff.mf);
3407 if (alignment.compare(.gt, old_alignment)) {
3408 log.debug("realignObject({s}) {d}->{d}", .{ name.toSlice(coff), old_alignment, alignment });
3409 sym.ni.realign(&coff.mf, gpa, alignment, true) catch |err| switch (err) {
3410 error.Unimplemented => unreachable,
3411 else => |e| return e,
3412 };
3413 }
3414
3404 try coff.verifyParentSectionAttributes(3415 try coff.verifyParentSectionAttributes(
3405 .object,3416 .object,
3406 sym.section_number.name(coff),3417 sym.section_number.name(coff),
...@@ -6182,9 +6193,8 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {...@@ -6182,9 +6193,8 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
6182 const lib_name = import.lib_name.toSlice(coff);6193 const lib_name = import.lib_name.toSlice(coff);
61836194
6184 try coff.nodes.ensureUnusedCapacity(gpa, 4);6195 try coff.nodes.ensureUnusedCapacity(gpa, 4);
6185 try coff.symbols.ensureUnusedCapacity(gpa, 1);6196 try coff.symbols.ensureUnusedCapacity(gpa, 2);
61866197
6187 const sym = si.get(coff);
6188 const target_endian = coff.targetEndian();6198 const target_endian = coff.targetEndian();
6189 const addr_info = coff.targetAddrInfo();6199 const addr_info = coff.targetAddrInfo();
6190 const gop = try coff.import_table.entries.getOrPutAdapted(6200 const gop = try coff.import_table.entries.getOrPutAdapted(
...@@ -6328,6 +6338,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {...@@ -6328,6 +6338,7 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
6328 }6338 }
6329 }6339 }
63306340
6341 const sym = si.get(coff);
6331 assert(sym.loc_relocs == .none);6342 assert(sym.loc_relocs == .none);
6332 const iat_offset: u32 = @intCast(addr_info.size * iat_symbol_gop.value_ptr.*);6343 const iat_offset: u32 = @intCast(addr_info.size * iat_symbol_gop.value_ptr.*);
6333 switch (import.kind) {6344 switch (import.kind) {
...@@ -6340,21 +6351,31 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {...@@ -6340,21 +6351,31 @@ fn flushGlobal(coff: *Coff, gmi: Node.GlobalMapIndex) !bool {
6340 (try gop.value_ptr.import_address_table_symbols.addOne(gpa)).* = si;6351 (try gop.value_ptr.import_address_table_symbols.addOne(gpa)).* = si;
6341 },6352 },
6342 .thunk => {6353 .thunk => {
6343 sym.section_number = Symbol.Index.text.get(coff).section_number;
6344 sym.loc_relocs = @enumFromInt(coff.relocs.items.len);6354 sym.loc_relocs = @enumFromInt(coff.relocs.items.len);
6355
6356 const target = &comp.root_mod.resolved_target.result;
6357 const alignment = switch (comp.root_mod.optimize_mode) {
6358 .Debug,
6359 .ReleaseSafe,
6360 .ReleaseFast,
6361 => target_util.defaultFunctionAlignment(target),
6362 .ReleaseSmall => target_util.minFunctionAlignment(target),
6363 }.toStdMem();
6364 const parent_si = (try coff.pseudoSectionMapIndex(
6365 .@".thunks",
6366 alignment,
6367 .{ .execute = true, .read = true },
6368 )).symbol(coff);
6369
6370 const parent_sym = parent_si.get(coff);
6371 sym.section_number = parent_sym.section_number;
6372
6345 switch (coff.targetLoad(&coff.headerPtr().machine)) {6373 switch (coff.targetLoad(&coff.headerPtr().machine)) {
6346 else => |tag| @panic(@tagName(tag)),6374 else => |tag| @panic(@tagName(tag)),
6347 .AMD64 => {6375 .AMD64 => {
6348 const init = [_]u8{ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 };6376 const init = [_]u8{ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 };
6349 const target = &comp.root_mod.resolved_target.result;6377 const ni = try coff.mf.addLastChildNode(gpa, parent_sym.ni, .{
6350 const ni = try coff.mf.addLastChildNode(gpa, Symbol.Index.text.node(coff), .{6378 .alignment = alignment,
6351 .alignment = switch (comp.root_mod.optimize_mode) {
6352 .Debug,
6353 .ReleaseSafe,
6354 .ReleaseFast,
6355 => target_util.defaultFunctionAlignment(target),
6356 .ReleaseSmall => target_util.minFunctionAlignment(target),
6357 }.toStdMem(),
6358 .size = init.len,6379 .size = init.len,
6359 });6380 });
6360 @memcpy(ni.slice(&coff.mf)[0..init.len], &init);6381 @memcpy(ni.slice(&coff.mf)[0..init.len], &init);