forked from ianlancetaylor/libbacktrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarf.c
3126 lines (2699 loc) · 79 KB
/
dwarf.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
/* dwarf.c -- Get file/line information from DWARF for backtraces.
Copyright (C) 2012-2018 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
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"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "backtrace.h"
#include "internal.h"
/* DWARF constants. */
enum dwarf_tag {
DW_TAG_entry_point = 0x3,
DW_TAG_compile_unit = 0x11,
DW_TAG_inlined_subroutine = 0x1d,
DW_TAG_subprogram = 0x2e,
};
enum dwarf_form {
DW_FORM_addr = 0x1,
DW_FORM_block2 = 0x3,
DW_FORM_block4 = 0x4,
DW_FORM_data2 = 0x5,
DW_FORM_data4 = 0x6,
DW_FORM_data8 = 0x07,
DW_FORM_string = 0x08,
DW_FORM_block = 0x09,
DW_FORM_block1 = 0x0a,
DW_FORM_data1 = 0x0b,
DW_FORM_flag = 0x0c,
DW_FORM_sdata = 0x0d,
DW_FORM_strp = 0x0e,
DW_FORM_udata = 0x0f,
DW_FORM_ref_addr = 0x10,
DW_FORM_ref1 = 0x11,
DW_FORM_ref2 = 0x12,
DW_FORM_ref4 = 0x13,
DW_FORM_ref8 = 0x14,
DW_FORM_ref_udata = 0x15,
DW_FORM_indirect = 0x16,
DW_FORM_sec_offset = 0x17,
DW_FORM_exprloc = 0x18,
DW_FORM_flag_present = 0x19,
DW_FORM_ref_sig8 = 0x20,
DW_FORM_GNU_addr_index = 0x1f01,
DW_FORM_GNU_str_index = 0x1f02,
DW_FORM_GNU_ref_alt = 0x1f20,
DW_FORM_GNU_strp_alt = 0x1f21,
};
enum dwarf_attribute {
DW_AT_name = 0x3,
DW_AT_stmt_list = 0x10,
DW_AT_low_pc = 0x11,
DW_AT_high_pc = 0x12,
DW_AT_comp_dir = 0x1b,
DW_AT_abstract_origin = 0x31,
DW_AT_specification = 0x47,
DW_AT_ranges = 0x55,
DW_AT_call_file = 0x58,
DW_AT_call_line = 0x59,
DW_AT_linkage_name = 0x6e,
DW_AT_MIPS_linkage_name = 0x2007,
};
enum dwarf_line_number_op {
DW_LNS_extended_op = 0x0,
DW_LNS_copy = 0x1,
DW_LNS_advance_pc = 0x2,
DW_LNS_advance_line = 0x3,
DW_LNS_set_file = 0x4,
DW_LNS_set_column = 0x5,
DW_LNS_negate_stmt = 0x6,
DW_LNS_set_basic_block = 0x7,
DW_LNS_const_add_pc = 0x8,
DW_LNS_fixed_advance_pc = 0x9,
DW_LNS_set_prologue_end = 0xa,
DW_LNS_set_epilogue_begin = 0xb,
DW_LNS_set_isa = 0xc,
};
enum dwarf_extedned_line_number_op {
DW_LNE_end_sequence = 0x1,
DW_LNE_set_address = 0x2,
DW_LNE_define_file = 0x3,
DW_LNE_set_discriminator = 0x4,
};
#if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__) || defined (__CYGWIN__)
# define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
#define HAS_DRIVE_SPEC(f) ((f)[0] && (f)[1] == ':')
# define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR(f[0]) || HAS_DRIVE_SPEC(f))
#else
# define IS_DIR_SEPARATOR(c) ((c) == '/')
# define IS_ABSOLUTE_PATH(f) IS_DIR_SEPARATOR(f[0])
#endif
#if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
/* If strnlen is not declared, provide our own version. */
static size_t
xstrnlen (const char *s, size_t maxlen)
{
size_t i;
for (i = 0; i < maxlen; ++i)
if (s[i] == '\0')
break;
return i;
}
#define strnlen xstrnlen
#endif
/* A buffer to read DWARF info. */
struct dwarf_buf
{
/* Buffer name for error messages. */
const char *name;
/* Start of the buffer. */
const unsigned char *start;
/* Next byte to read. */
const unsigned char *buf;
/* The number of bytes remaining. */
size_t left;
/* Whether the data is big-endian. */
int is_bigendian;
/* Error callback routine. */
backtrace_error_callback error_callback;
/* Data for error_callback. */
void *data;
/* Non-zero if we've reported an underflow error. */
int reported_underflow;
};
/* A single attribute in a DWARF abbreviation. */
struct attr
{
/* The attribute name. */
enum dwarf_attribute name;
/* The attribute form. */
enum dwarf_form form;
};
/* A single DWARF abbreviation. */
struct abbrev
{
/* The abbrev code--the number used to refer to the abbrev. */
uint64_t code;
/* The entry tag. */
enum dwarf_tag tag;
/* Non-zero if this abbrev has child entries. */
int has_children;
/* The number of attributes. */
size_t num_attrs;
/* The attributes. */
struct attr *attrs;
};
/* The DWARF abbreviations for a compilation unit. This structure
only exists while reading the compilation unit. Most DWARF readers
seem to a hash table to map abbrev ID's to abbrev entries.
However, we primarily care about GCC, and GCC simply issues ID's in
numerical order starting at 1. So we simply keep a sorted vector,
and try to just look up the code. */
struct abbrevs
{
/* The number of abbrevs in the vector. */
size_t num_abbrevs;
/* The abbrevs, sorted by the code field. */
struct abbrev *abbrevs;
};
/* The different kinds of attribute values. */
enum attr_val_encoding
{
/* An address. */
ATTR_VAL_ADDRESS,
/* A unsigned integer. */
ATTR_VAL_UINT,
/* A sigd integer. */
ATTR_VAL_SINT,
/* A string. */
ATTR_VAL_STRING,
/* An offset to other data in the containing unit. */
ATTR_VAL_REF_UNIT,
/* An offset to other data within the .dwarf_info section. */
ATTR_VAL_REF_INFO,
/* An offset to data in some other section. */
ATTR_VAL_REF_SECTION,
/* A type signature. */
ATTR_VAL_REF_TYPE,
/* A block of data (not represented). */
ATTR_VAL_BLOCK,
/* An expression (not represented). */
ATTR_VAL_EXPR,
};
/* An attribute value. */
struct attr_val
{
/* How the value is stored in the field u. */
enum attr_val_encoding encoding;
union
{
/* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
uint64_t uint;
/* ATTR_VAL_SINT. */
int64_t sint;
/* ATTR_VAL_STRING. */
const char *string;
/* ATTR_VAL_BLOCK not stored. */
} u;
};
/* The line number program header. */
struct line_header
{
/* The version of the line number information. */
int version;
/* The minimum instruction length. */
unsigned int min_insn_len;
/* The maximum number of ops per instruction. */
unsigned int max_ops_per_insn;
/* The line base for special opcodes. */
int line_base;
/* The line range for special opcodes. */
unsigned int line_range;
/* The opcode base--the first special opcode. */
unsigned int opcode_base;
/* Opcode lengths, indexed by opcode - 1. */
const unsigned char *opcode_lengths;
/* The number of directory entries. */
size_t dirs_count;
/* The directory entries. */
const char **dirs;
/* The number of filenames. */
size_t filenames_count;
/* The filenames. */
const char **filenames;
};
/* Map a single PC value to a file/line. We will keep a vector of
these sorted by PC value. Each file/line will be correct from the
PC up to the PC of the next entry if there is one. We allocate one
extra entry at the end so that we can use bsearch. */
struct line
{
/* PC. */
uintptr_t pc;
/* File name. Many entries in the array are expected to point to
the same file name. */
const char *filename;
/* Line number. */
int lineno;
/* Index of the object in the original array read from the DWARF
section, before it has been sorted. The index makes it possible
to use Quicksort and maintain stability. */
int idx;
};
/* A growable vector of line number information. This is used while
reading the line numbers. */
struct line_vector
{
/* Memory. This is an array of struct line. */
struct backtrace_vector vec;
/* Number of valid mappings. */
size_t count;
};
/* A function described in the debug info. */
struct function
{
/* The name of the function. */
const char *name;
/* If this is an inlined function, the filename of the call
site. */
const char *caller_filename;
/* If this is an inlined function, the line number of the call
site. */
int caller_lineno;
/* Map PC ranges to inlined functions. */
struct function_addrs *function_addrs;
size_t function_addrs_count;
};
/* An address range for a function. This maps a PC value to a
specific function. */
struct function_addrs
{
/* Range is LOW <= PC < HIGH. */
uint64_t low;
uint64_t high;
/* Function for this address range. */
struct function *function;
};
/* A growable vector of function address ranges. */
struct function_vector
{
/* Memory. This is an array of struct function_addrs. */
struct backtrace_vector vec;
/* Number of address ranges present. */
size_t count;
};
/* A DWARF compilation unit. This only holds the information we need
to map a PC to a file and line. */
struct unit
{
/* The first entry for this compilation unit. */
const unsigned char *unit_data;
/* The length of the data for this compilation unit. */
size_t unit_data_len;
/* The offset of UNIT_DATA from the start of the information for
this compilation unit. */
size_t unit_data_offset;
/* DWARF version. */
int version;
/* Whether unit is DWARF64. */
int is_dwarf64;
/* Address size. */
int addrsize;
/* Offset into line number information. */
off_t lineoff;
/* Primary source file. */
const char *filename;
/* Compilation command working directory. */
const char *comp_dir;
/* Absolute file name, only set if needed. */
const char *abs_filename;
/* The abbreviations for this unit. */
struct abbrevs abbrevs;
/* The fields above this point are read in during initialization and
may be accessed freely. The fields below this point are read in
as needed, and therefore require care, as different threads may
try to initialize them simultaneously. */
/* PC to line number mapping. This is NULL if the values have not
been read. This is (struct line *) -1 if there was an error
reading the values. */
struct line *lines;
/* Number of entries in lines. */
size_t lines_count;
/* PC ranges to function. */
struct function_addrs *function_addrs;
size_t function_addrs_count;
};
/* An address range for a compilation unit. This maps a PC value to a
specific compilation unit. Note that we invert the representation
in DWARF: instead of listing the units and attaching a list of
ranges, we list the ranges and have each one point to the unit.
This lets us do a binary search to find the unit. */
struct unit_addrs
{
/* Range is LOW <= PC < HIGH. */
uint64_t low;
uint64_t high;
/* Compilation unit for this address range. */
struct unit *u;
};
/* A growable vector of compilation unit address ranges. */
struct unit_addrs_vector
{
/* Memory. This is an array of struct unit_addrs. */
struct backtrace_vector vec;
/* Number of address ranges present. */
size_t count;
};
/* The information we need to map a PC to a file and line. */
struct dwarf_data
{
/* The data for the next file we know about. */
struct dwarf_data *next;
/* The base address for this file. */
uintptr_t base_address;
/* A sorted list of address ranges. */
struct unit_addrs *addrs;
/* Number of address ranges in list. */
size_t addrs_count;
/* The unparsed .debug_info section. */
const unsigned char *dwarf_info;
size_t dwarf_info_size;
/* The unparsed .debug_line section. */
const unsigned char *dwarf_line;
size_t dwarf_line_size;
/* The unparsed .debug_ranges section. */
const unsigned char *dwarf_ranges;
size_t dwarf_ranges_size;
/* The unparsed .debug_str section. */
const unsigned char *dwarf_str;
size_t dwarf_str_size;
/* Whether the data is big-endian or not. */
int is_bigendian;
/* A vector used for function addresses. We keep this here so that
we can grow the vector as we read more functions. */
struct function_vector fvec;
};
/* Report an error for a DWARF buffer. */
static void
dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
{
char b[200];
snprintf (b, sizeof b, "%s in %s at %d",
msg, buf->name, (int) (buf->buf - buf->start));
buf->error_callback (buf->data, b, 0);
}
/* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
error. */
static int
require (struct dwarf_buf *buf, size_t count)
{
if (buf->left >= count)
return 1;
if (!buf->reported_underflow)
{
dwarf_buf_error (buf, "DWARF underflow");
buf->reported_underflow = 1;
}
return 0;
}
/* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
error. */
static int
advance (struct dwarf_buf *buf, size_t count)
{
if (!require (buf, count))
return 0;
buf->buf += count;
buf->left -= count;
return 1;
}
/* Read one byte from BUF and advance 1 byte. */
static unsigned char
read_byte (struct dwarf_buf *buf)
{
const unsigned char *p = buf->buf;
if (!advance (buf, 1))
return 0;
return p[0];
}
/* Read a signed char from BUF and advance 1 byte. */
static signed char
read_sbyte (struct dwarf_buf *buf)
{
const unsigned char *p = buf->buf;
if (!advance (buf, 1))
return 0;
return (*p ^ 0x80) - 0x80;
}
/* Read a uint16 from BUF and advance 2 bytes. */
static uint16_t
read_uint16 (struct dwarf_buf *buf)
{
const unsigned char *p = buf->buf;
if (!advance (buf, 2))
return 0;
if (buf->is_bigendian)
return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
else
return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
}
/* Read a uint32 from BUF and advance 4 bytes. */
static uint32_t
read_uint32 (struct dwarf_buf *buf)
{
const unsigned char *p = buf->buf;
if (!advance (buf, 4))
return 0;
if (buf->is_bigendian)
return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
| ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
else
return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
| ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
}
/* Read a uint64 from BUF and advance 8 bytes. */
static uint64_t
read_uint64 (struct dwarf_buf *buf)
{
const unsigned char *p = buf->buf;
if (!advance (buf, 8))
return 0;
if (buf->is_bigendian)
return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
| ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
| ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
| ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
else
return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
| ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
| ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
| ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
}
/* Read an offset from BUF and advance the appropriate number of
bytes. */
static uint64_t
read_offset (struct dwarf_buf *buf, int is_dwarf64)
{
if (is_dwarf64)
return read_uint64 (buf);
else
return read_uint32 (buf);
}
/* Read an address from BUF and advance the appropriate number of
bytes. */
static uint64_t
read_address (struct dwarf_buf *buf, int addrsize)
{
switch (addrsize)
{
case 1:
return read_byte (buf);
case 2:
return read_uint16 (buf);
case 4:
return read_uint32 (buf);
case 8:
return read_uint64 (buf);
default:
dwarf_buf_error (buf, "unrecognized address size");
return 0;
}
}
/* Return whether a value is the highest possible address, given the
address size. */
static int
is_highest_address (uint64_t address, int addrsize)
{
switch (addrsize)
{
case 1:
return address == (unsigned char) -1;
case 2:
return address == (uint16_t) -1;
case 4:
return address == (uint32_t) -1;
case 8:
return address == (uint64_t) -1;
default:
return 0;
}
}
/* Read an unsigned LEB128 number. */
static uint64_t
read_uleb128 (struct dwarf_buf *buf)
{
uint64_t ret;
unsigned int shift;
int overflow;
unsigned char b;
ret = 0;
shift = 0;
overflow = 0;
do
{
const unsigned char *p;
p = buf->buf;
if (!advance (buf, 1))
return 0;
b = *p;
if (shift < 64)
ret |= ((uint64_t) (b & 0x7f)) << shift;
else if (!overflow)
{
dwarf_buf_error (buf, "LEB128 overflows uint64_t");
overflow = 1;
}
shift += 7;
}
while ((b & 0x80) != 0);
return ret;
}
/* Read a signed LEB128 number. */
static int64_t
read_sleb128 (struct dwarf_buf *buf)
{
uint64_t val;
unsigned int shift;
int overflow;
unsigned char b;
val = 0;
shift = 0;
overflow = 0;
do
{
const unsigned char *p;
p = buf->buf;
if (!advance (buf, 1))
return 0;
b = *p;
if (shift < 64)
val |= ((uint64_t) (b & 0x7f)) << shift;
else if (!overflow)
{
dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
overflow = 1;
}
shift += 7;
}
while ((b & 0x80) != 0);
if ((b & 0x40) != 0 && shift < 64)
val |= ((uint64_t) -1) << shift;
return (int64_t) val;
}
/* Return the length of an LEB128 number. */
static size_t
leb128_len (const unsigned char *p)
{
size_t ret;
ret = 1;
while ((*p & 0x80) != 0)
{
++p;
++ret;
}
return ret;
}
/* Free an abbreviations structure. */
static void
free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
backtrace_error_callback error_callback, void *data)
{
size_t i;
for (i = 0; i < abbrevs->num_abbrevs; ++i)
backtrace_free (state, abbrevs->abbrevs[i].attrs,
abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
error_callback, data);
backtrace_free (state, abbrevs->abbrevs,
abbrevs->num_abbrevs * sizeof (struct abbrev),
error_callback, data);
abbrevs->num_abbrevs = 0;
abbrevs->abbrevs = NULL;
}
/* Read an attribute value. Returns 1 on success, 0 on failure. If
the value can be represented as a uint64_t, sets *VAL and sets
*IS_VALID to 1. We don't try to store the value of other attribute
forms, because we don't care about them. */
static int
read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
int is_dwarf64, int version, int addrsize,
const unsigned char *dwarf_str, size_t dwarf_str_size,
struct attr_val *val)
{
/* Avoid warnings about val.u.FIELD may be used uninitialized if
this function is inlined. The warnings aren't valid but can
occur because the different fields are set and used
conditionally. */
memset (val, 0, sizeof *val);
switch (form)
{
case DW_FORM_addr:
val->encoding = ATTR_VAL_ADDRESS;
val->u.uint = read_address (buf, addrsize);
return 1;
case DW_FORM_block2:
val->encoding = ATTR_VAL_BLOCK;
return advance (buf, read_uint16 (buf));
case DW_FORM_block4:
val->encoding = ATTR_VAL_BLOCK;
return advance (buf, read_uint32 (buf));
case DW_FORM_data2:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_uint16 (buf);
return 1;
case DW_FORM_data4:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_uint32 (buf);
return 1;
case DW_FORM_data8:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_uint64 (buf);
return 1;
case DW_FORM_string:
val->encoding = ATTR_VAL_STRING;
val->u.string = (const char *) buf->buf;
return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
case DW_FORM_block:
val->encoding = ATTR_VAL_BLOCK;
return advance (buf, read_uleb128 (buf));
case DW_FORM_block1:
val->encoding = ATTR_VAL_BLOCK;
return advance (buf, read_byte (buf));
case DW_FORM_data1:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_byte (buf);
return 1;
case DW_FORM_flag:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_byte (buf);
return 1;
case DW_FORM_sdata:
val->encoding = ATTR_VAL_SINT;
val->u.sint = read_sleb128 (buf);
return 1;
case DW_FORM_strp:
{
uint64_t offset;
offset = read_offset (buf, is_dwarf64);
if (offset >= dwarf_str_size)
{
dwarf_buf_error (buf, "DW_FORM_strp out of range");
return 0;
}
val->encoding = ATTR_VAL_STRING;
val->u.string = (const char *) dwarf_str + offset;
return 1;
}
case DW_FORM_udata:
val->encoding = ATTR_VAL_UINT;
val->u.uint = read_uleb128 (buf);
return 1;
case DW_FORM_ref_addr:
val->encoding = ATTR_VAL_REF_INFO;
if (version == 2)
val->u.uint = read_address (buf, addrsize);
else
val->u.uint = read_offset (buf, is_dwarf64);
return 1;
case DW_FORM_ref1:
val->encoding = ATTR_VAL_REF_UNIT;
val->u.uint = read_byte (buf);
return 1;
case DW_FORM_ref2:
val->encoding = ATTR_VAL_REF_UNIT;
val->u.uint = read_uint16 (buf);
return 1;
case DW_FORM_ref4:
val->encoding = ATTR_VAL_REF_UNIT;
val->u.uint = read_uint32 (buf);
return 1;
case DW_FORM_ref8:
val->encoding = ATTR_VAL_REF_UNIT;
val->u.uint = read_uint64 (buf);
return 1;
case DW_FORM_ref_udata:
val->encoding = ATTR_VAL_REF_UNIT;
val->u.uint = read_uleb128 (buf);
return 1;
case DW_FORM_indirect:
{
uint64_t form;
form = read_uleb128 (buf);
return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
version, addrsize, dwarf_str, dwarf_str_size,
val);
}
case DW_FORM_sec_offset:
val->encoding = ATTR_VAL_REF_SECTION;
val->u.uint = read_offset (buf, is_dwarf64);
return 1;
case DW_FORM_exprloc:
val->encoding = ATTR_VAL_EXPR;
return advance (buf, read_uleb128 (buf));
case DW_FORM_flag_present:
val->encoding = ATTR_VAL_UINT;
val->u.uint = 1;
return 1;
case DW_FORM_ref_sig8:
val->encoding = ATTR_VAL_REF_TYPE;
val->u.uint = read_uint64 (buf);
return 1;
case DW_FORM_GNU_addr_index:
val->encoding = ATTR_VAL_REF_SECTION;
val->u.uint = read_uleb128 (buf);
return 1;
case DW_FORM_GNU_str_index:
val->encoding = ATTR_VAL_REF_SECTION;
val->u.uint = read_uleb128 (buf);
return 1;
case DW_FORM_GNU_ref_alt:
val->encoding = ATTR_VAL_REF_SECTION;
val->u.uint = read_offset (buf, is_dwarf64);
return 1;
case DW_FORM_GNU_strp_alt:
val->encoding = ATTR_VAL_REF_SECTION;
val->u.uint = read_offset (buf, is_dwarf64);
return 1;
default:
dwarf_buf_error (buf, "unrecognized DWARF form");
return 0;
}
}
/* Compare function_addrs for qsort. When ranges are nested, make the
smallest one sort last. */
static int
function_addrs_compare (const void *v1, const void *v2)
{
const struct function_addrs *a1 = (const struct function_addrs *) v1;
const struct function_addrs *a2 = (const struct function_addrs *) v2;
if (a1->low < a2->low)
return -1;
if (a1->low > a2->low)
return 1;
if (a1->high < a2->high)
return 1;
if (a1->high > a2->high)
return -1;
return strcmp (a1->function->name, a2->function->name);
}
/* Compare a PC against a function_addrs for bsearch. Note that if
there are multiple ranges containing PC, which one will be returned
is unpredictable. We compensate for that in dwarf_fileline. */
static int
function_addrs_search (const void *vkey, const void *ventry)
{
const uintptr_t *key = (const uintptr_t *) vkey;
const struct function_addrs *entry = (const struct function_addrs *) ventry;
uintptr_t pc;
pc = *key;
if (pc < entry->low)
return -1;
else if (pc >= entry->high)
return 1;
else
return 0;
}
/* Add a new compilation unit address range to a vector. Returns 1 on
success, 0 on failure. */
static int
add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
struct unit_addrs addrs,
backtrace_error_callback error_callback, void *data,
struct unit_addrs_vector *vec)
{
struct unit_addrs *p;
/* Add in the base address of the module here, so that we can look
up the PC directly. */
addrs.low += base_address;
addrs.high += base_address;
/* Try to merge with the last entry. */
if (vec->count > 0)
{
p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
if ((addrs.low == p->high || addrs.low == p->high + 1)
&& addrs.u == p->u)
{
if (addrs.high > p->high)
p->high = addrs.high;
return 1;
}
}
p = ((struct unit_addrs *)
backtrace_vector_grow (state, sizeof (struct unit_addrs),
error_callback, data, &vec->vec));
if (p == NULL)
return 0;
*p = addrs;
++vec->count;
return 1;
}
/* Free a unit address vector. */
static void
free_unit_addrs_vector (struct backtrace_state *state,
struct unit_addrs_vector *vec,
backtrace_error_callback error_callback, void *data)
{
struct unit_addrs *addrs;
size_t i;
addrs = (struct unit_addrs *) vec->vec.base;
for (i = 0; i < vec->count; ++i)
free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
}
/* Compare unit_addrs for qsort. When ranges are nested, make the
smallest one sort last. */
static int
unit_addrs_compare (const void *v1, const void *v2)
{
const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
if (a1->low < a2->low)
return -1;
if (a1->low > a2->low)
return 1;