Documentation
¶
Overview ¶
Parses an ELF binary providing additional details vs. debug/elf
Errors: ¶
All errors returned will be of the type ErrElf.
Usage: ¶
Construct a new Elf struct with elf.New
bin, err := elf.New(path)
bin is an Elf with the following structure:
{
Name: base filename
Path: absolute path
Class: 32-bit or 64-bit?
Type: EXE, BIN, PIE, ...
Interpreter: path to requested interpeter
Dependencies: slice of paths to dependencies as identified and found by the interpreter
}
Note: ¶
Only accepts static binaries or dynamic binaries which use ld-linux*.so as the interpreter
Index ¶
Constants ¶
View Source
const ( ELFNONE = debug_elf.ELFCLASSNONE // 0 ELF32 = debug_elf.ELFCLASS32 // 1 ELF64 = debug_elf.ELFCLASS64 // 2 )
Values for [EI_CLASS]
View Source
const ( UNDEF = 0 // Undefined EXEC = 1 // Executable: Use Elf.IsExe() to catch _any_ type of executable (including PIE) DYN = 2 // Dynamic: Use Elf.IsDyn() to catch _any_ type of dynamically linked binary (including ET_EXEC with Dyn table) )
Bitmask values for [Type]
Think carefully before directly comparing to bitmask (2^n) values. See value descriptions for individual hints.
View Source
const (
DYNEXE = 3 // EXEC + DYN (PIE or ET_EXEC with Dynamic table)
)
Meaningful combination values for [Type] ¶
Variables ¶
View Source
var ( // Error returned when the provided file is not a valid Elf. ErrInvalidElf = errors.New("invalid ELF file") // Error wrapping a failure when calling `ld-linux*.so` (like `ldd`) to identify dependencies ErrLdd = errors.New("ldd failed to execute") )
Primary Errors
View Source
var ( // Error returned if dynamic ELF has a bad entry for the interpreter ErrBadInterpreter = fmt.Errorf("%w: bad interpreter", ErrInvalidElf) // Error returned if the ELF is not a type we support (currently only ET_EXEC & ET_DYN) ErrUnsupportedElfType = fmt.Errorf("%w: %w (unsupported ELF Type)", ErrInvalidElf, errors.ErrUnsupported) // Error returned if the interpreter is not `ld-linux*.so` ErrUnsupportedInterpreter = fmt.Errorf("%w: %w (unsupported interpreter)", ErrInvalidElf, errors.ErrUnsupported) )
Specific errors which wrap [ErrInvalidElf]
Functions ¶
This section is empty.
Types ¶
type Elf ¶
type Elf struct {
// The filename
Name string
// Absolute, fully resolved path to the file
Path string
// 32 or 64 bit?
// - See https://man7.org/linux/man-pages/man5/elf.5.html#:~:text=.%20%20(3%3A%20%27F%27)-,EI_CLASS,-The%20fifth%20byte
Class EI_CLASS
// Simplified based on ET_DYN & DynFlag1
Type Type
// Absolute path to the interpreter (if executable), "" if not executable.
// - See https://gist.github.com/x0nu11byt3/bcb35c3de461e5fb66173071a2379779 for much more background
Interpreter string
// All requested libraries
Dependencies []string
}
A parsed Elf binary
func New ¶
Construct a new Elf for the file located at path, any error will be an ErrElf
- Returns a best-effort result on error
- Returns early if errors are encountered in resolving the Path or in initial parsing by debug_elf.Open, in this case Name & Path will be filled, although Path may not be fully resolved
- If errors are encountered in parsing these will be collected in the returned ErrElf and the result will contain as much valid information as possible
func (Elf) Diff ¶
Deeply check for diffs between two `Elf`s. Ignores differences in the Path, as long as:
- Both `Path`s are absolute
- Both `Path`s end in the same filename
type ErrElf ¶
type ErrElf struct {
// contains filtered or unexported fields
}
All errors returned will be of the type ErrElf.
- ErrElf can store multiple errors for a single Elf struct. Use .Join() to add an error.
- Will not == nil, even if empty. Use .IsEmpty() to check and then manually return (something, nil)
To extract the path use errors.As followed by .Path()
var errelf *ErrElf
if errors.As(err, &errelf) {
errelf.Path()
}
.
Click to show internal directories.
Click to hide internal directories.