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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
//=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Nate Begeman and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the MachOWriter class.
//
//===----------------------------------------------------------------------===//
#ifndef MACHOWRITER_H
#define MACHOWRITER_H
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineRelocation.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetMachOWriterInfo.h"
namespace llvm {
class GlobalVariable;
class Mangler;
class MachineCodeEmitter;
class MachOCodeEmitter;
class OutputBuffer;
/// MachOSym - This struct contains information about each symbol that is
/// added to logical symbol table for the module. This is eventually
/// turned into a real symbol table in the file.
struct MachOSym {
const GlobalValue *GV; // The global value this corresponds to.
std::string GVName; // The mangled name of the global value.
uint32_t n_strx; // index into the string table
uint8_t n_type; // type flag
uint8_t n_sect; // section number or NO_SECT
int16_t n_desc; // see <mach-o/stab.h>
uint64_t n_value; // value for this symbol (or stab offset)
// Constants for the n_sect field
// see <mach-o/nlist.h>
enum { NO_SECT = 0 }; // symbol is not in any section
// Constants for the n_type field
// see <mach-o/nlist.h>
enum { N_UNDF = 0x0, // undefined, n_sect == NO_SECT
N_ABS = 0x2, // absolute, n_sect == NO_SECT
N_SECT = 0xe, // defined in section number n_sect
N_PBUD = 0xc, // prebound undefined (defined in a dylib)
N_INDR = 0xa // indirect
};
// The following bits are OR'd into the types above. For example, a type
// of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
enum { N_EXT = 0x01, // external symbol bit
N_PEXT = 0x10 // private external symbol bit
};
// Constants for the n_desc field
// see <mach-o/loader.h>
enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
REFERENCE_FLAG_UNDEFINED_LAZY = 1,
REFERENCE_FLAG_DEFINED = 2,
REFERENCE_FLAG_PRIVATE_DEFINED = 3,
REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5
};
enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
N_WEAK_REF = 0x0040, // symbol is weak referenced
N_WEAK_DEF = 0x0080 // coalesced symbol is a weak definition
};
MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
TargetMachine &TM);
};
/// MachOWriter - This class implements the common target-independent code for
/// writing Mach-O files. Targets should derive a class from this to
/// parameterize the output format.
///
class MachOWriter : public MachineFunctionPass {
friend class MachOCodeEmitter;
public:
static char ID;
MachineCodeEmitter &getMachineCodeEmitter() const {
return *(MachineCodeEmitter*)MCE;
}
MachOWriter(std::ostream &O, TargetMachine &TM);
virtual ~MachOWriter();
virtual const char *getPassName() const {
return "Mach-O Writer";
}
typedef std::vector<unsigned char> DataBuffer;
protected:
/// Output stream to send the resultant object file to.
///
std::ostream &O;
/// Target machine description.
///
TargetMachine &TM;
/// Mang - The object used to perform name mangling for this module.
///
Mangler *Mang;
/// MCE - The MachineCodeEmitter object that we are exposing to emit machine
/// code for functions to the .o file.
MachOCodeEmitter *MCE;
/// is64Bit/isLittleEndian - This information is inferred from the target
/// machine directly, indicating what header values and flags to set.
bool is64Bit, isLittleEndian;
/// doInitialization - Emit the file header and all of the global variables
/// for the module to the Mach-O file.
bool doInitialization(Module &M);
bool runOnMachineFunction(MachineFunction &MF);
/// doFinalization - Now that the module has been completely processed, emit
/// the Mach-O file to 'O'.
bool doFinalization(Module &M);
/// MachOHeader - This struct contains the header information about a
/// specific architecture type/subtype pair that is emitted to the file.
struct MachOHeader {
uint32_t magic; // mach magic number identifier
uint32_t filetype; // type of file
uint32_t ncmds; // number of load commands
uint32_t sizeofcmds; // the size of all the load commands
uint32_t flags; // flags
uint32_t reserved; // 64-bit only
/// HeaderData - The actual data for the header which we are building
/// up for emission to the file.
DataBuffer HeaderData;
// Constants for the filetype field
// see <mach-o/loader.h> for additional info on the various types
enum { MH_OBJECT = 1, // relocatable object file
MH_EXECUTE = 2, // demand paged executable file
MH_FVMLIB = 3, // fixed VM shared library file
MH_CORE = 4, // core file
MH_PRELOAD = 5, // preloaded executable file
MH_DYLIB = 6, // dynamically bound shared library
MH_DYLINKER = 7, // dynamic link editor
MH_BUNDLE = 8, // dynamically bound bundle file
MH_DYLIB_STUB = 9, // shared library stub for static linking only
MH_DSYM = 10 // companion file wiht only debug sections
};
// Constants for the flags field
enum { MH_NOUNDEFS = 1 << 0,
// the object file has no undefined references
MH_INCRLINK = 1 << 1,
// the object file is the output of an incremental link against
// a base file and cannot be link edited again
MH_DYLDLINK = 1 << 2,
// the object file is input for the dynamic linker and cannot be
// statically link edited again.
MH_BINDATLOAD = 1 << 3,
// the object file's undefined references are bound by the
// dynamic linker when loaded.
MH_PREBOUND = 1 << 4,
// the file has its dynamic undefined references prebound
MH_SPLIT_SEGS = 1 << 5,
// the file has its read-only and read-write segments split
// see <mach/shared_memory_server.h>
MH_LAZY_INIT = 1 << 6,
// the shared library init routine is to be run lazily via
// catching memory faults to its writable segments (obsolete)
MH_TWOLEVEL = 1 << 7,
// the image is using two-level namespace bindings
MH_FORCE_FLAT = 1 << 8,
// the executable is forcing all images to use flat namespace
// bindings.
MH_NOMULTIDEFS = 1 << 8,
// this umbrella guarantees no multiple definitions of symbols
// in its sub-images so the two-level namespace hints can
// always be used.
MH_NOFIXPREBINDING = 1 << 10,
// do not have dyld notify the prebidning agent about this
// executable.
MH_PREBINDABLE = 1 << 11,
// the binary is not prebound but can have its prebinding
// redone. only used when MH_PREBOUND is not set.
MH_ALLMODSBOUND = 1 << 12,
// indicates that this binary binds to all two-level namespace
// modules of its dependent libraries. Only used when
// MH_PREBINDABLE and MH_TWOLEVEL are both set.
MH_SUBSECTIONS_VIA_SYMBOLS = 1 << 13,
// safe to divide up the sections into sub-sections via symbols
// for dead code stripping.
MH_CANONICAL = 1 << 14,
// the binary has been canonicalized via the unprebind operation
MH_WEAK_DEFINES = 1 << 15,
// the final linked image contains external weak symbols
MH_BINDS_TO_WEAK = 1 << 16,
// the final linked image uses weak symbols
MH_ALLOW_STACK_EXECUTION = 1 << 17
// When this bit is set, all stacks in the task will be given
// stack execution privilege. Only used in MH_EXECUTE filetype
};
MachOHeader() : magic(0), filetype(0), ncmds(0), sizeofcmds(0), flags(0),
reserved(0) { }
/// cmdSize - This routine returns the size of the MachOSection as written
/// to disk, depending on whether the destination is a 64 bit Mach-O file.
unsigned cmdSize(bool is64Bit) const {
if (is64Bit)
|