forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf_reader.cc
2416 lines (2141 loc) · 70.9 KB
/
dwarf_reader.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
// dwarf_reader.cc -- parse dwarf2/3 debug information
// Copyright (C) 2007-2016 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <[email protected]>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "gold.h"
#include <algorithm>
#include <utility>
#include <vector>
#include "elfcpp_swap.h"
#include "dwarf.h"
#include "object.h"
#include "reloc.h"
#include "dwarf_reader.h"
#include "int_encoding.h"
#include "compressed_output.h"
namespace gold {
// Class Sized_elf_reloc_mapper
// Initialize the relocation tracker for section RELOC_SHNDX.
template<int size, bool big_endian>
bool
Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
unsigned int reloc_shndx, unsigned int reloc_type)
{
this->reloc_type_ = reloc_type;
return this->track_relocs_.initialize(this->object_, reloc_shndx,
reloc_type);
}
// Looks in the symtab to see what section a symbol is in.
template<int size, bool big_endian>
unsigned int
Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
unsigned int symndx, Address* value, bool* is_ordinary)
{
const int symsize = elfcpp::Elf_sizes<size>::sym_size;
gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_);
elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
*value = elfsym.get_st_value();
return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
is_ordinary);
}
// Return the section index and offset within the section of
// the target of the relocation for RELOC_OFFSET.
template<int size, bool big_endian>
unsigned int
Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
off_t reloc_offset, off_t* target_offset)
{
this->track_relocs_.advance(reloc_offset);
if (reloc_offset != this->track_relocs_.next_offset())
return 0;
unsigned int symndx = this->track_relocs_.next_symndx();
typename elfcpp::Elf_types<size>::Elf_Addr value;
bool is_ordinary;
unsigned int target_shndx = this->symbol_section(symndx, &value,
&is_ordinary);
if (!is_ordinary)
return 0;
if (this->reloc_type_ == elfcpp::SHT_RELA)
value += this->track_relocs_.next_addend();
*target_offset = value;
return target_shndx;
}
static inline Elf_reloc_mapper*
make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
off_t symtab_size)
{
if (object->elfsize() == 32)
{
if (object->is_big_endian())
{
#ifdef HAVE_TARGET_32_BIG
return new Sized_elf_reloc_mapper<32, true>(object, symtab,
symtab_size);
#else
gold_unreachable();
#endif
}
else
{
#ifdef HAVE_TARGET_32_LITTLE
return new Sized_elf_reloc_mapper<32, false>(object, symtab,
symtab_size);
#else
gold_unreachable();
#endif
}
}
else if (object->elfsize() == 64)
{
if (object->is_big_endian())
{
#ifdef HAVE_TARGET_64_BIG
return new Sized_elf_reloc_mapper<64, true>(object, symtab,
symtab_size);
#else
gold_unreachable();
#endif
}
else
{
#ifdef HAVE_TARGET_64_LITTLE
return new Sized_elf_reloc_mapper<64, false>(object, symtab,
symtab_size);
#else
gold_unreachable();
#endif
}
}
else
gold_unreachable();
}
// class Dwarf_abbrev_table
void
Dwarf_abbrev_table::clear_abbrev_codes()
{
for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
{
if (this->low_abbrev_codes_[code] != NULL)
{
delete this->low_abbrev_codes_[code];
this->low_abbrev_codes_[code] = NULL;
}
}
for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
it != this->high_abbrev_codes_.end();
++it)
{
if (it->second != NULL)
delete it->second;
}
this->high_abbrev_codes_.clear();
}
// Read the abbrev table from an object file.
bool
Dwarf_abbrev_table::do_read_abbrevs(
Relobj* object,
unsigned int abbrev_shndx,
off_t abbrev_offset)
{
this->clear_abbrev_codes();
// If we don't have relocations, abbrev_shndx will be 0, and
// we'll have to hunt for the .debug_abbrev section.
if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
abbrev_shndx = this->abbrev_shndx_;
else if (abbrev_shndx == 0)
{
for (unsigned int i = 1; i < object->shnum(); ++i)
{
std::string name = object->section_name(i);
if (name == ".debug_abbrev" || name == ".zdebug_abbrev")
{
abbrev_shndx = i;
// Correct the offset. For incremental update links, we have a
// relocated offset that is relative to the output section, but
// here we need an offset relative to the input section.
abbrev_offset -= object->output_section_offset(i);
break;
}
}
if (abbrev_shndx == 0)
return false;
}
// Get the section contents and decompress if necessary.
if (abbrev_shndx != this->abbrev_shndx_)
{
if (this->owns_buffer_ && this->buffer_ != NULL)
{
delete[] this->buffer_;
this->owns_buffer_ = false;
}
section_size_type buffer_size;
this->buffer_ =
object->decompressed_section_contents(abbrev_shndx,
&buffer_size,
&this->owns_buffer_);
this->buffer_end_ = this->buffer_ + buffer_size;
this->abbrev_shndx_ = abbrev_shndx;
}
this->buffer_pos_ = this->buffer_ + abbrev_offset;
return true;
}
// Lookup the abbrev code entry for CODE. This function is called
// only when the abbrev code is not in the direct lookup table.
// It may be in the hash table, it may not have been read yet,
// or it may not exist in the abbrev table.
const Dwarf_abbrev_table::Abbrev_code*
Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
{
// See if the abbrev code is already in the hash table.
Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
if (it != this->high_abbrev_codes_.end())
return it->second;
// Read and store abbrev code definitions until we find the
// one we're looking for.
for (;;)
{
// Read the abbrev code. A zero here indicates the end of the
// abbrev table.
size_t len;
if (this->buffer_pos_ >= this->buffer_end_)
return NULL;
uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
if (nextcode == 0)
{
this->buffer_pos_ = this->buffer_end_;
return NULL;
}
this->buffer_pos_ += len;
// Read the tag.
if (this->buffer_pos_ >= this->buffer_end_)
return NULL;
uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
this->buffer_pos_ += len;
// Read the has_children flag.
if (this->buffer_pos_ >= this->buffer_end_)
return NULL;
bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
this->buffer_pos_ += 1;
// Read the list of (attribute, form) pairs.
Abbrev_code* entry = new Abbrev_code(tag, has_children);
for (;;)
{
// Read the attribute.
if (this->buffer_pos_ >= this->buffer_end_)
return NULL;
uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
this->buffer_pos_ += len;
// Read the form.
if (this->buffer_pos_ >= this->buffer_end_)
return NULL;
uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
this->buffer_pos_ += len;
// A (0,0) pair terminates the list.
if (attr == 0 && form == 0)
break;
if (attr == elfcpp::DW_AT_sibling)
entry->has_sibling_attribute = true;
entry->add_attribute(attr, form);
}
this->store_abbrev(nextcode, entry);
if (nextcode == code)
return entry;
}
return NULL;
}
// class Dwarf_ranges_table
// Read the ranges table from an object file.
bool
Dwarf_ranges_table::read_ranges_table(
Relobj* object,
const unsigned char* symtab,
off_t symtab_size,
unsigned int ranges_shndx)
{
// If we've already read this abbrev table, return immediately.
if (this->ranges_shndx_ > 0
&& this->ranges_shndx_ == ranges_shndx)
return true;
// If we don't have relocations, ranges_shndx will be 0, and
// we'll have to hunt for the .debug_ranges section.
if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
ranges_shndx = this->ranges_shndx_;
else if (ranges_shndx == 0)
{
for (unsigned int i = 1; i < object->shnum(); ++i)
{
std::string name = object->section_name(i);
if (name == ".debug_ranges" || name == ".zdebug_ranges")
{
ranges_shndx = i;
this->output_section_offset_ = object->output_section_offset(i);
break;
}
}
if (ranges_shndx == 0)
return false;
}
// Get the section contents and decompress if necessary.
if (ranges_shndx != this->ranges_shndx_)
{
if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
{
delete[] this->ranges_buffer_;
this->owns_ranges_buffer_ = false;
}
section_size_type buffer_size;
this->ranges_buffer_ =
object->decompressed_section_contents(ranges_shndx,
&buffer_size,
&this->owns_ranges_buffer_);
this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
this->ranges_shndx_ = ranges_shndx;
}
if (this->ranges_reloc_mapper_ != NULL)
{
delete this->ranges_reloc_mapper_;
this->ranges_reloc_mapper_ = NULL;
}
// For incremental objects, we have no relocations.
if (object->is_incremental())
return true;
// Find the relocation section for ".debug_ranges".
unsigned int reloc_shndx = 0;
unsigned int reloc_type = 0;
for (unsigned int i = 0; i < object->shnum(); ++i)
{
reloc_type = object->section_type(i);
if ((reloc_type == elfcpp::SHT_REL
|| reloc_type == elfcpp::SHT_RELA)
&& object->section_info(i) == ranges_shndx)
{
reloc_shndx = i;
break;
}
}
this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
symtab_size);
this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
this->reloc_type_ = reloc_type;
return true;
}
// Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
Dwarf_range_list*
Dwarf_ranges_table::read_range_list(
Relobj* object,
const unsigned char* symtab,
off_t symtab_size,
unsigned int addr_size,
unsigned int ranges_shndx,
off_t offset)
{
Dwarf_range_list* ranges;
if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
return NULL;
// Correct the offset. For incremental update links, we have a
// relocated offset that is relative to the output section, but
// here we need an offset relative to the input section.
offset -= this->output_section_offset_;
// Read the range list at OFFSET.
ranges = new Dwarf_range_list();
off_t base = 0;
for (;
this->ranges_buffer_ + offset < this->ranges_buffer_end_;
offset += 2 * addr_size)
{
off_t start;
off_t end;
// Read the raw contents of the section.
if (addr_size == 4)
{
start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
+ offset);
end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
+ offset + 4);
}
else
{
start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
+ offset);
end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
+ offset + 8);
}
// Check for relocations and adjust the values.
unsigned int shndx1 = 0;
unsigned int shndx2 = 0;
if (this->ranges_reloc_mapper_ != NULL)
{
shndx1 = this->lookup_reloc(offset, &start);
shndx2 = this->lookup_reloc(offset + addr_size, &end);
}
// End of list is marked by a pair of zeroes.
if (shndx1 == 0 && start == 0 && end == 0)
break;
// A "base address selection entry" is identified by
// 0xffffffff for the first value of the pair. The second
// value is used as a base for subsequent range list entries.
if (shndx1 == 0 && start == -1)
base = end;
else if (shndx1 == shndx2)
{
if (shndx1 == 0 || object->is_section_included(shndx1))
ranges->add(shndx1, base + start, base + end);
}
else
gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
"range list entry are in different sections"),
object->name().c_str());
}
return ranges;
}
// Look for a relocation at offset OFF in the range table,
// and return the section index and offset of the target.
unsigned int
Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
{
off_t value;
unsigned int shndx =
this->ranges_reloc_mapper_->get_reloc_target(off, &value);
if (shndx == 0)
return 0;
if (this->reloc_type_ == elfcpp::SHT_REL)
*target_off += value;
else
*target_off = value;
return shndx;
}
// class Dwarf_pubnames_table
// Read the pubnames section from the object file.
bool
Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab,
off_t symtab_size)
{
section_size_type buffer_size;
unsigned int shndx = 0;
const char* name = this->is_pubtypes_ ? "pubtypes" : "pubnames";
const char* gnu_name = (this->is_pubtypes_
? "gnu_pubtypes"
: "gnu_pubnames");
for (unsigned int i = 1; i < object->shnum(); ++i)
{
std::string section_name = object->section_name(i);
const char* section_name_suffix = section_name.c_str();
if (is_prefix_of(".debug_", section_name_suffix))
section_name_suffix += 7;
else if (is_prefix_of(".zdebug_", section_name_suffix))
section_name_suffix += 8;
else
continue;
if (strcmp(section_name_suffix, name) == 0)
{
shndx = i;
break;
}
else if (strcmp(section_name_suffix, gnu_name) == 0)
{
shndx = i;
this->is_gnu_style_ = true;
break;
}
}
if (shndx == 0)
return false;
this->buffer_ = object->decompressed_section_contents(shndx,
&buffer_size,
&this->owns_buffer_);
if (this->buffer_ == NULL)
return false;
this->buffer_end_ = this->buffer_ + buffer_size;
// For incremental objects, we have no relocations.
if (object->is_incremental())
return true;
// Find the relocation section
unsigned int reloc_shndx = 0;
unsigned int reloc_type = 0;
for (unsigned int i = 0; i < object->shnum(); ++i)
{
reloc_type = object->section_type(i);
if ((reloc_type == elfcpp::SHT_REL
|| reloc_type == elfcpp::SHT_RELA)
&& object->section_info(i) == shndx)
{
reloc_shndx = i;
break;
}
}
this->reloc_mapper_ = make_elf_reloc_mapper(object, symtab, symtab_size);
this->reloc_mapper_->initialize(reloc_shndx, reloc_type);
this->reloc_type_ = reloc_type;
return true;
}
// Read the header for the set at OFFSET.
bool
Dwarf_pubnames_table::read_header(off_t offset)
{
// Make sure we have actually read the section.
gold_assert(this->buffer_ != NULL);
if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
return false;
const unsigned char* pinfo = this->buffer_ + offset;
// Read the unit_length field.
uint64_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
pinfo += 4;
if (unit_length == 0xffffffff)
{
unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
this->unit_length_ = unit_length + 12;
pinfo += 8;
this->offset_size_ = 8;
}
else
{
this->unit_length_ = unit_length + 4;
this->offset_size_ = 4;
}
this->end_of_table_ = pinfo + unit_length;
// If unit_length is too big, maybe we should reject the whole table,
// but in cases we know about, it seems OK to assume that the table
// is valid through the actual end of the section.
if (this->end_of_table_ > this->buffer_end_)
this->end_of_table_ = this->buffer_end_;
// Check the version.
unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
pinfo += 2;
if (version != 2)
return false;
this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_,
&this->cu_offset_);
// Skip the debug_info_offset and debug_info_size fields.
pinfo += 2 * this->offset_size_;
if (pinfo >= this->buffer_end_)
return false;
this->pinfo_ = pinfo;
return true;
}
// Read the next name from the set.
const char*
Dwarf_pubnames_table::next_name(uint8_t* flag_byte)
{
const unsigned char* pinfo = this->pinfo_;
// Check for end of list. The table should be terminated by an
// entry containing nothing but a DIE offset of 0.
if (pinfo + this->offset_size_ >= this->end_of_table_)
return NULL;
// Skip the offset within the CU. If this is zero, but we're not
// at the end of the table, then we have a real pubnames entry
// whose DIE offset is 0 (likely to be a GCC bug). Since we
// don't actually use the DIE offset in building .gdb_index,
// it's harmless.
pinfo += this->offset_size_;
if (this->is_gnu_style_)
*flag_byte = *pinfo++;
else
*flag_byte = 0;
// Return a pointer to the string at the current location,
// and advance the pointer to the next entry.
const char* ret = reinterpret_cast<const char*>(pinfo);
while (pinfo < this->buffer_end_ && *pinfo != '\0')
++pinfo;
if (pinfo < this->buffer_end_)
++pinfo;
this->pinfo_ = pinfo;
return ret;
}
// class Dwarf_die
Dwarf_die::Dwarf_die(
Dwarf_info_reader* dwinfo,
off_t die_offset,
Dwarf_die* parent)
: dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
linkage_name_off_(-1), string_shndx_(0), specification_(0),
abstract_origin_(0)
{
size_t len;
const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
if (pdie == NULL)
return;
unsigned int code = read_unsigned_LEB_128(pdie, &len);
if (code == 0)
{
if (parent != NULL)
parent->set_sibling_offset(die_offset + len);
return;
}
this->attr_offset_ = len;
// Lookup the abbrev code in the abbrev table.
this->abbrev_code_ = dwinfo->get_abbrev(code);
}
// Read all the attributes of the DIE.
bool
Dwarf_die::read_attributes()
{
if (this->attributes_read_)
return true;
gold_assert(this->abbrev_code_ != NULL);
const unsigned char* pdie =
this->dwinfo_->buffer_at_offset(this->die_offset_);
if (pdie == NULL)
return false;
const unsigned char* pattr = pdie + this->attr_offset_;
unsigned int nattr = this->abbrev_code_->attributes.size();
this->attributes_.reserve(nattr);
for (unsigned int i = 0; i < nattr; ++i)
{
size_t len;
unsigned int attr = this->abbrev_code_->attributes[i].attr;
unsigned int form = this->abbrev_code_->attributes[i].form;
if (form == elfcpp::DW_FORM_indirect)
{
form = read_unsigned_LEB_128(pattr, &len);
pattr += len;
}
off_t attr_off = this->die_offset_ + (pattr - pdie);
bool ref_form = false;
Attribute_value attr_value;
attr_value.attr = attr;
attr_value.form = form;
attr_value.aux.shndx = 0;
switch(form)
{
case elfcpp::DW_FORM_flag_present:
attr_value.val.intval = 1;
break;
case elfcpp::DW_FORM_strp:
{
off_t str_off;
if (this->dwinfo_->offset_size() == 4)
str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
else
str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &str_off);
attr_value.aux.shndx = shndx;
attr_value.val.refval = str_off;
break;
}
case elfcpp::DW_FORM_sec_offset:
{
off_t sec_off;
if (this->dwinfo_->offset_size() == 4)
sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
else
sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.refval = sec_off;
ref_form = true;
break;
}
case elfcpp::DW_FORM_addr:
case elfcpp::DW_FORM_ref_addr:
{
off_t sec_off;
if (this->dwinfo_->address_size() == 4)
sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
else
sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.refval = sec_off;
ref_form = true;
break;
}
case elfcpp::DW_FORM_block1:
attr_value.aux.blocklen = *pattr++;
attr_value.val.blockval = pattr;
pattr += attr_value.aux.blocklen;
break;
case elfcpp::DW_FORM_block2:
attr_value.aux.blocklen =
this->dwinfo_->read_from_pointer<16>(&pattr);
attr_value.val.blockval = pattr;
pattr += attr_value.aux.blocklen;
break;
case elfcpp::DW_FORM_block4:
attr_value.aux.blocklen =
this->dwinfo_->read_from_pointer<32>(&pattr);
attr_value.val.blockval = pattr;
pattr += attr_value.aux.blocklen;
break;
case elfcpp::DW_FORM_block:
case elfcpp::DW_FORM_exprloc:
attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
attr_value.val.blockval = pattr + len;
pattr += len + attr_value.aux.blocklen;
break;
case elfcpp::DW_FORM_data1:
case elfcpp::DW_FORM_flag:
attr_value.val.intval = *pattr++;
break;
case elfcpp::DW_FORM_ref1:
attr_value.val.refval = *pattr++;
ref_form = true;
break;
case elfcpp::DW_FORM_data2:
attr_value.val.intval =
this->dwinfo_->read_from_pointer<16>(&pattr);
break;
case elfcpp::DW_FORM_ref2:
attr_value.val.refval =
this->dwinfo_->read_from_pointer<16>(&pattr);
ref_form = true;
break;
case elfcpp::DW_FORM_data4:
{
off_t sec_off;
sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.intval = sec_off;
break;
}
case elfcpp::DW_FORM_ref4:
{
off_t sec_off;
sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.refval = sec_off;
ref_form = true;
break;
}
case elfcpp::DW_FORM_data8:
{
off_t sec_off;
sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.intval = sec_off;
break;
}
case elfcpp::DW_FORM_ref_sig8:
attr_value.val.uintval =
this->dwinfo_->read_from_pointer<64>(&pattr);
break;
case elfcpp::DW_FORM_ref8:
{
off_t sec_off;
sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
unsigned int shndx =
this->dwinfo_->lookup_reloc(attr_off, &sec_off);
attr_value.aux.shndx = shndx;
attr_value.val.refval = sec_off;
ref_form = true;
break;
}
case elfcpp::DW_FORM_ref_udata:
attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
ref_form = true;
pattr += len;
break;
case elfcpp::DW_FORM_udata:
case elfcpp::DW_FORM_GNU_addr_index:
case elfcpp::DW_FORM_GNU_str_index:
attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
pattr += len;
break;
case elfcpp::DW_FORM_sdata:
attr_value.val.intval = read_signed_LEB_128(pattr, &len);
pattr += len;
break;
case elfcpp::DW_FORM_string:
attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
len = strlen(attr_value.val.stringval);
pattr += len + 1;
break;
default:
return false;
}
// Cache the most frequently-requested attributes.
switch (attr)
{
case elfcpp::DW_AT_name:
if (form == elfcpp::DW_FORM_string)
this->name_ = attr_value.val.stringval;
else if (form == elfcpp::DW_FORM_strp)
{
// All indirect strings should refer to the same
// string section, so we just save the last one seen.
this->string_shndx_ = attr_value.aux.shndx;
this->name_off_ = attr_value.val.refval;
}
break;
case elfcpp::DW_AT_linkage_name:
case elfcpp::DW_AT_MIPS_linkage_name:
if (form == elfcpp::DW_FORM_string)
this->linkage_name_ = attr_value.val.stringval;
else if (form == elfcpp::DW_FORM_strp)
{
// All indirect strings should refer to the same
// string section, so we just save the last one seen.
this->string_shndx_ = attr_value.aux.shndx;
this->linkage_name_off_ = attr_value.val.refval;
}
break;
case elfcpp::DW_AT_specification:
if (ref_form)
this->specification_ = attr_value.val.refval;
break;
case elfcpp::DW_AT_abstract_origin:
if (ref_form)
this->abstract_origin_ = attr_value.val.refval;
break;
case elfcpp::DW_AT_sibling:
if (ref_form && attr_value.aux.shndx == 0)
this->sibling_offset_ = attr_value.val.refval;
default:
break;
}
this->attributes_.push_back(attr_value);
}
// Now that we know where the next DIE begins, record the offset
// to avoid later recalculation.
if (this->has_children())
this->child_offset_ = this->die_offset_ + (pattr - pdie);
else
this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
this->attributes_read_ = true;
return true;
}
// Skip all the attributes of the DIE and return the offset of the next DIE.
off_t
Dwarf_die::skip_attributes()
{
gold_assert(this->abbrev_code_ != NULL);
const unsigned char* pdie =
this->dwinfo_->buffer_at_offset(this->die_offset_);
if (pdie == NULL)
return 0;
const unsigned char* pattr = pdie + this->attr_offset_;
for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
{
size_t len;
unsigned int form = this->abbrev_code_->attributes[i].form;
if (form == elfcpp::DW_FORM_indirect)
{
form = read_unsigned_LEB_128(pattr, &len);
pattr += len;
}
switch(form)
{
case elfcpp::DW_FORM_flag_present:
break;
case elfcpp::DW_FORM_strp:
case elfcpp::DW_FORM_sec_offset:
pattr += this->dwinfo_->offset_size();
break;
case elfcpp::DW_FORM_addr:
case elfcpp::DW_FORM_ref_addr:
pattr += this->dwinfo_->address_size();
break;
case elfcpp::DW_FORM_block1:
pattr += 1 + *pattr;
break;
case elfcpp::DW_FORM_block2:
{
uint16_t block_size;
block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
pattr += block_size;
break;
}
case elfcpp::DW_FORM_block4:
{
uint32_t block_size;
block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
pattr += block_size;
break;
}
case elfcpp::DW_FORM_block:
case elfcpp::DW_FORM_exprloc:
{
uint64_t block_size;
block_size = read_unsigned_LEB_128(pattr, &len);
pattr += len + block_size;
break;
}
case elfcpp::DW_FORM_data1:
case elfcpp::DW_FORM_ref1:
case elfcpp::DW_FORM_flag:
pattr += 1;
break;
case elfcpp::DW_FORM_data2:
case elfcpp::DW_FORM_ref2:
pattr += 2;
break;
case elfcpp::DW_FORM_data4:
case elfcpp::DW_FORM_ref4:
pattr += 4;
break;
case elfcpp::DW_FORM_data8:
case elfcpp::DW_FORM_ref8:
case elfcpp::DW_FORM_ref_sig8:
pattr += 8;
break;
case elfcpp::DW_FORM_ref_udata:
case elfcpp::DW_FORM_udata:
case elfcpp::DW_FORM_GNU_addr_index:
case elfcpp::DW_FORM_GNU_str_index:
read_unsigned_LEB_128(pattr, &len);