macho

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 11 Imported by: 35

README

go-macho [WIP] 🚧

GoDoc License

Package macho implements access to and creation of Mach-O object files.


Install

$ go get github.com/blacktop/go-macho

Getting Started

package main

import "github.com/blacktop/go-macho"

func main() {

}

License

MIT Copyright (c) 2020 blacktop

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFat = &FormatError{0, "not a fat Mach-O file", nil}

ErrNotFat is returned from NewFatFile or OpenFat when the file is not a universal binary but may be a thin binary, based on its magic number.

Functions

func RoundUp

func RoundUp(x, align uint64) uint64

Types

type BuildVersion

type BuildVersion struct {
	LoadBytes
	types.BuildVersionCmd
	Platform    string /* platform */
	Minos       string /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
	Sdk         string /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
	NumTools    uint32 /* number of tool entries following this */
	Tool        string
	ToolVersion string
}

A BuildVersion represents a Mach-O build for platform min OS version.

type CodeSignature

type CodeSignature struct {
	LoadBytes
	types.CodeSignatureCmd
	Offset uint32
	Size   uint32
}

type DataInCode

type DataInCode struct {
	LoadBytes
	types.DataInCodeCmd
	Entries []types.DataInCodeEntry
}

A DataInCode represents a Mach-O data in code command.

type DyldInfo

type DyldInfo struct {
	LoadBytes
	types.DyldInfoCmd
	RebaseOff    uint32 // file offset to rebase info
	RebaseSize   uint32 //  size of rebase info
	BindOff      uint32 // file offset to binding info
	BindSize     uint32 // size of binding info
	WeakBindOff  uint32 // file offset to weak binding info
	WeakBindSize uint32 //  size of weak binding info
	LazyBindOff  uint32 // file offset to lazy binding info
	LazyBindSize uint32 //  size of lazy binding info
	ExportOff    uint32 // file offset to export info
	ExportSize   uint32 //  size of export info
}

A DyldInfo represents a Mach-O id dyld info command.

type Dylib

type Dylib struct {
	LoadBytes
	types.DylibCmd
	Name           string
	Time           uint32
	CurrentVersion string
	CompatVersion  string
}

A Dylib represents a Mach-O load dynamic library command.

type DylibID

type DylibID Dylib

A DylibID represents a Mach-O load dynamic library ident command.

type Dysymtab

type Dysymtab struct {
	LoadBytes
	types.DysymtabCmd
	IndirectSyms []uint32 // indices into Symtab.Syms
}

A Dysymtab represents a Mach-O dynamic symbol table command.

type FatArch

type FatArch struct {
	FatArchHeader
	*File
}

A FatArch is a Mach-O File inside a FatFile.

type FatArchHeader

type FatArchHeader struct {
	CPU    types.CPU
	SubCPU types.CPUSubtype
	Offset uint32
	Size   uint32
	Align  uint32
}

A FatArchHeader represents a fat header for a specific image architecture.

type FatFile

type FatFile struct {
	Magic  types.Magic
	Arches []FatArch
	// contains filtered or unexported fields
}

A FatFile is a Mach-O universal binary that contains at least one architecture.

func NewFatFile

func NewFatFile(r io.ReaderAt) (*FatFile, error)

NewFatFile creates a new FatFile for accessing all the Mach-O images in a universal binary. The Mach-O binary is expected to start at position 0 in the ReaderAt.

func OpenFat

func OpenFat(name string) (*FatFile, error)

OpenFat opens the named file using os.Open and prepares it for use as a Mach-O universal binary.

func (*FatFile) Close

func (ff *FatFile) Close() error

Close with close the Mach-O Fat file.

type File

type File struct {
	FileTOC

	Symtab   *Symtab
	Dysymtab *Dysymtab
	// contains filtered or unexported fields
}

A File represents an open Mach-O file.

func NewFile

func NewFile(r io.ReaderAt, loads ...types.LoadCmd) (*File, error)

