mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
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:
parent
07c3f9ef8e
commit
78e07b8fc8
@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user