github.com/primecitizens/pcz/std@v0.2.1/encoding/binfmt/macho/macho.go (about)

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