From 78e07b8fc83469ba63de8654091145b93fe5a248 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Tue, 30 Sep 2025 22:08:43 -0700 Subject: [PATCH] std.coff: Fix SectionHeader.setAlignment (off by 1) Previously, `setAlignment` would set the value to 1 fewer than it should, so if you were intending to set alignment to 8 bytes, it would actually set it to 4 bytes, etc. --- lib/std/coff.zig | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/std/coff.zig b/lib/std/coff.zig index 27f3fbc099..5e6bd9e816 100644 --- a/lib/std/coff.zig +++ b/lib/std/coff.zig @@ -528,13 +528,11 @@ pub const SectionHeader = extern struct { /// Applicable only to section headers in COFF objects. pub fn getAlignment(self: SectionHeader) ?u16 { - if (self.flags.ALIGN == 0) return null; - return std.math.powi(u16, 2, self.flags.ALIGN - 1) catch unreachable; + return self.flags.ALIGN.toByteUnits(); } pub fn setAlignment(self: *SectionHeader, new_alignment: u16) void { - assert(new_alignment > 0 and new_alignment <= 8192); - self.flags.ALIGN = @intCast(std.math.log2(new_alignment)); + self.flags.ALIGN = .fromByteUnits(new_alignment); } pub fn isCode(self: SectionHeader) bool { @@ -651,6 +649,16 @@ pub const SectionHeader = extern struct { @"4096BYTES" = 13, @"8192BYTES" = 14, _, + + pub fn toByteUnits(a: Align) ?u16 { + if (a == .NONE) return null; + return @as(u16, 1) << (@intFromEnum(a) - 1); + } + + pub fn fromByteUnits(n: u16) Align { + std.debug.assert(std.math.isPowerOfTwo(n)); + return @enumFromInt(@ctz(n) + 1); + } }; }; };