summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/Pods/FirebaseCrashlytics/Crashlytics/Shared/FIRCLSMachO/FIRCLSMachO.m
blob: 9991121dd623b44284b0477aa2b9fe2d1adf548a (plain)
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
// Copyright 2019 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "Crashlytics/Shared/FIRCLSMachO/FIRCLSMachO.h"

#include <Foundation/Foundation.h>

#include <mach-o/dyld.h>
#include <mach-o/fat.h>
#include <mach-o/getsect.h>
#include <mach-o/ldsyms.h>

#include <sys/mman.h>
#include <sys/stat.h>

#include <dlfcn.h>
#include <fcntl.h>

#include <stdio.h>

#include <unistd.h>

// This is defined in newer versions of iOS/macOS in usr/include/mach/machine.h
#define CLS_CPU_SUBTYPE_ARM64E ((cpu_subtype_t)2)

static void FIRCLSMachOHeaderValues(FIRCLSMachOSliceRef slice,
                                    const struct load_command** cmds,
                                    uint32_t* cmdCount);
static bool FIRCLSMachOSliceIsValid(FIRCLSMachOSliceRef slice);

bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path) {
  struct stat statBuffer;

  if (!file || !path) {
    return false;
  }

  file->fd = 0;
  file->mappedFile = NULL;
  file->mappedSize = 0;

  file->fd = open(path, O_RDONLY);
  if (file->fd < 0) {
    // unable to open mach-o file
    return false;
  }

  if (fstat(file->fd, &statBuffer) == -1) {
    close(file->fd);
    return false;
  }

  // We need some minimum size for this to even be a possible mach-o file.  I believe
  // its probably quite a bit bigger than this, but this at least covers something.
  // We also need it to be a regular file.
  file->mappedSize = (size_t)statBuffer.st_size;
  if (statBuffer.st_size < 16 || !(statBuffer.st_mode & S_IFREG)) {
    close(file->fd);
    return false;
  }

  // Map the file to memory. MAP_SHARED can potentially reduce the amount of actual private
  // memory needed to do this mapping. Also, be sure to check for the correct failure result.
  file->mappedFile = mmap(0, file->mappedSize, PROT_READ, MAP_FILE | MAP_SHARED, file->fd, 0);
  if (!file->mappedFile || (file->mappedFile == MAP_FAILED)) {
    close(file->fd);
    return false;
  }

  return true;
}

bool FIRCLSMachOFileInitWithCurrent(FIRCLSMachOFileRef file) {
  struct FIRCLSMachOSlice slice = FIRCLSMachOSliceGetCurrent();

  const char* imagePath = FIRCLSMachOSliceGetExecutablePath(&slice);

  return FIRCLSMachOFileInitWithPath(file, imagePath);
}

void FIRCLSMachOFileDestroy(FIRCLSMachOFileRef file) {
  if (!file) {
    return;
  }

  if (file->mappedFile && file->mappedSize > 0) {
    munmap(file->mappedFile, file->mappedSize);
  }

  close(file->fd);
}

void FIRCLSMachOFileEnumerateSlices(FIRCLSMachOFileRef file, FIRCLSMachOSliceIterator block) {
  FIRCLSMachOEnumerateSlicesAtAddress(file->mappedFile, block);
}

void FIRCLSMachOEnumerateSlicesAtAddress(void* executableData, FIRCLSMachOSliceIterator block) {
  // check the magic value, to determine if we have a fat header or not
  uint32_t magicValue;
  uint32_t archCount;
  const struct fat_arch* fatArch;
  struct FIRCLSMachOSlice slice;

  memset(&slice, 0, sizeof(struct FIRCLSMachOSlice));

  magicValue = ((struct fat_header*)executableData)->magic;
  if ((magicValue != FAT_MAGIC) && (magicValue != FAT_CIGAM)) {
    slice.startAddress = executableData;

    // use this to fill in the values
    FIRCLSMachOHeaderValues(&slice, NULL, NULL);

    block(&slice);

    return;
  }

  archCount = OSSwapBigToHostInt32(((struct fat_header*)executableData)->nfat_arch);
  fatArch = executableData + sizeof(struct fat_header);

  for (uint32_t i = 0; i < archCount; ++i) {
    slice.cputype = OSSwapBigToHostInt32(fatArch->cputype);
    slice.cpusubtype = OSSwapBigToHostInt32(fatArch->cpusubtype);
    slice.startAddress = executableData + OSSwapBigToHostInt32(fatArch->offset);

    block(&slice);

    // advance to the next fat_arch structure
    fatArch = (struct fat_arch*)((uintptr_t)fatArch + sizeof(struct fat_arch));
  }
}

