-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathMachO.cpp
More file actions
721 lines (666 loc) · 24.7 KB
/
MachO.cpp
File metadata and controls
721 lines (666 loc) · 24.7 KB
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
#include "MachO.h"
#include "Utility.h"
#include "SharedCache.h"
#include "VirtualMemory.h"
using namespace BinaryNinja;
std::vector<uint64_t> SharedCacheMachOHeader::ReadFunctionTable(VirtualMemory& vm) const
{
// NOTE: The funcoff is relative to the file of the linkedit segment.
uint64_t funcStartsAddress = GetLinkEditFileBase() + functionStarts.funcoff;
auto funcStarts = vm.ReadBuffer(funcStartsAddress, functionStarts.funcsize);
uint64_t curfunc = textBase;
uint64_t curOffset = 0;
std::vector<uint64_t> functionTable = {};
auto current = static_cast<const uint8_t*>(funcStarts.GetData());
auto end = current + funcStarts.GetLength();
while (current != end)
{
curOffset = readLEB128(current, end);
// TODO: Verify this is the correct behavior.
// Skip unmapped functions.
if (curOffset == 0 || !vm.IsAddressMapped(curfunc))
continue;
curfunc += curOffset;
uint64_t target = curfunc;
functionTable.push_back(target);
}
return functionTable;
}
std::optional<SharedCacheMachOHeader> SharedCacheMachOHeader::ParseHeaderForAddress(
std::shared_ptr<VirtualMemory> vm, uint64_t address, const std::string& imagePath)
{
// Sanity check to make sure that the header is mapped.
// This should really only fail if we didn't grab all the required entries.
if (!vm->IsAddressMapped(address))
return std::nullopt;
SharedCacheMachOHeader header;
header.textBase = address;
header.installName = imagePath;
// The identifierPrefix is used for the display of the image name in the sections and segments.
header.identifierPrefix = BaseFileName(imagePath);
std::string errorMsg;
VirtualMemoryReader reader(vm);
reader.Seek(address);
header.ident.magic = reader.ReadUInt32();
BNEndianness endianness;
switch (header.ident.magic)
{
case MH_MAGIC:
case MH_MAGIC_64:
endianness = LittleEndian;
break;
case MH_CIGAM:
case MH_CIGAM_64:
endianness = BigEndian;
break;
default:
return {};
}
reader.SetEndianness(endianness);
header.ident.cputype = reader.ReadUInt32();
header.ident.cpusubtype = reader.ReadUInt32();
header.ident.filetype = reader.ReadUInt32();
header.ident.ncmds = reader.ReadUInt32();
header.ident.sizeofcmds = reader.ReadUInt32();
header.ident.flags = reader.ReadUInt32();
if ((header.ident.cputype & MachOABIMask) == MachOABI64) // address size == 8
{
header.ident.reserved = reader.ReadUInt32();
}
header.loadCommandOffset = reader.GetOffset();
bool first = true;
// Parse segment commands
try
{
for (size_t i = 0; i < header.ident.ncmds; i++)
{
// BNLogInfo("of 0x%llx", reader.GetOffset());
load_command load;
segment_command_64 segment64;
section_64 sect = {};
size_t curOffset = reader.GetOffset();
load.cmd = reader.ReadUInt32();
load.cmdsize = reader.ReadUInt32();
size_t nextOffset = curOffset + load.cmdsize;
if (load.cmdsize < sizeof(load_command))
return {};
switch (load.cmd)
{
case LC_MAIN:
{
uint64_t entryPoint = reader.ReadUInt64();
header.entryPoints.push_back({entryPoint, true});
(void)reader.ReadUInt64(); // Stack start
break;
}
case LC_SEGMENT: // map the 32bit version to 64 bits
segment64.cmd = LC_SEGMENT_64;
reader.Read(&segment64.segname, 16);
segment64.vmaddr = reader.ReadUInt32();
segment64.vmsize = reader.ReadUInt32();
segment64.fileoff = reader.ReadUInt32();
segment64.filesize = reader.ReadUInt32();
segment64.maxprot = reader.ReadUInt32();
segment64.initprot = reader.ReadUInt32();
segment64.nsects = reader.ReadUInt32();
segment64.flags = reader.ReadUInt32();
if (first)
{
if (!((header.ident.flags & MH_SPLIT_SEGS) || header.ident.cputype == MACHO_CPU_TYPE_X86_64)
|| (segment64.flags & MACHO_VM_PROT_WRITE))
{
header.relocationBase = segment64.vmaddr;
first = false;
}
}
for (size_t j = 0; j < segment64.nsects; j++)
{
reader.Read(§.sectname, 16);
reader.Read(§.segname, 16);
sect.addr = reader.ReadUInt32();
sect.size = reader.ReadUInt32();
sect.offset = reader.ReadUInt32();
sect.align = reader.ReadUInt32();
sect.reloff = reader.ReadUInt32();
sect.nreloc = reader.ReadUInt32();
sect.flags = reader.ReadUInt32();
sect.reserved1 = reader.ReadUInt32();
sect.reserved2 = reader.ReadUInt32();
// if the segment isn't mapped into virtual memory don't add the corresponding sections.
if (segment64.vmsize > 0)
{
header.sections.push_back(sect);
}
if (!strncmp(sect.sectname, "__mod_init_func", 15))
header.moduleInitSections.push_back(sect);
if ((sect.flags & (S_ATTR_SELF_MODIFYING_CODE | S_SYMBOL_STUBS))
== (S_ATTR_SELF_MODIFYING_CODE | S_SYMBOL_STUBS))
header.symbolStubSections.push_back(sect);
if ((sect.flags & S_NON_LAZY_SYMBOL_POINTERS) == S_NON_LAZY_SYMBOL_POINTERS)
header.symbolPointerSections.push_back(sect);
if ((sect.flags & S_LAZY_SYMBOL_POINTERS) == S_LAZY_SYMBOL_POINTERS)
header.symbolPointerSections.push_back(sect);
}
header.segments.push_back(segment64);
break;
case LC_SEGMENT_64:
segment64.cmd = LC_SEGMENT_64;
reader.Read(&segment64.segname, 16);
segment64.vmaddr = reader.ReadUInt64();
segment64.vmsize = reader.ReadUInt64();
segment64.fileoff = reader.ReadUInt64();
segment64.filesize = reader.ReadUInt64();
segment64.maxprot = reader.ReadUInt32();
segment64.initprot = reader.ReadUInt32();
segment64.nsects = reader.ReadUInt32();
segment64.flags = reader.ReadUInt32();
if (strncmp(segment64.segname, "__LINKEDIT", 10) == 0)
{
header.linkeditSegment = segment64;
header.linkeditPresent = true;
}
if (first)
{
if (!((header.ident.flags & MH_SPLIT_SEGS) || header.ident.cputype == MACHO_CPU_TYPE_X86_64)
|| (segment64.flags & MACHO_VM_PROT_WRITE))
{
header.relocationBase = segment64.vmaddr;
first = false;
}
}
for (size_t j = 0; j < segment64.nsects; j++)
{
reader.Read(§.sectname, 16);
reader.Read(§.segname, 16);
sect.addr = reader.ReadUInt64();
sect.size = reader.ReadUInt64();
sect.offset = reader.ReadUInt32();
sect.align = reader.ReadUInt32();
sect.reloff = reader.ReadUInt32();
sect.nreloc = reader.ReadUInt32();
sect.flags = reader.ReadUInt32();
sect.reserved1 = reader.ReadUInt32();
sect.reserved2 = reader.ReadUInt32();
sect.reserved3 = reader.ReadUInt32();
// if the segment isn't mapped into virtual memory don't add the corresponding sections.
if (segment64.vmsize > 0)
{
header.sections.push_back(sect);
}
if (!strncmp(sect.sectname, "__mod_init_func", 15))
header.moduleInitSections.push_back(sect);
if ((sect.flags & (S_ATTR_SELF_MODIFYING_CODE | S_SYMBOL_STUBS))
== (S_ATTR_SELF_MODIFYING_CODE | S_SYMBOL_STUBS))
header.symbolStubSections.push_back(sect);
if ((sect.flags & S_NON_LAZY_SYMBOL_POINTERS) == S_NON_LAZY_SYMBOL_POINTERS)
header.symbolPointerSections.push_back(sect);
if ((sect.flags & S_LAZY_SYMBOL_POINTERS) == S_LAZY_SYMBOL_POINTERS)
header.symbolPointerSections.push_back(sect);
}
header.segments.push_back(segment64);
break;
case LC_ROUTINES: // map the 32bit version to 64bits
header.routines64.cmd = LC_ROUTINES_64;
header.routines64.init_address = reader.ReadUInt32();
header.routines64.init_module = reader.ReadUInt32();
header.routines64.reserved1 = reader.ReadUInt32();
header.routines64.reserved2 = reader.ReadUInt32();
header.routines64.reserved3 = reader.ReadUInt32();
header.routines64.reserved4 = reader.ReadUInt32();
header.routines64.reserved5 = reader.ReadUInt32();
header.routines64.reserved6 = reader.ReadUInt32();
header.routinesPresent = true;
break;
case LC_ROUTINES_64:
header.routines64.cmd = LC_ROUTINES_64;
header.routines64.init_address = reader.ReadUInt64();
header.routines64.init_module = reader.ReadUInt64();
header.routines64.reserved1 = reader.ReadUInt64();
header.routines64.reserved2 = reader.ReadUInt64();
header.routines64.reserved3 = reader.ReadUInt64();
header.routines64.reserved4 = reader.ReadUInt64();
header.routines64.reserved5 = reader.ReadUInt64();
header.routines64.reserved6 = reader.ReadUInt64();
header.routinesPresent = true;
break;
case LC_FUNCTION_STARTS:
header.functionStarts.funcoff = reader.ReadUInt32();
header.functionStarts.funcsize = reader.ReadUInt32();
header.functionStartsPresent = true;
break;
case LC_SYMTAB:
header.symtab.symoff = reader.ReadUInt32();
header.symtab.nsyms = reader.ReadUInt32();
header.symtab.stroff = reader.ReadUInt32();
header.symtab.strsize = reader.ReadUInt32();
break;
case LC_DYSYMTAB:
header.dysymtab.ilocalsym = reader.ReadUInt32();
header.dysymtab.nlocalsym = reader.ReadUInt32();
header.dysymtab.iextdefsym = reader.ReadUInt32();
header.dysymtab.nextdefsym = reader.ReadUInt32();
header.dysymtab.iundefsym = reader.ReadUInt32();
header.dysymtab.nundefsym = reader.ReadUInt32();
header.dysymtab.tocoff = reader.ReadUInt32();
header.dysymtab.ntoc = reader.ReadUInt32();
header.dysymtab.modtaboff = reader.ReadUInt32();
header.dysymtab.nmodtab = reader.ReadUInt32();
header.dysymtab.extrefsymoff = reader.ReadUInt32();
header.dysymtab.nextrefsyms = reader.ReadUInt32();
header.dysymtab.indirectsymoff = reader.ReadUInt32();
header.dysymtab.nindirectsyms = reader.ReadUInt32();
header.dysymtab.extreloff = reader.ReadUInt32();
header.dysymtab.nextrel = reader.ReadUInt32();
header.dysymtab.locreloff = reader.ReadUInt32();
header.dysymtab.nlocrel = reader.ReadUInt32();
header.dysymPresent = true;
break;
case LC_DYLD_CHAINED_FIXUPS:
header.chainedFixups.dataoff = reader.ReadUInt32();
header.chainedFixups.datasize = reader.ReadUInt32();
header.chainedFixupsPresent = true;
break;
case LC_DYLD_INFO:
case LC_DYLD_INFO_ONLY:
header.dyldInfo.rebase_off = reader.ReadUInt32();
header.dyldInfo.rebase_size = reader.ReadUInt32();
header.dyldInfo.bind_off = reader.ReadUInt32();
header.dyldInfo.bind_size = reader.ReadUInt32();
header.dyldInfo.weak_bind_off = reader.ReadUInt32();
header.dyldInfo.weak_bind_size = reader.ReadUInt32();
header.dyldInfo.lazy_bind_off = reader.ReadUInt32();
header.dyldInfo.lazy_bind_size = reader.ReadUInt32();
header.dyldInfo.export_off = reader.ReadUInt32();
header.dyldInfo.export_size = reader.ReadUInt32();
header.exportTrie.dataoff = header.dyldInfo.export_off;
header.exportTrie.datasize = header.dyldInfo.export_size;
header.exportTriePresent = true;
header.dyldInfoPresent = true;
break;
case LC_DYLD_EXPORTS_TRIE:
header.exportTrie.dataoff = reader.ReadUInt32();
header.exportTrie.datasize = reader.ReadUInt32();
header.exportTriePresent = true;
break;
case LC_THREAD:
case LC_UNIXTHREAD:
/*while (reader.GetOffset() < nextOffset)
{
thread_command thread;
thread.flavor = reader.ReadUInt32();
thread.count = reader.ReadUInt32();
switch (m_archId)
{
case MachOx64:
m_logger->LogDebug("x86_64 Thread state\n");
if (thread.flavor != X86_THREAD_STATE64)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
//This wont be big endian so we can just read the whole thing
reader.Read(&thread.statex64, sizeof(thread.statex64));
header.entryPoints.push_back({thread.statex64.rip, false});
break;
case MachOx86:
m_logger->LogDebug("x86 Thread state\n");
if (thread.flavor != X86_THREAD_STATE32)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
//This wont be big endian so we can just read the whole thing
reader.Read(&thread.statex86, sizeof(thread.statex86));
header.entryPoints.push_back({thread.statex86.eip, false});
break;
case MachOArm:
m_logger->LogDebug("Arm Thread state\n");
if (thread.flavor != _ARM_THREAD_STATE)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
//This wont be big endian so we can just read the whole thing
reader.Read(&thread.statearmv7, sizeof(thread.statearmv7));
header.entryPoints.push_back({thread.statearmv7.r15, false});
break;
case MachOAarch64:
case MachOAarch6432:
m_logger->LogDebug("Aarch64 Thread state\n");
if (thread.flavor != _ARM_THREAD_STATE64)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
reader.Read(&thread.stateaarch64, sizeof(thread.stateaarch64));
header.entryPoints.push_back({thread.stateaarch64.pc, false});
break;
case MachOPPC:
m_logger->LogDebug("PPC Thread state\n");
if (thread.flavor != PPC_THREAD_STATE)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
//Read individual entries for endian reasons
header.entryPoints.push_back({reader.ReadUInt32(), false});
(void)reader.ReadUInt32();
(void)reader.ReadUInt32();
//Read the rest of the structure
(void)reader.Read(&thread.stateppc.r1, sizeof(thread.stateppc) - (3 * 4));
break;
case MachOPPC64:
m_logger->LogDebug("PPC64 Thread state\n");
if (thread.flavor != PPC_THREAD_STATE64)
{
reader.SeekRelative(thread.count * sizeof(uint32_t));
break;
}
header.entryPoints.push_back({reader.ReadUInt64(), false});
(void)reader.ReadUInt64();
(void)reader.ReadUInt64(); // Stack start
(void)reader.Read(&thread.stateppc64.r1, sizeof(thread.stateppc64) - (3 * 8));
break;
default:
m_logger->LogError("Unknown archid: %x", m_archId);
}
}*/
break;
case LC_LOAD_DYLIB:
{
uint32_t offset = reader.ReadUInt32();
if (offset < nextOffset)
{
reader.Seek(curOffset + offset);
std::string libname = reader.ReadCString(reader.GetOffset());
header.dylibs.push_back(libname);
}
}
break;
case LC_BUILD_VERSION:
{
// m_logger->LogDebug("LC_BUILD_VERSION:");
header.buildVersion.platform = reader.ReadUInt32();
header.buildVersion.minos = reader.ReadUInt32();
header.buildVersion.sdk = reader.ReadUInt32();
header.buildVersion.ntools = reader.ReadUInt32();
// m_logger->LogDebug("Platform: %s", BuildPlatformToString(header.buildVersion.platform).c_str());
// m_logger->LogDebug("MinOS: %s", BuildToolVersionToString(header.buildVersion.minos).c_str());
// m_logger->LogDebug("SDK: %s", BuildToolVersionToString(header.buildVersion.sdk).c_str());
for (uint32_t j = 0; (i < header.buildVersion.ntools) && (j < 10); j++)
{
uint32_t tool = reader.ReadUInt32();
uint32_t version = reader.ReadUInt32();
header.buildToolVersions.push_back({tool, version});
// m_logger->LogDebug("Build Tool: %s: %s", BuildToolToString(tool).c_str(),
// BuildToolVersionToString(version).c_str());
}
break;
}
case LC_FILESET_ENTRY:
{
throw ReadException();
}
default:
// m_logger->LogDebug("Unhandled command: %s : %" PRIu32 "\n", CommandToString(load.cmd).c_str(),
// load.cmdsize);
break;
}
if (reader.GetOffset() != nextOffset)
{
// m_logger->LogDebug("Didn't parse load command: %s fully %" PRIx64 ":%" PRIxPTR,
// CommandToString(load.cmd).c_str(), reader.GetOffset(), nextOffset);
}
reader.Seek(nextOffset);
}
for (auto& section : header.sections)
{
char sectionName[17];
memcpy(sectionName, section.sectname, sizeof(section.sectname));
sectionName[16] = 0;
char segmentName[sizeof(section.segname)+1];
memcpy(segmentName, section.segname, sizeof(section.segname));
segmentName[sizeof(segmentName)-1] = 0;
// Section names used to be image name and section only but some images have duplicate section names
// so we now also use the segment name, this also is more close to what is seen with LLVM.
// Justification: https://github.com/Vector35/binaryninja-api/pull/6454#issuecomment-2777465476
if (header.identifierPrefix.empty())
header.sectionNames.push_back(fmt::format("{}.{}", segmentName, sectionName));
else
header.sectionNames.push_back(fmt::format("{}::{}.{}", header.identifierPrefix, segmentName, sectionName));
}
}
catch (ReadException&)
{
return {};
}
return header;
}
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(VirtualMemory& vm, const TableInfo &symbolInfo, const TableInfo &stringInfo,
BNSymbolBinding bindingOverride) const
{
std::vector<CacheSymbol> symbolList;
// TODO: This assumes that 95% (or more) are going to be added.
symbolList.reserve(symbolInfo.entries);
for (uint64_t entryIndex = 0; entryIndex < symbolInfo.entries; entryIndex++)
{
nlist_64 nlist = {};
if (vm.GetAddressSize() == 4)
{
// 32-bit DSC
struct nlist nlist32 = {};
vm.Read(&nlist, symbolInfo.address + (entryIndex * sizeof(nlist32)), sizeof(nlist32));
nlist.n_strx = nlist32.n_strx;
nlist.n_type = nlist32.n_type;
nlist.n_sect = nlist32.n_sect;
nlist.n_desc = nlist32.n_desc;
nlist.n_value = nlist32.n_value;
}
else
{
// 64-bit DSC
vm.Read(&nlist, symbolInfo.address + (entryIndex * sizeof(nlist)), sizeof(nlist));
}
auto symbolAddress = nlist.n_value;
if (((nlist.n_type & N_TYPE) == N_INDR) || symbolAddress == 0)
continue;
if (nlist.n_strx >= stringInfo.entries)
{
// TODO: where logger?
LogErrorF(
"Symbol entry at index {} has a string offset of {:#x} which is outside the strings buffer of size {:#x} "
"for symbol table {:#x}",
entryIndex, nlist.n_strx, stringInfo.address, stringInfo.entries);
continue;
}
std::string symbolName = vm.ReadCString(stringInfo.address + nlist.n_strx);
if (symbolName == "<redacted>")
continue;
std::optional<BNSymbolType> symbolType;
if ((nlist.n_type & N_TYPE) == N_SECT && nlist.n_sect > 0 && (size_t)(nlist.n_sect - 1) < sections.size())
symbolType = DataSymbol;
else if ((nlist.n_type & N_TYPE) == N_ABS)
symbolType = DataSymbol;
else if ((nlist.n_type & N_EXT))
symbolType = ExternalSymbol;
if (!symbolType.has_value())
{
// TODO: Where logger?
LogErrorF("Symbol {:?} at address {:#x} has unknown symbol type", symbolName.c_str(), symbolAddress);
continue;
}
std::optional<uint32_t> flags;
for (auto s : sections)
{
if (s.addr <= symbolAddress && symbolAddress < s.addr + s.size)
{
// First section to contain the address we will use its flags.
flags = s.flags;
break;
}
}
if (symbolType != ExternalSymbol)
{
if (!flags.has_value())
{
// TODO: where logger?
LogErrorF("Symbol {:?} at address {:#x} is not in any section", symbolName.c_str(), symbolAddress);
continue;
}
if ((flags.value() & S_ATTR_PURE_INSTRUCTIONS) == S_ATTR_PURE_INSTRUCTIONS
|| (flags.value() & S_ATTR_SOME_INSTRUCTIONS) == S_ATTR_SOME_INSTRUCTIONS)
symbolType = FunctionSymbol;
else
symbolType = DataSymbol;
}
if ((nlist.n_desc & N_ARM_THUMB_DEF) == N_ARM_THUMB_DEF)
symbolAddress++;
BNSymbolBinding symbolBinding = GlobalBinding;
if (bindingOverride != NoBinding)
symbolBinding = bindingOverride;
else if (dysymPresent && dysymtab.nlocalsym && entryIndex >= dysymtab.ilocalsym && entryIndex < dysymtab.ilocalsym + dysymtab.nlocalsym)
symbolBinding = LocalBinding;
else if (nlist.n_desc & N_WEAK_DEF)
symbolBinding = WeakBinding;
symbolList.emplace_back(symbolType.value(), symbolBinding, symbolAddress, std::move(symbolName));
}
return symbolList;
}
bool SharedCacheMachOHeader::AddExportTerminalSymbol(
std::vector<CacheSymbol>& symbols, const std::string& symbolName, const uint8_t *current, const uint8_t *end) const
{
uint64_t symbolFlags = readValidULEB128(current, end);
if (symbolFlags & EXPORT_SYMBOL_FLAGS_REEXPORT)
return false;
uint64_t imageOffset = readValidULEB128(current, end);
uint64_t symbolAddress = textBase + imageOffset;
if (symbolName.empty() || symbolAddress == 0)
return false;
// Export trie entries are exported by definition.
BNSymbolBinding symbolBinding = (symbolFlags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION) ? WeakBinding : GlobalBinding;
// Tries to get the symbol type based off the section containing it.
auto sectionSymbolType = [&]() -> BNSymbolType {
uint32_t sectionFlags = 0;
for (const auto& section : sections)
{
if (symbolAddress >= section.addr && symbolAddress < section.addr + section.size)
{
// Take the flags from the first containing section.
sectionFlags = section.flags;
break;
}
}
// TODO: Is this enough to determine a function symbol?
// TODO: Might be the cause of https://github.com/Vector35/binaryninja-api/issues/6526
// Check the sections flags to see if we actually have a function symbol instead.
if (sectionFlags & S_ATTR_PURE_INSTRUCTIONS || sectionFlags & S_ATTR_SOME_INSTRUCTIONS)
return FunctionSymbol;
// By default, just return data symbol.
return DataSymbol;
};
switch (symbolFlags & EXPORT_SYMBOL_FLAGS_KIND_MASK)
{
case EXPORT_SYMBOL_FLAGS_KIND_REGULAR:
case EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL:
symbols.emplace_back(sectionSymbolType(), symbolBinding, symbolAddress, symbolName);
break;
case EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE:
symbols.emplace_back(DataSymbol, symbolBinding, symbolAddress, symbolName);
break;
default:
LogWarnF("Unhandled export symbol kind: {:#x}", symbolFlags & EXPORT_SYMBOL_FLAGS_KIND_MASK);
return false;
}
return true;
}
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadExportSymbolTrie(VirtualMemory& vm) const
{
// nothing to do if there’s no export‐trie
if (exportTrie.datasize == 0 || exportTrie.dataoff == 0)
return {};
std::vector<CacheSymbol> symbols = {};
try {
auto trieSpan = vm.ReadSpan(GetLinkEditFileBase() + exportTrie.dataoff, exportTrie.datasize);
const uint8_t *begin = trieSpan.data();
const uint8_t *end = begin + trieSpan.size();
const uint8_t *cursor = begin;
struct Node
{
const uint8_t* cursor;
std::string text;
};
std::vector<Node> stack;
stack.reserve(64);
stack.push_back({ /* cursor */ begin, /* text */ "" });
while (!stack.empty())
{
Node node = std::move(stack.back());
stack.pop_back();
cursor = node.cursor;
const std::string currentText = std::move(node.text);
if (cursor > end)
{
LogError("Export Trie: Cursor left trie during initial bounds check");
throw ReadException();
}
uint64_t terminalSize = readValidULEB128(cursor, end);
const uint8_t* childCursor = cursor + terminalSize;
// If there's terminal data, define the symbol
if (terminalSize != 0)
{
AddExportTerminalSymbol(symbols, currentText, cursor, end);
}
cursor = childCursor;
if (cursor > end)
{
LogError("Export Trie: Cursor left trie while moving to child offset");
throw ReadException();
}
uint8_t childCount = *cursor;
cursor++;
if (cursor > end)
{
LogError("Export Trie: Cursor left trie while reading child count");
throw ReadException();
}
std::vector<Node> children;
children.reserve(childCount);
for (uint8_t i = 0; i < childCount; ++i)
{
if (cursor > end)
{
LogError("Export Trie: Cursor left trie while reading children");
throw ReadException();
}
std::string childText;
while (cursor <= end && *cursor != 0) {
childText.push_back(*cursor);
cursor++;
}
cursor++; // skip the `\0`
if (cursor > end)
{
LogError("Export Trie: Cursor left trie while reading child text");
throw ReadException();
}
uint64_t nextOffset = readValidULEB128(cursor, end);
if (nextOffset == 0)
{
LogError("Export Trie: Child offset is zero");
throw ReadException();
}
children.push_back({ begin + nextOffset, currentText + childText });
}
// Push in reverse so that the first child is processed next
for (auto it = children.rbegin(); it != children.rend(); ++it)
{
stack.push_back(*it);
}
}
}
catch (std::exception&)
{
LogError("Export trie is malformed. Could not load Exported symbol names.");
}
return symbols;
}