Skip to content

Instantly share code, notes, and snippets.

@cthpw103
Created October 12, 2017 20:31
Show Gist options
  • Save cthpw103/a0be38a2c7223295cb6e579497461b02 to your computer and use it in GitHub Desktop.
Save cthpw103/a0be38a2c7223295cb6e579497461b02 to your computer and use it in GitHub Desktop.
mach-o in c++
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MH_MAGIC 0xfeedface
#define MH_CIGAM 0xcefaedfe
#define MH_MAGIC_64 0xfeedfacf
#define MH_CIGAM_64 0xcffaedfe
#define LC_SEGMENT_64 0x19
#define LC_ROUTINES_64 0x1a
#define CPU_TYPE_ARM64 (12 | 0x01000000)
#define FAT_CIGAM 0xbebafeca
#define FAT_MAGIC 0xcafebabe
#if !defined(CPU_TYPE_I386)
#define CPU_TYPE_I386 7
#endif
#if !defined(CPU_TYPE_X86_64)
#define CPU_TYPE_X86_64 (CPU_TYPE_I386 | 0x1000000)
#endif
#if !defined(CPU_TYPE_ARM)
#define CPU_TYPE_ARM 12
#endif
#if !defined(CPU_TYPE_SPARC)
#define CPU_TYPE_SPARC 14
#endif
#if !defined(CPU_TYPE_POWERPC)
#define CPU_TYPE_POWERPC 18
#endif
#if !defined(CPU_TYPE_POWERPC64)
#define CPU_TYPE_POWERPC64 (CPU_TYPE_POWERPC | 0x1000000)
#endif
#if !defined(CPU_SUBTYPE_I386_ALL)
#define CPU_SUBTYPE_I386_ALL 3
#endif
#if !defined(CPU_SUBTYPE_X86_64_ALL)
#define CPU_SUBTYPE_X86_64_ALL 3
#endif
#if !defined(CPU_SUBTYPE_ARM_ALL)
#define CPU_SUBTYPE_ARM_ALL 0
#endif
#if !defined(CPU_SUBTYPE_SPARC_ALL)
#define CPU_SUBTYPE_SPARC_ALL 0
#endif
#if !defined(CPU_SUBTYPE_POWERPC_ALL)
#define CPU_SUBTYPE_POWERPC_ALL 0
#endif
typedef int cpu_type_t;
struct mach_header {
uint32_t magic;
int cputype;
int cpusubtype;
uint32_t filetype;
uint32_t ncmds;
uint32_t sizeofcmds;
uint32_t flags;
};
struct load_command {
uint32_t cmd;
uint32_t cmdsize;
};
struct segment_command {
uint32_t cmd;
uint32_t cmdsize;
char segname[16];
uint32_t vmaddr;
uint32_t vmsize;
uint32_t fileoff;
uint32_t filesize;
int maxprot;
int initprot;
uint32_t nsects;
uint32_t flags;
};
struct _cpu_type_names {
cpu_type_t cputype;
const char *cpu_name;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment