forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwarf.cpp
More file actions
2036 lines (1831 loc) · 74.8 KB
/
Copy pathdwarf.cpp
File metadata and controls
2036 lines (1831 loc) · 74.8 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/Dwarf.cpp
// and modified by Doris
#if defined(__ELF__) && !defined(__FreeBSD__)
/*
* Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** This file was edited for ClickHouse.
*/
#include "common/dwarf.h"
#include <cstring>
#include "common/elf.h"
#include "common/logging.h"
#define DW_CHILDREN_no 0
#define DW_FORM_addr 1
#define DW_FORM_block1 0x0a
#define DW_FORM_block2 3
#define DW_FORM_block4 4
#define DW_FORM_block 9
#define DW_FORM_exprloc 0x18
#define DW_FORM_data1 0x0b
#define DW_FORM_ref1 0x11
#define DW_FORM_data2 0x05
#define DW_FORM_ref2 0x12
#define DW_FORM_data4 0x06
#define DW_FORM_ref4 0x13
#define DW_FORM_data8 0x07
#define DW_FORM_ref8 0x14
#define DW_FORM_ref_sig8 0x20
#define DW_FORM_sdata 0x0d
#define DW_FORM_udata 0x0f
#define DW_FORM_ref_udata 0x15
#define DW_FORM_flag 0x0c
#define DW_FORM_flag_present 0x19
#define DW_FORM_sec_offset 0x17
#define DW_FORM_ref_addr 0x10
#define DW_FORM_string 0x08
#define DW_FORM_strp 0x0e
#define DW_FORM_indirect 0x16
#define DW_FORM_strx 0x1a
#define DW_FORM_addrx 0x1b
#define DW_FORM_ref_sup4 0x1c
#define DW_FORM_strp_sup 0x1d
#define DW_FORM_data16 0x1e
#define DW_FORM_line_strp 0x1f
#define DW_FORM_implicit_const 0x21
#define DW_FORM_rnglistx 0x23
#define DW_FORM_loclistx 0x22
#define DW_FORM_ref_sup8 0x24
#define DW_FORM_strx1 0x25
#define DW_FORM_strx2 0x26
#define DW_FORM_strx3 0x27
#define DW_FORM_strx4 0x28
#define DW_FORM_addrx1 0x29
#define DW_FORM_addrx2 0x2a
#define DW_FORM_addrx3 0x2b
#define DW_FORM_addrx4 0x2c
#define DW_TAG_compile_unit 0x11
#define DW_TAG_subprogram 0x2e
#define DW_TAG_try_block 0x32
#define DW_TAG_catch_block 0x25
#define DW_TAG_entry_point 0x03
#define DW_TAG_common_block 0x1a
#define DW_TAG_lexical_block 0x0b
#define DW_AT_stmt_list 0x10
#define DW_AT_comp_dir 0x1b
#define DW_AT_name 0x03
#define DW_AT_high_pc 0x12
#define DW_AT_low_pc 0x11
#define DW_AT_entry_pc 0x52
#define DW_AT_ranges 0x55
#define DW_AT_abstract_origin 0x31
#define DW_AT_call_line 0x59
#define DW_AT_call_file 0x58
#define DW_AT_linkage_name 0x6e
#define DW_AT_specification 0x47
#define DW_AT_str_offsets_base 0x72
#define DW_AT_addr_base 0x73
#define DW_AT_rnglists_base 0x74
#define DW_AT_loclists_base 0x8c
#define DW_AT_GNU_ranges_base 0x2132
#define DW_AT_GNU_addr_base 0x2133
#define DW_LNE_define_file 0x03
#define DW_LNS_copy 0x01
#define DW_LNS_advance_pc 0x02
#define DW_LNS_advance_line 0x03
#define DW_LNS_set_file 0x04
#define DW_LNS_set_column 0x05
#define DW_LNS_negate_stmt 0x06
#define DW_LNS_set_basic_block 0x07
#define DW_LNS_const_add_pc 0x08
#define DW_LNS_fixed_advance_pc 0x09
#define DW_LNS_set_prologue_end 0x0a
#define DW_LNS_set_epilogue_begin 0x0b
#define DW_LNS_set_isa 0x0c
#define DW_LNE_end_sequence 0x01
#define DW_LNE_set_address 0x02
#define DW_LNE_set_discriminator 0x04
#define DW_LNCT_path 0x1
#define DW_LNCT_directory_index 0x2
#define DW_LNCT_timestamp 0x3
#define DW_LNCT_size 0x4
#define DW_LNCT_MD5 0x5
#define DW_RLE_end_of_list 0x0
#define DW_RLE_base_addressx 0x1
#define DW_RLE_startx_endx 0x2
#define DW_RLE_startx_length 0x3
#define DW_RLE_offset_pair 0x4
#define DW_RLE_base_address 0x5
#define DW_RLE_start_end 0x6
#define DW_RLE_start_length 0x7
namespace doris {
#include "common/compile_check_avoid_begin.h"
Dwarf::Dwarf(const std::shared_ptr<Elf>& elf)
: elf_(elf),
abbrev_(getSection(".debug_abbrev")),
addr_(getSection(".debug_addr")),
aranges_(getSection(".debug_aranges")),
info_(getSection(".debug_info")),
line_(getSection(".debug_line")),
line_str_(getSection(".debug_line_str")),
loclists_(getSection(".debug_loclists")),
ranges_(getSection(".debug_ranges")),
rnglists_(getSection(".debug_rnglists")),
str_(getSection(".debug_str")),
str_offsets_(getSection(".debug_str_offsets")) {
// Optional sections:
// - debugAranges_: for fast address range lookup.
// If missing .debug_info can be used - but it's much slower (linear
// scan).
// - debugRanges_ (DWARF 4) / debugRnglists_ (DWARF 5): non-contiguous
// address ranges of debugging information entries.
// Used for inline function address lookup.
if (info_.empty() || abbrev_.empty() || line_.empty() || str_.empty()) {
elf_ = nullptr;
}
}
Dwarf::Section::Section(std::string_view d) : is64_bit(false), data(d) {}
#define SAFE_CHECK(cond, ...) \
do { \
if (!(cond)) LOG(FATAL) << fmt::format(__VA_ARGS__); \
} while (false)
namespace {
// Maximum number of DIEAbbreviation to cache in a compilation unit. Used to
// speed up inline function lookup.
const uint32_t kMaxAbbreviationEntries = 1000;
// All following read* functions read from a std::string_view, advancing the
// std::string_view, and aborting if there's not enough room.
// Read (bitwise) one object of type T
template <typename T>
requires std::is_trivial_v<T> && std::is_standard_layout_v<T>
T read(std::string_view& sp) {
SAFE_CHECK(sp.size() >= sizeof(T), "underflow: expected bytes {}, got bytes {}", sizeof(T),
sp.size());
T x;
memcpy(&x, sp.data(), sizeof(T));
sp.remove_prefix(sizeof(T));
return x;
}
// Read (bitwise) an unsigned number of N bytes (N in 1, 2, 3, 4).
template <size_t N>
uint64_t readU64(std::string_view& sp) {
SAFE_CHECK(sp.size() >= N, "underflow");
uint64_t x = 0;
memcpy(&x, sp.data(), N);
sp.remove_prefix(N);
return x;
}
// Read ULEB (unsigned) varint value; algorithm from the DWARF spec
uint64_t readULEB(std::string_view& sp, uint8_t& shift, uint8_t& val) {
uint64_t r = 0;
shift = 0;
do {
val = read<uint8_t>(sp);
r |= (uint64_t(val & 0x7f) << shift);
shift += 7;
} while (val & 0x80);
return r;
}
uint64_t readULEB(std::string_view& sp) {
uint8_t shift;
uint8_t val;
return readULEB(sp, shift, val);
}
// Read SLEB (signed) varint value; algorithm from the DWARF spec
int64_t readSLEB(std::string_view& sp) {
uint8_t shift;
uint8_t val;
uint64_t r = readULEB(sp, shift, val);
if (shift < 64 && (val & 0x40)) {
r |= -(1ULL << shift); // sign extend
}
return r;
}
// Read a value of "section offset" type, which may be 4 or 8 bytes
uint64_t readOffset(std::string_view& sp, bool is64_bit) {
return is64_bit ? read<uint64_t>(sp) : read<uint32_t>(sp);
}
// Read "len" bytes
std::string_view readBytes(std::string_view& sp, uint64_t len) {
SAFE_CHECK(len <= sp.size(), "invalid string length: {} vs. {}", len, sp.size());
std::string_view ret(sp.data(), len);
sp.remove_prefix(len);
return ret;
}
// Read a null-terminated string
std::string_view readNullTerminated(std::string_view& sp) {
const char* p = static_cast<const char*>(memchr(sp.data(), 0, sp.size()));
SAFE_CHECK(p, "invalid null-terminated string");
std::string_view ret(sp.data(), p - sp.data());
sp = std::string_view(p + 1, sp.size());
return ret;
}
// Get a string from the section
std::string_view getStringFromStringSection(std::string_view section, uint64_t offset) {
SAFE_CHECK(offset < section.size(), "invalid section offset");
std::string_view sp(section);
sp.remove_prefix(offset);
return readNullTerminated(sp);
}
// Skip over padding until sp.data() - start is a multiple of alignment
void skipPadding(std::string_view& sp, const char* start, size_t alignment) {
size_t remainder = (sp.data() - start) % alignment;
if (remainder) {
SAFE_CHECK(alignment - remainder <= sp.size(), "invalid padding");
sp.remove_prefix(alignment - remainder);
}
}
} // namespace
Dwarf::Path::Path(std::string_view baseDir, std::string_view subDir, std::string_view file)
: baseDir_(baseDir), subDir_(subDir), file_(file) {
// Normalize
if (file_.empty()) {
baseDir_ = {};
subDir_ = {};
return;
}
if (file_[0] == '/') {
// file_ is absolute
baseDir_ = {};
subDir_ = {};
}
if (!subDir_.empty() && subDir_[0] == '/') {
baseDir_ = {}; // subDir_ is absolute
}
// Make sure it's never the case that baseDir_ is empty, but subDir_ isn't.
if (baseDir_.empty()) {
swap(baseDir_, subDir_);
}
}
size_t Dwarf::Path::size() const {
size_t size = 0;
bool needs_slash = false;
if (!baseDir_.empty()) {
size += baseDir_.size();
needs_slash = baseDir_.back() != '/';
}
if (!subDir_.empty()) {
size += needs_slash;
size += subDir_.size();
needs_slash = subDir_.back() != '/';
}
if (!file_.empty()) {
size += needs_slash;
size += file_.size();
}
return size;
}
size_t Dwarf::Path::toBuffer(char* buf, size_t bufSize) const {
size_t total_size = 0;
bool needs_slash = false;
auto append = [&](std::string_view sp) {
if (bufSize >= 2) {
size_t to_copy = std::min(sp.size(), bufSize - 1);
memcpy(buf, sp.data(), to_copy);
buf += to_copy;
bufSize -= to_copy;
}
total_size += sp.size();
};
if (!baseDir_.empty()) {
append(baseDir_);
needs_slash = baseDir_.back() != '/';
}
if (!subDir_.empty()) {
if (needs_slash) {
append("/");
}
append(subDir_);
needs_slash = subDir_.back() != '/';
}
if (!file_.empty()) {
if (needs_slash) {
append("/");
}
append(file_);
}
if (bufSize) {
*buf = '\0';
}
SAFE_CHECK(total_size == size(), "Size mismatch");
return total_size;
}
void Dwarf::Path::toString(std::string& dest) const {
size_t initial_size = dest.size();
dest.reserve(initial_size + size());
if (!baseDir_.empty()) {
dest.append(baseDir_.begin(), baseDir_.end());
}
if (!subDir_.empty()) {
if (!dest.empty() && dest.back() != '/') {
dest.push_back('/');
}
dest.append(subDir_.begin(), subDir_.end());
}
if (!file_.empty()) {
if (!dest.empty() && dest.back() != '/') {
dest.push_back('/');
}
dest.append(file_.begin(), file_.end());
}
SAFE_CHECK(dest.size() == initial_size + size(), "Size mismatch");
}
// Next chunk in section
bool Dwarf::Section::next(std::string_view& chunk) {
chunk = data;
if (chunk.empty()) {
return false;
}
// Initial length is a uint32_t value for a 32-bit section, and
// a 96-bit value (0xffffffff followed by the 64-bit length) for a 64-bit
// section.
auto initial_length = read<uint32_t>(chunk);
is64_bit = (initial_length == uint32_t(-1));
auto length = is64_bit ? read<uint64_t>(chunk) : initial_length;
SAFE_CHECK(length <= chunk.size(), "invalid DWARF section");
chunk = std::string_view(chunk.data(), length);
data = std::string_view(chunk.end(), data.end() - chunk.end());
return true;
}
std::string_view Dwarf::getSection(const char* name) const {
std::optional<Elf::Section> elf_section = elf_->findSectionByName(name);
if (!elf_section) {
return {};
}
#ifdef SHF_COMPRESSED
if (elf_section->header.sh_flags & SHF_COMPRESSED) {
return {};
}
#endif
return {elf_section->begin(), elf_section->size()};
}
// static
bool Dwarf::readAbbreviation(std::string_view& section, DIEAbbreviation& abbr) {
// abbreviation code
abbr.code = readULEB(section);
if (abbr.code == 0) {
return false;
}
// abbreviation tag
abbr.tag = readULEB(section);
// does this entry have children?
abbr.has_children = (read<uint8_t>(section) != DW_CHILDREN_no);
// attributes
const char* attribute_begin = section.data();
for (;;) {
SAFE_CHECK(!section.empty(), "invalid attribute section");
auto attr = readAttributeSpec(section);
if (attr.name == 0 && attr.form == 0) {
break;
}
}
abbr.attributes = std::string_view(attribute_begin, section.data() - attribute_begin);
return true;
}
// static
void Dwarf::readCompilationUnitAbbrs(std::string_view abbrev, CompilationUnit& cu) {
abbrev.remove_prefix(cu.abbrev_offset);
DIEAbbreviation abbr;
while (readAbbreviation(abbrev, abbr)) {
// Abbreviation code 0 is reserved for null debugging information entries.
if (abbr.code != 0 && abbr.code <= kMaxAbbreviationEntries) {
cu.abbr_cache[abbr.code - 1] = abbr;
}
}
}
size_t Dwarf::forEachChild(const CompilationUnit& cu, const Die& die,
std::function<bool(const Die& die)> f) const {
size_t next_die_offset = forEachAttribute(cu, die, [&](const Attribute&) { return true; });
if (!die.abbr.has_children) {
return next_die_offset;
}
auto child_die = getDieAtOffset(cu, next_die_offset);
while (child_die.code != 0) {
if (!f(child_die)) {
return child_die.offset;
}
// NOTE: Don't run `f` over grandchildren, just skip over them.
size_t sibling_offset = forEachChild(cu, child_die, [](const Die&) { return true; });
child_die = getDieAtOffset(cu, sibling_offset);
}
// childDie is now a dummy die whose offset is to the code 0 marking the
// end of the children. Need to add one to get the offset of the next die.
return child_die.offset + 1;
}
/*
* Iterate over all attributes of the given DIE, calling the given callable
* for each. Iteration is stopped early if any of the calls return false.
*/
size_t Dwarf::forEachAttribute(const CompilationUnit& cu, const Die& die,
std::function<bool(const Attribute& die)> f) const {
auto attrs = die.abbr.attributes;
auto values = std::string_view {info_.data() + die.offset + die.attr_offset,
cu.offset + cu.size - die.offset - die.attr_offset};
while (auto spec = readAttributeSpec(attrs)) {
auto attr = readAttribute(cu, die, spec, values);
if (!f(attr)) {
return static_cast<size_t>(-1);
}
}
return values.data() - info_.data();
}
Dwarf::Attribute Dwarf::readAttribute(const CompilationUnit& cu, const Die& die, AttributeSpec spec,
std::string_view& info) const {
// DWARF 5 introduces new FORMs whose values are relative to some base attrs:
// DW_AT_str_offsets_base, DW_AT_rnglists_base, DW_AT_addr_base.
// Debug Fission DWARF 4 uses GNU DW_AT_GNU_ranges_base & DW_AT_GNU_addr_base.
//
// The order in which attributes appear in a CU is not defined.
// The DW_AT_*_base attrs may appear after attributes that need them.
// The DW_AT_*_base attrs are CU specific; so we read them just after
// reading the CU header. During this first pass return empty values
// when encountering a FORM that depends on DW_AT_*_base.
auto get_string_using_offset_table = [&](uint64_t index) {
if (!cu.str_offsets_base.has_value()) {
return std::string_view();
}
// DWARF 5: 7.26 String Offsets Table
// The DW_AT_str_offsets_base attribute points to the first entry following
// the header. The entries are indexed sequentially from this base entry,
// starting from 0.
auto sp = str_offsets_.substr(*cu.str_offsets_base +
index * (cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t)));
uint64_t str_offset = readOffset(sp, cu.is64Bit);
return getStringFromStringSection(str_, str_offset);
};
auto read_debug_addr = [&](uint64_t index) {
if (!cu.addr_base.has_value()) {
return uint64_t(0);
}
// DWARF 5: 7.27 Address Table
// The DW_AT_addr_base attribute points to the first entry following the
// header. The entries are indexed sequentially from this base entry,
// starting from 0.
auto sp = addr_.substr(*cu.addr_base + index * sizeof(uint64_t));
return read<uint64_t>(sp);
};
switch (spec.form) {
case DW_FORM_addr:
return {spec, die, read<uintptr_t>(info)};
case DW_FORM_block1:
return {spec, die, readBytes(info, read<uint8_t>(info))};
case DW_FORM_block2:
return {spec, die, readBytes(info, read<uint16_t>(info))};
case DW_FORM_block4:
return {spec, die, readBytes(info, read<uint32_t>(info))};
case DW_FORM_block:
[[fallthrough]];
case DW_FORM_exprloc:
return {spec, die, readBytes(info, readULEB(info))};
case DW_FORM_data1:
[[fallthrough]];
case DW_FORM_ref1:
return {spec, die, read<uint8_t>(info)};
case DW_FORM_data2:
[[fallthrough]];
case DW_FORM_ref2:
return {spec, die, read<uint16_t>(info)};
case DW_FORM_data4:
[[fallthrough]];
case DW_FORM_ref4:
return {spec, die, read<uint32_t>(info)};
case DW_FORM_data8:
[[fallthrough]];
case DW_FORM_ref8:
[[fallthrough]];
case DW_FORM_ref_sig8:
return {spec, die, read<uint64_t>(info)};
case DW_FORM_sdata:
return {spec, die, static_cast<uint64_t>(readSLEB(info))};
case DW_FORM_udata:
[[fallthrough]];
case DW_FORM_ref_udata:
return {spec, die, readULEB(info)};
case DW_FORM_flag:
return {spec, die, read<uint8_t>(info)};
case DW_FORM_flag_present:
return {spec, die, 1ULL};
case DW_FORM_sec_offset:
[[fallthrough]];
case DW_FORM_ref_addr:
return {spec, die, readOffset(info, die.is64Bit)};
case DW_FORM_string:
return {spec, die, readNullTerminated(info)};
case DW_FORM_strp:
return {spec, die, getStringFromStringSection(str_, readOffset(info, die.is64Bit))};
case DW_FORM_indirect: // form is explicitly specified
// Update spec with the actual FORM.
spec.form = readULEB(info);
return readAttribute(cu, die, spec, info);
// DWARF 5:
case DW_FORM_implicit_const: // form is explicitly specified
// For attributes with this form, the attribute specification contains a
// third part, which is a signed LEB128 number. The value of this number
// is used as the value of the attribute, and no value is stored in the
// .debug_info section.
return {spec, die, static_cast<uint64_t>(spec.implicitConst)};
case DW_FORM_addrx:
return {spec, die, read_debug_addr(readULEB(info))};
case DW_FORM_addrx1:
return {spec, die, read_debug_addr(readU64<1>(info))};
case DW_FORM_addrx2:
return {spec, die, read_debug_addr(readU64<2>(info))};
case DW_FORM_addrx3:
return {spec, die, read_debug_addr(readU64<3>(info))};
case DW_FORM_addrx4:
return {spec, die, read_debug_addr(readU64<4>(info))};
case DW_FORM_line_strp:
return {spec, die, getStringFromStringSection(line_str_, readOffset(info, die.is64Bit))};
case DW_FORM_strx:
return {spec, die, get_string_using_offset_table(readULEB(info))};
case DW_FORM_strx1:
return {spec, die, get_string_using_offset_table(readU64<1>(info))};
case DW_FORM_strx2:
return {spec, die, get_string_using_offset_table(readU64<2>(info))};
case DW_FORM_strx3:
return {spec, die, get_string_using_offset_table(readU64<3>(info))};
case DW_FORM_strx4:
return {spec, die, get_string_using_offset_table(readU64<4>(info))};
case DW_FORM_rnglistx: {
auto index = readULEB(info);
if (!cu.rnglists_base.has_value()) {
return {spec, die, 0ULL};
}
const uint64_t offset_size = cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t);
auto sp = rnglists_.substr(*cu.rnglists_base + index * offset_size);
auto offset = readOffset(sp, cu.is64Bit);
return {spec, die, *cu.rnglists_base + offset};
}
case DW_FORM_loclistx: {
auto index = readULEB(info);
if (!cu.loclists_base.has_value()) {
return {spec, die, 0ULL};
}
const uint64_t offset_size = cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t);
auto sp = loclists_.substr(*cu.loclists_base + index * offset_size);
auto offset = readOffset(sp, cu.is64Bit);
return {spec, die, *cu.loclists_base + offset};
}
case DW_FORM_data16:
return {spec, die, readBytes(info, 16)};
case DW_FORM_ref_sup4:
case DW_FORM_ref_sup8:
case DW_FORM_strp_sup:
SAFE_CHECK(false, "Unexpected DWARF5 supplimentary object files");
default:
SAFE_CHECK(false, "invalid attribute form");
}
return {spec, die, 0ULL};
}
// static
Dwarf::AttributeSpec Dwarf::readAttributeSpec(std::string_view& sp) {
Dwarf::AttributeSpec spec;
spec.name = readULEB(sp);
spec.form = readULEB(sp);
if (spec.form == DW_FORM_implicit_const) {
spec.implicitConst = readSLEB(sp);
}
return spec;
}
Dwarf::CompilationUnit Dwarf::getCompilationUnit(uint64_t offset) const {
// SAFE_CHECK(offset < info_.size(), "unexpected offset");
CompilationUnit cu;
std::string_view chunk(info_);
cu.offset = offset;
chunk.remove_prefix(offset);
// 1) unit_length
auto initial_length = read<uint32_t>(chunk);
cu.is64Bit = (initial_length == uint32_t(-1));
cu.size = cu.is64Bit ? read<uint64_t>(chunk) : initial_length;
SAFE_CHECK(cu.size <= chunk.size(), "invalid chunk size");
cu.size += cu.is64Bit ? 12 : 4;
// 2) version
cu.version = read<uint16_t>(chunk);
SAFE_CHECK(cu.version >= 2 && cu.version <= 5, "invalid info version");
if (cu.version == 5) {
// DWARF5: 7.5.1.1 Full and Partial Compilation Unit Headers
// 3) unit_type (new DWARF 5)
cu.unit_type = read<uint8_t>(chunk);
if (cu.unit_type != DW_UT_compile && cu.unit_type != DW_UT_skeleton) {
return cu;
}
// 4) address_size
cu.addr_size = read<uint8_t>(chunk);
SAFE_CHECK(cu.addr_size == sizeof(uintptr_t), "invalid address size");
// 5) debug_abbrev_offset
cu.abbrev_offset = readOffset(chunk, cu.is64Bit);
if (cu.unit_type == DW_UT_skeleton) {
// 6) dwo_id
read<uint64_t>(chunk);
}
} else {
// DWARF4 has a single type of unit in .debug_info
cu.unit_type = DW_UT_compile;
// 3) debug_abbrev_offset
cu.abbrev_offset = readOffset(chunk, cu.is64Bit);
// 4) address_size
cu.addr_size = read<uint8_t>(chunk);
SAFE_CHECK(cu.addr_size == sizeof(uintptr_t), "invalid address size");
}
cu.first_die = chunk.data() - info_.data();
if (cu.version < 5) {
return cu;
}
Die die = getDieAtOffset(cu, cu.first_die);
if (die.abbr.tag != DW_TAG_compile_unit) {
return cu;
}
// Read the DW_AT_*_base attributes.
// Attributes which use FORMs relative to these base attrs
// will not have valid values during this first pass!
forEachAttribute(cu, die, [&](const Attribute& attr) {
switch (attr.spec.name) {
case DW_AT_addr_base:
case DW_AT_GNU_addr_base:
cu.addr_base = std::get<uint64_t>(attr.attr_value);
break;
case DW_AT_loclists_base:
cu.loclists_base = std::get<uint64_t>(attr.attr_value);
break;
case DW_AT_rnglists_base:
case DW_AT_GNU_ranges_base:
cu.rnglists_base = std::get<uint64_t>(attr.attr_value);
break;
case DW_AT_str_offsets_base:
cu.str_offsets_base = std::get<uint64_t>(attr.attr_value);
break;
}
return true; // continue forEachAttribute
});
return cu;
}
// Finds the Compilation Unit starting at offset.
Dwarf::CompilationUnit Dwarf::findCompilationUnit(uint64_t targetOffset) const {
// SAFE_CHECK(targetOffset < info_.size(), "unexpected target address");
uint64_t offset = 0;
while (offset < info_.size()) {
std::string_view chunk(info_);
chunk.remove_prefix(offset);
auto initial_length = read<uint32_t>(chunk);
auto is64_bit = (initial_length == static_cast<uint32_t>(-1));
auto size = is64_bit ? read<uint64_t>(chunk) : initial_length;
SAFE_CHECK(size <= chunk.size(), "invalid chunk size");
size += is64_bit ? 12 : 4;
if (offset + size > targetOffset) {
break;
}
offset += size;
}
return getCompilationUnit(offset);
}
Dwarf::DIEAbbreviation Dwarf::getAbbreviation(uint64_t code, uint64_t offset) const {
// Linear search in the .debug_abbrev section, starting at offset
std::string_view section = abbrev_;
section.remove_prefix(offset);
Dwarf::DIEAbbreviation abbr;
while (readAbbreviation(section, abbr)) {
if (abbr.code == code) {
return abbr;
}
}
SAFE_CHECK(false, "could not find abbreviation code");
}
Dwarf::AttributeValue Dwarf::readAttributeValue(std::string_view& sp, uint64_t form,
bool is64_bit) const {
switch (form) {
case DW_FORM_addr:
return uint64_t(read<uintptr_t>(sp));
case DW_FORM_block1:
return readBytes(sp, read<uint8_t>(sp));
case DW_FORM_block2:
return readBytes(sp, read<uint16_t>(sp));
case DW_FORM_block4:
return readBytes(sp, read<uint32_t>(sp));
case DW_FORM_block:
[[fallthrough]];
case DW_FORM_exprloc:
return readBytes(sp, readULEB(sp));
case DW_FORM_data1:
[[fallthrough]];
case DW_FORM_ref1:
return uint64_t(read<uint8_t>(sp));
case DW_FORM_data2:
[[fallthrough]];
case DW_FORM_ref2:
return uint64_t(read<uint16_t>(sp));
case DW_FORM_data4:
[[fallthrough]];
case DW_FORM_ref4:
return uint64_t(read<uint32_t>(sp));
case DW_FORM_data8:
[[fallthrough]];
case DW_FORM_ref8:
return read<uint64_t>(sp);
case DW_FORM_sdata:
return uint64_t(readSLEB(sp));
case DW_FORM_udata:
[[fallthrough]];
case DW_FORM_ref_udata:
return readULEB(sp);
case DW_FORM_flag:
return uint64_t(read<uint8_t>(sp));
case DW_FORM_flag_present:
return uint64_t(1);
case DW_FORM_sec_offset:
[[fallthrough]];
case DW_FORM_ref_addr:
return readOffset(sp, is64_bit);
case DW_FORM_string:
return readNullTerminated(sp);
case DW_FORM_strp:
return getStringFromStringSection(str_, readOffset(sp, is64_bit));
case DW_FORM_indirect: // form is explicitly specified
return readAttributeValue(sp, readULEB(sp), is64_bit);
default:
SAFE_CHECK(false, "invalid attribute form");
return uint64_t(1);
;
}
}
/**
* Find @address in .debug_aranges and return the offset in
* .debug_info for compilation unit to which this address belongs.
*/
bool Dwarf::findDebugInfoOffset(uintptr_t address, std::string_view aranges, uint64_t& offset) {
Section aranges_section(aranges);
std::string_view chunk;
while (aranges_section.next(chunk)) {
auto version = read<uint16_t>(chunk);
SAFE_CHECK(version == 2, "invalid aranges version");
offset = readOffset(chunk, aranges_section.is64Bit());
auto address_size = read<uint8_t>(chunk);
SAFE_CHECK(address_size == sizeof(uintptr_t), "invalid address size");
auto segment_size = read<uint8_t>(chunk);
SAFE_CHECK(segment_size == 0, "segmented architecture not supported");
// Padded to a multiple of 2 addresses.
// Strangely enough, this is the only place in the DWARF spec that requires
// padding.
skipPadding(chunk, aranges.data(), 2 * sizeof(uintptr_t));
for (;;) {
auto start = read<uintptr_t>(chunk);
auto length = read<uintptr_t>(chunk);
if (start == 0 && length == 0) {
break;
}
// Is our address in this range?
if (address >= start && address < start + length) {
return true;
}
}
}
return false;
}
Dwarf::Die Dwarf::getDieAtOffset(const CompilationUnit& cu, uint64_t offset) const {
SAFE_CHECK(offset < info_.size(), "unexpected offset {}, info size {}", offset, info_.size());
Die die;
std::string_view sp {info_.data() + offset, cu.offset + cu.size - offset};
die.offset = offset;
die.is64Bit = cu.is64Bit;
auto code = readULEB(sp);
die.code = code;
if (code == 0) {
return die;
}
die.attr_offset = sp.data() - info_.data() - offset;
die.abbr = !cu.abbr_cache.empty() && die.code < kMaxAbbreviationEntries
? cu.abbr_cache[die.code - 1]
: getAbbreviation(die.code, cu.abbrev_offset);
return die;
}
/**
* Find the @locationInfo for @address in the compilation unit represented
* by the @sp .debug_info entry.
* Returns whether the address was found.
* Advances @sp to the next entry in .debug_info.
*/
bool Dwarf::findLocation(uintptr_t address, const LocationInfoMode mode, CompilationUnit& cu,
LocationInfo& info, std::vector<SymbolizedFrame>& inline_frames) const {
Die die = getDieAtOffset(cu, cu.first_die);
// Partial compilation unit (DW_TAG_partial_unit) is not supported.
SAFE_CHECK(die.abbr.tag == DW_TAG_compile_unit, "expecting compile unit entry");
// Offset in .debug_line for the line number VM program for this CU
std::optional<uint64_t> line_offset = 0;
std::string_view compilation_directory;
std::optional<std::string_view> main_file_name;
std::optional<uint64_t> base_addr_cu;
forEachAttribute(cu, die, [&](const Attribute& attr) {
switch (attr.spec.name) {
case DW_AT_stmt_list:
// Offset in .debug_line for the line number VM program for this
// compilation unit
line_offset = std::get<uint64_t>(attr.attr_value);
break;
case DW_AT_comp_dir:
// Compilation directory
compilation_directory = std::get<std::string_view>(attr.attr_value);
break;
case DW_AT_name:
// File name of main file being compiled
main_file_name = std::get<std::string_view>(attr.attr_value);
break;
case DW_AT_low_pc:
case DW_AT_entry_pc:
// 2.17.1: historically DW_AT_low_pc was used. DW_AT_entry_pc was
// introduced in DWARF3. Support either to determine the base address of
// the CU.
base_addr_cu = std::get<uint64_t>(attr.attr_value);
break;
}
// Iterate through all attributes until find all above.
return true;
});
if (main_file_name) {
info.has_main_file = true;
info.main_file = Path(compilation_directory, "", *main_file_name);
}
if (!line_offset) {
return false;
}
std::string_view line_section(line_);
line_section.remove_prefix(*line_offset);
LineNumberVM line_vm(line_section, compilation_directory, str_, line_str_);
// Execute line number VM program to find file and line
info.has_file_and_line = line_vm.findAddress(address, info.file, info.line);
bool check_inline = (mode == LocationInfoMode::FULL_WITH_INLINE);
if (info.has_file_and_line && check_inline) {
// Re-get the compilation unit with abbreviation cached.
cu.abbr_cache.clear();
cu.abbr_cache.resize(kMaxAbbreviationEntries);
readCompilationUnitAbbrs(abbrev_, cu);
// Find the subprogram that matches the given address.
Die subprogram;
findSubProgramDieForAddress(cu, die, address, base_addr_cu, subprogram);
// Subprogram is the DIE of caller function.
if (/*check_inline &&*/ subprogram.abbr.has_children) {
// Use an extra location and get its call file and call line, so that
// they can be used for the second last location when we don't have
// enough inline frames for all inline functions call stack.
const size_t max_size = Dwarf::kMaxInlineLocationInfoPerFrame + 1;
std::vector<CallLocation> call_locations;
call_locations.reserve(Dwarf::kMaxInlineLocationInfoPerFrame + 1);
findInlinedSubroutineDieForAddress(cu, subprogram, line_vm, address, base_addr_cu,