authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-09-30 22:08:43-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-10-03 17:44:36-07:00
log78e07b8fc83469ba63de8654091145b93fe5a248
tree5256f6a169f86885c865a5040b127f45a11557a8
parent07c3f9ef8e0a5a557dce70322334b0d1b49fe154

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.

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

lib/std/coff.zig+12-4
......@@ -528,13 +528,11 @@ pub const SectionHeader = extern struct {
528528
529529 /// Applicable only to section headers in COFF objects.
530530 pub fn getAlignment(self: SectionHeader) ?u16 {
531 if (self.flags.ALIGN == 0) return null;
532 return std.math.powi(u16, 2, self.flags.ALIGN - 1) catch unreachable;
531 return self.flags.ALIGN.toByteUnits();
533532 }
534533
535534 pub fn setAlignment(self: *SectionHeader, new_alignment: u16) void {
536 assert(new_alignment > 0 and new_alignment <= 8192);
537 self.flags.ALIGN = @intCast(std.math.log2(new_alignment));
535 self.flags.ALIGN = .fromByteUnits(new_alignment);
538536 }
539537
540538 pub fn isCode(self: SectionHeader) bool {
......@@ -651,6 +649,16 @@ pub const SectionHeader = extern struct {
651649 @"4096BYTES" = 13,
652650 @"8192BYTES" = 14,
653651 _,
652
653 pub fn toByteUnits(a: Align) ?u16 {
654 if (a == .NONE) return null;
655 return @as(u16, 1) << (@intFromEnum(a) - 1);
656 }
657
658 pub fn fromByteUnits(n: u16) Align {
659 std.debug.assert(std.math.isPowerOfTwo(n));
660 return @enumFromInt(@ctz(n) + 1);
661 }
654662 };
655663 };
656664};