macho

package
v0.0.0-...-26db732 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: BSD-3-Clause Imports: 13 Imported by: 8

README

More Info

More info on the Mach-O file structure here: Mach-O Deep Dive

Documentation

Overview

Package macho implements access to Mach-O object files.

Index

Constants

View Source
const (
	Magic32  uint32 = 0xfeedface
	Magic64  uint32 = 0xfeedfacf
	MagicFat uint32 = 0xcafebabe
)
View Source
const (
	FlagNoUndefs              uint32 = 0x1
	FlagIncrLink              uint32 = 0x2
	FlagDyldLink              uint32 = 0x4
	FlagBindAtLoad            uint32 = 0x8
	FlagPrebound              uint32 = 0x10
	FlagSplitSegs             uint32 = 0x20
	FlagLazyInit              uint32 = 0x40
	FlagTwoLevel              uint32 = 0x80
	FlagForceFlat             uint32 = 0x100
	FlagNoMultiDefs           uint32 = 0x200
	FlagNoFixPrebinding       uint32 = 0x400
	FlagPrebindable           uint32 = 0x800
	FlagAllModsBound          uint32 = 0x1000
	FlagSubsectionsViaSymbols uint32 = 0x2000
	FlagCanonical             uint32 = 0x4000
	FlagWeakDefines           uint32 = 0x8000
	FlagBindsToWeak           uint32 = 0x10000
	FlagAllowStackExecution   uint32 = 0x20000
	FlagRootSafe              uint32 = 0x40000
	FlagSetuidSafe            uint32 = 0x80000
	FlagNoReexportedDylibs    uint32 = 0x100000
	FlagPIE                   uint32 = 0x200000
	FlagDeadStrippableDylib   uint32 = 0x400000
	FlagHasTLVDescriptors     uint32 = 0x800000
	FlagNoHeapExecution       uint32 = 0x1000000
	FlagAppExtensionSafe      uint32 = 0x2000000
)
View Source
const N_EXT = 0x01
View Source
const N_PEXT = 0x10
View Source
const N_SECT = 0x0E

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.

View Source
var FinalSegEnd uint64

Functions

This section is empty.

Types

type Cpu

type Cpu uint32

A Cpu is a Mach-O cpu type.

const (
	Cpu386   Cpu = 7
	CpuAmd64 Cpu = Cpu386 | cpuArch64
	CpuArm   Cpu = 12
	CpuArm64 Cpu = CpuArm | cpuArch64
	CpuPpc   Cpu = 18
	CpuPpc64 Cpu = CpuPpc | cpuArch64
)

func (Cpu) GoString

func (i Cpu) GoString() string

func (Cpu) String

func (i Cpu) String() string

type DataInCode

type DataInCode struct {
	Len    uint32
	Offset uint64
	RawDat []byte
}

type DataInCodeCmd

type DataInCodeCmd struct {
	Cmd      LoadCmd
	Len      uint32
	Dataoff  uint32
	Datasize uint32
}

A DataInCodeCmd is a Mach-O load for Data In Code command

type Dylib

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

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

type DylibCmd

type DylibCmd struct {
	Cmd            LoadCmd
	Len            uint32
	Name           uint32
	Time           uint32
	CurrentVersion uint32
	CompatVersion  uint32
}

A DylibCmd is a Mach-O load dynamic library command.

type DylinkInfo

type DylinkInfo struct {
	RebaseLen         uint32
	RebaseOffset      uint64
	RebaseDat         []byte
	BindingInfoLen    uint32
	BindingInfoOffset uint64
	BindingInfoDat    []byte
	WeakBindingLen    uint32
	WeakBindingOffset uint64
	WeakBindingDat    []byte
	LazyBindingLen    uint32
	LazyBindingOffset uint64
	LazyBindingDat    []byte
	ExportInfoLen     uint32
	ExportInfoOffset  uint64
	ExportInfoDat     []byte
}

type DylinkInfoCmd

type DylinkInfoCmd struct {
	Cmd             LoadCmd
	Len             uint32
	Rebaseoff       uint32
	Rebasesize      uint32
	Bindinginfooff  uint32
	Bindinginfosize uint32
	Weakbindingoff  uint32
	Weakbindingsize uint32
	Lazybindingoff  uint32
	Lazybindingsize uint32
	Exportinfooff   uint32
	Exportinfosize  uint32
}

A DylinkInfoCmd is a Mach-O load for Dynamic Linker Info Only Command

type Dylinker

type Dylinker struct {
	LoadBytes
	Name string
}

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

type DylinkerCmd

type DylinkerCmd struct {
	Cmd  LoadCmd
	Len  uint32
	Name uint32
}