struct FIRCLSMachOSlice FIRCLSMachOFileSliceWithArchitectureName(FIRCLSMachOFileRef file,
                                                                 const char* name) {
  __block struct FIRCLSMachOSlice value;

  memset(&value, 0, sizeof(struct FIRCLSMachOSlice));

  FIRCLSMachOFileEnumerateSlices(file, ^(FIRCLSMachOSliceRef slice) {
    if (strcmp(FIRCLSMachOSliceGetArchitectureName(slice), name) == 0) {
      value = *slice;
    }
  });

  return value;
}

static void FIRCLSMachOHeaderValues(FIRCLSMachOSliceRef slice,
                                    const struct load_command** cmds,
                                    uint32_t* cmdCount) {
  const struct mach_header* header32 = (const struct mach_header*)slice->startAddress;
  const struct mach_header_64* header64 = (const struct mach_header_64*)slice->startAddress;
  uint32_t commandCount;
  const void* commandsAddress;

  if (cmds) {
    *cmds = NULL;
  }

  if (cmdCount) {
    *cmdCount = 0;
  }

  if (!slice->startAddress) {
    return;
  }

  // the 32 and 64 bit versions have an identical structures, so this will work
  switch (header32->magic) {
    case MH_MAGIC:  // 32-bit
    case MH_CIGAM:
      slice->cputype = header32->cputype;
      slice->cpusubtype = header32->cpusubtype;
      commandCount = header32->ncmds;
      commandsAddress = slice->startAddress + sizeof(struct mach_header);
      break;
    case MH_MAGIC_64:  // 64-bit
    case MH_CIGAM_64:
      slice->cputype = header64->cputype;
      slice->cpusubtype = header64->cpusubtype;
      commandCount = header64->ncmds;
      commandsAddress = slice->startAddress + sizeof(struct mach_header_64);
      break;
    default:
      // not a valid header
      return;
  }

  // assign everything back by reference
  if (cmds) {
    *cmds = commandsAddress;
  }

  if (cmdCount) {
    *cmdCount = commandCount;
  }
}

static bool FIRCLSMachOSliceIsValid(FIRCLSMachOSliceRef slice) {
  if (!slice) {
    return false;
  }

  if (!slice->startAddress) {
    return false;
  }

  return true;
}

void FIRCLSMachOSliceEnumerateLoadCommands(FIRCLSMachOSliceRef slice,
                                           FIRCLSMachOLoadCommandIterator block) {
  const struct load_command* cmd;
  uint32_t cmdCount;

  if (!block) {
    return;
  }

  if (!FIRCLSMachOSliceIsValid(slice)) {
    return;
  }

  FIRCLSMachOHeaderValues(slice, &cmd, &cmdCount);

  for (uint32_t i = 0; cmd != NULL && i < cmdCount; ++i) {
    block(cmd->cmd, cmd->cmdsize, cmd);

    cmd = (struct load_command*)((uintptr_t)cmd + cmd->cmdsize);
  }
}

struct FIRCLSMachOSlice FIRCLSMachOSliceGetCurrent(void) {
  const NXArchInfo* archInfo;
  struct FIRCLSMachOSlice slice;
  void* executableSymbol;
  Dl_info dlinfo;

  archInfo = NXGetLocalArchInfo();
  if (archInfo) {
    slice.cputype = archInfo->cputype;
    slice.cpusubtype = archInfo->cpusubtype;
  }

  slice.startAddress = NULL;

  // This call can fail when Exported Symbols File in Build Settings is missing the symbol value
  // defined as _MH_EXECUTE_SYM (if you look in the header the underscored MH_EXECUTE_SYM define is
  // there)
  executableSymbol = dlsym(RTLD_MAIN_ONLY, MH_EXECUTE_SYM);