NewFile creates a new File for accessing a Mach-O binary in an underlying reader. The Mach-O binary is expected to start at position 0 in the ReaderAt.

func Open

func Open(name string) (*File, error)

Open opens the named file using os.Open and prepares it for use as a Mach-O binary.

func (*File) BuildVersion

func (f *File) BuildVersion() *BuildVersion

BuildVersion returns the build version load command, or nil if no build version exists.

func (*File) Close

func (f *File) Close() error

Close closes the File. If the File was created using NewFile directly instead of Open, Close has no effect.

func (*File) DWARF

func (f *File) DWARF() (*dwarf.Data, error)

DWARF returns the DWARF debug information for the Mach-O file.

func (*File) DyldInfo

func (f *File) DyldInfo() *DyldInfo

DyldInfo returns the dyld info load command, or nil if no dyld info exists.

func (*File) DylibID

func (f *File) DylibID() *DylibID

DylibID returns the dylib ID load command, or nil if no dylib ID exists.

func (*File) FindAddressSymbol

func (f *File) FindAddressSymbol(addr uint64) (string, error)

func (*File) FindSymbolAddress

func (f *File) FindSymbolAddress(symbol string) (uint64, error)

func (*File) ImportedLibraries

func (f *File) ImportedLibraries() ([]string, error)

ImportedLibraries returns the paths of all libraries referred to by the binary f that are expected to be linked with the binary at dynamic link time.

func (*File) ImportedSymbols

func (f *File) ImportedSymbols() ([]string, error)

ImportedSymbols returns the names of all symbols referred to by the binary f that are expected to be satisfied by other libraries at dynamic load time.

func (*File) Section

func (f *File) Section(name string) *Section

Section returns the first section with the given name, or nil if no such section exists.

func (*File) SectionByName added in v1.0.2

func (f *File) SectionByName(seg, sect string) *Section

func (*File) Segment

func (f *File) Segment(name string) *Segment

Segment returns the first Segment with the given name, or nil if no such segment exists.

func (*File) SegmentByName added in v1.0.2

func (f *File) SegmentByName(segname string) *Segment

func (*File) Segments

func (f *File) Segments() []*Segment

Segments returns all Segments.

func (*File) SourceVersion

func (f *File) SourceVersion() *SourceVersion

SourceVersion returns the source version load command, or nil if no source version exists.

func (*File) UUID

func (f *File) UUID() *UUID

UUID returns the UUID load command, or nil if no UUID exists.

type FileTOC

type FileTOC struct {
	types.FileHeader
	ByteOrder binary.ByteOrder
	Loads     []Load
	Sections  sections
}

func (*FileTOC) AddLoad

func (t *FileTOC) AddLoad(l Load)

func (*FileTOC) AddSection

func (t *FileTOC) AddSection(s *Section)

AddSection adds section to the most recently added Segment

func (*FileTOC) AddSegment

func (t *FileTOC) AddSegment(s *Segment)

AddSegment adds segment s to the file table of contents, and also zeroes out the segment information with the expectation that this will be added next.

func (*FileTOC) DerivedCopy

func (t *FileTOC) DerivedCopy(Type types.HeaderType, Flags types.HeaderFlag) *FileTOC

DerivedCopy returns a modified copy of the TOC, with empty loads and sections, and with the specified header type and flags.

func (*FileTOC) FileSize

func (t *FileTOC) FileSize() uint64

FileSize returns the size in bytes of the header, load commands, and the in-file contents of all the segments and sections included in those load commands, accounting for their offsets within the file.

func (*FileTOC) HdrSize

func (t *FileTOC) HdrSize() uint32

HdrSize returns the size in bytes of the Macho header for a given magic number (where the magic number has been appropriately byte-swapped).

func (*FileTOC) LoadAlign

func (t *FileTOC) LoadAlign() uint64

LoadAlign returns the required alignment of Load commands in a binary. This is used to add padding for necessary alignment.

func (*FileTOC) LoadSize

func (t *FileTOC) LoadSize() uint32

