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"
39 #include "elfcpp_file.h"
40 #include "dirsearch.h"
43 #include "compressed_output.h"
44 #include "stringpool.h"
45 #include "dwarf_reader.h"
48 usage() ATTRIBUTE_NORETURN
;
52 class Dwp_output_file
;
55 // This class may represent either a .dwo file or a .dwp file
56 // produced by an earlier run.
58 template <int size
, bool big_endian
>
59 class Sized_relobj_dwo
;
64 Dwo_file(const char* name
)
65 : name_(name
), obj_(NULL
), input_file_(NULL
), is_compressed_(),
71 // Read the input file and send its contents to OUTPUT_FILE.
73 read(Dwp_output_file
* output_file
);
76 // Types for mapping input string offsets to output string offsets.
77 typedef std::pair
<section_offset_type
, section_offset_type
>
79 typedef std::vector
<Str_offset_map_entry
> Str_offset_map
;
81 // A less-than comparison routine for Str_offset_map.
85 operator()(const Str_offset_map_entry
& i1
,
86 const Str_offset_map_entry
& i2
) const
87 { return i1
.first
< i2
.first
; }
90 // Create a Sized_relobj_dwo of the given size and endianness,
91 // and record the target info. P is a pointer to the ELF header
94 make_object(int size
, bool big_endian
, const unsigned char* p
,
95 Input_file
* input_file
, Dwp_output_file
* output_file
);
97 template <int size
, bool big_endian
>
99 sized_make_object(const unsigned char* p
, Input_file
* input_file
,
100 Dwp_output_file
* output_file
);
102 // Return the number of sections in the input object file.
105 { return this->obj_
->shnum(); }
107 // Return section type.
109 section_type(unsigned int shndx
)
110 { return this->obj_
->section_type(shndx
); }
112 // Get the name of a section.
114 section_name(unsigned int shndx
)
115 { return this->obj_
->section_name(shndx
); }
117 // Return a view of the contents of a section, decompressed if necessary.
118 // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
119 // deleted by the caller.
121 section_contents(unsigned int shndx
, section_size_type
* plen
, bool* is_new
)
122 { return this->obj_
->decompressed_section_contents(shndx
, plen
, is_new
); }
124 // Read the .debug_cu_index section of a .dwp file,
125 // and process the CU sets.
127 read_compunit_index(unsigned int, Dwp_output_file
*);
129 template <bool big_endian
>
131 sized_read_compunit_index(unsigned int, Dwp_output_file
*);
133 // Read the .debug_tu_index section of a .dwp file,
134 // and process the TU sets.
136 read_typeunit_index(unsigned int, Dwp_output_file
*);
138 template <bool big_endian
>
140 sized_read_typeunit_index(unsigned int, Dwp_output_file
*);
142 // Merge the input string table section into the output file.
144 add_strings(Dwp_output_file
*, unsigned int);
146 // Copy a section from the input file to the output file.
148 copy_section(Dwp_output_file
* output_file
, unsigned int shndx
,
149 const char* section_name
, bool is_str_offsets
);
151 // Remap the string offsets in the .debug_str_offsets.dwo section.
153 remap_str_offsets(const unsigned char* contents
, section_size_type len
);
155 template <bool big_endian
>
157 sized_remap_str_offsets(const unsigned char* contents
, section_size_type len
);
159 // Remap a single string offsets from an offset in the input string table
160 // to an offset in the output string table.
162 remap_str_offset(unsigned int val
);
164 // Add a set of .debug_info and related sections to OUTPUT_FILE.
166 add_cu_set(Dwp_output_file
* output_file
,
168 unsigned int debug_info
,
169 unsigned int debug_abbrev
,
170 unsigned int debug_line
,
171 unsigned int debug_loc
,
172 unsigned int debug_str_offsets
,
173 unsigned int debug_macinfo
,
174 unsigned int debug_macro
);
176 // Add a set of .debug_types and related sections to OUTPUT_FILE.
178 add_tu_set(Dwp_output_file
* output_file
,
180 unsigned int debug_types
,
181 unsigned int debug_abbrev
,
182 unsigned int debug_line
,
183 unsigned int debug_str_offsets
);
187 // The ELF file, represented as a gold Relobj instance.
189 // The Input_file object.
190 Input_file
* input_file_
;
191 // Flags indicating which sections are compressed.
192 std::vector
<bool> is_compressed_
;
193 // Map input section index onto output section index.
194 std::vector
<unsigned int> shndx_map_
;
195 // Map input string offsets to output string offsets.
196 Str_offset_map str_offset_map_
;
199 // An ELF input file.
200 // We derive from Sized_relobj so that we can use interfaces
201 // in libgold to access the file.
203 template <int size
, bool big_endian
>
204 class Sized_relobj_dwo
: public Sized_relobj
<size
, big_endian
>
207 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
208 typedef typename Sized_relobj
<size
, big_endian
>::Symbols Symbols
;
210 Sized_relobj_dwo(const char* name
, Input_file
* input_file
,
211 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
212 : Sized_relobj
<size
, big_endian
>(name
, input_file
),
213 elf_file_(this, ehdr
)
219 // Setup the section information.
224 // Return section type.
226 do_section_type(unsigned int shndx
)
227 { return this->elf_file_
.section_type(shndx
); }
229 // Get the name of a section.
231 do_section_name(unsigned int shndx
)
232 { return this->elf_file_
.section_name(shndx
); }
234 // Get the size of a section.
236 do_section_size(unsigned int shndx
)
237 { return this->elf_file_
.section_size(shndx
); }
239 // Return a view of the contents of a section.
241 do_section_contents(unsigned int, section_size_type
*, bool);
243 // Return a view of the uncompressed contents of a section. Set *PLEN
244 // to the size. Set *IS_NEW to true if the contents need to be deleted
247 do_decompressed_section_contents(unsigned int shndx
,
248 section_size_type
* plen
,
251 // The following virtual functions are abstract in the base classes,
252 // but are not used here.
256 do_read_symbols(Read_symbols_data
*)
257 { gold_unreachable(); }
259 // Lay out the input sections.
261 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*)
262 { gold_unreachable(); }
264 // Layout sections whose layout was deferred while waiting for
265 // input files from a plugin.
267 do_layout_deferred_sections(Layout
*)
268 { gold_unreachable(); }
270 // Add the symbols to the symbol table.
272 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*)
273 { gold_unreachable(); }
275 Archive::Should_include
276 do_should_include_member(Symbol_table
*, Layout
*, Read_symbols_data
*,
278 { gold_unreachable(); }
280 // Iterate over global symbols, calling a visitor class V for each.
282 do_for_all_global_symbols(Read_symbols_data
*,
283 Library_base::Symbol_visitor_base
*)
284 { gold_unreachable(); }
286 // Return section flags.
288 do_section_flags(unsigned int)
289 { gold_unreachable(); }
291 // Return section entsize.
293 do_section_entsize(unsigned int)
294 { gold_unreachable(); }
296 // Return section address.
298 do_section_address(unsigned int)
299 { gold_unreachable(); }
301 // Return the section link field.
303 do_section_link(unsigned int)
304 { gold_unreachable(); }
306 // Return the section link field.
308 do_section_info(unsigned int)
309 { gold_unreachable(); }
311 // Return the section alignment.
313 do_section_addralign(unsigned int)
314 { gold_unreachable(); }
316 // Return the Xindex structure to use.
318 do_initialize_xindex()
319 { gold_unreachable(); }
321 // Get symbol counts.
323 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const
324 { gold_unreachable(); }
326 // Get global symbols.
328 do_get_global_symbols() const
331 // Return the value of a local symbol.
333 do_local_symbol_value(unsigned int, uint64_t) const
334 { gold_unreachable(); }
337 do_local_plt_offset(unsigned int) const
338 { gold_unreachable(); }
340 // Return whether local symbol SYMNDX is a TLS symbol.
342 do_local_is_tls(unsigned int) const
343 { gold_unreachable(); }
345 // Return the number of local symbols.
347 do_local_symbol_count() const
348 { gold_unreachable(); }
350 // Return the number of local symbols in the output symbol table.
352 do_output_local_symbol_count() const
353 { gold_unreachable(); }
355 // Return the file offset for local symbols in the output symbol table.
357 do_local_symbol_offset() const
358 { gold_unreachable(); }
362 do_read_relocs(Read_relocs_data
*)
363 { gold_unreachable(); }
365 // Process the relocs to find list of referenced sections. Used only
366 // during garbage collection.
368 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*)
369 { gold_unreachable(); }
371 // Scan the relocs and adjust the symbol table.
373 do_scan_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*)
374 { gold_unreachable(); }
376 // Count the local symbols.
378 do_count_local_symbols(Stringpool_template
<char>*,
379 Stringpool_template
<char>*)
380 { gold_unreachable(); }
382 // Finalize the local symbols.
384 do_finalize_local_symbols(unsigned int, off_t
, Symbol_table
*)
385 { gold_unreachable(); }
387 // Set the offset where local dynamic symbol information will be stored.
389 do_set_local_dynsym_indexes(unsigned int)
390 { gold_unreachable(); }
392 // Set the offset where local dynamic symbol information will be stored.
394 do_set_local_dynsym_offset(off_t
)
395 { gold_unreachable(); }
397 // Relocate the input sections and write out the local symbols.
399 do_relocate(const Symbol_table
*, const Layout
*, Output_file
*)
400 { gold_unreachable(); }
403 // General access to the ELF file.
404 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
408 // This class is responsible for collecting the debug index information
409 // and writing the .dwp file in ELF format.
411 class Dwp_output_file
414 Dwp_output_file(const char* name
)
415 : name_(name
), machine_(0), size_(0), big_endian_(false), osabi_(0),
416 abiversion_(0), fd_(NULL
), next_file_offset_(0), shnum_(1), sections_(),
417 shoff_(0), shstrndx_(0), have_strings_(false), stringpool_(),
418 shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
421 this->stringpool_
.set_no_zero_null();
424 // Record the target info from an input file.
426 record_target_info(const char* name
, int machine
, int size
, bool big_endian
,
427 int osabi
, int abiversion
);
429 // Add a string to the debug strings section.
431 add_string(const char* str
, size_t len
);
433 // Add a section to the output file, and return the new section index.
435 add_section(const char* section_name
, const unsigned char* contents
,
436 section_size_type len
, int align
);
438 // Add a set of .debug_info and related sections to the output file.
440 add_cu_set(uint64_t dwo_id
, unsigned int debug_info
,
441 unsigned int debug_abbrev
, unsigned int debug_line
,
442 unsigned int debug_loc
, unsigned int debug_str_offsets
,
443 unsigned int debug_macinfo
, unsigned int debug_macro
);
445 // Lookup a type signature and return TRUE if we have already seen it.
447 lookup_tu(uint64_t type_sig
);
449 // Add a set of .debug_types and related sections to the output file.
451 add_tu_set(uint64_t type_sig
, unsigned int debug_types
,
452 unsigned int debug_abbrev
, unsigned int debug_line
,
453 unsigned int debug_str_offsets
);
455 // Finalize the file, write the string tables and index sections,
456 // and close the file.
461 // Sections in the output file.
466 section_size_type size
;
470 // A set of sections for a compilation unit or type unit.
474 unsigned int debug_info_or_types
;
475 unsigned int debug_abbrev
;
476 unsigned int debug_line
;
477 unsigned int debug_loc
;
478 unsigned int debug_str_offsets
;
479 unsigned int debug_macinfo
;
480 unsigned int debug_macro
;
483 // The index sections defined by the DWARF Package File Format spec.
487 // Vector for the section index pool.
488 typedef std::vector
<unsigned int> Shndx_pool
;
491 : capacity_(0), used_(0), hash_table_(NULL
), shndx_pool_()
497 // Find a slot in the hash table for SIGNATURE. Return TRUE
498 // if the entry already exists.
500 find_or_add(uint64_t signature
, unsigned int* slotp
);
502 // Enter a CU or TU set at the given SLOT in the hash table.
504 enter_set(unsigned int slot
, const Cu_or_tu_set
& set
);
506 // Return the contents of the given SLOT in the hash table of signatures.
508 hash_table(unsigned int slot
) const
509 { return this->hash_table_
[slot
]; }
511 // Return the contents of the given SLOT in the parallel table of
512 // shndx pool indexes.
514 index_table(unsigned int slot
) const
515 { return this->index_table_
[slot
]; }
517 // Return the total number of slots in the hash table.
519 hash_table_total_slots() const
520 { return this->capacity_
; }
522 // Return the number of used slots in the hash table.
524 hash_table_used_slots() const
525 { return this->used_
; }
527 // Return an iterator into the shndx pool.
528 Shndx_pool::const_iterator
530 { return this->shndx_pool_
.begin(); }
532 Shndx_pool::const_iterator
533 shndx_pool_end() const
534 { return this->shndx_pool_
.end(); }
536 // Return the number of entries in the shndx pool.
538 shndx_pool_size() const
539 { return this->shndx_pool_
.size(); }
542 // Initialize the hash table.
546 // Grow the hash table when we reach 2/3 capacity.
550 // The number of slots in the table, a power of 2 such that
551 // capacity > 3 * size / 2.
552 unsigned int capacity_
;
553 // The current number of used slots in the hash table.
555 // The storage for the hash table of signatures.
556 uint64_t* hash_table_
;
557 // The storage for the parallel table of shndx pool indexes.
558 uint32_t* index_table_
;
559 // The pool of section indexes.
560 Shndx_pool shndx_pool_
;
561 }; // End class Dwp_output_file::Dwp_index.
563 // Initialize the output file.
567 // Write the ELF header.
571 template<unsigned int size
, bool big_endian
>
575 // Write a section header.
577 write_shdr(const char* name
, unsigned int type
, unsigned int flags
,
578 uint64_t addr
, off_t offset
, section_size_type sect_size
,
579 unsigned int link
, unsigned int info
,
580 unsigned int align
, unsigned int ent_size
);
582 template<unsigned int size
, bool big_endian
>
584 sized_write_shdr(const char* name
, unsigned int type
, unsigned int flags
,
585 uint64_t addr
, off_t offset
, section_size_type sect_size
,
586 unsigned int link
, unsigned int info
,
587 unsigned int align
, unsigned int ent_size
);
589 // Write a CU or TU index section.
590 template<bool big_endian
>
592 write_index(const char* sect_name
, const Dwp_index
& index
);
594 // The output filename.
596 // ELF header parameters.
602 // The output file descriptor.
604 // Next available file offset.
605 off_t next_file_offset_
;
606 // The number of sections.
608 // Section table. The first entry is shndx 1.
609 std::vector
<Section
> sections_
;
610 // File offset of the section header table.
612 // Section index of the section string table.
613 unsigned int shstrndx_
;
614 // TRUE if we have added any strings to the string pool.
616 // String pool for the output .debug_str.dwo section.
617 Stringpool stringpool_
;
618 // String pool for the .shstrtab section.
619 Stringpool shstrtab_
;
620 // The compilation unit index.
622 // The type unit index.
624 // Cache of the last type signature looked up.
625 uint64_t last_type_sig_
;
626 // Cache of the slot index for the last type signature.
627 unsigned int last_tu_slot_
;
630 // A specialization of Dwarf_info_reader, for reading dwo_ids and
631 // type signatures from DWARF CUs and TUs.
633 class Dwo_id_info_reader
: public Dwarf_info_reader
636 Dwo_id_info_reader(bool is_type_unit
,
638 const unsigned char* symbols
,
641 unsigned int reloc_shndx
,
642 unsigned int reloc_type
)
643 : Dwarf_info_reader(is_type_unit
, object
, symbols
, symbols_size
, shndx
,
644 reloc_shndx
, reloc_type
),
645 dwo_id_found_(false), dwo_id_(0), type_sig_found_(false), type_sig_(0)
648 ~Dwo_id_info_reader()
651 // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID.
653 get_dwo_id(uint64_t* dwo_id
)
656 if (!this->dwo_id_found_
)
658 *dwo_id
= this->dwo_id_
;
662 // Return the type signature from a DWARF type unit DIE in *TYPE_SIG.
664 get_type_sig(uint64_t* type_sig
)
667 if (!this->type_sig_found_
)
669 *type_sig
= this->type_sig_
;
674 // Visit a compilation unit.
676 visit_compilation_unit(off_t cu_offset
, off_t cu_length
, Dwarf_die
*);
678 // Visit a type unit.
680 visit_type_unit(off_t tu_offset
, off_t type_offset
, uint64_t signature
,
684 // Visit a top-level DIE.
686 visit_top_die(Dwarf_die
* die
);
688 // TRUE if we found a dwo_id.
692 // TRUE if we found a type signature.
693 bool type_sig_found_
;
694 // The type signature.
698 // Class Sized_relobj_dwo.
700 // Setup the section information.
702 template <int size
, bool big_endian
>
704 Sized_relobj_dwo
<size
, big_endian
>::setup()
706 const unsigned int shnum
= this->elf_file_
.shnum();
707 this->set_shnum(shnum
);
708 this->section_offsets().resize(shnum
);
711 // Return a view of the contents of a section.
713 template <int size
, bool big_endian
>
715 Sized_relobj_dwo
<size
, big_endian
>::do_section_contents(
717 section_size_type
* plen
,
720 Object::Location
loc(this->elf_file_
.section_contents(shndx
));
721 *plen
= convert_to_section_size_type(loc
.data_size
);
724 static const unsigned char empty
[1] = { '\0' };
727 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
730 // Return a view of the uncompressed contents of a section. Set *PLEN
731 // to the size. Set *IS_NEW to true if the contents need to be deleted
734 template <int size
, bool big_endian
>
736 Sized_relobj_dwo
<size
, big_endian
>::do_decompressed_section_contents(
738 section_size_type
* plen
,
741 section_size_type buffer_size
;
742 const unsigned char* buffer
= this->do_section_contents(shndx
, &buffer_size
,
745 std::string sect_name
= this->do_section_name(shndx
);
746 if (!is_prefix_of(".zdebug_", sect_name
.c_str()))
753 section_size_type uncompressed_size
= get_uncompressed_size(buffer
,
755 unsigned char* uncompressed_data
= new unsigned char[uncompressed_size
];
756 if (!decompress_input_section(buffer
,
760 this->error(_("could not decompress section %s"),
761 this->section_name(shndx
).c_str());
762 *plen
= uncompressed_size
;
764 return uncompressed_data
;
769 Dwo_file::~Dwo_file()
771 if (this->input_file_
!= NULL
)
772 delete this->input_file_
;
773 if (this->obj_
!= NULL
)
777 // Read the input file and send its contents to OUTPUT_FILE.
780 Dwo_file::read(Dwp_output_file
* output_file
)
782 // Open the input file.
783 this->input_file_
= new Input_file(this->name_
);
786 if (!this->input_file_
->open(dirpath
, NULL
, &index
))
787 gold_fatal(_("%s: can't open"), this->name_
);
789 // Check that it's an ELF file.
790 off_t filesize
= this->input_file_
->file().filesize();
791 int hdrsize
= elfcpp::Elf_recognizer::max_header_size
;
792 if (filesize
< hdrsize
)
794 const unsigned char* p
=
795 this->input_file_
->file().get_view(0, 0, hdrsize
, true, false);
796 if (!elfcpp::Elf_recognizer::is_elf_file(p
, hdrsize
))
797 gold_fatal(_("%s: not an ELF object file"), this->name_
);
799 // Get the size, endianness, machine, etc. info from the header,
800 // make an appropriately-sized Relobj, and pass the target info
801 // to the output object.
805 if (!elfcpp::Elf_recognizer::is_valid_header(p
, hdrsize
, &size
,
806 &big_endian
, &error
))
807 gold_fatal(_("%s: %s"), this->name_
, error
.c_str());
809 this->obj_
= this->make_object(size
, big_endian
, p
, this->input_file_
,
812 unsigned int shnum
= this->shnum();
813 this->is_compressed_
.resize(shnum
);
814 this->shndx_map_
.resize(shnum
);
816 typedef std::vector
<unsigned int> Types_list
;
817 Types_list debug_types
;
818 unsigned int debug_info
= 0;
819 unsigned int debug_abbrev
= 0;
820 unsigned int debug_line
= 0;
821 unsigned int debug_loc
= 0;
822 unsigned int debug_str
= 0;
823 unsigned int debug_str_offsets
= 0;
824 unsigned int debug_macinfo
= 0;
825 unsigned int debug_macro
= 0;
826 unsigned int debug_cu_index
= 0;
827 unsigned int debug_tu_index
= 0;
829 // Scan the section table and look for .dwp index sections.
830 // (Section index 0 is a dummy section; skip it.)
831 for (unsigned int i
= 1; i
< shnum
; i
++)
833 if (this->section_type(i
) != elfcpp::SHT_PROGBITS
)
835 std::string sect_name
= this->section_name(i
);
836 const char* suffix
= sect_name
.c_str();
837 if (is_prefix_of(".debug_", suffix
))
839 else if (is_prefix_of(".zdebug_", suffix
))
841 this->is_compressed_
[i
] = true;
846 if (strcmp(suffix
, "cu_index") == 0)
848 else if (strcmp(suffix
, "tu_index") == 0)
850 else if (strcmp(suffix
, "str.dwo") == 0)
854 // Merge the input string table into the output string table.
855 this->add_strings(output_file
, debug_str
);
857 // If we found any .dwp index sections, read those and add the section
858 // sets to the output file.
859 if (debug_cu_index
> 0 || debug_tu_index
> 0)
861 if (debug_cu_index
> 0)
862 this->read_compunit_index(debug_cu_index
, output_file
);
863 if (debug_tu_index
> 0)
864 this->read_typeunit_index(debug_tu_index
, output_file
);
868 // If we found no index sections, this is a .dwo file.
869 // Scan the section table and collect the debug sections.
870 for (unsigned int i
= 1; i
< shnum
; i
++)
872 if (this->section_type(i
) != elfcpp::SHT_PROGBITS
)
874 std::string sect_name
= this->section_name(i
);
875 const char* suffix
= sect_name
.c_str();
876 if (is_prefix_of(".debug_", suffix
))
878 else if (is_prefix_of(".zdebug_", suffix
))
882 // TODO: Check for one of each section (except .debug_types).
883 if (strcmp(suffix
, "info.dwo") == 0)
885 else if (strcmp(suffix
, "types.dwo") == 0)
886 debug_types
.push_back(i
);
887 else if (strcmp(suffix
, "abbrev.dwo") == 0)
889 else if (strcmp(suffix
, "line.dwo") == 0)
891 else if (strcmp(suffix
, "loc.dwo") == 0)
893 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
894 debug_str_offsets
= i
;
895 else if (strcmp(suffix
, "macinfo.dwo") == 0)
897 else if (strcmp(suffix
, "macro.dwo") == 0)
903 // Extract the dwo_id from .debug_info.dwo section.
905 Dwo_id_info_reader
dwarf_reader(false, this->obj_
, NULL
, 0, debug_info
,
907 dwarf_reader
.set_abbrev_shndx(debug_abbrev
);
908 if (!dwarf_reader
.get_dwo_id(&dwo_id
))
909 gold_fatal(_("%s: .debug_info.dwo section does not have DW_AT_GNU_dwo_id "
910 "attribute"), this->name_
);
911 this->add_cu_set(output_file
, dwo_id
, debug_info
, debug_abbrev
,
912 debug_line
, debug_loc
, debug_str_offsets
,
913 debug_macinfo
, debug_macro
);
916 for (Types_list::const_iterator tp
= debug_types
.begin();
917 tp
!= debug_types
.end();
920 // Extract the type signature from .debug_types.dwo section.
922 gold_assert(*tp
> 0);
923 Dwo_id_info_reader
dwarf_reader(true, this->obj_
, NULL
, 0, *tp
, 0, 0);
924 dwarf_reader
.set_abbrev_shndx(debug_abbrev
);
925 if (!dwarf_reader
.get_type_sig(&type_sig
))
926 gold_fatal(_("%s: .debug_types.dwo section does not have type signature"),
928 this->add_tu_set(output_file
, type_sig
, *tp
, debug_abbrev
, debug_line
,
933 // Create a Sized_relobj_dwo of the given size and endianness,
934 // and record the target info. P is a pointer to the ELF header
938 Dwo_file::make_object(int size
, bool big_endian
, const unsigned char* p
,
939 Input_file
* input_file
, Dwp_output_file
* output_file
)
944 #ifdef HAVE_TARGET_32_BIG
945 return this->sized_make_object
<32, true>(p
, input_file
, output_file
);
950 #ifdef HAVE_TARGET_32_LITTLE
951 return this->sized_make_object
<32, false>(p
, input_file
, output_file
);
959 #ifdef HAVE_TARGET_64_BIG
960 return this->sized_make_object
<64, true>(p
, input_file
, output_file
);
965 #ifdef HAVE_TARGET_64_LITTLE
966 return this->sized_make_object
<64, false>(p
, input_file
, output_file
);
975 // Function template to create a Sized_relobj_dwo and record the target info.
976 // P is a pointer to the ELF header in memory.
978 template <int size
, bool big_endian
>
980 Dwo_file::sized_make_object(const unsigned char* p
, Input_file
* input_file
,
981 Dwp_output_file
* output_file
)
983 elfcpp::Ehdr
<size
, big_endian
> ehdr(p
);
984 Sized_relobj_dwo
<size
, big_endian
>* obj
=
985 new Sized_relobj_dwo
<size
, big_endian
>(this->name_
, input_file
, ehdr
);
987 output_file
->record_target_info(
988 this->name_
, ehdr
.get_e_machine(), size
, big_endian
,
989 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
990 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
994 // Read the .debug_cu_index section of a .dwp file,
995 // and process the CU sets.
998 Dwo_file::read_compunit_index(unsigned int shndx
, Dwp_output_file
* output_file
)
1000 if (this->obj_
->is_big_endian())
1001 this->sized_read_compunit_index
<true>(shndx
, output_file
);
1003 this->sized_read_compunit_index
<false>(shndx
, output_file
);
1006 template <bool big_endian
>
1008 Dwo_file::sized_read_compunit_index(unsigned int shndx
,
1009 Dwp_output_file
* output_file
)
1011 section_size_type len
;
1013 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1015 unsigned int version
=
1016 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
);
1018 gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"),
1019 this->name_
, version
);
1021 unsigned int nused
=
1022 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1023 + 2 * sizeof(uint32_t));
1027 unsigned int nslots
=
1028 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1029 + 3 * sizeof(uint32_t));
1031 const unsigned char* phash
= contents
+ 4 * sizeof(uint32_t);
1032 const unsigned char* pindex
= phash
+ nslots
* sizeof(uint64_t);
1033 const unsigned char* shndx_pool
= pindex
+ nslots
* sizeof(uint32_t);
1034 const unsigned char* limit
= contents
+ len
;
1036 if (shndx_pool
>= limit
)
1037 gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_
);
1039 // Loop over the slots of the hash table.
1040 for (unsigned int i
= 0; i
< nslots
; ++i
)
1043 elfcpp::Swap_unaligned
<64, big_endian
>::readval(phash
);
1046 unsigned int index
=
1047 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pindex
);
1048 const unsigned char* shndx_list
=
1049 shndx_pool
+ index
* sizeof(uint32_t);
1051 // Collect the debug sections for this compilation unit set.
1052 unsigned int debug_info
= 0;
1053 unsigned int debug_abbrev
= 0;
1054 unsigned int debug_line
= 0;
1055 unsigned int debug_loc
= 0;
1056 unsigned int debug_str_offsets
= 0;
1057 unsigned int debug_macinfo
= 0;
1058 unsigned int debug_macro
= 0;
1061 if (shndx_list
>= limit
)
1062 gold_fatal(_("%s: .debug_cu_index is corrupt"),
1064 unsigned int shndx
=
1065 elfcpp::Swap_unaligned
<32, big_endian
>::readval(shndx_list
);
1068 if (shndx
> this->shnum())
1069 gold_fatal(_("%s: .debug_cu_index has bad shndx"),
1071 std::string sect_name
= this->section_name(shndx
);
1072 const char* suffix
= sect_name
.c_str();
1073 if (is_prefix_of(".debug_", suffix
))
1075 else if (is_prefix_of(".zdebug_", suffix
))
1078 gold_fatal(_("%s: .debug_cu_index refers to "
1079 "non-debug section"), this->name_
);
1080 if (strcmp(suffix
, "info.dwo") == 0)
1082 else if (strcmp(suffix
, "abbrev.dwo") == 0)
1083 debug_abbrev
= shndx
;
1084 else if (strcmp(suffix
, "line.dwo") == 0)
1086 else if (strcmp(suffix
, "loc.dwo") == 0)
1088 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
1089 debug_str_offsets
= shndx
;
1090 else if (strcmp(suffix
, "macinfo.dwo") == 0)
1091 debug_macinfo
= shndx
;
1092 else if (strcmp(suffix
, "macro.dwo") == 0)
1093 debug_macro
= shndx
;
1094 shndx_list
+= sizeof(uint32_t);
1096 this->add_cu_set(output_file
, dwo_id
, debug_info
, debug_abbrev
,
1097 debug_line
, debug_loc
, debug_str_offsets
,
1098 debug_macinfo
, debug_macro
);
1100 phash
+= sizeof(uint64_t);
1101 pindex
+= sizeof(uint32_t);
1108 // Read the .debug_tu_index section of a .dwp file,
1109 // and process the TU sets.
1112 Dwo_file::read_typeunit_index(unsigned int shndx
, Dwp_output_file
* output_file
)
1114 if (this->obj_
->is_big_endian())
1115 this->sized_read_typeunit_index
<true>(shndx
, output_file
);
1117 this->sized_read_typeunit_index
<false>(shndx
, output_file
);
1120 template <bool big_endian
>
1122 Dwo_file::sized_read_typeunit_index(unsigned int shndx
,
1123 Dwp_output_file
* output_file
)
1125 section_size_type len
;
1127 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1129 unsigned int version
=
1130 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
);
1132 gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"),
1133 this->name_
, version
);
1135 unsigned int nused
=
1136 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1137 + 2 * sizeof(uint32_t));
1141 unsigned int nslots
=
1142 elfcpp::Swap_unaligned
<32, big_endian
>::readval(contents
1143 + 3 * sizeof(uint32_t));
1145 const unsigned char* phash
= contents
+ 4 * sizeof(uint32_t);
1146 const unsigned char* pindex
= phash
+ nslots
* sizeof(uint64_t);
1147 const unsigned char* shndx_pool
= pindex
+ nslots
* sizeof(uint32_t);
1148 const unsigned char* limit
= contents
+ len
;
1150 if (shndx_pool
>= limit
)
1151 gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_
);
1153 // Loop over the slots of the hash table.
1154 for (unsigned int i
= 0; i
< nslots
; ++i
)
1157 elfcpp::Swap_unaligned
<64, big_endian
>::readval(phash
);
1160 unsigned int index
=
1161 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pindex
);
1162 const unsigned char* shndx_list
=
1163 shndx_pool
+ index
* sizeof(uint32_t);
1165 // Collect the debug sections for this type unit set.
1166 unsigned int debug_types
= 0;
1167 unsigned int debug_abbrev
= 0;
1168 unsigned int debug_line
= 0;
1169 unsigned int debug_str_offsets
= 0;
1172 if (shndx_list
>= limit
)
1173 gold_fatal(_("%s: .debug_tu_index is corrupt"),
1175 unsigned int shndx
=
1176 elfcpp::Swap_unaligned
<32, big_endian
>::readval(shndx_list
);
1179 if (shndx
> this->shnum())
1180 gold_fatal(_("%s: .debug_tu_index has bad shndx"),
1182 std::string sect_name
= this->section_name(shndx
);
1183 const char* suffix
= sect_name
.c_str();
1184 if (is_prefix_of(".debug_", suffix
))
1186 else if (is_prefix_of(".zdebug_", suffix
))
1189 gold_fatal(_("%s: .debug_tu_index refers to "
1190 "non-debug section"), this->name_
);
1191 if (strcmp(suffix
, "types.dwo") == 0)
1192 debug_types
= shndx
;
1193 else if (strcmp(suffix
, "abbrev.dwo") == 0)
1194 debug_abbrev
= shndx
;
1195 else if (strcmp(suffix
, "line.dwo") == 0)
1197 else if (strcmp(suffix
, "str_offsets.dwo") == 0)
1198 debug_str_offsets
= shndx
;
1199 shndx_list
+= sizeof(uint32_t);
1201 this->add_tu_set(output_file
, type_sig
, debug_types
, debug_abbrev
,
1202 debug_line
, debug_str_offsets
);
1204 phash
+= sizeof(uint64_t);
1205 pindex
+= sizeof(uint32_t);
1212 // Merge the input string table section into the output file.
1215 Dwo_file::add_strings(Dwp_output_file
* output_file
, unsigned int debug_str
)
1217 section_size_type len
;
1219 const unsigned char* pdata
= this->section_contents(debug_str
, &len
, &is_new
);
1220 const char* p
= reinterpret_cast<const char*>(pdata
);
1221 const char* pend
= p
+ len
;
1223 // Check that the last string is null terminated.
1224 if (pend
[-1] != '\0')
1225 gold_fatal(_("%s: last entry in string section '%s' "
1226 "is not null terminated"),
1228 this->section_name(debug_str
).c_str());
1230 // Count the number of strings in the section, and size the map.
1232 for (const char* pt
= p
; pt
< pend
; pt
+= strlen(pt
) + 1)
1234 this->str_offset_map_
.reserve(count
+ 1);
1236 // Add the strings to the output string table, and record the new offsets
1238 section_offset_type i
= 0;
1239 section_offset_type new_offset
;
1242 size_t len
= strlen(p
);
1243 new_offset
= output_file
->add_string(p
, len
);
1244 this->str_offset_map_
.push_back(std::make_pair(i
, new_offset
));
1249 this->str_offset_map_
.push_back(std::make_pair(i
, new_offset
));
1254 // Copy a section from the input file to the output file.
1255 // If IS_STR_OFFSETS is true, remap the string offsets for the
1256 // output string table.
1259 Dwo_file::copy_section(Dwp_output_file
* output_file
, unsigned int shndx
,
1260 const char* section_name
, bool is_str_offsets
)
1262 // Some sections may be referenced from more than one set.
1263 // Don't copy a section more than once.
1264 if (this->shndx_map_
[shndx
] > 0)
1265 return this->shndx_map_
[shndx
];
1267 section_size_type len
;
1269 const unsigned char* contents
= this->section_contents(shndx
, &len
, &is_new
);
1273 const unsigned char* remapped
= this->remap_str_offsets(contents
, len
);
1276 contents
= remapped
;
1280 this->shndx_map_
[shndx
] = output_file
->add_section(section_name
, contents
,
1285 return this->shndx_map_
[shndx
];
1289 const unsigned char*
1290 Dwo_file::remap_str_offsets(const unsigned char* contents
,
1291 section_size_type len
)
1294 gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"),
1297 if (this->obj_
->is_big_endian())
1298 return this->sized_remap_str_offsets
<true>(contents
, len
);
1300 return this->sized_remap_str_offsets
<false>(contents
, len
);
1303 template <bool big_endian
>
1304 const unsigned char*
1305 Dwo_file::sized_remap_str_offsets(const unsigned char* contents
,
1306 section_size_type len
)
1308 unsigned char* remapped
= new unsigned char[len
];
1309 const unsigned char* p
= contents
;
1310 unsigned char* q
= remapped
;
1313 unsigned int val
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(p
);
1314 val
= this->remap_str_offset(val
);
1315 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(q
, val
);
1324 Dwo_file::remap_str_offset(unsigned int val
)
1326 Str_offset_map_entry entry
;
1329 Str_offset_map::const_iterator p
=
1330 std::lower_bound(this->str_offset_map_
.begin(),
1331 this->str_offset_map_
.end(),
1332 entry
, Offset_compare());
1334 if (p
== this->str_offset_map_
.end() || p
->first
> val
)
1336 if (p
== this->str_offset_map_
.begin())
1339 gold_assert(p
->first
<= val
);
1342 return p
->second
+ (val
- p
->first
);
1345 // Add a set of .debug_info and related sections to OUTPUT_FILE.
1348 Dwo_file::add_cu_set(Dwp_output_file
* output_file
,
1350 unsigned int debug_info
,
1351 unsigned int debug_abbrev
,
1352 unsigned int debug_line
,
1353 unsigned int debug_loc
,
1354 unsigned int debug_str_offsets
,
1355 unsigned int debug_macinfo
,
1356 unsigned int debug_macro
)
1358 if (debug_info
== 0)
1359 gold_fatal(_("%s: no .debug_info.dwo section found"), this->name_
);
1360 if (debug_abbrev
== 0)
1361 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_
);
1363 debug_abbrev
= this->copy_section(output_file
, debug_abbrev
,
1364 ".debug_abbrev.dwo", false);
1366 debug_line
= this->copy_section(output_file
, debug_line
,
1367 ".debug_line.dwo", false);
1369 debug_loc
= this->copy_section(output_file
, debug_loc
, ".debug_loc.dwo",
1371 if (debug_macinfo
> 0)
1372 debug_macinfo
= this->copy_section(output_file
, debug_macinfo
,
1373 ".debug_macinfo.dwo", false);
1374 if (debug_macro
> 0)
1375 debug_macro
= this->copy_section(output_file
, debug_macro
,
1376 ".debug_macro.dwo", false);
1378 if (debug_str_offsets
> 0)
1379 debug_str_offsets
= this->copy_section(output_file
, debug_str_offsets
,
1380 ".debug_str_offsets.dwo", true);
1382 debug_info
= this->copy_section(output_file
, debug_info
, ".debug_info.dwo",
1385 output_file
->add_cu_set(dwo_id
, debug_info
, debug_abbrev
, debug_line
,
1386 debug_loc
, debug_str_offsets
, debug_macinfo
,
1390 // Add a set of .debug_types and related sections to OUTPUT_FILE.
1393 Dwo_file::add_tu_set(Dwp_output_file
* output_file
,
1395 unsigned int debug_types
,
1396 unsigned int debug_abbrev
,
1397 unsigned int debug_line
,
1398 unsigned int debug_str_offsets
)
1400 if (debug_types
== 0)
1401 gold_fatal(_("%s: no .debug_types.dwo section found"), this->name_
);
1402 if (debug_abbrev
== 0)
1403 gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_
);
1405 // Ignore duplicate type signatures.
1406 if (output_file
->lookup_tu(type_sig
))
1409 debug_abbrev
= this->copy_section(output_file
, debug_abbrev
,
1410 ".debug_abbrev.dwo", false);
1412 debug_line
= this->copy_section(output_file
, debug_line
,
1413 ".debug_line.dwo", false);
1415 if (debug_str_offsets
> 0)
1416 debug_str_offsets
= this->copy_section(output_file
, debug_str_offsets
,
1417 ".debug_str_offsets.dwo", true);
1419 debug_types
= this->copy_section(output_file
, debug_types
,
1420 ".debug_types.dwo", false);
1422 output_file
->add_tu_set(type_sig
, debug_types
, debug_abbrev
, debug_line
,
1426 // Class Dwp_output_file.
1428 // Record the target info from an input file. On first call, we
1429 // set the ELF header values for the output file. On subsequent
1430 // calls, we just verify that the values match.
1433 Dwp_output_file::record_target_info(const char*, int machine
,
1434 int size
, bool big_endian
,
1435 int osabi
, int abiversion
)
1437 // TODO: Check the values on subsequent calls.
1438 if (this->size_
> 0)
1441 this->machine_
= machine
;
1443 this->big_endian_
= big_endian
;
1444 this->osabi_
= osabi
;
1445 this->abiversion_
= abiversion
;
1448 this->next_file_offset_
= elfcpp::Elf_sizes
<32>::ehdr_size
;
1449 else if (size
== 64)
1450 this->next_file_offset_
= elfcpp::Elf_sizes
<64>::ehdr_size
;
1454 this->fd_
= ::fopen(this->name_
, "wb");
1455 if (this->fd_
== NULL
)
1456 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
1458 // Write zeroes for the ELF header initially. We'll write
1459 // the actual header during finalize().
1460 static const char buf
[elfcpp::Elf_sizes
<64>::ehdr_size
] = { 0 };
1461 ::fwrite(buf
, 1, this->next_file_offset_
, this->fd_
);
1464 // Add a string to the debug strings section.
1467 Dwp_output_file::add_string(const char* str
, size_t len
)
1469 Stringpool::Key key
;
1470 this->stringpool_
.add_with_length(str
, len
, true, &key
);
1471 this->have_strings_
= true;
1472 // We aren't supposed to call get_offset() until after
1473 // calling set_string_offsets(), but the offsets will
1474 // not change unless optimizing the string pool.
1475 return this->stringpool_
.get_offset_from_key(key
);
1478 // Align the file offset to the given boundary.
1481 align_offset(off_t off
, int align
)
1483 return (off
+ align
- 1) & ~(align
- 1);
1486 // Add a section to the output file, and return the new section index.
1489 Dwp_output_file::add_section(const char* section_name
,
1490 const unsigned char* contents
,
1491 section_size_type len
,
1494 off_t file_offset
= this->next_file_offset_
;
1495 gold_assert(this->size_
> 0 && file_offset
> 0);
1497 file_offset
= align_offset(file_offset
, align
);
1499 ::fseek(this->fd_
, file_offset
, SEEK_SET
);
1500 if (::fwrite(contents
, 1, len
, this->fd_
) < len
)
1501 gold_fatal(_("%s: error writing section '%s'"), this->name_
, section_name
);
1503 section_name
= this->shstrtab_
.add_with_length(section_name
,
1504 strlen(section_name
),
1506 Section sect
= { section_name
, file_offset
, len
, align
};
1507 this->sections_
.push_back(sect
);
1509 this->next_file_offset_
= file_offset
+ len
;
1510 return this->shnum_
++;
1513 // Add a set of .debug_info and related sections to the output file.
1516 Dwp_output_file::add_cu_set(uint64_t dwo_id
,
1517 unsigned int debug_info
,
1518 unsigned int debug_abbrev
,
1519 unsigned int debug_line
,
1520 unsigned int debug_loc
,
1521 unsigned int debug_str_offsets
,
1522 unsigned int debug_macinfo
,
1523 unsigned int debug_macro
)
1525 Cu_or_tu_set cu_set
= { dwo_id
, debug_info
, debug_abbrev
, debug_line
,
1526 debug_loc
, debug_str_offsets
, debug_macinfo
,
1529 this->cu_index_
.find_or_add(dwo_id
, &slot
);
1530 this->cu_index_
.enter_set(slot
, cu_set
);
1533 // Lookup a type signature and return TRUE if we have already seen it.
1535 Dwp_output_file::lookup_tu(uint64_t type_sig
)
1537 this->last_type_sig_
= type_sig
;
1538 return this->tu_index_
.find_or_add(type_sig
, &this->last_tu_slot_
);
1541 // Add a set of .debug_types and related sections to the output file.
1544 Dwp_output_file::add_tu_set(uint64_t type_sig
,
1545 unsigned int debug_types
,
1546 unsigned int debug_abbrev
,
1547 unsigned int debug_line
,
1548 unsigned int debug_str_offsets
)
1550 Cu_or_tu_set tu_set
= { type_sig
, debug_types
, debug_abbrev
, debug_line
,
1551 0, debug_str_offsets
, 0, 0 };
1553 if (type_sig
== this->last_type_sig_
)
1554 slot
= this->last_tu_slot_
;
1556 this->tu_index_
.find_or_add(type_sig
, &slot
);
1557 this->tu_index_
.enter_set(slot
, tu_set
);
1560 // Find a slot in the hash table for SIGNATURE. Return TRUE
1561 // if the entry already exists.
1564 Dwp_output_file::Dwp_index::find_or_add(uint64_t signature
,
1565 unsigned int* slotp
)
1567 if (this->capacity_
== 0)
1570 static_cast<unsigned int>(signature
) & (this->capacity_
- 1);
1571 unsigned int secondary_hash
;
1572 uint64_t probe
= this->hash_table_
[slot
];
1573 if (probe
!= 0 && probe
!= signature
)
1575 secondary_hash
= (static_cast<unsigned int>(signature
>> 32)
1576 & (this->capacity_
- 1)) | 1;
1579 slot
= (slot
+ secondary_hash
) & (this->capacity_
- 1);
1580 probe
= this->hash_table_
[slot
];
1581 } while (probe
!= 0 && probe
!= signature
);
1584 return (probe
!= 0);
1587 // Enter a CU or TU set at the given SLOT in the hash table.
1590 Dwp_output_file::Dwp_index::enter_set(unsigned int slot
,
1591 const Cu_or_tu_set
& set
)
1593 gold_assert(slot
< this->capacity_
);
1594 gold_assert(set
.debug_info_or_types
> 0);
1595 gold_assert(set
.debug_abbrev
> 0);
1597 // Add the section indexes to the pool.
1598 uint32_t pool_index
= this->shndx_pool_
.size();
1599 this->shndx_pool_
.push_back(set
.debug_info_or_types
);
1600 this->shndx_pool_
.push_back(set
.debug_abbrev
);
1601 if (set
.debug_line
> 0)
1602 this->shndx_pool_
.push_back(set
.debug_line
);
1603 if (set
.debug_loc
> 0)
1604 this->shndx_pool_
.push_back(set
.debug_loc
);
1605 if (set
.debug_str_offsets
> 0)
1606 this->shndx_pool_
.push_back(set
.debug_str_offsets
);
1607 if (set
.debug_macinfo
> 0)
1608 this->shndx_pool_
.push_back(set
.debug_macinfo
);
1609 if (set
.debug_macro
> 0)
1610 this->shndx_pool_
.push_back(set
.debug_macro
);
1611 this->shndx_pool_
.push_back(0);
1613 // Enter the signature and pool index into the hash table.
1614 this->hash_table_
[slot
] = set
.signature
;
1615 this->index_table_
[slot
] = pool_index
;
1618 // Grow the hash table when we exceed 2/3 capacity.
1619 if (this->used_
* 3 > this->capacity_
* 2)
1623 // Initialize the hash table.
1626 Dwp_output_file::Dwp_index::initialize()
1628 this->capacity_
= 16;
1629 this->hash_table_
= new uint64_t[this->capacity_
];
1630 memset(this->hash_table_
, 0, this->capacity_
* sizeof(uint64_t));
1631 this->index_table_
= new uint32_t[this->capacity_
];
1632 memset(this->index_table_
, 0, this->capacity_
* sizeof(uint32_t));
1635 // Grow the hash table when we reach 2/3 capacity.
1638 Dwp_output_file::Dwp_index::grow()
1640 unsigned int old_capacity
= this->capacity_
;
1641 uint64_t* old_hash_table
= this->hash_table_
;
1642 uint32_t* old_index_table
= this->index_table_
;
1643 unsigned int old_used
= this->used_
;
1645 this->capacity_
= old_capacity
* 2;
1646 this->hash_table_
= new uint64_t[this->capacity_
];
1647 memset(this->hash_table_
, 0, this->capacity_
* sizeof(uint64_t));
1648 this->index_table_
= new uint32_t[this->capacity_
];
1649 memset(this->index_table_
, 0, this->capacity_
* sizeof(uint32_t));
1652 for (unsigned int i
= 0; i
< old_capacity
; ++i
)
1654 uint64_t signature
= old_hash_table
[i
];
1658 bool found
= this->find_or_add(signature
, &slot
);
1659 gold_assert(!found
);
1660 this->hash_table_
[slot
] = signature
;
1661 this->index_table_
[slot
] = old_index_table
[i
];
1665 gold_assert(this->used_
== old_used
);
1667 delete[] old_hash_table
;
1668 delete[] old_index_table
;
1671 // Initialize the output file.
1674 Dwp_output_file::initialize()
1676 // We can't initialize the output file until we've recorded the
1677 // target info from the first input file.
1678 gold_assert(this->size_
> 0);
1681 // Finalize the file, write the string tables and index sections,
1682 // and close the file.
1685 Dwp_output_file::finalize()
1689 // Write the debug string table.
1690 if (this->have_strings_
)
1692 this->stringpool_
.set_string_offsets();
1693 section_size_type len
= this->stringpool_
.get_strtab_size();
1694 buf
= new unsigned char[len
];
1695 this->stringpool_
.write_to_buffer(buf
, len
);
1696 this->add_section(".debug_str.dwo", buf
, len
, 1);
1700 // Write the CU and TU indexes.
1701 if (this->big_endian_
)
1703 this->write_index
<true>(".debug_cu_index", this->cu_index_
);
1704 this->write_index
<true>(".debug_tu_index", this->tu_index_
);
1708 this->write_index
<false>(".debug_cu_index", this->cu_index_
);
1709 this->write_index
<false>(".debug_tu_index", this->tu_index_
);
1712 off_t file_offset
= this->next_file_offset_
;
1714 // Write the section string table.
1715 this->shstrndx_
= this->shnum_
++;
1716 const char* shstrtab_name
=
1717 this->shstrtab_
.add_with_length(".shstrtab",
1718 sizeof(".shstrtab") - 1,
1720 this->shstrtab_
.set_string_offsets();
1721 section_size_type shstrtab_len
= this->shstrtab_
.get_strtab_size();
1722 buf
= new unsigned char[shstrtab_len
];
1723 this->shstrtab_
.write_to_buffer(buf
, shstrtab_len
);
1724 off_t shstrtab_off
= file_offset
;
1725 ::fseek(this->fd_
, file_offset
, 0);
1726 if (::fwrite(buf
, 1, shstrtab_len
, this->fd_
) < shstrtab_len
)
1727 gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_
);
1729 file_offset
+= shstrtab_len
;
1731 // Write the section header table. The first entry is a NULL entry.
1732 // This is followed by the debug sections, and finally we write the
1733 // .shstrtab section header.
1734 file_offset
= align_offset(file_offset
, this->size_
== 32 ? 4 : 8);
1735 this->shoff_
= file_offset
;
1736 ::fseek(this->fd_
, file_offset
, 0);
1737 section_size_type sh0_size
= 0;
1738 unsigned int sh0_link
= 0;
1739 if (this->shnum_
>= elfcpp::SHN_LORESERVE
)
1740 sh0_size
= this->shnum_
;
1741 if (this->shstrndx_
>= elfcpp::SHN_LORESERVE
)
1742 sh0_link
= this->shstrndx_
;
1743 this->write_shdr(NULL
, 0, 0, 0, 0, sh0_size
, sh0_link
, 0, 0, 0);
1744 for (unsigned int i
= 0; i
< this->sections_
.size(); ++i
)
1746 Section
& sect
= this->sections_
[i
];
1747 this->write_shdr(sect
.name
, elfcpp::SHT_PROGBITS
, 0, 0, sect
.offset
,
1748 sect
.size
, 0, 0, sect
.align
, 0);
1750 this->write_shdr(shstrtab_name
, elfcpp::SHT_STRTAB
, 0, 0,
1751 shstrtab_off
, shstrtab_len
, 0, 0, 1, 0);
1753 // Write the ELF header.
1757 if (this->fd_
!= NULL
)
1759 if (::fclose(this->fd_
) != 0)
1760 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
1765 // Write a CU or TU index section.
1766 template<bool big_endian
>
1768 Dwp_output_file::write_index(const char* sect_name
, const Dwp_index
& index
)
1770 const unsigned int nslots
= index
.hash_table_total_slots();
1771 const unsigned int nused
= index
.hash_table_used_slots();
1772 const unsigned int npool
= index
.shndx_pool_size();
1773 const section_size_type index_size
= (4 * sizeof(uint32_t)
1774 + nslots
* sizeof(uint64_t)
1775 + nslots
* sizeof(uint32_t)
1776 + npool
* sizeof(uint32_t));
1778 // Allocate a buffer for the section contents.
1779 unsigned char* buf
= new unsigned char[index_size
];
1780 unsigned char* p
= buf
;
1782 // Write the section header: version number, padding,
1783 // number of used slots and total number of slots.
1784 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, 1);
1785 p
+= sizeof(uint32_t);
1786 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, 0);
1787 p
+= sizeof(uint32_t);
1788 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, nused
);
1789 p
+= sizeof(uint32_t);
1790 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, nslots
);
1791 p
+= sizeof(uint32_t);
1793 // Write the hash table.
1794 for (unsigned int i
= 0; i
< nslots
; ++i
)
1796 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(p
, index
.hash_table(i
));
1797 p
+= sizeof(uint64_t);
1800 // Write the parallel index table.
1801 for (unsigned int i
= 0; i
< nslots
; ++i
)
1803 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, index
.index_table(i
));
1804 p
+= sizeof(uint32_t);
1807 // Write the section index pool.
1808 Dwp_index::Shndx_pool::const_iterator pool
= index
.shndx_pool();
1809 for (unsigned int i
= 0; i
< npool
; ++i
)
1811 gold_assert(pool
!= index
.shndx_pool_end());
1812 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(p
, *pool
);
1813 p
+= sizeof(uint32_t);
1817 gold_assert(p
== buf
+ index_size
);
1819 this->add_section(sect_name
, buf
, index_size
, sizeof(uint64_t));
1824 // Write the ELF header.
1827 Dwp_output_file::write_ehdr()
1829 if (this->size_
== 32)
1831 if (this->big_endian_
)
1832 return this->sized_write_ehdr
<32, true>();
1834 return this->sized_write_ehdr
<32, false>();
1836 else if (this->size_
== 64)
1838 if (this->big_endian_
)
1839 return this->sized_write_ehdr
<64, true>();
1841 return this->sized_write_ehdr
<64, false>();
1847 template<unsigned int size
, bool big_endian
>
1849 Dwp_output_file::sized_write_ehdr()
1851 const unsigned int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
1852 unsigned char buf
[ehdr_size
];
1853 elfcpp::Ehdr_write
<size
, big_endian
> ehdr(buf
);
1855 unsigned char e_ident
[elfcpp::EI_NIDENT
];
1856 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
1857 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
1858 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
1859 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
1860 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
1862 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
1863 else if (size
== 64)
1864 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
1867 e_ident
[elfcpp::EI_DATA
] = (big_endian
1868 ? elfcpp::ELFDATA2MSB
1869 : elfcpp::ELFDATA2LSB
);
1870 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
1871 ehdr
.put_e_ident(e_ident
);
1873 ehdr
.put_e_type(elfcpp::ET_REL
);
1874 ehdr
.put_e_machine(this->machine_
);
1875 ehdr
.put_e_version(elfcpp::EV_CURRENT
);
1876 ehdr
.put_e_entry(0);
1877 ehdr
.put_e_phoff(0);
1878 ehdr
.put_e_shoff(this->shoff_
);
1879 ehdr
.put_e_flags(0);
1880 ehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
1881 ehdr
.put_e_phentsize(0);
1882 ehdr
.put_e_phnum(0);
1883 ehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
1884 ehdr
.put_e_shnum(this->shnum_
< elfcpp::SHN_LORESERVE
? this->shnum_
: 0);
1885 ehdr
.put_e_shstrndx(this->shstrndx_
< elfcpp::SHN_LORESERVE
1887 : static_cast<unsigned int>(elfcpp::SHN_XINDEX
));
1889 ::fseek(this->fd_
, 0, 0);
1890 if (::fwrite(buf
, 1, ehdr_size
, this->fd_
) < ehdr_size
)
1891 gold_fatal(_("%s: error writing ELF header"), this->name_
);
1894 // Write a section header.
1897 Dwp_output_file::write_shdr(const char* name
, unsigned int type
,
1898 unsigned int flags
, uint64_t addr
, off_t offset
,
1899 section_size_type sect_size
, unsigned int link
,
1900 unsigned int info
, unsigned int align
,
1901 unsigned int ent_size
)
1903 if (this->size_
== 32)
1905 if (this->big_endian_
)
1906 return this->sized_write_shdr
<32, true>(name
, type
, flags
, addr
,
1907 offset
, sect_size
, link
, info
,
1910 return this->sized_write_shdr
<32, false>(name
, type
, flags
, addr
,
1911 offset
, sect_size
, link
, info
,
1914 else if (this->size_
== 64)
1916 if (this->big_endian_
)
1917 return this->sized_write_shdr
<64, true>(name
, type
, flags
, addr
,
1918 offset
, sect_size
, link
, info
,
1921 return this->sized_write_shdr
<64, false>(name
, type
, flags
, addr
,
1922 offset
, sect_size
, link
, info
,
1929 template<unsigned int size
, bool big_endian
>
1931 Dwp_output_file::sized_write_shdr(const char* name
, unsigned int type
,
1932 unsigned int flags
, uint64_t addr
,
1933 off_t offset
, section_size_type sect_size
,
1934 unsigned int link
, unsigned int info
,
1935 unsigned int align
, unsigned int ent_size
)
1937 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1938 unsigned char buf
[shdr_size
];
1939 elfcpp::Shdr_write
<size
, big_endian
> shdr(buf
);
1941 shdr
.put_sh_name(name
== NULL
? 0 : this->shstrtab_
.get_offset(name
));
1942 shdr
.put_sh_type(type
);
1943 shdr
.put_sh_flags(flags
);
1944 shdr
.put_sh_addr(addr
);
1945 shdr
.put_sh_offset(offset
);
1946 shdr
.put_sh_size(sect_size
);
1947 shdr
.put_sh_link(link
);
1948 shdr
.put_sh_info(info
);
1949 shdr
.put_sh_addralign(align
);
1950 shdr
.put_sh_entsize(ent_size
);
1951 if (::fwrite(buf
, 1, shdr_size
, this->fd_
) < shdr_size
)
1952 gold_fatal(_("%s: error writing section header table"), this->name_
);
1955 // Class Dwo_id_info_reader.
1957 // Visit a compilation unit.
1960 Dwo_id_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
* die
)
1962 this->dwo_id_
= die
->uint_attribute(elfcpp::DW_AT_GNU_dwo_id
);
1963 if (this->dwo_id_
!= 0)
1964 this->dwo_id_found_
= true;
1967 // Visit a type unit.
1970 Dwo_id_info_reader::visit_type_unit(off_t
, off_t
, uint64_t signature
, Dwarf_die
*)
1972 this->type_sig_
= signature
;
1973 this->type_sig_found_
= true;
1976 }; // End namespace gold
1978 using namespace gold
;
1982 struct option dwp_options
[] =
1984 { "verbose", no_argument
, NULL
, 'v' },
1985 { "output", required_argument
, NULL
, 'o' },
1986 { NULL
, 0, NULL
, 0 }
1989 // Print usage message and exit.
1994 fprintf(stderr
, _("Usage: %s [options] file...\n"), program_name
);
1995 fprintf(stderr
, _(" -v, --verbose Verbose output\n"));
1996 fprintf(stderr
, _(" -o FILE, --output FILE Set output dwp file name"
2004 main(int argc
, char** argv
)
2006 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2007 setlocale(LC_MESSAGES
, "");
2009 #if defined (HAVE_SETLOCALE)
2010 setlocale(LC_CTYPE
, "");
2012 bindtextdomain(PACKAGE
, LOCALEDIR
);
2013 textdomain(PACKAGE
);
2015 program_name
= argv
[0];
2017 // Initialize the global parameters, to let random code get to the
2019 Errors
errors(program_name
);
2020 set_parameters_errors(&errors
);
2022 // Initialize gold's global options. We don't use these in
2023 // this program, but they need to be initialized so that
2024 // functions we call from libgold work properly.
2025 General_options options
;
2026 set_parameters_options(&options
);
2028 // In libiberty; expands @filename to the args in "filename".
2029 expandargv(&argc
, &argv
);
2031 // Collect file names and options.
2032 typedef std::vector
<char*> File_list
;
2034 const char* output_filename
= NULL
;
2035 bool verbose
= false;
2037 while ((c
= getopt_long(argc
, argv
, "vo:", dwp_options
, NULL
)) != -1)
2045 output_filename
= optarg
;
2052 for (int i
= optind
; i
< argc
; ++i
)
2053 files
.push_back(argv
[i
]);
2056 gold_fatal(_("no input files"));
2057 if (output_filename
== NULL
)
2058 gold_fatal(_("no output file specified"));
2060 Dwp_output_file
output_file(output_filename
);
2062 // Process each file, adding its contents to the output file.
2063 for (File_list::const_iterator f
= files
.begin(); f
!= files
.end(); ++f
)
2066 fprintf(stderr
, "%s\n", *f
);
2067 Dwo_file
dwo_file(*f
);
2068 dwo_file
.read(&output_file
);
2071 output_file
.finalize();
2073 return EXIT_SUCCESS
;