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
|
//===- MachODumper.cpp - Object file dumping utility for llvm -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the MachO-specific dumper for llvm-readobj.
//
//===----------------------------------------------------------------------===//
#include "ObjDumper.h"
#include "StackMapPrinter.h"
#include "llvm-readobj.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Object/MachO.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ScopedPrinter.h"
using namespace llvm;
using namespace object;
namespace {
class MachODumper : public ObjDumper {
public:
MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
: ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
void printFileHeaders() override;
void printSectionHeaders() override;
void printRelocations() override;
void printUnwindInfo() override;
void printStackMap() const override;
void printCGProfile() override;
void printNeededLibraries() override;
bool canCompareSymbols() const override { return true; }
bool compareSymbolsByName(object::SymbolRef LHS,
object::SymbolRef RHS) const override;
bool compareSymbolsByType(object::SymbolRef LHS,
object::SymbolRef RHS) const override;
// MachO-specific.
void printMachODataInCode() override;
void printMachOVersionMin() override;
void printMachODysymtab() override;
void printMachOSegment() override;
void printMachOIndirectSymbols() override;
void printMachOLinkerOptions () override;
private:
template<class MachHeader>
void printFileHeaders(const MachHeader &Header);
StringRef getSymbolName(const SymbolRef &Symbol) const;
uint8_t getSymbolType(const SymbolRef &Symbol) const;
void printSymbols() override;
void printSymbols(std::optional<SymbolComparator> SymComp) override;
void printDynamicSymbols() override;
void printDynamicSymbols(std::optional<SymbolComparator> SymComp) override;
void printSymbol(const SymbolRef &Symbol, ScopedPrinter &W);
void printSymbol(const SymbolRef &Symbol);
void printRelocation(const RelocationRef &Reloc);
void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
void printSectionHeaders(const MachOObjectFile *Obj);
const MachOObjectFile *Obj;
};
} // namespace
namespace llvm {
std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
ScopedPrinter &Writer) {
return std::make_unique<MachODumper>(&Obj, Writer);
}
} // namespace llvm
const EnumEntry<uint32_t> MachOMagics[] = {
{ "Magic", MachO::MH_MAGIC },
{ "Cigam", MachO::MH_CIGAM },
{ "Magic64", MachO::MH_MAGIC_64 },
{ "Cigam64", MachO::MH_CIGAM_64 },
{ "FatMagic", MachO::FAT_MAGIC },
{ "FatCigam", MachO::FAT_CIGAM },
};
const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
{ "Relocatable", MachO::MH_OBJECT },
{ "Executable", MachO::MH_EXECUTE },
{ "FixedVMLibrary", MachO::MH_FVMLIB },
{ "Core", MachO::MH_CORE },
{ "PreloadedExecutable", MachO::MH_PRELOAD },
{ "DynamicLibrary", MachO::MH_DYLIB },
{ "DynamicLinker", MachO::MH_DYLINKER },
{ "Bundle", MachO::MH_BUNDLE },
{ "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
{ "DWARFSymbol", MachO::MH_DSYM },
{ "KextBundle", MachO::MH_KEXT_BUNDLE },
};
const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
{ "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
{ "X86" , MachO::CPU_TYPE_X86 },
{ "X86-64" , MachO::CPU_TYPE_X86_64 },
{ "Mc98000" , MachO::CPU_TYPE_MC98000 },
{ "Arm" , MachO::CPU_TYPE_ARM },
{ "Arm64" , MachO::CPU_TYPE_ARM64 },
{ "Sparc" , MachO::CPU_TYPE_SPARC },
{ "PowerPC" , MachO::CPU_TYPE_POWERPC },
{ "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP),
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H),
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM),
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_V8),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64E),
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL),
};
const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450),
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970),
};
const EnumEntry<uint32_t> MachOHeaderFlags[] = {
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK),
LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK),
LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD),
LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND),
LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT),
LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL),
LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT),
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING),
LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE),
LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND),
LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL),
LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES),
LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK),
LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION),
LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE),
LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE),
LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE),
LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB),
LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS),
LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION),
LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE),
};
const EnumEntry<unsigned> MachOSectionTypes[] = {
{ "Regular" , MachO::S_REGULAR },
{ "ZeroFill" , MachO::S_ZEROFILL },
{ "CStringLiterals" , MachO::S_CSTRING_LITERALS },
{ "4ByteLiterals" , MachO::S_4BYTE_LITERALS },
{ "8ByteLiterals" , MachO::S_8BYTE_LITERALS },
{ "LiteralPointers" , MachO::S_LITERAL_POINTERS },
{ "NonLazySymbolPointers" , MachO::S_NON_LAZY_SYMBOL_POINTERS },
{ "LazySymbolPointers" , MachO::S_LAZY_SYMBOL_POINTERS },
{ "SymbolStubs" , MachO::S_SYMBOL_STUBS },
{ "ModInitFuncPointers" , MachO::S_MOD_INIT_FUNC_POINTERS },
{ "ModTermFuncPointers" , MachO::S_MOD_TERM_FUNC_POINTERS },
{ "Coalesced" , MachO::S_COALESCED },
{ "GBZeroFill" , MachO::S_GB_ZEROFILL },
{ "Interposing" , MachO::S_INTERPOSING },
{ "16ByteLiterals" , MachO::S_16BYTE_LITERALS },
{ "DTraceDOF" , MachO::S_DTRACE_DOF },
{ "LazyDylibSymbolPointers" , MachO::S_LAZY_DYLIB_SYMBOL_POINTERS },
{ "ThreadLocalRegular" , MachO::S_THREAD_LOCAL_REGULAR },
{ "ThreadLocalZerofill" , MachO::S_THREAD_LOCAL_ZEROFILL },
{ "ThreadLocalVariables" , MachO::S_THREAD_LOCAL_VARIABLES },
{ "ThreadLocalVariablePointers" , MachO::S_THREAD_LOCAL_VARIABLE_POINTERS },
{ "ThreadLocalInitFunctionPointers", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS }
};
const EnumEntry<unsigned> MachOSectionAttributes[] = {
{ "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
{ "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
{ "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
{ "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
{ "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
{ "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
{ "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
{ "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
{ "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
{ "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
};
const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
{ "UndefinedNonLazy", 0 },
{ "ReferenceFlagUndefinedLazy", 1 },
{ "ReferenceFlagDefined", 2 },
{ "ReferenceFlagPrivateDefined", 3 },
{ "ReferenceFlagPrivateUndefinedNonLazy", 4 },
{ "ReferenceFlagPrivateUndefinedLazy", 5 }
};
const EnumEntry<unsigned> MachOSymbolFlags[] = {
{ "ThumbDef", 0x8 },
{ "ReferencedDynamically", 0x10 },
{ "NoDeadStrip", 0x20 },
{ "WeakRef", 0x40 },
{ "WeakDef", 0x80 },
{ "SymbolResolver", 0x100 },
{ "AltEntry", 0x200 },
{ "ColdFunc", 0x400 },
};
const EnumEntry<unsigned> MachOSymbolTypes[] = {
{ "Undef", 0x0 },
{ "Abs", 0x2 },
{ "Indirect", 0xA },
{ "PreboundUndef", 0xC },
{ "Section", 0xE }
};
namespace {
struct MachOSection {
ArrayRef<char> Name;
ArrayRef<char> SegmentName;
uint64_t Address;
uint64_t Size;
uint32_t Offset;
uint32_t Alignment;
uint32_t RelocationTableOffset;
uint32_t NumRelocationTableEntries;
uint32_t Flags;
uint32_t Reserved1;
uint32_t Reserved2;
uint32_t Reserved3;
};
struct MachOSegment {
std::string CmdName;
std::string SegName;
uint64_t cmdsize;
uint64_t vmaddr;
uint64_t vmsize;
uint64_t fileoff;
uint64_t filesize;
uint32_t maxprot;
uint32_t initprot;
uint32_t nsects;
uint32_t flags;
};
struct MachOSymbol {
uint32_t StringIndex;
uint8_t Type;
uint8_t SectionIndex;
uint16_t Flags;
uint64_t Value;
};
}
static std::string getMask(uint32_t prot)
{
// TODO (davide): This always assumes prot is valid.
// Catch mistakes and report if needed.
std::string Prot;
Prot = "";
Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
return Prot;
}
static void getSection(const MachOObjectFile *Obj,
DataRefImpl Sec,
MachOSection &Section) {
if (!Obj->is64Bit()) {
MachO::section Sect = Obj->getSection(Sec);
Section.Address = Sect.addr;
Section.Size = Sect.size;
Section.Offset = Sect.offset;
Section.Alignment = Sect.align;
Section.RelocationTableOffset = Sect.reloff;
Section.NumRelocationTableEntries = Sect.nreloc;
Section.Flags = Sect.flags;
Section.Reserved1 = Sect.reserved1;
Section.Reserved2 = Sect.reserved2;
return;
}
MachO::section_64 Sect = Obj->getSection64(Sec);
Section.Address = Sect.addr;
Section.Size = Sect.size;
Section.Offset = Sect.offset;
Section.Alignment = Sect.align;
Section.RelocationTableOffset = Sect.reloff;
Section.NumRelocationTableEntries = Sect.nreloc;
Section.Flags = Sect.flags;
Section.Reserved1 = Sect.reserved1;
Section.Reserved2 = Sect.reserved2;
Section.Reserved3 = Sect.reserved3;
}
static void getSegment(const MachOObjectFile *Obj,
const MachOObjectFile::LoadCommandInfo &L,
MachOSegment &Segment) {
if (!Obj->is64Bit()) {
MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
Segment.CmdName = "LC_SEGMENT";
Segment.SegName = SC.segname;
Segment.cmdsize = SC.cmdsize;
Segment.vmaddr = SC.vmaddr;
Segment.vmsize = SC.vmsize;
Segment.fileoff = SC.fileoff;
Segment.filesize = SC.filesize;
Segment.maxprot = SC.maxprot;
Segment.initprot = SC.initprot;
Segment.nsects = SC.nsects;
Segment.flags = SC.flags;
return;
}
MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
Segment.CmdName = "LC_SEGMENT_64";
Segment.SegName = SC.segname;
Segment.cmdsize = SC.cmdsize;
Segment.vmaddr = SC.vmaddr;
Segment.vmsize = SC.vmsize;
Segment.fileoff = SC.fileoff;
Segment.filesize = SC.filesize;
Segment.maxprot = SC.maxprot;
Segment.initprot = SC.initprot;
Segment.nsects = SC.nsects;
Segment.flags = SC.flags;
}
static void getSymbol(const MachOObjectFile *Obj,
DataRefImpl DRI,
MachOSymbol &Symbol) {
if (!Obj->is64Bit()) {
MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
Symbol.StringIndex = Entry.n_strx;
Symbol.Type = Entry.n_type;
Symbol.SectionIndex = Entry.n_sect;
Symbol.Flags = Entry.n_desc;
Symbol.Value = Entry.n_value;
return;
}
MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
Symbol.StringIndex = Entry.n_strx;
Symbol.Type = Entry.n_type;
Symbol.SectionIndex = Entry.n_sect;
Symbol.Flags = Entry.n_desc;
Symbol.Value = Entry.n_value;
}
void MachODumper::printFileHeaders() {
DictScope H(W, "MachHeader");
if (!Obj->is64Bit()) {
printFileHeaders(Obj->getHeader());
} else {
printFileHeaders(Obj->getHeader64());
W.printHex("Reserved", Obj->getHeader64().reserved);
}
}
template<class MachHeader>
void MachODumper::printFileHeaders(const MachHeader &Header) {
W.printEnum("Magic", Header.magic, ArrayRef(MachOMagics));
W.printEnum("CpuType", Header.cputype, ArrayRef(MachOHeaderCpuTypes));
uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
switch (Header.cputype) {
case MachO::CPU_TYPE_X86:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesX86));
break;
case MachO::CPU_TYPE_X86_64:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesX64));
break;
case MachO::CPU_TYPE_ARM:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesARM));
break;
case MachO::CPU_TYPE_POWERPC:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesPPC));
break;
case MachO::CPU_TYPE_SPARC:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesSPARC));
break;
case MachO::CPU_TYPE_ARM64:
W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesARM64));
break;
case MachO::CPU_TYPE_POWERPC64:
default:
W.printHex("CpuSubtype", subtype);
}
W.printEnum("FileType", Header.filetype, ArrayRef(MachOHeaderFileTypes));
W.printNumber("NumOfLoadCommands", Header.ncmds);
W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
W.printFlags("Flags", Header.flags, ArrayRef(MachOHeaderFlags));
}
void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); }
void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) {
ListScope Group(W, "Sections");
int SectionIndex = -1;
for (const SectionRef &Section : Obj->sections()) {
++SectionIndex;
MachOSection MOSection;
getSection(Obj, Section.getRawDataRefImpl(), MOSection);
DataRefImpl DR = Section.getRawDataRefImpl();
StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
ArrayRef<char> RawName = Obj->getSectionRawName(DR);
StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
DictScope SectionD(W, "Section");
W.printNumber("Index", SectionIndex);
W.printBinary("Name", Name, RawName);
W.printBinary("Segment", SegmentName, RawSegmentName);
W.printHex("Address", MOSection.Address);
W.printHex("Size", MOSection.Size);
W.printNumber("Offset", MOSection.Offset);
W.printNumber("Alignment", MOSection.Alignment);
W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
W.printEnum("Type", MOSection.Flags & 0xFF, ArrayRef(MachOSectionTypes));
W.printFlags("Attributes", MOSection.Flags >> 8,
ArrayRef(MachOSectionAttributes));
W.printHex("Reserved1", MOSection.Reserved1);
W.printHex("Reserved2", MOSection.Reserved2);
if (Obj->is64Bit())
W.printHex("Reserved3", MOSection.Reserved3);
if (opts::SectionRelocations) {
ListScope D(W, "Relocations");
for (const RelocationRef &Reloc : Section.relocations())
printRelocation(Reloc);
}
if (opts::SectionSymbols) {
ListScope D(W, "Symbols");
for (const SymbolRef &Symbol : Obj->symbols()) {
if (!Section.containsSymbol(Symbol))
continue;
printSymbol(Symbol);
}
}
if (opts::SectionData && !Section.isBSS())
W.printBinaryBlock("SectionData", unwrapOrError(Obj->getFileName(),
Section.getContents()));
}
}
void MachODumper::printRelocations() {
ListScope D(W, "Relocations");
std::error_code EC;
for (const SectionRef &Section : Obj->sections()) {
StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
bool PrintedGroup = false;
for (const RelocationRef &Reloc : Section.relocations()) {
if (!PrintedGroup) {
W.startLine() << "Section " << Name << " {\n";
W.indent();
PrintedGroup = true;
}
printRelocation(Reloc);
}
if (PrintedGroup) {
W.unindent();
W.startLine() << "}\n";
}
}
}
void MachODumper::printRelocation(const RelocationRef &Reloc) {
return printRelocation(Obj, Reloc);
}
void MachODumper::printRelocation(const MachOObjectFile *Obj,
const RelocationRef &Reloc) {
uint64_t Offset = Reloc.getOffset();
SmallString<32> RelocName;
Reloc.getTypeName(RelocName);
DataRefImpl DR = Reloc.getRawDataRefImpl();
MachO::any_relocation_info RE = Obj->getRelocation(DR);
bool IsScattered = Obj->isRelocationScattered(RE);
bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
StringRef TargetName;
if (IsExtern) {
symbol_iterator Symbol = Reloc.getSymbol();
if (Symbol != Obj->symbol_end()) {
TargetName = getSymbolName(*Symbol);
}
} else if (!IsScattered) {
section_iterator SecI = Obj->getRelocationSection(DR);
if (SecI != Obj->section_end())
TargetName = unwrapOrError(Obj->getFileName(), SecI->getName());
}
if (TargetName.empty())
TargetName = "-";
if (opts::ExpandRelocs) {
DictScope Group(W, "Relocation");
W.printHex("Offset", Offset);
W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
W.printNumber("Length", Obj->getAnyRelocationLength(RE));
W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
if (IsScattered) {
W.printHex("Value", Obj->getScatteredRelocationValue(RE));
} else {
const char *Kind = IsExtern ? "Symbol" : "Section";
W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
}
} else {
SmallString<32> SymbolNameOrOffset("0x");
if (IsScattered) {
// Scattered relocations don't really have an associated symbol for some
// reason, even if one exists in the symtab at the correct address.
SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
} else {
SymbolNameOrOffset = TargetName;
}
raw_ostream& OS = W.startLine();
OS << W.hex(Offset)
<< " " << Obj->getAnyRelocationPCRel(RE)
<< " " << Obj->getAnyRelocationLength(RE);
if (IsScattered)
OS << " n/a";
else
OS << " " << Obj->getPlainRelocationExternal(RE);
OS << " " << RelocName
<< " " << IsScattered
<< " " << SymbolNameOrOffset
<< "\n";
}
}
StringRef MachODumper::getSymbolName(const SymbolRef &Symbol) const {
Expected<StringRef> SymbolNameOrErr = Symbol.getName();
if (!SymbolNameOrErr) {
reportError(SymbolNameOrErr.takeError(), Obj->getFileName());
}
return *SymbolNameOrErr;
}
uint8_t MachODumper::getSymbolType(const SymbolRef &Symbol) const {
return Obj->is64Bit()
? Obj->getSymbol64TableEntry(Symbol.getRawDataRefImpl()).n_type
: Obj->getSymbolTableEntry(Symbol.getRawDataRefImpl()).n_type;
}
bool MachODumper::compareSymbolsByName(SymbolRef LHS, SymbolRef RHS) const {
return getSymbolName(LHS).str().compare(getSymbolName(RHS).str()) < 0;
}
bool MachODumper::compareSymbolsByType(SymbolRef LHS, SymbolRef RHS) const {
return getSymbolType(LHS) < getSymbolType(RHS);
}
void MachODumper::printSymbols() { printSymbols(std::nullopt); }
void MachODumper::printSymbols(std::optional<SymbolComparator> SymComp) {
ListScope Group(W, "Symbols");
if (SymComp) {
auto SymbolRange = Obj->symbols();
std::vector<SymbolRef> SortedSymbols(SymbolRange.begin(),
SymbolRange.end());
llvm::stable_sort(SortedSymbols, *SymComp);
for (SymbolRef Symbol : SortedSymbols)
printSymbol(Symbol);
} else {
for (const SymbolRef &Symbol : Obj->symbols()) {
printSymbol(Symbol);
}
}
}
void MachODumper::printDynamicSymbols() {
ListScope Group(W, "DynamicSymbols");
}
void MachODumper::printDynamicSymbols(std::optional<SymbolComparator> SymComp) {
ListScope Group(W, "DynamicSymbols");
}
void MachODumper::printSymbol(const SymbolRef &Symbol) {
printSymbol(Symbol, W);
}
void MachODumper::printSymbol(const SymbolRef &Symbol, ScopedPrinter &W) {
StringRef SymbolName = getSymbolName(Symbol);
MachOSymbol MOSymbol;
getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
StringRef SectionName = "";
// Don't ask a Mach-O STABS symbol for its section unless we know that
// STAB symbol's section field refers to a valid section index. Otherwise
// the symbol may error trying to load a section that does not exist.
// TODO: Add a whitelist of STABS symbol types that contain valid section
// indices.
if (!(MOSymbol.Type & MachO::N_STAB)) {
Expected<section_iterator> SecIOrErr = Symbol.getSection();
if (!SecIOrErr)
reportError(SecIOrErr.takeError(), Obj->getFileName());
section_iterator SecI = *SecIOrErr;
if (SecI != Obj->section_end())
SectionName = unwrapOrError(Obj->getFileName(), SecI->getName());
}
DictScope D(W, "Symbol");
W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
if (MOSymbol.Type & MachO::N_STAB) {
W.printHex("Type", "SymDebugTable", MOSymbol.Type);
} else {
if (MOSymbol.Type & MachO::N_PEXT)
W.startLine() << "PrivateExtern\n";
if (MOSymbol.Type & MachO::N_EXT)
W.startLine() << "Extern\n";
W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
ArrayRef(MachOSymbolTypes));
}
W.printHex("Section", SectionName, MOSymbol.SectionIndex);
W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0x7),
ArrayRef(MachOSymbolRefTypes));
W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0x7),
ArrayRef(MachOSymbolFlags));
W.printHex("Value", MOSymbol.Value);
}
void MachODumper::printUnwindInfo() {
W.startLine() << "UnwindInfo not implemented.\n";
}
void MachODumper::printStackMap() const {
object::SectionRef StackMapSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
if (Expected<StringRef> NameOrErr = Sec.getName())
Name = *NameOrErr;
else
consumeError(NameOrErr.takeError());
if (Name == "__llvm_stackmaps") {
StackMapSection = Sec;
break;
}
}
if (StackMapSection == object::SectionRef())
return;
StringRef StackMapContents =
unwrapOrError(Obj->getFileName(), StackMapSection.getContents());
ArrayRef<uint8_t> StackMapContentsArray =
arrayRefFromStringRef(StackMapContents);
if (Obj->isLittleEndian())
prettyPrintStackMap(
W, StackMapParser<support::little>(StackMapContentsArray));
else
prettyPrintStackMap(
W, StackMapParser<support::big>(StackMapContentsArray));
}
void MachODumper::printCGProfile() {
object::SectionRef CGProfileSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
if (Expected<StringRef> NameOrErr = Sec.getName())
Name = *NameOrErr;
else
consumeError(NameOrErr.takeError());
if (Name == "__cg_profile") {
CGProfileSection = Sec;
break;
}
}
if (CGProfileSection == object::SectionRef())
return;
StringRef CGProfileContents =
unwrapOrError(Obj->getFileName(), CGProfileSection.getContents());
BinaryStreamReader Reader(CGProfileContents, Obj->isLittleEndian()
? llvm::support::little
: llvm::support::big);
ListScope L(W, "CGProfile");
while (!Reader.empty()) {
uint32_t FromIndex, ToIndex;
uint64_t Count;
if (Error Err = Reader.readInteger(FromIndex))
reportError(std::move(Err), Obj->getFileName());
if (Error Err = Reader.readInteger(ToIndex))
reportError(std::move(Err), Obj->getFileName());
if (Error Err = Reader.readInteger(Count))
reportError(std::move(Err), Obj->getFileName());
DictScope D(W, "CGProfileEntry");
W.printNumber("From", getSymbolName(*Obj->getSymbolByIndex(FromIndex)),
FromIndex);
W.printNumber("To", getSymbolName(*Obj->getSymbolByIndex(ToIndex)),
ToIndex);
W.printNumber("Weight", Count);
}
}
void MachODumper::printNeededLibraries() {
ListScope D(W, "NeededLibraries");
using LibsTy = std::vector<StringRef>;
LibsTy Libs;
for (const auto &Command : Obj->load_commands()) {
if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
Command.C.cmd == MachO::LC_ID_DYLIB ||
Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
if (Dl.dylib.name < Dl.cmdsize) {
auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name;
Libs.push_back(P);
}
}
}
llvm::stable_sort(Libs);
for (const auto &L : Libs) {
W.startLine() << L << "\n";
}
}
void MachODumper::printMachODataInCode() {
for (const auto &Load : Obj->load_commands()) {
if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
DictScope Group(W, "DataInCode");
W.printNumber("Data offset", LLC.dataoff);
W.printNumber("Data size", LLC.datasize);
ListScope D(W, "Data entries");
unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
for (unsigned i = 0; i < NumRegions; ++i) {
MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
LLC.dataoff, i);
DictScope Group(W, "Entry");
W.printNumber("Index", i);
W.printNumber("Offset", DICE.offset);
W.printNumber("Length", DICE.length);
W.printNumber("Kind", DICE.kind);
}
}
}
}
void MachODumper::printMachOVersionMin() {
for (const auto &Load : Obj->load_commands()) {
StringRef Cmd;
switch (Load.C.cmd) {
case MachO::LC_VERSION_MIN_MACOSX:
Cmd = "LC_VERSION_MIN_MACOSX";
break;
case MachO::LC_VERSION_MIN_IPHONEOS:
Cmd = "LC_VERSION_MIN_IPHONEOS";
break;
case MachO::LC_VERSION_MIN_TVOS:
Cmd = "LC_VERSION_MIN_TVOS";
break;
case MachO::LC_VERSION_MIN_WATCHOS:
Cmd = "LC_VERSION_MIN_WATCHOS";
break;
case MachO::LC_BUILD_VERSION:
Cmd = "LC_BUILD_VERSION";
break;
default:
continue;
}
DictScope Group(W, "MinVersion");
// Handle LC_BUILD_VERSION.
if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load);
W.printString("Cmd", Cmd);
W.printNumber("Size", BVC.cmdsize);
W.printString("Platform",
MachOObjectFile::getBuildPlatform(BVC.platform));
W.printString("Version", MachOObjectFile::getVersionString(BVC.minos));
if (BVC.sdk)
W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk));
else
W.printString("SDK", StringRef("n/a"));
continue;
}
MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
W.printString("Cmd", Cmd);
W.printNumber("Size", VMC.cmdsize);
SmallString<32> Version;
Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
if (Update != 0)
Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
W.printString("Version", Version);
SmallString<32> SDK;
if (VMC.sdk == 0)
SDK = "n/a";
else {
SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
if (Update != 0)
SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
}
W.printString("SDK", SDK);
}
}
void MachODumper::printMachODysymtab() {
for (const auto &Load : Obj->load_commands()) {
if (Load.C.cmd == MachO::LC_DYSYMTAB) {
MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
DictScope Group(W, "Dysymtab");
W.printNumber("ilocalsym", DLC.ilocalsym);
W.printNumber("nlocalsym", DLC.nlocalsym);
W.printNumber("iextdefsym", DLC.iextdefsym);
W.printNumber("nextdefsym", DLC.nextdefsym);
W.printNumber("iundefsym", DLC.iundefsym);
W.printNumber("nundefsym", DLC.nundefsym);
W.printNumber("tocoff", DLC.tocoff);
W.printNumber("ntoc", DLC.ntoc);
W.printNumber("modtaboff", DLC.modtaboff);
W.printNumber("nmodtab", DLC.nmodtab);
W.printNumber("extrefsymoff", DLC.extrefsymoff);
W.printNumber("nextrefsyms", DLC.nextrefsyms);
W.printNumber("indirectsymoff", DLC.indirectsymoff);
W.printNumber("nindirectsyms", DLC.nindirectsyms);
W.printNumber("extreloff", DLC.extreloff);
W.printNumber("nextrel", DLC.nextrel);
W.printNumber("locreloff", DLC.locreloff);
W.printNumber("nlocrel", DLC.nlocrel);
}
}
}
void MachODumper::printMachOSegment() {
for (const auto &Load : Obj->load_commands()) {
if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
MachOSegment MOSegment;
getSegment(Obj, Load, MOSegment);
DictScope Group(W, "Segment");
W.printString("Cmd", MOSegment.CmdName);
W.printString("Name", MOSegment.SegName);
W.printNumber("Size", MOSegment.cmdsize);
W.printHex("vmaddr", MOSegment.vmaddr);
W.printHex("vmsize", MOSegment.vmsize);
W.printNumber("fileoff", MOSegment.fileoff);
W.printNumber("filesize", MOSegment.filesize);
W.printString("maxprot", getMask(MOSegment.maxprot));
W.printString("initprot", getMask(MOSegment.initprot));
W.printNumber("nsects", MOSegment.nsects);
W.printHex("flags", MOSegment.flags);
}
}
}
void MachODumper::printMachOIndirectSymbols() {
for (const auto &Load : Obj->load_commands()) {
if (Load.C.cmd == MachO::LC_DYSYMTAB) {
MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
DictScope Group(W, "Indirect Symbols");
W.printNumber("Number", DLC.nindirectsyms);
ListScope D(W, "Symbols");
for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
DictScope Group(W, "Entry");
W.printNumber("Entry Index", i);
W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
}
}
}
}
void MachODumper::printMachOLinkerOptions() {
for (const auto &Load : Obj->load_commands()) {
if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
DictScope Group(W, "Linker Options");
W.printNumber("Size", LOLC.cmdsize);
ListScope D(W, "Strings");
uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
StringRef Data(P, DataSize);
for (unsigned i = 0; i < LOLC.count; ++i) {
std::pair<StringRef,StringRef> Split = Data.split('\0');
W.printString("Value", Split.first);
Data = Split.second;
}
}
}
}
|