LoadSize returns the size of all the load commands in a file's table-of contents (but not their associated data, e.g., sections and symbol tables)

func (*FileTOC) Put

func (t *FileTOC) Put(buffer []byte) int

Put writes the header and all load commands to buffer, using the byte ordering specified in FileTOC t. For sections, this writes the headers that come in-line with the segment Load commands, but does not write the reference data for those sections.

func (*FileTOC) String

func (t *FileTOC) String() string

func (*FileTOC) SymbolSize

func (t *FileTOC) SymbolSize() uint32

SymbolSize returns the size in bytes of a Symbol (Nlist32 or Nlist64)

func (*FileTOC) TOCSize

func (t *FileTOC) TOCSize() uint32

TOCSize returns the size in bytes of the object file representation of the header and Load Commands (including Segments and Sections, but not their contents) at the beginning of a Mach-O file. This typically overlaps the text segment in the object file.

type FormatError

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

FormatError is returned by some operations if the data does not have the correct format for an object file.

func (*FormatError) Error

func (e *FormatError) Error() string

type FunctionStarts

type FunctionStarts struct {
	LoadBytes
	types.FunctionStartsCmd
	Offset uint32
	Size   uint32
}

A FunctionStarts represents a Mach-O function starts command.

type LinkEditData

type LinkEditData struct {
	LoadBytes
	types.LinkEditDataCmd
	Offset uint32
	Size   uint32
}

A LinkEditData represents a Mach-O linkedit data command.

type Load

type Load interface {
	Raw() []byte
	String() string
	Command() types.LoadCmd
	LoadSize(*FileTOC) uint32 // Need the TOC for alignment, sigh.
	Put([]byte, binary.ByteOrder) int
}

A Load represents any Mach-O load command.

type LoadBytes

type LoadBytes []byte

A LoadBytes is the uninterpreted bytes of a Mach-O load command.

func (LoadBytes) Copy

func (b LoadBytes) Copy() LoadBytes

func (LoadBytes) LoadSize

func (b LoadBytes) LoadSize(t *FileTOC) uint32

func (LoadBytes) Raw

func (b LoadBytes) Raw() []byte

func (LoadBytes) String

func (b LoadBytes) String() string

type LoadCmdBytes

type LoadCmdBytes struct {
	types.LoadCmd
	LoadBytes
}

LoadCmdBytes is a command-tagged sequence of bytes. This is used for Load Commands that are not (yet) interesting to us, and to common up this behavior for all those that are.

func (LoadCmdBytes) Copy

func (s LoadCmdBytes) Copy() LoadCmdBytes

func (LoadCmdBytes) String

func (s LoadCmdBytes) String() string

type ReExportDylib

type ReExportDylib Dylib

type Regs386

type Regs386 struct {
	AX    uint32
	BX    uint32
	CX    uint32
	DX    uint32
	DI    uint32
	SI    uint32
	BP    uint32
	SP    uint32
	SS    uint32
	FLAGS uint32
	IP    uint32
	CS    uint32
	DS    uint32
	ES    uint32
	FS    uint32
	GS    uint32
}

Regs386 is the Mach-O 386 register structure.

type RegsAMD64

type RegsAMD64 struct {
	AX    uint64
	BX    uint64
	CX    uint64
	DX    uint64
	DI    uint64
	SI    uint64
	BP    uint64
	SP    uint64
	R8    uint64
	R9    uint64
	R10   uint64
	R11   uint64
	R12   uint64
	R13   uint64
	R14   uint64
	R15   uint64
	IP    uint64
	FLAGS uint64
	CS    uint64
	FS    uint64
	GS    uint64
}

RegsAMD64 is the Mach-O AMD64 register structure.

type RegsARM

type RegsARM struct {
	R0   uint32
	R1   uint32
	R2   uint32
	R3   uint32
	R4   uint32
	R5   uint32
	R6   uint32
	R7   uint32
	R8   uint32
	R9   uint32
	R10  uint32
	R11  uint32
	R12  uint32
	SP   uint32
	LR   uint32
	PC   uint32
	CPSR uint32
}

