elf

package
v0.0.0-...-3f1fa26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const ProgramHeaderSize64 uint16 = 56
View Source
const SHN_UNDEF = 0x0

Variables

This section is empty.

Functions

func CreateTinyBinary

func CreateTinyBinary(m lib.MachineCode, path string) error

Demo function. Don't use this in anything serious: it puts data in an executable segment.

Types

type ELF

type ELF struct {
	*ELFHeader

	// A program header table, if present, tells the system how to create a
	// process image. Files used to build a process image (execute a program)
	// must have a program header table; relocatable files do not need one.
	ProgramHeaders ProgramHeaderTable

	Sections []*Section
}

func NewELF

func NewELF() *ELF

func ParseELF

func ParseELF(r *bytes.Reader) (*ELF, error)

func ParseELFFile

func ParseELFFile(path string) (*ELF, error)

func (*ELF) EncodeHeaders

func (e *ELF) EncodeHeaders() ([]byte, error)

Encodes the ELF header, program header table and section header table (TODO). Does not copy .Data!

func (*ELF) GetSection

func (e *ELF) GetSection(name string) *Section

func (*ELF) String

func (e *ELF) String() string

type ELFClass

type ELFClass uint8
const (
	ELFCLASSNONE ELFClass = 0
	ELFCLASS32   ELFClass = 1
	ELFCLASS64   ELFClass = 2
	ELFCLASSNUM  ELFClass = 3
)

func (ELFClass) String

func (i ELFClass) String() string

type ELFData

type ELFData uint8
const (
	ELFDATANONE ELFData = 0
	ELFDATA2LSB ELFData = 1
	ELFDATA2MSB ELFData = 2
)

func (ELFData) String

func (i ELFData) String() string

type ELFHeader

type ELFHeader struct {
	// ELF Identification
	Class ELFClass
	// ELF Identification
	Data ELFData
	// ELF Identification
	OS_ABI ELFOS_ABI
	// ELF Identification
	ABI_Version uint8

	Type    ELFType
	Machine ELFMachine
	Version ELFVersion

	// This member gives the virtual address to which the system first
	// transfers control, thus starting the process. If the file has no
	// associated entry point, this member holds zero.
	Entry Elf64_Addr

	// This member holds the program header table's file offset in bytes. If
	// the file has no program header table, this member holds zero.
	ProgramHeaderTableOffset Elf64_Off

	// This member holds the section header table's file offset in bytes. If
	// the file has no section header table, this member holds zero.
	SectionHeaderTableOffset Elf64_Off

	Flags                        uint32
	HeaderSize                   uint16
	ProgramHeaderEntrySize       uint16
	ProgramHeaderNumberOfEntries uint16
	SectionHeaderEntrySize       uint16
	SectionHeaderNumberOfEntries uint16

	// This member holds the section header table index of the entry associated
	// with the section name string table. If the file has no section name
	// string table, this member holds the value SHN_UNDEF.
	Shstrndx uint16
}

func NewELFHeader

func NewELFHeader() *ELFHeader

func ParseELFHeader

func ParseELFHeader(r *bytes.Reader) (*ELFHeader, error)

func (*ELFHeader) Encode

func (e *ELFHeader) Encode() ([]uint8, error)

func (*ELFHeader) GetByteOrder

func (e *ELFHeader) GetByteOrder() binary.ByteOrder

func (*ELFHeader) String

func (e *ELFHeader) String() string

type ELFMachine

type ELFMachine uint16
const (
	EM_NONE   ELFMachine = 0x0000 // An unknown machine
	EM_M32    ELFMachine = 0x0001 // AT&T WE 32100
	EM_SPARC  ELFMachine = 0x0002 // Sun Microsystems SPARC
	EM_386    ELFMachine = 0x0003 // Intel 80386
	EM_68K    ELFMachine = 0x0004 // Motorola 68000
	EM_88K    ELFMachine = 0x0005 // Motorola 88000
	EM_860    ELFMachine = 0x0007 // Intel 80860
	EM_MIPS   ELFMachine = 0x0008 // MIPS RS3000 (big-endian only)
	EM_PPC    ELFMachine = 0x0014 // PowerPC
	EM_PPC64  ELFMachine = 0x0015 // PowerPC 64-bit
	EM_ARM    ELFMachine = 0x0028 // Advanced RISC Machines
	EM_X86_64 ELFMachine = 0x003e // AMD x86-64
)

