1 // dwp.cc -- DWARF packaging utility
3 // Copyright 2012 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
6 // This file is part of dwp, the DWARF packaging utility.
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.
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.
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.
36 #include "libiberty.h"
37 #include "../bfd/bfdver.h"
40 #include "elfcpp_file.h"
41 #include "dirsearch.h"
44 #include "compressed_output.h"
45 #include "stringpool.h"
46 #include "dwarf_reader.h"
49 usage(FILE* fd, int) ATTRIBUTE_NORETURN;
52 print_version() ATTRIBUTE_NORETURN;
56 class Dwp_output_file;
59 // This class may represent either a .dwo file or a .dwp file
60 // produced by an earlier run.
62 template <int size, bool big_endian>
63 class Sized_relobj_dwo;
68 Dwo_file(const char* name)
69 : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(),
75 // Read the input file and send its contents to OUTPUT_FILE.
77 read(Dwp_output_file* output_file);
80 // Types for mapping input string offsets to output string offsets.
81 typedef std::pair<section_offset_type, section_offset_type>
83 typedef std::vector<Str_offset_map_entry> Str_offset_map;
85 // A less-than comparison routine for Str_offset_map.
89 operator()(const Str_offset_map_entry& i1,
90 const Str_offset_map_entry& i2) const
91 { return i1.first < i2.first; }
94 // Create a Sized_relobj_dwo of the given size and endianness,
95 // and record the target info. P is a pointer to the ELF header
98 make_object(int size, bool big_endian, const unsigned char* p,
99 Input_file* input_file, Dwp_output_file* output_file);
101 template <int size, bool big_endian>
103 sized_make_object(const unsigned char* p, Input_file* input_file,
104 Dwp_output_file* output_file);
106 // Return the number of sections in the input object file.
109 { return this->obj_->shnum(); }
111 // Return section type.
113 section_type(unsigned int shndx)
114 { return this->obj_->section_type(shndx); }
116 // Get the name of a section.
118 section_name(unsigned int shndx)
119 { return this->obj_->section_name(shndx); }
121 // Return a view of the contents of a section, decompressed if necessary.
122 // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
123 // deleted by the caller.
125 section_contents(unsigned int shndx, section_size_type* plen, bool* is_new)
126 { return this->obj_->decompressed_section_contents(shndx, plen, is_new); }
128 // Read the .debug_cu_index section of a .dwp file,
129 // and process the CU sets.
131 read_compunit_index(unsigned int, Dwp_output_file*);
133 template <bool big_endian>
135 sized_read_compunit_index(unsigned int, Dwp_output_file*);
137 // Read the .debug_tu_index section of a .dwp file,
138 // and process the TU sets.
140 read_typeunit_index(unsigned int, Dwp_output_file*);
142 template <bool big_endian>
144 sized_read_typeunit_index(unsigned int, Dwp_output_file*);
146 // Merge the input string table section into the output file.
148 add_strings(Dwp_output_file*, unsigned int);
150 // Copy a section from the input file to the output file.
152 copy_section(Dwp_output_file* output_file, unsigned int shndx,
153 const char* section_name, bool is_str_offsets);
155 // Remap the string offsets in the .debug_str_offsets.dwo section.
157 remap_str_offsets(const unsigned char* contents, section_size_type len);
159 template <bool big_endian>
161 sized_remap_str_offsets(const unsigned char* contents, section_size_type len);
163 // Remap a single string offsets from an offset in the input string table
164 // to an offset in the output string table.
166 remap_str_offset(section_offset_type val);
168 // Add a set of .debug_info and related sections to OUTPUT_FILE.
170 add_cu_set(Dwp_output_file* output_file,
172 unsigned int debug_info,
173 unsigned int debug_abbrev,
174 unsigned int debug_line,
175 unsigned int debug_loc,
176 unsigned int debug_str_offsets,
177 unsigned int debug_macinfo,
178 unsigned int debug_macro);
180 // Add a set of .debug_types and related sections to OUTPUT_FILE.
182 add_tu_set(Dwp_output_file* output_file,
184 unsigned int debug_types,
185 unsigned int debug_abbrev,
186 unsigned int debug_line,
187 unsigned int debug_str_offsets);
191 // The ELF file, represented as a gold Relobj instance.
193 // The Input_file object.
194 Input_file* input_file_;
195 // Flags indicating which sections are compressed.
196 std::vector<bool> is_compressed_;
197 // Map input section index onto output section index.
198 std::vector<unsigned int> shndx_map_;
199 // Map input string offsets to output string offsets.
200 Str_offset_map str_offset_map_;
203 // An ELF input file.
204 // We derive from Sized_relobj so that we can use interfaces
205 // in libgold to access the file.
207 template <int size, bool big_endian>
208 class Sized_relobj_dwo : public Sized_relobj<size, big_endian>
211 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
212 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
214 Sized_relobj_dwo(const char* name, Input_file* input_file,
215 const elfcpp::Ehdr<size, big_endian>& ehdr)
216 : Sized_relobj<size, big_endian>(name, input_file),
217 elf_file_(this, ehdr)
223 // Setup the section information.
228 // Return section type.
230 do_section_type(unsigned int shndx)
231 { return this->elf_file_.section_type(shndx); }
233 // Get the name of a section.
235 do_section_name(unsigned int shndx)
236 { return this->elf_file_.section_name(shndx); }
238 // Get the size of a section.
240 do_section_size(unsigned int shndx)
241 { return this->elf_file_.section_size(shndx); }
243 // Return a view of the contents of a section.
245 do_section_contents(unsigned int, section_size_type*, bool);
247 // Return a view of the uncompressed contents of a section. Set *PLEN
248 // to the size. Set *IS_NEW to true if the contents need to be deleted
251 do_decompressed_section_contents(unsigned int shndx,
252 section_size_type* plen,
255 // The following virtual functions are abstract in the base classes,
256 // but are not used here.
260 do_read_symbols(Read_symbols_data*)
261 { gold_unreachable(); }
263 // Lay out the input sections.
265 do_layout(Symbol_table*, Layout*, Read_symbols_data*)
266 { gold_unreachable(); }
268 // Layout sections whose layout was deferred while waiting for
269 // input files from a plugin.
271 do_layout_deferred_sections(Layout*)
272 { gold_unreachable(); }
274 // Add the symbols to the symbol table.
276 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*)
277 { gold_unreachable(); }
279 Archive::Should_include
280 do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*,
282 { gold_unreachable(); }
284 // Iterate over global symbols, calling a visitor class V for each.
286 do_for_all_global_symbols(Read_symbols_data*,
287 Library_base::Symbol_visitor_base*)
288 { gold_unreachable(); }
290 // Return section flags.
292 do_section_flags(unsigned int)
293 { gold_unreachable(); }
295 // Return section entsize.
297 do_section_entsize(unsigned int)
298 { gold_unreachable(); }
300 // Return section address.
302 do_section_address(unsigned int)
303 { gold_unreachable(); }
305 // Return the section link field.
307 do_section_link(unsigned int)
308 { gold_unreachable(); }
310 // Return the section link field.
312 do_section_info(unsigned int)
313 { gold_unreachable(); }
315 // Return the section alignment.
317 do_section_addralign(unsigned int)
318 { gold_unreachable(); }
320 // Return the Xindex structure to use.
322 do_initialize_xindex()
323 { gold_unreachable(); }
325 // Get symbol counts.
327 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const
328 { gold_unreachable(); }
330 // Get global symbols.
332 do_get_global_symbols() const
335 // Return the value of a local symbol.
337 do_local_symbol_value(unsigned int, uint64_t) const
338 { gold_unreachable(); }
341 do_local_plt_offset(unsigned int) const
342 { gold_unreachable(); }
344 // Return whether local symbol SYMNDX is a TLS symbol.
346 do_local_is_tls(unsigned int) const
347 { gold_unreachable(); }
349 // Return the number of local symbols.
351 do_local_symbol_count() const
352 { gold_unreachable(); }
354 // Return the number of local symbols in the output symbol table.
356 do_output_local_symbol_count() const
357 { gold_unreachable(); }
359 // Return the file offset for local symbols in the output symbol table.
361 do_local_symbol_offset() const
362 { gold_unreachable(); }
366 do_read_relocs(Read_relocs_data*)
367 { gold_unreachable(); }
369 // Process the relocs to find list of referenced sections. Used only
370 // during garbage collection.
372 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*)
373 { gold_unreachable(); }
375 // Scan the relocs and adjust the symbol table.
377 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*)
378 { gold_unreachable(); }
380 // Count the local symbols.
382 do_count_local_symbols(Stringpool_template<char>*,
383 Stringpool_template<char>*)
384 { gold_unreachable(); }
386 // Finalize the local symbols.
388 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*)
389 { gold_unreachable(); }
391 // Set the offset where local dynamic symbol information will be stored.
393 do_set_local_dynsym_indexes(unsigned int)
394 { gold_unreachable(); }
396 // Set the offset where local dynamic symbol information will be stored.
398 do_set_local_dynsym_offset(off_t)
399 { gold_unreachable(); }
401 // Relocate the input sections and write out the local symbols.
403 do_relocate(const Symbol_table*, const Layout*, Output_file*)
404 { gold_unreachable(); }
407 // General access to the ELF file.
408 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
412 // This class is responsible for collecting the debug index information
413 // and writing the .dwp file in ELF format.
415 class Dwp_output_file
418 Dwp_output_file(const char* name)
419 : name_(name), machine_(0), size_(0), big_endian_(false), osabi_(0),
420 abiversion_(0), fd_(NULL), next_file_offset_(0), shnum_(1), sections_(),
421 shoff_(0), shstrndx_(0), have_strings_(false), stringpool_(),
422 shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
425 this->stringpool_.set_no_zero_null();
428 // Record the target info from an input file.
430 record_target_info(const char* name, int machine, int size, bool big_endian,
431 int osabi, int abiversion);
433 // Add a string to the debug strings section.
435 add_string(const char* str, size_t len);
437 // Add a section to the output file, and return the new section index.
439 add_section(const char* section_name, const unsigned char* contents,
440 section_size_type len, int align);
442 // Add a set of .debug_info and related sections to the output file.
444 add_cu_set(uint64_t dwo_id, unsigned int debug_info,
445 unsigned int debug_abbrev, unsigned int debug_line,
446 unsigned int debug_loc, unsigned int debug_str_offsets,
447 unsigned int debug_macinfo, unsigned int debug_macro);
449 // Lookup a type signature and return TRUE if we have already seen it.
451 lookup_tu(uint64_t type_sig);
453 // Add a set of .debug_types and related sections to the output file.
455 add_tu_set(uint64_t type_sig, unsigned int debug_types,
456 unsigned int debug_abbrev, unsigned int debug_line,
457 unsigned int debug_str_offsets);
459 // Finalize the file, write the string tables and index sections,
460 // and close the file.
465 // Sections in the output file.
470 section_size_type size;
474 // A set of sections for a compilation unit or type unit.
478 unsigned int debug_info_or_types;
479 unsigned int debug_abbrev;
480 unsigned int debug_line;
481 unsigned int debug_loc;
482 unsigned int debug_str_offsets;
483 unsigned int debug_macinfo;
484 unsigned int debug_macro;
487 // The index sections defined by the DWARF Package File Format spec.
491 // Vector for the section index pool.
492 typedef std::vector<unsigned int> Shndx_pool;
495 : capacity_(0), used_(0), hash_table_(NULL), shndx_pool_()
501 // Find a slot in the hash table for SIGNATURE. Return TRUE
502 // if the entry already exists.
504 find_or_add(uint64_t signature, unsigned int* slotp);
506 // Enter a CU or TU set at the given SLOT in the hash table.
508 enter_set(unsigned int slot, const Cu_or_tu_set& set);
510 // Return the contents of the given SLOT in the hash table of signatures.
512 hash_table(unsigned int slot) const
513 { return this->hash_table_[slot]; }
515 // Return the contents of the given SLOT in the parallel table of
516 // shndx pool indexes.
518 index_table(unsigned int slot) const
519 { return this->index_table_[slot]; }
521 // Return the total number of slots in the hash table.
523 hash_table_total_slots() const
524 { return this->capacity_; }
526 // Return the number of used slots in the hash table.
528 hash_table_used_slots() const
529 { return this->used_; }
531 // Return an iterator into the shndx pool.
532 Shndx_pool::const_iterator
534 { return this->shndx_pool_.begin(); }
536 Shndx_pool::const_iterator
537 shndx_pool_end() const
538 { return this->shndx_pool_.end(); }
540 // Return the number of entries in the shndx pool.
542 shndx_pool_size() const
543 { return this->shndx_pool_.size(); }
546 // Initialize the hash table.
550 // Grow the hash table when we reach 2/3 capacity.
554 // The number of slots in the table, a power of 2 such that
555 // capacity > 3 * size / 2.
556 unsigned int capacity_;
557 // The current number of used slots in the hash table.
559 // The storage for the hash table of signatures.
560 uint64_t* hash_table_;
561 // The storage for the parallel table of shndx pool indexes.
562 uint32_t* index_table_;
563 // The pool of section indexes.
564 Shndx_pool shndx_pool_;
565 }; // End class Dwp_output_file::Dwp_index.
567 // Initialize the output file.
571 // Write the ELF header.
575 template<unsigned int size, bool big_endian>
579 // Write a section header.
581 write_shdr(const char* name, unsigned int type, unsigned int flags,
582 uint64_t addr, off_t offset, section_size_type sect_size,
583 unsigned int link, unsigned int info,
584 unsigned int align, unsigned int ent_size);
586 template<unsigned int size, bool big_endian>
588 sized_write_shdr(const char* name, unsigned int type, unsigned int flags,
589 uint64_t addr, off_t offset, section_size_type sect_size,
590 unsigned int link, unsigned int info,
591 unsigned int align, unsigned int ent_size);
593 // Write a CU or TU index section.
594 template<bool big_endian>
596 write_index(const char* sect_name, const Dwp_index& index);
598 // The output filename.
600 // ELF header parameters.
606 // The output file descriptor.
608 // Next available file offset.
609 off_t next_file_offset_;
610 // The number of sections.
612 // Section table. The first entry is shndx 1.
613 std::vector<Section> sections_;
614 // File offset of the section header table.
616 // Section index of the section string table.
617 unsigned int shstrndx_;
618 // TRUE if we have added any strings to the string pool.
620 // String pool for the output .debug_str.dwo section.
621 Stringpool stringpool_;
622 // String pool for the .shstrtab section.
623 Stringpool shstrtab_;
624 // The compilation unit index.
626 // The type unit index.
628 // Cache of the last type signature looked up.
629 uint64_t last_type_sig_;
630 // Cache of the slot index for the last type signature.
631 unsigned int last_tu_slot_;
634 // A specialization of Dwarf_info_reader, for reading dwo_ids and
635 // type signatures from DWARF CUs and TUs.
637 class Dwo_id_info_reader : public Dwarf_info_reader
640 Dwo_id_info_reader(bool is_type_unit,
642 const unsigned char* symbols,
645 unsigned int reloc_shndx,
646 unsigned int reloc_type)
647 : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
648 reloc_shndx, reloc_type),
649 dwo_id_found_(false), dwo_id_(0), type_sig_found_(false), type_sig_(0)
652 ~Dwo_id_info_reader()
655 // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID.
657 get_dwo_id(uint64_t* dwo_id)
660 if (!this->dwo_id_found_)
662 *dwo_id = this->dwo_id_;
666 // Return the type signature from a DWARF type unit DIE in *TYPE_SIG.
668 get_type_sig(uint64_t* type_sig)
671 if (!this->type_sig_found_)
673 *type_sig = this->type_sig_;
678 // Visit a compilation unit.
680 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
682 // Visit a type unit.
684 visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature,
688 // Visit a top-level DIE.
690 visit_top_die(Dwarf_die* die);
692 // TRUE if we found a dwo_id.
696 // TRUE if we found a type signature.
697 bool type_sig_found_;
698 // The type signature.
702 // Class Sized_relobj_dwo.
704 // Setup the section information.
706 template <int size, bool big_endian>
708 Sized_relobj_dwo<size, big_endian>::setup()
710 const unsigned int shnum = this->elf_file_.shnum();
711 this->set_shnum(shnum);
712 this->section_offsets().resize(shnum);
715 // Return a view of the contents of a section.
717 template <int size, bool big_endian>
719 Sized_relobj_dwo<size, big_endian>::do_section_contents(
721 section_size_type* plen,
724 Object::Location loc(this->elf_file_.section_contents(shndx));
725 *plen = convert_to_section_size_type(loc.data_size);
728 static const unsigned char empty[1] = { '\0' };
731 return this->get_view(loc.file_offset, *plen, true, cache);
734 // Return a view of the uncompressed contents of a section. Set *PLEN
735 // to the size. Set *IS_NEW to true if the contents need to be deleted
738 template <int size, bool big_endian>
740 Sized_relobj_dwo<size, big_endian>::do_decompressed_section_contents(
742 section_size_type* plen,
745 section_size_type buffer_size;
746 const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
749 std::string sect_name = this->do_section_name(shndx);
750 if (!is_prefix_of(".zdebug_", sect_name.c_str()))
757 section_size_type uncompressed_size = get_uncompressed_size(buffer,
759 unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
760 if (!decompress_input_section(buffer,
764 this->error(_("could not decompress section %s"),
765 this->section_name(shndx).c_str());
766 *plen = uncompressed_size;
768 return uncompressed_data;
773 Dwo_file::~Dwo_file()
775 if (this->input_file_ != NULL)
776 delete this->input_file_;
777 if (this->obj_ != NULL)
781 // Read the input file and send its contents to OUTPUT_FILE.
784 Dwo_file::read(Dwp_output_file* output_file)
786 // Open the input file.
787 this->input_file_ = new Input_file(this->name_);
790 if (!this->input_file_->open(dirpath, NULL, &index))
791 gold_fatal(_("%s: can't open"), this->name_);
793 // Check that it's an ELF file.
794 off_t filesize = this->input_file_->file().filesize();
795 int hdrsize = elfcpp::Elf_recognizer::max_header_size;
796 if (filesize < hdrsize)
798 const unsigned char* p =
799 this->input_file_->file().get_view(0, 0, hdrsize, true, false);
800 if (!elfcpp::Elf_recognizer::is_elf_file(p, hdrsize))
801 gold_fatal(_("%s: not an ELF object file"), this->name_);
803 // Get the size, endianness, machine, etc. info from the header,
804 // make an appropriately-sized Relobj, and pass the target info
805 // to the output object.
809 if (!elfcpp::Elf_recognizer::is_valid_header(p, hdrsize, &size,
810 &big_endian, &error))
811 gold_fatal(_("%s: %s"), this->name_, error.c_str());
813 this->obj_ = this->make_object(size, big_endian, p, this->input_file_,
816 unsigned int shnum = this->shnum();
817 this->is_compressed_.resize(shnum);
818 this->shndx_map_.resize(shnum);
820 typedef std::vector<unsigned int> Types_list;
821 Types_list debug_types;
822 unsigned int debug_info = 0;
823 unsigned int debug_abbrev = 0;
824 unsigned int debug_line = 0;
825 unsigned int debug_loc = 0;
826 unsigned int debug_str = 0;
827 unsigned int debug_str_offsets = 0;
828 unsigned int debug_macinfo = 0;
829 unsigned int debug_macro = 0;
830 unsigned int debug_cu_index = 0;
831 unsigned int debug_tu_index = 0;
833 // Scan the section table and look for .dwp index sections.
834 // (Section index 0 is a dummy section; skip it.)
835 for (unsigned int i = 1; i < shnum; i++)
837 if (this->section_type(i) != elfcpp::SHT_PROGBITS)
839 std::string sect_name = this->section_name(i);
840 const char* suffix = sect_name.c_str();
841 if (is_prefix_of(".debug_", suffix))
843 else if (is_prefix_of(".zdebug_", suffix))
845 this->is_compressed_[i] = true;
850 if (strcmp(suffix, "cu_index") == 0)
852 else if (strcmp(suffix, "tu_index") == 0)
854 else if (strcmp(suffix, "str.dwo") == 0)
858 // Merge the input string table into the output string table.
859 this->add_strings(output_file, debug_str);
861 // If we found any .dwp index sections, read those and add the section
862 // sets to the output file.
863 if (debug_cu_index > 0 || debug_tu_index > 0)
865 if (debug_cu_index > 0)
866 this->read_compunit_index(debug_cu_index, output_file);
867 if (debug_tu_index > 0)
868 this->read_typeunit_index(debug_tu_index, output_file);
872 // If we found no index sections, this is a .dwo file.
873 // Scan the section table and collect the debug sections.
874 for (unsigned int i = 1; i < shnum; i++)
876 if (this->section_type(i) != elfcpp::SHT_PROGBITS)
878 std::string sect_name = this->section_name(i);
879 const char* suffix = sect_name.c_str();
880 if (is_prefix_of(".debug_", suffix))
882 else if (is_prefix_of(".zdebug_", suffix))
886 // TODO: Check for one of each section (except .debug_types).
887 if (strcmp(suffix, "info.dwo") == 0)
889 else if (strcmp(suffix, "types.dwo") == 0)
890 debug_types.push_back(i);
891 else if (strcmp(suffix, "abbrev.dwo") == 0)
893 else if (strcmp(suffix, "line.dwo") == 0)
895 else if (strcmp(suffix, "loc.dwo") == 0)
897 else if (strcmp(suffix, "str_offsets.dwo") == 0)
898 debug_str_offsets = i;
899 else if (strcmp(suffix, "macinfo.dwo") == 0)
901 else if (strcmp(suffix, "macro.dwo") == 0)
907 // Extract the dwo_id from .debug_info.dwo section.
909 Dwo_id_info_reader dwarf_reader(false, this->obj_, NULL, 0, debug_info,
911 dwarf_reader.set_abbrev_shndx(debug_abbrev);
912 if (!dwarf_reader.get_dwo_id(&dwo_id))
913 gold_fatal(_("%s: .debug_info.dwo section does not have DW_AT_GNU_dwo_id "
914 "attribute"), this->name_);
915 this->add_cu_set(output_file, dwo_id, debug_info, debug_abbrev,
916 debug_line, debug_loc, debug_str_offsets,
917 debug_macinfo, debug_macro);
920 for (Types_list::const_iterator tp = debug_types.begin();
921 tp != debug_types.end();
924 // Extract the type signature from .debug_types.dwo section.
926 gold_assert(*tp > 0);
927 Dwo_id_info_reader dwarf_reader(true, this->obj_, NULL, 0, *tp, 0, 0);
928 dwarf_reader.set_abbrev_shndx(debug_abbrev);
929 if (!dwarf_reader.get_type_sig(&type_sig))
930 gold_fatal(_("%s: .debug_types.dwo section does not have type signature"),
932 this->add_tu_set(output_file, type_sig, *tp, debug_abbrev, debug_line,
937 // Create a Sized_relobj_dwo of the given size and endianness,
938 // and record the target info. P is a pointer to the ELF header
942 Dwo_file::make_object(int size, bool big_endian, const unsigned char* p,
943 Input_file* input_file, Dwp_output_file* output_file)
948 #ifdef HAVE_TARGET_32_BIG
949 return this->sized_make_object<32, true>(p, input_file, output_file);
954 #ifdef HAVE_TARGET_32_LITTLE
955 return this->sized_make_object<32, false>(p, input_file, output_file);
963 #ifdef HAVE_TARGET_64_BIG
964 return this->sized_make_object<64, true>(p, input_file, output_file);
969 #ifdef HAVE_TARGET_64_LITTLE
970 return this->sized_make_object<64, false>(p, input_file, output_file);
979 // Function template to create a Sized_relobj_dwo and record the target info.
980 // P is a pointer to the ELF header in memory.
982 template <int size, bool big_endian>
984 Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file,
985 Dwp_output_file* output_file)
987 elfcpp::Ehdr<size, big_endian> ehdr(p);
988 Sized_relobj_dwo<size, big_endian>* obj =
989 new Sized_relobj_dwo<size, big_endian>(this->name_, input_file, ehdr);
991 output_file->record_target_info(
992 this->name_, ehdr.get_e_machine(), size, big_endian,
993 ehdr.get_e_ident()[elfcpp::EI_OSABI],
994 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
998 // Read the .debug_cu_index section of a .dwp file,
999 // and process the CU sets.
1002 Dwo_file::read_compunit_index(unsigned int shndx, Dwp_output_file* output_file)
1004 if (this->obj_->is_big_endian())
1005 this->sized_read_compunit_index<true>(shndx, output_file);
1007 this->sized_read_compunit_index<false>(shndx, output_file);
1010 template <bool big_endian>
1012 Dwo_file::sized_read_compunit_index(unsigned int shndx,
1013 Dwp_output_file* output_file)
1015 section_size_type len;
1017 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1019 unsigned int version =
1020 elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
1022 gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"),
1023 this->name_, version);
1025 unsigned int nused =
1026 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1027 + 2 * sizeof(uint32_t));
1031 unsigned int nslots =
1032 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1033 + 3 * sizeof(uint32_t));
1035 const unsigned char* phash = contents + 4 * sizeof(uint32_t);
1036 const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
1037 const unsigned char* shndx_pool = pindex + nslots * sizeof(uint32_t);
1038 const unsigned char* limit = contents + len;
1040 if (shndx_pool >= limit)
1041 gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_);
1043 // Loop over the slots of the hash table.
1044 for (unsigned int i = 0; i < nslots; ++i)
1047 elfcpp::Swap_unaligned<64, big_endian>::readval(phash);
1050 unsigned int index =
1051 elfcpp::Swap_unaligned<32, big_endian>::readval(pindex);
1052 const unsigned char* shndx_list =
1053 shndx_pool + index * sizeof(uint32_t);
1055 // Collect the debug sections for this compilation unit set.
1056 unsigned int debug_info = 0;
1057 unsigned int debug_abbrev = 0;
1058 unsigned int debug_line = 0;
1059 unsigned int debug_loc = 0;
1060 unsigned int debug_str_offsets = 0;
1061 unsigned int debug_macinfo = 0;
1062 unsigned int debug_macro = 0;
1065 if (shndx_list >= limit)
1066 gold_fatal(_("%s: .debug_cu_index is corrupt"),
1068 unsigned int shndx =
1069 elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list);
1072 if (shndx > this->shnum())
1073 gold_fatal(_("%s: .debug_cu_index has bad shndx"),
1075 std::string sect_name = this->section_name(shndx);
1076 const char* suffix = sect_name.c_str();
1077 if (is_prefix_of(".debug_", suffix))
1079 else if (is_prefix_of(".zdebug_", suffix))
1082 gold_fatal(_("%s: .debug_cu_index refers to "
1083 "non-debug section"), this->name_);
1084 if (strcmp(suffix, "info.dwo") == 0)
1086 else if (strcmp(suffix, "abbrev.dwo") == 0)
1087 debug_abbrev = shndx;
1088 else if (strcmp(suffix, "line.dwo") == 0)
1090 else if (strcmp(suffix, "loc.dwo") == 0)
1092 else if (strcmp(suffix, "str_offsets.dwo") == 0)
1093 debug_str_offsets = shndx;
1094 else if (strcmp(suffix, "macinfo.dwo") == 0)
1095 debug_macinfo = shndx;
1096 else if (strcmp(suffix, "macro.dwo") == 0)
1097 debug_macro = shndx;
1098 shndx_list += sizeof(uint32_t);
1100 this->add_cu_set(output_file, dwo_id, debug_info, debug_abbrev,
1101 debug_line, debug_loc, debug_str_offsets,
1102 debug_macinfo, debug_macro);
1104 phash += sizeof(uint64_t);
1105 pindex += sizeof(uint32_t);
1112 // Read the .debug_tu_index section of a .dwp file,
1113 // and process the TU sets.
1116 Dwo_file::read_typeunit_index(unsigned int shndx, Dwp_output_file* output_file)
1118 if (this->obj_->is_big_endian())
1119 this->sized_read_typeunit_index<true>(shndx, output_file);
1121 this->sized_read_typeunit_index<false>(shndx, output_file);
1124 template <bool big_endian>
1126 Dwo_file::sized_read_typeunit_index(unsigned int shndx,
1127 Dwp_output_file* output_file)
1129 section_size_type len;
1131 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1133 unsigned int version =
1134 elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
1136 gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"),
1137 this->name_, version);
1139 unsigned int nused =
1140 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1141 + 2 * sizeof(uint32_t));
1145 unsigned int nslots =
1146 elfcpp::Swap_unaligned<32, big_endian>::readval(contents
1147 + 3 * sizeof(uint32_t));
1149 const unsigned char* phash = contents + 4 * sizeof(uint32_t);
1150 const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
1151 const unsigned char* shndx_pool = pindex + nslots * sizeof(uint32_t);
1152 const unsigned char* limit = contents + len;
1154 if (shndx_pool >= limit)
1155 gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_);
1157 // Loop over the slots of the hash table.
1158 for (unsigned int i = 0; i < nslots; ++i)
1161 elfcpp::Swap_unaligned<64, big_endian>::readval(phash);
1164 unsigned int index =
1165 elfcpp::Swap_unaligned<32, big_endian>::readval(pindex);
1166 const unsigned char* shndx_list =
1167 shndx_pool + index * sizeof(uint32_t);
1169 // Collect the debug sections for this type unit set.
1170 unsigned int debug_types = 0;
1171 unsigned int debug_abbrev = 0;
1172 unsigned int debug_line = 0;
1173 unsigned int debug_str_offsets = 0;
1176 if (shndx_list >= limit)
1177 gold_fatal(_("%s: .debug_tu_index is corrupt"),
1179 unsigned int shndx =
1180 elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list);
1183 if (shndx > this->shnum())
1184 gold_fatal(_("%s: .debug_tu_index has bad shndx"),
1186 std::string sect_name = this->section_name(shndx);
1187 const char* suffix = sect_name.c_str();
1188 if (is_prefix_of(".debug_", suffix))
1190 else if (is_prefix_of(".zdebug_", suffix))
1193 gold_fatal(_("%s: .debug_tu_index refers to "
1194 "non-debug section"), this->name_);
1195 if (strcmp(suffix, "types.dwo") == 0)
1196 debug_types = shndx;
1197 else if (strcmp(suffix, "abbrev.dwo") == 0)
1198 debug_abbrev = shndx;
1199 else if (strcmp(suffix, "line.dwo") == 0)
1201 else if (strcmp(suffix, "str_offsets.dwo") == 0)
1202 debug_str_offsets = shndx;
1203 shndx_list += sizeof(uint32_t);
1205 this->add_tu_set(output_file, type_sig, debug_types, debug_abbrev,
1206 debug_line, debug_str_offsets);
1208 phash += sizeof(uint64_t);
1209 pindex += sizeof(uint32_t);
1216 // Merge the input string table section into the output file.
1219 Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str)
1221 section_size_type len;
1223 const unsigned char* pdata = this->section_contents(debug_str, &len, &is_new);
1224 const char* p = reinterpret_cast<const char*>(pdata);
1225 const char* pend = p + len;
1227 // Check that the last string is null terminated.
1228 if (pend[-1] != '\0')
1229 gold_fatal(_("%s: last entry in string section '%s' "
1230 "is not null terminated"),
1232 this->section_name(debug_str).c_str());
1234 // Count the number of strings in the section, and size the map.
1236 for (const char* pt = p; pt < pend; pt += strlen(pt) + 1)
1238 this->str_offset_map_.reserve(count + 1);
1240 // Add the strings to the output string table, and record the new offsets
1242 section_offset_type i = 0;
1243 section_offset_type new_offset;
1246 size_t len = strlen(p);
1247 new_offset = output_file->add_string(p, len);
1248 this->str_offset_map_.push_back(std::make_pair(i, new_offset));
1253 this->str_offset_map_.push_back(std::make_pair(i, new_offset));
1258 // Copy a section from the input file to the output file.
1259 // If IS_STR_OFFSETS is true, remap the string offsets for the
1260 // output string table.
1263 Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx,
1264 const char* section_name, bool is_str_offsets)
1266 // Some sections may be referenced from more than one set.
1267 // Don't copy a section more than once.
1268 if (this->shndx_map_[shndx] > 0)
1269 return this->shndx_map_[shndx];
1271 section_size_type len;
1273 const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
1277 const unsigned char* remapped = this->remap_str_offsets(contents, len);
1280 contents = remapped;
1284 this->shndx_map_[shndx] = output_file->add_section(section_name, contents,
1289 return this->shndx_map_[shndx];
1293 const unsigned char*
1294 Dwo_file::remap_str_offsets(const unsigned char* contents,
1295 section_size_type len)
1298 gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"),
1301 if (this->obj_->is_big_endian())
1302 return this->sized_remap_str_offsets<true>(contents, len);
1304 return this->sized_remap_str_offsets<false>(contents, len);
1307 template <bool big_endian>
1308 const unsigned char*
1309 Dwo_file::sized_remap_str_offsets(const unsigned char* contents,
1310 section_size_type len)
1312 unsigned char* remapped = new unsigned char[len];
1313 const unsigned char* p = contents;
1314 unsigned char* q = remapped;
1317 unsigned int val = elfcpp::Swap_unaligned<32, big_endian>::readval(p);
1318 val = this->remap_str_offset(val);
1319 elfcpp::Swap_unaligned<32, big_endian>::writeval(q, val);
1328 Dwo_file::remap_str_offset(section_offset_type val)
1330 Str_offset_map_entry entry;
1333 Str_offset_map::const_iterator p =
1334 std::lower_bound(this->str_offset_map_.begin(),
1335 this->str_offset_map_.end(),
1336 entry, Offset_compare());
1338 if (p == this->str_offset_map_.end() || p->first > val)
1340 if (p == this->str_offset_map_.begin())
1343 gold_assert(p->first <= val);
1346 return p->second + (val - p->first);
1349 // Add a set of .debug_info and related sections to OUTPUT_FILE.
1352 Dwo_file::add_cu_set(Dwp_output_file* output_file,
1354 unsigned int debug_info,
1355 unsigned int debug_abbrev,
1356 unsigned int debug_line,
1357 unsigned int debug_loc,
1358 unsigned int debug_str_offsets,
1359 unsigned int debug_macinfo,
1360 unsigned int debug_macro)
1362 if (debug_info == 0)
1363 gold_fatal(_("%s: no .debug_info.dwo section found"), this->name_);
1364 if (debug_abbrev == 0)
1365 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_);
1367 debug_abbrev = this->copy_section(output_file, debug_abbrev,
1368 ".debug_abbrev.dwo", false);
1370 debug_line = this->copy_section(output_file, debug_line,
1371 ".debug_line.dwo", false);
1373 debug_loc = this->copy_section(output_file, debug_loc, ".debug_loc.dwo",
1375 if (debug_macinfo > 0)
1376 debug_macinfo = this->copy_section(output_file, debug_macinfo,
1377 ".debug_macinfo.dwo", false);
1378 if (debug_macro > 0)
1379 debug_macro = this->copy_section(output_file, debug_macro,
1380 ".debug_macro.dwo", false);
1382 if (debug_str_offsets > 0)
1383 debug_str_offsets = this->copy_section(output_file, debug_str_offsets,
1384 ".debug_str_offsets.dwo", true);
1386 debug_info = this->copy_section(output_file, debug_info, ".debug_info.dwo",
1389 output_file->add_cu_set(dwo_id, debug_info, debug_abbrev, debug_line,
1390 debug_loc, debug_str_offsets, debug_macinfo,
1394 // Add a set of .debug_types and related sections to OUTPUT_FILE.
1397 Dwo_file::add_tu_set(Dwp_output_file* output_file,
1399 unsigned int debug_types,
1400 unsigned int debug_abbrev,
1401 unsigned int debug_line,
1402 unsigned int debug_str_offsets)
1404 if (debug_types == 0)
1405 gold_fatal(_("%s: no .debug_types.dwo section found"), this->name_);
1406 if (debug_abbrev == 0)
1407 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_);
1409 // Ignore duplicate type signatures.
1410 if (output_file->lookup_tu(type_sig))
1413 debug_abbrev = this->copy_section(output_file, debug_abbrev,
1414 ".debug_abbrev.dwo", false);
1416 debug_line = this->copy_section(output_file, debug_line,
1417 ".debug_line.dwo", false);
1419 if (debug_str_offsets > 0)
1420 debug_str_offsets = this->copy_section(output_file, debug_str_offsets,
1421 ".debug_str_offsets.dwo", true);
1423 debug_types = this->copy_section(output_file, debug_types,
1424 ".debug_types.dwo", false);
1426 output_file->add_tu_set(type_sig, debug_types, debug_abbrev, debug_line,
1430 // Class Dwp_output_file.
1432 // Record the target info from an input file. On first call, we
1433 // set the ELF header values for the output file. On subsequent
1434 // calls, we just verify that the values match.
1437 Dwp_output_file::record_target_info(const char*, int machine,
1438 int size, bool big_endian,
1439 int osabi, int abiversion)
1441 // TODO: Check the values on subsequent calls.
1442 if (this->size_ > 0)
1445 this->machine_ = machine;
1447 this->big_endian_ = big_endian;
1448 this->osabi_ = osabi;
1449 this->abiversion_ = abiversion;
1452 this->next_file_offset_ = elfcpp::Elf_sizes<32>::ehdr_size;
1453 else if (size == 64)
1454 this->next_file_offset_ = elfcpp::Elf_sizes<64>::ehdr_size;
1458 this->fd_ = ::fopen(this->name_, "wb");
1459 if (this->fd_ == NULL)
1460 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
1462 // Write zeroes for the ELF header initially. We'll write
1463 // the actual header during finalize().
1464 static const char buf[elfcpp::Elf_sizes<64>::ehdr_size] = { 0 };
1465 if (::fwrite(buf, 1, this->next_file_offset_, this->fd_)
1466 < (size_t) this->next_file_offset_)
1467 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
1470 // Add a string to the debug strings section.
1473 Dwp_output_file::add_string(const char* str, size_t len)
1475 Stringpool::Key key;
1476 this->stringpool_.add_with_length(str, len, true, &key);
1477 this->have_strings_ = true;
1478 // We aren't supposed to call get_offset() until after
1479 // calling set_string_offsets(), but the offsets will
1480 // not change unless optimizing the string pool.
1481 return this->stringpool_.get_offset_from_key(key);
1484 // Align the file offset to the given boundary.
1487 align_offset(off_t off, int align)
1489 return (off + align - 1) & ~(align - 1);
1492 // Add a section to the output file, and return the new section index.
1495 Dwp_output_file::add_section(const char* section_name,
1496 const unsigned char* contents,
1497 section_size_type len,
1500 off_t file_offset = this->next_file_offset_;
1501 gold_assert(this->size_ > 0 && file_offset > 0);
1503 file_offset = align_offset(file_offset, align);
1505 ::fseek(this->fd_, file_offset, SEEK_SET);
1506 if (::fwrite(contents, 1, len, this->fd_) < len)
1507 gold_fatal(_("%s: error writing section '%s'"), this->name_, section_name);
1509 section_name = this->shstrtab_.add_with_length(section_name,
1510 strlen(section_name),
1512 Section sect = { section_name, file_offset, len, align };
1513 this->sections_.push_back(sect);
1515 this->next_file_offset_ = file_offset + len;
1516 return this->shnum_++;
1519 // Add a set of .debug_info and related sections to the output file.
1522 Dwp_output_file::add_cu_set(uint64_t dwo_id,
1523 unsigned int debug_info,
1524 unsigned int debug_abbrev,
1525 unsigned int debug_line,
1526 unsigned int debug_loc,
1527 unsigned int debug_str_offsets,
1528 unsigned int debug_macinfo,
1529 unsigned int debug_macro)
1531 Cu_or_tu_set cu_set = { dwo_id, debug_info, debug_abbrev, debug_line,
1532 debug_loc, debug_str_offsets, debug_macinfo,
1535 this->cu_index_.find_or_add(dwo_id, &slot);
1536 this->cu_index_.enter_set(slot, cu_set);
1539 // Lookup a type signature and return TRUE if we have already seen it.
1541 Dwp_output_file::lookup_tu(uint64_t type_sig)
1543 this->last_type_sig_ = type_sig;
1544 return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_);
1547 // Add a set of .debug_types and related sections to the output file.
1550 Dwp_output_file::add_tu_set(uint64_t type_sig,
1551 unsigned int debug_types,
1552 unsigned int debug_abbrev,
1553 unsigned int debug_line,
1554 unsigned int debug_str_offsets)
1556 Cu_or_tu_set tu_set = { type_sig, debug_types, debug_abbrev, debug_line,
1557 0, debug_str_offsets, 0, 0 };
1559 if (type_sig == this->last_type_sig_)
1560 slot = this->last_tu_slot_;
1562 this->tu_index_.find_or_add(type_sig, &slot);
1563 this->tu_index_.enter_set(slot, tu_set);
1566 // Find a slot in the hash table for SIGNATURE. Return TRUE
1567 // if the entry already exists.
1570 Dwp_output_file::Dwp_index::find_or_add(uint64_t signature,
1571 unsigned int* slotp)
1573 if (this->capacity_ == 0)
1576 static_cast<unsigned int>(signature) & (this->capacity_ - 1);
1577 unsigned int secondary_hash;
1578 uint64_t probe = this->hash_table_[slot];
1579 if (probe != 0 && probe != signature)
1581 secondary_hash = (static_cast<unsigned int>(signature >> 32)
1582 & (this->capacity_ - 1)) | 1;
1585 slot = (slot + secondary_hash) & (this->capacity_ - 1);
1586 probe = this->hash_table_[slot];
1587 } while (probe != 0 && probe != signature);
1590 return (probe != 0);
1593 // Enter a CU or TU set at the given SLOT in the hash table.
1596 Dwp_output_file::Dwp_index::enter_set(unsigned int slot,
1597 const Cu_or_tu_set& set)
1599 gold_assert(slot < this->capacity_);
1600 gold_assert(set.debug_info_or_types > 0);
1601 gold_assert(set.debug_abbrev > 0);
1603 // Add the section indexes to the pool.
1604 uint32_t pool_index = this->shndx_pool_.size();
1605 this->shndx_pool_.push_back(set.debug_info_or_types);
1606 this->shndx_pool_.push_back(set.debug_abbrev);
1607 if (set.debug_line > 0)
1608 this->shndx_pool_.push_back(set.debug_line);
1609 if (set.debug_loc > 0)
1610 this->shndx_pool_.push_back(set.debug_loc);
1611 if (set.debug_str_offsets > 0)
1612 this->shndx_pool_.push_back(set.debug_str_offsets);
1613 if (set.debug_macinfo > 0)
1614 this->shndx_pool_.push_back(set.debug_macinfo);
1615 if (set.debug_macro > 0)
1616 this->shndx_pool_.push_back(set.debug_macro);
1617 this->shndx_pool_.push_back(0);
1619 // Enter the signature and pool index into the hash table.
1620 this->hash_table_[slot] = set.signature;
1621 this->index_table_[slot] = pool_index;
1624 // Grow the hash table when we exceed 2/3 capacity.
1625 if (this->used_ * 3 > this->capacity_ * 2)
1629 // Initialize the hash table.
1632 Dwp_output_file::Dwp_index::initialize()
1634 this->capacity_ = 16;
1635 this->hash_table_ = new uint64_t[this->capacity_];
1636 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
1637 this->index_table_ = new uint32_t[this->capacity_];
1638 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
1641 // Grow the hash table when we reach 2/3 capacity.
1644 Dwp_output_file::Dwp_index::grow()
1646 unsigned int old_capacity = this->capacity_;
1647 uint64_t* old_hash_table = this->hash_table_;
1648 uint32_t* old_index_table = this->index_table_;
1649 unsigned int old_used = this->used_;
1651 this->capacity_ = old_capacity * 2;
1652 this->hash_table_ = new uint64_t[this->capacity_];
1653 memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
1654 this->index_table_ = new uint32_t[this->capacity_];
1655 memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
1658 for (unsigned int i = 0; i < old_capacity; ++i)
1660 uint64_t signature = old_hash_table[i];
1664 bool found = this->find_or_add(signature, &slot);
1665 gold_assert(!found);
1666 this->hash_table_[slot] = signature;
1667 this->index_table_[slot] = old_index_table[i];
1671 gold_assert(this->used_ == old_used);
1673 delete[] old_hash_table;
1674 delete[] old_index_table;
1677 // Initialize the output file.
1680 Dwp_output_file::initialize()
1682 // We can't initialize the output file until we've recorded the
1683 // target info from the first input file.
1684 gold_assert(this->size_ > 0);
1687 // Finalize the file, write the string tables and index sections,
1688 // and close the file.
1691 Dwp_output_file::finalize()
1695 // Write the debug string table.
1696 if (this->have_strings_)
1698 this->stringpool_.set_string_offsets();
1699 section_size_type len = this->stringpool_.get_strtab_size();
1700 buf = new unsigned char[len];
1701 this->stringpool_.write_to_buffer(buf, len);
1702 this->add_section(".debug_str.dwo", buf, len, 1);
1706 // Write the CU and TU indexes.
1707 if (this->big_endian_)
1709 this->write_index<true>(".debug_cu_index", this->cu_index_);
1710 this->write_index<true>(".debug_tu_index", this->tu_index_);
1714 this->write_index<false>(".debug_cu_index", this->cu_index_);
1715 this->write_index<false>(".debug_tu_index", this->tu_index_);
1718 off_t file_offset = this->next_file_offset_;
1720 // Write the section string table.
1721 this->shstrndx_ = this->shnum_++;
1722 const char* shstrtab_name =
1723 this->shstrtab_.add_with_length(".shstrtab",
1724 sizeof(".shstrtab") - 1,
1726 this->shstrtab_.set_string_offsets();
1727 section_size_type shstrtab_len = this->shstrtab_.get_strtab_size();
1728 buf = new unsigned char[shstrtab_len];
1729 this->shstrtab_.write_to_buffer(buf, shstrtab_len);
1730 off_t shstrtab_off = file_offset;
1731 ::fseek(this->fd_, file_offset, 0);
1732 if (::fwrite(buf, 1, shstrtab_len, this->fd_) < shstrtab_len)
1733 gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_);
1735 file_offset += shstrtab_len;
1737 // Write the section header table. The first entry is a NULL entry.
1738 // This is followed by the debug sections, and finally we write the
1739 // .shstrtab section header.
1740 file_offset = align_offset(file_offset, this->size_ == 32 ? 4 : 8);
1741 this->shoff_ = file_offset;
1742 ::fseek(this->fd_, file_offset, 0);
1743 section_size_type sh0_size = 0;
1744 unsigned int sh0_link = 0;
1745 if (this->shnum_ >= elfcpp::SHN_LORESERVE)
1746 sh0_size = this->shnum_;
1747 if (this->shstrndx_ >= elfcpp::SHN_LORESERVE)
1748 sh0_link = this->shstrndx_;
1749 this->write_shdr(NULL, 0, 0, 0, 0, sh0_size, sh0_link, 0, 0, 0);
1750 for (unsigned int i = 0; i < this->sections_.size(); ++i)
1752 Section& sect = this->sections_[i];
1753 this->write_shdr(sect.name, elfcpp::SHT_PROGBITS, 0, 0, sect.offset,
1754 sect.size, 0, 0, sect.align, 0);
1756 this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0,
1757 shstrtab_off, shstrtab_len, 0, 0, 1, 0);
1759 // Write the ELF header.
1763 if (this->fd_ != NULL)
1765 if (::fclose(this->fd_) != 0)
1766 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
1771 // Write a CU or TU index section.
1772 template<bool big_endian>
1774 Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index)
1776 const unsigned int nslots = index.hash_table_total_slots();
1777 const unsigned int nused = index.hash_table_used_slots();
1778 const unsigned int npool = index.shndx_pool_size();
1779 const section_size_type index_size = (4 * sizeof(uint32_t)
1780 + nslots * sizeof(uint64_t)
1781 + nslots * sizeof(uint32_t)
1782 + npool * sizeof(uint32_t));
1784 // Allocate a buffer for the section contents.
1785 unsigned char* buf = new unsigned char[index_size];
1786 unsigned char* p = buf;
1788 // Write the section header: version number, padding,
1789 // number of used slots and total number of slots.
1790 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 1);
1791 p += sizeof(uint32_t);
1792 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 0);
1793 p += sizeof(uint32_t);
1794 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nused);
1795 p += sizeof(uint32_t);
1796 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nslots);
1797 p += sizeof(uint32_t);
1799 // Write the hash table.
1800 for (unsigned int i = 0; i < nslots; ++i)
1802 elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i));
1803 p += sizeof(uint64_t);
1806 // Write the parallel index table.
1807 for (unsigned int i = 0; i < nslots; ++i)
1809 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i));
1810 p += sizeof(uint32_t);
1813 // Write the section index pool.
1814 Dwp_index::Shndx_pool::const_iterator pool = index.shndx_pool();
1815 for (unsigned int i = 0; i < npool; ++i)
1817 gold_assert(pool != index.shndx_pool_end());
1818 elfcpp::Swap_unaligned<32, big_endian>::writeval(p, *pool);
1819 p += sizeof(uint32_t);
1823 gold_assert(p == buf + index_size);
1825 this->add_section(sect_name, buf, index_size, sizeof(uint64_t));
1830 // Write the ELF header.
1833 Dwp_output_file::write_ehdr()
1835 if (this->size_ == 32)
1837 if (this->big_endian_)
1838 return this->sized_write_ehdr<32, true>();
1840 return this->sized_write_ehdr<32, false>();
1842 else if (this->size_ == 64)
1844 if (this->big_endian_)
1845 return this->sized_write_ehdr<64, true>();
1847 return this->sized_write_ehdr<64, false>();
1853 template<unsigned int size, bool big_endian>
1855 Dwp_output_file::sized_write_ehdr()
1857 const unsigned int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1858 unsigned char buf[ehdr_size];
1859 elfcpp::Ehdr_write<size, big_endian> ehdr(buf);
1861 unsigned char e_ident[elfcpp::EI_NIDENT];
1862 memset(e_ident, 0, elfcpp::EI_NIDENT);
1863 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
1864 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
1865 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
1866 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
1868 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
1869 else if (size == 64)
1870 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
1873 e_ident[elfcpp::EI_DATA] = (big_endian
1874 ? elfcpp::ELFDATA2MSB
1875 : elfcpp::ELFDATA2LSB);
1876 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
1877 ehdr.put_e_ident(e_ident);
1879 ehdr.put_e_type(elfcpp::ET_REL);
1880 ehdr.put_e_machine(this->machine_);
1881 ehdr.put_e_version(elfcpp::EV_CURRENT);
1882 ehdr.put_e_entry(0);
1883 ehdr.put_e_phoff(0);
1884 ehdr.put_e_shoff(this->shoff_);
1885 ehdr.put_e_flags(0);
1886 ehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
1887 ehdr.put_e_phentsize(0);
1888 ehdr.put_e_phnum(0);
1889 ehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
1890 ehdr.put_e_shnum(this->shnum_ < elfcpp::SHN_LORESERVE ? this->shnum_ : 0);
1891 ehdr.put_e_shstrndx(this->shstrndx_ < elfcpp::SHN_LORESERVE
1893 : static_cast<unsigned int>(elfcpp::SHN_XINDEX));
1895 ::fseek(this->fd_, 0, 0);
1896 if (::fwrite(buf, 1, ehdr_size, this->fd_) < ehdr_size)
1897 gold_fatal(_("%s: error writing ELF header"), this->name_);
1900 // Write a section header.
1903 Dwp_output_file::write_shdr(const char* name, unsigned int type,
1904 unsigned int flags, uint64_t addr, off_t offset,
1905 section_size_type sect_size, unsigned int link,
1906 unsigned int info, unsigned int align,
1907 unsigned int ent_size)
1909 if (this->size_ == 32)
1911 if (this->big_endian_)
1912 return this->sized_write_shdr<32, true>(name, type, flags, addr,
1913 offset, sect_size, link, info,
1916 return this->sized_write_shdr<32, false>(name, type, flags, addr,
1917 offset, sect_size, link, info,
1920 else if (this->size_ == 64)
1922 if (this->big_endian_)
1923 return this->sized_write_shdr<64, true>(name, type, flags, addr,
1924 offset, sect_size, link, info,
1927 return this->sized_write_shdr<64, false>(name, type, flags, addr,
1928 offset, sect_size, link, info,
1935 template<unsigned int size, bool big_endian>
1937 Dwp_output_file::sized_write_shdr(const char* name, unsigned int type,
1938 unsigned int flags, uint64_t addr,
1939 off_t offset, section_size_type sect_size,
1940 unsigned int link, unsigned int info,
1941 unsigned int align, unsigned int ent_size)
1943 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1944 unsigned char buf[shdr_size];
1945 elfcpp::Shdr_write<size, big_endian> shdr(buf);
1947 shdr.put_sh_name(name == NULL ? 0 : this->shstrtab_.get_offset(name));
1948 shdr.put_sh_type(type);
1949 shdr.put_sh_flags(flags);
1950 shdr.put_sh_addr(addr);
1951 shdr.put_sh_offset(offset);
1952 shdr.put_sh_size(sect_size);
1953 shdr.put_sh_link(link);
1954 shdr.put_sh_info(info);
1955 shdr.put_sh_addralign(align);
1956 shdr.put_sh_entsize(ent_size);
1957 if (::fwrite(buf, 1, shdr_size, this->fd_) < shdr_size)
1958 gold_fatal(_("%s: error writing section header table"), this->name_);
1961 // Class Dwo_id_info_reader.
1963 // Visit a compilation unit.
1966 Dwo_id_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die)
1968 this->dwo_id_ = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id);
1969 if (this->dwo_id_ != 0)
1970 this->dwo_id_found_ = true;
1973 // Visit a type unit.
1976 Dwo_id_info_reader::visit_type_unit(off_t, off_t, uint64_t signature, Dwarf_die*)
1978 this->type_sig_ = signature;
1979 this->type_sig_found_ = true;
1982 }; // End namespace gold
1984 using namespace gold;
1988 struct option dwp_options[] =
1990 { "help", no_argument, NULL, 'h' },
1991 { "output", required_argument, NULL, 'o' },
1992 { "verbose", no_argument, NULL, 'v' },
1993 { "version", no_argument, NULL, 'V' },
1994 { NULL, 0, NULL, 0 }
1997 // Print usage message and exit.
2000 usage(FILE* fd, int exit_status)
2002 fprintf(fd, _("Usage: %s [options] file...\n"), program_name);
2003 fprintf(fd, _(" -h, --help Print this help message\n"));
2004 fprintf(fd, _(" -o FILE, --output FILE Set output dwp file name"
2006 fprintf(fd, _(" -v, --verbose Verbose output\n"));
2007 fprintf(fd, _(" -V, --version Print version number\n"));
2009 // REPORT_BUGS_TO is defined in bfd/bfdver.h.
2010 const char* report = REPORT_BUGS_TO;
2011 if (*report != '\0')
2012 fprintf(fd, _("\nReport bugs to %s\n"), report);
2016 // Report version information.
2021 // This output is intended to follow the GNU standards.
2022 printf("GNU dwp %s\n", BFD_VERSION_STRING);
2023 printf(_("Copyright 2012 Free Software Foundation, Inc.\n"));
2025 This program is free software; you may redistribute it under the terms of\n\
2026 the GNU General Public License version 3 or (at your option) any later version.\n\
2027 This program has absolutely no warranty.\n"));
2034 main(int argc, char** argv)
2036 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2037 setlocale(LC_MESSAGES, "");
2039 #if defined (HAVE_SETLOCALE)
2040 setlocale(LC_CTYPE, "");
2042 bindtextdomain(PACKAGE, LOCALEDIR);
2043 textdomain(PACKAGE);
2045 program_name = argv[0];
2047 // Initialize the global parameters, to let random code get to the
2049 Errors errors(program_name);
2050 set_parameters_errors(&errors);
2052 // Initialize gold's global options. We don't use these in
2053 // this program, but they need to be initialized so that
2054 // functions we call from libgold work properly.
2055 General_options options;
2056 set_parameters_options(&options);
2058 // In libiberty; expands @filename to the args in "filename".
2059 expandargv(&argc, &argv);
2061 // Collect file names and options.
2062 typedef std::vector<char*> File_list;
2064 const char* output_filename = NULL;
2065 bool verbose = false;
2067 while ((c = getopt_long(argc, argv, "ho:vV", dwp_options, NULL)) != -1)
2072 usage(stdout, EXIT_SUCCESS);
2074 output_filename = optarg;
2083 usage(stderr, EXIT_FAILURE);
2086 for (int i = optind; i < argc; ++i)
2087 files.push_back(argv[i]);
2090 gold_fatal(_("no input files"));
2091 if (output_filename == NULL)
2092 gold_fatal(_("no output file specified"));
2094 Dwp_output_file output_file(output_filename);
2096 // Process each file, adding its contents to the output file.
2097 for (File_list::const_iterator f = files.begin(); f != files.end(); ++f)
2100 fprintf(stderr, "%s\n", *f);
2101 Dwo_file dwo_file(*f);
2102 dwo_file.read(&output_file);
2105 output_file.finalize();
2107 return EXIT_SUCCESS;