Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
macho.go342 linesDownload Raw Back to macho
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5// Mach-O header data structures6// Originally at:7// http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html (since deleted by Apple)8// Archived copy at:9// https://web.archive.org/web/20090819232456/http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/index.html10// For cloned PDF see:11// https://github.com/aidansteele/osx-abi-macho-file-format-reference12 13package macho14 15import "strconv"16 17// A FileHeader represents a Mach-O file header.18type FileHeader struct {19	Magic  uint3220	Cpu    Cpu21	SubCpu uint3222	Type   Type23	Ncmd   uint3224	Cmdsz  uint3225	Flags  uint3226}27 28const (29	fileHeaderSize32 = 7 * 430	fileHeaderSize64 = 8 * 431)32 33const (34	Magic32  uint32 = 0xfeedface35	Magic64  uint32 = 0xfeedfacf36	MagicFat uint32 = 0xcafebabe37)38 39// A Type is the Mach-O file type, e.g. an object file, executable, or dynamic library.40type Type uint3241 42const (43	TypeObj    Type = 144	TypeExec   Type = 245	TypeDylib  Type = 646	TypeBundle Type = 847)48 49var typeStrings = []intName{50	{uint32(TypeObj), "Obj"},51	{uint32(TypeExec), "Exec"},52	{uint32(TypeDylib), "Dylib"},53	{uint32(TypeBundle), "Bundle"},54}55 56func (t Type) String() string   { return stringName(uint32(t), typeStrings, false) }57func (t Type) GoString() string { return stringName(uint32(t), typeStrings, true) }58 59// A Cpu is a Mach-O cpu type.60type Cpu uint3261 62const cpuArch64 = 0x0100000063 64const (65	Cpu386   Cpu = 766	CpuAmd64 Cpu = Cpu386 | cpuArch6467	CpuArm   Cpu = 1268	CpuArm64 Cpu = CpuArm | cpuArch6469	CpuPpc   Cpu = 1870	CpuPpc64 Cpu = CpuPpc | cpuArch6471)72 73var cpuStrings = []intName{74	{uint32(Cpu386), "Cpu386"},75	{uint32(CpuAmd64), "CpuAmd64"},76	{uint32(CpuArm), "CpuArm"},77	{uint32(CpuArm64), "CpuArm64"},78	{uint32(CpuPpc), "CpuPpc"},79	{uint32(CpuPpc64), "CpuPpc64"},80}81 82func (i Cpu) String() string   { return stringName(uint32(i), cpuStrings, false) }83func (i Cpu) GoString() string { return stringName(uint32(i), cpuStrings, true) }84 85// A LoadCmd is a Mach-O load command.86type LoadCmd uint3287 88const (89	LoadCmdSegment    LoadCmd = 0x190	LoadCmdSymtab     LoadCmd = 0x291	LoadCmdThread     LoadCmd = 0x492	LoadCmdUnixThread LoadCmd = 0x5 // thread+stack93	LoadCmdDysymtab   LoadCmd = 0xb94	LoadCmdDylib      LoadCmd = 0xc // load dylib command95	LoadCmdDylinker   LoadCmd = 0xf // id dylinker command (not load dylinker command)96	LoadCmdSegment64  LoadCmd = 0x1997	LoadCmdRpath      LoadCmd = 0x8000001c98)99 100var cmdStrings = []intName{101	{uint32(LoadCmdSegment), "LoadCmdSegment"},102	{uint32(LoadCmdThread), "LoadCmdThread"},103	{uint32(LoadCmdUnixThread), "LoadCmdUnixThread"},104	{uint32(LoadCmdDylib), "LoadCmdDylib"},105	{uint32(LoadCmdSegment64), "LoadCmdSegment64"},106	{uint32(LoadCmdRpath), "LoadCmdRpath"},107}108 109func (i LoadCmd) String() string   { return stringName(uint32(i), cmdStrings, false) }110func (i LoadCmd) GoString() string { return stringName(uint32(i), cmdStrings, true) }111 112type (113	// A Segment32 is a 32-bit Mach-O segment load command.114	Segment32 struct {115		Cmd     LoadCmd116		Len     uint32117		Name    [16]byte118		Addr    uint32119		Memsz   uint32120		Offset  uint32121		Filesz  uint32122		Maxprot uint32123		Prot    uint32124		Nsect   uint32125		Flag    uint32126	}127 128	// A Segment64 is a 64-bit Mach-O segment load command.129	Segment64 struct {130		Cmd     LoadCmd131		Len     uint32132		Name    [16]byte133		Addr    uint64134		Memsz   uint64135		Offset  uint64136		Filesz  uint64137		Maxprot uint32138		Prot    uint32139		Nsect   uint32140		Flag    uint32141	}142 143	// A SymtabCmd is a Mach-O symbol table command.144	SymtabCmd struct {145		Cmd     LoadCmd146		Len     uint32147		Symoff  uint32148		Nsyms   uint32149		Stroff  uint32150		Strsize uint32151	}152 153	// A DysymtabCmd is a Mach-O dynamic symbol table command.154	DysymtabCmd struct {155		Cmd            LoadCmd156		Len            uint32157		Ilocalsym      uint32158		Nlocalsym      uint32159		Iextdefsym     uint32160		Nextdefsym     uint32161		Iundefsym      uint32162		Nundefsym      uint32163		Tocoffset      uint32164		Ntoc           uint32165		Modtaboff      uint32166		Nmodtab        uint32167		Extrefsymoff   uint32168		Nextrefsyms    uint32169		Indirectsymoff uint32170		Nindirectsyms  uint32171		Extreloff      uint32172		Nextrel        uint32173		Locreloff      uint32174		Nlocrel        uint32175	}176 177	// A DylibCmd is a Mach-O load dynamic library command.178	DylibCmd struct {179		Cmd            LoadCmd180		Len            uint32181		Name           uint32182		Time           uint32183		CurrentVersion uint32184		CompatVersion  uint32185	}186 187	// A RpathCmd is a Mach-O rpath command.188	RpathCmd struct {189		Cmd  LoadCmd190		Len  uint32191		Path uint32192	}193 194	// A Thread is a Mach-O thread state command.195	Thread struct {196		Cmd  LoadCmd197		Len  uint32198		Type uint32199		Data []uint32200	}201)202 203const (204	FlagNoUndefs              uint32 = 0x1205	FlagIncrLink              uint32 = 0x2206	FlagDyldLink              uint32 = 0x4207	FlagBindAtLoad            uint32 = 0x8208	FlagPrebound              uint32 = 0x10209	FlagSplitSegs             uint32 = 0x20210	FlagLazyInit              uint32 = 0x40211	FlagTwoLevel              uint32 = 0x80212	FlagForceFlat             uint32 = 0x100213	FlagNoMultiDefs           uint32 = 0x200214	FlagNoFixPrebinding       uint32 = 0x400215	FlagPrebindable           uint32 = 0x800216	FlagAllModsBound          uint32 = 0x1000217	FlagSubsectionsViaSymbols uint32 = 0x2000218	FlagCanonical             uint32 = 0x4000219	FlagWeakDefines           uint32 = 0x8000220	FlagBindsToWeak           uint32 = 0x10000221	FlagAllowStackExecution   uint32 = 0x20000222	FlagRootSafe              uint32 = 0x40000223	FlagSetuidSafe            uint32 = 0x80000224	FlagNoReexportedDylibs    uint32 = 0x100000225	FlagPIE                   uint32 = 0x200000226	FlagDeadStrippableDylib   uint32 = 0x400000227	FlagHasTLVDescriptors     uint32 = 0x800000228	FlagNoHeapExecution       uint32 = 0x1000000229	FlagAppExtensionSafe      uint32 = 0x2000000230)231 232// A Section32 is a 32-bit Mach-O section header.233type Section32 struct {234	Name     [16]byte235	Seg      [16]byte236	Addr     uint32237	Size     uint32238	Offset   uint32239	Align    uint32240	Reloff   uint32241	Nreloc   uint32242	Flags    uint32243	Reserve1 uint32244	Reserve2 uint32245}246 247// A Section64 is a 64-bit Mach-O section header.248type Section64 struct {249	Name     [16]byte250	Seg      [16]byte251	Addr     uint64252	Size     uint64253	Offset   uint32254	Align    uint32255	Reloff   uint32256	Nreloc   uint32257	Flags    uint32258	Reserve1 uint32259	Reserve2 uint32260	Reserve3 uint32261}262 263// An Nlist32 is a Mach-O 32-bit symbol table entry.264type Nlist32 struct {265	Name  uint32266	Type  uint8267	Sect  uint8268	Desc  uint16269	Value uint32270}271 272// An Nlist64 is a Mach-O 64-bit symbol table entry.273type Nlist64 struct {274	Name  uint32275	Type  uint8276	Sect  uint8277	Desc  uint16278	Value uint64279}280 281// Regs386 is the Mach-O 386 register structure.282type Regs386 struct {283	AX    uint32284	BX    uint32285	CX    uint32286	DX    uint32287	DI    uint32288	SI    uint32289	BP    uint32290	SP    uint32291	SS    uint32292	FLAGS uint32293	IP    uint32294	CS    uint32295	DS    uint32296	ES    uint32297	FS    uint32298	GS    uint32299}300 301// RegsAMD64 is the Mach-O AMD64 register structure.302type RegsAMD64 struct {303	AX    uint64304	BX    uint64305	CX    uint64306	DX    uint64307	DI    uint64308	SI    uint64309	BP    uint64310	SP    uint64311	R8    uint64312	R9    uint64313	R10   uint64314	R11   uint64315	R12   uint64316	R13   uint64317	R14   uint64318	R15   uint64319	IP    uint64320	FLAGS uint64321	CS    uint64322	FS    uint64323	GS    uint64324}325 326type intName struct {327	i uint32328	s string329}330 331func stringName(i uint32, names []intName, goSyntax bool) string {332	for _, n := range names {333		if n.i == i {334			if goSyntax {335				return "macho." + n.s336			}337			return n.s338		}339	}340	return strconv.FormatUint(uint64(i), 10)341}342 
codekingpro/portable-devtools · Team Ai