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" | |
37 | ||
38 | #include "elfcpp.h" | |
39 | #include "elfcpp_file.h" | |
40 | #include "dirsearch.h" | |
41 | #include "fileread.h" | |
42 | #include "object.h" | |
43 | #include "compressed_output.h" | |
44 | #include "stringpool.h" | |
45 | #include "dwarf_reader.h" | |
46 | ||
47 | static void | |
48 | usage() ATTRIBUTE_NORETURN; | |
49 | ||
50 | namespace gold { | |
51 | ||
52 | class Dwp_output_file; | |
53 | ||
54 | // An input file. | |
55 | // This class may represent either a .dwo file or a .dwp file | |
56 | // produced by an earlier run. | |
57 | ||
58 | template <int size, bool big_endian> | |
59 | class Sized_relobj_dwo; | |
60 | ||
61 | class Dwo_file | |
62 | { | |
63 | public: | |
64 | Dwo_file(const char* name) | |
65 | : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(), | |
66 | str_offset_map_() | |
67 | { } | |
68 | ||
69 | ~Dwo_file(); | |
70 | ||
71 | // Read the input file and send its contents to OUTPUT_FILE. | |
72 | void | |
73 | read(Dwp_output_file* output_file); | |
74 | ||
75 | private: | |
76 | // Types for mapping input string offsets to output string offsets. | |
77 | typedef std::pair<section_offset_type, section_offset_type> | |
78 | Str_offset_map_entry; | |
79 | typedef std::vector<Str_offset_map_entry> Str_offset_map; | |
80 | ||
81 | // A less-than comparison routine for Str_offset_map. | |
82 | struct Offset_compare | |
83 | { | |
84 | bool | |
85 | operator()(const Str_offset_map_entry& i1, | |
86 | const Str_offset_map_entry& i2) const | |
87 | { return i1.first < i2.first; } | |
88 | }; | |
89 | ||
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 | |
92 | // in memory. | |
93 | Relobj* | |
94 | make_object(int size, bool big_endian, const unsigned char* p, | |
95 | Input_file* input_file, Dwp_output_file* output_file); | |
96 | ||
97 | template <int size, bool big_endian> | |
98 | Relobj* | |
99 | sized_make_object(const unsigned char* p, Input_file* input_file, | |
100 | Dwp_output_file* output_file); | |
101 | ||
102 | // Return the number of sections in the input object file. | |
103 | unsigned int | |
104 | shnum() const | |
105 | { return this->obj_->shnum(); } | |
106 | ||
107 | // Return section type. | |
108 | unsigned int | |
109 | section_type(unsigned int shndx) | |
110 | { return this->obj_->section_type(shndx); } | |
111 | ||
112 | // Get the name of a section. | |
113 | std::string | |
114 | section_name(unsigned int shndx) | |
115 | { return this->obj_->section_name(shndx); } | |
116 | ||
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. | |
120 | const unsigned char* | |
121 | section_contents(unsigned int shndx, section_size_type* plen, bool* is_new) | |
122 | { return this->obj_->decompressed_section_contents(shndx, plen, is_new); } | |
123 | ||
124 | // Read the .debug_cu_index section of a .dwp file, | |
125 | // and process the CU sets. | |
126 | void | |
127 | read_compunit_index(unsigned int, Dwp_output_file*); | |
128 | ||
129 | template <bool big_endian> | |
130 | void | |
131 | sized_read_compunit_index(unsigned int, Dwp_output_file*); | |
132 | ||
133 | // Read the .debug_tu_index section of a .dwp file, | |
134 | // and process the TU sets. | |
135 | void | |
136 | read_typeunit_index(unsigned int, Dwp_output_file*); | |
137 | ||
138 | template <bool big_endian> | |
139 | void | |
140 | sized_read_typeunit_index(unsigned int, Dwp_output_file*); | |
141 | ||
142 | // Merge the input string table section into the output file. | |
143 | void | |
144 | add_strings(Dwp_output_file*, unsigned int); | |
145 | ||
146 | // Copy a section from the input file to the output file. | |
147 | unsigned int | |
148 | copy_section(Dwp_output_file* output_file, unsigned int shndx, | |
149 | const char* section_name, bool is_str_offsets); | |
150 | ||
151 | // Remap the string offsets in the .debug_str_offsets.dwo section. | |
152 | const unsigned char* | |
153 | remap_str_offsets(const unsigned char* contents, section_size_type len); | |
154 | ||
155 | template <bool big_endian> | |
156 | const unsigned char* | |
157 | sized_remap_str_offsets(const unsigned char* contents, section_size_type len); | |
158 | ||
159 | // Remap a single string offsets from an offset in the input string table | |
160 | // to an offset in the output string table. | |
161 | unsigned int | |
598c7410 | 162 | remap_str_offset(section_offset_type val); |
77429909 CC |
163 | |
164 | // Add a set of .debug_info and related sections to OUTPUT_FILE. | |
165 | void | |
166 | add_cu_set(Dwp_output_file* output_file, | |
167 | uint64_t dwo_id, | |
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); | |
175 | ||
176 | // Add a set of .debug_types and related sections to OUTPUT_FILE. | |
177 | void | |
178 | add_tu_set(Dwp_output_file* output_file, | |
179 | uint64_t type_sig, | |
180 | unsigned int debug_types, | |
181 | unsigned int debug_abbrev, | |
182 | unsigned int debug_line, | |
183 | unsigned int debug_str_offsets); | |
184 | ||
185 | // The filename. | |
186 | const char* name_; | |
187 | // The ELF file, represented as a gold Relobj instance. | |
188 | Relobj* obj_; | |
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_; | |
197 | }; | |
198 | ||
199 | // An ELF input file. | |
200 | // We derive from Sized_relobj so that we can use interfaces | |
201 | // in libgold to access the file. | |
202 | ||
203 | template <int size, bool big_endian> | |
204 | class Sized_relobj_dwo : public Sized_relobj<size, big_endian> | |
205 | { | |
206 | public: | |
207 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
208 | typedef typename Sized_relobj<size, big_endian>::Symbols Symbols; | |
209 | ||
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) | |
214 | { } | |
215 | ||
216 | ~Sized_relobj_dwo() | |
217 | { } | |
218 | ||
219 | // Setup the section information. | |
220 | void | |
221 | setup(); | |
222 | ||
223 | protected: | |
224 | // Return section type. | |
225 | unsigned int | |
226 | do_section_type(unsigned int shndx) | |
227 | { return this->elf_file_.section_type(shndx); } | |
228 | ||
229 | // Get the name of a section. | |
230 | std::string | |
231 | do_section_name(unsigned int shndx) | |
232 | { return this->elf_file_.section_name(shndx); } | |
233 | ||
234 | // Get the size of a section. | |
235 | uint64_t | |
236 | do_section_size(unsigned int shndx) | |
237 | { return this->elf_file_.section_size(shndx); } | |
238 | ||
239 | // Return a view of the contents of a section. | |
240 | const unsigned char* | |
241 | do_section_contents(unsigned int, section_size_type*, bool); | |
242 | ||
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 | |
245 | // by the caller. | |
246 | const unsigned char* | |
247 | do_decompressed_section_contents(unsigned int shndx, | |
248 | section_size_type* plen, | |
249 | bool* is_new); | |
250 | ||
251 | // The following virtual functions are abstract in the base classes, | |
252 | // but are not used here. | |
253 | ||
254 | // Read the symbols. | |
255 | void | |
256 | do_read_symbols(Read_symbols_data*) | |
257 | { gold_unreachable(); } | |
258 | ||
259 | // Lay out the input sections. | |
260 | void | |
261 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) | |
262 | { gold_unreachable(); } | |
263 | ||
264 | // Layout sections whose layout was deferred while waiting for | |
265 | // input files from a plugin. | |
266 | void | |
267 | do_layout_deferred_sections(Layout*) | |
268 | { gold_unreachable(); } | |
269 | ||
270 | // Add the symbols to the symbol table. | |
271 | void | |
272 | do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) | |
273 | { gold_unreachable(); } | |
274 | ||
275 | Archive::Should_include | |
276 | do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*, | |
277 | std::string*) | |
278 | { gold_unreachable(); } | |
279 | ||
280 | // Iterate over global symbols, calling a visitor class V for each. | |
281 | void | |
282 | do_for_all_global_symbols(Read_symbols_data*, | |
283 | Library_base::Symbol_visitor_base*) | |
284 | { gold_unreachable(); } | |
285 | ||
286 | // Return section flags. | |
287 | uint64_t | |
288 | do_section_flags(unsigned int) | |
289 | { gold_unreachable(); } | |
290 | ||
291 | // Return section entsize. | |
292 | uint64_t | |
293 | do_section_entsize(unsigned int) | |
294 | { gold_unreachable(); } | |
295 | ||
296 | // Return section address. | |
297 | uint64_t | |
298 | do_section_address(unsigned int) | |
299 | { gold_unreachable(); } | |
300 | ||
301 | // Return the section link field. | |
302 | unsigned int | |
303 | do_section_link(unsigned int) | |
304 | { gold_unreachable(); } | |
305 | ||
306 | // Return the section link field. | |
307 | unsigned int | |
308 | do_section_info(unsigned int) | |
309 | { gold_unreachable(); } | |
310 | ||
311 | // Return the section alignment. | |
312 | uint64_t | |
313 | do_section_addralign(unsigned int) | |
314 | { gold_unreachable(); } | |
315 | ||
316 | // Return the Xindex structure to use. | |
317 | Xindex* | |
318 | do_initialize_xindex() | |
319 | { gold_unreachable(); } | |
320 | ||
321 | // Get symbol counts. | |
322 | void | |
323 | do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const | |
324 | { gold_unreachable(); } | |
325 | ||
326 | // Get global symbols. | |
327 | const Symbols* | |
328 | do_get_global_symbols() const | |
329 | { return NULL; } | |
330 | ||
331 | // Return the value of a local symbol. | |
332 | uint64_t | |
333 | do_local_symbol_value(unsigned int, uint64_t) const | |
334 | { gold_unreachable(); } | |
335 | ||
336 | unsigned int | |
337 | do_local_plt_offset(unsigned int) const | |
338 | { gold_unreachable(); } | |
339 | ||
340 | // Return whether local symbol SYMNDX is a TLS symbol. | |
341 | bool | |
342 | do_local_is_tls(unsigned int) const | |
343 | { gold_unreachable(); } | |
344 | ||
345 | // Return the number of local symbols. | |
346 | unsigned int | |
347 | do_local_symbol_count() const | |
348 | { gold_unreachable(); } | |
349 | ||
350 | // Return the number of local symbols in the output symbol table. | |
351 | unsigned int | |
352 | do_output_local_symbol_count() const | |
353 | { gold_unreachable(); } | |
354 | ||
355 | // Return the file offset for local symbols in the output symbol table. | |
356 | off_t | |
357 | do_local_symbol_offset() const | |
358 | { gold_unreachable(); } | |
359 | ||
360 | // Read the relocs. | |
361 | void | |
362 | do_read_relocs(Read_relocs_data*) | |
363 | { gold_unreachable(); } | |
364 | ||
365 | // Process the relocs to find list of referenced sections. Used only | |
366 | // during garbage collection. | |
367 | void | |
368 | do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) | |
369 | { gold_unreachable(); } | |
370 | ||
371 | // Scan the relocs and adjust the symbol table. | |
372 | void | |
373 | do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) | |
374 | { gold_unreachable(); } | |
375 | ||
376 | // Count the local symbols. | |
377 | void | |
378 | do_count_local_symbols(Stringpool_template<char>*, | |
379 | Stringpool_template<char>*) | |
380 | { gold_unreachable(); } | |
381 | ||
382 | // Finalize the local symbols. | |
383 | unsigned int | |
384 | do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) | |
385 | { gold_unreachable(); } | |
386 | ||
387 | // Set the offset where local dynamic symbol information will be stored. | |
388 | unsigned int | |
389 | do_set_local_dynsym_indexes(unsigned int) | |
390 | { gold_unreachable(); } | |
391 | ||
392 | // Set the offset where local dynamic symbol information will be stored. | |
393 | unsigned int | |
394 | do_set_local_dynsym_offset(off_t) | |
395 | { gold_unreachable(); } | |
396 | ||
397 | // Relocate the input sections and write out the local symbols. | |
398 | void | |
399 | do_relocate(const Symbol_table*, const Layout*, Output_file*) | |
400 | { gold_unreachable(); } | |
401 | ||
402 | private: | |
403 | // General access to the ELF file. | |
404 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
405 | }; | |
406 | ||
407 | // The output file. | |
408 | // This class is responsible for collecting the debug index information | |
409 | // and writing the .dwp file in ELF format. | |
410 | ||
411 | class Dwp_output_file | |
412 | { | |
413 | public: | |
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), | |
419 | last_tu_slot_(0) | |
420 | { | |
421 | this->stringpool_.set_no_zero_null(); | |
422 | } | |
423 | ||
424 | // Record the target info from an input file. | |
425 | void | |
426 | record_target_info(const char* name, int machine, int size, bool big_endian, | |
427 | int osabi, int abiversion); | |
428 | ||
429 | // Add a string to the debug strings section. | |
430 | section_offset_type | |
431 | add_string(const char* str, size_t len); | |
432 | ||
433 | // Add a section to the output file, and return the new section index. | |
434 | unsigned int | |
435 | add_section(const char* section_name, const unsigned char* contents, | |
436 | section_size_type len, int align); | |
437 | ||
438 | // Add a set of .debug_info and related sections to the output file. | |
439 | void | |
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); | |
444 | ||
445 | // Lookup a type signature and return TRUE if we have already seen it. | |
446 | bool | |
447 | lookup_tu(uint64_t type_sig); | |
448 | ||
449 | // Add a set of .debug_types and related sections to the output file. | |
450 | void | |
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); | |
454 | ||
455 | // Finalize the file, write the string tables and index sections, | |
456 | // and close the file. | |
457 | void | |
458 | finalize(); | |
459 | ||
460 | private: | |
461 | // Sections in the output file. | |
462 | struct Section | |
463 | { | |
464 | const char* name; | |
465 | off_t offset; | |
466 | section_size_type size; | |
467 | int align; | |
468 | }; | |
469 | ||
470 | // A set of sections for a compilation unit or type unit. | |
471 | struct Cu_or_tu_set | |
472 | { | |
473 | uint64_t signature; | |
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; | |
481 | }; | |
482 | ||
483 | // The index sections defined by the DWARF Package File Format spec. | |
484 | class Dwp_index | |
485 | { | |
486 | public: | |
487 | // Vector for the section index pool. | |
488 | typedef std::vector<unsigned int> Shndx_pool; | |
489 | ||
490 | Dwp_index() | |
491 | : capacity_(0), used_(0), hash_table_(NULL), shndx_pool_() | |
492 | { } | |
493 | ||
494 | ~Dwp_index() | |
495 | { } | |
496 | ||
497 | // Find a slot in the hash table for SIGNATURE. Return TRUE | |
498 | // if the entry already exists. | |
499 | bool | |
500 | find_or_add(uint64_t signature, unsigned int* slotp); | |
501 | ||
502 | // Enter a CU or TU set at the given SLOT in the hash table. | |
503 | void | |
504 | enter_set(unsigned int slot, const Cu_or_tu_set& set); | |
505 | ||
506 | // Return the contents of the given SLOT in the hash table of signatures. | |
507 | uint64_t | |
508 | hash_table(unsigned int slot) const | |
509 | { return this->hash_table_[slot]; } | |
510 | ||
511 | // Return the contents of the given SLOT in the parallel table of | |
512 | // shndx pool indexes. | |
513 | uint32_t | |
514 | index_table(unsigned int slot) const | |
515 | { return this->index_table_[slot]; } | |
516 | ||
517 | // Return the total number of slots in the hash table. | |
518 | unsigned int | |
519 | hash_table_total_slots() const | |
520 | { return this->capacity_; } | |
521 | ||
522 | // Return the number of used slots in the hash table. | |
523 | unsigned int | |
524 | hash_table_used_slots() const | |
525 | { return this->used_; } | |
526 | ||
527 | // Return an iterator into the shndx pool. | |
528 | Shndx_pool::const_iterator | |
529 | shndx_pool() const | |
530 | { return this->shndx_pool_.begin(); } | |
531 | ||
532 | Shndx_pool::const_iterator | |
533 | shndx_pool_end() const | |
534 | { return this->shndx_pool_.end(); } | |
535 | ||
536 | // Return the number of entries in the shndx pool. | |
537 | unsigned int | |
538 | shndx_pool_size() const | |
539 | { return this->shndx_pool_.size(); } | |
540 | ||
541 | private: | |
542 | // Initialize the hash table. | |
543 | void | |
544 | initialize(); | |
545 | ||
546 | // Grow the hash table when we reach 2/3 capacity. | |
547 | void | |
548 | grow(); | |
549 | ||
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. | |
554 | unsigned int used_; | |
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. | |
562 | ||
563 | // Initialize the output file. | |
564 | void | |
565 | initialize(); | |
566 | ||
567 | // Write the ELF header. | |
568 | void | |
569 | write_ehdr(); | |
570 | ||
571 | template<unsigned int size, bool big_endian> | |
572 | void | |
573 | sized_write_ehdr(); | |
574 | ||
575 | // Write a section header. | |
576 | void | |
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); | |
581 | ||
582 | template<unsigned int size, bool big_endian> | |
583 | void | |
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); | |
588 | ||
589 | // Write a CU or TU index section. | |
590 | template<bool big_endian> | |
591 | void | |
592 | write_index(const char* sect_name, const Dwp_index& index); | |
593 | ||
594 | // The output filename. | |
595 | const char* name_; | |
596 | // ELF header parameters. | |
597 | int machine_; | |
598 | int size_; | |
599 | int big_endian_; | |
600 | int osabi_; | |
601 | int abiversion_; | |
602 | // The output file descriptor. | |
603 | FILE* fd_; | |
604 | // Next available file offset. | |
605 | off_t next_file_offset_; | |
606 | // The number of sections. | |
607 | unsigned int shnum_; | |
608 | // Section table. The first entry is shndx 1. | |
609 | std::vector<Section> sections_; | |
610 | // File offset of the section header table. | |
611 | off_t shoff_; | |
612 | // Section index of the section string table. | |
613 | unsigned int shstrndx_; | |
614 | // TRUE if we have added any strings to the string pool. | |
615 | bool have_strings_; | |
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. | |
621 | Dwp_index cu_index_; | |
622 | // The type unit index. | |
623 | Dwp_index tu_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_; | |
628 | }; | |
629 | ||
630 | // A specialization of Dwarf_info_reader, for reading dwo_ids and | |
631 | // type signatures from DWARF CUs and TUs. | |
632 | ||
633 | class Dwo_id_info_reader : public Dwarf_info_reader | |
634 | { | |
635 | public: | |
636 | Dwo_id_info_reader(bool is_type_unit, | |
637 | Relobj* object, | |
638 | const unsigned char* symbols, | |
639 | off_t symbols_size, | |
640 | unsigned int shndx, | |
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) | |
646 | { } | |
647 | ||
648 | ~Dwo_id_info_reader() | |
649 | { } | |
650 | ||
651 | // Return the dwo_id from a DWARF compilation unit DIE in *DWO_ID. | |
652 | bool | |
653 | get_dwo_id(uint64_t* dwo_id) | |
654 | { | |
655 | this->parse(); | |
656 | if (!this->dwo_id_found_) | |
657 | return false; | |
658 | *dwo_id = this->dwo_id_; | |
659 | return true; | |
660 | } | |
661 | ||
662 | // Return the type signature from a DWARF type unit DIE in *TYPE_SIG. | |
663 | bool | |
664 | get_type_sig(uint64_t* type_sig) | |
665 | { | |
666 | this->parse(); | |
667 | if (!this->type_sig_found_) | |
668 | return false; | |
669 | *type_sig = this->type_sig_; | |
670 | return true; | |
671 | } | |
672 | ||
673 | protected: | |
674 | // Visit a compilation unit. | |
675 | virtual void | |
676 | visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*); | |
677 | ||
678 | // Visit a type unit. | |
679 | virtual void | |
680 | visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature, | |
681 | Dwarf_die*); | |
682 | ||
683 | private: | |
684 | // Visit a top-level DIE. | |
685 | void | |
686 | visit_top_die(Dwarf_die* die); | |
687 | ||
688 | // TRUE if we found a dwo_id. | |
689 | bool dwo_id_found_; | |
690 | // The dwo_id. | |
691 | uint64_t dwo_id_; | |
692 | // TRUE if we found a type signature. | |
693 | bool type_sig_found_; | |
694 | // The type signature. | |
695 | uint64_t type_sig_; | |
696 | }; | |
697 | ||
698 | // Class Sized_relobj_dwo. | |
699 | ||
700 | // Setup the section information. | |
701 | ||
702 | template <int size, bool big_endian> | |
703 | void | |
704 | Sized_relobj_dwo<size, big_endian>::setup() | |
705 | { | |
706 | const unsigned int shnum = this->elf_file_.shnum(); | |
707 | this->set_shnum(shnum); | |
708 | this->section_offsets().resize(shnum); | |
709 | } | |
710 | ||
711 | // Return a view of the contents of a section. | |
712 | ||
713 | template <int size, bool big_endian> | |
714 | const unsigned char* | |
715 | Sized_relobj_dwo<size, big_endian>::do_section_contents( | |
716 | unsigned int shndx, | |
717 | section_size_type* plen, | |
718 | bool cache) | |
719 | { | |
720 | Object::Location loc(this->elf_file_.section_contents(shndx)); | |
721 | *plen = convert_to_section_size_type(loc.data_size); | |
722 | if (*plen == 0) | |
723 | { | |
724 | static const unsigned char empty[1] = { '\0' }; | |
725 | return empty; | |
726 | } | |
727 | return this->get_view(loc.file_offset, *plen, true, cache); | |
728 | } | |
729 | ||
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 | |
732 | // by the caller. | |
733 | ||
734 | template <int size, bool big_endian> | |
735 | const unsigned char* | |
736 | Sized_relobj_dwo<size, big_endian>::do_decompressed_section_contents( | |
737 | unsigned int shndx, | |
738 | section_size_type* plen, | |
739 | bool* is_new) | |
740 | { | |
741 | section_size_type buffer_size; | |
742 | const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size, | |
743 | false); | |
744 | ||
745 | std::string sect_name = this->do_section_name(shndx); | |
746 | if (!is_prefix_of(".zdebug_", sect_name.c_str())) | |
747 | { | |
748 | *plen = buffer_size; | |
749 | *is_new = false; | |
750 | return buffer; | |
751 | } | |
752 | ||
753 | section_size_type uncompressed_size = get_uncompressed_size(buffer, | |
754 | buffer_size); | |
755 | unsigned char* uncompressed_data = new unsigned char[uncompressed_size]; | |
756 | if (!decompress_input_section(buffer, | |
757 | buffer_size, | |
758 | uncompressed_data, | |
759 | uncompressed_size)) | |
760 | this->error(_("could not decompress section %s"), | |
761 | this->section_name(shndx).c_str()); | |
762 | *plen = uncompressed_size; | |
763 | *is_new = true; | |
764 | return uncompressed_data; | |
765 | } | |
766 | ||
767 | // Class Dwo_file. | |
768 | ||
769 | Dwo_file::~Dwo_file() | |
770 | { | |
771 | if (this->input_file_ != NULL) | |
772 | delete this->input_file_; | |
773 | if (this->obj_ != NULL) | |
774 | delete this->obj_; | |
775 | } | |
776 | ||
777 | // Read the input file and send its contents to OUTPUT_FILE. | |
778 | ||
779 | void | |
780 | Dwo_file::read(Dwp_output_file* output_file) | |
781 | { | |
782 | // Open the input file. | |
783 | this->input_file_ = new Input_file(this->name_); | |
784 | Dirsearch dirpath; | |
785 | int index; | |
786 | if (!this->input_file_->open(dirpath, NULL, &index)) | |
787 | gold_fatal(_("%s: can't open"), this->name_); | |
788 | ||
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) | |
793 | hdrsize = filesize; | |
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_); | |
798 | ||
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. | |
802 | int size; | |
803 | bool big_endian; | |
804 | std::string error; | |
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()); | |
808 | ||
809 | this->obj_ = this->make_object(size, big_endian, p, this->input_file_, | |
810 | output_file); | |
811 | ||
812 | unsigned int shnum = this->shnum(); | |
813 | this->is_compressed_.resize(shnum); | |
814 | this->shndx_map_.resize(shnum); | |
815 | ||
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; | |
828 | ||
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++) | |
832 | { | |
833 | if (this->section_type(i) != elfcpp::SHT_PROGBITS) | |
834 | continue; | |
835 | std::string sect_name = this->section_name(i); | |
836 | const char* suffix = sect_name.c_str(); | |
837 | if (is_prefix_of(".debug_", suffix)) | |
838 | suffix += 7; | |
839 | else if (is_prefix_of(".zdebug_", suffix)) | |
840 | { | |
841 | this->is_compressed_[i] = true; | |
842 | suffix += 8; | |
843 | } | |
844 | else | |
845 | continue; | |
846 | if (strcmp(suffix, "cu_index") == 0) | |
847 | debug_cu_index = i; | |
848 | else if (strcmp(suffix, "tu_index") == 0) | |
849 | debug_tu_index = i; | |
850 | else if (strcmp(suffix, "str.dwo") == 0) | |
851 | debug_str = i; | |
852 | } | |
853 | ||
854 | // Merge the input string table into the output string table. | |
855 | this->add_strings(output_file, debug_str); | |
856 | ||
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) | |
860 | { | |
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); | |
865 | return; | |
866 | } | |
867 | ||
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++) | |
871 | { | |
872 | if (this->section_type(i) != elfcpp::SHT_PROGBITS) | |
873 | continue; | |
874 | std::string sect_name = this->section_name(i); | |
875 | const char* suffix = sect_name.c_str(); | |
876 | if (is_prefix_of(".debug_", suffix)) | |
877 | suffix += 7; | |
878 | else if (is_prefix_of(".zdebug_", suffix)) | |
879 | suffix += 8; | |
880 | else | |
881 | continue; | |
882 | // TODO: Check for one of each section (except .debug_types). | |
883 | if (strcmp(suffix, "info.dwo") == 0) | |
884 | debug_info = i; | |
885 | else if (strcmp(suffix, "types.dwo") == 0) | |
886 | debug_types.push_back(i); | |
887 | else if (strcmp(suffix, "abbrev.dwo") == 0) | |
888 | debug_abbrev = i; | |
889 | else if (strcmp(suffix, "line.dwo") == 0) | |
890 | debug_line = i; | |
891 | else if (strcmp(suffix, "loc.dwo") == 0) | |
892 | debug_loc = i; | |
893 | else if (strcmp(suffix, "str_offsets.dwo") == 0) | |
894 | debug_str_offsets = i; | |
895 | else if (strcmp(suffix, "macinfo.dwo") == 0) | |
896 | debug_macinfo = i; | |
897 | else if (strcmp(suffix, "macro.dwo") == 0) | |
898 | debug_macro = i; | |
899 | } | |
900 | ||
901 | if (debug_info > 0) | |
902 | { | |
903 | // Extract the dwo_id from .debug_info.dwo section. | |
904 | uint64_t dwo_id; | |
905 | Dwo_id_info_reader dwarf_reader(false, this->obj_, NULL, 0, debug_info, | |
906 | 0, 0); | |
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); | |
914 | } | |
915 | ||
916 | for (Types_list::const_iterator tp = debug_types.begin(); | |
917 | tp != debug_types.end(); | |
918 | ++tp) | |
919 | { | |
920 | // Extract the type signature from .debug_types.dwo section. | |
921 | uint64_t type_sig; | |
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"), | |
927 | this->name_); | |
928 | this->add_tu_set(output_file, type_sig, *tp, debug_abbrev, debug_line, | |
929 | debug_str_offsets); | |
930 | } | |
931 | } | |
932 | ||
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 | |
935 | // in memory. | |
936 | ||
937 | Relobj* | |
938 | Dwo_file::make_object(int size, bool big_endian, const unsigned char* p, | |
939 | Input_file* input_file, Dwp_output_file* output_file) | |
940 | { | |
941 | if (size == 32) | |
942 | { | |
943 | if (big_endian) | |
944 | #ifdef HAVE_TARGET_32_BIG | |
945 | return this->sized_make_object<32, true>(p, input_file, output_file); | |
946 | #else | |
947 | gold_unreachable(); | |
948 | #endif | |
949 | else | |
950 | #ifdef HAVE_TARGET_32_LITTLE | |
951 | return this->sized_make_object<32, false>(p, input_file, output_file); | |
952 | #else | |
953 | gold_unreachable(); | |
954 | #endif | |
955 | } | |
956 | else if (size == 64) | |
957 | { | |
958 | if (big_endian) | |
959 | #ifdef HAVE_TARGET_64_BIG | |
960 | return this->sized_make_object<64, true>(p, input_file, output_file); | |
961 | #else | |
962 | gold_unreachable(); | |
963 | #endif | |
964 | else | |
965 | #ifdef HAVE_TARGET_64_LITTLE | |
966 | return this->sized_make_object<64, false>(p, input_file, output_file); | |
967 | #else | |
968 | gold_unreachable(); | |
969 | #endif | |
970 | } | |
971 | else | |
972 | gold_unreachable(); | |
973 | } | |
974 | ||
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. | |
977 | ||
978 | template <int size, bool big_endian> | |
979 | Relobj* | |
980 | Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file, | |
981 | Dwp_output_file* output_file) | |
982 | { | |
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); | |
986 | obj->setup(); | |
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]); | |
991 | return obj; | |
992 | } | |
993 | ||
994 | // Read the .debug_cu_index section of a .dwp file, | |
995 | // and process the CU sets. | |
996 | ||
997 | void | |
998 | Dwo_file::read_compunit_index(unsigned int shndx, Dwp_output_file* output_file) | |
999 | { | |
1000 | if (this->obj_->is_big_endian()) | |
1001 | this->sized_read_compunit_index<true>(shndx, output_file); | |
1002 | else | |
1003 | this->sized_read_compunit_index<false>(shndx, output_file); | |
1004 | } | |
1005 | ||
1006 | template <bool big_endian> | |
1007 | void | |
1008 | Dwo_file::sized_read_compunit_index(unsigned int shndx, | |
1009 | Dwp_output_file* output_file) | |
1010 | { | |
1011 | section_size_type len; | |
1012 | bool is_new; | |
1013 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1014 | ||
1015 | unsigned int version = | |
1016 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents); | |
1017 | if (version != 1) | |
1018 | gold_fatal(_("%s: .debug_cu_index has unsupported version number %d"), | |
1019 | this->name_, version); | |
1020 | ||
1021 | unsigned int nused = | |
1022 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1023 | + 2 * sizeof(uint32_t)); | |
1024 | if (nused == 0) | |
1025 | return; | |
1026 | ||
1027 | unsigned int nslots = | |
1028 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1029 | + 3 * sizeof(uint32_t)); | |
1030 | ||
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; | |
1035 | ||
1036 | if (shndx_pool >= limit) | |
1037 | gold_fatal(_("%s: .debug_cu_index is corrupt"), this->name_); | |
1038 | ||
1039 | // Loop over the slots of the hash table. | |
1040 | for (unsigned int i = 0; i < nslots; ++i) | |
1041 | { | |
1042 | uint64_t dwo_id = | |
1043 | elfcpp::Swap_unaligned<64, big_endian>::readval(phash); | |
1044 | if (dwo_id != 0) | |
1045 | { | |
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); | |
1050 | ||
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; | |
1059 | for (;;) | |
1060 | { | |
1061 | if (shndx_list >= limit) | |
1062 | gold_fatal(_("%s: .debug_cu_index is corrupt"), | |
1063 | this->name_); | |
1064 | unsigned int shndx = | |
1065 | elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list); | |
1066 | if (shndx == 0) | |
1067 | break; | |
1068 | if (shndx > this->shnum()) | |
1069 | gold_fatal(_("%s: .debug_cu_index has bad shndx"), | |
1070 | this->name_); | |
1071 | std::string sect_name = this->section_name(shndx); | |
1072 | const char* suffix = sect_name.c_str(); | |
1073 | if (is_prefix_of(".debug_", suffix)) | |
1074 | suffix += 7; | |
1075 | else if (is_prefix_of(".zdebug_", suffix)) | |
1076 | suffix += 8; | |
1077 | else | |
1078 | gold_fatal(_("%s: .debug_cu_index refers to " | |
1079 | "non-debug section"), this->name_); | |
1080 | if (strcmp(suffix, "info.dwo") == 0) | |
1081 | debug_info = shndx; | |
1082 | else if (strcmp(suffix, "abbrev.dwo") == 0) | |
1083 | debug_abbrev = shndx; | |
1084 | else if (strcmp(suffix, "line.dwo") == 0) | |
1085 | debug_line = shndx; | |
1086 | else if (strcmp(suffix, "loc.dwo") == 0) | |
1087 | debug_loc = shndx; | |
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); | |
1095 | } | |
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); | |
1099 | } | |
1100 | phash += sizeof(uint64_t); | |
1101 | pindex += sizeof(uint32_t); | |
1102 | } | |
1103 | ||
1104 | if (is_new) | |
1105 | delete[] contents; | |
1106 | } | |
1107 | ||
1108 | // Read the .debug_tu_index section of a .dwp file, | |
1109 | // and process the TU sets. | |
1110 | ||
1111 | void | |
1112 | Dwo_file::read_typeunit_index(unsigned int shndx, Dwp_output_file* output_file) | |
1113 | { | |
1114 | if (this->obj_->is_big_endian()) | |
1115 | this->sized_read_typeunit_index<true>(shndx, output_file); | |
1116 | else | |
1117 | this->sized_read_typeunit_index<false>(shndx, output_file); | |
1118 | } | |
1119 | ||
1120 | template <bool big_endian> | |
1121 | void | |
1122 | Dwo_file::sized_read_typeunit_index(unsigned int shndx, | |
1123 | Dwp_output_file* output_file) | |
1124 | { | |
1125 | section_size_type len; | |
1126 | bool is_new; | |
1127 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1128 | ||
1129 | unsigned int version = | |
1130 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents); | |
1131 | if (version != 1) | |
1132 | gold_fatal(_("%s: .debug_tu_index has unsupported version number %d"), | |
1133 | this->name_, version); | |
1134 | ||
1135 | unsigned int nused = | |
1136 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1137 | + 2 * sizeof(uint32_t)); | |
1138 | if (nused == 0) | |
1139 | return; | |
1140 | ||
1141 | unsigned int nslots = | |
1142 | elfcpp::Swap_unaligned<32, big_endian>::readval(contents | |
1143 | + 3 * sizeof(uint32_t)); | |
1144 | ||
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; | |
1149 | ||
1150 | if (shndx_pool >= limit) | |
1151 | gold_fatal(_("%s: .debug_tu_index is corrupt"), this->name_); | |
1152 | ||
1153 | // Loop over the slots of the hash table. | |
1154 | for (unsigned int i = 0; i < nslots; ++i) | |
1155 | { | |
1156 | uint64_t type_sig = | |
1157 | elfcpp::Swap_unaligned<64, big_endian>::readval(phash); | |
1158 | if (type_sig != 0) | |
1159 | { | |
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); | |
1164 | ||
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; | |
1170 | for (;;) | |
1171 | { | |
1172 | if (shndx_list >= limit) | |
1173 | gold_fatal(_("%s: .debug_tu_index is corrupt"), | |
1174 | this->name_); | |
1175 | unsigned int shndx = | |
1176 | elfcpp::Swap_unaligned<32, big_endian>::readval(shndx_list); | |
1177 | if (shndx == 0) | |
1178 | break; | |
1179 | if (shndx > this->shnum()) | |
1180 | gold_fatal(_("%s: .debug_tu_index has bad shndx"), | |
1181 | this->name_); | |
1182 | std::string sect_name = this->section_name(shndx); | |
1183 | const char* suffix = sect_name.c_str(); | |
1184 | if (is_prefix_of(".debug_", suffix)) | |
1185 | suffix += 7; | |
1186 | else if (is_prefix_of(".zdebug_", suffix)) | |
1187 | suffix += 8; | |
1188 | else | |
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) | |
1196 | debug_line = shndx; | |
1197 | else if (strcmp(suffix, "str_offsets.dwo") == 0) | |
1198 | debug_str_offsets = shndx; | |
1199 | shndx_list += sizeof(uint32_t); | |
1200 | } | |
1201 | this->add_tu_set(output_file, type_sig, debug_types, debug_abbrev, | |
1202 | debug_line, debug_str_offsets); | |
1203 | } | |
1204 | phash += sizeof(uint64_t); | |
1205 | pindex += sizeof(uint32_t); | |
1206 | } | |
1207 | ||
1208 | if (is_new) | |
1209 | delete[] contents; | |
1210 | } | |
1211 | ||
1212 | // Merge the input string table section into the output file. | |
1213 | ||
1214 | void | |
1215 | Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str) | |
1216 | { | |
1217 | section_size_type len; | |
1218 | bool is_new; | |
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; | |
1222 | ||
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"), | |
1227 | this->name_, | |
1228 | this->section_name(debug_str).c_str()); | |
1229 | ||
1230 | // Count the number of strings in the section, and size the map. | |
1231 | size_t count = 0; | |
1232 | for (const char* pt = p; pt < pend; pt += strlen(pt) + 1) | |
1233 | ++count; | |
1234 | this->str_offset_map_.reserve(count + 1); | |
1235 | ||
1236 | // Add the strings to the output string table, and record the new offsets | |
1237 | // in the map. | |
1238 | section_offset_type i = 0; | |
1239 | section_offset_type new_offset; | |
1240 | while (p < pend) | |
1241 | { | |
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)); | |
1245 | p += len + 1; | |
1246 | i += len + 1; | |
1247 | } | |
1248 | new_offset = 0; | |
1249 | this->str_offset_map_.push_back(std::make_pair(i, new_offset)); | |
1250 | if (is_new) | |
1251 | delete[] pdata; | |
1252 | } | |
1253 | ||
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. | |
1257 | ||
1258 | unsigned int | |
1259 | Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx, | |
1260 | const char* section_name, bool is_str_offsets) | |
1261 | { | |
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]; | |
1266 | ||
1267 | section_size_type len; | |
1268 | bool is_new; | |
1269 | const unsigned char* contents = this->section_contents(shndx, &len, &is_new); | |
1270 | ||
1271 | if (is_str_offsets) | |
1272 | { | |
1273 | const unsigned char* remapped = this->remap_str_offsets(contents, len); | |
1274 | if (is_new) | |
1275 | delete[] contents; | |
1276 | contents = remapped; | |
1277 | is_new = true; | |
1278 | } | |
1279 | ||
1280 | this->shndx_map_[shndx] = output_file->add_section(section_name, contents, | |
1281 | len, 1); | |
1282 | if (is_new) | |
1283 | delete[] contents; | |
1284 | ||
1285 | return this->shndx_map_[shndx]; | |
1286 | } | |
1287 | ||
1288 | // Remap the | |
1289 | const unsigned char* | |
1290 | Dwo_file::remap_str_offsets(const unsigned char* contents, | |
1291 | section_size_type len) | |
1292 | { | |
1293 | if ((len & 3) != 0) | |
1294 | gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"), | |
1295 | this->name_); | |
1296 | ||
1297 | if (this->obj_->is_big_endian()) | |
1298 | return this->sized_remap_str_offsets<true>(contents, len); | |
1299 | else | |
1300 | return this->sized_remap_str_offsets<false>(contents, len); | |
1301 | } | |
1302 | ||
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) | |
1307 | { | |
1308 | unsigned char* remapped = new unsigned char[len]; | |
1309 | const unsigned char* p = contents; | |
1310 | unsigned char* q = remapped; | |
1311 | while (len > 0) | |
1312 | { | |
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); | |
1316 | len -= 4; | |
1317 | p += 4; | |
1318 | q += 4; | |
1319 | } | |
1320 | return remapped; | |
1321 | } | |
1322 | ||
1323 | unsigned int | |
598c7410 | 1324 | Dwo_file::remap_str_offset(section_offset_type val) |
77429909 CC |
1325 | { |
1326 | Str_offset_map_entry entry; | |
1327 | entry.first = val; | |
1328 | ||
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()); | |
1333 | ||
1334 | if (p == this->str_offset_map_.end() || p->first > val) | |
1335 | { | |
1336 | if (p == this->str_offset_map_.begin()) | |
1337 | return 0; | |
1338 | --p; | |
1339 | gold_assert(p->first <= val); | |
1340 | } | |
1341 | ||
1342 | return p->second + (val - p->first); | |
1343 | } | |
1344 | ||
1345 | // Add a set of .debug_info and related sections to OUTPUT_FILE. | |
1346 | ||
1347 | void | |
1348 | Dwo_file::add_cu_set(Dwp_output_file* output_file, | |
1349 | uint64_t dwo_id, | |
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) | |
1357 | { | |
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_); | |
1362 | ||
1363 | debug_abbrev = this->copy_section(output_file, debug_abbrev, | |
1364 | ".debug_abbrev.dwo", false); | |
1365 | if (debug_line > 0) | |
1366 | debug_line = this->copy_section(output_file, debug_line, | |
1367 | ".debug_line.dwo", false); | |
1368 | if (debug_loc > 0) | |
1369 | debug_loc = this->copy_section(output_file, debug_loc, ".debug_loc.dwo", | |
1370 | false); | |
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); | |
1377 | ||
1378 | if (debug_str_offsets > 0) | |
1379 | debug_str_offsets = this->copy_section(output_file, debug_str_offsets, | |
1380 | ".debug_str_offsets.dwo", true); | |
1381 | ||
1382 | debug_info = this->copy_section(output_file, debug_info, ".debug_info.dwo", | |
1383 | false); | |
1384 | ||
1385 | output_file->add_cu_set(dwo_id, debug_info, debug_abbrev, debug_line, | |
1386 | debug_loc, debug_str_offsets, debug_macinfo, | |
1387 | debug_macro); | |
1388 | } | |
1389 | ||
1390 | // Add a set of .debug_types and related sections to OUTPUT_FILE. | |
1391 | ||
1392 | void | |
1393 | Dwo_file::add_tu_set(Dwp_output_file* output_file, | |
1394 | uint64_t type_sig, | |
1395 | unsigned int debug_types, | |
1396 | unsigned int debug_abbrev, | |
1397 | unsigned int debug_line, | |
1398 | unsigned int debug_str_offsets) | |
1399 | { | |
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_); | |
1404 | ||
1405 | // Ignore duplicate type signatures. | |
1406 | if (output_file->lookup_tu(type_sig)) | |
1407 | return; | |
1408 | ||
1409 | debug_abbrev = this->copy_section(output_file, debug_abbrev, | |
1410 | ".debug_abbrev.dwo", false); | |
1411 | if (debug_line > 0) | |
1412 | debug_line = this->copy_section(output_file, debug_line, | |
1413 | ".debug_line.dwo", false); | |
1414 | ||
1415 | if (debug_str_offsets > 0) | |
1416 | debug_str_offsets = this->copy_section(output_file, debug_str_offsets, | |
1417 | ".debug_str_offsets.dwo", true); | |
1418 | ||
1419 | debug_types = this->copy_section(output_file, debug_types, | |
1420 | ".debug_types.dwo", false); | |
1421 | ||
1422 | output_file->add_tu_set(type_sig, debug_types, debug_abbrev, debug_line, | |
1423 | debug_str_offsets); | |
1424 | } | |
1425 | ||
1426 | // Class Dwp_output_file. | |
1427 | ||
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. | |
1431 | ||
1432 | void | |
1433 | Dwp_output_file::record_target_info(const char*, int machine, | |
1434 | int size, bool big_endian, | |
1435 | int osabi, int abiversion) | |
1436 | { | |
1437 | // TODO: Check the values on subsequent calls. | |
1438 | if (this->size_ > 0) | |
1439 | return; | |
1440 | ||
1441 | this->machine_ = machine; | |
1442 | this->size_ = size; | |
1443 | this->big_endian_ = big_endian; | |
1444 | this->osabi_ = osabi; | |
1445 | this->abiversion_ = abiversion; | |
1446 | ||
1447 | if (size == 32) | |
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; | |
1451 | else | |
1452 | gold_unreachable(); | |
1453 | ||
1454 | this->fd_ = ::fopen(this->name_, "wb"); | |
1455 | if (this->fd_ == NULL) | |
1456 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
1457 | ||
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 }; | |
d361fafb L |
1461 | if (::fwrite(buf, 1, this->next_file_offset_, this->fd_) |
1462 | < (size_t) this->next_file_offset_) | |
1463 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
77429909 CC |
1464 | } |
1465 | ||
1466 | // Add a string to the debug strings section. | |
1467 | ||
1468 | section_offset_type | |
1469 | Dwp_output_file::add_string(const char* str, size_t len) | |
1470 | { | |
1471 | Stringpool::Key key; | |
1472 | this->stringpool_.add_with_length(str, len, true, &key); | |
1473 | this->have_strings_ = true; | |
1474 | // We aren't supposed to call get_offset() until after | |
1475 | // calling set_string_offsets(), but the offsets will | |
1476 | // not change unless optimizing the string pool. | |
1477 | return this->stringpool_.get_offset_from_key(key); | |
1478 | } | |
1479 | ||
1480 | // Align the file offset to the given boundary. | |
1481 | ||
1482 | static inline off_t | |
1483 | align_offset(off_t off, int align) | |
1484 | { | |
1485 | return (off + align - 1) & ~(align - 1); | |
1486 | } | |
1487 | ||
1488 | // Add a section to the output file, and return the new section index. | |
1489 | ||
1490 | unsigned int | |
1491 | Dwp_output_file::add_section(const char* section_name, | |
1492 | const unsigned char* contents, | |
1493 | section_size_type len, | |
1494 | int align) | |
1495 | { | |
1496 | off_t file_offset = this->next_file_offset_; | |
1497 | gold_assert(this->size_ > 0 && file_offset > 0); | |
1498 | ||
1499 | file_offset = align_offset(file_offset, align); | |
1500 | ||
1501 | ::fseek(this->fd_, file_offset, SEEK_SET); | |
1502 | if (::fwrite(contents, 1, len, this->fd_) < len) | |
1503 | gold_fatal(_("%s: error writing section '%s'"), this->name_, section_name); | |
1504 | ||
1505 | section_name = this->shstrtab_.add_with_length(section_name, | |
1506 | strlen(section_name), | |
1507 | false, NULL); | |
1508 | Section sect = { section_name, file_offset, len, align }; | |
1509 | this->sections_.push_back(sect); | |
1510 | ||
1511 | this->next_file_offset_ = file_offset + len; | |
1512 | return this->shnum_++; | |
1513 | } | |
1514 | ||
1515 | // Add a set of .debug_info and related sections to the output file. | |
1516 | ||
1517 | void | |
1518 | Dwp_output_file::add_cu_set(uint64_t dwo_id, | |
1519 | unsigned int debug_info, | |
1520 | unsigned int debug_abbrev, | |
1521 | unsigned int debug_line, | |
1522 | unsigned int debug_loc, | |
1523 | unsigned int debug_str_offsets, | |
1524 | unsigned int debug_macinfo, | |
1525 | unsigned int debug_macro) | |
1526 | { | |
1527 | Cu_or_tu_set cu_set = { dwo_id, debug_info, debug_abbrev, debug_line, | |
1528 | debug_loc, debug_str_offsets, debug_macinfo, | |
1529 | debug_macro }; | |
1530 | unsigned int slot; | |
1531 | this->cu_index_.find_or_add(dwo_id, &slot); | |
1532 | this->cu_index_.enter_set(slot, cu_set); | |
1533 | } | |
1534 | ||
1535 | // Lookup a type signature and return TRUE if we have already seen it. | |
1536 | bool | |
1537 | Dwp_output_file::lookup_tu(uint64_t type_sig) | |
1538 | { | |
1539 | this->last_type_sig_ = type_sig; | |
1540 | return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_); | |
1541 | } | |
1542 | ||
1543 | // Add a set of .debug_types and related sections to the output file. | |
1544 | ||
1545 | void | |
1546 | Dwp_output_file::add_tu_set(uint64_t type_sig, | |
1547 | unsigned int debug_types, | |
1548 | unsigned int debug_abbrev, | |
1549 | unsigned int debug_line, | |
1550 | unsigned int debug_str_offsets) | |
1551 | { | |
1552 | Cu_or_tu_set tu_set = { type_sig, debug_types, debug_abbrev, debug_line, | |
1553 | 0, debug_str_offsets, 0, 0 }; | |
1554 | unsigned int slot; | |
1555 | if (type_sig == this->last_type_sig_) | |
1556 | slot = this->last_tu_slot_; | |
1557 | else | |
1558 | this->tu_index_.find_or_add(type_sig, &slot); | |
1559 | this->tu_index_.enter_set(slot, tu_set); | |
1560 | } | |
1561 | ||
1562 | // Find a slot in the hash table for SIGNATURE. Return TRUE | |
1563 | // if the entry already exists. | |
1564 | ||
1565 | bool | |
1566 | Dwp_output_file::Dwp_index::find_or_add(uint64_t signature, | |
1567 | unsigned int* slotp) | |
1568 | { | |
1569 | if (this->capacity_ == 0) | |
1570 | this->initialize(); | |
1571 | unsigned int slot = | |
1572 | static_cast<unsigned int>(signature) & (this->capacity_ - 1); | |
1573 | unsigned int secondary_hash; | |
1574 | uint64_t probe = this->hash_table_[slot]; | |
1575 | if (probe != 0 && probe != signature) | |
1576 | { | |
1577 | secondary_hash = (static_cast<unsigned int>(signature >> 32) | |
1578 | & (this->capacity_ - 1)) | 1; | |
1579 | do | |
1580 | { | |
1581 | slot = (slot + secondary_hash) & (this->capacity_ - 1); | |
1582 | probe = this->hash_table_[slot]; | |
1583 | } while (probe != 0 && probe != signature); | |
1584 | } | |
1585 | *slotp = slot; | |
1586 | return (probe != 0); | |
1587 | } | |
1588 | ||
1589 | // Enter a CU or TU set at the given SLOT in the hash table. | |
1590 | ||
1591 | void | |
1592 | Dwp_output_file::Dwp_index::enter_set(unsigned int slot, | |
1593 | const Cu_or_tu_set& set) | |
1594 | { | |
1595 | gold_assert(slot < this->capacity_); | |
1596 | gold_assert(set.debug_info_or_types > 0); | |
1597 | gold_assert(set.debug_abbrev > 0); | |
1598 | ||
1599 | // Add the section indexes to the pool. | |
1600 | uint32_t pool_index = this->shndx_pool_.size(); | |
1601 | this->shndx_pool_.push_back(set.debug_info_or_types); | |
1602 | this->shndx_pool_.push_back(set.debug_abbrev); | |
1603 | if (set.debug_line > 0) | |
1604 | this->shndx_pool_.push_back(set.debug_line); | |
1605 | if (set.debug_loc > 0) | |
1606 | this->shndx_pool_.push_back(set.debug_loc); | |
1607 | if (set.debug_str_offsets > 0) | |
1608 | this->shndx_pool_.push_back(set.debug_str_offsets); | |
1609 | if (set.debug_macinfo > 0) | |
1610 | this->shndx_pool_.push_back(set.debug_macinfo); | |
1611 | if (set.debug_macro > 0) | |
1612 | this->shndx_pool_.push_back(set.debug_macro); | |
1613 | this->shndx_pool_.push_back(0); | |
1614 | ||
1615 | // Enter the signature and pool index into the hash table. | |
1616 | this->hash_table_[slot] = set.signature; | |
1617 | this->index_table_[slot] = pool_index; | |
1618 | ++this->used_; | |
1619 | ||
1620 | // Grow the hash table when we exceed 2/3 capacity. | |
1621 | if (this->used_ * 3 > this->capacity_ * 2) | |
1622 | this->grow(); | |
1623 | } | |
1624 | ||
1625 | // Initialize the hash table. | |
1626 | ||
1627 | void | |
1628 | Dwp_output_file::Dwp_index::initialize() | |
1629 | { | |
1630 | this->capacity_ = 16; | |
1631 | this->hash_table_ = new uint64_t[this->capacity_]; | |
1632 | memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t)); | |
1633 | this->index_table_ = new uint32_t[this->capacity_]; | |
1634 | memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t)); | |
1635 | } | |
1636 | ||
1637 | // Grow the hash table when we reach 2/3 capacity. | |
1638 | ||
1639 | void | |
1640 | Dwp_output_file::Dwp_index::grow() | |
1641 | { | |
1642 | unsigned int old_capacity = this->capacity_; | |
1643 | uint64_t* old_hash_table = this->hash_table_; | |
1644 | uint32_t* old_index_table = this->index_table_; | |
1645 | unsigned int old_used = this->used_; | |
1646 | ||
1647 | this->capacity_ = old_capacity * 2; | |
1648 | this->hash_table_ = new uint64_t[this->capacity_]; | |
1649 | memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t)); | |
1650 | this->index_table_ = new uint32_t[this->capacity_]; | |
1651 | memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t)); | |
1652 | this->used_ = 0; | |
1653 | ||
1654 | for (unsigned int i = 0; i < old_capacity; ++i) | |
1655 | { | |
1656 | uint64_t signature = old_hash_table[i]; | |
1657 | if (signature != 0) | |
1658 | { | |
1659 | unsigned int slot; | |
1660 | bool found = this->find_or_add(signature, &slot); | |
1661 | gold_assert(!found); | |
1662 | this->hash_table_[slot] = signature; | |
1663 | this->index_table_[slot] = old_index_table[i]; | |
1664 | ++this->used_; | |
1665 | } | |
1666 | } | |
1667 | gold_assert(this->used_ == old_used); | |
1668 | ||
1669 | delete[] old_hash_table; | |
1670 | delete[] old_index_table; | |
1671 | } | |
1672 | ||
1673 | // Initialize the output file. | |
1674 | ||
1675 | void | |
1676 | Dwp_output_file::initialize() | |
1677 | { | |
1678 | // We can't initialize the output file until we've recorded the | |
1679 | // target info from the first input file. | |
1680 | gold_assert(this->size_ > 0); | |
1681 | } | |
1682 | ||
1683 | // Finalize the file, write the string tables and index sections, | |
1684 | // and close the file. | |
1685 | ||
1686 | void | |
1687 | Dwp_output_file::finalize() | |
1688 | { | |
1689 | unsigned char* buf; | |
1690 | ||
1691 | // Write the debug string table. | |
1692 | if (this->have_strings_) | |
1693 | { | |
1694 | this->stringpool_.set_string_offsets(); | |
1695 | section_size_type len = this->stringpool_.get_strtab_size(); | |
1696 | buf = new unsigned char[len]; | |
1697 | this->stringpool_.write_to_buffer(buf, len); | |
1698 | this->add_section(".debug_str.dwo", buf, len, 1); | |
1699 | delete[] buf; | |
1700 | } | |
1701 | ||
1702 | // Write the CU and TU indexes. | |
1703 | if (this->big_endian_) | |
1704 | { | |
1705 | this->write_index<true>(".debug_cu_index", this->cu_index_); | |
1706 | this->write_index<true>(".debug_tu_index", this->tu_index_); | |
1707 | } | |
1708 | else | |
1709 | { | |
1710 | this->write_index<false>(".debug_cu_index", this->cu_index_); | |
1711 | this->write_index<false>(".debug_tu_index", this->tu_index_); | |
1712 | } | |
1713 | ||
1714 | off_t file_offset = this->next_file_offset_; | |
1715 | ||
1716 | // Write the section string table. | |
1717 | this->shstrndx_ = this->shnum_++; | |
1718 | const char* shstrtab_name = | |
1719 | this->shstrtab_.add_with_length(".shstrtab", | |
1720 | sizeof(".shstrtab") - 1, | |
1721 | false, NULL); | |
1722 | this->shstrtab_.set_string_offsets(); | |
1723 | section_size_type shstrtab_len = this->shstrtab_.get_strtab_size(); | |
1724 | buf = new unsigned char[shstrtab_len]; | |
1725 | this->shstrtab_.write_to_buffer(buf, shstrtab_len); | |
1726 | off_t shstrtab_off = file_offset; | |
1727 | ::fseek(this->fd_, file_offset, 0); | |
1728 | if (::fwrite(buf, 1, shstrtab_len, this->fd_) < shstrtab_len) | |
1729 | gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_); | |
1730 | delete[] buf; | |
1731 | file_offset += shstrtab_len; | |
1732 | ||
1733 | // Write the section header table. The first entry is a NULL entry. | |
1734 | // This is followed by the debug sections, and finally we write the | |
1735 | // .shstrtab section header. | |
1736 | file_offset = align_offset(file_offset, this->size_ == 32 ? 4 : 8); | |
1737 | this->shoff_ = file_offset; | |
1738 | ::fseek(this->fd_, file_offset, 0); | |
1739 | section_size_type sh0_size = 0; | |
1740 | unsigned int sh0_link = 0; | |
1741 | if (this->shnum_ >= elfcpp::SHN_LORESERVE) | |
1742 | sh0_size = this->shnum_; | |
1743 | if (this->shstrndx_ >= elfcpp::SHN_LORESERVE) | |
1744 | sh0_link = this->shstrndx_; | |
1745 | this->write_shdr(NULL, 0, 0, 0, 0, sh0_size, sh0_link, 0, 0, 0); | |
1746 | for (unsigned int i = 0; i < this->sections_.size(); ++i) | |
1747 | { | |
1748 | Section& sect = this->sections_[i]; | |
1749 | this->write_shdr(sect.name, elfcpp::SHT_PROGBITS, 0, 0, sect.offset, | |
1750 | sect.size, 0, 0, sect.align, 0); | |
1751 | } | |
1752 | this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0, | |
1753 | shstrtab_off, shstrtab_len, 0, 0, 1, 0); | |
1754 | ||
1755 | // Write the ELF header. | |
1756 | this->write_ehdr(); | |
1757 | ||
1758 | // Close the file. | |
1759 | if (this->fd_ != NULL) | |
1760 | { | |
1761 | if (::fclose(this->fd_) != 0) | |
1762 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
1763 | } | |
1764 | this->fd_ = NULL; | |
1765 | } | |
1766 | ||
1767 | // Write a CU or TU index section. | |
1768 | template<bool big_endian> | |
1769 | void | |
1770 | Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index) | |
1771 | { | |
1772 | const unsigned int nslots = index.hash_table_total_slots(); | |
1773 | const unsigned int nused = index.hash_table_used_slots(); | |
1774 | const unsigned int npool = index.shndx_pool_size(); | |
1775 | const section_size_type index_size = (4 * sizeof(uint32_t) | |
1776 | + nslots * sizeof(uint64_t) | |
1777 | + nslots * sizeof(uint32_t) | |
1778 | + npool * sizeof(uint32_t)); | |
1779 | ||
1780 | // Allocate a buffer for the section contents. | |
1781 | unsigned char* buf = new unsigned char[index_size]; | |
1782 | unsigned char* p = buf; | |
1783 | ||
1784 | // Write the section header: version number, padding, | |
1785 | // number of used slots and total number of slots. | |
1786 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 1); | |
1787 | p += sizeof(uint32_t); | |
1788 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 0); | |
1789 | p += sizeof(uint32_t); | |
1790 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nused); | |
1791 | p += sizeof(uint32_t); | |
1792 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nslots); | |
1793 | p += sizeof(uint32_t); | |
1794 | ||
1795 | // Write the hash table. | |
1796 | for (unsigned int i = 0; i < nslots; ++i) | |
1797 | { | |
1798 | elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i)); | |
1799 | p += sizeof(uint64_t); | |
1800 | } | |
1801 | ||
1802 | // Write the parallel index table. | |
1803 | for (unsigned int i = 0; i < nslots; ++i) | |
1804 | { | |
1805 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i)); | |
1806 | p += sizeof(uint32_t); | |
1807 | } | |
1808 | ||
1809 | // Write the section index pool. | |
1810 | Dwp_index::Shndx_pool::const_iterator pool = index.shndx_pool(); | |
1811 | for (unsigned int i = 0; i < npool; ++i) | |
1812 | { | |
1813 | gold_assert(pool != index.shndx_pool_end()); | |
1814 | elfcpp::Swap_unaligned<32, big_endian>::writeval(p, *pool); | |
1815 | p += sizeof(uint32_t); | |
1816 | ++pool; | |
1817 | } | |
1818 | ||
1819 | gold_assert(p == buf + index_size); | |
1820 | ||
1821 | this->add_section(sect_name, buf, index_size, sizeof(uint64_t)); | |
1822 | ||
1823 | delete[] buf; | |
1824 | } | |
1825 | ||
1826 | // Write the ELF header. | |
1827 | ||
1828 | void | |
1829 | Dwp_output_file::write_ehdr() | |
1830 | { | |
1831 | if (this->size_ == 32) | |
1832 | { | |
1833 | if (this->big_endian_) | |
1834 | return this->sized_write_ehdr<32, true>(); | |
1835 | else | |
1836 | return this->sized_write_ehdr<32, false>(); | |
1837 | } | |
1838 | else if (this->size_ == 64) | |
1839 | { | |
1840 | if (this->big_endian_) | |
1841 | return this->sized_write_ehdr<64, true>(); | |
1842 | else | |
1843 | return this->sized_write_ehdr<64, false>(); | |
1844 | } | |
1845 | else | |
1846 | gold_unreachable(); | |
1847 | } | |
1848 | ||
1849 | template<unsigned int size, bool big_endian> | |
1850 | void | |
1851 | Dwp_output_file::sized_write_ehdr() | |
1852 | { | |
1853 | const unsigned int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
1854 | unsigned char buf[ehdr_size]; | |
1855 | elfcpp::Ehdr_write<size, big_endian> ehdr(buf); | |
1856 | ||
1857 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
1858 | memset(e_ident, 0, elfcpp::EI_NIDENT); | |
1859 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; | |
1860 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; | |
1861 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; | |
1862 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; | |
1863 | if (size == 32) | |
1864 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
1865 | else if (size == 64) | |
1866 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
1867 | else | |
1868 | gold_unreachable(); | |
1869 | e_ident[elfcpp::EI_DATA] = (big_endian | |
1870 | ? elfcpp::ELFDATA2MSB | |
1871 | : elfcpp::ELFDATA2LSB); | |
1872 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; | |
1873 | ehdr.put_e_ident(e_ident); | |
1874 | ||
1875 | ehdr.put_e_type(elfcpp::ET_REL); | |
1876 | ehdr.put_e_machine(this->machine_); | |
1877 | ehdr.put_e_version(elfcpp::EV_CURRENT); | |
1878 | ehdr.put_e_entry(0); | |
1879 | ehdr.put_e_phoff(0); | |
1880 | ehdr.put_e_shoff(this->shoff_); | |
1881 | ehdr.put_e_flags(0); | |
1882 | ehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); | |
1883 | ehdr.put_e_phentsize(0); | |
1884 | ehdr.put_e_phnum(0); | |
1885 | ehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); | |
1886 | ehdr.put_e_shnum(this->shnum_ < elfcpp::SHN_LORESERVE ? this->shnum_ : 0); | |
1887 | ehdr.put_e_shstrndx(this->shstrndx_ < elfcpp::SHN_LORESERVE | |
1888 | ? this->shstrndx_ | |
1889 | : static_cast<unsigned int>(elfcpp::SHN_XINDEX)); | |
1890 | ||
1891 | ::fseek(this->fd_, 0, 0); | |
1892 | if (::fwrite(buf, 1, ehdr_size, this->fd_) < ehdr_size) | |
1893 | gold_fatal(_("%s: error writing ELF header"), this->name_); | |
1894 | } | |
1895 | ||
1896 | // Write a section header. | |
1897 | ||
1898 | void | |
1899 | Dwp_output_file::write_shdr(const char* name, unsigned int type, | |
1900 | unsigned int flags, uint64_t addr, off_t offset, | |
1901 | section_size_type sect_size, unsigned int link, | |
1902 | unsigned int info, unsigned int align, | |
1903 | unsigned int ent_size) | |
1904 | { | |
1905 | if (this->size_ == 32) | |
1906 | { | |
1907 | if (this->big_endian_) | |
1908 | return this->sized_write_shdr<32, true>(name, type, flags, addr, | |
1909 | offset, sect_size, link, info, | |
1910 | align, ent_size); | |
1911 | else | |
1912 | return this->sized_write_shdr<32, false>(name, type, flags, addr, | |
1913 | offset, sect_size, link, info, | |
1914 | align, ent_size); | |
1915 | } | |
1916 | else if (this->size_ == 64) | |
1917 | { | |
1918 | if (this->big_endian_) | |
1919 | return this->sized_write_shdr<64, true>(name, type, flags, addr, | |
1920 | offset, sect_size, link, info, | |
1921 | align, ent_size); | |
1922 | else | |
1923 | return this->sized_write_shdr<64, false>(name, type, flags, addr, | |
1924 | offset, sect_size, link, info, | |
1925 | align, ent_size); | |
1926 | } | |
1927 | else | |
1928 | gold_unreachable(); | |
1929 | } | |
1930 | ||
1931 | template<unsigned int size, bool big_endian> | |
1932 | void | |
1933 | Dwp_output_file::sized_write_shdr(const char* name, unsigned int type, | |
1934 | unsigned int flags, uint64_t addr, | |
1935 | off_t offset, section_size_type sect_size, | |
1936 | unsigned int link, unsigned int info, | |
1937 | unsigned int align, unsigned int ent_size) | |
1938 | { | |
1939 | const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
1940 | unsigned char buf[shdr_size]; | |
1941 | elfcpp::Shdr_write<size, big_endian> shdr(buf); | |
1942 | ||
1943 | shdr.put_sh_name(name == NULL ? 0 : this->shstrtab_.get_offset(name)); | |
1944 | shdr.put_sh_type(type); | |
1945 | shdr.put_sh_flags(flags); | |
1946 | shdr.put_sh_addr(addr); | |
1947 | shdr.put_sh_offset(offset); | |
1948 | shdr.put_sh_size(sect_size); | |
1949 | shdr.put_sh_link(link); | |
1950 | shdr.put_sh_info(info); | |
1951 | shdr.put_sh_addralign(align); | |
1952 | shdr.put_sh_entsize(ent_size); | |
1953 | if (::fwrite(buf, 1, shdr_size, this->fd_) < shdr_size) | |
1954 | gold_fatal(_("%s: error writing section header table"), this->name_); | |
1955 | } | |
1956 | ||
1957 | // Class Dwo_id_info_reader. | |
1958 | ||
1959 | // Visit a compilation unit. | |
1960 | ||
1961 | void | |
1962 | Dwo_id_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die) | |
1963 | { | |
1964 | this->dwo_id_ = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id); | |
1965 | if (this->dwo_id_ != 0) | |
1966 | this->dwo_id_found_ = true; | |
1967 | } | |
1968 | ||
1969 | // Visit a type unit. | |
1970 | ||
1971 | void | |
1972 | Dwo_id_info_reader::visit_type_unit(off_t, off_t, uint64_t signature, Dwarf_die*) | |
1973 | { | |
1974 | this->type_sig_ = signature; | |
1975 | this->type_sig_found_ = true; | |
1976 | } | |
1977 | ||
1978 | }; // End namespace gold | |
1979 | ||
1980 | using namespace gold; | |
1981 | ||
1982 | // Options. | |
1983 | ||
1984 | struct option dwp_options[] = | |
1985 | { | |
1986 | { "verbose", no_argument, NULL, 'v' }, | |
1987 | { "output", required_argument, NULL, 'o' }, | |
1988 | { NULL, 0, NULL, 0 } | |
1989 | }; | |
1990 | ||
1991 | // Print usage message and exit. | |
1992 | ||
1993 | static void | |
1994 | usage() | |
1995 | { | |
1996 | fprintf(stderr, _("Usage: %s [options] file...\n"), program_name); | |
1997 | fprintf(stderr, _(" -v, --verbose Verbose output\n")); | |
1998 | fprintf(stderr, _(" -o FILE, --output FILE Set output dwp file name" | |
1999 | " (required)\n")); | |
2000 | exit(1); | |
2001 | } | |
2002 | ||
2003 | // Main program. | |
2004 | ||
2005 | int | |
2006 | main(int argc, char** argv) | |
2007 | { | |
2008 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
2009 | setlocale(LC_MESSAGES, ""); | |
2010 | #endif | |
2011 | #if defined (HAVE_SETLOCALE) | |
2012 | setlocale(LC_CTYPE, ""); | |
2013 | #endif | |
2014 | bindtextdomain(PACKAGE, LOCALEDIR); | |
2015 | textdomain(PACKAGE); | |
2016 | ||
2017 | program_name = argv[0]; | |
2018 | ||
2019 | // Initialize the global parameters, to let random code get to the | |
2020 | // errors object. | |
2021 | Errors errors(program_name); | |
2022 | set_parameters_errors(&errors); | |
2023 | ||
2024 | // Initialize gold's global options. We don't use these in | |
2025 | // this program, but they need to be initialized so that | |
2026 | // functions we call from libgold work properly. | |
2027 | General_options options; | |
2028 | set_parameters_options(&options); | |
2029 | ||
2030 | // In libiberty; expands @filename to the args in "filename". | |
2031 | expandargv(&argc, &argv); | |
2032 | ||
2033 | // Collect file names and options. | |
2034 | typedef std::vector<char*> File_list; | |
2035 | File_list files; | |
2036 | const char* output_filename = NULL; | |
2037 | bool verbose = false; | |
2038 | int c; | |
2039 | while ((c = getopt_long(argc, argv, "vo:", dwp_options, NULL)) != -1) | |
2040 | { | |
2041 | switch (c) | |
2042 | { | |
2043 | case 'v': | |
2044 | verbose = true; | |
2045 | break; | |
2046 | case 'o': | |
2047 | output_filename = optarg; | |
2048 | break; | |
2049 | case '?': | |
2050 | default: | |
2051 | usage(); | |
2052 | } | |
2053 | } | |
2054 | for (int i = optind; i < argc; ++i) | |
2055 | files.push_back(argv[i]); | |
2056 | ||
2057 | if (files.empty()) | |
2058 | gold_fatal(_("no input files")); | |
2059 | if (output_filename == NULL) | |
2060 | gold_fatal(_("no output file specified")); | |
2061 | ||
2062 | Dwp_output_file output_file(output_filename); | |
2063 | ||
2064 | // Process each file, adding its contents to the output file. | |
2065 | for (File_list::const_iterator f = files.begin(); f != files.end(); ++f) | |
2066 | { | |
2067 | if (verbose) | |
2068 | fprintf(stderr, "%s\n", *f); | |
2069 | Dwo_file dwo_file(*f); | |
2070 | dwo_file.read(&output_file); | |
2071 | } | |
2072 | ||
2073 | output_file.finalize(); | |
2074 | ||
2075 | return EXIT_SUCCESS; | |
2076 | } |