func (ELFMachine) String

func (i ELFMachine) String() string

type ELFOS_ABI

type ELFOS_ABI uint8
const (
	ELF_OS_ABI_NONE  ELFOS_ABI = 0
	ELF_OS_ABI_LINUX ELFOS_ABI = 3
)

func (ELFOS_ABI) String

func (i ELFOS_ABI) String() string

type ELFType

type ELFType uint16
const (
	ET_NONE   ELFType = 0x000  // An unknown type.
	ET_REL    ELFType = 0x0001 // A relocatable file.
	ET_EXEC   ELFType = 0x0002 // An executable file.
	ET_DYN    ELFType = 0x0003 // A shared object.
	ET_CORE   ELFType = 0x0004 // A core file.
	ET_LOPROC ELFType = 0xff00
	ET_HIPROC ELFType = 0xffff
)

func (ELFType) String

func (i ELFType) String() string

type ELFVersion

type ELFVersion uint32
const (
	EV_NONE    ELFVersion = 0 // Invalid version
	EV_CURRENT ELFVersion = 1 // Current version
)

func (ELFVersion) String

func (i ELFVersion) String() string

type Elf64_Addr

type Elf64_Addr uint64

type Elf64_Off

type Elf64_Off uint64

type PHFlags

type PHFlags uint32
const (
	// Execute
	PF_X PHFlags = 0x1
	// Write
	PF_W PHFlags = 0x2
	// Execute & Write
	PF_WX PHFlags = 0x3
	// Read
	PF_R PHFlags = 0x4
	// Read & Execute
	PF_RX PHFlags = 0x5
	// Read & Write
	PF_RW PHFlags = 0x6
	// Read & Write & Execute
	PF_RWX PHFlags = 0x7
	// Unspecified
	PF_MASKPROC PHFlags = 0xf0000000
)

func (PHFlags) String

func (i PHFlags) String() string

type PHType

type PHType uint32
const (
	// The array element is unused; other members’ values are undefined.  This
	// type lets the program header table have ignored entries
	PT_NULL PHType = 0
	// The array element specifies a loadable segment, described by Filesize
	// and Memsize.  The bytes from the file are mapped to the beginning of the
	// memory segment.  If the segment’s memorysize (Memsize) is larger than
	// the file size (Filesize), the "extra" bytes are defined to hold the
	// value 0 and to follow the segment’s initialized area.  The file size may
	// not be larger than the memorysize.  Loadable segment entries in the
	// program header table appear in ascending order, sorted on the
	// SegmentVirtualAddress member.
	PT_LOAD PHType = 1
	// The array element specifies dynamic linking information.
	PT_DYNAMIC PHType = 2
	// The array element specifies the location and size of a null-terminated
	// path name to invoke as an interpreter.  This segment type is meaningful
	// only for executable files (though it may occur for shared objects); it
	// may not occur more than once in a file.  If it is present, it must
	// precede any loadable segment entry. (e.g. /lib64/ld-linux-x86-64.so.2)
	PT_INTERP PHType = 3
	// The array element specifies the location and size of auxiliaryinformation.
	PT_NOTE PHType = 4
	// This segment type is reserved but has unspecified semantics
	PT_SHLIB PHType = 5
	// The array element, if present, specifies the location and size of
	// the program header table itself, both in the file and in the memory image
	// of the program.  This segment type may not occur more than once in a
	// file.  Moreover, it may occur only if the programheader table is part of
	// the memory image of the program.  If it is present, it must precede any
	// loadable segment entry.
	PT_PHDR PHType = 6
	// Values in this inclusive range are reserved for processor-specific semantics.
	PT_LOPROC PHType = 0x70000000
	// Values in this inclusive range are reserved for processor-specific semantics.
	PT_HIPROC PHType = 0x7fffffff
)

func (PHType) String

func (i PHType) String() string

type ProgramHeader

type ProgramHeader struct {
	// This member tells what kind of segment this array element describes or
	// how to interpret the array element’s information.
	Type PHType
	// This member gives flags relevant to the segment.
	Flags PHFlags
	// This member gives the offset from the beginning of the file at which the
	// first byte of the segment resides.
	Offset Elf64_Off
	// This member gives the virtual address at which the first byte of the
	// segment resides in memory
	SegmentVirtualAddress Elf64_Addr
	// On systems for which physical addressing is relevant, this member is
	// reserved for the segment’s physical address.  Because System V ignores
	// physical addressing for application programs,this member has unspecified
	// contents for executable files and shared objects
	SegmentPhysicalAddress Elf64_Addr
	// Segment size in file
	Filesize uint64
	// Segment size in memory
	Memsize uint64
	// Segment alignment, file & memory
	Align uint64

	Data []byte
}