RegsARM is the Mach-O ARM register structure.

type RegsARM64

type RegsARM64 struct {
	X0   uint64
	X1   uint64
	X2   uint64
	X3   uint64
	X4   uint64
	X5   uint64
	X6   uint64
	X7   uint64
	X8   uint64
	X9   uint64
	X10  uint64
	X11  uint64
	X12  uint64
	X13  uint64
	X14  uint64
	X15  uint64
	X16  uint64
	X17  uint64
	X18  uint64
	X19  uint64
	X20  uint64
	X21  uint64
	X22  uint64
	X23  uint64
	X24  uint64
	X25  uint64
	X26  uint64
	X27  uint64
	X28  uint64
	FP   uint64
	LR   uint64
	SP   uint64
	PC   uint64
	CPSR uint32
	PAD  uint32
}

RegsARM64 is the Mach-O ARM 64 register structure.

type Reloc

type Reloc struct {
	Addr  uint32
	Value uint32
	// when Scattered == false && Extern == true, Value is the symbol number.
	// when Scattered == false && Extern == false, Value is the section number.
	// when Scattered == true, Value is the value that this reloc refers to.
	Type      uint8
	Len       uint8 // 0=byte, 1=word, 2=long, 3=quad
	Pcrel     bool
	Extern    bool // valid if Scattered == false
	Scattered bool
}

A Reloc represents a Mach-O relocation.

type Routines64

type Routines64 struct {
	LoadBytes
	types.Routines64Cmd
	InitAddress uint64
	InitModule  uint64
}

type Rpath

type Rpath struct {
	LoadBytes
	types.RpathCmd
	Path string
}

A Rpath represents a Mach-O rpath command.

type Section

type Section struct {
	SectionHeader
	Relocs []Reloc

	// Embed ReaderAt for ReadAt method.
	// Do not embed SectionReader directly
	// to avoid having Read and Seek.
	// If a client wants Read and Seek it must use
	// Open() to avoid fighting over the seek offset
	// with other clients.
	io.ReaderAt
	// contains filtered or unexported fields
}

func (*Section) Copy

func (s *Section) Copy() *Section

func (*Section) Data

func (s *Section) Data() ([]byte, error)

Data reads and returns the contents of the Mach-O section.

func (*Section) Open

func (s *Section) Open() io.ReadSeeker

Open returns a new ReadSeeker reading the Mach-O section.

func (*Section) Put32

func (s *Section) Put32(b []byte, o binary.ByteOrder) int

func (*Section) Put64

func (s *Section) Put64(b []byte, o binary.ByteOrder) int

func (*Section) PutData

func (s *Section) PutData(b []byte)

func (*Section) PutRelocs

func (s *Section) PutRelocs(b []byte, o binary.ByteOrder) int

func (*Section) PutUncompressedData

func (s *Section) PutUncompressedData(b []byte)

func (*Section) UncompressedSize

func (s *Section) UncompressedSize() uint64

type SectionHeader

type SectionHeader struct {
	Name      string
	Seg       string
	Addr      uint64
	Size      uint64
	Offset    uint32
	Align     uint32
	Reloff    uint32
	Nreloc    uint32
	Flags     types.SectionFlag
	Reserved1 uint32
	Reserved2 uint32
	Reserved3 uint32 // only present if original was 64-bit
}

type Segment

type Segment struct {
	SegmentHeader
	LoadBytes
	// Embed ReaderAt for ReadAt method.
	// Do not embed SectionReader directly
	// to avoid having Read and Seek.
	// If a client wants Read and Seek it must use
	// Open() to avoid fighting over the seek offset
	// with other clients.
	io.ReaderAt
	// contains filtered or unexported fields
}

A Segment represents a Mach-O 32-bit or 64-bit load segment command.

func (*Segment) Copy

func (s *Segment) Copy() *Segment

func (*Segment) CopyZeroed

