1 // dwp.cc -- DWARF packaging utility 2 3 // Copyright (C) 2012-2016 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 #include "../bfd/bfdver.h" 38 39 #include "elfcpp.h" 40 #include "elfcpp_file.h" 41 #include "dwarf.h" 42 #include "dirsearch.h" 43 #include "fileread.h" 44 #include "object.h" 45 #include "compressed_output.h" 46 #include "stringpool.h" 47 #include "dwarf_reader.h" 48 49 static void 50 usage(FILE* fd, int) ATTRIBUTE_NORETURN; 51 52 static void 53 print_version() ATTRIBUTE_NORETURN; 54 55 namespace gold { 56 57 class Dwp_output_file; 58 59 template <int size, bool big_endian> 60 class Sized_relobj_dwo; 61 62 // List of .dwo files to process. 63 struct Dwo_file_entry 64 { 65 Dwo_file_entry(uint64_t id, std::string name) 66 : dwo_id(id), dwo_name(name) 67 { } 68 uint64_t dwo_id; 69 std::string dwo_name; 70 }; 71 typedef std::vector<Dwo_file_entry> File_list; 72 73 // Type to hold the offset and length of an input section 74 // within an output section. 75 76 struct Section_bounds 77 { 78 section_offset_type offset; 79 section_size_type size; 80 81 Section_bounds() 82 : offset(0), size(0) 83 { } 84 85 Section_bounds(section_offset_type o, section_size_type s) 86 : offset(o), size(s) 87 { } 88 }; 89 90 // A set of sections for a compilation unit or type unit. 91 92 struct Unit_set 93 { 94 uint64_t signature; 95 Section_bounds sections[elfcpp::DW_SECT_MAX + 1]; 96 97 Unit_set() 98 : signature(0), sections() 99 { } 100 }; 101 102 // An input file. 103 // This class may represent a .dwo file, a .dwp file 104 // produced by an earlier run, or an executable file whose 105 // debug section identifies a set of .dwo files to read. 106 107 class Dwo_file 108 { 109 public: 110 Dwo_file(const char* name) 111 : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(), 112 sect_offsets_(), str_offset_map_() 113 { } 114 115 ~Dwo_file(); 116 117 // Read the input executable file and extract the list of .dwo files 118 // that it references. 119 void 120 read_executable(File_list* files); 121 122 // Read the input file and send its contents to OUTPUT_FILE. 123 void 124 read(Dwp_output_file* output_file); 125 126 // Verify a .dwp file given a list of .dwo files referenced by the 127 // corresponding executable file. Returns true if no problems 128 // were found. 129 bool 130 verify(const File_list& files); 131 132 private: 133 // Types for mapping input string offsets to output string offsets. 134 typedef std::pair<section_offset_type, section_offset_type> 135 Str_offset_map_entry; 136 typedef std::vector<Str_offset_map_entry> Str_offset_map; 137 138 // A less-than comparison routine for Str_offset_map. 139 struct Offset_compare 140 { 141 bool 142 operator()(const Str_offset_map_entry& i1, 143 const Str_offset_map_entry& i2) const 144 { return i1.first < i2.first; } 145 }; 146 147 // Create a Sized_relobj_dwo of the given size and endianness, 148 // and record the target info. P is a pointer to the ELF header 149 // in memory. 150 Relobj* 151 make_object(Dwp_output_file* output_file); 152 153 template <int size, bool big_endian> 154 Relobj* 155 sized_make_object(const unsigned char* p, Input_file* input_file, 156 Dwp_output_file* output_file); 157 158 // Return the number of sections in the input object file. 159 unsigned int 160 shnum() const 161 { return this->obj_->shnum(); } 162 163 // Return section type. 164 unsigned int 165 section_type(unsigned int shndx) 166 { return this->obj_->section_type(shndx); } 167 168 // Get the name of a section. 169 std::string 170 section_name(unsigned int shndx) 171 { return this->obj_->section_name(shndx); } 172 173 // Return a view of the contents of a section, decompressed if necessary. 174 // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be 175 // deleted by the caller. 176 const unsigned char* 177 section_contents(unsigned int shndx, section_size_type* plen, bool* is_new) 178 { return this->obj_->decompressed_section_contents(shndx, plen, is_new); } 179 180 // Read the .debug_cu_index or .debug_tu_index section of a .dwp file, 181 // and process the CU or TU sets. 182 void 183 read_unit_index(unsigned int, unsigned int *, Dwp_output_file*, 184 bool is_tu_index); 185 186 template <bool big_endian> 187 void 188 sized_read_unit_index(unsigned int, unsigned int *, Dwp_output_file*, 189 bool is_tu_index); 190 191 // Verify the .debug_cu_index section of a .dwp file, comparing it 192 // against the list of .dwo files referenced by the corresponding 193 // executable file. 194 bool 195 verify_dwo_list(unsigned int, const File_list& files); 196 197 template <bool big_endian> 198 bool 199 sized_verify_dwo_list(unsigned int, const File_list& files); 200 201 // Merge the input string table section into the output file. 202 void 203 add_strings(Dwp_output_file*, unsigned int); 204 205 // Copy a section from the input file to the output file. 206 Section_bounds 207 copy_section(Dwp_output_file* output_file, unsigned int shndx, 208 elfcpp::DW_SECT section_id); 209 210 // Remap the string offsets in the .debug_str_offsets.dwo section. 211 const unsigned char* 212 remap_str_offsets(const unsigned char* contents, section_size_type len); 213 214 template <bool big_endian> 215 const unsigned char* 216 sized_remap_str_offsets(const unsigned char* contents, section_size_type len); 217 218 // Remap a single string offsets from an offset in the input string table 219 // to an offset in the output string table. 220 unsigned int 221 remap_str_offset(section_offset_type val); 222 223 // Add a set of .debug_info.dwo or .debug_types.dwo and related sections 224 // to OUTPUT_FILE. 225 void 226 add_unit_set(Dwp_output_file* output_file, unsigned int *debug_shndx, 227 bool is_debug_types); 228 229 // The filename. 230 const char* name_; 231 // The ELF file, represented as a gold Relobj instance. 232 Relobj* obj_; 233 // The Input_file object. 234 Input_file* input_file_; 235 // Flags indicating which sections are compressed. 236 std::vector<bool> is_compressed_; 237 // Map input section index onto output section offset and size. 238 std::vector<Section_bounds> sect_offsets_; 239 // Map input string offsets to output string offsets. 240 Str_offset_map str_offset_map_; 241 }; 242 243 // An ELF input file. 244 // We derive from Sized_relobj so that we can use interfaces 245 // in libgold to access the file. 246 247 template <int size, bool big_endian> 248 class Sized_relobj_dwo : public Sized_relobj<size, big_endian> 249 { 250 public: 251 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 252 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols; 253 254 Sized_relobj_dwo(const char* name, Input_file* input_file, 255 const elfcpp::Ehdr<size, big_endian>& ehdr) 256 : Sized_relobj<size, big_endian>(name, input_file), 257 elf_file_(this, ehdr) 258 { } 259 260 ~Sized_relobj_dwo() 261 { } 262 263 // Setup the section information. 264 void 265 setup(); 266 267 protected: 268 // Return section type. 269 unsigned int 270 do_section_type(unsigned int shndx) 271 { return this->elf_file_.section_type(shndx); } 272 273 // Get the name of a section. 274 std::string 275 do_section_name(unsigned int shndx) const 276 { return this->elf_file_.section_name(shndx); } 277 278 // Get the size of a section. 279 uint64_t 280 do_section_size(unsigned int shndx) 281 { return this->elf_file_.section_size(shndx); } 282 283 // Return a view of the contents of a section. 284 const unsigned char* 285 do_section_contents(unsigned int, section_size_type*, bool); 286 287 // The following virtual functions are abstract in the base classes, 288 // but are not used here. 289 290 // Read the symbols. 291 void 292 do_read_symbols(Read_symbols_data*) 293 { gold_unreachable(); } 294 295 // Lay out the input sections. 296 void 297 do_layout(Symbol_table*, Layout*, Read_symbols_data*) 298 { gold_unreachable(); } 299 300 // Layout sections whose layout was deferred while waiting for 301 // input files from a plugin. 302 void 303 do_layout_deferred_sections(Layout*) 304 { gold_unreachable(); } 305 306 // Add the symbols to the symbol table. 307 void 308 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) 309 { gold_unreachable(); } 310 311 Archive::Should_include 312 do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*, 313 std::string*) 314 { gold_unreachable(); } 315 316 // Iterate over global symbols, calling a visitor class V for each. 317 void 318 do_for_all_global_symbols(Read_symbols_data*, 319 Library_base::Symbol_visitor_base*) 320 { gold_unreachable(); } 321 322 // Return section flags. 323 uint64_t 324 do_section_flags(unsigned int) 325 { gold_unreachable(); } 326 327 // Return section entsize. 328 uint64_t 329 do_section_entsize(unsigned int) 330 { gold_unreachable(); } 331 332 // Return section address. 333 uint64_t 334 do_section_address(unsigned int) 335 { gold_unreachable(); } 336 337 // Return the section link field. 338 unsigned int 339 do_section_link(unsigned int) 340 { gold_unreachable(); } 341 342 // Return the section link field. 343 unsigned int 344 do_section_info(unsigned int) 345 { gold_unreachable(); } 346 347 // Return the section alignment. 348 uint64_t 349 do_section_addralign(unsigned int) 350 { gold_unreachable(); } 351 352 // Return the Xindex structure to use. 353 Xindex* 354 do_initialize_xindex() 355 { gold_unreachable(); } 356 357 // Get symbol counts. 358 void 359 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const 360 { gold_unreachable(); } 361 362 // Get global symbols. 363 const Symbols* 364 do_get_global_symbols() const 365 { return NULL; } 366 367 // Return the value of a local symbol. 368 uint64_t 369 do_local_symbol_value(unsigned int, uint64_t) const 370 { gold_unreachable(); } 371 372 unsigned int 373 do_local_plt_offset(unsigned int) const 374 { gold_unreachable(); } 375 376 // Return whether local symbol SYMNDX is a TLS symbol. 377 bool 378 do_local_is_tls(unsigned int) const 379 { gold_unreachable(); } 380 381 // Return the number of local symbols. 382 unsigned int 383 do_local_symbol_count() const 384 { gold_unreachable(); } 385 386 // Return the number of local symbols in the output symbol table. 387 unsigned int 388 do_output_local_symbol_count() const 389 { gold_unreachable(); } 390 391 // Return the file offset for local symbols in the output symbol table. 392 off_t 393 do_local_symbol_offset() const 394 { gold_unreachable(); } 395 396 // Read the relocs. 397 void 398 do_read_relocs(Read_relocs_data*) 399 { gold_unreachable(); } 400 401 // Process the relocs to find list of referenced sections. Used only 402 // during garbage collection. 403 void 404 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) 405 { gold_unreachable(); } 406 407 // Scan the relocs and adjust the symbol table. 408 void 409 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) 410 { gold_unreachable(); } 411 412 // Count the local symbols. 413 void 414 do_count_local_symbols(Stringpool_template<char>*, 415 Stringpool_template<char>*) 416 { gold_unreachable(); } 417 418 // Finalize the local symbols. 419 unsigned int 420 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) 421 { gold_unreachable(); } 422 423 // Set the offset where local dynamic symbol information will be stored. 424 unsigned int 425 do_set_local_dynsym_indexes(unsigned int) 426 { gold_unreachable(); } 427 428 // Set the offset where local dynamic symbol information will be stored. 429 unsigned int 430 do_set_local_dynsym_offset(off_t) 431 { gold_unreachable(); } 432 433 // Relocate the input sections and write out the local symbols. 434 void 435 do_relocate(const Symbol_table*, const Layout*, Output_file*) 436 { gold_unreachable(); } 437 438 private: 439 // General access to the ELF file. 440 elfcpp::Elf_file<size, big_endian, Object> elf_file_; 441 }; 442 443 // The output file. 444 // This class is responsible for collecting the debug index information 445 // and writing the .dwp file in ELF format. 446 447 class Dwp_output_file 448 { 449 public: 450 Dwp_output_file(const char* name) 451 : name_(name), machine_(0), size_(0), big_endian_(false), osabi_(0), 452 abiversion_(0), fd_(NULL), next_file_offset_(0), shnum_(1), sections_(), 453 section_id_map_(), shoff_(0), shstrndx_(0), have_strings_(false), 454 stringpool_(), shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0), 455 last_tu_slot_(0) 456 { 457 this->section_id_map_.resize(elfcpp::DW_SECT_MAX + 1); 458 this->stringpool_.set_no_zero_null(); 459 } 460 461 // Record the target info from an input file. 462 void 463 record_target_info(const char* name, int machine, int size, bool big_endian, 464 int osabi, int abiversion); 465 466 // Add a string to the debug strings section. 467 section_offset_type 468 add_string(const char* str, size_t len); 469 470 // Add a section to the output file, and return the new section offset. 471 section_offset_type 472 add_contribution(elfcpp::DW_SECT section_id, const unsigned char* contents, 473 section_size_type len, int align); 474 475 // Add a set of .debug_info and related sections to the output file. 476 void 477 add_cu_set(Unit_set* cu_set); 478 479 // Lookup a type signature and return TRUE if we have already seen it. 480 bool 481 lookup_tu(uint64_t type_sig); 482 483 // Add a set of .debug_types and related sections to the output file. 484 void 485 add_tu_set(Unit_set* tu_set); 486 487 // Finalize the file, write the string tables and index sections, 488 // and close the file. 489 void 490 finalize(); 491 492 private: 493 // Contributions to output sections. 494 struct Contribution 495 { 496 section_offset_type output_offset; 497 section_size_type size; 498 const unsigned char* contents; 499 }; 500 501 // Sections in the output file. 502 struct Section 503 { 504 const char* name; 505 off_t offset; 506 section_size_type size; 507 int align; 508 std::vector<Contribution> contributions; 509 510 Section(const char* n, int a) 511 : name(n), offset(0), size(0), align(a), contributions() 512 { } 513 }; 514 515 // The index sections defined by the DWARF Package File Format spec. 516 class Dwp_index 517 { 518 public: 519 // Vector for the section table. 520 typedef std::vector<const Unit_set*> Section_table; 521 522 Dwp_index() 523 : capacity_(0), used_(0), hash_table_(NULL), section_table_(), 524 section_mask_(0) 525 { } 526 527 ~Dwp_index() 528 { } 529 530 // Find a slot in the hash table for SIGNATURE. Return TRUE 531 // if the entry already exists. 532 bool 533 find_or_add(uint64_t signature, unsigned int* slotp); 534 535 // Enter a CU or TU set at the given SLOT in the hash table. 536 void 537 enter_set(unsigned int slot, const Unit_set* set); 538 539 // Return the contents of the given SLOT in the hash table of signatures. 540 uint64_t 541 hash_table(unsigned int slot) const 542 { return this->hash_table_[slot]; } 543 544 // Return the contents of the given SLOT in the parallel table of 545 // shndx pool indexes. 546 uint32_t 547 index_table(unsigned int slot) const 548 { return this->index_table_[slot]; } 549 550 // Return the total number of slots in the hash table. 551 unsigned int 552 hash_table_total_slots() const 553 { return this->capacity_; } 554 555 // Return the number of used slots in the hash table. 556 unsigned int 557 hash_table_used_slots() const 558 { return this->used_; } 559 560 // Return an iterator into the shndx pool. 561 Section_table::const_iterator 562 section_table() const 563 { return this->section_table_.begin(); } 564 565 Section_table::const_iterator 566 section_table_end() const 567 { return this->section_table_.end(); } 568 569 // Return the number of rows in the section table. 570 unsigned int 571 section_table_rows() const 572 { return this->section_table_.size(); } 573 574 // Return the mask indicating which columns will be used 575 // in the section table. 576 int 577 section_table_cols() const 578 { return this->section_mask_; } 579 580 private: 581 // Initialize the hash table. 582 void 583 initialize(); 584 585 // Grow the hash table when we reach 2/3 capacity. 586 void 587 grow(); 588 589 // The number of slots in the table, a power of 2 such that 590 // capacity > 3 * size / 2. 591 unsigned int capacity_; 592 // The current number of used slots in the hash table. 593 unsigned int used_; 594 // The storage for the hash table of signatures. 595 uint64_t* hash_table_; 596 // The storage for the parallel table of shndx pool indexes. 597 uint32_t* index_table_; 598 // The table of section offsets and sizes. 599 Section_table section_table_; 600 // Bit mask to indicate which debug sections are present in the file. 601 int section_mask_; 602 }; // End class Dwp_output_file::Dwp_index. 603 604 // Add a new output section and return the section index. 605 unsigned int 606 add_output_section(const char* section_name, int align); 607 608 // Write a new section to the output file. 609 void 610 write_new_section(const char* section_name, const unsigned char* contents, 611 section_size_type len, int align); 612 613 // Write the ELF header. 614 void 615 write_ehdr(); 616 617 template<unsigned int size, bool big_endian> 618 void 619 sized_write_ehdr(); 620 621 // Write a section header. 622 void 623 write_shdr(const char* name, unsigned int type, unsigned int flags, 624 uint64_t addr, off_t offset, section_size_type sect_size, 625 unsigned int link, unsigned int info, 626 unsigned int align, unsigned int ent_size); 627 628 template<unsigned int size, bool big_endian> 629 void 630 sized_write_shdr(const char* name, unsigned int type, unsigned int flags, 631 uint64_t addr, off_t offset, section_size_type sect_size, 632 unsigned int link, unsigned int info, 633 unsigned int align, unsigned int ent_size); 634 635 // Write the contributions to an output section. 636 void 637 write_contributions(const Section& sect); 638 639 // Write a CU or TU index section. 640 template<bool big_endian> 641 void 642 write_index(const char* sect_name, const Dwp_index& index); 643 644 // The output filename. 645 const char* name_; 646 // ELF header parameters. 647 int machine_; 648 int size_; 649 int big_endian_; 650 int osabi_; 651 int abiversion_; 652 // The output file descriptor. 653 FILE* fd_; 654 // Next available file offset. 655 off_t next_file_offset_; 656 // The number of sections. 657 unsigned int shnum_; 658 // Section table. The first entry is shndx 1. 659 std::vector<Section> sections_; 660 // Section id map. This maps a DW_SECT enum to an shndx. 661 std::vector<unsigned int> section_id_map_; 662 // File offset of the section header table. 663 off_t shoff_; 664 // Section index of the section string table. 665 unsigned int shstrndx_; 666 // TRUE if we have added any strings to the string pool. 667 bool have_strings_; 668 // String pool for the output .debug_str.dwo section. 669 Stringpool stringpool_; 670 // String pool for the .shstrtab section. 671 Stringpool shstrtab_; 672 // The compilation unit index. 673 Dwp_index cu_index_; 674 // The type unit index. 675 Dwp_index tu_index_; 676 // Cache of the last type signature looked up. 677 uint64_t last_type_sig_; 678 // Cache of the slot index for the last type signature. 679 unsigned int last_tu_slot_; 680 }; 681 682 // A specialization of Dwarf_info_reader, for reading dwo_names from 683 // DWARF CUs. 684 685 class Dwo_name_info_reader : public Dwarf_info_reader 686 { 687 public: 688 Dwo_name_info_reader(Relobj* object, unsigned int shndx) 689 : Dwarf_info_reader(false, object, NULL, 0, shndx, 0, 0), 690 files_(NULL) 691 { } 692 693 ~Dwo_name_info_reader() 694 { } 695 696 // Get the dwo_names from the DWARF compilation unit DIEs. 697 void 698 get_dwo_names(File_list* files) 699 { 700 this->files_ = files; 701 this->parse(); 702 } 703 704 protected: 705 // Visit a compilation unit. 706 virtual void 707 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*); 708 709 private: 710 // The list of files to populate. 711 File_list* files_; 712 }; 713 714 // A specialization of Dwarf_info_reader, for reading DWARF CUs and TUs 715 // and adding them to the output file. 716 717 class Unit_reader : public Dwarf_info_reader 718 { 719 public: 720 Unit_reader(bool is_type_unit, Relobj* object, unsigned int shndx) 721 : Dwarf_info_reader(is_type_unit, object, NULL, 0, shndx, 0, 0), 722 output_file_(NULL), sections_(NULL) 723 { } 724 725 ~Unit_reader() 726 { } 727 728 // Read the CUs or TUs and add them to the output file. 729 void 730 add_units(Dwp_output_file*, unsigned int debug_abbrev, Section_bounds*); 731 732 protected: 733 // Visit a compilation unit. 734 virtual void 735 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*); 736 737 // Visit a type unit. 738 virtual void 739 visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset, 740 uint64_t signature, Dwarf_die*); 741 742 private: 743 Dwp_output_file* output_file_; 744 Section_bounds* sections_; 745 }; 746 747 // Return the name of a DWARF .dwo section. 748 749 static const char* 750 get_dwarf_section_name(elfcpp::DW_SECT section_id) 751 { 752 static const char* dwarf_section_names[] = { 753 NULL, // unused 754 ".debug_info.dwo", // DW_SECT_INFO = 1 755 ".debug_types.dwo", // DW_SECT_TYPES = 2 756 ".debug_abbrev.dwo", // DW_SECT_ABBREV = 3 757 ".debug_line.dwo", // DW_SECT_LINE = 4 758 ".debug_loc.dwo", // DW_SECT_LOC = 5 759 ".debug_str_offsets.dwo", // DW_SECT_STR_OFFSETS = 6 760 ".debug_macinfo.dwo", // DW_SECT_MACINFO = 7 761 ".debug_macro.dwo", // DW_SECT_MACRO = 8 762 }; 763 764 gold_assert(section_id > 0 && section_id <= elfcpp::DW_SECT_MAX); 765 return dwarf_section_names[section_id]; 766 } 767 768 // Class Sized_relobj_dwo. 769 770 // Setup the section information. 771 772 template <int size, bool big_endian> 773 void 774 Sized_relobj_dwo<size, big_endian>::setup() 775 { 776 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 777 const off_t shoff = this->elf_file_.shoff(); 778 const unsigned int shnum = this->elf_file_.shnum(); 779 780 this->set_shnum(shnum); 781 this->section_offsets().resize(shnum); 782 783 // Read the section headers. 784 const unsigned char* const pshdrs = this->get_view(shoff, shnum * shdr_size, 785 true, false); 786 787 // Read the section names. 788 const unsigned char* pshdrnames = 789 pshdrs + this->elf_file_.shstrndx() * shdr_size; 790 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); 791 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) 792 this->error(_("section name section has wrong type: %u"), 793 static_cast<unsigned int>(shdrnames.get_sh_type())); 794 section_size_type section_names_size = 795 convert_to_section_size_type(shdrnames.get_sh_size()); 796 const unsigned char* namesu = this->get_view(shdrnames.get_sh_offset(), 797 section_names_size, false, 798 false); 799 const char* names = reinterpret_cast<const char*>(namesu); 800 801 Compressed_section_map* compressed_sections = 802 build_compressed_section_map<size, big_endian>( 803 pshdrs, this->shnum(), names, section_names_size, this, true); 804 if (compressed_sections != NULL && !compressed_sections->empty()) 805 this->set_compressed_sections(compressed_sections); 806 } 807 808 // Return a view of the contents of a section. 809 810 template <int size, bool big_endian> 811 const unsigned char* 812 Sized_relobj_dwo<size, big_endian>::do_section_contents( 813 unsigned int shndx, 814 section_size_type* plen, 815 bool cache) 816 { 817 Object::Location loc(this->elf_file_.section_contents(shndx)); 818 *plen = convert_to_section_size_type(loc.data_size); 819 if (*plen == 0) 820 { 821 static const unsigned char empty[1] = { '\0' }; 822 return empty; 823 } 824 return this->get_view(loc.file_offset, *plen, true, cache); 825 } 826 827 // Class Dwo_file. 828 829 Dwo_file::~Dwo_file() 830 { 831 if (this->obj_ != NULL) 832 delete this->obj_; 833 if (this->input_file_ != NULL) 834 delete this->input_file_; 835 } 836 837 // Read the input executable file and extract the list of .dwo files 838 // that it references. 839 840 void 841 Dwo_file::read_executable(File_list* files) 842 { 843 this->obj_ = this->make_object(NULL); 844 845 unsigned int shnum = this->shnum(); 846 this->is_compressed_.resize(shnum); 847 this->sect_offsets_.resize(shnum); 848 849 unsigned int debug_info = 0; 850 unsigned int debug_abbrev = 0; 851 852 // Scan the section table and collect the debug sections we need. 853 // (Section index 0 is a dummy section; skip it.) 854 for (unsigned int i = 1; i < shnum; i++) 855 { 856 if (this->section_type(i) != elfcpp::SHT_PROGBITS) 857 continue; 858 std::string sect_name = this->section_name(i); 859 const char* suffix = sect_name.c_str(); 860 if (is_prefix_of(".debug_", suffix)) 861 suffix += 7; 862 else if (is_prefix_of(".zdebug_", suffix)) 863 { 864 this->is_compressed_[i] = true; 865 suffix += 8; 866 } 867 else 868 continue; 869 if (strcmp(suffix, "info") == 0) 870 debug_info = i; 871 else if (strcmp(suffix, "abbrev") == 0) 872 debug_abbrev = i; 873 } 874 875 if (debug_info > 0) 876 { 877 Dwo_name_info_reader dwarf_reader(this->obj_, debug_info); 878 dwarf_reader.set_abbrev_shndx(debug_abbrev); 879 dwarf_reader.get_dwo_names(files); 880 } 881 } 882 883 // Read the input file and send its contents to OUTPUT_FILE. 884 885 void 886 Dwo_file::read(Dwp_output_file* output_file) 887 { 888 this->obj_ = this->make_object(output_file); 889 890 unsigned int shnum = this->shnum(); 891 this->is_compressed_.resize(shnum); 892 this->sect_offsets_.resize(shnum); 893 894 typedef std::vector<unsigned int> Types_list; 895 Types_list debug_types; 896 unsigned int debug_shndx[elfcpp::DW_SECT_MAX + 1]; 897 for (unsigned int i = 0; i <= elfcpp::DW_SECT_MAX; i++) 898 debug_shndx[i] = 0; 899 unsigned int debug_str = 0; 900 unsigned int debug_cu_index = 0; 901 unsigned int debug_tu_index = 0; 902 903 // Scan the section table and collect debug sections. 904 // (Section index 0 is a dummy section; skip it.) 905 for (unsigned int i = 1; i < shnum; i++) 906 { 907 if (this->section_type(i) != elfcpp::SHT_PROGBITS) 908 continue; 909 std::string sect_name = this->section_name(i); 910 const char* suffix = sect_name.c_str(); 911 if (is_prefix_of(".debug_", suffix)) 912 suffix += 7; 913 else if (is_prefix_of(".zdebug_", suffix)) 914 { 915 this->is_compressed_[i] = true; 916 suffix += 8; 917 } 918 else 919 continue; 920 if (strcmp(suffix, "info.dwo") == 0) 921 debug_shndx[elfcpp::DW_SECT_INFO] = i; 922 else if (strcmp(suffix, "types.dwo") == 0) 923 debug_types.push_back(i); 924 else if (strcmp(suffix, "abbrev.dwo") == 0) 925 debug_shndx[elfcpp::DW_SECT_ABBREV] = i; 926 else if (strcmp(suffix, "line.dwo") == 0) 927 debug_shndx[elfcpp::DW_SECT_LINE] = i; 928 else if (strcmp(suffix, "loc.dwo") == 0) 929 debug_shndx[elfcpp::DW_SECT_LOC] = i; 930 else if (strcmp(suffix, "str.dwo") == 0) 931 debug_str = i; 932 else if (strcmp(suffix, "str_offsets.dwo") == 0) 933 debug_shndx[elfcpp::DW_SECT_STR_OFFSETS] = i; 934 else if (strcmp(suffix, "macinfo.dwo") == 0) 935 debug_shndx[elfcpp::DW_SECT_MACINFO] = i; 936 else if (strcmp(suffix, "macro.dwo") == 0) 937 debug_shndx[elfcpp::DW_SECT_MACRO] = i; 938 else if (strcmp(suffix, "cu_index") == 0) 939 debug_cu_index = i; 940 else if (strcmp(suffix, "tu_index") == 0) 941 debug_tu_index = i; 942 } 943 944 // Merge the input string table into the output string table. 945 this->add_strings(output_file, debug_str); 946 947 // If we found any .dwp index sections, read those and add the section 948 // sets to the output file. 949 if (debug_cu_index > 0 || debug_tu_index > 0) 950 { 951 if (debug_cu_index > 0) 952 this->read_unit_index(debug_cu_index, debug_shndx, output_file, false); 953 if (debug_tu_index > 0) 954 { 955 if (debug_types.size() > 1) 956 gold_fatal(_("%s: .dwp file must have no more than one " 957 ".debug_types.dwo section"), this->name_); 958 if (debug_types.size() == 1) 959 debug_shndx[elfcpp::DW_SECT_TYPES] = debug_types[0]; 960 else 961 debug_shndx[elfcpp::DW_SECT_TYPES] = 0; 962 this->read_unit_index(debug_tu_index, debug_shndx, output_file, true); 963 } 964 return; 965 } 966 967 // If we found no index sections, this is a .dwo file. 968 if (debug_shndx[elfcpp::DW_SECT_INFO] > 0) 969 this->add_unit_set(output_file, debug_shndx, false); 970 971 debug_shndx[elfcpp::DW_SECT_INFO] = 0; 972 for (Types_list::const_iterator tp = debug_types.begin(); 973 tp != debug_types.end(); 974 ++tp) 975 { 976 debug_shndx[elfcpp::DW_SECT_TYPES] = *tp; 977 this->add_unit_set(output_file, debug_shndx, true); 978 } 979 } 980 981 // Verify a .dwp file given a list of .dwo files referenced by the 982 // corresponding executable file. Returns true if no problems 983 // were found. 984 985 bool 986 Dwo_file::verify(const File_list& files) 987 { 988 this->obj_ = this->make_object(NULL); 989 990 unsigned int shnum = this->shnum(); 991 this->is_compressed_.resize(shnum); 992 this->sect_offsets_.resize(shnum); 993 994 unsigned int debug_cu_index = 0; 995 996 // Scan the section table and collect debug sections. 997 // (Section index 0 is a dummy section; skip it.) 998 for (unsigned int i = 1; i < shnum; i++) 999 { 1000 if (this->section_type(i) != elfcpp::SHT_PROGBITS) 1001 continue; 1002 std::string sect_name = this->section_name(i); 1003 const char* suffix = sect_name.c_str(); 1004 if (is_prefix_of(".debug_", suffix)) 1005 suffix += 7; 1006 else if (is_prefix_of(".zdebug_", suffix)) 1007 { 1008 this->is_compressed_[i] = true; 1009 suffix += 8; 1010 } 1011 else 1012 continue; 1013 if (strcmp(suffix, "cu_index") == 0) 1014 debug_cu_index = i; 1015 } 1016 1017 if (debug_cu_index == 0) 1018 gold_fatal(_("%s: no .debug_cu_index section found"), this->name_); 1019 1020 return this->verify_dwo_list(debug_cu_index, files); 1021 } 1022 1023 // Create a Sized_relobj_dwo of the given size and endianness, 1024 // and record the target info. 1025 1026 Relobj* 1027 Dwo_file::make_object(Dwp_output_file* output_file) 1028 { 1029 // Open the input file. 1030 Input_file* input_file = new Input_file(this->name_); 1031 this->input_file_ = input_file; 1032 Dirsearch dirpath; 1033 int index; 1034 if (!input_file->open(dirpath, NULL, &index)) 1035 gold_fatal(_("%s: can't open"), this->name_); 1036 1037 // Check that it's an ELF file. 1038 off_t filesize = input_file->file().filesize(); 1039 int hdrsize = elfcpp::Elf_recognizer::max_header_size; 1040 if (filesize < hdrsize) 1041 hdrsize = filesize; 1042 const unsigned char* elf_header = 1043 input_file->file().get_view(0, 0, hdrsize, true, false); 1044 if (!elfcpp::Elf_recognizer::is_elf_file(elf_header, hdrsize)) 1045 gold_fatal(_("%s: not an ELF object file"), this->name_); 1046 1047 // Get the size, endianness, machine, etc. info from the header, 1048 // make an appropriately-sized Relobj, and pass the target info 1049 // to the output object. 1050 int size; 1051 bool big_endian; 1052 std::string error; 1053 if (!elfcpp::Elf_recognizer::is_valid_header(elf_header, hdrsize, &size, 1054 &big_endian, &error)) 1055 gold_fatal(_("%s: %s"), this->name_, error.c_str()); 1056 1057 if (size == 32) 1058 { 1059 if (big_endian) 1060 #ifdef HAVE_TARGET_32_BIG 1061 return this->sized_make_object<32, true>(elf_header, input_file, 1062 output_file); 1063 #else 1064 gold_unreachable(); 1065 #endif 1066 else 1067 #ifdef HAVE_TARGET_32_LITTLE 1068 return this->sized_make_object<32, false>(elf_header, input_file, 1069 output_file); 1070 #else 1071 gold_unreachable(); 1072 #endif 1073 } 1074 else if (size == 64) 1075 { 1076 if (big_endian) 1077 #ifdef HAVE_TARGET_64_BIG 1078 return this->sized_make_object<64, true>(elf_header, input_file, 1079 output_file); 1080 #else 1081 gold_unreachable(); 1082 #endif 1083 else 1084 #ifdef HAVE_TARGET_64_LITTLE 1085 return this->sized_make_object<64, false>(elf_header, input_file, 1086 output_file); 1087 #else 1088 gold_unreachable(); 1089 #endif 1090 } 1091 else 1092 gold_unreachable(); 1093 } 1094 1095 // Function template to create a Sized_relobj_dwo and record the target info. 1096 // P is a pointer to the ELF header in memory. 1097 1098 template <int size, bool big_endian> 1099 Relobj* 1100 Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file, 1101 Dwp_output_file* output_file) 1102 { 1103 elfcpp::Ehdr<size, big_endian> ehdr(p); 1104 Sized_relobj_dwo<size, big_endian>* obj = 1105 new Sized_relobj_dwo<size, big_endian>(this->name_, input_file, ehdr); 1106 obj->setup(); 1107 if (output_file != NULL) 1108 output_file->record_target_info( 1109 this->name_, ehdr.get_e_machine(), size, big_endian, 1110 ehdr.get_e_ident()[elfcpp::EI_OSABI], 1111 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); 1112 return obj; 1113 } 1114 1115 // Read the .debug_cu_index or .debug_tu_index section of a .dwp file, 1116 // and process the CU or TU sets. 1117 1118 void 1119 Dwo_file::read_unit_index(unsigned int shndx, unsigned int *debug_shndx, 1120 Dwp_output_file* output_file, bool is_tu_index) 1121 { 1122 if (this->obj_->is_big_endian()) 1123 this->sized_read_unit_index<true>(shndx, debug_shndx, output_file, 1124 is_tu_index); 1125 else 1126 this->sized_read_unit_index<false>(shndx, debug_shndx, output_file, 1127 is_tu_index); 1128 } 1129 1130 template <bool big_endian> 1131 void 1132 Dwo_file::sized_read_unit_index(unsigned int shndx, 1133 unsigned int *debug_shndx, 1134 Dwp_output_file* output_file, 1135 bool is_tu_index) 1136 { 1137 elfcpp::DW_SECT info_sect = (is_tu_index 1138 ? elfcpp::DW_SECT_TYPES 1139 : elfcpp::DW_SECT_INFO); 1140 unsigned int info_shndx = debug_shndx[info_sect]; 1141 1142 gold_assert(shndx > 0); 1143 1144 section_size_type index_len; 1145 bool index_is_new; 1146 const unsigned char* contents = 1147 this->section_contents(shndx, &index_len, &index_is_new); 1148 1149 unsigned int version = 1150 elfcpp::Swap_unaligned<32, big_endian>::readval(contents); 1151 1152 // We don't support version 1 anymore because it was experimental 1153 // and because in normal use, dwp is not expected to read .dwp files 1154 // produced by an earlier version of the tool. 1155 if (version != 2) 1156 gold_fatal(_("%s: section %s has unsupported version number %d"), 1157 this->name_, this->section_name(shndx).c_str(), version); 1158 1159 unsigned int ncols = 1160 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1161 + sizeof(uint32_t)); 1162 unsigned int nused = 1163 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1164 + 2 * sizeof(uint32_t)); 1165 if (ncols == 0 || nused == 0) 1166 return; 1167 1168 gold_assert(info_shndx > 0); 1169 1170 unsigned int nslots = 1171 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1172 + 3 * sizeof(uint32_t)); 1173 1174 const unsigned char* phash = contents + 4 * sizeof(uint32_t); 1175 const unsigned char* pindex = phash + nslots * sizeof(uint64_t); 1176 const unsigned char* pcolhdrs = pindex + nslots * sizeof(uint32_t); 1177 const unsigned char* poffsets = pcolhdrs + ncols * sizeof(uint32_t); 1178 const unsigned char* psizes = poffsets + nused * ncols * sizeof(uint32_t); 1179 const unsigned char* pend = psizes + nused * ncols * sizeof(uint32_t); 1180 1181 if (pend > contents + index_len) 1182 gold_fatal(_("%s: section %s is corrupt"), this->name_, 1183 this->section_name(shndx).c_str()); 1184 1185 // Copy the related sections and track the section offsets and sizes. 1186 Section_bounds sections[elfcpp::DW_SECT_MAX + 1]; 1187 for (int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i) 1188 { 1189 if (debug_shndx[i] > 0) 1190 sections[i] = this->copy_section(output_file, debug_shndx[i], 1191 static_cast<elfcpp::DW_SECT>(i)); 1192 } 1193 1194 // Get the contents of the .debug_info.dwo or .debug_types.dwo section. 1195 section_size_type info_len; 1196 bool info_is_new; 1197 const unsigned char* info_contents = 1198 this->section_contents(info_shndx, &info_len, &info_is_new); 1199 1200 // Loop over the slots of the hash table. 1201 for (unsigned int i = 0; i < nslots; ++i) 1202 { 1203 uint64_t signature = 1204 elfcpp::Swap_unaligned<64, big_endian>::readval(phash); 1205 unsigned int index = 1206 elfcpp::Swap_unaligned<32, big_endian>::readval(pindex); 1207 if (index != 0 && (!is_tu_index || !output_file->lookup_tu(signature))) 1208 { 1209 Unit_set* unit_set = new Unit_set(); 1210 unit_set->signature = signature; 1211 const unsigned char* pch = pcolhdrs; 1212 const unsigned char* porow = 1213 poffsets + (index - 1) * ncols * sizeof(uint32_t); 1214 const unsigned char* psrow = 1215 psizes + (index - 1) * ncols * sizeof(uint32_t); 1216 1217 // Adjust the offset of each contribution within the input section 1218 // by the offset of the input section within the output section. 1219 for (unsigned int j = 0; j <= ncols; j++) 1220 { 1221 unsigned int dw_sect = 1222 elfcpp::Swap_unaligned<64, big_endian>::readval(pch); 1223 unsigned int offset = 1224 elfcpp::Swap_unaligned<64, big_endian>::readval(porow); 1225 unsigned int size = 1226 elfcpp::Swap_unaligned<64, big_endian>::readval(psrow); 1227 unit_set->sections[dw_sect].offset = (sections[dw_sect].offset 1228 + offset); 1229 unit_set->sections[dw_sect].size = size; 1230 pch += sizeof(uint32_t); 1231 porow += sizeof(uint32_t); 1232 psrow += sizeof(uint32_t); 1233 } 1234 1235 const unsigned char* unit_start = 1236 info_contents + unit_set->sections[info_sect].offset; 1237 section_size_type unit_length = unit_set->sections[info_sect].size; 1238 1239 // Dwp_output_file::add_contribution writes the .debug_info.dwo 1240 // section directly to the output file, so we only need to 1241 // duplicate contributions for .debug_types.dwo section. 1242 if (is_tu_index) 1243 { 1244 unsigned char *copy = new unsigned char[unit_length]; 1245 memcpy(copy, unit_start, unit_length); 1246 unit_start = copy; 1247 } 1248 section_offset_type off = 1249 output_file->add_contribution(info_sect, unit_start, 1250 unit_length, 1); 1251 unit_set->sections[info_sect].offset = off; 1252 if (is_tu_index) 1253 output_file->add_tu_set(unit_set); 1254 else 1255 output_file->add_cu_set(unit_set); 1256 } 1257 phash += sizeof(uint64_t); 1258 pindex += sizeof(uint32_t); 1259 } 1260 1261 if (index_is_new) 1262 delete[] contents; 1263 if (info_is_new) 1264 delete[] info_contents; 1265 } 1266 1267 // Verify the .debug_cu_index section of a .dwp file, comparing it 1268 // against the list of .dwo files referenced by the corresponding 1269 // executable file. 1270 1271 bool 1272 Dwo_file::verify_dwo_list(unsigned int shndx, const File_list& files) 1273 { 1274 if (this->obj_->is_big_endian()) 1275 return this->sized_verify_dwo_list<true>(shndx, files); 1276 else 1277 return this->sized_verify_dwo_list<false>(shndx, files); 1278 } 1279 1280 template <bool big_endian> 1281 bool 1282 Dwo_file::sized_verify_dwo_list(unsigned int shndx, const File_list& files) 1283 { 1284 gold_assert(shndx > 0); 1285 1286 section_size_type index_len; 1287 bool index_is_new; 1288 const unsigned char* contents = 1289 this->section_contents(shndx, &index_len, &index_is_new); 1290 1291 unsigned int version = 1292 elfcpp::Swap_unaligned<32, big_endian>::readval(contents); 1293 1294 // We don't support version 1 anymore because it was experimental 1295 // and because in normal use, dwp is not expected to read .dwp files 1296 // produced by an earlier version of the tool. 1297 if (version != 2) 1298 gold_fatal(_("%s: section %s has unsupported version number %d"), 1299 this->name_, this->section_name(shndx).c_str(), version); 1300 1301 unsigned int ncols = 1302 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1303 + sizeof(uint32_t)); 1304 unsigned int nused = 1305 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1306 + 2 * sizeof(uint32_t)); 1307 if (ncols == 0 || nused == 0) 1308 return true; 1309 1310 unsigned int nslots = 1311 elfcpp::Swap_unaligned<32, big_endian>::readval(contents 1312 + 3 * sizeof(uint32_t)); 1313 1314 const unsigned char* phash = contents + 4 * sizeof(uint32_t); 1315 const unsigned char* pindex = phash + nslots * sizeof(uint64_t); 1316 const unsigned char* pcolhdrs = pindex + nslots * sizeof(uint32_t); 1317 const unsigned char* poffsets = pcolhdrs + ncols * sizeof(uint32_t); 1318 const unsigned char* psizes = poffsets + nused * ncols * sizeof(uint32_t); 1319 const unsigned char* pend = psizes + nused * ncols * sizeof(uint32_t); 1320 1321 if (pend > contents + index_len) 1322 gold_fatal(_("%s: section %s is corrupt"), this->name_, 1323 this->section_name(shndx).c_str()); 1324 1325 int nmissing = 0; 1326 for (File_list::const_iterator f = files.begin(); f != files.end(); ++f) 1327 { 1328 uint64_t dwo_id = f->dwo_id; 1329 unsigned int slot = static_cast<unsigned int>(dwo_id) & (nslots - 1); 1330 const unsigned char* ph = phash + slot * sizeof(uint64_t); 1331 const unsigned char* pi = pindex + slot * sizeof(uint32_t); 1332 uint64_t probe = elfcpp::Swap_unaligned<64, big_endian>::readval(ph); 1333 uint32_t row_index = elfcpp::Swap_unaligned<32, big_endian>::readval(pi); 1334 if (row_index != 0 && probe != dwo_id) 1335 { 1336 unsigned int h2 = ((static_cast<unsigned int>(dwo_id >> 32) 1337 & (nslots - 1)) | 1); 1338 do 1339 { 1340 slot = (slot + h2) & (nslots - 1); 1341 ph = phash + slot * sizeof(uint64_t); 1342 pi = pindex + slot * sizeof(uint32_t); 1343 probe = elfcpp::Swap_unaligned<64, big_endian>::readval(ph); 1344 row_index = elfcpp::Swap_unaligned<32, big_endian>::readval(pi); 1345 } while (row_index != 0 && probe != dwo_id); 1346 } 1347 if (row_index == 0) 1348 { 1349 printf(_("missing .dwo file: %016llx %s\n"), 1350 static_cast<long long>(dwo_id), f->dwo_name.c_str()); 1351 ++nmissing; 1352 } 1353 } 1354 1355 gold_info(_("Found %d missing .dwo files"), nmissing); 1356 1357 if (index_is_new) 1358 delete[] contents; 1359 1360 return nmissing == 0; 1361 } 1362 1363 // Merge the input string table section into the output file. 1364 1365 void 1366 Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str) 1367 { 1368 section_size_type len; 1369 bool is_new; 1370 const unsigned char* pdata = this->section_contents(debug_str, &len, &is_new); 1371 const char* p = reinterpret_cast<const char*>(pdata); 1372 const char* pend = p + len; 1373 1374 // Check that the last string is null terminated. 1375 if (pend[-1] != '\0') 1376 gold_fatal(_("%s: last entry in string section '%s' " 1377 "is not null terminated"), 1378 this->name_, 1379 this->section_name(debug_str).c_str()); 1380 1381 // Count the number of strings in the section, and size the map. 1382 size_t count = 0; 1383 for (const char* pt = p; pt < pend; pt += strlen(pt) + 1) 1384 ++count; 1385 this->str_offset_map_.reserve(count + 1); 1386 1387 // Add the strings to the output string table, and record the new offsets 1388 // in the map. 1389 section_offset_type i = 0; 1390 section_offset_type new_offset; 1391 while (p < pend) 1392 { 1393 size_t len = strlen(p); 1394 new_offset = output_file->add_string(p, len); 1395 this->str_offset_map_.push_back(std::make_pair(i, new_offset)); 1396 p += len + 1; 1397 i += len + 1; 1398 } 1399 new_offset = 0; 1400 this->str_offset_map_.push_back(std::make_pair(i, new_offset)); 1401 if (is_new) 1402 delete[] pdata; 1403 } 1404 1405 // Copy a section from the input file to the output file. 1406 // Return the offset and length of this input section's contribution 1407 // in the output section. If copying .debug_str_offsets.dwo, remap 1408 // the string offsets for the output string table. 1409 1410 Section_bounds 1411 Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx, 1412 elfcpp::DW_SECT section_id) 1413 { 1414 // Some sections may be referenced from more than one set. 1415 // Don't copy a section more than once. 1416 if (this->sect_offsets_[shndx].size > 0) 1417 return this->sect_offsets_[shndx]; 1418 1419 // Get the section contents. Upon return, if IS_NEW is true, the memory 1420 // has been allocated via new; if false, the memory is part of the mapped 1421 // input file, and we will need to duplicate it so that it will persist 1422 // after we close the input file. 1423 section_size_type len; 1424 bool is_new; 1425 const unsigned char* contents = this->section_contents(shndx, &len, &is_new); 1426 1427 if (section_id == elfcpp::DW_SECT_STR_OFFSETS) 1428 { 1429 const unsigned char* remapped = this->remap_str_offsets(contents, len); 1430 if (is_new) 1431 delete[] contents; 1432 contents = remapped; 1433 } 1434 else if (!is_new) 1435 { 1436 unsigned char* copy = new unsigned char[len]; 1437 memcpy(copy, contents, len); 1438 contents = copy; 1439 } 1440 1441 // Add the contents of the input section to the output section. 1442 // The output file takes ownership of the memory pointed to by CONTENTS. 1443 section_offset_type off = output_file->add_contribution(section_id, contents, 1444 len, 1); 1445 1446 // Store the output section bounds. 1447 Section_bounds bounds(off, len); 1448 this->sect_offsets_[shndx] = bounds; 1449 1450 return bounds; 1451 } 1452 1453 // Remap the 1454 const unsigned char* 1455 Dwo_file::remap_str_offsets(const unsigned char* contents, 1456 section_size_type len) 1457 { 1458 if ((len & 3) != 0) 1459 gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"), 1460 this->name_); 1461 1462 if (this->obj_->is_big_endian()) 1463 return this->sized_remap_str_offsets<true>(contents, len); 1464 else 1465 return this->sized_remap_str_offsets<false>(contents, len); 1466 } 1467 1468 template <bool big_endian> 1469 const unsigned char* 1470 Dwo_file::sized_remap_str_offsets(const unsigned char* contents, 1471 section_size_type len) 1472 { 1473 unsigned char* remapped = new unsigned char[len]; 1474 const unsigned char* p = contents; 1475 unsigned char* q = remapped; 1476 while (len > 0) 1477 { 1478 unsigned int val = elfcpp::Swap_unaligned<32, big_endian>::readval(p); 1479 val = this->remap_str_offset(val); 1480 elfcpp::Swap_unaligned<32, big_endian>::writeval(q, val); 1481 len -= 4; 1482 p += 4; 1483 q += 4; 1484 } 1485 return remapped; 1486 } 1487 1488 unsigned int 1489 Dwo_file::remap_str_offset(section_offset_type val) 1490 { 1491 Str_offset_map_entry entry; 1492 entry.first = val; 1493 1494 Str_offset_map::const_iterator p = 1495 std::lower_bound(this->str_offset_map_.begin(), 1496 this->str_offset_map_.end(), 1497 entry, Offset_compare()); 1498 1499 if (p == this->str_offset_map_.end() || p->first > val) 1500 { 1501 if (p == this->str_offset_map_.begin()) 1502 return 0; 1503 --p; 1504 gold_assert(p->first <= val); 1505 } 1506 1507 return p->second + (val - p->first); 1508 } 1509 1510 // Add a set of .debug_info.dwo or .debug_types.dwo and related sections 1511 // to OUTPUT_FILE. 1512 1513 void 1514 Dwo_file::add_unit_set(Dwp_output_file* output_file, unsigned int *debug_shndx, 1515 bool is_debug_types) 1516 { 1517 unsigned int shndx = (is_debug_types 1518 ? debug_shndx[elfcpp::DW_SECT_TYPES] 1519 : debug_shndx[elfcpp::DW_SECT_INFO]); 1520 1521 gold_assert(shndx != 0); 1522 1523 if (debug_shndx[elfcpp::DW_SECT_ABBREV] == 0) 1524 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_); 1525 1526 // Copy the related sections and track the section offsets and sizes. 1527 Section_bounds sections[elfcpp::DW_SECT_MAX + 1]; 1528 for (int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i) 1529 { 1530 if (debug_shndx[i] > 0) 1531 sections[i] = this->copy_section(output_file, debug_shndx[i], 1532 static_cast<elfcpp::DW_SECT>(i)); 1533 } 1534 1535 // Parse the .debug_info or .debug_types section and add each compilation 1536 // or type unit to the output file, along with the contributions to the 1537 // related sections. 1538 Unit_reader reader(is_debug_types, this->obj_, shndx); 1539 reader.add_units(output_file, debug_shndx[elfcpp::DW_SECT_ABBREV], sections); 1540 } 1541 1542 // Class Dwp_output_file. 1543 1544 // Record the target info from an input file. On first call, we 1545 // set the ELF header values for the output file. On subsequent 1546 // calls, we just verify that the values match. 1547 1548 void 1549 Dwp_output_file::record_target_info(const char*, int machine, 1550 int size, bool big_endian, 1551 int osabi, int abiversion) 1552 { 1553 // TODO: Check the values on subsequent calls. 1554 if (this->size_ > 0) 1555 return; 1556 1557 this->machine_ = machine; 1558 this->size_ = size; 1559 this->big_endian_ = big_endian; 1560 this->osabi_ = osabi; 1561 this->abiversion_ = abiversion; 1562 1563 if (size == 32) 1564 this->next_file_offset_ = elfcpp::Elf_sizes<32>::ehdr_size; 1565 else if (size == 64) 1566 this->next_file_offset_ = elfcpp::Elf_sizes<64>::ehdr_size; 1567 else 1568 gold_unreachable(); 1569 1570 this->fd_ = ::fopen(this->name_, "wb"); 1571 if (this->fd_ == NULL) 1572 gold_fatal(_("%s: %s"), this->name_, strerror(errno)); 1573 1574 // Write zeroes for the ELF header initially. We'll write 1575 // the actual header during finalize(). 1576 static const char buf[elfcpp::Elf_sizes<64>::ehdr_size] = { 0 }; 1577 if (::fwrite(buf, 1, this->next_file_offset_, this->fd_) 1578 < (size_t) this->next_file_offset_) 1579 gold_fatal(_("%s: %s"), this->name_, strerror(errno)); 1580 } 1581 1582 // Add a string to the debug strings section. 1583 1584 section_offset_type 1585 Dwp_output_file::add_string(const char* str, size_t len) 1586 { 1587 Stringpool::Key key; 1588 this->stringpool_.add_with_length(str, len, true, &key); 1589 this->have_strings_ = true; 1590 // We aren't supposed to call get_offset() until after 1591 // calling set_string_offsets(), but the offsets will 1592 // not change unless optimizing the string pool. 1593 return this->stringpool_.get_offset_from_key(key); 1594 } 1595 1596 // Align the file offset to the given boundary. 1597 1598 static inline off_t 1599 align_offset(off_t off, int align) 1600 { 1601 return (off + align - 1) & ~(align - 1); 1602 } 1603 1604 // Add a new output section and return the section index. 1605 1606 unsigned int 1607 Dwp_output_file::add_output_section(const char* section_name, int align) 1608 { 1609 Section sect(section_name, align); 1610 this->sections_.push_back(sect); 1611 return this->shnum_++; 1612 } 1613 1614 // Add a contribution to a section in the output file, and return the offset 1615 // of the contribution within the output section. The .debug_info.dwo section 1616 // is expected to be the largest one, so we will write the contents of this 1617 // section directly to the output file as we receive contributions, allowing 1618 // us to free that memory as soon as possible. We will save the remaining 1619 // contributions until we finalize the layout of the output file. 1620 1621 section_offset_type 1622 Dwp_output_file::add_contribution(elfcpp::DW_SECT section_id, 1623 const unsigned char* contents, 1624 section_size_type len, 1625 int align) 1626 { 1627 const char* section_name = get_dwarf_section_name(section_id); 1628 gold_assert(static_cast<size_t>(section_id) < this->section_id_map_.size()); 1629 unsigned int shndx = this->section_id_map_[section_id]; 1630 1631 // Create the section if necessary. 1632 if (shndx == 0) 1633 { 1634 section_name = this->shstrtab_.add_with_length(section_name, 1635 strlen(section_name), 1636 false, NULL); 1637 shndx = this->add_output_section(section_name, align); 1638 this->section_id_map_[section_id] = shndx; 1639 } 1640 1641 Section& section = this->sections_[shndx - 1]; 1642 1643 section_offset_type section_offset; 1644 1645 if (section_id == elfcpp::DW_SECT_INFO) 1646 { 1647 // Write the .debug_info.dwo section directly. 1648 // We do not need to free the memory in this case. 1649 off_t file_offset = this->next_file_offset_; 1650 gold_assert(this->size_ > 0 && file_offset > 0); 1651 1652 file_offset = align_offset(file_offset, align); 1653 if (section.offset == 0) 1654 section.offset = file_offset; 1655 1656 if (align > section.align) 1657 { 1658 // Since we've already committed to the layout for this 1659 // section, an unexpected large alignment boundary may 1660 // be impossible to honor. 1661 if (align_offset(section.offset, align) != section.offset) 1662 gold_fatal(_("%s: alignment (%d) for section '%s' " 1663 "cannot be honored"), 1664 this->name_, align, section_name); 1665 section.align = align; 1666 } 1667 1668 section_offset = file_offset - section.offset; 1669 section.size = file_offset + len - section.offset; 1670 1671 ::fseek(this->fd_, file_offset, SEEK_SET); 1672 if (::fwrite(contents, 1, len, this->fd_) < len) 1673 gold_fatal(_("%s: error writing section '%s'"), this->name_, 1674 section_name); 1675 this->next_file_offset_ = file_offset + len; 1676 } 1677 else 1678 { 1679 // Collect the contributions and keep track of the total size. 1680 if (align > section.align) 1681 section.align = align; 1682 section_offset = align_offset(section.size, align); 1683 section.size = section_offset + len; 1684 Contribution contrib = { section_offset, len, contents }; 1685 section.contributions.push_back(contrib); 1686 } 1687 1688 return section_offset; 1689 } 1690 1691 // Add a set of .debug_info and related sections to the output file. 1692 1693 void 1694 Dwp_output_file::add_cu_set(Unit_set* cu_set) 1695 { 1696 uint64_t dwo_id = cu_set->signature; 1697 unsigned int slot; 1698 if (!this->cu_index_.find_or_add(dwo_id, &slot)) 1699 this->cu_index_.enter_set(slot, cu_set); 1700 else 1701 gold_warning(_("%s: duplicate entry for CU (dwo_id 0x%llx)"), 1702 this->name_, (unsigned long long)dwo_id); 1703 } 1704 1705 // Lookup a type signature and return TRUE if we have already seen it. 1706 bool 1707 Dwp_output_file::lookup_tu(uint64_t type_sig) 1708 { 1709 this->last_type_sig_ = type_sig; 1710 return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_); 1711 } 1712 1713 // Add a set of .debug_types and related sections to the output file. 1714 1715 void 1716 Dwp_output_file::add_tu_set(Unit_set* tu_set) 1717 { 1718 uint64_t type_sig = tu_set->signature; 1719 unsigned int slot; 1720 if (type_sig == this->last_type_sig_) 1721 slot = this->last_tu_slot_; 1722 else 1723 this->tu_index_.find_or_add(type_sig, &slot); 1724 this->tu_index_.enter_set(slot, tu_set); 1725 } 1726 1727 // Find a slot in the hash table for SIGNATURE. Return TRUE 1728 // if the entry already exists. 1729 1730 bool 1731 Dwp_output_file::Dwp_index::find_or_add(uint64_t signature, 1732 unsigned int* slotp) 1733 { 1734 if (this->capacity_ == 0) 1735 this->initialize(); 1736 unsigned int slot = 1737 static_cast<unsigned int>(signature) & (this->capacity_ - 1); 1738 unsigned int secondary_hash; 1739 uint64_t probe = this->hash_table_[slot]; 1740 uint32_t row_index = this->index_table_[slot]; 1741 if (row_index != 0 && probe != signature) 1742 { 1743 secondary_hash = (static_cast<unsigned int>(signature >> 32) 1744 & (this->capacity_ - 1)) | 1; 1745 do 1746 { 1747 slot = (slot + secondary_hash) & (this->capacity_ - 1); 1748 probe = this->hash_table_[slot]; 1749 row_index = this->index_table_[slot]; 1750 } while (row_index != 0 && probe != signature); 1751 } 1752 *slotp = slot; 1753 return (row_index != 0); 1754 } 1755 1756 // Enter a CU or TU set at the given SLOT in the hash table. 1757 1758 void 1759 Dwp_output_file::Dwp_index::enter_set(unsigned int slot, 1760 const Unit_set* set) 1761 { 1762 gold_assert(slot < this->capacity_); 1763 1764 // Add a row to the offsets and sizes tables. 1765 this->section_table_.push_back(set); 1766 uint32_t row_index = this->section_table_rows(); 1767 1768 // Mark the sections used in this set. 1769 for (unsigned int i = 1; i <= elfcpp::DW_SECT_MAX; i++) 1770 if (set->sections[i].size > 0) 1771 this->section_mask_ |= 1 << i; 1772 1773 // Enter the signature and pool index into the hash table. 1774 gold_assert(this->hash_table_[slot] == 0); 1775 this->hash_table_[slot] = set->signature; 1776 this->index_table_[slot] = row_index; 1777 ++this->used_; 1778 1779 // Grow the hash table when we exceed 2/3 capacity. 1780 if (this->used_ * 3 > this->capacity_ * 2) 1781 this->grow(); 1782 } 1783 1784 // Initialize the hash table. 1785 1786 void 1787 Dwp_output_file::Dwp_index::initialize() 1788 { 1789 this->capacity_ = 16; 1790 this->hash_table_ = new uint64_t[this->capacity_]; 1791 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t)); 1792 this->index_table_ = new uint32_t[this->capacity_]; 1793 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t)); 1794 } 1795 1796 // Grow the hash table when we reach 2/3 capacity. 1797 1798 void 1799 Dwp_output_file::Dwp_index::grow() 1800 { 1801 unsigned int old_capacity = this->capacity_; 1802 uint64_t* old_hash_table = this->hash_table_; 1803 uint32_t* old_index_table = this->index_table_; 1804 unsigned int old_used = this->used_; 1805 1806 this->capacity_ = old_capacity * 2; 1807 this->hash_table_ = new uint64_t[this->capacity_]; 1808 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t)); 1809 this->index_table_ = new uint32_t[this->capacity_]; 1810 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t)); 1811 this->used_ = 0; 1812 1813 for (unsigned int i = 0; i < old_capacity; ++i) 1814 { 1815 uint64_t signature = old_hash_table[i]; 1816 uint32_t row_index = old_index_table[i]; 1817 if (row_index != 0) 1818 { 1819 unsigned int slot; 1820 bool found = this->find_or_add(signature, &slot); 1821 gold_assert(!found); 1822 this->hash_table_[slot] = signature; 1823 this->index_table_[slot] = row_index; 1824 ++this->used_; 1825 } 1826 } 1827 gold_assert(this->used_ == old_used); 1828 1829 delete[] old_hash_table; 1830 delete[] old_index_table; 1831 } 1832 1833 // Finalize the file, write the string tables and index sections, 1834 // and close the file. 1835 1836 void 1837 Dwp_output_file::finalize() 1838 { 1839 unsigned char* buf; 1840 1841 // Write the accumulated output sections. 1842 for (unsigned int i = 0; i < this->sections_.size(); i++) 1843 { 1844 Section& sect = this->sections_[i]; 1845 // If the offset has already been assigned, the section has been written. 1846 if (sect.offset > 0 || sect.size == 0) 1847 continue; 1848 off_t file_offset = this->next_file_offset_; 1849 file_offset = align_offset(file_offset, sect.align); 1850 sect.offset = file_offset; 1851 this->write_contributions(sect); 1852 this->next_file_offset_ = file_offset + sect.size; 1853 } 1854 1855 // Write the debug string table. 1856 if (this->have_strings_) 1857 { 1858 this->stringpool_.set_string_offsets(); 1859 section_size_type len = this->stringpool_.get_strtab_size(); 1860 buf = new unsigned char[len]; 1861 this->stringpool_.write_to_buffer(buf, len); 1862 this->write_new_section(".debug_str.dwo", buf, len, 1); 1863 delete[] buf; 1864 } 1865 1866 // Write the CU and TU indexes. 1867 if (this->big_endian_) 1868 { 1869 this->write_index<true>(".debug_cu_index", this->cu_index_); 1870 this->write_index<true>(".debug_tu_index", this->tu_index_); 1871 } 1872 else 1873 { 1874 this->write_index<false>(".debug_cu_index", this->cu_index_); 1875 this->write_index<false>(".debug_tu_index", this->tu_index_); 1876 } 1877 1878 off_t file_offset = this->next_file_offset_; 1879 1880 // Write the section string table. 1881 this->shstrndx_ = this->shnum_++; 1882 const char* shstrtab_name = 1883 this->shstrtab_.add_with_length(".shstrtab", sizeof(".shstrtab") - 1, 1884 false, NULL); 1885 this->shstrtab_.set_string_offsets(); 1886 section_size_type shstrtab_len = this->shstrtab_.get_strtab_size(); 1887 buf = new unsigned char[shstrtab_len]; 1888 this->shstrtab_.write_to_buffer(buf, shstrtab_len); 1889 off_t shstrtab_off = file_offset; 1890 ::fseek(this->fd_, file_offset, 0); 1891 if (::fwrite(buf, 1, shstrtab_len, this->fd_) < shstrtab_len) 1892 gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_); 1893 delete[] buf; 1894 file_offset += shstrtab_len; 1895 1896 // Write the section header table. The first entry is a NULL entry. 1897 // This is followed by the debug sections, and finally we write the 1898 // .shstrtab section header. 1899 file_offset = align_offset(file_offset, this->size_ == 32 ? 4 : 8); 1900 this->shoff_ = file_offset; 1901 ::fseek(this->fd_, file_offset, 0); 1902 section_size_type sh0_size = 0; 1903 unsigned int sh0_link = 0; 1904 if (this->shnum_ >= elfcpp::SHN_LORESERVE) 1905 sh0_size = this->shnum_; 1906 if (this->shstrndx_ >= elfcpp::SHN_LORESERVE) 1907 sh0_link = this->shstrndx_; 1908 this->write_shdr(NULL, 0, 0, 0, 0, sh0_size, sh0_link, 0, 0, 0); 1909 for (unsigned int i = 0; i < this->sections_.size(); ++i) 1910 { 1911 Section& sect = this->sections_[i]; 1912 this->write_shdr(sect.name, elfcpp::SHT_PROGBITS, 0, 0, sect.offset, 1913 sect.size, 0, 0, sect.align, 0); 1914 } 1915 this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0, 1916 shstrtab_off, shstrtab_len, 0, 0, 1, 0); 1917 1918 // Write the ELF header. 1919 this->write_ehdr(); 1920 1921 // Close the file. 1922 if (this->fd_ != NULL) 1923 { 1924 if (::fclose(this->fd_) != 0) 1925 gold_fatal(_("%s: %s"), this->name_, strerror(errno)); 1926 } 1927 this->fd_ = NULL; 1928 } 1929 1930 // Write the contributions to an output section. 1931 1932 void 1933 Dwp_output_file::write_contributions(const Section& sect) 1934 { 1935 for (unsigned int i = 0; i < sect.contributions.size(); ++i) 1936 { 1937 const Contribution& c = sect.contributions[i]; 1938 ::fseek(this->fd_, sect.offset + c.output_offset, SEEK_SET); 1939 if (::fwrite(c.contents, 1, c.size, this->fd_) < c.size) 1940 gold_fatal(_("%s: error writing section '%s'"), this->name_, sect.name); 1941 delete[] c.contents; 1942 } 1943 } 1944 1945 // Write a new section to the output file. 1946 1947 void 1948 Dwp_output_file::write_new_section(const char* section_name, 1949 const unsigned char* contents, 1950 section_size_type len, int align) 1951 { 1952 section_name = this->shstrtab_.add_with_length(section_name, 1953 strlen(section_name), 1954 false, NULL); 1955 unsigned int shndx = this->add_output_section(section_name, align); 1956 Section& section = this->sections_[shndx - 1]; 1957 off_t file_offset = this->next_file_offset_; 1958 file_offset = align_offset(file_offset, align); 1959 section.offset = file_offset; 1960 section.size = len; 1961 ::fseek(this->fd_, file_offset, SEEK_SET); 1962 if (::fwrite(contents, 1, len, this->fd_) < len) 1963 gold_fatal(_("%s: error writing section '%s'"), this->name_, section_name); 1964 this->next_file_offset_ = file_offset + len; 1965 } 1966 1967 // Write a CU or TU index section. 1968 1969 template<bool big_endian> 1970 void 1971 Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index) 1972 { 1973 const unsigned int nslots = index.hash_table_total_slots(); 1974 const unsigned int nused = index.hash_table_used_slots(); 1975 const unsigned int nrows = index.section_table_rows(); 1976 1977 int column_mask = index.section_table_cols(); 1978 unsigned int ncols = 0; 1979 for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c) 1980 if (column_mask & (1 << c)) 1981 ncols++; 1982 const unsigned int ntable = (nrows * 2 + 1) * ncols; 1983 1984 const section_size_type index_size = (4 * sizeof(uint32_t) 1985 + nslots * sizeof(uint64_t) 1986 + nslots * sizeof(uint32_t) 1987 + ntable * sizeof(uint32_t)); 1988 1989 // Allocate a buffer for the section contents. 1990 unsigned char* buf = new unsigned char[index_size]; 1991 unsigned char* p = buf; 1992 1993 // Write the section header: version number, padding, 1994 // number of used slots and total number of slots. 1995 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 2); 1996 p += sizeof(uint32_t); 1997 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, ncols); 1998 p += sizeof(uint32_t); 1999 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nused); 2000 p += sizeof(uint32_t); 2001 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nslots); 2002 p += sizeof(uint32_t); 2003 2004 // Write the hash table. 2005 for (unsigned int i = 0; i < nslots; ++i) 2006 { 2007 elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i)); 2008 p += sizeof(uint64_t); 2009 } 2010 2011 // Write the parallel index table. 2012 for (unsigned int i = 0; i < nslots; ++i) 2013 { 2014 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i)); 2015 p += sizeof(uint32_t); 2016 } 2017 2018 // Write the first row of the table of section offsets. 2019 for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c) 2020 { 2021 if (column_mask & (1 << c)) 2022 { 2023 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, c); 2024 p += sizeof(uint32_t); 2025 } 2026 } 2027 2028 // Write the table of section offsets. 2029 Dwp_index::Section_table::const_iterator tbl = index.section_table(); 2030 for (unsigned int r = 0; r < nrows; ++r) 2031 { 2032 gold_assert(tbl != index.section_table_end()); 2033 const Section_bounds* sects = (*tbl)->sections; 2034 for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c) 2035 { 2036 if (column_mask & (1 << c)) 2037 { 2038 section_offset_type offset = sects[c].offset; 2039 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, offset); 2040 p += sizeof(uint32_t); 2041 } 2042 else 2043 gold_assert(sects[c].size == 0); 2044 } 2045 ++tbl; 2046 } 2047 2048 // Write the table of section sizes. 2049 tbl = index.section_table(); 2050 for (unsigned int r = 0; r < nrows; ++r) 2051 { 2052 gold_assert(tbl != index.section_table_end()); 2053 const Section_bounds* sects = (*tbl)->sections; 2054 for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c) 2055 { 2056 if (column_mask & (1 << c)) 2057 { 2058 section_size_type size = sects[c].size; 2059 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, size); 2060 p += sizeof(uint32_t); 2061 } 2062 else 2063 gold_assert(sects[c].size == 0); 2064 } 2065 ++tbl; 2066 } 2067 2068 gold_assert(p == buf + index_size); 2069 2070 this->write_new_section(sect_name, buf, index_size, sizeof(uint64_t)); 2071 2072 delete[] buf; 2073 } 2074 2075 // Write the ELF header. 2076 2077 void 2078 Dwp_output_file::write_ehdr() 2079 { 2080 if (this->size_ == 32) 2081 { 2082 if (this->big_endian_) 2083 return this->sized_write_ehdr<32, true>(); 2084 else 2085 return this->sized_write_ehdr<32, false>(); 2086 } 2087 else if (this->size_ == 64) 2088 { 2089 if (this->big_endian_) 2090 return this->sized_write_ehdr<64, true>(); 2091 else 2092 return this->sized_write_ehdr<64, false>(); 2093 } 2094 else 2095 gold_unreachable(); 2096 } 2097 2098 template<unsigned int size, bool big_endian> 2099 void 2100 Dwp_output_file::sized_write_ehdr() 2101 { 2102 const unsigned int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; 2103 unsigned char buf[ehdr_size]; 2104 elfcpp::Ehdr_write<size, big_endian> ehdr(buf); 2105 2106 unsigned char e_ident[elfcpp::EI_NIDENT]; 2107 memset(e_ident, 0, elfcpp::EI_NIDENT); 2108 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; 2109 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; 2110 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; 2111 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; 2112 if (size == 32) 2113 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; 2114 else if (size == 64) 2115 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; 2116 else 2117 gold_unreachable(); 2118 e_ident[elfcpp::EI_DATA] = (big_endian 2119 ? elfcpp::ELFDATA2MSB 2120 : elfcpp::ELFDATA2LSB); 2121 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; 2122 ehdr.put_e_ident(e_ident); 2123 2124 ehdr.put_e_type(elfcpp::ET_REL); 2125 ehdr.put_e_machine(this->machine_); 2126 ehdr.put_e_version(elfcpp::EV_CURRENT); 2127 ehdr.put_e_entry(0); 2128 ehdr.put_e_phoff(0); 2129 ehdr.put_e_shoff(this->shoff_); 2130 ehdr.put_e_flags(0); 2131 ehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); 2132 ehdr.put_e_phentsize(0); 2133 ehdr.put_e_phnum(0); 2134 ehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); 2135 ehdr.put_e_shnum(this->shnum_ < elfcpp::SHN_LORESERVE ? this->shnum_ : 0); 2136 ehdr.put_e_shstrndx(this->shstrndx_ < elfcpp::SHN_LORESERVE 2137 ? this->shstrndx_ 2138 : static_cast<unsigned int>(elfcpp::SHN_XINDEX)); 2139 2140 ::fseek(this->fd_, 0, 0); 2141 if (::fwrite(buf, 1, ehdr_size, this->fd_) < ehdr_size) 2142 gold_fatal(_("%s: error writing ELF header"), this->name_); 2143 } 2144 2145 // Write a section header. 2146 2147 void 2148 Dwp_output_file::write_shdr(const char* name, unsigned int type, 2149 unsigned int flags, uint64_t addr, off_t offset, 2150 section_size_type sect_size, unsigned int link, 2151 unsigned int info, unsigned int align, 2152 unsigned int ent_size) 2153 { 2154 if (this->size_ == 32) 2155 { 2156 if (this->big_endian_) 2157 return this->sized_write_shdr<32, true>(name, type, flags, addr, 2158 offset, sect_size, link, info, 2159 align, ent_size); 2160 else 2161 return this->sized_write_shdr<32, false>(name, type, flags, addr, 2162 offset, sect_size, link, info, 2163 align, ent_size); 2164 } 2165 else if (this->size_ == 64) 2166 { 2167 if (this->big_endian_) 2168 return this->sized_write_shdr<64, true>(name, type, flags, addr, 2169 offset, sect_size, link, info, 2170 align, ent_size); 2171 else 2172 return this->sized_write_shdr<64, false>(name, type, flags, addr, 2173 offset, sect_size, link, info, 2174 align, ent_size); 2175 } 2176 else 2177 gold_unreachable(); 2178 } 2179 2180 template<unsigned int size, bool big_endian> 2181 void 2182 Dwp_output_file::sized_write_shdr(const char* name, unsigned int type, 2183 unsigned int flags, uint64_t addr, 2184 off_t offset, section_size_type sect_size, 2185 unsigned int link, unsigned int info, 2186 unsigned int align, unsigned int ent_size) 2187 { 2188 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 2189 unsigned char buf[shdr_size]; 2190 elfcpp::Shdr_write<size, big_endian> shdr(buf); 2191 2192 shdr.put_sh_name(name == NULL ? 0 : this->shstrtab_.get_offset(name)); 2193 shdr.put_sh_type(type); 2194 shdr.put_sh_flags(flags); 2195 shdr.put_sh_addr(addr); 2196 shdr.put_sh_offset(offset); 2197 shdr.put_sh_size(sect_size); 2198 shdr.put_sh_link(link); 2199 shdr.put_sh_info(info); 2200 shdr.put_sh_addralign(align); 2201 shdr.put_sh_entsize(ent_size); 2202 if (::fwrite(buf, 1, shdr_size, this->fd_) < shdr_size) 2203 gold_fatal(_("%s: error writing section header table"), this->name_); 2204 } 2205 2206 // Class Dwo_name_info_reader. 2207 2208 // Visit a compilation unit. 2209 2210 void 2211 Dwo_name_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die) 2212 { 2213 const char* dwo_name = die->string_attribute(elfcpp::DW_AT_GNU_dwo_name); 2214 if (dwo_name != NULL) 2215 { 2216 uint64_t dwo_id = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id); 2217 this->files_->push_back(Dwo_file_entry(dwo_id, dwo_name)); 2218 } 2219 } 2220 2221 // Class Unit_reader. 2222 2223 // Read the CUs or TUs and add them to the output file. 2224 2225 void 2226 Unit_reader::add_units(Dwp_output_file* output_file, 2227 unsigned int debug_abbrev, 2228 Section_bounds* sections) 2229 { 2230 this->output_file_ = output_file; 2231 this->sections_ = sections; 2232 this->set_abbrev_shndx(debug_abbrev); 2233 this->parse(); 2234 } 2235 2236 // Visit a compilation unit. 2237 2238 void 2239 Unit_reader::visit_compilation_unit(off_t, off_t cu_length, Dwarf_die* die) 2240 { 2241 if (cu_length == 0) 2242 return; 2243 2244 Unit_set* unit_set = new Unit_set(); 2245 unit_set->signature = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id); 2246 for (unsigned int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i) 2247 unit_set->sections[i] = this->sections_[i]; 2248 2249 // Dwp_output_file::add_contribution writes the .debug_info.dwo section 2250 // directly to the output file, so we do not need to duplicate the 2251 // section contents, and add_contribution does not need to free the memory. 2252 section_offset_type off = 2253 this->output_file_->add_contribution(elfcpp::DW_SECT_INFO, 2254 this->buffer_at_offset(0), 2255 cu_length, 1); 2256 Section_bounds bounds(off, cu_length); 2257 unit_set->sections[elfcpp::DW_SECT_INFO] = bounds; 2258 this->output_file_->add_cu_set(unit_set); 2259 } 2260 2261 // Visit a type unit. 2262 2263 void 2264 Unit_reader::visit_type_unit(off_t, off_t tu_length, off_t, 2265 uint64_t signature, Dwarf_die*) 2266 { 2267 if (tu_length == 0) 2268 return; 2269 if (this->output_file_->lookup_tu(signature)) 2270 return; 2271 2272 Unit_set* unit_set = new Unit_set(); 2273 unit_set->signature = signature; 2274 for (unsigned int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i) 2275 unit_set->sections[i] = this->sections_[i]; 2276 2277 unsigned char* contents = new unsigned char[tu_length]; 2278 memcpy(contents, this->buffer_at_offset(0), tu_length); 2279 section_offset_type off = 2280 this->output_file_->add_contribution(elfcpp::DW_SECT_TYPES, contents, 2281 tu_length, 1); 2282 Section_bounds bounds(off, tu_length); 2283 unit_set->sections[elfcpp::DW_SECT_TYPES] = bounds; 2284 this->output_file_->add_tu_set(unit_set); 2285 } 2286 2287 }; // End namespace gold 2288 2289 using namespace gold; 2290 2291 // Options. 2292 2293 enum Dwp_options { 2294 VERIFY_ONLY = 0x101, 2295 }; 2296 2297 struct option dwp_options[] = 2298 { 2299 { "exec", required_argument, NULL, 'e' }, 2300 { "help", no_argument, NULL, 'h' }, 2301 { "output", required_argument, NULL, 'o' }, 2302 { "verbose", no_argument, NULL, 'v' }, 2303 { "verify-only", no_argument, NULL, VERIFY_ONLY }, 2304 { "version", no_argument, NULL, 'V' }, 2305 { NULL, 0, NULL, 0 } 2306 }; 2307 2308 // Print usage message and exit. 2309 2310 static void 2311 usage(FILE* fd, int exit_status) 2312 { 2313 fprintf(fd, _("Usage: %s [options] [file...]\n"), program_name); 2314 fprintf(fd, _(" -h, --help Print this help message\n")); 2315 fprintf(fd, _(" -e EXE, --exec EXE Get list of dwo files from EXE" 2316 " (defaults output to EXE.dwp)\n")); 2317 fprintf(fd, _(" -o FILE, --output FILE Set output dwp file name\n")); 2318 fprintf(fd, _(" -v, --verbose Verbose output\n")); 2319 fprintf(fd, _(" --verify-only Verify output file against" 2320 " exec file\n")); 2321 fprintf(fd, _(" -V, --version Print version number\n")); 2322 2323 // REPORT_BUGS_TO is defined in bfd/bfdver.h. 2324 const char* report = REPORT_BUGS_TO; 2325 if (*report != '\0') 2326 fprintf(fd, _("\nReport bugs to %s\n"), report); 2327 exit(exit_status); 2328 } 2329 2330 // Report version information. 2331 2332 static void 2333 print_version() 2334 { 2335 // This output is intended to follow the GNU standards. 2336 printf("GNU dwp %s\n", BFD_VERSION_STRING); 2337 printf(_("Copyright (C) 2016 Free Software Foundation, Inc.\n")); 2338 printf(_("\ 2339 This program is free software; you may redistribute it under the terms of\n\ 2340 the GNU General Public License version 3 or (at your option) any later version.\n\ 2341 This program has absolutely no warranty.\n")); 2342 exit(EXIT_SUCCESS); 2343 } 2344 2345 // Main program. 2346 2347 int 2348 main(int argc, char** argv) 2349 { 2350 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 2351 setlocale(LC_MESSAGES, ""); 2352 #endif 2353 #if defined (HAVE_SETLOCALE) 2354 setlocale(LC_CTYPE, ""); 2355 #endif 2356 bindtextdomain(PACKAGE, LOCALEDIR); 2357 textdomain(PACKAGE); 2358 2359 program_name = argv[0]; 2360 2361 // Initialize the global parameters, to let random code get to the 2362 // errors object. 2363 Errors errors(program_name); 2364 set_parameters_errors(&errors); 2365 2366 // Initialize gold's global options. We don't use these in 2367 // this program, but they need to be initialized so that 2368 // functions we call from libgold work properly. 2369 General_options options; 2370 set_parameters_options(&options); 2371 2372 // In libiberty; expands @filename to the args in "filename". 2373 expandargv(&argc, &argv); 2374 2375 // Collect file names and options. 2376 File_list files; 2377 std::string output_filename; 2378 const char* exe_filename = NULL; 2379 bool verbose = false; 2380 bool verify_only = false; 2381 int c; 2382 while ((c = getopt_long(argc, argv, "e:ho:vV", dwp_options, NULL)) != -1) 2383 { 2384 switch (c) 2385 { 2386 case 'h': 2387 usage(stdout, EXIT_SUCCESS); 2388 case 'e': 2389 exe_filename = optarg; 2390 break; 2391 case 'o': 2392 output_filename.assign(optarg); 2393 break; 2394 case 'v': 2395 verbose = true; 2396 break; 2397 case VERIFY_ONLY: 2398 verify_only = true; 2399 break; 2400 case 'V': 2401 print_version(); 2402 case '?': 2403 default: 2404 usage(stderr, EXIT_FAILURE); 2405 } 2406 } 2407 2408 if (output_filename.empty()) 2409 { 2410 if (exe_filename == NULL) 2411 gold_fatal(_("no output file specified")); 2412 output_filename.assign(exe_filename); 2413 output_filename.append(".dwp"); 2414 } 2415 2416 // Get list of .dwo files from the executable. 2417 if (exe_filename != NULL) 2418 { 2419 Dwo_file exe_file(exe_filename); 2420 exe_file.read_executable(&files); 2421 } 2422 2423 // Add any additional files listed on command line. 2424 for (int i = optind; i < argc; ++i) 2425 files.push_back(Dwo_file_entry(0, argv[i])); 2426 2427 if (exe_filename == NULL && files.empty()) 2428 gold_fatal(_("no input files and no executable specified")); 2429 2430 if (verify_only) 2431 { 2432 // Get list of DWO files in the DWP file and compare with 2433 // references found in the EXE file. 2434 Dwo_file dwp_file(output_filename.c_str()); 2435 bool ok = dwp_file.verify(files); 2436 return ok ? EXIT_SUCCESS : EXIT_FAILURE; 2437 } 2438 2439 // Process each file, adding its contents to the output file. 2440 Dwp_output_file output_file(output_filename.c_str()); 2441 for (File_list::const_iterator f = files.begin(); f != files.end(); ++f) 2442 { 2443 if (verbose) 2444 fprintf(stderr, "%s\n", f->dwo_name.c_str()); 2445 Dwo_file dwo_file(f->dwo_name.c_str()); 2446 dwo_file.read(&output_file); 2447 } 2448 output_file.finalize(); 2449 2450 return EXIT_SUCCESS; 2451 } 2452