DylinkerCmd is a load command to identify the dynamic linker

type Dysymtab

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

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

type DysymtabCmd

type DysymtabCmd struct {
	Cmd            LoadCmd
	Len            uint32
	Ilocalsym      uint32
	Nlocalsym      uint32
	Iextdefsym     uint32
	Nextdefsym     uint32
	Iundefsym      uint32
	Nundefsym      uint32
	Tocoffset      uint32
	Ntoc           uint32
	Modtaboff      uint32
	Nmodtab        uint32
	Extrefsymoff   uint32
	Nextrefsyms    uint32
	Indirectsymoff uint32
	Nindirectsyms  uint32
	Extreloff      uint32
	Nextrel        uint32
	Locreloff      uint32
	Nlocrel        uint32
}

A DysymtabCmd is a Mach-O dynamic symbol table command.

type EntryPointCmd

type EntryPointCmd struct {
	Cmd       uint32 /* LC_MAIN only used in MH_EXECUTE filetypes */
	CmdSize   uint32 /* 24 */
	EntryOff  uint64 /* file (__TEXT) offset of main() */
	StackSize uint64 /* if not zero, initial stack size */
}

A EntryPointCmd is a Mach-O entry point command.

type Export

type Export struct {
	//Ordinal        uint32 // no ordinals for Mach-O
	Name           string
	VirtualAddress uint64
}

Export - describes a single export entry (similar to PE version for future refactoring)

type FatArch

type FatArch struct {
	FatArchHeader
	*File
}

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

type FatArchHeader

type FatArchHeader struct {
	Cpu    Cpu
	SubCpu uint32
	Offset uint32
	Size   uint32
	Align  uint32
}

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

type FatFile

type FatFile struct {
	Magic  uint32
	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

func (*FatFile) WriteFatFile

func (FatyFile *FatFile) WriteFatFile(destFile string) error

WriteFatFile - Creates a new Fat file and multiple machos into it

type File

type File struct {
	FileHeader
	ByteOrder binary.ByteOrder
	Loads     []Load
	Sections  []*Section

	Symtab     *Symtab
	Dysymtab   *Dysymtab
	SigBlock   *SigBlock
	FuncStarts *FuncStarts
	DataInCode *DataInCode
	DylinkInfo *DylinkInfo

	EntryPoint uint64
	Insertion  []byte
	// contains filtered or unexported fields
}

A File represents an open Mach-O file.

func NewFile

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

NewFile creates a new macho.File for accessing a Mach-o binary file in an underlying reader.

func NewFileFromMemory

func NewFileFromMemory(r io.ReaderAt) (*File, error)

NewFileFromMemory creates a new macho.File for accessing a Mach-O binary in-memory image in an underlying reader.

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) Bytes

func (machoFile *File) Bytes() ([]byte, error)

Bytes - Returns the bytes of an assembled *macho.File

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) Exports

func (f *File) Exports() []Export

Exports - gets exports, including private exports

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) 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) WriteFile

func (machoFile *File) WriteFile(destFile string) error

WriteFile - Creates a new file and writes it using the Bytes func above

type FileHeader

type FileHeader struct {
	Magic  uint32
	Cpu    Cpu
	SubCpu uint32
	Type   Type
	Ncmd   uint32
	Cmdsz  uint32
	Flags  uint32
}

A FileHeader represents a Mach-O file header.

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 FuncStarts

type FuncStarts struct {
	Len    uint32
	Offset uint64
	RawDat []byte
}

type FuncStartsCmd

type FuncStartsCmd struct {
	Cmd      LoadCmd
	Len      uint32
	Dataoff  uint32
	Datasize uint32
}

A FuncStartsCmd is a Mach-O load Function Starts command

type Load

type Load interface {
	Raw() []byte
}

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) Raw

func (b LoadBytes) Raw() []byte

type LoadCmd

type LoadCmd uint32

A LoadCmd is a Mach-O load command.

const (
	LoadCmdSegment    LoadCmd = 0x1
	LoadCmdSymtab     LoadCmd = 0x2
	LoadCmdThread     LoadCmd = 0x4
	LoadCmdUnixThread LoadCmd = 0x5 // thread+stack
	LoadCmdDysymtab   LoadCmd = 0xb
	LoadCmdDylib      LoadCmd = 0xc // load dylib command
	LoadCmdDylinker   LoadCmd = 0xf // id dylinker command (not load dylinker command)
	LoadCmdSegment64  LoadCmd = 0x19
	LoadCmdSignature  LoadCmd = 0x1d
	LoadCmdFuncStarts LoadCmd = 0x26 // Function Starts
	LoadCmdDataInCode LoadCmd = 0x29 // Data In Code

	LoadReqDyld       LoadCmd = 0x80000000
	LoadCmdMain       LoadCmd = (0x28 | LoadReqDyld) // replacement for LC_UNIXTHREAD
	LoadCmdRpath      LoadCmd = 0x8000001c
	LoadCmdDylinkInfo LoadCmd = 0x80000022 // Dynamic Linker Info Only
)

