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
;
58 template <int size
, bool big_endian
>
59 class Sized_relobj_dwo
;
61 // List of .dwo files to process.
62 typedef std::vector
<std::string
> File_list
;
65 // This class may represent a .dwo file, a .dwp file
66 // produced by an earlier run, or an executable file whose
67 // debug section identifies a set of .dwo files to read.
72 Dwo_file(const char* name
)
73 : name_(name
), obj_(NULL
), input_file_(NULL
), is_compressed_(),
79 // Read the input executable file and extract the list of .dwo files
80 // that it references.
82 read_executable(File_list
* files
);
84 // Read the input file and send its contents to OUTPUT_FILE.
86 read(Dwp_output_file
* output_file
);
89 // Types for mapping input string offsets to output string offsets.
90 typedef std::pair
<section_offset_type
, section_offset_type
>
92 typedef std::vector
<Str_offset_map_entry
> Str_offset_map
;
94 // A less-than comparison routine for Str_offset_map.
98 operator()(const Str_offset_map_entry
& i1
,
99 const Str_offset_map_entry
& i2
) const
100 { return i1
.first
< i2
.first
; }
103 // Create a Sized_relobj_dwo of the given size and endianness,
104 // and record the target info. P is a pointer to the ELF header
107 make_object(Dwp_output_file
* output_file
);
109 template <int size
, bool big_endian
>
111 sized_make_object(const unsigned char* p
, Input_file
* input_file
,
112 Dwp_output_file
* output_file
);
114 // Return the number of sections in the input object file.
117 { return this->obj_
->shnum(); }
119 // Return section type.
121 section_type(unsigned int shndx
)
122 { return this->obj_
->section_type(shndx
); }
124 // Get the name of a section.
126 section_name(unsigned int shndx
)
127 { return this->obj_
->section_name(shndx
); }
129 // Return a view of the contents of a section, decompressed if necessary.
130 // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
131 // deleted by the caller.
133 section_contents(unsigned int shndx
, section_size_type
* plen
, bool* is_new
)
134 { return this->obj_
->decompressed_section_contents(shndx
, plen
, is_new
); }
136 // Read the .debug_cu_index section of a .dwp file,
137 // and process the CU sets.
139 read_compunit_index(unsigned int, Dwp_output_file
*);
141 template <bool big_endian
>
143 sized_read_compunit_index(unsigned int, Dwp_output_file
*);
145 // Read the .debug_tu_index section of a .dwp file,
146 // and process the TU sets.
148 read_typeunit_index(unsigned int, Dwp_output_file
*);
150 template <bool big_endian
>
152 sized_read_typeunit_index(unsigned int, Dwp_output_file
*);
154 // Merge the input string table section into the output file.
156 add_strings(Dwp_output_file
*, unsigned int);
158 // Copy a section from the input file to the output file.
160 copy_section(Dwp_output_file
* output_file
, unsigned int shndx
,
161 const char* section_name
, bool is_str_offsets
);
163 // Remap the string offsets in the .debug_str_offsets.dwo section.
165 remap_str_offsets(const unsigned char* contents
, section_size_type len
);
167 template <bool big_endian
>
169 sized_remap_str_offsets(const unsigned char* contents
, section_size_type len
);
171 // Remap a single string offsets from an offset in the input string table
172 // to an offset in the output string table.
174 remap_str_offset(section_offset_type val
);
176 // Add a set of .debug_info and related sections to OUTPUT_FILE.
178 add_cu_set(Dwp_output_file
* output_file
,
180 unsigned int debug_info
,
181 unsigned int debug_abbrev
,
182 unsigned int debug_line
,
183 unsigned int debug_loc
,
184 unsigned int debug_str_offsets
,
185 unsigned int debug_macinfo
,
186 unsigned int debug_macro
);
188 // Add a set of .debug_types and related sections to OUTPUT_FILE.
190 add_tu_set(Dwp_output_file
* output_file
,
192 unsigned int debug_types
,
193 unsigned int debug_abbrev
,
194 unsigned int debug_line
,
195 unsigned int debug_str_offsets
);
199 // The ELF file, represented as a gold Relobj instance.
201 // The Input_file object.
202 Input_file
* input_file_
;
203 // Flags indicating which sections are compressed.
204 std::vector
<bool> is_compressed_
;
205 // Map input section index onto output section index.
206 std::vector
<unsigned int> shndx_map_
;
207 // Map input string offsets to output string offsets.
208 Str_offset_map str_offset_map_
;
211 // An ELF input file.
212 // We derive from Sized_relobj so that we can use interfaces
213 // in libgold to access the file.
215 template <int size
, bool big_endian
>
216 class Sized_relobj_dwo
: public Sized_relobj
<size
, big_endian
>
219 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
220 typedef typename Sized_relobj
<size
, big_endian
>::Symbols Symbols
;
222 Sized_relobj_dwo(const char* name
, Input_file
* input_file
,
223 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
224 : Sized_relobj
<size
, big_endian
>(name
, input_file
),
225 elf_file_(this, ehdr
)
231 // Setup the section information.
236 // Return section type.
238 do_section_type(unsigned int shndx
)
239 { return this->elf_file_
.section_type(shndx
); }
241 // Get the name of a section.
243 do_section_name(unsigned int shndx
)
244 { return this->elf_file_
.section_name(shndx
); }
246 // Get the size of a section.
248 do_section_size(unsigned int shndx
)
249 { return this->elf_file_
.section_size(shndx
); }
251 // Return a view of the contents of a section.
253 do_section_contents(unsigned int, section_size_type
*, bool);
255 // Return a view of the uncompressed contents of a section. Set *PLEN
256 // to the size. Set *IS_NEW to true if the contents need to be deleted
259 do_decompressed_section_contents(unsigned int shndx
,
260 section_size_type
* plen
,
263 // The following virtual functions are abstract in the base classes,
264 // but are not used here.
268 do_read_symbols(Read_symbols_data
*)
269 { gold_unreachable(); }
271 // Lay out the input sections.
273 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*)
274 { gold_unreachable(); }
276 // Layout sections whose layout was deferred while waiting for
277 // input files from a plugin.
279 do_layout_deferred_sections(Layout
*)
280 { gold_unreachable(); }
282 // Add the symbols to the symbol table.
284 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*)
285 { gold_unreachable(); }
287 Archive::Should_include
288 do_should_include_member(Symbol_table
*, Layout
*, Read_symbols_data
*,
290 { gold_unreachable(); }
292 // Iterate over global symbols, calling a visitor class V for each.
294 do_for_all_global_symbols(Read_symbols_data
*,
295 Library_base::Symbol_visitor_base
*)
296 { gold_unreachable(); }
298 // Return section flags.
300 do_section_flags(unsigned int)
301 { gold_unreachable(); }
303 // Return section entsize.
305 do_section_entsize(unsigned int)
306 { gold_unreachable(); }
308 // Return section address.
310 do_section_address(unsigned int)
311 { gold_unreachable(); }
313 // Return the section link field.
315 do_section_link(unsigned int)
316 { gold_unreachable(); }
318 // Return the section link field.
320 do_section_info(unsigned int)
321 { gold_unreachable(); }
323 // Return the section alignment.
325 do_section_addralign(unsigned int)
326 { gold_unreachable(); }
328 // Return the Xindex structure to use.
330 do_initialize_xindex()
331 { gold_unreachable(); }
333 // Get symbol counts.
335 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const
336 { gold_unreachable(); }
338 // Get global symbols.
340 do_get_global_symbols() const
343 // Return the value of a local symbol.
345 do_local_symbol_value(unsigned int, uint64_t) const
346 { gold_unreachable(); }
349 do_local_plt_offset(unsigned int) const
350 { gold_unreachable(); }
352 // Return whether local symbol SYMNDX is a TLS symbol.
354 do_local_is_tls(unsigned int) const
355 { gold_unreachable(); }
357 // Return the number of local symbols.
359 do_local_symbol_count() const
360 { gold_unreachable(); }
362 // Return the number of local symbols in the output symbol table.
364 do_output_local_symbol_count() const
365 { gold_unreachable(); }
367 // Return the file offset for local symbols in the output symbol table.
369 do_local_symbol_offset() const
370 { gold_unreachable(); }
374 do_read_relocs(Read_relocs_data
*)
375 { gold_unreachable(); }
377 // Process the relocs to find list of referenced sections. Used only
378 // during garbage collection.
380 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*)
381 { gold_unreachable(); }
383 // Scan the relocs and adjust the symbol table.
385 do_scan_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*)
386 { gold_unreachable(); }
388 // Count the local symbols.
390 do_count_local_symbols(Stringpool_template
<char>*,
391 Stringpool_template
<char>*)
392 { gold_unreachable(); }
394 // Finalize the local symbols.
396 do_finalize_local_symbols(unsigned int, off_t
, Symbol_table
*)
397 { gold_unreachable(); }
399 // Set the offset where local dynamic symbol information will be stored.
401 do_set_local_dynsym_indexes(unsigned int)
402 { gold_unreachable(); }
404 // Set the offset where local dynamic symbol information will be stored.
406 do_set_local_dynsym_offset(off_t
)
407 { gold_unreachable(); }
409 // Relocate the input sections and write out the local symbols.
411 do_relocate(const Symbol_table
*, const Layout
*, Output_file
*)
412 { gold_unreachable(); }
415 // General access to the ELF file.
416 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
420 // This class is responsible for collecting the debug index information
421 // and writing the .dwp file in ELF format.
423 class Dwp_output_file
426 Dwp_output_file(const char* name
)
427 : name_(name
), machine_(0), size_(0), big_endian_(false), osabi_(0),
428 abiversion_(0), fd_(NULL
), next_file_offset_(0), shnum_(1), sections_(),
429 shoff_(0), shstrndx_(0), have_strings_(false), stringpool_(),
430 shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
433 this->stringpool_
.set_no_zero_null();
436 // Record the target info from an input file.
438 record_target_info(const char* name
, int machine
, int size
, bool big_endian
,
439 int osabi
, int abiversion
);
441 // Add a string to the debug strings section.
443 add_string(const char* str
, size_t len
);
445 // Add a section to the output file, and return the new section index.
447 add_section(const char* section_name
, const unsigned char* contents
,
448 section_size_type len
, int align
);
450 // Add a set of .debug_info and related sections to the output file.
452 add_cu_set(uint64_t dwo_id
, unsigned int debug_info
,
453 unsigned int debug_abbrev
, unsigned int debug_line
,
454 unsigned int debug_loc
, unsigned int debug_str_offsets
,
455 unsigned int debug_macinfo
, unsigned int debug_macro
);
457 // Lookup a type signature and return TRUE if we have already seen it.
459 lookup_tu(uint64_t type_sig
);
461 // Add a set of .debug_types and related sections to the output file.
463 add_tu_set(uint64_t type_sig
, unsigned int debug_types
,
464 unsigned int debug_abbrev
, unsigned int debug_line
,
465 unsigned int debug_str_offsets
);
467 // Finalize the file, write the string tables and index sections,
468 // and close the file.
473 // Sections in the output file.
478 section_size_type size
;
482 // A set of sections for a compilation unit or type unit.
486 unsigned int debug_info_or_types
;
487 unsigned int debug_abbrev
;
488 unsigned int debug_line
;
489 unsigned int debug_loc
;
490 unsigned int debug_str_offsets
;
491 unsigned int debug_macinfo
;
492 unsigned int debug_macro
;
495 // The index sections defined by the DWARF Package File Format spec.
499 // Vector for the section index pool.
500 typedef std::vector
<unsigned int> Shndx_pool
;
503 : capacity_(0), used_(0), hash_table_(NULL
), shndx_pool_()
509 // Find a slot in the hash table for SIGNATURE. Return TRUE
510 // if the entry already exists.
512 find_or_add(uint64_t signature
, unsigned int* slotp
);
514 // Enter a CU or TU set at the given SLOT in the hash table.
516 enter_set(unsigned int slot
, const Cu_or_tu_set
& set
);
518 // Return the contents of the given SLOT in the hash table of signatures.
520 hash_table(unsigned int slot
) const
521 { return this->hash_table_
[slot
]; }
523 // Return the contents of the given SLOT in the parallel table of
524 // shndx pool indexes.
526 index_table(unsigned int slot
) const
527 { return this->index_table_
[slot
]; }
529 // Return the total number of slots in the hash table.
531 hash_table_total_slots() const
532 { return this->capacity_
; }
534 // Return the number of used slots in the hash table.
536 hash_table_used_slots() const
537 { return this->used_
; }
539 // Return an iterator into the shndx pool.
540 Shndx_pool::const_iterator
542 { return this->shndx_pool_
.begin(); }
544 Shndx_pool::const_iterator
545 shndx_pool_end() const
546 { return this->shndx_pool_
.end(); }
548 // Return the number of entries in the shndx pool.
550 shndx_pool_size() const
551 { return this->shndx_pool_
.size(); }
554 // Initialize the hash table.
558 // Grow the hash table when we reach 2/3 capacity.
562 // The number of slots in the table, a power of 2 such that
563 // capacity > 3 * size / 2.
564 unsigned int capacity_
;
565 // The current number of used slots in the hash table.
567 // The storage for the hash table of signatures.
568 uint64_t* hash_table_
;
569 // The storage for the parallel table of shndx pool indexes.
570 uint32_t* index_table_
;
571 // The pool of section indexes.
572 Shndx_pool shndx_pool_
;
573 }; // End class Dwp_output_file::Dwp_index.
575 // Initialize the output file.
579 // Write the ELF header.
583 template<unsigned int size
, bool big_endian
>
587 // Write a section header.
589 write_shdr(const char* name
, unsigned int type
, unsigned int flags
,
590 uint64_t addr
, off_t offset
, section_size_type sect_size
,
591 unsigned int link
, unsigned int info
,
592 unsigned int align
, unsigned int ent_size
);
594 template<unsigned int size
, bool big_endian
>
596 sized_write_shdr(const char* name
, unsigned int type
, unsigned int flags
,
597 uint64_t addr
, off_t offset
, section_size_type sect_size
,
598 unsigned int link
, unsigned int info
,
599 unsigned int align
, unsigned int ent_size
);
601 // Write a CU or TU index section.
602 template<bool big_endian
>
604 write_index(const char* sect_name
, const Dwp_index
& index
);
606 // The output filename.
608 // ELF header parameters.
614 // The output file descriptor.
616 // Next available file offset.
617 off_t next_file_offset_
;
618 // The number of sections.
620 // Section table. The first entry is shndx 1.
621 std::vector
<Section
> sections_
;
622 // File offset of the section header table.
624 // Section index of the section string table.
625 unsigned int shstrndx_
;
626 // TRUE if we have added any strings to the string pool.
628 // String pool for the output .debug_str.dwo section.
629 Stringpool stringpool_
;
630 // String pool for the .shstrtab section.
631 Stringpool shstrtab_
;
632 // The compilation unit index.
634 // The type unit index.
636 // Cache of the last type signature looked up.
637 uint64_t last_type_sig_
;
638 // Cache of the slot index for the last type signature.
639 unsigned int last_tu_slot_
;
642 // A specialization of Dwarf_info_reader, for reading dwo_names from
645 class Dwo_name_info_reader
: public Dwarf_info_reader
648 Dwo_name_info_reader(Relobj
* object
, unsigned int shndx
)
649 : Dwarf_info_reader(false, object
, NULL
, 0, shndx
, 0, 0),
653 ~Dwo_name_info_reader()
656 // Get the dwo_names from the DWARF compilation unit DIEs.
658 get_dwo_names(File_list
* files
)
660 this->files_
= files
;
665 // Visit a compilation unit.
667 visit_compilation_unit(off_t cu_offset
, off_t cu_length
, Dwarf_die
*);
670 // The list of files to populate.
674 // A specialization of Dwarf_info_reader, for reading dwo_ids and
675 // type signatures from DWARF CUs and TUs.
677 class Dwo_id_info_reader
: public Dwarf_info_reader
680 Dwo_id_info_reader(bool is_type_unit
,
683 : Dwarf_info_reader(is_type_unit
, object
, NULL
, 0, shndx
, 0, 0),
684 dwo_id_found_(false), dwo_id_(0), type_sig_found_(false), type_sig_(0)
687 ~Dwo_id_info_reader()
690 // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID.
692 get_dwo_id(uint64_t* dwo_id
)
695 if (!this->dwo_id_found_
)
697 *dwo_id
= this->dwo_id_
;
701 // Return the type signature from a DWARF type unit DIE in *TYPE_SIG.
703 get_type_sig(uint64_t* type_sig
)
706 if (!this->type_sig_found_
)
708 *type_sig
= this->type_sig_
;
713 // Visit a compilation unit.
715 visit_compilation_unit(off_t cu_offset
, off_t cu_length
, Dwarf_die
*);
717 // Visit a type unit.
719 visit_type_unit(off_t tu_offset
, off_t type_offset
, uint64_t signature
,
723 // TRUE if we found a dwo_id.
727 // TRUE if we found a type signature.
728 bool type_sig_found_
;
729 // The type signature.
733 // Class Sized_relobj_dwo.
735 // Setup the section information.
737 template <int size
, bool big_endian
>
739 Sized_relobj_dwo
<size
, big_endian
>::setup()
741 const unsigned int shnum
= this->elf_file_
.shnum();
742 this->set_shnum(shnum
);
743 this->section_offsets().resize(shnum
);
746 // Return a view of the contents of a section.
748 template <int size
, bool big_endian
>
750 Sized_relobj_dwo
<size
, big_endian
>::do_section_contents(
752 section_size_type
* plen
,
755 Object::Location
loc(this->elf_file_
.section_contents(shndx
));
756 *plen
= convert_to_section_size_type(loc
.data_size
);
759 static const unsigned char empty
[1] = { '\0' };
762 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
765 // Return a view of the uncompressed contents of a section. Set *PLEN
766 // to the size. Set *IS_NEW to true if the contents need to be deleted
769 template <int size
, bool big_endian
>
771 Sized_relobj_dwo
<size
, big_endian
>::do_decompressed_section_contents(
773 section_size_type
* plen
,
776 section_size_type buffer_size
;
777 const unsigned char* buffer
= this->do_section_contents(shndx
, &buffer_size
,
780 std::string sect_name
= this->do_section_name(shndx
);
781 if (!is_prefix_of(".zdebug_", sect_name
.c_str()))
788 section_size_type uncompressed_size
= get_uncompressed_size(buffer
,
790 unsigned char* uncompressed_data
= new unsigned char[uncompressed_size
];
791 if (!decompress_input_section(buffer
,
795 this->error(_("could not decompress section %s"),
796 this->section_name(shndx
).c_str());
797 *plen
= uncompressed_size
;
799 return uncompressed_data
;
804 Dwo_file::~Dwo_file()
806 if (this->obj_
!= NULL
)
808 if (this->input_file_
!= NULL
)
809 delete this->input_file_
;
812 // Read the input executable file and extract the list of .dwo files
813 // that it references.
816 Dwo_file::read_executable(File_list
* files
)
818 this->obj_
= this->make_object(NULL
);
820 unsigned int shnum
= this->shnum();
821 this->is_compressed_
.resize(shnum
);
822 this->shndx_map_
.resize(shnum
);
824 unsigned int debug_info
= 0;
825 unsigned int debug_abbrev
= 0;
827 // Scan the section table and collect the debug sections we need.
828 // (Section index 0 is a dummy section; skip it.)
829 for (unsigned int i
= 1; i
< shnum
; i
++)
831 if (this->section_type(i
) != elfcpp::SHT_PROGBITS
)
833 std::string sect_name
= this->section_name(i
);
834 const char* suffix
= sect_name
.c_str();
835 if (is_prefix_of(".debug_", suffix
))
837 else if (is_prefix_of(".zdebug_", suffix
))
839 this->is_compressed_
[i
] = true;
844 if (strcmp(suffix
, "info") == 0)
846 else if (strcmp(suffix
, "abbrev") == 0)
852 Dwo_name_info_reader
dwarf_reader(this->obj_
, debug_info
);
853 dwarf_reader
.set_abbrev_shndx(debug_abbrev
);
854 dwarf_reader
.get_dwo_names(files
);
858 // Read the input file and send its contents to OUTPUT_FILE.
861 Dwo_file::read(Dwp_output_file
* output_file
)
863 this->obj_
= this->make_object(output_file
);
865 unsigned int shnum
= this->shnum();
866 this->is_compressed_
.resize(shnum
);
867 this->shndx_map_
.resize(shnum
);
869 typedef std::vector
<unsigned int> Types_list
;
870 Types_list debug_types
;
871 unsigned int debug_info
= 0;
872 unsigned int debug_abbrev
= 0;
873 unsigned int debug_line
= 0;
874 unsigned int debug_loc
= 0;
875 unsigned int debug_str
= 0;
876 unsigned int debug_str_offsets
= 0;
877 unsigned int debug_macinfo
= 0;
878 unsigned int debug_macro
= 0;
879 unsigned int debug_cu_index
= 0;
880 unsigned int debug_tu_index
= 0;
882 // Scan the section table and look for .dwp index sections.
883 // (Section index 0 is a dummy section; skip it.)
884 for (unsigned int i
= 1; i
< shnum
; i
++)
886 if (this->section_type(i
) != elfcpp::SHT_PROGBITS
)
888 std::string sect_name
= this->section_name(i
);
889 const char* suffix
= sect_name
.c_str();
890 if (is_prefix_of(".debug_", suffix
))
892 else if (is_prefix_of(".zdebug_", suffix
))
894 this->is_compressed_
[i
] = true;
899 if (strcmp(suffix
, "cu_index") == 0)
901 else if (strcmp(suffix
, "tu_index") == 0)
903 else if (strcmp(suffix
, "str.dwo") == 0)
907 // Merge the input string table into the output string table.
908 this->add_strings(output_file
, debug_str
);
910 // If we found any .dwp index sections, read those and add the section
911 // sets to the output file.
912 if (debug_cu_index
> 0 || debug_tu_index
> 0)
914 if (debug_cu_index
> 0)
915 this->read_compunit_index(debug_cu_index
, output_file
);
916 if (debug_tu_index
> 0)
917 this->read_typeunit_index(debug_tu_index
, output_file
);
921 // If we found no index sections, this is a .dwo file.
922 // Scan the section table and collect the debug sections.
923 for (unsigned int i
= 1; i
< shnum
; i
++)
925 if (this->section_type(i
) != elfcpp::SHT_PROGBITS
)
927 std::string sect_name
= this->section_name(i
);
928 const char* suffix
= sect_name
.c_str();
929 if (is_prefix_of(".debug_", suffix
))
931 else if (is_prefix_of(".zdebug_", suffix
))
935 // TODO: Check for one of each section (except .debug_types).
936 if (strcmp(suffix
, "info.dwo") == 0)
938 else if (strcmp(suffix
, "types.dwo") == 0)
939 debug_types
.push_back(i
);
940 else if (strcmp(suffix
, "abbrev.dwo") == 0)
942 else if (strcmp(suffix
, "line.dwo") == 0)
944 else if (strcmp(suffix
, "loc.dwo") == 0)
946 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
947 debug_str_offsets
= i
;
948 else if (strcmp(suffix
, "macinfo.dwo") == 0)
950 else if (strcmp(suffix
, "macro.dwo") == 0)
956 // Extract the dwo_id from .debug_info.dwo section.
958 Dwo_id_info_reader
dwarf_reader(false, this->obj_
, debug_info
);
959 dwarf_reader
.set_abbrev_shndx(debug_abbrev
);
960 if (!dwarf_reader
.get_dwo_id(&dwo_id
))
961 gold_fatal(_("%s: .debug_info.dwo section does not have DW_AT_GNU_dwo_id "
962 "attribute"), this->name_
);
963 this->add_cu_set(output_file
, dwo_id
, debug_info
, debug_abbrev
,
964 debug_line
, debug_loc
, debug_str_offsets
,
965 debug_macinfo
, debug_macro
);
968 for (Types_list::const_iterator tp
= debug_types
.begin();
969 tp
!= debug_types
.end();
972 // Extract the type signature from .debug_types.dwo section.
974 gold_assert(*tp
> 0);
975 Dwo_id_info_reader
dwarf_reader(true, this->obj_
, *tp
);
976 dwarf_reader
.set_abbrev_shndx(debug_abbrev
);
977 if (!dwarf_reader
.get_type_sig(&type_sig
))
978 gold_fatal(_("%s: .debug_types.dwo section does not have type signature"),
980 this->add_tu_set(output_file
, type_sig
, *tp
, debug_abbrev
, debug_line
,
985 // Create a Sized_relobj_dwo of the given size and endianness,
986 // and record the target info.
989 Dwo_file::make_object(Dwp_output_file
* output_file
)
991 // Open the input file.
992 Input_file
* input_file
= new Input_file(this->name_
);
993 this->input_file_
= input_file
;
996 if (!input_file
->open(dirpath
, NULL
, &index
))
997 gold_fatal(_("%s: can't open"), this->name_
);
999 // Check that it's an ELF file.
1000 off_t filesize
= input_file
->file().filesize();
1001 int hdrsize
= elfcpp::Elf_recognizer::max_header_size
;
1002 if (filesize
< hdrsize
)
1004 const unsigned char* elf_header
=
1005 input_file
->file().get_view(0, 0, hdrsize
, true, false);
1006 if (!elfcpp::Elf_recognizer::is_elf_file(elf_header
, hdrsize
))
1007 gold_fatal(_("%s: not an ELF object file"), this->name_
);
1009 // Get the size, endianness, machine, etc. info from the header,
1010 // make an appropriately-sized Relobj, and pass the target info
1011 // to the output object.
1015 if (!elfcpp::Elf_recognizer::is_valid_header(elf_header
, hdrsize
, &size
,
1016 &big_endian
, &error
))
1017 gold_fatal(_("%s: %s"), this->name_
, error
.c_str());
1022 #ifdef HAVE_TARGET_32_BIG
1023 return this->sized_make_object
<32, true>(elf_header
, input_file
,
1029 #ifdef HAVE_TARGET_32_LITTLE
1030 return this->sized_make_object
<32, false>(elf_header
, input_file
,
1036 else if (size
== 64)
1039 #ifdef HAVE_TARGET_64_BIG
1040 return this->sized_make_object
<64, true>(elf_header
, input_file
,
1046 #ifdef HAVE_TARGET_64_LITTLE
1047 return this->sized_make_object
<64, false>(elf_header
, input_file
,
1057 // Function template to create a Sized_relobj_dwo and record the target info.
1058 // P is a pointer to the ELF header in memory.
1060 template <int size
, bool big_endian
>
1062 Dwo_file::sized_make_object(const unsigned char* p
, Input_file
* input_file
,
1063 Dwp_output_file
* output_file
)
1065 elfcpp::Ehdr
<size
, big_endian
> ehdr(p
);
1066 Sized_relobj_dwo
<size
, big_endian
>* obj
=
1067 new Sized_relobj_dwo
<size
, big_endian
>(this->name_
, input_file
, ehdr
);
1069 if (output_file
!= NULL
)
1070 output_file
->record_target_info(
1071 this->name_
, ehdr
.get_e_machine(), size
, big_endian
,
1072 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
1073 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
1077 // Read the .debug_cu_index section of a .dwp file,
1078 // and process the CU sets.
1081 Dwo_file::read_compunit_index(unsigned int shndx
, Dwp_output_file
* output_file
)
1083 if (this->obj_
->is_big_endian())
1084 this->sized_read_compunit_index
<true>(shndx
, output_file
);
1086 this->sized_read_compunit_index
<false>(shndx
, output_file
);
1089 template <bool big_endian
>
1091 Dwo_file::sized_read_compunit_index(unsigned int shndx
,
1092 Dwp_output_file
* output_file
)
1094 section_size_type len
;
1096 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1098 unsigned int version
=
1099 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
);
1101 gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"),
1102 this->name_
, version
);
1104 unsigned int nused
=
1105 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1106 + 2 * sizeof(uint32_t));
1110 unsigned int nslots
=
1111 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1112 + 3 * sizeof(uint32_t));
1114 const unsigned char* phash
= contents
+ 4 * sizeof(uint32_t);
1115 const unsigned char* pindex
= phash
+ nslots
* sizeof(uint64_t);
1116 const unsigned char* shndx_pool
= pindex
+ nslots
* sizeof(uint32_t);
1117 const unsigned char* limit
= contents
+ len
;
1119 if (shndx_pool
>= limit
)
1120 gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_
);
1122 // Loop over the slots of the hash table.
1123 for (unsigned int i
= 0; i
< nslots
; ++i
)
1126 elfcpp::Swap_unaligned
<64, big_endian
>::readval(phash
);
1129 unsigned int index
=
1130 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pindex
);
1131 const unsigned char* shndx_list
=
1132 shndx_pool
+ index
* sizeof(uint32_t);
1134 // Collect the debug sections for this compilation unit set.
1135 unsigned int debug_info
= 0;
1136 unsigned int debug_abbrev
= 0;
1137 unsigned int debug_line
= 0;
1138 unsigned int debug_loc
= 0;
1139 unsigned int debug_str_offsets
= 0;
1140 unsigned int debug_macinfo
= 0;
1141 unsigned int debug_macro
= 0;
1144 if (shndx_list
>= limit
)
1145 gold_fatal(_("%s: .debug_cu_index is corrupt"),
1147 unsigned int shndx
=
1148 elfcpp::Swap_unaligned
<32, big_endian
>::readval(shndx_list
);
1151 if (shndx
> this->shnum())
1152 gold_fatal(_("%s: .debug_cu_index has bad shndx"),
1154 std::string sect_name
= this->section_name(shndx
);
1155 const char* suffix
= sect_name
.c_str();
1156 if (is_prefix_of(".debug_", suffix
))
1158 else if (is_prefix_of(".zdebug_", suffix
))
1161 gold_fatal(_("%s: .debug_cu_index refers to "
1162 "non-debug section"), this->name_
);
1163 if (strcmp(suffix
, "info.dwo") == 0)
1165 else if (strcmp(suffix
, "abbrev.dwo") == 0)
1166 debug_abbrev
= shndx
;
1167 else if (strcmp(suffix
, "line.dwo") == 0)
1169 else if (strcmp(suffix
, "loc.dwo") == 0)
1171 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
1172 debug_str_offsets
= shndx
;
1173 else if (strcmp(suffix
, "macinfo.dwo") == 0)
1174 debug_macinfo
= shndx
;
1175 else if (strcmp(suffix
, "macro.dwo") == 0)
1176 debug_macro
= shndx
;
1177 shndx_list
+= sizeof(uint32_t);
1179 this->add_cu_set(output_file
, dwo_id
, debug_info
, debug_abbrev
,
1180 debug_line
, debug_loc
, debug_str_offsets
,
1181 debug_macinfo
, debug_macro
);
1183 phash
+= sizeof(uint64_t);
1184 pindex
+= sizeof(uint32_t);
1191 // Read the .debug_tu_index section of a .dwp file,
1192 // and process the TU sets.
1195 Dwo_file::read_typeunit_index(unsigned int shndx
, Dwp_output_file
* output_file
)
1197 if (this->obj_
->is_big_endian())
1198 this->sized_read_typeunit_index
<true>(shndx
, output_file
);
1200 this->sized_read_typeunit_index
<false>(shndx
, output_file
);
1203 template <bool big_endian
>
1205 Dwo_file::sized_read_typeunit_index(unsigned int shndx
,
1206 Dwp_output_file
* output_file
)
1208 section_size_type len
;
1210 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1212 unsigned int version
=
1213 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
);
1215 gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"),
1216 this->name_
, version
);
1218 unsigned int nused
=
1219 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1220 + 2 * sizeof(uint32_t));
1224 unsigned int nslots
=
1225 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1226 + 3 * sizeof(uint32_t));
1228 const unsigned char* phash
= contents
+ 4 * sizeof(uint32_t);
1229 const unsigned char* pindex
= phash
+ nslots
* sizeof(uint64_t);
1230 const unsigned char* shndx_pool
= pindex
+ nslots
* sizeof(uint32_t);
1231 const unsigned char* limit
= contents
+ len
;
1233 if (shndx_pool
>= limit
)
1234 gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_
);
1236 // Loop over the slots of the hash table.
1237 for (unsigned int i
= 0; i
< nslots
; ++i
)
1240 elfcpp::Swap_unaligned
<64, big_endian
>::readval(phash
);
1243 unsigned int index
=
1244 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pindex
);
1245 const unsigned char* shndx_list
=
1246 shndx_pool
+ index
* sizeof(uint32_t);
1248 // Collect the debug sections for this type unit set.
1249 unsigned int debug_types
= 0;
1250 unsigned int debug_abbrev
= 0;
1251 unsigned int debug_line
= 0;
1252 unsigned int debug_str_offsets
= 0;
1255 if (shndx_list
>= limit
)
1256 gold_fatal(_("%s: .debug_tu_index is corrupt"),
1258 unsigned int shndx
=
1259 elfcpp::Swap_unaligned
<32, big_endian
>::readval(shndx_list
);
1262 if (shndx
> this->shnum())
1263 gold_fatal(_("%s: .debug_tu_index has bad shndx"),
1265 std::string sect_name
= this->section_name(shndx
);
1266 const char* suffix
= sect_name
.c_str();
1267 if (is_prefix_of(".debug_", suffix
))
1269 else if (is_prefix_of(".zdebug_", suffix
))
1272 gold_fatal(_("%s: .debug_tu_index refers to "
1273 "non-debug section"), this->name_
);
1274 if (strcmp(suffix
, "types.dwo") == 0)
1275 debug_types
= shndx
;
1276 else if (strcmp(suffix
, "abbrev.dwo") == 0)
1277 debug_abbrev
= shndx
;
1278 else if (strcmp(suffix
, "line.dwo") == 0)
1280 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
1281 debug_str_offsets
= shndx
;
1282 shndx_list
+= sizeof(uint32_t);
1284 this->add_tu_set(output_file
, type_sig
, debug_types
, debug_abbrev
,
1285 debug_line
, debug_str_offsets
);
1287 phash
+= sizeof(uint64_t);
1288 pindex
+= sizeof(uint32_t);
1295 // Merge the input string table section into the output file.
1298 Dwo_file::add_strings(Dwp_output_file
* output_file
, unsigned int debug_str
)
1300 section_size_type len
;
1302 const unsigned char* pdata
= this->section_contents(debug_str
, &len
, &is_new
);
1303 const char* p
= reinterpret_cast<const char*>(pdata
);
1304 const char* pend
= p
+ len
;
1306 // Check that the last string is null terminated.
1307 if (pend
[-1] != '\0')
1308 gold_fatal(_("%s: last entry in string section '%s' "
1309 "is not null terminated"),
1311 this->section_name(debug_str
).c_str());
1313 // Count the number of strings in the section, and size the map.
1315 for (const char* pt
= p
; pt
< pend
; pt
+= strlen(pt
) + 1)
1317 this->str_offset_map_
.reserve(count
+ 1);
1319 // Add the strings to the output string table, and record the new offsets
1321 section_offset_type i
= 0;
1322 section_offset_type new_offset
;
1325 size_t len
= strlen(p
);
1326 new_offset
= output_file
->add_string(p
, len
);
1327 this->str_offset_map_
.push_back(std::make_pair(i
, new_offset
));
1332 this->str_offset_map_
.push_back(std::make_pair(i
, new_offset
));
1337 // Copy a section from the input file to the output file.
1338 // If IS_STR_OFFSETS is true, remap the string offsets for the
1339 // output string table.
1342 Dwo_file::copy_section(Dwp_output_file
* output_file
, unsigned int shndx
,
1343 const char* section_name
, bool is_str_offsets
)
1345 // Some sections may be referenced from more than one set.
1346 // Don't copy a section more than once.
1347 if (this->shndx_map_
[shndx
] > 0)
1348 return this->shndx_map_
[shndx
];
1350 section_size_type len
;
1352 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1356 const unsigned char* remapped
= this->remap_str_offsets(contents
, len
);
1359 contents
= remapped
;
1363 this->shndx_map_
[shndx
] = output_file
->add_section(section_name
, contents
,
1368 return this->shndx_map_
[shndx
];
1372 const unsigned char*
1373 Dwo_file::remap_str_offsets(const unsigned char* contents
,
1374 section_size_type len
)
1377 gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"),
1380 if (this->obj_
->is_big_endian())
1381 return this->sized_remap_str_offsets
<true>(contents
, len
);
1383 return this->sized_remap_str_offsets
<false>(contents
, len
);
1386 template <bool big_endian
>
1387 const unsigned char*
1388 Dwo_file::sized_remap_str_offsets(const unsigned char* contents
,
1389 section_size_type len
)
1391 unsigned char* remapped
= new unsigned char[len
];
1392 const unsigned char* p
= contents
;
1393 unsigned char* q
= remapped
;
1396 unsigned int val
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(p
);
1397 val
= this->remap_str_offset(val
);
1398 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(q
, val
);
1407 Dwo_file::remap_str_offset(section_offset_type val
)
1409 Str_offset_map_entry entry
;
1412 Str_offset_map::const_iterator p
=
1413 std::lower_bound(this->str_offset_map_
.begin(),
1414 this->str_offset_map_
.end(),
1415 entry
, Offset_compare());
1417 if (p
== this->str_offset_map_
.end() || p
->first
> val
)
1419 if (p
== this->str_offset_map_
.begin())
1422 gold_assert(p
->first
<= val
);
1425 return p
->second
+ (val
- p
->first
);
1428 // Add a set of .debug_info and related sections to OUTPUT_FILE.
1431 Dwo_file::add_cu_set(Dwp_output_file
* output_file
,
1433 unsigned int debug_info
,
1434 unsigned int debug_abbrev
,
1435 unsigned int debug_line
,
1436 unsigned int debug_loc
,
1437 unsigned int debug_str_offsets
,
1438 unsigned int debug_macinfo
,
1439 unsigned int debug_macro
)
1441 if (debug_info
== 0)
1442 gold_fatal(_("%s: no .debug_info.dwo section found"), this->name_
);
1443 if (debug_abbrev
== 0)
1444 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_
);
1446 debug_abbrev
= this->copy_section(output_file
, debug_abbrev
,
1447 ".debug_abbrev.dwo", false);
1449 debug_line
= this->copy_section(output_file
, debug_line
,
1450 ".debug_line.dwo", false);
1452 debug_loc
= this->copy_section(output_file
, debug_loc
, ".debug_loc.dwo",
1454 if (debug_macinfo
> 0)
1455 debug_macinfo
= this->copy_section(output_file
, debug_macinfo
,
1456 ".debug_macinfo.dwo", false);
1457 if (debug_macro
> 0)
1458 debug_macro
= this->copy_section(output_file
, debug_macro
,
1459 ".debug_macro.dwo", false);
1461 if (debug_str_offsets
> 0)
1462 debug_str_offsets
= this->copy_section(output_file
, debug_str_offsets
,
1463 ".debug_str_offsets.dwo", true);
1465 debug_info
= this->copy_section(output_file
, debug_info
, ".debug_info.dwo",
1468 output_file
->add_cu_set(dwo_id
, debug_info
, debug_abbrev
, debug_line
,
1469 debug_loc
, debug_str_offsets
, debug_macinfo
,
1473 // Add a set of .debug_types and related sections to OUTPUT_FILE.
1476 Dwo_file::add_tu_set(Dwp_output_file
* output_file
,
1478 unsigned int debug_types
,
1479 unsigned int debug_abbrev
,
1480 unsigned int debug_line
,
1481 unsigned int debug_str_offsets
)
1483 if (debug_types
== 0)
1484 gold_fatal(_("%s: no .debug_types.dwo section found"), this->name_
);
1485 if (debug_abbrev
== 0)
1486 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_
);
1488 // Ignore duplicate type signatures.
1489 if (output_file
->lookup_tu(type_sig
))
1492 debug_abbrev
= this->copy_section(output_file
, debug_abbrev
,
1493 ".debug_abbrev.dwo", false);
1495 debug_line
= this->copy_section(output_file
, debug_line
,
1496 ".debug_line.dwo", false);
1498 if (debug_str_offsets
> 0)
1499 debug_str_offsets
= this->copy_section(output_file
, debug_str_offsets
,
1500 ".debug_str_offsets.dwo", true);
1502 debug_types
= this->copy_section(output_file
, debug_types
,
1503 ".debug_types.dwo", false);
1505 output_file
->add_tu_set(type_sig
, debug_types
, debug_abbrev
, debug_line
,
1509 // Class Dwp_output_file.
1511 // Record the target info from an input file. On first call, we
1512 // set the ELF header values for the output file. On subsequent
1513 // calls, we just verify that the values match.
1516 Dwp_output_file::record_target_info(const char*, int machine
,
1517 int size
, bool big_endian
,
1518 int osabi
, int abiversion
)
1520 // TODO: Check the values on subsequent calls.
1521 if (this->size_
> 0)
1524 this->machine_
= machine
;
1526 this->big_endian_
= big_endian
;
1527 this->osabi_
= osabi
;
1528 this->abiversion_
= abiversion
;
1531 this->next_file_offset_
= elfcpp::Elf_sizes
<32>::ehdr_size
;
1532 else if (size
== 64)
1533 this->next_file_offset_
= elfcpp::Elf_sizes
<64>::ehdr_size
;
1537 this->fd_
= ::fopen(this->name_
, "wb");
1538 if (this->fd_
== NULL
)
1539 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
1541 // Write zeroes for the ELF header initially. We'll write
1542 // the actual header during finalize().
1543 static const char buf
[elfcpp::Elf_sizes
<64>::ehdr_size
] = { 0 };
1544 if (::fwrite(buf
, 1, this->next_file_offset_
, this->fd_
)
1545 < (size_t) this->next_file_offset_
)
1546 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
1549 // Add a string to the debug strings section.
1552 Dwp_output_file::add_string(const char* str
, size_t len
)
1554 Stringpool::Key key
;
1555 this->stringpool_
.add_with_length(str
, len
, true, &key
);
1556 this->have_strings_
= true;
1557 // We aren't supposed to call get_offset() until after
1558 // calling set_string_offsets(), but the offsets will
1559 // not change unless optimizing the string pool.
1560 return this->stringpool_
.get_offset_from_key(key
);
1563 // Align the file offset to the given boundary.
1566 align_offset(off_t off
, int align
)
1568 return (off
+ align
- 1) & ~(align
- 1);
1571 // Add a section to the output file, and return the new section index.
1574 Dwp_output_file::add_section(const char* section_name
,
1575 const unsigned char* contents
,
1576 section_size_type len
,
1579 off_t file_offset
= this->next_file_offset_
;
1580 gold_assert(this->size_
> 0 && file_offset
> 0);
1582 file_offset
= align_offset(file_offset
, align
);
1584 ::fseek(this->fd_
, file_offset
, SEEK_SET
);
1585 if (::fwrite(contents
, 1, len
, this->fd_
) < len
)
1586 gold_fatal(_("%s: error writing section '%s'"), this->name_
, section_name
);
1588 section_name
= this->shstrtab_
.add_with_length(section_name
,
1589 strlen(section_name
),
1591 Section sect
= { section_name
, file_offset
, len
, align
};
1592 this->sections_
.push_back(sect
);
1594 this->next_file_offset_
= file_offset
+ len
;
1595 return this->shnum_
++;
1598 // Add a set of .debug_info and related sections to the output file.
1601 Dwp_output_file::add_cu_set(uint64_t dwo_id
,
1602 unsigned int debug_info
,
1603 unsigned int debug_abbrev
,
1604 unsigned int debug_line
,
1605 unsigned int debug_loc
,
1606 unsigned int debug_str_offsets
,
1607 unsigned int debug_macinfo
,
1608 unsigned int debug_macro
)
1610 Cu_or_tu_set cu_set
= { dwo_id
, debug_info
, debug_abbrev
, debug_line
,
1611 debug_loc
, debug_str_offsets
, debug_macinfo
,
1614 if (!this->cu_index_
.find_or_add(dwo_id
, &slot
))
1615 this->cu_index_
.enter_set(slot
, cu_set
);
1617 gold_warning(_("%s: duplicate entry for CU (dwo_id 0x%llx)"),
1618 this->name_
, (unsigned long long)dwo_id
);
1621 // Lookup a type signature and return TRUE if we have already seen it.
1623 Dwp_output_file::lookup_tu(uint64_t type_sig
)
1625 this->last_type_sig_
= type_sig
;
1626 return this->tu_index_
.find_or_add(type_sig
, &this->last_tu_slot_
);
1629 // Add a set of .debug_types and related sections to the output file.
1632 Dwp_output_file::add_tu_set(uint64_t type_sig
,
1633 unsigned int debug_types
,
1634 unsigned int debug_abbrev
,
1635 unsigned int debug_line
,
1636 unsigned int debug_str_offsets
)
1638 Cu_or_tu_set tu_set
= { type_sig
, debug_types
, debug_abbrev
, debug_line
,
1639 0, debug_str_offsets
, 0, 0 };
1641 if (type_sig
== this->last_type_sig_
)
1642 slot
= this->last_tu_slot_
;
1644 this->tu_index_
.find_or_add(type_sig
, &slot
);
1645 this->tu_index_
.enter_set(slot
, tu_set
);
1648 // Find a slot in the hash table for SIGNATURE. Return TRUE
1649 // if the entry already exists.
1652 Dwp_output_file::Dwp_index::find_or_add(uint64_t signature
,
1653 unsigned int* slotp
)
1655 if (this->capacity_
== 0)
1658 static_cast<unsigned int>(signature
) & (this->capacity_
- 1);
1659 unsigned int secondary_hash
;
1660 uint64_t probe
= this->hash_table_
[slot
];
1661 if (probe
!= 0 && probe
!= signature
)
1663 secondary_hash
= (static_cast<unsigned int>(signature
>> 32)
1664 & (this->capacity_
- 1)) | 1;
1667 slot
= (slot
+ secondary_hash
) & (this->capacity_
- 1);
1668 probe
= this->hash_table_
[slot
];
1669 } while (probe
!= 0 && probe
!= signature
);
1672 return (probe
!= 0);
1675 // Enter a CU or TU set at the given SLOT in the hash table.
1678 Dwp_output_file::Dwp_index::enter_set(unsigned int slot
,
1679 const Cu_or_tu_set
& set
)
1681 gold_assert(slot
< this->capacity_
);
1682 gold_assert(set
.debug_info_or_types
> 0);
1683 gold_assert(set
.debug_abbrev
> 0);
1685 // Add the section indexes to the pool.
1686 uint32_t pool_index
= this->shndx_pool_
.size();
1687 this->shndx_pool_
.push_back(set
.debug_info_or_types
);
1688 this->shndx_pool_
.push_back(set
.debug_abbrev
);
1689 if (set
.debug_line
> 0)
1690 this->shndx_pool_
.push_back(set
.debug_line
);
1691 if (set
.debug_loc
> 0)
1692 this->shndx_pool_
.push_back(set
.debug_loc
);
1693 if (set
.debug_str_offsets
> 0)
1694 this->shndx_pool_
.push_back(set
.debug_str_offsets
);
1695 if (set
.debug_macinfo
> 0)
1696 this->shndx_pool_
.push_back(set
.debug_macinfo
);
1697 if (set
.debug_macro
> 0)
1698 this->shndx_pool_
.push_back(set
.debug_macro
);
1699 this->shndx_pool_
.push_back(0);
1701 // Enter the signature and pool index into the hash table.
1702 gold_assert(this->hash_table_
[slot
] == 0);
1703 this->hash_table_
[slot
] = set
.signature
;
1704 this->index_table_
[slot
] = pool_index
;
1707 // Grow the hash table when we exceed 2/3 capacity.
1708 if (this->used_
* 3 > this->capacity_
* 2)
1712 // Initialize the hash table.
1715 Dwp_output_file::Dwp_index::initialize()
1717 this->capacity_
= 16;
1718 this->hash_table_
= new uint64_t[this->capacity_
];
1719 memset(this->hash_table_
, 0, this->capacity_
* sizeof(uint64_t));
1720 this->index_table_
= new uint32_t[this->capacity_
];
1721 memset(this->index_table_
, 0, this->capacity_
* sizeof(uint32_t));
1724 // Grow the hash table when we reach 2/3 capacity.
1727 Dwp_output_file::Dwp_index::grow()
1729 unsigned int old_capacity
= this->capacity_
;
1730 uint64_t* old_hash_table
= this->hash_table_
;
1731 uint32_t* old_index_table
= this->index_table_
;
1732 unsigned int old_used
= this->used_
;
1734 this->capacity_
= old_capacity
* 2;
1735 this->hash_table_
= new uint64_t[this->capacity_
];
1736 memset(this->hash_table_
, 0, this->capacity_
* sizeof(uint64_t));
1737 this->index_table_
= new uint32_t[this->capacity_
];
1738 memset(this->index_table_
, 0, this->capacity_
* sizeof(uint32_t));
1741 for (unsigned int i
= 0; i
< old_capacity
; ++i
)
1743 uint64_t signature
= old_hash_table
[i
];
1747 bool found
= this->find_or_add(signature
, &slot
);
1748 gold_assert(!found
);
1749 this->hash_table_
[slot
] = signature
;
1750 this->index_table_
[slot
] = old_index_table
[i
];
1754 gold_assert(this->used_
== old_used
);
1756 delete[] old_hash_table
;
1757 delete[] old_index_table
;
1760 // Initialize the output file.
1763 Dwp_output_file::initialize()
1765 // We can't initialize the output file until we've recorded the
1766 // target info from the first input file.
1767 gold_assert(this->size_
> 0);
1770 // Finalize the file, write the string tables and index sections,
1771 // and close the file.
1774 Dwp_output_file::finalize()
1778 // Write the debug string table.
1779 if (this->have_strings_
)
1781 this->stringpool_
.set_string_offsets();
1782 section_size_type len
= this->stringpool_
.get_strtab_size();
1783 buf
= new unsigned char[len
];
1784 this->stringpool_
.write_to_buffer(buf
, len
);
1785 this->add_section(".debug_str.dwo", buf
, len
, 1);
1789 // Write the CU and TU indexes.
1790 if (this->big_endian_
)
1792 this->write_index
<true>(".debug_cu_index", this->cu_index_
);
1793 this->write_index
<true>(".debug_tu_index", this->tu_index_
);
1797 this->write_index
<false>(".debug_cu_index", this->cu_index_
);
1798 this->write_index
<false>(".debug_tu_index", this->tu_index_
);
1801 off_t file_offset
= this->next_file_offset_
;
1803 // Write the section string table.
1804 this->shstrndx_
= this->shnum_
++;
1805 const char* shstrtab_name
=
1806 this->shstrtab_
.add_with_length(".shstrtab",
1807 sizeof(".shstrtab") - 1,
1809 this->shstrtab_
.set_string_offsets();
1810 section_size_type shstrtab_len
= this->shstrtab_
.get_strtab_size();
1811 buf
= new unsigned char[shstrtab_len
];
1812 this->shstrtab_
.write_to_buffer(buf
, shstrtab_len
);
1813 off_t shstrtab_off
= file_offset
;
1814 ::fseek(this->fd_
, file_offset
, 0);
1815 if (::fwrite(buf
, 1, shstrtab_len
, this->fd_
) < shstrtab_len
)
1816 gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_
);
1818 file_offset
+= shstrtab_len
;
1820 // Write the section header table. The first entry is a NULL entry.
1821 // This is followed by the debug sections, and finally we write the
1822 // .shstrtab section header.
1823 file_offset
= align_offset(file_offset
, this->size_
== 32 ? 4 : 8);
1824 this->shoff_
= file_offset
;
1825 ::fseek(this->fd_
, file_offset
, 0);
1826 section_size_type sh0_size
= 0;
1827 unsigned int sh0_link
= 0;
1828 if (this->shnum_
>= elfcpp::SHN_LORESERVE
)
1829 sh0_size
= this->shnum_
;
1830 if (this->shstrndx_
>= elfcpp::SHN_LORESERVE
)
1831 sh0_link
= this->shstrndx_
;
1832 this->write_shdr(NULL
, 0, 0, 0, 0, sh0_size
, sh0_link
, 0, 0, 0);
1833 for (unsigned int i
= 0; i
< this->sections_
.size(); ++i
)
1835 Section
& sect
= this->sections_
[i
];
1836 this->write_shdr(sect
.name
, elfcpp::SHT_PROGBITS
, 0, 0, sect
.offset
,
1837 sect
.size
, 0, 0, sect
.align
, 0);
1839 this->write_shdr(shstrtab_name
, elfcpp::SHT_STRTAB
, 0, 0,
1840 shstrtab_off
, shstrtab_len
, 0, 0, 1, 0);
1842 // Write the ELF header.
1846 if (this->fd_
!= NULL
)
1848 if (::fclose(this->fd_
) != 0)
1849 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
1854 // Write a CU or TU index section.
1855 template<bool big_endian
>
1857 Dwp_output_file::write_index(const char* sect_name
, const Dwp_index
& index
)
1859 const unsigned int nslots
= index
.hash_table_total_slots();
1860 const unsigned int nused
= index
.hash_table_used_slots();
1861 const unsigned int npool
= index
.shndx_pool_size();
1862 const section_size_type index_size
= (4 * sizeof(uint32_t)
1863 + nslots
* sizeof(uint64_t)
1864 + nslots
* sizeof(uint32_t)
1865 + npool
* sizeof(uint32_t));
1867 // Allocate a buffer for the section contents.
1868 unsigned char* buf
= new unsigned char[index_size
];
1869 unsigned char* p
= buf
;
1871 // Write the section header: version number, padding,
1872 // number of used slots and total number of slots.
1873 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, 1);
1874 p
+= sizeof(uint32_t);
1875 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, 0);
1876 p
+= sizeof(uint32_t);
1877 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, nused
);
1878 p
+= sizeof(uint32_t);
1879 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, nslots
);
1880 p
+= sizeof(uint32_t);
1882 // Write the hash table.
1883 for (unsigned int i
= 0; i
< nslots
; ++i
)
1885 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(p
, index
.hash_table(i
));
1886 p
+= sizeof(uint64_t);
1889 // Write the parallel index table.
1890 for (unsigned int i
= 0; i
< nslots
; ++i
)
1892 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, index
.index_table(i
));
1893 p
+= sizeof(uint32_t);
1896 // Write the section index pool.
1897 Dwp_index::Shndx_pool::const_iterator pool
= index
.shndx_pool();
1898 for (unsigned int i
= 0; i
< npool
; ++i
)
1900 gold_assert(pool
!= index
.shndx_pool_end());
1901 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, *pool
);
1902 p
+= sizeof(uint32_t);
1906 gold_assert(p
== buf
+ index_size
);
1908 this->add_section(sect_name
, buf
, index_size
, sizeof(uint64_t));
1913 // Write the ELF header.
1916 Dwp_output_file::write_ehdr()
1918 if (this->size_
== 32)
1920 if (this->big_endian_
)
1921 return this->sized_write_ehdr
<32, true>();
1923 return this->sized_write_ehdr
<32, false>();
1925 else if (this->size_
== 64)
1927 if (this->big_endian_
)
1928 return this->sized_write_ehdr
<64, true>();
1930 return this->sized_write_ehdr
<64, false>();
1936 template<unsigned int size
, bool big_endian
>
1938 Dwp_output_file::sized_write_ehdr()
1940 const unsigned int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
1941 unsigned char buf
[ehdr_size
];
1942 elfcpp::Ehdr_write
<size
, big_endian
> ehdr(buf
);
1944 unsigned char e_ident
[elfcpp::EI_NIDENT
];
1945 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
1946 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
1947 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
1948 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
1949 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
1951 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
1952 else if (size
== 64)
1953 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
1956 e_ident
[elfcpp::EI_DATA
] = (big_endian
1957 ? elfcpp::ELFDATA2MSB
1958 : elfcpp::ELFDATA2LSB
);
1959 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
1960 ehdr
.put_e_ident(e_ident
);
1962 ehdr
.put_e_type(elfcpp::ET_REL
);
1963 ehdr
.put_e_machine(this->machine_
);
1964 ehdr
.put_e_version(elfcpp::EV_CURRENT
);
1965 ehdr
.put_e_entry(0);
1966 ehdr
.put_e_phoff(0);
1967 ehdr
.put_e_shoff(this->shoff_
);
1968 ehdr
.put_e_flags(0);
1969 ehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
1970 ehdr
.put_e_phentsize(0);
1971 ehdr
.put_e_phnum(0);
1972 ehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
1973 ehdr
.put_e_shnum(this->shnum_
< elfcpp::SHN_LORESERVE
? this->shnum_
: 0);
1974 ehdr
.put_e_shstrndx(this->shstrndx_
< elfcpp::SHN_LORESERVE
1976 : static_cast<unsigned int>(elfcpp::SHN_XINDEX
));
1978 ::fseek(this->fd_
, 0, 0);
1979 if (::fwrite(buf
, 1, ehdr_size
, this->fd_
) < ehdr_size
)
1980 gold_fatal(_("%s: error writing ELF header"), this->name_
);
1983 // Write a section header.
1986 Dwp_output_file::write_shdr(const char* name
, unsigned int type
,
1987 unsigned int flags
, uint64_t addr
, off_t offset
,
1988 section_size_type sect_size
, unsigned int link
,
1989 unsigned int info
, unsigned int align
,
1990 unsigned int ent_size
)
1992 if (this->size_
== 32)
1994 if (this->big_endian_
)
1995 return this->sized_write_shdr
<32, true>(name
, type
, flags
, addr
,
1996 offset
, sect_size
, link
, info
,
1999 return this->sized_write_shdr
<32, false>(name
, type
, flags
, addr
,
2000 offset
, sect_size
, link
, info
,
2003 else if (this->size_
== 64)
2005 if (this->big_endian_
)
2006 return this->sized_write_shdr
<64, true>(name
, type
, flags
, addr
,
2007 offset
, sect_size
, link
, info
,
2010 return this->sized_write_shdr
<64, false>(name
, type
, flags
, addr
,
2011 offset
, sect_size
, link
, info
,
2018 template<unsigned int size
, bool big_endian
>
2020 Dwp_output_file::sized_write_shdr(const char* name
, unsigned int type
,
2021 unsigned int flags
, uint64_t addr
,
2022 off_t offset
, section_size_type sect_size
,
2023 unsigned int link
, unsigned int info
,
2024 unsigned int align
, unsigned int ent_size
)
2026 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2027 unsigned char buf
[shdr_size
];
2028 elfcpp::Shdr_write
<size
, big_endian
> shdr(buf
);
2030 shdr
.put_sh_name(name
== NULL
? 0 : this->shstrtab_
.get_offset(name
));
2031 shdr
.put_sh_type(type
);
2032 shdr
.put_sh_flags(flags
);
2033 shdr
.put_sh_addr(addr
);
2034 shdr
.put_sh_offset(offset
);
2035 shdr
.put_sh_size(sect_size
);
2036 shdr
.put_sh_link(link
);
2037 shdr
.put_sh_info(info
);
2038 shdr
.put_sh_addralign(align
);
2039 shdr
.put_sh_entsize(ent_size
);
2040 if (::fwrite(buf
, 1, shdr_size
, this->fd_
) < shdr_size
)
2041 gold_fatal(_("%s: error writing section header table"), this->name_
);
2044 // Class Dwo_name_info_reader.
2046 // Visit a compilation unit.
2049 Dwo_name_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
* die
)
2051 const char* dwo_name
= die
->string_attribute(elfcpp::DW_AT_GNU_dwo_name
);
2052 if (dwo_name
!= NULL
)
2053 this->files_
->push_back(dwo_name
);
2056 // Class Dwo_id_info_reader.
2058 // Visit a compilation unit.
2061 Dwo_id_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
* die
)
2063 this->dwo_id_
= die
->uint_attribute(elfcpp::DW_AT_GNU_dwo_id
);
2064 if (this->dwo_id_
!= 0)
2065 this->dwo_id_found_
= true;
2068 // Visit a type unit.
2071 Dwo_id_info_reader::visit_type_unit(off_t
, off_t
, uint64_t signature
,
2074 this->type_sig_
= signature
;
2075 this->type_sig_found_
= true;
2078 }; // End namespace gold
2080 using namespace gold
;
2084 struct option dwp_options
[] =
2086 { "exec", required_argument
, NULL
, 'e' },
2087 { "help", no_argument
, NULL
, 'h' },
2088 { "output", required_argument
, NULL
, 'o' },
2089 { "verbose", no_argument
, NULL
, 'v' },
2090 { "version", no_argument
, NULL
, 'V' },
2091 { NULL
, 0, NULL
, 0 }
2094 // Print usage message and exit.
2097 usage(FILE* fd
, int exit_status
)
2099 fprintf(fd
, _("Usage: %s [options] [file...]\n"), program_name
);
2100 fprintf(fd
, _(" -h, --help Print this help message\n"));
2101 fprintf(fd
, _(" -e EXE, --exec EXE Get list of dwo files from EXE"
2102 " (defaults output to EXE.dwp)\n"));
2103 fprintf(fd
, _(" -o FILE, --output FILE Set output dwp file name\n"));
2104 fprintf(fd
, _(" -v, --verbose Verbose output\n"));
2105 fprintf(fd
, _(" -V, --version Print version number\n"));
2107 // REPORT_BUGS_TO is defined in bfd/bfdver.h.
2108 const char* report
= REPORT_BUGS_TO
;
2109 if (*report
!= '\0')
2110 fprintf(fd
, _("\nReport bugs to %s\n"), report
);
2114 // Report version information.
2119 // This output is intended to follow the GNU standards.
2120 printf("GNU dwp %s\n", BFD_VERSION_STRING
);
2121 printf(_("Copyright 2012 Free Software Foundation, Inc.\n"));
2123 This program is free software; you may redistribute it under the terms of\n\
2124 the GNU General Public License version 3 or (at your option) any later version.\n\
2125 This program has absolutely no warranty.\n"));
2132 main(int argc
, char** argv
)
2134 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2135 setlocale(LC_MESSAGES
, "");
2137 #if defined (HAVE_SETLOCALE)
2138 setlocale(LC_CTYPE
, "");
2140 bindtextdomain(PACKAGE
, LOCALEDIR
);
2141 textdomain(PACKAGE
);
2143 program_name
= argv
[0];
2145 // Initialize the global parameters, to let random code get to the
2147 Errors
errors(program_name
);
2148 set_parameters_errors(&errors
);
2150 // Initialize gold's global options. We don't use these in
2151 // this program, but they need to be initialized so that
2152 // functions we call from libgold work properly.
2153 General_options options
;
2154 set_parameters_options(&options
);
2156 // In libiberty; expands @filename to the args in "filename".
2157 expandargv(&argc
, &argv
);
2159 // Collect file names and options.
2161 std::string output_filename
;
2162 const char* exe_filename
= NULL
;
2163 bool verbose
= false;
2165 while ((c
= getopt_long(argc
, argv
, "e:ho:vV", dwp_options
, NULL
)) != -1)
2170 usage(stdout
, EXIT_SUCCESS
);
2172 exe_filename
= optarg
;
2175 output_filename
.assign(optarg
);
2184 usage(stderr
, EXIT_FAILURE
);
2188 if (output_filename
.empty())
2190 if (exe_filename
== NULL
)
2191 gold_fatal(_("no output file specified"));
2192 output_filename
.assign(exe_filename
);
2193 output_filename
.append(".dwp");
2196 Dwp_output_file
output_file(output_filename
.c_str());
2198 // Get list of .dwo files from the executable.
2199 if (exe_filename
!= NULL
)
2201 Dwo_file
exe_file(exe_filename
);
2202 exe_file
.read_executable(&files
);
2205 // Add any additional files listed on command line.
2206 for (int i
= optind
; i
< argc
; ++i
)
2207 files
.push_back(argv
[i
]);
2209 if (exe_filename
== NULL
&& files
.empty())
2210 gold_fatal(_("no input files and no executable specified"));
2212 // Process each file, adding its contents to the output file.
2213 for (File_list::const_iterator f
= files
.begin(); f
!= files
.end(); ++f
)
2216 fprintf(stderr
, "%s\n", f
->c_str());
2217 Dwo_file
dwo_file(f
->c_str());
2218 dwo_file
.read(&output_file
);
2221 output_file
.finalize();
2223 return EXIT_SUCCESS
;