func NewProgramHeader

func NewProgramHeader(typ PHType, flags PHFlags) *ProgramHeader

func ParseProgramHeader

func ParseProgramHeader(header *ELFHeader, r *bytes.Reader) (*ProgramHeader, error)

func ParseProgramHeaderTable

func ParseProgramHeaderTable(header *ELFHeader, r *bytes.Reader) ([]*ProgramHeader, error)

func (*ProgramHeader) Encode

func (s *ProgramHeader) Encode(header *ELFHeader) ([]byte, error)

func (*ProgramHeader) String

func (s *ProgramHeader) String() string

type ProgramHeaderTable

type ProgramHeaderTable []*ProgramHeader

func (ProgramHeaderTable) Encode

func (p ProgramHeaderTable) Encode(header *ELFHeader) ([]byte, error)

type SHFlags

type SHFlags uint64
const (
	// No flags
	SHF_NULL SHFlags = 0x0
	// The section contains data that should be writable during process execution.
	SHF_WRITE SHFlags = 0x1
	// The section occupies memory during process execution.Some control
	// sections do not reside in the memory image of an object file; this
	// attribute is off for those sections.
	SHF_ALLOC SHFlags = 0x2
	// The section contains executable machine instructions.
	SHF_EXECINSTR      SHFlags = 0x4
	SHF_RELA_LIVEPATCH SHFlags = 0x00100000
	SHF_RO_AFTER_INIT  SHFlags = 0x00200000
	// All bits included in this mask are reserved for processor-specific
	// semantics.  If meanings are specified, the processorsupplement explains
	// them.
	SHF_MASKPROC SHFlags = 0xf0000000
)

func (SHFlags) String

func (i SHFlags) String() string

type SHType

type SHType uint32
const (
	// This value marks the section header as inactive; it does not have an
	// associated section.  Other members of the section header have undefined
	// values.
	SHT_NULL SHType = 0
	// The section holds information defined by the program, whose format and
	// meaning are determined solely by the program.
	SHT_PROGBITS SHType = 1
	// These sections hold a symbol table.  Currently, an object file may have
	// only one section of each type, but this restriction may be relaxed in the
	// future.  Typically, SHT_SYMTAB provides symbols for link editing,
	// though it may also be used for dynamic linking.  As a complete symbol
	// table, it may contain many symbols unnecessary for dynamic linking.
	// Consequently, an object file may also contain a SHT_DYNSYM
	// section, which holds a minimal set of dynamic linking symbols, to
	// save space.
	SHT_SYMTAB SHType = 2
	// The section holds a string table.  An object file may have multiple
	// string table sections.
	SHT_STRTAB SHType = 3
	// The section holds relocation entries with explicit addends, such as type
	// Elf32_Rela afor the 32-bit class of object files.  An object file
	// may have multiple relocation sections.
	SHT_RELA SHType = 4
	// The section holds a symbol hash table.  All objects participating in
	// dynamic linking must contain a symbol hash table.Currently, an object
	// file may have only one hash table, but thisrestriction may be relaxed in
	// the future.
	SHT_HASH SHType = 5
	// The section holds information for dynamic linking. Currently an object
	// file may have only one dynamic section, but this restriction may be
	// relaxed in the future.
	SHT_DYNAMIC SHType = 6
	// The section holds information that marks the file in some way. (no shit)
	SHT_NOTE SHType = 7
	// A section of this type occupies no space in the file but other-wise
	// resembles SHT_PROGBITS.  Although this section contains no
	// bytes, the sh_offset member contains the conceptual file offset.
	SHT_NOBITS SHType = 8
	// The section holds relocation entries without explicit addends,such as
	// type Elf32_Rel for the 32-bit class of object files.  Anobject
	// file may have multiple relocation sections.
	SHT_REL SHType = 9
	// This section type is reserved but has unspecified semantics.
	SHT_SHLIB SHType = 10
	// These sections hold a symbol table.  Currently, an object file may have
	// only one section of each type, but this restriction may be relaxed in the
	// future.  Typically, SHT_SYMTAB provides symbols for link editing,
	// though it may also be used for dynamic linking.  As a complete symbol
	// table, it may contain many symbols unnecessary for dynamic linking.
	// Consequently, an object file may also contain a SHT_DYNSYM
	// section, which holds a minimal set of dynamic linking symbols, to
	// save space.
	SHT_DYNSYM SHType = 11
	SHT_NUM    SHType = 12
	// Values in this inclusive range are reserved for processor-specific
	// semantics.  If meanings are specified, the processorsupplement explains
	// them.
	SHT_LOPROC SHType = 0x70000000
	// Values in this inclusive range are reserved for processor-specific
	// semantics.  If meanings are specified, the processorsupplement explains
	// them.
	SHT_HIPROC SHType = 0x7fffffff
	// This value specifies the lower bound of the range of indexesreserved for application programs
	SHT_LOUSER SHType = 0x80000000
	// This value specifies the upper bound of the range of indexesreserved for application programs.
	SHT_HIUSER SHType = 0xffffffff
)