func (LoadCmd) GoString

func (i LoadCmd) GoString() string

func (LoadCmd) String

func (i LoadCmd) String() string

type Nlist32

type Nlist32 struct {
	Name  uint32
	Type  uint8
	Sect  uint8
	Desc  uint16
	Value uint32
}

An Nlist32 is a Mach-O 32-bit symbol table entry.

type Nlist64

type Nlist64 struct {
	Name  uint32
	Type  uint8
	Sect  uint8
	Desc  uint16
	Value uint64
}

An Nlist64 is a Mach-O 64-bit symbol table entry.

type ProgFlag

type ProgFlag uint32

Prog.Flag

const (
	PF_NONE ProgFlag = 0x0 /* None */
	PF_R    ProgFlag = 0x1 /* Readable. */
	PF_W    ProgFlag = 0x2 /* Writable. */
	PF_X    ProgFlag = 0x4 /* Executable. */
)

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 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 RelocTypeARM

type RelocTypeARM int
const (
	ARM_RELOC_VANILLA        RelocTypeARM = 0
	ARM_RELOC_PAIR           RelocTypeARM = 1
	ARM_RELOC_SECTDIFF       RelocTypeARM = 2
	ARM_RELOC_LOCAL_SECTDIFF RelocTypeARM = 3
	ARM_RELOC_PB_LA_PTR      RelocTypeARM = 4
	ARM_RELOC_BR24           RelocTypeARM = 5
	ARM_THUMB_RELOC_BR22     RelocTypeARM = 6
	ARM_THUMB_32BIT_BRANCH   RelocTypeARM = 7
	ARM_RELOC_HALF           RelocTypeARM = 8
	ARM_RELOC_HALF_SECTDIFF  RelocTypeARM = 9
)

func (RelocTypeARM) GoString

func (r RelocTypeARM) GoString() string

func (RelocTypeARM) String

func (i RelocTypeARM) String() string

type RelocTypeARM64

type RelocTypeARM64 int
const (
	ARM64_RELOC_UNSIGNED            RelocTypeARM64 = 0
	ARM64_RELOC_SUBTRACTOR          RelocTypeARM64 = 1
	ARM64_RELOC_BRANCH26            RelocTypeARM64 = 2
	ARM64_RELOC_PAGE21              RelocTypeARM64 = 3
	ARM64_RELOC_PAGEOFF12           RelocTypeARM64 = 4
	ARM64_RELOC_GOT_LOAD_PAGE21     RelocTypeARM64 = 5
	ARM64_RELOC_GOT_LOAD_PAGEOFF12  RelocTypeARM64 = 6
	ARM64_RELOC_POINTER_TO_GOT      RelocTypeARM64 = 7
	ARM64_RELOC_TLVP_LOAD_PAGE21    RelocTypeARM64 = 8
	ARM64_RELOC_TLVP_LOAD_PAGEOFF12 RelocTypeARM64 = 9
	ARM64_RELOC_ADDEND              RelocTypeARM64 = 10
)

func (RelocTypeARM64) GoString

func (r RelocTypeARM64) GoString() string

func (RelocTypeARM64) String

func (i RelocTypeARM64) String() string

type RelocTypeGeneric

type RelocTypeGeneric int
const (
	GENERIC_RELOC_VANILLA        RelocTypeGeneric = 0
	GENERIC_RELOC_PAIR           RelocTypeGeneric = 1
	GENERIC_RELOC_SECTDIFF       RelocTypeGeneric = 2
	GENERIC_RELOC_PB_LA_PTR      RelocTypeGeneric = 3
	GENERIC_RELOC_LOCAL_SECTDIFF RelocTypeGeneric = 4
	GENERIC_RELOC_TLV            RelocTypeGeneric = 5
)

func (RelocTypeGeneric) GoString

func (r RelocTypeGeneric) GoString() string

func (RelocTypeGeneric) String

func (i RelocTypeGeneric) String() string

type RelocTypeX86_64

