pub const Header = struct pub fn iterateProgramHeaders(h: *const Header, file_reader: *Io.File.Reader) ProgramHeaderIteratorpub fn iterateProgramHeadersBuffer(h: *const Header, buf: []const u8) ProgramHeaderBufferIteratorpub fn iterateSectionHeaders(h: *const Header, file_reader: *Io.File.Reader) SectionHeaderIteratorpub fn iterateSectionHeadersBuffer(h: *const Header, buf: []const u8) SectionHeaderBufferIteratorpub fn iterateDynamicSection(h: *const Header, file_reader: *Io.File.Reader, offset: u64, size: u64) DynamicSectionIteratorpub fn iterateDynamicSectionBuffer(h: *const Header, buf: []const u8, offset: u64, size: u64) DynamicSectionBufferIteratorpub fn read(r: *Io.Reader) ReadError!HeaderIf this function fails, seek position of r is unchanged.
pub fn init(hdr: anytype, endian: Endian) Header661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799pub const Header = struct {
is_64: bool,
endian: Endian,
os_abi: OSABI,
/// The meaning of this value depends on `os_abi`.
abi_version: u8,
type: ET,
machine: EM,
entry: u64,
phoff: u64,
shoff: u64,
phentsize: u16,
phnum: u16,
shentsize: u16,
shnum: u16,
shstrndx: u16,
pub fn iterateProgramHeaders(h: *const Header, file_reader: *Io.File.Reader) ProgramHeaderIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.phnum = h.phnum,
.phoff = h.phoff,
.file_reader = file_reader,
};
}
pub fn iterateProgramHeadersBuffer(h: *const Header, buf: []const u8) ProgramHeaderBufferIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.phnum = h.phnum,
.phoff = h.phoff,
.buf = buf,
};
}
pub fn iterateSectionHeaders(h: *const Header, file_reader: *Io.File.Reader) SectionHeaderIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.shnum = h.shnum,
.shoff = h.shoff,
.file_reader = file_reader,
};
}
pub fn iterateSectionHeadersBuffer(h: *const Header, buf: []const u8) SectionHeaderBufferIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.shnum = h.shnum,
.shoff = h.shoff,
.buf = buf,
};
}
pub fn iterateDynamicSection(
h: *const Header,
file_reader: *Io.File.Reader,
offset: u64,
size: u64,
) DynamicSectionIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.offset = offset,
.end_offset = offset + size,
.file_reader = file_reader,
};
}
pub fn iterateDynamicSectionBuffer(
h: *const Header,
buf: []const u8,
offset: u64,
size: u64,
) DynamicSectionBufferIterator {
return .{
.is_64 = h.is_64,
.endian = h.endian,
.offset = offset,
.end_offset = offset + size,
.buf = buf,
};
}
pub const ReadError = Io.Reader.Error || error{
InvalidElfMagic,
InvalidElfVersion,
InvalidElfClass,
InvalidElfEndian,
};
/// If this function fails, seek position of `r` is unchanged.
pub fn read(r: *Io.Reader) ReadError!Header {
const buf = try r.peek(@sizeOf(Elf64_Ehdr));
if (!mem.eql(u8, buf[0..4], MAGIC)) return error.InvalidElfMagic;
if (buf[EI.VERSION] != 1) return error.InvalidElfVersion;
const endian: Endian = switch (buf[EI.DATA]) {
ELFDATA2LSB => .little,
ELFDATA2MSB => .big,
else => return error.InvalidElfEndian,
};
return switch (buf[EI.CLASS]) {
ELFCLASS32 => .init(try r.takeStruct(Elf32_Ehdr, endian), endian),
ELFCLASS64 => .init(try r.takeStruct(Elf64_Ehdr, endian), endian),
else => return error.InvalidElfClass,
};
}
pub fn init(hdr: anytype, endian: Endian) Header {
// Converting integers to exhaustive enums using `@enumFromInt` could cause a panic.
comptime assert(@typeInfo(OSABI).@"enum".mode == .nonexhaustive);
return .{
.is_64 = switch (@TypeOf(hdr)) {
Elf32_Ehdr => false,
Elf64_Ehdr => true,
else => @compileError("bad type"),
},
.endian = endian,
.os_abi = @fromBackingInt(@intCast(hdr.e_ident[EI.OSABI])),
.abi_version = hdr.e_ident[EI.ABIVERSION],
.type = hdr.e_type,
.machine = hdr.e_machine,
.entry = hdr.e_entry,
.phoff = hdr.e_phoff,
.shoff = hdr.e_shoff,
.phentsize = hdr.e_phentsize,
.phnum = hdr.e_phnum,
.shentsize = hdr.e_shentsize,
.shnum = hdr.e_shnum,
.shstrndx = hdr.e_shstrndx,
};
}
}
All integers are native endian.