Commit | Line | Data |
---|---|---|
77429909 CC |
1 | // dwp.cc -- DWARF packaging utility |
2 | ||
3 | // Copyright 2012 Free Software Foundation, Inc. | |
4 | // Written by Cary Coutant <ccoutant@google.com>. | |
5 | ||
6 | // This file is part of dwp, the DWARF packaging utility. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #include "dwp.h" | |
24 | ||
25 | #include <cstdarg> | |
26 | #include <cstddef> | |
27 | #include <cstdio> | |
28 | #include <cstdlib> | |
29 | #include <cstring> | |
30 | #include <cerrno> | |
31 | ||
32 | #include <vector> | |
33 | #include <algorithm> | |
34 | ||
35 | #include "getopt.h" | |
36 | #include "libiberty.h" | |
c1a8d56e | 37 | #include "../bfd/bfdver.h" |
77429909 CC |
38 | |
39 | #include "elfcpp.h" | |
40 | #include "elfcpp_file.h" | |
41 | #include "dirsearch.h" | |
42 | #include "fileread.h" | |
43 | #include "object.h" | |
44 | #include "compressed_output.h" | |
45 | #include "stringpool.h" | |
46 | #include "dwarf_reader.h" | |
47 | ||
48 | static void | |
c1a8d56e CC |
49 | usage(FILE* fd, int) ATTRIBUTE_NORETURN; |
50 | ||
51 | static void | |
52 | print_version() ATTRIBUTE_NORETURN; | |
77429909 CC |
53 | |
54 | namespace gold { | |
55 | ||
56 | class Dwp_output_file; | |
57 | ||
77429909 CC |
58 | template <int size, bool big_endian> |
59 | class Sized_relobj_dwo; | |
60 | ||
29bd8b6b CC |
61 | // List of .dwo files to process. |
62 | typedef std::vector<std::string> File_list; | |
63 | ||
64 | // An input file. | |
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. | |
68 | ||
77429909 CC |
69 | class Dwo_file |
70 | { | |
71 | public: | |
72 | Dwo_file(const char* name) | |
73 | : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(), | |
74 | str_offset_map_() | |
75 | { } | |
76 | ||
77 | ~Dwo_file(); | |
78 | ||
29bd8b6b CC |
79 | // Read the input executable file and extract the list of .dwo files |
80 | // that it references. | |
81 | void | |
82 | read_executable(File_list* files); | |
83 | ||
77429909 CC |
84 | // Read the input file and send its contents to OUTPUT_FILE. |
85 | void | |
86 | read(Dwp_output_file* output_file); | |
87 | ||
88 | private: | |
89 | // Types for mapping input string offsets to output string offsets. | |
90 | typedef std::pair<section_offset_type, section_offset_type> | |
91 | Str_offset_map_entry; | |
92 | typedef std::vector<Str_offset_map_entry> Str_offset_map; | |
93 | ||
94 | // A less-than comparison routine for Str_offset_map. | |
95 | struct Offset_compare | |
96 | { | |
97 | bool | |
98 | operator()(const Str_offset_map_entry& i1, | |
99 | const Str_offset_map_entry& i2) const | |
100 | { return i1.first < i2.first; } | |
101 | }; | |
102 | ||
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 | |
105 | // in memory. | |
106 | Relobj* | |
29bd8b6b | 107 | make_object(Dwp_output_file* output_file); |
77429909 CC |
108 | |
109 | template <int size, bool big_endian> | |
110 | Relobj* | |
111 | sized_make_object(const unsigned char* p, Input_file* input_file, | |
112 | Dwp_output_file* output_file); | |
113 | ||
114 | // Return the number of sections in the input object file. | |
115 | unsigned int | |
116 | shnum() const | |
117 | { return this->obj_->shnum(); } | |
118 | ||
119 | // Return section type. | |
120 | unsigned int | |
121 | section_type(unsigned int shndx) | |
122 | { return this->obj_->section_type(shndx); } | |
123 | ||
124 | // Get the name of a section. | |
125 | std::string | |
126 | section_name(unsigned int shndx) | |
127 | { return this->obj_->section_name(shndx); } | |
128 | ||
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. | |
132 | const unsigned char* | |
133 | section_contents(unsigned int shndx, section_size_type* plen, bool* is_new) | |
134 | { return this->obj_->decompressed_section_contents(shndx, plen, is_new); } | |
135 | ||
136 | // Read the .debug_cu_index section of a .dwp file, | |
137 | // and process the CU sets. | |
138 | void | |
139 | read_compunit_index(unsigned int, Dwp_output_file*); | |
140 | ||
141 | template <bool big_endian> | |
142 | void | |
143 | sized_read_compunit_index(unsigned int, Dwp_output_file*); | |
144 | ||
145 | // Read the .debug_tu_index section of a .dwp file, | |
146 | // and process the TU sets. | |
147 | void | |
148 | read_typeunit_index(unsigned int, Dwp_output_file*); | |
149 | ||
150 | template <bool big_endian> | |
151 | void | |
152 | sized_read_typeunit_index(unsigned int, Dwp_output_file*); | |
153 | ||
154 | // Merge the input string table section into the output file. | |
155 | void | |
156 | add_strings(Dwp_output_file*, unsigned int); | |
157 | ||
158 | // Copy a section from the input file to the output file. | |
159 | unsigned int | |
160 | copy_section(Dwp_output_file* output_file, unsigned int shndx, | |
161 | const char* section_name, bool is_str_offsets); | |
162 | ||
163 | // Remap the string offsets in the .debug_str_offsets.dwo section. | |
164 | const unsigned char* | |
165 | remap_str_offsets(const unsigned char* contents, section_size_type len); | |
166 | ||
167 | template <bool big_endian> | |
168 | const unsigned char* | |
169 | sized_remap_str_offsets(const unsigned char* contents, section_size_type len); | |
170 | ||
171 | // Remap a single string offsets from an offset in the input string table | |
172 | // to an offset in the output string table. | |
173 | unsigned int | |
598c7410 | 174 | remap_str_offset(section_offset_type val); |
77429909 CC |
175 | |
176 | // Add a set of .debug_info and related sections to OUTPUT_FILE. | |
177 | void | |
178 | add_cu_set(Dwp_output_file* output_file, | |
179 | uint64_t dwo_id, | |
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); | |
187 | ||
188 | // Add a set of .debug_types and related sections to OUTPUT_FILE. | |
189 | void | |
190 | add_tu_set(Dwp_output_file* output_file, | |
191 | uint64_t type_sig, | |
192 | unsigned int debug_types, | |
193 | unsigned int debug_abbrev, | |
194 | unsigned int debug_line, | |
195 | unsigned int debug_str_offsets); | |
196 | ||
197 | // The filename. | |
198 | const char* name_; | |
199 | // The ELF file, represented as a gold Relobj instance. | |
200 | Relobj* obj_; | |
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_; | |
209 | }; | |
210 | ||
211 | // An ELF input file. | |
212 | // We derive from Sized_relobj so that we can use interfaces | |
213 | // in libgold to access the file. | |
214 | ||
215 | template <int size, bool big_endian> | |
216 | class Sized_relobj_dwo : public Sized_relobj<size, big_endian> | |
217 | { | |
218 | public: | |
219 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
220 | typedef typename Sized_relobj<size, big_endian>::Symbols Symbols; | |
221 | ||
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) | |
226 | { } | |
227 | ||
228 | ~Sized_relobj_dwo() | |
229 | { } | |
230 | ||
231 | // Setup the section information. | |
232 | void | |
233 | setup(); | |
234 | ||
235 | protected: | |
236 | // Return section type. | |
237 | unsigned int | |
238 | do_section_type(unsigned int shndx) | |
239 | { return this->elf_file_.section_type(shndx); } | |
240 | ||
241 | // Get the name of a section. | |
242 | std::string | |
243 | do_section_name(unsigned int shndx) | |
244 | { return this->elf_file_.section_name(shndx); } | |
245 | ||
246 | // Get the size of a section. | |
247 | uint64_t | |
248 | do_section_size(unsigned int shndx) | |
249 | { return this->elf_file_.section_size(shndx); } | |
250 | ||
251 | // Return a view of the contents of a section. | |
252 | const unsigned char* | |
253 | do_section_contents(unsigned int, section_size_type*, bool); | |
254 | ||
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 | |
257 | // by the caller. | |
258 | const unsigned char* | |
259 | do_decompressed_section_contents(unsigned int shndx, | |
260 | section_size_type* plen, | |
261 | bool* is_new); | |
262 | ||
263 | // The following virtual functions are abstract in the base classes, | |
264 | // but are not used here. | |
265 | ||
266 | // Read the symbols. | |
267 | void | |
268 | do_read_symbols(Read_symbols_data*) | |
269 | { gold_unreachable(); } | |
270 | ||
271 | // Lay out the input sections. | |
272 | void | |
273 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) | |
274 | { gold_unreachable(); } | |
275 | ||
276 | // Layout sections whose layout was deferred while waiting for | |
277 | // input files from a plugin. | |
278 | void | |
279 | do_layout_deferred_sections(Layout*) | |
280 | { gold_unreachable(); } | |
281 | ||
282 | // Add the symbols to the symbol table. | |
283 | void | |
284 | do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) | |
285 | { gold_unreachable(); } | |
286 | ||
287 | Archive::Should_include | |
288 | do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*, | |
289 | std::string*) | |
290 | { gold_unreachable(); } | |
291 | ||
292 | // Iterate over global symbols, calling a visitor class V for each. | |
293 | void | |
294 | do_for_all_global_symbols(Read_symbols_data*, | |
295 | Library_base::Symbol_visitor_base*) | |
296 | { gold_unreachable(); } | |
297 | ||
298 | // Return section flags. | |
299 | uint64_t | |
300 | do_section_flags(unsigned int) | |
301 | { gold_unreachable(); } | |
302 | ||
303 | // Return section entsize. | |
304 | uint64_t | |
305 | do_section_entsize(unsigned int) | |
306 | { gold_unreachable(); } | |
307 | ||
308 | // Return section address. | |
309 | uint64_t | |
310 | do_section_address(unsigned int) | |
311 | { gold_unreachable(); } | |
312 | ||
313 | // Return the section link field. | |
314 | unsigned int | |
315 | do_section_link(unsigned int) | |
316 | { gold_unreachable(); } | |
317 | ||
318 | // Return the section link field. | |
319 | unsigned int | |
320 | do_section_info(unsigned int) | |
321 | { gold_unreachable(); } | |
322 | ||
323 | // Return the section alignment. | |
324 | uint64_t | |
325 | do_section_addralign(unsigned int) | |
326 | { gold_unreachable(); } | |
327 | ||
328 | // Return the Xindex structure to use. | |
329 | Xindex* | |
330 | do_initialize_xindex() | |
331 | { gold_unreachable(); } | |
332 | ||
333 | // Get symbol counts. | |
334 | void | |
335 | do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const | |
336 | { gold_unreachable(); } | |
337 | ||
338 | // Get global symbols. | |
339 | const Symbols* | |
340 | do_get_global_symbols() const | |
341 | { return NULL; } | |
342 | ||
343 | // Return the value of a local symbol. | |
344 | uint64_t | |
345 | do_local_symbol_value(unsigned int, uint64_t) const | |
346 | { gold_unreachable(); } | |
347 | ||
348 | unsigned int | |
349 | do_local_plt_offset(unsigned int) const | |
350 | { gold_unreachable(); } | |
351 | ||
352 | // Return whether local symbol SYMNDX is a TLS symbol. | |
353 | bool | |
354 | do_local_is_tls(unsigned int) const | |
355 | { gold_unreachable(); } | |
356 | ||
357 | // Return the number of local symbols. | |
358 | unsigned int | |
359 | do_local_symbol_count() const | |
360 | { gold_unreachable(); } | |
361 | ||
362 | // Return the number of local symbols in the output symbol table. | |
363 | unsigned int | |
364 | do_output_local_symbol_count() const | |
365 | { gold_unreachable(); } | |
366 | ||
367 | // Return the file offset for local symbols in the output symbol table. | |
368 | off_t | |
369 | do_local_symbol_offset() const | |
370 | { gold_unreachable(); } | |
371 | ||
372 | // Read the relocs. | |
373 | void | |
374 | do_read_relocs(Read_relocs_data*) | |
375 | { gold_unreachable(); } | |
376 | ||
377 | // Process the relocs to find list of referenced sections. Used only | |
378 | // during garbage collection. | |
379 | void | |
380 | do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) | |
381 | { gold_unreachable(); } | |
382 | ||
383 | // Scan the relocs and adjust the symbol table. | |
384 | void | |
385 | do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) | |
386 | { gold_unreachable(); } | |
387 | ||
388 | // Count the local symbols. | |
389 | void | |
390 | do_count_local_symbols(Stringpool_template<char>*, | |
391 | Stringpool_template<char>*) | |
392 | { gold_unreachable(); } | |
393 | ||
394 | // Finalize the local symbols. | |
395 | unsigned int | |
396 | do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) | |
397 | { gold_unreachable(); } | |
398 | ||
399 | // Set the offset where local dynamic symbol information will be stored. | |
400 | unsigned int | |
401 | do_set_local_dynsym_indexes(unsigned int) | |
402 | { gold_unreachable(); } | |
403 | ||
404 | // Set the offset where local dynamic symbol information will be stored. | |
405 | unsigned int | |
406 | do_set_local_dynsym_offset(off_t) | |
407 | { gold_unreachable(); } | |
408 | ||
409 | // Relocate the input sections and write out the local symbols. | |
410 | void | |
411 | do_relocate(const Symbol_table*, const Layout*, Output_file*) | |
412 | { gold_unreachable(); } | |
413 | ||
414 | private: | |
415 | // General access to the ELF file. | |
416 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
417 | }; | |
418 | ||
419 | // The output file. | |
420 | // This class is responsible for collecting the debug index information | |
421 | // and writing the .dwp file in ELF format. | |
422 | ||
423 | class Dwp_output_file | |
424 | { | |
425 | public: | |
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), | |
431 | last_tu_slot_(0) | |
432 | { | |
433 | this->stringpool_.set_no_zero_null(); | |
434 | } | |
435 | ||
436 | // Record the target info from an input file. | |
437 | void | |
438 | record_target_info(const char* name, int machine, int size, bool big_endian, | |
439 | int osabi, int abiversion); | |
440 | ||
441 | // Add a string to the debug strings section. | |
442 | section_offset_type | |
443 | add_string(const char* str, size_t len); | |
444 | ||
445 | // Add a section to the output file, and return the new section index. | |
446 | unsigned int | |
447 | add_section(const char* section_name, const unsigned char* contents, | |
448 | section_size_type len, int align); | |
449 | ||
450 | // Add a set of .debug_info and related sections to the output file. | |
451 | void | |
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); | |
456 | ||
457 | // Lookup a type signature and return TRUE if we have already seen it. | |
458 | bool | |
459 | lookup_tu(uint64_t type_sig); | |
460 | ||
461 | // Add a set of .debug_types and related sections to the output file. | |
462 | void | |
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); | |
466 | ||
467 | // Finalize the file, write the string tables and index sections, | |
468 | // and close the file. | |
469 | void | |
470 | finalize(); | |
471 | ||
472 | private: | |
473 | // Sections in the output file. | |
474 | struct Section | |
475 | { | |
476 | const char* name; | |
477 | off_t offset; | |
478 | section_size_type size; | |
479 | int align; | |
480 | }; | |
481 | ||
482 | // A set of sections for a compilation unit or type unit. | |
483 | struct Cu_or_tu_set | |
484 | { | |
485 | uint64_t signature; | |
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; | |
493 | }; | |
494 | ||
495 | // The index sections defined by the DWARF Package File Format spec. | |
496 | class Dwp_index | |
497 | { | |
498 | public: | |
499 | // Vector for the section index pool. | |
500 | typedef std::vector<unsigned int> Shndx_pool; | |
501 | ||
502 | Dwp_index() | |
503 | : capacity_(0), used_(0), hash_table_(NULL), shndx_pool_() | |
504 | { } | |
505 | ||
506 | ~Dwp_index() | |
507 | { } | |
508 | ||
509 | // Find a slot in the hash table for SIGNATURE. Return TRUE | |
510 | // if the entry already exists. | |
511 | bool | |
512 | find_or_add(uint64_t signature, unsigned int* slotp); | |
513 | ||
514 | // Enter a CU or TU set at the given SLOT in the hash table. | |
515 | void | |
516 | enter_set(unsigned int slot, const Cu_or_tu_set& set); | |
517 | ||
518 | // Return the contents of the given SLOT in the hash table of signatures. | |
519 | uint64_t | |
520 | hash_table(unsigned int slot) const | |
521 | { return this->hash_table_[slot]; } | |
522 | ||
523 | // Return the contents of the given SLOT in the parallel table of | |
524 | // shndx pool indexes. | |
525 | uint32_t | |
526 | index_table(unsigned int slot) const | |
527 | { return this->index_table_[slot]; } | |
528 | ||
529 | // Return the total number of slots in the hash table. | |
530 | unsigned int | |
531 | hash_table_total_slots() const | |
532 | { return this->capacity_; } | |
533 | ||
534 | // Return the number of used slots in the hash table. | |
535 | unsigned int | |
536 | hash_table_used_slots() const | |
537 | { return this->used_; } | |
538 | ||
539 | // Return an iterator into the shndx pool. | |
540 | Shndx_pool::const_iterator | |
541 | shndx_pool() const | |
542 | { return this->shndx_pool_.begin(); } | |
543 | ||
544 | Shndx_pool::const_iterator | |
545 | shndx_pool_end() const | |
546 | { return this->shndx_pool_.end(); } | |
547 | ||
548 | // Return the number of entries in the shndx pool. | |
549 | unsigned int | |
550 | shndx_pool_size() const | |
551 | { return this->shndx_pool_.size(); } | |
552 | ||
553 | private: | |
554 | // Initialize the hash table. | |
555 | void | |
556 | initialize(); | |
557 | ||
558 | // Grow the hash table when we reach 2/3 capacity. | |
559 | void | |
560 | grow(); | |
561 | ||
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. | |
566 | unsigned int used_; | |
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. | |
574 | ||
575 | // Initialize the output file. | |
576 | void | |
577 | initialize(); | |
578 | ||
579 | // Write the ELF header. | |
580 | void | |
581 | write_ehdr(); | |
582 | ||
583 | template<unsigned int size, bool big_endian> | |
584 | void | |
585 | sized_write_ehdr(); | |
586 | ||
587 | // Write a section header. | |
588 | void | |
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); | |
593 | ||
594 | template<unsigned int size, bool big_endian> | |
595 | void | |
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); | |
600 | ||
601 | // Write a CU or TU index section. | |
602 | template<bool big_endian> | |
603 | void | |
604 | write_index(const char* sect_name, const Dwp_index& index); | |
605 | ||
606 | // The output filename. | |
607 | const char* name_; | |
608 | // ELF header parameters. | |
609 | int machine_; | |
610 | int size_; | |
611 | int big_endian_; | |
612 | int osabi_; | |
613 | int abiversion_; | |
614 | // The output file descriptor. | |
615 | FILE* fd_; | |
616 | // Next available file offset. | |
617 | off_t next_file_offset_; | |
618 | // The number of sections. | |
619 | unsigned int shnum_; | |
620 | // Section table. The first entry is shndx 1. | |
621 | std::vector<Section> sections_; | |
622 | // File offset of the section header table. | |
623 | off_t shoff_; | |
624 | // Section index of the section string table. | |
625 | unsigned int shstrndx_; | |
626 | // TRUE if we have added any strings to the string pool. | |
627 | bool have_strings_; | |
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. | |
633 | Dwp_index cu_index_; | |
634 | // The type unit index. | |
635 | Dwp_index tu_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_; | |
640 | }; | |
641 | ||
29bd8b6b CC |
642 | // A specialization of Dwarf_info_reader, for reading dwo_names from |
643 | // DWARF CUs. | |
644 | ||
645 | class Dwo_name_info_reader : public Dwarf_info_reader | |
646 | { | |
647 | public: | |
648 | Dwo_name_info_reader(Relobj* object, unsigned int shndx) | |
649 | : Dwarf_info_reader(false, object, NULL, 0, shndx, 0, 0), | |
650 | files_(NULL) | |
651 | { } | |
652 | ||
653 | ~Dwo_name_info_reader() | |
654 | { } | |
655 | ||
656 | // Get the dwo_names from the DWARF compilation unit DIEs. | |
657 | void | |
658 | get_dwo_names(File_list* files) | |
659 | { | |
660 | this->files_ = files; | |
661 | this->parse(); | |
662 | } | |
663 | ||
664 | protected: | |
665 | // Visit a compilation unit. | |
666 | virtual void | |
667 | visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*); | |
668 | ||
669 | private: | |
670 | // The list of files to populate. | |
671 | File_list* files_; | |
672 | }; | |
673 | ||
77429909 CC |
674 | // A specialization of Dwarf_info_reader, for reading dwo_ids and |
675 | // type signatures from DWARF CUs and TUs. | |
676 | ||
677 | class Dwo_id_info_reader : public Dwarf_info_reader | |
678 | { | |
679 | public: | |
680 | Dwo_id_info_reader(bool is_type_unit, | |
681 | Relobj* object, | |
29bd8b6b CC |
682 | unsigned int shndx) |
683 | : Dwarf_info_reader(is_type_unit, object, NULL, 0, shndx, 0, 0), | |
77429909 CC |
684 | dwo_id_found_(false), dwo_id_(0), type_sig_found_(false), type_sig_(0) |
685 | { } | |
686 | ||
687 | ~Dwo_id_info_reader() | |
688 | { } | |
689 | ||
690 | // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID. | |
691 | bool | |
692 | get_dwo_id(uint64_t* dwo_id) | |
693 | { | |
694 | this->parse(); | |
695 | if (!this->dwo_id_found_) | |
696 | return false; | |
697 | *dwo_id = this->dwo_id_; | |
698 | return true; | |
699 | } | |
700 | ||
701 | // Return the type signature from a DWARF type unit DIE in *TYPE_SIG. | |
702 | bool | |
703 | get_type_sig(uint64_t* type_sig) | |
704 | { | |
705 | this->parse(); | |
706 | if (!this->type_sig_found_) | |
707 | return false; | |
708 | *type_sig = this->type_sig_; | |
709 | return true; | |
710 | } | |
711 | ||
712 | protected: | |
713 | // Visit a compilation unit. | |
714 | virtual void | |
715 | visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*); | |
716 | ||
717 | // Visit a type unit. | |
718 | virtual void | |
719 | visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature, | |
720 | Dwarf_die*); | |
721 | ||
722 | private: | |
77429909 CC |
723 | // TRUE if we found a dwo_id. |
724 | bool dwo_id_found_; | |
725 | // The dwo_id. | |
726 | uint64_t dwo_id_; | |
727 | // TRUE if we found a type signature. | |
728 | bool type_sig_found_; | |
729 | // The type signature. | |
730 | uint64_t type_sig_; | |
731 | }; | |
732 | ||
733 | // Class Sized_relobj_dwo. | |
734 | ||
735 | // Setup the section information. | |
736 | ||
737 | template <int size, bool big_endian> | |
738 | void | |
739 | Sized_relobj_dwo<size, big_endian>::setup() | |
740 | { | |
741 | const unsigned int shnum = this->elf_file_.shnum(); | |
742 | this->set_shnum(shnum); | |
743 | this->section_offsets().resize(shnum); | |
744 | } | |
745 | ||
746 | // Return a view of the contents of a section. | |
747 | ||
748 | template <int size, bool big_endian> | |
749 | const unsigned char* | |
750 | Sized_relobj_dwo<size, big_endian>::do_section_contents( | |
751 | unsigned int shndx, | |
752 | section_size_type* plen, | |
753 | bool cache) | |
754 | { | |
755 | Object::Location loc(this->elf_file_.section_contents(shndx)); | |
756 | *plen = convert_to_section_size_type(loc.data_size); | |
757 | if (*plen == 0) | |
758 | { | |
759 | static const unsigned char empty[1] = { '\0' }; | |
760 | return empty; | |
761 | } | |
762 | return this->get_view(loc.file_offset, *plen, true, cache); | |
763 | } | |
764 | ||
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 | |
767 | // by the caller. | |
768 | ||
769 | template <int size, bool big_endian> | |
770 | const unsigned char* | |
771 | Sized_relobj_dwo<size, big_endian>::do_decompressed_section_contents( | |
772 | unsigned int shndx, | |
773 | section_size_type* plen, | |
774 | bool* is_new) | |
775 | { | |
776 | section_size_type buffer_size; | |
777 | const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size, | |
778 | false); | |
779 | ||
780 | std::string sect_name = this->do_section_name(shndx); | |
781 | if (!is_prefix_of(".zdebug_", sect_name.c_str())) | |
782 | { | |
783 | *plen = buffer_size; | |
784 | *is_new = false; | |
785 | return buffer; | |
786 | } | |
787 | ||
788 | section_size_type uncompressed_size = get_uncompressed_size(buffer, | |
789 | buffer_size); | |
790 | unsigned char* uncompressed_data = new unsigned char[uncompressed_size]; | |
791 | if (!decompress_input_section(buffer, | |
792 | buffer_size, | |
793 | uncompressed_data, | |
794 | uncompressed_size)) | |
795 | this->error(_("could not decompress section %s"), | |
796 | this->section_name(shndx).c_str()); | |
797 | *plen = uncompressed_size; | |
798 | *is_new = true; | |
799 | return uncompressed_data; | |
800 | } | |
801 | ||
802 | // Class Dwo_file. | |
803 | ||
804 | Dwo_file::~Dwo_file() | |
805 | { | |
77429909 CC |
806 | if (this->obj_ != NULL) |
807 | delete this->obj_; | |
29bd8b6b CC |
808 | if (this->input_file_ != NULL) |
809 | delete this->input_file_; | |
810 | } | |
811 | ||
812 | // Read the input executable file and extract the list of .dwo files | |
813 | // that it references. | |
814 | ||
815 | void | |
816 | Dwo_file::read_executable(File_list* files) | |
817 | { | |
818 | this->obj_ = this->make_object(NULL); | |
819 | ||
820 | unsigned int shnum = this->shnum(); | |
821 | this->is_compressed_.resize(shnum); | |
822 | this->shndx_map_.resize(shnum); | |
823 | ||
824 | unsigned int debug_info = 0; | |
825 | unsigned int debug_abbrev = 0; | |
826 | ||
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++) | |
830 | { | |
831 | if (this->section_type(i) != elfcpp::SHT_PROGBITS) | |
832 | continue; | |
833 | std::string sect_name = this->section_name(i); | |
834 | const char* suffix = sect_name.c_str(); | |
835 | if (is_prefix_of(".debug_", suffix)) | |
836 | suffix += 7; | |
837 | else if (is_prefix_of(".zdebug_", suffix)) | |
838 | { | |
839 | this->is_compressed_[i] = true; | |
840 | suffix += 8; | |
841 | } | |
842 | else | |
843 | continue; | |
844 | if (strcmp(suffix, "info") == 0) | |
845 | debug_info = i; | |
846 | else if (strcmp(suffix, "abbrev") == 0) | |
847 | debug_abbrev = i; | |
848 | } | |
849 | ||
850 | if (debug_info > 0) | |
851 | { | |
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); | |
855 | } | |
77429909 CC |
856 | } |
857 | ||
858 | // Read the input file and send its contents to OUTPUT_FILE. | |
859 | ||
860 | void | |
861 | Dwo_file::read(Dwp_output_file* output_file) | |
862 | { | |
29bd8b6b | 863 | this->obj_ = this->make_object(output_file); |
77429909 CC |
864 | |
865 | unsigned int shnum = this->shnum(); | |
866 | this->is_compressed_.resize(shnum); | |
867 | this->shndx_map_.resize(shnum); | |
868 | ||
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; | |
881 | ||
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++) | |
885 | { | |
886 | if (this->section_type(i) != elfcpp::SHT_PROGBITS) | |
887 | continue; | |
888 | std::string sect_name = this->section_name(i); | |
889 | const char* suffix = sect_name.c_str(); | |
890 | if (is_prefix_of(".debug_", suffix)) | |
891 | suffix += 7; | |
892 | else if (is_prefix_of(".zdebug_", suffix)) | |
893 | { | |
894 | this->is_compressed_[i] = true; | |
895 | suffix += 8; | |
896 | } | |
897 | else | |
898 | continue; | |
899 | if (strcmp(suffix, "cu_index") == 0) | |
900 | debug_cu_index = i; | |
901 | else if (strcmp(suffix, "tu_index") == 0) | |
902 | debug_tu_index = i; | |
903 | else if (strcmp(suffix, "str.dwo") == 0) | |
904 | debug_str = i; | |
905 | } | |
906 | ||
907 | // Merge the input string table into the output string table. | |
908 | this->add_strings(output_file, debug_str); | |
909 | ||
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) | |
913 | { | |
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); | |
918 | return; | |
919 | } | |
920 | ||
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++) | |
924 | { | |
925 | if (this->section_type(i) != elfcpp::SHT_PROGBITS) | |
926 | continue; | |
927 | std::string sect_name = this->section_name(i); | |
928 | const char* suffix = sect_name.c_str(); | |
929 | if (is_prefix_of(".debug_", suffix)) | |
930 | suffix += 7; | |
931 | else if (is_prefix_of(".zdebug_", suffix)) | |
932 | suffix += 8; | |
933 | else | |
934 | continue; | |
935 | // TODO: Check for one of each section (except .debug_types). | |
936 | if (strcmp(suffix, "info.dwo") == 0) | |
937 | debug_info = i; | |
938 | else if (strcmp(suffix, "types.dwo") == 0) | |
939 | debug_types.push_back(i); | |
940 | else if (strcmp(suffix, "abbrev.dwo") == 0) | |
941 | debug_abbrev = i; | |
942 | else if (strcmp(suffix, "line.dwo") == 0) | |
943 | debug_line = i; | |
944 | else if (strcmp(suffix, "loc.dwo") == 0) | |
945 | debug_loc = i; | |
946 | else if (strcmp(suffix, "str_offsets.dwo") == 0) | |
947 | debug_str_offsets = i; | |
948 | else if (strcmp(suffix, "macinfo.dwo") == 0) | |
949 | debug_macinfo = i; | |
950 | else if (strcmp(suffix, "macro.dwo") == 0) | |
951 | debug_macro = i; | |
952 | } | |
953 | ||
954 | if (debug_info > 0) | |
955 | { | |
956 | // Extract the dwo_id from .debug_info.dwo section. | |
957 | uint64_t dwo_id; | |
29bd8b6b | 958 | Dwo_id_info_reader dwarf_reader(false, this->obj_, debug_info); |
77429909 CC |
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); | |
966 | } | |
967 | ||
968 | for (Types_list::const_iterator tp = debug_types.begin(); | |
969 | tp != debug_types.end(); | |
970 | ++tp) | |
971 | { | |
972 | // Extract the type signature from .debug_types.dwo section. | |
973 | uint64_t type_sig; | |
974 | gold_assert(*tp > 0); | |
29bd8b6b | 975 | Dwo_id_info_reader dwarf_reader(true, this->obj_, *tp); |
77429909 CC |
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"), | |
979 | this->name_); | |
980 | this->add_tu_set(output_file, type_sig, *tp, debug_abbrev, debug_line, | |
981 | debug_str_offsets); | |
982 | } | |
983 | } | |
984 | ||
985 | // Create a Sized_relobj_dwo of the given size and endianness, | |
29bd8b6b | 986 | // and record the target info. |
77429909 CC |
987 | |
988 | Relobj* | |
29bd8b6b | 989 | Dwo_file::make_object(Dwp_output_file* output_file) |
77429909 | 990 | { |
29bd8b6b CC |
991 | // Open the input file. |
992 | Input_file* input_file = new Input_file(this->name_); | |
993 | this->input_file_ = input_file; | |
994 | Dirsearch dirpath; | |
995 | int index; | |
996 | if (!input_file->open(dirpath, NULL, &index)) | |
997 | gold_fatal(_("%s: can't open"), this->name_); | |
998 | ||
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) | |
1003 | hdrsize = filesize; | |
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_); | |
1008 | ||
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. | |
1012 | int size; | |
1013 | bool big_endian; | |
1014 | std::string error; | |
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()); | |
1018 | ||
77429909 CC |
1019 | if (size == 32) |
1020 | { | |
1021 | if (big_endian) | |
1022 | #ifdef HAVE_TARGET_32_BIG | |
29bd8b6b CC |
1023 | return this->sized_make_object<32, true>(elf_header, input_file, |
1024 | output_file); | |
77429909 CC |
1025 | #else |
1026 | gold_unreachable(); | |
1027 | #endif | |
1028 | else | |
1029 | #ifdef HAVE_TARGET_32_LITTLE | |
29bd8b6b CC |
1030 | return this->sized_make_object<32, false>(elf_header, input_file, |
1031 | output_file); | |
77429909 CC |
1032 | #else |
1033 | gold_unreachable(); | |
1034 | #endif | |
1035 | } | |
1036 | else if (size == 64) | |
1037 | { | |
1038 | if (big_endian) | |
1039 | #ifdef HAVE_TARGET_64_BIG | |
29bd8b6b CC |
1040 | return this->sized_make_object<64, true>(elf_header, input_file, |
1041 | output_file); | |
77429909 CC |
1042 | #else |
1043 | gold_unreachable(); | |
1044 | #endif | |
1045 | else | |
1046 | #ifdef HAVE_TARGET_64_LITTLE | |
29bd8b6b CC |
1047 | return this->sized_make_object<64, false>(elf_header, input_file, |
1048 | output_file); | |
77429909 CC |
1049 | #else |
1050 | gold_unreachable(); | |
1051 | #endif | |
1052 | } | |
1053 | else | |
1054 | gold_unreachable(); | |
1055 | } | |
1056 | ||
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. | |
1059 | ||
1060 | template <int size, bool big_endian> | |
1061 | Relobj* | |
1062 | Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file, | |
1063 | Dwp_output_file* output_file) | |
1064 | { | |
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); | |
1068 | obj->setup(); | |
29bd8b6b CC |
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]); | |
77429909 CC |
1074 | return obj; |
1075 | } | |
1076 | ||
1077 | // Read the .debug_cu_index section of a .dwp file, | |
1078 | // and process the CU sets. | |
1079 | ||
1080 | void | |
1081 | Dwo_file::read_compunit_index(unsigned int shndx, Dwp_output_file* output_file) | |
1082 | { | |
1083 | if (this->obj_->is_big_endian()) | |
1084 | this->sized_read_compunit_index<true>(shndx, output_file); | |
1085 | else | |
1086 | this->sized_read_compunit_index<false>(shndx, output_file); | |
1087 | } | |
1088 | ||
1089 | template <bool big_endian> | |
1090 | void | |
1091 | Dwo_file::sized_read_compunit_index(unsigned int shndx, | |
1092 | Dwp_output_file* output_file) | |
1093 | { | |
1094 | section_size_type len; | |
1095 | bool is_new; | |
1096 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1097 | ||
1098 | unsigned int version = | |
1099 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents); | |
1100 | if (version != 1) | |
1101 | gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"), | |
1102 | this->name_, version); | |
1103 | ||
1104 | unsigned int nused = | |
1105 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1106 | + 2 * sizeof(uint32_t)); | |
1107 | if (nused == 0) | |
1108 | return; | |
1109 | ||
1110 | unsigned int nslots = | |
1111 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1112 | + 3 * sizeof(uint32_t)); | |
1113 | ||
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; | |
1118 | ||
1119 | if (shndx_pool >= limit) | |
1120 | gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_); | |
1121 | ||
1122 | // Loop over the slots of the hash table. | |
1123 | for (unsigned int i = 0; i < nslots; ++i) | |
1124 | { | |
1125 | uint64_t dwo_id = | |
1126 | elfcpp::Swap_unaligned<64, big_endian>::readval(phash); | |
1127 | if (dwo_id != 0) | |
1128 | { | |
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); | |
1133 | ||
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; | |
1142 | for (;;) | |
1143 | { | |
1144 | if (shndx_list >= limit) | |
1145 | gold_fatal(_("%s: .debug_cu_index is corrupt"), | |
1146 | this->name_); | |
1147 | unsigned int shndx = | |
1148 | elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list); | |
1149 | if (shndx == 0) | |
1150 | break; | |
1151 | if (shndx > this->shnum()) | |
1152 | gold_fatal(_("%s: .debug_cu_index has bad shndx"), | |
1153 | this->name_); | |
1154 | std::string sect_name = this->section_name(shndx); | |
1155 | const char* suffix = sect_name.c_str(); | |
1156 | if (is_prefix_of(".debug_", suffix)) | |
1157 | suffix += 7; | |
1158 | else if (is_prefix_of(".zdebug_", suffix)) | |
1159 | suffix += 8; | |
1160 | else | |
1161 | gold_fatal(_("%s: .debug_cu_index refers to " | |
1162 | "non-debug section"), this->name_); | |
1163 | if (strcmp(suffix, "info.dwo") == 0) | |
1164 | debug_info = shndx; | |
1165 | else if (strcmp(suffix, "abbrev.dwo") == 0) | |
1166 | debug_abbrev = shndx; | |
1167 | else if (strcmp(suffix, "line.dwo") == 0) | |
1168 | debug_line = shndx; | |
1169 | else if (strcmp(suffix, "loc.dwo") == 0) | |
1170 | debug_loc = shndx; | |
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); | |
1178 | } | |
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); | |
1182 | } | |
1183 | phash += sizeof(uint64_t); | |
1184 | pindex += sizeof(uint32_t); | |
1185 | } | |
1186 | ||
1187 | if (is_new) | |
1188 | delete[] contents; | |
1189 | } | |
1190 | ||
1191 | // Read the .debug_tu_index section of a .dwp file, | |
1192 | // and process the TU sets. | |
1193 | ||
1194 | void | |
1195 | Dwo_file::read_typeunit_index(unsigned int shndx, Dwp_output_file* output_file) | |
1196 | { | |
1197 | if (this->obj_->is_big_endian()) | |
1198 | this->sized_read_typeunit_index<true>(shndx, output_file); | |
1199 | else | |
1200 | this->sized_read_typeunit_index<false>(shndx, output_file); | |
1201 | } | |
1202 | ||
1203 | template <bool big_endian> | |
1204 | void | |
1205 | Dwo_file::sized_read_typeunit_index(unsigned int shndx, | |
1206 | Dwp_output_file* output_file) | |
1207 | { | |
1208 | section_size_type len; | |
1209 | bool is_new; | |
1210 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1211 | ||
1212 | unsigned int version = | |
1213 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents); | |
1214 | if (version != 1) | |
1215 | gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"), | |
1216 | this->name_, version); | |
1217 | ||
1218 | unsigned int nused = | |
1219 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1220 | + 2 * sizeof(uint32_t)); | |
1221 | if (nused == 0) | |
1222 | return; | |
1223 | ||
1224 | unsigned int nslots = | |
1225 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1226 | + 3 * sizeof(uint32_t)); | |
1227 | ||
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; | |
1232 | ||
1233 | if (shndx_pool >= limit) | |
1234 | gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_); | |
1235 | ||
1236 | // Loop over the slots of the hash table. | |
1237 | for (unsigned int i = 0; i < nslots; ++i) | |
1238 | { | |
1239 | uint64_t type_sig = | |
1240 | elfcpp::Swap_unaligned<64, big_endian>::readval(phash); | |
1241 | if (type_sig != 0) | |
1242 | { | |
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); | |
1247 | ||
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; | |
1253 | for (;;) | |
1254 | { | |
1255 | if (shndx_list >= limit) | |
1256 | gold_fatal(_("%s: .debug_tu_index is corrupt"), | |
1257 | this->name_); | |
1258 | unsigned int shndx = | |
1259 | elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list); | |
1260 | if (shndx == 0) | |
1261 | break; | |
1262 | if (shndx > this->shnum()) | |
1263 | gold_fatal(_("%s: .debug_tu_index has bad shndx"), | |
1264 | this->name_); | |
1265 | std::string sect_name = this->section_name(shndx); | |
1266 | const char* suffix = sect_name.c_str(); | |
1267 | if (is_prefix_of(".debug_", suffix)) | |
1268 | suffix += 7; | |
1269 | else if (is_prefix_of(".zdebug_", suffix)) | |
1270 | suffix += 8; | |
1271 | else | |
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) | |
1279 | debug_line = shndx; | |
1280 | else if (strcmp(suffix, "str_offsets.dwo") == 0) | |
1281 | debug_str_offsets = shndx; | |
1282 | shndx_list += sizeof(uint32_t); | |
1283 | } | |
1284 | this->add_tu_set(output_file, type_sig, debug_types, debug_abbrev, | |
1285 | debug_line, debug_str_offsets); | |
1286 | } | |
1287 | phash += sizeof(uint64_t); | |
1288 | pindex += sizeof(uint32_t); | |
1289 | } | |
1290 | ||
1291 | if (is_new) | |
1292 | delete[] contents; | |
1293 | } | |
1294 | ||
1295 | // Merge the input string table section into the output file. | |
1296 | ||
1297 | void | |
1298 | Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str) | |
1299 | { | |
1300 | section_size_type len; | |
1301 | bool is_new; | |
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; | |
1305 | ||
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"), | |
1310 | this->name_, | |
1311 | this->section_name(debug_str).c_str()); | |
1312 | ||
1313 | // Count the number of strings in the section, and size the map. | |
1314 | size_t count = 0; | |
1315 | for (const char* pt = p; pt < pend; pt += strlen(pt) + 1) | |
1316 | ++count; | |
1317 | this->str_offset_map_.reserve(count + 1); | |
1318 | ||
1319 | // Add the strings to the output string table, and record the new offsets | |
1320 | // in the map. | |
1321 | section_offset_type i = 0; | |
1322 | section_offset_type new_offset; | |
1323 | while (p < pend) | |
1324 | { | |
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)); | |
1328 | p += len + 1; | |
1329 | i += len + 1; | |
1330 | } | |
1331 | new_offset = 0; | |
1332 | this->str_offset_map_.push_back(std::make_pair(i, new_offset)); | |
1333 | if (is_new) | |
1334 | delete[] pdata; | |
1335 | } | |
1336 | ||
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. | |
1340 | ||
1341 | unsigned int | |
1342 | Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx, | |
1343 | const char* section_name, bool is_str_offsets) | |
1344 | { | |
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]; | |
1349 | ||
1350 | section_size_type len; | |
1351 | bool is_new; | |
1352 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1353 | ||
1354 | if (is_str_offsets) | |
1355 | { | |
1356 | const unsigned char* remapped = this->remap_str_offsets(contents, len); | |
1357 | if (is_new) | |
1358 | delete[] contents; | |
1359 | contents = remapped; | |
1360 | is_new = true; | |
1361 | } | |
1362 | ||
1363 | this->shndx_map_[shndx] = output_file->add_section(section_name, contents, | |
1364 | len, 1); | |
1365 | if (is_new) | |
1366 | delete[] contents; | |
1367 | ||
1368 | return this->shndx_map_[shndx]; | |
1369 | } | |
1370 | ||
1371 | // Remap the | |
1372 | const unsigned char* | |
1373 | Dwo_file::remap_str_offsets(const unsigned char* contents, | |
1374 | section_size_type len) | |
1375 | { | |
1376 | if ((len & 3) != 0) | |
1377 | gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"), | |
1378 | this->name_); | |
1379 | ||
1380 | if (this->obj_->is_big_endian()) | |
1381 | return this->sized_remap_str_offsets<true>(contents, len); | |
1382 | else | |
1383 | return this->sized_remap_str_offsets<false>(contents, len); | |
1384 | } | |
1385 | ||
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) | |
1390 | { | |
1391 | unsigned char* remapped = new unsigned char[len]; | |
1392 | const unsigned char* p = contents; | |
1393 | unsigned char* q = remapped; | |
1394 | while (len > 0) | |
1395 | { | |
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); | |
1399 | len -= 4; | |
1400 | p += 4; | |
1401 | q += 4; | |
1402 | } | |
1403 | return remapped; | |
1404 | } | |
1405 | ||
1406 | unsigned int | |
598c7410 | 1407 | Dwo_file::remap_str_offset(section_offset_type val) |
77429909 CC |
1408 | { |
1409 | Str_offset_map_entry entry; | |
1410 | entry.first = val; | |
1411 | ||
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()); | |
1416 | ||
1417 | if (p == this->str_offset_map_.end() || p->first > val) | |
1418 | { | |
1419 | if (p == this->str_offset_map_.begin()) | |
1420 | return 0; | |
1421 | --p; | |
1422 | gold_assert(p->first <= val); | |
1423 | } | |
1424 | ||
1425 | return p->second + (val - p->first); | |
1426 | } | |
1427 | ||
1428 | // Add a set of .debug_info and related sections to OUTPUT_FILE. | |
1429 | ||
1430 | void | |
1431 | Dwo_file::add_cu_set(Dwp_output_file* output_file, | |
1432 | uint64_t dwo_id, | |
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) | |
1440 | { | |
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_); | |
1445 | ||
1446 | debug_abbrev = this->copy_section(output_file, debug_abbrev, | |
1447 | ".debug_abbrev.dwo", false); | |
1448 | if (debug_line > 0) | |
1449 | debug_line = this->copy_section(output_file, debug_line, | |
1450 | ".debug_line.dwo", false); | |
1451 | if (debug_loc > 0) | |
1452 | debug_loc = this->copy_section(output_file, debug_loc, ".debug_loc.dwo", | |
1453 | false); | |
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); | |
1460 | ||
1461 | if (debug_str_offsets > 0) | |
1462 | debug_str_offsets = this->copy_section(output_file, debug_str_offsets, | |
1463 | ".debug_str_offsets.dwo", true); | |
1464 | ||
1465 | debug_info = this->copy_section(output_file, debug_info, ".debug_info.dwo", | |
1466 | false); | |
1467 | ||
1468 | output_file->add_cu_set(dwo_id, debug_info, debug_abbrev, debug_line, | |
1469 | debug_loc, debug_str_offsets, debug_macinfo, | |
1470 | debug_macro); | |
1471 | } | |
1472 | ||
1473 | // Add a set of .debug_types and related sections to OUTPUT_FILE. | |
1474 | ||
1475 | void | |
1476 | Dwo_file::add_tu_set(Dwp_output_file* output_file, | |
1477 | uint64_t type_sig, | |
1478 | unsigned int debug_types, | |
1479 | unsigned int debug_abbrev, | |
1480 | unsigned int debug_line, | |
1481 | unsigned int debug_str_offsets) | |
1482 | { | |
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_); | |
1487 | ||
1488 | // Ignore duplicate type signatures. | |
1489 | if (output_file->lookup_tu(type_sig)) | |
1490 | return; | |
1491 | ||
1492 | debug_abbrev = this->copy_section(output_file, debug_abbrev, | |
1493 | ".debug_abbrev.dwo", false); | |
1494 | if (debug_line > 0) | |
1495 | debug_line = this->copy_section(output_file, debug_line, | |
1496 | ".debug_line.dwo", false); | |
1497 | ||
1498 | if (debug_str_offsets > 0) | |
1499 | debug_str_offsets = this->copy_section(output_file, debug_str_offsets, | |
1500 | ".debug_str_offsets.dwo", true); | |
1501 | ||
1502 | debug_types = this->copy_section(output_file, debug_types, | |
1503 | ".debug_types.dwo", false); | |
1504 | ||
1505 | output_file->add_tu_set(type_sig, debug_types, debug_abbrev, debug_line, | |
1506 | debug_str_offsets); | |
1507 | } | |
1508 | ||
1509 | // Class Dwp_output_file. | |
1510 | ||
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. | |
1514 | ||
1515 | void | |
1516 | Dwp_output_file::record_target_info(const char*, int machine, | |
1517 | int size, bool big_endian, | |
1518 | int osabi, int abiversion) | |
1519 | { | |
1520 | // TODO: Check the values on subsequent calls. | |
1521 | if (this->size_ > 0) | |
1522 | return; | |
1523 | ||
1524 | this->machine_ = machine; | |
1525 | this->size_ = size; | |
1526 | this->big_endian_ = big_endian; | |
1527 | this->osabi_ = osabi; | |
1528 | this->abiversion_ = abiversion; | |
1529 | ||
1530 | if (size == 32) | |
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; | |
1534 | else | |
1535 | gold_unreachable(); | |
1536 | ||
1537 | this->fd_ = ::fopen(this->name_, "wb"); | |
1538 | if (this->fd_ == NULL) | |
1539 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
1540 | ||
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 }; | |
d361fafb L |
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)); | |
77429909 CC |
1547 | } |
1548 | ||
1549 | // Add a string to the debug strings section. | |
1550 | ||
1551 | section_offset_type | |
1552 | Dwp_output_file::add_string(const char* str, size_t len) | |
1553 | { | |
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); | |
1561 | } | |
1562 | ||
1563 | // Align the file offset to the given boundary. | |
1564 | ||
1565 | static inline off_t | |
1566 | align_offset(off_t off, int align) | |
1567 | { | |
1568 | return (off + align - 1) & ~(align - 1); | |
1569 | } | |
1570 | ||
1571 | // Add a section to the output file, and return the new section index. | |
1572 | ||
1573 | unsigned int | |
1574 | Dwp_output_file::add_section(const char* section_name, | |
1575 | const unsigned char* contents, | |
1576 | section_size_type len, | |
1577 | int align) | |
1578 | { | |
1579 | off_t file_offset = this->next_file_offset_; | |
1580 | gold_assert(this->size_ > 0 && file_offset > 0); | |
1581 | ||
1582 | file_offset = align_offset(file_offset, align); | |
1583 | ||
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); | |
1587 | ||
1588 | section_name = this->shstrtab_.add_with_length(section_name, | |
1589 | strlen(section_name), | |
1590 | false, NULL); | |
1591 | Section sect = { section_name, file_offset, len, align }; | |
1592 | this->sections_.push_back(sect); | |
1593 | ||
1594 | this->next_file_offset_ = file_offset + len; | |
1595 | return this->shnum_++; | |
1596 | } | |
1597 | ||
1598 | // Add a set of .debug_info and related sections to the output file. | |
1599 | ||
1600 | void | |
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) | |
1609 | { | |
1610 | Cu_or_tu_set cu_set = { dwo_id, debug_info, debug_abbrev, debug_line, | |
1611 | debug_loc, debug_str_offsets, debug_macinfo, | |
1612 | debug_macro }; | |
1613 | unsigned int slot; | |
2a77e2ab CC |
1614 | if (!this->cu_index_.find_or_add(dwo_id, &slot)) |
1615 | this->cu_index_.enter_set(slot, cu_set); | |
1616 | else | |
1617 | gold_warning(_("%s: duplicate entry for CU (dwo_id 0x%llx)"), | |
1618 | this->name_, (unsigned long long)dwo_id); | |
77429909 CC |
1619 | } |
1620 | ||
1621 | // Lookup a type signature and return TRUE if we have already seen it. | |
1622 | bool | |
1623 | Dwp_output_file::lookup_tu(uint64_t type_sig) | |
1624 | { | |
1625 | this->last_type_sig_ = type_sig; | |
1626 | return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_); | |
1627 | } | |
1628 | ||
1629 | // Add a set of .debug_types and related sections to the output file. | |
1630 | ||
1631 | void | |
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) | |
1637 | { | |
1638 | Cu_or_tu_set tu_set = { type_sig, debug_types, debug_abbrev, debug_line, | |
1639 | 0, debug_str_offsets, 0, 0 }; | |
1640 | unsigned int slot; | |
1641 | if (type_sig == this->last_type_sig_) | |
1642 | slot = this->last_tu_slot_; | |
1643 | else | |
1644 | this->tu_index_.find_or_add(type_sig, &slot); | |
1645 | this->tu_index_.enter_set(slot, tu_set); | |
1646 | } | |
1647 | ||
1648 | // Find a slot in the hash table for SIGNATURE. Return TRUE | |
1649 | // if the entry already exists. | |
1650 | ||
1651 | bool | |
1652 | Dwp_output_file::Dwp_index::find_or_add(uint64_t signature, | |
1653 | unsigned int* slotp) | |
1654 | { | |
1655 | if (this->capacity_ == 0) | |
1656 | this->initialize(); | |
1657 | unsigned int slot = | |
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) | |
1662 | { | |
1663 | secondary_hash = (static_cast<unsigned int>(signature >> 32) | |
1664 | & (this->capacity_ - 1)) | 1; | |
1665 | do | |
1666 | { | |
1667 | slot = (slot + secondary_hash) & (this->capacity_ - 1); | |
1668 | probe = this->hash_table_[slot]; | |
1669 | } while (probe != 0 && probe != signature); | |
1670 | } | |
1671 | *slotp = slot; | |
1672 | return (probe != 0); | |
1673 | } | |
1674 | ||
1675 | // Enter a CU or TU set at the given SLOT in the hash table. | |
1676 | ||
1677 | void | |
1678 | Dwp_output_file::Dwp_index::enter_set(unsigned int slot, | |
1679 | const Cu_or_tu_set& set) | |
1680 | { | |
1681 | gold_assert(slot < this->capacity_); | |
1682 | gold_assert(set.debug_info_or_types > 0); | |
1683 | gold_assert(set.debug_abbrev > 0); | |
1684 | ||
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); | |
1700 | ||
1701 | // Enter the signature and pool index into the hash table. | |
2a77e2ab | 1702 | gold_assert(this->hash_table_[slot] == 0); |
77429909 CC |
1703 | this->hash_table_[slot] = set.signature; |
1704 | this->index_table_[slot] = pool_index; | |
1705 | ++this->used_; | |
1706 | ||
1707 | // Grow the hash table when we exceed 2/3 capacity. | |
1708 | if (this->used_ * 3 > this->capacity_ * 2) | |
1709 | this->grow(); | |
1710 | } | |
1711 | ||
1712 | // Initialize the hash table. | |
1713 | ||
1714 | void | |
1715 | Dwp_output_file::Dwp_index::initialize() | |
1716 | { | |
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)); | |
1722 | } | |
1723 | ||
1724 | // Grow the hash table when we reach 2/3 capacity. | |
1725 | ||
1726 | void | |
1727 | Dwp_output_file::Dwp_index::grow() | |
1728 | { | |
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_; | |
1733 | ||
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)); | |
1739 | this->used_ = 0; | |
1740 | ||
1741 | for (unsigned int i = 0; i < old_capacity; ++i) | |
1742 | { | |
1743 | uint64_t signature = old_hash_table[i]; | |
1744 | if (signature != 0) | |
1745 | { | |
1746 | unsigned int slot; | |
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]; | |
1751 | ++this->used_; | |
1752 | } | |
1753 | } | |
1754 | gold_assert(this->used_ == old_used); | |
1755 | ||
1756 | delete[] old_hash_table; | |
1757 | delete[] old_index_table; | |
1758 | } | |
1759 | ||
1760 | // Initialize the output file. | |
1761 | ||
1762 | void | |
1763 | Dwp_output_file::initialize() | |
1764 | { | |
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); | |
1768 | } | |
1769 | ||
1770 | // Finalize the file, write the string tables and index sections, | |
1771 | // and close the file. | |
1772 | ||
1773 | void | |
1774 | Dwp_output_file::finalize() | |
1775 | { | |
1776 | unsigned char* buf; | |
1777 | ||
1778 | // Write the debug string table. | |
1779 | if (this->have_strings_) | |
1780 | { | |
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); | |
1786 | delete[] buf; | |
1787 | } | |
1788 | ||
1789 | // Write the CU and TU indexes. | |
1790 | if (this->big_endian_) | |
1791 | { | |
1792 | this->write_index<true>(".debug_cu_index", this->cu_index_); | |
1793 | this->write_index<true>(".debug_tu_index", this->tu_index_); | |
1794 | } | |
1795 | else | |
1796 | { | |
1797 | this->write_index<false>(".debug_cu_index", this->cu_index_); | |
1798 | this->write_index<false>(".debug_tu_index", this->tu_index_); | |
1799 | } | |
1800 | ||
1801 | off_t file_offset = this->next_file_offset_; | |
1802 | ||
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, | |
1808 | false, NULL); | |
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_); | |
1817 | delete[] buf; | |
1818 | file_offset += shstrtab_len; | |
1819 | ||
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) | |
1834 | { | |
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); | |
1838 | } | |
1839 | this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0, | |
1840 | shstrtab_off, shstrtab_len, 0, 0, 1, 0); | |
1841 | ||
1842 | // Write the ELF header. | |
1843 | this->write_ehdr(); | |
1844 | ||
1845 | // Close the file. | |
1846 | if (this->fd_ != NULL) | |
1847 | { | |
1848 | if (::fclose(this->fd_) != 0) | |
1849 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
1850 | } | |
1851 | this->fd_ = NULL; | |
1852 | } | |
1853 | ||
1854 | // Write a CU or TU index section. | |
1855 | template<bool big_endian> | |
1856 | void | |
1857 | Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index) | |
1858 | { | |
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)); | |
1866 | ||
1867 | // Allocate a buffer for the section contents. | |
1868 | unsigned char* buf = new unsigned char[index_size]; | |
1869 | unsigned char* p = buf; | |
1870 | ||
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); | |
1881 | ||
1882 | // Write the hash table. | |
1883 | for (unsigned int i = 0; i < nslots; ++i) | |
1884 | { | |
1885 | elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i)); | |
1886 | p += sizeof(uint64_t); | |
1887 | } | |
1888 | ||
1889 | // Write the parallel index table. | |
1890 | for (unsigned int i = 0; i < nslots; ++i) | |
1891 | { | |
1892 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i)); | |
1893 | p += sizeof(uint32_t); | |
1894 | } | |
1895 | ||
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) | |
1899 | { | |
1900 | gold_assert(pool != index.shndx_pool_end()); | |
1901 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, *pool); | |
1902 | p += sizeof(uint32_t); | |
1903 | ++pool; | |
1904 | } | |
1905 | ||
1906 | gold_assert(p == buf + index_size); | |
1907 | ||
1908 | this->add_section(sect_name, buf, index_size, sizeof(uint64_t)); | |
1909 | ||
1910 | delete[] buf; | |
1911 | } | |
1912 | ||
1913 | // Write the ELF header. | |
1914 | ||
1915 | void | |
1916 | Dwp_output_file::write_ehdr() | |
1917 | { | |
1918 | if (this->size_ == 32) | |
1919 | { | |
1920 | if (this->big_endian_) | |
1921 | return this->sized_write_ehdr<32, true>(); | |
1922 | else | |
1923 | return this->sized_write_ehdr<32, false>(); | |
1924 | } | |
1925 | else if (this->size_ == 64) | |
1926 | { | |
1927 | if (this->big_endian_) | |
1928 | return this->sized_write_ehdr<64, true>(); | |
1929 | else | |
1930 | return this->sized_write_ehdr<64, false>(); | |
1931 | } | |
1932 | else | |
1933 | gold_unreachable(); | |
1934 | } | |
1935 | ||
1936 | template<unsigned int size, bool big_endian> | |
1937 | void | |
1938 | Dwp_output_file::sized_write_ehdr() | |
1939 | { | |
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); | |
1943 | ||
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; | |
1950 | if (size == 32) | |
1951 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
1952 | else if (size == 64) | |
1953 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
1954 | else | |
1955 | gold_unreachable(); | |
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); | |
1961 | ||
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 | |
1975 | ? this->shstrndx_ | |
1976 | : static_cast<unsigned int>(elfcpp::SHN_XINDEX)); | |
1977 | ||
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_); | |
1981 | } | |
1982 | ||
1983 | // Write a section header. | |
1984 | ||
1985 | void | |
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) | |
1991 | { | |
1992 | if (this->size_ == 32) | |
1993 | { | |
1994 | if (this->big_endian_) | |
1995 | return this->sized_write_shdr<32, true>(name, type, flags, addr, | |
1996 | offset, sect_size, link, info, | |
1997 | align, ent_size); | |
1998 | else | |
1999 | return this->sized_write_shdr<32, false>(name, type, flags, addr, | |
2000 | offset, sect_size, link, info, | |
2001 | align, ent_size); | |
2002 | } | |
2003 | else if (this->size_ == 64) | |
2004 | { | |
2005 | if (this->big_endian_) | |
2006 | return this->sized_write_shdr<64, true>(name, type, flags, addr, | |
2007 | offset, sect_size, link, info, | |
2008 | align, ent_size); | |
2009 | else | |
2010 | return this->sized_write_shdr<64, false>(name, type, flags, addr, | |
2011 | offset, sect_size, link, info, | |
2012 | align, ent_size); | |
2013 | } | |
2014 | else | |
2015 | gold_unreachable(); | |
2016 | } | |
2017 | ||
2018 | template<unsigned int size, bool big_endian> | |
2019 | void | |
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) | |
2025 | { | |
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); | |
2029 | ||
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_); | |
2042 | } | |
2043 | ||
29bd8b6b CC |
2044 | // Class Dwo_name_info_reader. |
2045 | ||
2046 | // Visit a compilation unit. | |
2047 | ||
2048 | void | |
2049 | Dwo_name_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die) | |
2050 | { | |
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); | |
2054 | } | |
2055 | ||
77429909 CC |
2056 | // Class Dwo_id_info_reader. |
2057 | ||
2058 | // Visit a compilation unit. | |
2059 | ||
2060 | void | |
2061 | Dwo_id_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die) | |
2062 | { | |
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; | |
2066 | } | |
2067 | ||
2068 | // Visit a type unit. | |
2069 | ||
2070 | void | |
29bd8b6b CC |
2071 | Dwo_id_info_reader::visit_type_unit(off_t, off_t, uint64_t signature, |
2072 | Dwarf_die*) | |
77429909 CC |
2073 | { |
2074 | this->type_sig_ = signature; | |
2075 | this->type_sig_found_ = true; | |
2076 | } | |
2077 | ||
2078 | }; // End namespace gold | |
2079 | ||
2080 | using namespace gold; | |
2081 | ||
2082 | // Options. | |
2083 | ||
2084 | struct option dwp_options[] = | |
2085 | { | |
29bd8b6b | 2086 | { "exec", required_argument, NULL, 'e' }, |
c1a8d56e | 2087 | { "help", no_argument, NULL, 'h' }, |
77429909 | 2088 | { "output", required_argument, NULL, 'o' }, |
c1a8d56e CC |
2089 | { "verbose", no_argument, NULL, 'v' }, |
2090 | { "version", no_argument, NULL, 'V' }, | |
77429909 CC |
2091 | { NULL, 0, NULL, 0 } |
2092 | }; | |
2093 | ||
2094 | // Print usage message and exit. | |
2095 | ||
2096 | static void | |
c1a8d56e | 2097 | usage(FILE* fd, int exit_status) |
77429909 | 2098 | { |
29bd8b6b | 2099 | fprintf(fd, _("Usage: %s [options] [file...]\n"), program_name); |
c1a8d56e | 2100 | fprintf(fd, _(" -h, --help Print this help message\n")); |
29bd8b6b CC |
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")); | |
c1a8d56e CC |
2104 | fprintf(fd, _(" -v, --verbose Verbose output\n")); |
2105 | fprintf(fd, _(" -V, --version Print version number\n")); | |
2106 | ||
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); | |
2111 | exit(exit_status); | |
2112 | } | |
2113 | ||
2114 | // Report version information. | |
2115 | ||
2116 | static void | |
2117 | print_version() | |
2118 | { | |
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")); | |
2122 | printf(_("\ | |
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")); | |
2126 | exit(EXIT_SUCCESS); | |
77429909 CC |
2127 | } |
2128 | ||
2129 | // Main program. | |
2130 | ||
2131 | int | |
2132 | main(int argc, char** argv) | |
2133 | { | |
2134 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
2135 | setlocale(LC_MESSAGES, ""); | |
2136 | #endif | |
2137 | #if defined (HAVE_SETLOCALE) | |
2138 | setlocale(LC_CTYPE, ""); | |
2139 | #endif | |
2140 | bindtextdomain(PACKAGE, LOCALEDIR); | |
2141 | textdomain(PACKAGE); | |
2142 | ||
2143 | program_name = argv[0]; | |
2144 | ||
2145 | // Initialize the global parameters, to let random code get to the | |
2146 | // errors object. | |
2147 | Errors errors(program_name); | |
2148 | set_parameters_errors(&errors); | |
2149 | ||
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); | |
2155 | ||
2156 | // In libiberty; expands @filename to the args in "filename". | |
2157 | expandargv(&argc, &argv); | |
2158 | ||
2159 | // Collect file names and options. | |
77429909 | 2160 | File_list files; |
29bd8b6b CC |
2161 | std::string output_filename; |
2162 | const char* exe_filename = NULL; | |
77429909 CC |
2163 | bool verbose = false; |
2164 | int c; | |
29bd8b6b | 2165 | while ((c = getopt_long(argc, argv, "e:ho:vV", dwp_options, NULL)) != -1) |
77429909 CC |
2166 | { |
2167 | switch (c) | |
2168 | { | |
c1a8d56e CC |
2169 | case 'h': |
2170 | usage(stdout, EXIT_SUCCESS); | |
29bd8b6b CC |
2171 | case 'e': |
2172 | exe_filename = optarg; | |
2173 | break; | |
77429909 | 2174 | case 'o': |
29bd8b6b | 2175 | output_filename.assign(optarg); |
77429909 | 2176 | break; |
c1a8d56e CC |
2177 | case 'v': |
2178 | verbose = true; | |
2179 | break; | |
2180 | case 'V': | |
2181 | print_version(); | |
77429909 CC |
2182 | case '?': |
2183 | default: | |
c1a8d56e | 2184 | usage(stderr, EXIT_FAILURE); |
77429909 CC |
2185 | } |
2186 | } | |
29bd8b6b CC |
2187 | |
2188 | if (output_filename.empty()) | |
2189 | { | |
2190 | if (exe_filename == NULL) | |
2191 | gold_fatal(_("no output file specified")); | |
2192 | output_filename.assign(exe_filename); | |
2193 | output_filename.append(".dwp"); | |
2194 | } | |
2195 | ||
2196 | Dwp_output_file output_file(output_filename.c_str()); | |
2197 | ||
2198 | // Get list of .dwo files from the executable. | |
2199 | if (exe_filename != NULL) | |
2200 | { | |
2201 | Dwo_file exe_file(exe_filename); | |
2202 | exe_file.read_executable(&files); | |
2203 | } | |
2204 | ||
2205 | // Add any additional files listed on command line. | |
77429909 CC |
2206 | for (int i = optind; i < argc; ++i) |
2207 | files.push_back(argv[i]); | |
2208 | ||
29bd8b6b CC |
2209 | if (exe_filename == NULL && files.empty()) |
2210 | gold_fatal(_("no input files and no executable specified")); | |
77429909 | 2211 | |
77429909 CC |
2212 | // Process each file, adding its contents to the output file. |
2213 | for (File_list::const_iterator f = files.begin(); f != files.end(); ++f) | |
2214 | { | |
2215 | if (verbose) | |
29bd8b6b CC |
2216 | fprintf(stderr, "%s\n", f->c_str()); |
2217 | Dwo_file dwo_file(f->c_str()); | |
77429909 CC |
2218 | dwo_file.read(&output_file); |
2219 | } | |
2220 | ||
2221 | output_file.finalize(); | |
2222 | ||
2223 | return EXIT_SUCCESS; | |
2224 | } |