elflib
Version:
ELF file reader and writer
176 lines (175 loc) • 9.57 kB
JavaScript
import * as Enums from './enums.js';
export var Structs;
(function (Structs) {
class Header {
/** The magic number of the ELF file, always '\x7FELF'. */
magic = '\x7FELF'; //! uint32
/** The architecture of the ELF file, either 32 or 64 bits. */
_class = Enums.Class.None; //* uint8
/** The endianness of the data in the ELF file. */
_endian = Enums.Endian.None; //* uint8
/** The version of the ELF file. There is currently only one version. */
_version = Enums.Version.None; //* uint8
/** The ABI (Application Binary Interface) of this ELF file. This is typically not used and set to SystemV. */
_abi = Enums.ABI.SystemV; //* uint8
/** The ABI version. This is ABI specific data but is generally not used. */
_abiVersion = 0x00; //* uint8
/** 7 null bytes of padding. */
padding = '\0\0\0\0\0\0\0'; //* uint8[0x7]
/** The type of ELF file this is (e.g. executable, object file, shared library). */
_type = Enums.Type.None; //? uint16
/** The ISA (Instruction Set Architecture) for this ELF file. This corresponds to the type of processor this ELF file is for
* and does not necessarily include the entire specification of the ISA. isaVersion and flags may contain more information. */
_isa = Enums.ISA.None; //? uint16
/** The version of ISA used. The interpretation of version is ISA specific. */
_isaVersion = 0x00000000; //! uint32
/** The virtual address of the entry point. */
_entryPoint = 0x00000000; //! uint32 | uint64
/** Offset in the ELF file of the first program header entry. */
_programHeadersOffset = 0x00000000; //! uint32
/** Offset in the ELF file of the first section header entry. */
_sectionHeadersOffset = 0x00000000; //! uint32
/** Flags for the ISA used. The interpretation is ISA specific. */
_flags = 0x00000000; //! uint32
/** The size of this ELF header in bytes. */
headerSize = 0x0034; //? uint16 // TODO: Unhardcode for 64-bit support
/** The size of 1 program header entry. */
_programHeadersEntrySize = 0x0000; //? uint16
/** The total number of program header entries in the file. */
_programHeadersEntryCount = 0x0000; //? uint16
/** The size of 1 section header entry. */
_sectionHeadersEntrySize = 0x0000; //? uint16
/** The total number of program section entries in the file. */
_sectionHeadersEntryCount = 0x0000; //? uint16
/** The section index for the section headers string table (if any). */
_shstrIndex = 0x0000; //? uint16
}
Structs.Header = Header;
// TODO: Segments support
//export class Segment {
// /** The index of this segment, as parsed. */
// index: number = NaN;
// /** The type of this segment. */
// type: ProgramHeaderEntryType = NaN;
// /** A human readable description of type. */
// typeDescription: string = '';
// /** Flags for this segment */
// flags: number = NaN;
// /** A human readable description of flags. */
// flagsDescription: string = '';
// /** The file offset for data for this segment. */
// offset: number = NaN;
// /** The virtual address for this segment. Also called the VMA address. */
// vaddr: number | bigint = NaN;
// /** The physical address for this segment. Also called the LMA or load address. */
// paddr: number | bigint = NaN;
// /** The size of this segment in the ELF file */
// filesz: number = NaN;
// /** The size of this segment in (virtual) memory. */
// memsz: number = NaN;
// /** The alignment of this segment (the segment must be loaded to an address in multiples of this). */
// align: number = NaN;
//}
class Section {
/** Offset from the start of the {@link Header.shstrIndex section headers string table}
* to the address of this section's name in said table, if any. */
_nameOffset = 0x00000000; //! uint32
/** The type of this section. */
_type = Enums.SectionType.Null; //! uint32
/** The flags for this section. */
_flags = 0x00000000; //! uint32
/** The virtual address of this section. */
_addr = 0x00000000; //! uint32 | uint64
/** The absolute offset of the section in the file. */
_offset = 0x00000000; //! uint32
/** The size of this section, in bytes. */
_size = 0x00000000; //! uint32
/** A section linked to this section. For example for a symbol section the
* linked section is a string table section providing names for symbols. */
_link = 0x00000000; //! uint32
/** Section type specific info for this section. */
_info = 0x00000000; //! uint32
/** The alignment requirement of this section. */
_addrAlign = 0x00000000; //! uint32
/** The size of each "entity" in this section, if applicable.
* For example, if this is a symbol table section, this is the size of a symbol entry. */
_entSize = 0x00000000; //! uint32
}
Structs.Section = Section;
class Symbol {
/** Offset from the start of the {@link Section.link linked string table section} of
* this symbol's section, to the address of this symbol's name in said table, if any. */
_nameOffset = 0x00000000; //! uint32
/** The value of this symbol. The interpretation of the value is dependent on a few things but is generally an offset or address. */
_value = 0x00000000; //! uint32
/** The size of this symbol, if applicable. */
_size = 0x00000000; //! uint32
/** Symbol type specific information. */
_info = 0x00; //* uint8
/** Other symbol information. */
_other = 0x00; //* uint8
/** Section index for this symbol.
* @summary This is the index of the section for this symbol. There
* are also special values such as 0xFFF1 for an absolute index symbol
* in a relocatable ELF file (object file). */
_shndx = 0x0000; //? uint16
}
Structs.Symbol = Symbol;
class Relocation {
/** The location at which to apply the relocation action.
* @summary This member gives the location at which to apply the relocation action. For
* a relocatable file, the value is the byte offset from the beginning of the
* section to the storage unit affected by the relocation. For an executable file
* or a shared object, the value is the virtual address of the storage unit affected by the relocation. */
_addr = 0x00000000; //! uint32 | uint64
/** The symbol table index with respect to which the
* relocation must be made, and the type of relocation to apply.
* @summary This member gives both the symbol table index with respect to which the
* relocation must be made, and the type of relocation to apply. For example,
* a call instruction's relocation entry would hold the symbol table index of
* the function being called. If the index is STN_UNDEF, the undefined symbol
* index, the relocation uses 0 as the symbol value. Relocation types are
* processor-specific; descriptions of their behavior appear in the processor
* supplement. When the text in the processor supplement refers to a
* relocation entry's relocation type or symbol table index, it means the result
* of applying ELF32_R_TYPE or ELF32_R_SYM, respectively, to the entry's r_info member. */
_info = 0x00000000; //! uint32 | uint64
/** A constant addend used to compute the value to be stored into the relocatable field. */
_addend = undefined; //? sint32 | sint64
}
Structs.Relocation = Relocation;
/** RPL-exclusive file information section data structure. */
class RPLFileInfo {
/** Magic number of the RPL_FILEINFO section, always 0xCAFE */
magic = 0xCAFE; //? uint16
_version = 0x0000; //? uint16
_textSize = 0x00000000; //* uint32
_textAlign = 0x00000000; //* uint32
_dataSize = 0x00000000; //* uint32
_dataAlign = 0x00000000; //* uint32
_loadSize = 0x00000000; //* uint32
_loadAlign = 0x00000000; //* uint32
_tempSize = 0x00000000; //* uint32
_trampAdjust = 0x00000000; //* uint32
_sdaBase = 0x00000000; //* uint32
_sda2Base = 0x00000000; //* uint32
_stackSize = 0x00000000; //* uint32
/** The offset from the start of the section to the start of the strings array */
_stringsOffset = 0x00000000; //* uint32
_flags = 0x00000000; //* uint32
_heapSize = 0x00000000; //* uint32
_tagOffset = 0x00000000; //* uint32
_minVersion = 0x00000000; //* uint32
_compressionLevel = 0x00000000; //! sint32
_trampAddition = 0x00000000; //* uint32
_fileInfoPad = 0x00000000; //* uint32
_cafeSdkVersion = 0x00000000; //* uint32
_cafeSdkRevision = 0x00000000; //* uint32
_tlsModuleIndex = 0x0000; //? uint16
_tlsAlignShift = 0x0000; //? uint16
_runtimeFileInfoSize = 0x00000000; //* uint32
/** Array of null-terminated strings until the end of the file */
_strings = {};
}
Structs.RPLFileInfo = RPLFileInfo;
})(Structs || (Structs = {}));