-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathGrijjy.MachOApi.pas
2010 lines (1872 loc) · 94.3 KB
/
Grijjy.MachOApi.pas
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Grijjy.MachOApi;
{< Partial translations of some Mach-O related kernel headers. }
interface
{$REGION 'machine.h'}
type
cpu_type_t = Integer;
cpu_subtype_t = Integer;
const
CPU_ARCH_ABI64 = $01000000; (* 64 bit ABI *)
CPU_TYPE_X86 = 7;
CPU_TYPE_X86_64 = CPU_TYPE_X86 or CPU_ARCH_ABI64;
CPU_TYPE_ARM = 12;
CPU_TYPE_ARM64 = CPU_TYPE_ARM or CPU_ARCH_ABI64;
{$ENDREGION 'machine.h'}
{$REGION 'vm_prot.h'}
type
vm_prot_t = Integer;
{$ENDREGION 'vm_prot.h'}
{$REGION 'loader.h'}
(*
* This file describes the format of mach object files.
*)
type
(*
* The 32-bit mach header appears at the very beginning of the object file for
* 32-bit architectures.
*)
mach_header = packed record
magic: UInt32; (* mach magic number identifier *)
cputype: cpu_type_t; (* cpu specifier *)
cpusubtype: cpu_subtype_t; (* machine specifier *)
filetype: UInt32; (* type of file *)
ncmds: Int32; (* number of load commands *)
sizeofcmds: UInt32; (* the size of all the load commands *)
flags: UInt32; (* flags *)
end;
Pmach_header = ^mach_header;
const
(* Constant for the magic field of the mach_header (32-bit architectures) *)
MH_MAGIC = $feedface; (* the mach magic number *)
MH_CIGAM = $cefaedfe; (* NXSwapInt(MH_MAGIC) *)
type
(*
* The 64-bit mach header appears at the very beginning of object files for
* 64-bit architectures.
*)
mach_header_64 = packed record
magic: UInt32; (* mach magic number identifier *)
cputype: cpu_type_t; (* cpu specifier *)
cpusubtype: cpu_subtype_t; (* machine specifier *)
filetype: UInt32; (* type of file *)
ncmds: Int32; (* number of load commands *)
sizeofcmds: UInt32; (* the size of all the load commands *)
flags: UInt32; (* flags *)
reserved: UInt32; (* reserved *)
end;
Pmach_header_64 = ^mach_header_64;
const
(* Constant for the magic field of the mach_header_64 (64-bit architectures) *)
MH_MAGIC_64 = $feedfacf; (* the 64-bit mach magic number *)
MH_CIGAM_64 = $cffaedfe; (* NXSwapInt(MH_MAGIC_64) *)
const
(*
* The layout of the file depends on the filetype. For all but the MH_OBJECT
* file type the segments are padded out and aligned on a segment alignment
* boundary for efficient demand pageing. The MH_EXECUTE, MH_FVMLIB, MH_DYLIB,
* MH_DYLINKER and MH_BUNDLE file types also have the headers included as part
* of their first segment.
*
* The file type MH_OBJECT is a compact format intended as output of the
* assembler and input (and possibly output) of the link editor (the .o
* format). All sections are in one unnamed segment with no segment padding.
* This format is used as an executable format when the file is so small the
* segment padding greatly increases its size.
*
* The file type MH_PRELOAD is an executable format intended for things that
* are not executed under the kernel (proms, stand alones, kernels, etc). The
* format can be executed under the kernel but may demand paged it and not
* preload it before execution.
*
* A core file is in MH_CORE format and can be any in an arbritray legal
* Mach-O file.
*
* Constants for the filetype field of the mach_header
*)
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, no section contents *)
MH_DSYM = $a; (* companion file with only debug sections *)
MH_KEXT_BUNDLE = $b; (* x86_64 kexts *)
const
(* Constants for the flags field of the mach_header *)
MH_NOUNDEFS = $1; (* the object file has no undefined references *)
MH_INCRLINK = $2; (* the object file is the output of an
incremental link against a base file
and can't be link edited again *)
MH_DYLDLINK = $4; (* the object file is input for the
dynamic linker and can't be staticly
link edited again *)
MH_BINDATLOAD = $8; (* the object file's undefined
references are bound by the dynamic
linker when loaded. *)
MH_PREBOUND = $10; (* the file has its dynamic undefined
references prebound. *)
MH_SPLIT_SEGS = $20; (* the file has its read-only and
read-write segments split *)
MH_LAZY_INIT = $40; (* the shared library init routine is
to be run lazily via catching memory
faults to its writeable segments
(obsolete) *)
MH_TWOLEVEL = $80; (* the image is using two-level name
space bindings *)
MH_FORCE_FLAT = $100; (* the executable is forcing all images
to use flat name space bindings *)
MH_NOMULTIDEFS = $200; (* this umbrella guarantees no multiple
defintions of symbols in its
sub-images so the two-level namespace
hints can always be used. *)
MH_NOFIXPREBINDING = $400; (* do not have dyld notify the
prebinding agent about this
executable *)
MH_PREBINDABLE = $800; (* the binary is not prebound but can
have its prebinding redone. only used
when MH_PREBOUND is not set. *)
MH_ALLMODSBOUND = $1000; (* 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 = $2000; (* safe to divide up the sections into
sub-sections via symbols for dead
code stripping *)
MH_CANONICAL = $4000; (* the binary has been canonicalized
via the unprebind operation *)
MH_WEAK_DEFINES = $8000; (* the final linked image contains
external weak symbols *)
MH_BINDS_TO_WEAK = $10000; (* the final linked image uses
weak symbols *)
MH_ALLOW_STACK_EXECUTION = $20000; (* When this bit is set, all stacks
in the task will be given stack
execution privilege. Only used in
MH_EXECUTE filetypes. *)
MH_ROOT_SAFE = $40000; (* When this bit is set, the binary
declares it is safe for use in
processes with uid zero *)
MH_SETUID_SAFE = $80000; (* When this bit is set, the binary
declares it is safe for use in
processes when issetugid() is true *)
MH_NO_REEXPORTED_DYLIBS = $100000; (* When this bit is set on a dylib,
the static linker does not need to
examine dependent dylibs to see
if any are re-exported *)
MH_PIE = $200000; (* When this bit is set, the OS will
load the main executable at a
random address. Only used in
MH_EXECUTE filetypes. *)
MH_DEAD_STRIPPABLE_DYLIB = $400000; (* Only for use on dylibs. When
linking against a dylib that
has this bit set, the static linker
will automatically not create a
LC_LOAD_DYLIB load command to the
dylib if no symbols are being
referenced from the dylib. *)
MH_HAS_TLV_DESCRIPTORS = $800000; (* Contains a section of type
S_THREAD_LOCAL_VARIABLES *)
MH_NO_HEAP_EXECUTION = $1000000; (* When this bit is set, the OS will
run the main executable with
a non-executable heap even on
platforms (e.g. i386) that don't
require it. Only used in MH_EXECUTE
filetypes. *)
MH_APP_EXTENSION_SAFE = $02000000; (* The code was linked for use in an
application extension. *)
type
(*
* The load commands directly follow the mach_header. The total size of all
* of the commands is given by the sizeofcmds field in the mach_header. All
* load commands must have as their first two fields cmd and cmdsize. The cmd
* field is filled in with a constant for that command type. Each command type
* has a structure specifically for it. The cmdsize field is the size in bytes
* of the particular load command structure plus anything that follows it that
* is a part of the load command (i.e. section structures, strings, etc.). To
* advance to the next load command the cmdsize can be added to the offset or
* pointer of the current load command. The cmdsize for 32-bit architectures
* MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple
* of 8 bytes (these are forever the maximum alignment of any load commands).
* The padded bytes must be zero. All tables in the object file must also
* follow these rules so the file can be memory mapped. Otherwise the pointers
* to these tables will not work well or at all on some machines. With all
* padding zeroed like objects will compare byte for byte.
*)
load_command = record
cmd: UInt32; (* type of load command *)
cmdsize: UInt32; (* total size of command in bytes *)
end;
Pload_command = ^load_command;
const
(*
* After MacOS X 10.1 when a new load command is added that is required to be
* understood by the dynamic linker for the image to execute properly the
* LC_REQ_DYLD bit will be or'ed into the load command constant. If the dynamic
* linker sees such a load command it it does not understand will issue a
* "unknown load command required for execution" error and refuse to use the
* image. Other load commands without this bit that are not understood will
* simply be ignored.
*)
LC_REQ_DYLD = $80000000;
const
(* Constants for the cmd field of all load commands, the type *)
LC_SEGMENT = $1; (* segment of this file to be mapped *)
LC_SYMTAB = $2; (* link-edit stab symbol table info *)
LC_SYMSEG = $3; (* link-edit gdb symbol table info (obsolete) *)
LC_THREAD = $4; (* thread *)
LC_UNIXTHREAD = $5; (* unix thread (includes a stack) *)
LC_LOADFVMLIB = $6; (* load a specified fixed VM shared library *)
LC_IDFVMLIB = $7; (* fixed VM shared library identification *)
LC_IDENT = $8; (* object identification info (obsolete) *)
LC_FVMFILE = $9; (* fixed VM file inclusion (internal use) *)
LC_PREPAGE = $a; (* prepage command (internal use) *)
LC_DYSYMTAB = $b; (* dynamic link-edit symbol table info *)
LC_LOAD_DYLIB = $c; (* load a dynamically linked shared library *)
LC_ID_DYLIB = $d; (* dynamically linked shared lib ident *)
LC_LOAD_DYLINKER = $e; (* load a dynamic linker *)
LC_ID_DYLINKER = $f; (* dynamic linker identification *)
LC_PREBOUND_DYLIB = $10; (* modules prebound for a dynamically
linked shared library *)
LC_ROUTINES = $11; (* image routines *)
LC_SUB_FRAMEWORK = $12; (* sub framework *)
LC_SUB_UMBRELLA = $13; (* sub umbrella *)
LC_SUB_CLIENT = $14; (* sub client *)
LC_SUB_LIBRARY = $15; (* sub library *)
LC_TWOLEVEL_HINTS = $16; (* two-level namespace lookup hints *)
LC_PREBIND_CKSUM = $17; (* prebind checksum *)
(*
* load a dynamically linked shared library that is allowed to be missing
* (all symbols are weak imported).
*)
LC_LOAD_WEAK_DYLIB = $18 or LC_REQ_DYLD;
LC_SEGMENT_64 = $19; (* 64-bit segment of this file to be
mapped *)
LC_ROUTINES_64 = $1a; (* 64-bit image routines *)
LC_UUID = $1b; (* the uuid *)
LC_RPATH = $1c or LC_REQ_DYLD; (* runpath additions *)
LC_CODE_SIGNATURE = $1d; (* local of code signature *)
LC_SEGMENT_SPLIT_INFO = $1e; (* local of info to split segments *)
LC_REEXPORT_DYLIB = $1f or LC_REQ_DYLD; (* load and re-export dylib *)
LC_LAZY_LOAD_DYLIB = $20; (* delay load of dylib until first use *)
LC_ENCRYPTION_INFO = $21; (* encrypted segment information *)
LC_DYLD_INFO = $22; (* compressed dyld information *)
LC_DYLD_INFO_ONLY = $22 or LC_REQ_DYLD; (* compressed dyld information only *)
LC_LOAD_UPWARD_DYLIB = $23 or LC_REQ_DYLD; (* load upward dylib *)
LC_VERSION_MIN_MACOSX = $24; (* build for MacOSX min OS version *)
LC_VERSION_MIN_IPHONEOS = $25; (* build for iPhoneOS min OS version *)
LC_FUNCTION_STARTS = $26; (* compressed table of function start addresses *)
LC_DYLD_ENVIRONMENT = $27; (* string for dyld to treat
like environment variable *)
LC_MAIN = $28 or LC_REQ_DYLD; (* replacement for LC_UNIXTHREAD *)
LC_DATA_IN_CODE = $29; (* table of non-instructions in __text *)
LC_SOURCE_VERSION = $2A; (* source version used to build binary *)
LC_DYLIB_CODE_SIGN_DRS = $2B; (* Code signing DRs copied from linked dylibs *)
LC_ENCRYPTION_INFO_64 = $2C; (* 64-bit encrypted segment information *)
LC_LINKER_OPTION = $2D; (* linker options in MH_OBJECT files *)
LC_LINKER_OPTIMIZATION_HINT = $2E; (* optimization hints in MH_OBJECT files *)
LC_VERSION_MIN_TVOS = $2F; (* build for AppleTV min OS version *)
LC_VERSION_MIN_WATCHOS = $30; (* build for Watch min OS version *)
type
(*
* A variable length string in a load command is represented by an lc_str
* union. The strings are stored just after the load command structure and
* the offset is from the start of the load command structure. The size
* of the string is reflected in the cmdsize field of the load command.
* Once again any padded bytes to bring the cmdsize field to a multiple
* of 4 bytes must be zero.
*)
lc_str = record
case Byte of
0: (offset: UInt32); (* offset to the string *)
{$IFDEF CPU32BITS}
1: (ptr: PAnsiChar); (* pointer to the string *)
{$ENDIF}
end;
Plc_str = ^lc_str;
type
(*
* The segment load command indicates that a part of this file is to be
* mapped into the task's address space. The size of this segment in memory,
* vmsize, maybe equal to or larger than the amount to map from this file,
* filesize. The file is mapped starting at fileoff to the beginning of
* the segment in memory, vmaddr. The rest of the memory of the segment,
* if any, is allocated zero fill on demand. The segment's maximum virtual
* memory protection and initial virtual memory protection are specified
* by the maxprot and initprot fields. If the segment has sections then the
* section structures directly follow the segment command and their size is
* reflected in cmdsize.
*)
segment_command = record (* for 32-bit architectures *)
cmd: UInt32; (* LC_SEGMENT *)
cmdsize: UInt32; (* includes sizeof section structs *)
segname: array [0..15] of AnsiChar; (* segment name *)
vmaddr: UInt32; (* memory address of this segment *)
vmsize: UInt32; (* memory size of this segment *)
fileoff: UInt32; (* file offset of this segment *)
filesize: UInt32; (* amount to map from the file *)
maxprot: vm_prot_t; (* maximum VM protection *)
initprot: vm_prot_t; (* initial VM protection *)
nsects: Int32; (* number of sections in segment *)
flags: UInt32; (* flags *)
end;
Psegment_command = ^segment_command;
type
(*
* The 64-bit segment load command indicates that a part of this file is to be
* mapped into a 64-bit task's address space. If the 64-bit segment has
* sections then section_64 structures directly follow the 64-bit segment
* command and their size is reflected in cmdsize.
*)
segment_command_64 = record (* for 64-bit architectures *)
cmd: UInt32; (* LC_SEGMENT *)
cmdsize: UInt32; (* includes sizeof section structs *)
segname: array [0..15] of AnsiChar; (* segment name *)
vmaddr: UInt64; (* memory address of this segment *)
vmsize: UInt64; (* memory size of this segment *)
fileoff: UInt64; (* file offset of this segment *)
filesize: UInt64; (* amount to map from the file *)
maxprot: vm_prot_t; (* maximum VM protection *)
initprot: vm_prot_t; (* initial VM protection *)
nsects: Int32; (* number of sections in segment *)
flags: UInt32; (* flags *)
end;
Psegment_command_64 = ^segment_command_64;
const
(* Constants for the flags field of the segment_command *)
SG_HIGHVM = $1; (* the file contents for this segment is for
the high part of the VM space, the low part
is zero filled (for stacks in core files) *)
SG_FVMLIB = $2; (* this segment is the VM that is allocated by
a fixed VM library, for overlap checking in
the link editor *)
SG_NORELOC = $4; (* this segment has nothing that was relocated
in it and nothing relocated to it, that is
it maybe safely replaced without relocation*)
SG_PROTECTED_VERSION_1 = $8; (* This segment is protected. If the
segment starts at file offset 0, the
first page of the segment is not
protected. All other pages of the
segment are protected. *)
type
(*
* A segment is made up of zero or more sections. Non-MH_OBJECT files have
* all of their segments with the proper sections in each, and padded to the
* specified segment alignment when produced by the link editor. The first
* segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
* and load commands of the object file before its first section. The zero
* fill sections are always last in their segment (in all formats). This
* allows the zeroed segment padding to be mapped into memory where zero fill
* sections might be. The gigabyte zero fill sections, those with the section
* type S_GB_ZEROFILL, can only be in a segment with sections of this type.
* These segments are then placed after all other segments.
*
* The MH_OBJECT format has all of its sections in one segment for
* compactness. There is no padding to a specified segment boundary and the
* mach_header and load commands are not part of the segment.
*
* Sections with the same section name, sectname, going into the same segment,
* segname, are combined by the link editor. The resulting section is aligned
* to the maximum alignment of the combined sections and is the new section's
* alignment. The combined sections are aligned to their original alignment in
* the combined section. Any padded bytes to get the specified alignment are
* zeroed.
*
* The format of the relocation entries referenced by the reloff and nreloc
* fields of the section structure for mach object files is described in the
* header file <reloc.h>.
*)
section = record (* for 32-bit architectures *)
sectname: array [0..15] of AnsiChar; (* name of this section *)
segname: array [0..15] of AnsiChar; (* segment this section goes in *)
addr: UInt32; (* memory address of this section *)
size: UInt32; (* size in bytes of this section *)
offset: UInt32; (* file offset of this section *)
align: UInt32; (* section alignment (power of 2) *)
reloff: UInt32; (* file offset of relocation entries *)
nreloc: Int32; (* number of relocation entries *)
flags: UInt32; (* flags (section type and attributes)*)
reserved1: UInt32; (* reserved (for offset or index) *)
reserved2: UInt32; (* reserved (for count or sizeof) *)
end;
Psection = ^section;
type
section_64 = record (* for 64-bit architectures *)
sectname: array [0..15] of AnsiChar; (* name of this section *)
segname: array [0..15] of AnsiChar; (* segment this section goes in *)
addr: UInt64; (* memory address of this section *)
size: UInt64; (* size in bytes of this section *)
offset: UInt32; (* file offset of this section *)
align: UInt32; (* section alignment (power of 2) *)
reloff: UInt32; (* file offset of relocation entries *)
nreloc: Int32; (* number of relocation entries *)
flags: UInt32; (* flags (section type and attributes)*)
reserved1: UInt32; (* reserved (for offset or index) *)
reserved2: UInt32; (* reserved (for count or sizeof) *)
reserved3: UInt32; (* reserved *)
end;
Psection_64 = ^section_64;
const
(*
* The flags field of a section structure is separated into two parts a section
* type and section attributes. The section types are mutually exclusive (it
* can only have one type) but the section attributes are not (it may have more
* than one attribute).
*)
SECTION_TYPE = $000000ff; (* 256 section types *)
SECTION_ATTRIBUTES = $ffffff00; (* 24 section attributes *)
const
(* Constants for the type of a section *)
S_REGULAR = $0; (* regular section *)
S_ZEROFILL = $1; (* zero fill on demand section *)
S_CSTRING_LITERALS = $2; (* section with only literal C strings*)
S_4BYTE_LITERALS = $3; (* section with only 4 byte literals *)
S_8BYTE_LITERALS = $4; (* section with only 8 byte literals *)
S_LITERAL_POINTERS = $5; (* section with only pointers to *)
(* literals *)
(*
* For the two types of symbol pointers sections and the symbol stubs section
* they have indirect symbol table entries. For each of the entries in the
* section the indirect symbol table entries, in corresponding order in the
* indirect symbol table, start at the index stored in the reserved1 field
* of the section structure. Since the indirect symbol table entries
* correspond to the entries in the section the number of indirect symbol table
* entries is inferred from the size of the section divided by the size of the
* entries in the section. For symbol pointers sections the size of the entries
* in the section is 4 bytes and for symbol stubs sections the byte size of the
* stubs is stored in the reserved2 field of the section structure.
*)
S_NON_LAZY_SYMBOL_POINTERS = $6; (* section with only non-lazy
symbol pointers *)
S_LAZY_SYMBOL_POINTERS = $7; (* section with only lazy symbol
pointers *)
S_SYMBOL_STUBS = $8; (* section with only symbol
stubs, byte size of stub in
the reserved2 field *)
S_MOD_INIT_FUNC_POINTERS = $9; (* section with only function
pointers for initialization*)
S_MOD_TERM_FUNC_POINTERS = $a; (* section with only function
pointers for termination *)
S_COALESCED = $b; (* section contains symbols that
are to be coalesced *)
S_GB_ZEROFILL = $c; (* zero fill on demand section
(that can be larger than 4
gigabytes) *)
S_INTERPOSING = $d; (* section with only pairs of
function pointers for
interposing *)
S_16BYTE_LITERALS = $e; (* section with only 16 byte
literals *)
S_DTRACE_DOF = $f; (* section contains
DTrace Object Format *)
S_LAZY_DYLIB_SYMBOL_POINTERS = $10; (* section with only lazy
symbol pointers to lazy
loaded dylibs *)
(*
* Section types to support thread local variables
*)
S_THREAD_LOCAL_REGULAR = $11; (* template of initial
values for TLVs *)
S_THREAD_LOCAL_ZEROFILL = $12; (* template of initial
values for TLVs *)
S_THREAD_LOCAL_VARIABLES = $13; (* TLV descriptors *)
S_THREAD_LOCAL_VARIABLE_POINTERS = $14; (* pointers to TLV
descriptors *)
S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = $15; (* functions to call
to initialize TLV
values *)
const
(*
* Constants for the section attributes part of the flags field of a section
* structure.
*)
SECTION_ATTRIBUTES_USR = $ff000000; (* User setable attributes *)
S_ATTR_PURE_INSTRUCTIONS = $80000000; (* section contains only true
machine instructions *)
S_ATTR_NO_TOC = $40000000; (* section contains coalesced
symbols that are not to be
in a ranlib table of
contents *)
S_ATTR_STRIP_STATIC_SYMS = $20000000; (* ok to strip static symbols
in this section in files
with the MH_DYLDLINK flag *)
S_ATTR_NO_DEAD_STRIP = $10000000; (* no dead stripping *)
S_ATTR_LIVE_SUPPORT = $08000000; (* blocks are live if they
reference live blocks *)
S_ATTR_SELF_MODIFYING_CODE = $04000000; (* Used with i386 code stubs
written on by dyld *)
(*
* If a segment contains any sections marked with S_ATTR_DEBUG then all
* sections in that segment must have this attribute. No section other than
* a section marked with this attribute may reference the contents of this
* section. A section with this attribute may contain no symbols and must have
* a section type S_REGULAR. The static linker will not copy section contents
* from sections with this attribute into its output file. These sections
* generally contain DWARF debugging info.
*)
S_ATTR_DEBUG = $02000000; (* a debug section *)
SECTION_ATTRIBUTES_SYS = $00ffff00; (* system setable attributes *)
S_ATTR_SOME_INSTRUCTIONS = $00000400; (* section contains some
machine instructions *)
S_ATTR_EXT_RELOC = $00000200; (* section has external
relocation entries *)
S_ATTR_LOC_RELOC = $00000100; (* section has local
relocation entries *)
const
(*
* The names of segments and sections in them are mostly meaningless to the
* link-editor. But there are few things to support traditional UNIX
* executables that require the link-editor and assembler to use some names
* agreed upon by convention.
*
* The initial protection of the "__TEXT" segment has write protection turned
* off (not writeable).
*
* The link-editor will allocate common symbols at the end of the "__common"
* section in the "__DATA" segment. It will create the section and segment
* if needed.
*)
(* The currently known segment names and the section names in those segments *)
SEG_PAGEZERO = '__PAGEZERO'; (* the pagezero segment which has no
protections and catches NULL
references for MH_EXECUTE files *)
SEG_TEXT = '__TEXT'; (* the tradition UNIX text segment *)
SECT_TEXT = '__text'; (* the real text part of the text
section no headers, and no padding *)
SECT_FVMLIB_INIT0 = '__fvmlib_init0'; (* the fvmlib initialization section *)
SECT_FVMLIB_INIT1 = '__fvmlib_init1'; (* the section following the
fvmlib initialization section *)
SEG_DATA = '__DATA'; (* the tradition UNIX data segment *)
SECT_DATA = '__data'; (* the real initialized data section
no padding, no bss overlap *)
SECT_BSS = '__bss'; (* the real uninitialized data section
no padding *)
SECT_COMMON = '__common'; (* the section common symbols are
allocated in by the link editor *)
SEG_OBJC = '__OBJC'; (* objective-C runtime segment *)
SECT_OBJC_SYMBOLS = '__symbol_table'; (* symbol table *)
SECT_OBJC_MODULES = '__module_info'; (* module information *)
SECT_OBJC_STRINGS = '__selector_strs'; (* string table *)
SECT_OBJC_REFS = '__selector_refs'; (* string table *)
SEG_ICON = '__ICON'; (* the icon segment *)
SECT_ICON_HEADER = '__header'; (* the icon headers *)
SECT_ICON_TIFF = '__tiff'; (* the icons in tiff format *)
SEG_LINKEDIT = '__LINKEDIT'; (* the segment containing all structs
created and maintained by the link
editor. Created with -seglinkedit
option to ld(1) for MH_EXECUTE and
FVMLIB file types only *)
SEG_UNIXSTACK = '__UNIXSTACK'; (* the unix stack segment *)
SEG_IMPORT = '__IMPORT'; (* the segment for the self (dyld)
modifing code stubs that has read,
write and execute permissions *)
type
(*
* Fixed virtual memory shared libraries are identified by two things. The
* target pathname (the name of the library as found for execution), and the
* minor version number. The address of where the headers are loaded is in
* header_addr. (THIS IS OBSOLETE and no longer supported).
*)
fvmlib = record
name: lc_str; (* library's target pathname *)
minor_version: UInt32; (* library's minor version number *)
header_addr: UInt32; (* library's header address *)
end;
Pfvmlib = ^fvmlib;
type
(*
* A fixed virtual shared library (filetype == MH_FVMLIB in the mach header)
* contains a fvmlib_command (cmd == LC_IDFVMLIB) to identify the library.
* An object that uses a fixed virtual shared library also contains a
* fvmlib_command (cmd == LC_LOADFVMLIB) for each library it uses.
* (THIS IS OBSOLETE and no longer supported).
*)
fvmlib_command = record
cmd: UInt32; (* LC_IDFVMLIB or LC_LOADFVMLIB *)
cmdsize: UInt32; (* includes pathname string *)
fvmlib: fvmlib; (* the library identification *)
end;
Pfvmlib_command = ^fvmlib_command;
type
(*
* Dynamicly linked shared libraries are identified by two things. The
* pathname (the name of the library as found for execution), and the
* compatibility version number. The pathname must match and the compatibility
* number in the user of the library must be greater than or equal to the
* library being used. The time stamp is used to record the time a library was
* built and copied into user so it can be use to determined if the library used
* at runtime is exactly the same as used to built the program.
*)
dylib = record
name: lc_str; (* library's path name *)
timestamp: UInt32; (* library's build time stamp *)
current_version: UInt32; (* library's current version number *)
compatibility_version: UInt32; (* library's compatibility vers number*)
end;
Pdylib = ^dylib;
type
(*
* A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
* contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
* An object that uses a dynamically linked shared library also contains a
* dylib_command (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
* LC_REEXPORT_DYLIB) for each library it uses.
*)
dylib_command = record
cmd: UInt32; (* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB, LC_REEXPORT_DYLIB *)
cmdsize: UInt32; (* includes pathname string *)
dylib: dylib; (* the library identification *)
end;
Pdylib_command = ^dylib_command;
type
(*
* A dynamically linked shared library may be a subframework of an umbrella
* framework. If so it will be linked with "-umbrella umbrella_name" where
* Where "umbrella_name" is the name of the umbrella framework. A subframework
* can only be linked against by its umbrella framework or other subframeworks
* that are part of the same umbrella framework. Otherwise the static link
* editor produces an error and states to link against the umbrella framework.
* The name of the umbrella framework for subframeworks is recorded in the
* following structure.
*)
sub_framework_command = record
cmd: UInt32; (* LC_SUB_FRAMEWORK *)
cmdsize: UInt32; (* includes umbrella string *)
umbrella: lc_str; (* the umbrella framework name *)
end;
Psub_framework_command = ^sub_framework_command;
type
(*
* For dynamically linked shared libraries that are subframework of an umbrella
* framework they can allow clients other than the umbrella framework or other
* subframeworks in the same umbrella framework. To do this the subframework
* is built with "-allowable_client client_name" and an LC_SUB_CLIENT load
* command is created for each -allowable_client flag. The client_name is
* usually a framework name. It can also be a name used for bundles clients
* where the bundle is built with "-client_name client_name".
*)
sub_client_command = record
cmd: UInt32; (* LC_SUB_CLIENT *)
cmdsize: UInt32; (* includes client string *)
client: lc_str; (* the client name *)
end;
Psub_client_command = ^sub_client_command;
type
(*
* A dynamically linked shared library may be a sub_umbrella of an umbrella
* framework. If so it will be linked with "-sub_umbrella umbrella_name" where
* Where "umbrella_name" is the name of the sub_umbrella framework. When
* staticly linking when -twolevel_namespace is in effect a twolevel namespace
* umbrella framework will only cause its subframeworks and those frameworks
* listed as sub_umbrella frameworks to be implicited linked in. Any other
* dependent dynamic libraries will not be linked it when -twolevel_namespace
* is in effect. The primary library recorded by the static linker when
* resolving a symbol in these libraries will be the umbrella framework.
* Zero or more sub_umbrella frameworks may be use by an umbrella framework.
* The name of a sub_umbrella framework is recorded in the following structure.
*)
sub_umbrella_command = record
cmd: UInt32; (* LC_SUB_UMBRELLA *)
cmdsize: UInt32; (* includes sub_umbrella string *)
sub_umbrella: lc_str; (* the sub_umbrella framework name *)
end;
type
(*
* A dynamically linked shared library may be a sub_library of another shared
* library. If so it will be linked with "-sub_library library_name" where
* Where "library_name" is the name of the sub_library shared library. When
* staticly linking when -twolevel_namespace is in effect a twolevel namespace
* shared library will only cause its subframeworks and those frameworks
* listed as sub_umbrella frameworks and libraries listed as sub_libraries to
* be implicited linked in. Any other dependent dynamic libraries will not be
* linked it when -twolevel_namespace is in effect. The primary library
* recorded by the static linker when resolving a symbol in these libraries
* will be the umbrella framework (or dynamic library). Zero or more sub_library
* shared libraries may be use by an umbrella framework or (or dynamic library).
* The name of a sub_library framework is recorded in the following structure.
* For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc".
*)
sub_library_command = record
cmd: UInt32; (* LC_SUB_LIBRARY *)
cmdsize: UInt32; (* includes sub_library string *)
sub_library: lc_str; (* the sub_library name *)
end;
Psub_library_command = ^sub_library_command;
type
(*
* A program (filetype == MH_EXECUTE) that is
* prebound to its dynamic libraries has one of these for each library that
* the static linker used in prebinding. It contains a bit vector for the
* modules in the library. The bits indicate which modules are bound (1) and
* which are not (0) from the library. The bit for module 0 is the low bit
* of the first byte. So the bit for the Nth module is:
* (linked_modules[N/8] >> N%8) & 1
*)
prebound_dylib_command = record
cmd: UInt32; (* LC_PREBOUND_DYLIB *)
cmdsize: UInt32; (* includes strings *)
name: lc_str; (* library's path name *)
nmodules: Int32; (* number of modules in library *)
linked_modules: lc_str; (* bit vector of linked modules *)
end;
Pprebound_dylib_command = ^prebound_dylib_command;
type
(*
* A program that uses a dynamic linker contains a dylinker_command to identify
* the name of the dynamic linker (LC_LOAD_DYLINKER). And a dynamic linker
* contains a dylinker_command to identify the dynamic linker (LC_ID_DYLINKER).
* A file can have at most one of these.
* This struct is also used for the LC_DYLD_ENVIRONMENT load command and
* contains string for dyld to treat like environment variable.
*)
dylinker_command = record
cmd: UInt32; (* LC_ID_DYLINKER, LC_LOAD_DYLINKER or LC_DYLD_ENVIRONMENT *)
cmdsize: UInt32; (* includes pathname string *)
name: lc_str; (* dynamic linker's path name *)
end;
Pdylinker_command = ^dylinker_command;
type
(*
* Thread commands contain machine-specific data structures suitable for
* use in the thread state primitives. The machine specific data structures
* follow the struct thread_command as follows.
* Each flavor of machine specific data structure is preceded by an unsigned
* long constant for the flavor of that data structure, an uint32_t
* that is the count of longs of the size of the state data structure and then
* the state data structure follows. This triple may be repeated for many
* flavors. The constants for the flavors, counts and state data structure
* definitions are expected to be in the header file <machine/thread_status.h>.
* These machine specific data structures sizes must be multiples of
* 4 bytes The cmdsize reflects the total size of the thread_command
* and all of the sizes of the constants for the flavors, counts and state
* data structures.
*
* For executable objects that are unix processes there will be one
* thread_command (cmd == LC_UNIXTHREAD) created for it by the link-editor.
* This is the same as a LC_THREAD, except that a stack is automatically
* created (based on the shell's limit for the stack size). Command arguments
* and environment variables are copied onto that stack.
*)
thread_command = record
cmd: UInt32; (* LC_THREAD or LC_UNIXTHREAD *)
cmdsize: UInt32; (* total size of this command *)
(* uint32_t flavor flavor of thread state *)
(* uint32_t count count of longs in thread state *)
(* struct XXX_thread_state state thread state for this flavor *)
(* ... *)
end;
Pthread_command = ^thread_command;
type
(*
* The routines command contains the address of the dynamic shared library
* initialization routine and an index into the module table for the module
* that defines the routine. Before any modules are used from the library the
* dynamic linker fully binds the module that defines the initialization routine
* and then calls it. This gets called before any module initialization
* routines (used for C++ static constructors) in the library.
*)
routines_command = record (* for 32-bit architectures *)
cmd: UInt32; (* LC_ROUTINES *)
cmdsize: UInt32; (* total size of this command *)
init_address: UInt32; (* address of initialization routine *)
init_module: UInt32; (* index into the module table that
the init routine is defined in *)
reserved1: UInt32;
reserved2: UInt32;
reserved3: UInt32;
reserved4: UInt32;
reserved5: UInt32;
reserved6: UInt32;
end;
Proutines_command = ^routines_command;
type
(*
* The 64-bit routines command. Same use as above.
*)
routines_command_64 = record (* for 64-bit architectures *)
cmd: UInt32; (* LC_ROUTINE_64 *)
cmdsize: UInt32; (* total size of this command *)
init_address: UInt64; (* address of initialization routine *)
init_module: UInt64; (* index into the module table that
the init routine is defined in *)
reserved1: UInt64;
reserved2: UInt64;
reserved3: UInt64;
reserved4: UInt64;
reserved5: UInt64;
reserved6: UInt64;
end;
Proutines_command_64 = ^routines_command_64;
type
(*
* The symtab_command contains the offsets and sizes of the link-edit 4.3BSD
* "stab" style symbol table information as described in the header files
* <nlist.h> and <stab.h>.
*)
symtab_command = record
cmd: UInt32; (* LC_SYMTAB *)
cmdsize: UInt32; (* sizeof(struct symtab_command) *)
symoff: UInt32; (* symbol table offset *)
nsyms: Int32; (* number of symbol table entries *)
stroff: UInt32; (* string table offset *)
strsize: UInt32; (* string table size in bytes *)
end;
Psymtab_command = ^symtab_command;
type
(*
* This is the second set of the symbolic information which is used to support
* the data structures for the dynamically link editor.
*
* The original set of symbolic information in the symtab_command which contains
* the symbol and string tables must also be present when this load command is
* present. When this load command is present the symbol table is organized
* into three groups of symbols:
* local symbols (static and debugging symbols) - grouped by module
* defined external symbols - grouped by module (sorted by name if not lib)
* undefined external symbols (sorted by name if MH_BINDATLOAD is not set,
* and in order the were seen by the static
* linker if MH_BINDATLOAD is set)
* In this load command there are offsets and counts to each of the three groups
* of symbols.
*
* This load command contains a the offsets and sizes of the following new
* symbolic information tables:
* table of contents
* module table
* reference symbol table
* indirect symbol table
* The first three tables above (the table of contents, module table and
* reference symbol table) are only present if the file is a dynamically linked
* shared library. For executable and object modules, which are files
* containing only one module, the information that would be in these three
* tables is determined as follows:
* table of contents - the defined external symbols are sorted by name
* module table - the file contains only one module so everything in the
* file is part of the module.
* reference symbol table - is the defined and undefined external symbols
*
* For dynamically linked shared library files this load command also contains
* offsets and sizes to the pool of relocation entries for all sections
* separated into two groups:
* external relocation entries
* local relocation entries
* For executable and object modules the relocation entries continue to hang
* off the section structures.
*)
dysymtab_command = record
cmd: UInt32; (* LC_DYSYMTAB *)
cmdsize: UInt32; (* sizeof(struct dysymtab_command) *)
(*
* The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
* are grouped into the following three groups:
* local symbols (further grouped by the module they are from)
* defined external symbols (further grouped by the module they are from)
* undefined symbols
*
* The local symbols are used only for debugging. The dynamic binding
* process may have to use them to indicate to the debugger the local
* symbols for a module that is being bound.
*
* The last two groups are used by the dynamic binding process to do the
* binding (indirectly through the module table and the reference symbol
* table when this is a dynamically linked shared library file).
*)
ilocalsym: UInt32; (* index to local symbols *)
nlocalsym: Int32; (* number of local symbols *)
iextdefsym: UInt32;(* index to externally defined symbols *)
nextdefsym: Int32; (* number of externally defined symbols *)
iundefsym: UInt32; (* index to undefined symbols *)
nundefsym: Int32; (* number of undefined symbols *)
(*
* For the for the dynamic binding process to find which module a symbol
* is defined in the table of contents is used (analogous to the ranlib
* structure in an archive) which maps defined external symbols to modules
* they are defined in. This exists only in a dynamically linked shared
* library file. For executable and object modules the defined external
* symbols are sorted by name and is use as the table of contents.
*)
tocoff: UInt32; (* file offset to table of contents *)
ntoc: Int32; (* number of entries in table of contents *)
(*
* To support dynamic binding of "modules" (whole object files) the symbol
* table must reflect the modules that the file was created from. This is
* done by having a module table that has indexes and counts into the merged
* tables for each module. The module structure that these two entries
* refer to is described below. This exists only in a dynamically linked
* shared library file. For executable and object modules the file only
* contains one module so everything in the file belongs to the module.
*)
modtaboff: UInt32; (* file offset to module table *)
nmodtab: Int32; (* number of module table entries *)
(*
* To support dynamic module binding the module structure for each module
* indicates the external references (defined and undefined) each module
* makes. For each module there is an offset and a count into the
* reference symbol table for the symbols that the module references.
* This exists only in a dynamically linked shared library file. For
* executable and object modules the defined external symbols and the
* undefined external symbols indicates the external references.
*)
extrefsymoff: UInt32; (* offset to referenced symbol table *)
nextrefsyms: Int32; (* number of referenced symbol table entries *)
(*
* The sections that contain "symbol pointers" and "routine stubs" have
* indexes and (implied counts based on the size of the section and fixed
* size of the entry) into the "indirect symbol" table for each pointer
* and stub. For every section of these two types the index into the
* indirect symbol table is stored in the section header in the field
* reserved1. An indirect symbol table entry is simply a 32bit index into
* the symbol table to the symbol that the pointer or stub is referring to.
* The indirect symbol table is ordered to match the entries in the section.
*)
indirectsymoff: UInt32; (* file offset to the indirect symbol table *)
nindirectsyms: Int32; (* number of indirect symbol table entries *)
(*
* To support relocating an individual module in a library file quickly the
* external relocation entries for each module in the library need to be
* accessed efficiently. Since the relocation entries can't be accessed
* through the section headers for a library file they are separated into
* groups of local and external entries further grouped by module. In this
* case the presents of this load command who's extreloff, nextrel,
* locreloff and nlocrel fields are non-zero indicates that the relocation
* entries of non-merged sections are not referenced through the section
* structures (and the reloff and nreloc fields in the section headers are
* set to zero).
*
* Since the relocation entries are not accessed through the section headers
* this requires the r_address field to be something other than a section
* offset to identify the item to be relocated. In this case r_address is
* set to the offset from the vmaddr of the first LC_SEGMENT command.
* For MH_SPLIT_SEGS images r_address is set to the the offset from the
* vmaddr of the first read-write LC_SEGMENT command.
*
* The relocation entries are grouped by module and the module table
* entries have indexes and counts into them for the group of external
* relocation entries for that the module.