gold/
[deliverable/binutils-gdb.git] / gold / dwp.cc
CommitLineData
77429909
CC
1// dwp.cc -- DWARF packaging utility
2
3// Copyright 2012 Free Software Foundation, Inc.
4// Written by Cary Coutant <ccoutant@google.com>.
5
6// This file is part of dwp, the DWARF packaging utility.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "dwp.h"
24
25#include <cstdarg>
26#include <cstddef>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <cerrno>
31
32#include <vector>
33#include <algorithm>
34
35#include "getopt.h"
36#include "libiberty.h"
37
38#include "elfcpp.h"
39#include "elfcpp_file.h"
40#include "dirsearch.h"
41#include "fileread.h"
42#include "object.h"
43#include "compressed_output.h"
44#include "stringpool.h"
45#include "dwarf_reader.h"
46
47static void
48usage() ATTRIBUTE_NORETURN;
49
50namespace gold {
51
52class Dwp_output_file;
53
54// An input file.
55// This class may represent either a .dwo file or a .dwp file
56// produced by an earlier run.
57
58template <int size, bool big_endian>
59class Sized_relobj_dwo;
60
61class Dwo_file
62{
63 public:
64 Dwo_file(const char* name)
65 : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(),
66 str_offset_map_()
67 { }
68
69 ~Dwo_file();
70
71 // Read the input file and send its contents to OUTPUT_FILE.
72 void
73 read(Dwp_output_file* output_file);
74
75 private:
76 // Types for mapping input string offsets to output string offsets.
77 typedef std::pair<section_offset_type, section_offset_type>
78 Str_offset_map_entry;
79 typedef std::vector<Str_offset_map_entry> Str_offset_map;
80
81 // A less-than comparison routine for Str_offset_map.
82 struct Offset_compare
83 {
84 bool
85 operator()(const Str_offset_map_entry& i1,
86 const Str_offset_map_entry& i2) const
87 { return i1.first < i2.first; }
88 };
89
90 // Create a Sized_relobj_dwo of the given size and endianness,
91 // and record the target info. P is a pointer to the ELF header
92 // in memory.
93 Relobj*
94 make_object(int size, bool big_endian, const unsigned char* p,
95 Input_file* input_file, Dwp_output_file* output_file);
96
97 template <int size, bool big_endian>
98 Relobj*
99 sized_make_object(const unsigned char* p, Input_file* input_file,
100 Dwp_output_file* output_file);
101
102 // Return the number of sections in the input object file.
103 unsigned int
104 shnum() const
105 { return this->obj_->shnum(); }
106
107 // Return section type.
108 unsigned int
109 section_type(unsigned int shndx)
110 { return this->obj_->section_type(shndx); }
111
112 // Get the name of a section.
113 std::string
114 section_name(unsigned int shndx)
115 { return this->obj_->section_name(shndx); }
116
117 // Return a view of the contents of a section, decompressed if necessary.
118 // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
119 // deleted by the caller.
120 const unsigned char*
121 section_contents(unsigned int shndx, section_size_type* plen, bool* is_new)
122 { return this->obj_->decompressed_section_contents(shndx, plen, is_new); }
123
124 // Read the .debug_cu_index section of a .dwp file,
125 // and process the CU sets.
126 void
127 read_compunit_index(unsigned int, Dwp_output_file*);
128
129 template <bool big_endian>
130 void
131 sized_read_compunit_index(unsigned int, Dwp_output_file*);
132
133 // Read the .debug_tu_index section of a .dwp file,
134 // and process the TU sets.
135 void
136 read_typeunit_index(unsigned int, Dwp_output_file*);
137
138 template <bool big_endian>
139 void
140 sized_read_typeunit_index(unsigned int, Dwp_output_file*);
141
142 // Merge the input string table section into the output file.
143 void
144 add_strings(Dwp_output_file*, unsigned int);
145
146 // Copy a section from the input file to the output file.
147 unsigned int
148 copy_section(Dwp_output_file* output_file, unsigned int shndx,
149 const char* section_name, bool is_str_offsets);
150
151 // Remap the string offsets in the .debug_str_offsets.dwo section.
152 const unsigned char*
153 remap_str_offsets(const unsigned char* contents, section_size_type len);
154
155 template <bool big_endian>
156 const unsigned char*
157 sized_remap_str_offsets(const unsigned char* contents, section_size_type len);
158
159 // Remap a single string offsets from an offset in the input string table
160 // to an offset in the output string table.
161 unsigned int
162 remap_str_offset(unsigned int val);
163
164 // Add a set of .debug_info and related sections to OUTPUT_FILE.
165 void
166 add_cu_set(Dwp_output_file* output_file,
167 uint64_t dwo_id,
168 unsigned int debug_info,
169 unsigned int debug_abbrev,
170 unsigned int debug_line,
171 unsigned int debug_loc,
172 unsigned int debug_str_offsets,
173 unsigned int debug_macinfo,
174 unsigned int debug_macro);
175
176 // Add a set of .debug_types and related sections to OUTPUT_FILE.
177 void
178 add_tu_set(Dwp_output_file* output_file,
179 uint64_t type_sig,
180 unsigned int debug_types,
181 unsigned int debug_abbrev,
182 unsigned int debug_line,
183 unsigned int debug_str_offsets);
184
185 // The filename.
186 const char* name_;
187 // The ELF file, represented as a gold Relobj instance.
188 Relobj* obj_;
189 // The Input_file object.
190 Input_file* input_file_;
191 // Flags indicating which sections are compressed.
192 std::vector<bool> is_compressed_;
193 // Map input section index onto output section index.
194 std::vector<unsigned int> shndx_map_;
195 // Map input string offsets to output string offsets.
196 Str_offset_map str_offset_map_;
197};
198
199// An ELF input file.
200// We derive from Sized_relobj so that we can use interfaces
201// in libgold to access the file.
202
203template <int size, bool big_endian>
204class Sized_relobj_dwo : public Sized_relobj<size, big_endian>
205{
206 public:
207 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
208 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
209
210 Sized_relobj_dwo(const char* name, Input_file* input_file,
211 const elfcpp::Ehdr<size, big_endian>& ehdr)
212 : Sized_relobj<size, big_endian>(name, input_file),
213 elf_file_(this, ehdr)
214 { }
215
216 ~Sized_relobj_dwo()
217 { }
218
219 // Setup the section information.
220 void
221 setup();
222
223 protected:
224 // Return section type.
225 unsigned int
226 do_section_type(unsigned int shndx)
227 { return this->elf_file_.section_type(shndx); }
228
229 // Get the name of a section.
230 std::string
231 do_section_name(unsigned int shndx)
232 { return this->elf_file_.section_name(shndx); }
233
234 // Get the size of a section.
235 uint64_t
236 do_section_size(unsigned int shndx)
237 { return this->elf_file_.section_size(shndx); }
238
239 // Return a view of the contents of a section.
240 const unsigned char*
241 do_section_contents(unsigned int, section_size_type*, bool);
242
243 // Return a view of the uncompressed contents of a section. Set *PLEN
244 // to the size. Set *IS_NEW to true if the contents need to be deleted
245 // by the caller.
246 const unsigned char*
247 do_decompressed_section_contents(unsigned int shndx,
248 section_size_type* plen,
249 bool* is_new);
250
251 // The following virtual functions are abstract in the base classes,
252 // but are not used here.
253
254 // Read the symbols.
255 void
256 do_read_symbols(Read_symbols_data*)
257 { gold_unreachable(); }
258
259 // Lay out the input sections.
260 void
261 do_layout(Symbol_table*, Layout*, Read_symbols_data*)
262 { gold_unreachable(); }
263
264 // Layout sections whose layout was deferred while waiting for
265 // input files from a plugin.
266 void
267 do_layout_deferred_sections(Layout*)
268 { gold_unreachable(); }
269
270 // Add the symbols to the symbol table.
271 void
272 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*)
273 { gold_unreachable(); }
274
275 Archive::Should_include
276 do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*,
277 std::string*)
278 { gold_unreachable(); }
279
280 // Iterate over global symbols, calling a visitor class V for each.
281 void
282 do_for_all_global_symbols(Read_symbols_data*,
283 Library_base::Symbol_visitor_base*)
284 { gold_unreachable(); }
285
286 // Return section flags.
287 uint64_t
288 do_section_flags(unsigned int)
289 { gold_unreachable(); }
290
291 // Return section entsize.
292 uint64_t
293 do_section_entsize(unsigned int)
294 { gold_unreachable(); }
295
296 // Return section address.
297 uint64_t
298 do_section_address(unsigned int)
299 { gold_unreachable(); }
300
301 // Return the section link field.
302 unsigned int
303 do_section_link(unsigned int)
304 { gold_unreachable(); }
305
306 // Return the section link field.
307 unsigned int
308 do_section_info(unsigned int)
309 { gold_unreachable(); }
310
311 // Return the section alignment.
312 uint64_t
313 do_section_addralign(unsigned int)
314 { gold_unreachable(); }
315
316 // Return the Xindex structure to use.
317 Xindex*
318 do_initialize_xindex()
319 { gold_unreachable(); }
320
321 // Get symbol counts.
322 void
323 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const
324 { gold_unreachable(); }
325
326 // Get global symbols.
327 const Symbols*
328 do_get_global_symbols() const
329 { return NULL; }
330
331 // Return the value of a local symbol.
332 uint64_t
333 do_local_symbol_value(unsigned int, uint64_t) const
334 { gold_unreachable(); }
335
336 unsigned int
337 do_local_plt_offset(unsigned int) const
338 { gold_unreachable(); }
339
340 // Return whether local symbol SYMNDX is a TLS symbol.
341 bool
342 do_local_is_tls(unsigned int) const
343 { gold_unreachable(); }
344
345 // Return the number of local symbols.
346 unsigned int
347 do_local_symbol_count() const
348 { gold_unreachable(); }
349
350 // Return the number of local symbols in the output symbol table.
351 unsigned int
352 do_output_local_symbol_count() const
353 { gold_unreachable(); }
354
355 // Return the file offset for local symbols in the output symbol table.
356 off_t
357 do_local_symbol_offset() const
358 { gold_unreachable(); }
359
360 // Read the relocs.
361 void
362 do_read_relocs(Read_relocs_data*)
363 { gold_unreachable(); }
364
365 // Process the relocs to find list of referenced sections. Used only
366 // during garbage collection.
367 void
368 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*)
369 { gold_unreachable(); }
370
371 // Scan the relocs and adjust the symbol table.
372 void
373 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*)
374 { gold_unreachable(); }
375
376 // Count the local symbols.
377 void
378 do_count_local_symbols(Stringpool_template<char>*,
379 Stringpool_template<char>*)
380 { gold_unreachable(); }
381
382 // Finalize the local symbols.
383 unsigned int
384 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*)
385 { gold_unreachable(); }
386
387 // Set the offset where local dynamic symbol information will be stored.
388 unsigned int
389 do_set_local_dynsym_indexes(unsigned int)
390 { gold_unreachable(); }
391
392 // Set the offset where local dynamic symbol information will be stored.
393 unsigned int
394 do_set_local_dynsym_offset(off_t)
395 { gold_unreachable(); }
396
397 // Relocate the input sections and write out the local symbols.
398 void
399 do_relocate(const Symbol_table*, const Layout*, Output_file*)
400 { gold_unreachable(); }
401
402 private:
403 // General access to the ELF file.
404 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
405};
406
407// The output file.
408// This class is responsible for collecting the debug index information
409// and writing the .dwp file in ELF format.
410
411class Dwp_output_file
412{
413 public:
414 Dwp_output_file(const char* name)
415 : name_(name), machine_(0), size_(0), big_endian_(false), osabi_(0),
416 abiversion_(0), fd_(NULL), next_file_offset_(0), shnum_(1), sections_(),
417 shoff_(0), shstrndx_(0), have_strings_(false), stringpool_(),
418 shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
419 last_tu_slot_(0)
420 {
421 this->stringpool_.set_no_zero_null();
422 }
423
424 // Record the target info from an input file.
425 void
426 record_target_info(const char* name, int machine, int size, bool big_endian,
427 int osabi, int abiversion);
428
429 // Add a string to the debug strings section.
430 section_offset_type
431 add_string(const char* str, size_t len);
432
433 // Add a section to the output file, and return the new section index.
434 unsigned int
435 add_section(const char* section_name, const unsigned char* contents,
436 section_size_type len, int align);
437
438 // Add a set of .debug_info and related sections to the output file.
439 void
440 add_cu_set(uint64_t dwo_id, unsigned int debug_info,
441 unsigned int debug_abbrev, unsigned int debug_line,
442 unsigned int debug_loc, unsigned int debug_str_offsets,
443 unsigned int debug_macinfo, unsigned int debug_macro);
444
445 // Lookup a type signature and return TRUE if we have already seen it.
446 bool
447 lookup_tu(uint64_t type_sig);
448
449 // Add a set of .debug_types and related sections to the output file.
450 void
451 add_tu_set(uint64_t type_sig, unsigned int debug_types,
452 unsigned int debug_abbrev, unsigned int debug_line,
453 unsigned int debug_str_offsets);
454
455 // Finalize the file, write the string tables and index sections,
456 // and close the file.
457 void
458 finalize();
459
460 private:
461 // Sections in the output file.
462 struct Section
463 {
464 const char* name;
465 off_t offset;
466 section_size_type size;
467 int align;
468 };
469
470 // A set of sections for a compilation unit or type unit.
471 struct Cu_or_tu_set
472 {
473 uint64_t signature;
474 unsigned int debug_info_or_types;
475 unsigned int debug_abbrev;
476 unsigned int debug_line;
477 unsigned int debug_loc;
478 unsigned int debug_str_offsets;
479 unsigned int debug_macinfo;
480 unsigned int debug_macro;
481 };
482
483 // The index sections defined by the DWARF Package File Format spec.
484 class Dwp_index
485 {
486 public:
487 // Vector for the section index pool.
488 typedef std::vector<unsigned int> Shndx_pool;
489
490 Dwp_index()
491 : capacity_(0), used_(0), hash_table_(NULL), shndx_pool_()
492 { }
493
494 ~Dwp_index()
495 { }
496
497 // Find a slot in the hash table for SIGNATURE. Return TRUE
498 // if the entry already exists.
499 bool
500 find_or_add(uint64_t signature, unsigned int* slotp);
501
502 // Enter a CU or TU set at the given SLOT in the hash table.
503 void
504 enter_set(unsigned int slot, const Cu_or_tu_set& set);
505
506 // Return the contents of the given SLOT in the hash table of signatures.
507 uint64_t
508 hash_table(unsigned int slot) const
509 { return this->hash_table_[slot]; }
510
511 // Return the contents of the given SLOT in the parallel table of
512 // shndx pool indexes.
513 uint32_t
514 index_table(unsigned int slot) const
515 { return this->index_table_[slot]; }
516
517 // Return the total number of slots in the hash table.
518 unsigned int
519 hash_table_total_slots() const
520 { return this->capacity_; }
521
522 // Return the number of used slots in the hash table.
523 unsigned int
524 hash_table_used_slots() const
525 { return this->used_; }
526
527 // Return an iterator into the shndx pool.
528 Shndx_pool::const_iterator
529 shndx_pool() const
530 { return this->shndx_pool_.begin(); }
531
532 Shndx_pool::const_iterator
533 shndx_pool_end() const
534 { return this->shndx_pool_.end(); }
535
536 // Return the number of entries in the shndx pool.
537 unsigned int
538 shndx_pool_size() const
539 { return this->shndx_pool_.size(); }
540
541 private:
542 // Initialize the hash table.
543 void
544 initialize();
545
546 // Grow the hash table when we reach 2/3 capacity.
547 void
548 grow();
549
550 // The number of slots in the table, a power of 2 such that
551 // capacity > 3 * size / 2.
552 unsigned int capacity_;
553 // The current number of used slots in the hash table.
554 unsigned int used_;
555 // The storage for the hash table of signatures.
556 uint64_t* hash_table_;
557 // The storage for the parallel table of shndx pool indexes.
558 uint32_t* index_table_;
559 // The pool of section indexes.
560 Shndx_pool shndx_pool_;
561 }; // End class Dwp_output_file::Dwp_index.
562
563 // Initialize the output file.
564 void
565 initialize();
566
567 // Write the ELF header.
568 void
569 write_ehdr();
570
571 template<unsigned int size, bool big_endian>
572 void
573 sized_write_ehdr();
574
575 // Write a section header.
576 void
577 write_shdr(const char* name, unsigned int type, unsigned int flags,
578 uint64_t addr, off_t offset, section_size_type sect_size,
579 unsigned int link, unsigned int info,
580 unsigned int align, unsigned int ent_size);
581
582 template<unsigned int size, bool big_endian>
583 void
584 sized_write_shdr(const char* name, unsigned int type, unsigned int flags,
585 uint64_t addr, off_t offset, section_size_type sect_size,
586 unsigned int link, unsigned int info,
587 unsigned int align, unsigned int ent_size);
588
589 // Write a CU or TU index section.
590 template<bool big_endian>
591 void
592 write_index(const char* sect_name, const Dwp_index& index);
593
594 // The output filename.
595 const char* name_;
596 // ELF header parameters.
597 int machine_;
598 int size_;
599 int big_endian_;
600 int osabi_;
601 int abiversion_;
602 // The output file descriptor.
603 FILE* fd_;
604 // Next available file offset.
605 off_t next_file_offset_;
606 // The number of sections.
607 unsigned int shnum_;
608 // Section table. The first entry is shndx 1.
609 std::vector<Section> sections_;
610 // File offset of the section header table.
611 off_t shoff_;
612 // Section index of the section string table.
613 unsigned int shstrndx_;
614 // TRUE if we have added any strings to the string pool.
615 bool have_strings_;
616 // String pool for the output .debug_str.dwo section.
617 Stringpool stringpool_;
618 // String pool for the .shstrtab section.
619 Stringpool shstrtab_;
620 // The compilation unit index.
621 Dwp_index cu_index_;
622 // The type unit index.
623 Dwp_index tu_index_;
624 // Cache of the last type signature looked up.
625 uint64_t last_type_sig_;
626 // Cache of the slot index for the last type signature.
627 unsigned int last_tu_slot_;
628};
629
630// A specialization of Dwarf_info_reader, for reading dwo_ids and
631// type signatures from DWARF CUs and TUs.
632
633class Dwo_id_info_reader : public Dwarf_info_reader
634{
635 public:
636 Dwo_id_info_reader(bool is_type_unit,
637 Relobj* object,
638 const unsigned char* symbols,
639 off_t symbols_size,
640 unsigned int shndx,
641 unsigned int reloc_shndx,
642 unsigned int reloc_type)
643 : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
644 reloc_shndx, reloc_type),
645 dwo_id_found_(false), dwo_id_(0), type_sig_found_(false), type_sig_(0)
646 { }
647
648 ~Dwo_id_info_reader()
649 { }
650
651 // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID.
652 bool
653 get_dwo_id(uint64_t* dwo_id)
654 {
655 this->parse();
656 if (!this->dwo_id_found_)
657 return false;
658 *dwo_id = this->dwo_id_;
659 return true;
660 }
661
662 // Return the type signature from a DWARF type unit DIE in *TYPE_SIG.
663 bool
664 get_type_sig(uint64_t* type_sig)
665 {
666 this->parse();
667 if (!this->type_sig_found_)
668 return false;
669 *type_sig = this->type_sig_;
670 return true;
671 }
672
673 protected:
674 // Visit a compilation unit.
675 virtual void
676 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
677
678 // Visit a type unit.
679 virtual void
680 visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature,
681 Dwarf_die*);
682
683 private:
684 // Visit a top-level DIE.
685 void
686 visit_top_die(Dwarf_die* die);
687
688 // TRUE if we found a dwo_id.
689 bool dwo_id_found_;
690 // The dwo_id.
691 uint64_t dwo_id_;
692 // TRUE if we found a type signature.
693 bool type_sig_found_;
694 // The type signature.
695 uint64_t type_sig_;
696};
697
698// Class Sized_relobj_dwo.
699
700// Setup the section information.
701
702template <int size, bool big_endian>
703void
704Sized_relobj_dwo<size, big_endian>::setup()
705{
706 const unsigned int shnum = this->elf_file_.shnum();
707 this->set_shnum(shnum);
708 this->section_offsets().resize(shnum);
709}
710
711// Return a view of the contents of a section.
712
713template <int size, bool big_endian>
714const unsigned char*
715Sized_relobj_dwo<size, big_endian>::do_section_contents(
716 unsigned int shndx,
717 section_size_type* plen,
718 bool cache)
719{
720 Object::Location loc(this->elf_file_.section_contents(shndx));
721 *plen = convert_to_section_size_type(loc.data_size);
722 if (*plen == 0)
723 {
724 static const unsigned char empty[1] = { '\0' };
725 return empty;
726 }
727 return this->get_view(loc.file_offset, *plen, true, cache);
728}
729
730// Return a view of the uncompressed contents of a section. Set *PLEN
731// to the size. Set *IS_NEW to true if the contents need to be deleted
732// by the caller.
733
734template <int size, bool big_endian>
735const unsigned char*
736Sized_relobj_dwo<size, big_endian>::do_decompressed_section_contents(
737 unsigned int shndx,
738 section_size_type* plen,
739 bool* is_new)
740{
741 section_size_type buffer_size;
742 const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
743 false);
744
745 std::string sect_name = this->do_section_name(shndx);
746 if (!is_prefix_of(".zdebug_", sect_name.c_str()))
747 {
748 *plen = buffer_size;
749 *is_new = false;
750 return buffer;
751 }
752
753 section_size_type uncompressed_size = get_uncompressed_size(buffer,
754 buffer_size);
755 unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
756 if (!decompress_input_section(buffer,
757 buffer_size,
758 uncompressed_data,
759 uncompressed_size))
760 this->error(_("could not decompress section %s"),
761 this->section_name(shndx).c_str());
762 *plen = uncompressed_size;
763 *is_new = true;
764 return uncompressed_data;
765}
766
767// Class Dwo_file.
768
769Dwo_file::~Dwo_file()
770{
771 if (this->input_file_ != NULL)
772 delete this->input_file_;
773 if (this->obj_ != NULL)
774 delete this->obj_;
775}
776
777// Read the input file and send its contents to OUTPUT_FILE.
778
779void
780Dwo_file::read(Dwp_output_file* output_file)
781{
782 // Open the input file.
783 this->input_file_ = new Input_file(this->name_);
784 Dirsearch dirpath;
785 int index;
786 if (!this->input_file_->open(dirpath, NULL, &index))
787 gold_fatal(_("%s: can't open"), this->name_);
788
789 // Check that it's an ELF file.
790 off_t filesize = this->input_file_->file().filesize();
791 int hdrsize = elfcpp::Elf_recognizer::max_header_size;
792 if (filesize < hdrsize)
793 hdrsize = filesize;
794 const unsigned char* p =
795 this->input_file_->file().get_view(0, 0, hdrsize, true, false);
796 if (!elfcpp::Elf_recognizer::is_elf_file(p, hdrsize))
797 gold_fatal(_("%s: not an ELF object file"), this->name_);
798
799 // Get the size, endianness, machine, etc. info from the header,
800 // make an appropriately-sized Relobj, and pass the target info
801 // to the output object.
802 int size;
803 bool big_endian;
804 std::string error;
805 if (!elfcpp::Elf_recognizer::is_valid_header(p, hdrsize, &size,
806 &big_endian, &error))
807 gold_fatal(_("%s: %s"), this->name_, error.c_str());
808
809 this->obj_ = this->make_object(size, big_endian, p, this->input_file_,
810 output_file);
811
812 unsigned int shnum = this->shnum();
813 this->is_compressed_.resize(shnum);
814 this->shndx_map_.resize(shnum);
815
816 typedef std::vector<unsigned int> Types_list;
817 Types_list debug_types;
818 unsigned int debug_info = 0;
819 unsigned int debug_abbrev = 0;
820 unsigned int debug_line = 0;
821 unsigned int debug_loc = 0;
822 unsigned int debug_str = 0;
823 unsigned int debug_str_offsets = 0;
824 unsigned int debug_macinfo = 0;
825 unsigned int debug_macro = 0;
826 unsigned int debug_cu_index = 0;
827 unsigned int debug_tu_index = 0;
828
829 // Scan the section table and look for .dwp index sections.
830 // (Section index 0 is a dummy section; skip it.)
831 for (unsigned int i = 1; i < shnum; i++)
832 {
833 if (this->section_type(i) != elfcpp::SHT_PROGBITS)
834 continue;
835 std::string sect_name = this->section_name(i);
836 const char* suffix = sect_name.c_str();
837 if (is_prefix_of(".debug_", suffix))
838 suffix += 7;
839 else if (is_prefix_of(".zdebug_", suffix))
840 {
841 this->is_compressed_[i] = true;
842 suffix += 8;
843 }
844 else
845 continue;
846 if (strcmp(suffix, "cu_index") == 0)
847 debug_cu_index = i;
848 else if (strcmp(suffix, "tu_index") == 0)
849 debug_tu_index = i;
850 else if (strcmp(suffix, "str.dwo") == 0)
851 debug_str = i;
852 }
853
854 // Merge the input string table into the output string table.
855 this->add_strings(output_file, debug_str);
856
857 // If we found any .dwp index sections, read those and add the section
858 // sets to the output file.
859 if (debug_cu_index > 0 || debug_tu_index > 0)
860 {
861 if (debug_cu_index > 0)
862 this->read_compunit_index(debug_cu_index, output_file);
863 if (debug_tu_index > 0)
864 this->read_typeunit_index(debug_tu_index, output_file);
865 return;
866 }
867
868 // If we found no index sections, this is a .dwo file.
869 // Scan the section table and collect the debug sections.
870 for (unsigned int i = 1; i < shnum; i++)
871 {
872 if (this->section_type(i) != elfcpp::SHT_PROGBITS)
873 continue;
874 std::string sect_name = this->section_name(i);
875 const char* suffix = sect_name.c_str();
876 if (is_prefix_of(".debug_", suffix))
877 suffix += 7;
878 else if (is_prefix_of(".zdebug_", suffix))
879 suffix += 8;
880 else
881 continue;
882 // TODO: Check for one of each section (except .debug_types).
883 if (strcmp(suffix, "info.dwo") == 0)
884 debug_info = i;
885 else if (strcmp(suffix, "types.dwo") == 0)
886 debug_types.push_back(i);
887 else if (strcmp(suffix, "abbrev.dwo") == 0)
888 debug_abbrev = i;
889 else if (strcmp(suffix, "line.dwo") == 0)
890 debug_line = i;
891 else if (strcmp(suffix, "loc.dwo") == 0)
892 debug_loc = i;
893 else if (strcmp(suffix, "str_offsets.dwo") == 0)
894 debug_str_offsets = i;
895 else if (strcmp(suffix, "macinfo.dwo") == 0)
896 debug_macinfo = i;
897 else if (strcmp(suffix, "macro.dwo") == 0)
898 debug_macro = i;
899 }
900
901 if (debug_info > 0)
902 {
903 // Extract the dwo_id from .debug_info.dwo section.
904 uint64_t dwo_id;
905 Dwo_id_info_reader dwarf_reader(false, this->obj_, NULL, 0, debug_info,
906 0, 0);
907 dwarf_reader.set_abbrev_shndx(debug_abbrev);
908 if (!dwarf_reader.get_dwo_id(&dwo_id))
909 gold_fatal(_("%s: .debug_info.dwo section does not have DW_AT_GNU_dwo_id "
910 "attribute"), this->name_);
911 this->add_cu_set(output_file, dwo_id, debug_info, debug_abbrev,
912 debug_line, debug_loc, debug_str_offsets,
913 debug_macinfo, debug_macro);
914 }
915
916 for (Types_list::const_iterator tp = debug_types.begin();
917 tp != debug_types.end();
918 ++tp)
919 {
920 // Extract the type signature from .debug_types.dwo section.
921 uint64_t type_sig;
922 gold_assert(*tp > 0);
923 Dwo_id_info_reader dwarf_reader(true, this->obj_, NULL, 0, *tp, 0, 0);
924 dwarf_reader.set_abbrev_shndx(debug_abbrev);
925 if (!dwarf_reader.get_type_sig(&type_sig))
926 gold_fatal(_("%s: .debug_types.dwo section does not have type signature"),
927 this->name_);
928 this->add_tu_set(output_file, type_sig, *tp, debug_abbrev, debug_line,
929 debug_str_offsets);
930 }
931}
932
933// Create a Sized_relobj_dwo of the given size and endianness,
934// and record the target info. P is a pointer to the ELF header
935// in memory.
936
937Relobj*
938Dwo_file::make_object(int size, bool big_endian, const unsigned char* p,
939 Input_file* input_file, Dwp_output_file* output_file)
940{
941 if (size == 32)
942 {
943 if (big_endian)
944#ifdef HAVE_TARGET_32_BIG
945 return this->sized_make_object<32, true>(p, input_file, output_file);
946#else
947 gold_unreachable();
948#endif
949 else
950#ifdef HAVE_TARGET_32_LITTLE
951 return this->sized_make_object<32, false>(p, input_file, output_file);
952#else
953 gold_unreachable();
954#endif
955 }
956 else if (size == 64)
957 {
958 if (big_endian)
959#ifdef HAVE_TARGET_64_BIG
960 return this->sized_make_object<64, true>(p, input_file, output_file);
961#else
962 gold_unreachable();
963#endif
964 else
965#ifdef HAVE_TARGET_64_LITTLE
966 return this->sized_make_object<64, false>(p, input_file, output_file);
967#else
968 gold_unreachable();
969#endif
970 }
971 else
972 gold_unreachable();
973}
974
975// Function template to create a Sized_relobj_dwo and record the target info.
976// P is a pointer to the ELF header in memory.
977
978template <int size, bool big_endian>
979Relobj*
980Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file,
981 Dwp_output_file* output_file)
982{
983 elfcpp::Ehdr<size, big_endian> ehdr(p);
984 Sized_relobj_dwo<size, big_endian>* obj =
985 new Sized_relobj_dwo<size, big_endian>(this->name_, input_file, ehdr);
986 obj->setup();
987 output_file->record_target_info(
988 this->name_, ehdr.get_e_machine(), size, big_endian,
989 ehdr.get_e_ident()[elfcpp::EI_OSABI],
990 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
991 return obj;
992}
993
994// Read the .debug_cu_index section of a .dwp file,
995// and process the CU sets.
996
997void
998Dwo_file::read_compunit_index(unsigned int shndx, Dwp_output_file* output_file)
999{
1000 if (this->obj_->is_big_endian())
1001 this->sized_read_compunit_index<true>(shndx, output_file);
1002 else
1003 this->sized_read_compunit_index<false>(shndx, output_file);
1004}
1005
1006template <bool big_endian>
1007void
1008Dwo_file::sized_read_compunit_index(unsigned int shndx,
1009 Dwp_output_file* output_file)
1010{
1011 section_size_type len;
1012 bool is_new;
1013 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1014
1015 unsigned int version =
1016 elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
1017 if (version != 1)
1018 gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"),
1019 this->name_, version);
1020
1021 unsigned int nused =
1022 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1023 + 2 * sizeof(uint32_t));
1024 if (nused == 0)
1025 return;
1026
1027 unsigned int nslots =
1028 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1029 + 3 * sizeof(uint32_t));
1030
1031 const unsigned char* phash = contents + 4 * sizeof(uint32_t);
1032 const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
1033 const unsigned char* shndx_pool = pindex + nslots * sizeof(uint32_t);
1034 const unsigned char* limit = contents + len;
1035
1036 if (shndx_pool >= limit)
1037 gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_);
1038
1039 // Loop over the slots of the hash table.
1040 for (unsigned int i = 0; i < nslots; ++i)
1041 {
1042 uint64_t dwo_id =
1043 elfcpp::Swap_unaligned<64, big_endian>::readval(phash);
1044 if (dwo_id != 0)
1045 {
1046 unsigned int index =
1047 elfcpp::Swap_unaligned<32, big_endian>::readval(pindex);
1048 const unsigned char* shndx_list =
1049 shndx_pool + index * sizeof(uint32_t);
1050
1051 // Collect the debug sections for this compilation unit set.
1052 unsigned int debug_info = 0;
1053 unsigned int debug_abbrev = 0;
1054 unsigned int debug_line = 0;
1055 unsigned int debug_loc = 0;
1056 unsigned int debug_str_offsets = 0;
1057 unsigned int debug_macinfo = 0;
1058 unsigned int debug_macro = 0;
1059 for (;;)
1060 {
1061 if (shndx_list >= limit)
1062 gold_fatal(_("%s: .debug_cu_index is corrupt"),
1063 this->name_);
1064 unsigned int shndx =
1065 elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list);
1066 if (shndx == 0)
1067 break;
1068 if (shndx > this->shnum())
1069 gold_fatal(_("%s: .debug_cu_index has bad shndx"),
1070 this->name_);
1071 std::string sect_name = this->section_name(shndx);
1072 const char* suffix = sect_name.c_str();
1073 if (is_prefix_of(".debug_", suffix))
1074 suffix += 7;
1075 else if (is_prefix_of(".zdebug_", suffix))
1076 suffix += 8;
1077 else
1078 gold_fatal(_("%s: .debug_cu_index refers to "
1079 "non-debug section"), this->name_);
1080 if (strcmp(suffix, "info.dwo") == 0)
1081 debug_info = shndx;
1082 else if (strcmp(suffix, "abbrev.dwo") == 0)
1083 debug_abbrev = shndx;
1084 else if (strcmp(suffix, "line.dwo") == 0)
1085 debug_line = shndx;
1086 else if (strcmp(suffix, "loc.dwo") == 0)
1087 debug_loc = shndx;
1088 else if (strcmp(suffix, "str_offsets.dwo") == 0)
1089 debug_str_offsets = shndx;
1090 else if (strcmp(suffix, "macinfo.dwo") == 0)
1091 debug_macinfo = shndx;
1092 else if (strcmp(suffix, "macro.dwo") == 0)
1093 debug_macro = shndx;
1094 shndx_list += sizeof(uint32_t);
1095 }
1096 this->add_cu_set(output_file, dwo_id, debug_info, debug_abbrev,
1097 debug_line, debug_loc, debug_str_offsets,
1098 debug_macinfo, debug_macro);
1099 }
1100 phash += sizeof(uint64_t);
1101 pindex += sizeof(uint32_t);
1102 }
1103
1104 if (is_new)
1105 delete[] contents;
1106}
1107
1108// Read the .debug_tu_index section of a .dwp file,
1109// and process the TU sets.
1110
1111void
1112Dwo_file::read_typeunit_index(unsigned int shndx, Dwp_output_file* output_file)
1113{
1114 if (this->obj_->is_big_endian())
1115 this->sized_read_typeunit_index<true>(shndx, output_file);
1116 else
1117 this->sized_read_typeunit_index<false>(shndx, output_file);
1118}
1119
1120template <bool big_endian>
1121void
1122Dwo_file::sized_read_typeunit_index(unsigned int shndx,
1123 Dwp_output_file* output_file)
1124{
1125 section_size_type len;
1126 bool is_new;
1127 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1128
1129 unsigned int version =
1130 elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
1131 if (version != 1)
1132 gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"),
1133 this->name_, version);
1134
1135 unsigned int nused =
1136 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1137 + 2 * sizeof(uint32_t));
1138 if (nused == 0)
1139 return;
1140
1141 unsigned int nslots =
1142 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1143 + 3 * sizeof(uint32_t));
1144
1145 const unsigned char* phash = contents + 4 * sizeof(uint32_t);
1146 const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
1147 const unsigned char* shndx_pool = pindex + nslots * sizeof(uint32_t);
1148 const unsigned char* limit = contents + len;
1149
1150 if (shndx_pool >= limit)
1151 gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_);
1152
1153 // Loop over the slots of the hash table.
1154 for (unsigned int i = 0; i < nslots; ++i)
1155 {
1156 uint64_t type_sig =
1157 elfcpp::Swap_unaligned<64, big_endian>::readval(phash);
1158 if (type_sig != 0)
1159 {
1160 unsigned int index =
1161 elfcpp::Swap_unaligned<32, big_endian>::readval(pindex);
1162 const unsigned char* shndx_list =
1163 shndx_pool + index * sizeof(uint32_t);
1164
1165 // Collect the debug sections for this type unit set.
1166 unsigned int debug_types = 0;
1167 unsigned int debug_abbrev = 0;
1168 unsigned int debug_line = 0;
1169 unsigned int debug_str_offsets = 0;
1170 for (;;)
1171 {
1172 if (shndx_list >= limit)
1173 gold_fatal(_("%s: .debug_tu_index is corrupt"),
1174 this->name_);
1175 unsigned int shndx =
1176 elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list);
1177 if (shndx == 0)
1178 break;
1179 if (shndx > this->shnum())
1180 gold_fatal(_("%s: .debug_tu_index has bad shndx"),
1181 this->name_);
1182 std::string sect_name = this->section_name(shndx);
1183 const char* suffix = sect_name.c_str();
1184 if (is_prefix_of(".debug_", suffix))
1185 suffix += 7;
1186 else if (is_prefix_of(".zdebug_", suffix))
1187 suffix += 8;
1188 else
1189 gold_fatal(_("%s: .debug_tu_index refers to "
1190 "non-debug section"), this->name_);
1191 if (strcmp(suffix, "types.dwo") == 0)
1192 debug_types = shndx;
1193 else if (strcmp(suffix, "abbrev.dwo") == 0)
1194 debug_abbrev = shndx;
1195 else if (strcmp(suffix, "line.dwo") == 0)
1196 debug_line = shndx;
1197 else if (strcmp(suffix, "str_offsets.dwo") == 0)
1198 debug_str_offsets = shndx;
1199 shndx_list += sizeof(uint32_t);
1200 }
1201 this->add_tu_set(output_file, type_sig, debug_types, debug_abbrev,
1202 debug_line, debug_str_offsets);
1203 }
1204 phash += sizeof(uint64_t);
1205 pindex += sizeof(uint32_t);
1206 }
1207
1208 if (is_new)
1209 delete[] contents;
1210}
1211
1212// Merge the input string table section into the output file.
1213
1214void
1215Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str)
1216{
1217 section_size_type len;
1218 bool is_new;
1219 const unsigned char* pdata = this->section_contents(debug_str, &len, &is_new);
1220 const char* p = reinterpret_cast<const char*>(pdata);
1221 const char* pend = p + len;
1222
1223 // Check that the last string is null terminated.
1224 if (pend[-1] != '\0')
1225 gold_fatal(_("%s: last entry in string section '%s' "
1226 "is not null terminated"),
1227 this->name_,
1228 this->section_name(debug_str).c_str());
1229
1230 // Count the number of strings in the section, and size the map.
1231 size_t count = 0;
1232 for (const char* pt = p; pt < pend; pt += strlen(pt) + 1)
1233 ++count;
1234 this->str_offset_map_.reserve(count + 1);
1235
1236 // Add the strings to the output string table, and record the new offsets
1237 // in the map.
1238 section_offset_type i = 0;
1239 section_offset_type new_offset;
1240 while (p < pend)
1241 {
1242 size_t len = strlen(p);
1243 new_offset = output_file->add_string(p, len);
1244 this->str_offset_map_.push_back(std::make_pair(i, new_offset));
1245 p += len + 1;
1246 i += len + 1;
1247 }
1248 new_offset = 0;
1249 this->str_offset_map_.push_back(std::make_pair(i, new_offset));
1250 if (is_new)
1251 delete[] pdata;
1252}
1253
1254// Copy a section from the input file to the output file.
1255// If IS_STR_OFFSETS is true, remap the string offsets for the
1256// output string table.
1257
1258unsigned int
1259Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx,
1260 const char* section_name, bool is_str_offsets)
1261{
1262 // Some sections may be referenced from more than one set.
1263 // Don't copy a section more than once.
1264 if (this->shndx_map_[shndx] > 0)
1265 return this->shndx_map_[shndx];
1266
1267 section_size_type len;
1268 bool is_new;
1269 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1270
1271 if (is_str_offsets)
1272 {
1273 const unsigned char* remapped = this->remap_str_offsets(contents, len);
1274 if (is_new)
1275 delete[] contents;
1276 contents = remapped;
1277 is_new = true;
1278 }
1279
1280 this->shndx_map_[shndx] = output_file->add_section(section_name, contents,
1281 len, 1);
1282 if (is_new)
1283 delete[] contents;
1284
1285 return this->shndx_map_[shndx];
1286}
1287
1288// Remap the
1289const unsigned char*
1290Dwo_file::remap_str_offsets(const unsigned char* contents,
1291 section_size_type len)
1292{
1293 if ((len & 3) != 0)
1294 gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"),
1295 this->name_);
1296
1297 if (this->obj_->is_big_endian())
1298 return this->sized_remap_str_offsets<true>(contents, len);
1299 else
1300 return this->sized_remap_str_offsets<false>(contents, len);
1301}
1302
1303template <bool big_endian>
1304const unsigned char*
1305Dwo_file::sized_remap_str_offsets(const unsigned char* contents,
1306 section_size_type len)
1307{
1308 unsigned char* remapped = new unsigned char[len];
1309 const unsigned char* p = contents;
1310 unsigned char* q = remapped;
1311 while (len > 0)
1312 {
1313 unsigned int val = elfcpp::Swap_unaligned<32, big_endian>::readval(p);
1314 val = this->remap_str_offset(val);
1315 elfcpp::Swap_unaligned<32, big_endian>::writeval(q, val);
1316 len -= 4;
1317 p += 4;
1318 q += 4;
1319 }
1320 return remapped;
1321}
1322
1323unsigned int
1324Dwo_file::remap_str_offset(unsigned int val)
1325{
1326 Str_offset_map_entry entry;
1327 entry.first = val;
1328
1329 Str_offset_map::const_iterator p =
1330 std::lower_bound(this->str_offset_map_.begin(),
1331 this->str_offset_map_.end(),
1332 entry, Offset_compare());
1333
1334 if (p == this->str_offset_map_.end() || p->first > val)
1335 {
1336 if (p == this->str_offset_map_.begin())
1337 return 0;
1338 --p;
1339 gold_assert(p->first <= val);
1340 }
1341
1342 return p->second + (val - p->first);
1343}
1344
1345// Add a set of .debug_info and related sections to OUTPUT_FILE.
1346
1347void
1348Dwo_file::add_cu_set(Dwp_output_file* output_file,
1349 uint64_t dwo_id,
1350 unsigned int debug_info,
1351 unsigned int debug_abbrev,
1352 unsigned int debug_line,
1353 unsigned int debug_loc,
1354 unsigned int debug_str_offsets,
1355 unsigned int debug_macinfo,
1356 unsigned int debug_macro)
1357{
1358 if (debug_info == 0)
1359 gold_fatal(_("%s: no .debug_info.dwo section found"), this->name_);
1360 if (debug_abbrev == 0)
1361 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_);
1362
1363 debug_abbrev = this->copy_section(output_file, debug_abbrev,
1364 ".debug_abbrev.dwo", false);
1365 if (debug_line > 0)
1366 debug_line = this->copy_section(output_file, debug_line,
1367 ".debug_line.dwo", false);
1368 if (debug_loc > 0)
1369 debug_loc = this->copy_section(output_file, debug_loc, ".debug_loc.dwo",
1370 false);
1371 if (debug_macinfo > 0)
1372 debug_macinfo = this->copy_section(output_file, debug_macinfo,
1373 ".debug_macinfo.dwo", false);
1374 if (debug_macro > 0)
1375 debug_macro = this->copy_section(output_file, debug_macro,
1376 ".debug_macro.dwo", false);
1377
1378 if (debug_str_offsets > 0)
1379 debug_str_offsets = this->copy_section(output_file, debug_str_offsets,
1380 ".debug_str_offsets.dwo", true);
1381
1382 debug_info = this->copy_section(output_file, debug_info, ".debug_info.dwo",
1383 false);
1384
1385 output_file->add_cu_set(dwo_id, debug_info, debug_abbrev, debug_line,
1386 debug_loc, debug_str_offsets, debug_macinfo,
1387 debug_macro);
1388}
1389
1390// Add a set of .debug_types and related sections to OUTPUT_FILE.
1391
1392void
1393Dwo_file::add_tu_set(Dwp_output_file* output_file,
1394 uint64_t type_sig,
1395 unsigned int debug_types,
1396 unsigned int debug_abbrev,
1397 unsigned int debug_line,
1398 unsigned int debug_str_offsets)
1399{
1400 if (debug_types == 0)
1401 gold_fatal(_("%s: no .debug_types.dwo section found"), this->name_);
1402 if (debug_abbrev == 0)
1403 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_);
1404
1405 // Ignore duplicate type signatures.
1406 if (output_file->lookup_tu(type_sig))
1407 return;
1408
1409 debug_abbrev = this->copy_section(output_file, debug_abbrev,
1410 ".debug_abbrev.dwo", false);
1411 if (debug_line > 0)
1412 debug_line = this->copy_section(output_file, debug_line,
1413 ".debug_line.dwo", false);
1414
1415 if (debug_str_offsets > 0)
1416 debug_str_offsets = this->copy_section(output_file, debug_str_offsets,
1417 ".debug_str_offsets.dwo", true);
1418
1419 debug_types = this->copy_section(output_file, debug_types,
1420 ".debug_types.dwo", false);
1421
1422 output_file->add_tu_set(type_sig, debug_types, debug_abbrev, debug_line,
1423 debug_str_offsets);
1424}
1425
1426// Class Dwp_output_file.
1427
1428// Record the target info from an input file. On first call, we
1429// set the ELF header values for the output file. On subsequent
1430// calls, we just verify that the values match.
1431
1432void
1433Dwp_output_file::record_target_info(const char*, int machine,
1434 int size, bool big_endian,
1435 int osabi, int abiversion)
1436{
1437 // TODO: Check the values on subsequent calls.
1438 if (this->size_ > 0)
1439 return;
1440
1441 this->machine_ = machine;
1442 this->size_ = size;
1443 this->big_endian_ = big_endian;
1444 this->osabi_ = osabi;
1445 this->abiversion_ = abiversion;
1446
1447 if (size == 32)
1448 this->next_file_offset_ = elfcpp::Elf_sizes<32>::ehdr_size;
1449 else if (size == 64)
1450 this->next_file_offset_ = elfcpp::Elf_sizes<64>::ehdr_size;
1451 else
1452 gold_unreachable();
1453
1454 this->fd_ = ::fopen(this->name_, "wb");
1455 if (this->fd_ == NULL)
1456 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
1457
1458 // Write zeroes for the ELF header initially. We'll write
1459 // the actual header during finalize().
1460 static const char buf[elfcpp::Elf_sizes<64>::ehdr_size] = { 0 };
1461 ::fwrite(buf, 1, this->next_file_offset_, this->fd_);
1462}
1463
1464// Add a string to the debug strings section.
1465
1466section_offset_type
1467Dwp_output_file::add_string(const char* str, size_t len)
1468{
1469 Stringpool::Key key;
1470 this->stringpool_.add_with_length(str, len, true, &key);
1471 this->have_strings_ = true;
1472 // We aren't supposed to call get_offset() until after
1473 // calling set_string_offsets(), but the offsets will
1474 // not change unless optimizing the string pool.
1475 return this->stringpool_.get_offset_from_key(key);
1476}
1477
1478// Align the file offset to the given boundary.
1479
1480static inline off_t
1481align_offset(off_t off, int align)
1482{
1483 return (off + align - 1) & ~(align - 1);
1484}
1485
1486// Add a section to the output file, and return the new section index.
1487
1488unsigned int
1489Dwp_output_file::add_section(const char* section_name,
1490 const unsigned char* contents,
1491 section_size_type len,
1492 int align)
1493{
1494 off_t file_offset = this->next_file_offset_;
1495 gold_assert(this->size_ > 0 && file_offset > 0);
1496
1497 file_offset = align_offset(file_offset, align);
1498
1499 ::fseek(this->fd_, file_offset, SEEK_SET);
1500 if (::fwrite(contents, 1, len, this->fd_) < len)
1501 gold_fatal(_("%s: error writing section '%s'"), this->name_, section_name);
1502
1503 section_name = this->shstrtab_.add_with_length(section_name,
1504 strlen(section_name),
1505 false, NULL);
1506 Section sect = { section_name, file_offset, len, align };
1507 this->sections_.push_back(sect);
1508
1509 this->next_file_offset_ = file_offset + len;
1510 return this->shnum_++;
1511}
1512
1513// Add a set of .debug_info and related sections to the output file.
1514
1515void
1516Dwp_output_file::add_cu_set(uint64_t dwo_id,
1517 unsigned int debug_info,
1518 unsigned int debug_abbrev,
1519 unsigned int debug_line,
1520 unsigned int debug_loc,
1521 unsigned int debug_str_offsets,
1522 unsigned int debug_macinfo,
1523 unsigned int debug_macro)
1524{
1525 Cu_or_tu_set cu_set = { dwo_id, debug_info, debug_abbrev, debug_line,
1526 debug_loc, debug_str_offsets, debug_macinfo,
1527 debug_macro };
1528 unsigned int slot;
1529 this->cu_index_.find_or_add(dwo_id, &slot);
1530 this->cu_index_.enter_set(slot, cu_set);
1531}
1532
1533// Lookup a type signature and return TRUE if we have already seen it.
1534bool
1535Dwp_output_file::lookup_tu(uint64_t type_sig)
1536{
1537 this->last_type_sig_ = type_sig;
1538 return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_);
1539}
1540
1541// Add a set of .debug_types and related sections to the output file.
1542
1543void
1544Dwp_output_file::add_tu_set(uint64_t type_sig,
1545 unsigned int debug_types,
1546 unsigned int debug_abbrev,
1547 unsigned int debug_line,
1548 unsigned int debug_str_offsets)
1549{
1550 Cu_or_tu_set tu_set = { type_sig, debug_types, debug_abbrev, debug_line,
1551 0, debug_str_offsets, 0, 0 };
1552 unsigned int slot;
1553 if (type_sig == this->last_type_sig_)
1554 slot = this->last_tu_slot_;
1555 else
1556 this->tu_index_.find_or_add(type_sig, &slot);
1557 this->tu_index_.enter_set(slot, tu_set);
1558}
1559
1560// Find a slot in the hash table for SIGNATURE. Return TRUE
1561// if the entry already exists.
1562
1563bool
1564Dwp_output_file::Dwp_index::find_or_add(uint64_t signature,
1565 unsigned int* slotp)
1566{
1567 if (this->capacity_ == 0)
1568 this->initialize();
1569 unsigned int slot =
1570 static_cast<unsigned int>(signature) & (this->capacity_ - 1);
1571 unsigned int secondary_hash;
1572 uint64_t probe = this->hash_table_[slot];
1573 if (probe != 0 && probe != signature)
1574 {
1575 secondary_hash = (static_cast<unsigned int>(signature >> 32)
1576 & (this->capacity_ - 1)) | 1;
1577 do
1578 {
1579 slot = (slot + secondary_hash) & (this->capacity_ - 1);
1580 probe = this->hash_table_[slot];
1581 } while (probe != 0 && probe != signature);
1582 }
1583 *slotp = slot;
1584 return (probe != 0);
1585}
1586
1587// Enter a CU or TU set at the given SLOT in the hash table.
1588
1589void
1590Dwp_output_file::Dwp_index::enter_set(unsigned int slot,
1591 const Cu_or_tu_set& set)
1592{
1593 gold_assert(slot < this->capacity_);
1594 gold_assert(set.debug_info_or_types > 0);
1595 gold_assert(set.debug_abbrev > 0);
1596
1597 // Add the section indexes to the pool.
1598 uint32_t pool_index = this->shndx_pool_.size();
1599 this->shndx_pool_.push_back(set.debug_info_or_types);
1600 this->shndx_pool_.push_back(set.debug_abbrev);
1601 if (set.debug_line > 0)
1602 this->shndx_pool_.push_back(set.debug_line);
1603 if (set.debug_loc > 0)
1604 this->shndx_pool_.push_back(set.debug_loc);
1605 if (set.debug_str_offsets > 0)
1606 this->shndx_pool_.push_back(set.debug_str_offsets);
1607 if (set.debug_macinfo > 0)
1608 this->shndx_pool_.push_back(set.debug_macinfo);
1609 if (set.debug_macro > 0)
1610 this->shndx_pool_.push_back(set.debug_macro);
1611 this->shndx_pool_.push_back(0);
1612
1613 // Enter the signature and pool index into the hash table.
1614 this->hash_table_[slot] = set.signature;
1615 this->index_table_[slot] = pool_index;
1616 ++this->used_;
1617
1618 // Grow the hash table when we exceed 2/3 capacity.
1619 if (this->used_ * 3 > this->capacity_ * 2)
1620 this->grow();
1621}
1622
1623// Initialize the hash table.
1624
1625void
1626Dwp_output_file::Dwp_index::initialize()
1627{
1628 this->capacity_ = 16;
1629 this->hash_table_ = new uint64_t[this->capacity_];
1630 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
1631 this->index_table_ = new uint32_t[this->capacity_];
1632 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
1633}
1634
1635// Grow the hash table when we reach 2/3 capacity.
1636
1637void
1638Dwp_output_file::Dwp_index::grow()
1639{
1640 unsigned int old_capacity = this->capacity_;
1641 uint64_t* old_hash_table = this->hash_table_;
1642 uint32_t* old_index_table = this->index_table_;
1643 unsigned int old_used = this->used_;
1644
1645 this->capacity_ = old_capacity * 2;
1646 this->hash_table_ = new uint64_t[this->capacity_];
1647 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
1648 this->index_table_ = new uint32_t[this->capacity_];
1649 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
1650 this->used_ = 0;
1651
1652 for (unsigned int i = 0; i < old_capacity; ++i)
1653 {
1654 uint64_t signature = old_hash_table[i];
1655 if (signature != 0)
1656 {
1657 unsigned int slot;
1658 bool found = this->find_or_add(signature, &slot);
1659 gold_assert(!found);
1660 this->hash_table_[slot] = signature;
1661 this->index_table_[slot] = old_index_table[i];
1662 ++this->used_;
1663 }
1664 }
1665 gold_assert(this->used_ == old_used);
1666
1667 delete[] old_hash_table;
1668 delete[] old_index_table;
1669}
1670
1671// Initialize the output file.
1672
1673void
1674Dwp_output_file::initialize()
1675{
1676 // We can't initialize the output file until we've recorded the
1677 // target info from the first input file.
1678 gold_assert(this->size_ > 0);
1679}
1680
1681// Finalize the file, write the string tables and index sections,
1682// and close the file.
1683
1684void
1685Dwp_output_file::finalize()
1686{
1687 unsigned char* buf;
1688
1689 // Write the debug string table.
1690 if (this->have_strings_)
1691 {
1692 this->stringpool_.set_string_offsets();
1693 section_size_type len = this->stringpool_.get_strtab_size();
1694 buf = new unsigned char[len];
1695 this->stringpool_.write_to_buffer(buf, len);
1696 this->add_section(".debug_str.dwo", buf, len, 1);
1697 delete[] buf;
1698 }
1699
1700 // Write the CU and TU indexes.
1701 if (this->big_endian_)
1702 {
1703 this->write_index<true>(".debug_cu_index", this->cu_index_);
1704 this->write_index<true>(".debug_tu_index", this->tu_index_);
1705 }
1706 else
1707 {
1708 this->write_index<false>(".debug_cu_index", this->cu_index_);
1709 this->write_index<false>(".debug_tu_index", this->tu_index_);
1710 }
1711
1712 off_t file_offset = this->next_file_offset_;
1713
1714 // Write the section string table.
1715 this->shstrndx_ = this->shnum_++;
1716 const char* shstrtab_name =
1717 this->shstrtab_.add_with_length(".shstrtab",
1718 sizeof(".shstrtab") - 1,
1719 false, NULL);
1720 this->shstrtab_.set_string_offsets();
1721 section_size_type shstrtab_len = this->shstrtab_.get_strtab_size();
1722 buf = new unsigned char[shstrtab_len];
1723 this->shstrtab_.write_to_buffer(buf, shstrtab_len);
1724 off_t shstrtab_off = file_offset;
1725 ::fseek(this->fd_, file_offset, 0);
1726 if (::fwrite(buf, 1, shstrtab_len, this->fd_) < shstrtab_len)
1727 gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_);
1728 delete[] buf;
1729 file_offset += shstrtab_len;
1730
1731 // Write the section header table. The first entry is a NULL entry.
1732 // This is followed by the debug sections, and finally we write the
1733 // .shstrtab section header.
1734 file_offset = align_offset(file_offset, this->size_ == 32 ? 4 : 8);
1735 this->shoff_ = file_offset;
1736 ::fseek(this->fd_, file_offset, 0);
1737 section_size_type sh0_size = 0;
1738 unsigned int sh0_link = 0;
1739 if (this->shnum_ >= elfcpp::SHN_LORESERVE)
1740 sh0_size = this->shnum_;
1741 if (this->shstrndx_ >= elfcpp::SHN_LORESERVE)
1742 sh0_link = this->shstrndx_;
1743 this->write_shdr(NULL, 0, 0, 0, 0, sh0_size, sh0_link, 0, 0, 0);
1744 for (unsigned int i = 0; i < this->sections_.size(); ++i)
1745 {
1746 Section& sect = this->sections_[i];
1747 this->write_shdr(sect.name, elfcpp::SHT_PROGBITS, 0, 0, sect.offset,
1748 sect.size, 0, 0, sect.align, 0);
1749 }
1750 this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0,
1751 shstrtab_off, shstrtab_len, 0, 0, 1, 0);
1752
1753 // Write the ELF header.
1754 this->write_ehdr();
1755
1756 // Close the file.
1757 if (this->fd_ != NULL)
1758 {
1759 if (::fclose(this->fd_) != 0)
1760 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
1761 }
1762 this->fd_ = NULL;
1763}
1764
1765// Write a CU or TU index section.
1766template<bool big_endian>
1767void
1768Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index)
1769{
1770 const unsigned int nslots = index.hash_table_total_slots();
1771 const unsigned int nused = index.hash_table_used_slots();
1772 const unsigned int npool = index.shndx_pool_size();
1773 const section_size_type index_size = (4 * sizeof(uint32_t)
1774 + nslots * sizeof(uint64_t)
1775 + nslots * sizeof(uint32_t)
1776 + npool * sizeof(uint32_t));
1777
1778 // Allocate a buffer for the section contents.
1779 unsigned char* buf = new unsigned char[index_size];
1780 unsigned char* p = buf;
1781
1782 // Write the section header: version number, padding,
1783 // number of used slots and total number of slots.
1784 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 1);
1785 p += sizeof(uint32_t);
1786 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 0);
1787 p += sizeof(uint32_t);
1788 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nused);
1789 p += sizeof(uint32_t);
1790 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nslots);
1791 p += sizeof(uint32_t);
1792
1793 // Write the hash table.
1794 for (unsigned int i = 0; i < nslots; ++i)
1795 {
1796 elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i));
1797 p += sizeof(uint64_t);
1798 }
1799
1800 // Write the parallel index table.
1801 for (unsigned int i = 0; i < nslots; ++i)
1802 {
1803 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i));
1804 p += sizeof(uint32_t);
1805 }
1806
1807 // Write the section index pool.
1808 Dwp_index::Shndx_pool::const_iterator pool = index.shndx_pool();
1809 for (unsigned int i = 0; i < npool; ++i)
1810 {
1811 gold_assert(pool != index.shndx_pool_end());
1812 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, *pool);
1813 p += sizeof(uint32_t);
1814 ++pool;
1815 }
1816
1817 gold_assert(p == buf + index_size);
1818
1819 this->add_section(sect_name, buf, index_size, sizeof(uint64_t));
1820
1821 delete[] buf;
1822}
1823
1824// Write the ELF header.
1825
1826void
1827Dwp_output_file::write_ehdr()
1828{
1829 if (this->size_ == 32)
1830 {
1831 if (this->big_endian_)
1832 return this->sized_write_ehdr<32, true>();
1833 else
1834 return this->sized_write_ehdr<32, false>();
1835 }
1836 else if (this->size_ == 64)
1837 {
1838 if (this->big_endian_)
1839 return this->sized_write_ehdr<64, true>();
1840 else
1841 return this->sized_write_ehdr<64, false>();
1842 }
1843 else
1844 gold_unreachable();
1845}
1846
1847template<unsigned int size, bool big_endian>
1848void
1849Dwp_output_file::sized_write_ehdr()
1850{
1851 const unsigned int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1852 unsigned char buf[ehdr_size];
1853 elfcpp::Ehdr_write<size, big_endian> ehdr(buf);
1854
1855 unsigned char e_ident[elfcpp::EI_NIDENT];
1856 memset(e_ident, 0, elfcpp::EI_NIDENT);
1857 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
1858 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
1859 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
1860 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
1861 if (size == 32)
1862 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
1863 else if (size == 64)
1864 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
1865 else
1866 gold_unreachable();
1867 e_ident[elfcpp::EI_DATA] = (big_endian
1868 ? elfcpp::ELFDATA2MSB
1869 : elfcpp::ELFDATA2LSB);
1870 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
1871 ehdr.put_e_ident(e_ident);
1872
1873 ehdr.put_e_type(elfcpp::ET_REL);
1874 ehdr.put_e_machine(this->machine_);
1875 ehdr.put_e_version(elfcpp::EV_CURRENT);
1876 ehdr.put_e_entry(0);
1877 ehdr.put_e_phoff(0);
1878 ehdr.put_e_shoff(this->shoff_);
1879 ehdr.put_e_flags(0);
1880 ehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
1881 ehdr.put_e_phentsize(0);
1882 ehdr.put_e_phnum(0);
1883 ehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
1884 ehdr.put_e_shnum(this->shnum_ < elfcpp::SHN_LORESERVE ? this->shnum_ : 0);
1885 ehdr.put_e_shstrndx(this->shstrndx_ < elfcpp::SHN_LORESERVE
1886 ? this->shstrndx_
1887 : static_cast<unsigned int>(elfcpp::SHN_XINDEX));
1888
1889 ::fseek(this->fd_, 0, 0);
1890 if (::fwrite(buf, 1, ehdr_size, this->fd_) < ehdr_size)
1891 gold_fatal(_("%s: error writing ELF header"), this->name_);
1892}
1893
1894// Write a section header.
1895
1896void
1897Dwp_output_file::write_shdr(const char* name, unsigned int type,
1898 unsigned int flags, uint64_t addr, off_t offset,
1899 section_size_type sect_size, unsigned int link,
1900 unsigned int info, unsigned int align,
1901 unsigned int ent_size)
1902{
1903 if (this->size_ == 32)
1904 {
1905 if (this->big_endian_)
1906 return this->sized_write_shdr<32, true>(name, type, flags, addr,
1907 offset, sect_size, link, info,
1908 align, ent_size);
1909 else
1910 return this->sized_write_shdr<32, false>(name, type, flags, addr,
1911 offset, sect_size, link, info,
1912 align, ent_size);
1913 }
1914 else if (this->size_ == 64)
1915 {
1916 if (this->big_endian_)
1917 return this->sized_write_shdr<64, true>(name, type, flags, addr,
1918 offset, sect_size, link, info,
1919 align, ent_size);
1920 else
1921 return this->sized_write_shdr<64, false>(name, type, flags, addr,
1922 offset, sect_size, link, info,
1923 align, ent_size);
1924 }
1925 else
1926 gold_unreachable();
1927}
1928
1929template<unsigned int size, bool big_endian>
1930void
1931Dwp_output_file::sized_write_shdr(const char* name, unsigned int type,
1932 unsigned int flags, uint64_t addr,
1933 off_t offset, section_size_type sect_size,
1934 unsigned int link, unsigned int info,
1935 unsigned int align, unsigned int ent_size)
1936{
1937 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1938 unsigned char buf[shdr_size];
1939 elfcpp::Shdr_write<size, big_endian> shdr(buf);
1940
1941 shdr.put_sh_name(name == NULL ? 0 : this->shstrtab_.get_offset(name));
1942 shdr.put_sh_type(type);
1943 shdr.put_sh_flags(flags);
1944 shdr.put_sh_addr(addr);
1945 shdr.put_sh_offset(offset);
1946 shdr.put_sh_size(sect_size);
1947 shdr.put_sh_link(link);
1948 shdr.put_sh_info(info);
1949 shdr.put_sh_addralign(align);
1950 shdr.put_sh_entsize(ent_size);
1951 if (::fwrite(buf, 1, shdr_size, this->fd_) < shdr_size)
1952 gold_fatal(_("%s: error writing section header table"), this->name_);
1953}
1954
1955// Class Dwo_id_info_reader.
1956
1957// Visit a compilation unit.
1958
1959void
1960Dwo_id_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die)
1961{
1962 this->dwo_id_ = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id);
1963 if (this->dwo_id_ != 0)
1964 this->dwo_id_found_ = true;
1965}
1966
1967// Visit a type unit.
1968
1969void
1970Dwo_id_info_reader::visit_type_unit(off_t, off_t, uint64_t signature, Dwarf_die*)
1971{
1972 this->type_sig_ = signature;
1973 this->type_sig_found_ = true;
1974}
1975
1976}; // End namespace gold
1977
1978using namespace gold;
1979
1980// Options.
1981
1982struct option dwp_options[] =
1983 {
1984 { "verbose", no_argument, NULL, 'v' },
1985 { "output", required_argument, NULL, 'o' },
1986 { NULL, 0, NULL, 0 }
1987 };
1988
1989// Print usage message and exit.
1990
1991static void
1992usage()
1993{
1994 fprintf(stderr, _("Usage: %s [options] file...\n"), program_name);
1995 fprintf(stderr, _(" -v, --verbose Verbose output\n"));
1996 fprintf(stderr, _(" -o FILE, --output FILE Set output dwp file name"
1997 " (required)\n"));
1998 exit(1);
1999}
2000
2001// Main program.
2002
2003int
2004main(int argc, char** argv)
2005{
2006#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2007 setlocale(LC_MESSAGES, "");
2008#endif
2009#if defined (HAVE_SETLOCALE)
2010 setlocale(LC_CTYPE, "");
2011#endif
2012 bindtextdomain(PACKAGE, LOCALEDIR);
2013 textdomain(PACKAGE);
2014
2015 program_name = argv[0];
2016
2017 // Initialize the global parameters, to let random code get to the
2018 // errors object.
2019 Errors errors(program_name);
2020 set_parameters_errors(&errors);
2021
2022 // Initialize gold's global options. We don't use these in
2023 // this program, but they need to be initialized so that
2024 // functions we call from libgold work properly.
2025 General_options options;
2026 set_parameters_options(&options);
2027
2028 // In libiberty; expands @filename to the args in "filename".
2029 expandargv(&argc, &argv);
2030
2031 // Collect file names and options.
2032 typedef std::vector<char*> File_list;
2033 File_list files;
2034 const char* output_filename = NULL;
2035 bool verbose = false;
2036 int c;
2037 while ((c = getopt_long(argc, argv, "vo:", dwp_options, NULL)) != -1)
2038 {
2039 switch (c)
2040 {
2041 case 'v':
2042 verbose = true;
2043 break;
2044 case 'o':
2045 output_filename = optarg;
2046 break;
2047 case '?':
2048 default:
2049 usage();
2050 }
2051 }
2052 for (int i = optind; i < argc; ++i)
2053 files.push_back(argv[i]);
2054
2055 if (files.empty())
2056 gold_fatal(_("no input files"));
2057 if (output_filename == NULL)
2058 gold_fatal(_("no output file specified"));
2059
2060 Dwp_output_file output_file(output_filename);
2061
2062 // Process each file, adding its contents to the output file.
2063 for (File_list::const_iterator f = files.begin(); f != files.end(); ++f)
2064 {
2065 if (verbose)
2066 fprintf(stderr, "%s\n", *f);
2067 Dwo_file dwo_file(*f);
2068 dwo_file.read(&output_file);
2069 }
2070
2071 output_file.finalize();
2072
2073 return EXIT_SUCCESS;
2074}
This page took 0.098631 seconds and 4 git commands to generate.