forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwp.cc
2451 lines (2091 loc) · 71.9 KB
/
dwp.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
// dwp.cc -- DWARF packaging utility
// Copyright (C) 2012-2025 Free Software Foundation, Inc.
// Written by Cary Coutant <[email protected]>.
// This file is part of dwp, the DWARF packaging utility.
// 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 "dwp.h"
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <vector>
#include <algorithm>
#include "getopt.h"
#include "libiberty.h"
#include "../bfd/bfdver.h"
#include "elfcpp.h"
#include "elfcpp_file.h"
#include "dwarf.h"
#include "dirsearch.h"
#include "fileread.h"
#include "object.h"
#include "compressed_output.h"
#include "stringpool.h"
#include "dwarf_reader.h"
static void
usage(FILE* fd, int) ATTRIBUTE_NORETURN;
static void
print_version() ATTRIBUTE_NORETURN;
namespace gold {
class Dwp_output_file;
template <int size, bool big_endian>
class Sized_relobj_dwo;
// List of .dwo files to process.
struct Dwo_file_entry
{
Dwo_file_entry(uint64_t id, std::string name)
: dwo_id(id), dwo_name(name)
{ }
uint64_t dwo_id;
std::string dwo_name;
};
typedef std::vector<Dwo_file_entry> File_list;
// Type to hold the offset and length of an input section
// within an output section.
struct Section_bounds
{
section_offset_type offset;
section_size_type size;
Section_bounds()
: offset(0), size(0)
{ }
Section_bounds(section_offset_type o, section_size_type s)
: offset(o), size(s)
{ }
};
// A set of sections for a compilation unit or type unit.
struct Unit_set
{
uint64_t signature;
Section_bounds sections[elfcpp::DW_SECT_MAX + 1];
Unit_set()
: signature(0), sections()
{ }
};
// An input file.
// This class may represent a .dwo file, a .dwp file
// produced by an earlier run, or an executable file whose
// debug section identifies a set of .dwo files to read.
class Dwo_file
{
public:
Dwo_file(const char* name)
: name_(name), obj_(NULL), input_file_(NULL), is_compressed_(),
sect_offsets_(), str_offset_map_()
{ }
~Dwo_file();
// Read the input executable file and extract the list of .dwo files
// that it references.
void
read_executable(File_list* files);
// Read the input file and send its contents to OUTPUT_FILE.
void
read(Dwp_output_file* output_file);
// Verify a .dwp file given a list of .dwo files referenced by the
// corresponding executable file. Returns true if no problems
// were found.
bool
verify(const File_list& files);
private:
// Types for mapping input string offsets to output string offsets.
typedef std::pair<section_offset_type, section_offset_type>
Str_offset_map_entry;
typedef std::vector<Str_offset_map_entry> Str_offset_map;
// A less-than comparison routine for Str_offset_map.
struct Offset_compare
{
bool
operator()(const Str_offset_map_entry& i1,
const Str_offset_map_entry& i2) const
{ return i1.first < i2.first; }
};
// Create a Sized_relobj_dwo of the given size and endianness,
// and record the target info. P is a pointer to the ELF header
// in memory.
Relobj*
make_object(Dwp_output_file* output_file);
template <int size, bool big_endian>
Relobj*
sized_make_object(const unsigned char* p, Input_file* input_file,
Dwp_output_file* output_file);
// Return the number of sections in the input object file.
unsigned int
shnum() const
{ return this->obj_->shnum(); }
// Return section type.
unsigned int
section_type(unsigned int shndx)
{ return this->obj_->section_type(shndx); }
// Get the name of a section.
std::string
section_name(unsigned int shndx)
{ return this->obj_->section_name(shndx); }
// Return a view of the contents of a section, decompressed if necessary.
// Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
// deleted by the caller.
const unsigned char*
section_contents(unsigned int shndx, section_size_type* plen, bool* is_new)
{ return this->obj_->decompressed_section_contents(shndx, plen, is_new); }
// Read the .debug_cu_index or .debug_tu_index section of a .dwp file,
// and process the CU or TU sets.
void
read_unit_index(unsigned int, unsigned int *, Dwp_output_file*,
bool is_tu_index);
template <bool big_endian>
void
sized_read_unit_index(unsigned int, unsigned int *, Dwp_output_file*,
bool is_tu_index);
// Verify the .debug_cu_index section of a .dwp file, comparing it
// against the list of .dwo files referenced by the corresponding
// executable file.
bool
verify_dwo_list(unsigned int, const File_list& files);
template <bool big_endian>
bool
sized_verify_dwo_list(unsigned int, const File_list& files);
// Merge the input string table section into the output file.
void
add_strings(Dwp_output_file*, unsigned int);
// Copy a section from the input file to the output file.
Section_bounds
copy_section(Dwp_output_file* output_file, unsigned int shndx,
elfcpp::DW_SECT section_id);
// Remap the string offsets in the .debug_str_offsets.dwo section.
const unsigned char*
remap_str_offsets(const unsigned char* contents, section_size_type len);
template <bool big_endian>
const unsigned char*
sized_remap_str_offsets(const unsigned char* contents, section_size_type len);
// Remap a single string offsets from an offset in the input string table
// to an offset in the output string table.
unsigned int
remap_str_offset(section_offset_type val);
// Add a set of .debug_info.dwo or .debug_types.dwo and related sections
// to OUTPUT_FILE.
void
add_unit_set(Dwp_output_file* output_file, unsigned int *debug_shndx,
bool is_debug_types);
// The filename.
const char* name_;
// The ELF file, represented as a gold Relobj instance.
Relobj* obj_;
// The Input_file object.
Input_file* input_file_;
// Flags indicating which sections are compressed.
std::vector<bool> is_compressed_;
// Map input section index onto output section offset and size.
std::vector<Section_bounds> sect_offsets_;
// Map input string offsets to output string offsets.
Str_offset_map str_offset_map_;
};
// An ELF input file.
// We derive from Sized_relobj so that we can use interfaces
// in libgold to access the file.
template <int size, bool big_endian>
class Sized_relobj_dwo : public Sized_relobj<size, big_endian>
{
public:
typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
Sized_relobj_dwo(const char* name, Input_file* input_file,
const elfcpp::Ehdr<size, big_endian>& ehdr)
: Sized_relobj<size, big_endian>(name, input_file),
elf_file_(this, ehdr)
{ }
~Sized_relobj_dwo()
{ }
// Setup the section information.
void
setup();
protected:
// Return section type.
unsigned int
do_section_type(unsigned int shndx)
{ return this->elf_file_.section_type(shndx); }
// Get the name of a section.
std::string
do_section_name(unsigned int shndx) const
{ return this->elf_file_.section_name(shndx); }
// Get the size of a section.
uint64_t
do_section_size(unsigned int shndx)
{ return this->elf_file_.section_size(shndx); }
// Return a view of the contents of a section.
const unsigned char*
do_section_contents(unsigned int, section_size_type*, bool);
// The following virtual functions are abstract in the base classes,
// but are not used here.
// Read the symbols.
void
do_read_symbols(Read_symbols_data*)
{ gold_unreachable(); }
// Lay out the input sections.
void
do_layout(Symbol_table*, Layout*, Read_symbols_data*)
{ gold_unreachable(); }
// Layout sections whose layout was deferred while waiting for
// input files from a plugin.
void
do_layout_deferred_sections(Layout*)
{ gold_unreachable(); }
// Add the symbols to the symbol table.
void
do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*)
{ gold_unreachable(); }
Archive::Should_include
do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*,
std::string*)
{ gold_unreachable(); }
// Iterate over global symbols, calling a visitor class V for each.
void
do_for_all_global_symbols(Read_symbols_data*,
Library_base::Symbol_visitor_base*)
{ gold_unreachable(); }
// Return section flags.
uint64_t
do_section_flags(unsigned int)
{ gold_unreachable(); }
// Return section entsize.
uint64_t
do_section_entsize(unsigned int)
{ gold_unreachable(); }
// Return section address.
uint64_t
do_section_address(unsigned int)
{ gold_unreachable(); }
// Return the section link field.
unsigned int
do_section_link(unsigned int)
{ gold_unreachable(); }
// Return the section link field.
unsigned int
do_section_info(unsigned int)
{ gold_unreachable(); }
// Return the section alignment.
uint64_t
do_section_addralign(unsigned int)
{ gold_unreachable(); }
// Return the Xindex structure to use.
Xindex*
do_initialize_xindex()
{ gold_unreachable(); }
// Get symbol counts.
void
do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const
{ gold_unreachable(); }
// Get global symbols.
const Symbols*
do_get_global_symbols() const
{ return NULL; }
// Return the value of a local symbol.
uint64_t
do_local_symbol_value(unsigned int, uint64_t) const
{ gold_unreachable(); }
unsigned int
do_local_plt_offset(unsigned int) const
{ gold_unreachable(); }
// Return whether local symbol SYMNDX is a TLS symbol.
bool
do_local_is_tls(unsigned int) const
{ gold_unreachable(); }
// Return the number of local symbols.
unsigned int
do_local_symbol_count() const
{ gold_unreachable(); }
// Return the number of local symbols in the output symbol table.
unsigned int
do_output_local_symbol_count() const
{ gold_unreachable(); }
// Return the file offset for local symbols in the output symbol table.
off_t
do_local_symbol_offset() const
{ gold_unreachable(); }
// Read the relocs.
void
do_read_relocs(Read_relocs_data*)
{ gold_unreachable(); }
// Process the relocs to find list of referenced sections. Used only
// during garbage collection.
void
do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*)
{ gold_unreachable(); }
// Scan the relocs and adjust the symbol table.
void
do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*)
{ gold_unreachable(); }
// Count the local symbols.
void
do_count_local_symbols(Stringpool_template<char>*,
Stringpool_template<char>*)
{ gold_unreachable(); }
// Finalize the local symbols.
unsigned int
do_finalize_local_symbols(unsigned int, off_t, Symbol_table*)
{ gold_unreachable(); }
// Set the offset where local dynamic symbol information will be stored.
unsigned int
do_set_local_dynsym_indexes(unsigned int)
{ gold_unreachable(); }
// Set the offset where local dynamic symbol information will be stored.
unsigned int
do_set_local_dynsym_offset(off_t)
{ gold_unreachable(); }
// Relocate the input sections and write out the local symbols.
void
do_relocate(const Symbol_table*, const Layout*, Output_file*)
{ gold_unreachable(); }
private:
// General access to the ELF file.
elfcpp::Elf_file<size, big_endian, Object> elf_file_;
};
// The output file.
// This class is responsible for collecting the debug index information
// and writing the .dwp file in ELF format.
class Dwp_output_file
{
public:
Dwp_output_file(const char* name)
: name_(name), machine_(0), size_(0), big_endian_(false), osabi_(0),
abiversion_(0), fd_(NULL), next_file_offset_(0), shnum_(1), sections_(),
section_id_map_(), shoff_(0), shstrndx_(0), have_strings_(false),
stringpool_(), shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
last_tu_slot_(0)
{
this->section_id_map_.resize(elfcpp::DW_SECT_MAX + 1);
this->stringpool_.set_no_zero_null();
}
// Record the target info from an input file.
void
record_target_info(const char* name, int machine, int size, bool big_endian,
int osabi, int abiversion);
// Add a string to the debug strings section.
section_offset_type
add_string(const char* str, size_t len);
// Add a section to the output file, and return the new section offset.
section_offset_type
add_contribution(elfcpp::DW_SECT section_id, const unsigned char* contents,
section_size_type len, int align);
// Add a set of .debug_info and related sections to the output file.
void
add_cu_set(Unit_set* cu_set);
// Lookup a type signature and return TRUE if we have already seen it.
bool
lookup_tu(uint64_t type_sig);
// Add a set of .debug_types and related sections to the output file.
void
add_tu_set(Unit_set* tu_set);
// Finalize the file, write the string tables and index sections,
// and close the file.
void
finalize();
private:
// Contributions to output sections.
struct Contribution
{
section_offset_type output_offset;
section_size_type size;
const unsigned char* contents;
};
// Sections in the output file.
struct Section
{
const char* name;
off_t offset;
section_size_type size;
int align;
std::vector<Contribution> contributions;
Section(const char* n, int a)
: name(n), offset(0), size(0), align(a), contributions()
{ }
};
// The index sections defined by the DWARF Package File Format spec.
class Dwp_index
{
public:
// Vector for the section table.
typedef std::vector<const Unit_set*> Section_table;
Dwp_index()
: capacity_(0), used_(0), hash_table_(NULL), section_table_(),
section_mask_(0)
{ }
~Dwp_index()
{ }
// Find a slot in the hash table for SIGNATURE. Return TRUE
// if the entry already exists.
bool
find_or_add(uint64_t signature, unsigned int* slotp);
// Enter a CU or TU set at the given SLOT in the hash table.
void
enter_set(unsigned int slot, const Unit_set* set);
// Return the contents of the given SLOT in the hash table of signatures.
uint64_t
hash_table(unsigned int slot) const
{ return this->hash_table_[slot]; }
// Return the contents of the given SLOT in the parallel table of
// shndx pool indexes.
uint32_t
index_table(unsigned int slot) const
{ return this->index_table_[slot]; }
// Return the total number of slots in the hash table.
unsigned int
hash_table_total_slots() const
{ return this->capacity_; }
// Return the number of used slots in the hash table.
unsigned int
hash_table_used_slots() const
{ return this->used_; }
// Return an iterator into the shndx pool.
Section_table::const_iterator
section_table() const
{ return this->section_table_.begin(); }
Section_table::const_iterator
section_table_end() const
{ return this->section_table_.end(); }
// Return the number of rows in the section table.
unsigned int
section_table_rows() const
{ return this->section_table_.size(); }
// Return the mask indicating which columns will be used
// in the section table.
int
section_table_cols() const
{ return this->section_mask_; }
private:
// Initialize the hash table.
void
initialize();
// Grow the hash table when we reach 2/3 capacity.
void
grow();
// The number of slots in the table, a power of 2 such that
// capacity > 3 * size / 2.
unsigned int capacity_;
// The current number of used slots in the hash table.
unsigned int used_;
// The storage for the hash table of signatures.
uint64_t* hash_table_;
// The storage for the parallel table of shndx pool indexes.
uint32_t* index_table_;
// The table of section offsets and sizes.
Section_table section_table_;
// Bit mask to indicate which debug sections are present in the file.
int section_mask_;
}; // End class Dwp_output_file::Dwp_index.
// Add a new output section and return the section index.
unsigned int
add_output_section(const char* section_name, int align);
// Write a new section to the output file.
void
write_new_section(const char* section_name, const unsigned char* contents,
section_size_type len, int align);
// Write the ELF header.
void
write_ehdr();
template<unsigned int size, bool big_endian>
void
sized_write_ehdr();
// Write a section header.
void
write_shdr(const char* name, unsigned int type, unsigned int flags,
uint64_t addr, off_t offset, section_size_type sect_size,
unsigned int link, unsigned int info,
unsigned int align, unsigned int ent_size);
template<unsigned int size, bool big_endian>
void
sized_write_shdr(const char* name, unsigned int type, unsigned int flags,
uint64_t addr, off_t offset, section_size_type sect_size,
unsigned int link, unsigned int info,
unsigned int align, unsigned int ent_size);
// Write the contributions to an output section.
void
write_contributions(const Section& sect);
// Write a CU or TU index section.
template<bool big_endian>
void
write_index(const char* sect_name, const Dwp_index& index);
// The output filename.
const char* name_;
// ELF header parameters.
int machine_;
int size_;
int big_endian_;
int osabi_;
int abiversion_;
// The output file descriptor.
FILE* fd_;
// Next available file offset.
off_t next_file_offset_;
// The number of sections.
unsigned int shnum_;
// Section table. The first entry is shndx 1.
std::vector<Section> sections_;
// Section id map. This maps a DW_SECT enum to an shndx.
std::vector<unsigned int> section_id_map_;
// File offset of the section header table.
off_t shoff_;
// Section index of the section string table.
unsigned int shstrndx_;
// TRUE if we have added any strings to the string pool.
bool have_strings_;
// String pool for the output .debug_str.dwo section.
Stringpool stringpool_;
// String pool for the .shstrtab section.
Stringpool shstrtab_;
// The compilation unit index.
Dwp_index cu_index_;
// The type unit index.
Dwp_index tu_index_;
// Cache of the last type signature looked up.
uint64_t last_type_sig_;
// Cache of the slot index for the last type signature.
unsigned int last_tu_slot_;
};
// A specialization of Dwarf_info_reader, for reading dwo_names from
// DWARF CUs.
class Dwo_name_info_reader : public Dwarf_info_reader
{
public:
Dwo_name_info_reader(Relobj* object, unsigned int shndx)
: Dwarf_info_reader(false, object, NULL, 0, shndx, 0, 0),
files_(NULL)
{ }
~Dwo_name_info_reader()
{ }
// Get the dwo_names from the DWARF compilation unit DIEs.
void
get_dwo_names(File_list* files)
{
this->files_ = files;
this->parse();
}
protected:
// Visit a compilation unit.
virtual void
visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
private:
// The list of files to populate.
File_list* files_;
};
// A specialization of Dwarf_info_reader, for reading DWARF CUs and TUs
// and adding them to the output file.
class Unit_reader : public Dwarf_info_reader
{
public:
Unit_reader(bool is_type_unit, Relobj* object, unsigned int shndx)
: Dwarf_info_reader(is_type_unit, object, NULL, 0, shndx, 0, 0),
output_file_(NULL), sections_(NULL)
{ }
~Unit_reader()
{ }
// Read the CUs or TUs and add them to the output file.
void
add_units(Dwp_output_file*, unsigned int debug_abbrev, Section_bounds*);
protected:
// Visit a compilation unit.
virtual void
visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
// Visit a type unit.
virtual void
visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
uint64_t signature, Dwarf_die*);
private:
Dwp_output_file* output_file_;
Section_bounds* sections_;
};
// Return the name of a DWARF .dwo section.
static const char*
get_dwarf_section_name(elfcpp::DW_SECT section_id)
{
static const char* dwarf_section_names[] = {
NULL, // unused
".debug_info.dwo", // DW_SECT_INFO = 1
".debug_types.dwo", // DW_SECT_TYPES = 2
".debug_abbrev.dwo", // DW_SECT_ABBREV = 3
".debug_line.dwo", // DW_SECT_LINE = 4
".debug_loc.dwo", // DW_SECT_LOC = 5
".debug_str_offsets.dwo", // DW_SECT_STR_OFFSETS = 6
".debug_macinfo.dwo", // DW_SECT_MACINFO = 7
".debug_macro.dwo", // DW_SECT_MACRO = 8
};
gold_assert(section_id > 0 && section_id <= elfcpp::DW_SECT_MAX);
return dwarf_section_names[section_id];
}
// Class Sized_relobj_dwo.
// Setup the section information.
template <int size, bool big_endian>
void
Sized_relobj_dwo<size, big_endian>::setup()
{
const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
const off_t shoff = this->elf_file_.shoff();
const unsigned int shnum = this->elf_file_.shnum();
this->set_shnum(shnum);
this->section_offsets().resize(shnum);
// Read the section headers.
const unsigned char* const pshdrs = this->get_view(shoff, shnum * shdr_size,
true, false);
// Read the section names.
const unsigned char* pshdrnames =
pshdrs + this->elf_file_.shstrndx() * shdr_size;
typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
this->error(_("section name section has wrong type: %u"),
static_cast<unsigned int>(shdrnames.get_sh_type()));
section_size_type section_names_size =
convert_to_section_size_type(shdrnames.get_sh_size());
const unsigned char* namesu = this->get_view(shdrnames.get_sh_offset(),
section_names_size, false,
false);
const char* names = reinterpret_cast<const char*>(namesu);
Compressed_section_map* compressed_sections =
build_compressed_section_map<size, big_endian>(
pshdrs, this->shnum(), names, section_names_size, this, true);
if (compressed_sections != NULL && !compressed_sections->empty())
this->set_compressed_sections(compressed_sections);
}
// Return a view of the contents of a section.
template <int size, bool big_endian>
const unsigned char*
Sized_relobj_dwo<size, big_endian>::do_section_contents(
unsigned int shndx,
section_size_type* plen,
bool cache)
{
Object::Location loc(this->elf_file_.section_contents(shndx));
*plen = convert_to_section_size_type(loc.data_size);
if (*plen == 0)
{
static const unsigned char empty[1] = { '\0' };
return empty;
}
return this->get_view(loc.file_offset, *plen, true, cache);
}
// Class Dwo_file.
Dwo_file::~Dwo_file()
{
if (this->obj_ != NULL)
delete this->obj_;
if (this->input_file_ != NULL)
delete this->input_file_;
}
// Read the input executable file and extract the list of .dwo files
// that it references.
void
Dwo_file::read_executable(File_list* files)
{
this->obj_ = this->make_object(NULL);
unsigned int shnum = this->shnum();
this->is_compressed_.resize(shnum);
this->sect_offsets_.resize(shnum);
unsigned int debug_info = 0;
unsigned int debug_abbrev = 0;
// Scan the section table and collect the debug sections we need.
// (Section index 0 is a dummy section; skip it.)
for (unsigned int i = 1; i < shnum; i++)
{
if (this->section_type(i) != elfcpp::SHT_PROGBITS)
continue;
std::string sect_name = this->section_name(i);
const char* suffix = sect_name.c_str();
if (is_prefix_of(".debug_", suffix))
suffix += 7;
else if (is_prefix_of(".zdebug_", suffix))
{
this->is_compressed_[i] = true;
suffix += 8;
}
else
continue;
if (strcmp(suffix, "info") == 0)
debug_info = i;
else if (strcmp(suffix, "abbrev") == 0)
debug_abbrev = i;
}
if (debug_info > 0)
{
Dwo_name_info_reader dwarf_reader(this->obj_, debug_info);
dwarf_reader.set_abbrev_shndx(debug_abbrev);
dwarf_reader.get_dwo_names(files);
}
}
// Read the input file and send its contents to OUTPUT_FILE.
void
Dwo_file::read(Dwp_output_file* output_file)
{
this->obj_ = this->make_object(output_file);
unsigned int shnum = this->shnum();
this->is_compressed_.resize(shnum);
this->sect_offsets_.resize(shnum);
typedef std::vector<unsigned int> Types_list;
Types_list debug_types;
unsigned int debug_shndx[elfcpp::DW_SECT_MAX + 1];
for (unsigned int i = 0; i <= elfcpp::DW_SECT_MAX; i++)
debug_shndx[i] = 0;
unsigned int debug_str = 0;
unsigned int debug_cu_index = 0;
unsigned int debug_tu_index = 0;
// Scan the section table and collect debug sections.
// (Section index 0 is a dummy section; skip it.)
for (unsigned int i = 1; i < shnum; i++)
{
if (this->section_type(i) != elfcpp::SHT_PROGBITS)
continue;
std::string sect_name = this->section_name(i);
const char* suffix = sect_name.c_str();
if (is_prefix_of(".debug_", suffix))
suffix += 7;
else if (is_prefix_of(".zdebug_", suffix))
{
this->is_compressed_[i] = true;
suffix += 8;
}
else
continue;
if (strcmp(suffix, "info.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_INFO] = i;
else if (strcmp(suffix, "types.dwo") == 0)
debug_types.push_back(i);
else if (strcmp(suffix, "abbrev.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_ABBREV] = i;
else if (strcmp(suffix, "line.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_LINE] = i;
else if (strcmp(suffix, "loc.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_LOC] = i;
else if (strcmp(suffix, "str.dwo") == 0)
debug_str = i;
else if (strcmp(suffix, "str_offsets.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_STR_OFFSETS] = i;
else if (strcmp(suffix, "macinfo.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_MACINFO] = i;
else if (strcmp(suffix, "macro.dwo") == 0)
debug_shndx[elfcpp::DW_SECT_MACRO] = i;
else if (strcmp(suffix, "cu_index") == 0)
debug_cu_index = i;
else if (strcmp(suffix, "tu_index") == 0)
debug_tu_index = i;
}
// Merge the input string table into the output string table.
this->add_strings(output_file, debug_str);
// If we found any .dwp index sections, read those and add the section
// sets to the output file.
if (debug_cu_index > 0 || debug_tu_index > 0)
{
if (debug_cu_index > 0)
this->read_unit_index(debug_cu_index, debug_shndx, output_file, false);
if (debug_tu_index > 0)
{
if (debug_types.size() > 1)
gold_fatal(_("%s: .dwp file must have no more than one "
".debug_types.dwo section"), this->name_);
if (debug_types.size() == 1)
debug_shndx[elfcpp::DW_SECT_TYPES] = debug_types[0];
else
debug_shndx[elfcpp::DW_SECT_TYPES] = 0;
this->read_unit_index(debug_tu_index, debug_shndx, output_file, true);
}
return;
}
// If we found no index sections, this is a .dwo file.
if (debug_shndx[elfcpp::DW_SECT_INFO] > 0)
this->add_unit_set(output_file, debug_shndx, false);
debug_shndx[elfcpp::DW_SECT_INFO] = 0;
for (Types_list::const_iterator tp = debug_types.begin();
tp != debug_types.end();
++tp)
{
debug_shndx[elfcpp::DW_SECT_TYPES] = *tp;
this->add_unit_set(output_file, debug_shndx, true);
}
}
// Verify a .dwp file given a list of .dwo files referenced by the
// corresponding executable file. Returns true if no problems
// were found.
bool
Dwo_file::verify(const File_list& files)
{
this->obj_ = this->make_object(NULL);
unsigned int shnum = this->shnum();
this->is_compressed_.resize(shnum);
this->sect_offsets_.resize(shnum);
unsigned int debug_cu_index = 0;
// Scan the section table and collect debug sections.
// (Section index 0 is a dummy section; skip it.)
for (unsigned int i = 1; i < shnum; i++)
{
if (this->section_type(i) != elfcpp::SHT_PROGBITS)