func (SHType) String

func (i SHType) String() string

type Section

type Section struct {
	Name      string
	Type      SHType
	Flags     SHFlags
	Size      uint32
	Addr      Elf64_Addr
	Offset    Elf64_Off
	Link      uint32
	Info      uint32
	AddrAlign uint32
	EntSize   uint32
	Data      []byte
	// contains filtered or unexported fields
}

func NewBSSSection

func NewBSSSection() *Section

This section holds uninitialized data that contribute to the program’s memory image. By definition, the system initializes the data with zeros when the program begins to run. The section occupies no file space, as indicated by the section type, SHT_NOBITS

func NewCommentSection

func NewCommentSection() *Section

This section holds version control information.

func NewDataSection

func NewDataSection() *Section

These sections hold initialized data that contribute to the program’s memory image

func NewReadOnlyDataSection

func NewReadOnlyDataSection() *Section

These sections hold read-only data that typically contribute to a non-writable segment in the process image.

func NewSection

func NewSection(name string, typ SHType, flags SHFlags) *Section

func NewSectionHeaderStringSection

func NewSectionHeaderStringSection() *Section

This section holds section names.

func NewTextSection

func NewTextSection() *Section

This section holds the "text", or executable instructions, of a program

func ParseSections

func ParseSections(header *ELFHeader, shTable []*SectionHeader, r *bytes.Reader) ([]*Section, error)

func (*Section) GetStringTable

func (s *Section) GetStringTable() *StringTable

func (*Section) GetSymbolTable

func (s *Section) GetSymbolTable(strTable *StringTable) (*SymbolTable, error)

func (*Section) String

func (s *Section) String() string

type SectionHeader

type SectionHeader struct {
	// This member specifies the name of the section.  Its value is an index
	// into the section header string table section, giving the location of a
	// null-terminated string.
	Name uint32
	// This member categorizes the section’s contents and semantics.
	Type SHType
	// Sections support 1-bit flags that describe miscellaneous attributes.
	Flags SHFlags
	// If the section will appear in the memory image of a process, this member
	// gives the address at which the section’s first byte should reside.
	// Otherwise, the member contains 0
	Addr Elf64_Addr
	// This member’s value gives the byte offset from the beginning of the file
	// to the first byte in the section. One section type, SHT_NOBITS,
	// occupies no space in the file, and its sh_offset
	// member locates the conceptual placement in the file.
	Offset Elf64_Off
	// This member gives the section’s size in bytes.  Unless the section type is
	// SHT_NOBITS, the section occupiess h_size bytes in the file.
	// A section of type SHT_NOBITS may have a non-zerosize, but it occupies no space in the file.
	Size uint32
	// This member holds a section header table index link, whoseinterpretation depends on the section type.
	Link uint32
	// This member holds extra information, whose interpretationdepends on the section type.
	Info uint32
	// Some sections have address alignment constraints.  For example, if a
	// section holds a doubleword, the system must ensure doubleword alignment
	// for the entire section.  That is, the value of sh_addr must be
	// congruent to 0, modulo the value of sh_addralign.  Currently,
	// only 0 and positive integral powersof two are allowed.  Values 0 and 1
	// mean the section has noalignment constraints.
	AddrAlign uint32
	// Some sections hold a table of fixed-size entries, such as a symbol
	// table.  For such a section, this member gives the size in bytes of each
	// entry.  The member contains 0 if the section doesnot hold a table of
	// fixed-size entries
	EntSize uint32
}

