FIRCLSMachO.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "Crashlytics/Shared/FIRCLSMachO/FIRCLSMachO.h"
  15. #include "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
  16. #include <Foundation/Foundation.h>
  17. #include <mach-o/dyld.h>
  18. #include <mach-o/fat.h>
  19. #include <mach-o/getsect.h>
  20. #include <mach-o/ldsyms.h>
  21. #include <mach-o/utils.h>
  22. #include <sys/mman.h>
  23. #include <sys/stat.h>
  24. #include <dlfcn.h>
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. // This is defined in newer versions of iOS/macOS in usr/include/mach/machine.h
  29. #define CLS_CPU_SUBTYPE_ARM64E ((cpu_subtype_t)2)
  30. static void FIRCLSMachOHeaderValues(FIRCLSMachOSliceRef slice,
  31. const struct load_command** cmds,
  32. uint32_t* cmdCount);
  33. static bool FIRCLSMachOSliceIsValid(FIRCLSMachOSliceRef slice);
  34. bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path) {
  35. if (!file || !path) {
  36. return false;
  37. }
  38. file->fd = 0;
  39. file->mappedFile = NULL;
  40. file->mappedSize = 0;
  41. file->fd = open(path, O_RDONLY);
  42. if (file->fd < 0) {
  43. // unable to open mach-o file
  44. return false;
  45. }
  46. NSError* attributesError;
  47. NSString* objCPath = [NSString stringWithCString:path encoding:NSUTF8StringEncoding];
  48. NSDictionary* fileAttributes =
  49. [[NSFileManager defaultManager] attributesOfItemAtPath:objCPath error:&attributesError];
  50. if (attributesError != nil) {
  51. close(file->fd);
  52. return false;
  53. }
  54. NSNumber* fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
  55. long long currentFileSize = [fileSizeNumber longLongValue];
  56. NSFileAttributeType fileType = [fileAttributes objectForKey:NSFileType];
  57. // We need some minimum size for this to even be a possible mach-o file. I believe
  58. // its probably quite a bit bigger than this, but this at least covers something.
  59. // We also need it to be a regular file.
  60. file->mappedSize = (size_t)currentFileSize;
  61. if (currentFileSize < 16 || ![fileType isEqualToString:NSFileTypeRegular]) {
  62. close(file->fd);
  63. return false;
  64. }
  65. // Map the file to memory. MAP_SHARED can potentially reduce the amount of actual private
  66. // memory needed to do this mapping. Also, be sure to check for the correct failure result.
  67. file->mappedFile = mmap(0, file->mappedSize, PROT_READ, MAP_FILE | MAP_SHARED, file->fd, 0);
  68. if (!file->mappedFile || (file->mappedFile == MAP_FAILED)) {
  69. close(file->fd);
  70. return false;
  71. }
  72. return true;
  73. }
  74. bool FIRCLSMachOFileInitWithCurrent(FIRCLSMachOFileRef file) {
  75. struct FIRCLSMachOSlice slice = FIRCLSMachOSliceGetCurrent();
  76. const char* imagePath = FIRCLSMachOSliceGetExecutablePath(&slice);
  77. return FIRCLSMachOFileInitWithPath(file, imagePath);
  78. }
  79. void FIRCLSMachOFileDestroy(FIRCLSMachOFileRef file) {
  80. if (!file) {
  81. return;
  82. }
  83. if (file->mappedFile && file->mappedSize > 0) {
  84. munmap(file->mappedFile, file->mappedSize);
  85. }
  86. close(file->fd);
  87. }
  88. void FIRCLSMachOFileEnumerateSlices(FIRCLSMachOFileRef file, FIRCLSMachOSliceIterator block) {
  89. FIRCLSMachOEnumerateSlicesAtAddress(file->mappedFile, block);
  90. }
  91. void FIRCLSMachOEnumerateSlicesAtAddress(void* executableData, FIRCLSMachOSliceIterator block) {
  92. // check the magic value, to determine if we have a fat header or not
  93. uint32_t magicValue;
  94. uint32_t archCount;
  95. const struct fat_arch* fatArch;
  96. struct FIRCLSMachOSlice slice;
  97. memset(&slice, 0, sizeof(struct FIRCLSMachOSlice));
  98. magicValue = ((struct fat_header*)executableData)->magic;
  99. if ((magicValue != FAT_MAGIC) && (magicValue != FAT_CIGAM)) {
  100. slice.startAddress = executableData;
  101. // use this to fill in the values
  102. FIRCLSMachOHeaderValues(&slice, NULL, NULL);
  103. block(&slice);
  104. return;
  105. }
  106. archCount = OSSwapBigToHostInt32(((struct fat_header*)executableData)->nfat_arch);
  107. fatArch = executableData + sizeof(struct fat_header);
  108. for (uint32_t i = 0; i < archCount; ++i) {
  109. slice.cputype = OSSwapBigToHostInt32(fatArch->cputype);
  110. slice.cpusubtype = OSSwapBigToHostInt32(fatArch->cpusubtype);
  111. slice.startAddress = executableData + OSSwapBigToHostInt32(fatArch->offset);
  112. block(&slice);
  113. // advance to the next fat_arch structure
  114. fatArch = (struct fat_arch*)((uintptr_t)fatArch + sizeof(struct fat_arch));
  115. }
  116. }
  117. struct FIRCLSMachOSlice FIRCLSMachOFileSliceWithArchitectureName(FIRCLSMachOFileRef file,
  118. const char* name) {
  119. __block struct FIRCLSMachOSlice value;
  120. memset(&value, 0, sizeof(struct FIRCLSMachOSlice));
  121. FIRCLSMachOFileEnumerateSlices(file, ^(FIRCLSMachOSliceRef slice) {
  122. if (strcmp(FIRCLSMachOSliceGetArchitectureName(slice), name) == 0) {
  123. value = *slice;
  124. }
  125. });
  126. return value;
  127. }
  128. static void FIRCLSMachOHeaderValues(FIRCLSMachOSliceRef slice,
  129. const struct load_command** cmds,
  130. uint32_t* cmdCount) {
  131. const struct mach_header* header32 = (const struct mach_header*)slice->startAddress;
  132. const struct mach_header_64* header64 = (const struct mach_header_64*)slice->startAddress;
  133. uint32_t commandCount;
  134. const void* commandsAddress;
  135. if (cmds) {
  136. *cmds = NULL;
  137. }
  138. if (cmdCount) {
  139. *cmdCount = 0;
  140. }
  141. if (!slice->startAddress) {
  142. return;
  143. }
  144. // the 32 and 64 bit versions have an identical structures, so this will work
  145. switch (header32->magic) {
  146. case MH_MAGIC: // 32-bit
  147. case MH_CIGAM:
  148. slice->cputype = header32->cputype;
  149. slice->cpusubtype = header32->cpusubtype;
  150. commandCount = header32->ncmds;
  151. commandsAddress = slice->startAddress + sizeof(struct mach_header);
  152. break;
  153. case MH_MAGIC_64: // 64-bit
  154. case MH_CIGAM_64:
  155. slice->cputype = header64->cputype;
  156. slice->cpusubtype = header64->cpusubtype;
  157. commandCount = header64->ncmds;
  158. commandsAddress = slice->startAddress + sizeof(struct mach_header_64);
  159. break;
  160. default:
  161. // not a valid header
  162. return;
  163. }
  164. // assign everything back by reference
  165. if (cmds) {
  166. *cmds = commandsAddress;
  167. }
  168. if (cmdCount) {
  169. *cmdCount = commandCount;
  170. }
  171. }
  172. static bool FIRCLSMachOSliceIsValid(FIRCLSMachOSliceRef slice) {
  173. if (!slice) {
  174. return false;
  175. }
  176. if (!slice->startAddress) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. void FIRCLSMachOSliceEnumerateLoadCommands_f(FIRCLSMachOSliceRef slice,
  182. void* context,
  183. FIRCLSMachOLoadCommandIteratorFunc function) {
  184. const struct load_command* cmd;
  185. uint32_t cmdCount;
  186. if (!FIRCLSMachOSliceIsValid(slice)) {
  187. return;
  188. }
  189. FIRCLSMachOHeaderValues(slice, &cmd, &cmdCount);
  190. for (uint32_t i = 0; cmd != NULL && i < cmdCount; ++i) {
  191. function(cmd->cmd, cmd->cmdsize, cmd, context);
  192. cmd = (struct load_command*)((uintptr_t)cmd + cmd->cmdsize);
  193. }
  194. }
  195. void FIRCLSMachOSliceEnumerateLoadCommands(FIRCLSMachOSliceRef slice,
  196. FIRCLSMachOLoadCommandIterator block) {
  197. const struct load_command* cmd;
  198. uint32_t cmdCount;
  199. if (!block) {
  200. return;
  201. }
  202. if (!FIRCLSMachOSliceIsValid(slice)) {
  203. return;
  204. }
  205. FIRCLSMachOHeaderValues(slice, &cmd, &cmdCount);
  206. for (uint32_t i = 0; cmd != NULL && i < cmdCount; ++i) {
  207. block(cmd->cmd, cmd->cmdsize, cmd);
  208. cmd = (struct load_command*)((uintptr_t)cmd + cmd->cmdsize);
  209. }
  210. }
  211. struct FIRCLSMachOSlice FIRCLSMachOSliceGetCurrent(void) {
  212. struct FIRCLSMachOSlice slice;
  213. void* executableSymbol;
  214. Dl_info dlinfo;
  215. #if !CLS_TARGET_OS_VISION
  216. const NXArchInfo* archInfo;
  217. archInfo = NXGetLocalArchInfo();
  218. if (archInfo) {
  219. slice.cputype = archInfo->cputype;
  220. slice.cpusubtype = archInfo->cpusubtype;
  221. }
  222. #else
  223. cpu_type_t cputype;
  224. cpu_subtype_t cpusubtype;
  225. const char* archname = macho_arch_name_for_mach_header(NULL);
  226. bool hasArchInfo = macho_cpu_type_for_arch_name(archname, &cputype, &cpusubtype);
  227. if (hasArchInfo) {
  228. slice.cputype = cputype;
  229. slice.cpusubtype = cpusubtype;
  230. }
  231. #endif
  232. slice.startAddress = NULL;
  233. // This call can fail when Exported Symbols File in Build Settings is missing the symbol value
  234. // defined as _MH_EXECUTE_SYM (if you look in the header the underscored MH_EXECUTE_SYM define is
  235. // there)
  236. executableSymbol = dlsym(RTLD_MAIN_ONLY, MH_EXECUTE_SYM);
  237. // get the address of the main function
  238. if (dladdr(executableSymbol, &dlinfo) != 0) {
  239. slice.startAddress = dlinfo.dli_fbase;
  240. }
  241. return slice;
  242. }
  243. struct FIRCLSMachOSlice FIRCLSMachOSliceWithHeader(void* machHeader) {
  244. struct FIRCLSMachOSlice slice;
  245. slice.startAddress = machHeader;
  246. return slice;
  247. }
  248. const char* FIRCLSMachOSliceGetExecutablePath(FIRCLSMachOSliceRef slice) {
  249. Dl_info info;
  250. if (!FIRCLSMachOSliceIsValid(slice)) {
  251. return NULL;
  252. }
  253. // use dladdr here to look up the information we need for a binary image
  254. if (dladdr(slice->startAddress, &info) == 0) {
  255. return NULL;
  256. }
  257. return info.dli_fname;
  258. }
  259. const char* FIRCLSMachOSliceGetArchitectureName(FIRCLSMachOSliceRef slice) {
  260. // there are some special cases here for types not handled by earlier OSes
  261. if (slice->cputype == CPU_TYPE_ARM && slice->cpusubtype == CPU_SUBTYPE_ARM_V7S) {
  262. return "armv7s";
  263. }
  264. if (slice->cputype == (CPU_TYPE_ARM | CPU_ARCH_ABI64)) {
  265. if (slice->cpusubtype == CLS_CPU_SUBTYPE_ARM64E) {
  266. return "arm64e";
  267. } else if (slice->cpusubtype == CPU_SUBTYPE_ARM64_ALL) {
  268. return "arm64";
  269. }
  270. }
  271. if (slice->cputype == (CPU_TYPE_ARM) && slice->cpusubtype == CPU_SUBTYPE_ARM_V7K) {
  272. return "armv7k";
  273. }
  274. #if !CLS_TARGET_OS_VISION
  275. const NXArchInfo* archInfo;
  276. archInfo = NXGetArchInfoFromCpuType(slice->cputype, slice->cpusubtype);
  277. if (!archInfo) {
  278. return "unknown";
  279. }
  280. return archInfo->name;
  281. #else
  282. const char* archname = macho_arch_name_for_mach_header(slice->startAddress);
  283. if (!archname) {
  284. return "unknown";
  285. }
  286. return archname;
  287. #endif
  288. }
  289. bool FIRCLSMachOSliceIs64Bit(FIRCLSMachOSliceRef slice) {
  290. // I'm pretty sure this is sufficient...
  291. return (slice->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64;
  292. }
  293. // deprecated
  294. bool FIRCLSMachOSliceGetSectionByName(FIRCLSMachOSliceRef slice,
  295. const char* segName,
  296. const char* sectionName,
  297. const void** ptr) {
  298. if (!ptr) {
  299. return false;
  300. }
  301. *ptr = NULL; // make sure this is set before returning
  302. FIRCLSMachOSection section;
  303. if (!FIRCLSMachOSliceInitSectionByName(slice, segName, sectionName, &section)) {
  304. return false;
  305. }
  306. // WARNING: this calculation isn't correct, but is here to maintain backwards
  307. // compatibility for now with callers of FIRCLSMachOSliceGetSectionByName. All new
  308. // users should be calling FIRCLSMachOSliceInitSectionByName
  309. *ptr = (const void*)((uintptr_t)slice->startAddress + section.offset);
  310. return true;
  311. }
  312. bool FIRCLSMachOSliceInitSectionByName(FIRCLSMachOSliceRef slice,
  313. const char* segName,
  314. const char* sectionName,
  315. FIRCLSMachOSection* section) {
  316. if (!FIRCLSMachOSliceIsValid(slice)) {
  317. return false;
  318. }
  319. if (!section) {
  320. return false;
  321. }
  322. memset(section, 0, sizeof(FIRCLSMachOSection));
  323. // Deprecated code for vision OS, entire function is not used anywhere
  324. #pragma clang diagnostic push
  325. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  326. if (FIRCLSMachOSliceIs64Bit(slice)) {
  327. const struct section_64* sect =
  328. getsectbynamefromheader_64(slice->startAddress, segName, sectionName);
  329. if (!sect) {
  330. return false;
  331. }
  332. section->addr = sect->addr;
  333. section->size = sect->size;
  334. section->offset = sect->offset;
  335. } else {
  336. const struct section* sect = getsectbynamefromheader(slice->startAddress, segName, sectionName);
  337. if (!sect) {
  338. return false;
  339. }
  340. section->addr = sect->addr;
  341. section->size = sect->size;
  342. section->offset = sect->offset;
  343. }
  344. #pragma clang diagnostic pop
  345. return true;
  346. }
  347. // TODO: this is left in-place just to ensure that old crashltyics + new fabric are still compatible
  348. // with each other. As a happy bonus, if that situation does come up, this will also fix the bug
  349. // that was preventing compact unwind on arm64 + iOS 9 from working correctly.
  350. void FIRCLSMachOSliceGetUnwindInformation(FIRCLSMachOSliceRef slice,
  351. const void** ehFrame,
  352. const void** unwindInfo) {
  353. if (!unwindInfo && !ehFrame) {
  354. return;
  355. }
  356. bool found = false;
  357. intptr_t slide = 0;
  358. // This is inefficient, but we have no other safe way to do this correctly. Modifying the
  359. // FIRCLSMachOSlice structure is tempting, but could introduce weird binary-compatibility issues
  360. // with version mis-matches.
  361. for (uint32_t i = 0; i < _dyld_image_count(); ++i) {
  362. const struct mach_header* header = _dyld_get_image_header(i);
  363. if (header == slice->startAddress) {
  364. found = true;
  365. slide = _dyld_get_image_vmaddr_slide(i);
  366. break;
  367. }
  368. }
  369. // make sure we were able to find a matching value
  370. if (!found) {
  371. return;
  372. }
  373. FIRCLSMachOSection section;
  374. if (unwindInfo) {
  375. if (FIRCLSMachOSliceInitSectionByName(slice, SEG_TEXT, "__unwind_info", &section)) {
  376. *unwindInfo = (void*)(section.addr + slide);
  377. }
  378. }
  379. if (ehFrame) {
  380. if (FIRCLSMachOSliceInitSectionByName(slice, SEG_TEXT, "__eh_frame", &section)) {
  381. *ehFrame = (void*)(section.addr + slide);
  382. }
  383. }
  384. }
  385. uint8_t const* FIRCLSMachOGetUUID(const struct load_command* cmd) {
  386. return ((const struct uuid_command*)cmd)->uuid;
  387. }
  388. const char* FIRCLSMachOGetDylibPath(const struct load_command* cmd) {
  389. const struct dylib_command* dylibcmd = (const struct dylib_command*)cmd;
  390. return (const char*)((uintptr_t)cmd + dylibcmd->dylib.name.offset);
  391. }
  392. bool FIRCLSMachOGetEncrypted(const struct load_command* cmd) {
  393. return ((struct encryption_info_command*)cmd)->cryptid > 0;
  394. }
  395. static FIRCLSMachOVersion FIRCLSMachOVersionFromEncoded(uint32_t encoded) {
  396. FIRCLSMachOVersion version;
  397. version.major = (encoded & 0xffff0000) >> 16;
  398. version.minor = (encoded & 0x0000ff00) >> 8;
  399. version.bugfix = encoded & 0x000000ff;
  400. return version;
  401. }
  402. FIRCLSMachOVersion FIRCLSMachOGetMinimumOSVersion(const struct load_command* cmd) {
  403. return FIRCLSMachOVersionFromEncoded(((const struct version_min_command*)cmd)->version);
  404. }
  405. FIRCLSMachOVersion FIRCLSMachOGetLinkedSDKVersion(const struct load_command* cmd) {
  406. return FIRCLSMachOVersionFromEncoded(((const struct version_min_command*)cmd)->sdk);
  407. }
  408. FIRCLSMachOSegmentCommand FIRCLSMachOGetSegmentCommand(const struct load_command* cmd) {
  409. FIRCLSMachOSegmentCommand segmentCommand;
  410. memset(&segmentCommand, 0, sizeof(FIRCLSMachOSegmentCommand));
  411. if (!cmd) {
  412. return segmentCommand;
  413. }
  414. if (cmd->cmd == LC_SEGMENT) {
  415. struct segment_command* segCmd = (struct segment_command*)cmd;
  416. memcpy(segmentCommand.segname, segCmd->segname, 16);
  417. segmentCommand.vmaddr = segCmd->vmaddr;
  418. segmentCommand.vmsize = segCmd->vmsize;
  419. } else if (cmd->cmd == LC_SEGMENT_64) {
  420. struct segment_command_64* segCmd = (struct segment_command_64*)cmd;
  421. memcpy(segmentCommand.segname, segCmd->segname, 16);
  422. segmentCommand.vmaddr = segCmd->vmaddr;
  423. segmentCommand.vmsize = segCmd->vmsize;
  424. }
  425. return segmentCommand;
  426. }
  427. NSString* FIRCLSMachONormalizeUUID(CFUUIDBytes* uuidBytes) {
  428. CFUUIDRef uuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuidBytes);
  429. NSString* string = CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
  430. CFRelease(uuid);
  431. return [[string stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  432. }
  433. NSString* FIRCLSMachOFormatVersion(FIRCLSMachOVersion* version) {
  434. if (!version) {
  435. return nil;
  436. }
  437. return [NSString stringWithFormat:@"%d.%d.%d", version->major, version->minor, version->bugfix];
  438. }