Rizin
unix-like reverse engineering framework and cli tools
mach0.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2010-2020 nibble <nibble.ds@gmail.com>
2 // SPDX-FileCopyrightText: 2010-2020 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_bin.h>
6 #include <rz_types.h>
7 #include "mach0_specs.h"
8 
9 #ifndef _INCLUDE_RZ_BIN_MACH0_H_
10 #define _INCLUDE_RZ_BIN_MACH0_H_
11 
12 // 20% faster loading times for macho if enabled
13 #define FEATURE_SYMLIST 0
14 
15 #define RZ_BIN_MACH0_STRING_LENGTH 256
16 
17 #define CSMAGIC_CODEDIRECTORY 0xfade0c02
18 #define CSMAGIC_EMBEDDED_SIGNATURE 0xfade0cc0
19 #define CSMAGIC_DETACHED_SIGNATURE 0xfade0cc1 /* multi-arch collection of embedded signatures */
20 #define CSMAGIC_ENTITLEMENTS 0xfade7171
21 #define CSMAGIC_REQUIREMENT 0xfade0c00 /* single Requirement blob */
22 #define CSMAGIC_REQUIREMENTS 0xfade0c01 /* Requirements vector (internal requirements) */
23 
24 #define CS_PAGE_SIZE 4096
25 
26 #define CS_HASHTYPE_SHA1 1
27 #define CS_HASHTYPE_SHA256 2
28 #define CS_HASHTYPE_SHA256_TRUNCATED 3
29 
30 #define CS_HASH_SIZE_SHA1 20
31 #define CS_HASH_SIZE_SHA256 32
32 #define CS_HASH_SIZE_SHA256_TRUNCATED 20
33 
34 #define CSSLOT_CODEDIRECTORY 0
35 #define CSSLOT_INFOSLOT 1
36 #define CSSLOT_REQUIREMENTS 2
37 #define CSSLOT_RESOURCEDIR 3
38 #define CSSLOT_APPLICATION 4
39 #define CSSLOT_ENTITLEMENTS 5
40 #define CSSLOT_CMS_SIGNATURE 0x10000
41 
42 struct section_t {
49  int perm;
51  int last;
52 };
53 
54 struct symbol_t {
58  int bits;
59  int type;
61  char *name;
62  bool last;
63 };
64 
65 struct import_t {
67  int ord;
68  int last;
69 };
70 
71 struct reloc_t {
76  int ord;
77  int last;
78  char name[256];
79  bool external;
83 };
84 
85 struct addr_t {
89  int last;
90 };
91 
92 struct lib_t {
94  int last;
95 };
96 
97 struct blob_index_t {
100 };
101 
102 struct blob_t {
105 };
106 
107 struct super_blob_t {
108  struct blob_t blob;
110  struct blob_index_t index[];
111 };
112 
113 struct MACH0_(opts_t) {
114  bool verbose;
115  ut64 header_at;
116  ut64 symbols_off;
117  bool patch_relocs;
118 };
119 
120 struct MACH0_(obj_t) {
121  struct MACH0_(opts_t) options;
122  struct MACH0_(mach_header) hdr;
123  struct MACH0_(segment_command) * segs;
124  char *intrp;
125  char *compiler;
126  int nsegs;
127  struct rz_dyld_chained_starts_in_segment **chained_starts;
128  ut32 nchained_starts;
129  struct MACH0_(section) * sects;
130  int nsects;
131  struct MACH0_(nlist) * symtab;
132  ut8 *symstr;
133  ut8 *func_start; // buffer that hold the data from LC_FUNCTION_STARTS
134  int symstrlen;
135  int nsymtab;
136  ut32 *indirectsyms;
137  int nindirectsyms;
138 
139  RzBinImport **imports_by_ord;
140  size_t imports_by_ord_size;
141  HtPP *imports_by_name;
142 
143  struct dysymtab_command dysymtab;
144  struct load_command main_cmd;
145  struct dyld_info_command *dyld_info;
146  struct dylib_table_of_contents *toc;
147  int ntoc;
148  struct MACH0_(dylib_module) * modtab;
149  int nmodtab;
150  struct thread_command thread;
151  ut8 *signature;
152  union {
153  struct x86_thread_state32 x86_32;
154  struct x86_thread_state64 x86_64;
155  struct ppc_thread_state32 ppc_32;
156  struct ppc_thread_state64 ppc_64;
157  struct arm_thread_state32 arm_32;
158  struct arm_thread_state64 arm_64;
159  } thread_state;
161  int nlibs;
162  int size;
163  ut64 baddr;
164  ut64 entry;
165  bool big_endian;
166  const char *file;
167  RzBuffer *b;
168  int os;
169  Sdb *kv;
170  int has_crypto;
171  int has_canary;
172  int has_retguard;
173  int has_sanitizers;
174  int has_blocks_ext;
175  int dbg_info;
176  const char *lang;
177  int uuidn;
178  int func_size;
179  void *user;
180  ut64 (*va2pa)(ut64 p, ut32 *offset, ut32 *left, RzBinFile *bf);
181  struct symbol_t *symbols;
182  ut64 main_addr;
183 
184  RzList *sections_cache;
185  RzSkipList *relocs;
186  bool relocs_parsed;
187  bool reloc_targets_map_base_calculated;
188  bool relocs_patched;
189  RzBuffer *buf_patched;
191  RzPVector /* <struct reloc_t> */ *patchable_relocs;
192  RzHash *hash;
193 };
194 
195 #define MACH0_VFILE_NAME_REBASED_STRIPPED "rebased_stripped"
196 #define MACH0_VFILE_NAME_RELOC_TARGETS "reloc-targets"
197 #define MACH0_VFILE_NAME_PATCHED "patched"
198 
199 void MACH0_(opts_set_default)(struct MACH0_(opts_t) * options, RzBinFile *bf);
200 struct MACH0_(obj_t) * MACH0_(new_buf)(RzBuffer *buf, struct MACH0_(opts_t) * options);
201 void *MACH0_(mach0_free)(struct MACH0_(obj_t) * bin);
202 struct section_t *MACH0_(get_sections)(struct MACH0_(obj_t) * bin);
209 const struct symbol_t *MACH0_(get_symbols)(struct MACH0_(obj_t) * bin);
210 const RzList *MACH0_(get_symbols_list)(struct MACH0_(obj_t) * bin);
211 void MACH0_(pull_symbols)(struct MACH0_(obj_t) * mo, RzBinSymbolCallback cb, void *user);
212 struct import_t *MACH0_(get_imports)(struct MACH0_(obj_t) * bin);
213 RZ_BORROW RzSkipList *MACH0_(get_relocs)(struct MACH0_(obj_t) * bin);
214 struct addr_t *MACH0_(get_entrypoint)(struct MACH0_(obj_t) * bin);
215 struct lib_t *MACH0_(get_libs)(struct MACH0_(obj_t) * bin);
216 ut64 MACH0_(get_baddr)(struct MACH0_(obj_t) * bin);
217 char *MACH0_(get_class)(struct MACH0_(obj_t) * bin);
218 int MACH0_(get_bits)(struct MACH0_(obj_t) * bin);
219 bool MACH0_(is_big_endian)(struct MACH0_(obj_t) * bin);
220 bool MACH0_(is_pie)(struct MACH0_(obj_t) * bin);
221 bool MACH0_(has_nx)(struct MACH0_(obj_t) * bin);
222 const char *MACH0_(get_intrp)(struct MACH0_(obj_t) * bin);
223 const char *MACH0_(get_os)(struct MACH0_(obj_t) * bin);
224 const char *MACH0_(get_cputype)(struct MACH0_(obj_t) * bin);
225 char *MACH0_(get_cpusubtype)(struct MACH0_(obj_t) * bin);
226 char *MACH0_(get_cpusubtype_from_hdr)(struct MACH0_(mach_header) * hdr);
227 char *MACH0_(get_filetype)(struct MACH0_(obj_t) * bin);
228 char *MACH0_(get_filetype_from_hdr)(struct MACH0_(mach_header) * hdr);
229 ut64 MACH0_(get_main)(struct MACH0_(obj_t) * bin);
230 const char *MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) * hdr);
231 int MACH0_(get_bits_from_hdr)(struct MACH0_(mach_header) * hdr);
232 struct MACH0_(mach_header) * MACH0_(get_hdr)(RzBuffer *buf);
235 RZ_API RZ_OWN char *MACH0_(get_name)(struct MACH0_(obj_t) * mo, ut32 stridx, bool filter);
236 RZ_API ut64 MACH0_(paddr_to_vaddr)(struct MACH0_(obj_t) * bin, ut64 offset);
237 RZ_API ut64 MACH0_(vaddr_to_paddr)(struct MACH0_(obj_t) * bin, ut64 addr);
238 
239 RZ_API void MACH0_(rebase_buffer)(struct MACH0_(obj_t) * obj, ut64 off, ut8 *buf, ut64 count);
240 RZ_API RzBuffer *MACH0_(new_rebasing_and_stripping_buf)(struct MACH0_(obj_t) * obj);
241 RZ_API bool MACH0_(needs_rebasing_and_stripping)(struct MACH0_(obj_t) * obj);
242 RZ_API bool MACH0_(segment_needs_rebasing_and_stripping)(struct MACH0_(obj_t) * obj, size_t seg_index);
243 
244 RZ_API bool MACH0_(needs_reloc_patching)(struct MACH0_(obj_t) * obj);
245 RZ_API ut64 MACH0_(reloc_targets_vfile_size)(struct MACH0_(obj_t) * obj);
246 RZ_API ut64 MACH0_(reloc_targets_map_base)(RzBinFile *bf, struct MACH0_(obj_t) * obj);
247 RZ_API void MACH0_(patch_relocs)(RzBinFile *bf, struct MACH0_(obj_t) * obj);
248 
249 #endif
static ut64 baddr(RzBinFile *bf)
Definition: bin_any.c:58
static RzList * libs(RzBinFile *bf)
Definition: bin_coff.c:379
RzList * symbols(RzBinFile *bf)
Definition: bin_ne.c:102
RzList * relocs(RzBinFile *bf)
Definition: bin_ne.c:114
static char * signature(RzBinFile *bf, bool json)
Definition: bin_pe.c:117
#define RZ_API
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
uint32_t ut32
static ut64 va2pa(uint64_t addr, ut32 n_maps, cache_map_t *maps, RzBuffer *cache_buf, ut64 slide, ut32 *offset, ut32 *left)
Definition: dyldcache.c:22
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
RZ_API ut64 MACH0_() reloc_targets_map_base(RzBinFile *bf, struct MACH0_(obj_t) *obj)
base vaddr where to map the artificial reloc target vfile
Definition: mach0_relocs.c:556
RzList *MACH0_() mach_fields(RzBinFile *bf)
Definition: mach0.c:3969
RzList *MACH0_() get_maps_unpatched(RzBinFile *bf)
Definition: mach0.c:2217
RZ_BORROW RzSkipList *MACH0_() get_relocs(struct MACH0_(obj_t) *bin)
Definition: mach0_relocs.c:120
const char *MACH0_() get_intrp(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3320
RzList *MACH0_() get_maps(RzBinFile *bf)
Definition: mach0.c:2252
RZ_API void MACH0_() rebase_buffer(struct MACH0_(obj_t) *obj, ut64 off, ut8 *buf, ut64 count)
Definition: mach0_rebase.c:24
RzList *MACH0_() get_segments(RzBinFile *bf)
Definition: mach0.c:2267
char *MACH0_() section_type_to_string(ut64 type)
Definition: mach0.c:2347
const RzList *MACH0_() get_symbols_list(struct MACH0_(obj_t) *bin)
Definition: mach0.c:2761
char *MACH0_() get_class(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3277
RZ_API RZ_OWN char *MACH0_() get_name(struct MACH0_(obj_t) *mo, ut32 stridx, bool filter)
Get a string from the string table referenced by the LC_SYMTAB command.
Definition: mach0.c:2563
RZ_API bool MACH0_() needs_reloc_patching(struct MACH0_(obj_t) *obj)
Definition: mach0_relocs.c:532
RzList *MACH0_() section_flag_to_rzlist(ut64 flag)
Definition: mach0.c:2378
RZ_API ut64 MACH0_() reloc_targets_vfile_size(struct MACH0_(obj_t) *obj)
size of the artificial reloc target vfile
Definition: mach0_relocs.c:547
#define RZ_BIN_MACH0_STRING_LENGTH
Definition: mach0.h:15
void MACH0_() mach_headerfields(RzBinFile *bf)
Definition: mach0.c:3633
char *MACH0_() get_filetype(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3578
char *MACH0_() get_filetype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3559
RZ_API ut64 MACH0_() paddr_to_vaddr(struct MACH0_(obj_t) *bin, ut64 offset)
Definition: mach0.c:67
struct section_t *MACH0_() get_sections(struct MACH0_(obj_t) *bin)
Definition: mach0.c:2411
struct lib_t *MACH0_() get_libs(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3242
int MACH0_() get_bits_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3299
char *MACH0_() get_cpusubtype(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3546
const struct symbol_t *MACH0_() get_symbols(struct MACH0_(obj_t) *bin)
Definition: mach0.c:2959
ut64 MACH0_() get_main(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3582
void *MACH0_() mach0_free(struct MACH0_(obj_t) *bin)
Definition: mach0.c:2057
struct MACH0_(opts_t)
Definition: mach0.h:113
RZ_API RzBuffer *MACH0_() new_rebasing_and_stripping_buf(struct MACH0_(obj_t) *obj)
Definition: mach0_rebase.c:224
RZ_API bool MACH0_() needs_rebasing_and_stripping(struct MACH0_(obj_t) *obj)
Definition: mach0_rebase.c:228
bool MACH0_() is_big_endian(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3312
ut64 MACH0_() get_baddr(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3262
bool MACH0_() has_nx(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3554
bool MACH0_() is_pie(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3550
RzList *MACH0_() get_virtual_files(RzBinFile *bf)
Definition: mach0.c:2162
const char *MACH0_() get_cputype(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3383
RZ_API ut64 MACH0_() vaddr_to_paddr(struct MACH0_(obj_t) *bin, ut64 addr)
Definition: mach0.c:53
struct import_t *MACH0_() get_imports(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3146
int MACH0_() get_bits(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3288
const char *MACH0_() get_cputype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3336
RZ_API bool MACH0_() segment_needs_rebasing_and_stripping(struct MACH0_(obj_t) *obj, size_t seg_index)
Definition: mach0_rebase.c:232
RZ_API void MACH0_() patch_relocs(RzBinFile *bf, struct MACH0_(obj_t) *obj)
Patching of external relocs in a sparse overlay buffer.
Definition: mach0_relocs.c:614
void MACH0_() pull_symbols(struct MACH0_(obj_t) *mo, RzBinSymbolCallback cb, void *user)
char *MACH0_() get_cpusubtype_from_hdr(struct MACH0_(mach_header) *hdr)
Definition: mach0.c:3541
const char *MACH0_() get_os(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3324
void MACH0_() opts_set_default(struct MACH0_(opts_t) *options, RzBinFile *bf)
Definition: mach0.c:2103
struct addr_t *MACH0_() get_entrypoint(struct MACH0_(obj_t) *bin)
Definition: mach0.c:3196
int type
Definition: mipsasm.c:17
int off
Definition: pal.c:13
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_BORROW
Definition: rz_types.h:63
#define st64
Definition: rz_types_base.h:10
#define b(i)
Definition: sha256.c:42
Definition: mach0.h:85
ut64 addr
Definition: mach0.h:87
ut64 haddr
Definition: mach0.h:88
ut64 offset
Definition: mach0.h:86
int last
Definition: mach0.h:89
Definition: malloc.c:26
ut32 type
Definition: mach0.h:98
ut32 offset
Definition: mach0.h:99
Definition: mach0.h:102
ut32 length
Definition: mach0.h:104
ut32 magic
Definition: mach0.h:103
Definition: zipcmp.c:77
Definition: mach0.h:65
int last
Definition: mach0.h:68
int ord
Definition: mach0.h:67
Definition: mach0.h:92
int last
Definition: mach0.h:94
Definition: z80asm.h:102
Definition: mach0.h:71
ut64 target
Definition: mach0.h:82
bool pc_relative
Definition: mach0.h:80
int last
Definition: mach0.h:77
ut64 addr
Definition: mach0.h:73
int ord
Definition: mach0.h:76
bool external
Definition: mach0.h:79
ut64 offset
Definition: mach0.h:72
ut8 type
Definition: mach0.h:75
st64 addend
Definition: mach0.h:74
ut8 size
Definition: mach0.h:81
XX curplugin == o->plugin.
Definition: rz_bin.h:298
Definition: sdb.h:63
int perm
Definition: mach0.h:49
ut64 vsize
Definition: mach0.h:46
ut64 addr
Definition: mach0.h:44
ut32 flags
Definition: mach0.h:48
ut64 size
Definition: mach0.h:45
ut64 offset
Definition: mach0.h:43
int last
Definition: mach0.h:51
ut32 align
Definition: mach0.h:47
struct blob_index_t index[]
Definition: mach0.h:110
ut32 count
Definition: mach0.h:109
struct blob_t blob
Definition: mach0.h:108
Definition: mach0.h:54
bool last
Definition: mach0.h:62
bool is_imported
Definition: mach0.h:60
ut64 size
Definition: mach0.h:57
ut64 addr
Definition: mach0.h:56
int bits
Definition: mach0.h:58
ut64 offset
Definition: mach0.h:55
int type
Definition: mach0.h:59
char * name
Definition: mach0.h:61
static registers_t x86_32[]
Definition: core.c:27
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const char * cb[]
Definition: z80_tab.h:176
static int verbose
Definition: z80asm.c:73
static int file
Definition: z80asm.c:58
static int addr
Definition: z80asm.c:58