This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
forked from ianlancetaylor/libbacktrace
-
Notifications
You must be signed in to change notification settings - Fork 12
/
macho.c
1418 lines (1223 loc) · 43.2 KB
/
macho.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* macho.c -- Get debug data from an Mach-O file for backtraces.
Copyright (C) 2012-2016 Free Software Foundation, Inc.
Written by John Colanduoni.
Pending upstream pull request:
https://github.com/ianlancetaylor/libbacktrace/pull/2
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
(1) Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
(3) The name of the author may not be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
/* We can't use autotools to detect the pointer width of our program because
we may be building a fat Mach-O file containing both 32-bit and 64-bit
variants. However Mach-O runs a limited set of platforms so detection
via preprocessor is not difficult. */
#if defined(__MACH__)
#if defined(__LP64__)
#define BACKTRACE_BITS 64
#else
#define BACKTRACE_BITS 32
#endif
#else
#error Attempting to build Mach-O support on incorrect platform
#endif
#if defined(__x86_64__)
#define NATIVE_CPU_TYPE CPU_TYPE_X86_64
#elif defined(__i386__)
#define NATIVE_CPU_TYPE CPU_TYPE_X86
#elif defined(__aarch64__)
#define NATIVE_CPU_TYPE CPU_TYPE_ARM64
#elif defined(__arm__)
#define NATIVE_CPU_TYPE CPU_TYPE_ARM
#else
#error Could not detect native Mach-O cpu_type_t
#endif
#include <sys/types.h>
#include <sys/syslimits.h>
#include <string.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/fat.h>
#include <mach-o/dyld.h>
#include <uuid/uuid.h>
#include <dirent.h>
#include <stdlib.h>
#include "backtrace.h"
#include "internal.h"
struct macho_commands_view
{
struct backtrace_view view;
uint32_t commands_count;
uint32_t commands_total_size;
int bytes_swapped;
size_t base_offset;
};
enum debug_section
{
DEBUG_INFO,
DEBUG_LINE,
DEBUG_ABBREV,
DEBUG_RANGES,
DEBUG_STR,
DEBUG_MAX
};
static const char *const debug_section_names[DEBUG_MAX] =
{
"__debug_info",
"__debug_line",
"__debug_abbrev",
"__debug_ranges",
"__debug_str"
};
struct found_dwarf_section
{
uint32_t file_offset;
uintptr_t file_size;
const unsigned char *data;
};
/* Mach-O symbols don't have a length. As a result we have to infer it
by sorting the symbol addresses for each image and recording the
memory range attributed to each image. */
struct macho_symbol
{
uintptr_t addr;
size_t size;
const char *name;
};
struct macho_syminfo_data
{
struct macho_syminfo_data *next;
struct macho_symbol *symbols;
size_t symbol_count;
uintptr_t min_addr;
uintptr_t max_addr;
};
uint16_t
macho_file_to_host_u16 (int file_bytes_swapped, uint16_t input)
{
if (file_bytes_swapped)
return (input >> 8) | (input << 8);
else
return input;
}
uint32_t
macho_file_to_host_u32 (int file_bytes_swapped, uint32_t input)
{
if (file_bytes_swapped)
{
return ((input >> 24) & 0x000000FF)
| ((input >> 8) & 0x0000FF00)
| ((input << 8) & 0x00FF0000)
| ((input << 24) & 0xFF000000);
}
else
{
return input;
}
}
uint64_t
macho_file_to_host_u64 (int file_bytes_swapped, uint64_t input)
{
if (file_bytes_swapped)
{
return macho_file_to_host_u32 (file_bytes_swapped,
(uint32_t) (input >> 32))
| (((uint64_t) macho_file_to_host_u32 (file_bytes_swapped,
(uint32_t) input)) << 32);
}
else
{
return input;
}
}
#if BACKTRACE_BITS == 64
#define macho_file_to_host_usize macho_file_to_host_u64
typedef struct mach_header_64 mach_header_native_t;
#define LC_SEGMENT_NATIVE LC_SEGMENT_64
typedef struct segment_command_64 segment_command_native_t;
typedef struct nlist_64 nlist_native_t;
typedef struct section_64 section_native_t;
#else /* BACKTRACE_BITS == 32 */
#define macho_file_to_host_usize macho_file_to_host_u32
typedef struct mach_header mach_header_native_t;
#define LC_SEGMENT_NATIVE LC_SEGMENT
typedef struct segment_command segment_command_native_t;
typedef struct nlist nlist_native_t;
typedef struct section section_native_t;
#endif
// Gets a view into a Mach-O image, taking any slice offset into account
int
macho_get_view (struct backtrace_state *state, int descriptor,
off_t offset, size_t size,
backtrace_error_callback error_callback,
void *data, struct macho_commands_view *commands_view,
struct backtrace_view *view)
{
return backtrace_get_view (state, descriptor,
commands_view->base_offset + offset, size,
error_callback, data, view);
}
int
macho_get_commands (struct backtrace_state *state, int descriptor,
backtrace_error_callback error_callback,
void *data, struct macho_commands_view *commands_view,
int *incompatible)
{
int ret = 0;
int is_fat = 0;
struct backtrace_view file_header_view;
int file_header_view_valid = 0;
struct backtrace_view fat_archs_view;
int fat_archs_view_valid = 0;
const mach_header_native_t *file_header;
uint64_t commands_offset;
*incompatible = 0;
if (!backtrace_get_view (state, descriptor, 0, sizeof (mach_header_native_t),
error_callback, data, &file_header_view))
goto end;
file_header_view_valid = 1;
switch (*(uint32_t *) file_header_view.data)
{
case MH_MAGIC:
if (BACKTRACE_BITS == 32)
commands_view->bytes_swapped = 0;
else
{
*incompatible = 1;
goto end;
}
break;
case MH_CIGAM:
if (BACKTRACE_BITS == 32)
commands_view->bytes_swapped = 1;
else
{
*incompatible = 1;
goto end;
}
break;
case MH_MAGIC_64:
if (BACKTRACE_BITS == 64)
commands_view->bytes_swapped = 0;
else
{
*incompatible = 1;
goto end;
}
break;
case MH_CIGAM_64:
if (BACKTRACE_BITS == 64)
commands_view->bytes_swapped = 1;
else
{
*incompatible = 1;
goto end;
}
break;
case FAT_MAGIC:
is_fat = 1;
commands_view->bytes_swapped = 0;
break;
case FAT_CIGAM:
is_fat = 1;
commands_view->bytes_swapped = 1;
break;
default:
goto end;
}
if (is_fat)
{
uint32_t native_slice_offset;
size_t archs_total_size;
uint32_t arch_count;
const struct fat_header *fat_header;
const struct fat_arch *archs;
uint32_t i;
fat_header = file_header_view.data;
arch_count =
macho_file_to_host_u32 (commands_view->bytes_swapped,
fat_header->nfat_arch);
archs_total_size = arch_count * sizeof (struct fat_arch);
if (!backtrace_get_view (state, descriptor, sizeof (struct fat_header),
archs_total_size, error_callback,
data, &fat_archs_view))
goto end;
fat_archs_view_valid = 1;
native_slice_offset = 0;
archs = fat_archs_view.data;
for (i = 0; i < arch_count; i++)
{
const struct fat_arch *raw_arch = archs + i;
int cpu_type =
(int) macho_file_to_host_u32 (commands_view->bytes_swapped,
(uint32_t) raw_arch->cputype);
if (cpu_type == NATIVE_CPU_TYPE)
{
native_slice_offset =
macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_arch->offset);
break;
}
}
if (native_slice_offset == 0)
{
*incompatible = 1;
goto end;
}
backtrace_release_view (state, &file_header_view, error_callback, data);
file_header_view_valid = 0;
if (!backtrace_get_view (state, descriptor, native_slice_offset,
sizeof (mach_header_native_t), error_callback,
data, &file_header_view))
goto end;
file_header_view_valid = 1;
// The endianess of the slice may be different than the fat image
switch (*(uint32_t *) file_header_view.data)
{
case MH_MAGIC:
if (BACKTRACE_BITS == 32)
commands_view->bytes_swapped = 0;
else
goto end;
break;
case MH_CIGAM:
if (BACKTRACE_BITS == 32)
commands_view->bytes_swapped = 1;
else
goto end;
break;
case MH_MAGIC_64:
if (BACKTRACE_BITS == 64)
commands_view->bytes_swapped = 0;
else
goto end;
break;
case MH_CIGAM_64:
if (BACKTRACE_BITS == 64)
commands_view->bytes_swapped = 1;
else
goto end;
break;
default:
goto end;
}
commands_view->base_offset = native_slice_offset;
}
else
commands_view->base_offset = 0;
file_header = file_header_view.data;
commands_view->commands_count =
macho_file_to_host_u32 (commands_view->bytes_swapped,
file_header->ncmds);
commands_view->commands_total_size =
macho_file_to_host_u32 (commands_view->bytes_swapped,
file_header->sizeofcmds);
commands_offset =
commands_view->base_offset + sizeof (mach_header_native_t);
if (!backtrace_get_view (state, descriptor, commands_offset,
commands_view->commands_total_size, error_callback,
data, &commands_view->view))
goto end;
ret = 1;
end:
if (file_header_view_valid)
backtrace_release_view (state, &file_header_view, error_callback, data);
if (fat_archs_view_valid)
backtrace_release_view (state, &fat_archs_view, error_callback, data);
return ret;
}
int
macho_get_uuid (struct backtrace_state *state ATTRIBUTE_UNUSED,
int descriptor ATTRIBUTE_UNUSED,
backtrace_error_callback error_callback,
void *data, struct macho_commands_view *commands_view,
uuid_t *uuid)
{
size_t offset = 0;
uint32_t i = 0;
for (i = 0; i < commands_view->commands_count; i++)
{
const struct load_command *raw_command;
struct load_command command;
if (offset + sizeof (struct load_command)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
raw_command =
commands_view->view.data + offset;
command.cmd = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmd);
command.cmdsize = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmdsize);
if (command.cmd == LC_UUID)
{
const struct uuid_command *uuid_command;
if (offset + sizeof (struct uuid_command)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
uuid_command =
(struct uuid_command *) raw_command;
memcpy (uuid, uuid_command->uuid, sizeof (uuid_t));
return 1;
}
offset += command.cmdsize;
}
error_callback (data, "executable file is missing an identifying UUID", 0);
return 0;
}
/* Returns the base address of a Mach-O image, as encoded in the file header.
* WARNING: This does not take ASLR into account, which is ubiquitous on recent
* Darwin platforms.
*/
int
macho_get_addr_range (struct backtrace_state *state ATTRIBUTE_UNUSED,
int descriptor ATTRIBUTE_UNUSED,
backtrace_error_callback error_callback,
void *data, struct macho_commands_view *commands_view,
uintptr_t *base_address, uintptr_t *max_address)
{
size_t offset = 0;
int found_text = 0;
uint32_t i = 0;
*max_address = 0;
for (i = 0; i < commands_view->commands_count; i++)
{
const struct load_command *raw_command;
struct load_command command;
if (offset + sizeof (struct load_command)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
raw_command = commands_view->view.data + offset;
command.cmd = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmd);
command.cmdsize = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmdsize);
if (command.cmd == LC_SEGMENT_NATIVE)
{
const segment_command_native_t *raw_segment;
uintptr_t segment_vmaddr;
uintptr_t segment_vmsize;
uintptr_t segment_maxaddr;
uintptr_t text_fileoff;
if (offset + sizeof (segment_command_native_t)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
raw_segment = (segment_command_native_t *) raw_command;
segment_vmaddr = macho_file_to_host_usize (
commands_view->bytes_swapped, raw_segment->vmaddr);
segment_vmsize = macho_file_to_host_usize (
commands_view->bytes_swapped, raw_segment->vmsize);
segment_maxaddr = segment_vmaddr + segment_vmsize;
if (strncmp (raw_segment->segname, "__TEXT",
sizeof (raw_segment->segname)) == 0)
{
text_fileoff = macho_file_to_host_usize (
commands_view->bytes_swapped, raw_segment->fileoff);
*base_address = segment_vmaddr - text_fileoff;
found_text = 1;
}
if (segment_maxaddr > *max_address)
*max_address = segment_maxaddr;
}
offset += command.cmdsize;
}
if (found_text)
return 1;
else
{
error_callback (data, "executable is missing __TEXT segment", 0);
return 0;
}
}
static int
macho_symbol_compare_addr (const void *left_raw, const void *right_raw)
{
const struct macho_symbol *left = left_raw;
const struct macho_symbol *right = right_raw;
if (left->addr > right->addr)
return 1;
else if (left->addr < right->addr)
return -1;
else
return 0;
}
int
macho_symbol_type_relevant (uint8_t type)
{
uint8_t type_field = (uint8_t) (type & N_TYPE);
return !(type & N_EXT) &&
(type_field == N_ABS || type_field == N_SECT);
}
int
macho_add_symtab (struct backtrace_state *state,
backtrace_error_callback error_callback,
void *data, int descriptor,
struct macho_commands_view *commands_view,
uintptr_t base_address, uintptr_t max_image_address,
intptr_t vmslide, int *found_sym)
{
struct macho_syminfo_data *syminfo_data;
int ret = 0;
size_t offset = 0;
struct backtrace_view symtab_view;
int symtab_view_valid = 0;
struct backtrace_view strtab_view;
int strtab_view_valid = 0;
size_t syminfo_index = 0;
size_t function_count = 0;
uint32_t i = 0;
uint32_t j = 0;
uint32_t symtab_index = 0;
*found_sym = 0;
for (i = 0; i < commands_view->commands_count; i++)
{
const struct load_command *raw_command;
struct load_command command;
if (offset + sizeof (struct load_command)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
raw_command = commands_view->view.data + offset;
command.cmd = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmd);
command.cmdsize = macho_file_to_host_u32 (commands_view->bytes_swapped,
raw_command->cmdsize);
if (command.cmd == LC_SYMTAB)
{
const struct symtab_command *symtab_command;
uint32_t symbol_table_offset;
uint32_t symbol_count;
uint32_t string_table_offset;
uint32_t string_table_size;
if (offset + sizeof (struct symtab_command)
> commands_view->commands_total_size)
{
error_callback (data,
"executable file contains out of range command offset",
0);
return 0;
}
symtab_command = (struct symtab_command *) raw_command;
symbol_table_offset = macho_file_to_host_u32 (
commands_view->bytes_swapped, symtab_command->symoff);
symbol_count = macho_file_to_host_u32 (
commands_view->bytes_swapped, symtab_command->nsyms);
string_table_offset = macho_file_to_host_u32 (
commands_view->bytes_swapped, symtab_command->stroff);
string_table_size = macho_file_to_host_u32 (
commands_view->bytes_swapped, symtab_command->strsize);
if (!macho_get_view (state, descriptor, symbol_table_offset,
symbol_count * sizeof (nlist_native_t),
error_callback, data, commands_view,
&symtab_view))
goto end;
symtab_view_valid = 1;
if (!macho_get_view (state, descriptor, string_table_offset,
string_table_size, error_callback, data,
commands_view, &strtab_view))
goto end;
strtab_view_valid = 1;
// Count functions first
for (j = 0; j < symbol_count; j++)
{
const nlist_native_t *raw_sym =
((const nlist_native_t *) symtab_view.data) + j;
if (macho_symbol_type_relevant (raw_sym->n_type))
{
function_count += 1;
}
}
// Allocate space for the:
// (a) macho_syminfo_data for this image
// (b) macho_symbol entries
syminfo_data =
backtrace_alloc (state,
sizeof (struct macho_syminfo_data),
error_callback, data);
if (syminfo_data == NULL)
goto end;
syminfo_data->symbols = backtrace_alloc (
state, function_count * sizeof (struct macho_symbol),
error_callback, data);
if (syminfo_data->symbols == NULL)
goto end;
syminfo_data->symbol_count = function_count;
syminfo_data->next = NULL;
syminfo_data->min_addr = base_address;
syminfo_data->max_addr = max_image_address;
for (symtab_index = 0;
symtab_index < symbol_count; symtab_index++)
{
const nlist_native_t *raw_sym =
((const nlist_native_t *) symtab_view.data) +
symtab_index;
if (macho_symbol_type_relevant (raw_sym->n_type))
{
size_t strtab_index;
const char *name;
size_t max_len_plus_one;
syminfo_data->symbols[syminfo_index].addr =
macho_file_to_host_usize (commands_view->bytes_swapped,
raw_sym->n_value) + vmslide;
strtab_index = macho_file_to_host_u32 (
commands_view->bytes_swapped,
raw_sym->n_un.n_strx);
// Check the range of the supposed "string" we've been
// given
if (strtab_index >= string_table_size)
{
error_callback (
data,
"dSYM file contains out of range string table index",
0);
goto end;
}
name = strtab_view.data + strtab_index;
max_len_plus_one = string_table_size - strtab_index;
if (strnlen (name, max_len_plus_one) >= max_len_plus_one)
{
error_callback (
data,
"dSYM file contains unterminated string",
0);
goto end;
}
// Remove underscore prefixes
if (name[0] == '_')
{
name = name + 1;
}
syminfo_data->symbols[syminfo_index].name = name;
syminfo_index += 1;
}
}
backtrace_qsort (syminfo_data->symbols,
syminfo_data->symbol_count,
sizeof (struct macho_symbol),
macho_symbol_compare_addr);
// Calculate symbol sizes
for (syminfo_index = 0;
syminfo_index < syminfo_data->symbol_count; syminfo_index++)
{
if (syminfo_index + 1 < syminfo_data->symbol_count)
{
syminfo_data->symbols[syminfo_index].size =
syminfo_data->symbols[syminfo_index + 1].addr -
syminfo_data->symbols[syminfo_index].addr;
}
else
{
syminfo_data->symbols[syminfo_index].size =
max_image_address -
syminfo_data->symbols[syminfo_index].addr;
}
}
if (!state->threaded)
{
struct macho_syminfo_data **pp;
for (pp = (struct macho_syminfo_data **) (void *) &state->syminfo_data;
*pp != NULL;
pp = &(*pp)->next);
*pp = syminfo_data;
}
else
{
while (1)
{
struct macho_syminfo_data **pp;
pp = (struct macho_syminfo_data **) (void *) &state->syminfo_data;
while (1)
{
struct macho_syminfo_data *p;
p = backtrace_atomic_load_pointer (pp);
if (p == NULL)
break;
pp = &p->next;
}
if (__sync_bool_compare_and_swap (pp, NULL, syminfo_data))
break;
}
}
strtab_view_valid = 0; // We need to keep string table around
*found_sym = 1;
ret = 1;
goto end;
}
offset += command.cmdsize;
}
// No symbol table here
ret = 1;
goto end;
end:
if (symtab_view_valid)
backtrace_release_view (state, &symtab_view, error_callback, data);
if (strtab_view_valid)
backtrace_release_view (state, &strtab_view, error_callback, data);
return ret;
}
int
macho_try_dwarf (struct backtrace_state *state,
backtrace_error_callback error_callback,
void *data, fileline *fileline_fn, uuid_t *executable_uuid,
uintptr_t base_address, uintptr_t max_image_address,
intptr_t vmslide, char *dwarf_filename, int *matched,
int *found_sym, int *found_dwarf)
{
uuid_t dwarf_uuid;
int ret = 0;
int dwarf_descriptor;
int dwarf_descriptor_valid = 0;
struct macho_commands_view commands_view;
int commands_view_valid = 0;
struct backtrace_view dwarf_view;
int dwarf_view_valid = 0;
size_t offset = 0;
struct found_dwarf_section dwarf_sections[DEBUG_MAX];
uintptr_t min_dwarf_offset = 0;
uintptr_t max_dwarf_offset = 0;
uint32_t i = 0;
uint32_t j = 0;
int k = 0;
*matched = 0;
*found_sym = 0;
*found_dwarf = 0;
if ((dwarf_descriptor = backtrace_open (dwarf_filename, error_callback,
data, NULL)) == 0)
goto end;
dwarf_descriptor_valid = 1;
int incompatible;
if (!macho_get_commands (state, dwarf_descriptor, error_callback, data,
&commands_view, &incompatible))
{
// Failing to read the header here is fine, because this dSYM may be
// for a different architecture
if (incompatible)
{
ret = 1;
}
goto end;
}
commands_view_valid = 1;
// Get dSYM UUID and compare
if (!macho_get_uuid (state, dwarf_descriptor, error_callback, data,
&commands_view, &dwarf_uuid))
{
error_callback (data, "dSYM file is missing an identifying uuid", 0);
goto end;
}
if (memcmp (executable_uuid, &dwarf_uuid, sizeof (uuid_t)) != 0)
{
// DWARF doesn't belong to desired executable
ret = 1;
goto end;
}
*matched = 1;
// Read symbol table
if (!macho_add_symtab (state, error_callback, data, dwarf_descriptor,
&commands_view, base_address, max_image_address,
vmslide, found_sym))
goto end;
// Get DWARF sections
memset (dwarf_sections, 0, sizeof (dwarf_sections));
offset = 0;
for (i = 0; i < commands_view.commands_count; i++)
{
const struct load_command *raw_command;
struct load_command command;
if (offset + sizeof (struct load_command)
> commands_view.commands_total_size)
{
error_callback (data,
"dSYM file contains out of range command offset", 0);
goto end;
}
raw_command = commands_view.view.data + offset;
command.cmd = macho_file_to_host_u32 (commands_view.bytes_swapped,
raw_command->cmd);
command.cmdsize = macho_file_to_host_u32 (commands_view.bytes_swapped,
raw_command->cmdsize);
if (command.cmd == LC_SEGMENT_NATIVE)
{
uint32_t section_count;
size_t section_offset;
const segment_command_native_t *raw_segment;
if (offset + sizeof (segment_command_native_t)
> commands_view.commands_total_size)
{
error_callback (data,
"dSYM file contains out of range command offset",
0);
goto end;
}
raw_segment = (const segment_command_native_t *) raw_command;
if (strncmp (raw_segment->segname, "__DWARF",
sizeof (raw_segment->segname)) == 0)
{
section_count = macho_file_to_host_u32 (
commands_view.bytes_swapped,
raw_segment->nsects);
section_offset = offset + sizeof (segment_command_native_t);
// Search sections for relevant DWARF section names
for (j = 0; j < section_count; j++)
{
const section_native_t *raw_section;
if (section_offset + sizeof (section_native_t) >
commands_view.commands_total_size)
{
error_callback (data,
"dSYM file contains out of range command offset",
0);
goto end;
}
raw_section = commands_view.view.data + section_offset;
for (k = 0; k < DEBUG_MAX; k++)
{
uintptr_t dwarf_section_end;
if (strncmp (raw_section->sectname,
debug_section_names[k],
sizeof (raw_section->sectname)) == 0)
{
*found_dwarf = 1;
dwarf_sections[k].file_offset =
macho_file_to_host_u32 (
commands_view.bytes_swapped,
raw_section->offset);
dwarf_sections[k].file_size =
macho_file_to_host_usize (
commands_view.bytes_swapped,
raw_section->size);
if (min_dwarf_offset == 0 ||
dwarf_sections[k].file_offset <
min_dwarf_offset)
min_dwarf_offset = dwarf_sections[k].file_offset;
dwarf_section_end =
dwarf_sections[k].file_offset +
dwarf_sections[k].file_size;
if (dwarf_section_end > max_dwarf_offset)
max_dwarf_offset = dwarf_section_end;
break;
}
}
section_offset += sizeof (section_native_t);
}
break;
}
}
offset += command.cmdsize;
}
if (!*found_dwarf)
{
// No DWARF in this file
ret = 1;
goto end;
}
if (!macho_get_view (state, dwarf_descriptor, (off_t) min_dwarf_offset,
max_dwarf_offset - min_dwarf_offset, error_callback,
data, &commands_view, &dwarf_view))