pub const SectionHeader = extern struct name: [8]u8virtual_size: u32virtual_address: u32size_of_raw_data: u32pointer_to_raw_data: u32pointer_to_relocations: u32pointer_to_linenumbers: u32number_of_relocations: u16number_of_linenumbers: u16flags: SectionHeader.Flagspub fn getName(self: *align(1) const SectionHeader) ?[]const u8pub fn getNameOffset(self: SectionHeader) ?u32pub fn getAlignment(self: SectionHeader) ?u16Applicable only to section headers in COFF objects.
pub fn setAlignment(self: *SectionHeader, new_alignment: u16) voidpub fn isCode(self: SectionHeader) boolpub fn isComdat(self: SectionHeader) bool507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671pub 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);
}
};
};
}