  // get the address of the main function
  if (dladdr(executableSymbol, &dlinfo) != 0) {
    slice.startAddress = dlinfo.dli_fbase;
  }

  return slice;
}

struct FIRCLSMachOSlice FIRCLSMachOSliceWithHeader(void* machHeader) {
  struct FIRCLSMachOSlice slice;

  slice.startAddress = machHeader;

  return slice;
}

const char* FIRCLSMachOSliceGetExecutablePath(FIRCLSMachOSliceRef slice) {
  Dl_info info;

  if (!FIRCLSMachOSliceIsValid(slice)) {
    return NULL;
  }

  // use dladdr here to look up the information we need for a binary image
  if (dladdr(slice->startAddress, &info) == 0) {
    return NULL;
  }

  return info.dli_fname;
}

const char* FIRCLSMachOSliceGetArchitectureName(FIRCLSMachOSliceRef slice) {
  const NXArchInfo* archInfo;

  // there are some special cases here for types not handled by earlier OSes
  if (slice->cputype == CPU_TYPE_ARM && slice->cpusubtype == CPU_SUBTYPE_ARM_V7S) {
    return "armv7s";
  }

  if (slice->cputype == (CPU_TYPE_ARM | CPU_ARCH_ABI64)) {
    if (slice->cpusubtype == CLS_CPU_SUBTYPE_ARM64E) {
      return "arm64e";
    } else if (slice->cpusubtype == CPU_SUBTYPE_ARM64_ALL) {
      return "arm64";
    }
  }

  if (slice->cputype == (CPU_TYPE_ARM) && slice->cpusubtype == CPU_SUBTYPE_ARM_V7K) {
    return "armv7k";
  }

  archInfo = NXGetArchInfoFromCpuType(slice->cputype, slice->cpusubtype);
  if (!archInfo) {
    return "unknown";
  }

  return archInfo->name;
}

