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