forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf.cc
1766 lines (1579 loc) · 47.9 KB
/
dwarf.cc
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
// vim: expandtab:ts=4:sw=4
#include "libpstack/elf.h"
#include "libpstack/dwarf.h"
#include "libpstack/inflatereader.h"
#include <elf.h>
#include <err.h>
#include <libgen.h>
#include <unistd.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <set>
#include <sstream>
#include <stack>
#include <algorithm>
using std::make_unique;
using std::make_shared;
using std::string;
namespace {
struct Stats {
int totalDIEs;
int maxDIEs;
int currentDIEs;
Stats() : totalDIEs{}, maxDIEs{}, currentDIEs{} {}
~Stats() {
if (verbose > 2)
*debug << "DIEs: current=" << currentDIEs << ", total=" << totalDIEs
<< ", max=" << maxDIEs << std::endl;
}
void addone() {
totalDIEs++;
currentDIEs++;
if (currentDIEs > maxDIEs)
maxDIEs = currentDIEs;
}
void delone() {
currentDIEs--;
}
};
Stats stats;
}
namespace Dwarf {
class RawDIE {
RawDIE() = delete;
RawDIE(const RawDIE &) = delete;
static void readValue(DWARFReader &, const FormEntry &form, Value &value, Unit *);
const Abbreviation *type;
std::vector<Value> values;
Elf::Off parent; // 0 implies we do not yet know the parent's offset.
Elf::Off firstChild;
Elf::Off nextSibling;
public:
RawDIE(Unit *, DWARFReader &, size_t, Elf::Off parent);
~RawDIE();
friend class Attribute;
friend class DIE;
friend class DIEAttributes;
friend class Unit;
friend class DIEIter;
};
DIEIter &DIEIter::operator++() {
currentDIE = currentDIE.nextSibling(parent);
// if we loaded the child by a direct refrence into the middle of the
// unit, (and hence didn't know the parent at the time), take the
// opportunity to update its parent pointer
if (currentDIE && parent && currentDIE.raw->parent == 0)
currentDIE.raw->parent = parent.offset;
return *this;
}
DIEIter::DIEIter(const DIE &first, const DIE & parent_)
: parent(parent_)
, currentDIE(first)
{
// As above, take the opportunity to update the current DIE's parent field
// if it has not already been decided.
if (currentDIE && parent && currentDIE.raw->parent == 0)
currentDIE.raw->parent = parent.offset;
}
uintmax_t
DWARFReader::getuleb128shift(int &shift, bool &msb)
{
uintmax_t result;
unsigned char byte;
for (result = 0, shift = 0;;) {
io->readObj(off++, &byte);
result |= uintmax_t(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
msb = (byte & 0x40) != 0;
return result;
}
Pubname::Pubname(DWARFReader &r, uint32_t offset)
: offset(offset)
, name(r.getstring())
{
}
PubnameUnit::PubnameUnit(DWARFReader &r)
{
length = r.getu32();
Elf::Off next = r.getOffset() + length;
version = r.getu16();
infoOffset = r.getu32();
infoLength = r.getu32();
while (r.getOffset() < next) {
uint32_t offset;
offset = r.getu32();
if (offset == 0)
break;
pubnames.emplace_back(r, offset);
}
}
static Reader::csptr
sectionReader(Elf::Object &obj, const char *name, const char *compressedName,
const Elf::Section **secp = nullptr)
{
const auto &raw = obj.getSection(name, SHT_PROGBITS);
if (secp != nullptr)
*secp = nullptr;
if (raw) {
if (secp)
*secp = &raw;
return raw.io;
}
if (compressedName != nullptr) {
const auto &zraw = obj.getSection(compressedName, SHT_PROGBITS);
if (zraw) {
#ifdef WITH_ZLIB
unsigned char sig[12];
zraw.io->readObj(0, sig, sizeof sig);
if (memcmp((const char *)sig, "ZLIB", 4) != 0)
return Reader::csptr();
uint64_t sz = 0;
for (size_t i = 4; i < 12; ++i) {
sz <<= 8;
sz |= sig[i];
}
if (secp)
*secp = &zraw;
return make_shared<InflateReader>(sz, OffsetReader(zraw.io, sizeof sig, sz));
#else
std::clog << "warning: no zlib support to process compressed debug info in "
<< *obj.io << std::endl;
#endif
}
}
return Reader::csptr();
}
Info::Info(Elf::Object::sptr obj, ImageCache &cache_)
: io(sectionReader(*obj, ".debug_info", ".zdebug_info"))
, elf(obj)
, debugStrings(sectionReader(*obj, ".debug_str", ".zdebug_str"))
, abbrev(sectionReader(*obj, ".debug_abbrev", ".zdebug_abbrev"))
, lineshdr(sectionReader(*obj, ".debug_line", ".zdebug_line"))
, strOffsets(sectionReader(*obj, ".debug_str_offsets", ".zdebug_str_offsets"))
, altImageLoaded(false)
, imageCache(cache_)
, pubnamesh(sectionReader(*obj, ".debug_pubnames", ".zdebug_pubnames"))
, arangesh(sectionReader(*obj, ".debug_aranges", ".zdebug_aranges"))
, rangesh(sectionReader(*obj, ".debug_ranges", ".zdebug_ranges"))
, haveLines(bool(lineshdr))
, haveARanges(bool(arangesh))
{
auto f = [this, &obj](const char *name, const char *zname, FIType ftype) {
const Elf::Section *sec;
auto io = sectionReader(*obj, name, zname, &sec);
if (!io)
return std::unique_ptr<CFI>();
try {
return make_unique<CFI>(this, sec->shdr.sh_addr, io, ftype);
}
catch (const Exception &ex) {
*debug << "can't decode " << name << " for " << *obj->io << ": "
<< ex.what() << "\n";
}
return std::unique_ptr<CFI>();
};
ehFrame = f(".eh_frame", nullptr, FI_EH_FRAME);
debugFrame = f(".debug_frame", ".zdebug_frame", FI_DEBUG_FRAME);
}
const std::list<PubnameUnit> &
Info::pubnames() const
{
if (pubnamesh) {
DWARFReader r(pubnamesh);
while (!r.empty())
pubnameUnits.emplace_back(r);
pubnamesh = nullptr;
}
return pubnameUnits;
}
static size_t UNITCACHE_SIZE=1024;
Unit::sptr
UnitsCache::get(const Info *info, Elf::Off offset)
{
auto &ent = byOffset[offset];
if (ent != nullptr) {
auto idx = std::find(LRU.begin(), LRU.end(), ent);
assert(idx != LRU.end());
LRU.erase(idx);
} else {
DWARFReader r(info->io, offset);
ent = make_shared<Unit>(info, r);
if (verbose >= 3)
*debug << "create unit " << ent->name() << "@" << offset
<< " in " << *info->io << "\n";
}
LRU.push_front(ent);
if (LRU.size() > UNITCACHE_SIZE) {
auto old = LRU.back();
// There might still be active DIEs in this unit, but we can purge its
// RawDIEs to potentially free them
old->purge();
LRU.pop_back();
// don't erase from the map - we hold on to the offsets so we can quickly
// determine which unit contains a particular DIE.
byOffset[old->offset] = 0;
if (verbose > 3)
*debug << "evicted unit " << old->name() << "@" << old->offset
<< " in " << *info->io << "\n";
}
return ent;
}
DIE
Info::offsetToDIE(Elf::Off offset) const
{
// find the appropriate unit for a die with that offset.
auto it = std::lower_bound(
units.byOffset.begin(),
units.byOffset.end(),
offset,
[] (const std::pair<Elf::Off, std::shared_ptr<Unit>> &u, Elf::Off offset)
{ return u.first < offset; });
Elf::Off uOffset;
if (it == units.byOffset.begin() || it == units.byOffset.end()) {
uOffset = 0;
} else {
--it;
uOffset = it->first;
}
UnitIterator start(this, uOffset);
UnitIterator end;
for (int i = 1; start != end; ++start, ++i) {
const auto &u = *start;
DIE entry = u->offsetToDIE(offset);
if (entry) {
if (verbose > 2)
*debug << "search for DIE at " << offset
<< " started at " << uOffset
<< " and took " << i << " iterations\n";
return entry;
}
}
throw Exception() << "DIE not found";
}
Unit::sptr
Info::getUnit(Elf::Off offset) const
{
return units.get(this, offset);
}
Units
Info::getUnits() const
{
return Units(shared_from_this());
}
void
Info::decodeARangeSet(DWARFReader &r) const {
Elf::Off start = r.getOffset();
size_t dwarfLen;
uint32_t length = r.getlength(&dwarfLen);
Elf::Off next = r.getOffset() + length;
uint16_t version = r.getu16();
(void)version;
Elf::Off debugInfoOffset = r.getu32();
uint8_t addrlen = r.getu8();
if (addrlen == 0)
addrlen = 1;
r.addrLen = addrlen;
uint8_t segdesclen = r.getu8();
(void)segdesclen;
assert(segdesclen == 0);
unsigned tupleLen = addrlen * 2;
// Align on tupleLen-boundary.
Elf::Off used = r.getOffset() - start;
unsigned align = tupleLen - used % tupleLen;;
r.skip(align);
while (r.getOffset() < next) {
Elf::Addr start = r.getuint(addrlen);
Elf::Addr length = r.getuint(addrlen);
aranges.ranges[start + length] = std::make_pair(length, debugInfoOffset);
}
}
bool Info::hasARanges() const {
return haveARanges;
}
Unit::sptr
Info::lookupUnit(Elf::Addr addr) const {
if (arangesh) {
DWARFReader r(arangesh);
while (!r.empty())
decodeARangeSet(r);
arangesh = nullptr;
}
auto it = aranges.ranges.upper_bound(addr);
if (it != aranges.ranges.end() && it->first - it->second.first <= addr)
return getUnit(it->second.second);
if (!unitRangesCached) {
// Clang does not add debug_aranges. If we fail to find the unit via
// the aranges, walk through all the units, and check out their
// DW_AT_range attribute, and fold its content into the aranges data.
unitRangesCached = true;
for (auto u : getUnits()) {
auto root = u->root();
auto lowpc = root.attribute(DW_AT_low_pc);
auto highpc = root.attribute(DW_AT_high_pc);
auto ranges = root.attribute(DW_AT_ranges);
if (lowpc.valid() && highpc.valid()) {
aranges.ranges[uintmax_t(highpc)] = std::make_pair(uintmax_t(highpc) - uintmax_t(lowpc), u->offset);
}
if (ranges.valid()) {
auto rs = rangesAt(uintmax_t(ranges));
for (auto r : rs) {
aranges.ranges[r.second] = std::make_pair(r.first, u->offset);
}
}
}
}
// Try again now we've added all the unit ranges.
it = aranges.ranges.upper_bound(addr);
if (it != aranges.ranges.end() && it->first - it->second.first <= addr)
return getUnit(it->second.second);
return nullptr;
}
Info::~Info() = default;
Unit::Unit(const Info *di, DWARFReader &r)
: dwarf(di)
, io(r.io)
, offset(r.getOffset())
, length(r.getlength(&dwarfLen))
, end(r.getOffset() + length)
, version(r.getu16())
{
if (version <= 2) // DWARF Version 2 uses the architecture's address size.
dwarfLen = ELF_BYTES;
Elf::Off abbrevOffset;
if (version >= 5) {
unitType = UnitType(r.getu8());
r.addrLen = addrlen = r.getu8();
abbrevOffset = r.getuint(dwarfLen);
} else {
abbrevOffset = r.getuint(version <= 2 ? 4 : dwarfLen);
r.addrLen = addrlen = r.getu8();
}
DWARFReader abbR(di->abbrev, abbrevOffset);
uintmax_t code;
while ((code = abbR.getuleb128()) != 0)
abbreviations.emplace(std::piecewise_construct,
std::forward_as_tuple(code),
std::forward_as_tuple(abbR));
topDIEOffset = r.getOffset();
r.setOffset(end);
}
/*
* Convert an offset to a DIE.
* If the parent is not known, it can be null
* If we later need to find the parent, it may require scanning the entire
* DIE tree to do so if we don't know parent's offset when requested.
*/
std::shared_ptr<RawDIE>
Unit::offsetToRawDIE(const DIE &parent, Elf::Off offset) {
if (offset == 0 || offset < this->offset || offset >= this->end)
return nullptr;
auto &rawptr = allEntries[offset];
if (rawptr == nullptr)
rawptr = decodeEntry(parent, offset);
return rawptr;
}
DIE
Unit::offsetToDIE(const DIE &parent, Elf::Off offset) {
return DIE(shared_from_this(), offset, offsetToRawDIE(parent, offset));
}
DIE
Unit::offsetToDIE(Elf::Off offset) {
return DIE(shared_from_this(), offset, offsetToRawDIE(DIE(), offset));
}
string
Unit::name()
{
return root().name();
}
Unit::~Unit() = default;
Abbreviation::Abbreviation(DWARFReader &r)
: tag(Tag(r.getuleb128()))
, hasChildren(HasChildren(r.getu8()) == DW_CHILDREN_yes)
, nextSibIdx(-1)
{
for (size_t i = 0;; ++i) {
auto name = AttrName(r.getuleb128());
auto form = Form(r.getuleb128());
if (name == 0 && form == 0)
break;
if (name == DW_AT_sibling)
nextSibIdx = int(i);
intmax_t value = (form == DW_FORM_implicit_const) ? r.getsleb128() : 0;
forms.emplace_back(form, value);
attrName2Idx[name] = i;
}
}
AttrName
Attribute::name() const
{
size_t off = formp - &dieref.raw->type->forms[0];
for (auto ent : dieref.raw->type->attrName2Idx) {
if (ent.second == off)
return ent.first;
}
return DW_AT_none;
}
Attribute::operator intmax_t() const
{
if (!valid())
return 0;
switch (formp->form) {
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_sdata:
case DW_FORM_udata:
case DW_FORM_implicit_const:
return value().sdata;
case DW_FORM_sec_offset:
return value().addr;
default:
abort();
}
}
Attribute::operator uintmax_t() const
{
if (!valid())
return 0;
switch (formp->form) {
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_udata:
return value().udata;
case DW_FORM_addr:
case DW_FORM_sec_offset:
return value().addr;
default:
abort();
}
}
LineState::LineState(LineInfo *li)
: addr { 0 }
, file { &li->files[1] }
, line { 1 }
, column { 0 }
, isa { 0 }
, is_stmt { li->default_is_stmt }
, basic_block { false }
, end_sequence { false }
, prologue_end { false }
, epilogue_begin { false }
{}
static void
dwarfStateAddRow(LineInfo *li, const LineState &state)
{
li->matrix.push_back(state);
}
void
LineInfo::build(DWARFReader &r, const Unit *unit)
{
size_t dwarfLen;
uint32_t total_length = r.getlength(&dwarfLen);
Elf::Off end = r.getOffset() + total_length;
uint16_t version = r.getu16();
(void)version;
Elf::Off header_length = r.getuint(version > 2 ? dwarfLen: 4);
Elf::Off expectedEnd = header_length + r.getOffset();
int min_insn_length = r.getu8();
int maximum_operations_per_instruction = version >= 4 ? r.getu8() : 1; // new in DWARF 4.
(void)maximum_operations_per_instruction; // XXX: work out what to do with this.
default_is_stmt = r.getu8() != 0;
int line_base = r.gets8();
int line_range = r.getu8();
opcode_base = r.getu8();
opcode_lengths.resize(opcode_base);
for (size_t i = 1; i < opcode_base; ++i)
opcode_lengths[i] = r.getu8();
directories.emplace_back(".");
int count;
for (count = 0;; count++) {
const auto &s = r.getstring();
if (s == "")
break;
directories.push_back(s);
}
files.emplace_back("unknown", "unknown", 0U, 0U); // index 0 is special
for (count = 1;; count++) {
char c;
r.io->readObj(r.getOffset(), &c);
if (c == 0) {
r.getu8(); // skip terminator.
break;
}
files.emplace_back(r, this);
}
auto diff = expectedEnd - r.getOffset();
if (diff != 0) {
if (verbose > 0)
*debug << "warning: left " << diff
<< " bytes in line info table of " << *r.io << std::endl;
r.skip(diff);
}
LineState state(this);
while (r.getOffset() < end) {
unsigned c = r.getu8();
if (c >= opcode_base) {
/* Special opcode */
c -= opcode_base;
int addrIncr = c / line_range;
int lineIncr = c % line_range + line_base;
state.addr += addrIncr * min_insn_length;
state.line += lineIncr;
dwarfStateAddRow(this, state);
state.basic_block = false;
} else if (c == 0) {
/* Extended opcode */
int len = r.getuleb128();
auto code = LineEOpcode(r.getu8());
switch (code) {
case DW_LNE_end_sequence:
state.end_sequence = true;
dwarfStateAddRow(this, state);
state = LineState(this);
break;
case DW_LNE_set_address:
state.addr = r.getuint(unit->addrlen);
break;
case DW_LNE_set_discriminator:
r.getuleb128(); // XXX: what's this?
break;
default:
r.skip(len - 1);
abort();
break;
}
} else {
/* Standard opcode. */
auto opcode = LineSOpcode(c);
int argCount, i;
switch (opcode) {
case DW_LNS_const_add_pc:
state.addr += ((255 - opcode_base) / line_range) * min_insn_length;
break;
case DW_LNS_advance_pc:
state.addr += r.getuleb128() * min_insn_length;
break;
case DW_LNS_fixed_advance_pc:
state.addr += r.getu16() * min_insn_length;
break;
case DW_LNS_advance_line:
state.line += r.getsleb128();
break;
case DW_LNS_set_file:
state.file = &files[r.getuleb128()];
break;
case DW_LNS_copy:
dwarfStateAddRow(this, state);
state.basic_block = false;
break;
case DW_LNS_set_column:
state.column = r.getuleb128();
break;
case DW_LNS_negate_stmt:
state.is_stmt = !state.is_stmt;
break;
case DW_LNS_set_basic_block:
state.basic_block = true;
break;
case DW_LNS_set_prologue_end:
state.prologue_end = true;
break;
case DW_LNS_set_epilogue_begin:
state.epilogue_begin = true;
break;
case DW_LNS_set_isa:
state.isa = r.getuleb128();
break;
default:
abort();
argCount = opcode_lengths[opcode - 1];
for (i = 0; i < argCount; i++)
r.getuleb128();
break;
case DW_LNS_none:
break;
}
}
}
}
FileEntry::FileEntry(string name_, string dir_, unsigned lastMod_, unsigned length_)
: name(std::move(name_))
, directory(std::move(dir_))
, lastMod(lastMod_)
, length(length_)
{
}
FileEntry::FileEntry(DWARFReader &r, LineInfo *info)
: name(r.getstring())
, directory(info->directories[r.getuleb128()])
, lastMod(r.getuleb128())
, length(r.getuleb128())
{
}
Attribute::operator std::string() const
{
if (!valid())
return "";
const Info *dwarf = dieref.unit->dwarf;
assert(dwarf != nullptr);
switch (formp->form) {
case DW_FORM_GNU_strp_alt: {
const auto &alt = dwarf->getAltDwarf();
if (!alt)
return "(alt string table unavailable)";
auto &strs = alt->debugStrings;
if (!strs)
return "(alt string table unavailable)";
return strs->readString(value().addr);
}
case DW_FORM_strp:
return dwarf->debugStrings->readString(value().addr);
case DW_FORM_string:
return dieref.unit->io->readString(value().addr);
case DW_FORM_strx1:
case DW_FORM_strx2:
case DW_FORM_strx3:
case DW_FORM_strx4:
case DW_FORM_strx: {
if (!dwarf->strOffsets)
throw Exception() << "no string offsets table, but have strx form";
// Get the root die, and the string offset base.
auto root = die().unit->root();
auto base = intmax_t(root.attribute(DW_AT_str_offsets_base));
auto idx = value().addr;
auto len = die().unit->dwarfLen;
DWARFReader r(dwarf->strOffsets, base + len * idx);
return dwarf->debugStrings->readString(r.getuint(len));
}
default:
abort();
}
}
void
RawDIE::readValue(DWARFReader &r, const FormEntry &forment, Value &value, Unit *unit)
{
switch (forment.form) {
case DW_FORM_GNU_strp_alt: {
value.addr = r.getint(unit->dwarfLen);
break;
}
case DW_FORM_strp:
value.addr = r.getint(unit->version <= 2 ? 4 : unit->dwarfLen);
break;
case DW_FORM_GNU_ref_alt:
value.addr = r.getuint(unit->dwarfLen);
break;
case DW_FORM_addr:
value.addr = r.getuint(unit->addrlen);
break;
case DW_FORM_data1:
value.udata = r.getu8();
break;
case DW_FORM_data2:
value.udata = r.getu16();
break;
case DW_FORM_data4:
value.udata = r.getu32();
break;
case DW_FORM_data8:
value.udata = r.getuint(8);
break;
case DW_FORM_sdata:
value.sdata = r.getsleb128();
break;
case DW_FORM_udata:
value.udata = r.getuleb128();
break;
case DW_FORM_strx:
case DW_FORM_rnglistx:
case DW_FORM_addrx:
case DW_FORM_ref_udata:
value.addr = r.getuleb128();
break;
case DW_FORM_strx1:
case DW_FORM_addrx1:
case DW_FORM_ref1:
value.addr = r.getu8();
break;
case DW_FORM_strx2:
case DW_FORM_ref2:
value.addr = r.getu16();
break;
case DW_FORM_addrx3:
case DW_FORM_strx3:
value.addr = r.getuint(3);
break;
case DW_FORM_strx4:
case DW_FORM_addrx4:
case DW_FORM_ref4:
value.addr = r.getu32();
break;
case DW_FORM_ref_addr:
value.addr = r.getuint(unit->dwarfLen);
break;
case DW_FORM_ref8:
value.addr = r.getuint(8);
break;
case DW_FORM_string:
value.addr = r.getOffset();
r.getstring();
break;
case DW_FORM_block1:
value.block = new Block();
value.block->length = r.getu8();
value.block->offset = r.getOffset();
r.skip(value.block->length);
break;
case DW_FORM_block2:
value.block = new Block();
value.block->length = r.getu16();
value.block->offset = r.getOffset();
r.skip(value.block->length);
break;
case DW_FORM_block4:
value.block = new Block();
value.block->length = r.getu32();
value.block->offset = r.getOffset();
r.skip(value.block->length);
break;
case DW_FORM_exprloc:
case DW_FORM_block:
value.block = new Block();
value.block->length = r.getuleb128();
value.block->offset = r.getOffset();
r.skip(value.block->length);
break;
case DW_FORM_flag:
value.flag = r.getu8() != 0;
break;
case DW_FORM_flag_present:
value.flag = true;
break;
case DW_FORM_sec_offset:
value.addr = r.getint(unit->dwarfLen);
break;
case DW_FORM_ref_sig8:
value.signature = r.getuint(8);
break;
case DW_FORM_implicit_const:
value.sdata = forment.value;
break;
default:
value.addr = 0;
abort();
break;
}
}
RawDIE::~RawDIE()
{
stats.delone();
int i = 0;
for (auto &forment : type->forms) {
switch (forment.form) {
case DW_FORM_exprloc:
case DW_FORM_block:
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
delete values[i].block;
break;
default:
break;
}
++i;
}
}
const LineInfo *
Unit::getLines()
{
if (lines != nullptr)
return lines.get();
if (dwarf->lineshdr == nullptr)
return nullptr;
const auto &r = root();
if (r.tag() != DW_TAG_partial_unit && r.tag() != DW_TAG_compile_unit)
return nullptr; // XXX: assert?
auto attr = r.attribute(DW_AT_stmt_list);
if (!attr.valid())
return nullptr;
auto stmts = intmax_t(attr);
DWARFReader r2(dwarf->lineshdr, stmts);
lines.reset(new LineInfo());
lines->build(r2, this);
return lines.get();
}
RawDIE::RawDIE(Unit *unit, DWARFReader &r, size_t abbrev, Elf::Off parent_)
: type(unit->findAbbreviation(abbrev))
, values(type->forms.size())
, parent(parent_)
, firstChild(0)
, nextSibling(0)
{
size_t i = 0;
for (auto &form : type->forms) {
readValue(r, form, values[i], unit);
if (int(i) == type->nextSibIdx)
nextSibling = values[i].sdata + unit->offset;
++i;
}
if (type->hasChildren) {
// If the type has children, last offset read is the first child.
firstChild = r.getOffset();
} else {
nextSibling = r.getOffset(); // we have no children, so next DIE is next sib
firstChild = 0; // no children.
}
stats.addone();
}
const Abbreviation *
Unit::findAbbreviation(size_t code) const
{
auto it = abbreviations.find(code);
return it != abbreviations.end() ? &it->second : nullptr;
}
std::shared_ptr<RawDIE>
Unit::decodeEntry(const DIE &parent, Elf::Off offset)
{
DWARFReader r(io, offset);
size_t abbrev = r.getuleb128();
if (abbrev == 0) {
// If we get to the terminator, then we now know the parent's nextSibling:
// update it now.
if (parent)
parent.raw->nextSibling = r.getOffset();
return nullptr;
}
return std::make_shared<RawDIE>(this, r, abbrev, parent.getOffset());
}
void
Unit::purge()
{
auto start = stats.currentDIEs;
{
AllEntries destroy;
std::swap(allEntries, destroy);
}
auto end = stats.currentDIEs;
if (verbose >= 3)
*debug << "purging " << name() << " in " << *dwarf->elf->io
<< " freed " << start - end << " DIEs (total now "
<< stats.currentDIEs << ")" << std::endl;
}
string
Info::getAltImageName() const
{
auto §ion = elf->getSection(".gnu_debugaltlink", 0);
const auto &name = section.io->readString(0);
if (name[0] == '/')
return name;
// relative - prefix it with dirname of the image
const auto &exedir = dirname(linkResolve(io->filename()));
return stringify(exedir, "/", name);
}
Info::sptr
Info::getAltDwarf() const
{
if (!altImageLoaded) {
altDwarf = imageCache.getDwarf(getAltImageName());
altImageLoaded = true;
}
if (altDwarf == nullptr)