...
Run Format

Source file src/debug/macho/macho.go

Documentation: debug/macho

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Mach-O header data structures
     6  // http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html
     7  
     8  package macho
     9  
    10  import "strconv"
    11  
    12  // A FileHeader represents a Mach-O file header.
    13  type FileHeader struct {
    14  	Magic  uint32
    15  	Cpu    Cpu
    16  	SubCpu uint32
    17  	Type   Type
    18  	Ncmd   uint32
    19  	Cmdsz  uint32
    20  	Flags  uint32
    21  }
    22  
    23  const (
    24  	fileHeaderSize32 = 7 * 4
    25  	fileHeaderSize64 = 8 * 4
    26  )
    27  
    28  const (
    29  	Magic32  uint32 = 0xfeedface
    30  	Magic64  uint32 = 0xfeedfacf
    31  	MagicFat uint32 = 0xcafebabe
    32  )
    33  
    34  // A Type is the Mach-O file type, e.g. an object file, executable, or dynamic library.
    35  type Type uint32
    36  
    37  const (
    38  	TypeObj    Type = 1
    39  	TypeExec   Type = 2
    40  	TypeDylib  Type = 6
    41  	TypeBundle Type = 8
    42  )
    43  
    44  var typeStrings = []intName{
    45  	{uint32(TypeObj), "Obj"},
    46  	{uint32(TypeExec), "Exec"},
    47  	{uint32(TypeDylib), "Dylib"},
    48  	{uint32(TypeBundle), "Bundle"},
    49  }
    50  
    51  func (t Type) String() string   { return stringName(uint32(t), typeStrings, false) }
    52  func (t Type) GoString() string { return stringName(uint32(t), typeStrings, true) }
    53  
    54  // A Cpu is a Mach-O cpu type.
    55  type Cpu uint32
    56  
    57  const cpuArch64 = 0x01000000
    58  
    59  const (
    60  	Cpu386   Cpu = 7
    61  	CpuAmd64 Cpu = Cpu386 | cpuArch64
    62  	CpuArm   Cpu = 12
    63  	CpuArm64 Cpu = CpuArm | cpuArch64
    64  	CpuPpc   Cpu = 18
    65  	CpuPpc64 Cpu = CpuPpc | cpuArch64
    66  )
    67  
    68  var cpuStrings = []intName{
    69  	{uint32(Cpu386), "Cpu386"},
    70  	{uint32(CpuAmd64), "CpuAmd64"},
    71  	{uint32(CpuArm), "CpuArm"},
    72  	{uint32(CpuArm64), "CpuArm64"},
    73  	{uint32(CpuPpc), "CpuPpc"},
    74  	{uint32(CpuPpc64), "CpuPpc64"},
    75  }
    76  
    77  func (i Cpu) String() string   { return stringName(uint32(i), cpuStrings, false) }
    78  func (i Cpu) GoString() string { return stringName(uint32(i), cpuStrings, true) }
    79  
    80  // A LoadCmd is a Mach-O load command.
    81  type LoadCmd uint32
    82  
    83  const (
    84  	LoadCmdSegment    LoadCmd = 0x1
    85  	LoadCmdSymtab     LoadCmd = 0x2
    86  	LoadCmdThread     LoadCmd = 0x4
    87  	LoadCmdUnixThread LoadCmd = 0x5 // thread+stack
    88  	LoadCmdDysymtab   LoadCmd = 0xb
    89  	LoadCmdDylib      LoadCmd = 0xc // load dylib command
    90  	LoadCmdDylinker   LoadCmd = 0xf // id dylinker command (not load dylinker command)
    91  	LoadCmdSegment64  LoadCmd = 0x19
    92  	LoadCmdRpath      LoadCmd = 0x8000001c
    93  )
    94  
    95  var cmdStrings = []intName{
    96  	{uint32(LoadCmdSegment), "LoadCmdSegment"},
    97  	{uint32(LoadCmdThread), "LoadCmdThread"},
    98  	{uint32(LoadCmdUnixThread), "LoadCmdUnixThread"},
    99  	{uint32(LoadCmdDylib), "LoadCmdDylib"},
   100  	{uint32(LoadCmdSegment64), "LoadCmdSegment64"},
   101  	{uint32(LoadCmdRpath), "LoadCmdRpath"},
   102  }
   103  
   104  func (i LoadCmd) String() string   { return stringName(uint32(i), cmdStrings, false) }
   105  func (i LoadCmd) GoString() string { return stringName(uint32(i), cmdStrings, true) }
   106  
   107  type (
   108  	// A Segment32 is a 32-bit Mach-O segment load command.
   109  	Segment32 struct {
   110  		Cmd     LoadCmd
   111  		Len     uint32
   112  		Name    [16]byte
   113  		Addr    uint32
   114  		Memsz   uint32
   115  		Offset  uint32
   116  		Filesz  uint32
   117  		Maxprot uint32
   118  		Prot    uint32
   119  		Nsect   uint32
   120  		Flag    uint32
   121  	}
   122  
   123  	// A Segment64 is a 64-bit Mach-O segment load command.
   124  	Segment64 struct {
   125  		Cmd     LoadCmd
   126  		Len     uint32
   127  		Name    [16]byte
   128  		Addr    uint64
   129  		Memsz   uint64
   130  		Offset  uint64
   131  		Filesz  uint64
   132  		Maxprot uint32
   133  		Prot    uint32
   134  		Nsect   uint32
   135  		Flag    uint32
   136  	}
   137  
   138  	// A SymtabCmd is a Mach-O symbol table command.
   139  	SymtabCmd struct {
   140  		Cmd     LoadCmd
   141  		Len     uint32
   142  		Symoff  uint32
   143  		Nsyms   uint32
   144  		Stroff  uint32
   145  		Strsize uint32
   146  	}
   147  
   148  	// A DysymtabCmd is a Mach-O dynamic symbol table command.
   149  	DysymtabCmd struct {
   150  		Cmd            LoadCmd
   151  		Len            uint32
   152  		Ilocalsym      uint32
   153  		Nlocalsym      uint32
   154  		Iextdefsym     uint32
   155  		Nextdefsym     uint32
   156  		Iundefsym      uint32
   157  		Nundefsym      uint32
   158  		Tocoffset      uint32
   159  		Ntoc           uint32
   160  		Modtaboff      uint32
   161  		Nmodtab        uint32
   162  		Extrefsymoff   uint32
   163  		Nextrefsyms    uint32
   164  		Indirectsymoff uint32
   165  		Nindirectsyms  uint32
   166  		Extreloff      uint32
   167  		Nextrel        uint32
   168  		Locreloff      uint32
   169  		Nlocrel        uint32
   170  	}
   171  
   172  	// A DylibCmd is a Mach-O load dynamic library command.
   173  	DylibCmd struct {
   174  		Cmd            LoadCmd
   175  		Len            uint32
   176  		Name           uint32
   177  		Time           uint32
   178  		CurrentVersion uint32
   179  		CompatVersion  uint32
   180  	}
   181  
   182  	// A RpathCmd is a Mach-O rpath command.
   183  	RpathCmd struct {
   184  		Cmd  LoadCmd
   185  		Len  uint32
   186  		Path uint32
   187  	}
   188  
   189  	// A Thread is a Mach-O thread state command.
   190  	Thread struct {
   191  		Cmd  LoadCmd
   192  		Len  uint32
   193  		Type uint32
   194  		Data []uint32
   195  	}
   196  )
   197  
   198  const (
   199  	FlagNoUndefs              uint32 = 0x1
   200  	FlagIncrLink              uint32 = 0x2
   201  	FlagDyldLink              uint32 = 0x4
   202  	FlagBindAtLoad            uint32 = 0x8
   203  	FlagPrebound              uint32 = 0x10
   204  	FlagSplitSegs             uint32 = 0x20
   205  	FlagLazyInit              uint32 = 0x40
   206  	FlagTwoLevel              uint32 = 0x80
   207  	FlagForceFlat             uint32 = 0x100
   208  	FlagNoMultiDefs           uint32 = 0x200
   209  	FlagNoFixPrebinding       uint32 = 0x400
   210  	FlagPrebindable           uint32 = 0x800
   211  	FlagAllModsBound          uint32 = 0x1000
   212  	FlagSubsectionsViaSymbols uint32 = 0x2000
   213  	FlagCanonical             uint32 = 0x4000
   214  	FlagWeakDefines           uint32 = 0x8000
   215  	FlagBindsToWeak           uint32 = 0x10000
   216  	FlagAllowStackExecution   uint32 = 0x20000
   217  	FlagRootSafe              uint32 = 0x40000
   218  	FlagSetuidSafe            uint32 = 0x80000
   219  	FlagNoReexportedDylibs    uint32 = 0x100000
   220  	FlagPIE                   uint32 = 0x200000
   221  	FlagDeadStrippableDylib   uint32 = 0x400000
   222  	FlagHasTLVDescriptors     uint32 = 0x800000
   223  	FlagNoHeapExecution       uint32 = 0x1000000
   224  	FlagAppExtensionSafe      uint32 = 0x2000000
   225  )
   226  
   227  // A Section32 is a 32-bit Mach-O section header.
   228  type Section32 struct {
   229  	Name     [16]byte
   230  	Seg      [16]byte
   231  	Addr     uint32
   232  	Size     uint32
   233  	Offset   uint32
   234  	Align    uint32
   235  	Reloff   uint32
   236  	Nreloc   uint32
   237  	Flags    uint32
   238  	Reserve1 uint32
   239  	Reserve2 uint32
   240  }
   241  
   242  // A Section64 is a 64-bit Mach-O section header.
   243  type Section64 struct {
   244  	Name     [16]byte
   245  	Seg      [16]byte
   246  	Addr     uint64
   247  	Size     uint64
   248  	Offset   uint32
   249  	Align    uint32
   250  	Reloff   uint32
   251  	Nreloc   uint32
   252  	Flags    uint32
   253  	Reserve1 uint32
   254  	Reserve2 uint32
   255  	Reserve3 uint32
   256  }
   257  
   258  // An Nlist32 is a Mach-O 32-bit symbol table entry.
   259  type Nlist32 struct {
   260  	Name  uint32
   261  	Type  uint8
   262  	Sect  uint8
   263  	Desc  uint16
   264  	Value uint32
   265  }
   266  
   267  // An Nlist64 is a Mach-O 64-bit symbol table entry.
   268  type Nlist64 struct {
   269  	Name  uint32
   270  	Type  uint8
   271  	Sect  uint8
   272  	Desc  uint16
   273  	Value uint64
   274  }
   275  
   276  // Regs386 is the Mach-O 386 register structure.
   277  type Regs386 struct {
   278  	AX    uint32
   279  	BX    uint32
   280  	CX    uint32
   281  	DX    uint32
   282  	DI    uint32
   283  	SI    uint32
   284  	BP    uint32
   285  	SP    uint32
   286  	SS    uint32
   287  	FLAGS uint32
   288  	IP    uint32
   289  	CS    uint32
   290  	DS    uint32
   291  	ES    uint32
   292  	FS    uint32
   293  	GS    uint32
   294  }
   295  
   296  // RegsAMD64 is the Mach-O AMD64 register structure.
   297  type RegsAMD64 struct {
   298  	AX    uint64
   299  	BX    uint64
   300  	CX    uint64
   301  	DX    uint64
   302  	DI    uint64
   303  	SI    uint64
   304  	BP    uint64
   305  	SP    uint64
   306  	R8    uint64
   307  	R9    uint64
   308  	R10   uint64
   309  	R11   uint64
   310  	R12   uint64
   311  	R13   uint64
   312  	R14   uint64
   313  	R15   uint64
   314  	IP    uint64
   315  	FLAGS uint64
   316  	CS    uint64
   317  	FS    uint64
   318  	GS    uint64
   319  }
   320  
   321  type intName struct {
   322  	i uint32
   323  	s string
   324  }
   325  
   326  func stringName(i uint32, names []intName, goSyntax bool) string {
   327  	for _, n := range names {
   328  		if n.i == i {
   329  			if goSyntax {
   330  				return "macho." + n.s
   331  			}
   332  			return n.s
   333  		}
   334  	}
   335  	return strconv.FormatUint(uint64(i), 10)
   336  }
   337  

View as plain text