version: 1.10

package macho

import "debug/macho"

Overview

Package macho implements access to Mach-O object files.

Index

Package files

fat.go file.go macho.go reloctype.go reloctype_string.go

Constants

  1. const (
  2. Magic32 uint32 = 0xfeedface
  3. Magic64 uint32 = 0xfeedfacf
  4. MagicFat uint32 = 0xcafebabe
  5. )
  1. const (
  2. FlagNoUndefs uint32 = 0x1
  3. FlagIncrLink uint32 = 0x2
  4. FlagDyldLink uint32 = 0x4
  5. FlagBindAtLoad uint32 = 0x8
  6. FlagPrebound uint32 = 0x10
  7. FlagSplitSegs uint32 = 0x20
  8. FlagLazyInit uint32 = 0x40
  9. FlagTwoLevel uint32 = 0x80
  10. FlagForceFlat uint32 = 0x100
  11. FlagNoMultiDefs uint32 = 0x200
  12. FlagNoFixPrebinding uint32 = 0x400
  13. FlagPrebindable uint32 = 0x800
  14. FlagAllModsBound uint32 = 0x1000
  15. FlagSubsectionsViaSymbols uint32 = 0x2000
  16. FlagCanonical uint32 = 0x4000
  17. FlagWeakDefines uint32 = 0x8000
  18. FlagBindsToWeak uint32 = 0x10000
  19. FlagAllowStackExecution uint32 = 0x20000
  20. FlagRootSafe uint32 = 0x40000
  21. FlagSetuidSafe uint32 = 0x80000
  22. FlagNoReexportedDylibs uint32 = 0x100000
  23. FlagPIE uint32 = 0x200000
  24. FlagDeadStrippableDylib uint32 = 0x400000
  25. FlagHasTLVDescriptors uint32 = 0x800000
  26. FlagNoHeapExecution uint32 = 0x1000000
  27. FlagAppExtensionSafe uint32 = 0x2000000
  28. )

Variables

  1. 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.

type Cpu

  1. type Cpu uint32

A Cpu is a Mach-O cpu type.

  1. const (
  2. Cpu386 Cpu = 7
  3. CpuAmd64 Cpu = Cpu386 | cpuArch64
  4. CpuArm Cpu = 12
  5. CpuPpc Cpu = 18
  6. CpuPpc64 Cpu = CpuPpc | cpuArch64
  7. )

func (Cpu) GoString

  1. func (i Cpu) GoString() string

func (Cpu) String

  1. func (i Cpu) String() string

type Dylib

  1. type Dylib struct {
  2. LoadBytes
  3. Name string
  4. Time uint32
  5. CurrentVersion uint32
  6. CompatVersion uint32
  7. }

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

type DylibCmd

  1. type DylibCmd struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Name uint32
  5. Time uint32
  6. CurrentVersion uint32
  7. CompatVersion uint32
  8. }

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

type Dysymtab

  1. type Dysymtab struct {
  2. LoadBytes
  3. DysymtabCmd
  4. IndirectSyms []uint32 // indices into Symtab.Syms
  5. }

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

type DysymtabCmd

  1. type DysymtabCmd struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Ilocalsym uint32
  5. Nlocalsym uint32
  6. Iextdefsym uint32
  7. Nextdefsym uint32
  8. Iundefsym uint32
  9. Nundefsym uint32
  10. Tocoffset uint32
  11. Ntoc uint32
  12. Modtaboff uint32
  13. Nmodtab uint32
  14. Extrefsymoff uint32
  15. Nextrefsyms uint32
  16. Indirectsymoff uint32
  17. Nindirectsyms uint32
  18. Extreloff uint32
  19. Nextrel uint32
  20. Locreloff uint32
  21. Nlocrel uint32
  22. }

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

type FatArch

  1. type FatArch struct {
  2. FatArchHeader
  3. *File
  4. }

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

type FatArchHeader

  1. type FatArchHeader struct {
  2. Cpu Cpu
  3. SubCpu uint32
  4. Offset uint32
  5. Size uint32
  6. Align uint32
  7. }

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

type FatFile

  1. type FatFile struct {
  2. Magic uint32
  3. Arches []FatArch
  4. // contains filtered or unexported fields
  5. }

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

func NewFatFile

  1. 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

  1. 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

  1. func (ff *FatFile) Close() error

type File

  1. type File struct {
  2. FileHeader
  3. ByteOrder binary.ByteOrder
  4. Loads []Load
  5. Sections []*Section
  6.  
  7. Symtab *Symtab
  8. Dysymtab *Dysymtab
  9. // contains filtered or unexported fields
  10. }

A File represents an open Mach-O file.

func NewFile

  1. func NewFile(r io.ReaderAt) (*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

  1. 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) Close

  1. 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

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

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

func (*File) ImportedLibraries

  1. 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

  1. 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

  1. 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

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

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

type FileHeader

  1. type FileHeader struct {
  2. Magic uint32
  3. Cpu Cpu
  4. SubCpu uint32
  5. Type Type
  6. Ncmd uint32
  7. Cmdsz uint32
  8. Flags uint32
  9. }

A FileHeader represents a Mach-O file header.

type FormatError

  1. type FormatError struct {
  2. // contains filtered or unexported fields
  3. }

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

func (*FormatError) Error

  1. func (e *FormatError) Error() string

type Load

  1. type Load interface {
  2. Raw() []byte
  3. }

A Load represents any Mach-O load command.

type LoadBytes

  1. type LoadBytes []byte

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

func (LoadBytes) Raw

  1. func (b LoadBytes) Raw() []byte

type LoadCmd

  1. type LoadCmd uint32

A LoadCmd is a Mach-O load command.

  1. const (
  2. LoadCmdSegment LoadCmd = 0x1
  3. LoadCmdSymtab LoadCmd = 0x2
  4. LoadCmdThread LoadCmd = 0x4
  5. LoadCmdUnixThread LoadCmd = 0x5 // thread+stack
  6. LoadCmdDysymtab LoadCmd = 0xb
  7. LoadCmdDylib LoadCmd = 0xc // load dylib command
  8. LoadCmdDylinker LoadCmd = 0xf // id dylinker command (not load dylinker command)
  9. LoadCmdSegment64 LoadCmd = 0x19
  10. LoadCmdRpath LoadCmd = 0x8000001c
  11. )

func (LoadCmd) GoString

  1. func (i LoadCmd) GoString() string

func (LoadCmd) String

  1. func (i LoadCmd) String() string

type Nlist32

  1. type Nlist32 struct {
  2. Name uint32
  3. Type uint8
  4. Sect uint8
  5. Desc uint16
  6. Value uint32
  7. }

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

type Nlist64

  1. type Nlist64 struct {
  2. Name uint32
  3. Type uint8
  4. Sect uint8
  5. Desc uint16
  6. Value uint64
  7. }

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

type Regs386

  1. type Regs386 struct {
  2. AX uint32
  3. BX uint32
  4. CX uint32
  5. DX uint32
  6. DI uint32
  7. SI uint32
  8. BP uint32
  9. SP uint32
  10. SS uint32
  11. FLAGS uint32
  12. IP uint32
  13. CS uint32
  14. DS uint32
  15. ES uint32
  16. FS uint32
  17. GS uint32
  18. }

Regs386 is the Mach-O 386 register structure.

type RegsAMD64

  1. type RegsAMD64 struct {
  2. AX uint64
  3. BX uint64
  4. CX uint64
  5. DX uint64
  6. DI uint64
  7. SI uint64
  8. BP uint64
  9. SP uint64
  10. R8 uint64
  11. R9 uint64
  12. R10 uint64
  13. R11 uint64
  14. R12 uint64
  15. R13 uint64
  16. R14 uint64
  17. R15 uint64
  18. IP uint64
  19. FLAGS uint64
  20. CS uint64
  21. FS uint64
  22. GS uint64
  23. }

RegsAMD64 is the Mach-O AMD64 register structure.

type Reloc

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

A Reloc represents a Mach-O relocation.

type RelocTypeARM

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

func (RelocTypeARM) GoString

  1. func (r RelocTypeARM) GoString() string

func (RelocTypeARM) String

  1. func (i RelocTypeARM) String() string

type RelocTypeARM64

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

func (RelocTypeARM64) GoString

  1. func (r RelocTypeARM64) GoString() string

func (RelocTypeARM64) String

  1. func (i RelocTypeARM64) String() string

type RelocTypeGeneric

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

func (RelocTypeGeneric) GoString

  1. func (r RelocTypeGeneric) GoString() string

func (RelocTypeGeneric) String

  1. func (i RelocTypeGeneric) String() string

type RelocTypeX86_64

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

func (RelocTypeX86_64) GoString

  1. func (r RelocTypeX86_64) GoString() string

func (RelocTypeX86_64) String

  1. func (i RelocTypeX86_64) String() string

type Rpath

  1. type Rpath struct {
  2. LoadBytes
  3. Path string
  4. }

A Rpath represents a Mach-O rpath command.

type RpathCmd

  1. type RpathCmd struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Path uint32
  5. }

A RpathCmd is a Mach-O rpath command.

type Section

  1. type Section struct {
  2. SectionHeader
  3. Relocs []Reloc
  4.  
  5. // Embed ReaderAt for ReadAt method.
  6. // Do not embed SectionReader directly
  7. // to avoid having Read and Seek.
  8. // If a client wants Read and Seek it must use
  9. // Open() to avoid fighting over the seek offset
  10. // with other clients.
  11. io.ReaderAt
  12. // contains filtered or unexported fields
  13. }

func (*Section) Data

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

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

func (*Section) Open

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

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

type Section32

  1. type Section32 struct {
  2. Name [16]byte
  3. Seg [16]byte
  4. Addr uint32
  5. Size uint32
  6. Offset uint32
  7. Align uint32
  8. Reloff uint32
  9. Nreloc uint32
  10. Flags uint32
  11. Reserve1 uint32
  12. Reserve2 uint32
  13. }

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

type Section64

  1. type Section64 struct {
  2. Name [16]byte
  3. Seg [16]byte
  4. Addr uint64
  5. Size uint64
  6. Offset uint32
  7. Align uint32
  8. Reloff uint32
  9. Nreloc uint32
  10. Flags uint32
  11. Reserve1 uint32
  12. Reserve2 uint32
  13. Reserve3 uint32
  14. }

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

type SectionHeader

  1. type SectionHeader struct {
  2. Name string
  3. Seg string
  4. Addr uint64
  5. Size uint64
  6. Offset uint32
  7. Align uint32
  8. Reloff uint32
  9. Nreloc uint32
  10. Flags uint32
  11. }

type Segment

  1. type Segment struct {
  2. LoadBytes
  3. SegmentHeader
  4.  
  5. // Embed ReaderAt for ReadAt method.
  6. // Do not embed SectionReader directly
  7. // to avoid having Read and Seek.
  8. // If a client wants Read and Seek it must use
  9. // Open() to avoid fighting over the seek offset
  10. // with other clients.
  11. io.ReaderAt
  12. // contains filtered or unexported fields
  13. }

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

func (*Segment) Data

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

Data reads and returns the contents of the segment.

func (*Segment) Open

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

Open returns a new ReadSeeker reading the segment.

type Segment32

  1. type Segment32 struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Name [16]byte
  5. Addr uint32
  6. Memsz uint32
  7. Offset uint32
  8. Filesz uint32
  9. Maxprot uint32
  10. Prot uint32
  11. Nsect uint32
  12. Flag uint32
  13. }

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

type Segment64

  1. type Segment64 struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Name [16]byte
  5. Addr uint64
  6. Memsz uint64
  7. Offset uint64
  8. Filesz uint64
  9. Maxprot uint32
  10. Prot uint32
  11. Nsect uint32
  12. Flag uint32
  13. }

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

type SegmentHeader

  1. type SegmentHeader struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Name string
  5. Addr uint64
  6. Memsz uint64
  7. Offset uint64
  8. Filesz uint64
  9. Maxprot uint32
  10. Prot uint32
  11. Nsect uint32
  12. Flag uint32
  13. }

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

type Symbol

  1. type Symbol struct {
  2. Name string
  3. Type uint8
  4. Sect uint8
  5. Desc uint16
  6. Value uint64
  7. }

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

type Symtab

  1. type Symtab struct {
  2. LoadBytes
  3. SymtabCmd
  4. Syms []Symbol
  5. }

A Symtab represents a Mach-O symbol table command.

type SymtabCmd

  1. type SymtabCmd struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Symoff uint32
  5. Nsyms uint32
  6. Stroff uint32
  7. Strsize uint32
  8. }

A SymtabCmd is a Mach-O symbol table command.

type Thread

  1. type Thread struct {
  2. Cmd LoadCmd
  3. Len uint32
  4. Type uint32
  5. Data []uint32
  6. }

A Thread is a Mach-O thread state command.

type Type

  1. type Type uint32

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

  1. const (
  2. TypeObj Type = 1
  3. TypeExec Type = 2
  4. TypeDylib Type = 6
  5. TypeBundle Type = 8
  6. )

func (Type) GoString

  1. func (t Type) GoString() string

func (Type) String

  1. func (t Type) String() string