summaryrefslogtreecommitdiff
path: root/includes/compiler/MachO.h
blob: 9be6c3e2f07accbffa6a23bafb1f6e33f543c7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef COMPILER_MACHO_H
#define COMPILER_MACHO_H

#include "compiler/common.h"
#include "compiler/CompilerTools.h"

typedef struct MachOReloc MachOReloc;
typedef struct MachOSection MachOSection;
typedef struct MachOSegment MachOSegment;
typedef struct MachOSymbol MachOSymbol;
typedef struct SymbolData SymbolData;

struct SymbolData {
    union {
        char *name;
        SInt32 strx;
    } u;
    UInt8 type;
    SInt16 desc;
    UInt32 value;
    MachOSection *section;
};

typedef enum RelocType {
    RT_0,
    RT_1,
    RT_2,
    RT_3,
    RT_4,
    RT_5,
    RT_6,
    RT_7,
    RT_8,
    RT_9
} RelocType;

typedef struct RelocDataT {
    RelocType type;
    UInt8 privFlag;
    UInt8 x2;
    UInt8 x3;
    UInt32 x4;
    MachOSection *section;
    UInt32 gapC;
    MachOSection *sect10;
    char *name;
    SInt32 x18;
} RelocDataT;

#define N_GSYM  0x20
#define N_FNAME 0x22
#define N_FUN   0x24
#define N_STSYM 0x26
#define N_LCSYM 0x28
#define N_BNSYM 0x2E
#define N_OPT   0x3C
#define N_RSYM  0x40
#define N_SLINE 0x44
#define N_ENSYM 0x4E
#define N_SSYM  0x60
#define N_SO    0x64
#define N_OSO   0x66
#define N_LSYM  0x80
#define N_BINCL 0x82
#define N_SOL   0x84
#define N_PARAMS  0x86
#define N_VERSION 0x88
#define N_OLEVEL  0x8A
#define N_PSYM  0xA0
#define N_EINCL 0xA2
#define N_ENTRY 0xA4
#define N_LBRAC 0xC0
#define N_EXCL  0xC2
#define N_RBRAC 0xE0
#define N_BCOMM 0xE2
#define N_ECOMM 0xE4
#define N_ECOML 0xE8
#define N_LENG  0xFE

#define N_STAB 0xE0
#define N_PEXT 0x10
#define N_TYPE 0x0E
#define N_EXT 0x01

#define N_UNDF 0
#define N_ABS 2
#define N_SECT 0xE
#define N_PBUD 0xC
#define N_INDR 0xA

// Mach-O header
struct mach_header {
    uint32_t magic;
    SInt32 cputype;
    SInt32 cpusubtype;
    uint32_t filetype;
    uint32_t ncmds;
    uint32_t sizeofcmds;
    uint32_t flags;
};

#define MH_MAGIC 0xfeedface
#define MH_OBJECT 1
#define CPU_TYPE_POWERPC 18
#define CPU_SUBTYPE_MC98000_ALL 0

// load commands
#define LC_SEGMENT 1
#define LC_SYMTAB 2
#define LC_DYSYMTAB 11

// segments
struct segment_command {
    uint32_t cmd;
    uint32_t cmdsize;
    char segname[16];
    uint32_t vmaddr;
    uint32_t vmsize;
    uint32_t fileoff;
    uint32_t filesize;
    SInt32 maxprot;
    SInt32 initprot;
    uint32_t nsects;
    uint32_t flags;
};

// sections
struct section {
    char sectname[16];
    char segname[16];
    uint32_t addr;
    uint32_t size;
    uint32_t offset;
    uint32_t align;
    uint32_t reloff;
    uint32_t nreloc;
    uint32_t flags;
    uint32_t reserved1;
    uint32_t reserved2;
};

#define S_REGULAR 0
#define S_ZEROFILL 1
#define S_CSTRING_LITERALS 2
#define S_4BYTE_LITERALS 3
#define S_8BYTE_LITERALS 4
#define S_LITERAL_POINTERS 5
#define S_NON_LAZY_SYMBOL_POINTERS 6
#define S_LAZY_SYMBOL_POINTERS 7
#define S_SYMBOL_STUBS 8
#define S_MOD_INIT_FUNC_POINTERS 9
#define S_MOD_TERM_FUNC_POINTERS 10
#define S_COALESCED 11
#define S_GB_ZEROFILL 12
#define S_INTERPOSING 13
#define S_16BYTE_LITERALS 14

