elf

package
v0.0.0-...-49ba637 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EI_NIDENT = 16

	// Magic
	ELFMAG0 = 0x7f
	ELFMAG1 = 'E'
	ELFMAG2 = 'L'
	ELFMAG3 = 'F'

	// Class / Data
	ELFCLASS64  = 2
	ELFDATA2LSB = 1
	EV_CURRENT  = 1

	// OS ABI
	ELFOSABI_NONE  = 0
	ELFOSABI_LINUX = 3

	// Object Types
	ET_REL  = 1 // Relocatable file (.o)
	ET_EXEC = 2 // Executable file
	ET_DYN  = 3 // Shared object (.so)

	// Machine
	EM_X86_64 = 62

	// Section Types
	SHT_NULL     = 0
	SHT_PROGBITS = 1
	SHT_SYMTAB   = 2
	SHT_STRTAB   = 3
	SHT_RELA     = 4
	SHT_DYNAMIC  = 6 // Added: Dynamic linking information
	SHT_NOBITS   = 8

	// Section Flags
	SHF_WRITE     = 0x1
	SHF_ALLOC     = 0x2
	SHF_EXECINSTR = 0x4

	// Segment Types (Phdr)
	PT_NULL    = 0
	PT_LOAD    = 1
	PT_DYNAMIC = 2
	PT_INTERP  = 3
	PT_PHDR    = 6

	// Segment Flags
	PF_X = 0x1 // Execute
	PF_W = 0x2 // Write
	PF_R = 0x4 // Read

	// Symbol Bindings
	STB_LOCAL  = 0
	STB_GLOBAL = 1
	STB_WEAK   = 2

	// Symbol Types
	STT_NOTYPE = 0
	STT_OBJECT = 1
	STT_FUNC   = 2

	// Relocations (AMD64)
	R_X86_64_NONE     = 0
	R_X86_64_64       = 1
	R_X86_64_PC32     = 2
	R_X86_64_PLT32    = 4
	R_X86_64_GLOB_DAT = 6
	R_X86_64_JMP_SLOT = 7
	R_X86_64_RELATIVE = 8
	R_X86_64_32       = 10
	R_X86_64_32S      = 11

	// Dynamic Array Tags (d_tag)
	DT_NULL     = 0
	DT_NEEDED   = 1
	DT_STRTAB   = 5
	DT_SYMTAB   = 6
	DT_RELA     = 7
	DT_RELASZ   = 8
	DT_RELAENT  = 9
	DT_STRSZ    = 10
	DT_SYMENT   = 11
	DT_INIT     = 12
	DT_FINI     = 13
	DT_SONAME   = 14
	DT_RPATH    = 15
	DT_REL      = 17
	DT_DEBUG    = 21
	DT_TEXTREL  = 22
	DT_JMPREL   = 23
	DT_BIND_NOW = 24

	DT_PLTRELSZ = 2
	DT_PLTREL   = 20

	DT_PLTGOT = 3
)

ELF Constants

Variables

ByteOrder helper

Functions

This section is empty.

Types

type Config

type Config struct {
	Entry       string
	BaseAddr    uint64
	Interpreter string
}

type Elf64Rela

type Elf64Rela struct {
	Offset uint64
	Info   uint64
	Addend int64
}

Elf64Rela represents a relocation entry (24 bytes)

type Elf64Sym

type Elf64Sym struct {
	Name  uint32
	Info  uint8
	Other uint8
	Shndx uint16
	Value uint64
	Size  uint64
}

Elf64Sym represents the symbol structure in the binary (24 bytes)

type Header struct {
	Ident     [16]byte
	Type      uint16
	Machine   uint16
	Version   uint32
	Entry     uint64
	Phoff     uint64
	Shoff     uint64
	Flags     uint32
	Ehsize    uint16
	Phentsize uint16
	Phnum     uint16
	Shentsize uint16
	Shnum     uint16
	Shstrndx  uint16
}

Header represents the ELF File Header (Elf64_Ehdr)

type InputObject

type InputObject struct {
	Name     string
	Sections []*InputSection
	Symbols  []*InputSymbol
}

InputObject represents a parsed .o file

func LoadArchive

func LoadArchive(path string) ([]*InputObject, error)

LoadArchive iterates a .a file and returns all contained ELF objects.

func LoadObject

func LoadObject(name string, data []byte) (*InputObject, error)

LoadObject parses an ELF object manually to ensure 1:1 index mapping

type InputReloc

type InputReloc struct {
	Offset uint64
	Type   uint32
	Addend int64
	Sym    *InputSymbol
}

type InputSection

type InputSection struct {
	Name   string
	Type   uint32
	Flags  uint64
	Data   []byte
	Relocs []InputReloc

	// Output mapping
	VirtualAddress uint64
	OutputOffset   uint64
}

type InputSymbol

type InputSymbol struct {
	Name    string
	Type    uint8
	Bind    uint8
	Section *InputSection // Nil if Undefined
	Value   uint64
	Size    uint64
}

type Linker

type Linker struct {
	Config      Config
	Objects     []*InputObject
	SharedLibs  []*SharedObject
	GlobalTable map[string]*ResolvedSymbol

	DynStrTab  []byte
	DynSyms    []Elf64Sym
	RelaDyn    []Elf64Rela
	GotEntries []string

	InterpSect  []byte
	DynSect     []byte
	DynSymSect  []byte
	DynStrSect  []byte
	RelaDynSect []byte
	TextSection []byte
	DataSection []byte

	ShStrTab []byte

	TextAddr  uint64
	DataAddr  uint64
	BssAddr   uint64
	EntryAddr uint64
	BssSize   uint64

	SectionOffsets map[*InputSection]uint64
}

func NewLinker

func NewLinker(cfg Config) *Linker

func (*Linker) AddArchive

func (l *Linker) AddArchive(path string) error

func (*Linker) AddObject

func (l *Linker) AddObject(name string, data []byte) error

func (*Linker) AddSharedLib

func (l *Linker) AddSharedLib(path string, data []byte) error
func (l *Linker) Link(outPath string) error

type ProgHeader

type ProgHeader struct {
	Type   uint32
	Flags  uint32
	Off    uint64
	Vaddr  uint64
	Paddr  uint64
	Filesz uint64
	Memsz  uint64
	Align  uint64
}

ProgHeader represents a Program Header (Elf64_Phdr)

type ResolvedSymbol

type ResolvedSymbol struct {
	Name    string
	Value   uint64
	Section string
	Defined bool
}

type SectionHeader

type SectionHeader struct {
	Name      uint32
	Type      uint32
	Flags     uint64
	Addr      uint64
	Offset    uint64
	Size      uint64
	Link      uint32
	Info      uint32
	Addralign uint64
	Entsize   uint64
}

SectionHeader represents a Section Header (Elf64_Shdr)

type SharedObject

type SharedObject struct {
	Name    string
	Symbols []string
}

SharedObject represents a dynamic library (e.g., libc.so)

func LoadSharedObject

func LoadSharedObject(name string, data []byte) (*SharedObject, error)

Jump to

Keyboard shortcuts

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