-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
load_commands.rb
1386 lines (1165 loc) · 45.4 KB
/
load_commands.rb
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
# frozen_string_literal: true
module MachO
# Classes and constants for parsing load commands in Mach-O binaries.
module LoadCommands
# load commands added after OS X 10.1 need to be bitwise ORed with
# LC_REQ_DYLD to be recognized by the dynamic linker (dyld)
# @api private
LC_REQ_DYLD = 0x80000000
# association of load commands to symbol representations
# @api private
LOAD_COMMANDS = {
0x1 => :LC_SEGMENT,
0x2 => :LC_SYMTAB,
0x3 => :LC_SYMSEG,
0x4 => :LC_THREAD,
0x5 => :LC_UNIXTHREAD,
0x6 => :LC_LOADFVMLIB,
0x7 => :LC_IDFVMLIB,
0x8 => :LC_IDENT,
0x9 => :LC_FVMFILE,
0xa => :LC_PREPAGE,
0xb => :LC_DYSYMTAB,
0xc => :LC_LOAD_DYLIB,
0xd => :LC_ID_DYLIB,
0xe => :LC_LOAD_DYLINKER,
0xf => :LC_ID_DYLINKER,
0x10 => :LC_PREBOUND_DYLIB,
0x11 => :LC_ROUTINES,
0x12 => :LC_SUB_FRAMEWORK,
0x13 => :LC_SUB_UMBRELLA,
0x14 => :LC_SUB_CLIENT,
0x15 => :LC_SUB_LIBRARY,
0x16 => :LC_TWOLEVEL_HINTS,
0x17 => :LC_PREBIND_CKSUM,
(LC_REQ_DYLD | 0x18) => :LC_LOAD_WEAK_DYLIB,
0x19 => :LC_SEGMENT_64,
0x1a => :LC_ROUTINES_64,
0x1b => :LC_UUID,
(LC_REQ_DYLD | 0x1c) => :LC_RPATH,
0x1d => :LC_CODE_SIGNATURE,
0x1e => :LC_SEGMENT_SPLIT_INFO,
(LC_REQ_DYLD | 0x1f) => :LC_REEXPORT_DYLIB,
0x20 => :LC_LAZY_LOAD_DYLIB,
0x21 => :LC_ENCRYPTION_INFO,
0x22 => :LC_DYLD_INFO,
(LC_REQ_DYLD | 0x22) => :LC_DYLD_INFO_ONLY,
(LC_REQ_DYLD | 0x23) => :LC_LOAD_UPWARD_DYLIB,
0x24 => :LC_VERSION_MIN_MACOSX,
0x25 => :LC_VERSION_MIN_IPHONEOS,
0x26 => :LC_FUNCTION_STARTS,
0x27 => :LC_DYLD_ENVIRONMENT,
(LC_REQ_DYLD | 0x28) => :LC_MAIN,
0x29 => :LC_DATA_IN_CODE,
0x2a => :LC_SOURCE_VERSION,
0x2b => :LC_DYLIB_CODE_SIGN_DRS,
0x2c => :LC_ENCRYPTION_INFO_64,
0x2d => :LC_LINKER_OPTION,
0x2e => :LC_LINKER_OPTIMIZATION_HINT,
0x2f => :LC_VERSION_MIN_TVOS,
0x30 => :LC_VERSION_MIN_WATCHOS,
0x31 => :LC_NOTE,
0x32 => :LC_BUILD_VERSION,
(LC_REQ_DYLD | 0x33) => :LC_DYLD_EXPORTS_TRIE,
(LC_REQ_DYLD | 0x34) => :LC_DYLD_CHAINED_FIXUPS,
(LC_REQ_DYLD | 0x35) => :LC_FILESET_ENTRY,
}.freeze
# association of symbol representations to load command constants
# @api private
LOAD_COMMAND_CONSTANTS = LOAD_COMMANDS.invert.freeze
# load commands responsible for loading dylibs
# @api private
DYLIB_LOAD_COMMANDS = %i[
LC_LOAD_DYLIB
LC_LOAD_WEAK_DYLIB
LC_REEXPORT_DYLIB
LC_LAZY_LOAD_DYLIB
LC_LOAD_UPWARD_DYLIB
].freeze
# load commands that can be created manually via {LoadCommand.create}
# @api private
CREATABLE_LOAD_COMMANDS = DYLIB_LOAD_COMMANDS + %i[
LC_ID_DYLIB
LC_RPATH
LC_LOAD_DYLINKER
].freeze
# association of load command symbols to string representations of classes
# @api private
LC_STRUCTURES = {
:LC_SEGMENT => "SegmentCommand",
:LC_SYMTAB => "SymtabCommand",
# "obsolete"
:LC_SYMSEG => "SymsegCommand",
# seems obsolete, but not documented as such
:LC_THREAD => "ThreadCommand",
:LC_UNIXTHREAD => "ThreadCommand",
# "obsolete"
:LC_LOADFVMLIB => "FvmlibCommand",
# "obsolete"
:LC_IDFVMLIB => "FvmlibCommand",
# "obsolete"
:LC_IDENT => "IdentCommand",
# "reserved for internal use only"
:LC_FVMFILE => "FvmfileCommand",
# "reserved for internal use only", no public struct
:LC_PREPAGE => "LoadCommand",
:LC_DYSYMTAB => "DysymtabCommand",
:LC_LOAD_DYLIB => "DylibCommand",
:LC_ID_DYLIB => "DylibCommand",
:LC_LOAD_DYLINKER => "DylinkerCommand",
:LC_ID_DYLINKER => "DylinkerCommand",
:LC_PREBOUND_DYLIB => "PreboundDylibCommand",
:LC_ROUTINES => "RoutinesCommand",
:LC_SUB_FRAMEWORK => "SubFrameworkCommand",
:LC_SUB_UMBRELLA => "SubUmbrellaCommand",
:LC_SUB_CLIENT => "SubClientCommand",
:LC_SUB_LIBRARY => "SubLibraryCommand",
:LC_TWOLEVEL_HINTS => "TwolevelHintsCommand",
:LC_PREBIND_CKSUM => "PrebindCksumCommand",
:LC_LOAD_WEAK_DYLIB => "DylibCommand",
:LC_SEGMENT_64 => "SegmentCommand64",
:LC_ROUTINES_64 => "RoutinesCommand64",
:LC_UUID => "UUIDCommand",
:LC_RPATH => "RpathCommand",
:LC_CODE_SIGNATURE => "LinkeditDataCommand",
:LC_SEGMENT_SPLIT_INFO => "LinkeditDataCommand",
:LC_REEXPORT_DYLIB => "DylibCommand",
:LC_LAZY_LOAD_DYLIB => "DylibCommand",
:LC_ENCRYPTION_INFO => "EncryptionInfoCommand",
:LC_DYLD_INFO => "DyldInfoCommand",
:LC_DYLD_INFO_ONLY => "DyldInfoCommand",
:LC_LOAD_UPWARD_DYLIB => "DylibCommand",
:LC_VERSION_MIN_MACOSX => "VersionMinCommand",
:LC_VERSION_MIN_IPHONEOS => "VersionMinCommand",
:LC_FUNCTION_STARTS => "LinkeditDataCommand",
:LC_DYLD_ENVIRONMENT => "DylinkerCommand",
:LC_MAIN => "EntryPointCommand",
:LC_DATA_IN_CODE => "LinkeditDataCommand",
:LC_SOURCE_VERSION => "SourceVersionCommand",
:LC_DYLIB_CODE_SIGN_DRS => "LinkeditDataCommand",
:LC_ENCRYPTION_INFO_64 => "EncryptionInfoCommand64",
:LC_LINKER_OPTION => "LinkerOptionCommand",
:LC_LINKER_OPTIMIZATION_HINT => "LinkeditDataCommand",
:LC_VERSION_MIN_TVOS => "VersionMinCommand",
:LC_VERSION_MIN_WATCHOS => "VersionMinCommand",
:LC_NOTE => "NoteCommand",
:LC_BUILD_VERSION => "BuildVersionCommand",
:LC_DYLD_EXPORTS_TRIE => "LinkeditDataCommand",
:LC_DYLD_CHAINED_FIXUPS => "LinkeditDataCommand",
:LC_FILESET_ENTRY => "FilesetEntryCommand",
}.freeze
# association of segment name symbols to names
# @api private
SEGMENT_NAMES = {
:SEG_PAGEZERO => "__PAGEZERO",
:SEG_TEXT => "__TEXT",
:SEG_TEXT_EXEC => "__TEXT_EXEC",
:SEG_DATA => "__DATA",
:SEG_DATA_CONST => "__DATA_CONST",
:SEG_OBJC => "__OBJC",
:SEG_OBJC_CONST => "__OBJC_CONST",
:SEG_ICON => "__ICON",
:SEG_LINKEDIT => "__LINKEDIT",
:SEG_LINKINFO => "__LINKINFO",
:SEG_UNIXSTACK => "__UNIXSTACK",
:SEG_IMPORT => "__IMPORT",
:SEG_KLD => "__KLD",
:SEG_KLDDATA => "__KLDDATA",
:SEG_HIB => "__HIB",
:SEG_VECTORS => "__VECTORS",
:SEG_LAST => "__LAST",
:SEG_LASTDATA_CONST => "__LASTDATA_CONST",
:SEG_PRELINK_TEXT => "__PRELINK_TEXT",
:SEG_PRELINK_INFO => "__PRELINK_INFO",
:SEG_CTF => "__CTF",
:SEG_AUTH => "__AUTH",
:SEG_AUTH_CONST => "__AUTH_CONST",
}.freeze
# association of segment flag symbols to values
# @api private
SEGMENT_FLAGS = {
:SG_HIGHVM => 0x1,
:SG_FVMLIB => 0x2,
:SG_NORELOC => 0x4,
:SG_PROTECTED_VERSION_1 => 0x8,
:SG_READ_ONLY => 0x10,
}.freeze
# The top-level Mach-O load command structure.
#
# This is the most generic load command -- only the type ID and size are
# represented. Used when a more specific class isn't available or isn't implemented.
class LoadCommand < MachOStructure
# @return [MachO::MachOView, nil] the raw view associated with the load command,
# or nil if the load command was created via {create}.
field :view, :view
# @return [Integer] the load command's type ID
field :cmd, :uint32
# @return [Integer] the size of the load command, in bytes
field :cmdsize, :uint32
# Instantiates a new LoadCommand given a view into its origin Mach-O
# @param view [MachO::MachOView] the load command's raw view
# @return [LoadCommand] the new load command
# @api private
def self.new_from_bin(view)
bin = view.raw_data.slice(view.offset, bytesize)
format = Utils.specialize_format(self.format, view.endianness)
new(view, *bin.unpack(format))
end
# Creates a new (viewless) command corresponding to the symbol provided
# @param cmd_sym [Symbol] the symbol of the load command being created
# @param args [Array] the arguments for the load command being created
def self.create(cmd_sym, *args)
raise LoadCommandNotCreatableError, cmd_sym unless CREATABLE_LOAD_COMMANDS.include?(cmd_sym)
klass = LoadCommands.const_get LC_STRUCTURES[cmd_sym]
cmd = LOAD_COMMAND_CONSTANTS[cmd_sym]
# cmd will be filled in, view and cmdsize will be left unpopulated
klass_arity = klass.min_args - 3
raise LoadCommandCreationArityError.new(cmd_sym, klass_arity, args.size) if klass_arity > args.size
klass.new(nil, cmd, nil, *args)
end
# @return [Boolean] whether the load command can be serialized
def serializable?
CREATABLE_LOAD_COMMANDS.include?(LOAD_COMMANDS[cmd])
end
# @param context [SerializationContext] the context
# to serialize into
# @return [String, nil] the serialized fields of the load command, or nil
# if the load command can't be serialized
# @api private
def serialize(context)
raise LoadCommandNotSerializableError, LOAD_COMMANDS[cmd] unless serializable?
format = Utils.specialize_format(self.class.format, context.endianness)
[cmd, self.class.bytesize].pack(format)
end
# @return [Integer] the load command's offset in the source file
# @deprecated use {#view} instead
def offset
view.offset
end
# @return [Symbol, nil] a symbol representation of the load command's
# type ID, or nil if the ID doesn't correspond to a known load command class
def type
LOAD_COMMANDS[cmd]
end
alias to_sym type
# @return [String] a string representation of the load command's
# identifying number
def to_s
type.to_s
end
# @return [Hash] a hash representation of this load command
# @note Children should override this to include additional information.
def to_h
{
"view" => view.to_h,
"cmd" => cmd,
"cmdsize" => cmdsize,
"type" => type,
}.merge super
end
# Represents a Load Command string. A rough analogue to the lc_str
# struct used internally by OS X. This class allows ruby-macho to
# pretend that strings stored in LCs are immediately available without
# explicit operations on the raw Mach-O data.
class LCStr
# @param lc [LoadCommand] the load command
# @param lc_str [Integer, String] the offset to the beginning of the
# string, or the string itself if not being initialized with a view.
# @raise [MachO::LCStrMalformedError] if the string is malformed
# @todo devise a solution such that the `lc_str` parameter is not
# interpreted differently depending on `lc.view`. The current behavior
# is a hack to allow viewless load command creation.
# @api private
def initialize(lc, lc_str)
view = lc.view
if view
lc_str_abs = view.offset + lc_str
lc_end = view.offset + lc.cmdsize - 1
raw_string = view.raw_data.slice(lc_str_abs..lc_end)
@string, null_byte, _padding = raw_string.partition("\x00")
raise LCStrMalformedError, lc if null_byte.empty?
@string_offset = lc_str
else
@string = lc_str
@string_offset = 0
end
end
# @return [String] a string representation of the LCStr
def to_s
@string
end
# @return [Integer] the offset to the beginning of the string in the
# load command
def to_i
@string_offset
end
# @return [Hash] a hash representation of this {LCStr}.
def to_h
{
"string" => to_s,
"offset" => to_i,
}
end
end
# Represents the contextual information needed by a load command to
# serialize itself correctly into a binary string.
class SerializationContext
# @return [Symbol] the endianness of the serialized load command
attr_reader :endianness
# @return [Integer] the constant alignment value used to pad the
# serialized load command
attr_reader :alignment
# @param macho [MachO::MachOFile] the file to contextualize
# @return [SerializationContext] the
# resulting context
def self.context_for(macho)
new(macho.endianness, macho.alignment)
end
# @param endianness [Symbol] the endianness of the context
# @param alignment [Integer] the alignment of the context
# @api private
def initialize(endianness, alignment)
@endianness = endianness
@alignment = alignment
end
end
end
# A load command containing a single 128-bit unique random number
# identifying an object produced by static link editor. Corresponds to
# LC_UUID.
class UUIDCommand < LoadCommand
# @return [Array<Integer>] the UUID
field :uuid, :string, :size => 16, :unpack => "C16"
# @return [String] a string representation of the UUID
def uuid_string
hexes = uuid.map { |elem| "%02<elem>x" % { :elem => elem } }
segs = [
hexes[0..3].join, hexes[4..5].join, hexes[6..7].join,
hexes[8..9].join, hexes[10..15].join
]
segs.join("-")
end
# @return [String] an alias for uuid_string
def to_s
uuid_string
end
# @return [Hash] returns a hash representation of this {UUIDCommand}
def to_h
{
"uuid" => uuid,
"uuid_string" => uuid_string,
}.merge super
end
end
# A load command indicating that part of this file is to be mapped into
# the task's address space. Corresponds to LC_SEGMENT.
class SegmentCommand < LoadCommand
# @return [String] the name of the segment
field :segname, :string, :padding => :null, :size => 16, :to_s => true
# @return [Integer] the memory address of the segment
field :vmaddr, :uint32
# @return [Integer] the memory size of the segment
field :vmsize, :uint32
# @return [Integer] the file offset of the segment
field :fileoff, :uint32
# @return [Integer] the amount to map from the file
field :filesize, :uint32
# @return [Integer] the maximum VM protection
field :maxprot, :int32
# @return [Integer] the initial VM protection
field :initprot, :int32
# @return [Integer] the number of sections in the segment
field :nsects, :uint32
# @return [Integer] any flags associated with the segment
field :flags, :uint32
# All sections referenced within this segment.
# @return [Array<MachO::Sections::Section>] if the Mach-O is 32-bit
# @return [Array<MachO::Sections::Section64>] if the Mach-O is 64-bit
def sections
klass = case self
when SegmentCommand64
MachO::Sections::Section64
when SegmentCommand
MachO::Sections::Section
end
offset = view.offset + self.class.bytesize
length = nsects * klass.bytesize
bins = view.raw_data[offset, length]
bins.unpack("a#{klass.bytesize}" * nsects).map do |bin|
klass.new_from_bin(view.endianness, bin)
end
end
# @example
# puts "this segment relocated in/to it" if sect.flag?(:SG_NORELOC)
# @param flag [Symbol] a segment flag symbol
# @return [Boolean] true if `flag` is present in the segment's flag field
def flag?(flag)
flag = SEGMENT_FLAGS[flag]
return false if flag.nil?
flags & flag == flag
end
# Guesses the alignment of the segment.
# @return [Integer] the guessed alignment, as a power of 2
# @note See `guess_align` in `cctools/misc/lipo.c`
def guess_align
return Sections::MAX_SECT_ALIGN if vmaddr.zero?
align = 0
segalign = 1
while (segalign & vmaddr).zero?
segalign <<= 1
align += 1
end
return 2 if align < 2
return Sections::MAX_SECT_ALIGN if align > Sections::MAX_SECT_ALIGN
align
end
# @return [Hash] a hash representation of this {SegmentCommand}
def to_h
{
"segname" => segname,
"vmaddr" => vmaddr,
"vmsize" => vmsize,
"fileoff" => fileoff,
"filesize" => filesize,
"maxprot" => maxprot,
"initprot" => initprot,
"nsects" => nsects,
"flags" => flags,
"sections" => sections.map(&:to_h),
}.merge super
end
end
# A load command indicating that part of this file is to be mapped into
# the task's address space. Corresponds to LC_SEGMENT_64.
class SegmentCommand64 < SegmentCommand
# @return [Integer] the memory address of the segment
field :vmaddr, :uint64
# @return [Integer] the memory size of the segment
field :vmsize, :uint64
# @return [Integer] the file offset of the segment
field :fileoff, :uint64
# @return [Integer] the amount to map from the file
field :filesize, :uint64
end
# A load command representing some aspect of shared libraries, depending
# on filetype. Corresponds to LC_ID_DYLIB, LC_LOAD_DYLIB,
# LC_LOAD_WEAK_DYLIB, and LC_REEXPORT_DYLIB.
class DylibCommand < LoadCommand
# @return [LCStr] the library's path
# name as an LCStr
field :name, :lcstr, :to_s => true
# @return [Integer] the library's build time stamp
field :timestamp, :uint32
# @return [Integer] the library's current version number
field :current_version, :uint32
# @return [Integer] the library's compatibility version number
field :compatibility_version, :uint32
# @param context [SerializationContext]
# the context
# @return [String] the serialized fields of the load command
# @api private
def serialize(context)
format = Utils.specialize_format(self.class.format, context.endianness)
string_payload, string_offsets = Utils.pack_strings(self.class.bytesize,
context.alignment,
:name => name.to_s)
cmdsize = self.class.bytesize + string_payload.bytesize
[cmd, cmdsize, string_offsets[:name], timestamp, current_version,
compatibility_version].pack(format) + string_payload
end
# @return [Hash] a hash representation of this {DylibCommand}
def to_h
{
"name" => name.to_h,
"timestamp" => timestamp,
"current_version" => current_version,
"compatibility_version" => compatibility_version,
}.merge super
end
end
# A load command representing some aspect of the dynamic linker, depending
# on filetype. Corresponds to LC_ID_DYLINKER, LC_LOAD_DYLINKER, and
# LC_DYLD_ENVIRONMENT.
class DylinkerCommand < LoadCommand
# @return [LCStr] the dynamic linker's
# path name as an LCStr
field :name, :lcstr, :to_s => true
# @param context [SerializationContext]
# the context
# @return [String] the serialized fields of the load command
# @api private
def serialize(context)
format = Utils.specialize_format(self.class.format, context.endianness)
string_payload, string_offsets = Utils.pack_strings(self.class.bytesize,
context.alignment,
:name => name.to_s)
cmdsize = self.class.bytesize + string_payload.bytesize
[cmd, cmdsize, string_offsets[:name]].pack(format) + string_payload
end
# @return [Hash] a hash representation of this {DylinkerCommand}
def to_h
{
"name" => name.to_h,
}.merge super
end
end
# A load command used to indicate dynamic libraries used in prebinding.
# Corresponds to LC_PREBOUND_DYLIB.
class PreboundDylibCommand < LoadCommand
# @return [LCStr] the library's path
# name as an LCStr
field :name, :lcstr, :to_s => true
# @return [Integer] the number of modules in the library
field :nmodules, :uint32
# @return [Integer] a bit vector of linked modules
field :linked_modules, :uint32
# @return [Hash] a hash representation of this {PreboundDylibCommand}
def to_h
{
"name" => name.to_h,
"nmodules" => nmodules,
"linked_modules" => linked_modules,
}.merge super
end
end
# A load command used to represent threads.
# @note cctools-870 and onwards have all fields of thread_command commented
# out except the common ones (cmd, cmdsize)
class ThreadCommand < LoadCommand
end
# A load command containing the address of the dynamic shared library
# initialization routine and an index into the module table for the module
# that defines the routine. Corresponds to LC_ROUTINES.
class RoutinesCommand < LoadCommand
# @return [Integer] the address of the initialization routine
field :init_address, :uint32
# @return [Integer] the index into the module table that the init routine
# is defined in
field :init_module, :uint32
# @return [void]
field :reserved1, :uint32
# @return [void]
field :reserved2, :uint32
# @return [void]
field :reserved3, :uint32
# @return [void]
field :reserved4, :uint32
# @return [void]
field :reserved5, :uint32
# @return [void]
field :reserved6, :uint32
# @return [Hash] a hash representation of this {RoutinesCommand}
def to_h
{
"init_address" => init_address,
"init_module" => init_module,
"reserved1" => reserved1,
"reserved2" => reserved2,
"reserved3" => reserved3,
"reserved4" => reserved4,
"reserved5" => reserved5,
"reserved6" => reserved6,
}.merge super
end
end
# A load command containing the address of the dynamic shared library
# initialization routine and an index into the module table for the module
# that defines the routine. Corresponds to LC_ROUTINES_64.
class RoutinesCommand64 < RoutinesCommand
# @return [Integer] the address of the initialization routine
field :init_address, :uint64
# @return [Integer] the index into the module table that the init routine
# is defined in
field :init_module, :uint64
# @return [void]
field :reserved1, :uint64
# @return [void]
field :reserved2, :uint64
# @return [void]
field :reserved3, :uint64
# @return [void]
field :reserved4, :uint64
# @return [void]
field :reserved5, :uint64
# @return [void]
field :reserved6, :uint64
end
# A load command signifying membership of a subframework containing the name
# of an umbrella framework. Corresponds to LC_SUB_FRAMEWORK.
class SubFrameworkCommand < LoadCommand
# @return [LCStr] the umbrella framework name as an LCStr
field :umbrella, :lcstr, :to_s => true
# @return [Hash] a hash representation of this {SubFrameworkCommand}
def to_h
{
"umbrella" => umbrella.to_h,
}.merge super
end
end
# A load command signifying membership of a subumbrella containing the name
# of an umbrella framework. Corresponds to LC_SUB_UMBRELLA.
class SubUmbrellaCommand < LoadCommand
# @return [LCStr] the subumbrella framework name as an LCStr
field :sub_umbrella, :lcstr, :to_s => true
# @return [Hash] a hash representation of this {SubUmbrellaCommand}
def to_h
{
"sub_umbrella" => sub_umbrella.to_h,
}.merge super
end
end
# A load command signifying a sublibrary of a shared library. Corresponds
# to LC_SUB_LIBRARY.
class SubLibraryCommand < LoadCommand
# @return [LCStr] the sublibrary name as an LCStr
field :sub_library, :lcstr, :to_s => true
# @return [Hash] a hash representation of this {SubLibraryCommand}
def to_h
{
"sub_library" => sub_library.to_h,
}.merge super
end
end
# A load command signifying a shared library that is a subframework of
# an umbrella framework. Corresponds to LC_SUB_CLIENT.
class SubClientCommand < LoadCommand
# @return [LCStr] the subclient name as an LCStr
field :sub_client, :lcstr, :to_s => true
# @return [Hash] a hash representation of this {SubClientCommand}
def to_h
{
"sub_client" => sub_client.to_h,
}.merge super
end
end
# A load command containing the offsets and sizes of the link-edit 4.3BSD
# "stab" style symbol table information. Corresponds to LC_SYMTAB.
class SymtabCommand < LoadCommand
# @return [Integer] the symbol table's offset
field :symoff, :uint32
# @return [Integer] the number of symbol table entries
field :nsyms, :uint32
# @return [Integer] the string table's offset
field :stroff, :uint32
# @return [Integer] the string table size in bytes
field :strsize, :uint32
# @return [Hash] a hash representation of this {SymtabCommand}
def to_h
{
"symoff" => symoff,
"nsyms" => nsyms,
"stroff" => stroff,
"strsize" => strsize,
}.merge super
end
end
# A load command containing symbolic information needed to support data
# structures used by the dynamic link editor. Corresponds to LC_DYSYMTAB.
class DysymtabCommand < LoadCommand
# @return [Integer] the index to local symbols
field :ilocalsym, :uint32
# @return [Integer] the number of local symbols
field :nlocalsym, :uint32
# @return [Integer] the index to externally defined symbols
field :iextdefsym, :uint32
# @return [Integer] the number of externally defined symbols
field :nextdefsym, :uint32
# @return [Integer] the index to undefined symbols
field :iundefsym, :uint32
# @return [Integer] the number of undefined symbols
field :nundefsym, :uint32
# @return [Integer] the file offset to the table of contents
field :tocoff, :uint32
# @return [Integer] the number of entries in the table of contents
field :ntoc, :uint32
# @return [Integer] the file offset to the module table
field :modtaboff, :uint32
# @return [Integer] the number of entries in the module table
field :nmodtab, :uint32
# @return [Integer] the file offset to the referenced symbol table
field :extrefsymoff, :uint32
# @return [Integer] the number of entries in the referenced symbol table
field :nextrefsyms, :uint32
# @return [Integer] the file offset to the indirect symbol table
field :indirectsymoff, :uint32
# @return [Integer] the number of entries in the indirect symbol table
field :nindirectsyms, :uint32
# @return [Integer] the file offset to the external relocation entries
field :extreloff, :uint32
# @return [Integer] the number of external relocation entries
field :nextrel, :uint32
# @return [Integer] the file offset to the local relocation entries
field :locreloff, :uint32
# @return [Integer] the number of local relocation entries
field :nlocrel, :uint32
# @return [Hash] a hash representation of this {DysymtabCommand}
def to_h
{
"ilocalsym" => ilocalsym,
"nlocalsym" => nlocalsym,
"iextdefsym" => iextdefsym,
"nextdefsym" => nextdefsym,
"iundefsym" => iundefsym,
"nundefsym" => nundefsym,
"tocoff" => tocoff,
"ntoc" => ntoc,
"modtaboff" => modtaboff,
"nmodtab" => nmodtab,
"extrefsymoff" => extrefsymoff,
"nextrefsyms" => nextrefsyms,
"indirectsymoff" => indirectsymoff,
"nindirectsyms" => nindirectsyms,
"extreloff" => extreloff,
"nextrel" => nextrel,
"locreloff" => locreloff,
"nlocrel" => nlocrel,
}.merge super
end
end
# A load command containing the offset and number of hints in the two-level
# namespace lookup hints table. Corresponds to LC_TWOLEVEL_HINTS.
class TwolevelHintsCommand < LoadCommand
# @return [Integer] the offset to the hint table
field :htoffset, :uint32
# @return [Integer] the number of hints in the hint table
field :nhints, :uint32
# @return [TwolevelHintsTable]
# the hint table
field :table, :two_level_hints_table
# @return [Hash] a hash representation of this {TwolevelHintsCommand}
def to_h
{
"htoffset" => htoffset,
"nhints" => nhints,
"table" => table.hints.map(&:to_h),
}.merge super
end
# A representation of the two-level namespace lookup hints table exposed
# by a {TwolevelHintsCommand} (`LC_TWOLEVEL_HINTS`).
class TwolevelHintsTable
# @return [Array<TwolevelHint>] all hints in the table
attr_reader :hints
# @param view [MachO::MachOView] the view into the current Mach-O
# @param htoffset [Integer] the offset of the hints table
# @param nhints [Integer] the number of two-level hints in the table
# @api private
def initialize(view, htoffset, nhints)
format = Utils.specialize_format("L=#{nhints}", view.endianness)
raw_table = view.raw_data[htoffset, nhints * 4]
blobs = raw_table.unpack(format)
@hints = blobs.map { |b| TwolevelHint.new(b) }
end
# An individual two-level namespace lookup hint.
class TwolevelHint
# @return [Integer] the index into the sub-images
attr_reader :isub_image
# @return [Integer] the index into the table of contents
attr_reader :itoc
# @param blob [Integer] the 32-bit number containing the lookup hint
# @api private
def initialize(blob)
@isub_image = blob >> 24
@itoc = blob & 0x00FFFFFF
end
# @return [Hash] a hash representation of this {TwolevelHint}
def to_h
{
"isub_image" => isub_image,
"itoc" => itoc,
}
end
end
end
end
# A load command containing the value of the original checksum for prebound
# files, or zero. Corresponds to LC_PREBIND_CKSUM.
class PrebindCksumCommand < LoadCommand
# @return [Integer] the checksum or 0
field :cksum, :uint32
# @return [Hash] a hash representation of this {PrebindCksumCommand}
def to_h
{
"cksum" => cksum,
}.merge super
end
end
# A load command representing an rpath, which specifies a path that should
# be added to the current run path used to find @rpath prefixed dylibs.
# Corresponds to LC_RPATH.
class RpathCommand < LoadCommand
# @return [LCStr] the path to add to the run path as an LCStr
field :path, :lcstr, :to_s => true
# @param context [SerializationContext] the context
# @return [String] the serialized fields of the load command
# @api private
def serialize(context)
format = Utils.specialize_format(self.class.format, context.endianness)
string_payload, string_offsets = Utils.pack_strings(self.class.bytesize,
context.alignment,
:path => path.to_s)
cmdsize = self.class.bytesize + string_payload.bytesize
[cmd, cmdsize, string_offsets[:path]].pack(format) + string_payload
end
# @return [Hash] a hash representation of this {RpathCommand}
def to_h
{
"path" => path.to_h,
}.merge super
end
end
# A load command representing the offsets and sizes of a blob of data in
# the __LINKEDIT segment. Corresponds to LC_CODE_SIGNATURE,
# LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE,
# LC_DYLIB_CODE_SIGN_DRS, LC_LINKER_OPTIMIZATION_HINT, LC_DYLD_EXPORTS_TRIE,
# or LC_DYLD_CHAINED_FIXUPS.
class LinkeditDataCommand < LoadCommand
# @return [Integer] offset to the data in the __LINKEDIT segment
field :dataoff, :uint32
# @return [Integer] size of the data in the __LINKEDIT segment
field :datasize, :uint32
# @return [Hash] a hash representation of this {LinkeditDataCommand}
def to_h
{
"dataoff" => dataoff,
"datasize" => datasize,
}.merge super
end
# TODO(ww): doc
def superblob
raise LinkeditTypeMismatchError.new(__method__, type) unless type == :LC_CODE_SIGNATURE
superblob_view = MachOView.new view.raw_data, view.endianness, dataoff
CodeSigning::SuperBlob.new_from_bin superblob_view
end
end
# A load command representing the offset to and size of an encrypted
# segment. Corresponds to LC_ENCRYPTION_INFO.
class EncryptionInfoCommand < LoadCommand
# @return [Integer] the offset to the encrypted segment
field :cryptoff, :uint32
# @return [Integer] the size of the encrypted segment
field :cryptsize, :uint32
# @return [Integer] the encryption system, or 0 if not encrypted yet
field :cryptid, :uint32
# @return [Hash] a hash representation of this {EncryptionInfoCommand}
def to_h
{