func ParseSectionHeader

func ParseSectionHeader(header *ELFHeader, r *bytes.Reader) (*SectionHeader, error)

func ParseSectionHeaderTable

func ParseSectionHeaderTable(header *ELFHeader, r *bytes.Reader) ([]*SectionHeader, error)

func (*SectionHeader) String

func (s *SectionHeader) String() string

type StringTable

type StringTable struct {
	// contains filtered or unexported fields
}

func NewStringTable

func NewStringTable(data []byte) *StringTable

func ParseStringTable

func ParseStringTable(header *ELFHeader, sectionHeader *SectionHeader, r *bytes.Reader) (*StringTable, error)

func (*StringTable) GetString

func (s *StringTable) GetString(ix int) (string, error)

func (*StringTable) String

func (s *StringTable) String() string

type Symbol

type Symbol struct {
	// This member holds an index into the object file’s symbol string table,
	// which holds the character representations of the symbol names. If the
	// value is non-zero, it represents a string table index that gives the
	// symbol name. Otherwise, the symbol table entry has no name.
	Name uint32

	// This member specifies the symbol’s binding attribute.
	Binding SymbolBinding

	// This member specifies the symbol’s type attribute.
	Type SymbolType

	// This member currently holds 0 and has no defined meaning.
	Other byte

	// Every symbol table entry is ‘‘defined’’ in relation to some section;
	// this member holds the relevant section header table index.
	Shndx uint16

	// This member gives the value of the associated symbol. Depend- ing on the
	// context, this may be an absolute value, an address, and so on
	Value Elf64_Addr

	// Many symbols have associated sizes. For example, a data object’s size is
	// the number of bytes contained in the object. This member holds 0 if the
	// symbol has no size or an unknown size.
	Size uint64
	// contains filtered or unexported fields
}

func ParseSymbol

func ParseSymbol(header *ELFHeader, stringTable *StringTable, r *bytes.Reader) (*Symbol, error)

func (*Symbol) String

func (s *Symbol) String() string

type SymbolBinding

type SymbolBinding uint8
const (
	// Local symbols are not visible outside the object file containing their
	// definition. Local symbols of the same name may exist in multiple files
	// without interfering with each other.
	SB_LOCAL SymbolBinding = 0

	// Global symbols are visible to all object files being combined. One
	// file’s definition of a global symbol will satisfy another file’s
	// undefined reference to the same global symbol.
	SB_GLOBAL SymbolBinding = 1

	// Weak symbols resemble global symbols, but their definitions have lower
	// precedence.
	SB_WEAK SymbolBinding = 2

	// Processor-specific
	SB_LOPROC SymbolBinding = 13

	// Processor-specific
	SB_HIPROC SymbolBinding = 15
)

func (SymbolBinding) String

func (i SymbolBinding) String() string

type SymbolTable

type SymbolTable struct {
	Symbols []*Symbol
	// contains filtered or unexported fields
}

func ParseSymbolTable

func ParseSymbolTable(header *ELFHeader, stringTable *StringTable, r *bytes.Reader) (*SymbolTable, error)

func (*SymbolTable) GetSymbol

func (s *SymbolTable) GetSymbol(name string) *Symbol

type SymbolType

type SymbolType uint8
const (
	// The symbol’s type is not specified.
	STT_NOTYPE SymbolType = 0

	// The symbol is associated with a data object, such as a variable, an
	// array, and so on.
	STT_OBJECT SymbolType = 1

	// The symbol is associated with a function or other executable code.
	STT_FUNC SymbolType = 2

	// The symbol is associated with a section. Symbol table entries of this
	// type exist primarily for relocation and normally have STB_LOCAL
	// binding.
	STT_SECTION SymbolType = 3

	// Conventionally, the symbol’s name gives the name of the source file
	// associated with the object file. A file symbol has STB_LOCAL binding,
	// its section index is SHN_ABS, and it precedes the other STB_LOCAL
	// symbols for the file, if it is present.
	STT_FILE SymbolType = 4

	// Processor-specific
	STT_LOPROC SymbolType = 13

	// Processor-specific
	STT_HIPROC SymbolType = 15
)

func (SymbolType) String

func (i SymbolType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL