首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

debug/macho

  • import "debug/macho"
  • 概述
  • 参数

概述

macho 可以访问 Mach-O 目标文件。

参数

  • 常量
  • 变量
  • type Cpu
  • func (i Cpu) GoString() string
  • func (i Cpu) String() string
  • type Dylib
  • type DylibCmd
  • type Dysymtab
  • type DysymtabCmd
  • type FatArch
  • type FatArchHeader
  • type FatFile
  • func NewFatFile(r io.ReaderAt) (*FatFile, error)
  • func OpenFat(name string) (*FatFile, error)
  • func (ff *FatFile) Close() error
  • type File
  • func NewFile(r io.ReaderAt) (*File, error)
  • func Open(name string) (*File, error)
  • func (f *File) Close() error
  • func (f *File) DWARF() (*dwarf.Data, error)
  • func (f *File) ImportedLibraries() ([]string, error)
  • func (f *File) ImportedSymbols() ([]string, error)
  • func (f *File) Section(name string) *Section
  • func (f *File) Segment(name string) *Segment
  • type FileHeader
  • type FormatError
  • func (e *FormatError) Error() string
  • type Load
  • type LoadBytes
  • func (b LoadBytes) Raw() []byte
  • type LoadCmd
  • func (i LoadCmd) GoString() string
  • func (i LoadCmd) String() string
  • type Nlist32
  • type Nlist64
  • type Regs386
  • type RegsAMD64
  • type Section
  • func (s *Section) Data() ([]byte, error)
  • func (s *Section) Open() io.ReadSeeker
  • type Section32
  • type Section64
  • type SectionHeader
  • type Segment
  • func (s *Segment) Data() ([]byte, error)
  • func (s *Segment) Open() io.ReadSeeker
  • type Segment32
  • type Segment64
  • type SegmentHeader
  • type Symbol
  • type Symtab
  • type SymtabCmd
  • type Thread
  • type Type

打包文件

常量

代码语言:javascript
复制
const (
        Magic32  uint32 = 0xfeedface
        Magic64  uint32 = 0xfeedfacf
        MagicFat uint32 = 0xcafebabe
)

变量

当文件不是通用二进制文件时,ErrNotFat 从 NewFatFile 或 OpenFat 返回,但可能是基于其幻数的精简二进制文件。

代码语言:javascript
复制
var ErrNotFat = &FormatError{0, "not a fat Mach-O file", nil}

Cpu 是一个 Mach-O CPU 类型。

代码语言:javascript
复制
type Cpu uint32
代码语言:javascript
复制
const (
        Cpu386   Cpu = 7
        CpuAmd64 Cpu = Cpu386 | cpuArch64
        CpuArm   Cpu = 12
        CpuPpc   Cpu = 18
        CpuPpc64 Cpu = CpuPpc | cpuArch64
)

func (Cpu) GoString(查看源代码)

代码语言:javascript
复制
func (i Cpu) GoString() string

func (Cpu) String(查看源代码)

代码语言:javascript
复制
func (i Cpu) String() string

Dylib 代表 Mach-O 加载动态库命令。

代码语言:javascript
复制
type Dylib struct {
        LoadBytes
        Name           string
        Time           uint32
        CurrentVersion uint32
        CompatVersion  uint32
}

DylibCmd 是一个 Mach-O 加载动态库命令。

代码语言:javascript
复制
type DylibCmd struct {
        Cmd            LoadCmd
        Len            uint32
        Name           uint32
        Time           uint32
        CurrentVersion uint32
        CompatVersion  uint32
}

Dysymtab 表示一个 Mach-O 动态符号表命令。

代码语言:txt
复制
type Dysymtab struct {
        LoadBytes
        DysymtabCmd
        IndirectSyms []uint32 // 索引到Symtab.Syms
}

type DysymtabCmd(查看源代码)

DysymtabCmd 是一个 Mach-O 动态符号表命令。

代码语言:javascript
复制
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
}

FatArch 是 FatFile 内的 Mach-O 文件。

代码语言:javascript
复制
type FatArch struct {
        FatArchHeader
        *File
}

type FatArchHeader(查看源代码)

FatArchHeader 表示特定图像体系结构的多头。

代码语言:javascript
复制
type FatArchHeader struct {
        Cpu    Cpu
        SubCpu uint32
        Offset uint32
        Size   uint32
        Align  uint32
}

FatFile 是一个包含至少一个体系结构的 Mach-O 通用二进制文件。

代码语言:txt
复制
type FatFile struct {
        Magic  uint32
        Arches []FatArch
        // 包含已过滤或未导出的字段
}

func NewFatFile(查看源代码)

代码语言:javascript
复制
func NewFatFile(r io.ReaderAt) (*FatFile, error)

NewFatFile 创建一个新的 FatFile,用于访问通用二进制文件中的所有 Mach-O 图像。Mach-O 二进制文件预计从 ReaderAt 的位置0开始。

代码语言:javascript
复制
func OpenFat(name string) (*FatFile, error)

OpenFat 使用 os.Open 打开指定的文件并准备将其用作 Mach-O 通用二进制文件。

func (*FatFile) Close(查看源代码)

代码语言:javascript
复制
func (ff *FatFile) Close() error

文件表示一个开放的 Mach-O 文件。

代码语言:txt
复制
type File struct {
        FileHeader
        ByteOrder binary.ByteOrder
        Loads     []Load
        Sections  []*Section

        Symtab   *Symtab
        Dysymtab *Dysymtab
        // 包含已过滤或未导出的字段
}
代码语言:javascript
复制
func NewFile(r io.ReaderAt) (*File, error)

NewFile 创建一个新的文件,用于访问底层阅读器中的 Mach-O 二进制文件。Mach-O 二进制文件预计从ReaderAt 的位置0开始。

代码语言:javascript
复制
func Open(name string) (*File, error)

打开使用 os.Open 打开命名文件,并准备将其用作 Mach-O 二进制文件。

func (*File) Close(查看源代码)

代码语言:javascript
复制
func (f *File) Close() error

关闭文件。如果文件是直接使用 NewFile 而不是 Open 来创建的,则 Close 不起作用。

func (*File) DWARF(查看源代码)

代码语言:javascript
复制
func (f *File) DWARF() (*dwarf.Data, error)

DWARF 返回 Mach-O 文件的 DWARF 调试信息。

func (*File) ImportedLibraries(查看源代码)

代码语言:javascript
复制
func (f *File) ImportedLibraries() ([]string, error)

ImportedLibraries 返回由二进制文件 f 引用的所有库的路径,这些库在动态链接时间期望与二进制链接。

func (*File) ImportedSymbols(查看源代码)

代码语言:javascript
复制
func (f *File) ImportedSymbols() ([]string, error)

ImportedSymbols 返回二进制文件f引用的所有符号的名称,动态加载时期望其他库会满足这些名称。

func (*File) Section(查看源代码)

代码语言:javascript
复制
func (f *File) Section(name string) *Section

Section 返回给定名称的第一部分,如果不存在这样的部分,则返回 nil。

func (*File) Segment(查看源代码)

代码语言:javascript
复制
func (f *File) Segment(name string) *Segment

Segment 会返回具有给定名称的第一个 Segment,如果不存在此 Segment,则为零。

type FileHeader(查看源代码)

FileHeader 表示一个 Mach-O 文件头。

代码语言:javascript
复制
type FileHeader struct {
        Magic  uint32
        Cpu    Cpu
        SubCpu uint32
        Type   Type
        Ncmd   uint32
        Cmdsz  uint32
        Flags  uint32
}

type FormatError(查看源代码)

如果数据的目标文件格式不正确,FormatError 会被某些操作返回。

代码语言:txt
复制
type FormatError struct {
        // 包含已过滤或未导出的字段
}

func (*FormatError) Error(查看源代码)

代码语言:javascript
复制
func (e *FormatError) Error() string

Load 表示任何 Mach-O 加载命令。

代码语言:javascript
复制
type Load interface {
        Raw() []byte
}

type LoadBytes(查看源代码)

LoadBytes 是 Mach-O 加载命令的未解释字节。

代码语言:javascript
复制
type LoadBytes []byte

func (LoadBytes) Raw(查看源代码)

代码语言:javascript
复制
func (b LoadBytes) Raw() []byte

LoadCmd 是一个 Mach-O 加载命令。

代码语言:javascript
复制
type LoadCmd uint32
代码语言:javascript
复制
const (
        LoadCmdSegment    LoadCmd = 1
        LoadCmdSymtab     LoadCmd = 2
        LoadCmdThread     LoadCmd = 4
        LoadCmdUnixThread LoadCmd = 5 // thread+stack
        LoadCmdDysymtab   LoadCmd = 11
        LoadCmdDylib      LoadCmd = 12
        LoadCmdDylinker   LoadCmd = 15
        LoadCmdSegment64  LoadCmd = 25
)

func (LoadCmd) GoString(查看源代码)

代码语言:javascript
复制
func (i LoadCmd) GoString() string

func (LoadCmd) String(查看源代码)

代码语言:javascript
复制
func (i LoadCmd) String() string

Nlist32 是 Mach-O 32 位符号表项。

代码语言:javascript
复制
type Nlist32 struct {
        Name  uint32
        Type  uint8
        Sect  uint8
        Desc  uint16
        Value uint32
}

Nlist64 是一个 Mach-O 64 位符号表项。

代码语言:javascript
复制
type Nlist64 struct {
        Name  uint32
        Type  uint8
        Sect  uint8
        Desc  uint16
        Value uint64
}

Regs386 是 Mach-O 386 寄存器结构。

代码语言:javascript
复制
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
}

type RegsAMD64(查看源代码)

RegsAMD64 是 Mach-O AMD64 寄存器结构。

代码语言:javascript
复制
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
}
代码语言:txt
复制
type Section struct {
        SectionHeader

        // 为ReadAt方法嵌入ReaderAt。
        // 不要直接嵌入SectionReader
        // 避免Read和Seek。
        // 如果客户想要阅读和寻求它必须使用
        // Open()以避免争夺搜索偏移量
        // 与其他客户。
        io.ReaderAt
        // 包含已过滤或未导出的字段
}

func (*Section) Data(查看源代码)

代码语言:javascript
复制
func (s *Section) Data() ([]byte, error)

数据读取并返回 Mach-O 部分的内容。

func (*Section) Open(查看源代码)

代码语言:javascript
复制
func (s *Section) Open() io.ReadSeeker

Open 返回读取 Mach-O 部分的新 ReadSeeker。

type Section32(查看源代码)

Section32 是一个32位 Mach-O 节头。

代码语言:javascript
复制
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
}

type Section64(查看源代码)

Section64 是一个64位 Mach-O 节头。

代码语言:javascript
复制
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
}

type SectionHeader(查看源代码)

代码语言:javascript
复制
type SectionHeader struct {
        Name   string
        Seg    string
        Addr   uint64
        Size   uint64
        Offset uint32
        Align  uint32
        Reloff uint32
        Nreloc uint32
        Flags  uint32
}

Segment 表示 Mach-O 32 位或64位加载段命令。

代码语言:txt
复制
type Segment struct {
        LoadBytes
        SegmentHeader

        // 为ReadAt方法嵌入ReaderAt。
        // 不要直接嵌入SectionReader
        // 避免Read阅读和Seek寻求。
        // 如果客户想要阅读和寻求它必须使用
        // Open()以避免争夺搜索偏移量
        // 与其他客户。
        io.ReaderAt
        // 包含已过滤或未导出的字段
}

func (*Segment) Data(查看源代码)

代码语言:javascript
复制
func (s *Segment) Data() ([]byte, error)

数据读取并返回段的内容。

func (*Segment) Open(查看源代码)

代码语言:javascript
复制
func (s *Segment) Open() io.ReadSeeker

Open 返回一个新的 ReadSeeker 读取该段。

type Segment32(查看源代码)

Segment32 是一个32位的 Mach-O segment

加载命令。

代码语言:javascript
复制
type Segment32 struct {
        Cmd     LoadCmd
        Len     uint32
        Name    [16]byte
        Addr    uint32
        Memsz   uint32
        Offset  uint32
        Filesz  uint32
        Maxprot uint32
        Prot    uint32
        Nsect   uint32
        Flag    uint32
}

type Segment64(查看源代码)

Segment64是一个64位的Mach-O段加载命令。

代码语言:javascript
复制
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
}

type SegmentHeader(查看源代码)

SegmentHeader 是 Mach-O 32 位或64位加载段命令的标头。

代码语言:javascript
复制
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
}

符号是一个 Mach-O 32 位或64位符号表条目。

代码语言:javascript
复制
type Symbol struct {
        Name  string
        Type  uint8
        Sect  uint8
        Desc  uint16
        Value uint64
}

Symtab 代表 Mach-O 符号表命令。

代码语言:javascript
复制
type Symtab struct {
        LoadBytes
        SymtabCmd
        Syms []Symbol
}

type SymtabCmd(查看源代码)

SymtabCmd 是一个 Mach-O符号表命令。

代码语言:javascript
复制
type SymtabCmd struct {
        Cmd     LoadCmd
        Len     uint32
        Symoff  uint32
        Nsyms   uint32
        Stroff  uint32
        Strsize uint32
}

线程是一个 Mach-O 线程状态命令。

代码语言:javascript
复制
type Thread struct {
        Cmd  LoadCmd
        Len  uint32
        Type uint32
        Data []uint32
}

Type 是 Mach-O 文件类型,例如目标文件,可执行文件或动态库。

代码语言:javascript
复制
type Type uint32
代码语言:javascript
复制
const (
        TypeObj    Type = 1
        TypeExec   Type = 2
        TypeDylib  Type = 6
        TypeBundle Type = 8
)

扫码关注腾讯云开发者

领取腾讯云代金券