#define S_ATTR_PURE_INSTRUCTIONS 0x80000000
#define S_ATTR_NO_TOC 0x40000000
#define S_ATTR_STRIP_STATIC_SYMS 0x20000000
#define S_ATTR_NO_DEAD_STRIP 0x10000000
#define S_ATTR_LIVE_SUPPORT 0x8000000
#define S_ATTR_SELF_MODIFYING_CODE 0x4000000
#define S_ATTR_DBUG 0x2000000
#define S_ATTR_SOME_INSTRUCTIONS 0x400
#define S_ATTR_EXT_RELOC 0x200
#define S_ATTR_LOC_RELOC 0x100

// symbols
struct symtab_command {
    uint32_t cmd;
    uint32_t cmdsize;
    uint32_t symoff;
    uint32_t nsyms;
    uint32_t stroff;
    uint32_t strsize;
};

struct dysymtab_command {
    uint32_t cmd;
    uint32_t cmdsize;
    uint32_t ilocalsym;
    uint32_t nlocalsym;
    uint32_t iextdefsym;
    uint32_t nextdefsym;
    uint32_t iundefsym;
    uint32_t nundefsym;
    uint32_t tocoff;
    uint32_t ntoc;
    uint32_t modtaboff;
    uint32_t nmodtab;
    uint32_t extrefsymoff;
    uint32_t nextrefsyms;
    uint32_t indirectsymoff;
    uint32_t nindirectsyms;
    uint32_t extreloff;
    uint32_t nextrel;
    uint32_t locreloff;
    uint32_t nlocrel;
};

struct nlist {
    int32_t n_strx;
    uint8_t n_type;
    uint8_t n_sect;
    int16_t n_desc;
    uint32_t n_value;
};

// relocations
enum reloc_type_ppc {
    PPC_RELOC_VANILLA,
    PPC_RELOC_PAIR,
    PPC_RELOC_BR14,
    PPC_RELOC_BR24,
    PPC_RELOC_HI16,
    PPC_RELOC_LO16,
    PPC_RELOC_HA16,
    PPC_RELOC_LO14,
    PPC_RELOC_SECTDIFF,
    PPC_RELOC_PB_LA_PTR,
    PPC_RELOC_HI16_SECTDIFF,
    PPC_RELOC_LO16_SECTDIFF,
    PPC_RELOC_HA16_SECTDIFF,
    PPC_RELOC_JBSR,
    PPC_RELOC_LO14_SECTDIFF,
    PPC_RELOC_LOCAL_SECTDIFF
};

#define R_SCATTERED 0x80000000

#ifdef __MWERKS__
#pragma options align=mac68k
#endif
struct MachOSection {
    struct section section;
    GList glist;
    MachOReloc *firstReloc;
    MachOReloc *lastReloc;
    int x5C;
    UInt32 num;
    UInt32 id;
    MachOSection *next;
};

struct MachOSegment {
    struct segment_command cmd;
    MachOSection *firstSection;
    MachOSection *lastSection;
    MachOSegment *next;
};

struct MachOReloc {
    char length; // size of the relocation
    char reltype;
    char is_extern;
    char is_pcrel; // was relocated pc relative already
    UInt32 address; // offset to what is being relocated
    UInt32 value;
    MachOReloc *next;
};

struct MachOSymbol {
    SymbolData data;
    UInt32 index;
    MachOSymbol *next;
};
#ifdef __MWERKS__
#pragma options align=reset
#endif

extern void MachO_Setup(void);
extern void MachO_Finish(void);
extern void MachO_Cleanup(void);
extern MachOSegment *MachO_CreateSegment(char *segname, int maxprot, int initprot, UInt32 flags);
extern MachOSection *MachO_CreateSection(MachOSegment *segment, char *segname, char *sectname, UInt32 align, UInt32 flags, UInt32 sectionID);
extern GList *MachO_GetGList(MachOSection *section);
extern void MachO_SetSectionSize(void);
extern void MachO_Relocate(MachOSection *section, UInt32 address, UInt32 value, char length, char is_pcrel, Boolean is_extern, int reltype);
extern UInt32 MachO_DeclareSymbol(char *name, MachOSection *section, UInt32 value, Boolean isAbsolute, short type, short desc);
extern void MachO_ReOrderSections(void);
extern void MachO_AddIndirectSymbol(SInt32 symbol);
extern UInt32 MachO_NumIndirectSym(void);
extern SInt32 MachO_OutputStab(SymbolData *data, SInt32 strIndex);

#endif