bool FIRCLSMachOSliceIs64Bit(FIRCLSMachOSliceRef slice) {
  // I'm pretty sure this is sufficient...
  return (slice->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64;
}

bool FIRCLSMachOSliceGetSectionByName(FIRCLSMachOSliceRef slice,
                                      const char* segName,
                                      const char* sectionName,
                                      const void** ptr) {
  if (!ptr) {
    return false;
  }

  *ptr = NULL;  // make sure this is set before returning

  FIRCLSMachOSection section;

  if (!FIRCLSMachOSliceInitSectionByName(slice, segName, sectionName, &section)) {
    return false;
  }

  // WARNING: this calculation isn't correct, but is here to maintain backwards
  // compatibility for now with callers of FIRCLSMachOSliceGetSectionByName. All new
  // users should be calling FIRCLSMachOSliceInitSectionByName
  *ptr = (const void*)((uintptr_t)slice->startAddress + section.offset);

  return true;
}

bool FIRCLSMachOSliceInitSectionByName(FIRCLSMachOSliceRef slice,
                                       const char* segName,
                                       const char* sectionName,
                                       FIRCLSMachOSection* section) {
  if (!FIRCLSMachOSliceIsValid(slice)) {
    return false;
  }

  if (!section) {
    return false;
  }

  memset(section, 0, sizeof(FIRCLSMachOSection));

  if (FIRCLSMachOSliceIs64Bit(slice)) {
    const struct section_64* sect =
        getsectbynamefromheader_64(slice->startAddress, segName, sectionName);
    if (!sect) {
      return false;
    }

    section->addr = sect->addr;
    section->size = sect->size;
    section->offset = sect->offset;
  } else {
    const struct section* sect = getsectbynamefromheader(slice->startAddress, segName, sectionName);
    if (!sect) {
      return false;
    }

    section->addr = sect->addr;
    section->size = sect->size;
    section->offset = sect->offset;
  }

  return true;
}

// TODO: this is left in-place just to ensure that old crashltyics + new fabric are still compatible
// with each other. As a happy bonus, if that situation does come up, this will also fix the bug
// that was preventing compact unwind on arm64 + iOS 9 from working correctly.
void FIRCLSMachOSliceGetUnwindInformation(FIRCLSMachOSliceRef slice,
                                          const void** ehFrame,
                                          const void** unwindInfo) {
  if (!unwindInfo && !ehFrame) {
    return;
  }

  bool found = false;
  intptr_t slide = 0;

  // This is inefficient, but we have no other safe way to do this correctly. Modifying the
  // FIRCLSMachOSlice structure is tempting, but could introduce weird binary-compatibility issues
  // with version mis-matches.
  for (uint32_t i = 0; i < _dyld_image_count(); ++i) {
    const struct mach_header* header = _dyld_get_image_header(i);

    if (header == slice->startAddress) {
      found = true;
      slide = _dyld_get_image_vmaddr_slide(i);
      break;
    }
  }

  // make sure we were able to find a matching value
  if (!found) {
    return;
  }

  FIRCLSMachOSection section;

  if (unwindInfo) {
    if (FIRCLSMachOSliceInitSectionByName(slice, SEG_TEXT, "__unwind_info", &section)) {
      *unwindInfo = (void*)(section.addr + slide);
    }
  }

  if (ehFrame) {
    if (FIRCLSMachOSliceInitSectionByName(slice, SEG_TEXT, "__eh_frame", &section)) {
      *ehFrame = (void*)(section.addr + slide);
    }
  }
}

uint8_t const* FIRCLSMachOGetUUID(const struct load_command* cmd) {
  return ((const struct uuid_command*)cmd)->uuid;
}

const char* FIRCLSMachOGetDylibPath(const struct load_command* cmd) {
  const struct dylib_command* dylibcmd = (const struct dylib_command*)cmd;

  return (const char*)((uintptr_t)cmd + dylibcmd->dylib.name.offset);
}

bool FIRCLSMachOGetEncrypted(const struct load_command* cmd) {
  return ((struct encryption_info_command*)cmd)->cryptid > 0;
}

static FIRCLSMachOVersion FIRCLSMachOVersionFromEncoded(uint32_t encoded) {
  FIRCLSMachOVersion version;

  version.major = (encoded & 0xffff0000) >> 16;
  version.minor = (encoded & 0x0000ff00) >> 8;
  version.bugfix = encoded & 0x000000ff;

  return version;
}

FIRCLSMachOVersion FIRCLSMachOGetMinimumOSVersion(const struct load_command* cmd) {
  return FIRCLSMachOVersionFromEncoded(((const struct version_min_command*)cmd)->version);
}

FIRCLSMachOVersion FIRCLSMachOGetLinkedSDKVersion(const struct load_command* cmd) {
  return FIRCLSMachOVersionFromEncoded(((const struct version_min_command*)cmd)->sdk);
}

FIRCLSMachOSegmentCommand FIRCLSMachOGetSegmentCommand(const struct load_command* cmd) {
  FIRCLSMachOSegmentCommand segmentCommand;

  memset(&segmentCommand, 0, sizeof(FIRCLSMachOSegmentCommand));

  if (!cmd) {
    return segmentCommand;
  }

  if (cmd->cmd == LC_SEGMENT) {
    struct segment_command* segCmd = (struct segment_command*)cmd;

    memcpy(segmentCommand.segname, segCmd->segname, 16);
    segmentCommand.vmaddr = segCmd->vmaddr;
    segmentCommand.vmsize = segCmd->vmsize;
  } else if (cmd->cmd == LC_SEGMENT_64) {
    struct segment_command_64* segCmd = (struct segment_command_64*)cmd;

    memcpy(segmentCommand.segname, segCmd->segname, 16);
    segmentCommand.vmaddr = segCmd->vmaddr;
    segmentCommand.vmsize = segCmd->vmsize;
  }

  return segmentCommand;
}

NSString* FIRCLSMachONormalizeUUID(CFUUIDBytes* uuidBytes) {
  CFUUIDRef uuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuidBytes);

  NSString* string = CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));

  CFRelease(uuid);

  return [[string stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
}

NSString* FIRCLSMachOFormatVersion(FIRCLSMachOVersion* version) {
  if (!version) {
    return nil;
  }

  return [NSString stringWithFormat:@"%d.%d.%d", version->major, version->minor, version->bugfix];
}