type RelocTypeX86_64 int
const (
	X86_64_RELOC_UNSIGNED   RelocTypeX86_64 = 0
	X86_64_RELOC_SIGNED     RelocTypeX86_64 = 1
	X86_64_RELOC_BRANCH     RelocTypeX86_64 = 2
	X86_64_RELOC_GOT_LOAD   RelocTypeX86_64 = 3
	X86_64_RELOC_GOT        RelocTypeX86_64 = 4
	X86_64_RELOC_SUBTRACTOR RelocTypeX86_64 = 5
	X86_64_RELOC_SIGNED_1   RelocTypeX86_64 = 6
	X86_64_RELOC_SIGNED_2   RelocTypeX86_64 = 7
	X86_64_RELOC_SIGNED_4   RelocTypeX86_64 = 8
	X86_64_RELOC_TLV        RelocTypeX86_64 = 9
)

func (RelocTypeX86_64) GoString

func (r RelocTypeX86_64) GoString() string

func (RelocTypeX86_64) String

func (i RelocTypeX86_64) String() string

type Rpath

type Rpath struct {
	LoadBytes
	Path string
}

A Rpath represents a Mach-O rpath command.

type RpathCmd

type RpathCmd struct {
	Cmd  LoadCmd
	Len  uint32
	Path uint32
}

A RpathCmd is 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) 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.

type Section32

type Section32 struct {
	Name     [16]byte
	Seg      [16]byte
	Addr     uint32
	Size     uint32
	Offset   uint32
	Align    uint32
	Reloff   uint32
	Nreloc   uint32
	Flags    uint32
	Reserve1 uint32
	Reserve2 uint32
}

A Section32 is a 32-bit Mach-O section header.

type Section64

type Section64 struct {
	Name     [16]byte
	Seg      [16]byte
	Addr     uint64
	Size     uint64
	Offset   uint32
	Align    uint32
	Reloff   uint32
	Nreloc   uint32
	Flags    uint32
	Reserve1 uint32
	Reserve2 uint32
	Reserve3 uint32
}

A Section64 is a 64-bit Mach-O section header.

type SectionHeader

type SectionHeader struct {
	Name   string
	Seg    string
	Addr   uint64
	Size   uint64
	Offset uint32
	Align  uint32
	Reloff uint32
	Nreloc uint32
	Flags  uint32
}

type Segment

type Segment struct {
	LoadBytes
	SegmentHeader

	// 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) Data

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

Data reads and returns the contents of the segment.

func (*Segment) Open

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

Open returns a new ReadSeeker reading the segment.

type Segment32

type Segment32 struct {
	Cmd     LoadCmd
	Len     uint32
	Name    [16]byte
	Addr    uint32
	Memsz   uint32
	Offset  uint32
	Filesz  uint32
	Maxprot uint32 // ProgFlag - Maximum Segment Virtual Memory Protections
	Prot    uint32 // ProgFlag - Initial Segment Virtual Memory Protections
	Nsect   uint32
	Flag    uint32
}

A Segment32 is a 32-bit Mach-O segment load command.

type Segment64

type Segment64 struct {
	Cmd     LoadCmd
	Len     uint32
	Name    [16]byte
	Addr    uint64
	Memsz   uint64
	Offset  uint64
	Filesz  uint64
	Maxprot uint32
	Prot    uint32
	Nsect   uint32
	Flag    uint32
}

A Segment64 is a 64-bit Mach-O segment load command.

type SegmentHeader

type SegmentHeader struct {
	Cmd     LoadCmd
	Len     uint32
	Name    string
	Addr    uint64
	Memsz   uint64
	Offset  uint64
	Filesz  uint64
	Maxprot uint32
	Prot    uint32
	Nsect   uint32
	Flag    uint32
}

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

type SigBlock

type SigBlock struct {
	Len    uint32
	Offset uint64
	RawDat []byte
}

type SigBlockCmd

type SigBlockCmd struct {
	Cmd     LoadCmd
	Len     uint32
	Sigoff  uint32
	Sigsize uint32
}

A SigBlockCmd is a Mach-O symbol table command.

type Symbol

type Symbol struct {
	Name  string
	Type  uint8
	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
	SymtabCmd
	Syms         []Symbol
	RawSymtab    []byte
	RawStringtab []byte
}

A Symtab represents a Mach-O symbol table command.

type SymtabCmd

type SymtabCmd struct {
	Cmd     LoadCmd
	Len     uint32
	Symoff  uint32
	Nsyms   uint32
	Stroff  uint32
	Strsize uint32
}

A SymtabCmd is a Mach-O symbol table command.

type Thread

type Thread struct {
	Cmd  LoadCmd
	Len  uint32
	Type uint32
	Data []uint32
}

A Thread is a Mach-O thread state command.

type Type

type Type uint32

A Type is the Mach-O file type, e.g. an object file, executable, or dynamic library.

const (
	TypeObj    Type = 1
	TypeExec   Type = 2
	TypeDylib  Type = 6
	TypeBundle Type = 8
)

func (Type) GoString

func (t Type) GoString() string

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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