macho.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /* elf.c -- Get debug data from a Mach-O file for backtraces.
  2. Copyright (C) 2020-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <sys/types.h>
  29. #include <dirent.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #ifdef HAVE_MACH_O_DYLD_H
  33. #include <mach-o/dyld.h>
  34. #endif
  35. #include "backtrace.h"
  36. #include "internal.h"
  37. /* Mach-O file header for a 32-bit executable. */
  38. struct macho_header_32
  39. {
  40. uint32_t magic; /* Magic number (MACH_O_MAGIC_32) */
  41. uint32_t cputype; /* CPU type */
  42. uint32_t cpusubtype; /* CPU subtype */
  43. uint32_t filetype; /* Type of file (object, executable) */
  44. uint32_t ncmds; /* Number of load commands */
  45. uint32_t sizeofcmds; /* Total size of load commands */
  46. uint32_t flags; /* Flags for special features */
  47. };
  48. /* Mach-O file header for a 64-bit executable. */
  49. struct macho_header_64
  50. {
  51. uint32_t magic; /* Magic number (MACH_O_MAGIC_64) */
  52. uint32_t cputype; /* CPU type */
  53. uint32_t cpusubtype; /* CPU subtype */
  54. uint32_t filetype; /* Type of file (object, executable) */
  55. uint32_t ncmds; /* Number of load commands */
  56. uint32_t sizeofcmds; /* Total size of load commands */
  57. uint32_t flags; /* Flags for special features */
  58. uint32_t reserved; /* Reserved */
  59. };
  60. /* Mach-O file header for a fat executable. */
  61. struct macho_header_fat
  62. {
  63. uint32_t magic; /* Magic number (MACH_O_MH_(MAGIC|CIGAM)_FAT(_64)?) */
  64. uint32_t nfat_arch; /* Number of components */
  65. };
  66. /* Values for the header magic field. */
  67. #define MACH_O_MH_MAGIC_32 0xfeedface
  68. #define MACH_O_MH_MAGIC_64 0xfeedfacf
  69. #define MACH_O_MH_MAGIC_FAT 0xcafebabe
  70. #define MACH_O_MH_CIGAM_FAT 0xbebafeca
  71. #define MACH_O_MH_MAGIC_FAT_64 0xcafebabf
  72. #define MACH_O_MH_CIGAM_FAT_64 0xbfbafeca
  73. /* Value for the header filetype field. */
  74. #define MACH_O_MH_EXECUTE 0x02
  75. #define MACH_O_MH_DYLIB 0x06
  76. #define MACH_O_MH_DSYM 0x0a
  77. /* A component of a fat file. A fat file starts with a
  78. macho_header_fat followed by nfat_arch instances of this
  79. struct. */
  80. struct macho_fat_arch
  81. {
  82. uint32_t cputype; /* CPU type */
  83. uint32_t cpusubtype; /* CPU subtype */
  84. uint32_t offset; /* File offset of this entry */
  85. uint32_t size; /* Size of this entry */
  86. uint32_t align; /* Alignment of this entry */
  87. };
  88. /* A component of a 64-bit fat file. This is used if the magic field
  89. is MAGIC_FAT_64. This is only used when some file size or file
  90. offset is too large to represent in the 32-bit format. */
  91. struct macho_fat_arch_64
  92. {
  93. uint32_t cputype; /* CPU type */
  94. uint32_t cpusubtype; /* CPU subtype */
  95. uint64_t offset; /* File offset of this entry */
  96. uint64_t size; /* Size of this entry */
  97. uint32_t align; /* Alignment of this entry */
  98. uint32_t reserved; /* Reserved */
  99. };
  100. /* Values for the fat_arch cputype field (and the header cputype
  101. field). */
  102. #define MACH_O_CPU_ARCH_ABI64 0x01000000
  103. #define MACH_O_CPU_TYPE_X86 7
  104. #define MACH_O_CPU_TYPE_ARM 12
  105. #define MACH_O_CPU_TYPE_PPC 18
  106. #define MACH_O_CPU_TYPE_X86_64 (MACH_O_CPU_TYPE_X86 | MACH_O_CPU_ARCH_ABI64)
  107. #define MACH_O_CPU_TYPE_ARM64 (MACH_O_CPU_TYPE_ARM | MACH_O_CPU_ARCH_ABI64)
  108. #define MACH_O_CPU_TYPE_PPC64 (MACH_O_CPU_TYPE_PPC | MACH_O_CPU_ARCH_ABI64)
  109. /* The header of a load command. */
  110. struct macho_load_command
  111. {
  112. uint32_t cmd; /* The type of load command */
  113. uint32_t cmdsize; /* Size in bytes of the entire command */
  114. };
  115. /* Values for the load_command cmd field. */
  116. #define MACH_O_LC_SEGMENT 0x01
  117. #define MACH_O_LC_SYMTAB 0x02
  118. #define MACH_O_LC_SEGMENT_64 0x19
  119. #define MACH_O_LC_UUID 0x1b
  120. /* The length of a section of segment name. */
  121. #define MACH_O_NAMELEN (16)
  122. /* LC_SEGMENT load command. */
  123. struct macho_segment_command
  124. {
  125. uint32_t cmd; /* The type of load command (LC_SEGMENT) */
  126. uint32_t cmdsize; /* Size in bytes of the entire command */
  127. char segname[MACH_O_NAMELEN]; /* Segment name */
  128. uint32_t vmaddr; /* Virtual memory address */
  129. uint32_t vmsize; /* Virtual memory size */
  130. uint32_t fileoff; /* Offset of data to be mapped */
  131. uint32_t filesize; /* Size of data in file */
  132. uint32_t maxprot; /* Maximum permitted virtual protection */
  133. uint32_t initprot; /* Initial virtual memory protection */
  134. uint32_t nsects; /* Number of sections in this segment */
  135. uint32_t flags; /* Flags */
  136. };
  137. /* LC_SEGMENT_64 load command. */
  138. struct macho_segment_64_command
  139. {
  140. uint32_t cmd; /* The type of load command (LC_SEGMENT) */
  141. uint32_t cmdsize; /* Size in bytes of the entire command */
  142. char segname[MACH_O_NAMELEN]; /* Segment name */
  143. uint64_t vmaddr; /* Virtual memory address */
  144. uint64_t vmsize; /* Virtual memory size */
  145. uint64_t fileoff; /* Offset of data to be mapped */
  146. uint64_t filesize; /* Size of data in file */
  147. uint32_t maxprot; /* Maximum permitted virtual protection */
  148. uint32_t initprot; /* Initial virtual memory protection */
  149. uint32_t nsects; /* Number of sections in this segment */
  150. uint32_t flags; /* Flags */
  151. };
  152. /* LC_SYMTAB load command. */
  153. struct macho_symtab_command
  154. {
  155. uint32_t cmd; /* The type of load command (LC_SEGMENT) */
  156. uint32_t cmdsize; /* Size in bytes of the entire command */
  157. uint32_t symoff; /* File offset of symbol table */
  158. uint32_t nsyms; /* Number of symbols */
  159. uint32_t stroff; /* File offset of string table */
  160. uint32_t strsize; /* String table size */
  161. };
  162. /* The length of a Mach-O uuid. */
  163. #define MACH_O_UUID_LEN (16)
  164. /* LC_UUID load command. */
  165. struct macho_uuid_command
  166. {
  167. uint32_t cmd; /* Type of load command (LC_UUID) */
  168. uint32_t cmdsize; /* Size in bytes of command */
  169. unsigned char uuid[MACH_O_UUID_LEN]; /* UUID */
  170. };
  171. /* 32-bit section header within a LC_SEGMENT segment. */
  172. struct macho_section
  173. {
  174. char sectname[MACH_O_NAMELEN]; /* Section name */
  175. char segment[MACH_O_NAMELEN]; /* Segment of this section */
  176. uint32_t addr; /* Address in memory */
  177. uint32_t size; /* Section size */
  178. uint32_t offset; /* File offset */
  179. uint32_t align; /* Log2 of section alignment */
  180. uint32_t reloff; /* File offset of relocations */
  181. uint32_t nreloc; /* Number of relocs for this section */
  182. uint32_t flags; /* Flags */
  183. uint32_t reserved1;
  184. uint32_t reserved2;
  185. };
  186. /* 64-bit section header within a LC_SEGMENT_64 segment. */
  187. struct macho_section_64
  188. {
  189. char sectname[MACH_O_NAMELEN]; /* Section name */
  190. char segment[MACH_O_NAMELEN]; /* Segment of this section */
  191. uint64_t addr; /* Address in memory */
  192. uint64_t size; /* Section size */
  193. uint32_t offset; /* File offset */
  194. uint32_t align; /* Log2 of section alignment */
  195. uint32_t reloff; /* File offset of section relocations */
  196. uint32_t nreloc; /* Number of relocs for this section */
  197. uint32_t flags; /* Flags */
  198. uint32_t reserved1;
  199. uint32_t reserved2;
  200. uint32_t reserved3;
  201. };
  202. /* 32-bit symbol data. */
  203. struct macho_nlist
  204. {
  205. uint32_t n_strx; /* Index of name in string table */
  206. uint8_t n_type; /* Type flag */
  207. uint8_t n_sect; /* Section number */
  208. uint16_t n_desc; /* Stabs description field */
  209. uint32_t n_value; /* Value */
  210. };
  211. /* 64-bit symbol data. */
  212. struct macho_nlist_64
  213. {
  214. uint32_t n_strx; /* Index of name in string table */
  215. uint8_t n_type; /* Type flag */
  216. uint8_t n_sect; /* Section number */
  217. uint16_t n_desc; /* Stabs description field */
  218. uint64_t n_value; /* Value */
  219. };
  220. /* Value found in nlist n_type field. */
  221. #define MACH_O_N_EXT 0x01 /* Extern symbol */
  222. #define MACH_O_N_ABS 0x02 /* Absolute symbol */
  223. #define MACH_O_N_SECT 0x0e /* Defined in section */
  224. #define MACH_O_N_TYPE 0x0e /* Mask for type bits */
  225. #define MACH_O_N_STAB 0xe0 /* Stabs debugging symbol */
  226. /* Information we keep for a Mach-O symbol. */
  227. struct macho_symbol
  228. {
  229. const char *name; /* Symbol name */
  230. uintptr_t address; /* Symbol address */
  231. };
  232. /* Information to pass to macho_syminfo. */
  233. struct macho_syminfo_data
  234. {
  235. struct macho_syminfo_data *next; /* Next module */
  236. struct macho_symbol *symbols; /* Symbols sorted by address */
  237. size_t count; /* Number of symbols */
  238. };
  239. /* Names of sections, indexed by enum dwarf_section in internal.h. */
  240. static const char * const dwarf_section_names[DEBUG_MAX] =
  241. {
  242. "__debug_info",
  243. "__debug_line",
  244. "__debug_abbrev",
  245. "__debug_ranges",
  246. "__debug_str",
  247. "", /* DEBUG_ADDR */
  248. "__debug_str_offs",
  249. "", /* DEBUG_LINE_STR */
  250. "__debug_rnglists"
  251. };
  252. /* Forward declaration. */
  253. static int macho_add (struct backtrace_state *, const char *, int, off_t,
  254. const unsigned char *, uintptr_t, int,
  255. backtrace_error_callback, void *, fileline *, int *);
  256. /* A dummy callback function used when we can't find any debug info. */
  257. static int
  258. macho_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
  259. uintptr_t pc ATTRIBUTE_UNUSED,
  260. backtrace_full_callback callback ATTRIBUTE_UNUSED,
  261. backtrace_error_callback error_callback, void *data)
  262. {
  263. error_callback (data, "no debug info in Mach-O executable", -1);
  264. return 0;
  265. }
  266. /* A dummy callback function used when we can't find a symbol
  267. table. */
  268. static void
  269. macho_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
  270. uintptr_t addr ATTRIBUTE_UNUSED,
  271. backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
  272. backtrace_error_callback error_callback, void *data)
  273. {
  274. error_callback (data, "no symbol table in Mach-O executable", -1);
  275. }
  276. /* Add a single DWARF section to DWARF_SECTIONS, if we need the
  277. section. Returns 1 on success, 0 on failure. */
  278. static int
  279. macho_add_dwarf_section (struct backtrace_state *state, int descriptor,
  280. const char *sectname, uint32_t offset, uint64_t size,
  281. backtrace_error_callback error_callback, void *data,
  282. struct dwarf_sections *dwarf_sections)
  283. {
  284. int i;
  285. for (i = 0; i < (int) DEBUG_MAX; ++i)
  286. {
  287. if (dwarf_section_names[i][0] != '\0'
  288. && strncmp (sectname, dwarf_section_names[i], MACH_O_NAMELEN) == 0)
  289. {
  290. struct backtrace_view section_view;
  291. /* FIXME: Perhaps it would be better to try to use a single
  292. view to read all the DWARF data, as we try to do for
  293. ELF. */
  294. if (!backtrace_get_view (state, descriptor, offset, size,
  295. error_callback, data, &section_view))
  296. return 0;
  297. dwarf_sections->data[i] = (const unsigned char *) section_view.data;
  298. dwarf_sections->size[i] = size;
  299. break;
  300. }
  301. }
  302. return 1;
  303. }
  304. /* Collect DWARF sections from a DWARF segment. Returns 1 on success,
  305. 0 on failure. */
  306. static int
  307. macho_add_dwarf_segment (struct backtrace_state *state, int descriptor,
  308. off_t offset, unsigned int cmd, const char *psecs,
  309. size_t sizesecs, unsigned int nsects,
  310. backtrace_error_callback error_callback, void *data,
  311. struct dwarf_sections *dwarf_sections)
  312. {
  313. size_t sec_header_size;
  314. size_t secoffset;
  315. unsigned int i;
  316. switch (cmd)
  317. {
  318. case MACH_O_LC_SEGMENT:
  319. sec_header_size = sizeof (struct macho_section);
  320. break;
  321. case MACH_O_LC_SEGMENT_64:
  322. sec_header_size = sizeof (struct macho_section_64);
  323. break;
  324. default:
  325. abort ();
  326. }
  327. secoffset = 0;
  328. for (i = 0; i < nsects; ++i)
  329. {
  330. if (secoffset + sec_header_size > sizesecs)
  331. {
  332. error_callback (data, "section overflow withing segment", 0);
  333. return 0;
  334. }
  335. switch (cmd)
  336. {
  337. case MACH_O_LC_SEGMENT:
  338. {
  339. struct macho_section section;
  340. memcpy (&section, psecs + secoffset, sizeof section);
  341. macho_add_dwarf_section (state, descriptor, section.sectname,
  342. offset + section.offset, section.size,
  343. error_callback, data, dwarf_sections);
  344. }
  345. break;
  346. case MACH_O_LC_SEGMENT_64:
  347. {
  348. struct macho_section_64 section;
  349. memcpy (&section, psecs + secoffset, sizeof section);
  350. macho_add_dwarf_section (state, descriptor, section.sectname,
  351. offset + section.offset, section.size,
  352. error_callback, data, dwarf_sections);
  353. }
  354. break;
  355. default:
  356. abort ();
  357. }
  358. secoffset += sec_header_size;
  359. }
  360. return 1;
  361. }
  362. /* Compare struct macho_symbol for qsort. */
  363. static int
  364. macho_symbol_compare (const void *v1, const void *v2)
  365. {
  366. const struct macho_symbol *m1 = (const struct macho_symbol *) v1;
  367. const struct macho_symbol *m2 = (const struct macho_symbol *) v2;
  368. if (m1->address < m2->address)
  369. return -1;
  370. else if (m1->address > m2->address)
  371. return 1;
  372. else
  373. return 0;
  374. }
  375. /* Compare an address against a macho_symbol for bsearch. We allocate
  376. one extra entry in the array so that this can safely look at the
  377. next entry. */
  378. static int
  379. macho_symbol_search (const void *vkey, const void *ventry)
  380. {
  381. const uintptr_t *key = (const uintptr_t *) vkey;
  382. const struct macho_symbol *entry = (const struct macho_symbol *) ventry;
  383. uintptr_t addr;
  384. addr = *key;
  385. if (addr < entry->address)
  386. return -1;
  387. else if (entry->name[0] == '\0'
  388. && entry->address == ~(uintptr_t) 0)
  389. return -1;
  390. else if ((entry + 1)->name[0] == '\0'
  391. && (entry + 1)->address == ~(uintptr_t) 0)
  392. return -1;
  393. else if (addr >= (entry + 1)->address)
  394. return 1;
  395. else
  396. return 0;
  397. }
  398. /* Return whether the symbol type field indicates a symbol table entry
  399. that we care about: a function or data symbol. */
  400. static int
  401. macho_defined_symbol (uint8_t type)
  402. {
  403. if ((type & MACH_O_N_STAB) != 0)
  404. return 0;
  405. if ((type & MACH_O_N_EXT) != 0)
  406. return 0;
  407. switch (type & MACH_O_N_TYPE)
  408. {
  409. case MACH_O_N_ABS:
  410. return 1;
  411. case MACH_O_N_SECT:
  412. return 1;
  413. default:
  414. return 0;
  415. }
  416. }
  417. /* Add symbol table information for a Mach-O file. */
  418. static int
  419. macho_add_symtab (struct backtrace_state *state, int descriptor,
  420. uintptr_t base_address, int is_64,
  421. off_t symoff, unsigned int nsyms, off_t stroff,
  422. unsigned int strsize,
  423. backtrace_error_callback error_callback, void *data)
  424. {
  425. size_t symsize;
  426. struct backtrace_view sym_view;
  427. int sym_view_valid;
  428. struct backtrace_view str_view;
  429. int str_view_valid;
  430. size_t ndefs;
  431. size_t symtaboff;
  432. unsigned int i;
  433. size_t macho_symbol_size;
  434. struct macho_symbol *macho_symbols;
  435. unsigned int j;
  436. struct macho_syminfo_data *sdata;
  437. sym_view_valid = 0;
  438. str_view_valid = 0;
  439. macho_symbol_size = 0;
  440. macho_symbols = NULL;
  441. if (is_64)
  442. symsize = sizeof (struct macho_nlist_64);
  443. else
  444. symsize = sizeof (struct macho_nlist);
  445. if (!backtrace_get_view (state, descriptor, symoff, nsyms * symsize,
  446. error_callback, data, &sym_view))
  447. goto fail;
  448. sym_view_valid = 1;
  449. if (!backtrace_get_view (state, descriptor, stroff, strsize,
  450. error_callback, data, &str_view))
  451. return 0;
  452. str_view_valid = 1;
  453. ndefs = 0;
  454. symtaboff = 0;
  455. for (i = 0; i < nsyms; ++i, symtaboff += symsize)
  456. {
  457. if (is_64)
  458. {
  459. struct macho_nlist_64 nlist;
  460. memcpy (&nlist, (const char *) sym_view.data + symtaboff,
  461. sizeof nlist);
  462. if (macho_defined_symbol (nlist.n_type))
  463. ++ndefs;
  464. }
  465. else
  466. {
  467. struct macho_nlist nlist;
  468. memcpy (&nlist, (const char *) sym_view.data + symtaboff,
  469. sizeof nlist);
  470. if (macho_defined_symbol (nlist.n_type))
  471. ++ndefs;
  472. }
  473. }
  474. /* Add 1 to ndefs to make room for a sentinel. */
  475. macho_symbol_size = (ndefs + 1) * sizeof (struct macho_symbol);
  476. macho_symbols = ((struct macho_symbol *)
  477. backtrace_alloc (state, macho_symbol_size, error_callback,
  478. data));
  479. if (macho_symbols == NULL)
  480. goto fail;
  481. j = 0;
  482. symtaboff = 0;
  483. for (i = 0; i < nsyms; ++i, symtaboff += symsize)
  484. {
  485. uint32_t strx;
  486. uint64_t value;
  487. const char *name;
  488. strx = 0;
  489. value = 0;
  490. if (is_64)
  491. {
  492. struct macho_nlist_64 nlist;
  493. memcpy (&nlist, (const char *) sym_view.data + symtaboff,
  494. sizeof nlist);
  495. if (!macho_defined_symbol (nlist.n_type))
  496. continue;
  497. strx = nlist.n_strx;
  498. value = nlist.n_value;
  499. }
  500. else
  501. {
  502. struct macho_nlist nlist;
  503. memcpy (&nlist, (const char *) sym_view.data + symtaboff,
  504. sizeof nlist);
  505. if (!macho_defined_symbol (nlist.n_type))
  506. continue;
  507. strx = nlist.n_strx;
  508. value = nlist.n_value;
  509. }
  510. if (strx >= strsize)
  511. {
  512. error_callback (data, "symbol string index out of range", 0);
  513. goto fail;
  514. }
  515. name = (const char *) str_view.data + strx;
  516. if (name[0] == '_')
  517. ++name;
  518. macho_symbols[j].name = name;
  519. macho_symbols[j].address = value + base_address;
  520. ++j;
  521. }
  522. sdata = ((struct macho_syminfo_data *)
  523. backtrace_alloc (state, sizeof *sdata, error_callback, data));
  524. if (sdata == NULL)
  525. goto fail;
  526. /* We need to keep the string table since it holds the names, but we
  527. can release the symbol table. */
  528. backtrace_release_view (state, &sym_view, error_callback, data);
  529. sym_view_valid = 0;
  530. str_view_valid = 0;
  531. /* Add a trailing sentinel symbol. */
  532. macho_symbols[j].name = "";
  533. macho_symbols[j].address = ~(uintptr_t) 0;
  534. backtrace_qsort (macho_symbols, ndefs + 1, sizeof (struct macho_symbol),
  535. macho_symbol_compare);
  536. sdata->next = NULL;
  537. sdata->symbols = macho_symbols;
  538. sdata->count = ndefs;
  539. if (!state->threaded)
  540. {
  541. struct macho_syminfo_data **pp;
  542. for (pp = (struct macho_syminfo_data **) (void *) &state->syminfo_data;
  543. *pp != NULL;
  544. pp = &(*pp)->next)
  545. ;
  546. *pp = sdata;
  547. }
  548. else
  549. {
  550. while (1)
  551. {
  552. struct macho_syminfo_data **pp;
  553. pp = (struct macho_syminfo_data **) (void *) &state->syminfo_data;
  554. while (1)
  555. {
  556. struct macho_syminfo_data *p;
  557. p = backtrace_atomic_load_pointer (pp);
  558. if (p == NULL)
  559. break;
  560. pp = &p->next;
  561. }
  562. if (__sync_bool_compare_and_swap (pp, NULL, sdata))
  563. break;
  564. }
  565. }
  566. return 1;
  567. fail:
  568. if (macho_symbols != NULL)
  569. backtrace_free (state, macho_symbols, macho_symbol_size,
  570. error_callback, data);
  571. if (sym_view_valid)
  572. backtrace_release_view (state, &sym_view, error_callback, data);
  573. if (str_view_valid)
  574. backtrace_release_view (state, &str_view, error_callback, data);
  575. return 0;
  576. }
  577. /* Return the symbol name and value for an ADDR. */
  578. static void
  579. macho_syminfo (struct backtrace_state *state, uintptr_t addr,
  580. backtrace_syminfo_callback callback,
  581. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  582. void *data)
  583. {
  584. struct macho_syminfo_data *sdata;
  585. struct macho_symbol *sym;
  586. sym = NULL;
  587. if (!state->threaded)
  588. {
  589. for (sdata = (struct macho_syminfo_data *) state->syminfo_data;
  590. sdata != NULL;
  591. sdata = sdata->next)
  592. {
  593. sym = ((struct macho_symbol *)
  594. bsearch (&addr, sdata->symbols, sdata->count,
  595. sizeof (struct macho_symbol), macho_symbol_search));
  596. if (sym != NULL)
  597. break;
  598. }
  599. }
  600. else
  601. {
  602. struct macho_syminfo_data **pp;
  603. pp = (struct macho_syminfo_data **) (void *) &state->syminfo_data;
  604. while (1)
  605. {
  606. sdata = backtrace_atomic_load_pointer (pp);
  607. if (sdata == NULL)
  608. break;
  609. sym = ((struct macho_symbol *)
  610. bsearch (&addr, sdata->symbols, sdata->count,
  611. sizeof (struct macho_symbol), macho_symbol_search));
  612. if (sym != NULL)
  613. break;
  614. pp = &sdata->next;
  615. }
  616. }
  617. if (sym == NULL)
  618. callback (data, addr, NULL, 0, 0);
  619. else
  620. callback (data, addr, sym->name, sym->address, 0);
  621. }
  622. /* Look through a fat file to find the relevant executable. Returns 1
  623. on success, 0 on failure (in both cases descriptor is closed). */
  624. static int
  625. macho_add_fat (struct backtrace_state *state, const char *filename,
  626. int descriptor, int swapped, off_t offset,
  627. const unsigned char *match_uuid, uintptr_t base_address,
  628. int skip_symtab, uint32_t nfat_arch, int is_64,
  629. backtrace_error_callback error_callback, void *data,
  630. fileline *fileline_fn, int *found_sym)
  631. {
  632. int arch_view_valid;
  633. unsigned int cputype;
  634. size_t arch_size;
  635. struct backtrace_view arch_view;
  636. unsigned int i;
  637. arch_view_valid = 0;
  638. #if defined (__x86_64__)
  639. cputype = MACH_O_CPU_TYPE_X86_64;
  640. #elif defined (__i386__)
  641. cputype = MACH_O_CPU_TYPE_X86;
  642. #elif defined (__aarch64__)
  643. cputype = MACH_O_CPU_TYPE_ARM64;
  644. #elif defined (__arm__)
  645. cputype = MACH_O_CPU_TYPE_ARM;
  646. #elif defined (__ppc__)
  647. cputype = MACH_O_CPU_TYPE_PPC;
  648. #elif defined (__ppc64__)
  649. cputype = MACH_O_CPU_TYPE_PPC64;
  650. #else
  651. error_callback (data, "unknown Mach-O architecture", 0);
  652. goto fail;
  653. #endif
  654. if (is_64)
  655. arch_size = sizeof (struct macho_fat_arch_64);
  656. else
  657. arch_size = sizeof (struct macho_fat_arch);
  658. if (!backtrace_get_view (state, descriptor, offset,
  659. nfat_arch * arch_size,
  660. error_callback, data, &arch_view))
  661. goto fail;
  662. for (i = 0; i < nfat_arch; ++i)
  663. {
  664. uint32_t fcputype;
  665. uint64_t foffset;
  666. if (is_64)
  667. {
  668. struct macho_fat_arch_64 fat_arch_64;
  669. memcpy (&fat_arch_64,
  670. (const char *) arch_view.data + i * arch_size,
  671. arch_size);
  672. fcputype = fat_arch_64.cputype;
  673. foffset = fat_arch_64.offset;
  674. if (swapped)
  675. {
  676. fcputype = __builtin_bswap32 (fcputype);
  677. foffset = __builtin_bswap64 (foffset);
  678. }
  679. }
  680. else
  681. {
  682. struct macho_fat_arch fat_arch_32;
  683. memcpy (&fat_arch_32,
  684. (const char *) arch_view.data + i * arch_size,
  685. arch_size);
  686. fcputype = fat_arch_32.cputype;
  687. foffset = (uint64_t) fat_arch_32.offset;
  688. if (swapped)
  689. {
  690. fcputype = __builtin_bswap32 (fcputype);
  691. foffset = (uint64_t) __builtin_bswap32 ((uint32_t) foffset);
  692. }
  693. }
  694. if (fcputype == cputype)
  695. {
  696. /* FIXME: What about cpusubtype? */
  697. backtrace_release_view (state, &arch_view, error_callback, data);
  698. return macho_add (state, filename, descriptor, foffset, match_uuid,
  699. base_address, skip_symtab, error_callback, data,
  700. fileline_fn, found_sym);
  701. }
  702. }
  703. error_callback (data, "could not find executable in fat file", 0);
  704. fail:
  705. if (arch_view_valid)
  706. backtrace_release_view (state, &arch_view, error_callback, data);
  707. if (descriptor != -1)
  708. backtrace_close (descriptor, error_callback, data);
  709. return 0;
  710. }
  711. /* Look for the dsym file for FILENAME. This is called if FILENAME
  712. does not have debug info or a symbol table. Returns 1 on success,
  713. 0 on failure. */
  714. static int
  715. macho_add_dsym (struct backtrace_state *state, const char *filename,
  716. uintptr_t base_address, const unsigned char *uuid,
  717. backtrace_error_callback error_callback, void *data,
  718. fileline* fileline_fn)
  719. {
  720. const char *p;
  721. const char *dirname;
  722. char *diralc;
  723. size_t dirnamelen;
  724. const char *basename;
  725. size_t basenamelen;
  726. const char *dsymsuffixdir;
  727. size_t dsymsuffixdirlen;
  728. size_t dsymlen;
  729. char *dsym;
  730. char *ps;
  731. int d;
  732. int does_not_exist;
  733. int dummy_found_sym;
  734. diralc = NULL;
  735. dirnamelen = 0;
  736. dsym = NULL;
  737. dsymlen = 0;
  738. p = strrchr (filename, '/');
  739. if (p == NULL)
  740. {
  741. dirname = ".";
  742. dirnamelen = 1;
  743. basename = filename;
  744. basenamelen = strlen (basename);
  745. diralc = NULL;
  746. }
  747. else
  748. {
  749. dirnamelen = p - filename;
  750. diralc = backtrace_alloc (state, dirnamelen + 1, error_callback, data);
  751. if (diralc == NULL)
  752. goto fail;
  753. memcpy (diralc, filename, dirnamelen);
  754. diralc[dirnamelen] = '\0';
  755. dirname = diralc;
  756. basename = p + 1;
  757. basenamelen = strlen (basename);
  758. }
  759. dsymsuffixdir = ".dSYM/Contents/Resources/DWARF/";
  760. dsymsuffixdirlen = strlen (dsymsuffixdir);
  761. dsymlen = (dirnamelen
  762. + 1
  763. + basenamelen
  764. + dsymsuffixdirlen
  765. + basenamelen
  766. + 1);
  767. dsym = backtrace_alloc (state, dsymlen, error_callback, data);
  768. if (dsym == NULL)
  769. goto fail;
  770. ps = dsym;
  771. memcpy (ps, dirname, dirnamelen);
  772. ps += dirnamelen;
  773. *ps++ = '/';
  774. memcpy (ps, basename, basenamelen);
  775. ps += basenamelen;
  776. memcpy (ps, dsymsuffixdir, dsymsuffixdirlen);
  777. ps += dsymsuffixdirlen;
  778. memcpy (ps, basename, basenamelen);
  779. ps += basenamelen;
  780. *ps = '\0';
  781. if (diralc != NULL)
  782. {
  783. backtrace_free (state, diralc, dirnamelen + 1, error_callback, data);
  784. diralc = NULL;
  785. }
  786. d = backtrace_open (dsym, error_callback, data, &does_not_exist);
  787. if (d < 0)
  788. {
  789. /* The file does not exist, so we can't read the debug info.
  790. Just return success. */
  791. backtrace_free (state, dsym, dsymlen, error_callback, data);
  792. return 1;
  793. }
  794. if (!macho_add (state, dsym, d, 0, uuid, base_address, 1,
  795. error_callback, data, fileline_fn, &dummy_found_sym))
  796. goto fail;
  797. backtrace_free (state, dsym, dsymlen, error_callback, data);
  798. return 1;
  799. fail:
  800. if (dsym != NULL)
  801. backtrace_free (state, dsym, dsymlen, error_callback, data);
  802. if (diralc != NULL)
  803. backtrace_free (state, diralc, dirnamelen, error_callback, data);
  804. return 0;
  805. }
  806. /* Add the backtrace data for a Macho-O file. Returns 1 on success, 0
  807. on failure (in both cases descriptor is closed).
  808. FILENAME: the name of the executable.
  809. DESCRIPTOR: an open descriptor for the executable, closed here.
  810. OFFSET: the offset within the file of this executable, for fat files.
  811. MATCH_UUID: if not NULL, UUID that must match.
  812. BASE_ADDRESS: the load address of the executable.
  813. SKIP_SYMTAB: if non-zero, ignore the symbol table; used for dSYM files.
  814. FILELINE_FN: set to the fileline function, by backtrace_dwarf_add.
  815. FOUND_SYM: set to non-zero if we found the symbol table.
  816. */
  817. static int
  818. macho_add (struct backtrace_state *state, const char *filename, int descriptor,
  819. off_t offset, const unsigned char *match_uuid,
  820. uintptr_t base_address, int skip_symtab,
  821. backtrace_error_callback error_callback, void *data,
  822. fileline *fileline_fn, int *found_sym)
  823. {
  824. struct backtrace_view header_view;
  825. struct macho_header_32 header;
  826. off_t hdroffset;
  827. int is_64;
  828. struct backtrace_view cmds_view;
  829. int cmds_view_valid;
  830. struct dwarf_sections dwarf_sections;
  831. int have_dwarf;
  832. unsigned char uuid[MACH_O_UUID_LEN];
  833. int have_uuid;
  834. size_t cmdoffset;
  835. unsigned int i;
  836. *found_sym = 0;
  837. cmds_view_valid = 0;
  838. /* The 32-bit and 64-bit file headers start out the same, so we can
  839. just always read the 32-bit version. A fat header is shorter but
  840. it will always be followed by data, so it's OK to read extra. */
  841. if (!backtrace_get_view (state, descriptor, offset,
  842. sizeof (struct macho_header_32),
  843. error_callback, data, &header_view))
  844. goto fail;
  845. memcpy (&header, header_view.data, sizeof header);
  846. backtrace_release_view (state, &header_view, error_callback, data);
  847. switch (header.magic)
  848. {
  849. case MACH_O_MH_MAGIC_32:
  850. is_64 = 0;
  851. hdroffset = offset + sizeof (struct macho_header_32);
  852. break;
  853. case MACH_O_MH_MAGIC_64:
  854. is_64 = 1;
  855. hdroffset = offset + sizeof (struct macho_header_64);
  856. break;
  857. case MACH_O_MH_MAGIC_FAT:
  858. case MACH_O_MH_MAGIC_FAT_64:
  859. {
  860. struct macho_header_fat fat_header;
  861. hdroffset = offset + sizeof (struct macho_header_fat);
  862. memcpy (&fat_header, &header, sizeof fat_header);
  863. return macho_add_fat (state, filename, descriptor, 0, hdroffset,
  864. match_uuid, base_address, skip_symtab,
  865. fat_header.nfat_arch,
  866. header.magic == MACH_O_MH_MAGIC_FAT_64,
  867. error_callback, data, fileline_fn, found_sym);
  868. }
  869. case MACH_O_MH_CIGAM_FAT:
  870. case MACH_O_MH_CIGAM_FAT_64:
  871. {
  872. struct macho_header_fat fat_header;
  873. uint32_t nfat_arch;
  874. hdroffset = offset + sizeof (struct macho_header_fat);
  875. memcpy (&fat_header, &header, sizeof fat_header);
  876. nfat_arch = __builtin_bswap32 (fat_header.nfat_arch);
  877. return macho_add_fat (state, filename, descriptor, 1, hdroffset,
  878. match_uuid, base_address, skip_symtab,
  879. nfat_arch,
  880. header.magic == MACH_O_MH_CIGAM_FAT_64,
  881. error_callback, data, fileline_fn, found_sym);
  882. }
  883. default:
  884. error_callback (data, "executable file is not in Mach-O format", 0);
  885. goto fail;
  886. }
  887. switch (header.filetype)
  888. {
  889. case MACH_O_MH_EXECUTE:
  890. case MACH_O_MH_DYLIB:
  891. case MACH_O_MH_DSYM:
  892. break;
  893. default:
  894. error_callback (data, "executable file is not an executable", 0);
  895. goto fail;
  896. }
  897. if (!backtrace_get_view (state, descriptor, hdroffset, header.sizeofcmds,
  898. error_callback, data, &cmds_view))
  899. goto fail;
  900. cmds_view_valid = 1;
  901. memset (&dwarf_sections, 0, sizeof dwarf_sections);
  902. have_dwarf = 0;
  903. memset (&uuid, 0, sizeof uuid);
  904. have_uuid = 0;
  905. cmdoffset = 0;
  906. for (i = 0; i < header.ncmds; ++i)
  907. {
  908. const char *pcmd;
  909. struct macho_load_command load_command;
  910. if (cmdoffset + sizeof load_command > header.sizeofcmds)
  911. break;
  912. pcmd = (const char *) cmds_view.data + cmdoffset;
  913. memcpy (&load_command, pcmd, sizeof load_command);
  914. switch (load_command.cmd)
  915. {
  916. case MACH_O_LC_SEGMENT:
  917. {
  918. struct macho_segment_command segcmd;
  919. memcpy (&segcmd, pcmd, sizeof segcmd);
  920. if (memcmp (segcmd.segname,
  921. "__DWARF\0\0\0\0\0\0\0\0\0",
  922. MACH_O_NAMELEN) == 0)
  923. {
  924. if (!macho_add_dwarf_segment (state, descriptor, offset,
  925. load_command.cmd,
  926. pcmd + sizeof segcmd,
  927. (load_command.cmdsize
  928. - sizeof segcmd),
  929. segcmd.nsects, error_callback,
  930. data, &dwarf_sections))
  931. goto fail;
  932. have_dwarf = 1;
  933. }
  934. }
  935. break;
  936. case MACH_O_LC_SEGMENT_64:
  937. {
  938. struct macho_segment_64_command segcmd;
  939. memcpy (&segcmd, pcmd, sizeof segcmd);
  940. if (memcmp (segcmd.segname,
  941. "__DWARF\0\0\0\0\0\0\0\0\0",
  942. MACH_O_NAMELEN) == 0)
  943. {
  944. if (!macho_add_dwarf_segment (state, descriptor, offset,
  945. load_command.cmd,
  946. pcmd + sizeof segcmd,
  947. (load_command.cmdsize
  948. - sizeof segcmd),
  949. segcmd.nsects, error_callback,
  950. data, &dwarf_sections))
  951. goto fail;
  952. have_dwarf = 1;
  953. }
  954. }
  955. break;
  956. case MACH_O_LC_SYMTAB:
  957. if (!skip_symtab)
  958. {
  959. struct macho_symtab_command symcmd;
  960. memcpy (&symcmd, pcmd, sizeof symcmd);
  961. if (!macho_add_symtab (state, descriptor, base_address, is_64,
  962. offset + symcmd.symoff, symcmd.nsyms,
  963. offset + symcmd.stroff, symcmd.strsize,
  964. error_callback, data))
  965. goto fail;
  966. *found_sym = 1;
  967. }
  968. break;
  969. case MACH_O_LC_UUID:
  970. {
  971. struct macho_uuid_command uuidcmd;
  972. memcpy (&uuidcmd, pcmd, sizeof uuidcmd);
  973. memcpy (&uuid[0], &uuidcmd.uuid[0], MACH_O_UUID_LEN);
  974. have_uuid = 1;
  975. }
  976. break;
  977. default:
  978. break;
  979. }
  980. cmdoffset += load_command.cmdsize;
  981. }
  982. if (!backtrace_close (descriptor, error_callback, data))
  983. goto fail;
  984. descriptor = -1;
  985. backtrace_release_view (state, &cmds_view, error_callback, data);
  986. cmds_view_valid = 0;
  987. if (match_uuid != NULL)
  988. {
  989. /* If we don't have a UUID, or it doesn't match, just ignore
  990. this file. */
  991. if (!have_uuid
  992. || memcmp (match_uuid, &uuid[0], MACH_O_UUID_LEN) != 0)
  993. return 1;
  994. }
  995. if (have_dwarf)
  996. {
  997. int is_big_endian;
  998. is_big_endian = 0;
  999. #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
  1000. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  1001. is_big_endian = 1;
  1002. #endif
  1003. #endif
  1004. if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
  1005. is_big_endian, NULL, error_callback, data,
  1006. fileline_fn, NULL))
  1007. goto fail;
  1008. }
  1009. if (!have_dwarf && have_uuid)
  1010. {
  1011. if (!macho_add_dsym (state, filename, base_address, &uuid[0],
  1012. error_callback, data, fileline_fn))
  1013. goto fail;
  1014. }
  1015. return 1;
  1016. fail:
  1017. if (cmds_view_valid)
  1018. backtrace_release_view (state, &cmds_view, error_callback, data);
  1019. if (descriptor != -1)
  1020. backtrace_close (descriptor, error_callback, data);
  1021. return 0;
  1022. }
  1023. #ifdef HAVE_MACH_O_DYLD_H
  1024. /* Initialize the backtrace data we need from a Mach-O executable
  1025. using the dyld support functions. This closes descriptor. */
  1026. int
  1027. backtrace_initialize (struct backtrace_state *state, const char *filename,
  1028. int descriptor, backtrace_error_callback error_callback,
  1029. void *data, fileline *fileline_fn)
  1030. {
  1031. uint32_t c;
  1032. uint32_t i;
  1033. int closed_descriptor;
  1034. int found_sym;
  1035. fileline macho_fileline_fn;
  1036. closed_descriptor = 0;
  1037. found_sym = 0;
  1038. macho_fileline_fn = macho_nodebug;
  1039. c = _dyld_image_count ();
  1040. for (i = 0; i < c; ++i)
  1041. {
  1042. uintptr_t base_address;
  1043. const char *name;
  1044. int d;
  1045. fileline mff;
  1046. int mfs;
  1047. name = _dyld_get_image_name (i);
  1048. if (name == NULL)
  1049. continue;
  1050. if (strcmp (name, filename) == 0 && !closed_descriptor)
  1051. {
  1052. d = descriptor;
  1053. closed_descriptor = 1;
  1054. }
  1055. else
  1056. {
  1057. int does_not_exist;
  1058. d = backtrace_open (name, error_callback, data, &does_not_exist);
  1059. if (d < 0)
  1060. continue;
  1061. }
  1062. base_address = _dyld_get_image_vmaddr_slide (i);
  1063. mff = macho_nodebug;
  1064. if (!macho_add (state, name, d, 0, NULL, base_address, 0,
  1065. error_callback, data, &mff, &mfs))
  1066. return 0;
  1067. if (mff != macho_nodebug)
  1068. macho_fileline_fn = mff;
  1069. if (mfs)
  1070. found_sym = 1;
  1071. }
  1072. if (!closed_descriptor)
  1073. backtrace_close (descriptor, error_callback, data);
  1074. if (!state->threaded)
  1075. {
  1076. if (found_sym)
  1077. state->syminfo_fn = macho_syminfo;
  1078. else if (state->syminfo_fn == NULL)
  1079. state->syminfo_fn = macho_nosyms;
  1080. }
  1081. else
  1082. {
  1083. if (found_sym)
  1084. backtrace_atomic_store_pointer (&state->syminfo_fn, macho_syminfo);
  1085. else
  1086. (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
  1087. macho_nosyms);
  1088. }
  1089. if (!state->threaded)
  1090. *fileline_fn = state->fileline_fn;
  1091. else
  1092. *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  1093. if (*fileline_fn == NULL || *fileline_fn == macho_nodebug)
  1094. *fileline_fn = macho_fileline_fn;
  1095. return 1;
  1096. }
  1097. #else /* !defined (HAVE_MACH_O_DYLD_H) */
  1098. /* Initialize the backtrace data we need from a Mach-O executable
  1099. without using the dyld support functions. This closes
  1100. descriptor. */
  1101. int
  1102. backtrace_initialize (struct backtrace_state *state, const char *filename,
  1103. int descriptor, backtrace_error_callback error_callback,
  1104. void *data, fileline *fileline_fn)
  1105. {
  1106. fileline macho_fileline_fn;
  1107. int found_sym;
  1108. macho_fileline_fn = macho_nodebug;
  1109. if (!macho_add (state, filename, descriptor, 0, NULL, 0, 0,
  1110. error_callback, data, &macho_fileline_fn, &found_sym))
  1111. return 0;
  1112. if (!state->threaded)
  1113. {
  1114. if (found_sym)
  1115. state->syminfo_fn = macho_syminfo;
  1116. else if (state->syminfo_fn == NULL)
  1117. state->syminfo_fn = macho_nosyms;
  1118. }
  1119. else
  1120. {
  1121. if (found_sym)
  1122. backtrace_atomic_store_pointer (&state->syminfo_fn, macho_syminfo);
  1123. else
  1124. (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
  1125. macho_nosyms);
  1126. }
  1127. if (!state->threaded)
  1128. *fileline_fn = state->fileline_fn;
  1129. else
  1130. *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  1131. if (*fileline_fn == NULL || *fileline_fn == macho_nodebug)
  1132. *fileline_fn = macho_fileline_fn;
  1133. return 1;
  1134. }
  1135. #endif /* !defined (HAVE_MACH_O_DYLD_H) */