func (s *Segment) CopyZeroed() *Segment

func (*Segment) Data

func (s *Segment) Data() ([]byte, error)

Data reads and returns the contents of the segment.

func (*Segment) LoadSize

func (s *Segment) LoadSize(t *FileTOC) uint32

func (*Segment) Open

func (s *Segment) Open() io.ReadSeeker

Open returns a new ReadSeeker reading the segment.

func (*Segment) Put32

func (s *Segment) Put32(b []byte, o binary.ByteOrder) int

func (*Segment) Put64

func (s *Segment) Put64(b []byte, o binary.ByteOrder) int

func (*Segment) String

func (s *Segment) String() string

func (*Segment) UncompressedSize

func (s *Segment) UncompressedSize(t *FileTOC, align uint64) uint64

UncompressedSize returns the size of the segment with its sections uncompressed, ignoring its offset within the file. The returned size is rounded up to the power of two in align.

type SegmentHeader

type SegmentHeader struct {
	types.LoadCmd
	Len       uint32
	Name      string
	Addr      uint64
	Memsz     uint64
	Offset    uint64
	Filesz    uint64
	Maxprot   types.VmProtection
	Prot      types.VmProtection
	Nsect     uint32
	Flag      types.SegFlag
	Firstsect uint32
}

A SegmentHeader is the header for a Mach-O 32-bit or 64-bit load segment command.

func (*SegmentHeader) String

func (s *SegmentHeader) String() string

type SourceVersion

type SourceVersion struct {
	LoadBytes
	types.SourceVersionCmd
	Version string
}

A SourceVersion represents a Mach-O source version.

type SplitInfo

type SplitInfo struct {
	LoadBytes
	types.SegmentSplitInfoCmd
	Offset uint32
	Size   uint32
}

type SubClient

type SubClient struct {
	LoadBytes
	types.SubClientCmd
	Name string
}

A SubClient is a Mach-O dynamic sub client command.

type SubFramework

type SubFramework struct {
	LoadBytes
	types.SubFrameworkCmd
	Framework string
}

type Symbol

type Symbol struct {
	Name  string
	Type  types.NLType
	Sect  uint8
	Desc  uint16
	Value uint64
}

A Symbol is a Mach-O 32-bit or 64-bit symbol table entry.

type Symtab

type Symtab struct {
	LoadBytes
	types.SymtabCmd
	Syms []Symbol
}

A Symtab represents a Mach-O symbol table command.

func (*Symtab) Copy

func (s *Symtab) Copy() *Symtab

func (*Symtab) LoadSize

func (s *Symtab) LoadSize(t *FileTOC) uint32

func (*Symtab) Put

func (s *Symtab) Put(b []byte, o binary.ByteOrder) int

func (*Symtab) String

func (s *Symtab) String() string

type UUID

type UUID struct {
	LoadBytes
	types.UUIDCmd
	ID string
}

UUID represents a Mach-O uuid command.

func (*UUID) Copy

func (s *UUID) Copy() *UUID

func (*UUID) LoadSize

func (s *UUID) LoadSize(t *FileTOC) uint32

func (*UUID) Put

func (s *UUID) Put(b []byte, o binary.ByteOrder) int

func (*UUID) String

func (s *UUID) String() string

type UnixThread

type UnixThread struct {
	LoadBytes
	types.UnixThreadCmd
}

A UnixThread represents a Mach-O unix thread command.

type UpwardDylib

type UpwardDylib Dylib

A UpwardDylib represents a Mach-O load upward dylib command.

type VersionMinIphoneos

type VersionMinIphoneos struct {
	LoadBytes
	types.VersionMinIPhoneOSCmd
	Version string
	Sdk     string
}

type VersionMinMacosx

type VersionMinMacosx struct {
	LoadBytes
	types.VersionMinMacOSCmd
	Version string
	Sdk     string
}

type WeakDylib

type WeakDylib Dylib

A WeakDylib represents a Mach-O load weak dynamic library command.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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