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.
This commit is contained in:
Ryan Liptak 2025-09-30 22:08:43 -07:00
parent 07c3f9ef8e
commit 78e07b8fc8

View File

@ -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);
}
};
};
};