Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

SectionHeader

coff.SectionHeader
pub const SectionHeader = extern struct

File

lib/std/coff.zig:507

Code

pub const SectionHeader = extern struct {
    name: [8]u8,
    virtual_size: u32,
    virtual_address: u32,
    size_of_raw_data: u32,
    pointer_to_raw_data: u32,
    pointer_to_relocations: u32,
    pointer_to_linenumbers: u32,
    number_of_relocations: u16,
    number_of_linenumbers: u16,
    flags: SectionHeader.Flags,

    pub fn getName(self: *align(1) const SectionHeader) ?[]const u8 {
        if (self.name[0] == '/') return null;
        const len = std.mem.findScalar(u8, &self.name, @as(u8, 0)) orelse self.name.len;
        return self.name[0..len];
    }

    pub fn getNameOffset(self: SectionHeader) ?u32 {
        if (self.name[0] != '/') return null;
        const len = std.mem.findScalar(u8, &self.name, @as(u8, 0)) orelse self.name.len;
        const offset = std.fmt.parseInt(u32, self.name[1..len], 10) catch unreachable;
        return offset;
    }

    /// Applicable only to section headers in COFF objects.
    pub fn getAlignment(self: SectionHeader) ?u16 {
        return self.flags.ALIGN.toByteUnits();
    }

    pub fn setAlignment(self: *SectionHeader, new_alignment: u16) void {
        self.flags.ALIGN = .fromByteUnits(new_alignment);
    }

    pub fn isCode(self: SectionHeader) bool {
        return self.flags.CNT_CODE;
    }

    pub fn isComdat(self: SectionHeader) bool {
        return self.flags.LNK_COMDAT;
    }

    pub const Flags = packed struct(u32) {
        SCALE_INDEX: bool = false,

        unused1: u2 = 0,

        /// The section should not be padded to the next boundary.
        /// This flag is obsolete and is replaced by `.ALIGN = .@"1BYTES"`.
        /// This is valid only for object files.
        TYPE_NO_PAD: bool = false,

        unused4: u1 = 0,

        /// The section contains executable code.
        CNT_CODE: bool = false,

        /// The section contains initialized data.
        CNT_INITIALIZED_DATA: bool = false,

        /// The section contains uninitialized data.
        CNT_UNINITIALIZED_DATA: bool = false,

        /// Reserved for future use.
        LNK_OTHER: bool = false,

        /// The section contains comments or other information.
        /// The .drectve section has this type.
        /// This is valid for object files only.
        LNK_INFO: bool = false,

        unused10: u1 = 0,

        /// The section will not become part of the image.
        /// This is valid only for object files.
        LNK_REMOVE: bool = false,

        /// The section contains COMDAT data.
        /// For more information, see COMDAT Sections (Object Only).
        /// This is valid only for object files.
        LNK_COMDAT: bool = false,

        unused13: u2 = 0,

        union14: packed union {
            mask: u1,
            /// The section contains data referenced through the global pointer (GP).
            GPREL: bool,
            MEM_FARDATA: bool,
        } = .{ .mask = 0 },

        unused15: u1 = 0,

        union16: packed union {
            mask: u1,
            MEM_PURGEABLE: bool,
            MEM_16BIT: bool,
        } = .{ .mask = 0 },

        /// Reserved for future use.
        MEM_LOCKED: bool = false,

        /// Reserved for future use.
        MEM_PRELOAD: bool = false,

        ALIGN: SectionHeader.Flags.Align = .NONE,

        /// The section contains extended relocations.
        LNK_NRELOC_OVFL: bool = false,

        /// The section can be discarded as needed.
        MEM_DISCARDABLE: bool = false,

        /// The section cannot be cached.
        MEM_NOT_CACHED: bool = false,

        /// The section is not pageable.
        MEM_NOT_PAGED: bool = false,

        /// The section can be shared in memory.
        MEM_SHARED: bool = false,

        /// The section can be executed as code.
        MEM_EXECUTE: bool = false,

        /// The section can be read.
        MEM_READ: bool = false,

        /// The section can be written to.
        MEM_WRITE: bool = false,

        pub const Align = enum(u4) {
            NONE = 0,
            @"1BYTES" = 1,
            @"2BYTES" = 2,
            @"4BYTES" = 3,
            @"8BYTES" = 4,
            @"16BYTES" = 5,
            @"32BYTES" = 6,
            @"64BYTES" = 7,
            @"128BYTES" = 8,
            @"256BYTES" = 9,
            @"512BYTES" = 10,
            @"1024BYTES" = 11,
            @"2048BYTES" = 12,
            @"4096BYTES" = 13,
            @"8192BYTES" = 14,
            _,

            pub fn toByteUnits(a: Align) ?u16 {
                if (a == .NONE) return null;
                return @as(u16, 1) << (@backingInt(a) - 1);
            }

            pub fn fromByteUnits(n: u16) Align {
                std.debug.assert(std.math.isPowerOfTwo(n));
                return @fromBackingInt(@intCast(@ctz(n) + 1));
            }

            pub fn alignment(a: Align) ?std.mem.Alignment {
                return .fromByteUnitsOptional(a.toByteUnits() orelse null);
            }
        };
    };
}