Fix PR c++/21323: GDB thinks char16_t and char32_t are signed in C++
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76
77 #include <fcntl.h>
78 #include <sys/types.h>
79 #include <algorithm>
80
81 typedef struct symbol *symbolp;
82 DEF_VEC_P (symbolp);
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When non-zero, cross-check physname against demangler. */
96 static int check_physname = 0;
97
98 /* When non-zero, do not reject deprecated .gdb_index sections. */
99 static int use_deprecated_index_sections = 0;
100
101 static const struct objfile_data *dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* A descriptor for dwarf sections.
111
112 S.ASECTION, SIZE are typically initialized when the objfile is first
113 scanned. BUFFER, READIN are filled in later when the section is read.
114 If the section contained compressed data then SIZE is updated to record
115 the uncompressed size of the section.
116
117 DWP file format V2 introduces a wrinkle that is easiest to handle by
118 creating the concept of virtual sections contained within a real section.
119 In DWP V2 the sections of the input DWO files are concatenated together
120 into one section, but section offsets are kept relative to the original
121 input section.
122 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
123 the real section this "virtual" section is contained in, and BUFFER,SIZE
124 describe the virtual section. */
125
126 struct dwarf2_section_info
127 {
128 union
129 {
130 /* If this is a real section, the bfd section. */
131 asection *section;
132 /* If this is a virtual section, pointer to the containing ("real")
133 section. */
134 struct dwarf2_section_info *containing_section;
135 } s;
136 /* Pointer to section data, only valid if readin. */
137 const gdb_byte *buffer;
138 /* The size of the section, real or virtual. */
139 bfd_size_type size;
140 /* If this is a virtual section, the offset in the real section.
141 Only valid if is_virtual. */
142 bfd_size_type virtual_offset;
143 /* True if we have tried to read this section. */
144 char readin;
145 /* True if this is a virtual section, False otherwise.
146 This specifies which of s.section and s.containing_section to use. */
147 char is_virtual;
148 };
149
150 typedef struct dwarf2_section_info dwarf2_section_info_def;
151 DEF_VEC_O (dwarf2_section_info_def);
152
153 /* All offsets in the index are of this type. It must be
154 architecture-independent. */
155 typedef uint32_t offset_type;
156
157 DEF_VEC_I (offset_type);
158
159 /* Ensure only legit values are used. */
160 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
161 do { \
162 gdb_assert ((unsigned int) (value) <= 1); \
163 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
164 } while (0)
165
166 /* Ensure only legit values are used. */
167 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
168 do { \
169 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
170 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
171 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
175 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
178 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
179 } while (0)
180
181 /* A description of the mapped index. The file format is described in
182 a comment by the code that writes the index. */
183 struct mapped_index
184 {
185 /* Index data format version. */
186 int version;
187
188 /* The total length of the buffer. */
189 off_t total_size;
190
191 /* A pointer to the address table data. */
192 const gdb_byte *address_table;
193
194 /* Size of the address table data in bytes. */
195 offset_type address_table_size;
196
197 /* The symbol table, implemented as a hash table. */
198 const offset_type *symbol_table;
199
200 /* Size in slots, each slot is 2 offset_types. */
201 offset_type symbol_table_slots;
202
203 /* A pointer to the constant pool. */
204 const char *constant_pool;
205 };
206
207 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
208 DEF_VEC_P (dwarf2_per_cu_ptr);
209
210 struct tu_stats
211 {
212 int nr_uniq_abbrev_tables;
213 int nr_symtabs;
214 int nr_symtab_sharers;
215 int nr_stmt_less_type_units;
216 int nr_all_type_units_reallocs;
217 };
218
219 /* Collection of data recorded per objfile.
220 This hangs off of dwarf2_objfile_data_key. */
221
222 struct dwarf2_per_objfile
223 {
224 struct dwarf2_section_info info;
225 struct dwarf2_section_info abbrev;
226 struct dwarf2_section_info line;
227 struct dwarf2_section_info loc;
228 struct dwarf2_section_info loclists;
229 struct dwarf2_section_info macinfo;
230 struct dwarf2_section_info macro;
231 struct dwarf2_section_info str;
232 struct dwarf2_section_info line_str;
233 struct dwarf2_section_info ranges;
234 struct dwarf2_section_info rnglists;
235 struct dwarf2_section_info addr;
236 struct dwarf2_section_info frame;
237 struct dwarf2_section_info eh_frame;
238 struct dwarf2_section_info gdb_index;
239
240 VEC (dwarf2_section_info_def) *types;
241
242 /* Back link. */
243 struct objfile *objfile;
244
245 /* Table of all the compilation units. This is used to locate
246 the target compilation unit of a particular reference. */
247 struct dwarf2_per_cu_data **all_comp_units;
248
249 /* The number of compilation units in ALL_COMP_UNITS. */
250 int n_comp_units;
251
252 /* The number of .debug_types-related CUs. */
253 int n_type_units;
254
255 /* The number of elements allocated in all_type_units.
256 If there are skeleton-less TUs, we add them to all_type_units lazily. */
257 int n_allocated_type_units;
258
259 /* The .debug_types-related CUs (TUs).
260 This is stored in malloc space because we may realloc it. */
261 struct signatured_type **all_type_units;
262
263 /* Table of struct type_unit_group objects.
264 The hash key is the DW_AT_stmt_list value. */
265 htab_t type_unit_groups;
266
267 /* A table mapping .debug_types signatures to its signatured_type entry.
268 This is NULL if the .debug_types section hasn't been read in yet. */
269 htab_t signatured_types;
270
271 /* Type unit statistics, to see how well the scaling improvements
272 are doing. */
273 struct tu_stats tu_stats;
274
275 /* A chain of compilation units that are currently read in, so that
276 they can be freed later. */
277 struct dwarf2_per_cu_data *read_in_chain;
278
279 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
280 This is NULL if the table hasn't been allocated yet. */
281 htab_t dwo_files;
282
283 /* Non-zero if we've check for whether there is a DWP file. */
284 int dwp_checked;
285
286 /* The DWP file if there is one, or NULL. */
287 struct dwp_file *dwp_file;
288
289 /* The shared '.dwz' file, if one exists. This is used when the
290 original data was compressed using 'dwz -m'. */
291 struct dwz_file *dwz_file;
292
293 /* A flag indicating wether this objfile has a section loaded at a
294 VMA of 0. */
295 int has_section_at_zero;
296
297 /* True if we are using the mapped index,
298 or we are faking it for OBJF_READNOW's sake. */
299 unsigned char using_index;
300
301 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
302 struct mapped_index *index_table;
303
304 /* When using index_table, this keeps track of all quick_file_names entries.
305 TUs typically share line table entries with a CU, so we maintain a
306 separate table of all line table entries to support the sharing.
307 Note that while there can be way more TUs than CUs, we've already
308 sorted all the TUs into "type unit groups", grouped by their
309 DW_AT_stmt_list value. Therefore the only sharing done here is with a
310 CU and its associated TU group if there is one. */
311 htab_t quick_file_names_table;
312
313 /* Set during partial symbol reading, to prevent queueing of full
314 symbols. */
315 int reading_partial_symbols;
316
317 /* Table mapping type DIEs to their struct type *.
318 This is NULL if not allocated yet.
319 The mapping is done via (CU/TU + DIE offset) -> type. */
320 htab_t die_type_hash;
321
322 /* The CUs we recently read. */
323 VEC (dwarf2_per_cu_ptr) *just_read_cus;
324
325 /* Table containing line_header indexed by offset and offset_in_dwz. */
326 htab_t line_header_hash;
327 };
328
329 static struct dwarf2_per_objfile *dwarf2_per_objfile;
330
331 /* Default names of the debugging sections. */
332
333 /* Note that if the debugging section has been compressed, it might
334 have a name like .zdebug_info. */
335
336 static const struct dwarf2_debug_sections dwarf2_elf_names =
337 {
338 { ".debug_info", ".zdebug_info" },
339 { ".debug_abbrev", ".zdebug_abbrev" },
340 { ".debug_line", ".zdebug_line" },
341 { ".debug_loc", ".zdebug_loc" },
342 { ".debug_loclists", ".zdebug_loclists" },
343 { ".debug_macinfo", ".zdebug_macinfo" },
344 { ".debug_macro", ".zdebug_macro" },
345 { ".debug_str", ".zdebug_str" },
346 { ".debug_line_str", ".zdebug_line_str" },
347 { ".debug_ranges", ".zdebug_ranges" },
348 { ".debug_rnglists", ".zdebug_rnglists" },
349 { ".debug_types", ".zdebug_types" },
350 { ".debug_addr", ".zdebug_addr" },
351 { ".debug_frame", ".zdebug_frame" },
352 { ".eh_frame", NULL },
353 { ".gdb_index", ".zgdb_index" },
354 23
355 };
356
357 /* List of DWO/DWP sections. */
358
359 static const struct dwop_section_names
360 {
361 struct dwarf2_section_names abbrev_dwo;
362 struct dwarf2_section_names info_dwo;
363 struct dwarf2_section_names line_dwo;
364 struct dwarf2_section_names loc_dwo;
365 struct dwarf2_section_names loclists_dwo;
366 struct dwarf2_section_names macinfo_dwo;
367 struct dwarf2_section_names macro_dwo;
368 struct dwarf2_section_names str_dwo;
369 struct dwarf2_section_names str_offsets_dwo;
370 struct dwarf2_section_names types_dwo;
371 struct dwarf2_section_names cu_index;
372 struct dwarf2_section_names tu_index;
373 }
374 dwop_section_names =
375 {
376 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
377 { ".debug_info.dwo", ".zdebug_info.dwo" },
378 { ".debug_line.dwo", ".zdebug_line.dwo" },
379 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
380 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
381 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
382 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
383 { ".debug_str.dwo", ".zdebug_str.dwo" },
384 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
385 { ".debug_types.dwo", ".zdebug_types.dwo" },
386 { ".debug_cu_index", ".zdebug_cu_index" },
387 { ".debug_tu_index", ".zdebug_tu_index" },
388 };
389
390 /* local data types */
391
392 /* The data in a compilation unit header, after target2host
393 translation, looks like this. */
394 struct comp_unit_head
395 {
396 unsigned int length;
397 short version;
398 unsigned char addr_size;
399 unsigned char signed_addr_p;
400 sect_offset abbrev_sect_off;
401
402 /* Size of file offsets; either 4 or 8. */
403 unsigned int offset_size;
404
405 /* Size of the length field; either 4 or 12. */
406 unsigned int initial_length_size;
407
408 enum dwarf_unit_type unit_type;
409
410 /* Offset to the first byte of this compilation unit header in the
411 .debug_info section, for resolving relative reference dies. */
412 sect_offset sect_off;
413
414 /* Offset to first die in this cu from the start of the cu.
415 This will be the first byte following the compilation unit header. */
416 cu_offset first_die_cu_offset;
417
418 /* 64-bit signature of this type unit - it is valid only for
419 UNIT_TYPE DW_UT_type. */
420 ULONGEST signature;
421
422 /* For types, offset in the type's DIE of the type defined by this TU. */
423 cu_offset type_cu_offset_in_tu;
424 };
425
426 /* Type used for delaying computation of method physnames.
427 See comments for compute_delayed_physnames. */
428 struct delayed_method_info
429 {
430 /* The type to which the method is attached, i.e., its parent class. */
431 struct type *type;
432
433 /* The index of the method in the type's function fieldlists. */
434 int fnfield_index;
435
436 /* The index of the method in the fieldlist. */
437 int index;
438
439 /* The name of the DIE. */
440 const char *name;
441
442 /* The DIE associated with this method. */
443 struct die_info *die;
444 };
445
446 typedef struct delayed_method_info delayed_method_info;
447 DEF_VEC_O (delayed_method_info);
448
449 /* Internal state when decoding a particular compilation unit. */
450 struct dwarf2_cu
451 {
452 /* The objfile containing this compilation unit. */
453 struct objfile *objfile;
454
455 /* The header of the compilation unit. */
456 struct comp_unit_head header;
457
458 /* Base address of this compilation unit. */
459 CORE_ADDR base_address;
460
461 /* Non-zero if base_address has been set. */
462 int base_known;
463
464 /* The language we are debugging. */
465 enum language language;
466 const struct language_defn *language_defn;
467
468 const char *producer;
469
470 /* The generic symbol table building routines have separate lists for
471 file scope symbols and all all other scopes (local scopes). So
472 we need to select the right one to pass to add_symbol_to_list().
473 We do it by keeping a pointer to the correct list in list_in_scope.
474
475 FIXME: The original dwarf code just treated the file scope as the
476 first local scope, and all other local scopes as nested local
477 scopes, and worked fine. Check to see if we really need to
478 distinguish these in buildsym.c. */
479 struct pending **list_in_scope;
480
481 /* The abbrev table for this CU.
482 Normally this points to the abbrev table in the objfile.
483 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
484 struct abbrev_table *abbrev_table;
485
486 /* Hash table holding all the loaded partial DIEs
487 with partial_die->offset.SECT_OFF as hash. */
488 htab_t partial_dies;
489
490 /* Storage for things with the same lifetime as this read-in compilation
491 unit, including partial DIEs. */
492 struct obstack comp_unit_obstack;
493
494 /* When multiple dwarf2_cu structures are living in memory, this field
495 chains them all together, so that they can be released efficiently.
496 We will probably also want a generation counter so that most-recently-used
497 compilation units are cached... */
498 struct dwarf2_per_cu_data *read_in_chain;
499
500 /* Backlink to our per_cu entry. */
501 struct dwarf2_per_cu_data *per_cu;
502
503 /* How many compilation units ago was this CU last referenced? */
504 int last_used;
505
506 /* A hash table of DIE cu_offset for following references with
507 die_info->offset.sect_off as hash. */
508 htab_t die_hash;
509
510 /* Full DIEs if read in. */
511 struct die_info *dies;
512
513 /* A set of pointers to dwarf2_per_cu_data objects for compilation
514 units referenced by this one. Only set during full symbol processing;
515 partial symbol tables do not have dependencies. */
516 htab_t dependencies;
517
518 /* Header data from the line table, during full symbol processing. */
519 struct line_header *line_header;
520
521 /* A list of methods which need to have physnames computed
522 after all type information has been read. */
523 VEC (delayed_method_info) *method_list;
524
525 /* To be copied to symtab->call_site_htab. */
526 htab_t call_site_htab;
527
528 /* Non-NULL if this CU came from a DWO file.
529 There is an invariant here that is important to remember:
530 Except for attributes copied from the top level DIE in the "main"
531 (or "stub") file in preparation for reading the DWO file
532 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
533 Either there isn't a DWO file (in which case this is NULL and the point
534 is moot), or there is and either we're not going to read it (in which
535 case this is NULL) or there is and we are reading it (in which case this
536 is non-NULL). */
537 struct dwo_unit *dwo_unit;
538
539 /* The DW_AT_addr_base attribute if present, zero otherwise
540 (zero is a valid value though).
541 Note this value comes from the Fission stub CU/TU's DIE. */
542 ULONGEST addr_base;
543
544 /* The DW_AT_ranges_base attribute if present, zero otherwise
545 (zero is a valid value though).
546 Note this value comes from the Fission stub CU/TU's DIE.
547 Also note that the value is zero in the non-DWO case so this value can
548 be used without needing to know whether DWO files are in use or not.
549 N.B. This does not apply to DW_AT_ranges appearing in
550 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
551 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
552 DW_AT_ranges_base *would* have to be applied, and we'd have to care
553 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
554 ULONGEST ranges_base;
555
556 /* Mark used when releasing cached dies. */
557 unsigned int mark : 1;
558
559 /* This CU references .debug_loc. See the symtab->locations_valid field.
560 This test is imperfect as there may exist optimized debug code not using
561 any location list and still facing inlining issues if handled as
562 unoptimized code. For a future better test see GCC PR other/32998. */
563 unsigned int has_loclist : 1;
564
565 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
566 if all the producer_is_* fields are valid. This information is cached
567 because profiling CU expansion showed excessive time spent in
568 producer_is_gxx_lt_4_6. */
569 unsigned int checked_producer : 1;
570 unsigned int producer_is_gxx_lt_4_6 : 1;
571 unsigned int producer_is_gcc_lt_4_3 : 1;
572 unsigned int producer_is_icc : 1;
573
574 /* When set, the file that we're processing is known to have
575 debugging info for C++ namespaces. GCC 3.3.x did not produce
576 this information, but later versions do. */
577
578 unsigned int processing_has_namespace_info : 1;
579 };
580
581 /* Persistent data held for a compilation unit, even when not
582 processing it. We put a pointer to this structure in the
583 read_symtab_private field of the psymtab. */
584
585 struct dwarf2_per_cu_data
586 {
587 /* The start offset and length of this compilation unit.
588 NOTE: Unlike comp_unit_head.length, this length includes
589 initial_length_size.
590 If the DIE refers to a DWO file, this is always of the original die,
591 not the DWO file. */
592 sect_offset sect_off;
593 unsigned int length;
594
595 /* DWARF standard version this data has been read from (such as 4 or 5). */
596 short dwarf_version;
597
598 /* Flag indicating this compilation unit will be read in before
599 any of the current compilation units are processed. */
600 unsigned int queued : 1;
601
602 /* This flag will be set when reading partial DIEs if we need to load
603 absolutely all DIEs for this compilation unit, instead of just the ones
604 we think are interesting. It gets set if we look for a DIE in the
605 hash table and don't find it. */
606 unsigned int load_all_dies : 1;
607
608 /* Non-zero if this CU is from .debug_types.
609 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
610 this is non-zero. */
611 unsigned int is_debug_types : 1;
612
613 /* Non-zero if this CU is from the .dwz file. */
614 unsigned int is_dwz : 1;
615
616 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
617 This flag is only valid if is_debug_types is true.
618 We can't read a CU directly from a DWO file: There are required
619 attributes in the stub. */
620 unsigned int reading_dwo_directly : 1;
621
622 /* Non-zero if the TU has been read.
623 This is used to assist the "Stay in DWO Optimization" for Fission:
624 When reading a DWO, it's faster to read TUs from the DWO instead of
625 fetching them from random other DWOs (due to comdat folding).
626 If the TU has already been read, the optimization is unnecessary
627 (and unwise - we don't want to change where gdb thinks the TU lives
628 "midflight").
629 This flag is only valid if is_debug_types is true. */
630 unsigned int tu_read : 1;
631
632 /* The section this CU/TU lives in.
633 If the DIE refers to a DWO file, this is always the original die,
634 not the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
638 of the CU cache it gets reset to NULL again. This is left as NULL for
639 dummy CUs (a CU header, but nothing else). */
640 struct dwarf2_cu *cu;
641
642 /* The corresponding objfile.
643 Normally we can get the objfile from dwarf2_per_objfile.
644 However we can enter this file with just a "per_cu" handle. */
645 struct objfile *objfile;
646
647 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
648 is active. Otherwise, the 'psymtab' field is active. */
649 union
650 {
651 /* The partial symbol table associated with this compilation unit,
652 or NULL for unread partial units. */
653 struct partial_symtab *psymtab;
654
655 /* Data needed by the "quick" functions. */
656 struct dwarf2_per_cu_quick_data *quick;
657 } v;
658
659 /* The CUs we import using DW_TAG_imported_unit. This is filled in
660 while reading psymtabs, used to compute the psymtab dependencies,
661 and then cleared. Then it is filled in again while reading full
662 symbols, and only deleted when the objfile is destroyed.
663
664 This is also used to work around a difference between the way gold
665 generates .gdb_index version <=7 and the way gdb does. Arguably this
666 is a gold bug. For symbols coming from TUs, gold records in the index
667 the CU that includes the TU instead of the TU itself. This breaks
668 dw2_lookup_symbol: It assumes that if the index says symbol X lives
669 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
670 will find X. Alas TUs live in their own symtab, so after expanding CU Y
671 we need to look in TU Z to find X. Fortunately, this is akin to
672 DW_TAG_imported_unit, so we just use the same mechanism: For
673 .gdb_index version <=7 this also records the TUs that the CU referred
674 to. Concurrently with this change gdb was modified to emit version 8
675 indices so we only pay a price for gold generated indices.
676 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
677 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
678 };
679
680 /* Entry in the signatured_types hash table. */
681
682 struct signatured_type
683 {
684 /* The "per_cu" object of this type.
685 This struct is used iff per_cu.is_debug_types.
686 N.B.: This is the first member so that it's easy to convert pointers
687 between them. */
688 struct dwarf2_per_cu_data per_cu;
689
690 /* The type's signature. */
691 ULONGEST signature;
692
693 /* Offset in the TU of the type's DIE, as read from the TU header.
694 If this TU is a DWO stub and the definition lives in a DWO file
695 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
696 cu_offset type_offset_in_tu;
697
698 /* Offset in the section of the type's DIE.
699 If the definition lives in a DWO file, this is the offset in the
700 .debug_types.dwo section.
701 The value is zero until the actual value is known.
702 Zero is otherwise not a valid section offset. */
703 sect_offset type_offset_in_section;
704
705 /* Type units are grouped by their DW_AT_stmt_list entry so that they
706 can share them. This points to the containing symtab. */
707 struct type_unit_group *type_unit_group;
708
709 /* The type.
710 The first time we encounter this type we fully read it in and install it
711 in the symbol tables. Subsequent times we only need the type. */
712 struct type *type;
713
714 /* Containing DWO unit.
715 This field is valid iff per_cu.reading_dwo_directly. */
716 struct dwo_unit *dwo_unit;
717 };
718
719 typedef struct signatured_type *sig_type_ptr;
720 DEF_VEC_P (sig_type_ptr);
721
722 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
723 This includes type_unit_group and quick_file_names. */
724
725 struct stmt_list_hash
726 {
727 /* The DWO unit this table is from or NULL if there is none. */
728 struct dwo_unit *dwo_unit;
729
730 /* Offset in .debug_line or .debug_line.dwo. */
731 sect_offset line_sect_off;
732 };
733
734 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
735 an object of this type. */
736
737 struct type_unit_group
738 {
739 /* dwarf2read.c's main "handle" on a TU symtab.
740 To simplify things we create an artificial CU that "includes" all the
741 type units using this stmt_list so that the rest of the code still has
742 a "per_cu" handle on the symtab.
743 This PER_CU is recognized by having no section. */
744 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
745 struct dwarf2_per_cu_data per_cu;
746
747 /* The TUs that share this DW_AT_stmt_list entry.
748 This is added to while parsing type units to build partial symtabs,
749 and is deleted afterwards and not used again. */
750 VEC (sig_type_ptr) *tus;
751
752 /* The compunit symtab.
753 Type units in a group needn't all be defined in the same source file,
754 so we create an essentially anonymous symtab as the compunit symtab. */
755 struct compunit_symtab *compunit_symtab;
756
757 /* The data used to construct the hash key. */
758 struct stmt_list_hash hash;
759
760 /* The number of symtabs from the line header.
761 The value here must match line_header.num_file_names. */
762 unsigned int num_symtabs;
763
764 /* The symbol tables for this TU (obtained from the files listed in
765 DW_AT_stmt_list).
766 WARNING: The order of entries here must match the order of entries
767 in the line header. After the first TU using this type_unit_group, the
768 line header for the subsequent TUs is recreated from this. This is done
769 because we need to use the same symtabs for each TU using the same
770 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
771 there's no guarantee the line header doesn't have duplicate entries. */
772 struct symtab **symtabs;
773 };
774
775 /* These sections are what may appear in a (real or virtual) DWO file. */
776
777 struct dwo_sections
778 {
779 struct dwarf2_section_info abbrev;
780 struct dwarf2_section_info line;
781 struct dwarf2_section_info loc;
782 struct dwarf2_section_info loclists;
783 struct dwarf2_section_info macinfo;
784 struct dwarf2_section_info macro;
785 struct dwarf2_section_info str;
786 struct dwarf2_section_info str_offsets;
787 /* In the case of a virtual DWO file, these two are unused. */
788 struct dwarf2_section_info info;
789 VEC (dwarf2_section_info_def) *types;
790 };
791
792 /* CUs/TUs in DWP/DWO files. */
793
794 struct dwo_unit
795 {
796 /* Backlink to the containing struct dwo_file. */
797 struct dwo_file *dwo_file;
798
799 /* The "id" that distinguishes this CU/TU.
800 .debug_info calls this "dwo_id", .debug_types calls this "signature".
801 Since signatures came first, we stick with it for consistency. */
802 ULONGEST signature;
803
804 /* The section this CU/TU lives in, in the DWO file. */
805 struct dwarf2_section_info *section;
806
807 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
808 sect_offset sect_off;
809 unsigned int length;
810
811 /* For types, offset in the type's DIE of the type defined by this TU. */
812 cu_offset type_offset_in_tu;
813 };
814
815 /* include/dwarf2.h defines the DWP section codes.
816 It defines a max value but it doesn't define a min value, which we
817 use for error checking, so provide one. */
818
819 enum dwp_v2_section_ids
820 {
821 DW_SECT_MIN = 1
822 };
823
824 /* Data for one DWO file.
825
826 This includes virtual DWO files (a virtual DWO file is a DWO file as it
827 appears in a DWP file). DWP files don't really have DWO files per se -
828 comdat folding of types "loses" the DWO file they came from, and from
829 a high level view DWP files appear to contain a mass of random types.
830 However, to maintain consistency with the non-DWP case we pretend DWP
831 files contain virtual DWO files, and we assign each TU with one virtual
832 DWO file (generally based on the line and abbrev section offsets -
833 a heuristic that seems to work in practice). */
834
835 struct dwo_file
836 {
837 /* The DW_AT_GNU_dwo_name attribute.
838 For virtual DWO files the name is constructed from the section offsets
839 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
840 from related CU+TUs. */
841 const char *dwo_name;
842
843 /* The DW_AT_comp_dir attribute. */
844 const char *comp_dir;
845
846 /* The bfd, when the file is open. Otherwise this is NULL.
847 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
848 bfd *dbfd;
849
850 /* The sections that make up this DWO file.
851 Remember that for virtual DWO files in DWP V2, these are virtual
852 sections (for lack of a better name). */
853 struct dwo_sections sections;
854
855 /* The CU in the file.
856 We only support one because having more than one requires hacking the
857 dwo_name of each to match, which is highly unlikely to happen.
858 Doing this means all TUs can share comp_dir: We also assume that
859 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
860 struct dwo_unit *cu;
861
862 /* Table of TUs in the file.
863 Each element is a struct dwo_unit. */
864 htab_t tus;
865 };
866
867 /* These sections are what may appear in a DWP file. */
868
869 struct dwp_sections
870 {
871 /* These are used by both DWP version 1 and 2. */
872 struct dwarf2_section_info str;
873 struct dwarf2_section_info cu_index;
874 struct dwarf2_section_info tu_index;
875
876 /* These are only used by DWP version 2 files.
877 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
878 sections are referenced by section number, and are not recorded here.
879 In DWP version 2 there is at most one copy of all these sections, each
880 section being (effectively) comprised of the concatenation of all of the
881 individual sections that exist in the version 1 format.
882 To keep the code simple we treat each of these concatenated pieces as a
883 section itself (a virtual section?). */
884 struct dwarf2_section_info abbrev;
885 struct dwarf2_section_info info;
886 struct dwarf2_section_info line;
887 struct dwarf2_section_info loc;
888 struct dwarf2_section_info macinfo;
889 struct dwarf2_section_info macro;
890 struct dwarf2_section_info str_offsets;
891 struct dwarf2_section_info types;
892 };
893
894 /* These sections are what may appear in a virtual DWO file in DWP version 1.
895 A virtual DWO file is a DWO file as it appears in a DWP file. */
896
897 struct virtual_v1_dwo_sections
898 {
899 struct dwarf2_section_info abbrev;
900 struct dwarf2_section_info line;
901 struct dwarf2_section_info loc;
902 struct dwarf2_section_info macinfo;
903 struct dwarf2_section_info macro;
904 struct dwarf2_section_info str_offsets;
905 /* Each DWP hash table entry records one CU or one TU.
906 That is recorded here, and copied to dwo_unit.section. */
907 struct dwarf2_section_info info_or_types;
908 };
909
910 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
911 In version 2, the sections of the DWO files are concatenated together
912 and stored in one section of that name. Thus each ELF section contains
913 several "virtual" sections. */
914
915 struct virtual_v2_dwo_sections
916 {
917 bfd_size_type abbrev_offset;
918 bfd_size_type abbrev_size;
919
920 bfd_size_type line_offset;
921 bfd_size_type line_size;
922
923 bfd_size_type loc_offset;
924 bfd_size_type loc_size;
925
926 bfd_size_type macinfo_offset;
927 bfd_size_type macinfo_size;
928
929 bfd_size_type macro_offset;
930 bfd_size_type macro_size;
931
932 bfd_size_type str_offsets_offset;
933 bfd_size_type str_offsets_size;
934
935 /* Each DWP hash table entry records one CU or one TU.
936 That is recorded here, and copied to dwo_unit.section. */
937 bfd_size_type info_or_types_offset;
938 bfd_size_type info_or_types_size;
939 };
940
941 /* Contents of DWP hash tables. */
942
943 struct dwp_hash_table
944 {
945 uint32_t version, nr_columns;
946 uint32_t nr_units, nr_slots;
947 const gdb_byte *hash_table, *unit_table;
948 union
949 {
950 struct
951 {
952 const gdb_byte *indices;
953 } v1;
954 struct
955 {
956 /* This is indexed by column number and gives the id of the section
957 in that column. */
958 #define MAX_NR_V2_DWO_SECTIONS \
959 (1 /* .debug_info or .debug_types */ \
960 + 1 /* .debug_abbrev */ \
961 + 1 /* .debug_line */ \
962 + 1 /* .debug_loc */ \
963 + 1 /* .debug_str_offsets */ \
964 + 1 /* .debug_macro or .debug_macinfo */)
965 int section_ids[MAX_NR_V2_DWO_SECTIONS];
966 const gdb_byte *offsets;
967 const gdb_byte *sizes;
968 } v2;
969 } section_pool;
970 };
971
972 /* Data for one DWP file. */
973
974 struct dwp_file
975 {
976 /* Name of the file. */
977 const char *name;
978
979 /* File format version. */
980 int version;
981
982 /* The bfd. */
983 bfd *dbfd;
984
985 /* Section info for this file. */
986 struct dwp_sections sections;
987
988 /* Table of CUs in the file. */
989 const struct dwp_hash_table *cus;
990
991 /* Table of TUs in the file. */
992 const struct dwp_hash_table *tus;
993
994 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
995 htab_t loaded_cus;
996 htab_t loaded_tus;
997
998 /* Table to map ELF section numbers to their sections.
999 This is only needed for the DWP V1 file format. */
1000 unsigned int num_sections;
1001 asection **elf_sections;
1002 };
1003
1004 /* This represents a '.dwz' file. */
1005
1006 struct dwz_file
1007 {
1008 /* A dwz file can only contain a few sections. */
1009 struct dwarf2_section_info abbrev;
1010 struct dwarf2_section_info info;
1011 struct dwarf2_section_info str;
1012 struct dwarf2_section_info line;
1013 struct dwarf2_section_info macro;
1014 struct dwarf2_section_info gdb_index;
1015
1016 /* The dwz's BFD. */
1017 bfd *dwz_bfd;
1018 };
1019
1020 /* Struct used to pass misc. parameters to read_die_and_children, et
1021 al. which are used for both .debug_info and .debug_types dies.
1022 All parameters here are unchanging for the life of the call. This
1023 struct exists to abstract away the constant parameters of die reading. */
1024
1025 struct die_reader_specs
1026 {
1027 /* The bfd of die_section. */
1028 bfd* abfd;
1029
1030 /* The CU of the DIE we are parsing. */
1031 struct dwarf2_cu *cu;
1032
1033 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1034 struct dwo_file *dwo_file;
1035
1036 /* The section the die comes from.
1037 This is either .debug_info or .debug_types, or the .dwo variants. */
1038 struct dwarf2_section_info *die_section;
1039
1040 /* die_section->buffer. */
1041 const gdb_byte *buffer;
1042
1043 /* The end of the buffer. */
1044 const gdb_byte *buffer_end;
1045
1046 /* The value of the DW_AT_comp_dir attribute. */
1047 const char *comp_dir;
1048 };
1049
1050 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1051 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1052 const gdb_byte *info_ptr,
1053 struct die_info *comp_unit_die,
1054 int has_children,
1055 void *data);
1056
1057 /* A 1-based directory index. This is a strong typedef to prevent
1058 accidentally using a directory index as a 0-based index into an
1059 array/vector. */
1060 enum class dir_index : unsigned int {};
1061
1062 /* Likewise, a 1-based file name index. */
1063 enum class file_name_index : unsigned int {};
1064
1065 struct file_entry
1066 {
1067 file_entry () = default;
1068
1069 file_entry (const char *name_, dir_index d_index_,
1070 unsigned int mod_time_, unsigned int length_)
1071 : name (name_),
1072 d_index (d_index_),
1073 mod_time (mod_time_),
1074 length (length_)
1075 {}
1076
1077 /* Return the include directory at D_INDEX stored in LH. Returns
1078 NULL if D_INDEX is out of bounds. */
1079 const char *include_dir (const line_header *lh) const;
1080
1081 /* The file name. Note this is an observing pointer. The memory is
1082 owned by debug_line_buffer. */
1083 const char *name {};
1084
1085 /* The directory index (1-based). */
1086 dir_index d_index {};
1087
1088 unsigned int mod_time {};
1089
1090 unsigned int length {};
1091
1092 /* True if referenced by the Line Number Program. */
1093 bool included_p {};
1094
1095 /* The associated symbol table, if any. */
1096 struct symtab *symtab {};
1097 };
1098
1099 /* The line number information for a compilation unit (found in the
1100 .debug_line section) begins with a "statement program header",
1101 which contains the following information. */
1102 struct line_header
1103 {
1104 line_header ()
1105 : offset_in_dwz {}
1106 {}
1107
1108 /* Add an entry to the include directory table. */
1109 void add_include_dir (const char *include_dir);
1110
1111 /* Add an entry to the file name table. */
1112 void add_file_name (const char *name, dir_index d_index,
1113 unsigned int mod_time, unsigned int length);
1114
1115 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1116 is out of bounds. */
1117 const char *include_dir_at (dir_index index) const
1118 {
1119 /* Convert directory index number (1-based) to vector index
1120 (0-based). */
1121 size_t vec_index = to_underlying (index) - 1;
1122
1123 if (vec_index >= include_dirs.size ())
1124 return NULL;
1125 return include_dirs[vec_index];
1126 }
1127
1128 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1129 is out of bounds. */
1130 file_entry *file_name_at (file_name_index index)
1131 {
1132 /* Convert file name index number (1-based) to vector index
1133 (0-based). */
1134 size_t vec_index = to_underlying (index) - 1;
1135
1136 if (vec_index >= file_names.size ())
1137 return NULL;
1138 return &file_names[vec_index];
1139 }
1140
1141 /* Const version of the above. */
1142 const file_entry *file_name_at (unsigned int index) const
1143 {
1144 if (index >= file_names.size ())
1145 return NULL;
1146 return &file_names[index];
1147 }
1148
1149 /* Offset of line number information in .debug_line section. */
1150 sect_offset sect_off {};
1151
1152 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1153 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1154
1155 unsigned int total_length {};
1156 unsigned short version {};
1157 unsigned int header_length {};
1158 unsigned char minimum_instruction_length {};
1159 unsigned char maximum_ops_per_instruction {};
1160 unsigned char default_is_stmt {};
1161 int line_base {};
1162 unsigned char line_range {};
1163 unsigned char opcode_base {};
1164
1165 /* standard_opcode_lengths[i] is the number of operands for the
1166 standard opcode whose value is i. This means that
1167 standard_opcode_lengths[0] is unused, and the last meaningful
1168 element is standard_opcode_lengths[opcode_base - 1]. */
1169 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1170
1171 /* The include_directories table. Note these are observing
1172 pointers. The memory is owned by debug_line_buffer. */
1173 std::vector<const char *> include_dirs;
1174
1175 /* The file_names table. */
1176 std::vector<file_entry> file_names;
1177
1178 /* The start and end of the statement program following this
1179 header. These point into dwarf2_per_objfile->line_buffer. */
1180 const gdb_byte *statement_program_start {}, *statement_program_end {};
1181 };
1182
1183 typedef std::unique_ptr<line_header> line_header_up;
1184
1185 const char *
1186 file_entry::include_dir (const line_header *lh) const
1187 {
1188 return lh->include_dir_at (d_index);
1189 }
1190
1191 /* When we construct a partial symbol table entry we only
1192 need this much information. */
1193 struct partial_die_info
1194 {
1195 /* Offset of this DIE. */
1196 sect_offset sect_off;
1197
1198 /* DWARF-2 tag for this DIE. */
1199 ENUM_BITFIELD(dwarf_tag) tag : 16;
1200
1201 /* Assorted flags describing the data found in this DIE. */
1202 unsigned int has_children : 1;
1203 unsigned int is_external : 1;
1204 unsigned int is_declaration : 1;
1205 unsigned int has_type : 1;
1206 unsigned int has_specification : 1;
1207 unsigned int has_pc_info : 1;
1208 unsigned int may_be_inlined : 1;
1209
1210 /* This DIE has been marked DW_AT_main_subprogram. */
1211 unsigned int main_subprogram : 1;
1212
1213 /* Flag set if the SCOPE field of this structure has been
1214 computed. */
1215 unsigned int scope_set : 1;
1216
1217 /* Flag set if the DIE has a byte_size attribute. */
1218 unsigned int has_byte_size : 1;
1219
1220 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1221 unsigned int has_const_value : 1;
1222
1223 /* Flag set if any of the DIE's children are template arguments. */
1224 unsigned int has_template_arguments : 1;
1225
1226 /* Flag set if fixup_partial_die has been called on this die. */
1227 unsigned int fixup_called : 1;
1228
1229 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1230 unsigned int is_dwz : 1;
1231
1232 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1233 unsigned int spec_is_dwz : 1;
1234
1235 /* The name of this DIE. Normally the value of DW_AT_name, but
1236 sometimes a default name for unnamed DIEs. */
1237 const char *name;
1238
1239 /* The linkage name, if present. */
1240 const char *linkage_name;
1241
1242 /* The scope to prepend to our children. This is generally
1243 allocated on the comp_unit_obstack, so will disappear
1244 when this compilation unit leaves the cache. */
1245 const char *scope;
1246
1247 /* Some data associated with the partial DIE. The tag determines
1248 which field is live. */
1249 union
1250 {
1251 /* The location description associated with this DIE, if any. */
1252 struct dwarf_block *locdesc;
1253 /* The offset of an import, for DW_TAG_imported_unit. */
1254 sect_offset sect_off;
1255 } d;
1256
1257 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1258 CORE_ADDR lowpc;
1259 CORE_ADDR highpc;
1260
1261 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1262 DW_AT_sibling, if any. */
1263 /* NOTE: This member isn't strictly necessary, read_partial_die could
1264 return DW_AT_sibling values to its caller load_partial_dies. */
1265 const gdb_byte *sibling;
1266
1267 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1268 DW_AT_specification (or DW_AT_abstract_origin or
1269 DW_AT_extension). */
1270 sect_offset spec_offset;
1271
1272 /* Pointers to this DIE's parent, first child, and next sibling,
1273 if any. */
1274 struct partial_die_info *die_parent, *die_child, *die_sibling;
1275 };
1276
1277 /* This data structure holds the information of an abbrev. */
1278 struct abbrev_info
1279 {
1280 unsigned int number; /* number identifying abbrev */
1281 enum dwarf_tag tag; /* dwarf tag */
1282 unsigned short has_children; /* boolean */
1283 unsigned short num_attrs; /* number of attributes */
1284 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1285 struct abbrev_info *next; /* next in chain */
1286 };
1287
1288 struct attr_abbrev
1289 {
1290 ENUM_BITFIELD(dwarf_attribute) name : 16;
1291 ENUM_BITFIELD(dwarf_form) form : 16;
1292
1293 /* It is valid only if FORM is DW_FORM_implicit_const. */
1294 LONGEST implicit_const;
1295 };
1296
1297 /* Size of abbrev_table.abbrev_hash_table. */
1298 #define ABBREV_HASH_SIZE 121
1299
1300 /* Top level data structure to contain an abbreviation table. */
1301
1302 struct abbrev_table
1303 {
1304 /* Where the abbrev table came from.
1305 This is used as a sanity check when the table is used. */
1306 sect_offset sect_off;
1307
1308 /* Storage for the abbrev table. */
1309 struct obstack abbrev_obstack;
1310
1311 /* Hash table of abbrevs.
1312 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1313 It could be statically allocated, but the previous code didn't so we
1314 don't either. */
1315 struct abbrev_info **abbrevs;
1316 };
1317
1318 /* Attributes have a name and a value. */
1319 struct attribute
1320 {
1321 ENUM_BITFIELD(dwarf_attribute) name : 16;
1322 ENUM_BITFIELD(dwarf_form) form : 15;
1323
1324 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1325 field should be in u.str (existing only for DW_STRING) but it is kept
1326 here for better struct attribute alignment. */
1327 unsigned int string_is_canonical : 1;
1328
1329 union
1330 {
1331 const char *str;
1332 struct dwarf_block *blk;
1333 ULONGEST unsnd;
1334 LONGEST snd;
1335 CORE_ADDR addr;
1336 ULONGEST signature;
1337 }
1338 u;
1339 };
1340
1341 /* This data structure holds a complete die structure. */
1342 struct die_info
1343 {
1344 /* DWARF-2 tag for this DIE. */
1345 ENUM_BITFIELD(dwarf_tag) tag : 16;
1346
1347 /* Number of attributes */
1348 unsigned char num_attrs;
1349
1350 /* True if we're presently building the full type name for the
1351 type derived from this DIE. */
1352 unsigned char building_fullname : 1;
1353
1354 /* True if this die is in process. PR 16581. */
1355 unsigned char in_process : 1;
1356
1357 /* Abbrev number */
1358 unsigned int abbrev;
1359
1360 /* Offset in .debug_info or .debug_types section. */
1361 sect_offset sect_off;
1362
1363 /* The dies in a compilation unit form an n-ary tree. PARENT
1364 points to this die's parent; CHILD points to the first child of
1365 this node; and all the children of a given node are chained
1366 together via their SIBLING fields. */
1367 struct die_info *child; /* Its first child, if any. */
1368 struct die_info *sibling; /* Its next sibling, if any. */
1369 struct die_info *parent; /* Its parent, if any. */
1370
1371 /* An array of attributes, with NUM_ATTRS elements. There may be
1372 zero, but it's not common and zero-sized arrays are not
1373 sufficiently portable C. */
1374 struct attribute attrs[1];
1375 };
1376
1377 /* Get at parts of an attribute structure. */
1378
1379 #define DW_STRING(attr) ((attr)->u.str)
1380 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1381 #define DW_UNSND(attr) ((attr)->u.unsnd)
1382 #define DW_BLOCK(attr) ((attr)->u.blk)
1383 #define DW_SND(attr) ((attr)->u.snd)
1384 #define DW_ADDR(attr) ((attr)->u.addr)
1385 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1386
1387 /* Blocks are a bunch of untyped bytes. */
1388 struct dwarf_block
1389 {
1390 size_t size;
1391
1392 /* Valid only if SIZE is not zero. */
1393 const gdb_byte *data;
1394 };
1395
1396 #ifndef ATTR_ALLOC_CHUNK
1397 #define ATTR_ALLOC_CHUNK 4
1398 #endif
1399
1400 /* Allocate fields for structs, unions and enums in this size. */
1401 #ifndef DW_FIELD_ALLOC_CHUNK
1402 #define DW_FIELD_ALLOC_CHUNK 4
1403 #endif
1404
1405 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1406 but this would require a corresponding change in unpack_field_as_long
1407 and friends. */
1408 static int bits_per_byte = 8;
1409
1410 struct nextfield
1411 {
1412 struct nextfield *next;
1413 int accessibility;
1414 int virtuality;
1415 struct field field;
1416 };
1417
1418 struct nextfnfield
1419 {
1420 struct nextfnfield *next;
1421 struct fn_field fnfield;
1422 };
1423
1424 struct fnfieldlist
1425 {
1426 const char *name;
1427 int length;
1428 struct nextfnfield *head;
1429 };
1430
1431 struct typedef_field_list
1432 {
1433 struct typedef_field field;
1434 struct typedef_field_list *next;
1435 };
1436
1437 /* The routines that read and process dies for a C struct or C++ class
1438 pass lists of data member fields and lists of member function fields
1439 in an instance of a field_info structure, as defined below. */
1440 struct field_info
1441 {
1442 /* List of data member and baseclasses fields. */
1443 struct nextfield *fields, *baseclasses;
1444
1445 /* Number of fields (including baseclasses). */
1446 int nfields;
1447
1448 /* Number of baseclasses. */
1449 int nbaseclasses;
1450
1451 /* Set if the accesibility of one of the fields is not public. */
1452 int non_public_fields;
1453
1454 /* Member function fields array, entries are allocated in the order they
1455 are encountered in the object file. */
1456 struct nextfnfield *fnfields;
1457
1458 /* Member function fieldlist array, contains name of possibly overloaded
1459 member function, number of overloaded member functions and a pointer
1460 to the head of the member function field chain. */
1461 struct fnfieldlist *fnfieldlists;
1462
1463 /* Number of entries in the fnfieldlists array. */
1464 int nfnfields;
1465
1466 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1467 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1468 struct typedef_field_list *typedef_field_list;
1469 unsigned typedef_field_list_count;
1470 };
1471
1472 /* One item on the queue of compilation units to read in full symbols
1473 for. */
1474 struct dwarf2_queue_item
1475 {
1476 struct dwarf2_per_cu_data *per_cu;
1477 enum language pretend_language;
1478 struct dwarf2_queue_item *next;
1479 };
1480
1481 /* The current queue. */
1482 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1483
1484 /* Loaded secondary compilation units are kept in memory until they
1485 have not been referenced for the processing of this many
1486 compilation units. Set this to zero to disable caching. Cache
1487 sizes of up to at least twenty will improve startup time for
1488 typical inter-CU-reference binaries, at an obvious memory cost. */
1489 static int dwarf_max_cache_age = 5;
1490 static void
1491 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1492 struct cmd_list_element *c, const char *value)
1493 {
1494 fprintf_filtered (file, _("The upper bound on the age of cached "
1495 "DWARF compilation units is %s.\n"),
1496 value);
1497 }
1498 \f
1499 /* local function prototypes */
1500
1501 static const char *get_section_name (const struct dwarf2_section_info *);
1502
1503 static const char *get_section_file_name (const struct dwarf2_section_info *);
1504
1505 static void dwarf2_locate_sections (bfd *, asection *, void *);
1506
1507 static void dwarf2_find_base_address (struct die_info *die,
1508 struct dwarf2_cu *cu);
1509
1510 static struct partial_symtab *create_partial_symtab
1511 (struct dwarf2_per_cu_data *per_cu, const char *name);
1512
1513 static void dwarf2_build_psymtabs_hard (struct objfile *);
1514
1515 static void scan_partial_symbols (struct partial_die_info *,
1516 CORE_ADDR *, CORE_ADDR *,
1517 int, struct dwarf2_cu *);
1518
1519 static void add_partial_symbol (struct partial_die_info *,
1520 struct dwarf2_cu *);
1521
1522 static void add_partial_namespace (struct partial_die_info *pdi,
1523 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1524 int set_addrmap, struct dwarf2_cu *cu);
1525
1526 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1527 CORE_ADDR *highpc, int set_addrmap,
1528 struct dwarf2_cu *cu);
1529
1530 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1531 struct dwarf2_cu *cu);
1532
1533 static void add_partial_subprogram (struct partial_die_info *pdi,
1534 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1535 int need_pc, struct dwarf2_cu *cu);
1536
1537 static void dwarf2_read_symtab (struct partial_symtab *,
1538 struct objfile *);
1539
1540 static void psymtab_to_symtab_1 (struct partial_symtab *);
1541
1542 static struct abbrev_info *abbrev_table_lookup_abbrev
1543 (const struct abbrev_table *, unsigned int);
1544
1545 static struct abbrev_table *abbrev_table_read_table
1546 (struct dwarf2_section_info *, sect_offset);
1547
1548 static void abbrev_table_free (struct abbrev_table *);
1549
1550 static void abbrev_table_free_cleanup (void *);
1551
1552 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1553 struct dwarf2_section_info *);
1554
1555 static void dwarf2_free_abbrev_table (void *);
1556
1557 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1558
1559 static struct partial_die_info *load_partial_dies
1560 (const struct die_reader_specs *, const gdb_byte *, int);
1561
1562 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1563 struct partial_die_info *,
1564 struct abbrev_info *,
1565 unsigned int,
1566 const gdb_byte *);
1567
1568 static struct partial_die_info *find_partial_die (sect_offset, int,
1569 struct dwarf2_cu *);
1570
1571 static void fixup_partial_die (struct partial_die_info *,
1572 struct dwarf2_cu *);
1573
1574 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1575 struct attribute *, struct attr_abbrev *,
1576 const gdb_byte *);
1577
1578 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1579
1580 static int read_1_signed_byte (bfd *, const gdb_byte *);
1581
1582 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1583
1584 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1585
1586 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1587
1588 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1589 unsigned int *);
1590
1591 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1592
1593 static LONGEST read_checked_initial_length_and_offset
1594 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1595 unsigned int *, unsigned int *);
1596
1597 static LONGEST read_offset (bfd *, const gdb_byte *,
1598 const struct comp_unit_head *,
1599 unsigned int *);
1600
1601 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1602
1603 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1604 sect_offset);
1605
1606 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1607
1608 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1609
1610 static const char *read_indirect_string (bfd *, const gdb_byte *,
1611 const struct comp_unit_head *,
1612 unsigned int *);
1613
1614 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1615 const struct comp_unit_head *,
1616 unsigned int *);
1617
1618 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1619
1620 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1621
1622 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1623 const gdb_byte *,
1624 unsigned int *);
1625
1626 static const char *read_str_index (const struct die_reader_specs *reader,
1627 ULONGEST str_index);
1628
1629 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1630
1631 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1632 struct dwarf2_cu *);
1633
1634 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1635 unsigned int);
1636
1637 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1638 struct dwarf2_cu *cu);
1639
1640 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1641 struct dwarf2_cu *cu);
1642
1643 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1644
1645 static struct die_info *die_specification (struct die_info *die,
1646 struct dwarf2_cu **);
1647
1648 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1649 struct dwarf2_cu *cu);
1650
1651 static void dwarf_decode_lines (struct line_header *, const char *,
1652 struct dwarf2_cu *, struct partial_symtab *,
1653 CORE_ADDR, int decode_mapping);
1654
1655 static void dwarf2_start_subfile (const char *, const char *);
1656
1657 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1658 const char *, const char *,
1659 CORE_ADDR);
1660
1661 static struct symbol *new_symbol (struct die_info *, struct type *,
1662 struct dwarf2_cu *);
1663
1664 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1665 struct dwarf2_cu *, struct symbol *);
1666
1667 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1668 struct dwarf2_cu *);
1669
1670 static void dwarf2_const_value_attr (const struct attribute *attr,
1671 struct type *type,
1672 const char *name,
1673 struct obstack *obstack,
1674 struct dwarf2_cu *cu, LONGEST *value,
1675 const gdb_byte **bytes,
1676 struct dwarf2_locexpr_baton **baton);
1677
1678 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1679
1680 static int need_gnat_info (struct dwarf2_cu *);
1681
1682 static struct type *die_descriptive_type (struct die_info *,
1683 struct dwarf2_cu *);
1684
1685 static void set_descriptive_type (struct type *, struct die_info *,
1686 struct dwarf2_cu *);
1687
1688 static struct type *die_containing_type (struct die_info *,
1689 struct dwarf2_cu *);
1690
1691 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1692 struct dwarf2_cu *);
1693
1694 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1695
1696 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1697
1698 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1699
1700 static char *typename_concat (struct obstack *obs, const char *prefix,
1701 const char *suffix, int physname,
1702 struct dwarf2_cu *cu);
1703
1704 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1705
1706 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1707
1708 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1709
1710 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1711
1712 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1713
1714 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1715 struct dwarf2_cu *, struct partial_symtab *);
1716
1717 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1718 values. Keep the items ordered with increasing constraints compliance. */
1719 enum pc_bounds_kind
1720 {
1721 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1722 PC_BOUNDS_NOT_PRESENT,
1723
1724 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1725 were present but they do not form a valid range of PC addresses. */
1726 PC_BOUNDS_INVALID,
1727
1728 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1729 PC_BOUNDS_RANGES,
1730
1731 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1732 PC_BOUNDS_HIGH_LOW,
1733 };
1734
1735 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1736 CORE_ADDR *, CORE_ADDR *,
1737 struct dwarf2_cu *,
1738 struct partial_symtab *);
1739
1740 static void get_scope_pc_bounds (struct die_info *,
1741 CORE_ADDR *, CORE_ADDR *,
1742 struct dwarf2_cu *);
1743
1744 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1745 CORE_ADDR, struct dwarf2_cu *);
1746
1747 static void dwarf2_add_field (struct field_info *, struct die_info *,
1748 struct dwarf2_cu *);
1749
1750 static void dwarf2_attach_fields_to_type (struct field_info *,
1751 struct type *, struct dwarf2_cu *);
1752
1753 static void dwarf2_add_member_fn (struct field_info *,
1754 struct die_info *, struct type *,
1755 struct dwarf2_cu *);
1756
1757 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1758 struct type *,
1759 struct dwarf2_cu *);
1760
1761 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1762
1763 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1764
1765 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1766
1767 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1768
1769 static struct using_direct **using_directives (enum language);
1770
1771 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1772
1773 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1774
1775 static struct type *read_module_type (struct die_info *die,
1776 struct dwarf2_cu *cu);
1777
1778 static const char *namespace_name (struct die_info *die,
1779 int *is_anonymous, struct dwarf2_cu *);
1780
1781 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1782
1783 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1784
1785 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1786 struct dwarf2_cu *);
1787
1788 static struct die_info *read_die_and_siblings_1
1789 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1790 struct die_info *);
1791
1792 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1793 const gdb_byte *info_ptr,
1794 const gdb_byte **new_info_ptr,
1795 struct die_info *parent);
1796
1797 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1798 struct die_info **, const gdb_byte *,
1799 int *, int);
1800
1801 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1802 struct die_info **, const gdb_byte *,
1803 int *);
1804
1805 static void process_die (struct die_info *, struct dwarf2_cu *);
1806
1807 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1808 struct obstack *);
1809
1810 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1811
1812 static const char *dwarf2_full_name (const char *name,
1813 struct die_info *die,
1814 struct dwarf2_cu *cu);
1815
1816 static const char *dwarf2_physname (const char *name, struct die_info *die,
1817 struct dwarf2_cu *cu);
1818
1819 static struct die_info *dwarf2_extension (struct die_info *die,
1820 struct dwarf2_cu **);
1821
1822 static const char *dwarf_tag_name (unsigned int);
1823
1824 static const char *dwarf_attr_name (unsigned int);
1825
1826 static const char *dwarf_form_name (unsigned int);
1827
1828 static const char *dwarf_bool_name (unsigned int);
1829
1830 static const char *dwarf_type_encoding_name (unsigned int);
1831
1832 static struct die_info *sibling_die (struct die_info *);
1833
1834 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1835
1836 static void dump_die_for_error (struct die_info *);
1837
1838 static void dump_die_1 (struct ui_file *, int level, int max_level,
1839 struct die_info *);
1840
1841 /*static*/ void dump_die (struct die_info *, int max_level);
1842
1843 static void store_in_ref_table (struct die_info *,
1844 struct dwarf2_cu *);
1845
1846 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1847
1848 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1849
1850 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1851 const struct attribute *,
1852 struct dwarf2_cu **);
1853
1854 static struct die_info *follow_die_ref (struct die_info *,
1855 const struct attribute *,
1856 struct dwarf2_cu **);
1857
1858 static struct die_info *follow_die_sig (struct die_info *,
1859 const struct attribute *,
1860 struct dwarf2_cu **);
1861
1862 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1863 struct dwarf2_cu *);
1864
1865 static struct type *get_DW_AT_signature_type (struct die_info *,
1866 const struct attribute *,
1867 struct dwarf2_cu *);
1868
1869 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1870
1871 static void read_signatured_type (struct signatured_type *);
1872
1873 static int attr_to_dynamic_prop (const struct attribute *attr,
1874 struct die_info *die, struct dwarf2_cu *cu,
1875 struct dynamic_prop *prop);
1876
1877 /* memory allocation interface */
1878
1879 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1880
1881 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1882
1883 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1884
1885 static int attr_form_is_block (const struct attribute *);
1886
1887 static int attr_form_is_section_offset (const struct attribute *);
1888
1889 static int attr_form_is_constant (const struct attribute *);
1890
1891 static int attr_form_is_ref (const struct attribute *);
1892
1893 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1894 struct dwarf2_loclist_baton *baton,
1895 const struct attribute *attr);
1896
1897 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1898 struct symbol *sym,
1899 struct dwarf2_cu *cu,
1900 int is_block);
1901
1902 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1903 const gdb_byte *info_ptr,
1904 struct abbrev_info *abbrev);
1905
1906 static void free_stack_comp_unit (void *);
1907
1908 static hashval_t partial_die_hash (const void *item);
1909
1910 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1911
1912 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1913 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1914
1915 static void init_one_comp_unit (struct dwarf2_cu *cu,
1916 struct dwarf2_per_cu_data *per_cu);
1917
1918 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1919 struct die_info *comp_unit_die,
1920 enum language pretend_language);
1921
1922 static void free_heap_comp_unit (void *);
1923
1924 static void free_cached_comp_units (void *);
1925
1926 static void age_cached_comp_units (void);
1927
1928 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1929
1930 static struct type *set_die_type (struct die_info *, struct type *,
1931 struct dwarf2_cu *);
1932
1933 static void create_all_comp_units (struct objfile *);
1934
1935 static int create_all_type_units (struct objfile *);
1936
1937 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1938 enum language);
1939
1940 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1941 enum language);
1942
1943 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1944 enum language);
1945
1946 static void dwarf2_add_dependence (struct dwarf2_cu *,
1947 struct dwarf2_per_cu_data *);
1948
1949 static void dwarf2_mark (struct dwarf2_cu *);
1950
1951 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1952
1953 static struct type *get_die_type_at_offset (sect_offset,
1954 struct dwarf2_per_cu_data *);
1955
1956 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1957
1958 static void dwarf2_release_queue (void *dummy);
1959
1960 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1961 enum language pretend_language);
1962
1963 static void process_queue (void);
1964
1965 /* The return type of find_file_and_directory. Note, the enclosed
1966 string pointers are only valid while this object is valid. */
1967
1968 struct file_and_directory
1969 {
1970 /* The filename. This is never NULL. */
1971 const char *name;
1972
1973 /* The compilation directory. NULL if not known. If we needed to
1974 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1975 points directly to the DW_AT_comp_dir string attribute owned by
1976 the obstack that owns the DIE. */
1977 const char *comp_dir;
1978
1979 /* If we needed to build a new string for comp_dir, this is what
1980 owns the storage. */
1981 std::string comp_dir_storage;
1982 };
1983
1984 static file_and_directory find_file_and_directory (struct die_info *die,
1985 struct dwarf2_cu *cu);
1986
1987 static char *file_full_name (int file, struct line_header *lh,
1988 const char *comp_dir);
1989
1990 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1991 enum class rcuh_kind { COMPILE, TYPE };
1992
1993 static const gdb_byte *read_and_check_comp_unit_head
1994 (struct comp_unit_head *header,
1995 struct dwarf2_section_info *section,
1996 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1997 rcuh_kind section_kind);
1998
1999 static void init_cutu_and_read_dies
2000 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2001 int use_existing_cu, int keep,
2002 die_reader_func_ftype *die_reader_func, void *data);
2003
2004 static void init_cutu_and_read_dies_simple
2005 (struct dwarf2_per_cu_data *this_cu,
2006 die_reader_func_ftype *die_reader_func, void *data);
2007
2008 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2009
2010 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2011
2012 static struct dwo_unit *lookup_dwo_unit_in_dwp
2013 (struct dwp_file *dwp_file, const char *comp_dir,
2014 ULONGEST signature, int is_debug_types);
2015
2016 static struct dwp_file *get_dwp_file (void);
2017
2018 static struct dwo_unit *lookup_dwo_comp_unit
2019 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2020
2021 static struct dwo_unit *lookup_dwo_type_unit
2022 (struct signatured_type *, const char *, const char *);
2023
2024 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2025
2026 static void free_dwo_file_cleanup (void *);
2027
2028 static void process_cu_includes (void);
2029
2030 static void check_producer (struct dwarf2_cu *cu);
2031
2032 static void free_line_header_voidp (void *arg);
2033 \f
2034 /* Various complaints about symbol reading that don't abort the process. */
2035
2036 static void
2037 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2038 {
2039 complaint (&symfile_complaints,
2040 _("statement list doesn't fit in .debug_line section"));
2041 }
2042
2043 static void
2044 dwarf2_debug_line_missing_file_complaint (void)
2045 {
2046 complaint (&symfile_complaints,
2047 _(".debug_line section has line data without a file"));
2048 }
2049
2050 static void
2051 dwarf2_debug_line_missing_end_sequence_complaint (void)
2052 {
2053 complaint (&symfile_complaints,
2054 _(".debug_line section has line "
2055 "program sequence without an end"));
2056 }
2057
2058 static void
2059 dwarf2_complex_location_expr_complaint (void)
2060 {
2061 complaint (&symfile_complaints, _("location expression too complex"));
2062 }
2063
2064 static void
2065 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2066 int arg3)
2067 {
2068 complaint (&symfile_complaints,
2069 _("const value length mismatch for '%s', got %d, expected %d"),
2070 arg1, arg2, arg3);
2071 }
2072
2073 static void
2074 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2075 {
2076 complaint (&symfile_complaints,
2077 _("debug info runs off end of %s section"
2078 " [in module %s]"),
2079 get_section_name (section),
2080 get_section_file_name (section));
2081 }
2082
2083 static void
2084 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2085 {
2086 complaint (&symfile_complaints,
2087 _("macro debug info contains a "
2088 "malformed macro definition:\n`%s'"),
2089 arg1);
2090 }
2091
2092 static void
2093 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2094 {
2095 complaint (&symfile_complaints,
2096 _("invalid attribute class or form for '%s' in '%s'"),
2097 arg1, arg2);
2098 }
2099
2100 /* Hash function for line_header_hash. */
2101
2102 static hashval_t
2103 line_header_hash (const struct line_header *ofs)
2104 {
2105 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2106 }
2107
2108 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2109
2110 static hashval_t
2111 line_header_hash_voidp (const void *item)
2112 {
2113 const struct line_header *ofs = (const struct line_header *) item;
2114
2115 return line_header_hash (ofs);
2116 }
2117
2118 /* Equality function for line_header_hash. */
2119
2120 static int
2121 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2122 {
2123 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2124 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2125
2126 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2127 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2128 }
2129
2130 \f
2131 #if WORDS_BIGENDIAN
2132
2133 /* Convert VALUE between big- and little-endian. */
2134 static offset_type
2135 byte_swap (offset_type value)
2136 {
2137 offset_type result;
2138
2139 result = (value & 0xff) << 24;
2140 result |= (value & 0xff00) << 8;
2141 result |= (value & 0xff0000) >> 8;
2142 result |= (value & 0xff000000) >> 24;
2143 return result;
2144 }
2145
2146 #define MAYBE_SWAP(V) byte_swap (V)
2147
2148 #else
2149 #define MAYBE_SWAP(V) (V)
2150 #endif /* WORDS_BIGENDIAN */
2151
2152 /* Read the given attribute value as an address, taking the attribute's
2153 form into account. */
2154
2155 static CORE_ADDR
2156 attr_value_as_address (struct attribute *attr)
2157 {
2158 CORE_ADDR addr;
2159
2160 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2161 {
2162 /* Aside from a few clearly defined exceptions, attributes that
2163 contain an address must always be in DW_FORM_addr form.
2164 Unfortunately, some compilers happen to be violating this
2165 requirement by encoding addresses using other forms, such
2166 as DW_FORM_data4 for example. For those broken compilers,
2167 we try to do our best, without any guarantee of success,
2168 to interpret the address correctly. It would also be nice
2169 to generate a complaint, but that would require us to maintain
2170 a list of legitimate cases where a non-address form is allowed,
2171 as well as update callers to pass in at least the CU's DWARF
2172 version. This is more overhead than what we're willing to
2173 expand for a pretty rare case. */
2174 addr = DW_UNSND (attr);
2175 }
2176 else
2177 addr = DW_ADDR (attr);
2178
2179 return addr;
2180 }
2181
2182 /* The suffix for an index file. */
2183 #define INDEX_SUFFIX ".gdb-index"
2184
2185 /* Try to locate the sections we need for DWARF 2 debugging
2186 information and return true if we have enough to do something.
2187 NAMES points to the dwarf2 section names, or is NULL if the standard
2188 ELF names are used. */
2189
2190 int
2191 dwarf2_has_info (struct objfile *objfile,
2192 const struct dwarf2_debug_sections *names)
2193 {
2194 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2195 objfile_data (objfile, dwarf2_objfile_data_key));
2196 if (!dwarf2_per_objfile)
2197 {
2198 /* Initialize per-objfile state. */
2199 struct dwarf2_per_objfile *data
2200 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2201
2202 memset (data, 0, sizeof (*data));
2203 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2204 dwarf2_per_objfile = data;
2205
2206 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2207 (void *) names);
2208 dwarf2_per_objfile->objfile = objfile;
2209 }
2210 return (!dwarf2_per_objfile->info.is_virtual
2211 && dwarf2_per_objfile->info.s.section != NULL
2212 && !dwarf2_per_objfile->abbrev.is_virtual
2213 && dwarf2_per_objfile->abbrev.s.section != NULL);
2214 }
2215
2216 /* Return the containing section of virtual section SECTION. */
2217
2218 static struct dwarf2_section_info *
2219 get_containing_section (const struct dwarf2_section_info *section)
2220 {
2221 gdb_assert (section->is_virtual);
2222 return section->s.containing_section;
2223 }
2224
2225 /* Return the bfd owner of SECTION. */
2226
2227 static struct bfd *
2228 get_section_bfd_owner (const struct dwarf2_section_info *section)
2229 {
2230 if (section->is_virtual)
2231 {
2232 section = get_containing_section (section);
2233 gdb_assert (!section->is_virtual);
2234 }
2235 return section->s.section->owner;
2236 }
2237
2238 /* Return the bfd section of SECTION.
2239 Returns NULL if the section is not present. */
2240
2241 static asection *
2242 get_section_bfd_section (const struct dwarf2_section_info *section)
2243 {
2244 if (section->is_virtual)
2245 {
2246 section = get_containing_section (section);
2247 gdb_assert (!section->is_virtual);
2248 }
2249 return section->s.section;
2250 }
2251
2252 /* Return the name of SECTION. */
2253
2254 static const char *
2255 get_section_name (const struct dwarf2_section_info *section)
2256 {
2257 asection *sectp = get_section_bfd_section (section);
2258
2259 gdb_assert (sectp != NULL);
2260 return bfd_section_name (get_section_bfd_owner (section), sectp);
2261 }
2262
2263 /* Return the name of the file SECTION is in. */
2264
2265 static const char *
2266 get_section_file_name (const struct dwarf2_section_info *section)
2267 {
2268 bfd *abfd = get_section_bfd_owner (section);
2269
2270 return bfd_get_filename (abfd);
2271 }
2272
2273 /* Return the id of SECTION.
2274 Returns 0 if SECTION doesn't exist. */
2275
2276 static int
2277 get_section_id (const struct dwarf2_section_info *section)
2278 {
2279 asection *sectp = get_section_bfd_section (section);
2280
2281 if (sectp == NULL)
2282 return 0;
2283 return sectp->id;
2284 }
2285
2286 /* Return the flags of SECTION.
2287 SECTION (or containing section if this is a virtual section) must exist. */
2288
2289 static int
2290 get_section_flags (const struct dwarf2_section_info *section)
2291 {
2292 asection *sectp = get_section_bfd_section (section);
2293
2294 gdb_assert (sectp != NULL);
2295 return bfd_get_section_flags (sectp->owner, sectp);
2296 }
2297
2298 /* When loading sections, we look either for uncompressed section or for
2299 compressed section names. */
2300
2301 static int
2302 section_is_p (const char *section_name,
2303 const struct dwarf2_section_names *names)
2304 {
2305 if (names->normal != NULL
2306 && strcmp (section_name, names->normal) == 0)
2307 return 1;
2308 if (names->compressed != NULL
2309 && strcmp (section_name, names->compressed) == 0)
2310 return 1;
2311 return 0;
2312 }
2313
2314 /* This function is mapped across the sections and remembers the
2315 offset and size of each of the debugging sections we are interested
2316 in. */
2317
2318 static void
2319 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2320 {
2321 const struct dwarf2_debug_sections *names;
2322 flagword aflag = bfd_get_section_flags (abfd, sectp);
2323
2324 if (vnames == NULL)
2325 names = &dwarf2_elf_names;
2326 else
2327 names = (const struct dwarf2_debug_sections *) vnames;
2328
2329 if ((aflag & SEC_HAS_CONTENTS) == 0)
2330 {
2331 }
2332 else if (section_is_p (sectp->name, &names->info))
2333 {
2334 dwarf2_per_objfile->info.s.section = sectp;
2335 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2336 }
2337 else if (section_is_p (sectp->name, &names->abbrev))
2338 {
2339 dwarf2_per_objfile->abbrev.s.section = sectp;
2340 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2341 }
2342 else if (section_is_p (sectp->name, &names->line))
2343 {
2344 dwarf2_per_objfile->line.s.section = sectp;
2345 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2346 }
2347 else if (section_is_p (sectp->name, &names->loc))
2348 {
2349 dwarf2_per_objfile->loc.s.section = sectp;
2350 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2351 }
2352 else if (section_is_p (sectp->name, &names->loclists))
2353 {
2354 dwarf2_per_objfile->loclists.s.section = sectp;
2355 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2356 }
2357 else if (section_is_p (sectp->name, &names->macinfo))
2358 {
2359 dwarf2_per_objfile->macinfo.s.section = sectp;
2360 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2361 }
2362 else if (section_is_p (sectp->name, &names->macro))
2363 {
2364 dwarf2_per_objfile->macro.s.section = sectp;
2365 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2366 }
2367 else if (section_is_p (sectp->name, &names->str))
2368 {
2369 dwarf2_per_objfile->str.s.section = sectp;
2370 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2371 }
2372 else if (section_is_p (sectp->name, &names->line_str))
2373 {
2374 dwarf2_per_objfile->line_str.s.section = sectp;
2375 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names->addr))
2378 {
2379 dwarf2_per_objfile->addr.s.section = sectp;
2380 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names->frame))
2383 {
2384 dwarf2_per_objfile->frame.s.section = sectp;
2385 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names->eh_frame))
2388 {
2389 dwarf2_per_objfile->eh_frame.s.section = sectp;
2390 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names->ranges))
2393 {
2394 dwarf2_per_objfile->ranges.s.section = sectp;
2395 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names->rnglists))
2398 {
2399 dwarf2_per_objfile->rnglists.s.section = sectp;
2400 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names->types))
2403 {
2404 struct dwarf2_section_info type_section;
2405
2406 memset (&type_section, 0, sizeof (type_section));
2407 type_section.s.section = sectp;
2408 type_section.size = bfd_get_section_size (sectp);
2409
2410 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2411 &type_section);
2412 }
2413 else if (section_is_p (sectp->name, &names->gdb_index))
2414 {
2415 dwarf2_per_objfile->gdb_index.s.section = sectp;
2416 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2417 }
2418
2419 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2420 && bfd_section_vma (abfd, sectp) == 0)
2421 dwarf2_per_objfile->has_section_at_zero = 1;
2422 }
2423
2424 /* A helper function that decides whether a section is empty,
2425 or not present. */
2426
2427 static int
2428 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2429 {
2430 if (section->is_virtual)
2431 return section->size == 0;
2432 return section->s.section == NULL || section->size == 0;
2433 }
2434
2435 /* Read the contents of the section INFO.
2436 OBJFILE is the main object file, but not necessarily the file where
2437 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2438 of the DWO file.
2439 If the section is compressed, uncompress it before returning. */
2440
2441 static void
2442 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2443 {
2444 asection *sectp;
2445 bfd *abfd;
2446 gdb_byte *buf, *retbuf;
2447
2448 if (info->readin)
2449 return;
2450 info->buffer = NULL;
2451 info->readin = 1;
2452
2453 if (dwarf2_section_empty_p (info))
2454 return;
2455
2456 sectp = get_section_bfd_section (info);
2457
2458 /* If this is a virtual section we need to read in the real one first. */
2459 if (info->is_virtual)
2460 {
2461 struct dwarf2_section_info *containing_section =
2462 get_containing_section (info);
2463
2464 gdb_assert (sectp != NULL);
2465 if ((sectp->flags & SEC_RELOC) != 0)
2466 {
2467 error (_("Dwarf Error: DWP format V2 with relocations is not"
2468 " supported in section %s [in module %s]"),
2469 get_section_name (info), get_section_file_name (info));
2470 }
2471 dwarf2_read_section (objfile, containing_section);
2472 /* Other code should have already caught virtual sections that don't
2473 fit. */
2474 gdb_assert (info->virtual_offset + info->size
2475 <= containing_section->size);
2476 /* If the real section is empty or there was a problem reading the
2477 section we shouldn't get here. */
2478 gdb_assert (containing_section->buffer != NULL);
2479 info->buffer = containing_section->buffer + info->virtual_offset;
2480 return;
2481 }
2482
2483 /* If the section has relocations, we must read it ourselves.
2484 Otherwise we attach it to the BFD. */
2485 if ((sectp->flags & SEC_RELOC) == 0)
2486 {
2487 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2488 return;
2489 }
2490
2491 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2492 info->buffer = buf;
2493
2494 /* When debugging .o files, we may need to apply relocations; see
2495 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2496 We never compress sections in .o files, so we only need to
2497 try this when the section is not compressed. */
2498 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2499 if (retbuf != NULL)
2500 {
2501 info->buffer = retbuf;
2502 return;
2503 }
2504
2505 abfd = get_section_bfd_owner (info);
2506 gdb_assert (abfd != NULL);
2507
2508 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2509 || bfd_bread (buf, info->size, abfd) != info->size)
2510 {
2511 error (_("Dwarf Error: Can't read DWARF data"
2512 " in section %s [in module %s]"),
2513 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2514 }
2515 }
2516
2517 /* A helper function that returns the size of a section in a safe way.
2518 If you are positive that the section has been read before using the
2519 size, then it is safe to refer to the dwarf2_section_info object's
2520 "size" field directly. In other cases, you must call this
2521 function, because for compressed sections the size field is not set
2522 correctly until the section has been read. */
2523
2524 static bfd_size_type
2525 dwarf2_section_size (struct objfile *objfile,
2526 struct dwarf2_section_info *info)
2527 {
2528 if (!info->readin)
2529 dwarf2_read_section (objfile, info);
2530 return info->size;
2531 }
2532
2533 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2534 SECTION_NAME. */
2535
2536 void
2537 dwarf2_get_section_info (struct objfile *objfile,
2538 enum dwarf2_section_enum sect,
2539 asection **sectp, const gdb_byte **bufp,
2540 bfd_size_type *sizep)
2541 {
2542 struct dwarf2_per_objfile *data
2543 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2544 dwarf2_objfile_data_key);
2545 struct dwarf2_section_info *info;
2546
2547 /* We may see an objfile without any DWARF, in which case we just
2548 return nothing. */
2549 if (data == NULL)
2550 {
2551 *sectp = NULL;
2552 *bufp = NULL;
2553 *sizep = 0;
2554 return;
2555 }
2556 switch (sect)
2557 {
2558 case DWARF2_DEBUG_FRAME:
2559 info = &data->frame;
2560 break;
2561 case DWARF2_EH_FRAME:
2562 info = &data->eh_frame;
2563 break;
2564 default:
2565 gdb_assert_not_reached ("unexpected section");
2566 }
2567
2568 dwarf2_read_section (objfile, info);
2569
2570 *sectp = get_section_bfd_section (info);
2571 *bufp = info->buffer;
2572 *sizep = info->size;
2573 }
2574
2575 /* A helper function to find the sections for a .dwz file. */
2576
2577 static void
2578 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2579 {
2580 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2581
2582 /* Note that we only support the standard ELF names, because .dwz
2583 is ELF-only (at the time of writing). */
2584 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2585 {
2586 dwz_file->abbrev.s.section = sectp;
2587 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2588 }
2589 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2590 {
2591 dwz_file->info.s.section = sectp;
2592 dwz_file->info.size = bfd_get_section_size (sectp);
2593 }
2594 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2595 {
2596 dwz_file->str.s.section = sectp;
2597 dwz_file->str.size = bfd_get_section_size (sectp);
2598 }
2599 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2600 {
2601 dwz_file->line.s.section = sectp;
2602 dwz_file->line.size = bfd_get_section_size (sectp);
2603 }
2604 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2605 {
2606 dwz_file->macro.s.section = sectp;
2607 dwz_file->macro.size = bfd_get_section_size (sectp);
2608 }
2609 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2610 {
2611 dwz_file->gdb_index.s.section = sectp;
2612 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2613 }
2614 }
2615
2616 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2617 there is no .gnu_debugaltlink section in the file. Error if there
2618 is such a section but the file cannot be found. */
2619
2620 static struct dwz_file *
2621 dwarf2_get_dwz_file (void)
2622 {
2623 char *data;
2624 struct cleanup *cleanup;
2625 const char *filename;
2626 struct dwz_file *result;
2627 bfd_size_type buildid_len_arg;
2628 size_t buildid_len;
2629 bfd_byte *buildid;
2630
2631 if (dwarf2_per_objfile->dwz_file != NULL)
2632 return dwarf2_per_objfile->dwz_file;
2633
2634 bfd_set_error (bfd_error_no_error);
2635 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2636 &buildid_len_arg, &buildid);
2637 if (data == NULL)
2638 {
2639 if (bfd_get_error () == bfd_error_no_error)
2640 return NULL;
2641 error (_("could not read '.gnu_debugaltlink' section: %s"),
2642 bfd_errmsg (bfd_get_error ()));
2643 }
2644 cleanup = make_cleanup (xfree, data);
2645 make_cleanup (xfree, buildid);
2646
2647 buildid_len = (size_t) buildid_len_arg;
2648
2649 filename = (const char *) data;
2650
2651 std::string abs_storage;
2652 if (!IS_ABSOLUTE_PATH (filename))
2653 {
2654 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2655
2656 make_cleanup (xfree, abs);
2657 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2658 filename = abs_storage.c_str ();
2659 }
2660
2661 /* First try the file name given in the section. If that doesn't
2662 work, try to use the build-id instead. */
2663 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2664 if (dwz_bfd != NULL)
2665 {
2666 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2667 dwz_bfd.release ();
2668 }
2669
2670 if (dwz_bfd == NULL)
2671 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2672
2673 if (dwz_bfd == NULL)
2674 error (_("could not find '.gnu_debugaltlink' file for %s"),
2675 objfile_name (dwarf2_per_objfile->objfile));
2676
2677 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2678 struct dwz_file);
2679 result->dwz_bfd = dwz_bfd.release ();
2680
2681 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2682
2683 do_cleanups (cleanup);
2684
2685 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2686 dwarf2_per_objfile->dwz_file = result;
2687 return result;
2688 }
2689 \f
2690 /* DWARF quick_symbols_functions support. */
2691
2692 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2693 unique line tables, so we maintain a separate table of all .debug_line
2694 derived entries to support the sharing.
2695 All the quick functions need is the list of file names. We discard the
2696 line_header when we're done and don't need to record it here. */
2697 struct quick_file_names
2698 {
2699 /* The data used to construct the hash key. */
2700 struct stmt_list_hash hash;
2701
2702 /* The number of entries in file_names, real_names. */
2703 unsigned int num_file_names;
2704
2705 /* The file names from the line table, after being run through
2706 file_full_name. */
2707 const char **file_names;
2708
2709 /* The file names from the line table after being run through
2710 gdb_realpath. These are computed lazily. */
2711 const char **real_names;
2712 };
2713
2714 /* When using the index (and thus not using psymtabs), each CU has an
2715 object of this type. This is used to hold information needed by
2716 the various "quick" methods. */
2717 struct dwarf2_per_cu_quick_data
2718 {
2719 /* The file table. This can be NULL if there was no file table
2720 or it's currently not read in.
2721 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2722 struct quick_file_names *file_names;
2723
2724 /* The corresponding symbol table. This is NULL if symbols for this
2725 CU have not yet been read. */
2726 struct compunit_symtab *compunit_symtab;
2727
2728 /* A temporary mark bit used when iterating over all CUs in
2729 expand_symtabs_matching. */
2730 unsigned int mark : 1;
2731
2732 /* True if we've tried to read the file table and found there isn't one.
2733 There will be no point in trying to read it again next time. */
2734 unsigned int no_file_data : 1;
2735 };
2736
2737 /* Utility hash function for a stmt_list_hash. */
2738
2739 static hashval_t
2740 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2741 {
2742 hashval_t v = 0;
2743
2744 if (stmt_list_hash->dwo_unit != NULL)
2745 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2746 v += to_underlying (stmt_list_hash->line_sect_off);
2747 return v;
2748 }
2749
2750 /* Utility equality function for a stmt_list_hash. */
2751
2752 static int
2753 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2754 const struct stmt_list_hash *rhs)
2755 {
2756 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2757 return 0;
2758 if (lhs->dwo_unit != NULL
2759 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2760 return 0;
2761
2762 return lhs->line_sect_off == rhs->line_sect_off;
2763 }
2764
2765 /* Hash function for a quick_file_names. */
2766
2767 static hashval_t
2768 hash_file_name_entry (const void *e)
2769 {
2770 const struct quick_file_names *file_data
2771 = (const struct quick_file_names *) e;
2772
2773 return hash_stmt_list_entry (&file_data->hash);
2774 }
2775
2776 /* Equality function for a quick_file_names. */
2777
2778 static int
2779 eq_file_name_entry (const void *a, const void *b)
2780 {
2781 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2782 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2783
2784 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2785 }
2786
2787 /* Delete function for a quick_file_names. */
2788
2789 static void
2790 delete_file_name_entry (void *e)
2791 {
2792 struct quick_file_names *file_data = (struct quick_file_names *) e;
2793 int i;
2794
2795 for (i = 0; i < file_data->num_file_names; ++i)
2796 {
2797 xfree ((void*) file_data->file_names[i]);
2798 if (file_data->real_names)
2799 xfree ((void*) file_data->real_names[i]);
2800 }
2801
2802 /* The space for the struct itself lives on objfile_obstack,
2803 so we don't free it here. */
2804 }
2805
2806 /* Create a quick_file_names hash table. */
2807
2808 static htab_t
2809 create_quick_file_names_table (unsigned int nr_initial_entries)
2810 {
2811 return htab_create_alloc (nr_initial_entries,
2812 hash_file_name_entry, eq_file_name_entry,
2813 delete_file_name_entry, xcalloc, xfree);
2814 }
2815
2816 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2817 have to be created afterwards. You should call age_cached_comp_units after
2818 processing PER_CU->CU. dw2_setup must have been already called. */
2819
2820 static void
2821 load_cu (struct dwarf2_per_cu_data *per_cu)
2822 {
2823 if (per_cu->is_debug_types)
2824 load_full_type_unit (per_cu);
2825 else
2826 load_full_comp_unit (per_cu, language_minimal);
2827
2828 if (per_cu->cu == NULL)
2829 return; /* Dummy CU. */
2830
2831 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2832 }
2833
2834 /* Read in the symbols for PER_CU. */
2835
2836 static void
2837 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2838 {
2839 struct cleanup *back_to;
2840
2841 /* Skip type_unit_groups, reading the type units they contain
2842 is handled elsewhere. */
2843 if (IS_TYPE_UNIT_GROUP (per_cu))
2844 return;
2845
2846 back_to = make_cleanup (dwarf2_release_queue, NULL);
2847
2848 if (dwarf2_per_objfile->using_index
2849 ? per_cu->v.quick->compunit_symtab == NULL
2850 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2851 {
2852 queue_comp_unit (per_cu, language_minimal);
2853 load_cu (per_cu);
2854
2855 /* If we just loaded a CU from a DWO, and we're working with an index
2856 that may badly handle TUs, load all the TUs in that DWO as well.
2857 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2858 if (!per_cu->is_debug_types
2859 && per_cu->cu != NULL
2860 && per_cu->cu->dwo_unit != NULL
2861 && dwarf2_per_objfile->index_table != NULL
2862 && dwarf2_per_objfile->index_table->version <= 7
2863 /* DWP files aren't supported yet. */
2864 && get_dwp_file () == NULL)
2865 queue_and_load_all_dwo_tus (per_cu);
2866 }
2867
2868 process_queue ();
2869
2870 /* Age the cache, releasing compilation units that have not
2871 been used recently. */
2872 age_cached_comp_units ();
2873
2874 do_cleanups (back_to);
2875 }
2876
2877 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2878 the objfile from which this CU came. Returns the resulting symbol
2879 table. */
2880
2881 static struct compunit_symtab *
2882 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2883 {
2884 gdb_assert (dwarf2_per_objfile->using_index);
2885 if (!per_cu->v.quick->compunit_symtab)
2886 {
2887 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2888 increment_reading_symtab ();
2889 dw2_do_instantiate_symtab (per_cu);
2890 process_cu_includes ();
2891 do_cleanups (back_to);
2892 }
2893
2894 return per_cu->v.quick->compunit_symtab;
2895 }
2896
2897 /* Return the CU/TU given its index.
2898
2899 This is intended for loops like:
2900
2901 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2902 + dwarf2_per_objfile->n_type_units); ++i)
2903 {
2904 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2905
2906 ...;
2907 }
2908 */
2909
2910 static struct dwarf2_per_cu_data *
2911 dw2_get_cutu (int index)
2912 {
2913 if (index >= dwarf2_per_objfile->n_comp_units)
2914 {
2915 index -= dwarf2_per_objfile->n_comp_units;
2916 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2917 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2918 }
2919
2920 return dwarf2_per_objfile->all_comp_units[index];
2921 }
2922
2923 /* Return the CU given its index.
2924 This differs from dw2_get_cutu in that it's for when you know INDEX
2925 refers to a CU. */
2926
2927 static struct dwarf2_per_cu_data *
2928 dw2_get_cu (int index)
2929 {
2930 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2931
2932 return dwarf2_per_objfile->all_comp_units[index];
2933 }
2934
2935 /* A helper for create_cus_from_index that handles a given list of
2936 CUs. */
2937
2938 static void
2939 create_cus_from_index_list (struct objfile *objfile,
2940 const gdb_byte *cu_list, offset_type n_elements,
2941 struct dwarf2_section_info *section,
2942 int is_dwz,
2943 int base_offset)
2944 {
2945 offset_type i;
2946
2947 for (i = 0; i < n_elements; i += 2)
2948 {
2949 gdb_static_assert (sizeof (ULONGEST) >= 8);
2950
2951 sect_offset sect_off
2952 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2953 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2954 cu_list += 2 * 8;
2955
2956 dwarf2_per_cu_data *the_cu
2957 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2958 struct dwarf2_per_cu_data);
2959 the_cu->sect_off = sect_off;
2960 the_cu->length = length;
2961 the_cu->objfile = objfile;
2962 the_cu->section = section;
2963 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_quick_data);
2965 the_cu->is_dwz = is_dwz;
2966 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2967 }
2968 }
2969
2970 /* Read the CU list from the mapped index, and use it to create all
2971 the CU objects for this objfile. */
2972
2973 static void
2974 create_cus_from_index (struct objfile *objfile,
2975 const gdb_byte *cu_list, offset_type cu_list_elements,
2976 const gdb_byte *dwz_list, offset_type dwz_elements)
2977 {
2978 struct dwz_file *dwz;
2979
2980 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2981 dwarf2_per_objfile->all_comp_units =
2982 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2983 dwarf2_per_objfile->n_comp_units);
2984
2985 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2986 &dwarf2_per_objfile->info, 0, 0);
2987
2988 if (dwz_elements == 0)
2989 return;
2990
2991 dwz = dwarf2_get_dwz_file ();
2992 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2993 cu_list_elements / 2);
2994 }
2995
2996 /* Create the signatured type hash table from the index. */
2997
2998 static void
2999 create_signatured_type_table_from_index (struct objfile *objfile,
3000 struct dwarf2_section_info *section,
3001 const gdb_byte *bytes,
3002 offset_type elements)
3003 {
3004 offset_type i;
3005 htab_t sig_types_hash;
3006
3007 dwarf2_per_objfile->n_type_units
3008 = dwarf2_per_objfile->n_allocated_type_units
3009 = elements / 3;
3010 dwarf2_per_objfile->all_type_units =
3011 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3012
3013 sig_types_hash = allocate_signatured_type_table (objfile);
3014
3015 for (i = 0; i < elements; i += 3)
3016 {
3017 struct signatured_type *sig_type;
3018 ULONGEST signature;
3019 void **slot;
3020 cu_offset type_offset_in_tu;
3021
3022 gdb_static_assert (sizeof (ULONGEST) >= 8);
3023 sect_offset sect_off
3024 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3025 type_offset_in_tu
3026 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3027 BFD_ENDIAN_LITTLE);
3028 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3029 bytes += 3 * 8;
3030
3031 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3032 struct signatured_type);
3033 sig_type->signature = signature;
3034 sig_type->type_offset_in_tu = type_offset_in_tu;
3035 sig_type->per_cu.is_debug_types = 1;
3036 sig_type->per_cu.section = section;
3037 sig_type->per_cu.sect_off = sect_off;
3038 sig_type->per_cu.objfile = objfile;
3039 sig_type->per_cu.v.quick
3040 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3041 struct dwarf2_per_cu_quick_data);
3042
3043 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3044 *slot = sig_type;
3045
3046 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3047 }
3048
3049 dwarf2_per_objfile->signatured_types = sig_types_hash;
3050 }
3051
3052 /* Read the address map data from the mapped index, and use it to
3053 populate the objfile's psymtabs_addrmap. */
3054
3055 static void
3056 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3057 {
3058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3059 const gdb_byte *iter, *end;
3060 struct obstack temp_obstack;
3061 struct addrmap *mutable_map;
3062 struct cleanup *cleanup;
3063 CORE_ADDR baseaddr;
3064
3065 obstack_init (&temp_obstack);
3066 cleanup = make_cleanup_obstack_free (&temp_obstack);
3067 mutable_map = addrmap_create_mutable (&temp_obstack);
3068
3069 iter = index->address_table;
3070 end = iter + index->address_table_size;
3071
3072 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3073
3074 while (iter < end)
3075 {
3076 ULONGEST hi, lo, cu_index;
3077 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3078 iter += 8;
3079 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3080 iter += 8;
3081 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3082 iter += 4;
3083
3084 if (lo > hi)
3085 {
3086 complaint (&symfile_complaints,
3087 _(".gdb_index address table has invalid range (%s - %s)"),
3088 hex_string (lo), hex_string (hi));
3089 continue;
3090 }
3091
3092 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3093 {
3094 complaint (&symfile_complaints,
3095 _(".gdb_index address table has invalid CU number %u"),
3096 (unsigned) cu_index);
3097 continue;
3098 }
3099
3100 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3101 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3102 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3103 }
3104
3105 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3106 &objfile->objfile_obstack);
3107 do_cleanups (cleanup);
3108 }
3109
3110 /* The hash function for strings in the mapped index. This is the same as
3111 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3112 implementation. This is necessary because the hash function is tied to the
3113 format of the mapped index file. The hash values do not have to match with
3114 SYMBOL_HASH_NEXT.
3115
3116 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3117
3118 static hashval_t
3119 mapped_index_string_hash (int index_version, const void *p)
3120 {
3121 const unsigned char *str = (const unsigned char *) p;
3122 hashval_t r = 0;
3123 unsigned char c;
3124
3125 while ((c = *str++) != 0)
3126 {
3127 if (index_version >= 5)
3128 c = tolower (c);
3129 r = r * 67 + c - 113;
3130 }
3131
3132 return r;
3133 }
3134
3135 /* Find a slot in the mapped index INDEX for the object named NAME.
3136 If NAME is found, set *VEC_OUT to point to the CU vector in the
3137 constant pool and return 1. If NAME cannot be found, return 0. */
3138
3139 static int
3140 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3141 offset_type **vec_out)
3142 {
3143 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3144 offset_type hash;
3145 offset_type slot, step;
3146 int (*cmp) (const char *, const char *);
3147
3148 if (current_language->la_language == language_cplus
3149 || current_language->la_language == language_fortran
3150 || current_language->la_language == language_d)
3151 {
3152 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3153 not contain any. */
3154
3155 if (strchr (name, '(') != NULL)
3156 {
3157 char *without_params = cp_remove_params (name);
3158
3159 if (without_params != NULL)
3160 {
3161 make_cleanup (xfree, without_params);
3162 name = without_params;
3163 }
3164 }
3165 }
3166
3167 /* Index version 4 did not support case insensitive searches. But the
3168 indices for case insensitive languages are built in lowercase, therefore
3169 simulate our NAME being searched is also lowercased. */
3170 hash = mapped_index_string_hash ((index->version == 4
3171 && case_sensitivity == case_sensitive_off
3172 ? 5 : index->version),
3173 name);
3174
3175 slot = hash & (index->symbol_table_slots - 1);
3176 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3177 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3178
3179 for (;;)
3180 {
3181 /* Convert a slot number to an offset into the table. */
3182 offset_type i = 2 * slot;
3183 const char *str;
3184 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3185 {
3186 do_cleanups (back_to);
3187 return 0;
3188 }
3189
3190 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3191 if (!cmp (name, str))
3192 {
3193 *vec_out = (offset_type *) (index->constant_pool
3194 + MAYBE_SWAP (index->symbol_table[i + 1]));
3195 do_cleanups (back_to);
3196 return 1;
3197 }
3198
3199 slot = (slot + step) & (index->symbol_table_slots - 1);
3200 }
3201 }
3202
3203 /* A helper function that reads the .gdb_index from SECTION and fills
3204 in MAP. FILENAME is the name of the file containing the section;
3205 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3206 ok to use deprecated sections.
3207
3208 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3209 out parameters that are filled in with information about the CU and
3210 TU lists in the section.
3211
3212 Returns 1 if all went well, 0 otherwise. */
3213
3214 static int
3215 read_index_from_section (struct objfile *objfile,
3216 const char *filename,
3217 int deprecated_ok,
3218 struct dwarf2_section_info *section,
3219 struct mapped_index *map,
3220 const gdb_byte **cu_list,
3221 offset_type *cu_list_elements,
3222 const gdb_byte **types_list,
3223 offset_type *types_list_elements)
3224 {
3225 const gdb_byte *addr;
3226 offset_type version;
3227 offset_type *metadata;
3228 int i;
3229
3230 if (dwarf2_section_empty_p (section))
3231 return 0;
3232
3233 /* Older elfutils strip versions could keep the section in the main
3234 executable while splitting it for the separate debug info file. */
3235 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3236 return 0;
3237
3238 dwarf2_read_section (objfile, section);
3239
3240 addr = section->buffer;
3241 /* Version check. */
3242 version = MAYBE_SWAP (*(offset_type *) addr);
3243 /* Versions earlier than 3 emitted every copy of a psymbol. This
3244 causes the index to behave very poorly for certain requests. Version 3
3245 contained incomplete addrmap. So, it seems better to just ignore such
3246 indices. */
3247 if (version < 4)
3248 {
3249 static int warning_printed = 0;
3250 if (!warning_printed)
3251 {
3252 warning (_("Skipping obsolete .gdb_index section in %s."),
3253 filename);
3254 warning_printed = 1;
3255 }
3256 return 0;
3257 }
3258 /* Index version 4 uses a different hash function than index version
3259 5 and later.
3260
3261 Versions earlier than 6 did not emit psymbols for inlined
3262 functions. Using these files will cause GDB not to be able to
3263 set breakpoints on inlined functions by name, so we ignore these
3264 indices unless the user has done
3265 "set use-deprecated-index-sections on". */
3266 if (version < 6 && !deprecated_ok)
3267 {
3268 static int warning_printed = 0;
3269 if (!warning_printed)
3270 {
3271 warning (_("\
3272 Skipping deprecated .gdb_index section in %s.\n\
3273 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3274 to use the section anyway."),
3275 filename);
3276 warning_printed = 1;
3277 }
3278 return 0;
3279 }
3280 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3281 of the TU (for symbols coming from TUs),
3282 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3283 Plus gold-generated indices can have duplicate entries for global symbols,
3284 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3285 These are just performance bugs, and we can't distinguish gdb-generated
3286 indices from gold-generated ones, so issue no warning here. */
3287
3288 /* Indexes with higher version than the one supported by GDB may be no
3289 longer backward compatible. */
3290 if (version > 8)
3291 return 0;
3292
3293 map->version = version;
3294 map->total_size = section->size;
3295
3296 metadata = (offset_type *) (addr + sizeof (offset_type));
3297
3298 i = 0;
3299 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3300 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3301 / 8);
3302 ++i;
3303
3304 *types_list = addr + MAYBE_SWAP (metadata[i]);
3305 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3306 - MAYBE_SWAP (metadata[i]))
3307 / 8);
3308 ++i;
3309
3310 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3311 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3312 - MAYBE_SWAP (metadata[i]));
3313 ++i;
3314
3315 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3316 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3317 - MAYBE_SWAP (metadata[i]))
3318 / (2 * sizeof (offset_type)));
3319 ++i;
3320
3321 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3322
3323 return 1;
3324 }
3325
3326
3327 /* Read the index file. If everything went ok, initialize the "quick"
3328 elements of all the CUs and return 1. Otherwise, return 0. */
3329
3330 static int
3331 dwarf2_read_index (struct objfile *objfile)
3332 {
3333 struct mapped_index local_map, *map;
3334 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3335 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3336 struct dwz_file *dwz;
3337
3338 if (!read_index_from_section (objfile, objfile_name (objfile),
3339 use_deprecated_index_sections,
3340 &dwarf2_per_objfile->gdb_index, &local_map,
3341 &cu_list, &cu_list_elements,
3342 &types_list, &types_list_elements))
3343 return 0;
3344
3345 /* Don't use the index if it's empty. */
3346 if (local_map.symbol_table_slots == 0)
3347 return 0;
3348
3349 /* If there is a .dwz file, read it so we can get its CU list as
3350 well. */
3351 dwz = dwarf2_get_dwz_file ();
3352 if (dwz != NULL)
3353 {
3354 struct mapped_index dwz_map;
3355 const gdb_byte *dwz_types_ignore;
3356 offset_type dwz_types_elements_ignore;
3357
3358 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3359 1,
3360 &dwz->gdb_index, &dwz_map,
3361 &dwz_list, &dwz_list_elements,
3362 &dwz_types_ignore,
3363 &dwz_types_elements_ignore))
3364 {
3365 warning (_("could not read '.gdb_index' section from %s; skipping"),
3366 bfd_get_filename (dwz->dwz_bfd));
3367 return 0;
3368 }
3369 }
3370
3371 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3372 dwz_list_elements);
3373
3374 if (types_list_elements)
3375 {
3376 struct dwarf2_section_info *section;
3377
3378 /* We can only handle a single .debug_types when we have an
3379 index. */
3380 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3381 return 0;
3382
3383 section = VEC_index (dwarf2_section_info_def,
3384 dwarf2_per_objfile->types, 0);
3385
3386 create_signatured_type_table_from_index (objfile, section, types_list,
3387 types_list_elements);
3388 }
3389
3390 create_addrmap_from_index (objfile, &local_map);
3391
3392 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3393 *map = local_map;
3394
3395 dwarf2_per_objfile->index_table = map;
3396 dwarf2_per_objfile->using_index = 1;
3397 dwarf2_per_objfile->quick_file_names_table =
3398 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3399
3400 return 1;
3401 }
3402
3403 /* A helper for the "quick" functions which sets the global
3404 dwarf2_per_objfile according to OBJFILE. */
3405
3406 static void
3407 dw2_setup (struct objfile *objfile)
3408 {
3409 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3410 objfile_data (objfile, dwarf2_objfile_data_key));
3411 gdb_assert (dwarf2_per_objfile);
3412 }
3413
3414 /* die_reader_func for dw2_get_file_names. */
3415
3416 static void
3417 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3418 const gdb_byte *info_ptr,
3419 struct die_info *comp_unit_die,
3420 int has_children,
3421 void *data)
3422 {
3423 struct dwarf2_cu *cu = reader->cu;
3424 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3425 struct objfile *objfile = dwarf2_per_objfile->objfile;
3426 struct dwarf2_per_cu_data *lh_cu;
3427 struct attribute *attr;
3428 int i;
3429 void **slot;
3430 struct quick_file_names *qfn;
3431
3432 gdb_assert (! this_cu->is_debug_types);
3433
3434 /* Our callers never want to match partial units -- instead they
3435 will match the enclosing full CU. */
3436 if (comp_unit_die->tag == DW_TAG_partial_unit)
3437 {
3438 this_cu->v.quick->no_file_data = 1;
3439 return;
3440 }
3441
3442 lh_cu = this_cu;
3443 slot = NULL;
3444
3445 line_header_up lh;
3446 sect_offset line_offset {};
3447
3448 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3449 if (attr)
3450 {
3451 struct quick_file_names find_entry;
3452
3453 line_offset = (sect_offset) DW_UNSND (attr);
3454
3455 /* We may have already read in this line header (TU line header sharing).
3456 If we have we're done. */
3457 find_entry.hash.dwo_unit = cu->dwo_unit;
3458 find_entry.hash.line_sect_off = line_offset;
3459 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3460 &find_entry, INSERT);
3461 if (*slot != NULL)
3462 {
3463 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3464 return;
3465 }
3466
3467 lh = dwarf_decode_line_header (line_offset, cu);
3468 }
3469 if (lh == NULL)
3470 {
3471 lh_cu->v.quick->no_file_data = 1;
3472 return;
3473 }
3474
3475 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3476 qfn->hash.dwo_unit = cu->dwo_unit;
3477 qfn->hash.line_sect_off = line_offset;
3478 gdb_assert (slot != NULL);
3479 *slot = qfn;
3480
3481 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3482
3483 qfn->num_file_names = lh->file_names.size ();
3484 qfn->file_names =
3485 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3486 for (i = 0; i < lh->file_names.size (); ++i)
3487 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3488 qfn->real_names = NULL;
3489
3490 lh_cu->v.quick->file_names = qfn;
3491 }
3492
3493 /* A helper for the "quick" functions which attempts to read the line
3494 table for THIS_CU. */
3495
3496 static struct quick_file_names *
3497 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3498 {
3499 /* This should never be called for TUs. */
3500 gdb_assert (! this_cu->is_debug_types);
3501 /* Nor type unit groups. */
3502 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3503
3504 if (this_cu->v.quick->file_names != NULL)
3505 return this_cu->v.quick->file_names;
3506 /* If we know there is no line data, no point in looking again. */
3507 if (this_cu->v.quick->no_file_data)
3508 return NULL;
3509
3510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3511
3512 if (this_cu->v.quick->no_file_data)
3513 return NULL;
3514 return this_cu->v.quick->file_names;
3515 }
3516
3517 /* A helper for the "quick" functions which computes and caches the
3518 real path for a given file name from the line table. */
3519
3520 static const char *
3521 dw2_get_real_path (struct objfile *objfile,
3522 struct quick_file_names *qfn, int index)
3523 {
3524 if (qfn->real_names == NULL)
3525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3526 qfn->num_file_names, const char *);
3527
3528 if (qfn->real_names[index] == NULL)
3529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3530
3531 return qfn->real_names[index];
3532 }
3533
3534 static struct symtab *
3535 dw2_find_last_source_symtab (struct objfile *objfile)
3536 {
3537 struct compunit_symtab *cust;
3538 int index;
3539
3540 dw2_setup (objfile);
3541 index = dwarf2_per_objfile->n_comp_units - 1;
3542 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3543 if (cust == NULL)
3544 return NULL;
3545 return compunit_primary_filetab (cust);
3546 }
3547
3548 /* Traversal function for dw2_forget_cached_source_info. */
3549
3550 static int
3551 dw2_free_cached_file_names (void **slot, void *info)
3552 {
3553 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3554
3555 if (file_data->real_names)
3556 {
3557 int i;
3558
3559 for (i = 0; i < file_data->num_file_names; ++i)
3560 {
3561 xfree ((void*) file_data->real_names[i]);
3562 file_data->real_names[i] = NULL;
3563 }
3564 }
3565
3566 return 1;
3567 }
3568
3569 static void
3570 dw2_forget_cached_source_info (struct objfile *objfile)
3571 {
3572 dw2_setup (objfile);
3573
3574 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3575 dw2_free_cached_file_names, NULL);
3576 }
3577
3578 /* Helper function for dw2_map_symtabs_matching_filename that expands
3579 the symtabs and calls the iterator. */
3580
3581 static int
3582 dw2_map_expand_apply (struct objfile *objfile,
3583 struct dwarf2_per_cu_data *per_cu,
3584 const char *name, const char *real_path,
3585 gdb::function_view<bool (symtab *)> callback)
3586 {
3587 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3588
3589 /* Don't visit already-expanded CUs. */
3590 if (per_cu->v.quick->compunit_symtab)
3591 return 0;
3592
3593 /* This may expand more than one symtab, and we want to iterate over
3594 all of them. */
3595 dw2_instantiate_symtab (per_cu);
3596
3597 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3598 last_made, callback);
3599 }
3600
3601 /* Implementation of the map_symtabs_matching_filename method. */
3602
3603 static bool
3604 dw2_map_symtabs_matching_filename
3605 (struct objfile *objfile, const char *name, const char *real_path,
3606 gdb::function_view<bool (symtab *)> callback)
3607 {
3608 int i;
3609 const char *name_basename = lbasename (name);
3610
3611 dw2_setup (objfile);
3612
3613 /* The rule is CUs specify all the files, including those used by
3614 any TU, so there's no need to scan TUs here. */
3615
3616 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3617 {
3618 int j;
3619 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3620 struct quick_file_names *file_data;
3621
3622 /* We only need to look at symtabs not already expanded. */
3623 if (per_cu->v.quick->compunit_symtab)
3624 continue;
3625
3626 file_data = dw2_get_file_names (per_cu);
3627 if (file_data == NULL)
3628 continue;
3629
3630 for (j = 0; j < file_data->num_file_names; ++j)
3631 {
3632 const char *this_name = file_data->file_names[j];
3633 const char *this_real_name;
3634
3635 if (compare_filenames_for_search (this_name, name))
3636 {
3637 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3638 callback))
3639 return true;
3640 continue;
3641 }
3642
3643 /* Before we invoke realpath, which can get expensive when many
3644 files are involved, do a quick comparison of the basenames. */
3645 if (! basenames_may_differ
3646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3647 continue;
3648
3649 this_real_name = dw2_get_real_path (objfile, file_data, j);
3650 if (compare_filenames_for_search (this_real_name, name))
3651 {
3652 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3653 callback))
3654 return true;
3655 continue;
3656 }
3657
3658 if (real_path != NULL)
3659 {
3660 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3661 gdb_assert (IS_ABSOLUTE_PATH (name));
3662 if (this_real_name != NULL
3663 && FILENAME_CMP (real_path, this_real_name) == 0)
3664 {
3665 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3666 callback))
3667 return true;
3668 continue;
3669 }
3670 }
3671 }
3672 }
3673
3674 return false;
3675 }
3676
3677 /* Struct used to manage iterating over all CUs looking for a symbol. */
3678
3679 struct dw2_symtab_iterator
3680 {
3681 /* The internalized form of .gdb_index. */
3682 struct mapped_index *index;
3683 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3684 int want_specific_block;
3685 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3686 Unused if !WANT_SPECIFIC_BLOCK. */
3687 int block_index;
3688 /* The kind of symbol we're looking for. */
3689 domain_enum domain;
3690 /* The list of CUs from the index entry of the symbol,
3691 or NULL if not found. */
3692 offset_type *vec;
3693 /* The next element in VEC to look at. */
3694 int next;
3695 /* The number of elements in VEC, or zero if there is no match. */
3696 int length;
3697 /* Have we seen a global version of the symbol?
3698 If so we can ignore all further global instances.
3699 This is to work around gold/15646, inefficient gold-generated
3700 indices. */
3701 int global_seen;
3702 };
3703
3704 /* Initialize the index symtab iterator ITER.
3705 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3706 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3707
3708 static void
3709 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3710 struct mapped_index *index,
3711 int want_specific_block,
3712 int block_index,
3713 domain_enum domain,
3714 const char *name)
3715 {
3716 iter->index = index;
3717 iter->want_specific_block = want_specific_block;
3718 iter->block_index = block_index;
3719 iter->domain = domain;
3720 iter->next = 0;
3721 iter->global_seen = 0;
3722
3723 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3724 iter->length = MAYBE_SWAP (*iter->vec);
3725 else
3726 {
3727 iter->vec = NULL;
3728 iter->length = 0;
3729 }
3730 }
3731
3732 /* Return the next matching CU or NULL if there are no more. */
3733
3734 static struct dwarf2_per_cu_data *
3735 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3736 {
3737 for ( ; iter->next < iter->length; ++iter->next)
3738 {
3739 offset_type cu_index_and_attrs =
3740 MAYBE_SWAP (iter->vec[iter->next + 1]);
3741 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3742 struct dwarf2_per_cu_data *per_cu;
3743 int want_static = iter->block_index != GLOBAL_BLOCK;
3744 /* This value is only valid for index versions >= 7. */
3745 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3746 gdb_index_symbol_kind symbol_kind =
3747 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3748 /* Only check the symbol attributes if they're present.
3749 Indices prior to version 7 don't record them,
3750 and indices >= 7 may elide them for certain symbols
3751 (gold does this). */
3752 int attrs_valid =
3753 (iter->index->version >= 7
3754 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3755
3756 /* Don't crash on bad data. */
3757 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3758 + dwarf2_per_objfile->n_type_units))
3759 {
3760 complaint (&symfile_complaints,
3761 _(".gdb_index entry has bad CU index"
3762 " [in module %s]"),
3763 objfile_name (dwarf2_per_objfile->objfile));
3764 continue;
3765 }
3766
3767 per_cu = dw2_get_cutu (cu_index);
3768
3769 /* Skip if already read in. */
3770 if (per_cu->v.quick->compunit_symtab)
3771 continue;
3772
3773 /* Check static vs global. */
3774 if (attrs_valid)
3775 {
3776 if (iter->want_specific_block
3777 && want_static != is_static)
3778 continue;
3779 /* Work around gold/15646. */
3780 if (!is_static && iter->global_seen)
3781 continue;
3782 if (!is_static)
3783 iter->global_seen = 1;
3784 }
3785
3786 /* Only check the symbol's kind if it has one. */
3787 if (attrs_valid)
3788 {
3789 switch (iter->domain)
3790 {
3791 case VAR_DOMAIN:
3792 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3793 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3794 /* Some types are also in VAR_DOMAIN. */
3795 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3796 continue;
3797 break;
3798 case STRUCT_DOMAIN:
3799 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3800 continue;
3801 break;
3802 case LABEL_DOMAIN:
3803 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3804 continue;
3805 break;
3806 default:
3807 break;
3808 }
3809 }
3810
3811 ++iter->next;
3812 return per_cu;
3813 }
3814
3815 return NULL;
3816 }
3817
3818 static struct compunit_symtab *
3819 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3820 const char *name, domain_enum domain)
3821 {
3822 struct compunit_symtab *stab_best = NULL;
3823 struct mapped_index *index;
3824
3825 dw2_setup (objfile);
3826
3827 index = dwarf2_per_objfile->index_table;
3828
3829 /* index is NULL if OBJF_READNOW. */
3830 if (index)
3831 {
3832 struct dw2_symtab_iterator iter;
3833 struct dwarf2_per_cu_data *per_cu;
3834
3835 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3836
3837 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3838 {
3839 struct symbol *sym, *with_opaque = NULL;
3840 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3841 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3842 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3843
3844 sym = block_find_symbol (block, name, domain,
3845 block_find_non_opaque_type_preferred,
3846 &with_opaque);
3847
3848 /* Some caution must be observed with overloaded functions
3849 and methods, since the index will not contain any overload
3850 information (but NAME might contain it). */
3851
3852 if (sym != NULL
3853 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3854 return stab;
3855 if (with_opaque != NULL
3856 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3857 stab_best = stab;
3858
3859 /* Keep looking through other CUs. */
3860 }
3861 }
3862
3863 return stab_best;
3864 }
3865
3866 static void
3867 dw2_print_stats (struct objfile *objfile)
3868 {
3869 int i, total, count;
3870
3871 dw2_setup (objfile);
3872 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3873 count = 0;
3874 for (i = 0; i < total; ++i)
3875 {
3876 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3877
3878 if (!per_cu->v.quick->compunit_symtab)
3879 ++count;
3880 }
3881 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3882 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3883 }
3884
3885 /* This dumps minimal information about the index.
3886 It is called via "mt print objfiles".
3887 One use is to verify .gdb_index has been loaded by the
3888 gdb.dwarf2/gdb-index.exp testcase. */
3889
3890 static void
3891 dw2_dump (struct objfile *objfile)
3892 {
3893 dw2_setup (objfile);
3894 gdb_assert (dwarf2_per_objfile->using_index);
3895 printf_filtered (".gdb_index:");
3896 if (dwarf2_per_objfile->index_table != NULL)
3897 {
3898 printf_filtered (" version %d\n",
3899 dwarf2_per_objfile->index_table->version);
3900 }
3901 else
3902 printf_filtered (" faked for \"readnow\"\n");
3903 printf_filtered ("\n");
3904 }
3905
3906 static void
3907 dw2_relocate (struct objfile *objfile,
3908 const struct section_offsets *new_offsets,
3909 const struct section_offsets *delta)
3910 {
3911 /* There's nothing to relocate here. */
3912 }
3913
3914 static void
3915 dw2_expand_symtabs_for_function (struct objfile *objfile,
3916 const char *func_name)
3917 {
3918 struct mapped_index *index;
3919
3920 dw2_setup (objfile);
3921
3922 index = dwarf2_per_objfile->index_table;
3923
3924 /* index is NULL if OBJF_READNOW. */
3925 if (index)
3926 {
3927 struct dw2_symtab_iterator iter;
3928 struct dwarf2_per_cu_data *per_cu;
3929
3930 /* Note: It doesn't matter what we pass for block_index here. */
3931 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3932 func_name);
3933
3934 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3935 dw2_instantiate_symtab (per_cu);
3936 }
3937 }
3938
3939 static void
3940 dw2_expand_all_symtabs (struct objfile *objfile)
3941 {
3942 int i;
3943
3944 dw2_setup (objfile);
3945
3946 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3947 + dwarf2_per_objfile->n_type_units); ++i)
3948 {
3949 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3950
3951 dw2_instantiate_symtab (per_cu);
3952 }
3953 }
3954
3955 static void
3956 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3957 const char *fullname)
3958 {
3959 int i;
3960
3961 dw2_setup (objfile);
3962
3963 /* We don't need to consider type units here.
3964 This is only called for examining code, e.g. expand_line_sal.
3965 There can be an order of magnitude (or more) more type units
3966 than comp units, and we avoid them if we can. */
3967
3968 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3969 {
3970 int j;
3971 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3972 struct quick_file_names *file_data;
3973
3974 /* We only need to look at symtabs not already expanded. */
3975 if (per_cu->v.quick->compunit_symtab)
3976 continue;
3977
3978 file_data = dw2_get_file_names (per_cu);
3979 if (file_data == NULL)
3980 continue;
3981
3982 for (j = 0; j < file_data->num_file_names; ++j)
3983 {
3984 const char *this_fullname = file_data->file_names[j];
3985
3986 if (filename_cmp (this_fullname, fullname) == 0)
3987 {
3988 dw2_instantiate_symtab (per_cu);
3989 break;
3990 }
3991 }
3992 }
3993 }
3994
3995 static void
3996 dw2_map_matching_symbols (struct objfile *objfile,
3997 const char * name, domain_enum domain,
3998 int global,
3999 int (*callback) (struct block *,
4000 struct symbol *, void *),
4001 void *data, symbol_compare_ftype *match,
4002 symbol_compare_ftype *ordered_compare)
4003 {
4004 /* Currently unimplemented; used for Ada. The function can be called if the
4005 current language is Ada for a non-Ada objfile using GNU index. As Ada
4006 does not look for non-Ada symbols this function should just return. */
4007 }
4008
4009 static void
4010 dw2_expand_symtabs_matching
4011 (struct objfile *objfile,
4012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4013 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4014 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4015 enum search_domain kind)
4016 {
4017 int i;
4018 offset_type iter;
4019 struct mapped_index *index;
4020
4021 dw2_setup (objfile);
4022
4023 /* index_table is NULL if OBJF_READNOW. */
4024 if (!dwarf2_per_objfile->index_table)
4025 return;
4026 index = dwarf2_per_objfile->index_table;
4027
4028 if (file_matcher != NULL)
4029 {
4030 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4031 htab_eq_pointer,
4032 NULL, xcalloc, xfree));
4033 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4034 htab_eq_pointer,
4035 NULL, xcalloc, xfree));
4036
4037 /* The rule is CUs specify all the files, including those used by
4038 any TU, so there's no need to scan TUs here. */
4039
4040 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4041 {
4042 int j;
4043 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4044 struct quick_file_names *file_data;
4045 void **slot;
4046
4047 QUIT;
4048
4049 per_cu->v.quick->mark = 0;
4050
4051 /* We only need to look at symtabs not already expanded. */
4052 if (per_cu->v.quick->compunit_symtab)
4053 continue;
4054
4055 file_data = dw2_get_file_names (per_cu);
4056 if (file_data == NULL)
4057 continue;
4058
4059 if (htab_find (visited_not_found.get (), file_data) != NULL)
4060 continue;
4061 else if (htab_find (visited_found.get (), file_data) != NULL)
4062 {
4063 per_cu->v.quick->mark = 1;
4064 continue;
4065 }
4066
4067 for (j = 0; j < file_data->num_file_names; ++j)
4068 {
4069 const char *this_real_name;
4070
4071 if (file_matcher (file_data->file_names[j], false))
4072 {
4073 per_cu->v.quick->mark = 1;
4074 break;
4075 }
4076
4077 /* Before we invoke realpath, which can get expensive when many
4078 files are involved, do a quick comparison of the basenames. */
4079 if (!basenames_may_differ
4080 && !file_matcher (lbasename (file_data->file_names[j]),
4081 true))
4082 continue;
4083
4084 this_real_name = dw2_get_real_path (objfile, file_data, j);
4085 if (file_matcher (this_real_name, false))
4086 {
4087 per_cu->v.quick->mark = 1;
4088 break;
4089 }
4090 }
4091
4092 slot = htab_find_slot (per_cu->v.quick->mark
4093 ? visited_found.get ()
4094 : visited_not_found.get (),
4095 file_data, INSERT);
4096 *slot = file_data;
4097 }
4098 }
4099
4100 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4101 {
4102 offset_type idx = 2 * iter;
4103 const char *name;
4104 offset_type *vec, vec_len, vec_idx;
4105 int global_seen = 0;
4106
4107 QUIT;
4108
4109 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4110 continue;
4111
4112 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4113
4114 if (!symbol_matcher (name))
4115 continue;
4116
4117 /* The name was matched, now expand corresponding CUs that were
4118 marked. */
4119 vec = (offset_type *) (index->constant_pool
4120 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4121 vec_len = MAYBE_SWAP (vec[0]);
4122 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4123 {
4124 struct dwarf2_per_cu_data *per_cu;
4125 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4126 /* This value is only valid for index versions >= 7. */
4127 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4128 gdb_index_symbol_kind symbol_kind =
4129 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4130 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4131 /* Only check the symbol attributes if they're present.
4132 Indices prior to version 7 don't record them,
4133 and indices >= 7 may elide them for certain symbols
4134 (gold does this). */
4135 int attrs_valid =
4136 (index->version >= 7
4137 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4138
4139 /* Work around gold/15646. */
4140 if (attrs_valid)
4141 {
4142 if (!is_static && global_seen)
4143 continue;
4144 if (!is_static)
4145 global_seen = 1;
4146 }
4147
4148 /* Only check the symbol's kind if it has one. */
4149 if (attrs_valid)
4150 {
4151 switch (kind)
4152 {
4153 case VARIABLES_DOMAIN:
4154 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4155 continue;
4156 break;
4157 case FUNCTIONS_DOMAIN:
4158 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4159 continue;
4160 break;
4161 case TYPES_DOMAIN:
4162 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4163 continue;
4164 break;
4165 default:
4166 break;
4167 }
4168 }
4169
4170 /* Don't crash on bad data. */
4171 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4172 + dwarf2_per_objfile->n_type_units))
4173 {
4174 complaint (&symfile_complaints,
4175 _(".gdb_index entry has bad CU index"
4176 " [in module %s]"), objfile_name (objfile));
4177 continue;
4178 }
4179
4180 per_cu = dw2_get_cutu (cu_index);
4181 if (file_matcher == NULL || per_cu->v.quick->mark)
4182 {
4183 int symtab_was_null =
4184 (per_cu->v.quick->compunit_symtab == NULL);
4185
4186 dw2_instantiate_symtab (per_cu);
4187
4188 if (expansion_notify != NULL
4189 && symtab_was_null
4190 && per_cu->v.quick->compunit_symtab != NULL)
4191 {
4192 expansion_notify (per_cu->v.quick->compunit_symtab);
4193 }
4194 }
4195 }
4196 }
4197 }
4198
4199 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4200 symtab. */
4201
4202 static struct compunit_symtab *
4203 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4204 CORE_ADDR pc)
4205 {
4206 int i;
4207
4208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4210 return cust;
4211
4212 if (cust->includes == NULL)
4213 return NULL;
4214
4215 for (i = 0; cust->includes[i]; ++i)
4216 {
4217 struct compunit_symtab *s = cust->includes[i];
4218
4219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4220 if (s != NULL)
4221 return s;
4222 }
4223
4224 return NULL;
4225 }
4226
4227 static struct compunit_symtab *
4228 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4229 struct bound_minimal_symbol msymbol,
4230 CORE_ADDR pc,
4231 struct obj_section *section,
4232 int warn_if_readin)
4233 {
4234 struct dwarf2_per_cu_data *data;
4235 struct compunit_symtab *result;
4236
4237 dw2_setup (objfile);
4238
4239 if (!objfile->psymtabs_addrmap)
4240 return NULL;
4241
4242 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4243 pc);
4244 if (!data)
4245 return NULL;
4246
4247 if (warn_if_readin && data->v.quick->compunit_symtab)
4248 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4249 paddress (get_objfile_arch (objfile), pc));
4250
4251 result
4252 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4253 pc);
4254 gdb_assert (result != NULL);
4255 return result;
4256 }
4257
4258 static void
4259 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4260 void *data, int need_fullname)
4261 {
4262 int i;
4263 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4264 NULL, xcalloc, xfree));
4265
4266 dw2_setup (objfile);
4267
4268 /* The rule is CUs specify all the files, including those used by
4269 any TU, so there's no need to scan TUs here.
4270 We can ignore file names coming from already-expanded CUs. */
4271
4272 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4273 {
4274 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4275
4276 if (per_cu->v.quick->compunit_symtab)
4277 {
4278 void **slot = htab_find_slot (visited.get (),
4279 per_cu->v.quick->file_names,
4280 INSERT);
4281
4282 *slot = per_cu->v.quick->file_names;
4283 }
4284 }
4285
4286 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4287 {
4288 int j;
4289 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4290 struct quick_file_names *file_data;
4291 void **slot;
4292
4293 /* We only need to look at symtabs not already expanded. */
4294 if (per_cu->v.quick->compunit_symtab)
4295 continue;
4296
4297 file_data = dw2_get_file_names (per_cu);
4298 if (file_data == NULL)
4299 continue;
4300
4301 slot = htab_find_slot (visited.get (), file_data, INSERT);
4302 if (*slot)
4303 {
4304 /* Already visited. */
4305 continue;
4306 }
4307 *slot = file_data;
4308
4309 for (j = 0; j < file_data->num_file_names; ++j)
4310 {
4311 const char *this_real_name;
4312
4313 if (need_fullname)
4314 this_real_name = dw2_get_real_path (objfile, file_data, j);
4315 else
4316 this_real_name = NULL;
4317 (*fun) (file_data->file_names[j], this_real_name, data);
4318 }
4319 }
4320 }
4321
4322 static int
4323 dw2_has_symbols (struct objfile *objfile)
4324 {
4325 return 1;
4326 }
4327
4328 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4329 {
4330 dw2_has_symbols,
4331 dw2_find_last_source_symtab,
4332 dw2_forget_cached_source_info,
4333 dw2_map_symtabs_matching_filename,
4334 dw2_lookup_symbol,
4335 dw2_print_stats,
4336 dw2_dump,
4337 dw2_relocate,
4338 dw2_expand_symtabs_for_function,
4339 dw2_expand_all_symtabs,
4340 dw2_expand_symtabs_with_fullname,
4341 dw2_map_matching_symbols,
4342 dw2_expand_symtabs_matching,
4343 dw2_find_pc_sect_compunit_symtab,
4344 dw2_map_symbol_filenames
4345 };
4346
4347 /* Initialize for reading DWARF for this objfile. Return 0 if this
4348 file will use psymtabs, or 1 if using the GNU index. */
4349
4350 int
4351 dwarf2_initialize_objfile (struct objfile *objfile)
4352 {
4353 /* If we're about to read full symbols, don't bother with the
4354 indices. In this case we also don't care if some other debug
4355 format is making psymtabs, because they are all about to be
4356 expanded anyway. */
4357 if ((objfile->flags & OBJF_READNOW))
4358 {
4359 int i;
4360
4361 dwarf2_per_objfile->using_index = 1;
4362 create_all_comp_units (objfile);
4363 create_all_type_units (objfile);
4364 dwarf2_per_objfile->quick_file_names_table =
4365 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4366
4367 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4368 + dwarf2_per_objfile->n_type_units); ++i)
4369 {
4370 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4371
4372 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4373 struct dwarf2_per_cu_quick_data);
4374 }
4375
4376 /* Return 1 so that gdb sees the "quick" functions. However,
4377 these functions will be no-ops because we will have expanded
4378 all symtabs. */
4379 return 1;
4380 }
4381
4382 if (dwarf2_read_index (objfile))
4383 return 1;
4384
4385 return 0;
4386 }
4387
4388 \f
4389
4390 /* Build a partial symbol table. */
4391
4392 void
4393 dwarf2_build_psymtabs (struct objfile *objfile)
4394 {
4395
4396 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4397 {
4398 init_psymbol_list (objfile, 1024);
4399 }
4400
4401 TRY
4402 {
4403 /* This isn't really ideal: all the data we allocate on the
4404 objfile's obstack is still uselessly kept around. However,
4405 freeing it seems unsafe. */
4406 psymtab_discarder psymtabs (objfile);
4407 dwarf2_build_psymtabs_hard (objfile);
4408 psymtabs.keep ();
4409 }
4410 CATCH (except, RETURN_MASK_ERROR)
4411 {
4412 exception_print (gdb_stderr, except);
4413 }
4414 END_CATCH
4415 }
4416
4417 /* Return the total length of the CU described by HEADER. */
4418
4419 static unsigned int
4420 get_cu_length (const struct comp_unit_head *header)
4421 {
4422 return header->initial_length_size + header->length;
4423 }
4424
4425 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4426
4427 static inline bool
4428 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4429 {
4430 sect_offset bottom = cu_header->sect_off;
4431 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4432
4433 return sect_off >= bottom && sect_off < top;
4434 }
4435
4436 /* Find the base address of the compilation unit for range lists and
4437 location lists. It will normally be specified by DW_AT_low_pc.
4438 In DWARF-3 draft 4, the base address could be overridden by
4439 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4440 compilation units with discontinuous ranges. */
4441
4442 static void
4443 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4444 {
4445 struct attribute *attr;
4446
4447 cu->base_known = 0;
4448 cu->base_address = 0;
4449
4450 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4451 if (attr)
4452 {
4453 cu->base_address = attr_value_as_address (attr);
4454 cu->base_known = 1;
4455 }
4456 else
4457 {
4458 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4459 if (attr)
4460 {
4461 cu->base_address = attr_value_as_address (attr);
4462 cu->base_known = 1;
4463 }
4464 }
4465 }
4466
4467 /* Read in the comp unit header information from the debug_info at info_ptr.
4468 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4469 NOTE: This leaves members offset, first_die_offset to be filled in
4470 by the caller. */
4471
4472 static const gdb_byte *
4473 read_comp_unit_head (struct comp_unit_head *cu_header,
4474 const gdb_byte *info_ptr,
4475 struct dwarf2_section_info *section,
4476 rcuh_kind section_kind)
4477 {
4478 int signed_addr;
4479 unsigned int bytes_read;
4480 const char *filename = get_section_file_name (section);
4481 bfd *abfd = get_section_bfd_owner (section);
4482
4483 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4484 cu_header->initial_length_size = bytes_read;
4485 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4486 info_ptr += bytes_read;
4487 cu_header->version = read_2_bytes (abfd, info_ptr);
4488 info_ptr += 2;
4489 if (cu_header->version < 5)
4490 switch (section_kind)
4491 {
4492 case rcuh_kind::COMPILE:
4493 cu_header->unit_type = DW_UT_compile;
4494 break;
4495 case rcuh_kind::TYPE:
4496 cu_header->unit_type = DW_UT_type;
4497 break;
4498 default:
4499 internal_error (__FILE__, __LINE__,
4500 _("read_comp_unit_head: invalid section_kind"));
4501 }
4502 else
4503 {
4504 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4505 (read_1_byte (abfd, info_ptr));
4506 info_ptr += 1;
4507 switch (cu_header->unit_type)
4508 {
4509 case DW_UT_compile:
4510 if (section_kind != rcuh_kind::COMPILE)
4511 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4512 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4513 filename);
4514 break;
4515 case DW_UT_type:
4516 section_kind = rcuh_kind::TYPE;
4517 break;
4518 default:
4519 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4520 "(is %d, should be %d or %d) [in module %s]"),
4521 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4522 }
4523
4524 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4525 info_ptr += 1;
4526 }
4527 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4528 cu_header,
4529 &bytes_read);
4530 info_ptr += bytes_read;
4531 if (cu_header->version < 5)
4532 {
4533 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4534 info_ptr += 1;
4535 }
4536 signed_addr = bfd_get_sign_extend_vma (abfd);
4537 if (signed_addr < 0)
4538 internal_error (__FILE__, __LINE__,
4539 _("read_comp_unit_head: dwarf from non elf file"));
4540 cu_header->signed_addr_p = signed_addr;
4541
4542 if (section_kind == rcuh_kind::TYPE)
4543 {
4544 LONGEST type_offset;
4545
4546 cu_header->signature = read_8_bytes (abfd, info_ptr);
4547 info_ptr += 8;
4548
4549 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4550 info_ptr += bytes_read;
4551 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4552 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4553 error (_("Dwarf Error: Too big type_offset in compilation unit "
4554 "header (is %s) [in module %s]"), plongest (type_offset),
4555 filename);
4556 }
4557
4558 return info_ptr;
4559 }
4560
4561 /* Helper function that returns the proper abbrev section for
4562 THIS_CU. */
4563
4564 static struct dwarf2_section_info *
4565 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4566 {
4567 struct dwarf2_section_info *abbrev;
4568
4569 if (this_cu->is_dwz)
4570 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4571 else
4572 abbrev = &dwarf2_per_objfile->abbrev;
4573
4574 return abbrev;
4575 }
4576
4577 /* Subroutine of read_and_check_comp_unit_head and
4578 read_and_check_type_unit_head to simplify them.
4579 Perform various error checking on the header. */
4580
4581 static void
4582 error_check_comp_unit_head (struct comp_unit_head *header,
4583 struct dwarf2_section_info *section,
4584 struct dwarf2_section_info *abbrev_section)
4585 {
4586 const char *filename = get_section_file_name (section);
4587
4588 if (header->version < 2 || header->version > 5)
4589 error (_("Dwarf Error: wrong version in compilation unit header "
4590 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4591 filename);
4592
4593 if (to_underlying (header->abbrev_sect_off)
4594 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4595 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4596 "(offset 0x%x + 6) [in module %s]"),
4597 to_underlying (header->abbrev_sect_off),
4598 to_underlying (header->sect_off),
4599 filename);
4600
4601 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4602 avoid potential 32-bit overflow. */
4603 if (((ULONGEST) header->sect_off + get_cu_length (header))
4604 > section->size)
4605 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4606 "(offset 0x%x + 0) [in module %s]"),
4607 header->length, to_underlying (header->sect_off),
4608 filename);
4609 }
4610
4611 /* Read in a CU/TU header and perform some basic error checking.
4612 The contents of the header are stored in HEADER.
4613 The result is a pointer to the start of the first DIE. */
4614
4615 static const gdb_byte *
4616 read_and_check_comp_unit_head (struct comp_unit_head *header,
4617 struct dwarf2_section_info *section,
4618 struct dwarf2_section_info *abbrev_section,
4619 const gdb_byte *info_ptr,
4620 rcuh_kind section_kind)
4621 {
4622 const gdb_byte *beg_of_comp_unit = info_ptr;
4623 bfd *abfd = get_section_bfd_owner (section);
4624
4625 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4626
4627 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4628
4629 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4630
4631 error_check_comp_unit_head (header, section, abbrev_section);
4632
4633 return info_ptr;
4634 }
4635
4636 /* Fetch the abbreviation table offset from a comp or type unit header. */
4637
4638 static sect_offset
4639 read_abbrev_offset (struct dwarf2_section_info *section,
4640 sect_offset sect_off)
4641 {
4642 bfd *abfd = get_section_bfd_owner (section);
4643 const gdb_byte *info_ptr;
4644 unsigned int initial_length_size, offset_size;
4645 uint16_t version;
4646
4647 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4648 info_ptr = section->buffer + to_underlying (sect_off);
4649 read_initial_length (abfd, info_ptr, &initial_length_size);
4650 offset_size = initial_length_size == 4 ? 4 : 8;
4651 info_ptr += initial_length_size;
4652
4653 version = read_2_bytes (abfd, info_ptr);
4654 info_ptr += 2;
4655 if (version >= 5)
4656 {
4657 /* Skip unit type and address size. */
4658 info_ptr += 2;
4659 }
4660
4661 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4662 }
4663
4664 /* Allocate a new partial symtab for file named NAME and mark this new
4665 partial symtab as being an include of PST. */
4666
4667 static void
4668 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4669 struct objfile *objfile)
4670 {
4671 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4672
4673 if (!IS_ABSOLUTE_PATH (subpst->filename))
4674 {
4675 /* It shares objfile->objfile_obstack. */
4676 subpst->dirname = pst->dirname;
4677 }
4678
4679 subpst->textlow = 0;
4680 subpst->texthigh = 0;
4681
4682 subpst->dependencies
4683 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4684 subpst->dependencies[0] = pst;
4685 subpst->number_of_dependencies = 1;
4686
4687 subpst->globals_offset = 0;
4688 subpst->n_global_syms = 0;
4689 subpst->statics_offset = 0;
4690 subpst->n_static_syms = 0;
4691 subpst->compunit_symtab = NULL;
4692 subpst->read_symtab = pst->read_symtab;
4693 subpst->readin = 0;
4694
4695 /* No private part is necessary for include psymtabs. This property
4696 can be used to differentiate between such include psymtabs and
4697 the regular ones. */
4698 subpst->read_symtab_private = NULL;
4699 }
4700
4701 /* Read the Line Number Program data and extract the list of files
4702 included by the source file represented by PST. Build an include
4703 partial symtab for each of these included files. */
4704
4705 static void
4706 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4707 struct die_info *die,
4708 struct partial_symtab *pst)
4709 {
4710 line_header_up lh;
4711 struct attribute *attr;
4712
4713 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4714 if (attr)
4715 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4716 if (lh == NULL)
4717 return; /* No linetable, so no includes. */
4718
4719 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4720 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4721 }
4722
4723 static hashval_t
4724 hash_signatured_type (const void *item)
4725 {
4726 const struct signatured_type *sig_type
4727 = (const struct signatured_type *) item;
4728
4729 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4730 return sig_type->signature;
4731 }
4732
4733 static int
4734 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4735 {
4736 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4737 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4738
4739 return lhs->signature == rhs->signature;
4740 }
4741
4742 /* Allocate a hash table for signatured types. */
4743
4744 static htab_t
4745 allocate_signatured_type_table (struct objfile *objfile)
4746 {
4747 return htab_create_alloc_ex (41,
4748 hash_signatured_type,
4749 eq_signatured_type,
4750 NULL,
4751 &objfile->objfile_obstack,
4752 hashtab_obstack_allocate,
4753 dummy_obstack_deallocate);
4754 }
4755
4756 /* A helper function to add a signatured type CU to a table. */
4757
4758 static int
4759 add_signatured_type_cu_to_table (void **slot, void *datum)
4760 {
4761 struct signatured_type *sigt = (struct signatured_type *) *slot;
4762 struct signatured_type ***datap = (struct signatured_type ***) datum;
4763
4764 **datap = sigt;
4765 ++*datap;
4766
4767 return 1;
4768 }
4769
4770 /* A helper for create_debug_types_hash_table. Read types from SECTION
4771 and fill them into TYPES_HTAB. It will process only type units,
4772 therefore DW_UT_type. */
4773
4774 static void
4775 create_debug_type_hash_table (struct dwo_file *dwo_file,
4776 dwarf2_section_info *section, htab_t &types_htab,
4777 rcuh_kind section_kind)
4778 {
4779 struct objfile *objfile = dwarf2_per_objfile->objfile;
4780 struct dwarf2_section_info *abbrev_section;
4781 bfd *abfd;
4782 const gdb_byte *info_ptr, *end_ptr;
4783
4784 abbrev_section = (dwo_file != NULL
4785 ? &dwo_file->sections.abbrev
4786 : &dwarf2_per_objfile->abbrev);
4787
4788 if (dwarf_read_debug)
4789 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4790 get_section_name (section),
4791 get_section_file_name (abbrev_section));
4792
4793 dwarf2_read_section (objfile, section);
4794 info_ptr = section->buffer;
4795
4796 if (info_ptr == NULL)
4797 return;
4798
4799 /* We can't set abfd until now because the section may be empty or
4800 not present, in which case the bfd is unknown. */
4801 abfd = get_section_bfd_owner (section);
4802
4803 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4804 because we don't need to read any dies: the signature is in the
4805 header. */
4806
4807 end_ptr = info_ptr + section->size;
4808 while (info_ptr < end_ptr)
4809 {
4810 struct signatured_type *sig_type;
4811 struct dwo_unit *dwo_tu;
4812 void **slot;
4813 const gdb_byte *ptr = info_ptr;
4814 struct comp_unit_head header;
4815 unsigned int length;
4816
4817 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4818
4819 /* Initialize it due to a false compiler warning. */
4820 header.signature = -1;
4821 header.type_cu_offset_in_tu = (cu_offset) -1;
4822
4823 /* We need to read the type's signature in order to build the hash
4824 table, but we don't need anything else just yet. */
4825
4826 ptr = read_and_check_comp_unit_head (&header, section,
4827 abbrev_section, ptr, section_kind);
4828
4829 length = get_cu_length (&header);
4830
4831 /* Skip dummy type units. */
4832 if (ptr >= info_ptr + length
4833 || peek_abbrev_code (abfd, ptr) == 0
4834 || header.unit_type != DW_UT_type)
4835 {
4836 info_ptr += length;
4837 continue;
4838 }
4839
4840 if (types_htab == NULL)
4841 {
4842 if (dwo_file)
4843 types_htab = allocate_dwo_unit_table (objfile);
4844 else
4845 types_htab = allocate_signatured_type_table (objfile);
4846 }
4847
4848 if (dwo_file)
4849 {
4850 sig_type = NULL;
4851 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4852 struct dwo_unit);
4853 dwo_tu->dwo_file = dwo_file;
4854 dwo_tu->signature = header.signature;
4855 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4856 dwo_tu->section = section;
4857 dwo_tu->sect_off = sect_off;
4858 dwo_tu->length = length;
4859 }
4860 else
4861 {
4862 /* N.B.: type_offset is not usable if this type uses a DWO file.
4863 The real type_offset is in the DWO file. */
4864 dwo_tu = NULL;
4865 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4866 struct signatured_type);
4867 sig_type->signature = header.signature;
4868 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4869 sig_type->per_cu.objfile = objfile;
4870 sig_type->per_cu.is_debug_types = 1;
4871 sig_type->per_cu.section = section;
4872 sig_type->per_cu.sect_off = sect_off;
4873 sig_type->per_cu.length = length;
4874 }
4875
4876 slot = htab_find_slot (types_htab,
4877 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4878 INSERT);
4879 gdb_assert (slot != NULL);
4880 if (*slot != NULL)
4881 {
4882 sect_offset dup_sect_off;
4883
4884 if (dwo_file)
4885 {
4886 const struct dwo_unit *dup_tu
4887 = (const struct dwo_unit *) *slot;
4888
4889 dup_sect_off = dup_tu->sect_off;
4890 }
4891 else
4892 {
4893 const struct signatured_type *dup_tu
4894 = (const struct signatured_type *) *slot;
4895
4896 dup_sect_off = dup_tu->per_cu.sect_off;
4897 }
4898
4899 complaint (&symfile_complaints,
4900 _("debug type entry at offset 0x%x is duplicate to"
4901 " the entry at offset 0x%x, signature %s"),
4902 to_underlying (sect_off), to_underlying (dup_sect_off),
4903 hex_string (header.signature));
4904 }
4905 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4906
4907 if (dwarf_read_debug > 1)
4908 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4909 to_underlying (sect_off),
4910 hex_string (header.signature));
4911
4912 info_ptr += length;
4913 }
4914 }
4915
4916 /* Create the hash table of all entries in the .debug_types
4917 (or .debug_types.dwo) section(s).
4918 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4919 otherwise it is NULL.
4920
4921 The result is a pointer to the hash table or NULL if there are no types.
4922
4923 Note: This function processes DWO files only, not DWP files. */
4924
4925 static void
4926 create_debug_types_hash_table (struct dwo_file *dwo_file,
4927 VEC (dwarf2_section_info_def) *types,
4928 htab_t &types_htab)
4929 {
4930 int ix;
4931 struct dwarf2_section_info *section;
4932
4933 if (VEC_empty (dwarf2_section_info_def, types))
4934 return;
4935
4936 for (ix = 0;
4937 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4938 ++ix)
4939 create_debug_type_hash_table (dwo_file, section, types_htab,
4940 rcuh_kind::TYPE);
4941 }
4942
4943 /* Create the hash table of all entries in the .debug_types section,
4944 and initialize all_type_units.
4945 The result is zero if there is an error (e.g. missing .debug_types section),
4946 otherwise non-zero. */
4947
4948 static int
4949 create_all_type_units (struct objfile *objfile)
4950 {
4951 htab_t types_htab = NULL;
4952 struct signatured_type **iter;
4953
4954 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4955 rcuh_kind::COMPILE);
4956 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4957 if (types_htab == NULL)
4958 {
4959 dwarf2_per_objfile->signatured_types = NULL;
4960 return 0;
4961 }
4962
4963 dwarf2_per_objfile->signatured_types = types_htab;
4964
4965 dwarf2_per_objfile->n_type_units
4966 = dwarf2_per_objfile->n_allocated_type_units
4967 = htab_elements (types_htab);
4968 dwarf2_per_objfile->all_type_units =
4969 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4970 iter = &dwarf2_per_objfile->all_type_units[0];
4971 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4972 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4973 == dwarf2_per_objfile->n_type_units);
4974
4975 return 1;
4976 }
4977
4978 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4979 If SLOT is non-NULL, it is the entry to use in the hash table.
4980 Otherwise we find one. */
4981
4982 static struct signatured_type *
4983 add_type_unit (ULONGEST sig, void **slot)
4984 {
4985 struct objfile *objfile = dwarf2_per_objfile->objfile;
4986 int n_type_units = dwarf2_per_objfile->n_type_units;
4987 struct signatured_type *sig_type;
4988
4989 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4990 ++n_type_units;
4991 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4992 {
4993 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4994 dwarf2_per_objfile->n_allocated_type_units = 1;
4995 dwarf2_per_objfile->n_allocated_type_units *= 2;
4996 dwarf2_per_objfile->all_type_units
4997 = XRESIZEVEC (struct signatured_type *,
4998 dwarf2_per_objfile->all_type_units,
4999 dwarf2_per_objfile->n_allocated_type_units);
5000 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5001 }
5002 dwarf2_per_objfile->n_type_units = n_type_units;
5003
5004 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5005 struct signatured_type);
5006 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5007 sig_type->signature = sig;
5008 sig_type->per_cu.is_debug_types = 1;
5009 if (dwarf2_per_objfile->using_index)
5010 {
5011 sig_type->per_cu.v.quick =
5012 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5013 struct dwarf2_per_cu_quick_data);
5014 }
5015
5016 if (slot == NULL)
5017 {
5018 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5019 sig_type, INSERT);
5020 }
5021 gdb_assert (*slot == NULL);
5022 *slot = sig_type;
5023 /* The rest of sig_type must be filled in by the caller. */
5024 return sig_type;
5025 }
5026
5027 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5028 Fill in SIG_ENTRY with DWO_ENTRY. */
5029
5030 static void
5031 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5032 struct signatured_type *sig_entry,
5033 struct dwo_unit *dwo_entry)
5034 {
5035 /* Make sure we're not clobbering something we don't expect to. */
5036 gdb_assert (! sig_entry->per_cu.queued);
5037 gdb_assert (sig_entry->per_cu.cu == NULL);
5038 if (dwarf2_per_objfile->using_index)
5039 {
5040 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5041 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5042 }
5043 else
5044 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5045 gdb_assert (sig_entry->signature == dwo_entry->signature);
5046 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5047 gdb_assert (sig_entry->type_unit_group == NULL);
5048 gdb_assert (sig_entry->dwo_unit == NULL);
5049
5050 sig_entry->per_cu.section = dwo_entry->section;
5051 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5052 sig_entry->per_cu.length = dwo_entry->length;
5053 sig_entry->per_cu.reading_dwo_directly = 1;
5054 sig_entry->per_cu.objfile = objfile;
5055 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5056 sig_entry->dwo_unit = dwo_entry;
5057 }
5058
5059 /* Subroutine of lookup_signatured_type.
5060 If we haven't read the TU yet, create the signatured_type data structure
5061 for a TU to be read in directly from a DWO file, bypassing the stub.
5062 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5063 using .gdb_index, then when reading a CU we want to stay in the DWO file
5064 containing that CU. Otherwise we could end up reading several other DWO
5065 files (due to comdat folding) to process the transitive closure of all the
5066 mentioned TUs, and that can be slow. The current DWO file will have every
5067 type signature that it needs.
5068 We only do this for .gdb_index because in the psymtab case we already have
5069 to read all the DWOs to build the type unit groups. */
5070
5071 static struct signatured_type *
5072 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5073 {
5074 struct objfile *objfile = dwarf2_per_objfile->objfile;
5075 struct dwo_file *dwo_file;
5076 struct dwo_unit find_dwo_entry, *dwo_entry;
5077 struct signatured_type find_sig_entry, *sig_entry;
5078 void **slot;
5079
5080 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5081
5082 /* If TU skeletons have been removed then we may not have read in any
5083 TUs yet. */
5084 if (dwarf2_per_objfile->signatured_types == NULL)
5085 {
5086 dwarf2_per_objfile->signatured_types
5087 = allocate_signatured_type_table (objfile);
5088 }
5089
5090 /* We only ever need to read in one copy of a signatured type.
5091 Use the global signatured_types array to do our own comdat-folding
5092 of types. If this is the first time we're reading this TU, and
5093 the TU has an entry in .gdb_index, replace the recorded data from
5094 .gdb_index with this TU. */
5095
5096 find_sig_entry.signature = sig;
5097 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5098 &find_sig_entry, INSERT);
5099 sig_entry = (struct signatured_type *) *slot;
5100
5101 /* We can get here with the TU already read, *or* in the process of being
5102 read. Don't reassign the global entry to point to this DWO if that's
5103 the case. Also note that if the TU is already being read, it may not
5104 have come from a DWO, the program may be a mix of Fission-compiled
5105 code and non-Fission-compiled code. */
5106
5107 /* Have we already tried to read this TU?
5108 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5109 needn't exist in the global table yet). */
5110 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5111 return sig_entry;
5112
5113 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5114 dwo_unit of the TU itself. */
5115 dwo_file = cu->dwo_unit->dwo_file;
5116
5117 /* Ok, this is the first time we're reading this TU. */
5118 if (dwo_file->tus == NULL)
5119 return NULL;
5120 find_dwo_entry.signature = sig;
5121 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5122 if (dwo_entry == NULL)
5123 return NULL;
5124
5125 /* If the global table doesn't have an entry for this TU, add one. */
5126 if (sig_entry == NULL)
5127 sig_entry = add_type_unit (sig, slot);
5128
5129 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5130 sig_entry->per_cu.tu_read = 1;
5131 return sig_entry;
5132 }
5133
5134 /* Subroutine of lookup_signatured_type.
5135 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5136 then try the DWP file. If the TU stub (skeleton) has been removed then
5137 it won't be in .gdb_index. */
5138
5139 static struct signatured_type *
5140 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5141 {
5142 struct objfile *objfile = dwarf2_per_objfile->objfile;
5143 struct dwp_file *dwp_file = get_dwp_file ();
5144 struct dwo_unit *dwo_entry;
5145 struct signatured_type find_sig_entry, *sig_entry;
5146 void **slot;
5147
5148 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5149 gdb_assert (dwp_file != NULL);
5150
5151 /* If TU skeletons have been removed then we may not have read in any
5152 TUs yet. */
5153 if (dwarf2_per_objfile->signatured_types == NULL)
5154 {
5155 dwarf2_per_objfile->signatured_types
5156 = allocate_signatured_type_table (objfile);
5157 }
5158
5159 find_sig_entry.signature = sig;
5160 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5161 &find_sig_entry, INSERT);
5162 sig_entry = (struct signatured_type *) *slot;
5163
5164 /* Have we already tried to read this TU?
5165 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5166 needn't exist in the global table yet). */
5167 if (sig_entry != NULL)
5168 return sig_entry;
5169
5170 if (dwp_file->tus == NULL)
5171 return NULL;
5172 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5173 sig, 1 /* is_debug_types */);
5174 if (dwo_entry == NULL)
5175 return NULL;
5176
5177 sig_entry = add_type_unit (sig, slot);
5178 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5179
5180 return sig_entry;
5181 }
5182
5183 /* Lookup a signature based type for DW_FORM_ref_sig8.
5184 Returns NULL if signature SIG is not present in the table.
5185 It is up to the caller to complain about this. */
5186
5187 static struct signatured_type *
5188 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5189 {
5190 if (cu->dwo_unit
5191 && dwarf2_per_objfile->using_index)
5192 {
5193 /* We're in a DWO/DWP file, and we're using .gdb_index.
5194 These cases require special processing. */
5195 if (get_dwp_file () == NULL)
5196 return lookup_dwo_signatured_type (cu, sig);
5197 else
5198 return lookup_dwp_signatured_type (cu, sig);
5199 }
5200 else
5201 {
5202 struct signatured_type find_entry, *entry;
5203
5204 if (dwarf2_per_objfile->signatured_types == NULL)
5205 return NULL;
5206 find_entry.signature = sig;
5207 entry = ((struct signatured_type *)
5208 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5209 return entry;
5210 }
5211 }
5212 \f
5213 /* Low level DIE reading support. */
5214
5215 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5216
5217 static void
5218 init_cu_die_reader (struct die_reader_specs *reader,
5219 struct dwarf2_cu *cu,
5220 struct dwarf2_section_info *section,
5221 struct dwo_file *dwo_file)
5222 {
5223 gdb_assert (section->readin && section->buffer != NULL);
5224 reader->abfd = get_section_bfd_owner (section);
5225 reader->cu = cu;
5226 reader->dwo_file = dwo_file;
5227 reader->die_section = section;
5228 reader->buffer = section->buffer;
5229 reader->buffer_end = section->buffer + section->size;
5230 reader->comp_dir = NULL;
5231 }
5232
5233 /* Subroutine of init_cutu_and_read_dies to simplify it.
5234 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5235 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5236 already.
5237
5238 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5239 from it to the DIE in the DWO. If NULL we are skipping the stub.
5240 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5241 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5242 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5243 STUB_COMP_DIR may be non-NULL.
5244 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5245 are filled in with the info of the DIE from the DWO file.
5246 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5247 provided an abbrev table to use.
5248 The result is non-zero if a valid (non-dummy) DIE was found. */
5249
5250 static int
5251 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5252 struct dwo_unit *dwo_unit,
5253 int abbrev_table_provided,
5254 struct die_info *stub_comp_unit_die,
5255 const char *stub_comp_dir,
5256 struct die_reader_specs *result_reader,
5257 const gdb_byte **result_info_ptr,
5258 struct die_info **result_comp_unit_die,
5259 int *result_has_children)
5260 {
5261 struct objfile *objfile = dwarf2_per_objfile->objfile;
5262 struct dwarf2_cu *cu = this_cu->cu;
5263 struct dwarf2_section_info *section;
5264 bfd *abfd;
5265 const gdb_byte *begin_info_ptr, *info_ptr;
5266 ULONGEST signature; /* Or dwo_id. */
5267 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5268 int i,num_extra_attrs;
5269 struct dwarf2_section_info *dwo_abbrev_section;
5270 struct attribute *attr;
5271 struct die_info *comp_unit_die;
5272
5273 /* At most one of these may be provided. */
5274 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5275
5276 /* These attributes aren't processed until later:
5277 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5278 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5279 referenced later. However, these attributes are found in the stub
5280 which we won't have later. In order to not impose this complication
5281 on the rest of the code, we read them here and copy them to the
5282 DWO CU/TU die. */
5283
5284 stmt_list = NULL;
5285 low_pc = NULL;
5286 high_pc = NULL;
5287 ranges = NULL;
5288 comp_dir = NULL;
5289
5290 if (stub_comp_unit_die != NULL)
5291 {
5292 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5293 DWO file. */
5294 if (! this_cu->is_debug_types)
5295 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5296 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5297 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5298 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5299 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5300
5301 /* There should be a DW_AT_addr_base attribute here (if needed).
5302 We need the value before we can process DW_FORM_GNU_addr_index. */
5303 cu->addr_base = 0;
5304 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5305 if (attr)
5306 cu->addr_base = DW_UNSND (attr);
5307
5308 /* There should be a DW_AT_ranges_base attribute here (if needed).
5309 We need the value before we can process DW_AT_ranges. */
5310 cu->ranges_base = 0;
5311 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5312 if (attr)
5313 cu->ranges_base = DW_UNSND (attr);
5314 }
5315 else if (stub_comp_dir != NULL)
5316 {
5317 /* Reconstruct the comp_dir attribute to simplify the code below. */
5318 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5319 comp_dir->name = DW_AT_comp_dir;
5320 comp_dir->form = DW_FORM_string;
5321 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5322 DW_STRING (comp_dir) = stub_comp_dir;
5323 }
5324
5325 /* Set up for reading the DWO CU/TU. */
5326 cu->dwo_unit = dwo_unit;
5327 section = dwo_unit->section;
5328 dwarf2_read_section (objfile, section);
5329 abfd = get_section_bfd_owner (section);
5330 begin_info_ptr = info_ptr = (section->buffer
5331 + to_underlying (dwo_unit->sect_off));
5332 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5333 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5334
5335 if (this_cu->is_debug_types)
5336 {
5337 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5338
5339 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5340 dwo_abbrev_section,
5341 info_ptr, rcuh_kind::TYPE);
5342 /* This is not an assert because it can be caused by bad debug info. */
5343 if (sig_type->signature != cu->header.signature)
5344 {
5345 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5346 " TU at offset 0x%x [in module %s]"),
5347 hex_string (sig_type->signature),
5348 hex_string (cu->header.signature),
5349 to_underlying (dwo_unit->sect_off),
5350 bfd_get_filename (abfd));
5351 }
5352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5353 /* For DWOs coming from DWP files, we don't know the CU length
5354 nor the type's offset in the TU until now. */
5355 dwo_unit->length = get_cu_length (&cu->header);
5356 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5357
5358 /* Establish the type offset that can be used to lookup the type.
5359 For DWO files, we don't know it until now. */
5360 sig_type->type_offset_in_section
5361 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5362 }
5363 else
5364 {
5365 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5366 dwo_abbrev_section,
5367 info_ptr, rcuh_kind::COMPILE);
5368 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5369 /* For DWOs coming from DWP files, we don't know the CU length
5370 until now. */
5371 dwo_unit->length = get_cu_length (&cu->header);
5372 }
5373
5374 /* Replace the CU's original abbrev table with the DWO's.
5375 Reminder: We can't read the abbrev table until we've read the header. */
5376 if (abbrev_table_provided)
5377 {
5378 /* Don't free the provided abbrev table, the caller of
5379 init_cutu_and_read_dies owns it. */
5380 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5381 /* Ensure the DWO abbrev table gets freed. */
5382 make_cleanup (dwarf2_free_abbrev_table, cu);
5383 }
5384 else
5385 {
5386 dwarf2_free_abbrev_table (cu);
5387 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5388 /* Leave any existing abbrev table cleanup as is. */
5389 }
5390
5391 /* Read in the die, but leave space to copy over the attributes
5392 from the stub. This has the benefit of simplifying the rest of
5393 the code - all the work to maintain the illusion of a single
5394 DW_TAG_{compile,type}_unit DIE is done here. */
5395 num_extra_attrs = ((stmt_list != NULL)
5396 + (low_pc != NULL)
5397 + (high_pc != NULL)
5398 + (ranges != NULL)
5399 + (comp_dir != NULL));
5400 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5401 result_has_children, num_extra_attrs);
5402
5403 /* Copy over the attributes from the stub to the DIE we just read in. */
5404 comp_unit_die = *result_comp_unit_die;
5405 i = comp_unit_die->num_attrs;
5406 if (stmt_list != NULL)
5407 comp_unit_die->attrs[i++] = *stmt_list;
5408 if (low_pc != NULL)
5409 comp_unit_die->attrs[i++] = *low_pc;
5410 if (high_pc != NULL)
5411 comp_unit_die->attrs[i++] = *high_pc;
5412 if (ranges != NULL)
5413 comp_unit_die->attrs[i++] = *ranges;
5414 if (comp_dir != NULL)
5415 comp_unit_die->attrs[i++] = *comp_dir;
5416 comp_unit_die->num_attrs += num_extra_attrs;
5417
5418 if (dwarf_die_debug)
5419 {
5420 fprintf_unfiltered (gdb_stdlog,
5421 "Read die from %s@0x%x of %s:\n",
5422 get_section_name (section),
5423 (unsigned) (begin_info_ptr - section->buffer),
5424 bfd_get_filename (abfd));
5425 dump_die (comp_unit_die, dwarf_die_debug);
5426 }
5427
5428 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5429 TUs by skipping the stub and going directly to the entry in the DWO file.
5430 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5431 to get it via circuitous means. Blech. */
5432 if (comp_dir != NULL)
5433 result_reader->comp_dir = DW_STRING (comp_dir);
5434
5435 /* Skip dummy compilation units. */
5436 if (info_ptr >= begin_info_ptr + dwo_unit->length
5437 || peek_abbrev_code (abfd, info_ptr) == 0)
5438 return 0;
5439
5440 *result_info_ptr = info_ptr;
5441 return 1;
5442 }
5443
5444 /* Subroutine of init_cutu_and_read_dies to simplify it.
5445 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5446 Returns NULL if the specified DWO unit cannot be found. */
5447
5448 static struct dwo_unit *
5449 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5450 struct die_info *comp_unit_die)
5451 {
5452 struct dwarf2_cu *cu = this_cu->cu;
5453 struct attribute *attr;
5454 ULONGEST signature;
5455 struct dwo_unit *dwo_unit;
5456 const char *comp_dir, *dwo_name;
5457
5458 gdb_assert (cu != NULL);
5459
5460 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5461 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5462 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5463
5464 if (this_cu->is_debug_types)
5465 {
5466 struct signatured_type *sig_type;
5467
5468 /* Since this_cu is the first member of struct signatured_type,
5469 we can go from a pointer to one to a pointer to the other. */
5470 sig_type = (struct signatured_type *) this_cu;
5471 signature = sig_type->signature;
5472 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5473 }
5474 else
5475 {
5476 struct attribute *attr;
5477
5478 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5479 if (! attr)
5480 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5481 " [in module %s]"),
5482 dwo_name, objfile_name (this_cu->objfile));
5483 signature = DW_UNSND (attr);
5484 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5485 signature);
5486 }
5487
5488 return dwo_unit;
5489 }
5490
5491 /* Subroutine of init_cutu_and_read_dies to simplify it.
5492 See it for a description of the parameters.
5493 Read a TU directly from a DWO file, bypassing the stub.
5494
5495 Note: This function could be a little bit simpler if we shared cleanups
5496 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5497 to do, so we keep this function self-contained. Or we could move this
5498 into our caller, but it's complex enough already. */
5499
5500 static void
5501 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5502 int use_existing_cu, int keep,
5503 die_reader_func_ftype *die_reader_func,
5504 void *data)
5505 {
5506 struct dwarf2_cu *cu;
5507 struct signatured_type *sig_type;
5508 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5509 struct die_reader_specs reader;
5510 const gdb_byte *info_ptr;
5511 struct die_info *comp_unit_die;
5512 int has_children;
5513
5514 /* Verify we can do the following downcast, and that we have the
5515 data we need. */
5516 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5517 sig_type = (struct signatured_type *) this_cu;
5518 gdb_assert (sig_type->dwo_unit != NULL);
5519
5520 cleanups = make_cleanup (null_cleanup, NULL);
5521
5522 if (use_existing_cu && this_cu->cu != NULL)
5523 {
5524 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5525 cu = this_cu->cu;
5526 /* There's no need to do the rereading_dwo_cu handling that
5527 init_cutu_and_read_dies does since we don't read the stub. */
5528 }
5529 else
5530 {
5531 /* If !use_existing_cu, this_cu->cu must be NULL. */
5532 gdb_assert (this_cu->cu == NULL);
5533 cu = XNEW (struct dwarf2_cu);
5534 init_one_comp_unit (cu, this_cu);
5535 /* If an error occurs while loading, release our storage. */
5536 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5537 }
5538
5539 /* A future optimization, if needed, would be to use an existing
5540 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5541 could share abbrev tables. */
5542
5543 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5544 0 /* abbrev_table_provided */,
5545 NULL /* stub_comp_unit_die */,
5546 sig_type->dwo_unit->dwo_file->comp_dir,
5547 &reader, &info_ptr,
5548 &comp_unit_die, &has_children) == 0)
5549 {
5550 /* Dummy die. */
5551 do_cleanups (cleanups);
5552 return;
5553 }
5554
5555 /* All the "real" work is done here. */
5556 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5557
5558 /* This duplicates the code in init_cutu_and_read_dies,
5559 but the alternative is making the latter more complex.
5560 This function is only for the special case of using DWO files directly:
5561 no point in overly complicating the general case just to handle this. */
5562 if (free_cu_cleanup != NULL)
5563 {
5564 if (keep)
5565 {
5566 /* We've successfully allocated this compilation unit. Let our
5567 caller clean it up when finished with it. */
5568 discard_cleanups (free_cu_cleanup);
5569
5570 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5571 So we have to manually free the abbrev table. */
5572 dwarf2_free_abbrev_table (cu);
5573
5574 /* Link this CU into read_in_chain. */
5575 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5576 dwarf2_per_objfile->read_in_chain = this_cu;
5577 }
5578 else
5579 do_cleanups (free_cu_cleanup);
5580 }
5581
5582 do_cleanups (cleanups);
5583 }
5584
5585 /* Initialize a CU (or TU) and read its DIEs.
5586 If the CU defers to a DWO file, read the DWO file as well.
5587
5588 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5589 Otherwise the table specified in the comp unit header is read in and used.
5590 This is an optimization for when we already have the abbrev table.
5591
5592 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5593 Otherwise, a new CU is allocated with xmalloc.
5594
5595 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5596 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5597
5598 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5599 linker) then DIE_READER_FUNC will not get called. */
5600
5601 static void
5602 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5603 struct abbrev_table *abbrev_table,
5604 int use_existing_cu, int keep,
5605 die_reader_func_ftype *die_reader_func,
5606 void *data)
5607 {
5608 struct objfile *objfile = dwarf2_per_objfile->objfile;
5609 struct dwarf2_section_info *section = this_cu->section;
5610 bfd *abfd = get_section_bfd_owner (section);
5611 struct dwarf2_cu *cu;
5612 const gdb_byte *begin_info_ptr, *info_ptr;
5613 struct die_reader_specs reader;
5614 struct die_info *comp_unit_die;
5615 int has_children;
5616 struct attribute *attr;
5617 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5618 struct signatured_type *sig_type = NULL;
5619 struct dwarf2_section_info *abbrev_section;
5620 /* Non-zero if CU currently points to a DWO file and we need to
5621 reread it. When this happens we need to reread the skeleton die
5622 before we can reread the DWO file (this only applies to CUs, not TUs). */
5623 int rereading_dwo_cu = 0;
5624
5625 if (dwarf_die_debug)
5626 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5627 this_cu->is_debug_types ? "type" : "comp",
5628 to_underlying (this_cu->sect_off));
5629
5630 if (use_existing_cu)
5631 gdb_assert (keep);
5632
5633 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5634 file (instead of going through the stub), short-circuit all of this. */
5635 if (this_cu->reading_dwo_directly)
5636 {
5637 /* Narrow down the scope of possibilities to have to understand. */
5638 gdb_assert (this_cu->is_debug_types);
5639 gdb_assert (abbrev_table == NULL);
5640 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5641 die_reader_func, data);
5642 return;
5643 }
5644
5645 cleanups = make_cleanup (null_cleanup, NULL);
5646
5647 /* This is cheap if the section is already read in. */
5648 dwarf2_read_section (objfile, section);
5649
5650 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5651
5652 abbrev_section = get_abbrev_section_for_cu (this_cu);
5653
5654 if (use_existing_cu && this_cu->cu != NULL)
5655 {
5656 cu = this_cu->cu;
5657 /* If this CU is from a DWO file we need to start over, we need to
5658 refetch the attributes from the skeleton CU.
5659 This could be optimized by retrieving those attributes from when we
5660 were here the first time: the previous comp_unit_die was stored in
5661 comp_unit_obstack. But there's no data yet that we need this
5662 optimization. */
5663 if (cu->dwo_unit != NULL)
5664 rereading_dwo_cu = 1;
5665 }
5666 else
5667 {
5668 /* If !use_existing_cu, this_cu->cu must be NULL. */
5669 gdb_assert (this_cu->cu == NULL);
5670 cu = XNEW (struct dwarf2_cu);
5671 init_one_comp_unit (cu, this_cu);
5672 /* If an error occurs while loading, release our storage. */
5673 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5674 }
5675
5676 /* Get the header. */
5677 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5678 {
5679 /* We already have the header, there's no need to read it in again. */
5680 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5681 }
5682 else
5683 {
5684 if (this_cu->is_debug_types)
5685 {
5686 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5687 abbrev_section, info_ptr,
5688 rcuh_kind::TYPE);
5689
5690 /* Since per_cu is the first member of struct signatured_type,
5691 we can go from a pointer to one to a pointer to the other. */
5692 sig_type = (struct signatured_type *) this_cu;
5693 gdb_assert (sig_type->signature == cu->header.signature);
5694 gdb_assert (sig_type->type_offset_in_tu
5695 == cu->header.type_cu_offset_in_tu);
5696 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5697
5698 /* LENGTH has not been set yet for type units if we're
5699 using .gdb_index. */
5700 this_cu->length = get_cu_length (&cu->header);
5701
5702 /* Establish the type offset that can be used to lookup the type. */
5703 sig_type->type_offset_in_section =
5704 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5705
5706 this_cu->dwarf_version = cu->header.version;
5707 }
5708 else
5709 {
5710 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5711 abbrev_section,
5712 info_ptr,
5713 rcuh_kind::COMPILE);
5714
5715 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5716 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5717 this_cu->dwarf_version = cu->header.version;
5718 }
5719 }
5720
5721 /* Skip dummy compilation units. */
5722 if (info_ptr >= begin_info_ptr + this_cu->length
5723 || peek_abbrev_code (abfd, info_ptr) == 0)
5724 {
5725 do_cleanups (cleanups);
5726 return;
5727 }
5728
5729 /* If we don't have them yet, read the abbrevs for this compilation unit.
5730 And if we need to read them now, make sure they're freed when we're
5731 done. Note that it's important that if the CU had an abbrev table
5732 on entry we don't free it when we're done: Somewhere up the call stack
5733 it may be in use. */
5734 if (abbrev_table != NULL)
5735 {
5736 gdb_assert (cu->abbrev_table == NULL);
5737 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5738 cu->abbrev_table = abbrev_table;
5739 }
5740 else if (cu->abbrev_table == NULL)
5741 {
5742 dwarf2_read_abbrevs (cu, abbrev_section);
5743 make_cleanup (dwarf2_free_abbrev_table, cu);
5744 }
5745 else if (rereading_dwo_cu)
5746 {
5747 dwarf2_free_abbrev_table (cu);
5748 dwarf2_read_abbrevs (cu, abbrev_section);
5749 }
5750
5751 /* Read the top level CU/TU die. */
5752 init_cu_die_reader (&reader, cu, section, NULL);
5753 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5754
5755 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5756 from the DWO file.
5757 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5758 DWO CU, that this test will fail (the attribute will not be present). */
5759 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5760 if (attr)
5761 {
5762 struct dwo_unit *dwo_unit;
5763 struct die_info *dwo_comp_unit_die;
5764
5765 if (has_children)
5766 {
5767 complaint (&symfile_complaints,
5768 _("compilation unit with DW_AT_GNU_dwo_name"
5769 " has children (offset 0x%x) [in module %s]"),
5770 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5771 }
5772 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5773 if (dwo_unit != NULL)
5774 {
5775 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5776 abbrev_table != NULL,
5777 comp_unit_die, NULL,
5778 &reader, &info_ptr,
5779 &dwo_comp_unit_die, &has_children) == 0)
5780 {
5781 /* Dummy die. */
5782 do_cleanups (cleanups);
5783 return;
5784 }
5785 comp_unit_die = dwo_comp_unit_die;
5786 }
5787 else
5788 {
5789 /* Yikes, we couldn't find the rest of the DIE, we only have
5790 the stub. A complaint has already been logged. There's
5791 not much more we can do except pass on the stub DIE to
5792 die_reader_func. We don't want to throw an error on bad
5793 debug info. */
5794 }
5795 }
5796
5797 /* All of the above is setup for this call. Yikes. */
5798 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5799
5800 /* Done, clean up. */
5801 if (free_cu_cleanup != NULL)
5802 {
5803 if (keep)
5804 {
5805 /* We've successfully allocated this compilation unit. Let our
5806 caller clean it up when finished with it. */
5807 discard_cleanups (free_cu_cleanup);
5808
5809 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5810 So we have to manually free the abbrev table. */
5811 dwarf2_free_abbrev_table (cu);
5812
5813 /* Link this CU into read_in_chain. */
5814 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5815 dwarf2_per_objfile->read_in_chain = this_cu;
5816 }
5817 else
5818 do_cleanups (free_cu_cleanup);
5819 }
5820
5821 do_cleanups (cleanups);
5822 }
5823
5824 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5825 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5826 to have already done the lookup to find the DWO file).
5827
5828 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5829 THIS_CU->is_debug_types, but nothing else.
5830
5831 We fill in THIS_CU->length.
5832
5833 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5834 linker) then DIE_READER_FUNC will not get called.
5835
5836 THIS_CU->cu is always freed when done.
5837 This is done in order to not leave THIS_CU->cu in a state where we have
5838 to care whether it refers to the "main" CU or the DWO CU. */
5839
5840 static void
5841 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5842 struct dwo_file *dwo_file,
5843 die_reader_func_ftype *die_reader_func,
5844 void *data)
5845 {
5846 struct objfile *objfile = dwarf2_per_objfile->objfile;
5847 struct dwarf2_section_info *section = this_cu->section;
5848 bfd *abfd = get_section_bfd_owner (section);
5849 struct dwarf2_section_info *abbrev_section;
5850 struct dwarf2_cu cu;
5851 const gdb_byte *begin_info_ptr, *info_ptr;
5852 struct die_reader_specs reader;
5853 struct cleanup *cleanups;
5854 struct die_info *comp_unit_die;
5855 int has_children;
5856
5857 if (dwarf_die_debug)
5858 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5859 this_cu->is_debug_types ? "type" : "comp",
5860 to_underlying (this_cu->sect_off));
5861
5862 gdb_assert (this_cu->cu == NULL);
5863
5864 abbrev_section = (dwo_file != NULL
5865 ? &dwo_file->sections.abbrev
5866 : get_abbrev_section_for_cu (this_cu));
5867
5868 /* This is cheap if the section is already read in. */
5869 dwarf2_read_section (objfile, section);
5870
5871 init_one_comp_unit (&cu, this_cu);
5872
5873 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5874
5875 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5876 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5877 abbrev_section, info_ptr,
5878 (this_cu->is_debug_types
5879 ? rcuh_kind::TYPE
5880 : rcuh_kind::COMPILE));
5881
5882 this_cu->length = get_cu_length (&cu.header);
5883
5884 /* Skip dummy compilation units. */
5885 if (info_ptr >= begin_info_ptr + this_cu->length
5886 || peek_abbrev_code (abfd, info_ptr) == 0)
5887 {
5888 do_cleanups (cleanups);
5889 return;
5890 }
5891
5892 dwarf2_read_abbrevs (&cu, abbrev_section);
5893 make_cleanup (dwarf2_free_abbrev_table, &cu);
5894
5895 init_cu_die_reader (&reader, &cu, section, dwo_file);
5896 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5897
5898 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5899
5900 do_cleanups (cleanups);
5901 }
5902
5903 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5904 does not lookup the specified DWO file.
5905 This cannot be used to read DWO files.
5906
5907 THIS_CU->cu is always freed when done.
5908 This is done in order to not leave THIS_CU->cu in a state where we have
5909 to care whether it refers to the "main" CU or the DWO CU.
5910 We can revisit this if the data shows there's a performance issue. */
5911
5912 static void
5913 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5914 die_reader_func_ftype *die_reader_func,
5915 void *data)
5916 {
5917 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5918 }
5919 \f
5920 /* Type Unit Groups.
5921
5922 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5923 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5924 so that all types coming from the same compilation (.o file) are grouped
5925 together. A future step could be to put the types in the same symtab as
5926 the CU the types ultimately came from. */
5927
5928 static hashval_t
5929 hash_type_unit_group (const void *item)
5930 {
5931 const struct type_unit_group *tu_group
5932 = (const struct type_unit_group *) item;
5933
5934 return hash_stmt_list_entry (&tu_group->hash);
5935 }
5936
5937 static int
5938 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5939 {
5940 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5941 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5942
5943 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5944 }
5945
5946 /* Allocate a hash table for type unit groups. */
5947
5948 static htab_t
5949 allocate_type_unit_groups_table (void)
5950 {
5951 return htab_create_alloc_ex (3,
5952 hash_type_unit_group,
5953 eq_type_unit_group,
5954 NULL,
5955 &dwarf2_per_objfile->objfile->objfile_obstack,
5956 hashtab_obstack_allocate,
5957 dummy_obstack_deallocate);
5958 }
5959
5960 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5961 partial symtabs. We combine several TUs per psymtab to not let the size
5962 of any one psymtab grow too big. */
5963 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5964 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5965
5966 /* Helper routine for get_type_unit_group.
5967 Create the type_unit_group object used to hold one or more TUs. */
5968
5969 static struct type_unit_group *
5970 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5971 {
5972 struct objfile *objfile = dwarf2_per_objfile->objfile;
5973 struct dwarf2_per_cu_data *per_cu;
5974 struct type_unit_group *tu_group;
5975
5976 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5977 struct type_unit_group);
5978 per_cu = &tu_group->per_cu;
5979 per_cu->objfile = objfile;
5980
5981 if (dwarf2_per_objfile->using_index)
5982 {
5983 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5984 struct dwarf2_per_cu_quick_data);
5985 }
5986 else
5987 {
5988 unsigned int line_offset = to_underlying (line_offset_struct);
5989 struct partial_symtab *pst;
5990 char *name;
5991
5992 /* Give the symtab a useful name for debug purposes. */
5993 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5994 name = xstrprintf ("<type_units_%d>",
5995 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5996 else
5997 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5998
5999 pst = create_partial_symtab (per_cu, name);
6000 pst->anonymous = 1;
6001
6002 xfree (name);
6003 }
6004
6005 tu_group->hash.dwo_unit = cu->dwo_unit;
6006 tu_group->hash.line_sect_off = line_offset_struct;
6007
6008 return tu_group;
6009 }
6010
6011 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6012 STMT_LIST is a DW_AT_stmt_list attribute. */
6013
6014 static struct type_unit_group *
6015 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6016 {
6017 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6018 struct type_unit_group *tu_group;
6019 void **slot;
6020 unsigned int line_offset;
6021 struct type_unit_group type_unit_group_for_lookup;
6022
6023 if (dwarf2_per_objfile->type_unit_groups == NULL)
6024 {
6025 dwarf2_per_objfile->type_unit_groups =
6026 allocate_type_unit_groups_table ();
6027 }
6028
6029 /* Do we need to create a new group, or can we use an existing one? */
6030
6031 if (stmt_list)
6032 {
6033 line_offset = DW_UNSND (stmt_list);
6034 ++tu_stats->nr_symtab_sharers;
6035 }
6036 else
6037 {
6038 /* Ugh, no stmt_list. Rare, but we have to handle it.
6039 We can do various things here like create one group per TU or
6040 spread them over multiple groups to split up the expansion work.
6041 To avoid worst case scenarios (too many groups or too large groups)
6042 we, umm, group them in bunches. */
6043 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6044 | (tu_stats->nr_stmt_less_type_units
6045 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6046 ++tu_stats->nr_stmt_less_type_units;
6047 }
6048
6049 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6050 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6051 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6052 &type_unit_group_for_lookup, INSERT);
6053 if (*slot != NULL)
6054 {
6055 tu_group = (struct type_unit_group *) *slot;
6056 gdb_assert (tu_group != NULL);
6057 }
6058 else
6059 {
6060 sect_offset line_offset_struct = (sect_offset) line_offset;
6061 tu_group = create_type_unit_group (cu, line_offset_struct);
6062 *slot = tu_group;
6063 ++tu_stats->nr_symtabs;
6064 }
6065
6066 return tu_group;
6067 }
6068 \f
6069 /* Partial symbol tables. */
6070
6071 /* Create a psymtab named NAME and assign it to PER_CU.
6072
6073 The caller must fill in the following details:
6074 dirname, textlow, texthigh. */
6075
6076 static struct partial_symtab *
6077 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6078 {
6079 struct objfile *objfile = per_cu->objfile;
6080 struct partial_symtab *pst;
6081
6082 pst = start_psymtab_common (objfile, name, 0,
6083 objfile->global_psymbols.next,
6084 objfile->static_psymbols.next);
6085
6086 pst->psymtabs_addrmap_supported = 1;
6087
6088 /* This is the glue that links PST into GDB's symbol API. */
6089 pst->read_symtab_private = per_cu;
6090 pst->read_symtab = dwarf2_read_symtab;
6091 per_cu->v.psymtab = pst;
6092
6093 return pst;
6094 }
6095
6096 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6097 type. */
6098
6099 struct process_psymtab_comp_unit_data
6100 {
6101 /* True if we are reading a DW_TAG_partial_unit. */
6102
6103 int want_partial_unit;
6104
6105 /* The "pretend" language that is used if the CU doesn't declare a
6106 language. */
6107
6108 enum language pretend_language;
6109 };
6110
6111 /* die_reader_func for process_psymtab_comp_unit. */
6112
6113 static void
6114 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6115 const gdb_byte *info_ptr,
6116 struct die_info *comp_unit_die,
6117 int has_children,
6118 void *data)
6119 {
6120 struct dwarf2_cu *cu = reader->cu;
6121 struct objfile *objfile = cu->objfile;
6122 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6123 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6124 CORE_ADDR baseaddr;
6125 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6126 struct partial_symtab *pst;
6127 enum pc_bounds_kind cu_bounds_kind;
6128 const char *filename;
6129 struct process_psymtab_comp_unit_data *info
6130 = (struct process_psymtab_comp_unit_data *) data;
6131
6132 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6133 return;
6134
6135 gdb_assert (! per_cu->is_debug_types);
6136
6137 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6138
6139 cu->list_in_scope = &file_symbols;
6140
6141 /* Allocate a new partial symbol table structure. */
6142 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6143 if (filename == NULL)
6144 filename = "";
6145
6146 pst = create_partial_symtab (per_cu, filename);
6147
6148 /* This must be done before calling dwarf2_build_include_psymtabs. */
6149 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6150
6151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6152
6153 dwarf2_find_base_address (comp_unit_die, cu);
6154
6155 /* Possibly set the default values of LOWPC and HIGHPC from
6156 `DW_AT_ranges'. */
6157 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6158 &best_highpc, cu, pst);
6159 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6160 /* Store the contiguous range if it is not empty; it can be empty for
6161 CUs with no code. */
6162 addrmap_set_empty (objfile->psymtabs_addrmap,
6163 gdbarch_adjust_dwarf2_addr (gdbarch,
6164 best_lowpc + baseaddr),
6165 gdbarch_adjust_dwarf2_addr (gdbarch,
6166 best_highpc + baseaddr) - 1,
6167 pst);
6168
6169 /* Check if comp unit has_children.
6170 If so, read the rest of the partial symbols from this comp unit.
6171 If not, there's no more debug_info for this comp unit. */
6172 if (has_children)
6173 {
6174 struct partial_die_info *first_die;
6175 CORE_ADDR lowpc, highpc;
6176
6177 lowpc = ((CORE_ADDR) -1);
6178 highpc = ((CORE_ADDR) 0);
6179
6180 first_die = load_partial_dies (reader, info_ptr, 1);
6181
6182 scan_partial_symbols (first_die, &lowpc, &highpc,
6183 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6184
6185 /* If we didn't find a lowpc, set it to highpc to avoid
6186 complaints from `maint check'. */
6187 if (lowpc == ((CORE_ADDR) -1))
6188 lowpc = highpc;
6189
6190 /* If the compilation unit didn't have an explicit address range,
6191 then use the information extracted from its child dies. */
6192 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6193 {
6194 best_lowpc = lowpc;
6195 best_highpc = highpc;
6196 }
6197 }
6198 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6199 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6200
6201 end_psymtab_common (objfile, pst);
6202
6203 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6204 {
6205 int i;
6206 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6207 struct dwarf2_per_cu_data *iter;
6208
6209 /* Fill in 'dependencies' here; we fill in 'users' in a
6210 post-pass. */
6211 pst->number_of_dependencies = len;
6212 pst->dependencies =
6213 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6214 for (i = 0;
6215 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6216 i, iter);
6217 ++i)
6218 pst->dependencies[i] = iter->v.psymtab;
6219
6220 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6221 }
6222
6223 /* Get the list of files included in the current compilation unit,
6224 and build a psymtab for each of them. */
6225 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6226
6227 if (dwarf_read_debug)
6228 {
6229 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6230
6231 fprintf_unfiltered (gdb_stdlog,
6232 "Psymtab for %s unit @0x%x: %s - %s"
6233 ", %d global, %d static syms\n",
6234 per_cu->is_debug_types ? "type" : "comp",
6235 to_underlying (per_cu->sect_off),
6236 paddress (gdbarch, pst->textlow),
6237 paddress (gdbarch, pst->texthigh),
6238 pst->n_global_syms, pst->n_static_syms);
6239 }
6240 }
6241
6242 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6243 Process compilation unit THIS_CU for a psymtab. */
6244
6245 static void
6246 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6247 int want_partial_unit,
6248 enum language pretend_language)
6249 {
6250 struct process_psymtab_comp_unit_data info;
6251
6252 /* If this compilation unit was already read in, free the
6253 cached copy in order to read it in again. This is
6254 necessary because we skipped some symbols when we first
6255 read in the compilation unit (see load_partial_dies).
6256 This problem could be avoided, but the benefit is unclear. */
6257 if (this_cu->cu != NULL)
6258 free_one_cached_comp_unit (this_cu);
6259
6260 gdb_assert (! this_cu->is_debug_types);
6261 info.want_partial_unit = want_partial_unit;
6262 info.pretend_language = pretend_language;
6263 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6264 process_psymtab_comp_unit_reader,
6265 &info);
6266
6267 /* Age out any secondary CUs. */
6268 age_cached_comp_units ();
6269 }
6270
6271 /* Reader function for build_type_psymtabs. */
6272
6273 static void
6274 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6275 const gdb_byte *info_ptr,
6276 struct die_info *type_unit_die,
6277 int has_children,
6278 void *data)
6279 {
6280 struct objfile *objfile = dwarf2_per_objfile->objfile;
6281 struct dwarf2_cu *cu = reader->cu;
6282 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6283 struct signatured_type *sig_type;
6284 struct type_unit_group *tu_group;
6285 struct attribute *attr;
6286 struct partial_die_info *first_die;
6287 CORE_ADDR lowpc, highpc;
6288 struct partial_symtab *pst;
6289
6290 gdb_assert (data == NULL);
6291 gdb_assert (per_cu->is_debug_types);
6292 sig_type = (struct signatured_type *) per_cu;
6293
6294 if (! has_children)
6295 return;
6296
6297 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6298 tu_group = get_type_unit_group (cu, attr);
6299
6300 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6301
6302 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6303 cu->list_in_scope = &file_symbols;
6304 pst = create_partial_symtab (per_cu, "");
6305 pst->anonymous = 1;
6306
6307 first_die = load_partial_dies (reader, info_ptr, 1);
6308
6309 lowpc = (CORE_ADDR) -1;
6310 highpc = (CORE_ADDR) 0;
6311 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6312
6313 end_psymtab_common (objfile, pst);
6314 }
6315
6316 /* Struct used to sort TUs by their abbreviation table offset. */
6317
6318 struct tu_abbrev_offset
6319 {
6320 struct signatured_type *sig_type;
6321 sect_offset abbrev_offset;
6322 };
6323
6324 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6325
6326 static int
6327 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6328 {
6329 const struct tu_abbrev_offset * const *a
6330 = (const struct tu_abbrev_offset * const*) ap;
6331 const struct tu_abbrev_offset * const *b
6332 = (const struct tu_abbrev_offset * const*) bp;
6333 sect_offset aoff = (*a)->abbrev_offset;
6334 sect_offset boff = (*b)->abbrev_offset;
6335
6336 return (aoff > boff) - (aoff < boff);
6337 }
6338
6339 /* Efficiently read all the type units.
6340 This does the bulk of the work for build_type_psymtabs.
6341
6342 The efficiency is because we sort TUs by the abbrev table they use and
6343 only read each abbrev table once. In one program there are 200K TUs
6344 sharing 8K abbrev tables.
6345
6346 The main purpose of this function is to support building the
6347 dwarf2_per_objfile->type_unit_groups table.
6348 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6349 can collapse the search space by grouping them by stmt_list.
6350 The savings can be significant, in the same program from above the 200K TUs
6351 share 8K stmt_list tables.
6352
6353 FUNC is expected to call get_type_unit_group, which will create the
6354 struct type_unit_group if necessary and add it to
6355 dwarf2_per_objfile->type_unit_groups. */
6356
6357 static void
6358 build_type_psymtabs_1 (void)
6359 {
6360 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6361 struct cleanup *cleanups;
6362 struct abbrev_table *abbrev_table;
6363 sect_offset abbrev_offset;
6364 struct tu_abbrev_offset *sorted_by_abbrev;
6365 int i;
6366
6367 /* It's up to the caller to not call us multiple times. */
6368 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6369
6370 if (dwarf2_per_objfile->n_type_units == 0)
6371 return;
6372
6373 /* TUs typically share abbrev tables, and there can be way more TUs than
6374 abbrev tables. Sort by abbrev table to reduce the number of times we
6375 read each abbrev table in.
6376 Alternatives are to punt or to maintain a cache of abbrev tables.
6377 This is simpler and efficient enough for now.
6378
6379 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6380 symtab to use). Typically TUs with the same abbrev offset have the same
6381 stmt_list value too so in practice this should work well.
6382
6383 The basic algorithm here is:
6384
6385 sort TUs by abbrev table
6386 for each TU with same abbrev table:
6387 read abbrev table if first user
6388 read TU top level DIE
6389 [IWBN if DWO skeletons had DW_AT_stmt_list]
6390 call FUNC */
6391
6392 if (dwarf_read_debug)
6393 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6394
6395 /* Sort in a separate table to maintain the order of all_type_units
6396 for .gdb_index: TU indices directly index all_type_units. */
6397 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6398 dwarf2_per_objfile->n_type_units);
6399 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6400 {
6401 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6402
6403 sorted_by_abbrev[i].sig_type = sig_type;
6404 sorted_by_abbrev[i].abbrev_offset =
6405 read_abbrev_offset (sig_type->per_cu.section,
6406 sig_type->per_cu.sect_off);
6407 }
6408 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6409 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6410 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6411
6412 abbrev_offset = (sect_offset) ~(unsigned) 0;
6413 abbrev_table = NULL;
6414 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6415
6416 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6417 {
6418 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6419
6420 /* Switch to the next abbrev table if necessary. */
6421 if (abbrev_table == NULL
6422 || tu->abbrev_offset != abbrev_offset)
6423 {
6424 if (abbrev_table != NULL)
6425 {
6426 abbrev_table_free (abbrev_table);
6427 /* Reset to NULL in case abbrev_table_read_table throws
6428 an error: abbrev_table_free_cleanup will get called. */
6429 abbrev_table = NULL;
6430 }
6431 abbrev_offset = tu->abbrev_offset;
6432 abbrev_table =
6433 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6434 abbrev_offset);
6435 ++tu_stats->nr_uniq_abbrev_tables;
6436 }
6437
6438 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6439 build_type_psymtabs_reader, NULL);
6440 }
6441
6442 do_cleanups (cleanups);
6443 }
6444
6445 /* Print collected type unit statistics. */
6446
6447 static void
6448 print_tu_stats (void)
6449 {
6450 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6451
6452 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6453 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6454 dwarf2_per_objfile->n_type_units);
6455 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6456 tu_stats->nr_uniq_abbrev_tables);
6457 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6458 tu_stats->nr_symtabs);
6459 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6460 tu_stats->nr_symtab_sharers);
6461 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6462 tu_stats->nr_stmt_less_type_units);
6463 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6464 tu_stats->nr_all_type_units_reallocs);
6465 }
6466
6467 /* Traversal function for build_type_psymtabs. */
6468
6469 static int
6470 build_type_psymtab_dependencies (void **slot, void *info)
6471 {
6472 struct objfile *objfile = dwarf2_per_objfile->objfile;
6473 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6474 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6475 struct partial_symtab *pst = per_cu->v.psymtab;
6476 int len = VEC_length (sig_type_ptr, tu_group->tus);
6477 struct signatured_type *iter;
6478 int i;
6479
6480 gdb_assert (len > 0);
6481 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6482
6483 pst->number_of_dependencies = len;
6484 pst->dependencies =
6485 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6486 for (i = 0;
6487 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6488 ++i)
6489 {
6490 gdb_assert (iter->per_cu.is_debug_types);
6491 pst->dependencies[i] = iter->per_cu.v.psymtab;
6492 iter->type_unit_group = tu_group;
6493 }
6494
6495 VEC_free (sig_type_ptr, tu_group->tus);
6496
6497 return 1;
6498 }
6499
6500 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6501 Build partial symbol tables for the .debug_types comp-units. */
6502
6503 static void
6504 build_type_psymtabs (struct objfile *objfile)
6505 {
6506 if (! create_all_type_units (objfile))
6507 return;
6508
6509 build_type_psymtabs_1 ();
6510 }
6511
6512 /* Traversal function for process_skeletonless_type_unit.
6513 Read a TU in a DWO file and build partial symbols for it. */
6514
6515 static int
6516 process_skeletonless_type_unit (void **slot, void *info)
6517 {
6518 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6519 struct objfile *objfile = (struct objfile *) info;
6520 struct signatured_type find_entry, *entry;
6521
6522 /* If this TU doesn't exist in the global table, add it and read it in. */
6523
6524 if (dwarf2_per_objfile->signatured_types == NULL)
6525 {
6526 dwarf2_per_objfile->signatured_types
6527 = allocate_signatured_type_table (objfile);
6528 }
6529
6530 find_entry.signature = dwo_unit->signature;
6531 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6532 INSERT);
6533 /* If we've already seen this type there's nothing to do. What's happening
6534 is we're doing our own version of comdat-folding here. */
6535 if (*slot != NULL)
6536 return 1;
6537
6538 /* This does the job that create_all_type_units would have done for
6539 this TU. */
6540 entry = add_type_unit (dwo_unit->signature, slot);
6541 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6542 *slot = entry;
6543
6544 /* This does the job that build_type_psymtabs_1 would have done. */
6545 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6546 build_type_psymtabs_reader, NULL);
6547
6548 return 1;
6549 }
6550
6551 /* Traversal function for process_skeletonless_type_units. */
6552
6553 static int
6554 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6555 {
6556 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6557
6558 if (dwo_file->tus != NULL)
6559 {
6560 htab_traverse_noresize (dwo_file->tus,
6561 process_skeletonless_type_unit, info);
6562 }
6563
6564 return 1;
6565 }
6566
6567 /* Scan all TUs of DWO files, verifying we've processed them.
6568 This is needed in case a TU was emitted without its skeleton.
6569 Note: This can't be done until we know what all the DWO files are. */
6570
6571 static void
6572 process_skeletonless_type_units (struct objfile *objfile)
6573 {
6574 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6575 if (get_dwp_file () == NULL
6576 && dwarf2_per_objfile->dwo_files != NULL)
6577 {
6578 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6579 process_dwo_file_for_skeletonless_type_units,
6580 objfile);
6581 }
6582 }
6583
6584 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6585
6586 static void
6587 psymtabs_addrmap_cleanup (void *o)
6588 {
6589 struct objfile *objfile = (struct objfile *) o;
6590
6591 objfile->psymtabs_addrmap = NULL;
6592 }
6593
6594 /* Compute the 'user' field for each psymtab in OBJFILE. */
6595
6596 static void
6597 set_partial_user (struct objfile *objfile)
6598 {
6599 int i;
6600
6601 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6602 {
6603 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6604 struct partial_symtab *pst = per_cu->v.psymtab;
6605 int j;
6606
6607 if (pst == NULL)
6608 continue;
6609
6610 for (j = 0; j < pst->number_of_dependencies; ++j)
6611 {
6612 /* Set the 'user' field only if it is not already set. */
6613 if (pst->dependencies[j]->user == NULL)
6614 pst->dependencies[j]->user = pst;
6615 }
6616 }
6617 }
6618
6619 /* Build the partial symbol table by doing a quick pass through the
6620 .debug_info and .debug_abbrev sections. */
6621
6622 static void
6623 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6624 {
6625 struct cleanup *back_to, *addrmap_cleanup;
6626 struct obstack temp_obstack;
6627 int i;
6628
6629 if (dwarf_read_debug)
6630 {
6631 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6632 objfile_name (objfile));
6633 }
6634
6635 dwarf2_per_objfile->reading_partial_symbols = 1;
6636
6637 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6638
6639 /* Any cached compilation units will be linked by the per-objfile
6640 read_in_chain. Make sure to free them when we're done. */
6641 back_to = make_cleanup (free_cached_comp_units, NULL);
6642
6643 build_type_psymtabs (objfile);
6644
6645 create_all_comp_units (objfile);
6646
6647 /* Create a temporary address map on a temporary obstack. We later
6648 copy this to the final obstack. */
6649 obstack_init (&temp_obstack);
6650 make_cleanup_obstack_free (&temp_obstack);
6651 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6652 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6653
6654 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6655 {
6656 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6657
6658 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6659 }
6660
6661 /* This has to wait until we read the CUs, we need the list of DWOs. */
6662 process_skeletonless_type_units (objfile);
6663
6664 /* Now that all TUs have been processed we can fill in the dependencies. */
6665 if (dwarf2_per_objfile->type_unit_groups != NULL)
6666 {
6667 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6668 build_type_psymtab_dependencies, NULL);
6669 }
6670
6671 if (dwarf_read_debug)
6672 print_tu_stats ();
6673
6674 set_partial_user (objfile);
6675
6676 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6677 &objfile->objfile_obstack);
6678 discard_cleanups (addrmap_cleanup);
6679
6680 do_cleanups (back_to);
6681
6682 if (dwarf_read_debug)
6683 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6684 objfile_name (objfile));
6685 }
6686
6687 /* die_reader_func for load_partial_comp_unit. */
6688
6689 static void
6690 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6691 const gdb_byte *info_ptr,
6692 struct die_info *comp_unit_die,
6693 int has_children,
6694 void *data)
6695 {
6696 struct dwarf2_cu *cu = reader->cu;
6697
6698 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6699
6700 /* Check if comp unit has_children.
6701 If so, read the rest of the partial symbols from this comp unit.
6702 If not, there's no more debug_info for this comp unit. */
6703 if (has_children)
6704 load_partial_dies (reader, info_ptr, 0);
6705 }
6706
6707 /* Load the partial DIEs for a secondary CU into memory.
6708 This is also used when rereading a primary CU with load_all_dies. */
6709
6710 static void
6711 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6712 {
6713 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6714 load_partial_comp_unit_reader, NULL);
6715 }
6716
6717 static void
6718 read_comp_units_from_section (struct objfile *objfile,
6719 struct dwarf2_section_info *section,
6720 unsigned int is_dwz,
6721 int *n_allocated,
6722 int *n_comp_units,
6723 struct dwarf2_per_cu_data ***all_comp_units)
6724 {
6725 const gdb_byte *info_ptr;
6726 bfd *abfd = get_section_bfd_owner (section);
6727
6728 if (dwarf_read_debug)
6729 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6730 get_section_name (section),
6731 get_section_file_name (section));
6732
6733 dwarf2_read_section (objfile, section);
6734
6735 info_ptr = section->buffer;
6736
6737 while (info_ptr < section->buffer + section->size)
6738 {
6739 unsigned int length, initial_length_size;
6740 struct dwarf2_per_cu_data *this_cu;
6741
6742 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6743
6744 /* Read just enough information to find out where the next
6745 compilation unit is. */
6746 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6747
6748 /* Save the compilation unit for later lookup. */
6749 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6750 memset (this_cu, 0, sizeof (*this_cu));
6751 this_cu->sect_off = sect_off;
6752 this_cu->length = length + initial_length_size;
6753 this_cu->is_dwz = is_dwz;
6754 this_cu->objfile = objfile;
6755 this_cu->section = section;
6756
6757 if (*n_comp_units == *n_allocated)
6758 {
6759 *n_allocated *= 2;
6760 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6761 *all_comp_units, *n_allocated);
6762 }
6763 (*all_comp_units)[*n_comp_units] = this_cu;
6764 ++*n_comp_units;
6765
6766 info_ptr = info_ptr + this_cu->length;
6767 }
6768 }
6769
6770 /* Create a list of all compilation units in OBJFILE.
6771 This is only done for -readnow and building partial symtabs. */
6772
6773 static void
6774 create_all_comp_units (struct objfile *objfile)
6775 {
6776 int n_allocated;
6777 int n_comp_units;
6778 struct dwarf2_per_cu_data **all_comp_units;
6779 struct dwz_file *dwz;
6780
6781 n_comp_units = 0;
6782 n_allocated = 10;
6783 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6784
6785 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6786 &n_allocated, &n_comp_units, &all_comp_units);
6787
6788 dwz = dwarf2_get_dwz_file ();
6789 if (dwz != NULL)
6790 read_comp_units_from_section (objfile, &dwz->info, 1,
6791 &n_allocated, &n_comp_units,
6792 &all_comp_units);
6793
6794 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6795 struct dwarf2_per_cu_data *,
6796 n_comp_units);
6797 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6798 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6799 xfree (all_comp_units);
6800 dwarf2_per_objfile->n_comp_units = n_comp_units;
6801 }
6802
6803 /* Process all loaded DIEs for compilation unit CU, starting at
6804 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6805 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6806 DW_AT_ranges). See the comments of add_partial_subprogram on how
6807 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6808
6809 static void
6810 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6811 CORE_ADDR *highpc, int set_addrmap,
6812 struct dwarf2_cu *cu)
6813 {
6814 struct partial_die_info *pdi;
6815
6816 /* Now, march along the PDI's, descending into ones which have
6817 interesting children but skipping the children of the other ones,
6818 until we reach the end of the compilation unit. */
6819
6820 pdi = first_die;
6821
6822 while (pdi != NULL)
6823 {
6824 fixup_partial_die (pdi, cu);
6825
6826 /* Anonymous namespaces or modules have no name but have interesting
6827 children, so we need to look at them. Ditto for anonymous
6828 enums. */
6829
6830 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6831 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6832 || pdi->tag == DW_TAG_imported_unit)
6833 {
6834 switch (pdi->tag)
6835 {
6836 case DW_TAG_subprogram:
6837 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6838 break;
6839 case DW_TAG_constant:
6840 case DW_TAG_variable:
6841 case DW_TAG_typedef:
6842 case DW_TAG_union_type:
6843 if (!pdi->is_declaration)
6844 {
6845 add_partial_symbol (pdi, cu);
6846 }
6847 break;
6848 case DW_TAG_class_type:
6849 case DW_TAG_interface_type:
6850 case DW_TAG_structure_type:
6851 if (!pdi->is_declaration)
6852 {
6853 add_partial_symbol (pdi, cu);
6854 }
6855 if (cu->language == language_rust && pdi->has_children)
6856 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6857 set_addrmap, cu);
6858 break;
6859 case DW_TAG_enumeration_type:
6860 if (!pdi->is_declaration)
6861 add_partial_enumeration (pdi, cu);
6862 break;
6863 case DW_TAG_base_type:
6864 case DW_TAG_subrange_type:
6865 /* File scope base type definitions are added to the partial
6866 symbol table. */
6867 add_partial_symbol (pdi, cu);
6868 break;
6869 case DW_TAG_namespace:
6870 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6871 break;
6872 case DW_TAG_module:
6873 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6874 break;
6875 case DW_TAG_imported_unit:
6876 {
6877 struct dwarf2_per_cu_data *per_cu;
6878
6879 /* For now we don't handle imported units in type units. */
6880 if (cu->per_cu->is_debug_types)
6881 {
6882 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6883 " supported in type units [in module %s]"),
6884 objfile_name (cu->objfile));
6885 }
6886
6887 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6888 pdi->is_dwz,
6889 cu->objfile);
6890
6891 /* Go read the partial unit, if needed. */
6892 if (per_cu->v.psymtab == NULL)
6893 process_psymtab_comp_unit (per_cu, 1, cu->language);
6894
6895 VEC_safe_push (dwarf2_per_cu_ptr,
6896 cu->per_cu->imported_symtabs, per_cu);
6897 }
6898 break;
6899 case DW_TAG_imported_declaration:
6900 add_partial_symbol (pdi, cu);
6901 break;
6902 default:
6903 break;
6904 }
6905 }
6906
6907 /* If the die has a sibling, skip to the sibling. */
6908
6909 pdi = pdi->die_sibling;
6910 }
6911 }
6912
6913 /* Functions used to compute the fully scoped name of a partial DIE.
6914
6915 Normally, this is simple. For C++, the parent DIE's fully scoped
6916 name is concatenated with "::" and the partial DIE's name.
6917 Enumerators are an exception; they use the scope of their parent
6918 enumeration type, i.e. the name of the enumeration type is not
6919 prepended to the enumerator.
6920
6921 There are two complexities. One is DW_AT_specification; in this
6922 case "parent" means the parent of the target of the specification,
6923 instead of the direct parent of the DIE. The other is compilers
6924 which do not emit DW_TAG_namespace; in this case we try to guess
6925 the fully qualified name of structure types from their members'
6926 linkage names. This must be done using the DIE's children rather
6927 than the children of any DW_AT_specification target. We only need
6928 to do this for structures at the top level, i.e. if the target of
6929 any DW_AT_specification (if any; otherwise the DIE itself) does not
6930 have a parent. */
6931
6932 /* Compute the scope prefix associated with PDI's parent, in
6933 compilation unit CU. The result will be allocated on CU's
6934 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6935 field. NULL is returned if no prefix is necessary. */
6936 static const char *
6937 partial_die_parent_scope (struct partial_die_info *pdi,
6938 struct dwarf2_cu *cu)
6939 {
6940 const char *grandparent_scope;
6941 struct partial_die_info *parent, *real_pdi;
6942
6943 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6944 then this means the parent of the specification DIE. */
6945
6946 real_pdi = pdi;
6947 while (real_pdi->has_specification)
6948 real_pdi = find_partial_die (real_pdi->spec_offset,
6949 real_pdi->spec_is_dwz, cu);
6950
6951 parent = real_pdi->die_parent;
6952 if (parent == NULL)
6953 return NULL;
6954
6955 if (parent->scope_set)
6956 return parent->scope;
6957
6958 fixup_partial_die (parent, cu);
6959
6960 grandparent_scope = partial_die_parent_scope (parent, cu);
6961
6962 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6963 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6964 Work around this problem here. */
6965 if (cu->language == language_cplus
6966 && parent->tag == DW_TAG_namespace
6967 && strcmp (parent->name, "::") == 0
6968 && grandparent_scope == NULL)
6969 {
6970 parent->scope = NULL;
6971 parent->scope_set = 1;
6972 return NULL;
6973 }
6974
6975 if (pdi->tag == DW_TAG_enumerator)
6976 /* Enumerators should not get the name of the enumeration as a prefix. */
6977 parent->scope = grandparent_scope;
6978 else if (parent->tag == DW_TAG_namespace
6979 || parent->tag == DW_TAG_module
6980 || parent->tag == DW_TAG_structure_type
6981 || parent->tag == DW_TAG_class_type
6982 || parent->tag == DW_TAG_interface_type
6983 || parent->tag == DW_TAG_union_type
6984 || parent->tag == DW_TAG_enumeration_type)
6985 {
6986 if (grandparent_scope == NULL)
6987 parent->scope = parent->name;
6988 else
6989 parent->scope = typename_concat (&cu->comp_unit_obstack,
6990 grandparent_scope,
6991 parent->name, 0, cu);
6992 }
6993 else
6994 {
6995 /* FIXME drow/2004-04-01: What should we be doing with
6996 function-local names? For partial symbols, we should probably be
6997 ignoring them. */
6998 complaint (&symfile_complaints,
6999 _("unhandled containing DIE tag %d for DIE at %d"),
7000 parent->tag, to_underlying (pdi->sect_off));
7001 parent->scope = grandparent_scope;
7002 }
7003
7004 parent->scope_set = 1;
7005 return parent->scope;
7006 }
7007
7008 /* Return the fully scoped name associated with PDI, from compilation unit
7009 CU. The result will be allocated with malloc. */
7010
7011 static char *
7012 partial_die_full_name (struct partial_die_info *pdi,
7013 struct dwarf2_cu *cu)
7014 {
7015 const char *parent_scope;
7016
7017 /* If this is a template instantiation, we can not work out the
7018 template arguments from partial DIEs. So, unfortunately, we have
7019 to go through the full DIEs. At least any work we do building
7020 types here will be reused if full symbols are loaded later. */
7021 if (pdi->has_template_arguments)
7022 {
7023 fixup_partial_die (pdi, cu);
7024
7025 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7026 {
7027 struct die_info *die;
7028 struct attribute attr;
7029 struct dwarf2_cu *ref_cu = cu;
7030
7031 /* DW_FORM_ref_addr is using section offset. */
7032 attr.name = (enum dwarf_attribute) 0;
7033 attr.form = DW_FORM_ref_addr;
7034 attr.u.unsnd = to_underlying (pdi->sect_off);
7035 die = follow_die_ref (NULL, &attr, &ref_cu);
7036
7037 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7038 }
7039 }
7040
7041 parent_scope = partial_die_parent_scope (pdi, cu);
7042 if (parent_scope == NULL)
7043 return NULL;
7044 else
7045 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7046 }
7047
7048 static void
7049 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7050 {
7051 struct objfile *objfile = cu->objfile;
7052 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7053 CORE_ADDR addr = 0;
7054 const char *actual_name = NULL;
7055 CORE_ADDR baseaddr;
7056 char *built_actual_name;
7057
7058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7059
7060 built_actual_name = partial_die_full_name (pdi, cu);
7061 if (built_actual_name != NULL)
7062 actual_name = built_actual_name;
7063
7064 if (actual_name == NULL)
7065 actual_name = pdi->name;
7066
7067 switch (pdi->tag)
7068 {
7069 case DW_TAG_subprogram:
7070 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7071 if (pdi->is_external || cu->language == language_ada)
7072 {
7073 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7074 of the global scope. But in Ada, we want to be able to access
7075 nested procedures globally. So all Ada subprograms are stored
7076 in the global scope. */
7077 add_psymbol_to_list (actual_name, strlen (actual_name),
7078 built_actual_name != NULL,
7079 VAR_DOMAIN, LOC_BLOCK,
7080 &objfile->global_psymbols,
7081 addr, cu->language, objfile);
7082 }
7083 else
7084 {
7085 add_psymbol_to_list (actual_name, strlen (actual_name),
7086 built_actual_name != NULL,
7087 VAR_DOMAIN, LOC_BLOCK,
7088 &objfile->static_psymbols,
7089 addr, cu->language, objfile);
7090 }
7091
7092 if (pdi->main_subprogram && actual_name != NULL)
7093 set_objfile_main_name (objfile, actual_name, cu->language);
7094 break;
7095 case DW_TAG_constant:
7096 {
7097 struct psymbol_allocation_list *list;
7098
7099 if (pdi->is_external)
7100 list = &objfile->global_psymbols;
7101 else
7102 list = &objfile->static_psymbols;
7103 add_psymbol_to_list (actual_name, strlen (actual_name),
7104 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7105 list, 0, cu->language, objfile);
7106 }
7107 break;
7108 case DW_TAG_variable:
7109 if (pdi->d.locdesc)
7110 addr = decode_locdesc (pdi->d.locdesc, cu);
7111
7112 if (pdi->d.locdesc
7113 && addr == 0
7114 && !dwarf2_per_objfile->has_section_at_zero)
7115 {
7116 /* A global or static variable may also have been stripped
7117 out by the linker if unused, in which case its address
7118 will be nullified; do not add such variables into partial
7119 symbol table then. */
7120 }
7121 else if (pdi->is_external)
7122 {
7123 /* Global Variable.
7124 Don't enter into the minimal symbol tables as there is
7125 a minimal symbol table entry from the ELF symbols already.
7126 Enter into partial symbol table if it has a location
7127 descriptor or a type.
7128 If the location descriptor is missing, new_symbol will create
7129 a LOC_UNRESOLVED symbol, the address of the variable will then
7130 be determined from the minimal symbol table whenever the variable
7131 is referenced.
7132 The address for the partial symbol table entry is not
7133 used by GDB, but it comes in handy for debugging partial symbol
7134 table building. */
7135
7136 if (pdi->d.locdesc || pdi->has_type)
7137 add_psymbol_to_list (actual_name, strlen (actual_name),
7138 built_actual_name != NULL,
7139 VAR_DOMAIN, LOC_STATIC,
7140 &objfile->global_psymbols,
7141 addr + baseaddr,
7142 cu->language, objfile);
7143 }
7144 else
7145 {
7146 int has_loc = pdi->d.locdesc != NULL;
7147
7148 /* Static Variable. Skip symbols whose value we cannot know (those
7149 without location descriptors or constant values). */
7150 if (!has_loc && !pdi->has_const_value)
7151 {
7152 xfree (built_actual_name);
7153 return;
7154 }
7155
7156 add_psymbol_to_list (actual_name, strlen (actual_name),
7157 built_actual_name != NULL,
7158 VAR_DOMAIN, LOC_STATIC,
7159 &objfile->static_psymbols,
7160 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7161 cu->language, objfile);
7162 }
7163 break;
7164 case DW_TAG_typedef:
7165 case DW_TAG_base_type:
7166 case DW_TAG_subrange_type:
7167 add_psymbol_to_list (actual_name, strlen (actual_name),
7168 built_actual_name != NULL,
7169 VAR_DOMAIN, LOC_TYPEDEF,
7170 &objfile->static_psymbols,
7171 0, cu->language, objfile);
7172 break;
7173 case DW_TAG_imported_declaration:
7174 case DW_TAG_namespace:
7175 add_psymbol_to_list (actual_name, strlen (actual_name),
7176 built_actual_name != NULL,
7177 VAR_DOMAIN, LOC_TYPEDEF,
7178 &objfile->global_psymbols,
7179 0, cu->language, objfile);
7180 break;
7181 case DW_TAG_module:
7182 add_psymbol_to_list (actual_name, strlen (actual_name),
7183 built_actual_name != NULL,
7184 MODULE_DOMAIN, LOC_TYPEDEF,
7185 &objfile->global_psymbols,
7186 0, cu->language, objfile);
7187 break;
7188 case DW_TAG_class_type:
7189 case DW_TAG_interface_type:
7190 case DW_TAG_structure_type:
7191 case DW_TAG_union_type:
7192 case DW_TAG_enumeration_type:
7193 /* Skip external references. The DWARF standard says in the section
7194 about "Structure, Union, and Class Type Entries": "An incomplete
7195 structure, union or class type is represented by a structure,
7196 union or class entry that does not have a byte size attribute
7197 and that has a DW_AT_declaration attribute." */
7198 if (!pdi->has_byte_size && pdi->is_declaration)
7199 {
7200 xfree (built_actual_name);
7201 return;
7202 }
7203
7204 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7205 static vs. global. */
7206 add_psymbol_to_list (actual_name, strlen (actual_name),
7207 built_actual_name != NULL,
7208 STRUCT_DOMAIN, LOC_TYPEDEF,
7209 cu->language == language_cplus
7210 ? &objfile->global_psymbols
7211 : &objfile->static_psymbols,
7212 0, cu->language, objfile);
7213
7214 break;
7215 case DW_TAG_enumerator:
7216 add_psymbol_to_list (actual_name, strlen (actual_name),
7217 built_actual_name != NULL,
7218 VAR_DOMAIN, LOC_CONST,
7219 cu->language == language_cplus
7220 ? &objfile->global_psymbols
7221 : &objfile->static_psymbols,
7222 0, cu->language, objfile);
7223 break;
7224 default:
7225 break;
7226 }
7227
7228 xfree (built_actual_name);
7229 }
7230
7231 /* Read a partial die corresponding to a namespace; also, add a symbol
7232 corresponding to that namespace to the symbol table. NAMESPACE is
7233 the name of the enclosing namespace. */
7234
7235 static void
7236 add_partial_namespace (struct partial_die_info *pdi,
7237 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7238 int set_addrmap, struct dwarf2_cu *cu)
7239 {
7240 /* Add a symbol for the namespace. */
7241
7242 add_partial_symbol (pdi, cu);
7243
7244 /* Now scan partial symbols in that namespace. */
7245
7246 if (pdi->has_children)
7247 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7248 }
7249
7250 /* Read a partial die corresponding to a Fortran module. */
7251
7252 static void
7253 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7254 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7255 {
7256 /* Add a symbol for the namespace. */
7257
7258 add_partial_symbol (pdi, cu);
7259
7260 /* Now scan partial symbols in that module. */
7261
7262 if (pdi->has_children)
7263 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7264 }
7265
7266 /* Read a partial die corresponding to a subprogram and create a partial
7267 symbol for that subprogram. When the CU language allows it, this
7268 routine also defines a partial symbol for each nested subprogram
7269 that this subprogram contains. If SET_ADDRMAP is true, record the
7270 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7271 and highest PC values found in PDI.
7272
7273 PDI may also be a lexical block, in which case we simply search
7274 recursively for subprograms defined inside that lexical block.
7275 Again, this is only performed when the CU language allows this
7276 type of definitions. */
7277
7278 static void
7279 add_partial_subprogram (struct partial_die_info *pdi,
7280 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7281 int set_addrmap, struct dwarf2_cu *cu)
7282 {
7283 if (pdi->tag == DW_TAG_subprogram)
7284 {
7285 if (pdi->has_pc_info)
7286 {
7287 if (pdi->lowpc < *lowpc)
7288 *lowpc = pdi->lowpc;
7289 if (pdi->highpc > *highpc)
7290 *highpc = pdi->highpc;
7291 if (set_addrmap)
7292 {
7293 struct objfile *objfile = cu->objfile;
7294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7295 CORE_ADDR baseaddr;
7296 CORE_ADDR highpc;
7297 CORE_ADDR lowpc;
7298
7299 baseaddr = ANOFFSET (objfile->section_offsets,
7300 SECT_OFF_TEXT (objfile));
7301 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7302 pdi->lowpc + baseaddr);
7303 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7304 pdi->highpc + baseaddr);
7305 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7306 cu->per_cu->v.psymtab);
7307 }
7308 }
7309
7310 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7311 {
7312 if (!pdi->is_declaration)
7313 /* Ignore subprogram DIEs that do not have a name, they are
7314 illegal. Do not emit a complaint at this point, we will
7315 do so when we convert this psymtab into a symtab. */
7316 if (pdi->name)
7317 add_partial_symbol (pdi, cu);
7318 }
7319 }
7320
7321 if (! pdi->has_children)
7322 return;
7323
7324 if (cu->language == language_ada)
7325 {
7326 pdi = pdi->die_child;
7327 while (pdi != NULL)
7328 {
7329 fixup_partial_die (pdi, cu);
7330 if (pdi->tag == DW_TAG_subprogram
7331 || pdi->tag == DW_TAG_lexical_block)
7332 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7333 pdi = pdi->die_sibling;
7334 }
7335 }
7336 }
7337
7338 /* Read a partial die corresponding to an enumeration type. */
7339
7340 static void
7341 add_partial_enumeration (struct partial_die_info *enum_pdi,
7342 struct dwarf2_cu *cu)
7343 {
7344 struct partial_die_info *pdi;
7345
7346 if (enum_pdi->name != NULL)
7347 add_partial_symbol (enum_pdi, cu);
7348
7349 pdi = enum_pdi->die_child;
7350 while (pdi)
7351 {
7352 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7353 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7354 else
7355 add_partial_symbol (pdi, cu);
7356 pdi = pdi->die_sibling;
7357 }
7358 }
7359
7360 /* Return the initial uleb128 in the die at INFO_PTR. */
7361
7362 static unsigned int
7363 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7364 {
7365 unsigned int bytes_read;
7366
7367 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7368 }
7369
7370 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7371 Return the corresponding abbrev, or NULL if the number is zero (indicating
7372 an empty DIE). In either case *BYTES_READ will be set to the length of
7373 the initial number. */
7374
7375 static struct abbrev_info *
7376 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7377 struct dwarf2_cu *cu)
7378 {
7379 bfd *abfd = cu->objfile->obfd;
7380 unsigned int abbrev_number;
7381 struct abbrev_info *abbrev;
7382
7383 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7384
7385 if (abbrev_number == 0)
7386 return NULL;
7387
7388 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7389 if (!abbrev)
7390 {
7391 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7392 " at offset 0x%x [in module %s]"),
7393 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7394 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7395 }
7396
7397 return abbrev;
7398 }
7399
7400 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7401 Returns a pointer to the end of a series of DIEs, terminated by an empty
7402 DIE. Any children of the skipped DIEs will also be skipped. */
7403
7404 static const gdb_byte *
7405 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7406 {
7407 struct dwarf2_cu *cu = reader->cu;
7408 struct abbrev_info *abbrev;
7409 unsigned int bytes_read;
7410
7411 while (1)
7412 {
7413 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7414 if (abbrev == NULL)
7415 return info_ptr + bytes_read;
7416 else
7417 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7418 }
7419 }
7420
7421 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7422 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7423 abbrev corresponding to that skipped uleb128 should be passed in
7424 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7425 children. */
7426
7427 static const gdb_byte *
7428 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7429 struct abbrev_info *abbrev)
7430 {
7431 unsigned int bytes_read;
7432 struct attribute attr;
7433 bfd *abfd = reader->abfd;
7434 struct dwarf2_cu *cu = reader->cu;
7435 const gdb_byte *buffer = reader->buffer;
7436 const gdb_byte *buffer_end = reader->buffer_end;
7437 unsigned int form, i;
7438
7439 for (i = 0; i < abbrev->num_attrs; i++)
7440 {
7441 /* The only abbrev we care about is DW_AT_sibling. */
7442 if (abbrev->attrs[i].name == DW_AT_sibling)
7443 {
7444 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7445 if (attr.form == DW_FORM_ref_addr)
7446 complaint (&symfile_complaints,
7447 _("ignoring absolute DW_AT_sibling"));
7448 else
7449 {
7450 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7451 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7452
7453 if (sibling_ptr < info_ptr)
7454 complaint (&symfile_complaints,
7455 _("DW_AT_sibling points backwards"));
7456 else if (sibling_ptr > reader->buffer_end)
7457 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7458 else
7459 return sibling_ptr;
7460 }
7461 }
7462
7463 /* If it isn't DW_AT_sibling, skip this attribute. */
7464 form = abbrev->attrs[i].form;
7465 skip_attribute:
7466 switch (form)
7467 {
7468 case DW_FORM_ref_addr:
7469 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7470 and later it is offset sized. */
7471 if (cu->header.version == 2)
7472 info_ptr += cu->header.addr_size;
7473 else
7474 info_ptr += cu->header.offset_size;
7475 break;
7476 case DW_FORM_GNU_ref_alt:
7477 info_ptr += cu->header.offset_size;
7478 break;
7479 case DW_FORM_addr:
7480 info_ptr += cu->header.addr_size;
7481 break;
7482 case DW_FORM_data1:
7483 case DW_FORM_ref1:
7484 case DW_FORM_flag:
7485 info_ptr += 1;
7486 break;
7487 case DW_FORM_flag_present:
7488 case DW_FORM_implicit_const:
7489 break;
7490 case DW_FORM_data2:
7491 case DW_FORM_ref2:
7492 info_ptr += 2;
7493 break;
7494 case DW_FORM_data4:
7495 case DW_FORM_ref4:
7496 info_ptr += 4;
7497 break;
7498 case DW_FORM_data8:
7499 case DW_FORM_ref8:
7500 case DW_FORM_ref_sig8:
7501 info_ptr += 8;
7502 break;
7503 case DW_FORM_data16:
7504 info_ptr += 16;
7505 break;
7506 case DW_FORM_string:
7507 read_direct_string (abfd, info_ptr, &bytes_read);
7508 info_ptr += bytes_read;
7509 break;
7510 case DW_FORM_sec_offset:
7511 case DW_FORM_strp:
7512 case DW_FORM_GNU_strp_alt:
7513 info_ptr += cu->header.offset_size;
7514 break;
7515 case DW_FORM_exprloc:
7516 case DW_FORM_block:
7517 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7518 info_ptr += bytes_read;
7519 break;
7520 case DW_FORM_block1:
7521 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7522 break;
7523 case DW_FORM_block2:
7524 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7525 break;
7526 case DW_FORM_block4:
7527 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7528 break;
7529 case DW_FORM_sdata:
7530 case DW_FORM_udata:
7531 case DW_FORM_ref_udata:
7532 case DW_FORM_GNU_addr_index:
7533 case DW_FORM_GNU_str_index:
7534 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7535 break;
7536 case DW_FORM_indirect:
7537 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7538 info_ptr += bytes_read;
7539 /* We need to continue parsing from here, so just go back to
7540 the top. */
7541 goto skip_attribute;
7542
7543 default:
7544 error (_("Dwarf Error: Cannot handle %s "
7545 "in DWARF reader [in module %s]"),
7546 dwarf_form_name (form),
7547 bfd_get_filename (abfd));
7548 }
7549 }
7550
7551 if (abbrev->has_children)
7552 return skip_children (reader, info_ptr);
7553 else
7554 return info_ptr;
7555 }
7556
7557 /* Locate ORIG_PDI's sibling.
7558 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7559
7560 static const gdb_byte *
7561 locate_pdi_sibling (const struct die_reader_specs *reader,
7562 struct partial_die_info *orig_pdi,
7563 const gdb_byte *info_ptr)
7564 {
7565 /* Do we know the sibling already? */
7566
7567 if (orig_pdi->sibling)
7568 return orig_pdi->sibling;
7569
7570 /* Are there any children to deal with? */
7571
7572 if (!orig_pdi->has_children)
7573 return info_ptr;
7574
7575 /* Skip the children the long way. */
7576
7577 return skip_children (reader, info_ptr);
7578 }
7579
7580 /* Expand this partial symbol table into a full symbol table. SELF is
7581 not NULL. */
7582
7583 static void
7584 dwarf2_read_symtab (struct partial_symtab *self,
7585 struct objfile *objfile)
7586 {
7587 if (self->readin)
7588 {
7589 warning (_("bug: psymtab for %s is already read in."),
7590 self->filename);
7591 }
7592 else
7593 {
7594 if (info_verbose)
7595 {
7596 printf_filtered (_("Reading in symbols for %s..."),
7597 self->filename);
7598 gdb_flush (gdb_stdout);
7599 }
7600
7601 /* Restore our global data. */
7602 dwarf2_per_objfile
7603 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7604 dwarf2_objfile_data_key);
7605
7606 /* If this psymtab is constructed from a debug-only objfile, the
7607 has_section_at_zero flag will not necessarily be correct. We
7608 can get the correct value for this flag by looking at the data
7609 associated with the (presumably stripped) associated objfile. */
7610 if (objfile->separate_debug_objfile_backlink)
7611 {
7612 struct dwarf2_per_objfile *dpo_backlink
7613 = ((struct dwarf2_per_objfile *)
7614 objfile_data (objfile->separate_debug_objfile_backlink,
7615 dwarf2_objfile_data_key));
7616
7617 dwarf2_per_objfile->has_section_at_zero
7618 = dpo_backlink->has_section_at_zero;
7619 }
7620
7621 dwarf2_per_objfile->reading_partial_symbols = 0;
7622
7623 psymtab_to_symtab_1 (self);
7624
7625 /* Finish up the debug error message. */
7626 if (info_verbose)
7627 printf_filtered (_("done.\n"));
7628 }
7629
7630 process_cu_includes ();
7631 }
7632 \f
7633 /* Reading in full CUs. */
7634
7635 /* Add PER_CU to the queue. */
7636
7637 static void
7638 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7639 enum language pretend_language)
7640 {
7641 struct dwarf2_queue_item *item;
7642
7643 per_cu->queued = 1;
7644 item = XNEW (struct dwarf2_queue_item);
7645 item->per_cu = per_cu;
7646 item->pretend_language = pretend_language;
7647 item->next = NULL;
7648
7649 if (dwarf2_queue == NULL)
7650 dwarf2_queue = item;
7651 else
7652 dwarf2_queue_tail->next = item;
7653
7654 dwarf2_queue_tail = item;
7655 }
7656
7657 /* If PER_CU is not yet queued, add it to the queue.
7658 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7659 dependency.
7660 The result is non-zero if PER_CU was queued, otherwise the result is zero
7661 meaning either PER_CU is already queued or it is already loaded.
7662
7663 N.B. There is an invariant here that if a CU is queued then it is loaded.
7664 The caller is required to load PER_CU if we return non-zero. */
7665
7666 static int
7667 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7668 struct dwarf2_per_cu_data *per_cu,
7669 enum language pretend_language)
7670 {
7671 /* We may arrive here during partial symbol reading, if we need full
7672 DIEs to process an unusual case (e.g. template arguments). Do
7673 not queue PER_CU, just tell our caller to load its DIEs. */
7674 if (dwarf2_per_objfile->reading_partial_symbols)
7675 {
7676 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7677 return 1;
7678 return 0;
7679 }
7680
7681 /* Mark the dependence relation so that we don't flush PER_CU
7682 too early. */
7683 if (dependent_cu != NULL)
7684 dwarf2_add_dependence (dependent_cu, per_cu);
7685
7686 /* If it's already on the queue, we have nothing to do. */
7687 if (per_cu->queued)
7688 return 0;
7689
7690 /* If the compilation unit is already loaded, just mark it as
7691 used. */
7692 if (per_cu->cu != NULL)
7693 {
7694 per_cu->cu->last_used = 0;
7695 return 0;
7696 }
7697
7698 /* Add it to the queue. */
7699 queue_comp_unit (per_cu, pretend_language);
7700
7701 return 1;
7702 }
7703
7704 /* Process the queue. */
7705
7706 static void
7707 process_queue (void)
7708 {
7709 struct dwarf2_queue_item *item, *next_item;
7710
7711 if (dwarf_read_debug)
7712 {
7713 fprintf_unfiltered (gdb_stdlog,
7714 "Expanding one or more symtabs of objfile %s ...\n",
7715 objfile_name (dwarf2_per_objfile->objfile));
7716 }
7717
7718 /* The queue starts out with one item, but following a DIE reference
7719 may load a new CU, adding it to the end of the queue. */
7720 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7721 {
7722 if ((dwarf2_per_objfile->using_index
7723 ? !item->per_cu->v.quick->compunit_symtab
7724 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7725 /* Skip dummy CUs. */
7726 && item->per_cu->cu != NULL)
7727 {
7728 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7729 unsigned int debug_print_threshold;
7730 char buf[100];
7731
7732 if (per_cu->is_debug_types)
7733 {
7734 struct signatured_type *sig_type =
7735 (struct signatured_type *) per_cu;
7736
7737 sprintf (buf, "TU %s at offset 0x%x",
7738 hex_string (sig_type->signature),
7739 to_underlying (per_cu->sect_off));
7740 /* There can be 100s of TUs.
7741 Only print them in verbose mode. */
7742 debug_print_threshold = 2;
7743 }
7744 else
7745 {
7746 sprintf (buf, "CU at offset 0x%x",
7747 to_underlying (per_cu->sect_off));
7748 debug_print_threshold = 1;
7749 }
7750
7751 if (dwarf_read_debug >= debug_print_threshold)
7752 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7753
7754 if (per_cu->is_debug_types)
7755 process_full_type_unit (per_cu, item->pretend_language);
7756 else
7757 process_full_comp_unit (per_cu, item->pretend_language);
7758
7759 if (dwarf_read_debug >= debug_print_threshold)
7760 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7761 }
7762
7763 item->per_cu->queued = 0;
7764 next_item = item->next;
7765 xfree (item);
7766 }
7767
7768 dwarf2_queue_tail = NULL;
7769
7770 if (dwarf_read_debug)
7771 {
7772 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7773 objfile_name (dwarf2_per_objfile->objfile));
7774 }
7775 }
7776
7777 /* Free all allocated queue entries. This function only releases anything if
7778 an error was thrown; if the queue was processed then it would have been
7779 freed as we went along. */
7780
7781 static void
7782 dwarf2_release_queue (void *dummy)
7783 {
7784 struct dwarf2_queue_item *item, *last;
7785
7786 item = dwarf2_queue;
7787 while (item)
7788 {
7789 /* Anything still marked queued is likely to be in an
7790 inconsistent state, so discard it. */
7791 if (item->per_cu->queued)
7792 {
7793 if (item->per_cu->cu != NULL)
7794 free_one_cached_comp_unit (item->per_cu);
7795 item->per_cu->queued = 0;
7796 }
7797
7798 last = item;
7799 item = item->next;
7800 xfree (last);
7801 }
7802
7803 dwarf2_queue = dwarf2_queue_tail = NULL;
7804 }
7805
7806 /* Read in full symbols for PST, and anything it depends on. */
7807
7808 static void
7809 psymtab_to_symtab_1 (struct partial_symtab *pst)
7810 {
7811 struct dwarf2_per_cu_data *per_cu;
7812 int i;
7813
7814 if (pst->readin)
7815 return;
7816
7817 for (i = 0; i < pst->number_of_dependencies; i++)
7818 if (!pst->dependencies[i]->readin
7819 && pst->dependencies[i]->user == NULL)
7820 {
7821 /* Inform about additional files that need to be read in. */
7822 if (info_verbose)
7823 {
7824 /* FIXME: i18n: Need to make this a single string. */
7825 fputs_filtered (" ", gdb_stdout);
7826 wrap_here ("");
7827 fputs_filtered ("and ", gdb_stdout);
7828 wrap_here ("");
7829 printf_filtered ("%s...", pst->dependencies[i]->filename);
7830 wrap_here (""); /* Flush output. */
7831 gdb_flush (gdb_stdout);
7832 }
7833 psymtab_to_symtab_1 (pst->dependencies[i]);
7834 }
7835
7836 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7837
7838 if (per_cu == NULL)
7839 {
7840 /* It's an include file, no symbols to read for it.
7841 Everything is in the parent symtab. */
7842 pst->readin = 1;
7843 return;
7844 }
7845
7846 dw2_do_instantiate_symtab (per_cu);
7847 }
7848
7849 /* Trivial hash function for die_info: the hash value of a DIE
7850 is its offset in .debug_info for this objfile. */
7851
7852 static hashval_t
7853 die_hash (const void *item)
7854 {
7855 const struct die_info *die = (const struct die_info *) item;
7856
7857 return to_underlying (die->sect_off);
7858 }
7859
7860 /* Trivial comparison function for die_info structures: two DIEs
7861 are equal if they have the same offset. */
7862
7863 static int
7864 die_eq (const void *item_lhs, const void *item_rhs)
7865 {
7866 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7867 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7868
7869 return die_lhs->sect_off == die_rhs->sect_off;
7870 }
7871
7872 /* die_reader_func for load_full_comp_unit.
7873 This is identical to read_signatured_type_reader,
7874 but is kept separate for now. */
7875
7876 static void
7877 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7878 const gdb_byte *info_ptr,
7879 struct die_info *comp_unit_die,
7880 int has_children,
7881 void *data)
7882 {
7883 struct dwarf2_cu *cu = reader->cu;
7884 enum language *language_ptr = (enum language *) data;
7885
7886 gdb_assert (cu->die_hash == NULL);
7887 cu->die_hash =
7888 htab_create_alloc_ex (cu->header.length / 12,
7889 die_hash,
7890 die_eq,
7891 NULL,
7892 &cu->comp_unit_obstack,
7893 hashtab_obstack_allocate,
7894 dummy_obstack_deallocate);
7895
7896 if (has_children)
7897 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7898 &info_ptr, comp_unit_die);
7899 cu->dies = comp_unit_die;
7900 /* comp_unit_die is not stored in die_hash, no need. */
7901
7902 /* We try not to read any attributes in this function, because not
7903 all CUs needed for references have been loaded yet, and symbol
7904 table processing isn't initialized. But we have to set the CU language,
7905 or we won't be able to build types correctly.
7906 Similarly, if we do not read the producer, we can not apply
7907 producer-specific interpretation. */
7908 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7909 }
7910
7911 /* Load the DIEs associated with PER_CU into memory. */
7912
7913 static void
7914 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7915 enum language pretend_language)
7916 {
7917 gdb_assert (! this_cu->is_debug_types);
7918
7919 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7920 load_full_comp_unit_reader, &pretend_language);
7921 }
7922
7923 /* Add a DIE to the delayed physname list. */
7924
7925 static void
7926 add_to_method_list (struct type *type, int fnfield_index, int index,
7927 const char *name, struct die_info *die,
7928 struct dwarf2_cu *cu)
7929 {
7930 struct delayed_method_info mi;
7931 mi.type = type;
7932 mi.fnfield_index = fnfield_index;
7933 mi.index = index;
7934 mi.name = name;
7935 mi.die = die;
7936 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7937 }
7938
7939 /* A cleanup for freeing the delayed method list. */
7940
7941 static void
7942 free_delayed_list (void *ptr)
7943 {
7944 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7945 if (cu->method_list != NULL)
7946 {
7947 VEC_free (delayed_method_info, cu->method_list);
7948 cu->method_list = NULL;
7949 }
7950 }
7951
7952 /* Compute the physnames of any methods on the CU's method list.
7953
7954 The computation of method physnames is delayed in order to avoid the
7955 (bad) condition that one of the method's formal parameters is of an as yet
7956 incomplete type. */
7957
7958 static void
7959 compute_delayed_physnames (struct dwarf2_cu *cu)
7960 {
7961 int i;
7962 struct delayed_method_info *mi;
7963 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7964 {
7965 const char *physname;
7966 struct fn_fieldlist *fn_flp
7967 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7968 physname = dwarf2_physname (mi->name, mi->die, cu);
7969 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7970 = physname ? physname : "";
7971 }
7972 }
7973
7974 /* Go objects should be embedded in a DW_TAG_module DIE,
7975 and it's not clear if/how imported objects will appear.
7976 To keep Go support simple until that's worked out,
7977 go back through what we've read and create something usable.
7978 We could do this while processing each DIE, and feels kinda cleaner,
7979 but that way is more invasive.
7980 This is to, for example, allow the user to type "p var" or "b main"
7981 without having to specify the package name, and allow lookups
7982 of module.object to work in contexts that use the expression
7983 parser. */
7984
7985 static void
7986 fixup_go_packaging (struct dwarf2_cu *cu)
7987 {
7988 char *package_name = NULL;
7989 struct pending *list;
7990 int i;
7991
7992 for (list = global_symbols; list != NULL; list = list->next)
7993 {
7994 for (i = 0; i < list->nsyms; ++i)
7995 {
7996 struct symbol *sym = list->symbol[i];
7997
7998 if (SYMBOL_LANGUAGE (sym) == language_go
7999 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8000 {
8001 char *this_package_name = go_symbol_package_name (sym);
8002
8003 if (this_package_name == NULL)
8004 continue;
8005 if (package_name == NULL)
8006 package_name = this_package_name;
8007 else
8008 {
8009 if (strcmp (package_name, this_package_name) != 0)
8010 complaint (&symfile_complaints,
8011 _("Symtab %s has objects from two different Go packages: %s and %s"),
8012 (symbol_symtab (sym) != NULL
8013 ? symtab_to_filename_for_display
8014 (symbol_symtab (sym))
8015 : objfile_name (cu->objfile)),
8016 this_package_name, package_name);
8017 xfree (this_package_name);
8018 }
8019 }
8020 }
8021 }
8022
8023 if (package_name != NULL)
8024 {
8025 struct objfile *objfile = cu->objfile;
8026 const char *saved_package_name
8027 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8028 package_name,
8029 strlen (package_name));
8030 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8031 saved_package_name);
8032 struct symbol *sym;
8033
8034 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8035
8036 sym = allocate_symbol (objfile);
8037 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8038 SYMBOL_SET_NAMES (sym, saved_package_name,
8039 strlen (saved_package_name), 0, objfile);
8040 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8041 e.g., "main" finds the "main" module and not C's main(). */
8042 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8044 SYMBOL_TYPE (sym) = type;
8045
8046 add_symbol_to_list (sym, &global_symbols);
8047
8048 xfree (package_name);
8049 }
8050 }
8051
8052 /* Return the symtab for PER_CU. This works properly regardless of
8053 whether we're using the index or psymtabs. */
8054
8055 static struct compunit_symtab *
8056 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8057 {
8058 return (dwarf2_per_objfile->using_index
8059 ? per_cu->v.quick->compunit_symtab
8060 : per_cu->v.psymtab->compunit_symtab);
8061 }
8062
8063 /* A helper function for computing the list of all symbol tables
8064 included by PER_CU. */
8065
8066 static void
8067 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8068 htab_t all_children, htab_t all_type_symtabs,
8069 struct dwarf2_per_cu_data *per_cu,
8070 struct compunit_symtab *immediate_parent)
8071 {
8072 void **slot;
8073 int ix;
8074 struct compunit_symtab *cust;
8075 struct dwarf2_per_cu_data *iter;
8076
8077 slot = htab_find_slot (all_children, per_cu, INSERT);
8078 if (*slot != NULL)
8079 {
8080 /* This inclusion and its children have been processed. */
8081 return;
8082 }
8083
8084 *slot = per_cu;
8085 /* Only add a CU if it has a symbol table. */
8086 cust = get_compunit_symtab (per_cu);
8087 if (cust != NULL)
8088 {
8089 /* If this is a type unit only add its symbol table if we haven't
8090 seen it yet (type unit per_cu's can share symtabs). */
8091 if (per_cu->is_debug_types)
8092 {
8093 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8094 if (*slot == NULL)
8095 {
8096 *slot = cust;
8097 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8098 if (cust->user == NULL)
8099 cust->user = immediate_parent;
8100 }
8101 }
8102 else
8103 {
8104 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8105 if (cust->user == NULL)
8106 cust->user = immediate_parent;
8107 }
8108 }
8109
8110 for (ix = 0;
8111 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8112 ++ix)
8113 {
8114 recursively_compute_inclusions (result, all_children,
8115 all_type_symtabs, iter, cust);
8116 }
8117 }
8118
8119 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8120 PER_CU. */
8121
8122 static void
8123 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8124 {
8125 gdb_assert (! per_cu->is_debug_types);
8126
8127 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8128 {
8129 int ix, len;
8130 struct dwarf2_per_cu_data *per_cu_iter;
8131 struct compunit_symtab *compunit_symtab_iter;
8132 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8133 htab_t all_children, all_type_symtabs;
8134 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8135
8136 /* If we don't have a symtab, we can just skip this case. */
8137 if (cust == NULL)
8138 return;
8139
8140 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8141 NULL, xcalloc, xfree);
8142 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8143 NULL, xcalloc, xfree);
8144
8145 for (ix = 0;
8146 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8147 ix, per_cu_iter);
8148 ++ix)
8149 {
8150 recursively_compute_inclusions (&result_symtabs, all_children,
8151 all_type_symtabs, per_cu_iter,
8152 cust);
8153 }
8154
8155 /* Now we have a transitive closure of all the included symtabs. */
8156 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8157 cust->includes
8158 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8159 struct compunit_symtab *, len + 1);
8160 for (ix = 0;
8161 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8162 compunit_symtab_iter);
8163 ++ix)
8164 cust->includes[ix] = compunit_symtab_iter;
8165 cust->includes[len] = NULL;
8166
8167 VEC_free (compunit_symtab_ptr, result_symtabs);
8168 htab_delete (all_children);
8169 htab_delete (all_type_symtabs);
8170 }
8171 }
8172
8173 /* Compute the 'includes' field for the symtabs of all the CUs we just
8174 read. */
8175
8176 static void
8177 process_cu_includes (void)
8178 {
8179 int ix;
8180 struct dwarf2_per_cu_data *iter;
8181
8182 for (ix = 0;
8183 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8184 ix, iter);
8185 ++ix)
8186 {
8187 if (! iter->is_debug_types)
8188 compute_compunit_symtab_includes (iter);
8189 }
8190
8191 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8192 }
8193
8194 /* Generate full symbol information for PER_CU, whose DIEs have
8195 already been loaded into memory. */
8196
8197 static void
8198 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8199 enum language pretend_language)
8200 {
8201 struct dwarf2_cu *cu = per_cu->cu;
8202 struct objfile *objfile = per_cu->objfile;
8203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8204 CORE_ADDR lowpc, highpc;
8205 struct compunit_symtab *cust;
8206 struct cleanup *back_to, *delayed_list_cleanup;
8207 CORE_ADDR baseaddr;
8208 struct block *static_block;
8209 CORE_ADDR addr;
8210
8211 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8212
8213 buildsym_init ();
8214 back_to = make_cleanup (really_free_pendings, NULL);
8215 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8216
8217 cu->list_in_scope = &file_symbols;
8218
8219 cu->language = pretend_language;
8220 cu->language_defn = language_def (cu->language);
8221
8222 /* Do line number decoding in read_file_scope () */
8223 process_die (cu->dies, cu);
8224
8225 /* For now fudge the Go package. */
8226 if (cu->language == language_go)
8227 fixup_go_packaging (cu);
8228
8229 /* Now that we have processed all the DIEs in the CU, all the types
8230 should be complete, and it should now be safe to compute all of the
8231 physnames. */
8232 compute_delayed_physnames (cu);
8233 do_cleanups (delayed_list_cleanup);
8234
8235 /* Some compilers don't define a DW_AT_high_pc attribute for the
8236 compilation unit. If the DW_AT_high_pc is missing, synthesize
8237 it, by scanning the DIE's below the compilation unit. */
8238 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8239
8240 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8241 static_block = end_symtab_get_static_block (addr, 0, 1);
8242
8243 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8244 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8245 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8246 addrmap to help ensure it has an accurate map of pc values belonging to
8247 this comp unit. */
8248 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8249
8250 cust = end_symtab_from_static_block (static_block,
8251 SECT_OFF_TEXT (objfile), 0);
8252
8253 if (cust != NULL)
8254 {
8255 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8256
8257 /* Set symtab language to language from DW_AT_language. If the
8258 compilation is from a C file generated by language preprocessors, do
8259 not set the language if it was already deduced by start_subfile. */
8260 if (!(cu->language == language_c
8261 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8262 COMPUNIT_FILETABS (cust)->language = cu->language;
8263
8264 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8265 produce DW_AT_location with location lists but it can be possibly
8266 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8267 there were bugs in prologue debug info, fixed later in GCC-4.5
8268 by "unwind info for epilogues" patch (which is not directly related).
8269
8270 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8271 needed, it would be wrong due to missing DW_AT_producer there.
8272
8273 Still one can confuse GDB by using non-standard GCC compilation
8274 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8275 */
8276 if (cu->has_loclist && gcc_4_minor >= 5)
8277 cust->locations_valid = 1;
8278
8279 if (gcc_4_minor >= 5)
8280 cust->epilogue_unwind_valid = 1;
8281
8282 cust->call_site_htab = cu->call_site_htab;
8283 }
8284
8285 if (dwarf2_per_objfile->using_index)
8286 per_cu->v.quick->compunit_symtab = cust;
8287 else
8288 {
8289 struct partial_symtab *pst = per_cu->v.psymtab;
8290 pst->compunit_symtab = cust;
8291 pst->readin = 1;
8292 }
8293
8294 /* Push it for inclusion processing later. */
8295 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8296
8297 do_cleanups (back_to);
8298 }
8299
8300 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8301 already been loaded into memory. */
8302
8303 static void
8304 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8305 enum language pretend_language)
8306 {
8307 struct dwarf2_cu *cu = per_cu->cu;
8308 struct objfile *objfile = per_cu->objfile;
8309 struct compunit_symtab *cust;
8310 struct cleanup *back_to, *delayed_list_cleanup;
8311 struct signatured_type *sig_type;
8312
8313 gdb_assert (per_cu->is_debug_types);
8314 sig_type = (struct signatured_type *) per_cu;
8315
8316 buildsym_init ();
8317 back_to = make_cleanup (really_free_pendings, NULL);
8318 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8319
8320 cu->list_in_scope = &file_symbols;
8321
8322 cu->language = pretend_language;
8323 cu->language_defn = language_def (cu->language);
8324
8325 /* The symbol tables are set up in read_type_unit_scope. */
8326 process_die (cu->dies, cu);
8327
8328 /* For now fudge the Go package. */
8329 if (cu->language == language_go)
8330 fixup_go_packaging (cu);
8331
8332 /* Now that we have processed all the DIEs in the CU, all the types
8333 should be complete, and it should now be safe to compute all of the
8334 physnames. */
8335 compute_delayed_physnames (cu);
8336 do_cleanups (delayed_list_cleanup);
8337
8338 /* TUs share symbol tables.
8339 If this is the first TU to use this symtab, complete the construction
8340 of it with end_expandable_symtab. Otherwise, complete the addition of
8341 this TU's symbols to the existing symtab. */
8342 if (sig_type->type_unit_group->compunit_symtab == NULL)
8343 {
8344 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8345 sig_type->type_unit_group->compunit_symtab = cust;
8346
8347 if (cust != NULL)
8348 {
8349 /* Set symtab language to language from DW_AT_language. If the
8350 compilation is from a C file generated by language preprocessors,
8351 do not set the language if it was already deduced by
8352 start_subfile. */
8353 if (!(cu->language == language_c
8354 && COMPUNIT_FILETABS (cust)->language != language_c))
8355 COMPUNIT_FILETABS (cust)->language = cu->language;
8356 }
8357 }
8358 else
8359 {
8360 augment_type_symtab ();
8361 cust = sig_type->type_unit_group->compunit_symtab;
8362 }
8363
8364 if (dwarf2_per_objfile->using_index)
8365 per_cu->v.quick->compunit_symtab = cust;
8366 else
8367 {
8368 struct partial_symtab *pst = per_cu->v.psymtab;
8369 pst->compunit_symtab = cust;
8370 pst->readin = 1;
8371 }
8372
8373 do_cleanups (back_to);
8374 }
8375
8376 /* Process an imported unit DIE. */
8377
8378 static void
8379 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8380 {
8381 struct attribute *attr;
8382
8383 /* For now we don't handle imported units in type units. */
8384 if (cu->per_cu->is_debug_types)
8385 {
8386 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8387 " supported in type units [in module %s]"),
8388 objfile_name (cu->objfile));
8389 }
8390
8391 attr = dwarf2_attr (die, DW_AT_import, cu);
8392 if (attr != NULL)
8393 {
8394 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8395 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8396 dwarf2_per_cu_data *per_cu
8397 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8398
8399 /* If necessary, add it to the queue and load its DIEs. */
8400 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8401 load_full_comp_unit (per_cu, cu->language);
8402
8403 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8404 per_cu);
8405 }
8406 }
8407
8408 /* Reset the in_process bit of a die. */
8409
8410 static void
8411 reset_die_in_process (void *arg)
8412 {
8413 struct die_info *die = (struct die_info *) arg;
8414
8415 die->in_process = 0;
8416 }
8417
8418 /* Process a die and its children. */
8419
8420 static void
8421 process_die (struct die_info *die, struct dwarf2_cu *cu)
8422 {
8423 struct cleanup *in_process;
8424
8425 /* We should only be processing those not already in process. */
8426 gdb_assert (!die->in_process);
8427
8428 die->in_process = 1;
8429 in_process = make_cleanup (reset_die_in_process,die);
8430
8431 switch (die->tag)
8432 {
8433 case DW_TAG_padding:
8434 break;
8435 case DW_TAG_compile_unit:
8436 case DW_TAG_partial_unit:
8437 read_file_scope (die, cu);
8438 break;
8439 case DW_TAG_type_unit:
8440 read_type_unit_scope (die, cu);
8441 break;
8442 case DW_TAG_subprogram:
8443 case DW_TAG_inlined_subroutine:
8444 read_func_scope (die, cu);
8445 break;
8446 case DW_TAG_lexical_block:
8447 case DW_TAG_try_block:
8448 case DW_TAG_catch_block:
8449 read_lexical_block_scope (die, cu);
8450 break;
8451 case DW_TAG_call_site:
8452 case DW_TAG_GNU_call_site:
8453 read_call_site_scope (die, cu);
8454 break;
8455 case DW_TAG_class_type:
8456 case DW_TAG_interface_type:
8457 case DW_TAG_structure_type:
8458 case DW_TAG_union_type:
8459 process_structure_scope (die, cu);
8460 break;
8461 case DW_TAG_enumeration_type:
8462 process_enumeration_scope (die, cu);
8463 break;
8464
8465 /* These dies have a type, but processing them does not create
8466 a symbol or recurse to process the children. Therefore we can
8467 read them on-demand through read_type_die. */
8468 case DW_TAG_subroutine_type:
8469 case DW_TAG_set_type:
8470 case DW_TAG_array_type:
8471 case DW_TAG_pointer_type:
8472 case DW_TAG_ptr_to_member_type:
8473 case DW_TAG_reference_type:
8474 case DW_TAG_rvalue_reference_type:
8475 case DW_TAG_string_type:
8476 break;
8477
8478 case DW_TAG_base_type:
8479 case DW_TAG_subrange_type:
8480 case DW_TAG_typedef:
8481 /* Add a typedef symbol for the type definition, if it has a
8482 DW_AT_name. */
8483 new_symbol (die, read_type_die (die, cu), cu);
8484 break;
8485 case DW_TAG_common_block:
8486 read_common_block (die, cu);
8487 break;
8488 case DW_TAG_common_inclusion:
8489 break;
8490 case DW_TAG_namespace:
8491 cu->processing_has_namespace_info = 1;
8492 read_namespace (die, cu);
8493 break;
8494 case DW_TAG_module:
8495 cu->processing_has_namespace_info = 1;
8496 read_module (die, cu);
8497 break;
8498 case DW_TAG_imported_declaration:
8499 cu->processing_has_namespace_info = 1;
8500 if (read_namespace_alias (die, cu))
8501 break;
8502 /* The declaration is not a global namespace alias: fall through. */
8503 case DW_TAG_imported_module:
8504 cu->processing_has_namespace_info = 1;
8505 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8506 || cu->language != language_fortran))
8507 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8508 dwarf_tag_name (die->tag));
8509 read_import_statement (die, cu);
8510 break;
8511
8512 case DW_TAG_imported_unit:
8513 process_imported_unit_die (die, cu);
8514 break;
8515
8516 default:
8517 new_symbol (die, NULL, cu);
8518 break;
8519 }
8520
8521 do_cleanups (in_process);
8522 }
8523 \f
8524 /* DWARF name computation. */
8525
8526 /* A helper function for dwarf2_compute_name which determines whether DIE
8527 needs to have the name of the scope prepended to the name listed in the
8528 die. */
8529
8530 static int
8531 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8532 {
8533 struct attribute *attr;
8534
8535 switch (die->tag)
8536 {
8537 case DW_TAG_namespace:
8538 case DW_TAG_typedef:
8539 case DW_TAG_class_type:
8540 case DW_TAG_interface_type:
8541 case DW_TAG_structure_type:
8542 case DW_TAG_union_type:
8543 case DW_TAG_enumeration_type:
8544 case DW_TAG_enumerator:
8545 case DW_TAG_subprogram:
8546 case DW_TAG_inlined_subroutine:
8547 case DW_TAG_member:
8548 case DW_TAG_imported_declaration:
8549 return 1;
8550
8551 case DW_TAG_variable:
8552 case DW_TAG_constant:
8553 /* We only need to prefix "globally" visible variables. These include
8554 any variable marked with DW_AT_external or any variable that
8555 lives in a namespace. [Variables in anonymous namespaces
8556 require prefixing, but they are not DW_AT_external.] */
8557
8558 if (dwarf2_attr (die, DW_AT_specification, cu))
8559 {
8560 struct dwarf2_cu *spec_cu = cu;
8561
8562 return die_needs_namespace (die_specification (die, &spec_cu),
8563 spec_cu);
8564 }
8565
8566 attr = dwarf2_attr (die, DW_AT_external, cu);
8567 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8568 && die->parent->tag != DW_TAG_module)
8569 return 0;
8570 /* A variable in a lexical block of some kind does not need a
8571 namespace, even though in C++ such variables may be external
8572 and have a mangled name. */
8573 if (die->parent->tag == DW_TAG_lexical_block
8574 || die->parent->tag == DW_TAG_try_block
8575 || die->parent->tag == DW_TAG_catch_block
8576 || die->parent->tag == DW_TAG_subprogram)
8577 return 0;
8578 return 1;
8579
8580 default:
8581 return 0;
8582 }
8583 }
8584
8585 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8586 compute the physname for the object, which include a method's:
8587 - formal parameters (C++),
8588 - receiver type (Go),
8589
8590 The term "physname" is a bit confusing.
8591 For C++, for example, it is the demangled name.
8592 For Go, for example, it's the mangled name.
8593
8594 For Ada, return the DIE's linkage name rather than the fully qualified
8595 name. PHYSNAME is ignored..
8596
8597 The result is allocated on the objfile_obstack and canonicalized. */
8598
8599 static const char *
8600 dwarf2_compute_name (const char *name,
8601 struct die_info *die, struct dwarf2_cu *cu,
8602 int physname)
8603 {
8604 struct objfile *objfile = cu->objfile;
8605
8606 if (name == NULL)
8607 name = dwarf2_name (die, cu);
8608
8609 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8610 but otherwise compute it by typename_concat inside GDB.
8611 FIXME: Actually this is not really true, or at least not always true.
8612 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8613 Fortran names because there is no mangling standard. So new_symbol_full
8614 will set the demangled name to the result of dwarf2_full_name, and it is
8615 the demangled name that GDB uses if it exists. */
8616 if (cu->language == language_ada
8617 || (cu->language == language_fortran && physname))
8618 {
8619 /* For Ada unit, we prefer the linkage name over the name, as
8620 the former contains the exported name, which the user expects
8621 to be able to reference. Ideally, we want the user to be able
8622 to reference this entity using either natural or linkage name,
8623 but we haven't started looking at this enhancement yet. */
8624 const char *linkage_name;
8625
8626 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8627 if (linkage_name == NULL)
8628 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8629 if (linkage_name != NULL)
8630 return linkage_name;
8631 }
8632
8633 /* These are the only languages we know how to qualify names in. */
8634 if (name != NULL
8635 && (cu->language == language_cplus
8636 || cu->language == language_fortran || cu->language == language_d
8637 || cu->language == language_rust))
8638 {
8639 if (die_needs_namespace (die, cu))
8640 {
8641 long length;
8642 const char *prefix;
8643 const char *canonical_name = NULL;
8644
8645 string_file buf;
8646
8647 prefix = determine_prefix (die, cu);
8648 if (*prefix != '\0')
8649 {
8650 char *prefixed_name = typename_concat (NULL, prefix, name,
8651 physname, cu);
8652
8653 buf.puts (prefixed_name);
8654 xfree (prefixed_name);
8655 }
8656 else
8657 buf.puts (name);
8658
8659 /* Template parameters may be specified in the DIE's DW_AT_name, or
8660 as children with DW_TAG_template_type_param or
8661 DW_TAG_value_type_param. If the latter, add them to the name
8662 here. If the name already has template parameters, then
8663 skip this step; some versions of GCC emit both, and
8664 it is more efficient to use the pre-computed name.
8665
8666 Something to keep in mind about this process: it is very
8667 unlikely, or in some cases downright impossible, to produce
8668 something that will match the mangled name of a function.
8669 If the definition of the function has the same debug info,
8670 we should be able to match up with it anyway. But fallbacks
8671 using the minimal symbol, for instance to find a method
8672 implemented in a stripped copy of libstdc++, will not work.
8673 If we do not have debug info for the definition, we will have to
8674 match them up some other way.
8675
8676 When we do name matching there is a related problem with function
8677 templates; two instantiated function templates are allowed to
8678 differ only by their return types, which we do not add here. */
8679
8680 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8681 {
8682 struct attribute *attr;
8683 struct die_info *child;
8684 int first = 1;
8685
8686 die->building_fullname = 1;
8687
8688 for (child = die->child; child != NULL; child = child->sibling)
8689 {
8690 struct type *type;
8691 LONGEST value;
8692 const gdb_byte *bytes;
8693 struct dwarf2_locexpr_baton *baton;
8694 struct value *v;
8695
8696 if (child->tag != DW_TAG_template_type_param
8697 && child->tag != DW_TAG_template_value_param)
8698 continue;
8699
8700 if (first)
8701 {
8702 buf.puts ("<");
8703 first = 0;
8704 }
8705 else
8706 buf.puts (", ");
8707
8708 attr = dwarf2_attr (child, DW_AT_type, cu);
8709 if (attr == NULL)
8710 {
8711 complaint (&symfile_complaints,
8712 _("template parameter missing DW_AT_type"));
8713 buf.puts ("UNKNOWN_TYPE");
8714 continue;
8715 }
8716 type = die_type (child, cu);
8717
8718 if (child->tag == DW_TAG_template_type_param)
8719 {
8720 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8721 continue;
8722 }
8723
8724 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8725 if (attr == NULL)
8726 {
8727 complaint (&symfile_complaints,
8728 _("template parameter missing "
8729 "DW_AT_const_value"));
8730 buf.puts ("UNKNOWN_VALUE");
8731 continue;
8732 }
8733
8734 dwarf2_const_value_attr (attr, type, name,
8735 &cu->comp_unit_obstack, cu,
8736 &value, &bytes, &baton);
8737
8738 if (TYPE_NOSIGN (type))
8739 /* GDB prints characters as NUMBER 'CHAR'. If that's
8740 changed, this can use value_print instead. */
8741 c_printchar (value, type, &buf);
8742 else
8743 {
8744 struct value_print_options opts;
8745
8746 if (baton != NULL)
8747 v = dwarf2_evaluate_loc_desc (type, NULL,
8748 baton->data,
8749 baton->size,
8750 baton->per_cu);
8751 else if (bytes != NULL)
8752 {
8753 v = allocate_value (type);
8754 memcpy (value_contents_writeable (v), bytes,
8755 TYPE_LENGTH (type));
8756 }
8757 else
8758 v = value_from_longest (type, value);
8759
8760 /* Specify decimal so that we do not depend on
8761 the radix. */
8762 get_formatted_print_options (&opts, 'd');
8763 opts.raw = 1;
8764 value_print (v, &buf, &opts);
8765 release_value (v);
8766 value_free (v);
8767 }
8768 }
8769
8770 die->building_fullname = 0;
8771
8772 if (!first)
8773 {
8774 /* Close the argument list, with a space if necessary
8775 (nested templates). */
8776 if (!buf.empty () && buf.string ().back () == '>')
8777 buf.puts (" >");
8778 else
8779 buf.puts (">");
8780 }
8781 }
8782
8783 /* For C++ methods, append formal parameter type
8784 information, if PHYSNAME. */
8785
8786 if (physname && die->tag == DW_TAG_subprogram
8787 && cu->language == language_cplus)
8788 {
8789 struct type *type = read_type_die (die, cu);
8790
8791 c_type_print_args (type, &buf, 1, cu->language,
8792 &type_print_raw_options);
8793
8794 if (cu->language == language_cplus)
8795 {
8796 /* Assume that an artificial first parameter is
8797 "this", but do not crash if it is not. RealView
8798 marks unnamed (and thus unused) parameters as
8799 artificial; there is no way to differentiate
8800 the two cases. */
8801 if (TYPE_NFIELDS (type) > 0
8802 && TYPE_FIELD_ARTIFICIAL (type, 0)
8803 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8804 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8805 0))))
8806 buf.puts (" const");
8807 }
8808 }
8809
8810 const std::string &intermediate_name = buf.string ();
8811
8812 if (cu->language == language_cplus)
8813 canonical_name
8814 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8815 &objfile->per_bfd->storage_obstack);
8816
8817 /* If we only computed INTERMEDIATE_NAME, or if
8818 INTERMEDIATE_NAME is already canonical, then we need to
8819 copy it to the appropriate obstack. */
8820 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8821 name = ((const char *)
8822 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8823 intermediate_name.c_str (),
8824 intermediate_name.length ()));
8825 else
8826 name = canonical_name;
8827 }
8828 }
8829
8830 return name;
8831 }
8832
8833 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8834 If scope qualifiers are appropriate they will be added. The result
8835 will be allocated on the storage_obstack, or NULL if the DIE does
8836 not have a name. NAME may either be from a previous call to
8837 dwarf2_name or NULL.
8838
8839 The output string will be canonicalized (if C++). */
8840
8841 static const char *
8842 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8843 {
8844 return dwarf2_compute_name (name, die, cu, 0);
8845 }
8846
8847 /* Construct a physname for the given DIE in CU. NAME may either be
8848 from a previous call to dwarf2_name or NULL. The result will be
8849 allocated on the objfile_objstack or NULL if the DIE does not have a
8850 name.
8851
8852 The output string will be canonicalized (if C++). */
8853
8854 static const char *
8855 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8856 {
8857 struct objfile *objfile = cu->objfile;
8858 const char *retval, *mangled = NULL, *canon = NULL;
8859 struct cleanup *back_to;
8860 int need_copy = 1;
8861
8862 /* In this case dwarf2_compute_name is just a shortcut not building anything
8863 on its own. */
8864 if (!die_needs_namespace (die, cu))
8865 return dwarf2_compute_name (name, die, cu, 1);
8866
8867 back_to = make_cleanup (null_cleanup, NULL);
8868
8869 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8870 if (mangled == NULL)
8871 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8872
8873 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8874 See https://github.com/rust-lang/rust/issues/32925. */
8875 if (cu->language == language_rust && mangled != NULL
8876 && strchr (mangled, '{') != NULL)
8877 mangled = NULL;
8878
8879 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8880 has computed. */
8881 if (mangled != NULL)
8882 {
8883 char *demangled;
8884
8885 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8886 type. It is easier for GDB users to search for such functions as
8887 `name(params)' than `long name(params)'. In such case the minimal
8888 symbol names do not match the full symbol names but for template
8889 functions there is never a need to look up their definition from their
8890 declaration so the only disadvantage remains the minimal symbol
8891 variant `long name(params)' does not have the proper inferior type.
8892 */
8893
8894 if (cu->language == language_go)
8895 {
8896 /* This is a lie, but we already lie to the caller new_symbol_full.
8897 new_symbol_full assumes we return the mangled name.
8898 This just undoes that lie until things are cleaned up. */
8899 demangled = NULL;
8900 }
8901 else
8902 {
8903 demangled = gdb_demangle (mangled,
8904 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8905 }
8906 if (demangled)
8907 {
8908 make_cleanup (xfree, demangled);
8909 canon = demangled;
8910 }
8911 else
8912 {
8913 canon = mangled;
8914 need_copy = 0;
8915 }
8916 }
8917
8918 if (canon == NULL || check_physname)
8919 {
8920 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8921
8922 if (canon != NULL && strcmp (physname, canon) != 0)
8923 {
8924 /* It may not mean a bug in GDB. The compiler could also
8925 compute DW_AT_linkage_name incorrectly. But in such case
8926 GDB would need to be bug-to-bug compatible. */
8927
8928 complaint (&symfile_complaints,
8929 _("Computed physname <%s> does not match demangled <%s> "
8930 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8931 physname, canon, mangled, to_underlying (die->sect_off),
8932 objfile_name (objfile));
8933
8934 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8935 is available here - over computed PHYSNAME. It is safer
8936 against both buggy GDB and buggy compilers. */
8937
8938 retval = canon;
8939 }
8940 else
8941 {
8942 retval = physname;
8943 need_copy = 0;
8944 }
8945 }
8946 else
8947 retval = canon;
8948
8949 if (need_copy)
8950 retval = ((const char *)
8951 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8952 retval, strlen (retval)));
8953
8954 do_cleanups (back_to);
8955 return retval;
8956 }
8957
8958 /* Inspect DIE in CU for a namespace alias. If one exists, record
8959 a new symbol for it.
8960
8961 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8962
8963 static int
8964 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8965 {
8966 struct attribute *attr;
8967
8968 /* If the die does not have a name, this is not a namespace
8969 alias. */
8970 attr = dwarf2_attr (die, DW_AT_name, cu);
8971 if (attr != NULL)
8972 {
8973 int num;
8974 struct die_info *d = die;
8975 struct dwarf2_cu *imported_cu = cu;
8976
8977 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8978 keep inspecting DIEs until we hit the underlying import. */
8979 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8980 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8981 {
8982 attr = dwarf2_attr (d, DW_AT_import, cu);
8983 if (attr == NULL)
8984 break;
8985
8986 d = follow_die_ref (d, attr, &imported_cu);
8987 if (d->tag != DW_TAG_imported_declaration)
8988 break;
8989 }
8990
8991 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8992 {
8993 complaint (&symfile_complaints,
8994 _("DIE at 0x%x has too many recursively imported "
8995 "declarations"), to_underlying (d->sect_off));
8996 return 0;
8997 }
8998
8999 if (attr != NULL)
9000 {
9001 struct type *type;
9002 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9003
9004 type = get_die_type_at_offset (sect_off, cu->per_cu);
9005 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9006 {
9007 /* This declaration is a global namespace alias. Add
9008 a symbol for it whose type is the aliased namespace. */
9009 new_symbol (die, type, cu);
9010 return 1;
9011 }
9012 }
9013 }
9014
9015 return 0;
9016 }
9017
9018 /* Return the using directives repository (global or local?) to use in the
9019 current context for LANGUAGE.
9020
9021 For Ada, imported declarations can materialize renamings, which *may* be
9022 global. However it is impossible (for now?) in DWARF to distinguish
9023 "external" imported declarations and "static" ones. As all imported
9024 declarations seem to be static in all other languages, make them all CU-wide
9025 global only in Ada. */
9026
9027 static struct using_direct **
9028 using_directives (enum language language)
9029 {
9030 if (language == language_ada && context_stack_depth == 0)
9031 return &global_using_directives;
9032 else
9033 return &local_using_directives;
9034 }
9035
9036 /* Read the import statement specified by the given die and record it. */
9037
9038 static void
9039 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9040 {
9041 struct objfile *objfile = cu->objfile;
9042 struct attribute *import_attr;
9043 struct die_info *imported_die, *child_die;
9044 struct dwarf2_cu *imported_cu;
9045 const char *imported_name;
9046 const char *imported_name_prefix;
9047 const char *canonical_name;
9048 const char *import_alias;
9049 const char *imported_declaration = NULL;
9050 const char *import_prefix;
9051 VEC (const_char_ptr) *excludes = NULL;
9052 struct cleanup *cleanups;
9053
9054 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9055 if (import_attr == NULL)
9056 {
9057 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9058 dwarf_tag_name (die->tag));
9059 return;
9060 }
9061
9062 imported_cu = cu;
9063 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9064 imported_name = dwarf2_name (imported_die, imported_cu);
9065 if (imported_name == NULL)
9066 {
9067 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9068
9069 The import in the following code:
9070 namespace A
9071 {
9072 typedef int B;
9073 }
9074
9075 int main ()
9076 {
9077 using A::B;
9078 B b;
9079 return b;
9080 }
9081
9082 ...
9083 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9084 <52> DW_AT_decl_file : 1
9085 <53> DW_AT_decl_line : 6
9086 <54> DW_AT_import : <0x75>
9087 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9088 <59> DW_AT_name : B
9089 <5b> DW_AT_decl_file : 1
9090 <5c> DW_AT_decl_line : 2
9091 <5d> DW_AT_type : <0x6e>
9092 ...
9093 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9094 <76> DW_AT_byte_size : 4
9095 <77> DW_AT_encoding : 5 (signed)
9096
9097 imports the wrong die ( 0x75 instead of 0x58 ).
9098 This case will be ignored until the gcc bug is fixed. */
9099 return;
9100 }
9101
9102 /* Figure out the local name after import. */
9103 import_alias = dwarf2_name (die, cu);
9104
9105 /* Figure out where the statement is being imported to. */
9106 import_prefix = determine_prefix (die, cu);
9107
9108 /* Figure out what the scope of the imported die is and prepend it
9109 to the name of the imported die. */
9110 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9111
9112 if (imported_die->tag != DW_TAG_namespace
9113 && imported_die->tag != DW_TAG_module)
9114 {
9115 imported_declaration = imported_name;
9116 canonical_name = imported_name_prefix;
9117 }
9118 else if (strlen (imported_name_prefix) > 0)
9119 canonical_name = obconcat (&objfile->objfile_obstack,
9120 imported_name_prefix,
9121 (cu->language == language_d ? "." : "::"),
9122 imported_name, (char *) NULL);
9123 else
9124 canonical_name = imported_name;
9125
9126 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9127
9128 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9129 for (child_die = die->child; child_die && child_die->tag;
9130 child_die = sibling_die (child_die))
9131 {
9132 /* DWARF-4: A Fortran use statement with a “rename list” may be
9133 represented by an imported module entry with an import attribute
9134 referring to the module and owned entries corresponding to those
9135 entities that are renamed as part of being imported. */
9136
9137 if (child_die->tag != DW_TAG_imported_declaration)
9138 {
9139 complaint (&symfile_complaints,
9140 _("child DW_TAG_imported_declaration expected "
9141 "- DIE at 0x%x [in module %s]"),
9142 to_underlying (child_die->sect_off), objfile_name (objfile));
9143 continue;
9144 }
9145
9146 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9147 if (import_attr == NULL)
9148 {
9149 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9150 dwarf_tag_name (child_die->tag));
9151 continue;
9152 }
9153
9154 imported_cu = cu;
9155 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9156 &imported_cu);
9157 imported_name = dwarf2_name (imported_die, imported_cu);
9158 if (imported_name == NULL)
9159 {
9160 complaint (&symfile_complaints,
9161 _("child DW_TAG_imported_declaration has unknown "
9162 "imported name - DIE at 0x%x [in module %s]"),
9163 to_underlying (child_die->sect_off), objfile_name (objfile));
9164 continue;
9165 }
9166
9167 VEC_safe_push (const_char_ptr, excludes, imported_name);
9168
9169 process_die (child_die, cu);
9170 }
9171
9172 add_using_directive (using_directives (cu->language),
9173 import_prefix,
9174 canonical_name,
9175 import_alias,
9176 imported_declaration,
9177 excludes,
9178 0,
9179 &objfile->objfile_obstack);
9180
9181 do_cleanups (cleanups);
9182 }
9183
9184 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9185 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9186 this, it was first present in GCC release 4.3.0. */
9187
9188 static int
9189 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9190 {
9191 if (!cu->checked_producer)
9192 check_producer (cu);
9193
9194 return cu->producer_is_gcc_lt_4_3;
9195 }
9196
9197 static file_and_directory
9198 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9199 {
9200 file_and_directory res;
9201
9202 /* Find the filename. Do not use dwarf2_name here, since the filename
9203 is not a source language identifier. */
9204 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9205 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9206
9207 if (res.comp_dir == NULL
9208 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9209 && IS_ABSOLUTE_PATH (res.name))
9210 {
9211 res.comp_dir_storage = ldirname (res.name);
9212 if (!res.comp_dir_storage.empty ())
9213 res.comp_dir = res.comp_dir_storage.c_str ();
9214 }
9215 if (res.comp_dir != NULL)
9216 {
9217 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9218 directory, get rid of it. */
9219 const char *cp = strchr (res.comp_dir, ':');
9220
9221 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9222 res.comp_dir = cp + 1;
9223 }
9224
9225 if (res.name == NULL)
9226 res.name = "<unknown>";
9227
9228 return res;
9229 }
9230
9231 /* Handle DW_AT_stmt_list for a compilation unit.
9232 DIE is the DW_TAG_compile_unit die for CU.
9233 COMP_DIR is the compilation directory. LOWPC is passed to
9234 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9235
9236 static void
9237 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9238 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9239 {
9240 struct objfile *objfile = dwarf2_per_objfile->objfile;
9241 struct attribute *attr;
9242 struct line_header line_header_local;
9243 hashval_t line_header_local_hash;
9244 unsigned u;
9245 void **slot;
9246 int decode_mapping;
9247
9248 gdb_assert (! cu->per_cu->is_debug_types);
9249
9250 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9251 if (attr == NULL)
9252 return;
9253
9254 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9255
9256 /* The line header hash table is only created if needed (it exists to
9257 prevent redundant reading of the line table for partial_units).
9258 If we're given a partial_unit, we'll need it. If we're given a
9259 compile_unit, then use the line header hash table if it's already
9260 created, but don't create one just yet. */
9261
9262 if (dwarf2_per_objfile->line_header_hash == NULL
9263 && die->tag == DW_TAG_partial_unit)
9264 {
9265 dwarf2_per_objfile->line_header_hash
9266 = htab_create_alloc_ex (127, line_header_hash_voidp,
9267 line_header_eq_voidp,
9268 free_line_header_voidp,
9269 &objfile->objfile_obstack,
9270 hashtab_obstack_allocate,
9271 dummy_obstack_deallocate);
9272 }
9273
9274 line_header_local.sect_off = line_offset;
9275 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9276 line_header_local_hash = line_header_hash (&line_header_local);
9277 if (dwarf2_per_objfile->line_header_hash != NULL)
9278 {
9279 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9280 &line_header_local,
9281 line_header_local_hash, NO_INSERT);
9282
9283 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9284 is not present in *SLOT (since if there is something in *SLOT then
9285 it will be for a partial_unit). */
9286 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9287 {
9288 gdb_assert (*slot != NULL);
9289 cu->line_header = (struct line_header *) *slot;
9290 return;
9291 }
9292 }
9293
9294 /* dwarf_decode_line_header does not yet provide sufficient information.
9295 We always have to call also dwarf_decode_lines for it. */
9296 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9297 if (lh == NULL)
9298 return;
9299 cu->line_header = lh.get ();
9300
9301 if (dwarf2_per_objfile->line_header_hash == NULL)
9302 slot = NULL;
9303 else
9304 {
9305 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9306 &line_header_local,
9307 line_header_local_hash, INSERT);
9308 gdb_assert (slot != NULL);
9309 }
9310 if (slot != NULL && *slot == NULL)
9311 {
9312 /* This newly decoded line number information unit will be owned
9313 by line_header_hash hash table. */
9314 *slot = cu->line_header;
9315 }
9316 else
9317 {
9318 /* We cannot free any current entry in (*slot) as that struct line_header
9319 may be already used by multiple CUs. Create only temporary decoded
9320 line_header for this CU - it may happen at most once for each line
9321 number information unit. And if we're not using line_header_hash
9322 then this is what we want as well. */
9323 gdb_assert (die->tag != DW_TAG_partial_unit);
9324 }
9325 decode_mapping = (die->tag != DW_TAG_partial_unit);
9326 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9327 decode_mapping);
9328
9329 lh.release ();
9330 }
9331
9332 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9333
9334 static void
9335 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9336 {
9337 struct objfile *objfile = dwarf2_per_objfile->objfile;
9338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9339 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9340 CORE_ADDR highpc = ((CORE_ADDR) 0);
9341 struct attribute *attr;
9342 struct die_info *child_die;
9343 CORE_ADDR baseaddr;
9344
9345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9346
9347 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9348
9349 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9350 from finish_block. */
9351 if (lowpc == ((CORE_ADDR) -1))
9352 lowpc = highpc;
9353 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9354
9355 file_and_directory fnd = find_file_and_directory (die, cu);
9356
9357 prepare_one_comp_unit (cu, die, cu->language);
9358
9359 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9360 standardised yet. As a workaround for the language detection we fall
9361 back to the DW_AT_producer string. */
9362 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9363 cu->language = language_opencl;
9364
9365 /* Similar hack for Go. */
9366 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9367 set_cu_language (DW_LANG_Go, cu);
9368
9369 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9370
9371 /* Decode line number information if present. We do this before
9372 processing child DIEs, so that the line header table is available
9373 for DW_AT_decl_file. */
9374 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9375
9376 /* Process all dies in compilation unit. */
9377 if (die->child != NULL)
9378 {
9379 child_die = die->child;
9380 while (child_die && child_die->tag)
9381 {
9382 process_die (child_die, cu);
9383 child_die = sibling_die (child_die);
9384 }
9385 }
9386
9387 /* Decode macro information, if present. Dwarf 2 macro information
9388 refers to information in the line number info statement program
9389 header, so we can only read it if we've read the header
9390 successfully. */
9391 attr = dwarf2_attr (die, DW_AT_macros, cu);
9392 if (attr == NULL)
9393 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9394 if (attr && cu->line_header)
9395 {
9396 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9397 complaint (&symfile_complaints,
9398 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9399
9400 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9401 }
9402 else
9403 {
9404 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9405 if (attr && cu->line_header)
9406 {
9407 unsigned int macro_offset = DW_UNSND (attr);
9408
9409 dwarf_decode_macros (cu, macro_offset, 0);
9410 }
9411 }
9412 }
9413
9414 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9415 Create the set of symtabs used by this TU, or if this TU is sharing
9416 symtabs with another TU and the symtabs have already been created
9417 then restore those symtabs in the line header.
9418 We don't need the pc/line-number mapping for type units. */
9419
9420 static void
9421 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9422 {
9423 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9424 struct type_unit_group *tu_group;
9425 int first_time;
9426 struct attribute *attr;
9427 unsigned int i;
9428 struct signatured_type *sig_type;
9429
9430 gdb_assert (per_cu->is_debug_types);
9431 sig_type = (struct signatured_type *) per_cu;
9432
9433 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9434
9435 /* If we're using .gdb_index (includes -readnow) then
9436 per_cu->type_unit_group may not have been set up yet. */
9437 if (sig_type->type_unit_group == NULL)
9438 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9439 tu_group = sig_type->type_unit_group;
9440
9441 /* If we've already processed this stmt_list there's no real need to
9442 do it again, we could fake it and just recreate the part we need
9443 (file name,index -> symtab mapping). If data shows this optimization
9444 is useful we can do it then. */
9445 first_time = tu_group->compunit_symtab == NULL;
9446
9447 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9448 debug info. */
9449 line_header_up lh;
9450 if (attr != NULL)
9451 {
9452 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9453 lh = dwarf_decode_line_header (line_offset, cu);
9454 }
9455 if (lh == NULL)
9456 {
9457 if (first_time)
9458 dwarf2_start_symtab (cu, "", NULL, 0);
9459 else
9460 {
9461 gdb_assert (tu_group->symtabs == NULL);
9462 restart_symtab (tu_group->compunit_symtab, "", 0);
9463 }
9464 return;
9465 }
9466
9467 cu->line_header = lh.get ();
9468
9469 if (first_time)
9470 {
9471 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9472
9473 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9474 still initializing it, and our caller (a few levels up)
9475 process_full_type_unit still needs to know if this is the first
9476 time. */
9477
9478 tu_group->num_symtabs = lh->file_names.size ();
9479 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
9480
9481 for (i = 0; i < lh->file_names.size (); ++i)
9482 {
9483 file_entry &fe = lh->file_names[i];
9484
9485 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
9486
9487 if (current_subfile->symtab == NULL)
9488 {
9489 /* NOTE: start_subfile will recognize when it's been passed
9490 a file it has already seen. So we can't assume there's a
9491 simple mapping from lh->file_names to subfiles, plus
9492 lh->file_names may contain dups. */
9493 current_subfile->symtab
9494 = allocate_symtab (cust, current_subfile->name);
9495 }
9496
9497 fe.symtab = current_subfile->symtab;
9498 tu_group->symtabs[i] = fe.symtab;
9499 }
9500 }
9501 else
9502 {
9503 restart_symtab (tu_group->compunit_symtab, "", 0);
9504
9505 for (i = 0; i < lh->file_names.size (); ++i)
9506 {
9507 struct file_entry *fe = &lh->file_names[i];
9508
9509 fe->symtab = tu_group->symtabs[i];
9510 }
9511 }
9512
9513 lh.release ();
9514
9515 /* The main symtab is allocated last. Type units don't have DW_AT_name
9516 so they don't have a "real" (so to speak) symtab anyway.
9517 There is later code that will assign the main symtab to all symbols
9518 that don't have one. We need to handle the case of a symbol with a
9519 missing symtab (DW_AT_decl_file) anyway. */
9520 }
9521
9522 /* Process DW_TAG_type_unit.
9523 For TUs we want to skip the first top level sibling if it's not the
9524 actual type being defined by this TU. In this case the first top
9525 level sibling is there to provide context only. */
9526
9527 static void
9528 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9529 {
9530 struct die_info *child_die;
9531
9532 prepare_one_comp_unit (cu, die, language_minimal);
9533
9534 /* Initialize (or reinitialize) the machinery for building symtabs.
9535 We do this before processing child DIEs, so that the line header table
9536 is available for DW_AT_decl_file. */
9537 setup_type_unit_groups (die, cu);
9538
9539 if (die->child != NULL)
9540 {
9541 child_die = die->child;
9542 while (child_die && child_die->tag)
9543 {
9544 process_die (child_die, cu);
9545 child_die = sibling_die (child_die);
9546 }
9547 }
9548 }
9549 \f
9550 /* DWO/DWP files.
9551
9552 http://gcc.gnu.org/wiki/DebugFission
9553 http://gcc.gnu.org/wiki/DebugFissionDWP
9554
9555 To simplify handling of both DWO files ("object" files with the DWARF info)
9556 and DWP files (a file with the DWOs packaged up into one file), we treat
9557 DWP files as having a collection of virtual DWO files. */
9558
9559 static hashval_t
9560 hash_dwo_file (const void *item)
9561 {
9562 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9563 hashval_t hash;
9564
9565 hash = htab_hash_string (dwo_file->dwo_name);
9566 if (dwo_file->comp_dir != NULL)
9567 hash += htab_hash_string (dwo_file->comp_dir);
9568 return hash;
9569 }
9570
9571 static int
9572 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9573 {
9574 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9575 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9576
9577 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9578 return 0;
9579 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9580 return lhs->comp_dir == rhs->comp_dir;
9581 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9582 }
9583
9584 /* Allocate a hash table for DWO files. */
9585
9586 static htab_t
9587 allocate_dwo_file_hash_table (void)
9588 {
9589 struct objfile *objfile = dwarf2_per_objfile->objfile;
9590
9591 return htab_create_alloc_ex (41,
9592 hash_dwo_file,
9593 eq_dwo_file,
9594 NULL,
9595 &objfile->objfile_obstack,
9596 hashtab_obstack_allocate,
9597 dummy_obstack_deallocate);
9598 }
9599
9600 /* Lookup DWO file DWO_NAME. */
9601
9602 static void **
9603 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9604 {
9605 struct dwo_file find_entry;
9606 void **slot;
9607
9608 if (dwarf2_per_objfile->dwo_files == NULL)
9609 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9610
9611 memset (&find_entry, 0, sizeof (find_entry));
9612 find_entry.dwo_name = dwo_name;
9613 find_entry.comp_dir = comp_dir;
9614 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9615
9616 return slot;
9617 }
9618
9619 static hashval_t
9620 hash_dwo_unit (const void *item)
9621 {
9622 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9623
9624 /* This drops the top 32 bits of the id, but is ok for a hash. */
9625 return dwo_unit->signature;
9626 }
9627
9628 static int
9629 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9630 {
9631 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9632 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9633
9634 /* The signature is assumed to be unique within the DWO file.
9635 So while object file CU dwo_id's always have the value zero,
9636 that's OK, assuming each object file DWO file has only one CU,
9637 and that's the rule for now. */
9638 return lhs->signature == rhs->signature;
9639 }
9640
9641 /* Allocate a hash table for DWO CUs,TUs.
9642 There is one of these tables for each of CUs,TUs for each DWO file. */
9643
9644 static htab_t
9645 allocate_dwo_unit_table (struct objfile *objfile)
9646 {
9647 /* Start out with a pretty small number.
9648 Generally DWO files contain only one CU and maybe some TUs. */
9649 return htab_create_alloc_ex (3,
9650 hash_dwo_unit,
9651 eq_dwo_unit,
9652 NULL,
9653 &objfile->objfile_obstack,
9654 hashtab_obstack_allocate,
9655 dummy_obstack_deallocate);
9656 }
9657
9658 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9659
9660 struct create_dwo_cu_data
9661 {
9662 struct dwo_file *dwo_file;
9663 struct dwo_unit dwo_unit;
9664 };
9665
9666 /* die_reader_func for create_dwo_cu. */
9667
9668 static void
9669 create_dwo_cu_reader (const struct die_reader_specs *reader,
9670 const gdb_byte *info_ptr,
9671 struct die_info *comp_unit_die,
9672 int has_children,
9673 void *datap)
9674 {
9675 struct dwarf2_cu *cu = reader->cu;
9676 sect_offset sect_off = cu->per_cu->sect_off;
9677 struct dwarf2_section_info *section = cu->per_cu->section;
9678 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9679 struct dwo_file *dwo_file = data->dwo_file;
9680 struct dwo_unit *dwo_unit = &data->dwo_unit;
9681 struct attribute *attr;
9682
9683 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9684 if (attr == NULL)
9685 {
9686 complaint (&symfile_complaints,
9687 _("Dwarf Error: debug entry at offset 0x%x is missing"
9688 " its dwo_id [in module %s]"),
9689 to_underlying (sect_off), dwo_file->dwo_name);
9690 return;
9691 }
9692
9693 dwo_unit->dwo_file = dwo_file;
9694 dwo_unit->signature = DW_UNSND (attr);
9695 dwo_unit->section = section;
9696 dwo_unit->sect_off = sect_off;
9697 dwo_unit->length = cu->per_cu->length;
9698
9699 if (dwarf_read_debug)
9700 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9701 to_underlying (sect_off),
9702 hex_string (dwo_unit->signature));
9703 }
9704
9705 /* Create the dwo_unit for the lone CU in DWO_FILE.
9706 Note: This function processes DWO files only, not DWP files. */
9707
9708 static struct dwo_unit *
9709 create_dwo_cu (struct dwo_file *dwo_file)
9710 {
9711 struct objfile *objfile = dwarf2_per_objfile->objfile;
9712 struct dwarf2_section_info *section = &dwo_file->sections.info;
9713 const gdb_byte *info_ptr, *end_ptr;
9714 struct create_dwo_cu_data create_dwo_cu_data;
9715 struct dwo_unit *dwo_unit;
9716
9717 dwarf2_read_section (objfile, section);
9718 info_ptr = section->buffer;
9719
9720 if (info_ptr == NULL)
9721 return NULL;
9722
9723 if (dwarf_read_debug)
9724 {
9725 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9726 get_section_name (section),
9727 get_section_file_name (section));
9728 }
9729
9730 create_dwo_cu_data.dwo_file = dwo_file;
9731 dwo_unit = NULL;
9732
9733 end_ptr = info_ptr + section->size;
9734 while (info_ptr < end_ptr)
9735 {
9736 struct dwarf2_per_cu_data per_cu;
9737
9738 memset (&create_dwo_cu_data.dwo_unit, 0,
9739 sizeof (create_dwo_cu_data.dwo_unit));
9740 memset (&per_cu, 0, sizeof (per_cu));
9741 per_cu.objfile = objfile;
9742 per_cu.is_debug_types = 0;
9743 per_cu.sect_off = sect_offset (info_ptr - section->buffer);
9744 per_cu.section = section;
9745
9746 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9747 create_dwo_cu_reader,
9748 &create_dwo_cu_data);
9749
9750 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9751 {
9752 /* If we've already found one, complain. We only support one
9753 because having more than one requires hacking the dwo_name of
9754 each to match, which is highly unlikely to happen. */
9755 if (dwo_unit != NULL)
9756 {
9757 complaint (&symfile_complaints,
9758 _("Multiple CUs in DWO file %s [in module %s]"),
9759 dwo_file->dwo_name, objfile_name (objfile));
9760 break;
9761 }
9762
9763 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9764 *dwo_unit = create_dwo_cu_data.dwo_unit;
9765 }
9766
9767 info_ptr += per_cu.length;
9768 }
9769
9770 return dwo_unit;
9771 }
9772
9773 /* DWP file .debug_{cu,tu}_index section format:
9774 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9775
9776 DWP Version 1:
9777
9778 Both index sections have the same format, and serve to map a 64-bit
9779 signature to a set of section numbers. Each section begins with a header,
9780 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9781 indexes, and a pool of 32-bit section numbers. The index sections will be
9782 aligned at 8-byte boundaries in the file.
9783
9784 The index section header consists of:
9785
9786 V, 32 bit version number
9787 -, 32 bits unused
9788 N, 32 bit number of compilation units or type units in the index
9789 M, 32 bit number of slots in the hash table
9790
9791 Numbers are recorded using the byte order of the application binary.
9792
9793 The hash table begins at offset 16 in the section, and consists of an array
9794 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9795 order of the application binary). Unused slots in the hash table are 0.
9796 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9797
9798 The parallel table begins immediately after the hash table
9799 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9800 array of 32-bit indexes (using the byte order of the application binary),
9801 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9802 table contains a 32-bit index into the pool of section numbers. For unused
9803 hash table slots, the corresponding entry in the parallel table will be 0.
9804
9805 The pool of section numbers begins immediately following the hash table
9806 (at offset 16 + 12 * M from the beginning of the section). The pool of
9807 section numbers consists of an array of 32-bit words (using the byte order
9808 of the application binary). Each item in the array is indexed starting
9809 from 0. The hash table entry provides the index of the first section
9810 number in the set. Additional section numbers in the set follow, and the
9811 set is terminated by a 0 entry (section number 0 is not used in ELF).
9812
9813 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9814 section must be the first entry in the set, and the .debug_abbrev.dwo must
9815 be the second entry. Other members of the set may follow in any order.
9816
9817 ---
9818
9819 DWP Version 2:
9820
9821 DWP Version 2 combines all the .debug_info, etc. sections into one,
9822 and the entries in the index tables are now offsets into these sections.
9823 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9824 section.
9825
9826 Index Section Contents:
9827 Header
9828 Hash Table of Signatures dwp_hash_table.hash_table
9829 Parallel Table of Indices dwp_hash_table.unit_table
9830 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9831 Table of Section Sizes dwp_hash_table.v2.sizes
9832
9833 The index section header consists of:
9834
9835 V, 32 bit version number
9836 L, 32 bit number of columns in the table of section offsets
9837 N, 32 bit number of compilation units or type units in the index
9838 M, 32 bit number of slots in the hash table
9839
9840 Numbers are recorded using the byte order of the application binary.
9841
9842 The hash table has the same format as version 1.
9843 The parallel table of indices has the same format as version 1,
9844 except that the entries are origin-1 indices into the table of sections
9845 offsets and the table of section sizes.
9846
9847 The table of offsets begins immediately following the parallel table
9848 (at offset 16 + 12 * M from the beginning of the section). The table is
9849 a two-dimensional array of 32-bit words (using the byte order of the
9850 application binary), with L columns and N+1 rows, in row-major order.
9851 Each row in the array is indexed starting from 0. The first row provides
9852 a key to the remaining rows: each column in this row provides an identifier
9853 for a debug section, and the offsets in the same column of subsequent rows
9854 refer to that section. The section identifiers are:
9855
9856 DW_SECT_INFO 1 .debug_info.dwo
9857 DW_SECT_TYPES 2 .debug_types.dwo
9858 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9859 DW_SECT_LINE 4 .debug_line.dwo
9860 DW_SECT_LOC 5 .debug_loc.dwo
9861 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9862 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9863 DW_SECT_MACRO 8 .debug_macro.dwo
9864
9865 The offsets provided by the CU and TU index sections are the base offsets
9866 for the contributions made by each CU or TU to the corresponding section
9867 in the package file. Each CU and TU header contains an abbrev_offset
9868 field, used to find the abbreviations table for that CU or TU within the
9869 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9870 be interpreted as relative to the base offset given in the index section.
9871 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9872 should be interpreted as relative to the base offset for .debug_line.dwo,
9873 and offsets into other debug sections obtained from DWARF attributes should
9874 also be interpreted as relative to the corresponding base offset.
9875
9876 The table of sizes begins immediately following the table of offsets.
9877 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9878 with L columns and N rows, in row-major order. Each row in the array is
9879 indexed starting from 1 (row 0 is shared by the two tables).
9880
9881 ---
9882
9883 Hash table lookup is handled the same in version 1 and 2:
9884
9885 We assume that N and M will not exceed 2^32 - 1.
9886 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9887
9888 Given a 64-bit compilation unit signature or a type signature S, an entry
9889 in the hash table is located as follows:
9890
9891 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9892 the low-order k bits all set to 1.
9893
9894 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9895
9896 3) If the hash table entry at index H matches the signature, use that
9897 entry. If the hash table entry at index H is unused (all zeroes),
9898 terminate the search: the signature is not present in the table.
9899
9900 4) Let H = (H + H') modulo M. Repeat at Step 3.
9901
9902 Because M > N and H' and M are relatively prime, the search is guaranteed
9903 to stop at an unused slot or find the match. */
9904
9905 /* Create a hash table to map DWO IDs to their CU/TU entry in
9906 .debug_{info,types}.dwo in DWP_FILE.
9907 Returns NULL if there isn't one.
9908 Note: This function processes DWP files only, not DWO files. */
9909
9910 static struct dwp_hash_table *
9911 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9912 {
9913 struct objfile *objfile = dwarf2_per_objfile->objfile;
9914 bfd *dbfd = dwp_file->dbfd;
9915 const gdb_byte *index_ptr, *index_end;
9916 struct dwarf2_section_info *index;
9917 uint32_t version, nr_columns, nr_units, nr_slots;
9918 struct dwp_hash_table *htab;
9919
9920 if (is_debug_types)
9921 index = &dwp_file->sections.tu_index;
9922 else
9923 index = &dwp_file->sections.cu_index;
9924
9925 if (dwarf2_section_empty_p (index))
9926 return NULL;
9927 dwarf2_read_section (objfile, index);
9928
9929 index_ptr = index->buffer;
9930 index_end = index_ptr + index->size;
9931
9932 version = read_4_bytes (dbfd, index_ptr);
9933 index_ptr += 4;
9934 if (version == 2)
9935 nr_columns = read_4_bytes (dbfd, index_ptr);
9936 else
9937 nr_columns = 0;
9938 index_ptr += 4;
9939 nr_units = read_4_bytes (dbfd, index_ptr);
9940 index_ptr += 4;
9941 nr_slots = read_4_bytes (dbfd, index_ptr);
9942 index_ptr += 4;
9943
9944 if (version != 1 && version != 2)
9945 {
9946 error (_("Dwarf Error: unsupported DWP file version (%s)"
9947 " [in module %s]"),
9948 pulongest (version), dwp_file->name);
9949 }
9950 if (nr_slots != (nr_slots & -nr_slots))
9951 {
9952 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9953 " is not power of 2 [in module %s]"),
9954 pulongest (nr_slots), dwp_file->name);
9955 }
9956
9957 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9958 htab->version = version;
9959 htab->nr_columns = nr_columns;
9960 htab->nr_units = nr_units;
9961 htab->nr_slots = nr_slots;
9962 htab->hash_table = index_ptr;
9963 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9964
9965 /* Exit early if the table is empty. */
9966 if (nr_slots == 0 || nr_units == 0
9967 || (version == 2 && nr_columns == 0))
9968 {
9969 /* All must be zero. */
9970 if (nr_slots != 0 || nr_units != 0
9971 || (version == 2 && nr_columns != 0))
9972 {
9973 complaint (&symfile_complaints,
9974 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9975 " all zero [in modules %s]"),
9976 dwp_file->name);
9977 }
9978 return htab;
9979 }
9980
9981 if (version == 1)
9982 {
9983 htab->section_pool.v1.indices =
9984 htab->unit_table + sizeof (uint32_t) * nr_slots;
9985 /* It's harder to decide whether the section is too small in v1.
9986 V1 is deprecated anyway so we punt. */
9987 }
9988 else
9989 {
9990 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9991 int *ids = htab->section_pool.v2.section_ids;
9992 /* Reverse map for error checking. */
9993 int ids_seen[DW_SECT_MAX + 1];
9994 int i;
9995
9996 if (nr_columns < 2)
9997 {
9998 error (_("Dwarf Error: bad DWP hash table, too few columns"
9999 " in section table [in module %s]"),
10000 dwp_file->name);
10001 }
10002 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10003 {
10004 error (_("Dwarf Error: bad DWP hash table, too many columns"
10005 " in section table [in module %s]"),
10006 dwp_file->name);
10007 }
10008 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10009 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10010 for (i = 0; i < nr_columns; ++i)
10011 {
10012 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10013
10014 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10015 {
10016 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10017 " in section table [in module %s]"),
10018 id, dwp_file->name);
10019 }
10020 if (ids_seen[id] != -1)
10021 {
10022 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10023 " id %d in section table [in module %s]"),
10024 id, dwp_file->name);
10025 }
10026 ids_seen[id] = i;
10027 ids[i] = id;
10028 }
10029 /* Must have exactly one info or types section. */
10030 if (((ids_seen[DW_SECT_INFO] != -1)
10031 + (ids_seen[DW_SECT_TYPES] != -1))
10032 != 1)
10033 {
10034 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10035 " DWO info/types section [in module %s]"),
10036 dwp_file->name);
10037 }
10038 /* Must have an abbrev section. */
10039 if (ids_seen[DW_SECT_ABBREV] == -1)
10040 {
10041 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10042 " section [in module %s]"),
10043 dwp_file->name);
10044 }
10045 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10046 htab->section_pool.v2.sizes =
10047 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10048 * nr_units * nr_columns);
10049 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10050 * nr_units * nr_columns))
10051 > index_end)
10052 {
10053 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10054 " [in module %s]"),
10055 dwp_file->name);
10056 }
10057 }
10058
10059 return htab;
10060 }
10061
10062 /* Update SECTIONS with the data from SECTP.
10063
10064 This function is like the other "locate" section routines that are
10065 passed to bfd_map_over_sections, but in this context the sections to
10066 read comes from the DWP V1 hash table, not the full ELF section table.
10067
10068 The result is non-zero for success, or zero if an error was found. */
10069
10070 static int
10071 locate_v1_virtual_dwo_sections (asection *sectp,
10072 struct virtual_v1_dwo_sections *sections)
10073 {
10074 const struct dwop_section_names *names = &dwop_section_names;
10075
10076 if (section_is_p (sectp->name, &names->abbrev_dwo))
10077 {
10078 /* There can be only one. */
10079 if (sections->abbrev.s.section != NULL)
10080 return 0;
10081 sections->abbrev.s.section = sectp;
10082 sections->abbrev.size = bfd_get_section_size (sectp);
10083 }
10084 else if (section_is_p (sectp->name, &names->info_dwo)
10085 || section_is_p (sectp->name, &names->types_dwo))
10086 {
10087 /* There can be only one. */
10088 if (sections->info_or_types.s.section != NULL)
10089 return 0;
10090 sections->info_or_types.s.section = sectp;
10091 sections->info_or_types.size = bfd_get_section_size (sectp);
10092 }
10093 else if (section_is_p (sectp->name, &names->line_dwo))
10094 {
10095 /* There can be only one. */
10096 if (sections->line.s.section != NULL)
10097 return 0;
10098 sections->line.s.section = sectp;
10099 sections->line.size = bfd_get_section_size (sectp);
10100 }
10101 else if (section_is_p (sectp->name, &names->loc_dwo))
10102 {
10103 /* There can be only one. */
10104 if (sections->loc.s.section != NULL)
10105 return 0;
10106 sections->loc.s.section = sectp;
10107 sections->loc.size = bfd_get_section_size (sectp);
10108 }
10109 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10110 {
10111 /* There can be only one. */
10112 if (sections->macinfo.s.section != NULL)
10113 return 0;
10114 sections->macinfo.s.section = sectp;
10115 sections->macinfo.size = bfd_get_section_size (sectp);
10116 }
10117 else if (section_is_p (sectp->name, &names->macro_dwo))
10118 {
10119 /* There can be only one. */
10120 if (sections->macro.s.section != NULL)
10121 return 0;
10122 sections->macro.s.section = sectp;
10123 sections->macro.size = bfd_get_section_size (sectp);
10124 }
10125 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10126 {
10127 /* There can be only one. */
10128 if (sections->str_offsets.s.section != NULL)
10129 return 0;
10130 sections->str_offsets.s.section = sectp;
10131 sections->str_offsets.size = bfd_get_section_size (sectp);
10132 }
10133 else
10134 {
10135 /* No other kind of section is valid. */
10136 return 0;
10137 }
10138
10139 return 1;
10140 }
10141
10142 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10143 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10144 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10145 This is for DWP version 1 files. */
10146
10147 static struct dwo_unit *
10148 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10149 uint32_t unit_index,
10150 const char *comp_dir,
10151 ULONGEST signature, int is_debug_types)
10152 {
10153 struct objfile *objfile = dwarf2_per_objfile->objfile;
10154 const struct dwp_hash_table *dwp_htab =
10155 is_debug_types ? dwp_file->tus : dwp_file->cus;
10156 bfd *dbfd = dwp_file->dbfd;
10157 const char *kind = is_debug_types ? "TU" : "CU";
10158 struct dwo_file *dwo_file;
10159 struct dwo_unit *dwo_unit;
10160 struct virtual_v1_dwo_sections sections;
10161 void **dwo_file_slot;
10162 char *virtual_dwo_name;
10163 struct cleanup *cleanups;
10164 int i;
10165
10166 gdb_assert (dwp_file->version == 1);
10167
10168 if (dwarf_read_debug)
10169 {
10170 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10171 kind,
10172 pulongest (unit_index), hex_string (signature),
10173 dwp_file->name);
10174 }
10175
10176 /* Fetch the sections of this DWO unit.
10177 Put a limit on the number of sections we look for so that bad data
10178 doesn't cause us to loop forever. */
10179
10180 #define MAX_NR_V1_DWO_SECTIONS \
10181 (1 /* .debug_info or .debug_types */ \
10182 + 1 /* .debug_abbrev */ \
10183 + 1 /* .debug_line */ \
10184 + 1 /* .debug_loc */ \
10185 + 1 /* .debug_str_offsets */ \
10186 + 1 /* .debug_macro or .debug_macinfo */ \
10187 + 1 /* trailing zero */)
10188
10189 memset (&sections, 0, sizeof (sections));
10190 cleanups = make_cleanup (null_cleanup, 0);
10191
10192 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10193 {
10194 asection *sectp;
10195 uint32_t section_nr =
10196 read_4_bytes (dbfd,
10197 dwp_htab->section_pool.v1.indices
10198 + (unit_index + i) * sizeof (uint32_t));
10199
10200 if (section_nr == 0)
10201 break;
10202 if (section_nr >= dwp_file->num_sections)
10203 {
10204 error (_("Dwarf Error: bad DWP hash table, section number too large"
10205 " [in module %s]"),
10206 dwp_file->name);
10207 }
10208
10209 sectp = dwp_file->elf_sections[section_nr];
10210 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10211 {
10212 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10213 " [in module %s]"),
10214 dwp_file->name);
10215 }
10216 }
10217
10218 if (i < 2
10219 || dwarf2_section_empty_p (&sections.info_or_types)
10220 || dwarf2_section_empty_p (&sections.abbrev))
10221 {
10222 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10223 " [in module %s]"),
10224 dwp_file->name);
10225 }
10226 if (i == MAX_NR_V1_DWO_SECTIONS)
10227 {
10228 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10229 " [in module %s]"),
10230 dwp_file->name);
10231 }
10232
10233 /* It's easier for the rest of the code if we fake a struct dwo_file and
10234 have dwo_unit "live" in that. At least for now.
10235
10236 The DWP file can be made up of a random collection of CUs and TUs.
10237 However, for each CU + set of TUs that came from the same original DWO
10238 file, we can combine them back into a virtual DWO file to save space
10239 (fewer struct dwo_file objects to allocate). Remember that for really
10240 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10241
10242 virtual_dwo_name =
10243 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10244 get_section_id (&sections.abbrev),
10245 get_section_id (&sections.line),
10246 get_section_id (&sections.loc),
10247 get_section_id (&sections.str_offsets));
10248 make_cleanup (xfree, virtual_dwo_name);
10249 /* Can we use an existing virtual DWO file? */
10250 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10251 /* Create one if necessary. */
10252 if (*dwo_file_slot == NULL)
10253 {
10254 if (dwarf_read_debug)
10255 {
10256 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10257 virtual_dwo_name);
10258 }
10259 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10260 dwo_file->dwo_name
10261 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10262 virtual_dwo_name,
10263 strlen (virtual_dwo_name));
10264 dwo_file->comp_dir = comp_dir;
10265 dwo_file->sections.abbrev = sections.abbrev;
10266 dwo_file->sections.line = sections.line;
10267 dwo_file->sections.loc = sections.loc;
10268 dwo_file->sections.macinfo = sections.macinfo;
10269 dwo_file->sections.macro = sections.macro;
10270 dwo_file->sections.str_offsets = sections.str_offsets;
10271 /* The "str" section is global to the entire DWP file. */
10272 dwo_file->sections.str = dwp_file->sections.str;
10273 /* The info or types section is assigned below to dwo_unit,
10274 there's no need to record it in dwo_file.
10275 Also, we can't simply record type sections in dwo_file because
10276 we record a pointer into the vector in dwo_unit. As we collect more
10277 types we'll grow the vector and eventually have to reallocate space
10278 for it, invalidating all copies of pointers into the previous
10279 contents. */
10280 *dwo_file_slot = dwo_file;
10281 }
10282 else
10283 {
10284 if (dwarf_read_debug)
10285 {
10286 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10287 virtual_dwo_name);
10288 }
10289 dwo_file = (struct dwo_file *) *dwo_file_slot;
10290 }
10291 do_cleanups (cleanups);
10292
10293 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10294 dwo_unit->dwo_file = dwo_file;
10295 dwo_unit->signature = signature;
10296 dwo_unit->section =
10297 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10298 *dwo_unit->section = sections.info_or_types;
10299 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10300
10301 return dwo_unit;
10302 }
10303
10304 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10305 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10306 piece within that section used by a TU/CU, return a virtual section
10307 of just that piece. */
10308
10309 static struct dwarf2_section_info
10310 create_dwp_v2_section (struct dwarf2_section_info *section,
10311 bfd_size_type offset, bfd_size_type size)
10312 {
10313 struct dwarf2_section_info result;
10314 asection *sectp;
10315
10316 gdb_assert (section != NULL);
10317 gdb_assert (!section->is_virtual);
10318
10319 memset (&result, 0, sizeof (result));
10320 result.s.containing_section = section;
10321 result.is_virtual = 1;
10322
10323 if (size == 0)
10324 return result;
10325
10326 sectp = get_section_bfd_section (section);
10327
10328 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10329 bounds of the real section. This is a pretty-rare event, so just
10330 flag an error (easier) instead of a warning and trying to cope. */
10331 if (sectp == NULL
10332 || offset + size > bfd_get_section_size (sectp))
10333 {
10334 bfd *abfd = sectp->owner;
10335
10336 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10337 " in section %s [in module %s]"),
10338 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10339 objfile_name (dwarf2_per_objfile->objfile));
10340 }
10341
10342 result.virtual_offset = offset;
10343 result.size = size;
10344 return result;
10345 }
10346
10347 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10348 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10349 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10350 This is for DWP version 2 files. */
10351
10352 static struct dwo_unit *
10353 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10354 uint32_t unit_index,
10355 const char *comp_dir,
10356 ULONGEST signature, int is_debug_types)
10357 {
10358 struct objfile *objfile = dwarf2_per_objfile->objfile;
10359 const struct dwp_hash_table *dwp_htab =
10360 is_debug_types ? dwp_file->tus : dwp_file->cus;
10361 bfd *dbfd = dwp_file->dbfd;
10362 const char *kind = is_debug_types ? "TU" : "CU";
10363 struct dwo_file *dwo_file;
10364 struct dwo_unit *dwo_unit;
10365 struct virtual_v2_dwo_sections sections;
10366 void **dwo_file_slot;
10367 char *virtual_dwo_name;
10368 struct cleanup *cleanups;
10369 int i;
10370
10371 gdb_assert (dwp_file->version == 2);
10372
10373 if (dwarf_read_debug)
10374 {
10375 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10376 kind,
10377 pulongest (unit_index), hex_string (signature),
10378 dwp_file->name);
10379 }
10380
10381 /* Fetch the section offsets of this DWO unit. */
10382
10383 memset (&sections, 0, sizeof (sections));
10384 cleanups = make_cleanup (null_cleanup, 0);
10385
10386 for (i = 0; i < dwp_htab->nr_columns; ++i)
10387 {
10388 uint32_t offset = read_4_bytes (dbfd,
10389 dwp_htab->section_pool.v2.offsets
10390 + (((unit_index - 1) * dwp_htab->nr_columns
10391 + i)
10392 * sizeof (uint32_t)));
10393 uint32_t size = read_4_bytes (dbfd,
10394 dwp_htab->section_pool.v2.sizes
10395 + (((unit_index - 1) * dwp_htab->nr_columns
10396 + i)
10397 * sizeof (uint32_t)));
10398
10399 switch (dwp_htab->section_pool.v2.section_ids[i])
10400 {
10401 case DW_SECT_INFO:
10402 case DW_SECT_TYPES:
10403 sections.info_or_types_offset = offset;
10404 sections.info_or_types_size = size;
10405 break;
10406 case DW_SECT_ABBREV:
10407 sections.abbrev_offset = offset;
10408 sections.abbrev_size = size;
10409 break;
10410 case DW_SECT_LINE:
10411 sections.line_offset = offset;
10412 sections.line_size = size;
10413 break;
10414 case DW_SECT_LOC:
10415 sections.loc_offset = offset;
10416 sections.loc_size = size;
10417 break;
10418 case DW_SECT_STR_OFFSETS:
10419 sections.str_offsets_offset = offset;
10420 sections.str_offsets_size = size;
10421 break;
10422 case DW_SECT_MACINFO:
10423 sections.macinfo_offset = offset;
10424 sections.macinfo_size = size;
10425 break;
10426 case DW_SECT_MACRO:
10427 sections.macro_offset = offset;
10428 sections.macro_size = size;
10429 break;
10430 }
10431 }
10432
10433 /* It's easier for the rest of the code if we fake a struct dwo_file and
10434 have dwo_unit "live" in that. At least for now.
10435
10436 The DWP file can be made up of a random collection of CUs and TUs.
10437 However, for each CU + set of TUs that came from the same original DWO
10438 file, we can combine them back into a virtual DWO file to save space
10439 (fewer struct dwo_file objects to allocate). Remember that for really
10440 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10441
10442 virtual_dwo_name =
10443 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10444 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10445 (long) (sections.line_size ? sections.line_offset : 0),
10446 (long) (sections.loc_size ? sections.loc_offset : 0),
10447 (long) (sections.str_offsets_size
10448 ? sections.str_offsets_offset : 0));
10449 make_cleanup (xfree, virtual_dwo_name);
10450 /* Can we use an existing virtual DWO file? */
10451 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10452 /* Create one if necessary. */
10453 if (*dwo_file_slot == NULL)
10454 {
10455 if (dwarf_read_debug)
10456 {
10457 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10458 virtual_dwo_name);
10459 }
10460 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10461 dwo_file->dwo_name
10462 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10463 virtual_dwo_name,
10464 strlen (virtual_dwo_name));
10465 dwo_file->comp_dir = comp_dir;
10466 dwo_file->sections.abbrev =
10467 create_dwp_v2_section (&dwp_file->sections.abbrev,
10468 sections.abbrev_offset, sections.abbrev_size);
10469 dwo_file->sections.line =
10470 create_dwp_v2_section (&dwp_file->sections.line,
10471 sections.line_offset, sections.line_size);
10472 dwo_file->sections.loc =
10473 create_dwp_v2_section (&dwp_file->sections.loc,
10474 sections.loc_offset, sections.loc_size);
10475 dwo_file->sections.macinfo =
10476 create_dwp_v2_section (&dwp_file->sections.macinfo,
10477 sections.macinfo_offset, sections.macinfo_size);
10478 dwo_file->sections.macro =
10479 create_dwp_v2_section (&dwp_file->sections.macro,
10480 sections.macro_offset, sections.macro_size);
10481 dwo_file->sections.str_offsets =
10482 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10483 sections.str_offsets_offset,
10484 sections.str_offsets_size);
10485 /* The "str" section is global to the entire DWP file. */
10486 dwo_file->sections.str = dwp_file->sections.str;
10487 /* The info or types section is assigned below to dwo_unit,
10488 there's no need to record it in dwo_file.
10489 Also, we can't simply record type sections in dwo_file because
10490 we record a pointer into the vector in dwo_unit. As we collect more
10491 types we'll grow the vector and eventually have to reallocate space
10492 for it, invalidating all copies of pointers into the previous
10493 contents. */
10494 *dwo_file_slot = dwo_file;
10495 }
10496 else
10497 {
10498 if (dwarf_read_debug)
10499 {
10500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10501 virtual_dwo_name);
10502 }
10503 dwo_file = (struct dwo_file *) *dwo_file_slot;
10504 }
10505 do_cleanups (cleanups);
10506
10507 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10508 dwo_unit->dwo_file = dwo_file;
10509 dwo_unit->signature = signature;
10510 dwo_unit->section =
10511 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10512 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10513 ? &dwp_file->sections.types
10514 : &dwp_file->sections.info,
10515 sections.info_or_types_offset,
10516 sections.info_or_types_size);
10517 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10518
10519 return dwo_unit;
10520 }
10521
10522 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10523 Returns NULL if the signature isn't found. */
10524
10525 static struct dwo_unit *
10526 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10527 ULONGEST signature, int is_debug_types)
10528 {
10529 const struct dwp_hash_table *dwp_htab =
10530 is_debug_types ? dwp_file->tus : dwp_file->cus;
10531 bfd *dbfd = dwp_file->dbfd;
10532 uint32_t mask = dwp_htab->nr_slots - 1;
10533 uint32_t hash = signature & mask;
10534 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10535 unsigned int i;
10536 void **slot;
10537 struct dwo_unit find_dwo_cu;
10538
10539 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10540 find_dwo_cu.signature = signature;
10541 slot = htab_find_slot (is_debug_types
10542 ? dwp_file->loaded_tus
10543 : dwp_file->loaded_cus,
10544 &find_dwo_cu, INSERT);
10545
10546 if (*slot != NULL)
10547 return (struct dwo_unit *) *slot;
10548
10549 /* Use a for loop so that we don't loop forever on bad debug info. */
10550 for (i = 0; i < dwp_htab->nr_slots; ++i)
10551 {
10552 ULONGEST signature_in_table;
10553
10554 signature_in_table =
10555 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10556 if (signature_in_table == signature)
10557 {
10558 uint32_t unit_index =
10559 read_4_bytes (dbfd,
10560 dwp_htab->unit_table + hash * sizeof (uint32_t));
10561
10562 if (dwp_file->version == 1)
10563 {
10564 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10565 comp_dir, signature,
10566 is_debug_types);
10567 }
10568 else
10569 {
10570 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10571 comp_dir, signature,
10572 is_debug_types);
10573 }
10574 return (struct dwo_unit *) *slot;
10575 }
10576 if (signature_in_table == 0)
10577 return NULL;
10578 hash = (hash + hash2) & mask;
10579 }
10580
10581 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10582 " [in module %s]"),
10583 dwp_file->name);
10584 }
10585
10586 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10587 Open the file specified by FILE_NAME and hand it off to BFD for
10588 preliminary analysis. Return a newly initialized bfd *, which
10589 includes a canonicalized copy of FILE_NAME.
10590 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10591 SEARCH_CWD is true if the current directory is to be searched.
10592 It will be searched before debug-file-directory.
10593 If successful, the file is added to the bfd include table of the
10594 objfile's bfd (see gdb_bfd_record_inclusion).
10595 If unable to find/open the file, return NULL.
10596 NOTE: This function is derived from symfile_bfd_open. */
10597
10598 static gdb_bfd_ref_ptr
10599 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10600 {
10601 int desc, flags;
10602 char *absolute_name;
10603 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10604 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10605 to debug_file_directory. */
10606 char *search_path;
10607 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10608
10609 if (search_cwd)
10610 {
10611 if (*debug_file_directory != '\0')
10612 search_path = concat (".", dirname_separator_string,
10613 debug_file_directory, (char *) NULL);
10614 else
10615 search_path = xstrdup (".");
10616 }
10617 else
10618 search_path = xstrdup (debug_file_directory);
10619
10620 flags = OPF_RETURN_REALPATH;
10621 if (is_dwp)
10622 flags |= OPF_SEARCH_IN_PATH;
10623 desc = openp (search_path, flags, file_name,
10624 O_RDONLY | O_BINARY, &absolute_name);
10625 xfree (search_path);
10626 if (desc < 0)
10627 return NULL;
10628
10629 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10630 xfree (absolute_name);
10631 if (sym_bfd == NULL)
10632 return NULL;
10633 bfd_set_cacheable (sym_bfd.get (), 1);
10634
10635 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10636 return NULL;
10637
10638 /* Success. Record the bfd as having been included by the objfile's bfd.
10639 This is important because things like demangled_names_hash lives in the
10640 objfile's per_bfd space and may have references to things like symbol
10641 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10642 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10643
10644 return sym_bfd;
10645 }
10646
10647 /* Try to open DWO file FILE_NAME.
10648 COMP_DIR is the DW_AT_comp_dir attribute.
10649 The result is the bfd handle of the file.
10650 If there is a problem finding or opening the file, return NULL.
10651 Upon success, the canonicalized path of the file is stored in the bfd,
10652 same as symfile_bfd_open. */
10653
10654 static gdb_bfd_ref_ptr
10655 open_dwo_file (const char *file_name, const char *comp_dir)
10656 {
10657 if (IS_ABSOLUTE_PATH (file_name))
10658 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10659
10660 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10661
10662 if (comp_dir != NULL)
10663 {
10664 char *path_to_try = concat (comp_dir, SLASH_STRING,
10665 file_name, (char *) NULL);
10666
10667 /* NOTE: If comp_dir is a relative path, this will also try the
10668 search path, which seems useful. */
10669 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10670 1 /*search_cwd*/));
10671 xfree (path_to_try);
10672 if (abfd != NULL)
10673 return abfd;
10674 }
10675
10676 /* That didn't work, try debug-file-directory, which, despite its name,
10677 is a list of paths. */
10678
10679 if (*debug_file_directory == '\0')
10680 return NULL;
10681
10682 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10683 }
10684
10685 /* This function is mapped across the sections and remembers the offset and
10686 size of each of the DWO debugging sections we are interested in. */
10687
10688 static void
10689 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10690 {
10691 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10692 const struct dwop_section_names *names = &dwop_section_names;
10693
10694 if (section_is_p (sectp->name, &names->abbrev_dwo))
10695 {
10696 dwo_sections->abbrev.s.section = sectp;
10697 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10698 }
10699 else if (section_is_p (sectp->name, &names->info_dwo))
10700 {
10701 dwo_sections->info.s.section = sectp;
10702 dwo_sections->info.size = bfd_get_section_size (sectp);
10703 }
10704 else if (section_is_p (sectp->name, &names->line_dwo))
10705 {
10706 dwo_sections->line.s.section = sectp;
10707 dwo_sections->line.size = bfd_get_section_size (sectp);
10708 }
10709 else if (section_is_p (sectp->name, &names->loc_dwo))
10710 {
10711 dwo_sections->loc.s.section = sectp;
10712 dwo_sections->loc.size = bfd_get_section_size (sectp);
10713 }
10714 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10715 {
10716 dwo_sections->macinfo.s.section = sectp;
10717 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10718 }
10719 else if (section_is_p (sectp->name, &names->macro_dwo))
10720 {
10721 dwo_sections->macro.s.section = sectp;
10722 dwo_sections->macro.size = bfd_get_section_size (sectp);
10723 }
10724 else if (section_is_p (sectp->name, &names->str_dwo))
10725 {
10726 dwo_sections->str.s.section = sectp;
10727 dwo_sections->str.size = bfd_get_section_size (sectp);
10728 }
10729 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10730 {
10731 dwo_sections->str_offsets.s.section = sectp;
10732 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10733 }
10734 else if (section_is_p (sectp->name, &names->types_dwo))
10735 {
10736 struct dwarf2_section_info type_section;
10737
10738 memset (&type_section, 0, sizeof (type_section));
10739 type_section.s.section = sectp;
10740 type_section.size = bfd_get_section_size (sectp);
10741 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10742 &type_section);
10743 }
10744 }
10745
10746 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10747 by PER_CU. This is for the non-DWP case.
10748 The result is NULL if DWO_NAME can't be found. */
10749
10750 static struct dwo_file *
10751 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10752 const char *dwo_name, const char *comp_dir)
10753 {
10754 struct objfile *objfile = dwarf2_per_objfile->objfile;
10755 struct dwo_file *dwo_file;
10756 struct cleanup *cleanups;
10757
10758 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10759 if (dbfd == NULL)
10760 {
10761 if (dwarf_read_debug)
10762 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10763 return NULL;
10764 }
10765 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10766 dwo_file->dwo_name = dwo_name;
10767 dwo_file->comp_dir = comp_dir;
10768 dwo_file->dbfd = dbfd.release ();
10769
10770 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10771
10772 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10773 &dwo_file->sections);
10774
10775 dwo_file->cu = create_dwo_cu (dwo_file);
10776
10777 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10778 dwo_file->tus);
10779
10780 discard_cleanups (cleanups);
10781
10782 if (dwarf_read_debug)
10783 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10784
10785 return dwo_file;
10786 }
10787
10788 /* This function is mapped across the sections and remembers the offset and
10789 size of each of the DWP debugging sections common to version 1 and 2 that
10790 we are interested in. */
10791
10792 static void
10793 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10794 void *dwp_file_ptr)
10795 {
10796 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10797 const struct dwop_section_names *names = &dwop_section_names;
10798 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10799
10800 /* Record the ELF section number for later lookup: this is what the
10801 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10802 gdb_assert (elf_section_nr < dwp_file->num_sections);
10803 dwp_file->elf_sections[elf_section_nr] = sectp;
10804
10805 /* Look for specific sections that we need. */
10806 if (section_is_p (sectp->name, &names->str_dwo))
10807 {
10808 dwp_file->sections.str.s.section = sectp;
10809 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10810 }
10811 else if (section_is_p (sectp->name, &names->cu_index))
10812 {
10813 dwp_file->sections.cu_index.s.section = sectp;
10814 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10815 }
10816 else if (section_is_p (sectp->name, &names->tu_index))
10817 {
10818 dwp_file->sections.tu_index.s.section = sectp;
10819 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10820 }
10821 }
10822
10823 /* This function is mapped across the sections and remembers the offset and
10824 size of each of the DWP version 2 debugging sections that we are interested
10825 in. This is split into a separate function because we don't know if we
10826 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10827
10828 static void
10829 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10830 {
10831 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10832 const struct dwop_section_names *names = &dwop_section_names;
10833 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10834
10835 /* Record the ELF section number for later lookup: this is what the
10836 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10837 gdb_assert (elf_section_nr < dwp_file->num_sections);
10838 dwp_file->elf_sections[elf_section_nr] = sectp;
10839
10840 /* Look for specific sections that we need. */
10841 if (section_is_p (sectp->name, &names->abbrev_dwo))
10842 {
10843 dwp_file->sections.abbrev.s.section = sectp;
10844 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10845 }
10846 else if (section_is_p (sectp->name, &names->info_dwo))
10847 {
10848 dwp_file->sections.info.s.section = sectp;
10849 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10850 }
10851 else if (section_is_p (sectp->name, &names->line_dwo))
10852 {
10853 dwp_file->sections.line.s.section = sectp;
10854 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10855 }
10856 else if (section_is_p (sectp->name, &names->loc_dwo))
10857 {
10858 dwp_file->sections.loc.s.section = sectp;
10859 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10862 {
10863 dwp_file->sections.macinfo.s.section = sectp;
10864 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10865 }
10866 else if (section_is_p (sectp->name, &names->macro_dwo))
10867 {
10868 dwp_file->sections.macro.s.section = sectp;
10869 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10870 }
10871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10872 {
10873 dwp_file->sections.str_offsets.s.section = sectp;
10874 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10875 }
10876 else if (section_is_p (sectp->name, &names->types_dwo))
10877 {
10878 dwp_file->sections.types.s.section = sectp;
10879 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10880 }
10881 }
10882
10883 /* Hash function for dwp_file loaded CUs/TUs. */
10884
10885 static hashval_t
10886 hash_dwp_loaded_cutus (const void *item)
10887 {
10888 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10889
10890 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10891 return dwo_unit->signature;
10892 }
10893
10894 /* Equality function for dwp_file loaded CUs/TUs. */
10895
10896 static int
10897 eq_dwp_loaded_cutus (const void *a, const void *b)
10898 {
10899 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10900 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10901
10902 return dua->signature == dub->signature;
10903 }
10904
10905 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10906
10907 static htab_t
10908 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10909 {
10910 return htab_create_alloc_ex (3,
10911 hash_dwp_loaded_cutus,
10912 eq_dwp_loaded_cutus,
10913 NULL,
10914 &objfile->objfile_obstack,
10915 hashtab_obstack_allocate,
10916 dummy_obstack_deallocate);
10917 }
10918
10919 /* Try to open DWP file FILE_NAME.
10920 The result is the bfd handle of the file.
10921 If there is a problem finding or opening the file, return NULL.
10922 Upon success, the canonicalized path of the file is stored in the bfd,
10923 same as symfile_bfd_open. */
10924
10925 static gdb_bfd_ref_ptr
10926 open_dwp_file (const char *file_name)
10927 {
10928 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10929 1 /*search_cwd*/));
10930 if (abfd != NULL)
10931 return abfd;
10932
10933 /* Work around upstream bug 15652.
10934 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10935 [Whether that's a "bug" is debatable, but it is getting in our way.]
10936 We have no real idea where the dwp file is, because gdb's realpath-ing
10937 of the executable's path may have discarded the needed info.
10938 [IWBN if the dwp file name was recorded in the executable, akin to
10939 .gnu_debuglink, but that doesn't exist yet.]
10940 Strip the directory from FILE_NAME and search again. */
10941 if (*debug_file_directory != '\0')
10942 {
10943 /* Don't implicitly search the current directory here.
10944 If the user wants to search "." to handle this case,
10945 it must be added to debug-file-directory. */
10946 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10947 0 /*search_cwd*/);
10948 }
10949
10950 return NULL;
10951 }
10952
10953 /* Initialize the use of the DWP file for the current objfile.
10954 By convention the name of the DWP file is ${objfile}.dwp.
10955 The result is NULL if it can't be found. */
10956
10957 static struct dwp_file *
10958 open_and_init_dwp_file (void)
10959 {
10960 struct objfile *objfile = dwarf2_per_objfile->objfile;
10961 struct dwp_file *dwp_file;
10962
10963 /* Try to find first .dwp for the binary file before any symbolic links
10964 resolving. */
10965
10966 /* If the objfile is a debug file, find the name of the real binary
10967 file and get the name of dwp file from there. */
10968 std::string dwp_name;
10969 if (objfile->separate_debug_objfile_backlink != NULL)
10970 {
10971 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10972 const char *backlink_basename = lbasename (backlink->original_name);
10973
10974 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
10975 }
10976 else
10977 dwp_name = objfile->original_name;
10978
10979 dwp_name += ".dwp";
10980
10981 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
10982 if (dbfd == NULL
10983 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10984 {
10985 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10986 dwp_name = objfile_name (objfile);
10987 dwp_name += ".dwp";
10988 dbfd = open_dwp_file (dwp_name.c_str ());
10989 }
10990
10991 if (dbfd == NULL)
10992 {
10993 if (dwarf_read_debug)
10994 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
10995 return NULL;
10996 }
10997 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10998 dwp_file->name = bfd_get_filename (dbfd.get ());
10999 dwp_file->dbfd = dbfd.release ();
11000
11001 /* +1: section 0 is unused */
11002 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11003 dwp_file->elf_sections =
11004 OBSTACK_CALLOC (&objfile->objfile_obstack,
11005 dwp_file->num_sections, asection *);
11006
11007 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11008 dwp_file);
11009
11010 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11011
11012 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11013
11014 /* The DWP file version is stored in the hash table. Oh well. */
11015 if (dwp_file->cus->version != dwp_file->tus->version)
11016 {
11017 /* Technically speaking, we should try to limp along, but this is
11018 pretty bizarre. We use pulongest here because that's the established
11019 portability solution (e.g, we cannot use %u for uint32_t). */
11020 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11021 " TU version %s [in DWP file %s]"),
11022 pulongest (dwp_file->cus->version),
11023 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11024 }
11025 dwp_file->version = dwp_file->cus->version;
11026
11027 if (dwp_file->version == 2)
11028 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11029 dwp_file);
11030
11031 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11032 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11033
11034 if (dwarf_read_debug)
11035 {
11036 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11037 fprintf_unfiltered (gdb_stdlog,
11038 " %s CUs, %s TUs\n",
11039 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11040 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11041 }
11042
11043 return dwp_file;
11044 }
11045
11046 /* Wrapper around open_and_init_dwp_file, only open it once. */
11047
11048 static struct dwp_file *
11049 get_dwp_file (void)
11050 {
11051 if (! dwarf2_per_objfile->dwp_checked)
11052 {
11053 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11054 dwarf2_per_objfile->dwp_checked = 1;
11055 }
11056 return dwarf2_per_objfile->dwp_file;
11057 }
11058
11059 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11060 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11061 or in the DWP file for the objfile, referenced by THIS_UNIT.
11062 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11063 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11064
11065 This is called, for example, when wanting to read a variable with a
11066 complex location. Therefore we don't want to do file i/o for every call.
11067 Therefore we don't want to look for a DWO file on every call.
11068 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11069 then we check if we've already seen DWO_NAME, and only THEN do we check
11070 for a DWO file.
11071
11072 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11073 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11074
11075 static struct dwo_unit *
11076 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11077 const char *dwo_name, const char *comp_dir,
11078 ULONGEST signature, int is_debug_types)
11079 {
11080 struct objfile *objfile = dwarf2_per_objfile->objfile;
11081 const char *kind = is_debug_types ? "TU" : "CU";
11082 void **dwo_file_slot;
11083 struct dwo_file *dwo_file;
11084 struct dwp_file *dwp_file;
11085
11086 /* First see if there's a DWP file.
11087 If we have a DWP file but didn't find the DWO inside it, don't
11088 look for the original DWO file. It makes gdb behave differently
11089 depending on whether one is debugging in the build tree. */
11090
11091 dwp_file = get_dwp_file ();
11092 if (dwp_file != NULL)
11093 {
11094 const struct dwp_hash_table *dwp_htab =
11095 is_debug_types ? dwp_file->tus : dwp_file->cus;
11096
11097 if (dwp_htab != NULL)
11098 {
11099 struct dwo_unit *dwo_cutu =
11100 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11101 signature, is_debug_types);
11102
11103 if (dwo_cutu != NULL)
11104 {
11105 if (dwarf_read_debug)
11106 {
11107 fprintf_unfiltered (gdb_stdlog,
11108 "Virtual DWO %s %s found: @%s\n",
11109 kind, hex_string (signature),
11110 host_address_to_string (dwo_cutu));
11111 }
11112 return dwo_cutu;
11113 }
11114 }
11115 }
11116 else
11117 {
11118 /* No DWP file, look for the DWO file. */
11119
11120 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11121 if (*dwo_file_slot == NULL)
11122 {
11123 /* Read in the file and build a table of the CUs/TUs it contains. */
11124 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11125 }
11126 /* NOTE: This will be NULL if unable to open the file. */
11127 dwo_file = (struct dwo_file *) *dwo_file_slot;
11128
11129 if (dwo_file != NULL)
11130 {
11131 struct dwo_unit *dwo_cutu = NULL;
11132
11133 if (is_debug_types && dwo_file->tus)
11134 {
11135 struct dwo_unit find_dwo_cutu;
11136
11137 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11138 find_dwo_cutu.signature = signature;
11139 dwo_cutu
11140 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11141 }
11142 else if (!is_debug_types && dwo_file->cu)
11143 {
11144 if (signature == dwo_file->cu->signature)
11145 dwo_cutu = dwo_file->cu;
11146 }
11147
11148 if (dwo_cutu != NULL)
11149 {
11150 if (dwarf_read_debug)
11151 {
11152 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11153 kind, dwo_name, hex_string (signature),
11154 host_address_to_string (dwo_cutu));
11155 }
11156 return dwo_cutu;
11157 }
11158 }
11159 }
11160
11161 /* We didn't find it. This could mean a dwo_id mismatch, or
11162 someone deleted the DWO/DWP file, or the search path isn't set up
11163 correctly to find the file. */
11164
11165 if (dwarf_read_debug)
11166 {
11167 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11168 kind, dwo_name, hex_string (signature));
11169 }
11170
11171 /* This is a warning and not a complaint because it can be caused by
11172 pilot error (e.g., user accidentally deleting the DWO). */
11173 {
11174 /* Print the name of the DWP file if we looked there, helps the user
11175 better diagnose the problem. */
11176 char *dwp_text = NULL;
11177 struct cleanup *cleanups;
11178
11179 if (dwp_file != NULL)
11180 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11181 cleanups = make_cleanup (xfree, dwp_text);
11182
11183 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11184 " [in module %s]"),
11185 kind, dwo_name, hex_string (signature),
11186 dwp_text != NULL ? dwp_text : "",
11187 this_unit->is_debug_types ? "TU" : "CU",
11188 to_underlying (this_unit->sect_off), objfile_name (objfile));
11189
11190 do_cleanups (cleanups);
11191 }
11192 return NULL;
11193 }
11194
11195 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11196 See lookup_dwo_cutu_unit for details. */
11197
11198 static struct dwo_unit *
11199 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11200 const char *dwo_name, const char *comp_dir,
11201 ULONGEST signature)
11202 {
11203 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11204 }
11205
11206 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11207 See lookup_dwo_cutu_unit for details. */
11208
11209 static struct dwo_unit *
11210 lookup_dwo_type_unit (struct signatured_type *this_tu,
11211 const char *dwo_name, const char *comp_dir)
11212 {
11213 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11214 }
11215
11216 /* Traversal function for queue_and_load_all_dwo_tus. */
11217
11218 static int
11219 queue_and_load_dwo_tu (void **slot, void *info)
11220 {
11221 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11222 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11223 ULONGEST signature = dwo_unit->signature;
11224 struct signatured_type *sig_type =
11225 lookup_dwo_signatured_type (per_cu->cu, signature);
11226
11227 if (sig_type != NULL)
11228 {
11229 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11230
11231 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11232 a real dependency of PER_CU on SIG_TYPE. That is detected later
11233 while processing PER_CU. */
11234 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11235 load_full_type_unit (sig_cu);
11236 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11237 }
11238
11239 return 1;
11240 }
11241
11242 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11243 The DWO may have the only definition of the type, though it may not be
11244 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11245 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11246
11247 static void
11248 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11249 {
11250 struct dwo_unit *dwo_unit;
11251 struct dwo_file *dwo_file;
11252
11253 gdb_assert (!per_cu->is_debug_types);
11254 gdb_assert (get_dwp_file () == NULL);
11255 gdb_assert (per_cu->cu != NULL);
11256
11257 dwo_unit = per_cu->cu->dwo_unit;
11258 gdb_assert (dwo_unit != NULL);
11259
11260 dwo_file = dwo_unit->dwo_file;
11261 if (dwo_file->tus != NULL)
11262 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11263 }
11264
11265 /* Free all resources associated with DWO_FILE.
11266 Close the DWO file and munmap the sections.
11267 All memory should be on the objfile obstack. */
11268
11269 static void
11270 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11271 {
11272
11273 /* Note: dbfd is NULL for virtual DWO files. */
11274 gdb_bfd_unref (dwo_file->dbfd);
11275
11276 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11277 }
11278
11279 /* Wrapper for free_dwo_file for use in cleanups. */
11280
11281 static void
11282 free_dwo_file_cleanup (void *arg)
11283 {
11284 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11285 struct objfile *objfile = dwarf2_per_objfile->objfile;
11286
11287 free_dwo_file (dwo_file, objfile);
11288 }
11289
11290 /* Traversal function for free_dwo_files. */
11291
11292 static int
11293 free_dwo_file_from_slot (void **slot, void *info)
11294 {
11295 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11296 struct objfile *objfile = (struct objfile *) info;
11297
11298 free_dwo_file (dwo_file, objfile);
11299
11300 return 1;
11301 }
11302
11303 /* Free all resources associated with DWO_FILES. */
11304
11305 static void
11306 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11307 {
11308 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11309 }
11310 \f
11311 /* Read in various DIEs. */
11312
11313 /* qsort helper for inherit_abstract_dies. */
11314
11315 static int
11316 unsigned_int_compar (const void *ap, const void *bp)
11317 {
11318 unsigned int a = *(unsigned int *) ap;
11319 unsigned int b = *(unsigned int *) bp;
11320
11321 return (a > b) - (b > a);
11322 }
11323
11324 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11325 Inherit only the children of the DW_AT_abstract_origin DIE not being
11326 already referenced by DW_AT_abstract_origin from the children of the
11327 current DIE. */
11328
11329 static void
11330 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11331 {
11332 struct die_info *child_die;
11333 unsigned die_children_count;
11334 /* CU offsets which were referenced by children of the current DIE. */
11335 sect_offset *offsets;
11336 sect_offset *offsets_end, *offsetp;
11337 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11338 struct die_info *origin_die;
11339 /* Iterator of the ORIGIN_DIE children. */
11340 struct die_info *origin_child_die;
11341 struct cleanup *cleanups;
11342 struct attribute *attr;
11343 struct dwarf2_cu *origin_cu;
11344 struct pending **origin_previous_list_in_scope;
11345
11346 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11347 if (!attr)
11348 return;
11349
11350 /* Note that following die references may follow to a die in a
11351 different cu. */
11352
11353 origin_cu = cu;
11354 origin_die = follow_die_ref (die, attr, &origin_cu);
11355
11356 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11357 symbols in. */
11358 origin_previous_list_in_scope = origin_cu->list_in_scope;
11359 origin_cu->list_in_scope = cu->list_in_scope;
11360
11361 if (die->tag != origin_die->tag
11362 && !(die->tag == DW_TAG_inlined_subroutine
11363 && origin_die->tag == DW_TAG_subprogram))
11364 complaint (&symfile_complaints,
11365 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11366 to_underlying (die->sect_off),
11367 to_underlying (origin_die->sect_off));
11368
11369 child_die = die->child;
11370 die_children_count = 0;
11371 while (child_die && child_die->tag)
11372 {
11373 child_die = sibling_die (child_die);
11374 die_children_count++;
11375 }
11376 offsets = XNEWVEC (sect_offset, die_children_count);
11377 cleanups = make_cleanup (xfree, offsets);
11378
11379 offsets_end = offsets;
11380 for (child_die = die->child;
11381 child_die && child_die->tag;
11382 child_die = sibling_die (child_die))
11383 {
11384 struct die_info *child_origin_die;
11385 struct dwarf2_cu *child_origin_cu;
11386
11387 /* We are trying to process concrete instance entries:
11388 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11389 it's not relevant to our analysis here. i.e. detecting DIEs that are
11390 present in the abstract instance but not referenced in the concrete
11391 one. */
11392 if (child_die->tag == DW_TAG_call_site
11393 || child_die->tag == DW_TAG_GNU_call_site)
11394 continue;
11395
11396 /* For each CHILD_DIE, find the corresponding child of
11397 ORIGIN_DIE. If there is more than one layer of
11398 DW_AT_abstract_origin, follow them all; there shouldn't be,
11399 but GCC versions at least through 4.4 generate this (GCC PR
11400 40573). */
11401 child_origin_die = child_die;
11402 child_origin_cu = cu;
11403 while (1)
11404 {
11405 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11406 child_origin_cu);
11407 if (attr == NULL)
11408 break;
11409 child_origin_die = follow_die_ref (child_origin_die, attr,
11410 &child_origin_cu);
11411 }
11412
11413 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11414 counterpart may exist. */
11415 if (child_origin_die != child_die)
11416 {
11417 if (child_die->tag != child_origin_die->tag
11418 && !(child_die->tag == DW_TAG_inlined_subroutine
11419 && child_origin_die->tag == DW_TAG_subprogram))
11420 complaint (&symfile_complaints,
11421 _("Child DIE 0x%x and its abstract origin 0x%x have "
11422 "different tags"),
11423 to_underlying (child_die->sect_off),
11424 to_underlying (child_origin_die->sect_off));
11425 if (child_origin_die->parent != origin_die)
11426 complaint (&symfile_complaints,
11427 _("Child DIE 0x%x and its abstract origin 0x%x have "
11428 "different parents"),
11429 to_underlying (child_die->sect_off),
11430 to_underlying (child_origin_die->sect_off));
11431 else
11432 *offsets_end++ = child_origin_die->sect_off;
11433 }
11434 }
11435 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11436 unsigned_int_compar);
11437 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11438 if (offsetp[-1] == *offsetp)
11439 complaint (&symfile_complaints,
11440 _("Multiple children of DIE 0x%x refer "
11441 "to DIE 0x%x as their abstract origin"),
11442 to_underlying (die->sect_off), to_underlying (*offsetp));
11443
11444 offsetp = offsets;
11445 origin_child_die = origin_die->child;
11446 while (origin_child_die && origin_child_die->tag)
11447 {
11448 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11449 while (offsetp < offsets_end
11450 && *offsetp < origin_child_die->sect_off)
11451 offsetp++;
11452 if (offsetp >= offsets_end
11453 || *offsetp > origin_child_die->sect_off)
11454 {
11455 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11456 Check whether we're already processing ORIGIN_CHILD_DIE.
11457 This can happen with mutually referenced abstract_origins.
11458 PR 16581. */
11459 if (!origin_child_die->in_process)
11460 process_die (origin_child_die, origin_cu);
11461 }
11462 origin_child_die = sibling_die (origin_child_die);
11463 }
11464 origin_cu->list_in_scope = origin_previous_list_in_scope;
11465
11466 do_cleanups (cleanups);
11467 }
11468
11469 static void
11470 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11471 {
11472 struct objfile *objfile = cu->objfile;
11473 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11474 struct context_stack *newobj;
11475 CORE_ADDR lowpc;
11476 CORE_ADDR highpc;
11477 struct die_info *child_die;
11478 struct attribute *attr, *call_line, *call_file;
11479 const char *name;
11480 CORE_ADDR baseaddr;
11481 struct block *block;
11482 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11483 VEC (symbolp) *template_args = NULL;
11484 struct template_symbol *templ_func = NULL;
11485
11486 if (inlined_func)
11487 {
11488 /* If we do not have call site information, we can't show the
11489 caller of this inlined function. That's too confusing, so
11490 only use the scope for local variables. */
11491 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11492 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11493 if (call_line == NULL || call_file == NULL)
11494 {
11495 read_lexical_block_scope (die, cu);
11496 return;
11497 }
11498 }
11499
11500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11501
11502 name = dwarf2_name (die, cu);
11503
11504 /* Ignore functions with missing or empty names. These are actually
11505 illegal according to the DWARF standard. */
11506 if (name == NULL)
11507 {
11508 complaint (&symfile_complaints,
11509 _("missing name for subprogram DIE at %d"),
11510 to_underlying (die->sect_off));
11511 return;
11512 }
11513
11514 /* Ignore functions with missing or invalid low and high pc attributes. */
11515 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11516 <= PC_BOUNDS_INVALID)
11517 {
11518 attr = dwarf2_attr (die, DW_AT_external, cu);
11519 if (!attr || !DW_UNSND (attr))
11520 complaint (&symfile_complaints,
11521 _("cannot get low and high bounds "
11522 "for subprogram DIE at %d"),
11523 to_underlying (die->sect_off));
11524 return;
11525 }
11526
11527 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11528 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11529
11530 /* If we have any template arguments, then we must allocate a
11531 different sort of symbol. */
11532 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11533 {
11534 if (child_die->tag == DW_TAG_template_type_param
11535 || child_die->tag == DW_TAG_template_value_param)
11536 {
11537 templ_func = allocate_template_symbol (objfile);
11538 templ_func->base.is_cplus_template_function = 1;
11539 break;
11540 }
11541 }
11542
11543 newobj = push_context (0, lowpc);
11544 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11545 (struct symbol *) templ_func);
11546
11547 /* If there is a location expression for DW_AT_frame_base, record
11548 it. */
11549 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11550 if (attr)
11551 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11552
11553 /* If there is a location for the static link, record it. */
11554 newobj->static_link = NULL;
11555 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11556 if (attr)
11557 {
11558 newobj->static_link
11559 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11560 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11561 }
11562
11563 cu->list_in_scope = &local_symbols;
11564
11565 if (die->child != NULL)
11566 {
11567 child_die = die->child;
11568 while (child_die && child_die->tag)
11569 {
11570 if (child_die->tag == DW_TAG_template_type_param
11571 || child_die->tag == DW_TAG_template_value_param)
11572 {
11573 struct symbol *arg = new_symbol (child_die, NULL, cu);
11574
11575 if (arg != NULL)
11576 VEC_safe_push (symbolp, template_args, arg);
11577 }
11578 else
11579 process_die (child_die, cu);
11580 child_die = sibling_die (child_die);
11581 }
11582 }
11583
11584 inherit_abstract_dies (die, cu);
11585
11586 /* If we have a DW_AT_specification, we might need to import using
11587 directives from the context of the specification DIE. See the
11588 comment in determine_prefix. */
11589 if (cu->language == language_cplus
11590 && dwarf2_attr (die, DW_AT_specification, cu))
11591 {
11592 struct dwarf2_cu *spec_cu = cu;
11593 struct die_info *spec_die = die_specification (die, &spec_cu);
11594
11595 while (spec_die)
11596 {
11597 child_die = spec_die->child;
11598 while (child_die && child_die->tag)
11599 {
11600 if (child_die->tag == DW_TAG_imported_module)
11601 process_die (child_die, spec_cu);
11602 child_die = sibling_die (child_die);
11603 }
11604
11605 /* In some cases, GCC generates specification DIEs that
11606 themselves contain DW_AT_specification attributes. */
11607 spec_die = die_specification (spec_die, &spec_cu);
11608 }
11609 }
11610
11611 newobj = pop_context ();
11612 /* Make a block for the local symbols within. */
11613 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11614 newobj->static_link, lowpc, highpc);
11615
11616 /* For C++, set the block's scope. */
11617 if ((cu->language == language_cplus
11618 || cu->language == language_fortran
11619 || cu->language == language_d
11620 || cu->language == language_rust)
11621 && cu->processing_has_namespace_info)
11622 block_set_scope (block, determine_prefix (die, cu),
11623 &objfile->objfile_obstack);
11624
11625 /* If we have address ranges, record them. */
11626 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11627
11628 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11629
11630 /* Attach template arguments to function. */
11631 if (! VEC_empty (symbolp, template_args))
11632 {
11633 gdb_assert (templ_func != NULL);
11634
11635 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11636 templ_func->template_arguments
11637 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11638 templ_func->n_template_arguments);
11639 memcpy (templ_func->template_arguments,
11640 VEC_address (symbolp, template_args),
11641 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11642 VEC_free (symbolp, template_args);
11643 }
11644
11645 /* In C++, we can have functions nested inside functions (e.g., when
11646 a function declares a class that has methods). This means that
11647 when we finish processing a function scope, we may need to go
11648 back to building a containing block's symbol lists. */
11649 local_symbols = newobj->locals;
11650 local_using_directives = newobj->local_using_directives;
11651
11652 /* If we've finished processing a top-level function, subsequent
11653 symbols go in the file symbol list. */
11654 if (outermost_context_p ())
11655 cu->list_in_scope = &file_symbols;
11656 }
11657
11658 /* Process all the DIES contained within a lexical block scope. Start
11659 a new scope, process the dies, and then close the scope. */
11660
11661 static void
11662 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11663 {
11664 struct objfile *objfile = cu->objfile;
11665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11666 struct context_stack *newobj;
11667 CORE_ADDR lowpc, highpc;
11668 struct die_info *child_die;
11669 CORE_ADDR baseaddr;
11670
11671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11672
11673 /* Ignore blocks with missing or invalid low and high pc attributes. */
11674 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11675 as multiple lexical blocks? Handling children in a sane way would
11676 be nasty. Might be easier to properly extend generic blocks to
11677 describe ranges. */
11678 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11679 {
11680 case PC_BOUNDS_NOT_PRESENT:
11681 /* DW_TAG_lexical_block has no attributes, process its children as if
11682 there was no wrapping by that DW_TAG_lexical_block.
11683 GCC does no longer produces such DWARF since GCC r224161. */
11684 for (child_die = die->child;
11685 child_die != NULL && child_die->tag;
11686 child_die = sibling_die (child_die))
11687 process_die (child_die, cu);
11688 return;
11689 case PC_BOUNDS_INVALID:
11690 return;
11691 }
11692 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11693 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11694
11695 push_context (0, lowpc);
11696 if (die->child != NULL)
11697 {
11698 child_die = die->child;
11699 while (child_die && child_die->tag)
11700 {
11701 process_die (child_die, cu);
11702 child_die = sibling_die (child_die);
11703 }
11704 }
11705 inherit_abstract_dies (die, cu);
11706 newobj = pop_context ();
11707
11708 if (local_symbols != NULL || local_using_directives != NULL)
11709 {
11710 struct block *block
11711 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11712 newobj->start_addr, highpc);
11713
11714 /* Note that recording ranges after traversing children, as we
11715 do here, means that recording a parent's ranges entails
11716 walking across all its children's ranges as they appear in
11717 the address map, which is quadratic behavior.
11718
11719 It would be nicer to record the parent's ranges before
11720 traversing its children, simply overriding whatever you find
11721 there. But since we don't even decide whether to create a
11722 block until after we've traversed its children, that's hard
11723 to do. */
11724 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11725 }
11726 local_symbols = newobj->locals;
11727 local_using_directives = newobj->local_using_directives;
11728 }
11729
11730 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11731
11732 static void
11733 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11734 {
11735 struct objfile *objfile = cu->objfile;
11736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11737 CORE_ADDR pc, baseaddr;
11738 struct attribute *attr;
11739 struct call_site *call_site, call_site_local;
11740 void **slot;
11741 int nparams;
11742 struct die_info *child_die;
11743
11744 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11745
11746 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11747 if (attr == NULL)
11748 {
11749 /* This was a pre-DWARF-5 GNU extension alias
11750 for DW_AT_call_return_pc. */
11751 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11752 }
11753 if (!attr)
11754 {
11755 complaint (&symfile_complaints,
11756 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11757 "DIE 0x%x [in module %s]"),
11758 to_underlying (die->sect_off), objfile_name (objfile));
11759 return;
11760 }
11761 pc = attr_value_as_address (attr) + baseaddr;
11762 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11763
11764 if (cu->call_site_htab == NULL)
11765 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11766 NULL, &objfile->objfile_obstack,
11767 hashtab_obstack_allocate, NULL);
11768 call_site_local.pc = pc;
11769 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11770 if (*slot != NULL)
11771 {
11772 complaint (&symfile_complaints,
11773 _("Duplicate PC %s for DW_TAG_call_site "
11774 "DIE 0x%x [in module %s]"),
11775 paddress (gdbarch, pc), to_underlying (die->sect_off),
11776 objfile_name (objfile));
11777 return;
11778 }
11779
11780 /* Count parameters at the caller. */
11781
11782 nparams = 0;
11783 for (child_die = die->child; child_die && child_die->tag;
11784 child_die = sibling_die (child_die))
11785 {
11786 if (child_die->tag != DW_TAG_call_site_parameter
11787 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11788 {
11789 complaint (&symfile_complaints,
11790 _("Tag %d is not DW_TAG_call_site_parameter in "
11791 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11792 child_die->tag, to_underlying (child_die->sect_off),
11793 objfile_name (objfile));
11794 continue;
11795 }
11796
11797 nparams++;
11798 }
11799
11800 call_site
11801 = ((struct call_site *)
11802 obstack_alloc (&objfile->objfile_obstack,
11803 sizeof (*call_site)
11804 + (sizeof (*call_site->parameter) * (nparams - 1))));
11805 *slot = call_site;
11806 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11807 call_site->pc = pc;
11808
11809 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11810 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11811 {
11812 struct die_info *func_die;
11813
11814 /* Skip also over DW_TAG_inlined_subroutine. */
11815 for (func_die = die->parent;
11816 func_die && func_die->tag != DW_TAG_subprogram
11817 && func_die->tag != DW_TAG_subroutine_type;
11818 func_die = func_die->parent);
11819
11820 /* DW_AT_call_all_calls is a superset
11821 of DW_AT_call_all_tail_calls. */
11822 if (func_die
11823 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11824 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11825 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11826 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11827 {
11828 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11829 not complete. But keep CALL_SITE for look ups via call_site_htab,
11830 both the initial caller containing the real return address PC and
11831 the final callee containing the current PC of a chain of tail
11832 calls do not need to have the tail call list complete. But any
11833 function candidate for a virtual tail call frame searched via
11834 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11835 determined unambiguously. */
11836 }
11837 else
11838 {
11839 struct type *func_type = NULL;
11840
11841 if (func_die)
11842 func_type = get_die_type (func_die, cu);
11843 if (func_type != NULL)
11844 {
11845 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11846
11847 /* Enlist this call site to the function. */
11848 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11849 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11850 }
11851 else
11852 complaint (&symfile_complaints,
11853 _("Cannot find function owning DW_TAG_call_site "
11854 "DIE 0x%x [in module %s]"),
11855 to_underlying (die->sect_off), objfile_name (objfile));
11856 }
11857 }
11858
11859 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11860 if (attr == NULL)
11861 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11862 if (attr == NULL)
11863 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11864 if (attr == NULL)
11865 {
11866 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11867 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11868 }
11869 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11870 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11871 /* Keep NULL DWARF_BLOCK. */;
11872 else if (attr_form_is_block (attr))
11873 {
11874 struct dwarf2_locexpr_baton *dlbaton;
11875
11876 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11877 dlbaton->data = DW_BLOCK (attr)->data;
11878 dlbaton->size = DW_BLOCK (attr)->size;
11879 dlbaton->per_cu = cu->per_cu;
11880
11881 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11882 }
11883 else if (attr_form_is_ref (attr))
11884 {
11885 struct dwarf2_cu *target_cu = cu;
11886 struct die_info *target_die;
11887
11888 target_die = follow_die_ref (die, attr, &target_cu);
11889 gdb_assert (target_cu->objfile == objfile);
11890 if (die_is_declaration (target_die, target_cu))
11891 {
11892 const char *target_physname;
11893
11894 /* Prefer the mangled name; otherwise compute the demangled one. */
11895 target_physname = dwarf2_string_attr (target_die,
11896 DW_AT_linkage_name,
11897 target_cu);
11898 if (target_physname == NULL)
11899 target_physname = dwarf2_string_attr (target_die,
11900 DW_AT_MIPS_linkage_name,
11901 target_cu);
11902 if (target_physname == NULL)
11903 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11904 if (target_physname == NULL)
11905 complaint (&symfile_complaints,
11906 _("DW_AT_call_target target DIE has invalid "
11907 "physname, for referencing DIE 0x%x [in module %s]"),
11908 to_underlying (die->sect_off), objfile_name (objfile));
11909 else
11910 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11911 }
11912 else
11913 {
11914 CORE_ADDR lowpc;
11915
11916 /* DW_AT_entry_pc should be preferred. */
11917 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11918 <= PC_BOUNDS_INVALID)
11919 complaint (&symfile_complaints,
11920 _("DW_AT_call_target target DIE has invalid "
11921 "low pc, for referencing DIE 0x%x [in module %s]"),
11922 to_underlying (die->sect_off), objfile_name (objfile));
11923 else
11924 {
11925 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11926 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11927 }
11928 }
11929 }
11930 else
11931 complaint (&symfile_complaints,
11932 _("DW_TAG_call_site DW_AT_call_target is neither "
11933 "block nor reference, for DIE 0x%x [in module %s]"),
11934 to_underlying (die->sect_off), objfile_name (objfile));
11935
11936 call_site->per_cu = cu->per_cu;
11937
11938 for (child_die = die->child;
11939 child_die && child_die->tag;
11940 child_die = sibling_die (child_die))
11941 {
11942 struct call_site_parameter *parameter;
11943 struct attribute *loc, *origin;
11944
11945 if (child_die->tag != DW_TAG_call_site_parameter
11946 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11947 {
11948 /* Already printed the complaint above. */
11949 continue;
11950 }
11951
11952 gdb_assert (call_site->parameter_count < nparams);
11953 parameter = &call_site->parameter[call_site->parameter_count];
11954
11955 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11956 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11957 register is contained in DW_AT_call_value. */
11958
11959 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11960 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11961 if (origin == NULL)
11962 {
11963 /* This was a pre-DWARF-5 GNU extension alias
11964 for DW_AT_call_parameter. */
11965 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11966 }
11967 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11968 {
11969 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11970
11971 sect_offset sect_off
11972 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11973 if (!offset_in_cu_p (&cu->header, sect_off))
11974 {
11975 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11976 binding can be done only inside one CU. Such referenced DIE
11977 therefore cannot be even moved to DW_TAG_partial_unit. */
11978 complaint (&symfile_complaints,
11979 _("DW_AT_call_parameter offset is not in CU for "
11980 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11981 to_underlying (child_die->sect_off),
11982 objfile_name (objfile));
11983 continue;
11984 }
11985 parameter->u.param_cu_off
11986 = (cu_offset) (sect_off - cu->header.sect_off);
11987 }
11988 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11989 {
11990 complaint (&symfile_complaints,
11991 _("No DW_FORM_block* DW_AT_location for "
11992 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11993 to_underlying (child_die->sect_off), objfile_name (objfile));
11994 continue;
11995 }
11996 else
11997 {
11998 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11999 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12000 if (parameter->u.dwarf_reg != -1)
12001 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12002 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12003 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12004 &parameter->u.fb_offset))
12005 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12006 else
12007 {
12008 complaint (&symfile_complaints,
12009 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12010 "for DW_FORM_block* DW_AT_location is supported for "
12011 "DW_TAG_call_site child DIE 0x%x "
12012 "[in module %s]"),
12013 to_underlying (child_die->sect_off),
12014 objfile_name (objfile));
12015 continue;
12016 }
12017 }
12018
12019 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12020 if (attr == NULL)
12021 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12022 if (!attr_form_is_block (attr))
12023 {
12024 complaint (&symfile_complaints,
12025 _("No DW_FORM_block* DW_AT_call_value for "
12026 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12027 to_underlying (child_die->sect_off),
12028 objfile_name (objfile));
12029 continue;
12030 }
12031 parameter->value = DW_BLOCK (attr)->data;
12032 parameter->value_size = DW_BLOCK (attr)->size;
12033
12034 /* Parameters are not pre-cleared by memset above. */
12035 parameter->data_value = NULL;
12036 parameter->data_value_size = 0;
12037 call_site->parameter_count++;
12038
12039 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12040 if (attr == NULL)
12041 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12042 if (attr)
12043 {
12044 if (!attr_form_is_block (attr))
12045 complaint (&symfile_complaints,
12046 _("No DW_FORM_block* DW_AT_call_data_value for "
12047 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12048 to_underlying (child_die->sect_off),
12049 objfile_name (objfile));
12050 else
12051 {
12052 parameter->data_value = DW_BLOCK (attr)->data;
12053 parameter->data_value_size = DW_BLOCK (attr)->size;
12054 }
12055 }
12056 }
12057 }
12058
12059 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12060 reading .debug_rnglists.
12061 Callback's type should be:
12062 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12063 Return true if the attributes are present and valid, otherwise,
12064 return false. */
12065
12066 template <typename Callback>
12067 static bool
12068 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12069 Callback &&callback)
12070 {
12071 struct objfile *objfile = cu->objfile;
12072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12073 struct comp_unit_head *cu_header = &cu->header;
12074 bfd *obfd = objfile->obfd;
12075 unsigned int addr_size = cu_header->addr_size;
12076 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12077 /* Base address selection entry. */
12078 CORE_ADDR base;
12079 int found_base;
12080 unsigned int dummy;
12081 const gdb_byte *buffer;
12082 CORE_ADDR low = 0;
12083 CORE_ADDR high = 0;
12084 CORE_ADDR baseaddr;
12085 bool overflow = false;
12086
12087 found_base = cu->base_known;
12088 base = cu->base_address;
12089
12090 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12091 if (offset >= dwarf2_per_objfile->rnglists.size)
12092 {
12093 complaint (&symfile_complaints,
12094 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12095 offset);
12096 return false;
12097 }
12098 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12099
12100 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12101
12102 while (1)
12103 {
12104 /* Initialize it due to a false compiler warning. */
12105 CORE_ADDR range_beginning = 0, range_end = 0;
12106 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12107 + dwarf2_per_objfile->rnglists.size);
12108 unsigned int bytes_read;
12109
12110 if (buffer == buf_end)
12111 {
12112 overflow = true;
12113 break;
12114 }
12115 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12116 switch (rlet)
12117 {
12118 case DW_RLE_end_of_list:
12119 break;
12120 case DW_RLE_base_address:
12121 if (buffer + cu->header.addr_size > buf_end)
12122 {
12123 overflow = true;
12124 break;
12125 }
12126 base = read_address (obfd, buffer, cu, &bytes_read);
12127 found_base = 1;
12128 buffer += bytes_read;
12129 break;
12130 case DW_RLE_start_length:
12131 if (buffer + cu->header.addr_size > buf_end)
12132 {
12133 overflow = true;
12134 break;
12135 }
12136 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12137 buffer += bytes_read;
12138 range_end = (range_beginning
12139 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12140 buffer += bytes_read;
12141 if (buffer > buf_end)
12142 {
12143 overflow = true;
12144 break;
12145 }
12146 break;
12147 case DW_RLE_offset_pair:
12148 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12149 buffer += bytes_read;
12150 if (buffer > buf_end)
12151 {
12152 overflow = true;
12153 break;
12154 }
12155 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12156 buffer += bytes_read;
12157 if (buffer > buf_end)
12158 {
12159 overflow = true;
12160 break;
12161 }
12162 break;
12163 case DW_RLE_start_end:
12164 if (buffer + 2 * cu->header.addr_size > buf_end)
12165 {
12166 overflow = true;
12167 break;
12168 }
12169 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12170 buffer += bytes_read;
12171 range_end = read_address (obfd, buffer, cu, &bytes_read);
12172 buffer += bytes_read;
12173 break;
12174 default:
12175 complaint (&symfile_complaints,
12176 _("Invalid .debug_rnglists data (no base address)"));
12177 return false;
12178 }
12179 if (rlet == DW_RLE_end_of_list || overflow)
12180 break;
12181 if (rlet == DW_RLE_base_address)
12182 continue;
12183
12184 if (!found_base)
12185 {
12186 /* We have no valid base address for the ranges
12187 data. */
12188 complaint (&symfile_complaints,
12189 _("Invalid .debug_rnglists data (no base address)"));
12190 return false;
12191 }
12192
12193 if (range_beginning > range_end)
12194 {
12195 /* Inverted range entries are invalid. */
12196 complaint (&symfile_complaints,
12197 _("Invalid .debug_rnglists data (inverted range)"));
12198 return false;
12199 }
12200
12201 /* Empty range entries have no effect. */
12202 if (range_beginning == range_end)
12203 continue;
12204
12205 range_beginning += base;
12206 range_end += base;
12207
12208 /* A not-uncommon case of bad debug info.
12209 Don't pollute the addrmap with bad data. */
12210 if (range_beginning + baseaddr == 0
12211 && !dwarf2_per_objfile->has_section_at_zero)
12212 {
12213 complaint (&symfile_complaints,
12214 _(".debug_rnglists entry has start address of zero"
12215 " [in module %s]"), objfile_name (objfile));
12216 continue;
12217 }
12218
12219 callback (range_beginning, range_end);
12220 }
12221
12222 if (overflow)
12223 {
12224 complaint (&symfile_complaints,
12225 _("Offset %d is not terminated "
12226 "for DW_AT_ranges attribute"),
12227 offset);
12228 return false;
12229 }
12230
12231 return true;
12232 }
12233
12234 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12235 Callback's type should be:
12236 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12237 Return 1 if the attributes are present and valid, otherwise, return 0. */
12238
12239 template <typename Callback>
12240 static int
12241 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12242 Callback &&callback)
12243 {
12244 struct objfile *objfile = cu->objfile;
12245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12246 struct comp_unit_head *cu_header = &cu->header;
12247 bfd *obfd = objfile->obfd;
12248 unsigned int addr_size = cu_header->addr_size;
12249 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12250 /* Base address selection entry. */
12251 CORE_ADDR base;
12252 int found_base;
12253 unsigned int dummy;
12254 const gdb_byte *buffer;
12255 CORE_ADDR baseaddr;
12256
12257 if (cu_header->version >= 5)
12258 return dwarf2_rnglists_process (offset, cu, callback);
12259
12260 found_base = cu->base_known;
12261 base = cu->base_address;
12262
12263 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12264 if (offset >= dwarf2_per_objfile->ranges.size)
12265 {
12266 complaint (&symfile_complaints,
12267 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12268 offset);
12269 return 0;
12270 }
12271 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12272
12273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12274
12275 while (1)
12276 {
12277 CORE_ADDR range_beginning, range_end;
12278
12279 range_beginning = read_address (obfd, buffer, cu, &dummy);
12280 buffer += addr_size;
12281 range_end = read_address (obfd, buffer, cu, &dummy);
12282 buffer += addr_size;
12283 offset += 2 * addr_size;
12284
12285 /* An end of list marker is a pair of zero addresses. */
12286 if (range_beginning == 0 && range_end == 0)
12287 /* Found the end of list entry. */
12288 break;
12289
12290 /* Each base address selection entry is a pair of 2 values.
12291 The first is the largest possible address, the second is
12292 the base address. Check for a base address here. */
12293 if ((range_beginning & mask) == mask)
12294 {
12295 /* If we found the largest possible address, then we already
12296 have the base address in range_end. */
12297 base = range_end;
12298 found_base = 1;
12299 continue;
12300 }
12301
12302 if (!found_base)
12303 {
12304 /* We have no valid base address for the ranges
12305 data. */
12306 complaint (&symfile_complaints,
12307 _("Invalid .debug_ranges data (no base address)"));
12308 return 0;
12309 }
12310
12311 if (range_beginning > range_end)
12312 {
12313 /* Inverted range entries are invalid. */
12314 complaint (&symfile_complaints,
12315 _("Invalid .debug_ranges data (inverted range)"));
12316 return 0;
12317 }
12318
12319 /* Empty range entries have no effect. */
12320 if (range_beginning == range_end)
12321 continue;
12322
12323 range_beginning += base;
12324 range_end += base;
12325
12326 /* A not-uncommon case of bad debug info.
12327 Don't pollute the addrmap with bad data. */
12328 if (range_beginning + baseaddr == 0
12329 && !dwarf2_per_objfile->has_section_at_zero)
12330 {
12331 complaint (&symfile_complaints,
12332 _(".debug_ranges entry has start address of zero"
12333 " [in module %s]"), objfile_name (objfile));
12334 continue;
12335 }
12336
12337 callback (range_beginning, range_end);
12338 }
12339
12340 return 1;
12341 }
12342
12343 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12344 Return 1 if the attributes are present and valid, otherwise, return 0.
12345 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12346
12347 static int
12348 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12349 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12350 struct partial_symtab *ranges_pst)
12351 {
12352 struct objfile *objfile = cu->objfile;
12353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12354 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12355 SECT_OFF_TEXT (objfile));
12356 int low_set = 0;
12357 CORE_ADDR low = 0;
12358 CORE_ADDR high = 0;
12359 int retval;
12360
12361 retval = dwarf2_ranges_process (offset, cu,
12362 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12363 {
12364 if (ranges_pst != NULL)
12365 {
12366 CORE_ADDR lowpc;
12367 CORE_ADDR highpc;
12368
12369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12370 range_beginning + baseaddr);
12371 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12372 range_end + baseaddr);
12373 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12374 ranges_pst);
12375 }
12376
12377 /* FIXME: This is recording everything as a low-high
12378 segment of consecutive addresses. We should have a
12379 data structure for discontiguous block ranges
12380 instead. */
12381 if (! low_set)
12382 {
12383 low = range_beginning;
12384 high = range_end;
12385 low_set = 1;
12386 }
12387 else
12388 {
12389 if (range_beginning < low)
12390 low = range_beginning;
12391 if (range_end > high)
12392 high = range_end;
12393 }
12394 });
12395 if (!retval)
12396 return 0;
12397
12398 if (! low_set)
12399 /* If the first entry is an end-of-list marker, the range
12400 describes an empty scope, i.e. no instructions. */
12401 return 0;
12402
12403 if (low_return)
12404 *low_return = low;
12405 if (high_return)
12406 *high_return = high;
12407 return 1;
12408 }
12409
12410 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12411 definition for the return value. *LOWPC and *HIGHPC are set iff
12412 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12413
12414 static enum pc_bounds_kind
12415 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12416 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12417 struct partial_symtab *pst)
12418 {
12419 struct attribute *attr;
12420 struct attribute *attr_high;
12421 CORE_ADDR low = 0;
12422 CORE_ADDR high = 0;
12423 enum pc_bounds_kind ret;
12424
12425 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12426 if (attr_high)
12427 {
12428 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12429 if (attr)
12430 {
12431 low = attr_value_as_address (attr);
12432 high = attr_value_as_address (attr_high);
12433 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12434 high += low;
12435 }
12436 else
12437 /* Found high w/o low attribute. */
12438 return PC_BOUNDS_INVALID;
12439
12440 /* Found consecutive range of addresses. */
12441 ret = PC_BOUNDS_HIGH_LOW;
12442 }
12443 else
12444 {
12445 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12446 if (attr != NULL)
12447 {
12448 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12449 We take advantage of the fact that DW_AT_ranges does not appear
12450 in DW_TAG_compile_unit of DWO files. */
12451 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12452 unsigned int ranges_offset = (DW_UNSND (attr)
12453 + (need_ranges_base
12454 ? cu->ranges_base
12455 : 0));
12456
12457 /* Value of the DW_AT_ranges attribute is the offset in the
12458 .debug_ranges section. */
12459 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12460 return PC_BOUNDS_INVALID;
12461 /* Found discontinuous range of addresses. */
12462 ret = PC_BOUNDS_RANGES;
12463 }
12464 else
12465 return PC_BOUNDS_NOT_PRESENT;
12466 }
12467
12468 /* read_partial_die has also the strict LOW < HIGH requirement. */
12469 if (high <= low)
12470 return PC_BOUNDS_INVALID;
12471
12472 /* When using the GNU linker, .gnu.linkonce. sections are used to
12473 eliminate duplicate copies of functions and vtables and such.
12474 The linker will arbitrarily choose one and discard the others.
12475 The AT_*_pc values for such functions refer to local labels in
12476 these sections. If the section from that file was discarded, the
12477 labels are not in the output, so the relocs get a value of 0.
12478 If this is a discarded function, mark the pc bounds as invalid,
12479 so that GDB will ignore it. */
12480 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12481 return PC_BOUNDS_INVALID;
12482
12483 *lowpc = low;
12484 if (highpc)
12485 *highpc = high;
12486 return ret;
12487 }
12488
12489 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12490 its low and high PC addresses. Do nothing if these addresses could not
12491 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12492 and HIGHPC to the high address if greater than HIGHPC. */
12493
12494 static void
12495 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12496 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12497 struct dwarf2_cu *cu)
12498 {
12499 CORE_ADDR low, high;
12500 struct die_info *child = die->child;
12501
12502 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12503 {
12504 *lowpc = std::min (*lowpc, low);
12505 *highpc = std::max (*highpc, high);
12506 }
12507
12508 /* If the language does not allow nested subprograms (either inside
12509 subprograms or lexical blocks), we're done. */
12510 if (cu->language != language_ada)
12511 return;
12512
12513 /* Check all the children of the given DIE. If it contains nested
12514 subprograms, then check their pc bounds. Likewise, we need to
12515 check lexical blocks as well, as they may also contain subprogram
12516 definitions. */
12517 while (child && child->tag)
12518 {
12519 if (child->tag == DW_TAG_subprogram
12520 || child->tag == DW_TAG_lexical_block)
12521 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12522 child = sibling_die (child);
12523 }
12524 }
12525
12526 /* Get the low and high pc's represented by the scope DIE, and store
12527 them in *LOWPC and *HIGHPC. If the correct values can't be
12528 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12529
12530 static void
12531 get_scope_pc_bounds (struct die_info *die,
12532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12533 struct dwarf2_cu *cu)
12534 {
12535 CORE_ADDR best_low = (CORE_ADDR) -1;
12536 CORE_ADDR best_high = (CORE_ADDR) 0;
12537 CORE_ADDR current_low, current_high;
12538
12539 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12540 >= PC_BOUNDS_RANGES)
12541 {
12542 best_low = current_low;
12543 best_high = current_high;
12544 }
12545 else
12546 {
12547 struct die_info *child = die->child;
12548
12549 while (child && child->tag)
12550 {
12551 switch (child->tag) {
12552 case DW_TAG_subprogram:
12553 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12554 break;
12555 case DW_TAG_namespace:
12556 case DW_TAG_module:
12557 /* FIXME: carlton/2004-01-16: Should we do this for
12558 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12559 that current GCC's always emit the DIEs corresponding
12560 to definitions of methods of classes as children of a
12561 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12562 the DIEs giving the declarations, which could be
12563 anywhere). But I don't see any reason why the
12564 standards says that they have to be there. */
12565 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12566
12567 if (current_low != ((CORE_ADDR) -1))
12568 {
12569 best_low = std::min (best_low, current_low);
12570 best_high = std::max (best_high, current_high);
12571 }
12572 break;
12573 default:
12574 /* Ignore. */
12575 break;
12576 }
12577
12578 child = sibling_die (child);
12579 }
12580 }
12581
12582 *lowpc = best_low;
12583 *highpc = best_high;
12584 }
12585
12586 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12587 in DIE. */
12588
12589 static void
12590 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12591 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12592 {
12593 struct objfile *objfile = cu->objfile;
12594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12595 struct attribute *attr;
12596 struct attribute *attr_high;
12597
12598 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12599 if (attr_high)
12600 {
12601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12602 if (attr)
12603 {
12604 CORE_ADDR low = attr_value_as_address (attr);
12605 CORE_ADDR high = attr_value_as_address (attr_high);
12606
12607 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12608 high += low;
12609
12610 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12611 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12612 record_block_range (block, low, high - 1);
12613 }
12614 }
12615
12616 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12617 if (attr)
12618 {
12619 bfd *obfd = objfile->obfd;
12620 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12621 We take advantage of the fact that DW_AT_ranges does not appear
12622 in DW_TAG_compile_unit of DWO files. */
12623 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12624
12625 /* The value of the DW_AT_ranges attribute is the offset of the
12626 address range list in the .debug_ranges section. */
12627 unsigned long offset = (DW_UNSND (attr)
12628 + (need_ranges_base ? cu->ranges_base : 0));
12629 const gdb_byte *buffer;
12630
12631 /* For some target architectures, but not others, the
12632 read_address function sign-extends the addresses it returns.
12633 To recognize base address selection entries, we need a
12634 mask. */
12635 unsigned int addr_size = cu->header.addr_size;
12636 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12637
12638 /* The base address, to which the next pair is relative. Note
12639 that this 'base' is a DWARF concept: most entries in a range
12640 list are relative, to reduce the number of relocs against the
12641 debugging information. This is separate from this function's
12642 'baseaddr' argument, which GDB uses to relocate debugging
12643 information from a shared library based on the address at
12644 which the library was loaded. */
12645 CORE_ADDR base = cu->base_address;
12646 int base_known = cu->base_known;
12647
12648 dwarf2_ranges_process (offset, cu,
12649 [&] (CORE_ADDR start, CORE_ADDR end)
12650 {
12651 start += baseaddr;
12652 end += baseaddr;
12653 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12654 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12655 record_block_range (block, start, end - 1);
12656 });
12657 }
12658 }
12659
12660 /* Check whether the producer field indicates either of GCC < 4.6, or the
12661 Intel C/C++ compiler, and cache the result in CU. */
12662
12663 static void
12664 check_producer (struct dwarf2_cu *cu)
12665 {
12666 int major, minor;
12667
12668 if (cu->producer == NULL)
12669 {
12670 /* For unknown compilers expect their behavior is DWARF version
12671 compliant.
12672
12673 GCC started to support .debug_types sections by -gdwarf-4 since
12674 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12675 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12676 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12677 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12678 }
12679 else if (producer_is_gcc (cu->producer, &major, &minor))
12680 {
12681 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12682 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12683 }
12684 else if (startswith (cu->producer, "Intel(R) C"))
12685 cu->producer_is_icc = 1;
12686 else
12687 {
12688 /* For other non-GCC compilers, expect their behavior is DWARF version
12689 compliant. */
12690 }
12691
12692 cu->checked_producer = 1;
12693 }
12694
12695 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12696 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12697 during 4.6.0 experimental. */
12698
12699 static int
12700 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12701 {
12702 if (!cu->checked_producer)
12703 check_producer (cu);
12704
12705 return cu->producer_is_gxx_lt_4_6;
12706 }
12707
12708 /* Return the default accessibility type if it is not overriden by
12709 DW_AT_accessibility. */
12710
12711 static enum dwarf_access_attribute
12712 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12713 {
12714 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12715 {
12716 /* The default DWARF 2 accessibility for members is public, the default
12717 accessibility for inheritance is private. */
12718
12719 if (die->tag != DW_TAG_inheritance)
12720 return DW_ACCESS_public;
12721 else
12722 return DW_ACCESS_private;
12723 }
12724 else
12725 {
12726 /* DWARF 3+ defines the default accessibility a different way. The same
12727 rules apply now for DW_TAG_inheritance as for the members and it only
12728 depends on the container kind. */
12729
12730 if (die->parent->tag == DW_TAG_class_type)
12731 return DW_ACCESS_private;
12732 else
12733 return DW_ACCESS_public;
12734 }
12735 }
12736
12737 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12738 offset. If the attribute was not found return 0, otherwise return
12739 1. If it was found but could not properly be handled, set *OFFSET
12740 to 0. */
12741
12742 static int
12743 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12744 LONGEST *offset)
12745 {
12746 struct attribute *attr;
12747
12748 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12749 if (attr != NULL)
12750 {
12751 *offset = 0;
12752
12753 /* Note that we do not check for a section offset first here.
12754 This is because DW_AT_data_member_location is new in DWARF 4,
12755 so if we see it, we can assume that a constant form is really
12756 a constant and not a section offset. */
12757 if (attr_form_is_constant (attr))
12758 *offset = dwarf2_get_attr_constant_value (attr, 0);
12759 else if (attr_form_is_section_offset (attr))
12760 dwarf2_complex_location_expr_complaint ();
12761 else if (attr_form_is_block (attr))
12762 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12763 else
12764 dwarf2_complex_location_expr_complaint ();
12765
12766 return 1;
12767 }
12768
12769 return 0;
12770 }
12771
12772 /* Add an aggregate field to the field list. */
12773
12774 static void
12775 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12776 struct dwarf2_cu *cu)
12777 {
12778 struct objfile *objfile = cu->objfile;
12779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12780 struct nextfield *new_field;
12781 struct attribute *attr;
12782 struct field *fp;
12783 const char *fieldname = "";
12784
12785 /* Allocate a new field list entry and link it in. */
12786 new_field = XNEW (struct nextfield);
12787 make_cleanup (xfree, new_field);
12788 memset (new_field, 0, sizeof (struct nextfield));
12789
12790 if (die->tag == DW_TAG_inheritance)
12791 {
12792 new_field->next = fip->baseclasses;
12793 fip->baseclasses = new_field;
12794 }
12795 else
12796 {
12797 new_field->next = fip->fields;
12798 fip->fields = new_field;
12799 }
12800 fip->nfields++;
12801
12802 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12803 if (attr)
12804 new_field->accessibility = DW_UNSND (attr);
12805 else
12806 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12807 if (new_field->accessibility != DW_ACCESS_public)
12808 fip->non_public_fields = 1;
12809
12810 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12811 if (attr)
12812 new_field->virtuality = DW_UNSND (attr);
12813 else
12814 new_field->virtuality = DW_VIRTUALITY_none;
12815
12816 fp = &new_field->field;
12817
12818 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12819 {
12820 LONGEST offset;
12821
12822 /* Data member other than a C++ static data member. */
12823
12824 /* Get type of field. */
12825 fp->type = die_type (die, cu);
12826
12827 SET_FIELD_BITPOS (*fp, 0);
12828
12829 /* Get bit size of field (zero if none). */
12830 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12831 if (attr)
12832 {
12833 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12834 }
12835 else
12836 {
12837 FIELD_BITSIZE (*fp) = 0;
12838 }
12839
12840 /* Get bit offset of field. */
12841 if (handle_data_member_location (die, cu, &offset))
12842 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12843 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12844 if (attr)
12845 {
12846 if (gdbarch_bits_big_endian (gdbarch))
12847 {
12848 /* For big endian bits, the DW_AT_bit_offset gives the
12849 additional bit offset from the MSB of the containing
12850 anonymous object to the MSB of the field. We don't
12851 have to do anything special since we don't need to
12852 know the size of the anonymous object. */
12853 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12854 }
12855 else
12856 {
12857 /* For little endian bits, compute the bit offset to the
12858 MSB of the anonymous object, subtract off the number of
12859 bits from the MSB of the field to the MSB of the
12860 object, and then subtract off the number of bits of
12861 the field itself. The result is the bit offset of
12862 the LSB of the field. */
12863 int anonymous_size;
12864 int bit_offset = DW_UNSND (attr);
12865
12866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12867 if (attr)
12868 {
12869 /* The size of the anonymous object containing
12870 the bit field is explicit, so use the
12871 indicated size (in bytes). */
12872 anonymous_size = DW_UNSND (attr);
12873 }
12874 else
12875 {
12876 /* The size of the anonymous object containing
12877 the bit field must be inferred from the type
12878 attribute of the data member containing the
12879 bit field. */
12880 anonymous_size = TYPE_LENGTH (fp->type);
12881 }
12882 SET_FIELD_BITPOS (*fp,
12883 (FIELD_BITPOS (*fp)
12884 + anonymous_size * bits_per_byte
12885 - bit_offset - FIELD_BITSIZE (*fp)));
12886 }
12887 }
12888 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12889 if (attr != NULL)
12890 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12891 + dwarf2_get_attr_constant_value (attr, 0)));
12892
12893 /* Get name of field. */
12894 fieldname = dwarf2_name (die, cu);
12895 if (fieldname == NULL)
12896 fieldname = "";
12897
12898 /* The name is already allocated along with this objfile, so we don't
12899 need to duplicate it for the type. */
12900 fp->name = fieldname;
12901
12902 /* Change accessibility for artificial fields (e.g. virtual table
12903 pointer or virtual base class pointer) to private. */
12904 if (dwarf2_attr (die, DW_AT_artificial, cu))
12905 {
12906 FIELD_ARTIFICIAL (*fp) = 1;
12907 new_field->accessibility = DW_ACCESS_private;
12908 fip->non_public_fields = 1;
12909 }
12910 }
12911 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12912 {
12913 /* C++ static member. */
12914
12915 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12916 is a declaration, but all versions of G++ as of this writing
12917 (so through at least 3.2.1) incorrectly generate
12918 DW_TAG_variable tags. */
12919
12920 const char *physname;
12921
12922 /* Get name of field. */
12923 fieldname = dwarf2_name (die, cu);
12924 if (fieldname == NULL)
12925 return;
12926
12927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12928 if (attr
12929 /* Only create a symbol if this is an external value.
12930 new_symbol checks this and puts the value in the global symbol
12931 table, which we want. If it is not external, new_symbol
12932 will try to put the value in cu->list_in_scope which is wrong. */
12933 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12934 {
12935 /* A static const member, not much different than an enum as far as
12936 we're concerned, except that we can support more types. */
12937 new_symbol (die, NULL, cu);
12938 }
12939
12940 /* Get physical name. */
12941 physname = dwarf2_physname (fieldname, die, cu);
12942
12943 /* The name is already allocated along with this objfile, so we don't
12944 need to duplicate it for the type. */
12945 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12946 FIELD_TYPE (*fp) = die_type (die, cu);
12947 FIELD_NAME (*fp) = fieldname;
12948 }
12949 else if (die->tag == DW_TAG_inheritance)
12950 {
12951 LONGEST offset;
12952
12953 /* C++ base class field. */
12954 if (handle_data_member_location (die, cu, &offset))
12955 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12956 FIELD_BITSIZE (*fp) = 0;
12957 FIELD_TYPE (*fp) = die_type (die, cu);
12958 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12959 fip->nbaseclasses++;
12960 }
12961 }
12962
12963 /* Add a typedef defined in the scope of the FIP's class. */
12964
12965 static void
12966 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12967 struct dwarf2_cu *cu)
12968 {
12969 struct typedef_field_list *new_field;
12970 struct typedef_field *fp;
12971
12972 /* Allocate a new field list entry and link it in. */
12973 new_field = XCNEW (struct typedef_field_list);
12974 make_cleanup (xfree, new_field);
12975
12976 gdb_assert (die->tag == DW_TAG_typedef);
12977
12978 fp = &new_field->field;
12979
12980 /* Get name of field. */
12981 fp->name = dwarf2_name (die, cu);
12982 if (fp->name == NULL)
12983 return;
12984
12985 fp->type = read_type_die (die, cu);
12986
12987 new_field->next = fip->typedef_field_list;
12988 fip->typedef_field_list = new_field;
12989 fip->typedef_field_list_count++;
12990 }
12991
12992 /* Create the vector of fields, and attach it to the type. */
12993
12994 static void
12995 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12996 struct dwarf2_cu *cu)
12997 {
12998 int nfields = fip->nfields;
12999
13000 /* Record the field count, allocate space for the array of fields,
13001 and create blank accessibility bitfields if necessary. */
13002 TYPE_NFIELDS (type) = nfields;
13003 TYPE_FIELDS (type) = (struct field *)
13004 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13005 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13006
13007 if (fip->non_public_fields && cu->language != language_ada)
13008 {
13009 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13010
13011 TYPE_FIELD_PRIVATE_BITS (type) =
13012 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13013 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13014
13015 TYPE_FIELD_PROTECTED_BITS (type) =
13016 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13017 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13018
13019 TYPE_FIELD_IGNORE_BITS (type) =
13020 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13021 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13022 }
13023
13024 /* If the type has baseclasses, allocate and clear a bit vector for
13025 TYPE_FIELD_VIRTUAL_BITS. */
13026 if (fip->nbaseclasses && cu->language != language_ada)
13027 {
13028 int num_bytes = B_BYTES (fip->nbaseclasses);
13029 unsigned char *pointer;
13030
13031 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13032 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13033 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13034 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13035 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13036 }
13037
13038 /* Copy the saved-up fields into the field vector. Start from the head of
13039 the list, adding to the tail of the field array, so that they end up in
13040 the same order in the array in which they were added to the list. */
13041 while (nfields-- > 0)
13042 {
13043 struct nextfield *fieldp;
13044
13045 if (fip->fields)
13046 {
13047 fieldp = fip->fields;
13048 fip->fields = fieldp->next;
13049 }
13050 else
13051 {
13052 fieldp = fip->baseclasses;
13053 fip->baseclasses = fieldp->next;
13054 }
13055
13056 TYPE_FIELD (type, nfields) = fieldp->field;
13057 switch (fieldp->accessibility)
13058 {
13059 case DW_ACCESS_private:
13060 if (cu->language != language_ada)
13061 SET_TYPE_FIELD_PRIVATE (type, nfields);
13062 break;
13063
13064 case DW_ACCESS_protected:
13065 if (cu->language != language_ada)
13066 SET_TYPE_FIELD_PROTECTED (type, nfields);
13067 break;
13068
13069 case DW_ACCESS_public:
13070 break;
13071
13072 default:
13073 /* Unknown accessibility. Complain and treat it as public. */
13074 {
13075 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13076 fieldp->accessibility);
13077 }
13078 break;
13079 }
13080 if (nfields < fip->nbaseclasses)
13081 {
13082 switch (fieldp->virtuality)
13083 {
13084 case DW_VIRTUALITY_virtual:
13085 case DW_VIRTUALITY_pure_virtual:
13086 if (cu->language == language_ada)
13087 error (_("unexpected virtuality in component of Ada type"));
13088 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13089 break;
13090 }
13091 }
13092 }
13093 }
13094
13095 /* Return true if this member function is a constructor, false
13096 otherwise. */
13097
13098 static int
13099 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13100 {
13101 const char *fieldname;
13102 const char *type_name;
13103 int len;
13104
13105 if (die->parent == NULL)
13106 return 0;
13107
13108 if (die->parent->tag != DW_TAG_structure_type
13109 && die->parent->tag != DW_TAG_union_type
13110 && die->parent->tag != DW_TAG_class_type)
13111 return 0;
13112
13113 fieldname = dwarf2_name (die, cu);
13114 type_name = dwarf2_name (die->parent, cu);
13115 if (fieldname == NULL || type_name == NULL)
13116 return 0;
13117
13118 len = strlen (fieldname);
13119 return (strncmp (fieldname, type_name, len) == 0
13120 && (type_name[len] == '\0' || type_name[len] == '<'));
13121 }
13122
13123 /* Add a member function to the proper fieldlist. */
13124
13125 static void
13126 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13127 struct type *type, struct dwarf2_cu *cu)
13128 {
13129 struct objfile *objfile = cu->objfile;
13130 struct attribute *attr;
13131 struct fnfieldlist *flp;
13132 int i;
13133 struct fn_field *fnp;
13134 const char *fieldname;
13135 struct nextfnfield *new_fnfield;
13136 struct type *this_type;
13137 enum dwarf_access_attribute accessibility;
13138
13139 if (cu->language == language_ada)
13140 error (_("unexpected member function in Ada type"));
13141
13142 /* Get name of member function. */
13143 fieldname = dwarf2_name (die, cu);
13144 if (fieldname == NULL)
13145 return;
13146
13147 /* Look up member function name in fieldlist. */
13148 for (i = 0; i < fip->nfnfields; i++)
13149 {
13150 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13151 break;
13152 }
13153
13154 /* Create new list element if necessary. */
13155 if (i < fip->nfnfields)
13156 flp = &fip->fnfieldlists[i];
13157 else
13158 {
13159 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13160 {
13161 fip->fnfieldlists = (struct fnfieldlist *)
13162 xrealloc (fip->fnfieldlists,
13163 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13164 * sizeof (struct fnfieldlist));
13165 if (fip->nfnfields == 0)
13166 make_cleanup (free_current_contents, &fip->fnfieldlists);
13167 }
13168 flp = &fip->fnfieldlists[fip->nfnfields];
13169 flp->name = fieldname;
13170 flp->length = 0;
13171 flp->head = NULL;
13172 i = fip->nfnfields++;
13173 }
13174
13175 /* Create a new member function field and chain it to the field list
13176 entry. */
13177 new_fnfield = XNEW (struct nextfnfield);
13178 make_cleanup (xfree, new_fnfield);
13179 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13180 new_fnfield->next = flp->head;
13181 flp->head = new_fnfield;
13182 flp->length++;
13183
13184 /* Fill in the member function field info. */
13185 fnp = &new_fnfield->fnfield;
13186
13187 /* Delay processing of the physname until later. */
13188 if (cu->language == language_cplus)
13189 {
13190 add_to_method_list (type, i, flp->length - 1, fieldname,
13191 die, cu);
13192 }
13193 else
13194 {
13195 const char *physname = dwarf2_physname (fieldname, die, cu);
13196 fnp->physname = physname ? physname : "";
13197 }
13198
13199 fnp->type = alloc_type (objfile);
13200 this_type = read_type_die (die, cu);
13201 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13202 {
13203 int nparams = TYPE_NFIELDS (this_type);
13204
13205 /* TYPE is the domain of this method, and THIS_TYPE is the type
13206 of the method itself (TYPE_CODE_METHOD). */
13207 smash_to_method_type (fnp->type, type,
13208 TYPE_TARGET_TYPE (this_type),
13209 TYPE_FIELDS (this_type),
13210 TYPE_NFIELDS (this_type),
13211 TYPE_VARARGS (this_type));
13212
13213 /* Handle static member functions.
13214 Dwarf2 has no clean way to discern C++ static and non-static
13215 member functions. G++ helps GDB by marking the first
13216 parameter for non-static member functions (which is the this
13217 pointer) as artificial. We obtain this information from
13218 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13219 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13220 fnp->voffset = VOFFSET_STATIC;
13221 }
13222 else
13223 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13224 dwarf2_full_name (fieldname, die, cu));
13225
13226 /* Get fcontext from DW_AT_containing_type if present. */
13227 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13228 fnp->fcontext = die_containing_type (die, cu);
13229
13230 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13231 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13232
13233 /* Get accessibility. */
13234 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13235 if (attr)
13236 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13237 else
13238 accessibility = dwarf2_default_access_attribute (die, cu);
13239 switch (accessibility)
13240 {
13241 case DW_ACCESS_private:
13242 fnp->is_private = 1;
13243 break;
13244 case DW_ACCESS_protected:
13245 fnp->is_protected = 1;
13246 break;
13247 }
13248
13249 /* Check for artificial methods. */
13250 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13251 if (attr && DW_UNSND (attr) != 0)
13252 fnp->is_artificial = 1;
13253
13254 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13255
13256 /* Get index in virtual function table if it is a virtual member
13257 function. For older versions of GCC, this is an offset in the
13258 appropriate virtual table, as specified by DW_AT_containing_type.
13259 For everyone else, it is an expression to be evaluated relative
13260 to the object address. */
13261
13262 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13263 if (attr)
13264 {
13265 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13266 {
13267 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13268 {
13269 /* Old-style GCC. */
13270 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13271 }
13272 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13273 || (DW_BLOCK (attr)->size > 1
13274 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13275 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13276 {
13277 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13278 if ((fnp->voffset % cu->header.addr_size) != 0)
13279 dwarf2_complex_location_expr_complaint ();
13280 else
13281 fnp->voffset /= cu->header.addr_size;
13282 fnp->voffset += 2;
13283 }
13284 else
13285 dwarf2_complex_location_expr_complaint ();
13286
13287 if (!fnp->fcontext)
13288 {
13289 /* If there is no `this' field and no DW_AT_containing_type,
13290 we cannot actually find a base class context for the
13291 vtable! */
13292 if (TYPE_NFIELDS (this_type) == 0
13293 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13294 {
13295 complaint (&symfile_complaints,
13296 _("cannot determine context for virtual member "
13297 "function \"%s\" (offset %d)"),
13298 fieldname, to_underlying (die->sect_off));
13299 }
13300 else
13301 {
13302 fnp->fcontext
13303 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13304 }
13305 }
13306 }
13307 else if (attr_form_is_section_offset (attr))
13308 {
13309 dwarf2_complex_location_expr_complaint ();
13310 }
13311 else
13312 {
13313 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13314 fieldname);
13315 }
13316 }
13317 else
13318 {
13319 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13320 if (attr && DW_UNSND (attr))
13321 {
13322 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13323 complaint (&symfile_complaints,
13324 _("Member function \"%s\" (offset %d) is virtual "
13325 "but the vtable offset is not specified"),
13326 fieldname, to_underlying (die->sect_off));
13327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13328 TYPE_CPLUS_DYNAMIC (type) = 1;
13329 }
13330 }
13331 }
13332
13333 /* Create the vector of member function fields, and attach it to the type. */
13334
13335 static void
13336 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13337 struct dwarf2_cu *cu)
13338 {
13339 struct fnfieldlist *flp;
13340 int i;
13341
13342 if (cu->language == language_ada)
13343 error (_("unexpected member functions in Ada type"));
13344
13345 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13346 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13347 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13348
13349 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13350 {
13351 struct nextfnfield *nfp = flp->head;
13352 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13353 int k;
13354
13355 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13356 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13357 fn_flp->fn_fields = (struct fn_field *)
13358 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13359 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13360 fn_flp->fn_fields[k] = nfp->fnfield;
13361 }
13362
13363 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13364 }
13365
13366 /* Returns non-zero if NAME is the name of a vtable member in CU's
13367 language, zero otherwise. */
13368 static int
13369 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13370 {
13371 static const char vptr[] = "_vptr";
13372 static const char vtable[] = "vtable";
13373
13374 /* Look for the C++ form of the vtable. */
13375 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13376 return 1;
13377
13378 return 0;
13379 }
13380
13381 /* GCC outputs unnamed structures that are really pointers to member
13382 functions, with the ABI-specified layout. If TYPE describes
13383 such a structure, smash it into a member function type.
13384
13385 GCC shouldn't do this; it should just output pointer to member DIEs.
13386 This is GCC PR debug/28767. */
13387
13388 static void
13389 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13390 {
13391 struct type *pfn_type, *self_type, *new_type;
13392
13393 /* Check for a structure with no name and two children. */
13394 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13395 return;
13396
13397 /* Check for __pfn and __delta members. */
13398 if (TYPE_FIELD_NAME (type, 0) == NULL
13399 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13400 || TYPE_FIELD_NAME (type, 1) == NULL
13401 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13402 return;
13403
13404 /* Find the type of the method. */
13405 pfn_type = TYPE_FIELD_TYPE (type, 0);
13406 if (pfn_type == NULL
13407 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13408 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13409 return;
13410
13411 /* Look for the "this" argument. */
13412 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13413 if (TYPE_NFIELDS (pfn_type) == 0
13414 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13415 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13416 return;
13417
13418 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13419 new_type = alloc_type (objfile);
13420 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13421 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13422 TYPE_VARARGS (pfn_type));
13423 smash_to_methodptr_type (type, new_type);
13424 }
13425
13426 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13427 (icc). */
13428
13429 static int
13430 producer_is_icc (struct dwarf2_cu *cu)
13431 {
13432 if (!cu->checked_producer)
13433 check_producer (cu);
13434
13435 return cu->producer_is_icc;
13436 }
13437
13438 /* Called when we find the DIE that starts a structure or union scope
13439 (definition) to create a type for the structure or union. Fill in
13440 the type's name and general properties; the members will not be
13441 processed until process_structure_scope. A symbol table entry for
13442 the type will also not be done until process_structure_scope (assuming
13443 the type has a name).
13444
13445 NOTE: we need to call these functions regardless of whether or not the
13446 DIE has a DW_AT_name attribute, since it might be an anonymous
13447 structure or union. This gets the type entered into our set of
13448 user defined types. */
13449
13450 static struct type *
13451 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13452 {
13453 struct objfile *objfile = cu->objfile;
13454 struct type *type;
13455 struct attribute *attr;
13456 const char *name;
13457
13458 /* If the definition of this type lives in .debug_types, read that type.
13459 Don't follow DW_AT_specification though, that will take us back up
13460 the chain and we want to go down. */
13461 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13462 if (attr)
13463 {
13464 type = get_DW_AT_signature_type (die, attr, cu);
13465
13466 /* The type's CU may not be the same as CU.
13467 Ensure TYPE is recorded with CU in die_type_hash. */
13468 return set_die_type (die, type, cu);
13469 }
13470
13471 type = alloc_type (objfile);
13472 INIT_CPLUS_SPECIFIC (type);
13473
13474 name = dwarf2_name (die, cu);
13475 if (name != NULL)
13476 {
13477 if (cu->language == language_cplus
13478 || cu->language == language_d
13479 || cu->language == language_rust)
13480 {
13481 const char *full_name = dwarf2_full_name (name, die, cu);
13482
13483 /* dwarf2_full_name might have already finished building the DIE's
13484 type. If so, there is no need to continue. */
13485 if (get_die_type (die, cu) != NULL)
13486 return get_die_type (die, cu);
13487
13488 TYPE_TAG_NAME (type) = full_name;
13489 if (die->tag == DW_TAG_structure_type
13490 || die->tag == DW_TAG_class_type)
13491 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13492 }
13493 else
13494 {
13495 /* The name is already allocated along with this objfile, so
13496 we don't need to duplicate it for the type. */
13497 TYPE_TAG_NAME (type) = name;
13498 if (die->tag == DW_TAG_class_type)
13499 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13500 }
13501 }
13502
13503 if (die->tag == DW_TAG_structure_type)
13504 {
13505 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13506 }
13507 else if (die->tag == DW_TAG_union_type)
13508 {
13509 TYPE_CODE (type) = TYPE_CODE_UNION;
13510 }
13511 else
13512 {
13513 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13514 }
13515
13516 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13517 TYPE_DECLARED_CLASS (type) = 1;
13518
13519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13520 if (attr)
13521 {
13522 if (attr_form_is_constant (attr))
13523 TYPE_LENGTH (type) = DW_UNSND (attr);
13524 else
13525 {
13526 /* For the moment, dynamic type sizes are not supported
13527 by GDB's struct type. The actual size is determined
13528 on-demand when resolving the type of a given object,
13529 so set the type's length to zero for now. Otherwise,
13530 we record an expression as the length, and that expression
13531 could lead to a very large value, which could eventually
13532 lead to us trying to allocate that much memory when creating
13533 a value of that type. */
13534 TYPE_LENGTH (type) = 0;
13535 }
13536 }
13537 else
13538 {
13539 TYPE_LENGTH (type) = 0;
13540 }
13541
13542 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13543 {
13544 /* ICC does not output the required DW_AT_declaration
13545 on incomplete types, but gives them a size of zero. */
13546 TYPE_STUB (type) = 1;
13547 }
13548 else
13549 TYPE_STUB_SUPPORTED (type) = 1;
13550
13551 if (die_is_declaration (die, cu))
13552 TYPE_STUB (type) = 1;
13553 else if (attr == NULL && die->child == NULL
13554 && producer_is_realview (cu->producer))
13555 /* RealView does not output the required DW_AT_declaration
13556 on incomplete types. */
13557 TYPE_STUB (type) = 1;
13558
13559 /* We need to add the type field to the die immediately so we don't
13560 infinitely recurse when dealing with pointers to the structure
13561 type within the structure itself. */
13562 set_die_type (die, type, cu);
13563
13564 /* set_die_type should be already done. */
13565 set_descriptive_type (type, die, cu);
13566
13567 return type;
13568 }
13569
13570 /* Finish creating a structure or union type, including filling in
13571 its members and creating a symbol for it. */
13572
13573 static void
13574 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13575 {
13576 struct objfile *objfile = cu->objfile;
13577 struct die_info *child_die;
13578 struct type *type;
13579
13580 type = get_die_type (die, cu);
13581 if (type == NULL)
13582 type = read_structure_type (die, cu);
13583
13584 if (die->child != NULL && ! die_is_declaration (die, cu))
13585 {
13586 struct field_info fi;
13587 VEC (symbolp) *template_args = NULL;
13588 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13589
13590 memset (&fi, 0, sizeof (struct field_info));
13591
13592 child_die = die->child;
13593
13594 while (child_die && child_die->tag)
13595 {
13596 if (child_die->tag == DW_TAG_member
13597 || child_die->tag == DW_TAG_variable)
13598 {
13599 /* NOTE: carlton/2002-11-05: A C++ static data member
13600 should be a DW_TAG_member that is a declaration, but
13601 all versions of G++ as of this writing (so through at
13602 least 3.2.1) incorrectly generate DW_TAG_variable
13603 tags for them instead. */
13604 dwarf2_add_field (&fi, child_die, cu);
13605 }
13606 else if (child_die->tag == DW_TAG_subprogram)
13607 {
13608 /* Rust doesn't have member functions in the C++ sense.
13609 However, it does emit ordinary functions as children
13610 of a struct DIE. */
13611 if (cu->language == language_rust)
13612 read_func_scope (child_die, cu);
13613 else
13614 {
13615 /* C++ member function. */
13616 dwarf2_add_member_fn (&fi, child_die, type, cu);
13617 }
13618 }
13619 else if (child_die->tag == DW_TAG_inheritance)
13620 {
13621 /* C++ base class field. */
13622 dwarf2_add_field (&fi, child_die, cu);
13623 }
13624 else if (child_die->tag == DW_TAG_typedef)
13625 dwarf2_add_typedef (&fi, child_die, cu);
13626 else if (child_die->tag == DW_TAG_template_type_param
13627 || child_die->tag == DW_TAG_template_value_param)
13628 {
13629 struct symbol *arg = new_symbol (child_die, NULL, cu);
13630
13631 if (arg != NULL)
13632 VEC_safe_push (symbolp, template_args, arg);
13633 }
13634
13635 child_die = sibling_die (child_die);
13636 }
13637
13638 /* Attach template arguments to type. */
13639 if (! VEC_empty (symbolp, template_args))
13640 {
13641 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13642 TYPE_N_TEMPLATE_ARGUMENTS (type)
13643 = VEC_length (symbolp, template_args);
13644 TYPE_TEMPLATE_ARGUMENTS (type)
13645 = XOBNEWVEC (&objfile->objfile_obstack,
13646 struct symbol *,
13647 TYPE_N_TEMPLATE_ARGUMENTS (type));
13648 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13649 VEC_address (symbolp, template_args),
13650 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13651 * sizeof (struct symbol *)));
13652 VEC_free (symbolp, template_args);
13653 }
13654
13655 /* Attach fields and member functions to the type. */
13656 if (fi.nfields)
13657 dwarf2_attach_fields_to_type (&fi, type, cu);
13658 if (fi.nfnfields)
13659 {
13660 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13661
13662 /* Get the type which refers to the base class (possibly this
13663 class itself) which contains the vtable pointer for the current
13664 class from the DW_AT_containing_type attribute. This use of
13665 DW_AT_containing_type is a GNU extension. */
13666
13667 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13668 {
13669 struct type *t = die_containing_type (die, cu);
13670
13671 set_type_vptr_basetype (type, t);
13672 if (type == t)
13673 {
13674 int i;
13675
13676 /* Our own class provides vtbl ptr. */
13677 for (i = TYPE_NFIELDS (t) - 1;
13678 i >= TYPE_N_BASECLASSES (t);
13679 --i)
13680 {
13681 const char *fieldname = TYPE_FIELD_NAME (t, i);
13682
13683 if (is_vtable_name (fieldname, cu))
13684 {
13685 set_type_vptr_fieldno (type, i);
13686 break;
13687 }
13688 }
13689
13690 /* Complain if virtual function table field not found. */
13691 if (i < TYPE_N_BASECLASSES (t))
13692 complaint (&symfile_complaints,
13693 _("virtual function table pointer "
13694 "not found when defining class '%s'"),
13695 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13696 "");
13697 }
13698 else
13699 {
13700 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13701 }
13702 }
13703 else if (cu->producer
13704 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13705 {
13706 /* The IBM XLC compiler does not provide direct indication
13707 of the containing type, but the vtable pointer is
13708 always named __vfp. */
13709
13710 int i;
13711
13712 for (i = TYPE_NFIELDS (type) - 1;
13713 i >= TYPE_N_BASECLASSES (type);
13714 --i)
13715 {
13716 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13717 {
13718 set_type_vptr_fieldno (type, i);
13719 set_type_vptr_basetype (type, type);
13720 break;
13721 }
13722 }
13723 }
13724 }
13725
13726 /* Copy fi.typedef_field_list linked list elements content into the
13727 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13728 if (fi.typedef_field_list)
13729 {
13730 int i = fi.typedef_field_list_count;
13731
13732 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13733 TYPE_TYPEDEF_FIELD_ARRAY (type)
13734 = ((struct typedef_field *)
13735 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13736 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13737
13738 /* Reverse the list order to keep the debug info elements order. */
13739 while (--i >= 0)
13740 {
13741 struct typedef_field *dest, *src;
13742
13743 dest = &TYPE_TYPEDEF_FIELD (type, i);
13744 src = &fi.typedef_field_list->field;
13745 fi.typedef_field_list = fi.typedef_field_list->next;
13746 *dest = *src;
13747 }
13748 }
13749
13750 do_cleanups (back_to);
13751 }
13752
13753 quirk_gcc_member_function_pointer (type, objfile);
13754
13755 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13756 snapshots) has been known to create a die giving a declaration
13757 for a class that has, as a child, a die giving a definition for a
13758 nested class. So we have to process our children even if the
13759 current die is a declaration. Normally, of course, a declaration
13760 won't have any children at all. */
13761
13762 child_die = die->child;
13763
13764 while (child_die != NULL && child_die->tag)
13765 {
13766 if (child_die->tag == DW_TAG_member
13767 || child_die->tag == DW_TAG_variable
13768 || child_die->tag == DW_TAG_inheritance
13769 || child_die->tag == DW_TAG_template_value_param
13770 || child_die->tag == DW_TAG_template_type_param)
13771 {
13772 /* Do nothing. */
13773 }
13774 else
13775 process_die (child_die, cu);
13776
13777 child_die = sibling_die (child_die);
13778 }
13779
13780 /* Do not consider external references. According to the DWARF standard,
13781 these DIEs are identified by the fact that they have no byte_size
13782 attribute, and a declaration attribute. */
13783 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13784 || !die_is_declaration (die, cu))
13785 new_symbol (die, type, cu);
13786 }
13787
13788 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13789 update TYPE using some information only available in DIE's children. */
13790
13791 static void
13792 update_enumeration_type_from_children (struct die_info *die,
13793 struct type *type,
13794 struct dwarf2_cu *cu)
13795 {
13796 struct obstack obstack;
13797 struct die_info *child_die;
13798 int unsigned_enum = 1;
13799 int flag_enum = 1;
13800 ULONGEST mask = 0;
13801 struct cleanup *old_chain;
13802
13803 obstack_init (&obstack);
13804 old_chain = make_cleanup_obstack_free (&obstack);
13805
13806 for (child_die = die->child;
13807 child_die != NULL && child_die->tag;
13808 child_die = sibling_die (child_die))
13809 {
13810 struct attribute *attr;
13811 LONGEST value;
13812 const gdb_byte *bytes;
13813 struct dwarf2_locexpr_baton *baton;
13814 const char *name;
13815
13816 if (child_die->tag != DW_TAG_enumerator)
13817 continue;
13818
13819 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13820 if (attr == NULL)
13821 continue;
13822
13823 name = dwarf2_name (child_die, cu);
13824 if (name == NULL)
13825 name = "<anonymous enumerator>";
13826
13827 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13828 &value, &bytes, &baton);
13829 if (value < 0)
13830 {
13831 unsigned_enum = 0;
13832 flag_enum = 0;
13833 }
13834 else if ((mask & value) != 0)
13835 flag_enum = 0;
13836 else
13837 mask |= value;
13838
13839 /* If we already know that the enum type is neither unsigned, nor
13840 a flag type, no need to look at the rest of the enumerates. */
13841 if (!unsigned_enum && !flag_enum)
13842 break;
13843 }
13844
13845 if (unsigned_enum)
13846 TYPE_UNSIGNED (type) = 1;
13847 if (flag_enum)
13848 TYPE_FLAG_ENUM (type) = 1;
13849
13850 do_cleanups (old_chain);
13851 }
13852
13853 /* Given a DW_AT_enumeration_type die, set its type. We do not
13854 complete the type's fields yet, or create any symbols. */
13855
13856 static struct type *
13857 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13858 {
13859 struct objfile *objfile = cu->objfile;
13860 struct type *type;
13861 struct attribute *attr;
13862 const char *name;
13863
13864 /* If the definition of this type lives in .debug_types, read that type.
13865 Don't follow DW_AT_specification though, that will take us back up
13866 the chain and we want to go down. */
13867 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13868 if (attr)
13869 {
13870 type = get_DW_AT_signature_type (die, attr, cu);
13871
13872 /* The type's CU may not be the same as CU.
13873 Ensure TYPE is recorded with CU in die_type_hash. */
13874 return set_die_type (die, type, cu);
13875 }
13876
13877 type = alloc_type (objfile);
13878
13879 TYPE_CODE (type) = TYPE_CODE_ENUM;
13880 name = dwarf2_full_name (NULL, die, cu);
13881 if (name != NULL)
13882 TYPE_TAG_NAME (type) = name;
13883
13884 attr = dwarf2_attr (die, DW_AT_type, cu);
13885 if (attr != NULL)
13886 {
13887 struct type *underlying_type = die_type (die, cu);
13888
13889 TYPE_TARGET_TYPE (type) = underlying_type;
13890 }
13891
13892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13893 if (attr)
13894 {
13895 TYPE_LENGTH (type) = DW_UNSND (attr);
13896 }
13897 else
13898 {
13899 TYPE_LENGTH (type) = 0;
13900 }
13901
13902 /* The enumeration DIE can be incomplete. In Ada, any type can be
13903 declared as private in the package spec, and then defined only
13904 inside the package body. Such types are known as Taft Amendment
13905 Types. When another package uses such a type, an incomplete DIE
13906 may be generated by the compiler. */
13907 if (die_is_declaration (die, cu))
13908 TYPE_STUB (type) = 1;
13909
13910 /* Finish the creation of this type by using the enum's children.
13911 We must call this even when the underlying type has been provided
13912 so that we can determine if we're looking at a "flag" enum. */
13913 update_enumeration_type_from_children (die, type, cu);
13914
13915 /* If this type has an underlying type that is not a stub, then we
13916 may use its attributes. We always use the "unsigned" attribute
13917 in this situation, because ordinarily we guess whether the type
13918 is unsigned -- but the guess can be wrong and the underlying type
13919 can tell us the reality. However, we defer to a local size
13920 attribute if one exists, because this lets the compiler override
13921 the underlying type if needed. */
13922 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13923 {
13924 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13925 if (TYPE_LENGTH (type) == 0)
13926 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13927 }
13928
13929 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13930
13931 return set_die_type (die, type, cu);
13932 }
13933
13934 /* Given a pointer to a die which begins an enumeration, process all
13935 the dies that define the members of the enumeration, and create the
13936 symbol for the enumeration type.
13937
13938 NOTE: We reverse the order of the element list. */
13939
13940 static void
13941 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13942 {
13943 struct type *this_type;
13944
13945 this_type = get_die_type (die, cu);
13946 if (this_type == NULL)
13947 this_type = read_enumeration_type (die, cu);
13948
13949 if (die->child != NULL)
13950 {
13951 struct die_info *child_die;
13952 struct symbol *sym;
13953 struct field *fields = NULL;
13954 int num_fields = 0;
13955 const char *name;
13956
13957 child_die = die->child;
13958 while (child_die && child_die->tag)
13959 {
13960 if (child_die->tag != DW_TAG_enumerator)
13961 {
13962 process_die (child_die, cu);
13963 }
13964 else
13965 {
13966 name = dwarf2_name (child_die, cu);
13967 if (name)
13968 {
13969 sym = new_symbol (child_die, this_type, cu);
13970
13971 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13972 {
13973 fields = (struct field *)
13974 xrealloc (fields,
13975 (num_fields + DW_FIELD_ALLOC_CHUNK)
13976 * sizeof (struct field));
13977 }
13978
13979 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13980 FIELD_TYPE (fields[num_fields]) = NULL;
13981 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13982 FIELD_BITSIZE (fields[num_fields]) = 0;
13983
13984 num_fields++;
13985 }
13986 }
13987
13988 child_die = sibling_die (child_die);
13989 }
13990
13991 if (num_fields)
13992 {
13993 TYPE_NFIELDS (this_type) = num_fields;
13994 TYPE_FIELDS (this_type) = (struct field *)
13995 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13996 memcpy (TYPE_FIELDS (this_type), fields,
13997 sizeof (struct field) * num_fields);
13998 xfree (fields);
13999 }
14000 }
14001
14002 /* If we are reading an enum from a .debug_types unit, and the enum
14003 is a declaration, and the enum is not the signatured type in the
14004 unit, then we do not want to add a symbol for it. Adding a
14005 symbol would in some cases obscure the true definition of the
14006 enum, giving users an incomplete type when the definition is
14007 actually available. Note that we do not want to do this for all
14008 enums which are just declarations, because C++0x allows forward
14009 enum declarations. */
14010 if (cu->per_cu->is_debug_types
14011 && die_is_declaration (die, cu))
14012 {
14013 struct signatured_type *sig_type;
14014
14015 sig_type = (struct signatured_type *) cu->per_cu;
14016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14017 if (sig_type->type_offset_in_section != die->sect_off)
14018 return;
14019 }
14020
14021 new_symbol (die, this_type, cu);
14022 }
14023
14024 /* Extract all information from a DW_TAG_array_type DIE and put it in
14025 the DIE's type field. For now, this only handles one dimensional
14026 arrays. */
14027
14028 static struct type *
14029 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14030 {
14031 struct objfile *objfile = cu->objfile;
14032 struct die_info *child_die;
14033 struct type *type;
14034 struct type *element_type, *range_type, *index_type;
14035 struct type **range_types = NULL;
14036 struct attribute *attr;
14037 int ndim = 0;
14038 struct cleanup *back_to;
14039 const char *name;
14040 unsigned int bit_stride = 0;
14041
14042 element_type = die_type (die, cu);
14043
14044 /* The die_type call above may have already set the type for this DIE. */
14045 type = get_die_type (die, cu);
14046 if (type)
14047 return type;
14048
14049 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14050 if (attr != NULL)
14051 bit_stride = DW_UNSND (attr) * 8;
14052
14053 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14054 if (attr != NULL)
14055 bit_stride = DW_UNSND (attr);
14056
14057 /* Irix 6.2 native cc creates array types without children for
14058 arrays with unspecified length. */
14059 if (die->child == NULL)
14060 {
14061 index_type = objfile_type (objfile)->builtin_int;
14062 range_type = create_static_range_type (NULL, index_type, 0, -1);
14063 type = create_array_type_with_stride (NULL, element_type, range_type,
14064 bit_stride);
14065 return set_die_type (die, type, cu);
14066 }
14067
14068 back_to = make_cleanup (null_cleanup, NULL);
14069 child_die = die->child;
14070 while (child_die && child_die->tag)
14071 {
14072 if (child_die->tag == DW_TAG_subrange_type)
14073 {
14074 struct type *child_type = read_type_die (child_die, cu);
14075
14076 if (child_type != NULL)
14077 {
14078 /* The range type was succesfully read. Save it for the
14079 array type creation. */
14080 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14081 {
14082 range_types = (struct type **)
14083 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14084 * sizeof (struct type *));
14085 if (ndim == 0)
14086 make_cleanup (free_current_contents, &range_types);
14087 }
14088 range_types[ndim++] = child_type;
14089 }
14090 }
14091 child_die = sibling_die (child_die);
14092 }
14093
14094 /* Dwarf2 dimensions are output from left to right, create the
14095 necessary array types in backwards order. */
14096
14097 type = element_type;
14098
14099 if (read_array_order (die, cu) == DW_ORD_col_major)
14100 {
14101 int i = 0;
14102
14103 while (i < ndim)
14104 type = create_array_type_with_stride (NULL, type, range_types[i++],
14105 bit_stride);
14106 }
14107 else
14108 {
14109 while (ndim-- > 0)
14110 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14111 bit_stride);
14112 }
14113
14114 /* Understand Dwarf2 support for vector types (like they occur on
14115 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14116 array type. This is not part of the Dwarf2/3 standard yet, but a
14117 custom vendor extension. The main difference between a regular
14118 array and the vector variant is that vectors are passed by value
14119 to functions. */
14120 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14121 if (attr)
14122 make_vector_type (type);
14123
14124 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14125 implementation may choose to implement triple vectors using this
14126 attribute. */
14127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14128 if (attr)
14129 {
14130 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14131 TYPE_LENGTH (type) = DW_UNSND (attr);
14132 else
14133 complaint (&symfile_complaints,
14134 _("DW_AT_byte_size for array type smaller "
14135 "than the total size of elements"));
14136 }
14137
14138 name = dwarf2_name (die, cu);
14139 if (name)
14140 TYPE_NAME (type) = name;
14141
14142 /* Install the type in the die. */
14143 set_die_type (die, type, cu);
14144
14145 /* set_die_type should be already done. */
14146 set_descriptive_type (type, die, cu);
14147
14148 do_cleanups (back_to);
14149
14150 return type;
14151 }
14152
14153 static enum dwarf_array_dim_ordering
14154 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14155 {
14156 struct attribute *attr;
14157
14158 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14159
14160 if (attr)
14161 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14162
14163 /* GNU F77 is a special case, as at 08/2004 array type info is the
14164 opposite order to the dwarf2 specification, but data is still
14165 laid out as per normal fortran.
14166
14167 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14168 version checking. */
14169
14170 if (cu->language == language_fortran
14171 && cu->producer && strstr (cu->producer, "GNU F77"))
14172 {
14173 return DW_ORD_row_major;
14174 }
14175
14176 switch (cu->language_defn->la_array_ordering)
14177 {
14178 case array_column_major:
14179 return DW_ORD_col_major;
14180 case array_row_major:
14181 default:
14182 return DW_ORD_row_major;
14183 };
14184 }
14185
14186 /* Extract all information from a DW_TAG_set_type DIE and put it in
14187 the DIE's type field. */
14188
14189 static struct type *
14190 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14191 {
14192 struct type *domain_type, *set_type;
14193 struct attribute *attr;
14194
14195 domain_type = die_type (die, cu);
14196
14197 /* The die_type call above may have already set the type for this DIE. */
14198 set_type = get_die_type (die, cu);
14199 if (set_type)
14200 return set_type;
14201
14202 set_type = create_set_type (NULL, domain_type);
14203
14204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14205 if (attr)
14206 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14207
14208 return set_die_type (die, set_type, cu);
14209 }
14210
14211 /* A helper for read_common_block that creates a locexpr baton.
14212 SYM is the symbol which we are marking as computed.
14213 COMMON_DIE is the DIE for the common block.
14214 COMMON_LOC is the location expression attribute for the common
14215 block itself.
14216 MEMBER_LOC is the location expression attribute for the particular
14217 member of the common block that we are processing.
14218 CU is the CU from which the above come. */
14219
14220 static void
14221 mark_common_block_symbol_computed (struct symbol *sym,
14222 struct die_info *common_die,
14223 struct attribute *common_loc,
14224 struct attribute *member_loc,
14225 struct dwarf2_cu *cu)
14226 {
14227 struct objfile *objfile = dwarf2_per_objfile->objfile;
14228 struct dwarf2_locexpr_baton *baton;
14229 gdb_byte *ptr;
14230 unsigned int cu_off;
14231 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14232 LONGEST offset = 0;
14233
14234 gdb_assert (common_loc && member_loc);
14235 gdb_assert (attr_form_is_block (common_loc));
14236 gdb_assert (attr_form_is_block (member_loc)
14237 || attr_form_is_constant (member_loc));
14238
14239 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14240 baton->per_cu = cu->per_cu;
14241 gdb_assert (baton->per_cu);
14242
14243 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14244
14245 if (attr_form_is_constant (member_loc))
14246 {
14247 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14248 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14249 }
14250 else
14251 baton->size += DW_BLOCK (member_loc)->size;
14252
14253 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14254 baton->data = ptr;
14255
14256 *ptr++ = DW_OP_call4;
14257 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14258 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14259 ptr += 4;
14260
14261 if (attr_form_is_constant (member_loc))
14262 {
14263 *ptr++ = DW_OP_addr;
14264 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14265 ptr += cu->header.addr_size;
14266 }
14267 else
14268 {
14269 /* We have to copy the data here, because DW_OP_call4 will only
14270 use a DW_AT_location attribute. */
14271 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14272 ptr += DW_BLOCK (member_loc)->size;
14273 }
14274
14275 *ptr++ = DW_OP_plus;
14276 gdb_assert (ptr - baton->data == baton->size);
14277
14278 SYMBOL_LOCATION_BATON (sym) = baton;
14279 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14280 }
14281
14282 /* Create appropriate locally-scoped variables for all the
14283 DW_TAG_common_block entries. Also create a struct common_block
14284 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14285 is used to sepate the common blocks name namespace from regular
14286 variable names. */
14287
14288 static void
14289 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14290 {
14291 struct attribute *attr;
14292
14293 attr = dwarf2_attr (die, DW_AT_location, cu);
14294 if (attr)
14295 {
14296 /* Support the .debug_loc offsets. */
14297 if (attr_form_is_block (attr))
14298 {
14299 /* Ok. */
14300 }
14301 else if (attr_form_is_section_offset (attr))
14302 {
14303 dwarf2_complex_location_expr_complaint ();
14304 attr = NULL;
14305 }
14306 else
14307 {
14308 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14309 "common block member");
14310 attr = NULL;
14311 }
14312 }
14313
14314 if (die->child != NULL)
14315 {
14316 struct objfile *objfile = cu->objfile;
14317 struct die_info *child_die;
14318 size_t n_entries = 0, size;
14319 struct common_block *common_block;
14320 struct symbol *sym;
14321
14322 for (child_die = die->child;
14323 child_die && child_die->tag;
14324 child_die = sibling_die (child_die))
14325 ++n_entries;
14326
14327 size = (sizeof (struct common_block)
14328 + (n_entries - 1) * sizeof (struct symbol *));
14329 common_block
14330 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14331 size);
14332 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14333 common_block->n_entries = 0;
14334
14335 for (child_die = die->child;
14336 child_die && child_die->tag;
14337 child_die = sibling_die (child_die))
14338 {
14339 /* Create the symbol in the DW_TAG_common_block block in the current
14340 symbol scope. */
14341 sym = new_symbol (child_die, NULL, cu);
14342 if (sym != NULL)
14343 {
14344 struct attribute *member_loc;
14345
14346 common_block->contents[common_block->n_entries++] = sym;
14347
14348 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14349 cu);
14350 if (member_loc)
14351 {
14352 /* GDB has handled this for a long time, but it is
14353 not specified by DWARF. It seems to have been
14354 emitted by gfortran at least as recently as:
14355 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14356 complaint (&symfile_complaints,
14357 _("Variable in common block has "
14358 "DW_AT_data_member_location "
14359 "- DIE at 0x%x [in module %s]"),
14360 to_underlying (child_die->sect_off),
14361 objfile_name (cu->objfile));
14362
14363 if (attr_form_is_section_offset (member_loc))
14364 dwarf2_complex_location_expr_complaint ();
14365 else if (attr_form_is_constant (member_loc)
14366 || attr_form_is_block (member_loc))
14367 {
14368 if (attr)
14369 mark_common_block_symbol_computed (sym, die, attr,
14370 member_loc, cu);
14371 }
14372 else
14373 dwarf2_complex_location_expr_complaint ();
14374 }
14375 }
14376 }
14377
14378 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14379 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14380 }
14381 }
14382
14383 /* Create a type for a C++ namespace. */
14384
14385 static struct type *
14386 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14387 {
14388 struct objfile *objfile = cu->objfile;
14389 const char *previous_prefix, *name;
14390 int is_anonymous;
14391 struct type *type;
14392
14393 /* For extensions, reuse the type of the original namespace. */
14394 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14395 {
14396 struct die_info *ext_die;
14397 struct dwarf2_cu *ext_cu = cu;
14398
14399 ext_die = dwarf2_extension (die, &ext_cu);
14400 type = read_type_die (ext_die, ext_cu);
14401
14402 /* EXT_CU may not be the same as CU.
14403 Ensure TYPE is recorded with CU in die_type_hash. */
14404 return set_die_type (die, type, cu);
14405 }
14406
14407 name = namespace_name (die, &is_anonymous, cu);
14408
14409 /* Now build the name of the current namespace. */
14410
14411 previous_prefix = determine_prefix (die, cu);
14412 if (previous_prefix[0] != '\0')
14413 name = typename_concat (&objfile->objfile_obstack,
14414 previous_prefix, name, 0, cu);
14415
14416 /* Create the type. */
14417 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14418 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14419
14420 return set_die_type (die, type, cu);
14421 }
14422
14423 /* Read a namespace scope. */
14424
14425 static void
14426 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14427 {
14428 struct objfile *objfile = cu->objfile;
14429 int is_anonymous;
14430
14431 /* Add a symbol associated to this if we haven't seen the namespace
14432 before. Also, add a using directive if it's an anonymous
14433 namespace. */
14434
14435 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14436 {
14437 struct type *type;
14438
14439 type = read_type_die (die, cu);
14440 new_symbol (die, type, cu);
14441
14442 namespace_name (die, &is_anonymous, cu);
14443 if (is_anonymous)
14444 {
14445 const char *previous_prefix = determine_prefix (die, cu);
14446
14447 add_using_directive (using_directives (cu->language),
14448 previous_prefix, TYPE_NAME (type), NULL,
14449 NULL, NULL, 0, &objfile->objfile_obstack);
14450 }
14451 }
14452
14453 if (die->child != NULL)
14454 {
14455 struct die_info *child_die = die->child;
14456
14457 while (child_die && child_die->tag)
14458 {
14459 process_die (child_die, cu);
14460 child_die = sibling_die (child_die);
14461 }
14462 }
14463 }
14464
14465 /* Read a Fortran module as type. This DIE can be only a declaration used for
14466 imported module. Still we need that type as local Fortran "use ... only"
14467 declaration imports depend on the created type in determine_prefix. */
14468
14469 static struct type *
14470 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14471 {
14472 struct objfile *objfile = cu->objfile;
14473 const char *module_name;
14474 struct type *type;
14475
14476 module_name = dwarf2_name (die, cu);
14477 if (!module_name)
14478 complaint (&symfile_complaints,
14479 _("DW_TAG_module has no name, offset 0x%x"),
14480 to_underlying (die->sect_off));
14481 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14482
14483 /* determine_prefix uses TYPE_TAG_NAME. */
14484 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14485
14486 return set_die_type (die, type, cu);
14487 }
14488
14489 /* Read a Fortran module. */
14490
14491 static void
14492 read_module (struct die_info *die, struct dwarf2_cu *cu)
14493 {
14494 struct die_info *child_die = die->child;
14495 struct type *type;
14496
14497 type = read_type_die (die, cu);
14498 new_symbol (die, type, cu);
14499
14500 while (child_die && child_die->tag)
14501 {
14502 process_die (child_die, cu);
14503 child_die = sibling_die (child_die);
14504 }
14505 }
14506
14507 /* Return the name of the namespace represented by DIE. Set
14508 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14509 namespace. */
14510
14511 static const char *
14512 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14513 {
14514 struct die_info *current_die;
14515 const char *name = NULL;
14516
14517 /* Loop through the extensions until we find a name. */
14518
14519 for (current_die = die;
14520 current_die != NULL;
14521 current_die = dwarf2_extension (die, &cu))
14522 {
14523 /* We don't use dwarf2_name here so that we can detect the absence
14524 of a name -> anonymous namespace. */
14525 name = dwarf2_string_attr (die, DW_AT_name, cu);
14526
14527 if (name != NULL)
14528 break;
14529 }
14530
14531 /* Is it an anonymous namespace? */
14532
14533 *is_anonymous = (name == NULL);
14534 if (*is_anonymous)
14535 name = CP_ANONYMOUS_NAMESPACE_STR;
14536
14537 return name;
14538 }
14539
14540 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14541 the user defined type vector. */
14542
14543 static struct type *
14544 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14545 {
14546 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14547 struct comp_unit_head *cu_header = &cu->header;
14548 struct type *type;
14549 struct attribute *attr_byte_size;
14550 struct attribute *attr_address_class;
14551 int byte_size, addr_class;
14552 struct type *target_type;
14553
14554 target_type = die_type (die, cu);
14555
14556 /* The die_type call above may have already set the type for this DIE. */
14557 type = get_die_type (die, cu);
14558 if (type)
14559 return type;
14560
14561 type = lookup_pointer_type (target_type);
14562
14563 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14564 if (attr_byte_size)
14565 byte_size = DW_UNSND (attr_byte_size);
14566 else
14567 byte_size = cu_header->addr_size;
14568
14569 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14570 if (attr_address_class)
14571 addr_class = DW_UNSND (attr_address_class);
14572 else
14573 addr_class = DW_ADDR_none;
14574
14575 /* If the pointer size or address class is different than the
14576 default, create a type variant marked as such and set the
14577 length accordingly. */
14578 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14579 {
14580 if (gdbarch_address_class_type_flags_p (gdbarch))
14581 {
14582 int type_flags;
14583
14584 type_flags = gdbarch_address_class_type_flags
14585 (gdbarch, byte_size, addr_class);
14586 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14587 == 0);
14588 type = make_type_with_address_space (type, type_flags);
14589 }
14590 else if (TYPE_LENGTH (type) != byte_size)
14591 {
14592 complaint (&symfile_complaints,
14593 _("invalid pointer size %d"), byte_size);
14594 }
14595 else
14596 {
14597 /* Should we also complain about unhandled address classes? */
14598 }
14599 }
14600
14601 TYPE_LENGTH (type) = byte_size;
14602 return set_die_type (die, type, cu);
14603 }
14604
14605 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14606 the user defined type vector. */
14607
14608 static struct type *
14609 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14610 {
14611 struct type *type;
14612 struct type *to_type;
14613 struct type *domain;
14614
14615 to_type = die_type (die, cu);
14616 domain = die_containing_type (die, cu);
14617
14618 /* The calls above may have already set the type for this DIE. */
14619 type = get_die_type (die, cu);
14620 if (type)
14621 return type;
14622
14623 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14624 type = lookup_methodptr_type (to_type);
14625 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14626 {
14627 struct type *new_type = alloc_type (cu->objfile);
14628
14629 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14630 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14631 TYPE_VARARGS (to_type));
14632 type = lookup_methodptr_type (new_type);
14633 }
14634 else
14635 type = lookup_memberptr_type (to_type, domain);
14636
14637 return set_die_type (die, type, cu);
14638 }
14639
14640 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14641 the user defined type vector. */
14642
14643 static struct type *
14644 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14645 enum type_code refcode)
14646 {
14647 struct comp_unit_head *cu_header = &cu->header;
14648 struct type *type, *target_type;
14649 struct attribute *attr;
14650
14651 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14652
14653 target_type = die_type (die, cu);
14654
14655 /* The die_type call above may have already set the type for this DIE. */
14656 type = get_die_type (die, cu);
14657 if (type)
14658 return type;
14659
14660 type = lookup_reference_type (target_type, refcode);
14661 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14662 if (attr)
14663 {
14664 TYPE_LENGTH (type) = DW_UNSND (attr);
14665 }
14666 else
14667 {
14668 TYPE_LENGTH (type) = cu_header->addr_size;
14669 }
14670 return set_die_type (die, type, cu);
14671 }
14672
14673 /* Add the given cv-qualifiers to the element type of the array. GCC
14674 outputs DWARF type qualifiers that apply to an array, not the
14675 element type. But GDB relies on the array element type to carry
14676 the cv-qualifiers. This mimics section 6.7.3 of the C99
14677 specification. */
14678
14679 static struct type *
14680 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14681 struct type *base_type, int cnst, int voltl)
14682 {
14683 struct type *el_type, *inner_array;
14684
14685 base_type = copy_type (base_type);
14686 inner_array = base_type;
14687
14688 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14689 {
14690 TYPE_TARGET_TYPE (inner_array) =
14691 copy_type (TYPE_TARGET_TYPE (inner_array));
14692 inner_array = TYPE_TARGET_TYPE (inner_array);
14693 }
14694
14695 el_type = TYPE_TARGET_TYPE (inner_array);
14696 cnst |= TYPE_CONST (el_type);
14697 voltl |= TYPE_VOLATILE (el_type);
14698 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14699
14700 return set_die_type (die, base_type, cu);
14701 }
14702
14703 static struct type *
14704 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14705 {
14706 struct type *base_type, *cv_type;
14707
14708 base_type = die_type (die, cu);
14709
14710 /* The die_type call above may have already set the type for this DIE. */
14711 cv_type = get_die_type (die, cu);
14712 if (cv_type)
14713 return cv_type;
14714
14715 /* In case the const qualifier is applied to an array type, the element type
14716 is so qualified, not the array type (section 6.7.3 of C99). */
14717 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14718 return add_array_cv_type (die, cu, base_type, 1, 0);
14719
14720 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14721 return set_die_type (die, cv_type, cu);
14722 }
14723
14724 static struct type *
14725 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14726 {
14727 struct type *base_type, *cv_type;
14728
14729 base_type = die_type (die, cu);
14730
14731 /* The die_type call above may have already set the type for this DIE. */
14732 cv_type = get_die_type (die, cu);
14733 if (cv_type)
14734 return cv_type;
14735
14736 /* In case the volatile qualifier is applied to an array type, the
14737 element type is so qualified, not the array type (section 6.7.3
14738 of C99). */
14739 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14740 return add_array_cv_type (die, cu, base_type, 0, 1);
14741
14742 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14743 return set_die_type (die, cv_type, cu);
14744 }
14745
14746 /* Handle DW_TAG_restrict_type. */
14747
14748 static struct type *
14749 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14750 {
14751 struct type *base_type, *cv_type;
14752
14753 base_type = die_type (die, cu);
14754
14755 /* The die_type call above may have already set the type for this DIE. */
14756 cv_type = get_die_type (die, cu);
14757 if (cv_type)
14758 return cv_type;
14759
14760 cv_type = make_restrict_type (base_type);
14761 return set_die_type (die, cv_type, cu);
14762 }
14763
14764 /* Handle DW_TAG_atomic_type. */
14765
14766 static struct type *
14767 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14768 {
14769 struct type *base_type, *cv_type;
14770
14771 base_type = die_type (die, cu);
14772
14773 /* The die_type call above may have already set the type for this DIE. */
14774 cv_type = get_die_type (die, cu);
14775 if (cv_type)
14776 return cv_type;
14777
14778 cv_type = make_atomic_type (base_type);
14779 return set_die_type (die, cv_type, cu);
14780 }
14781
14782 /* Extract all information from a DW_TAG_string_type DIE and add to
14783 the user defined type vector. It isn't really a user defined type,
14784 but it behaves like one, with other DIE's using an AT_user_def_type
14785 attribute to reference it. */
14786
14787 static struct type *
14788 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14789 {
14790 struct objfile *objfile = cu->objfile;
14791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14792 struct type *type, *range_type, *index_type, *char_type;
14793 struct attribute *attr;
14794 unsigned int length;
14795
14796 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14797 if (attr)
14798 {
14799 length = DW_UNSND (attr);
14800 }
14801 else
14802 {
14803 /* Check for the DW_AT_byte_size attribute. */
14804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14805 if (attr)
14806 {
14807 length = DW_UNSND (attr);
14808 }
14809 else
14810 {
14811 length = 1;
14812 }
14813 }
14814
14815 index_type = objfile_type (objfile)->builtin_int;
14816 range_type = create_static_range_type (NULL, index_type, 1, length);
14817 char_type = language_string_char_type (cu->language_defn, gdbarch);
14818 type = create_string_type (NULL, char_type, range_type);
14819
14820 return set_die_type (die, type, cu);
14821 }
14822
14823 /* Assuming that DIE corresponds to a function, returns nonzero
14824 if the function is prototyped. */
14825
14826 static int
14827 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14828 {
14829 struct attribute *attr;
14830
14831 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14832 if (attr && (DW_UNSND (attr) != 0))
14833 return 1;
14834
14835 /* The DWARF standard implies that the DW_AT_prototyped attribute
14836 is only meaninful for C, but the concept also extends to other
14837 languages that allow unprototyped functions (Eg: Objective C).
14838 For all other languages, assume that functions are always
14839 prototyped. */
14840 if (cu->language != language_c
14841 && cu->language != language_objc
14842 && cu->language != language_opencl)
14843 return 1;
14844
14845 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14846 prototyped and unprototyped functions; default to prototyped,
14847 since that is more common in modern code (and RealView warns
14848 about unprototyped functions). */
14849 if (producer_is_realview (cu->producer))
14850 return 1;
14851
14852 return 0;
14853 }
14854
14855 /* Handle DIES due to C code like:
14856
14857 struct foo
14858 {
14859 int (*funcp)(int a, long l);
14860 int b;
14861 };
14862
14863 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14864
14865 static struct type *
14866 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14867 {
14868 struct objfile *objfile = cu->objfile;
14869 struct type *type; /* Type that this function returns. */
14870 struct type *ftype; /* Function that returns above type. */
14871 struct attribute *attr;
14872
14873 type = die_type (die, cu);
14874
14875 /* The die_type call above may have already set the type for this DIE. */
14876 ftype = get_die_type (die, cu);
14877 if (ftype)
14878 return ftype;
14879
14880 ftype = lookup_function_type (type);
14881
14882 if (prototyped_function_p (die, cu))
14883 TYPE_PROTOTYPED (ftype) = 1;
14884
14885 /* Store the calling convention in the type if it's available in
14886 the subroutine die. Otherwise set the calling convention to
14887 the default value DW_CC_normal. */
14888 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14889 if (attr)
14890 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14891 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14892 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14893 else
14894 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14895
14896 /* Record whether the function returns normally to its caller or not
14897 if the DWARF producer set that information. */
14898 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14899 if (attr && (DW_UNSND (attr) != 0))
14900 TYPE_NO_RETURN (ftype) = 1;
14901
14902 /* We need to add the subroutine type to the die immediately so
14903 we don't infinitely recurse when dealing with parameters
14904 declared as the same subroutine type. */
14905 set_die_type (die, ftype, cu);
14906
14907 if (die->child != NULL)
14908 {
14909 struct type *void_type = objfile_type (objfile)->builtin_void;
14910 struct die_info *child_die;
14911 int nparams, iparams;
14912
14913 /* Count the number of parameters.
14914 FIXME: GDB currently ignores vararg functions, but knows about
14915 vararg member functions. */
14916 nparams = 0;
14917 child_die = die->child;
14918 while (child_die && child_die->tag)
14919 {
14920 if (child_die->tag == DW_TAG_formal_parameter)
14921 nparams++;
14922 else if (child_die->tag == DW_TAG_unspecified_parameters)
14923 TYPE_VARARGS (ftype) = 1;
14924 child_die = sibling_die (child_die);
14925 }
14926
14927 /* Allocate storage for parameters and fill them in. */
14928 TYPE_NFIELDS (ftype) = nparams;
14929 TYPE_FIELDS (ftype) = (struct field *)
14930 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14931
14932 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14933 even if we error out during the parameters reading below. */
14934 for (iparams = 0; iparams < nparams; iparams++)
14935 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14936
14937 iparams = 0;
14938 child_die = die->child;
14939 while (child_die && child_die->tag)
14940 {
14941 if (child_die->tag == DW_TAG_formal_parameter)
14942 {
14943 struct type *arg_type;
14944
14945 /* DWARF version 2 has no clean way to discern C++
14946 static and non-static member functions. G++ helps
14947 GDB by marking the first parameter for non-static
14948 member functions (which is the this pointer) as
14949 artificial. We pass this information to
14950 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14951
14952 DWARF version 3 added DW_AT_object_pointer, which GCC
14953 4.5 does not yet generate. */
14954 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14955 if (attr)
14956 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14957 else
14958 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14959 arg_type = die_type (child_die, cu);
14960
14961 /* RealView does not mark THIS as const, which the testsuite
14962 expects. GCC marks THIS as const in method definitions,
14963 but not in the class specifications (GCC PR 43053). */
14964 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14965 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14966 {
14967 int is_this = 0;
14968 struct dwarf2_cu *arg_cu = cu;
14969 const char *name = dwarf2_name (child_die, cu);
14970
14971 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14972 if (attr)
14973 {
14974 /* If the compiler emits this, use it. */
14975 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14976 is_this = 1;
14977 }
14978 else if (name && strcmp (name, "this") == 0)
14979 /* Function definitions will have the argument names. */
14980 is_this = 1;
14981 else if (name == NULL && iparams == 0)
14982 /* Declarations may not have the names, so like
14983 elsewhere in GDB, assume an artificial first
14984 argument is "this". */
14985 is_this = 1;
14986
14987 if (is_this)
14988 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14989 arg_type, 0);
14990 }
14991
14992 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14993 iparams++;
14994 }
14995 child_die = sibling_die (child_die);
14996 }
14997 }
14998
14999 return ftype;
15000 }
15001
15002 static struct type *
15003 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15004 {
15005 struct objfile *objfile = cu->objfile;
15006 const char *name = NULL;
15007 struct type *this_type, *target_type;
15008
15009 name = dwarf2_full_name (NULL, die, cu);
15010 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15011 TYPE_TARGET_STUB (this_type) = 1;
15012 set_die_type (die, this_type, cu);
15013 target_type = die_type (die, cu);
15014 if (target_type != this_type)
15015 TYPE_TARGET_TYPE (this_type) = target_type;
15016 else
15017 {
15018 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15019 spec and cause infinite loops in GDB. */
15020 complaint (&symfile_complaints,
15021 _("Self-referential DW_TAG_typedef "
15022 "- DIE at 0x%x [in module %s]"),
15023 to_underlying (die->sect_off), objfile_name (objfile));
15024 TYPE_TARGET_TYPE (this_type) = NULL;
15025 }
15026 return this_type;
15027 }
15028
15029 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15030 (which may be different from NAME) to the architecture back-end to allow
15031 it to guess the correct format if necessary. */
15032
15033 static struct type *
15034 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15035 const char *name_hint)
15036 {
15037 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15038 const struct floatformat **format;
15039 struct type *type;
15040
15041 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15042 if (format)
15043 type = init_float_type (objfile, bits, name, format);
15044 else
15045 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15046
15047 return type;
15048 }
15049
15050 /* Find a representation of a given base type and install
15051 it in the TYPE field of the die. */
15052
15053 static struct type *
15054 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15055 {
15056 struct objfile *objfile = cu->objfile;
15057 struct type *type;
15058 struct attribute *attr;
15059 int encoding = 0, bits = 0;
15060 const char *name;
15061
15062 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15063 if (attr)
15064 {
15065 encoding = DW_UNSND (attr);
15066 }
15067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15068 if (attr)
15069 {
15070 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15071 }
15072 name = dwarf2_name (die, cu);
15073 if (!name)
15074 {
15075 complaint (&symfile_complaints,
15076 _("DW_AT_name missing from DW_TAG_base_type"));
15077 }
15078
15079 switch (encoding)
15080 {
15081 case DW_ATE_address:
15082 /* Turn DW_ATE_address into a void * pointer. */
15083 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15084 type = init_pointer_type (objfile, bits, name, type);
15085 break;
15086 case DW_ATE_boolean:
15087 type = init_boolean_type (objfile, bits, 1, name);
15088 break;
15089 case DW_ATE_complex_float:
15090 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15091 type = init_complex_type (objfile, name, type);
15092 break;
15093 case DW_ATE_decimal_float:
15094 type = init_decfloat_type (objfile, bits, name);
15095 break;
15096 case DW_ATE_float:
15097 type = dwarf2_init_float_type (objfile, bits, name, name);
15098 break;
15099 case DW_ATE_signed:
15100 type = init_integer_type (objfile, bits, 0, name);
15101 break;
15102 case DW_ATE_unsigned:
15103 if (cu->language == language_fortran
15104 && name
15105 && startswith (name, "character("))
15106 type = init_character_type (objfile, bits, 1, name);
15107 else
15108 type = init_integer_type (objfile, bits, 1, name);
15109 break;
15110 case DW_ATE_signed_char:
15111 if (cu->language == language_ada || cu->language == language_m2
15112 || cu->language == language_pascal
15113 || cu->language == language_fortran)
15114 type = init_character_type (objfile, bits, 0, name);
15115 else
15116 type = init_integer_type (objfile, bits, 0, name);
15117 break;
15118 case DW_ATE_unsigned_char:
15119 if (cu->language == language_ada || cu->language == language_m2
15120 || cu->language == language_pascal
15121 || cu->language == language_fortran
15122 || cu->language == language_rust)
15123 type = init_character_type (objfile, bits, 1, name);
15124 else
15125 type = init_integer_type (objfile, bits, 1, name);
15126 break;
15127 case DW_ATE_UTF:
15128 {
15129 gdbarch *arch = get_objfile_arch (objfile);
15130
15131 if (bits == 16)
15132 type = builtin_type (arch)->builtin_char16;
15133 else if (bits == 32)
15134 type = builtin_type (arch)->builtin_char32;
15135 else
15136 {
15137 complaint (&symfile_complaints,
15138 _("unsupported DW_ATE_UTF bit size: '%d'"),
15139 bits);
15140 type = init_integer_type (objfile, bits, 1, name);
15141 }
15142 return set_die_type (die, type, cu);
15143 }
15144 break;
15145
15146 default:
15147 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15148 dwarf_type_encoding_name (encoding));
15149 type = init_type (objfile, TYPE_CODE_ERROR,
15150 bits / TARGET_CHAR_BIT, name);
15151 break;
15152 }
15153
15154 if (name && strcmp (name, "char") == 0)
15155 TYPE_NOSIGN (type) = 1;
15156
15157 return set_die_type (die, type, cu);
15158 }
15159
15160 /* Parse dwarf attribute if it's a block, reference or constant and put the
15161 resulting value of the attribute into struct bound_prop.
15162 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15163
15164 static int
15165 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15166 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15167 {
15168 struct dwarf2_property_baton *baton;
15169 struct obstack *obstack = &cu->objfile->objfile_obstack;
15170
15171 if (attr == NULL || prop == NULL)
15172 return 0;
15173
15174 if (attr_form_is_block (attr))
15175 {
15176 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15177 baton->referenced_type = NULL;
15178 baton->locexpr.per_cu = cu->per_cu;
15179 baton->locexpr.size = DW_BLOCK (attr)->size;
15180 baton->locexpr.data = DW_BLOCK (attr)->data;
15181 prop->data.baton = baton;
15182 prop->kind = PROP_LOCEXPR;
15183 gdb_assert (prop->data.baton != NULL);
15184 }
15185 else if (attr_form_is_ref (attr))
15186 {
15187 struct dwarf2_cu *target_cu = cu;
15188 struct die_info *target_die;
15189 struct attribute *target_attr;
15190
15191 target_die = follow_die_ref (die, attr, &target_cu);
15192 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15193 if (target_attr == NULL)
15194 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15195 target_cu);
15196 if (target_attr == NULL)
15197 return 0;
15198
15199 switch (target_attr->name)
15200 {
15201 case DW_AT_location:
15202 if (attr_form_is_section_offset (target_attr))
15203 {
15204 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15205 baton->referenced_type = die_type (target_die, target_cu);
15206 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15207 prop->data.baton = baton;
15208 prop->kind = PROP_LOCLIST;
15209 gdb_assert (prop->data.baton != NULL);
15210 }
15211 else if (attr_form_is_block (target_attr))
15212 {
15213 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15214 baton->referenced_type = die_type (target_die, target_cu);
15215 baton->locexpr.per_cu = cu->per_cu;
15216 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15217 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15218 prop->data.baton = baton;
15219 prop->kind = PROP_LOCEXPR;
15220 gdb_assert (prop->data.baton != NULL);
15221 }
15222 else
15223 {
15224 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15225 "dynamic property");
15226 return 0;
15227 }
15228 break;
15229 case DW_AT_data_member_location:
15230 {
15231 LONGEST offset;
15232
15233 if (!handle_data_member_location (target_die, target_cu,
15234 &offset))
15235 return 0;
15236
15237 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15238 baton->referenced_type = read_type_die (target_die->parent,
15239 target_cu);
15240 baton->offset_info.offset = offset;
15241 baton->offset_info.type = die_type (target_die, target_cu);
15242 prop->data.baton = baton;
15243 prop->kind = PROP_ADDR_OFFSET;
15244 break;
15245 }
15246 }
15247 }
15248 else if (attr_form_is_constant (attr))
15249 {
15250 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15251 prop->kind = PROP_CONST;
15252 }
15253 else
15254 {
15255 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15256 dwarf2_name (die, cu));
15257 return 0;
15258 }
15259
15260 return 1;
15261 }
15262
15263 /* Read the given DW_AT_subrange DIE. */
15264
15265 static struct type *
15266 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15267 {
15268 struct type *base_type, *orig_base_type;
15269 struct type *range_type;
15270 struct attribute *attr;
15271 struct dynamic_prop low, high;
15272 int low_default_is_valid;
15273 int high_bound_is_count = 0;
15274 const char *name;
15275 LONGEST negative_mask;
15276
15277 orig_base_type = die_type (die, cu);
15278 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15279 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15280 creating the range type, but we use the result of check_typedef
15281 when examining properties of the type. */
15282 base_type = check_typedef (orig_base_type);
15283
15284 /* The die_type call above may have already set the type for this DIE. */
15285 range_type = get_die_type (die, cu);
15286 if (range_type)
15287 return range_type;
15288
15289 low.kind = PROP_CONST;
15290 high.kind = PROP_CONST;
15291 high.data.const_val = 0;
15292
15293 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15294 omitting DW_AT_lower_bound. */
15295 switch (cu->language)
15296 {
15297 case language_c:
15298 case language_cplus:
15299 low.data.const_val = 0;
15300 low_default_is_valid = 1;
15301 break;
15302 case language_fortran:
15303 low.data.const_val = 1;
15304 low_default_is_valid = 1;
15305 break;
15306 case language_d:
15307 case language_objc:
15308 case language_rust:
15309 low.data.const_val = 0;
15310 low_default_is_valid = (cu->header.version >= 4);
15311 break;
15312 case language_ada:
15313 case language_m2:
15314 case language_pascal:
15315 low.data.const_val = 1;
15316 low_default_is_valid = (cu->header.version >= 4);
15317 break;
15318 default:
15319 low.data.const_val = 0;
15320 low_default_is_valid = 0;
15321 break;
15322 }
15323
15324 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15325 if (attr)
15326 attr_to_dynamic_prop (attr, die, cu, &low);
15327 else if (!low_default_is_valid)
15328 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15329 "- DIE at 0x%x [in module %s]"),
15330 to_underlying (die->sect_off), objfile_name (cu->objfile));
15331
15332 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15333 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15334 {
15335 attr = dwarf2_attr (die, DW_AT_count, cu);
15336 if (attr_to_dynamic_prop (attr, die, cu, &high))
15337 {
15338 /* If bounds are constant do the final calculation here. */
15339 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15340 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15341 else
15342 high_bound_is_count = 1;
15343 }
15344 }
15345
15346 /* Dwarf-2 specifications explicitly allows to create subrange types
15347 without specifying a base type.
15348 In that case, the base type must be set to the type of
15349 the lower bound, upper bound or count, in that order, if any of these
15350 three attributes references an object that has a type.
15351 If no base type is found, the Dwarf-2 specifications say that
15352 a signed integer type of size equal to the size of an address should
15353 be used.
15354 For the following C code: `extern char gdb_int [];'
15355 GCC produces an empty range DIE.
15356 FIXME: muller/2010-05-28: Possible references to object for low bound,
15357 high bound or count are not yet handled by this code. */
15358 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15359 {
15360 struct objfile *objfile = cu->objfile;
15361 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15362 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15363 struct type *int_type = objfile_type (objfile)->builtin_int;
15364
15365 /* Test "int", "long int", and "long long int" objfile types,
15366 and select the first one having a size above or equal to the
15367 architecture address size. */
15368 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15369 base_type = int_type;
15370 else
15371 {
15372 int_type = objfile_type (objfile)->builtin_long;
15373 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15374 base_type = int_type;
15375 else
15376 {
15377 int_type = objfile_type (objfile)->builtin_long_long;
15378 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15379 base_type = int_type;
15380 }
15381 }
15382 }
15383
15384 /* Normally, the DWARF producers are expected to use a signed
15385 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15386 But this is unfortunately not always the case, as witnessed
15387 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15388 is used instead. To work around that ambiguity, we treat
15389 the bounds as signed, and thus sign-extend their values, when
15390 the base type is signed. */
15391 negative_mask =
15392 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15393 if (low.kind == PROP_CONST
15394 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15395 low.data.const_val |= negative_mask;
15396 if (high.kind == PROP_CONST
15397 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15398 high.data.const_val |= negative_mask;
15399
15400 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15401
15402 if (high_bound_is_count)
15403 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15404
15405 /* Ada expects an empty array on no boundary attributes. */
15406 if (attr == NULL && cu->language != language_ada)
15407 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15408
15409 name = dwarf2_name (die, cu);
15410 if (name)
15411 TYPE_NAME (range_type) = name;
15412
15413 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15414 if (attr)
15415 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15416
15417 set_die_type (die, range_type, cu);
15418
15419 /* set_die_type should be already done. */
15420 set_descriptive_type (range_type, die, cu);
15421
15422 return range_type;
15423 }
15424
15425 static struct type *
15426 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15427 {
15428 struct type *type;
15429
15430 /* For now, we only support the C meaning of an unspecified type: void. */
15431
15432 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15433 TYPE_NAME (type) = dwarf2_name (die, cu);
15434
15435 return set_die_type (die, type, cu);
15436 }
15437
15438 /* Read a single die and all its descendents. Set the die's sibling
15439 field to NULL; set other fields in the die correctly, and set all
15440 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15441 location of the info_ptr after reading all of those dies. PARENT
15442 is the parent of the die in question. */
15443
15444 static struct die_info *
15445 read_die_and_children (const struct die_reader_specs *reader,
15446 const gdb_byte *info_ptr,
15447 const gdb_byte **new_info_ptr,
15448 struct die_info *parent)
15449 {
15450 struct die_info *die;
15451 const gdb_byte *cur_ptr;
15452 int has_children;
15453
15454 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15455 if (die == NULL)
15456 {
15457 *new_info_ptr = cur_ptr;
15458 return NULL;
15459 }
15460 store_in_ref_table (die, reader->cu);
15461
15462 if (has_children)
15463 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15464 else
15465 {
15466 die->child = NULL;
15467 *new_info_ptr = cur_ptr;
15468 }
15469
15470 die->sibling = NULL;
15471 die->parent = parent;
15472 return die;
15473 }
15474
15475 /* Read a die, all of its descendents, and all of its siblings; set
15476 all of the fields of all of the dies correctly. Arguments are as
15477 in read_die_and_children. */
15478
15479 static struct die_info *
15480 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15481 const gdb_byte *info_ptr,
15482 const gdb_byte **new_info_ptr,
15483 struct die_info *parent)
15484 {
15485 struct die_info *first_die, *last_sibling;
15486 const gdb_byte *cur_ptr;
15487
15488 cur_ptr = info_ptr;
15489 first_die = last_sibling = NULL;
15490
15491 while (1)
15492 {
15493 struct die_info *die
15494 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15495
15496 if (die == NULL)
15497 {
15498 *new_info_ptr = cur_ptr;
15499 return first_die;
15500 }
15501
15502 if (!first_die)
15503 first_die = die;
15504 else
15505 last_sibling->sibling = die;
15506
15507 last_sibling = die;
15508 }
15509 }
15510
15511 /* Read a die, all of its descendents, and all of its siblings; set
15512 all of the fields of all of the dies correctly. Arguments are as
15513 in read_die_and_children.
15514 This the main entry point for reading a DIE and all its children. */
15515
15516 static struct die_info *
15517 read_die_and_siblings (const struct die_reader_specs *reader,
15518 const gdb_byte *info_ptr,
15519 const gdb_byte **new_info_ptr,
15520 struct die_info *parent)
15521 {
15522 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15523 new_info_ptr, parent);
15524
15525 if (dwarf_die_debug)
15526 {
15527 fprintf_unfiltered (gdb_stdlog,
15528 "Read die from %s@0x%x of %s:\n",
15529 get_section_name (reader->die_section),
15530 (unsigned) (info_ptr - reader->die_section->buffer),
15531 bfd_get_filename (reader->abfd));
15532 dump_die (die, dwarf_die_debug);
15533 }
15534
15535 return die;
15536 }
15537
15538 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15539 attributes.
15540 The caller is responsible for filling in the extra attributes
15541 and updating (*DIEP)->num_attrs.
15542 Set DIEP to point to a newly allocated die with its information,
15543 except for its child, sibling, and parent fields.
15544 Set HAS_CHILDREN to tell whether the die has children or not. */
15545
15546 static const gdb_byte *
15547 read_full_die_1 (const struct die_reader_specs *reader,
15548 struct die_info **diep, const gdb_byte *info_ptr,
15549 int *has_children, int num_extra_attrs)
15550 {
15551 unsigned int abbrev_number, bytes_read, i;
15552 struct abbrev_info *abbrev;
15553 struct die_info *die;
15554 struct dwarf2_cu *cu = reader->cu;
15555 bfd *abfd = reader->abfd;
15556
15557 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15558 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15559 info_ptr += bytes_read;
15560 if (!abbrev_number)
15561 {
15562 *diep = NULL;
15563 *has_children = 0;
15564 return info_ptr;
15565 }
15566
15567 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15568 if (!abbrev)
15569 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15570 abbrev_number,
15571 bfd_get_filename (abfd));
15572
15573 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15574 die->sect_off = sect_off;
15575 die->tag = abbrev->tag;
15576 die->abbrev = abbrev_number;
15577
15578 /* Make the result usable.
15579 The caller needs to update num_attrs after adding the extra
15580 attributes. */
15581 die->num_attrs = abbrev->num_attrs;
15582
15583 for (i = 0; i < abbrev->num_attrs; ++i)
15584 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15585 info_ptr);
15586
15587 *diep = die;
15588 *has_children = abbrev->has_children;
15589 return info_ptr;
15590 }
15591
15592 /* Read a die and all its attributes.
15593 Set DIEP to point to a newly allocated die with its information,
15594 except for its child, sibling, and parent fields.
15595 Set HAS_CHILDREN to tell whether the die has children or not. */
15596
15597 static const gdb_byte *
15598 read_full_die (const struct die_reader_specs *reader,
15599 struct die_info **diep, const gdb_byte *info_ptr,
15600 int *has_children)
15601 {
15602 const gdb_byte *result;
15603
15604 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15605
15606 if (dwarf_die_debug)
15607 {
15608 fprintf_unfiltered (gdb_stdlog,
15609 "Read die from %s@0x%x of %s:\n",
15610 get_section_name (reader->die_section),
15611 (unsigned) (info_ptr - reader->die_section->buffer),
15612 bfd_get_filename (reader->abfd));
15613 dump_die (*diep, dwarf_die_debug);
15614 }
15615
15616 return result;
15617 }
15618 \f
15619 /* Abbreviation tables.
15620
15621 In DWARF version 2, the description of the debugging information is
15622 stored in a separate .debug_abbrev section. Before we read any
15623 dies from a section we read in all abbreviations and install them
15624 in a hash table. */
15625
15626 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15627
15628 static struct abbrev_info *
15629 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15630 {
15631 struct abbrev_info *abbrev;
15632
15633 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15634 memset (abbrev, 0, sizeof (struct abbrev_info));
15635
15636 return abbrev;
15637 }
15638
15639 /* Add an abbreviation to the table. */
15640
15641 static void
15642 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15643 unsigned int abbrev_number,
15644 struct abbrev_info *abbrev)
15645 {
15646 unsigned int hash_number;
15647
15648 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15649 abbrev->next = abbrev_table->abbrevs[hash_number];
15650 abbrev_table->abbrevs[hash_number] = abbrev;
15651 }
15652
15653 /* Look up an abbrev in the table.
15654 Returns NULL if the abbrev is not found. */
15655
15656 static struct abbrev_info *
15657 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15658 unsigned int abbrev_number)
15659 {
15660 unsigned int hash_number;
15661 struct abbrev_info *abbrev;
15662
15663 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15664 abbrev = abbrev_table->abbrevs[hash_number];
15665
15666 while (abbrev)
15667 {
15668 if (abbrev->number == abbrev_number)
15669 return abbrev;
15670 abbrev = abbrev->next;
15671 }
15672 return NULL;
15673 }
15674
15675 /* Read in an abbrev table. */
15676
15677 static struct abbrev_table *
15678 abbrev_table_read_table (struct dwarf2_section_info *section,
15679 sect_offset sect_off)
15680 {
15681 struct objfile *objfile = dwarf2_per_objfile->objfile;
15682 bfd *abfd = get_section_bfd_owner (section);
15683 struct abbrev_table *abbrev_table;
15684 const gdb_byte *abbrev_ptr;
15685 struct abbrev_info *cur_abbrev;
15686 unsigned int abbrev_number, bytes_read, abbrev_name;
15687 unsigned int abbrev_form;
15688 struct attr_abbrev *cur_attrs;
15689 unsigned int allocated_attrs;
15690
15691 abbrev_table = XNEW (struct abbrev_table);
15692 abbrev_table->sect_off = sect_off;
15693 obstack_init (&abbrev_table->abbrev_obstack);
15694 abbrev_table->abbrevs =
15695 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15696 ABBREV_HASH_SIZE);
15697 memset (abbrev_table->abbrevs, 0,
15698 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15699
15700 dwarf2_read_section (objfile, section);
15701 abbrev_ptr = section->buffer + to_underlying (sect_off);
15702 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15703 abbrev_ptr += bytes_read;
15704
15705 allocated_attrs = ATTR_ALLOC_CHUNK;
15706 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15707
15708 /* Loop until we reach an abbrev number of 0. */
15709 while (abbrev_number)
15710 {
15711 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15712
15713 /* read in abbrev header */
15714 cur_abbrev->number = abbrev_number;
15715 cur_abbrev->tag
15716 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15717 abbrev_ptr += bytes_read;
15718 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15719 abbrev_ptr += 1;
15720
15721 /* now read in declarations */
15722 for (;;)
15723 {
15724 LONGEST implicit_const;
15725
15726 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15727 abbrev_ptr += bytes_read;
15728 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15729 abbrev_ptr += bytes_read;
15730 if (abbrev_form == DW_FORM_implicit_const)
15731 {
15732 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15733 &bytes_read);
15734 abbrev_ptr += bytes_read;
15735 }
15736 else
15737 {
15738 /* Initialize it due to a false compiler warning. */
15739 implicit_const = -1;
15740 }
15741
15742 if (abbrev_name == 0)
15743 break;
15744
15745 if (cur_abbrev->num_attrs == allocated_attrs)
15746 {
15747 allocated_attrs += ATTR_ALLOC_CHUNK;
15748 cur_attrs
15749 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15750 }
15751
15752 cur_attrs[cur_abbrev->num_attrs].name
15753 = (enum dwarf_attribute) abbrev_name;
15754 cur_attrs[cur_abbrev->num_attrs].form
15755 = (enum dwarf_form) abbrev_form;
15756 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15757 ++cur_abbrev->num_attrs;
15758 }
15759
15760 cur_abbrev->attrs =
15761 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15762 cur_abbrev->num_attrs);
15763 memcpy (cur_abbrev->attrs, cur_attrs,
15764 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15765
15766 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15767
15768 /* Get next abbreviation.
15769 Under Irix6 the abbreviations for a compilation unit are not
15770 always properly terminated with an abbrev number of 0.
15771 Exit loop if we encounter an abbreviation which we have
15772 already read (which means we are about to read the abbreviations
15773 for the next compile unit) or if the end of the abbreviation
15774 table is reached. */
15775 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15776 break;
15777 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15778 abbrev_ptr += bytes_read;
15779 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15780 break;
15781 }
15782
15783 xfree (cur_attrs);
15784 return abbrev_table;
15785 }
15786
15787 /* Free the resources held by ABBREV_TABLE. */
15788
15789 static void
15790 abbrev_table_free (struct abbrev_table *abbrev_table)
15791 {
15792 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15793 xfree (abbrev_table);
15794 }
15795
15796 /* Same as abbrev_table_free but as a cleanup.
15797 We pass in a pointer to the pointer to the table so that we can
15798 set the pointer to NULL when we're done. It also simplifies
15799 build_type_psymtabs_1. */
15800
15801 static void
15802 abbrev_table_free_cleanup (void *table_ptr)
15803 {
15804 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15805
15806 if (*abbrev_table_ptr != NULL)
15807 abbrev_table_free (*abbrev_table_ptr);
15808 *abbrev_table_ptr = NULL;
15809 }
15810
15811 /* Read the abbrev table for CU from ABBREV_SECTION. */
15812
15813 static void
15814 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15815 struct dwarf2_section_info *abbrev_section)
15816 {
15817 cu->abbrev_table =
15818 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15819 }
15820
15821 /* Release the memory used by the abbrev table for a compilation unit. */
15822
15823 static void
15824 dwarf2_free_abbrev_table (void *ptr_to_cu)
15825 {
15826 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15827
15828 if (cu->abbrev_table != NULL)
15829 abbrev_table_free (cu->abbrev_table);
15830 /* Set this to NULL so that we SEGV if we try to read it later,
15831 and also because free_comp_unit verifies this is NULL. */
15832 cu->abbrev_table = NULL;
15833 }
15834 \f
15835 /* Returns nonzero if TAG represents a type that we might generate a partial
15836 symbol for. */
15837
15838 static int
15839 is_type_tag_for_partial (int tag)
15840 {
15841 switch (tag)
15842 {
15843 #if 0
15844 /* Some types that would be reasonable to generate partial symbols for,
15845 that we don't at present. */
15846 case DW_TAG_array_type:
15847 case DW_TAG_file_type:
15848 case DW_TAG_ptr_to_member_type:
15849 case DW_TAG_set_type:
15850 case DW_TAG_string_type:
15851 case DW_TAG_subroutine_type:
15852 #endif
15853 case DW_TAG_base_type:
15854 case DW_TAG_class_type:
15855 case DW_TAG_interface_type:
15856 case DW_TAG_enumeration_type:
15857 case DW_TAG_structure_type:
15858 case DW_TAG_subrange_type:
15859 case DW_TAG_typedef:
15860 case DW_TAG_union_type:
15861 return 1;
15862 default:
15863 return 0;
15864 }
15865 }
15866
15867 /* Load all DIEs that are interesting for partial symbols into memory. */
15868
15869 static struct partial_die_info *
15870 load_partial_dies (const struct die_reader_specs *reader,
15871 const gdb_byte *info_ptr, int building_psymtab)
15872 {
15873 struct dwarf2_cu *cu = reader->cu;
15874 struct objfile *objfile = cu->objfile;
15875 struct partial_die_info *part_die;
15876 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15877 struct abbrev_info *abbrev;
15878 unsigned int bytes_read;
15879 unsigned int load_all = 0;
15880 int nesting_level = 1;
15881
15882 parent_die = NULL;
15883 last_die = NULL;
15884
15885 gdb_assert (cu->per_cu != NULL);
15886 if (cu->per_cu->load_all_dies)
15887 load_all = 1;
15888
15889 cu->partial_dies
15890 = htab_create_alloc_ex (cu->header.length / 12,
15891 partial_die_hash,
15892 partial_die_eq,
15893 NULL,
15894 &cu->comp_unit_obstack,
15895 hashtab_obstack_allocate,
15896 dummy_obstack_deallocate);
15897
15898 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15899
15900 while (1)
15901 {
15902 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15903
15904 /* A NULL abbrev means the end of a series of children. */
15905 if (abbrev == NULL)
15906 {
15907 if (--nesting_level == 0)
15908 {
15909 /* PART_DIE was probably the last thing allocated on the
15910 comp_unit_obstack, so we could call obstack_free
15911 here. We don't do that because the waste is small,
15912 and will be cleaned up when we're done with this
15913 compilation unit. This way, we're also more robust
15914 against other users of the comp_unit_obstack. */
15915 return first_die;
15916 }
15917 info_ptr += bytes_read;
15918 last_die = parent_die;
15919 parent_die = parent_die->die_parent;
15920 continue;
15921 }
15922
15923 /* Check for template arguments. We never save these; if
15924 they're seen, we just mark the parent, and go on our way. */
15925 if (parent_die != NULL
15926 && cu->language == language_cplus
15927 && (abbrev->tag == DW_TAG_template_type_param
15928 || abbrev->tag == DW_TAG_template_value_param))
15929 {
15930 parent_die->has_template_arguments = 1;
15931
15932 if (!load_all)
15933 {
15934 /* We don't need a partial DIE for the template argument. */
15935 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15936 continue;
15937 }
15938 }
15939
15940 /* We only recurse into c++ subprograms looking for template arguments.
15941 Skip their other children. */
15942 if (!load_all
15943 && cu->language == language_cplus
15944 && parent_die != NULL
15945 && parent_die->tag == DW_TAG_subprogram)
15946 {
15947 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15948 continue;
15949 }
15950
15951 /* Check whether this DIE is interesting enough to save. Normally
15952 we would not be interested in members here, but there may be
15953 later variables referencing them via DW_AT_specification (for
15954 static members). */
15955 if (!load_all
15956 && !is_type_tag_for_partial (abbrev->tag)
15957 && abbrev->tag != DW_TAG_constant
15958 && abbrev->tag != DW_TAG_enumerator
15959 && abbrev->tag != DW_TAG_subprogram
15960 && abbrev->tag != DW_TAG_lexical_block
15961 && abbrev->tag != DW_TAG_variable
15962 && abbrev->tag != DW_TAG_namespace
15963 && abbrev->tag != DW_TAG_module
15964 && abbrev->tag != DW_TAG_member
15965 && abbrev->tag != DW_TAG_imported_unit
15966 && abbrev->tag != DW_TAG_imported_declaration)
15967 {
15968 /* Otherwise we skip to the next sibling, if any. */
15969 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15970 continue;
15971 }
15972
15973 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15974 info_ptr);
15975
15976 /* This two-pass algorithm for processing partial symbols has a
15977 high cost in cache pressure. Thus, handle some simple cases
15978 here which cover the majority of C partial symbols. DIEs
15979 which neither have specification tags in them, nor could have
15980 specification tags elsewhere pointing at them, can simply be
15981 processed and discarded.
15982
15983 This segment is also optional; scan_partial_symbols and
15984 add_partial_symbol will handle these DIEs if we chain
15985 them in normally. When compilers which do not emit large
15986 quantities of duplicate debug information are more common,
15987 this code can probably be removed. */
15988
15989 /* Any complete simple types at the top level (pretty much all
15990 of them, for a language without namespaces), can be processed
15991 directly. */
15992 if (parent_die == NULL
15993 && part_die->has_specification == 0
15994 && part_die->is_declaration == 0
15995 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15996 || part_die->tag == DW_TAG_base_type
15997 || part_die->tag == DW_TAG_subrange_type))
15998 {
15999 if (building_psymtab && part_die->name != NULL)
16000 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16001 VAR_DOMAIN, LOC_TYPEDEF,
16002 &objfile->static_psymbols,
16003 0, cu->language, objfile);
16004 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16005 continue;
16006 }
16007
16008 /* The exception for DW_TAG_typedef with has_children above is
16009 a workaround of GCC PR debug/47510. In the case of this complaint
16010 type_name_no_tag_or_error will error on such types later.
16011
16012 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16013 it could not find the child DIEs referenced later, this is checked
16014 above. In correct DWARF DW_TAG_typedef should have no children. */
16015
16016 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16017 complaint (&symfile_complaints,
16018 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16019 "- DIE at 0x%x [in module %s]"),
16020 to_underlying (part_die->sect_off), objfile_name (objfile));
16021
16022 /* If we're at the second level, and we're an enumerator, and
16023 our parent has no specification (meaning possibly lives in a
16024 namespace elsewhere), then we can add the partial symbol now
16025 instead of queueing it. */
16026 if (part_die->tag == DW_TAG_enumerator
16027 && parent_die != NULL
16028 && parent_die->die_parent == NULL
16029 && parent_die->tag == DW_TAG_enumeration_type
16030 && parent_die->has_specification == 0)
16031 {
16032 if (part_die->name == NULL)
16033 complaint (&symfile_complaints,
16034 _("malformed enumerator DIE ignored"));
16035 else if (building_psymtab)
16036 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16037 VAR_DOMAIN, LOC_CONST,
16038 cu->language == language_cplus
16039 ? &objfile->global_psymbols
16040 : &objfile->static_psymbols,
16041 0, cu->language, objfile);
16042
16043 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16044 continue;
16045 }
16046
16047 /* We'll save this DIE so link it in. */
16048 part_die->die_parent = parent_die;
16049 part_die->die_sibling = NULL;
16050 part_die->die_child = NULL;
16051
16052 if (last_die && last_die == parent_die)
16053 last_die->die_child = part_die;
16054 else if (last_die)
16055 last_die->die_sibling = part_die;
16056
16057 last_die = part_die;
16058
16059 if (first_die == NULL)
16060 first_die = part_die;
16061
16062 /* Maybe add the DIE to the hash table. Not all DIEs that we
16063 find interesting need to be in the hash table, because we
16064 also have the parent/sibling/child chains; only those that we
16065 might refer to by offset later during partial symbol reading.
16066
16067 For now this means things that might have be the target of a
16068 DW_AT_specification, DW_AT_abstract_origin, or
16069 DW_AT_extension. DW_AT_extension will refer only to
16070 namespaces; DW_AT_abstract_origin refers to functions (and
16071 many things under the function DIE, but we do not recurse
16072 into function DIEs during partial symbol reading) and
16073 possibly variables as well; DW_AT_specification refers to
16074 declarations. Declarations ought to have the DW_AT_declaration
16075 flag. It happens that GCC forgets to put it in sometimes, but
16076 only for functions, not for types.
16077
16078 Adding more things than necessary to the hash table is harmless
16079 except for the performance cost. Adding too few will result in
16080 wasted time in find_partial_die, when we reread the compilation
16081 unit with load_all_dies set. */
16082
16083 if (load_all
16084 || abbrev->tag == DW_TAG_constant
16085 || abbrev->tag == DW_TAG_subprogram
16086 || abbrev->tag == DW_TAG_variable
16087 || abbrev->tag == DW_TAG_namespace
16088 || part_die->is_declaration)
16089 {
16090 void **slot;
16091
16092 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16093 to_underlying (part_die->sect_off),
16094 INSERT);
16095 *slot = part_die;
16096 }
16097
16098 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16099
16100 /* For some DIEs we want to follow their children (if any). For C
16101 we have no reason to follow the children of structures; for other
16102 languages we have to, so that we can get at method physnames
16103 to infer fully qualified class names, for DW_AT_specification,
16104 and for C++ template arguments. For C++, we also look one level
16105 inside functions to find template arguments (if the name of the
16106 function does not already contain the template arguments).
16107
16108 For Ada, we need to scan the children of subprograms and lexical
16109 blocks as well because Ada allows the definition of nested
16110 entities that could be interesting for the debugger, such as
16111 nested subprograms for instance. */
16112 if (last_die->has_children
16113 && (load_all
16114 || last_die->tag == DW_TAG_namespace
16115 || last_die->tag == DW_TAG_module
16116 || last_die->tag == DW_TAG_enumeration_type
16117 || (cu->language == language_cplus
16118 && last_die->tag == DW_TAG_subprogram
16119 && (last_die->name == NULL
16120 || strchr (last_die->name, '<') == NULL))
16121 || (cu->language != language_c
16122 && (last_die->tag == DW_TAG_class_type
16123 || last_die->tag == DW_TAG_interface_type
16124 || last_die->tag == DW_TAG_structure_type
16125 || last_die->tag == DW_TAG_union_type))
16126 || (cu->language == language_ada
16127 && (last_die->tag == DW_TAG_subprogram
16128 || last_die->tag == DW_TAG_lexical_block))))
16129 {
16130 nesting_level++;
16131 parent_die = last_die;
16132 continue;
16133 }
16134
16135 /* Otherwise we skip to the next sibling, if any. */
16136 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16137
16138 /* Back to the top, do it again. */
16139 }
16140 }
16141
16142 /* Read a minimal amount of information into the minimal die structure. */
16143
16144 static const gdb_byte *
16145 read_partial_die (const struct die_reader_specs *reader,
16146 struct partial_die_info *part_die,
16147 struct abbrev_info *abbrev, unsigned int abbrev_len,
16148 const gdb_byte *info_ptr)
16149 {
16150 struct dwarf2_cu *cu = reader->cu;
16151 struct objfile *objfile = cu->objfile;
16152 const gdb_byte *buffer = reader->buffer;
16153 unsigned int i;
16154 struct attribute attr;
16155 int has_low_pc_attr = 0;
16156 int has_high_pc_attr = 0;
16157 int high_pc_relative = 0;
16158
16159 memset (part_die, 0, sizeof (struct partial_die_info));
16160
16161 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16162
16163 info_ptr += abbrev_len;
16164
16165 if (abbrev == NULL)
16166 return info_ptr;
16167
16168 part_die->tag = abbrev->tag;
16169 part_die->has_children = abbrev->has_children;
16170
16171 for (i = 0; i < abbrev->num_attrs; ++i)
16172 {
16173 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16174
16175 /* Store the data if it is of an attribute we want to keep in a
16176 partial symbol table. */
16177 switch (attr.name)
16178 {
16179 case DW_AT_name:
16180 switch (part_die->tag)
16181 {
16182 case DW_TAG_compile_unit:
16183 case DW_TAG_partial_unit:
16184 case DW_TAG_type_unit:
16185 /* Compilation units have a DW_AT_name that is a filename, not
16186 a source language identifier. */
16187 case DW_TAG_enumeration_type:
16188 case DW_TAG_enumerator:
16189 /* These tags always have simple identifiers already; no need
16190 to canonicalize them. */
16191 part_die->name = DW_STRING (&attr);
16192 break;
16193 default:
16194 part_die->name
16195 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16196 &objfile->per_bfd->storage_obstack);
16197 break;
16198 }
16199 break;
16200 case DW_AT_linkage_name:
16201 case DW_AT_MIPS_linkage_name:
16202 /* Note that both forms of linkage name might appear. We
16203 assume they will be the same, and we only store the last
16204 one we see. */
16205 if (cu->language == language_ada)
16206 part_die->name = DW_STRING (&attr);
16207 part_die->linkage_name = DW_STRING (&attr);
16208 break;
16209 case DW_AT_low_pc:
16210 has_low_pc_attr = 1;
16211 part_die->lowpc = attr_value_as_address (&attr);
16212 break;
16213 case DW_AT_high_pc:
16214 has_high_pc_attr = 1;
16215 part_die->highpc = attr_value_as_address (&attr);
16216 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16217 high_pc_relative = 1;
16218 break;
16219 case DW_AT_location:
16220 /* Support the .debug_loc offsets. */
16221 if (attr_form_is_block (&attr))
16222 {
16223 part_die->d.locdesc = DW_BLOCK (&attr);
16224 }
16225 else if (attr_form_is_section_offset (&attr))
16226 {
16227 dwarf2_complex_location_expr_complaint ();
16228 }
16229 else
16230 {
16231 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16232 "partial symbol information");
16233 }
16234 break;
16235 case DW_AT_external:
16236 part_die->is_external = DW_UNSND (&attr);
16237 break;
16238 case DW_AT_declaration:
16239 part_die->is_declaration = DW_UNSND (&attr);
16240 break;
16241 case DW_AT_type:
16242 part_die->has_type = 1;
16243 break;
16244 case DW_AT_abstract_origin:
16245 case DW_AT_specification:
16246 case DW_AT_extension:
16247 part_die->has_specification = 1;
16248 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16249 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16250 || cu->per_cu->is_dwz);
16251 break;
16252 case DW_AT_sibling:
16253 /* Ignore absolute siblings, they might point outside of
16254 the current compile unit. */
16255 if (attr.form == DW_FORM_ref_addr)
16256 complaint (&symfile_complaints,
16257 _("ignoring absolute DW_AT_sibling"));
16258 else
16259 {
16260 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16261 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16262
16263 if (sibling_ptr < info_ptr)
16264 complaint (&symfile_complaints,
16265 _("DW_AT_sibling points backwards"));
16266 else if (sibling_ptr > reader->buffer_end)
16267 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16268 else
16269 part_die->sibling = sibling_ptr;
16270 }
16271 break;
16272 case DW_AT_byte_size:
16273 part_die->has_byte_size = 1;
16274 break;
16275 case DW_AT_const_value:
16276 part_die->has_const_value = 1;
16277 break;
16278 case DW_AT_calling_convention:
16279 /* DWARF doesn't provide a way to identify a program's source-level
16280 entry point. DW_AT_calling_convention attributes are only meant
16281 to describe functions' calling conventions.
16282
16283 However, because it's a necessary piece of information in
16284 Fortran, and before DWARF 4 DW_CC_program was the only
16285 piece of debugging information whose definition refers to
16286 a 'main program' at all, several compilers marked Fortran
16287 main programs with DW_CC_program --- even when those
16288 functions use the standard calling conventions.
16289
16290 Although DWARF now specifies a way to provide this
16291 information, we support this practice for backward
16292 compatibility. */
16293 if (DW_UNSND (&attr) == DW_CC_program
16294 && cu->language == language_fortran)
16295 part_die->main_subprogram = 1;
16296 break;
16297 case DW_AT_inline:
16298 if (DW_UNSND (&attr) == DW_INL_inlined
16299 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16300 part_die->may_be_inlined = 1;
16301 break;
16302
16303 case DW_AT_import:
16304 if (part_die->tag == DW_TAG_imported_unit)
16305 {
16306 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16307 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16308 || cu->per_cu->is_dwz);
16309 }
16310 break;
16311
16312 case DW_AT_main_subprogram:
16313 part_die->main_subprogram = DW_UNSND (&attr);
16314 break;
16315
16316 default:
16317 break;
16318 }
16319 }
16320
16321 if (high_pc_relative)
16322 part_die->highpc += part_die->lowpc;
16323
16324 if (has_low_pc_attr && has_high_pc_attr)
16325 {
16326 /* When using the GNU linker, .gnu.linkonce. sections are used to
16327 eliminate duplicate copies of functions and vtables and such.
16328 The linker will arbitrarily choose one and discard the others.
16329 The AT_*_pc values for such functions refer to local labels in
16330 these sections. If the section from that file was discarded, the
16331 labels are not in the output, so the relocs get a value of 0.
16332 If this is a discarded function, mark the pc bounds as invalid,
16333 so that GDB will ignore it. */
16334 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16335 {
16336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16337
16338 complaint (&symfile_complaints,
16339 _("DW_AT_low_pc %s is zero "
16340 "for DIE at 0x%x [in module %s]"),
16341 paddress (gdbarch, part_die->lowpc),
16342 to_underlying (part_die->sect_off), objfile_name (objfile));
16343 }
16344 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16345 else if (part_die->lowpc >= part_die->highpc)
16346 {
16347 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16348
16349 complaint (&symfile_complaints,
16350 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16351 "for DIE at 0x%x [in module %s]"),
16352 paddress (gdbarch, part_die->lowpc),
16353 paddress (gdbarch, part_die->highpc),
16354 to_underlying (part_die->sect_off),
16355 objfile_name (objfile));
16356 }
16357 else
16358 part_die->has_pc_info = 1;
16359 }
16360
16361 return info_ptr;
16362 }
16363
16364 /* Find a cached partial DIE at OFFSET in CU. */
16365
16366 static struct partial_die_info *
16367 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16368 {
16369 struct partial_die_info *lookup_die = NULL;
16370 struct partial_die_info part_die;
16371
16372 part_die.sect_off = sect_off;
16373 lookup_die = ((struct partial_die_info *)
16374 htab_find_with_hash (cu->partial_dies, &part_die,
16375 to_underlying (sect_off)));
16376
16377 return lookup_die;
16378 }
16379
16380 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16381 except in the case of .debug_types DIEs which do not reference
16382 outside their CU (they do however referencing other types via
16383 DW_FORM_ref_sig8). */
16384
16385 static struct partial_die_info *
16386 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16387 {
16388 struct objfile *objfile = cu->objfile;
16389 struct dwarf2_per_cu_data *per_cu = NULL;
16390 struct partial_die_info *pd = NULL;
16391
16392 if (offset_in_dwz == cu->per_cu->is_dwz
16393 && offset_in_cu_p (&cu->header, sect_off))
16394 {
16395 pd = find_partial_die_in_comp_unit (sect_off, cu);
16396 if (pd != NULL)
16397 return pd;
16398 /* We missed recording what we needed.
16399 Load all dies and try again. */
16400 per_cu = cu->per_cu;
16401 }
16402 else
16403 {
16404 /* TUs don't reference other CUs/TUs (except via type signatures). */
16405 if (cu->per_cu->is_debug_types)
16406 {
16407 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16408 " external reference to offset 0x%x [in module %s].\n"),
16409 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16410 bfd_get_filename (objfile->obfd));
16411 }
16412 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16413 objfile);
16414
16415 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16416 load_partial_comp_unit (per_cu);
16417
16418 per_cu->cu->last_used = 0;
16419 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16420 }
16421
16422 /* If we didn't find it, and not all dies have been loaded,
16423 load them all and try again. */
16424
16425 if (pd == NULL && per_cu->load_all_dies == 0)
16426 {
16427 per_cu->load_all_dies = 1;
16428
16429 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16430 THIS_CU->cu may already be in use. So we can't just free it and
16431 replace its DIEs with the ones we read in. Instead, we leave those
16432 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16433 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16434 set. */
16435 load_partial_comp_unit (per_cu);
16436
16437 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16438 }
16439
16440 if (pd == NULL)
16441 internal_error (__FILE__, __LINE__,
16442 _("could not find partial DIE 0x%x "
16443 "in cache [from module %s]\n"),
16444 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16445 return pd;
16446 }
16447
16448 /* See if we can figure out if the class lives in a namespace. We do
16449 this by looking for a member function; its demangled name will
16450 contain namespace info, if there is any. */
16451
16452 static void
16453 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16454 struct dwarf2_cu *cu)
16455 {
16456 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16457 what template types look like, because the demangler
16458 frequently doesn't give the same name as the debug info. We
16459 could fix this by only using the demangled name to get the
16460 prefix (but see comment in read_structure_type). */
16461
16462 struct partial_die_info *real_pdi;
16463 struct partial_die_info *child_pdi;
16464
16465 /* If this DIE (this DIE's specification, if any) has a parent, then
16466 we should not do this. We'll prepend the parent's fully qualified
16467 name when we create the partial symbol. */
16468
16469 real_pdi = struct_pdi;
16470 while (real_pdi->has_specification)
16471 real_pdi = find_partial_die (real_pdi->spec_offset,
16472 real_pdi->spec_is_dwz, cu);
16473
16474 if (real_pdi->die_parent != NULL)
16475 return;
16476
16477 for (child_pdi = struct_pdi->die_child;
16478 child_pdi != NULL;
16479 child_pdi = child_pdi->die_sibling)
16480 {
16481 if (child_pdi->tag == DW_TAG_subprogram
16482 && child_pdi->linkage_name != NULL)
16483 {
16484 char *actual_class_name
16485 = language_class_name_from_physname (cu->language_defn,
16486 child_pdi->linkage_name);
16487 if (actual_class_name != NULL)
16488 {
16489 struct_pdi->name
16490 = ((const char *)
16491 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16492 actual_class_name,
16493 strlen (actual_class_name)));
16494 xfree (actual_class_name);
16495 }
16496 break;
16497 }
16498 }
16499 }
16500
16501 /* Adjust PART_DIE before generating a symbol for it. This function
16502 may set the is_external flag or change the DIE's name. */
16503
16504 static void
16505 fixup_partial_die (struct partial_die_info *part_die,
16506 struct dwarf2_cu *cu)
16507 {
16508 /* Once we've fixed up a die, there's no point in doing so again.
16509 This also avoids a memory leak if we were to call
16510 guess_partial_die_structure_name multiple times. */
16511 if (part_die->fixup_called)
16512 return;
16513
16514 /* If we found a reference attribute and the DIE has no name, try
16515 to find a name in the referred to DIE. */
16516
16517 if (part_die->name == NULL && part_die->has_specification)
16518 {
16519 struct partial_die_info *spec_die;
16520
16521 spec_die = find_partial_die (part_die->spec_offset,
16522 part_die->spec_is_dwz, cu);
16523
16524 fixup_partial_die (spec_die, cu);
16525
16526 if (spec_die->name)
16527 {
16528 part_die->name = spec_die->name;
16529
16530 /* Copy DW_AT_external attribute if it is set. */
16531 if (spec_die->is_external)
16532 part_die->is_external = spec_die->is_external;
16533 }
16534 }
16535
16536 /* Set default names for some unnamed DIEs. */
16537
16538 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16539 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16540
16541 /* If there is no parent die to provide a namespace, and there are
16542 children, see if we can determine the namespace from their linkage
16543 name. */
16544 if (cu->language == language_cplus
16545 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16546 && part_die->die_parent == NULL
16547 && part_die->has_children
16548 && (part_die->tag == DW_TAG_class_type
16549 || part_die->tag == DW_TAG_structure_type
16550 || part_die->tag == DW_TAG_union_type))
16551 guess_partial_die_structure_name (part_die, cu);
16552
16553 /* GCC might emit a nameless struct or union that has a linkage
16554 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16555 if (part_die->name == NULL
16556 && (part_die->tag == DW_TAG_class_type
16557 || part_die->tag == DW_TAG_interface_type
16558 || part_die->tag == DW_TAG_structure_type
16559 || part_die->tag == DW_TAG_union_type)
16560 && part_die->linkage_name != NULL)
16561 {
16562 char *demangled;
16563
16564 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16565 if (demangled)
16566 {
16567 const char *base;
16568
16569 /* Strip any leading namespaces/classes, keep only the base name.
16570 DW_AT_name for named DIEs does not contain the prefixes. */
16571 base = strrchr (demangled, ':');
16572 if (base && base > demangled && base[-1] == ':')
16573 base++;
16574 else
16575 base = demangled;
16576
16577 part_die->name
16578 = ((const char *)
16579 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16580 base, strlen (base)));
16581 xfree (demangled);
16582 }
16583 }
16584
16585 part_die->fixup_called = 1;
16586 }
16587
16588 /* Read an attribute value described by an attribute form. */
16589
16590 static const gdb_byte *
16591 read_attribute_value (const struct die_reader_specs *reader,
16592 struct attribute *attr, unsigned form,
16593 LONGEST implicit_const, const gdb_byte *info_ptr)
16594 {
16595 struct dwarf2_cu *cu = reader->cu;
16596 struct objfile *objfile = cu->objfile;
16597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16598 bfd *abfd = reader->abfd;
16599 struct comp_unit_head *cu_header = &cu->header;
16600 unsigned int bytes_read;
16601 struct dwarf_block *blk;
16602
16603 attr->form = (enum dwarf_form) form;
16604 switch (form)
16605 {
16606 case DW_FORM_ref_addr:
16607 if (cu->header.version == 2)
16608 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16609 else
16610 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16611 &cu->header, &bytes_read);
16612 info_ptr += bytes_read;
16613 break;
16614 case DW_FORM_GNU_ref_alt:
16615 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16616 info_ptr += bytes_read;
16617 break;
16618 case DW_FORM_addr:
16619 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16620 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16621 info_ptr += bytes_read;
16622 break;
16623 case DW_FORM_block2:
16624 blk = dwarf_alloc_block (cu);
16625 blk->size = read_2_bytes (abfd, info_ptr);
16626 info_ptr += 2;
16627 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16628 info_ptr += blk->size;
16629 DW_BLOCK (attr) = blk;
16630 break;
16631 case DW_FORM_block4:
16632 blk = dwarf_alloc_block (cu);
16633 blk->size = read_4_bytes (abfd, info_ptr);
16634 info_ptr += 4;
16635 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16636 info_ptr += blk->size;
16637 DW_BLOCK (attr) = blk;
16638 break;
16639 case DW_FORM_data2:
16640 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16641 info_ptr += 2;
16642 break;
16643 case DW_FORM_data4:
16644 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16645 info_ptr += 4;
16646 break;
16647 case DW_FORM_data8:
16648 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16649 info_ptr += 8;
16650 break;
16651 case DW_FORM_data16:
16652 blk = dwarf_alloc_block (cu);
16653 blk->size = 16;
16654 blk->data = read_n_bytes (abfd, info_ptr, 16);
16655 info_ptr += 16;
16656 DW_BLOCK (attr) = blk;
16657 break;
16658 case DW_FORM_sec_offset:
16659 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16660 info_ptr += bytes_read;
16661 break;
16662 case DW_FORM_string:
16663 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16664 DW_STRING_IS_CANONICAL (attr) = 0;
16665 info_ptr += bytes_read;
16666 break;
16667 case DW_FORM_strp:
16668 if (!cu->per_cu->is_dwz)
16669 {
16670 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16671 &bytes_read);
16672 DW_STRING_IS_CANONICAL (attr) = 0;
16673 info_ptr += bytes_read;
16674 break;
16675 }
16676 /* FALLTHROUGH */
16677 case DW_FORM_line_strp:
16678 if (!cu->per_cu->is_dwz)
16679 {
16680 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16681 cu_header, &bytes_read);
16682 DW_STRING_IS_CANONICAL (attr) = 0;
16683 info_ptr += bytes_read;
16684 break;
16685 }
16686 /* FALLTHROUGH */
16687 case DW_FORM_GNU_strp_alt:
16688 {
16689 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16690 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16691 &bytes_read);
16692
16693 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16694 DW_STRING_IS_CANONICAL (attr) = 0;
16695 info_ptr += bytes_read;
16696 }
16697 break;
16698 case DW_FORM_exprloc:
16699 case DW_FORM_block:
16700 blk = dwarf_alloc_block (cu);
16701 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16702 info_ptr += bytes_read;
16703 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16704 info_ptr += blk->size;
16705 DW_BLOCK (attr) = blk;
16706 break;
16707 case DW_FORM_block1:
16708 blk = dwarf_alloc_block (cu);
16709 blk->size = read_1_byte (abfd, info_ptr);
16710 info_ptr += 1;
16711 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16712 info_ptr += blk->size;
16713 DW_BLOCK (attr) = blk;
16714 break;
16715 case DW_FORM_data1:
16716 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16717 info_ptr += 1;
16718 break;
16719 case DW_FORM_flag:
16720 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16721 info_ptr += 1;
16722 break;
16723 case DW_FORM_flag_present:
16724 DW_UNSND (attr) = 1;
16725 break;
16726 case DW_FORM_sdata:
16727 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16728 info_ptr += bytes_read;
16729 break;
16730 case DW_FORM_udata:
16731 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16732 info_ptr += bytes_read;
16733 break;
16734 case DW_FORM_ref1:
16735 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16736 + read_1_byte (abfd, info_ptr));
16737 info_ptr += 1;
16738 break;
16739 case DW_FORM_ref2:
16740 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16741 + read_2_bytes (abfd, info_ptr));
16742 info_ptr += 2;
16743 break;
16744 case DW_FORM_ref4:
16745 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16746 + read_4_bytes (abfd, info_ptr));
16747 info_ptr += 4;
16748 break;
16749 case DW_FORM_ref8:
16750 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16751 + read_8_bytes (abfd, info_ptr));
16752 info_ptr += 8;
16753 break;
16754 case DW_FORM_ref_sig8:
16755 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16756 info_ptr += 8;
16757 break;
16758 case DW_FORM_ref_udata:
16759 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16760 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16761 info_ptr += bytes_read;
16762 break;
16763 case DW_FORM_indirect:
16764 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16765 info_ptr += bytes_read;
16766 if (form == DW_FORM_implicit_const)
16767 {
16768 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16769 info_ptr += bytes_read;
16770 }
16771 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16772 info_ptr);
16773 break;
16774 case DW_FORM_implicit_const:
16775 DW_SND (attr) = implicit_const;
16776 break;
16777 case DW_FORM_GNU_addr_index:
16778 if (reader->dwo_file == NULL)
16779 {
16780 /* For now flag a hard error.
16781 Later we can turn this into a complaint. */
16782 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16783 dwarf_form_name (form),
16784 bfd_get_filename (abfd));
16785 }
16786 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16787 info_ptr += bytes_read;
16788 break;
16789 case DW_FORM_GNU_str_index:
16790 if (reader->dwo_file == NULL)
16791 {
16792 /* For now flag a hard error.
16793 Later we can turn this into a complaint if warranted. */
16794 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16795 dwarf_form_name (form),
16796 bfd_get_filename (abfd));
16797 }
16798 {
16799 ULONGEST str_index =
16800 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16801
16802 DW_STRING (attr) = read_str_index (reader, str_index);
16803 DW_STRING_IS_CANONICAL (attr) = 0;
16804 info_ptr += bytes_read;
16805 }
16806 break;
16807 default:
16808 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16809 dwarf_form_name (form),
16810 bfd_get_filename (abfd));
16811 }
16812
16813 /* Super hack. */
16814 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16815 attr->form = DW_FORM_GNU_ref_alt;
16816
16817 /* We have seen instances where the compiler tried to emit a byte
16818 size attribute of -1 which ended up being encoded as an unsigned
16819 0xffffffff. Although 0xffffffff is technically a valid size value,
16820 an object of this size seems pretty unlikely so we can relatively
16821 safely treat these cases as if the size attribute was invalid and
16822 treat them as zero by default. */
16823 if (attr->name == DW_AT_byte_size
16824 && form == DW_FORM_data4
16825 && DW_UNSND (attr) >= 0xffffffff)
16826 {
16827 complaint
16828 (&symfile_complaints,
16829 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16830 hex_string (DW_UNSND (attr)));
16831 DW_UNSND (attr) = 0;
16832 }
16833
16834 return info_ptr;
16835 }
16836
16837 /* Read an attribute described by an abbreviated attribute. */
16838
16839 static const gdb_byte *
16840 read_attribute (const struct die_reader_specs *reader,
16841 struct attribute *attr, struct attr_abbrev *abbrev,
16842 const gdb_byte *info_ptr)
16843 {
16844 attr->name = abbrev->name;
16845 return read_attribute_value (reader, attr, abbrev->form,
16846 abbrev->implicit_const, info_ptr);
16847 }
16848
16849 /* Read dwarf information from a buffer. */
16850
16851 static unsigned int
16852 read_1_byte (bfd *abfd, const gdb_byte *buf)
16853 {
16854 return bfd_get_8 (abfd, buf);
16855 }
16856
16857 static int
16858 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16859 {
16860 return bfd_get_signed_8 (abfd, buf);
16861 }
16862
16863 static unsigned int
16864 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16865 {
16866 return bfd_get_16 (abfd, buf);
16867 }
16868
16869 static int
16870 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16871 {
16872 return bfd_get_signed_16 (abfd, buf);
16873 }
16874
16875 static unsigned int
16876 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16877 {
16878 return bfd_get_32 (abfd, buf);
16879 }
16880
16881 static int
16882 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16883 {
16884 return bfd_get_signed_32 (abfd, buf);
16885 }
16886
16887 static ULONGEST
16888 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16889 {
16890 return bfd_get_64 (abfd, buf);
16891 }
16892
16893 static CORE_ADDR
16894 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16895 unsigned int *bytes_read)
16896 {
16897 struct comp_unit_head *cu_header = &cu->header;
16898 CORE_ADDR retval = 0;
16899
16900 if (cu_header->signed_addr_p)
16901 {
16902 switch (cu_header->addr_size)
16903 {
16904 case 2:
16905 retval = bfd_get_signed_16 (abfd, buf);
16906 break;
16907 case 4:
16908 retval = bfd_get_signed_32 (abfd, buf);
16909 break;
16910 case 8:
16911 retval = bfd_get_signed_64 (abfd, buf);
16912 break;
16913 default:
16914 internal_error (__FILE__, __LINE__,
16915 _("read_address: bad switch, signed [in module %s]"),
16916 bfd_get_filename (abfd));
16917 }
16918 }
16919 else
16920 {
16921 switch (cu_header->addr_size)
16922 {
16923 case 2:
16924 retval = bfd_get_16 (abfd, buf);
16925 break;
16926 case 4:
16927 retval = bfd_get_32 (abfd, buf);
16928 break;
16929 case 8:
16930 retval = bfd_get_64 (abfd, buf);
16931 break;
16932 default:
16933 internal_error (__FILE__, __LINE__,
16934 _("read_address: bad switch, "
16935 "unsigned [in module %s]"),
16936 bfd_get_filename (abfd));
16937 }
16938 }
16939
16940 *bytes_read = cu_header->addr_size;
16941 return retval;
16942 }
16943
16944 /* Read the initial length from a section. The (draft) DWARF 3
16945 specification allows the initial length to take up either 4 bytes
16946 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16947 bytes describe the length and all offsets will be 8 bytes in length
16948 instead of 4.
16949
16950 An older, non-standard 64-bit format is also handled by this
16951 function. The older format in question stores the initial length
16952 as an 8-byte quantity without an escape value. Lengths greater
16953 than 2^32 aren't very common which means that the initial 4 bytes
16954 is almost always zero. Since a length value of zero doesn't make
16955 sense for the 32-bit format, this initial zero can be considered to
16956 be an escape value which indicates the presence of the older 64-bit
16957 format. As written, the code can't detect (old format) lengths
16958 greater than 4GB. If it becomes necessary to handle lengths
16959 somewhat larger than 4GB, we could allow other small values (such
16960 as the non-sensical values of 1, 2, and 3) to also be used as
16961 escape values indicating the presence of the old format.
16962
16963 The value returned via bytes_read should be used to increment the
16964 relevant pointer after calling read_initial_length().
16965
16966 [ Note: read_initial_length() and read_offset() are based on the
16967 document entitled "DWARF Debugging Information Format", revision
16968 3, draft 8, dated November 19, 2001. This document was obtained
16969 from:
16970
16971 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16972
16973 This document is only a draft and is subject to change. (So beware.)
16974
16975 Details regarding the older, non-standard 64-bit format were
16976 determined empirically by examining 64-bit ELF files produced by
16977 the SGI toolchain on an IRIX 6.5 machine.
16978
16979 - Kevin, July 16, 2002
16980 ] */
16981
16982 static LONGEST
16983 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16984 {
16985 LONGEST length = bfd_get_32 (abfd, buf);
16986
16987 if (length == 0xffffffff)
16988 {
16989 length = bfd_get_64 (abfd, buf + 4);
16990 *bytes_read = 12;
16991 }
16992 else if (length == 0)
16993 {
16994 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16995 length = bfd_get_64 (abfd, buf);
16996 *bytes_read = 8;
16997 }
16998 else
16999 {
17000 *bytes_read = 4;
17001 }
17002
17003 return length;
17004 }
17005
17006 /* Cover function for read_initial_length.
17007 Returns the length of the object at BUF, and stores the size of the
17008 initial length in *BYTES_READ and stores the size that offsets will be in
17009 *OFFSET_SIZE.
17010 If the initial length size is not equivalent to that specified in
17011 CU_HEADER then issue a complaint.
17012 This is useful when reading non-comp-unit headers. */
17013
17014 static LONGEST
17015 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17016 const struct comp_unit_head *cu_header,
17017 unsigned int *bytes_read,
17018 unsigned int *offset_size)
17019 {
17020 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17021
17022 gdb_assert (cu_header->initial_length_size == 4
17023 || cu_header->initial_length_size == 8
17024 || cu_header->initial_length_size == 12);
17025
17026 if (cu_header->initial_length_size != *bytes_read)
17027 complaint (&symfile_complaints,
17028 _("intermixed 32-bit and 64-bit DWARF sections"));
17029
17030 *offset_size = (*bytes_read == 4) ? 4 : 8;
17031 return length;
17032 }
17033
17034 /* Read an offset from the data stream. The size of the offset is
17035 given by cu_header->offset_size. */
17036
17037 static LONGEST
17038 read_offset (bfd *abfd, const gdb_byte *buf,
17039 const struct comp_unit_head *cu_header,
17040 unsigned int *bytes_read)
17041 {
17042 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17043
17044 *bytes_read = cu_header->offset_size;
17045 return offset;
17046 }
17047
17048 /* Read an offset from the data stream. */
17049
17050 static LONGEST
17051 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17052 {
17053 LONGEST retval = 0;
17054
17055 switch (offset_size)
17056 {
17057 case 4:
17058 retval = bfd_get_32 (abfd, buf);
17059 break;
17060 case 8:
17061 retval = bfd_get_64 (abfd, buf);
17062 break;
17063 default:
17064 internal_error (__FILE__, __LINE__,
17065 _("read_offset_1: bad switch [in module %s]"),
17066 bfd_get_filename (abfd));
17067 }
17068
17069 return retval;
17070 }
17071
17072 static const gdb_byte *
17073 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17074 {
17075 /* If the size of a host char is 8 bits, we can return a pointer
17076 to the buffer, otherwise we have to copy the data to a buffer
17077 allocated on the temporary obstack. */
17078 gdb_assert (HOST_CHAR_BIT == 8);
17079 return buf;
17080 }
17081
17082 static const char *
17083 read_direct_string (bfd *abfd, const gdb_byte *buf,
17084 unsigned int *bytes_read_ptr)
17085 {
17086 /* If the size of a host char is 8 bits, we can return a pointer
17087 to the string, otherwise we have to copy the string to a buffer
17088 allocated on the temporary obstack. */
17089 gdb_assert (HOST_CHAR_BIT == 8);
17090 if (*buf == '\0')
17091 {
17092 *bytes_read_ptr = 1;
17093 return NULL;
17094 }
17095 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17096 return (const char *) buf;
17097 }
17098
17099 /* Return pointer to string at section SECT offset STR_OFFSET with error
17100 reporting strings FORM_NAME and SECT_NAME. */
17101
17102 static const char *
17103 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17104 struct dwarf2_section_info *sect,
17105 const char *form_name,
17106 const char *sect_name)
17107 {
17108 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17109 if (sect->buffer == NULL)
17110 error (_("%s used without %s section [in module %s]"),
17111 form_name, sect_name, bfd_get_filename (abfd));
17112 if (str_offset >= sect->size)
17113 error (_("%s pointing outside of %s section [in module %s]"),
17114 form_name, sect_name, bfd_get_filename (abfd));
17115 gdb_assert (HOST_CHAR_BIT == 8);
17116 if (sect->buffer[str_offset] == '\0')
17117 return NULL;
17118 return (const char *) (sect->buffer + str_offset);
17119 }
17120
17121 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17122
17123 static const char *
17124 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17125 {
17126 return read_indirect_string_at_offset_from (abfd, str_offset,
17127 &dwarf2_per_objfile->str,
17128 "DW_FORM_strp", ".debug_str");
17129 }
17130
17131 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17132
17133 static const char *
17134 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17135 {
17136 return read_indirect_string_at_offset_from (abfd, str_offset,
17137 &dwarf2_per_objfile->line_str,
17138 "DW_FORM_line_strp",
17139 ".debug_line_str");
17140 }
17141
17142 /* Read a string at offset STR_OFFSET in the .debug_str section from
17143 the .dwz file DWZ. Throw an error if the offset is too large. If
17144 the string consists of a single NUL byte, return NULL; otherwise
17145 return a pointer to the string. */
17146
17147 static const char *
17148 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17149 {
17150 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17151
17152 if (dwz->str.buffer == NULL)
17153 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17154 "section [in module %s]"),
17155 bfd_get_filename (dwz->dwz_bfd));
17156 if (str_offset >= dwz->str.size)
17157 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17158 ".debug_str section [in module %s]"),
17159 bfd_get_filename (dwz->dwz_bfd));
17160 gdb_assert (HOST_CHAR_BIT == 8);
17161 if (dwz->str.buffer[str_offset] == '\0')
17162 return NULL;
17163 return (const char *) (dwz->str.buffer + str_offset);
17164 }
17165
17166 /* Return pointer to string at .debug_str offset as read from BUF.
17167 BUF is assumed to be in a compilation unit described by CU_HEADER.
17168 Return *BYTES_READ_PTR count of bytes read from BUF. */
17169
17170 static const char *
17171 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17172 const struct comp_unit_head *cu_header,
17173 unsigned int *bytes_read_ptr)
17174 {
17175 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17176
17177 return read_indirect_string_at_offset (abfd, str_offset);
17178 }
17179
17180 /* Return pointer to string at .debug_line_str offset as read from BUF.
17181 BUF is assumed to be in a compilation unit described by CU_HEADER.
17182 Return *BYTES_READ_PTR count of bytes read from BUF. */
17183
17184 static const char *
17185 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17186 const struct comp_unit_head *cu_header,
17187 unsigned int *bytes_read_ptr)
17188 {
17189 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17190
17191 return read_indirect_line_string_at_offset (abfd, str_offset);
17192 }
17193
17194 ULONGEST
17195 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17196 unsigned int *bytes_read_ptr)
17197 {
17198 ULONGEST result;
17199 unsigned int num_read;
17200 int shift;
17201 unsigned char byte;
17202
17203 result = 0;
17204 shift = 0;
17205 num_read = 0;
17206 while (1)
17207 {
17208 byte = bfd_get_8 (abfd, buf);
17209 buf++;
17210 num_read++;
17211 result |= ((ULONGEST) (byte & 127) << shift);
17212 if ((byte & 128) == 0)
17213 {
17214 break;
17215 }
17216 shift += 7;
17217 }
17218 *bytes_read_ptr = num_read;
17219 return result;
17220 }
17221
17222 static LONGEST
17223 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17224 unsigned int *bytes_read_ptr)
17225 {
17226 LONGEST result;
17227 int shift, num_read;
17228 unsigned char byte;
17229
17230 result = 0;
17231 shift = 0;
17232 num_read = 0;
17233 while (1)
17234 {
17235 byte = bfd_get_8 (abfd, buf);
17236 buf++;
17237 num_read++;
17238 result |= ((LONGEST) (byte & 127) << shift);
17239 shift += 7;
17240 if ((byte & 128) == 0)
17241 {
17242 break;
17243 }
17244 }
17245 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17246 result |= -(((LONGEST) 1) << shift);
17247 *bytes_read_ptr = num_read;
17248 return result;
17249 }
17250
17251 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17252 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17253 ADDR_SIZE is the size of addresses from the CU header. */
17254
17255 static CORE_ADDR
17256 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17257 {
17258 struct objfile *objfile = dwarf2_per_objfile->objfile;
17259 bfd *abfd = objfile->obfd;
17260 const gdb_byte *info_ptr;
17261
17262 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17263 if (dwarf2_per_objfile->addr.buffer == NULL)
17264 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17265 objfile_name (objfile));
17266 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17267 error (_("DW_FORM_addr_index pointing outside of "
17268 ".debug_addr section [in module %s]"),
17269 objfile_name (objfile));
17270 info_ptr = (dwarf2_per_objfile->addr.buffer
17271 + addr_base + addr_index * addr_size);
17272 if (addr_size == 4)
17273 return bfd_get_32 (abfd, info_ptr);
17274 else
17275 return bfd_get_64 (abfd, info_ptr);
17276 }
17277
17278 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17279
17280 static CORE_ADDR
17281 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17282 {
17283 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17284 }
17285
17286 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17287
17288 static CORE_ADDR
17289 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17290 unsigned int *bytes_read)
17291 {
17292 bfd *abfd = cu->objfile->obfd;
17293 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17294
17295 return read_addr_index (cu, addr_index);
17296 }
17297
17298 /* Data structure to pass results from dwarf2_read_addr_index_reader
17299 back to dwarf2_read_addr_index. */
17300
17301 struct dwarf2_read_addr_index_data
17302 {
17303 ULONGEST addr_base;
17304 int addr_size;
17305 };
17306
17307 /* die_reader_func for dwarf2_read_addr_index. */
17308
17309 static void
17310 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17311 const gdb_byte *info_ptr,
17312 struct die_info *comp_unit_die,
17313 int has_children,
17314 void *data)
17315 {
17316 struct dwarf2_cu *cu = reader->cu;
17317 struct dwarf2_read_addr_index_data *aidata =
17318 (struct dwarf2_read_addr_index_data *) data;
17319
17320 aidata->addr_base = cu->addr_base;
17321 aidata->addr_size = cu->header.addr_size;
17322 }
17323
17324 /* Given an index in .debug_addr, fetch the value.
17325 NOTE: This can be called during dwarf expression evaluation,
17326 long after the debug information has been read, and thus per_cu->cu
17327 may no longer exist. */
17328
17329 CORE_ADDR
17330 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17331 unsigned int addr_index)
17332 {
17333 struct objfile *objfile = per_cu->objfile;
17334 struct dwarf2_cu *cu = per_cu->cu;
17335 ULONGEST addr_base;
17336 int addr_size;
17337
17338 /* This is intended to be called from outside this file. */
17339 dw2_setup (objfile);
17340
17341 /* We need addr_base and addr_size.
17342 If we don't have PER_CU->cu, we have to get it.
17343 Nasty, but the alternative is storing the needed info in PER_CU,
17344 which at this point doesn't seem justified: it's not clear how frequently
17345 it would get used and it would increase the size of every PER_CU.
17346 Entry points like dwarf2_per_cu_addr_size do a similar thing
17347 so we're not in uncharted territory here.
17348 Alas we need to be a bit more complicated as addr_base is contained
17349 in the DIE.
17350
17351 We don't need to read the entire CU(/TU).
17352 We just need the header and top level die.
17353
17354 IWBN to use the aging mechanism to let us lazily later discard the CU.
17355 For now we skip this optimization. */
17356
17357 if (cu != NULL)
17358 {
17359 addr_base = cu->addr_base;
17360 addr_size = cu->header.addr_size;
17361 }
17362 else
17363 {
17364 struct dwarf2_read_addr_index_data aidata;
17365
17366 /* Note: We can't use init_cutu_and_read_dies_simple here,
17367 we need addr_base. */
17368 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17369 dwarf2_read_addr_index_reader, &aidata);
17370 addr_base = aidata.addr_base;
17371 addr_size = aidata.addr_size;
17372 }
17373
17374 return read_addr_index_1 (addr_index, addr_base, addr_size);
17375 }
17376
17377 /* Given a DW_FORM_GNU_str_index, fetch the string.
17378 This is only used by the Fission support. */
17379
17380 static const char *
17381 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17382 {
17383 struct objfile *objfile = dwarf2_per_objfile->objfile;
17384 const char *objf_name = objfile_name (objfile);
17385 bfd *abfd = objfile->obfd;
17386 struct dwarf2_cu *cu = reader->cu;
17387 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17388 struct dwarf2_section_info *str_offsets_section =
17389 &reader->dwo_file->sections.str_offsets;
17390 const gdb_byte *info_ptr;
17391 ULONGEST str_offset;
17392 static const char form_name[] = "DW_FORM_GNU_str_index";
17393
17394 dwarf2_read_section (objfile, str_section);
17395 dwarf2_read_section (objfile, str_offsets_section);
17396 if (str_section->buffer == NULL)
17397 error (_("%s used without .debug_str.dwo section"
17398 " in CU at offset 0x%x [in module %s]"),
17399 form_name, to_underlying (cu->header.sect_off), objf_name);
17400 if (str_offsets_section->buffer == NULL)
17401 error (_("%s used without .debug_str_offsets.dwo section"
17402 " in CU at offset 0x%x [in module %s]"),
17403 form_name, to_underlying (cu->header.sect_off), objf_name);
17404 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17405 error (_("%s pointing outside of .debug_str_offsets.dwo"
17406 " section in CU at offset 0x%x [in module %s]"),
17407 form_name, to_underlying (cu->header.sect_off), objf_name);
17408 info_ptr = (str_offsets_section->buffer
17409 + str_index * cu->header.offset_size);
17410 if (cu->header.offset_size == 4)
17411 str_offset = bfd_get_32 (abfd, info_ptr);
17412 else
17413 str_offset = bfd_get_64 (abfd, info_ptr);
17414 if (str_offset >= str_section->size)
17415 error (_("Offset from %s pointing outside of"
17416 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17417 form_name, to_underlying (cu->header.sect_off), objf_name);
17418 return (const char *) (str_section->buffer + str_offset);
17419 }
17420
17421 /* Return the length of an LEB128 number in BUF. */
17422
17423 static int
17424 leb128_size (const gdb_byte *buf)
17425 {
17426 const gdb_byte *begin = buf;
17427 gdb_byte byte;
17428
17429 while (1)
17430 {
17431 byte = *buf++;
17432 if ((byte & 128) == 0)
17433 return buf - begin;
17434 }
17435 }
17436
17437 static void
17438 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17439 {
17440 switch (lang)
17441 {
17442 case DW_LANG_C89:
17443 case DW_LANG_C99:
17444 case DW_LANG_C11:
17445 case DW_LANG_C:
17446 case DW_LANG_UPC:
17447 cu->language = language_c;
17448 break;
17449 case DW_LANG_Java:
17450 case DW_LANG_C_plus_plus:
17451 case DW_LANG_C_plus_plus_11:
17452 case DW_LANG_C_plus_plus_14:
17453 cu->language = language_cplus;
17454 break;
17455 case DW_LANG_D:
17456 cu->language = language_d;
17457 break;
17458 case DW_LANG_Fortran77:
17459 case DW_LANG_Fortran90:
17460 case DW_LANG_Fortran95:
17461 case DW_LANG_Fortran03:
17462 case DW_LANG_Fortran08:
17463 cu->language = language_fortran;
17464 break;
17465 case DW_LANG_Go:
17466 cu->language = language_go;
17467 break;
17468 case DW_LANG_Mips_Assembler:
17469 cu->language = language_asm;
17470 break;
17471 case DW_LANG_Ada83:
17472 case DW_LANG_Ada95:
17473 cu->language = language_ada;
17474 break;
17475 case DW_LANG_Modula2:
17476 cu->language = language_m2;
17477 break;
17478 case DW_LANG_Pascal83:
17479 cu->language = language_pascal;
17480 break;
17481 case DW_LANG_ObjC:
17482 cu->language = language_objc;
17483 break;
17484 case DW_LANG_Rust:
17485 case DW_LANG_Rust_old:
17486 cu->language = language_rust;
17487 break;
17488 case DW_LANG_Cobol74:
17489 case DW_LANG_Cobol85:
17490 default:
17491 cu->language = language_minimal;
17492 break;
17493 }
17494 cu->language_defn = language_def (cu->language);
17495 }
17496
17497 /* Return the named attribute or NULL if not there. */
17498
17499 static struct attribute *
17500 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17501 {
17502 for (;;)
17503 {
17504 unsigned int i;
17505 struct attribute *spec = NULL;
17506
17507 for (i = 0; i < die->num_attrs; ++i)
17508 {
17509 if (die->attrs[i].name == name)
17510 return &die->attrs[i];
17511 if (die->attrs[i].name == DW_AT_specification
17512 || die->attrs[i].name == DW_AT_abstract_origin)
17513 spec = &die->attrs[i];
17514 }
17515
17516 if (!spec)
17517 break;
17518
17519 die = follow_die_ref (die, spec, &cu);
17520 }
17521
17522 return NULL;
17523 }
17524
17525 /* Return the named attribute or NULL if not there,
17526 but do not follow DW_AT_specification, etc.
17527 This is for use in contexts where we're reading .debug_types dies.
17528 Following DW_AT_specification, DW_AT_abstract_origin will take us
17529 back up the chain, and we want to go down. */
17530
17531 static struct attribute *
17532 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17533 {
17534 unsigned int i;
17535
17536 for (i = 0; i < die->num_attrs; ++i)
17537 if (die->attrs[i].name == name)
17538 return &die->attrs[i];
17539
17540 return NULL;
17541 }
17542
17543 /* Return the string associated with a string-typed attribute, or NULL if it
17544 is either not found or is of an incorrect type. */
17545
17546 static const char *
17547 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17548 {
17549 struct attribute *attr;
17550 const char *str = NULL;
17551
17552 attr = dwarf2_attr (die, name, cu);
17553
17554 if (attr != NULL)
17555 {
17556 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17557 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17558 str = DW_STRING (attr);
17559 else
17560 complaint (&symfile_complaints,
17561 _("string type expected for attribute %s for "
17562 "DIE at 0x%x in module %s"),
17563 dwarf_attr_name (name), to_underlying (die->sect_off),
17564 objfile_name (cu->objfile));
17565 }
17566
17567 return str;
17568 }
17569
17570 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17571 and holds a non-zero value. This function should only be used for
17572 DW_FORM_flag or DW_FORM_flag_present attributes. */
17573
17574 static int
17575 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17576 {
17577 struct attribute *attr = dwarf2_attr (die, name, cu);
17578
17579 return (attr && DW_UNSND (attr));
17580 }
17581
17582 static int
17583 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17584 {
17585 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17586 which value is non-zero. However, we have to be careful with
17587 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17588 (via dwarf2_flag_true_p) follows this attribute. So we may
17589 end up accidently finding a declaration attribute that belongs
17590 to a different DIE referenced by the specification attribute,
17591 even though the given DIE does not have a declaration attribute. */
17592 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17593 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17594 }
17595
17596 /* Return the die giving the specification for DIE, if there is
17597 one. *SPEC_CU is the CU containing DIE on input, and the CU
17598 containing the return value on output. If there is no
17599 specification, but there is an abstract origin, that is
17600 returned. */
17601
17602 static struct die_info *
17603 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17604 {
17605 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17606 *spec_cu);
17607
17608 if (spec_attr == NULL)
17609 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17610
17611 if (spec_attr == NULL)
17612 return NULL;
17613 else
17614 return follow_die_ref (die, spec_attr, spec_cu);
17615 }
17616
17617 /* Stub for free_line_header to match void * callback types. */
17618
17619 static void
17620 free_line_header_voidp (void *arg)
17621 {
17622 struct line_header *lh = (struct line_header *) arg;
17623
17624 delete lh;
17625 }
17626
17627 void
17628 line_header::add_include_dir (const char *include_dir)
17629 {
17630 if (dwarf_line_debug >= 2)
17631 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17632 include_dirs.size () + 1, include_dir);
17633
17634 include_dirs.push_back (include_dir);
17635 }
17636
17637 void
17638 line_header::add_file_name (const char *name,
17639 dir_index d_index,
17640 unsigned int mod_time,
17641 unsigned int length)
17642 {
17643 if (dwarf_line_debug >= 2)
17644 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17645 (unsigned) file_names.size () + 1, name);
17646
17647 file_names.emplace_back (name, d_index, mod_time, length);
17648 }
17649
17650 /* A convenience function to find the proper .debug_line section for a CU. */
17651
17652 static struct dwarf2_section_info *
17653 get_debug_line_section (struct dwarf2_cu *cu)
17654 {
17655 struct dwarf2_section_info *section;
17656
17657 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17658 DWO file. */
17659 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17660 section = &cu->dwo_unit->dwo_file->sections.line;
17661 else if (cu->per_cu->is_dwz)
17662 {
17663 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17664
17665 section = &dwz->line;
17666 }
17667 else
17668 section = &dwarf2_per_objfile->line;
17669
17670 return section;
17671 }
17672
17673 /* Read directory or file name entry format, starting with byte of
17674 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17675 entries count and the entries themselves in the described entry
17676 format. */
17677
17678 static void
17679 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17680 struct line_header *lh,
17681 const struct comp_unit_head *cu_header,
17682 void (*callback) (struct line_header *lh,
17683 const char *name,
17684 dir_index d_index,
17685 unsigned int mod_time,
17686 unsigned int length))
17687 {
17688 gdb_byte format_count, formati;
17689 ULONGEST data_count, datai;
17690 const gdb_byte *buf = *bufp;
17691 const gdb_byte *format_header_data;
17692 int i;
17693 unsigned int bytes_read;
17694
17695 format_count = read_1_byte (abfd, buf);
17696 buf += 1;
17697 format_header_data = buf;
17698 for (formati = 0; formati < format_count; formati++)
17699 {
17700 read_unsigned_leb128 (abfd, buf, &bytes_read);
17701 buf += bytes_read;
17702 read_unsigned_leb128 (abfd, buf, &bytes_read);
17703 buf += bytes_read;
17704 }
17705
17706 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17707 buf += bytes_read;
17708 for (datai = 0; datai < data_count; datai++)
17709 {
17710 const gdb_byte *format = format_header_data;
17711 struct file_entry fe;
17712
17713 for (formati = 0; formati < format_count; formati++)
17714 {
17715 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17716 format += bytes_read;
17717
17718 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17719 format += bytes_read;
17720
17721 gdb::optional<const char *> string;
17722 gdb::optional<unsigned int> uint;
17723
17724 switch (form)
17725 {
17726 case DW_FORM_string:
17727 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17728 buf += bytes_read;
17729 break;
17730
17731 case DW_FORM_line_strp:
17732 string.emplace (read_indirect_line_string (abfd, buf,
17733 cu_header,
17734 &bytes_read));
17735 buf += bytes_read;
17736 break;
17737
17738 case DW_FORM_data1:
17739 uint.emplace (read_1_byte (abfd, buf));
17740 buf += 1;
17741 break;
17742
17743 case DW_FORM_data2:
17744 uint.emplace (read_2_bytes (abfd, buf));
17745 buf += 2;
17746 break;
17747
17748 case DW_FORM_data4:
17749 uint.emplace (read_4_bytes (abfd, buf));
17750 buf += 4;
17751 break;
17752
17753 case DW_FORM_data8:
17754 uint.emplace (read_8_bytes (abfd, buf));
17755 buf += 8;
17756 break;
17757
17758 case DW_FORM_udata:
17759 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17760 buf += bytes_read;
17761 break;
17762
17763 case DW_FORM_block:
17764 /* It is valid only for DW_LNCT_timestamp which is ignored by
17765 current GDB. */
17766 break;
17767 }
17768
17769 switch (content_type)
17770 {
17771 case DW_LNCT_path:
17772 if (string.has_value ())
17773 fe.name = *string;
17774 break;
17775 case DW_LNCT_directory_index:
17776 if (uint.has_value ())
17777 fe.d_index = (dir_index) *uint;
17778 break;
17779 case DW_LNCT_timestamp:
17780 if (uint.has_value ())
17781 fe.mod_time = *uint;
17782 break;
17783 case DW_LNCT_size:
17784 if (uint.has_value ())
17785 fe.length = *uint;
17786 break;
17787 case DW_LNCT_MD5:
17788 break;
17789 default:
17790 complaint (&symfile_complaints,
17791 _("Unknown format content type %s"),
17792 pulongest (content_type));
17793 }
17794 }
17795
17796 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17797 }
17798
17799 *bufp = buf;
17800 }
17801
17802 /* Read the statement program header starting at OFFSET in
17803 .debug_line, or .debug_line.dwo. Return a pointer
17804 to a struct line_header, allocated using xmalloc.
17805 Returns NULL if there is a problem reading the header, e.g., if it
17806 has a version we don't understand.
17807
17808 NOTE: the strings in the include directory and file name tables of
17809 the returned object point into the dwarf line section buffer,
17810 and must not be freed. */
17811
17812 static line_header_up
17813 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17814 {
17815 const gdb_byte *line_ptr;
17816 unsigned int bytes_read, offset_size;
17817 int i;
17818 const char *cur_dir, *cur_file;
17819 struct dwarf2_section_info *section;
17820 bfd *abfd;
17821
17822 section = get_debug_line_section (cu);
17823 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17824 if (section->buffer == NULL)
17825 {
17826 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17827 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17828 else
17829 complaint (&symfile_complaints, _("missing .debug_line section"));
17830 return 0;
17831 }
17832
17833 /* We can't do this until we know the section is non-empty.
17834 Only then do we know we have such a section. */
17835 abfd = get_section_bfd_owner (section);
17836
17837 /* Make sure that at least there's room for the total_length field.
17838 That could be 12 bytes long, but we're just going to fudge that. */
17839 if (to_underlying (sect_off) + 4 >= section->size)
17840 {
17841 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17842 return 0;
17843 }
17844
17845 line_header_up lh (new line_header ());
17846
17847 lh->sect_off = sect_off;
17848 lh->offset_in_dwz = cu->per_cu->is_dwz;
17849
17850 line_ptr = section->buffer + to_underlying (sect_off);
17851
17852 /* Read in the header. */
17853 lh->total_length =
17854 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17855 &bytes_read, &offset_size);
17856 line_ptr += bytes_read;
17857 if (line_ptr + lh->total_length > (section->buffer + section->size))
17858 {
17859 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17860 return 0;
17861 }
17862 lh->statement_program_end = line_ptr + lh->total_length;
17863 lh->version = read_2_bytes (abfd, line_ptr);
17864 line_ptr += 2;
17865 if (lh->version > 5)
17866 {
17867 /* This is a version we don't understand. The format could have
17868 changed in ways we don't handle properly so just punt. */
17869 complaint (&symfile_complaints,
17870 _("unsupported version in .debug_line section"));
17871 return NULL;
17872 }
17873 if (lh->version >= 5)
17874 {
17875 gdb_byte segment_selector_size;
17876
17877 /* Skip address size. */
17878 read_1_byte (abfd, line_ptr);
17879 line_ptr += 1;
17880
17881 segment_selector_size = read_1_byte (abfd, line_ptr);
17882 line_ptr += 1;
17883 if (segment_selector_size != 0)
17884 {
17885 complaint (&symfile_complaints,
17886 _("unsupported segment selector size %u "
17887 "in .debug_line section"),
17888 segment_selector_size);
17889 return NULL;
17890 }
17891 }
17892 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17893 line_ptr += offset_size;
17894 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17895 line_ptr += 1;
17896 if (lh->version >= 4)
17897 {
17898 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17899 line_ptr += 1;
17900 }
17901 else
17902 lh->maximum_ops_per_instruction = 1;
17903
17904 if (lh->maximum_ops_per_instruction == 0)
17905 {
17906 lh->maximum_ops_per_instruction = 1;
17907 complaint (&symfile_complaints,
17908 _("invalid maximum_ops_per_instruction "
17909 "in `.debug_line' section"));
17910 }
17911
17912 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17913 line_ptr += 1;
17914 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17915 line_ptr += 1;
17916 lh->line_range = read_1_byte (abfd, line_ptr);
17917 line_ptr += 1;
17918 lh->opcode_base = read_1_byte (abfd, line_ptr);
17919 line_ptr += 1;
17920 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
17921
17922 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17923 for (i = 1; i < lh->opcode_base; ++i)
17924 {
17925 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17926 line_ptr += 1;
17927 }
17928
17929 if (lh->version >= 5)
17930 {
17931 /* Read directory table. */
17932 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17933 [] (struct line_header *lh, const char *name,
17934 dir_index d_index, unsigned int mod_time,
17935 unsigned int length)
17936 {
17937 lh->add_include_dir (name);
17938 });
17939
17940 /* Read file name table. */
17941 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17942 [] (struct line_header *lh, const char *name,
17943 dir_index d_index, unsigned int mod_time,
17944 unsigned int length)
17945 {
17946 lh->add_file_name (name, d_index, mod_time, length);
17947 });
17948 }
17949 else
17950 {
17951 /* Read directory table. */
17952 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17953 {
17954 line_ptr += bytes_read;
17955 lh->add_include_dir (cur_dir);
17956 }
17957 line_ptr += bytes_read;
17958
17959 /* Read file name table. */
17960 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17961 {
17962 unsigned int mod_time, length;
17963 dir_index d_index;
17964
17965 line_ptr += bytes_read;
17966 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17967 line_ptr += bytes_read;
17968 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17969 line_ptr += bytes_read;
17970 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17971 line_ptr += bytes_read;
17972
17973 lh->add_file_name (cur_file, d_index, mod_time, length);
17974 }
17975 line_ptr += bytes_read;
17976 }
17977 lh->statement_program_start = line_ptr;
17978
17979 if (line_ptr > (section->buffer + section->size))
17980 complaint (&symfile_complaints,
17981 _("line number info header doesn't "
17982 "fit in `.debug_line' section"));
17983
17984 return lh;
17985 }
17986
17987 /* Subroutine of dwarf_decode_lines to simplify it.
17988 Return the file name of the psymtab for included file FILE_INDEX
17989 in line header LH of PST.
17990 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17991 If space for the result is malloc'd, it will be freed by a cleanup.
17992 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17993
17994 The function creates dangling cleanup registration. */
17995
17996 static const char *
17997 psymtab_include_file_name (const struct line_header *lh, int file_index,
17998 const struct partial_symtab *pst,
17999 const char *comp_dir)
18000 {
18001 const file_entry &fe = lh->file_names[file_index];
18002 const char *include_name = fe.name;
18003 const char *include_name_to_compare = include_name;
18004 const char *pst_filename;
18005 char *copied_name = NULL;
18006 int file_is_pst;
18007
18008 const char *dir_name = fe.include_dir (lh);
18009
18010 if (!IS_ABSOLUTE_PATH (include_name)
18011 && (dir_name != NULL || comp_dir != NULL))
18012 {
18013 /* Avoid creating a duplicate psymtab for PST.
18014 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18015 Before we do the comparison, however, we need to account
18016 for DIR_NAME and COMP_DIR.
18017 First prepend dir_name (if non-NULL). If we still don't
18018 have an absolute path prepend comp_dir (if non-NULL).
18019 However, the directory we record in the include-file's
18020 psymtab does not contain COMP_DIR (to match the
18021 corresponding symtab(s)).
18022
18023 Example:
18024
18025 bash$ cd /tmp
18026 bash$ gcc -g ./hello.c
18027 include_name = "hello.c"
18028 dir_name = "."
18029 DW_AT_comp_dir = comp_dir = "/tmp"
18030 DW_AT_name = "./hello.c"
18031
18032 */
18033
18034 if (dir_name != NULL)
18035 {
18036 char *tem = concat (dir_name, SLASH_STRING,
18037 include_name, (char *)NULL);
18038
18039 make_cleanup (xfree, tem);
18040 include_name = tem;
18041 include_name_to_compare = include_name;
18042 }
18043 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18044 {
18045 char *tem = concat (comp_dir, SLASH_STRING,
18046 include_name, (char *)NULL);
18047
18048 make_cleanup (xfree, tem);
18049 include_name_to_compare = tem;
18050 }
18051 }
18052
18053 pst_filename = pst->filename;
18054 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18055 {
18056 copied_name = concat (pst->dirname, SLASH_STRING,
18057 pst_filename, (char *)NULL);
18058 pst_filename = copied_name;
18059 }
18060
18061 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18062
18063 if (copied_name != NULL)
18064 xfree (copied_name);
18065
18066 if (file_is_pst)
18067 return NULL;
18068 return include_name;
18069 }
18070
18071 /* State machine to track the state of the line number program. */
18072
18073 class lnp_state_machine
18074 {
18075 public:
18076 /* Initialize a machine state for the start of a line number
18077 program. */
18078 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18079
18080 file_entry *current_file ()
18081 {
18082 /* lh->file_names is 0-based, but the file name numbers in the
18083 statement program are 1-based. */
18084 return m_line_header->file_name_at (m_file);
18085 }
18086
18087 /* Record the line in the state machine. END_SEQUENCE is true if
18088 we're processing the end of a sequence. */
18089 void record_line (bool end_sequence);
18090
18091 /* Check address and if invalid nop-out the rest of the lines in this
18092 sequence. */
18093 void check_line_address (struct dwarf2_cu *cu,
18094 const gdb_byte *line_ptr,
18095 CORE_ADDR lowpc, CORE_ADDR address);
18096
18097 void handle_set_discriminator (unsigned int discriminator)
18098 {
18099 m_discriminator = discriminator;
18100 m_line_has_non_zero_discriminator |= discriminator != 0;
18101 }
18102
18103 /* Handle DW_LNE_set_address. */
18104 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18105 {
18106 m_op_index = 0;
18107 address += baseaddr;
18108 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18109 }
18110
18111 /* Handle DW_LNS_advance_pc. */
18112 void handle_advance_pc (CORE_ADDR adjust);
18113
18114 /* Handle a special opcode. */
18115 void handle_special_opcode (unsigned char op_code);
18116
18117 /* Handle DW_LNS_advance_line. */
18118 void handle_advance_line (int line_delta)
18119 {
18120 advance_line (line_delta);
18121 }
18122
18123 /* Handle DW_LNS_set_file. */
18124 void handle_set_file (file_name_index file);
18125
18126 /* Handle DW_LNS_negate_stmt. */
18127 void handle_negate_stmt ()
18128 {
18129 m_is_stmt = !m_is_stmt;
18130 }
18131
18132 /* Handle DW_LNS_const_add_pc. */
18133 void handle_const_add_pc ();
18134
18135 /* Handle DW_LNS_fixed_advance_pc. */
18136 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18137 {
18138 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18139 m_op_index = 0;
18140 }
18141
18142 /* Handle DW_LNS_copy. */
18143 void handle_copy ()
18144 {
18145 record_line (false);
18146 m_discriminator = 0;
18147 }
18148
18149 /* Handle DW_LNE_end_sequence. */
18150 void handle_end_sequence ()
18151 {
18152 m_record_line_callback = ::record_line;
18153 }
18154
18155 private:
18156 /* Advance the line by LINE_DELTA. */
18157 void advance_line (int line_delta)
18158 {
18159 m_line += line_delta;
18160
18161 if (line_delta != 0)
18162 m_line_has_non_zero_discriminator = m_discriminator != 0;
18163 }
18164
18165 gdbarch *m_gdbarch;
18166
18167 /* True if we're recording lines.
18168 Otherwise we're building partial symtabs and are just interested in
18169 finding include files mentioned by the line number program. */
18170 bool m_record_lines_p;
18171
18172 /* The line number header. */
18173 line_header *m_line_header;
18174
18175 /* These are part of the standard DWARF line number state machine,
18176 and initialized according to the DWARF spec. */
18177
18178 unsigned char m_op_index = 0;
18179 /* The line table index (1-based) of the current file. */
18180 file_name_index m_file = (file_name_index) 1;
18181 unsigned int m_line = 1;
18182
18183 /* These are initialized in the constructor. */
18184
18185 CORE_ADDR m_address;
18186 bool m_is_stmt;
18187 unsigned int m_discriminator;
18188
18189 /* Additional bits of state we need to track. */
18190
18191 /* The last file that we called dwarf2_start_subfile for.
18192 This is only used for TLLs. */
18193 unsigned int m_last_file = 0;
18194 /* The last file a line number was recorded for. */
18195 struct subfile *m_last_subfile = NULL;
18196
18197 /* The function to call to record a line. */
18198 record_line_ftype *m_record_line_callback = NULL;
18199
18200 /* The last line number that was recorded, used to coalesce
18201 consecutive entries for the same line. This can happen, for
18202 example, when discriminators are present. PR 17276. */
18203 unsigned int m_last_line = 0;
18204 bool m_line_has_non_zero_discriminator = false;
18205 };
18206
18207 void
18208 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18209 {
18210 CORE_ADDR addr_adj = (((m_op_index + adjust)
18211 / m_line_header->maximum_ops_per_instruction)
18212 * m_line_header->minimum_instruction_length);
18213 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18214 m_op_index = ((m_op_index + adjust)
18215 % m_line_header->maximum_ops_per_instruction);
18216 }
18217
18218 void
18219 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18220 {
18221 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18222 CORE_ADDR addr_adj = (((m_op_index
18223 + (adj_opcode / m_line_header->line_range))
18224 / m_line_header->maximum_ops_per_instruction)
18225 * m_line_header->minimum_instruction_length);
18226 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18227 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18228 % m_line_header->maximum_ops_per_instruction);
18229
18230 int line_delta = (m_line_header->line_base
18231 + (adj_opcode % m_line_header->line_range));
18232 advance_line (line_delta);
18233 record_line (false);
18234 m_discriminator = 0;
18235 }
18236
18237 void
18238 lnp_state_machine::handle_set_file (file_name_index file)
18239 {
18240 m_file = file;
18241
18242 const file_entry *fe = current_file ();
18243 if (fe == NULL)
18244 dwarf2_debug_line_missing_file_complaint ();
18245 else if (m_record_lines_p)
18246 {
18247 const char *dir = fe->include_dir (m_line_header);
18248
18249 m_last_subfile = current_subfile;
18250 m_line_has_non_zero_discriminator = m_discriminator != 0;
18251 dwarf2_start_subfile (fe->name, dir);
18252 }
18253 }
18254
18255 void
18256 lnp_state_machine::handle_const_add_pc ()
18257 {
18258 CORE_ADDR adjust
18259 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18260
18261 CORE_ADDR addr_adj
18262 = (((m_op_index + adjust)
18263 / m_line_header->maximum_ops_per_instruction)
18264 * m_line_header->minimum_instruction_length);
18265
18266 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18267 m_op_index = ((m_op_index + adjust)
18268 % m_line_header->maximum_ops_per_instruction);
18269 }
18270
18271 /* Ignore this record_line request. */
18272
18273 static void
18274 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18275 {
18276 return;
18277 }
18278
18279 /* Return non-zero if we should add LINE to the line number table.
18280 LINE is the line to add, LAST_LINE is the last line that was added,
18281 LAST_SUBFILE is the subfile for LAST_LINE.
18282 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18283 had a non-zero discriminator.
18284
18285 We have to be careful in the presence of discriminators.
18286 E.g., for this line:
18287
18288 for (i = 0; i < 100000; i++);
18289
18290 clang can emit four line number entries for that one line,
18291 each with a different discriminator.
18292 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18293
18294 However, we want gdb to coalesce all four entries into one.
18295 Otherwise the user could stepi into the middle of the line and
18296 gdb would get confused about whether the pc really was in the
18297 middle of the line.
18298
18299 Things are further complicated by the fact that two consecutive
18300 line number entries for the same line is a heuristic used by gcc
18301 to denote the end of the prologue. So we can't just discard duplicate
18302 entries, we have to be selective about it. The heuristic we use is
18303 that we only collapse consecutive entries for the same line if at least
18304 one of those entries has a non-zero discriminator. PR 17276.
18305
18306 Note: Addresses in the line number state machine can never go backwards
18307 within one sequence, thus this coalescing is ok. */
18308
18309 static int
18310 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18311 int line_has_non_zero_discriminator,
18312 struct subfile *last_subfile)
18313 {
18314 if (current_subfile != last_subfile)
18315 return 1;
18316 if (line != last_line)
18317 return 1;
18318 /* Same line for the same file that we've seen already.
18319 As a last check, for pr 17276, only record the line if the line
18320 has never had a non-zero discriminator. */
18321 if (!line_has_non_zero_discriminator)
18322 return 1;
18323 return 0;
18324 }
18325
18326 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18327 in the line table of subfile SUBFILE. */
18328
18329 static void
18330 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18331 unsigned int line, CORE_ADDR address,
18332 record_line_ftype p_record_line)
18333 {
18334 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18335
18336 if (dwarf_line_debug)
18337 {
18338 fprintf_unfiltered (gdb_stdlog,
18339 "Recording line %u, file %s, address %s\n",
18340 line, lbasename (subfile->name),
18341 paddress (gdbarch, address));
18342 }
18343
18344 (*p_record_line) (subfile, line, addr);
18345 }
18346
18347 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18348 Mark the end of a set of line number records.
18349 The arguments are the same as for dwarf_record_line_1.
18350 If SUBFILE is NULL the request is ignored. */
18351
18352 static void
18353 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18354 CORE_ADDR address, record_line_ftype p_record_line)
18355 {
18356 if (subfile == NULL)
18357 return;
18358
18359 if (dwarf_line_debug)
18360 {
18361 fprintf_unfiltered (gdb_stdlog,
18362 "Finishing current line, file %s, address %s\n",
18363 lbasename (subfile->name),
18364 paddress (gdbarch, address));
18365 }
18366
18367 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18368 }
18369
18370 void
18371 lnp_state_machine::record_line (bool end_sequence)
18372 {
18373 if (dwarf_line_debug)
18374 {
18375 fprintf_unfiltered (gdb_stdlog,
18376 "Processing actual line %u: file %u,"
18377 " address %s, is_stmt %u, discrim %u\n",
18378 m_line, to_underlying (m_file),
18379 paddress (m_gdbarch, m_address),
18380 m_is_stmt, m_discriminator);
18381 }
18382
18383 file_entry *fe = current_file ();
18384
18385 if (fe == NULL)
18386 dwarf2_debug_line_missing_file_complaint ();
18387 /* For now we ignore lines not starting on an instruction boundary.
18388 But not when processing end_sequence for compatibility with the
18389 previous version of the code. */
18390 else if (m_op_index == 0 || end_sequence)
18391 {
18392 fe->included_p = 1;
18393 if (m_record_lines_p && m_is_stmt)
18394 {
18395 if (m_last_subfile != current_subfile || end_sequence)
18396 {
18397 dwarf_finish_line (m_gdbarch, m_last_subfile,
18398 m_address, m_record_line_callback);
18399 }
18400
18401 if (!end_sequence)
18402 {
18403 if (dwarf_record_line_p (m_line, m_last_line,
18404 m_line_has_non_zero_discriminator,
18405 m_last_subfile))
18406 {
18407 dwarf_record_line_1 (m_gdbarch, current_subfile,
18408 m_line, m_address,
18409 m_record_line_callback);
18410 }
18411 m_last_subfile = current_subfile;
18412 m_last_line = m_line;
18413 }
18414 }
18415 }
18416 }
18417
18418 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18419 bool record_lines_p)
18420 {
18421 m_gdbarch = arch;
18422 m_record_lines_p = record_lines_p;
18423 m_line_header = lh;
18424
18425 m_record_line_callback = ::record_line;
18426
18427 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18428 was a line entry for it so that the backend has a chance to adjust it
18429 and also record it in case it needs it. This is currently used by MIPS
18430 code, cf. `mips_adjust_dwarf2_line'. */
18431 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18432 m_is_stmt = lh->default_is_stmt;
18433 m_discriminator = 0;
18434 }
18435
18436 void
18437 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18438 const gdb_byte *line_ptr,
18439 CORE_ADDR lowpc, CORE_ADDR address)
18440 {
18441 /* If address < lowpc then it's not a usable value, it's outside the
18442 pc range of the CU. However, we restrict the test to only address
18443 values of zero to preserve GDB's previous behaviour which is to
18444 handle the specific case of a function being GC'd by the linker. */
18445
18446 if (address == 0 && address < lowpc)
18447 {
18448 /* This line table is for a function which has been
18449 GCd by the linker. Ignore it. PR gdb/12528 */
18450
18451 struct objfile *objfile = cu->objfile;
18452 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18453
18454 complaint (&symfile_complaints,
18455 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18456 line_offset, objfile_name (objfile));
18457 m_record_line_callback = noop_record_line;
18458 /* Note: record_line_callback is left as noop_record_line until
18459 we see DW_LNE_end_sequence. */
18460 }
18461 }
18462
18463 /* Subroutine of dwarf_decode_lines to simplify it.
18464 Process the line number information in LH.
18465 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18466 program in order to set included_p for every referenced header. */
18467
18468 static void
18469 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18470 const int decode_for_pst_p, CORE_ADDR lowpc)
18471 {
18472 const gdb_byte *line_ptr, *extended_end;
18473 const gdb_byte *line_end;
18474 unsigned int bytes_read, extended_len;
18475 unsigned char op_code, extended_op;
18476 CORE_ADDR baseaddr;
18477 struct objfile *objfile = cu->objfile;
18478 bfd *abfd = objfile->obfd;
18479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18480 /* True if we're recording line info (as opposed to building partial
18481 symtabs and just interested in finding include files mentioned by
18482 the line number program). */
18483 bool record_lines_p = !decode_for_pst_p;
18484
18485 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18486
18487 line_ptr = lh->statement_program_start;
18488 line_end = lh->statement_program_end;
18489
18490 /* Read the statement sequences until there's nothing left. */
18491 while (line_ptr < line_end)
18492 {
18493 /* The DWARF line number program state machine. Reset the state
18494 machine at the start of each sequence. */
18495 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18496 bool end_sequence = false;
18497
18498 if (record_lines_p)
18499 {
18500 /* Start a subfile for the current file of the state
18501 machine. */
18502 const file_entry *fe = state_machine.current_file ();
18503
18504 if (fe != NULL)
18505 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18506 }
18507
18508 /* Decode the table. */
18509 while (line_ptr < line_end && !end_sequence)
18510 {
18511 op_code = read_1_byte (abfd, line_ptr);
18512 line_ptr += 1;
18513
18514 if (op_code >= lh->opcode_base)
18515 {
18516 /* Special opcode. */
18517 state_machine.handle_special_opcode (op_code);
18518 }
18519 else switch (op_code)
18520 {
18521 case DW_LNS_extended_op:
18522 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18523 &bytes_read);
18524 line_ptr += bytes_read;
18525 extended_end = line_ptr + extended_len;
18526 extended_op = read_1_byte (abfd, line_ptr);
18527 line_ptr += 1;
18528 switch (extended_op)
18529 {
18530 case DW_LNE_end_sequence:
18531 state_machine.handle_end_sequence ();
18532 end_sequence = true;
18533 break;
18534 case DW_LNE_set_address:
18535 {
18536 CORE_ADDR address
18537 = read_address (abfd, line_ptr, cu, &bytes_read);
18538 line_ptr += bytes_read;
18539
18540 state_machine.check_line_address (cu, line_ptr,
18541 lowpc, address);
18542 state_machine.handle_set_address (baseaddr, address);
18543 }
18544 break;
18545 case DW_LNE_define_file:
18546 {
18547 const char *cur_file;
18548 unsigned int mod_time, length;
18549 dir_index dindex;
18550
18551 cur_file = read_direct_string (abfd, line_ptr,
18552 &bytes_read);
18553 line_ptr += bytes_read;
18554 dindex = (dir_index)
18555 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18556 line_ptr += bytes_read;
18557 mod_time =
18558 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18559 line_ptr += bytes_read;
18560 length =
18561 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18562 line_ptr += bytes_read;
18563 lh->add_file_name (cur_file, dindex, mod_time, length);
18564 }
18565 break;
18566 case DW_LNE_set_discriminator:
18567 {
18568 /* The discriminator is not interesting to the
18569 debugger; just ignore it. We still need to
18570 check its value though:
18571 if there are consecutive entries for the same
18572 (non-prologue) line we want to coalesce them.
18573 PR 17276. */
18574 unsigned int discr
18575 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18576 line_ptr += bytes_read;
18577
18578 state_machine.handle_set_discriminator (discr);
18579 }
18580 break;
18581 default:
18582 complaint (&symfile_complaints,
18583 _("mangled .debug_line section"));
18584 return;
18585 }
18586 /* Make sure that we parsed the extended op correctly. If e.g.
18587 we expected a different address size than the producer used,
18588 we may have read the wrong number of bytes. */
18589 if (line_ptr != extended_end)
18590 {
18591 complaint (&symfile_complaints,
18592 _("mangled .debug_line section"));
18593 return;
18594 }
18595 break;
18596 case DW_LNS_copy:
18597 state_machine.handle_copy ();
18598 break;
18599 case DW_LNS_advance_pc:
18600 {
18601 CORE_ADDR adjust
18602 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18603 line_ptr += bytes_read;
18604
18605 state_machine.handle_advance_pc (adjust);
18606 }
18607 break;
18608 case DW_LNS_advance_line:
18609 {
18610 int line_delta
18611 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18612 line_ptr += bytes_read;
18613
18614 state_machine.handle_advance_line (line_delta);
18615 }
18616 break;
18617 case DW_LNS_set_file:
18618 {
18619 file_name_index file
18620 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18621 &bytes_read);
18622 line_ptr += bytes_read;
18623
18624 state_machine.handle_set_file (file);
18625 }
18626 break;
18627 case DW_LNS_set_column:
18628 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18629 line_ptr += bytes_read;
18630 break;
18631 case DW_LNS_negate_stmt:
18632 state_machine.handle_negate_stmt ();
18633 break;
18634 case DW_LNS_set_basic_block:
18635 break;
18636 /* Add to the address register of the state machine the
18637 address increment value corresponding to special opcode
18638 255. I.e., this value is scaled by the minimum
18639 instruction length since special opcode 255 would have
18640 scaled the increment. */
18641 case DW_LNS_const_add_pc:
18642 state_machine.handle_const_add_pc ();
18643 break;
18644 case DW_LNS_fixed_advance_pc:
18645 {
18646 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18647 line_ptr += 2;
18648
18649 state_machine.handle_fixed_advance_pc (addr_adj);
18650 }
18651 break;
18652 default:
18653 {
18654 /* Unknown standard opcode, ignore it. */
18655 int i;
18656
18657 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18658 {
18659 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18660 line_ptr += bytes_read;
18661 }
18662 }
18663 }
18664 }
18665
18666 if (!end_sequence)
18667 dwarf2_debug_line_missing_end_sequence_complaint ();
18668
18669 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18670 in which case we still finish recording the last line). */
18671 state_machine.record_line (true);
18672 }
18673 }
18674
18675 /* Decode the Line Number Program (LNP) for the given line_header
18676 structure and CU. The actual information extracted and the type
18677 of structures created from the LNP depends on the value of PST.
18678
18679 1. If PST is NULL, then this procedure uses the data from the program
18680 to create all necessary symbol tables, and their linetables.
18681
18682 2. If PST is not NULL, this procedure reads the program to determine
18683 the list of files included by the unit represented by PST, and
18684 builds all the associated partial symbol tables.
18685
18686 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18687 It is used for relative paths in the line table.
18688 NOTE: When processing partial symtabs (pst != NULL),
18689 comp_dir == pst->dirname.
18690
18691 NOTE: It is important that psymtabs have the same file name (via strcmp)
18692 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18693 symtab we don't use it in the name of the psymtabs we create.
18694 E.g. expand_line_sal requires this when finding psymtabs to expand.
18695 A good testcase for this is mb-inline.exp.
18696
18697 LOWPC is the lowest address in CU (or 0 if not known).
18698
18699 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18700 for its PC<->lines mapping information. Otherwise only the filename
18701 table is read in. */
18702
18703 static void
18704 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18705 struct dwarf2_cu *cu, struct partial_symtab *pst,
18706 CORE_ADDR lowpc, int decode_mapping)
18707 {
18708 struct objfile *objfile = cu->objfile;
18709 const int decode_for_pst_p = (pst != NULL);
18710
18711 if (decode_mapping)
18712 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18713
18714 if (decode_for_pst_p)
18715 {
18716 int file_index;
18717
18718 /* Now that we're done scanning the Line Header Program, we can
18719 create the psymtab of each included file. */
18720 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18721 if (lh->file_names[file_index].included_p == 1)
18722 {
18723 const char *include_name =
18724 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18725 if (include_name != NULL)
18726 dwarf2_create_include_psymtab (include_name, pst, objfile);
18727 }
18728 }
18729 else
18730 {
18731 /* Make sure a symtab is created for every file, even files
18732 which contain only variables (i.e. no code with associated
18733 line numbers). */
18734 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18735 int i;
18736
18737 for (i = 0; i < lh->file_names.size (); i++)
18738 {
18739 file_entry &fe = lh->file_names[i];
18740
18741 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18742
18743 if (current_subfile->symtab == NULL)
18744 {
18745 current_subfile->symtab
18746 = allocate_symtab (cust, current_subfile->name);
18747 }
18748 fe.symtab = current_subfile->symtab;
18749 }
18750 }
18751 }
18752
18753 /* Start a subfile for DWARF. FILENAME is the name of the file and
18754 DIRNAME the name of the source directory which contains FILENAME
18755 or NULL if not known.
18756 This routine tries to keep line numbers from identical absolute and
18757 relative file names in a common subfile.
18758
18759 Using the `list' example from the GDB testsuite, which resides in
18760 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18761 of /srcdir/list0.c yields the following debugging information for list0.c:
18762
18763 DW_AT_name: /srcdir/list0.c
18764 DW_AT_comp_dir: /compdir
18765 files.files[0].name: list0.h
18766 files.files[0].dir: /srcdir
18767 files.files[1].name: list0.c
18768 files.files[1].dir: /srcdir
18769
18770 The line number information for list0.c has to end up in a single
18771 subfile, so that `break /srcdir/list0.c:1' works as expected.
18772 start_subfile will ensure that this happens provided that we pass the
18773 concatenation of files.files[1].dir and files.files[1].name as the
18774 subfile's name. */
18775
18776 static void
18777 dwarf2_start_subfile (const char *filename, const char *dirname)
18778 {
18779 char *copy = NULL;
18780
18781 /* In order not to lose the line information directory,
18782 we concatenate it to the filename when it makes sense.
18783 Note that the Dwarf3 standard says (speaking of filenames in line
18784 information): ``The directory index is ignored for file names
18785 that represent full path names''. Thus ignoring dirname in the
18786 `else' branch below isn't an issue. */
18787
18788 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18789 {
18790 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18791 filename = copy;
18792 }
18793
18794 start_subfile (filename);
18795
18796 if (copy != NULL)
18797 xfree (copy);
18798 }
18799
18800 /* Start a symtab for DWARF.
18801 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18802
18803 static struct compunit_symtab *
18804 dwarf2_start_symtab (struct dwarf2_cu *cu,
18805 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18806 {
18807 struct compunit_symtab *cust
18808 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18809
18810 record_debugformat ("DWARF 2");
18811 record_producer (cu->producer);
18812
18813 /* We assume that we're processing GCC output. */
18814 processing_gcc_compilation = 2;
18815
18816 cu->processing_has_namespace_info = 0;
18817
18818 return cust;
18819 }
18820
18821 static void
18822 var_decode_location (struct attribute *attr, struct symbol *sym,
18823 struct dwarf2_cu *cu)
18824 {
18825 struct objfile *objfile = cu->objfile;
18826 struct comp_unit_head *cu_header = &cu->header;
18827
18828 /* NOTE drow/2003-01-30: There used to be a comment and some special
18829 code here to turn a symbol with DW_AT_external and a
18830 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18831 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18832 with some versions of binutils) where shared libraries could have
18833 relocations against symbols in their debug information - the
18834 minimal symbol would have the right address, but the debug info
18835 would not. It's no longer necessary, because we will explicitly
18836 apply relocations when we read in the debug information now. */
18837
18838 /* A DW_AT_location attribute with no contents indicates that a
18839 variable has been optimized away. */
18840 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18841 {
18842 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18843 return;
18844 }
18845
18846 /* Handle one degenerate form of location expression specially, to
18847 preserve GDB's previous behavior when section offsets are
18848 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18849 then mark this symbol as LOC_STATIC. */
18850
18851 if (attr_form_is_block (attr)
18852 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18853 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18854 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18855 && (DW_BLOCK (attr)->size
18856 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18857 {
18858 unsigned int dummy;
18859
18860 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18861 SYMBOL_VALUE_ADDRESS (sym) =
18862 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18863 else
18864 SYMBOL_VALUE_ADDRESS (sym) =
18865 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18866 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18867 fixup_symbol_section (sym, objfile);
18868 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18869 SYMBOL_SECTION (sym));
18870 return;
18871 }
18872
18873 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18874 expression evaluator, and use LOC_COMPUTED only when necessary
18875 (i.e. when the value of a register or memory location is
18876 referenced, or a thread-local block, etc.). Then again, it might
18877 not be worthwhile. I'm assuming that it isn't unless performance
18878 or memory numbers show me otherwise. */
18879
18880 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18881
18882 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18883 cu->has_loclist = 1;
18884 }
18885
18886 /* Given a pointer to a DWARF information entry, figure out if we need
18887 to make a symbol table entry for it, and if so, create a new entry
18888 and return a pointer to it.
18889 If TYPE is NULL, determine symbol type from the die, otherwise
18890 used the passed type.
18891 If SPACE is not NULL, use it to hold the new symbol. If it is
18892 NULL, allocate a new symbol on the objfile's obstack. */
18893
18894 static struct symbol *
18895 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18896 struct symbol *space)
18897 {
18898 struct objfile *objfile = cu->objfile;
18899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18900 struct symbol *sym = NULL;
18901 const char *name;
18902 struct attribute *attr = NULL;
18903 struct attribute *attr2 = NULL;
18904 CORE_ADDR baseaddr;
18905 struct pending **list_to_add = NULL;
18906
18907 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18908
18909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18910
18911 name = dwarf2_name (die, cu);
18912 if (name)
18913 {
18914 const char *linkagename;
18915 int suppress_add = 0;
18916
18917 if (space)
18918 sym = space;
18919 else
18920 sym = allocate_symbol (objfile);
18921 OBJSTAT (objfile, n_syms++);
18922
18923 /* Cache this symbol's name and the name's demangled form (if any). */
18924 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18925 linkagename = dwarf2_physname (name, die, cu);
18926 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18927
18928 /* Fortran does not have mangling standard and the mangling does differ
18929 between gfortran, iFort etc. */
18930 if (cu->language == language_fortran
18931 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18932 symbol_set_demangled_name (&(sym->ginfo),
18933 dwarf2_full_name (name, die, cu),
18934 NULL);
18935
18936 /* Default assumptions.
18937 Use the passed type or decode it from the die. */
18938 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18939 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18940 if (type != NULL)
18941 SYMBOL_TYPE (sym) = type;
18942 else
18943 SYMBOL_TYPE (sym) = die_type (die, cu);
18944 attr = dwarf2_attr (die,
18945 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18946 cu);
18947 if (attr)
18948 {
18949 SYMBOL_LINE (sym) = DW_UNSND (attr);
18950 }
18951
18952 attr = dwarf2_attr (die,
18953 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18954 cu);
18955 if (attr)
18956 {
18957 file_name_index file_index = (file_name_index) DW_UNSND (attr);
18958 struct file_entry *fe;
18959
18960 if (cu->line_header != NULL)
18961 fe = cu->line_header->file_name_at (file_index);
18962 else
18963 fe = NULL;
18964
18965 if (fe == NULL)
18966 complaint (&symfile_complaints,
18967 _("file index out of range"));
18968 else
18969 symbol_set_symtab (sym, fe->symtab);
18970 }
18971
18972 switch (die->tag)
18973 {
18974 case DW_TAG_label:
18975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18976 if (attr)
18977 {
18978 CORE_ADDR addr;
18979
18980 addr = attr_value_as_address (attr);
18981 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18982 SYMBOL_VALUE_ADDRESS (sym) = addr;
18983 }
18984 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18985 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18986 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18987 add_symbol_to_list (sym, cu->list_in_scope);
18988 break;
18989 case DW_TAG_subprogram:
18990 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18991 finish_block. */
18992 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18994 if ((attr2 && (DW_UNSND (attr2) != 0))
18995 || cu->language == language_ada)
18996 {
18997 /* Subprograms marked external are stored as a global symbol.
18998 Ada subprograms, whether marked external or not, are always
18999 stored as a global symbol, because we want to be able to
19000 access them globally. For instance, we want to be able
19001 to break on a nested subprogram without having to
19002 specify the context. */
19003 list_to_add = &global_symbols;
19004 }
19005 else
19006 {
19007 list_to_add = cu->list_in_scope;
19008 }
19009 break;
19010 case DW_TAG_inlined_subroutine:
19011 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19012 finish_block. */
19013 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19014 SYMBOL_INLINED (sym) = 1;
19015 list_to_add = cu->list_in_scope;
19016 break;
19017 case DW_TAG_template_value_param:
19018 suppress_add = 1;
19019 /* Fall through. */
19020 case DW_TAG_constant:
19021 case DW_TAG_variable:
19022 case DW_TAG_member:
19023 /* Compilation with minimal debug info may result in
19024 variables with missing type entries. Change the
19025 misleading `void' type to something sensible. */
19026 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19027 SYMBOL_TYPE (sym)
19028 = objfile_type (objfile)->nodebug_data_symbol;
19029
19030 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19031 /* In the case of DW_TAG_member, we should only be called for
19032 static const members. */
19033 if (die->tag == DW_TAG_member)
19034 {
19035 /* dwarf2_add_field uses die_is_declaration,
19036 so we do the same. */
19037 gdb_assert (die_is_declaration (die, cu));
19038 gdb_assert (attr);
19039 }
19040 if (attr)
19041 {
19042 dwarf2_const_value (attr, sym, cu);
19043 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19044 if (!suppress_add)
19045 {
19046 if (attr2 && (DW_UNSND (attr2) != 0))
19047 list_to_add = &global_symbols;
19048 else
19049 list_to_add = cu->list_in_scope;
19050 }
19051 break;
19052 }
19053 attr = dwarf2_attr (die, DW_AT_location, cu);
19054 if (attr)
19055 {
19056 var_decode_location (attr, sym, cu);
19057 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19058
19059 /* Fortran explicitly imports any global symbols to the local
19060 scope by DW_TAG_common_block. */
19061 if (cu->language == language_fortran && die->parent
19062 && die->parent->tag == DW_TAG_common_block)
19063 attr2 = NULL;
19064
19065 if (SYMBOL_CLASS (sym) == LOC_STATIC
19066 && SYMBOL_VALUE_ADDRESS (sym) == 0
19067 && !dwarf2_per_objfile->has_section_at_zero)
19068 {
19069 /* When a static variable is eliminated by the linker,
19070 the corresponding debug information is not stripped
19071 out, but the variable address is set to null;
19072 do not add such variables into symbol table. */
19073 }
19074 else if (attr2 && (DW_UNSND (attr2) != 0))
19075 {
19076 /* Workaround gfortran PR debug/40040 - it uses
19077 DW_AT_location for variables in -fPIC libraries which may
19078 get overriden by other libraries/executable and get
19079 a different address. Resolve it by the minimal symbol
19080 which may come from inferior's executable using copy
19081 relocation. Make this workaround only for gfortran as for
19082 other compilers GDB cannot guess the minimal symbol
19083 Fortran mangling kind. */
19084 if (cu->language == language_fortran && die->parent
19085 && die->parent->tag == DW_TAG_module
19086 && cu->producer
19087 && startswith (cu->producer, "GNU Fortran"))
19088 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19089
19090 /* A variable with DW_AT_external is never static,
19091 but it may be block-scoped. */
19092 list_to_add = (cu->list_in_scope == &file_symbols
19093 ? &global_symbols : cu->list_in_scope);
19094 }
19095 else
19096 list_to_add = cu->list_in_scope;
19097 }
19098 else
19099 {
19100 /* We do not know the address of this symbol.
19101 If it is an external symbol and we have type information
19102 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19103 The address of the variable will then be determined from
19104 the minimal symbol table whenever the variable is
19105 referenced. */
19106 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19107
19108 /* Fortran explicitly imports any global symbols to the local
19109 scope by DW_TAG_common_block. */
19110 if (cu->language == language_fortran && die->parent
19111 && die->parent->tag == DW_TAG_common_block)
19112 {
19113 /* SYMBOL_CLASS doesn't matter here because
19114 read_common_block is going to reset it. */
19115 if (!suppress_add)
19116 list_to_add = cu->list_in_scope;
19117 }
19118 else if (attr2 && (DW_UNSND (attr2) != 0)
19119 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19120 {
19121 /* A variable with DW_AT_external is never static, but it
19122 may be block-scoped. */
19123 list_to_add = (cu->list_in_scope == &file_symbols
19124 ? &global_symbols : cu->list_in_scope);
19125
19126 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19127 }
19128 else if (!die_is_declaration (die, cu))
19129 {
19130 /* Use the default LOC_OPTIMIZED_OUT class. */
19131 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19132 if (!suppress_add)
19133 list_to_add = cu->list_in_scope;
19134 }
19135 }
19136 break;
19137 case DW_TAG_formal_parameter:
19138 /* If we are inside a function, mark this as an argument. If
19139 not, we might be looking at an argument to an inlined function
19140 when we do not have enough information to show inlined frames;
19141 pretend it's a local variable in that case so that the user can
19142 still see it. */
19143 if (context_stack_depth > 0
19144 && context_stack[context_stack_depth - 1].name != NULL)
19145 SYMBOL_IS_ARGUMENT (sym) = 1;
19146 attr = dwarf2_attr (die, DW_AT_location, cu);
19147 if (attr)
19148 {
19149 var_decode_location (attr, sym, cu);
19150 }
19151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19152 if (attr)
19153 {
19154 dwarf2_const_value (attr, sym, cu);
19155 }
19156
19157 list_to_add = cu->list_in_scope;
19158 break;
19159 case DW_TAG_unspecified_parameters:
19160 /* From varargs functions; gdb doesn't seem to have any
19161 interest in this information, so just ignore it for now.
19162 (FIXME?) */
19163 break;
19164 case DW_TAG_template_type_param:
19165 suppress_add = 1;
19166 /* Fall through. */
19167 case DW_TAG_class_type:
19168 case DW_TAG_interface_type:
19169 case DW_TAG_structure_type:
19170 case DW_TAG_union_type:
19171 case DW_TAG_set_type:
19172 case DW_TAG_enumeration_type:
19173 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19174 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19175
19176 {
19177 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19178 really ever be static objects: otherwise, if you try
19179 to, say, break of a class's method and you're in a file
19180 which doesn't mention that class, it won't work unless
19181 the check for all static symbols in lookup_symbol_aux
19182 saves you. See the OtherFileClass tests in
19183 gdb.c++/namespace.exp. */
19184
19185 if (!suppress_add)
19186 {
19187 list_to_add = (cu->list_in_scope == &file_symbols
19188 && cu->language == language_cplus
19189 ? &global_symbols : cu->list_in_scope);
19190
19191 /* The semantics of C++ state that "struct foo {
19192 ... }" also defines a typedef for "foo". */
19193 if (cu->language == language_cplus
19194 || cu->language == language_ada
19195 || cu->language == language_d
19196 || cu->language == language_rust)
19197 {
19198 /* The symbol's name is already allocated along
19199 with this objfile, so we don't need to
19200 duplicate it for the type. */
19201 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19202 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19203 }
19204 }
19205 }
19206 break;
19207 case DW_TAG_typedef:
19208 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19209 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19210 list_to_add = cu->list_in_scope;
19211 break;
19212 case DW_TAG_base_type:
19213 case DW_TAG_subrange_type:
19214 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19216 list_to_add = cu->list_in_scope;
19217 break;
19218 case DW_TAG_enumerator:
19219 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19220 if (attr)
19221 {
19222 dwarf2_const_value (attr, sym, cu);
19223 }
19224 {
19225 /* NOTE: carlton/2003-11-10: See comment above in the
19226 DW_TAG_class_type, etc. block. */
19227
19228 list_to_add = (cu->list_in_scope == &file_symbols
19229 && cu->language == language_cplus
19230 ? &global_symbols : cu->list_in_scope);
19231 }
19232 break;
19233 case DW_TAG_imported_declaration:
19234 case DW_TAG_namespace:
19235 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19236 list_to_add = &global_symbols;
19237 break;
19238 case DW_TAG_module:
19239 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19240 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19241 list_to_add = &global_symbols;
19242 break;
19243 case DW_TAG_common_block:
19244 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19245 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19246 add_symbol_to_list (sym, cu->list_in_scope);
19247 break;
19248 default:
19249 /* Not a tag we recognize. Hopefully we aren't processing
19250 trash data, but since we must specifically ignore things
19251 we don't recognize, there is nothing else we should do at
19252 this point. */
19253 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19254 dwarf_tag_name (die->tag));
19255 break;
19256 }
19257
19258 if (suppress_add)
19259 {
19260 sym->hash_next = objfile->template_symbols;
19261 objfile->template_symbols = sym;
19262 list_to_add = NULL;
19263 }
19264
19265 if (list_to_add != NULL)
19266 add_symbol_to_list (sym, list_to_add);
19267
19268 /* For the benefit of old versions of GCC, check for anonymous
19269 namespaces based on the demangled name. */
19270 if (!cu->processing_has_namespace_info
19271 && cu->language == language_cplus)
19272 cp_scan_for_anonymous_namespaces (sym, objfile);
19273 }
19274 return (sym);
19275 }
19276
19277 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19278
19279 static struct symbol *
19280 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19281 {
19282 return new_symbol_full (die, type, cu, NULL);
19283 }
19284
19285 /* Given an attr with a DW_FORM_dataN value in host byte order,
19286 zero-extend it as appropriate for the symbol's type. The DWARF
19287 standard (v4) is not entirely clear about the meaning of using
19288 DW_FORM_dataN for a constant with a signed type, where the type is
19289 wider than the data. The conclusion of a discussion on the DWARF
19290 list was that this is unspecified. We choose to always zero-extend
19291 because that is the interpretation long in use by GCC. */
19292
19293 static gdb_byte *
19294 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19295 struct dwarf2_cu *cu, LONGEST *value, int bits)
19296 {
19297 struct objfile *objfile = cu->objfile;
19298 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19299 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19300 LONGEST l = DW_UNSND (attr);
19301
19302 if (bits < sizeof (*value) * 8)
19303 {
19304 l &= ((LONGEST) 1 << bits) - 1;
19305 *value = l;
19306 }
19307 else if (bits == sizeof (*value) * 8)
19308 *value = l;
19309 else
19310 {
19311 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19312 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19313 return bytes;
19314 }
19315
19316 return NULL;
19317 }
19318
19319 /* Read a constant value from an attribute. Either set *VALUE, or if
19320 the value does not fit in *VALUE, set *BYTES - either already
19321 allocated on the objfile obstack, or newly allocated on OBSTACK,
19322 or, set *BATON, if we translated the constant to a location
19323 expression. */
19324
19325 static void
19326 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19327 const char *name, struct obstack *obstack,
19328 struct dwarf2_cu *cu,
19329 LONGEST *value, const gdb_byte **bytes,
19330 struct dwarf2_locexpr_baton **baton)
19331 {
19332 struct objfile *objfile = cu->objfile;
19333 struct comp_unit_head *cu_header = &cu->header;
19334 struct dwarf_block *blk;
19335 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19336 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19337
19338 *value = 0;
19339 *bytes = NULL;
19340 *baton = NULL;
19341
19342 switch (attr->form)
19343 {
19344 case DW_FORM_addr:
19345 case DW_FORM_GNU_addr_index:
19346 {
19347 gdb_byte *data;
19348
19349 if (TYPE_LENGTH (type) != cu_header->addr_size)
19350 dwarf2_const_value_length_mismatch_complaint (name,
19351 cu_header->addr_size,
19352 TYPE_LENGTH (type));
19353 /* Symbols of this form are reasonably rare, so we just
19354 piggyback on the existing location code rather than writing
19355 a new implementation of symbol_computed_ops. */
19356 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19357 (*baton)->per_cu = cu->per_cu;
19358 gdb_assert ((*baton)->per_cu);
19359
19360 (*baton)->size = 2 + cu_header->addr_size;
19361 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19362 (*baton)->data = data;
19363
19364 data[0] = DW_OP_addr;
19365 store_unsigned_integer (&data[1], cu_header->addr_size,
19366 byte_order, DW_ADDR (attr));
19367 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19368 }
19369 break;
19370 case DW_FORM_string:
19371 case DW_FORM_strp:
19372 case DW_FORM_GNU_str_index:
19373 case DW_FORM_GNU_strp_alt:
19374 /* DW_STRING is already allocated on the objfile obstack, point
19375 directly to it. */
19376 *bytes = (const gdb_byte *) DW_STRING (attr);
19377 break;
19378 case DW_FORM_block1:
19379 case DW_FORM_block2:
19380 case DW_FORM_block4:
19381 case DW_FORM_block:
19382 case DW_FORM_exprloc:
19383 case DW_FORM_data16:
19384 blk = DW_BLOCK (attr);
19385 if (TYPE_LENGTH (type) != blk->size)
19386 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19387 TYPE_LENGTH (type));
19388 *bytes = blk->data;
19389 break;
19390
19391 /* The DW_AT_const_value attributes are supposed to carry the
19392 symbol's value "represented as it would be on the target
19393 architecture." By the time we get here, it's already been
19394 converted to host endianness, so we just need to sign- or
19395 zero-extend it as appropriate. */
19396 case DW_FORM_data1:
19397 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19398 break;
19399 case DW_FORM_data2:
19400 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19401 break;
19402 case DW_FORM_data4:
19403 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19404 break;
19405 case DW_FORM_data8:
19406 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19407 break;
19408
19409 case DW_FORM_sdata:
19410 *value = DW_SND (attr);
19411 break;
19412
19413 case DW_FORM_udata:
19414 *value = DW_UNSND (attr);
19415 break;
19416
19417 default:
19418 complaint (&symfile_complaints,
19419 _("unsupported const value attribute form: '%s'"),
19420 dwarf_form_name (attr->form));
19421 *value = 0;
19422 break;
19423 }
19424 }
19425
19426
19427 /* Copy constant value from an attribute to a symbol. */
19428
19429 static void
19430 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19431 struct dwarf2_cu *cu)
19432 {
19433 struct objfile *objfile = cu->objfile;
19434 LONGEST value;
19435 const gdb_byte *bytes;
19436 struct dwarf2_locexpr_baton *baton;
19437
19438 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19439 SYMBOL_PRINT_NAME (sym),
19440 &objfile->objfile_obstack, cu,
19441 &value, &bytes, &baton);
19442
19443 if (baton != NULL)
19444 {
19445 SYMBOL_LOCATION_BATON (sym) = baton;
19446 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19447 }
19448 else if (bytes != NULL)
19449 {
19450 SYMBOL_VALUE_BYTES (sym) = bytes;
19451 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19452 }
19453 else
19454 {
19455 SYMBOL_VALUE (sym) = value;
19456 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19457 }
19458 }
19459
19460 /* Return the type of the die in question using its DW_AT_type attribute. */
19461
19462 static struct type *
19463 die_type (struct die_info *die, struct dwarf2_cu *cu)
19464 {
19465 struct attribute *type_attr;
19466
19467 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19468 if (!type_attr)
19469 {
19470 /* A missing DW_AT_type represents a void type. */
19471 return objfile_type (cu->objfile)->builtin_void;
19472 }
19473
19474 return lookup_die_type (die, type_attr, cu);
19475 }
19476
19477 /* True iff CU's producer generates GNAT Ada auxiliary information
19478 that allows to find parallel types through that information instead
19479 of having to do expensive parallel lookups by type name. */
19480
19481 static int
19482 need_gnat_info (struct dwarf2_cu *cu)
19483 {
19484 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19485 of GNAT produces this auxiliary information, without any indication
19486 that it is produced. Part of enhancing the FSF version of GNAT
19487 to produce that information will be to put in place an indicator
19488 that we can use in order to determine whether the descriptive type
19489 info is available or not. One suggestion that has been made is
19490 to use a new attribute, attached to the CU die. For now, assume
19491 that the descriptive type info is not available. */
19492 return 0;
19493 }
19494
19495 /* Return the auxiliary type of the die in question using its
19496 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19497 attribute is not present. */
19498
19499 static struct type *
19500 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19501 {
19502 struct attribute *type_attr;
19503
19504 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19505 if (!type_attr)
19506 return NULL;
19507
19508 return lookup_die_type (die, type_attr, cu);
19509 }
19510
19511 /* If DIE has a descriptive_type attribute, then set the TYPE's
19512 descriptive type accordingly. */
19513
19514 static void
19515 set_descriptive_type (struct type *type, struct die_info *die,
19516 struct dwarf2_cu *cu)
19517 {
19518 struct type *descriptive_type = die_descriptive_type (die, cu);
19519
19520 if (descriptive_type)
19521 {
19522 ALLOCATE_GNAT_AUX_TYPE (type);
19523 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19524 }
19525 }
19526
19527 /* Return the containing type of the die in question using its
19528 DW_AT_containing_type attribute. */
19529
19530 static struct type *
19531 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19532 {
19533 struct attribute *type_attr;
19534
19535 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19536 if (!type_attr)
19537 error (_("Dwarf Error: Problem turning containing type into gdb type "
19538 "[in module %s]"), objfile_name (cu->objfile));
19539
19540 return lookup_die_type (die, type_attr, cu);
19541 }
19542
19543 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19544
19545 static struct type *
19546 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19547 {
19548 struct objfile *objfile = dwarf2_per_objfile->objfile;
19549 char *message, *saved;
19550
19551 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19552 objfile_name (objfile),
19553 to_underlying (cu->header.sect_off),
19554 to_underlying (die->sect_off));
19555 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19556 message, strlen (message));
19557 xfree (message);
19558
19559 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19560 }
19561
19562 /* Look up the type of DIE in CU using its type attribute ATTR.
19563 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19564 DW_AT_containing_type.
19565 If there is no type substitute an error marker. */
19566
19567 static struct type *
19568 lookup_die_type (struct die_info *die, const struct attribute *attr,
19569 struct dwarf2_cu *cu)
19570 {
19571 struct objfile *objfile = cu->objfile;
19572 struct type *this_type;
19573
19574 gdb_assert (attr->name == DW_AT_type
19575 || attr->name == DW_AT_GNAT_descriptive_type
19576 || attr->name == DW_AT_containing_type);
19577
19578 /* First see if we have it cached. */
19579
19580 if (attr->form == DW_FORM_GNU_ref_alt)
19581 {
19582 struct dwarf2_per_cu_data *per_cu;
19583 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19584
19585 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19586 this_type = get_die_type_at_offset (sect_off, per_cu);
19587 }
19588 else if (attr_form_is_ref (attr))
19589 {
19590 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19591
19592 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19593 }
19594 else if (attr->form == DW_FORM_ref_sig8)
19595 {
19596 ULONGEST signature = DW_SIGNATURE (attr);
19597
19598 return get_signatured_type (die, signature, cu);
19599 }
19600 else
19601 {
19602 complaint (&symfile_complaints,
19603 _("Dwarf Error: Bad type attribute %s in DIE"
19604 " at 0x%x [in module %s]"),
19605 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19606 objfile_name (objfile));
19607 return build_error_marker_type (cu, die);
19608 }
19609
19610 /* If not cached we need to read it in. */
19611
19612 if (this_type == NULL)
19613 {
19614 struct die_info *type_die = NULL;
19615 struct dwarf2_cu *type_cu = cu;
19616
19617 if (attr_form_is_ref (attr))
19618 type_die = follow_die_ref (die, attr, &type_cu);
19619 if (type_die == NULL)
19620 return build_error_marker_type (cu, die);
19621 /* If we find the type now, it's probably because the type came
19622 from an inter-CU reference and the type's CU got expanded before
19623 ours. */
19624 this_type = read_type_die (type_die, type_cu);
19625 }
19626
19627 /* If we still don't have a type use an error marker. */
19628
19629 if (this_type == NULL)
19630 return build_error_marker_type (cu, die);
19631
19632 return this_type;
19633 }
19634
19635 /* Return the type in DIE, CU.
19636 Returns NULL for invalid types.
19637
19638 This first does a lookup in die_type_hash,
19639 and only reads the die in if necessary.
19640
19641 NOTE: This can be called when reading in partial or full symbols. */
19642
19643 static struct type *
19644 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19645 {
19646 struct type *this_type;
19647
19648 this_type = get_die_type (die, cu);
19649 if (this_type)
19650 return this_type;
19651
19652 return read_type_die_1 (die, cu);
19653 }
19654
19655 /* Read the type in DIE, CU.
19656 Returns NULL for invalid types. */
19657
19658 static struct type *
19659 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19660 {
19661 struct type *this_type = NULL;
19662
19663 switch (die->tag)
19664 {
19665 case DW_TAG_class_type:
19666 case DW_TAG_interface_type:
19667 case DW_TAG_structure_type:
19668 case DW_TAG_union_type:
19669 this_type = read_structure_type (die, cu);
19670 break;
19671 case DW_TAG_enumeration_type:
19672 this_type = read_enumeration_type (die, cu);
19673 break;
19674 case DW_TAG_subprogram:
19675 case DW_TAG_subroutine_type:
19676 case DW_TAG_inlined_subroutine:
19677 this_type = read_subroutine_type (die, cu);
19678 break;
19679 case DW_TAG_array_type:
19680 this_type = read_array_type (die, cu);
19681 break;
19682 case DW_TAG_set_type:
19683 this_type = read_set_type (die, cu);
19684 break;
19685 case DW_TAG_pointer_type:
19686 this_type = read_tag_pointer_type (die, cu);
19687 break;
19688 case DW_TAG_ptr_to_member_type:
19689 this_type = read_tag_ptr_to_member_type (die, cu);
19690 break;
19691 case DW_TAG_reference_type:
19692 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19693 break;
19694 case DW_TAG_rvalue_reference_type:
19695 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19696 break;
19697 case DW_TAG_const_type:
19698 this_type = read_tag_const_type (die, cu);
19699 break;
19700 case DW_TAG_volatile_type:
19701 this_type = read_tag_volatile_type (die, cu);
19702 break;
19703 case DW_TAG_restrict_type:
19704 this_type = read_tag_restrict_type (die, cu);
19705 break;
19706 case DW_TAG_string_type:
19707 this_type = read_tag_string_type (die, cu);
19708 break;
19709 case DW_TAG_typedef:
19710 this_type = read_typedef (die, cu);
19711 break;
19712 case DW_TAG_subrange_type:
19713 this_type = read_subrange_type (die, cu);
19714 break;
19715 case DW_TAG_base_type:
19716 this_type = read_base_type (die, cu);
19717 break;
19718 case DW_TAG_unspecified_type:
19719 this_type = read_unspecified_type (die, cu);
19720 break;
19721 case DW_TAG_namespace:
19722 this_type = read_namespace_type (die, cu);
19723 break;
19724 case DW_TAG_module:
19725 this_type = read_module_type (die, cu);
19726 break;
19727 case DW_TAG_atomic_type:
19728 this_type = read_tag_atomic_type (die, cu);
19729 break;
19730 default:
19731 complaint (&symfile_complaints,
19732 _("unexpected tag in read_type_die: '%s'"),
19733 dwarf_tag_name (die->tag));
19734 break;
19735 }
19736
19737 return this_type;
19738 }
19739
19740 /* See if we can figure out if the class lives in a namespace. We do
19741 this by looking for a member function; its demangled name will
19742 contain namespace info, if there is any.
19743 Return the computed name or NULL.
19744 Space for the result is allocated on the objfile's obstack.
19745 This is the full-die version of guess_partial_die_structure_name.
19746 In this case we know DIE has no useful parent. */
19747
19748 static char *
19749 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19750 {
19751 struct die_info *spec_die;
19752 struct dwarf2_cu *spec_cu;
19753 struct die_info *child;
19754
19755 spec_cu = cu;
19756 spec_die = die_specification (die, &spec_cu);
19757 if (spec_die != NULL)
19758 {
19759 die = spec_die;
19760 cu = spec_cu;
19761 }
19762
19763 for (child = die->child;
19764 child != NULL;
19765 child = child->sibling)
19766 {
19767 if (child->tag == DW_TAG_subprogram)
19768 {
19769 const char *linkage_name;
19770
19771 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19772 if (linkage_name == NULL)
19773 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19774 cu);
19775 if (linkage_name != NULL)
19776 {
19777 char *actual_name
19778 = language_class_name_from_physname (cu->language_defn,
19779 linkage_name);
19780 char *name = NULL;
19781
19782 if (actual_name != NULL)
19783 {
19784 const char *die_name = dwarf2_name (die, cu);
19785
19786 if (die_name != NULL
19787 && strcmp (die_name, actual_name) != 0)
19788 {
19789 /* Strip off the class name from the full name.
19790 We want the prefix. */
19791 int die_name_len = strlen (die_name);
19792 int actual_name_len = strlen (actual_name);
19793
19794 /* Test for '::' as a sanity check. */
19795 if (actual_name_len > die_name_len + 2
19796 && actual_name[actual_name_len
19797 - die_name_len - 1] == ':')
19798 name = (char *) obstack_copy0 (
19799 &cu->objfile->per_bfd->storage_obstack,
19800 actual_name, actual_name_len - die_name_len - 2);
19801 }
19802 }
19803 xfree (actual_name);
19804 return name;
19805 }
19806 }
19807 }
19808
19809 return NULL;
19810 }
19811
19812 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19813 prefix part in such case. See
19814 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19815
19816 static const char *
19817 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19818 {
19819 struct attribute *attr;
19820 const char *base;
19821
19822 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19823 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19824 return NULL;
19825
19826 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19827 return NULL;
19828
19829 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19830 if (attr == NULL)
19831 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19832 if (attr == NULL || DW_STRING (attr) == NULL)
19833 return NULL;
19834
19835 /* dwarf2_name had to be already called. */
19836 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19837
19838 /* Strip the base name, keep any leading namespaces/classes. */
19839 base = strrchr (DW_STRING (attr), ':');
19840 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19841 return "";
19842
19843 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19844 DW_STRING (attr),
19845 &base[-1] - DW_STRING (attr));
19846 }
19847
19848 /* Return the name of the namespace/class that DIE is defined within,
19849 or "" if we can't tell. The caller should not xfree the result.
19850
19851 For example, if we're within the method foo() in the following
19852 code:
19853
19854 namespace N {
19855 class C {
19856 void foo () {
19857 }
19858 };
19859 }
19860
19861 then determine_prefix on foo's die will return "N::C". */
19862
19863 static const char *
19864 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19865 {
19866 struct die_info *parent, *spec_die;
19867 struct dwarf2_cu *spec_cu;
19868 struct type *parent_type;
19869 const char *retval;
19870
19871 if (cu->language != language_cplus
19872 && cu->language != language_fortran && cu->language != language_d
19873 && cu->language != language_rust)
19874 return "";
19875
19876 retval = anonymous_struct_prefix (die, cu);
19877 if (retval)
19878 return retval;
19879
19880 /* We have to be careful in the presence of DW_AT_specification.
19881 For example, with GCC 3.4, given the code
19882
19883 namespace N {
19884 void foo() {
19885 // Definition of N::foo.
19886 }
19887 }
19888
19889 then we'll have a tree of DIEs like this:
19890
19891 1: DW_TAG_compile_unit
19892 2: DW_TAG_namespace // N
19893 3: DW_TAG_subprogram // declaration of N::foo
19894 4: DW_TAG_subprogram // definition of N::foo
19895 DW_AT_specification // refers to die #3
19896
19897 Thus, when processing die #4, we have to pretend that we're in
19898 the context of its DW_AT_specification, namely the contex of die
19899 #3. */
19900 spec_cu = cu;
19901 spec_die = die_specification (die, &spec_cu);
19902 if (spec_die == NULL)
19903 parent = die->parent;
19904 else
19905 {
19906 parent = spec_die->parent;
19907 cu = spec_cu;
19908 }
19909
19910 if (parent == NULL)
19911 return "";
19912 else if (parent->building_fullname)
19913 {
19914 const char *name;
19915 const char *parent_name;
19916
19917 /* It has been seen on RealView 2.2 built binaries,
19918 DW_TAG_template_type_param types actually _defined_ as
19919 children of the parent class:
19920
19921 enum E {};
19922 template class <class Enum> Class{};
19923 Class<enum E> class_e;
19924
19925 1: DW_TAG_class_type (Class)
19926 2: DW_TAG_enumeration_type (E)
19927 3: DW_TAG_enumerator (enum1:0)
19928 3: DW_TAG_enumerator (enum2:1)
19929 ...
19930 2: DW_TAG_template_type_param
19931 DW_AT_type DW_FORM_ref_udata (E)
19932
19933 Besides being broken debug info, it can put GDB into an
19934 infinite loop. Consider:
19935
19936 When we're building the full name for Class<E>, we'll start
19937 at Class, and go look over its template type parameters,
19938 finding E. We'll then try to build the full name of E, and
19939 reach here. We're now trying to build the full name of E,
19940 and look over the parent DIE for containing scope. In the
19941 broken case, if we followed the parent DIE of E, we'd again
19942 find Class, and once again go look at its template type
19943 arguments, etc., etc. Simply don't consider such parent die
19944 as source-level parent of this die (it can't be, the language
19945 doesn't allow it), and break the loop here. */
19946 name = dwarf2_name (die, cu);
19947 parent_name = dwarf2_name (parent, cu);
19948 complaint (&symfile_complaints,
19949 _("template param type '%s' defined within parent '%s'"),
19950 name ? name : "<unknown>",
19951 parent_name ? parent_name : "<unknown>");
19952 return "";
19953 }
19954 else
19955 switch (parent->tag)
19956 {
19957 case DW_TAG_namespace:
19958 parent_type = read_type_die (parent, cu);
19959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19961 Work around this problem here. */
19962 if (cu->language == language_cplus
19963 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19964 return "";
19965 /* We give a name to even anonymous namespaces. */
19966 return TYPE_TAG_NAME (parent_type);
19967 case DW_TAG_class_type:
19968 case DW_TAG_interface_type:
19969 case DW_TAG_structure_type:
19970 case DW_TAG_union_type:
19971 case DW_TAG_module:
19972 parent_type = read_type_die (parent, cu);
19973 if (TYPE_TAG_NAME (parent_type) != NULL)
19974 return TYPE_TAG_NAME (parent_type);
19975 else
19976 /* An anonymous structure is only allowed non-static data
19977 members; no typedefs, no member functions, et cetera.
19978 So it does not need a prefix. */
19979 return "";
19980 case DW_TAG_compile_unit:
19981 case DW_TAG_partial_unit:
19982 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19983 if (cu->language == language_cplus
19984 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19985 && die->child != NULL
19986 && (die->tag == DW_TAG_class_type
19987 || die->tag == DW_TAG_structure_type
19988 || die->tag == DW_TAG_union_type))
19989 {
19990 char *name = guess_full_die_structure_name (die, cu);
19991 if (name != NULL)
19992 return name;
19993 }
19994 return "";
19995 case DW_TAG_enumeration_type:
19996 parent_type = read_type_die (parent, cu);
19997 if (TYPE_DECLARED_CLASS (parent_type))
19998 {
19999 if (TYPE_TAG_NAME (parent_type) != NULL)
20000 return TYPE_TAG_NAME (parent_type);
20001 return "";
20002 }
20003 /* Fall through. */
20004 default:
20005 return determine_prefix (parent, cu);
20006 }
20007 }
20008
20009 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20010 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20011 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20012 an obconcat, otherwise allocate storage for the result. The CU argument is
20013 used to determine the language and hence, the appropriate separator. */
20014
20015 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20016
20017 static char *
20018 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20019 int physname, struct dwarf2_cu *cu)
20020 {
20021 const char *lead = "";
20022 const char *sep;
20023
20024 if (suffix == NULL || suffix[0] == '\0'
20025 || prefix == NULL || prefix[0] == '\0')
20026 sep = "";
20027 else if (cu->language == language_d)
20028 {
20029 /* For D, the 'main' function could be defined in any module, but it
20030 should never be prefixed. */
20031 if (strcmp (suffix, "D main") == 0)
20032 {
20033 prefix = "";
20034 sep = "";
20035 }
20036 else
20037 sep = ".";
20038 }
20039 else if (cu->language == language_fortran && physname)
20040 {
20041 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20042 DW_AT_MIPS_linkage_name is preferred and used instead. */
20043
20044 lead = "__";
20045 sep = "_MOD_";
20046 }
20047 else
20048 sep = "::";
20049
20050 if (prefix == NULL)
20051 prefix = "";
20052 if (suffix == NULL)
20053 suffix = "";
20054
20055 if (obs == NULL)
20056 {
20057 char *retval
20058 = ((char *)
20059 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20060
20061 strcpy (retval, lead);
20062 strcat (retval, prefix);
20063 strcat (retval, sep);
20064 strcat (retval, suffix);
20065 return retval;
20066 }
20067 else
20068 {
20069 /* We have an obstack. */
20070 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20071 }
20072 }
20073
20074 /* Return sibling of die, NULL if no sibling. */
20075
20076 static struct die_info *
20077 sibling_die (struct die_info *die)
20078 {
20079 return die->sibling;
20080 }
20081
20082 /* Get name of a die, return NULL if not found. */
20083
20084 static const char *
20085 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20086 struct obstack *obstack)
20087 {
20088 if (name && cu->language == language_cplus)
20089 {
20090 std::string canon_name = cp_canonicalize_string (name);
20091
20092 if (!canon_name.empty ())
20093 {
20094 if (canon_name != name)
20095 name = (const char *) obstack_copy0 (obstack,
20096 canon_name.c_str (),
20097 canon_name.length ());
20098 }
20099 }
20100
20101 return name;
20102 }
20103
20104 /* Get name of a die, return NULL if not found.
20105 Anonymous namespaces are converted to their magic string. */
20106
20107 static const char *
20108 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20109 {
20110 struct attribute *attr;
20111
20112 attr = dwarf2_attr (die, DW_AT_name, cu);
20113 if ((!attr || !DW_STRING (attr))
20114 && die->tag != DW_TAG_namespace
20115 && die->tag != DW_TAG_class_type
20116 && die->tag != DW_TAG_interface_type
20117 && die->tag != DW_TAG_structure_type
20118 && die->tag != DW_TAG_union_type)
20119 return NULL;
20120
20121 switch (die->tag)
20122 {
20123 case DW_TAG_compile_unit:
20124 case DW_TAG_partial_unit:
20125 /* Compilation units have a DW_AT_name that is a filename, not
20126 a source language identifier. */
20127 case DW_TAG_enumeration_type:
20128 case DW_TAG_enumerator:
20129 /* These tags always have simple identifiers already; no need
20130 to canonicalize them. */
20131 return DW_STRING (attr);
20132
20133 case DW_TAG_namespace:
20134 if (attr != NULL && DW_STRING (attr) != NULL)
20135 return DW_STRING (attr);
20136 return CP_ANONYMOUS_NAMESPACE_STR;
20137
20138 case DW_TAG_class_type:
20139 case DW_TAG_interface_type:
20140 case DW_TAG_structure_type:
20141 case DW_TAG_union_type:
20142 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20143 structures or unions. These were of the form "._%d" in GCC 4.1,
20144 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20145 and GCC 4.4. We work around this problem by ignoring these. */
20146 if (attr && DW_STRING (attr)
20147 && (startswith (DW_STRING (attr), "._")
20148 || startswith (DW_STRING (attr), "<anonymous")))
20149 return NULL;
20150
20151 /* GCC might emit a nameless typedef that has a linkage name. See
20152 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20153 if (!attr || DW_STRING (attr) == NULL)
20154 {
20155 char *demangled = NULL;
20156
20157 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20158 if (attr == NULL)
20159 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20160
20161 if (attr == NULL || DW_STRING (attr) == NULL)
20162 return NULL;
20163
20164 /* Avoid demangling DW_STRING (attr) the second time on a second
20165 call for the same DIE. */
20166 if (!DW_STRING_IS_CANONICAL (attr))
20167 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20168
20169 if (demangled)
20170 {
20171 const char *base;
20172
20173 /* FIXME: we already did this for the partial symbol... */
20174 DW_STRING (attr)
20175 = ((const char *)
20176 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20177 demangled, strlen (demangled)));
20178 DW_STRING_IS_CANONICAL (attr) = 1;
20179 xfree (demangled);
20180
20181 /* Strip any leading namespaces/classes, keep only the base name.
20182 DW_AT_name for named DIEs does not contain the prefixes. */
20183 base = strrchr (DW_STRING (attr), ':');
20184 if (base && base > DW_STRING (attr) && base[-1] == ':')
20185 return &base[1];
20186 else
20187 return DW_STRING (attr);
20188 }
20189 }
20190 break;
20191
20192 default:
20193 break;
20194 }
20195
20196 if (!DW_STRING_IS_CANONICAL (attr))
20197 {
20198 DW_STRING (attr)
20199 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20200 &cu->objfile->per_bfd->storage_obstack);
20201 DW_STRING_IS_CANONICAL (attr) = 1;
20202 }
20203 return DW_STRING (attr);
20204 }
20205
20206 /* Return the die that this die in an extension of, or NULL if there
20207 is none. *EXT_CU is the CU containing DIE on input, and the CU
20208 containing the return value on output. */
20209
20210 static struct die_info *
20211 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20212 {
20213 struct attribute *attr;
20214
20215 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20216 if (attr == NULL)
20217 return NULL;
20218
20219 return follow_die_ref (die, attr, ext_cu);
20220 }
20221
20222 /* Convert a DIE tag into its string name. */
20223
20224 static const char *
20225 dwarf_tag_name (unsigned tag)
20226 {
20227 const char *name = get_DW_TAG_name (tag);
20228
20229 if (name == NULL)
20230 return "DW_TAG_<unknown>";
20231
20232 return name;
20233 }
20234
20235 /* Convert a DWARF attribute code into its string name. */
20236
20237 static const char *
20238 dwarf_attr_name (unsigned attr)
20239 {
20240 const char *name;
20241
20242 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20243 if (attr == DW_AT_MIPS_fde)
20244 return "DW_AT_MIPS_fde";
20245 #else
20246 if (attr == DW_AT_HP_block_index)
20247 return "DW_AT_HP_block_index";
20248 #endif
20249
20250 name = get_DW_AT_name (attr);
20251
20252 if (name == NULL)
20253 return "DW_AT_<unknown>";
20254
20255 return name;
20256 }
20257
20258 /* Convert a DWARF value form code into its string name. */
20259
20260 static const char *
20261 dwarf_form_name (unsigned form)
20262 {
20263 const char *name = get_DW_FORM_name (form);
20264
20265 if (name == NULL)
20266 return "DW_FORM_<unknown>";
20267
20268 return name;
20269 }
20270
20271 static const char *
20272 dwarf_bool_name (unsigned mybool)
20273 {
20274 if (mybool)
20275 return "TRUE";
20276 else
20277 return "FALSE";
20278 }
20279
20280 /* Convert a DWARF type code into its string name. */
20281
20282 static const char *
20283 dwarf_type_encoding_name (unsigned enc)
20284 {
20285 const char *name = get_DW_ATE_name (enc);
20286
20287 if (name == NULL)
20288 return "DW_ATE_<unknown>";
20289
20290 return name;
20291 }
20292
20293 static void
20294 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20295 {
20296 unsigned int i;
20297
20298 print_spaces (indent, f);
20299 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20300 dwarf_tag_name (die->tag), die->abbrev,
20301 to_underlying (die->sect_off));
20302
20303 if (die->parent != NULL)
20304 {
20305 print_spaces (indent, f);
20306 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20307 to_underlying (die->parent->sect_off));
20308 }
20309
20310 print_spaces (indent, f);
20311 fprintf_unfiltered (f, " has children: %s\n",
20312 dwarf_bool_name (die->child != NULL));
20313
20314 print_spaces (indent, f);
20315 fprintf_unfiltered (f, " attributes:\n");
20316
20317 for (i = 0; i < die->num_attrs; ++i)
20318 {
20319 print_spaces (indent, f);
20320 fprintf_unfiltered (f, " %s (%s) ",
20321 dwarf_attr_name (die->attrs[i].name),
20322 dwarf_form_name (die->attrs[i].form));
20323
20324 switch (die->attrs[i].form)
20325 {
20326 case DW_FORM_addr:
20327 case DW_FORM_GNU_addr_index:
20328 fprintf_unfiltered (f, "address: ");
20329 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20330 break;
20331 case DW_FORM_block2:
20332 case DW_FORM_block4:
20333 case DW_FORM_block:
20334 case DW_FORM_block1:
20335 fprintf_unfiltered (f, "block: size %s",
20336 pulongest (DW_BLOCK (&die->attrs[i])->size));
20337 break;
20338 case DW_FORM_exprloc:
20339 fprintf_unfiltered (f, "expression: size %s",
20340 pulongest (DW_BLOCK (&die->attrs[i])->size));
20341 break;
20342 case DW_FORM_data16:
20343 fprintf_unfiltered (f, "constant of 16 bytes");
20344 break;
20345 case DW_FORM_ref_addr:
20346 fprintf_unfiltered (f, "ref address: ");
20347 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20348 break;
20349 case DW_FORM_GNU_ref_alt:
20350 fprintf_unfiltered (f, "alt ref address: ");
20351 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20352 break;
20353 case DW_FORM_ref1:
20354 case DW_FORM_ref2:
20355 case DW_FORM_ref4:
20356 case DW_FORM_ref8:
20357 case DW_FORM_ref_udata:
20358 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20359 (long) (DW_UNSND (&die->attrs[i])));
20360 break;
20361 case DW_FORM_data1:
20362 case DW_FORM_data2:
20363 case DW_FORM_data4:
20364 case DW_FORM_data8:
20365 case DW_FORM_udata:
20366 case DW_FORM_sdata:
20367 fprintf_unfiltered (f, "constant: %s",
20368 pulongest (DW_UNSND (&die->attrs[i])));
20369 break;
20370 case DW_FORM_sec_offset:
20371 fprintf_unfiltered (f, "section offset: %s",
20372 pulongest (DW_UNSND (&die->attrs[i])));
20373 break;
20374 case DW_FORM_ref_sig8:
20375 fprintf_unfiltered (f, "signature: %s",
20376 hex_string (DW_SIGNATURE (&die->attrs[i])));
20377 break;
20378 case DW_FORM_string:
20379 case DW_FORM_strp:
20380 case DW_FORM_line_strp:
20381 case DW_FORM_GNU_str_index:
20382 case DW_FORM_GNU_strp_alt:
20383 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20384 DW_STRING (&die->attrs[i])
20385 ? DW_STRING (&die->attrs[i]) : "",
20386 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20387 break;
20388 case DW_FORM_flag:
20389 if (DW_UNSND (&die->attrs[i]))
20390 fprintf_unfiltered (f, "flag: TRUE");
20391 else
20392 fprintf_unfiltered (f, "flag: FALSE");
20393 break;
20394 case DW_FORM_flag_present:
20395 fprintf_unfiltered (f, "flag: TRUE");
20396 break;
20397 case DW_FORM_indirect:
20398 /* The reader will have reduced the indirect form to
20399 the "base form" so this form should not occur. */
20400 fprintf_unfiltered (f,
20401 "unexpected attribute form: DW_FORM_indirect");
20402 break;
20403 default:
20404 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20405 die->attrs[i].form);
20406 break;
20407 }
20408 fprintf_unfiltered (f, "\n");
20409 }
20410 }
20411
20412 static void
20413 dump_die_for_error (struct die_info *die)
20414 {
20415 dump_die_shallow (gdb_stderr, 0, die);
20416 }
20417
20418 static void
20419 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20420 {
20421 int indent = level * 4;
20422
20423 gdb_assert (die != NULL);
20424
20425 if (level >= max_level)
20426 return;
20427
20428 dump_die_shallow (f, indent, die);
20429
20430 if (die->child != NULL)
20431 {
20432 print_spaces (indent, f);
20433 fprintf_unfiltered (f, " Children:");
20434 if (level + 1 < max_level)
20435 {
20436 fprintf_unfiltered (f, "\n");
20437 dump_die_1 (f, level + 1, max_level, die->child);
20438 }
20439 else
20440 {
20441 fprintf_unfiltered (f,
20442 " [not printed, max nesting level reached]\n");
20443 }
20444 }
20445
20446 if (die->sibling != NULL && level > 0)
20447 {
20448 dump_die_1 (f, level, max_level, die->sibling);
20449 }
20450 }
20451
20452 /* This is called from the pdie macro in gdbinit.in.
20453 It's not static so gcc will keep a copy callable from gdb. */
20454
20455 void
20456 dump_die (struct die_info *die, int max_level)
20457 {
20458 dump_die_1 (gdb_stdlog, 0, max_level, die);
20459 }
20460
20461 static void
20462 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20463 {
20464 void **slot;
20465
20466 slot = htab_find_slot_with_hash (cu->die_hash, die,
20467 to_underlying (die->sect_off),
20468 INSERT);
20469
20470 *slot = die;
20471 }
20472
20473 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20474 required kind. */
20475
20476 static sect_offset
20477 dwarf2_get_ref_die_offset (const struct attribute *attr)
20478 {
20479 if (attr_form_is_ref (attr))
20480 return (sect_offset) DW_UNSND (attr);
20481
20482 complaint (&symfile_complaints,
20483 _("unsupported die ref attribute form: '%s'"),
20484 dwarf_form_name (attr->form));
20485 return {};
20486 }
20487
20488 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20489 * the value held by the attribute is not constant. */
20490
20491 static LONGEST
20492 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20493 {
20494 if (attr->form == DW_FORM_sdata)
20495 return DW_SND (attr);
20496 else if (attr->form == DW_FORM_udata
20497 || attr->form == DW_FORM_data1
20498 || attr->form == DW_FORM_data2
20499 || attr->form == DW_FORM_data4
20500 || attr->form == DW_FORM_data8)
20501 return DW_UNSND (attr);
20502 else
20503 {
20504 /* For DW_FORM_data16 see attr_form_is_constant. */
20505 complaint (&symfile_complaints,
20506 _("Attribute value is not a constant (%s)"),
20507 dwarf_form_name (attr->form));
20508 return default_value;
20509 }
20510 }
20511
20512 /* Follow reference or signature attribute ATTR of SRC_DIE.
20513 On entry *REF_CU is the CU of SRC_DIE.
20514 On exit *REF_CU is the CU of the result. */
20515
20516 static struct die_info *
20517 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20518 struct dwarf2_cu **ref_cu)
20519 {
20520 struct die_info *die;
20521
20522 if (attr_form_is_ref (attr))
20523 die = follow_die_ref (src_die, attr, ref_cu);
20524 else if (attr->form == DW_FORM_ref_sig8)
20525 die = follow_die_sig (src_die, attr, ref_cu);
20526 else
20527 {
20528 dump_die_for_error (src_die);
20529 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20530 objfile_name ((*ref_cu)->objfile));
20531 }
20532
20533 return die;
20534 }
20535
20536 /* Follow reference OFFSET.
20537 On entry *REF_CU is the CU of the source die referencing OFFSET.
20538 On exit *REF_CU is the CU of the result.
20539 Returns NULL if OFFSET is invalid. */
20540
20541 static struct die_info *
20542 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20543 struct dwarf2_cu **ref_cu)
20544 {
20545 struct die_info temp_die;
20546 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20547
20548 gdb_assert (cu->per_cu != NULL);
20549
20550 target_cu = cu;
20551
20552 if (cu->per_cu->is_debug_types)
20553 {
20554 /* .debug_types CUs cannot reference anything outside their CU.
20555 If they need to, they have to reference a signatured type via
20556 DW_FORM_ref_sig8. */
20557 if (!offset_in_cu_p (&cu->header, sect_off))
20558 return NULL;
20559 }
20560 else if (offset_in_dwz != cu->per_cu->is_dwz
20561 || !offset_in_cu_p (&cu->header, sect_off))
20562 {
20563 struct dwarf2_per_cu_data *per_cu;
20564
20565 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20566 cu->objfile);
20567
20568 /* If necessary, add it to the queue and load its DIEs. */
20569 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20570 load_full_comp_unit (per_cu, cu->language);
20571
20572 target_cu = per_cu->cu;
20573 }
20574 else if (cu->dies == NULL)
20575 {
20576 /* We're loading full DIEs during partial symbol reading. */
20577 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20578 load_full_comp_unit (cu->per_cu, language_minimal);
20579 }
20580
20581 *ref_cu = target_cu;
20582 temp_die.sect_off = sect_off;
20583 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20584 &temp_die,
20585 to_underlying (sect_off));
20586 }
20587
20588 /* Follow reference attribute ATTR of SRC_DIE.
20589 On entry *REF_CU is the CU of SRC_DIE.
20590 On exit *REF_CU is the CU of the result. */
20591
20592 static struct die_info *
20593 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20594 struct dwarf2_cu **ref_cu)
20595 {
20596 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20597 struct dwarf2_cu *cu = *ref_cu;
20598 struct die_info *die;
20599
20600 die = follow_die_offset (sect_off,
20601 (attr->form == DW_FORM_GNU_ref_alt
20602 || cu->per_cu->is_dwz),
20603 ref_cu);
20604 if (!die)
20605 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20606 "at 0x%x [in module %s]"),
20607 to_underlying (sect_off), to_underlying (src_die->sect_off),
20608 objfile_name (cu->objfile));
20609
20610 return die;
20611 }
20612
20613 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20614 Returned value is intended for DW_OP_call*. Returned
20615 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20616
20617 struct dwarf2_locexpr_baton
20618 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20619 struct dwarf2_per_cu_data *per_cu,
20620 CORE_ADDR (*get_frame_pc) (void *baton),
20621 void *baton)
20622 {
20623 struct dwarf2_cu *cu;
20624 struct die_info *die;
20625 struct attribute *attr;
20626 struct dwarf2_locexpr_baton retval;
20627
20628 dw2_setup (per_cu->objfile);
20629
20630 if (per_cu->cu == NULL)
20631 load_cu (per_cu);
20632 cu = per_cu->cu;
20633 if (cu == NULL)
20634 {
20635 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20636 Instead just throw an error, not much else we can do. */
20637 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20638 to_underlying (sect_off), objfile_name (per_cu->objfile));
20639 }
20640
20641 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20642 if (!die)
20643 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20644 to_underlying (sect_off), objfile_name (per_cu->objfile));
20645
20646 attr = dwarf2_attr (die, DW_AT_location, cu);
20647 if (!attr)
20648 {
20649 /* DWARF: "If there is no such attribute, then there is no effect.".
20650 DATA is ignored if SIZE is 0. */
20651
20652 retval.data = NULL;
20653 retval.size = 0;
20654 }
20655 else if (attr_form_is_section_offset (attr))
20656 {
20657 struct dwarf2_loclist_baton loclist_baton;
20658 CORE_ADDR pc = (*get_frame_pc) (baton);
20659 size_t size;
20660
20661 fill_in_loclist_baton (cu, &loclist_baton, attr);
20662
20663 retval.data = dwarf2_find_location_expression (&loclist_baton,
20664 &size, pc);
20665 retval.size = size;
20666 }
20667 else
20668 {
20669 if (!attr_form_is_block (attr))
20670 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20671 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20672 to_underlying (sect_off), objfile_name (per_cu->objfile));
20673
20674 retval.data = DW_BLOCK (attr)->data;
20675 retval.size = DW_BLOCK (attr)->size;
20676 }
20677 retval.per_cu = cu->per_cu;
20678
20679 age_cached_comp_units ();
20680
20681 return retval;
20682 }
20683
20684 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20685 offset. */
20686
20687 struct dwarf2_locexpr_baton
20688 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20689 struct dwarf2_per_cu_data *per_cu,
20690 CORE_ADDR (*get_frame_pc) (void *baton),
20691 void *baton)
20692 {
20693 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20694
20695 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20696 }
20697
20698 /* Write a constant of a given type as target-ordered bytes into
20699 OBSTACK. */
20700
20701 static const gdb_byte *
20702 write_constant_as_bytes (struct obstack *obstack,
20703 enum bfd_endian byte_order,
20704 struct type *type,
20705 ULONGEST value,
20706 LONGEST *len)
20707 {
20708 gdb_byte *result;
20709
20710 *len = TYPE_LENGTH (type);
20711 result = (gdb_byte *) obstack_alloc (obstack, *len);
20712 store_unsigned_integer (result, *len, byte_order, value);
20713
20714 return result;
20715 }
20716
20717 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20718 pointer to the constant bytes and set LEN to the length of the
20719 data. If memory is needed, allocate it on OBSTACK. If the DIE
20720 does not have a DW_AT_const_value, return NULL. */
20721
20722 const gdb_byte *
20723 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20724 struct dwarf2_per_cu_data *per_cu,
20725 struct obstack *obstack,
20726 LONGEST *len)
20727 {
20728 struct dwarf2_cu *cu;
20729 struct die_info *die;
20730 struct attribute *attr;
20731 const gdb_byte *result = NULL;
20732 struct type *type;
20733 LONGEST value;
20734 enum bfd_endian byte_order;
20735
20736 dw2_setup (per_cu->objfile);
20737
20738 if (per_cu->cu == NULL)
20739 load_cu (per_cu);
20740 cu = per_cu->cu;
20741 if (cu == NULL)
20742 {
20743 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20744 Instead just throw an error, not much else we can do. */
20745 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20746 to_underlying (sect_off), objfile_name (per_cu->objfile));
20747 }
20748
20749 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20750 if (!die)
20751 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20752 to_underlying (sect_off), objfile_name (per_cu->objfile));
20753
20754
20755 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20756 if (attr == NULL)
20757 return NULL;
20758
20759 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20760 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20761
20762 switch (attr->form)
20763 {
20764 case DW_FORM_addr:
20765 case DW_FORM_GNU_addr_index:
20766 {
20767 gdb_byte *tem;
20768
20769 *len = cu->header.addr_size;
20770 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20771 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20772 result = tem;
20773 }
20774 break;
20775 case DW_FORM_string:
20776 case DW_FORM_strp:
20777 case DW_FORM_GNU_str_index:
20778 case DW_FORM_GNU_strp_alt:
20779 /* DW_STRING is already allocated on the objfile obstack, point
20780 directly to it. */
20781 result = (const gdb_byte *) DW_STRING (attr);
20782 *len = strlen (DW_STRING (attr));
20783 break;
20784 case DW_FORM_block1:
20785 case DW_FORM_block2:
20786 case DW_FORM_block4:
20787 case DW_FORM_block:
20788 case DW_FORM_exprloc:
20789 case DW_FORM_data16:
20790 result = DW_BLOCK (attr)->data;
20791 *len = DW_BLOCK (attr)->size;
20792 break;
20793
20794 /* The DW_AT_const_value attributes are supposed to carry the
20795 symbol's value "represented as it would be on the target
20796 architecture." By the time we get here, it's already been
20797 converted to host endianness, so we just need to sign- or
20798 zero-extend it as appropriate. */
20799 case DW_FORM_data1:
20800 type = die_type (die, cu);
20801 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20802 if (result == NULL)
20803 result = write_constant_as_bytes (obstack, byte_order,
20804 type, value, len);
20805 break;
20806 case DW_FORM_data2:
20807 type = die_type (die, cu);
20808 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20809 if (result == NULL)
20810 result = write_constant_as_bytes (obstack, byte_order,
20811 type, value, len);
20812 break;
20813 case DW_FORM_data4:
20814 type = die_type (die, cu);
20815 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20816 if (result == NULL)
20817 result = write_constant_as_bytes (obstack, byte_order,
20818 type, value, len);
20819 break;
20820 case DW_FORM_data8:
20821 type = die_type (die, cu);
20822 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20823 if (result == NULL)
20824 result = write_constant_as_bytes (obstack, byte_order,
20825 type, value, len);
20826 break;
20827
20828 case DW_FORM_sdata:
20829 type = die_type (die, cu);
20830 result = write_constant_as_bytes (obstack, byte_order,
20831 type, DW_SND (attr), len);
20832 break;
20833
20834 case DW_FORM_udata:
20835 type = die_type (die, cu);
20836 result = write_constant_as_bytes (obstack, byte_order,
20837 type, DW_UNSND (attr), len);
20838 break;
20839
20840 default:
20841 complaint (&symfile_complaints,
20842 _("unsupported const value attribute form: '%s'"),
20843 dwarf_form_name (attr->form));
20844 break;
20845 }
20846
20847 return result;
20848 }
20849
20850 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20851 valid type for this die is found. */
20852
20853 struct type *
20854 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20855 struct dwarf2_per_cu_data *per_cu)
20856 {
20857 struct dwarf2_cu *cu;
20858 struct die_info *die;
20859
20860 dw2_setup (per_cu->objfile);
20861
20862 if (per_cu->cu == NULL)
20863 load_cu (per_cu);
20864 cu = per_cu->cu;
20865 if (!cu)
20866 return NULL;
20867
20868 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20869 if (!die)
20870 return NULL;
20871
20872 return die_type (die, cu);
20873 }
20874
20875 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20876 PER_CU. */
20877
20878 struct type *
20879 dwarf2_get_die_type (cu_offset die_offset,
20880 struct dwarf2_per_cu_data *per_cu)
20881 {
20882 dw2_setup (per_cu->objfile);
20883
20884 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20885 return get_die_type_at_offset (die_offset_sect, per_cu);
20886 }
20887
20888 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20889 On entry *REF_CU is the CU of SRC_DIE.
20890 On exit *REF_CU is the CU of the result.
20891 Returns NULL if the referenced DIE isn't found. */
20892
20893 static struct die_info *
20894 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20895 struct dwarf2_cu **ref_cu)
20896 {
20897 struct die_info temp_die;
20898 struct dwarf2_cu *sig_cu;
20899 struct die_info *die;
20900
20901 /* While it might be nice to assert sig_type->type == NULL here,
20902 we can get here for DW_AT_imported_declaration where we need
20903 the DIE not the type. */
20904
20905 /* If necessary, add it to the queue and load its DIEs. */
20906
20907 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20908 read_signatured_type (sig_type);
20909
20910 sig_cu = sig_type->per_cu.cu;
20911 gdb_assert (sig_cu != NULL);
20912 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20913 temp_die.sect_off = sig_type->type_offset_in_section;
20914 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20915 to_underlying (temp_die.sect_off));
20916 if (die)
20917 {
20918 /* For .gdb_index version 7 keep track of included TUs.
20919 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20920 if (dwarf2_per_objfile->index_table != NULL
20921 && dwarf2_per_objfile->index_table->version <= 7)
20922 {
20923 VEC_safe_push (dwarf2_per_cu_ptr,
20924 (*ref_cu)->per_cu->imported_symtabs,
20925 sig_cu->per_cu);
20926 }
20927
20928 *ref_cu = sig_cu;
20929 return die;
20930 }
20931
20932 return NULL;
20933 }
20934
20935 /* Follow signatured type referenced by ATTR in SRC_DIE.
20936 On entry *REF_CU is the CU of SRC_DIE.
20937 On exit *REF_CU is the CU of the result.
20938 The result is the DIE of the type.
20939 If the referenced type cannot be found an error is thrown. */
20940
20941 static struct die_info *
20942 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20943 struct dwarf2_cu **ref_cu)
20944 {
20945 ULONGEST signature = DW_SIGNATURE (attr);
20946 struct signatured_type *sig_type;
20947 struct die_info *die;
20948
20949 gdb_assert (attr->form == DW_FORM_ref_sig8);
20950
20951 sig_type = lookup_signatured_type (*ref_cu, signature);
20952 /* sig_type will be NULL if the signatured type is missing from
20953 the debug info. */
20954 if (sig_type == NULL)
20955 {
20956 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20957 " from DIE at 0x%x [in module %s]"),
20958 hex_string (signature), to_underlying (src_die->sect_off),
20959 objfile_name ((*ref_cu)->objfile));
20960 }
20961
20962 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20963 if (die == NULL)
20964 {
20965 dump_die_for_error (src_die);
20966 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20967 " from DIE at 0x%x [in module %s]"),
20968 hex_string (signature), to_underlying (src_die->sect_off),
20969 objfile_name ((*ref_cu)->objfile));
20970 }
20971
20972 return die;
20973 }
20974
20975 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20976 reading in and processing the type unit if necessary. */
20977
20978 static struct type *
20979 get_signatured_type (struct die_info *die, ULONGEST signature,
20980 struct dwarf2_cu *cu)
20981 {
20982 struct signatured_type *sig_type;
20983 struct dwarf2_cu *type_cu;
20984 struct die_info *type_die;
20985 struct type *type;
20986
20987 sig_type = lookup_signatured_type (cu, signature);
20988 /* sig_type will be NULL if the signatured type is missing from
20989 the debug info. */
20990 if (sig_type == NULL)
20991 {
20992 complaint (&symfile_complaints,
20993 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20994 " from DIE at 0x%x [in module %s]"),
20995 hex_string (signature), to_underlying (die->sect_off),
20996 objfile_name (dwarf2_per_objfile->objfile));
20997 return build_error_marker_type (cu, die);
20998 }
20999
21000 /* If we already know the type we're done. */
21001 if (sig_type->type != NULL)
21002 return sig_type->type;
21003
21004 type_cu = cu;
21005 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21006 if (type_die != NULL)
21007 {
21008 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21009 is created. This is important, for example, because for c++ classes
21010 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21011 type = read_type_die (type_die, type_cu);
21012 if (type == NULL)
21013 {
21014 complaint (&symfile_complaints,
21015 _("Dwarf Error: Cannot build signatured type %s"
21016 " referenced from DIE at 0x%x [in module %s]"),
21017 hex_string (signature), to_underlying (die->sect_off),
21018 objfile_name (dwarf2_per_objfile->objfile));
21019 type = build_error_marker_type (cu, die);
21020 }
21021 }
21022 else
21023 {
21024 complaint (&symfile_complaints,
21025 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21026 " from DIE at 0x%x [in module %s]"),
21027 hex_string (signature), to_underlying (die->sect_off),
21028 objfile_name (dwarf2_per_objfile->objfile));
21029 type = build_error_marker_type (cu, die);
21030 }
21031 sig_type->type = type;
21032
21033 return type;
21034 }
21035
21036 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21037 reading in and processing the type unit if necessary. */
21038
21039 static struct type *
21040 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21041 struct dwarf2_cu *cu) /* ARI: editCase function */
21042 {
21043 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21044 if (attr_form_is_ref (attr))
21045 {
21046 struct dwarf2_cu *type_cu = cu;
21047 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21048
21049 return read_type_die (type_die, type_cu);
21050 }
21051 else if (attr->form == DW_FORM_ref_sig8)
21052 {
21053 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21054 }
21055 else
21056 {
21057 complaint (&symfile_complaints,
21058 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21059 " at 0x%x [in module %s]"),
21060 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21061 objfile_name (dwarf2_per_objfile->objfile));
21062 return build_error_marker_type (cu, die);
21063 }
21064 }
21065
21066 /* Load the DIEs associated with type unit PER_CU into memory. */
21067
21068 static void
21069 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21070 {
21071 struct signatured_type *sig_type;
21072
21073 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21074 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21075
21076 /* We have the per_cu, but we need the signatured_type.
21077 Fortunately this is an easy translation. */
21078 gdb_assert (per_cu->is_debug_types);
21079 sig_type = (struct signatured_type *) per_cu;
21080
21081 gdb_assert (per_cu->cu == NULL);
21082
21083 read_signatured_type (sig_type);
21084
21085 gdb_assert (per_cu->cu != NULL);
21086 }
21087
21088 /* die_reader_func for read_signatured_type.
21089 This is identical to load_full_comp_unit_reader,
21090 but is kept separate for now. */
21091
21092 static void
21093 read_signatured_type_reader (const struct die_reader_specs *reader,
21094 const gdb_byte *info_ptr,
21095 struct die_info *comp_unit_die,
21096 int has_children,
21097 void *data)
21098 {
21099 struct dwarf2_cu *cu = reader->cu;
21100
21101 gdb_assert (cu->die_hash == NULL);
21102 cu->die_hash =
21103 htab_create_alloc_ex (cu->header.length / 12,
21104 die_hash,
21105 die_eq,
21106 NULL,
21107 &cu->comp_unit_obstack,
21108 hashtab_obstack_allocate,
21109 dummy_obstack_deallocate);
21110
21111 if (has_children)
21112 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21113 &info_ptr, comp_unit_die);
21114 cu->dies = comp_unit_die;
21115 /* comp_unit_die is not stored in die_hash, no need. */
21116
21117 /* We try not to read any attributes in this function, because not
21118 all CUs needed for references have been loaded yet, and symbol
21119 table processing isn't initialized. But we have to set the CU language,
21120 or we won't be able to build types correctly.
21121 Similarly, if we do not read the producer, we can not apply
21122 producer-specific interpretation. */
21123 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21124 }
21125
21126 /* Read in a signatured type and build its CU and DIEs.
21127 If the type is a stub for the real type in a DWO file,
21128 read in the real type from the DWO file as well. */
21129
21130 static void
21131 read_signatured_type (struct signatured_type *sig_type)
21132 {
21133 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21134
21135 gdb_assert (per_cu->is_debug_types);
21136 gdb_assert (per_cu->cu == NULL);
21137
21138 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21139 read_signatured_type_reader, NULL);
21140 sig_type->per_cu.tu_read = 1;
21141 }
21142
21143 /* Decode simple location descriptions.
21144 Given a pointer to a dwarf block that defines a location, compute
21145 the location and return the value.
21146
21147 NOTE drow/2003-11-18: This function is called in two situations
21148 now: for the address of static or global variables (partial symbols
21149 only) and for offsets into structures which are expected to be
21150 (more or less) constant. The partial symbol case should go away,
21151 and only the constant case should remain. That will let this
21152 function complain more accurately. A few special modes are allowed
21153 without complaint for global variables (for instance, global
21154 register values and thread-local values).
21155
21156 A location description containing no operations indicates that the
21157 object is optimized out. The return value is 0 for that case.
21158 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21159 callers will only want a very basic result and this can become a
21160 complaint.
21161
21162 Note that stack[0] is unused except as a default error return. */
21163
21164 static CORE_ADDR
21165 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21166 {
21167 struct objfile *objfile = cu->objfile;
21168 size_t i;
21169 size_t size = blk->size;
21170 const gdb_byte *data = blk->data;
21171 CORE_ADDR stack[64];
21172 int stacki;
21173 unsigned int bytes_read, unsnd;
21174 gdb_byte op;
21175
21176 i = 0;
21177 stacki = 0;
21178 stack[stacki] = 0;
21179 stack[++stacki] = 0;
21180
21181 while (i < size)
21182 {
21183 op = data[i++];
21184 switch (op)
21185 {
21186 case DW_OP_lit0:
21187 case DW_OP_lit1:
21188 case DW_OP_lit2:
21189 case DW_OP_lit3:
21190 case DW_OP_lit4:
21191 case DW_OP_lit5:
21192 case DW_OP_lit6:
21193 case DW_OP_lit7:
21194 case DW_OP_lit8:
21195 case DW_OP_lit9:
21196 case DW_OP_lit10:
21197 case DW_OP_lit11:
21198 case DW_OP_lit12:
21199 case DW_OP_lit13:
21200 case DW_OP_lit14:
21201 case DW_OP_lit15:
21202 case DW_OP_lit16:
21203 case DW_OP_lit17:
21204 case DW_OP_lit18:
21205 case DW_OP_lit19:
21206 case DW_OP_lit20:
21207 case DW_OP_lit21:
21208 case DW_OP_lit22:
21209 case DW_OP_lit23:
21210 case DW_OP_lit24:
21211 case DW_OP_lit25:
21212 case DW_OP_lit26:
21213 case DW_OP_lit27:
21214 case DW_OP_lit28:
21215 case DW_OP_lit29:
21216 case DW_OP_lit30:
21217 case DW_OP_lit31:
21218 stack[++stacki] = op - DW_OP_lit0;
21219 break;
21220
21221 case DW_OP_reg0:
21222 case DW_OP_reg1:
21223 case DW_OP_reg2:
21224 case DW_OP_reg3:
21225 case DW_OP_reg4:
21226 case DW_OP_reg5:
21227 case DW_OP_reg6:
21228 case DW_OP_reg7:
21229 case DW_OP_reg8:
21230 case DW_OP_reg9:
21231 case DW_OP_reg10:
21232 case DW_OP_reg11:
21233 case DW_OP_reg12:
21234 case DW_OP_reg13:
21235 case DW_OP_reg14:
21236 case DW_OP_reg15:
21237 case DW_OP_reg16:
21238 case DW_OP_reg17:
21239 case DW_OP_reg18:
21240 case DW_OP_reg19:
21241 case DW_OP_reg20:
21242 case DW_OP_reg21:
21243 case DW_OP_reg22:
21244 case DW_OP_reg23:
21245 case DW_OP_reg24:
21246 case DW_OP_reg25:
21247 case DW_OP_reg26:
21248 case DW_OP_reg27:
21249 case DW_OP_reg28:
21250 case DW_OP_reg29:
21251 case DW_OP_reg30:
21252 case DW_OP_reg31:
21253 stack[++stacki] = op - DW_OP_reg0;
21254 if (i < size)
21255 dwarf2_complex_location_expr_complaint ();
21256 break;
21257
21258 case DW_OP_regx:
21259 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21260 i += bytes_read;
21261 stack[++stacki] = unsnd;
21262 if (i < size)
21263 dwarf2_complex_location_expr_complaint ();
21264 break;
21265
21266 case DW_OP_addr:
21267 stack[++stacki] = read_address (objfile->obfd, &data[i],
21268 cu, &bytes_read);
21269 i += bytes_read;
21270 break;
21271
21272 case DW_OP_const1u:
21273 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21274 i += 1;
21275 break;
21276
21277 case DW_OP_const1s:
21278 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21279 i += 1;
21280 break;
21281
21282 case DW_OP_const2u:
21283 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21284 i += 2;
21285 break;
21286
21287 case DW_OP_const2s:
21288 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21289 i += 2;
21290 break;
21291
21292 case DW_OP_const4u:
21293 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21294 i += 4;
21295 break;
21296
21297 case DW_OP_const4s:
21298 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21299 i += 4;
21300 break;
21301
21302 case DW_OP_const8u:
21303 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21304 i += 8;
21305 break;
21306
21307 case DW_OP_constu:
21308 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21309 &bytes_read);
21310 i += bytes_read;
21311 break;
21312
21313 case DW_OP_consts:
21314 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21315 i += bytes_read;
21316 break;
21317
21318 case DW_OP_dup:
21319 stack[stacki + 1] = stack[stacki];
21320 stacki++;
21321 break;
21322
21323 case DW_OP_plus:
21324 stack[stacki - 1] += stack[stacki];
21325 stacki--;
21326 break;
21327
21328 case DW_OP_plus_uconst:
21329 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21330 &bytes_read);
21331 i += bytes_read;
21332 break;
21333
21334 case DW_OP_minus:
21335 stack[stacki - 1] -= stack[stacki];
21336 stacki--;
21337 break;
21338
21339 case DW_OP_deref:
21340 /* If we're not the last op, then we definitely can't encode
21341 this using GDB's address_class enum. This is valid for partial
21342 global symbols, although the variable's address will be bogus
21343 in the psymtab. */
21344 if (i < size)
21345 dwarf2_complex_location_expr_complaint ();
21346 break;
21347
21348 case DW_OP_GNU_push_tls_address:
21349 case DW_OP_form_tls_address:
21350 /* The top of the stack has the offset from the beginning
21351 of the thread control block at which the variable is located. */
21352 /* Nothing should follow this operator, so the top of stack would
21353 be returned. */
21354 /* This is valid for partial global symbols, but the variable's
21355 address will be bogus in the psymtab. Make it always at least
21356 non-zero to not look as a variable garbage collected by linker
21357 which have DW_OP_addr 0. */
21358 if (i < size)
21359 dwarf2_complex_location_expr_complaint ();
21360 stack[stacki]++;
21361 break;
21362
21363 case DW_OP_GNU_uninit:
21364 break;
21365
21366 case DW_OP_GNU_addr_index:
21367 case DW_OP_GNU_const_index:
21368 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21369 &bytes_read);
21370 i += bytes_read;
21371 break;
21372
21373 default:
21374 {
21375 const char *name = get_DW_OP_name (op);
21376
21377 if (name)
21378 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21379 name);
21380 else
21381 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21382 op);
21383 }
21384
21385 return (stack[stacki]);
21386 }
21387
21388 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21389 outside of the allocated space. Also enforce minimum>0. */
21390 if (stacki >= ARRAY_SIZE (stack) - 1)
21391 {
21392 complaint (&symfile_complaints,
21393 _("location description stack overflow"));
21394 return 0;
21395 }
21396
21397 if (stacki <= 0)
21398 {
21399 complaint (&symfile_complaints,
21400 _("location description stack underflow"));
21401 return 0;
21402 }
21403 }
21404 return (stack[stacki]);
21405 }
21406
21407 /* memory allocation interface */
21408
21409 static struct dwarf_block *
21410 dwarf_alloc_block (struct dwarf2_cu *cu)
21411 {
21412 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21413 }
21414
21415 static struct die_info *
21416 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21417 {
21418 struct die_info *die;
21419 size_t size = sizeof (struct die_info);
21420
21421 if (num_attrs > 1)
21422 size += (num_attrs - 1) * sizeof (struct attribute);
21423
21424 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21425 memset (die, 0, sizeof (struct die_info));
21426 return (die);
21427 }
21428
21429 \f
21430 /* Macro support. */
21431
21432 /* Return file name relative to the compilation directory of file number I in
21433 *LH's file name table. The result is allocated using xmalloc; the caller is
21434 responsible for freeing it. */
21435
21436 static char *
21437 file_file_name (int file, struct line_header *lh)
21438 {
21439 /* Is the file number a valid index into the line header's file name
21440 table? Remember that file numbers start with one, not zero. */
21441 if (1 <= file && file <= lh->file_names.size ())
21442 {
21443 const file_entry &fe = lh->file_names[file - 1];
21444
21445 if (!IS_ABSOLUTE_PATH (fe.name))
21446 {
21447 const char *dir = fe.include_dir (lh);
21448 if (dir != NULL)
21449 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21450 }
21451 return xstrdup (fe.name);
21452 }
21453 else
21454 {
21455 /* The compiler produced a bogus file number. We can at least
21456 record the macro definitions made in the file, even if we
21457 won't be able to find the file by name. */
21458 char fake_name[80];
21459
21460 xsnprintf (fake_name, sizeof (fake_name),
21461 "<bad macro file number %d>", file);
21462
21463 complaint (&symfile_complaints,
21464 _("bad file number in macro information (%d)"),
21465 file);
21466
21467 return xstrdup (fake_name);
21468 }
21469 }
21470
21471 /* Return the full name of file number I in *LH's file name table.
21472 Use COMP_DIR as the name of the current directory of the
21473 compilation. The result is allocated using xmalloc; the caller is
21474 responsible for freeing it. */
21475 static char *
21476 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21477 {
21478 /* Is the file number a valid index into the line header's file name
21479 table? Remember that file numbers start with one, not zero. */
21480 if (1 <= file && file <= lh->file_names.size ())
21481 {
21482 char *relative = file_file_name (file, lh);
21483
21484 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21485 return relative;
21486 return reconcat (relative, comp_dir, SLASH_STRING,
21487 relative, (char *) NULL);
21488 }
21489 else
21490 return file_file_name (file, lh);
21491 }
21492
21493
21494 static struct macro_source_file *
21495 macro_start_file (int file, int line,
21496 struct macro_source_file *current_file,
21497 struct line_header *lh)
21498 {
21499 /* File name relative to the compilation directory of this source file. */
21500 char *file_name = file_file_name (file, lh);
21501
21502 if (! current_file)
21503 {
21504 /* Note: We don't create a macro table for this compilation unit
21505 at all until we actually get a filename. */
21506 struct macro_table *macro_table = get_macro_table ();
21507
21508 /* If we have no current file, then this must be the start_file
21509 directive for the compilation unit's main source file. */
21510 current_file = macro_set_main (macro_table, file_name);
21511 macro_define_special (macro_table);
21512 }
21513 else
21514 current_file = macro_include (current_file, line, file_name);
21515
21516 xfree (file_name);
21517
21518 return current_file;
21519 }
21520
21521
21522 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21523 followed by a null byte. */
21524 static char *
21525 copy_string (const char *buf, int len)
21526 {
21527 char *s = (char *) xmalloc (len + 1);
21528
21529 memcpy (s, buf, len);
21530 s[len] = '\0';
21531 return s;
21532 }
21533
21534
21535 static const char *
21536 consume_improper_spaces (const char *p, const char *body)
21537 {
21538 if (*p == ' ')
21539 {
21540 complaint (&symfile_complaints,
21541 _("macro definition contains spaces "
21542 "in formal argument list:\n`%s'"),
21543 body);
21544
21545 while (*p == ' ')
21546 p++;
21547 }
21548
21549 return p;
21550 }
21551
21552
21553 static void
21554 parse_macro_definition (struct macro_source_file *file, int line,
21555 const char *body)
21556 {
21557 const char *p;
21558
21559 /* The body string takes one of two forms. For object-like macro
21560 definitions, it should be:
21561
21562 <macro name> " " <definition>
21563
21564 For function-like macro definitions, it should be:
21565
21566 <macro name> "() " <definition>
21567 or
21568 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21569
21570 Spaces may appear only where explicitly indicated, and in the
21571 <definition>.
21572
21573 The Dwarf 2 spec says that an object-like macro's name is always
21574 followed by a space, but versions of GCC around March 2002 omit
21575 the space when the macro's definition is the empty string.
21576
21577 The Dwarf 2 spec says that there should be no spaces between the
21578 formal arguments in a function-like macro's formal argument list,
21579 but versions of GCC around March 2002 include spaces after the
21580 commas. */
21581
21582
21583 /* Find the extent of the macro name. The macro name is terminated
21584 by either a space or null character (for an object-like macro) or
21585 an opening paren (for a function-like macro). */
21586 for (p = body; *p; p++)
21587 if (*p == ' ' || *p == '(')
21588 break;
21589
21590 if (*p == ' ' || *p == '\0')
21591 {
21592 /* It's an object-like macro. */
21593 int name_len = p - body;
21594 char *name = copy_string (body, name_len);
21595 const char *replacement;
21596
21597 if (*p == ' ')
21598 replacement = body + name_len + 1;
21599 else
21600 {
21601 dwarf2_macro_malformed_definition_complaint (body);
21602 replacement = body + name_len;
21603 }
21604
21605 macro_define_object (file, line, name, replacement);
21606
21607 xfree (name);
21608 }
21609 else if (*p == '(')
21610 {
21611 /* It's a function-like macro. */
21612 char *name = copy_string (body, p - body);
21613 int argc = 0;
21614 int argv_size = 1;
21615 char **argv = XNEWVEC (char *, argv_size);
21616
21617 p++;
21618
21619 p = consume_improper_spaces (p, body);
21620
21621 /* Parse the formal argument list. */
21622 while (*p && *p != ')')
21623 {
21624 /* Find the extent of the current argument name. */
21625 const char *arg_start = p;
21626
21627 while (*p && *p != ',' && *p != ')' && *p != ' ')
21628 p++;
21629
21630 if (! *p || p == arg_start)
21631 dwarf2_macro_malformed_definition_complaint (body);
21632 else
21633 {
21634 /* Make sure argv has room for the new argument. */
21635 if (argc >= argv_size)
21636 {
21637 argv_size *= 2;
21638 argv = XRESIZEVEC (char *, argv, argv_size);
21639 }
21640
21641 argv[argc++] = copy_string (arg_start, p - arg_start);
21642 }
21643
21644 p = consume_improper_spaces (p, body);
21645
21646 /* Consume the comma, if present. */
21647 if (*p == ',')
21648 {
21649 p++;
21650
21651 p = consume_improper_spaces (p, body);
21652 }
21653 }
21654
21655 if (*p == ')')
21656 {
21657 p++;
21658
21659 if (*p == ' ')
21660 /* Perfectly formed definition, no complaints. */
21661 macro_define_function (file, line, name,
21662 argc, (const char **) argv,
21663 p + 1);
21664 else if (*p == '\0')
21665 {
21666 /* Complain, but do define it. */
21667 dwarf2_macro_malformed_definition_complaint (body);
21668 macro_define_function (file, line, name,
21669 argc, (const char **) argv,
21670 p);
21671 }
21672 else
21673 /* Just complain. */
21674 dwarf2_macro_malformed_definition_complaint (body);
21675 }
21676 else
21677 /* Just complain. */
21678 dwarf2_macro_malformed_definition_complaint (body);
21679
21680 xfree (name);
21681 {
21682 int i;
21683
21684 for (i = 0; i < argc; i++)
21685 xfree (argv[i]);
21686 }
21687 xfree (argv);
21688 }
21689 else
21690 dwarf2_macro_malformed_definition_complaint (body);
21691 }
21692
21693 /* Skip some bytes from BYTES according to the form given in FORM.
21694 Returns the new pointer. */
21695
21696 static const gdb_byte *
21697 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21698 enum dwarf_form form,
21699 unsigned int offset_size,
21700 struct dwarf2_section_info *section)
21701 {
21702 unsigned int bytes_read;
21703
21704 switch (form)
21705 {
21706 case DW_FORM_data1:
21707 case DW_FORM_flag:
21708 ++bytes;
21709 break;
21710
21711 case DW_FORM_data2:
21712 bytes += 2;
21713 break;
21714
21715 case DW_FORM_data4:
21716 bytes += 4;
21717 break;
21718
21719 case DW_FORM_data8:
21720 bytes += 8;
21721 break;
21722
21723 case DW_FORM_data16:
21724 bytes += 16;
21725 break;
21726
21727 case DW_FORM_string:
21728 read_direct_string (abfd, bytes, &bytes_read);
21729 bytes += bytes_read;
21730 break;
21731
21732 case DW_FORM_sec_offset:
21733 case DW_FORM_strp:
21734 case DW_FORM_GNU_strp_alt:
21735 bytes += offset_size;
21736 break;
21737
21738 case DW_FORM_block:
21739 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21740 bytes += bytes_read;
21741 break;
21742
21743 case DW_FORM_block1:
21744 bytes += 1 + read_1_byte (abfd, bytes);
21745 break;
21746 case DW_FORM_block2:
21747 bytes += 2 + read_2_bytes (abfd, bytes);
21748 break;
21749 case DW_FORM_block4:
21750 bytes += 4 + read_4_bytes (abfd, bytes);
21751 break;
21752
21753 case DW_FORM_sdata:
21754 case DW_FORM_udata:
21755 case DW_FORM_GNU_addr_index:
21756 case DW_FORM_GNU_str_index:
21757 bytes = gdb_skip_leb128 (bytes, buffer_end);
21758 if (bytes == NULL)
21759 {
21760 dwarf2_section_buffer_overflow_complaint (section);
21761 return NULL;
21762 }
21763 break;
21764
21765 default:
21766 {
21767 complain:
21768 complaint (&symfile_complaints,
21769 _("invalid form 0x%x in `%s'"),
21770 form, get_section_name (section));
21771 return NULL;
21772 }
21773 }
21774
21775 return bytes;
21776 }
21777
21778 /* A helper for dwarf_decode_macros that handles skipping an unknown
21779 opcode. Returns an updated pointer to the macro data buffer; or,
21780 on error, issues a complaint and returns NULL. */
21781
21782 static const gdb_byte *
21783 skip_unknown_opcode (unsigned int opcode,
21784 const gdb_byte **opcode_definitions,
21785 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21786 bfd *abfd,
21787 unsigned int offset_size,
21788 struct dwarf2_section_info *section)
21789 {
21790 unsigned int bytes_read, i;
21791 unsigned long arg;
21792 const gdb_byte *defn;
21793
21794 if (opcode_definitions[opcode] == NULL)
21795 {
21796 complaint (&symfile_complaints,
21797 _("unrecognized DW_MACFINO opcode 0x%x"),
21798 opcode);
21799 return NULL;
21800 }
21801
21802 defn = opcode_definitions[opcode];
21803 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21804 defn += bytes_read;
21805
21806 for (i = 0; i < arg; ++i)
21807 {
21808 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21809 (enum dwarf_form) defn[i], offset_size,
21810 section);
21811 if (mac_ptr == NULL)
21812 {
21813 /* skip_form_bytes already issued the complaint. */
21814 return NULL;
21815 }
21816 }
21817
21818 return mac_ptr;
21819 }
21820
21821 /* A helper function which parses the header of a macro section.
21822 If the macro section is the extended (for now called "GNU") type,
21823 then this updates *OFFSET_SIZE. Returns a pointer to just after
21824 the header, or issues a complaint and returns NULL on error. */
21825
21826 static const gdb_byte *
21827 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21828 bfd *abfd,
21829 const gdb_byte *mac_ptr,
21830 unsigned int *offset_size,
21831 int section_is_gnu)
21832 {
21833 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21834
21835 if (section_is_gnu)
21836 {
21837 unsigned int version, flags;
21838
21839 version = read_2_bytes (abfd, mac_ptr);
21840 if (version != 4 && version != 5)
21841 {
21842 complaint (&symfile_complaints,
21843 _("unrecognized version `%d' in .debug_macro section"),
21844 version);
21845 return NULL;
21846 }
21847 mac_ptr += 2;
21848
21849 flags = read_1_byte (abfd, mac_ptr);
21850 ++mac_ptr;
21851 *offset_size = (flags & 1) ? 8 : 4;
21852
21853 if ((flags & 2) != 0)
21854 /* We don't need the line table offset. */
21855 mac_ptr += *offset_size;
21856
21857 /* Vendor opcode descriptions. */
21858 if ((flags & 4) != 0)
21859 {
21860 unsigned int i, count;
21861
21862 count = read_1_byte (abfd, mac_ptr);
21863 ++mac_ptr;
21864 for (i = 0; i < count; ++i)
21865 {
21866 unsigned int opcode, bytes_read;
21867 unsigned long arg;
21868
21869 opcode = read_1_byte (abfd, mac_ptr);
21870 ++mac_ptr;
21871 opcode_definitions[opcode] = mac_ptr;
21872 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21873 mac_ptr += bytes_read;
21874 mac_ptr += arg;
21875 }
21876 }
21877 }
21878
21879 return mac_ptr;
21880 }
21881
21882 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21883 including DW_MACRO_import. */
21884
21885 static void
21886 dwarf_decode_macro_bytes (bfd *abfd,
21887 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21888 struct macro_source_file *current_file,
21889 struct line_header *lh,
21890 struct dwarf2_section_info *section,
21891 int section_is_gnu, int section_is_dwz,
21892 unsigned int offset_size,
21893 htab_t include_hash)
21894 {
21895 struct objfile *objfile = dwarf2_per_objfile->objfile;
21896 enum dwarf_macro_record_type macinfo_type;
21897 int at_commandline;
21898 const gdb_byte *opcode_definitions[256];
21899
21900 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21901 &offset_size, section_is_gnu);
21902 if (mac_ptr == NULL)
21903 {
21904 /* We already issued a complaint. */
21905 return;
21906 }
21907
21908 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21909 GDB is still reading the definitions from command line. First
21910 DW_MACINFO_start_file will need to be ignored as it was already executed
21911 to create CURRENT_FILE for the main source holding also the command line
21912 definitions. On first met DW_MACINFO_start_file this flag is reset to
21913 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21914
21915 at_commandline = 1;
21916
21917 do
21918 {
21919 /* Do we at least have room for a macinfo type byte? */
21920 if (mac_ptr >= mac_end)
21921 {
21922 dwarf2_section_buffer_overflow_complaint (section);
21923 break;
21924 }
21925
21926 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21927 mac_ptr++;
21928
21929 /* Note that we rely on the fact that the corresponding GNU and
21930 DWARF constants are the same. */
21931 switch (macinfo_type)
21932 {
21933 /* A zero macinfo type indicates the end of the macro
21934 information. */
21935 case 0:
21936 break;
21937
21938 case DW_MACRO_define:
21939 case DW_MACRO_undef:
21940 case DW_MACRO_define_strp:
21941 case DW_MACRO_undef_strp:
21942 case DW_MACRO_define_sup:
21943 case DW_MACRO_undef_sup:
21944 {
21945 unsigned int bytes_read;
21946 int line;
21947 const char *body;
21948 int is_define;
21949
21950 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21951 mac_ptr += bytes_read;
21952
21953 if (macinfo_type == DW_MACRO_define
21954 || macinfo_type == DW_MACRO_undef)
21955 {
21956 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21957 mac_ptr += bytes_read;
21958 }
21959 else
21960 {
21961 LONGEST str_offset;
21962
21963 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21964 mac_ptr += offset_size;
21965
21966 if (macinfo_type == DW_MACRO_define_sup
21967 || macinfo_type == DW_MACRO_undef_sup
21968 || section_is_dwz)
21969 {
21970 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21971
21972 body = read_indirect_string_from_dwz (dwz, str_offset);
21973 }
21974 else
21975 body = read_indirect_string_at_offset (abfd, str_offset);
21976 }
21977
21978 is_define = (macinfo_type == DW_MACRO_define
21979 || macinfo_type == DW_MACRO_define_strp
21980 || macinfo_type == DW_MACRO_define_sup);
21981 if (! current_file)
21982 {
21983 /* DWARF violation as no main source is present. */
21984 complaint (&symfile_complaints,
21985 _("debug info with no main source gives macro %s "
21986 "on line %d: %s"),
21987 is_define ? _("definition") : _("undefinition"),
21988 line, body);
21989 break;
21990 }
21991 if ((line == 0 && !at_commandline)
21992 || (line != 0 && at_commandline))
21993 complaint (&symfile_complaints,
21994 _("debug info gives %s macro %s with %s line %d: %s"),
21995 at_commandline ? _("command-line") : _("in-file"),
21996 is_define ? _("definition") : _("undefinition"),
21997 line == 0 ? _("zero") : _("non-zero"), line, body);
21998
21999 if (is_define)
22000 parse_macro_definition (current_file, line, body);
22001 else
22002 {
22003 gdb_assert (macinfo_type == DW_MACRO_undef
22004 || macinfo_type == DW_MACRO_undef_strp
22005 || macinfo_type == DW_MACRO_undef_sup);
22006 macro_undef (current_file, line, body);
22007 }
22008 }
22009 break;
22010
22011 case DW_MACRO_start_file:
22012 {
22013 unsigned int bytes_read;
22014 int line, file;
22015
22016 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22017 mac_ptr += bytes_read;
22018 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22019 mac_ptr += bytes_read;
22020
22021 if ((line == 0 && !at_commandline)
22022 || (line != 0 && at_commandline))
22023 complaint (&symfile_complaints,
22024 _("debug info gives source %d included "
22025 "from %s at %s line %d"),
22026 file, at_commandline ? _("command-line") : _("file"),
22027 line == 0 ? _("zero") : _("non-zero"), line);
22028
22029 if (at_commandline)
22030 {
22031 /* This DW_MACRO_start_file was executed in the
22032 pass one. */
22033 at_commandline = 0;
22034 }
22035 else
22036 current_file = macro_start_file (file, line, current_file, lh);
22037 }
22038 break;
22039
22040 case DW_MACRO_end_file:
22041 if (! current_file)
22042 complaint (&symfile_complaints,
22043 _("macro debug info has an unmatched "
22044 "`close_file' directive"));
22045 else
22046 {
22047 current_file = current_file->included_by;
22048 if (! current_file)
22049 {
22050 enum dwarf_macro_record_type next_type;
22051
22052 /* GCC circa March 2002 doesn't produce the zero
22053 type byte marking the end of the compilation
22054 unit. Complain if it's not there, but exit no
22055 matter what. */
22056
22057 /* Do we at least have room for a macinfo type byte? */
22058 if (mac_ptr >= mac_end)
22059 {
22060 dwarf2_section_buffer_overflow_complaint (section);
22061 return;
22062 }
22063
22064 /* We don't increment mac_ptr here, so this is just
22065 a look-ahead. */
22066 next_type
22067 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22068 mac_ptr);
22069 if (next_type != 0)
22070 complaint (&symfile_complaints,
22071 _("no terminating 0-type entry for "
22072 "macros in `.debug_macinfo' section"));
22073
22074 return;
22075 }
22076 }
22077 break;
22078
22079 case DW_MACRO_import:
22080 case DW_MACRO_import_sup:
22081 {
22082 LONGEST offset;
22083 void **slot;
22084 bfd *include_bfd = abfd;
22085 struct dwarf2_section_info *include_section = section;
22086 const gdb_byte *include_mac_end = mac_end;
22087 int is_dwz = section_is_dwz;
22088 const gdb_byte *new_mac_ptr;
22089
22090 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22091 mac_ptr += offset_size;
22092
22093 if (macinfo_type == DW_MACRO_import_sup)
22094 {
22095 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22096
22097 dwarf2_read_section (objfile, &dwz->macro);
22098
22099 include_section = &dwz->macro;
22100 include_bfd = get_section_bfd_owner (include_section);
22101 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22102 is_dwz = 1;
22103 }
22104
22105 new_mac_ptr = include_section->buffer + offset;
22106 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22107
22108 if (*slot != NULL)
22109 {
22110 /* This has actually happened; see
22111 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22112 complaint (&symfile_complaints,
22113 _("recursive DW_MACRO_import in "
22114 ".debug_macro section"));
22115 }
22116 else
22117 {
22118 *slot = (void *) new_mac_ptr;
22119
22120 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22121 include_mac_end, current_file, lh,
22122 section, section_is_gnu, is_dwz,
22123 offset_size, include_hash);
22124
22125 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22126 }
22127 }
22128 break;
22129
22130 case DW_MACINFO_vendor_ext:
22131 if (!section_is_gnu)
22132 {
22133 unsigned int bytes_read;
22134
22135 /* This reads the constant, but since we don't recognize
22136 any vendor extensions, we ignore it. */
22137 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22138 mac_ptr += bytes_read;
22139 read_direct_string (abfd, mac_ptr, &bytes_read);
22140 mac_ptr += bytes_read;
22141
22142 /* We don't recognize any vendor extensions. */
22143 break;
22144 }
22145 /* FALLTHROUGH */
22146
22147 default:
22148 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22149 mac_ptr, mac_end, abfd, offset_size,
22150 section);
22151 if (mac_ptr == NULL)
22152 return;
22153 break;
22154 }
22155 } while (macinfo_type != 0);
22156 }
22157
22158 static void
22159 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22160 int section_is_gnu)
22161 {
22162 struct objfile *objfile = dwarf2_per_objfile->objfile;
22163 struct line_header *lh = cu->line_header;
22164 bfd *abfd;
22165 const gdb_byte *mac_ptr, *mac_end;
22166 struct macro_source_file *current_file = 0;
22167 enum dwarf_macro_record_type macinfo_type;
22168 unsigned int offset_size = cu->header.offset_size;
22169 const gdb_byte *opcode_definitions[256];
22170 struct cleanup *cleanup;
22171 void **slot;
22172 struct dwarf2_section_info *section;
22173 const char *section_name;
22174
22175 if (cu->dwo_unit != NULL)
22176 {
22177 if (section_is_gnu)
22178 {
22179 section = &cu->dwo_unit->dwo_file->sections.macro;
22180 section_name = ".debug_macro.dwo";
22181 }
22182 else
22183 {
22184 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22185 section_name = ".debug_macinfo.dwo";
22186 }
22187 }
22188 else
22189 {
22190 if (section_is_gnu)
22191 {
22192 section = &dwarf2_per_objfile->macro;
22193 section_name = ".debug_macro";
22194 }
22195 else
22196 {
22197 section = &dwarf2_per_objfile->macinfo;
22198 section_name = ".debug_macinfo";
22199 }
22200 }
22201
22202 dwarf2_read_section (objfile, section);
22203 if (section->buffer == NULL)
22204 {
22205 complaint (&symfile_complaints, _("missing %s section"), section_name);
22206 return;
22207 }
22208 abfd = get_section_bfd_owner (section);
22209
22210 /* First pass: Find the name of the base filename.
22211 This filename is needed in order to process all macros whose definition
22212 (or undefinition) comes from the command line. These macros are defined
22213 before the first DW_MACINFO_start_file entry, and yet still need to be
22214 associated to the base file.
22215
22216 To determine the base file name, we scan the macro definitions until we
22217 reach the first DW_MACINFO_start_file entry. We then initialize
22218 CURRENT_FILE accordingly so that any macro definition found before the
22219 first DW_MACINFO_start_file can still be associated to the base file. */
22220
22221 mac_ptr = section->buffer + offset;
22222 mac_end = section->buffer + section->size;
22223
22224 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22225 &offset_size, section_is_gnu);
22226 if (mac_ptr == NULL)
22227 {
22228 /* We already issued a complaint. */
22229 return;
22230 }
22231
22232 do
22233 {
22234 /* Do we at least have room for a macinfo type byte? */
22235 if (mac_ptr >= mac_end)
22236 {
22237 /* Complaint is printed during the second pass as GDB will probably
22238 stop the first pass earlier upon finding
22239 DW_MACINFO_start_file. */
22240 break;
22241 }
22242
22243 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22244 mac_ptr++;
22245
22246 /* Note that we rely on the fact that the corresponding GNU and
22247 DWARF constants are the same. */
22248 switch (macinfo_type)
22249 {
22250 /* A zero macinfo type indicates the end of the macro
22251 information. */
22252 case 0:
22253 break;
22254
22255 case DW_MACRO_define:
22256 case DW_MACRO_undef:
22257 /* Only skip the data by MAC_PTR. */
22258 {
22259 unsigned int bytes_read;
22260
22261 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22262 mac_ptr += bytes_read;
22263 read_direct_string (abfd, mac_ptr, &bytes_read);
22264 mac_ptr += bytes_read;
22265 }
22266 break;
22267
22268 case DW_MACRO_start_file:
22269 {
22270 unsigned int bytes_read;
22271 int line, file;
22272
22273 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22274 mac_ptr += bytes_read;
22275 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22276 mac_ptr += bytes_read;
22277
22278 current_file = macro_start_file (file, line, current_file, lh);
22279 }
22280 break;
22281
22282 case DW_MACRO_end_file:
22283 /* No data to skip by MAC_PTR. */
22284 break;
22285
22286 case DW_MACRO_define_strp:
22287 case DW_MACRO_undef_strp:
22288 case DW_MACRO_define_sup:
22289 case DW_MACRO_undef_sup:
22290 {
22291 unsigned int bytes_read;
22292
22293 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22294 mac_ptr += bytes_read;
22295 mac_ptr += offset_size;
22296 }
22297 break;
22298
22299 case DW_MACRO_import:
22300 case DW_MACRO_import_sup:
22301 /* Note that, according to the spec, a transparent include
22302 chain cannot call DW_MACRO_start_file. So, we can just
22303 skip this opcode. */
22304 mac_ptr += offset_size;
22305 break;
22306
22307 case DW_MACINFO_vendor_ext:
22308 /* Only skip the data by MAC_PTR. */
22309 if (!section_is_gnu)
22310 {
22311 unsigned int bytes_read;
22312
22313 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22314 mac_ptr += bytes_read;
22315 read_direct_string (abfd, mac_ptr, &bytes_read);
22316 mac_ptr += bytes_read;
22317 }
22318 /* FALLTHROUGH */
22319
22320 default:
22321 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22322 mac_ptr, mac_end, abfd, offset_size,
22323 section);
22324 if (mac_ptr == NULL)
22325 return;
22326 break;
22327 }
22328 } while (macinfo_type != 0 && current_file == NULL);
22329
22330 /* Second pass: Process all entries.
22331
22332 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22333 command-line macro definitions/undefinitions. This flag is unset when we
22334 reach the first DW_MACINFO_start_file entry. */
22335
22336 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22337 htab_eq_pointer,
22338 NULL, xcalloc, xfree));
22339 mac_ptr = section->buffer + offset;
22340 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22341 *slot = (void *) mac_ptr;
22342 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22343 current_file, lh, section,
22344 section_is_gnu, 0, offset_size,
22345 include_hash.get ());
22346 }
22347
22348 /* Check if the attribute's form is a DW_FORM_block*
22349 if so return true else false. */
22350
22351 static int
22352 attr_form_is_block (const struct attribute *attr)
22353 {
22354 return (attr == NULL ? 0 :
22355 attr->form == DW_FORM_block1
22356 || attr->form == DW_FORM_block2
22357 || attr->form == DW_FORM_block4
22358 || attr->form == DW_FORM_block
22359 || attr->form == DW_FORM_exprloc);
22360 }
22361
22362 /* Return non-zero if ATTR's value is a section offset --- classes
22363 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22364 You may use DW_UNSND (attr) to retrieve such offsets.
22365
22366 Section 7.5.4, "Attribute Encodings", explains that no attribute
22367 may have a value that belongs to more than one of these classes; it
22368 would be ambiguous if we did, because we use the same forms for all
22369 of them. */
22370
22371 static int
22372 attr_form_is_section_offset (const struct attribute *attr)
22373 {
22374 return (attr->form == DW_FORM_data4
22375 || attr->form == DW_FORM_data8
22376 || attr->form == DW_FORM_sec_offset);
22377 }
22378
22379 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22380 zero otherwise. When this function returns true, you can apply
22381 dwarf2_get_attr_constant_value to it.
22382
22383 However, note that for some attributes you must check
22384 attr_form_is_section_offset before using this test. DW_FORM_data4
22385 and DW_FORM_data8 are members of both the constant class, and of
22386 the classes that contain offsets into other debug sections
22387 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22388 that, if an attribute's can be either a constant or one of the
22389 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22390 taken as section offsets, not constants.
22391
22392 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22393 cannot handle that. */
22394
22395 static int
22396 attr_form_is_constant (const struct attribute *attr)
22397 {
22398 switch (attr->form)
22399 {
22400 case DW_FORM_sdata:
22401 case DW_FORM_udata:
22402 case DW_FORM_data1:
22403 case DW_FORM_data2:
22404 case DW_FORM_data4:
22405 case DW_FORM_data8:
22406 return 1;
22407 default:
22408 return 0;
22409 }
22410 }
22411
22412
22413 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22414 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22415
22416 static int
22417 attr_form_is_ref (const struct attribute *attr)
22418 {
22419 switch (attr->form)
22420 {
22421 case DW_FORM_ref_addr:
22422 case DW_FORM_ref1:
22423 case DW_FORM_ref2:
22424 case DW_FORM_ref4:
22425 case DW_FORM_ref8:
22426 case DW_FORM_ref_udata:
22427 case DW_FORM_GNU_ref_alt:
22428 return 1;
22429 default:
22430 return 0;
22431 }
22432 }
22433
22434 /* Return the .debug_loc section to use for CU.
22435 For DWO files use .debug_loc.dwo. */
22436
22437 static struct dwarf2_section_info *
22438 cu_debug_loc_section (struct dwarf2_cu *cu)
22439 {
22440 if (cu->dwo_unit)
22441 {
22442 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22443
22444 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22445 }
22446 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22447 : &dwarf2_per_objfile->loc);
22448 }
22449
22450 /* A helper function that fills in a dwarf2_loclist_baton. */
22451
22452 static void
22453 fill_in_loclist_baton (struct dwarf2_cu *cu,
22454 struct dwarf2_loclist_baton *baton,
22455 const struct attribute *attr)
22456 {
22457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22458
22459 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22460
22461 baton->per_cu = cu->per_cu;
22462 gdb_assert (baton->per_cu);
22463 /* We don't know how long the location list is, but make sure we
22464 don't run off the edge of the section. */
22465 baton->size = section->size - DW_UNSND (attr);
22466 baton->data = section->buffer + DW_UNSND (attr);
22467 baton->base_address = cu->base_address;
22468 baton->from_dwo = cu->dwo_unit != NULL;
22469 }
22470
22471 static void
22472 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22473 struct dwarf2_cu *cu, int is_block)
22474 {
22475 struct objfile *objfile = dwarf2_per_objfile->objfile;
22476 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22477
22478 if (attr_form_is_section_offset (attr)
22479 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22480 the section. If so, fall through to the complaint in the
22481 other branch. */
22482 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22483 {
22484 struct dwarf2_loclist_baton *baton;
22485
22486 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22487
22488 fill_in_loclist_baton (cu, baton, attr);
22489
22490 if (cu->base_known == 0)
22491 complaint (&symfile_complaints,
22492 _("Location list used without "
22493 "specifying the CU base address."));
22494
22495 SYMBOL_ACLASS_INDEX (sym) = (is_block
22496 ? dwarf2_loclist_block_index
22497 : dwarf2_loclist_index);
22498 SYMBOL_LOCATION_BATON (sym) = baton;
22499 }
22500 else
22501 {
22502 struct dwarf2_locexpr_baton *baton;
22503
22504 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22505 baton->per_cu = cu->per_cu;
22506 gdb_assert (baton->per_cu);
22507
22508 if (attr_form_is_block (attr))
22509 {
22510 /* Note that we're just copying the block's data pointer
22511 here, not the actual data. We're still pointing into the
22512 info_buffer for SYM's objfile; right now we never release
22513 that buffer, but when we do clean up properly this may
22514 need to change. */
22515 baton->size = DW_BLOCK (attr)->size;
22516 baton->data = DW_BLOCK (attr)->data;
22517 }
22518 else
22519 {
22520 dwarf2_invalid_attrib_class_complaint ("location description",
22521 SYMBOL_NATURAL_NAME (sym));
22522 baton->size = 0;
22523 }
22524
22525 SYMBOL_ACLASS_INDEX (sym) = (is_block
22526 ? dwarf2_locexpr_block_index
22527 : dwarf2_locexpr_index);
22528 SYMBOL_LOCATION_BATON (sym) = baton;
22529 }
22530 }
22531
22532 /* Return the OBJFILE associated with the compilation unit CU. If CU
22533 came from a separate debuginfo file, then the master objfile is
22534 returned. */
22535
22536 struct objfile *
22537 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22538 {
22539 struct objfile *objfile = per_cu->objfile;
22540
22541 /* Return the master objfile, so that we can report and look up the
22542 correct file containing this variable. */
22543 if (objfile->separate_debug_objfile_backlink)
22544 objfile = objfile->separate_debug_objfile_backlink;
22545
22546 return objfile;
22547 }
22548
22549 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22550 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22551 CU_HEADERP first. */
22552
22553 static const struct comp_unit_head *
22554 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22555 struct dwarf2_per_cu_data *per_cu)
22556 {
22557 const gdb_byte *info_ptr;
22558
22559 if (per_cu->cu)
22560 return &per_cu->cu->header;
22561
22562 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22563
22564 memset (cu_headerp, 0, sizeof (*cu_headerp));
22565 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22566 rcuh_kind::COMPILE);
22567
22568 return cu_headerp;
22569 }
22570
22571 /* Return the address size given in the compilation unit header for CU. */
22572
22573 int
22574 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22575 {
22576 struct comp_unit_head cu_header_local;
22577 const struct comp_unit_head *cu_headerp;
22578
22579 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22580
22581 return cu_headerp->addr_size;
22582 }
22583
22584 /* Return the offset size given in the compilation unit header for CU. */
22585
22586 int
22587 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22588 {
22589 struct comp_unit_head cu_header_local;
22590 const struct comp_unit_head *cu_headerp;
22591
22592 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22593
22594 return cu_headerp->offset_size;
22595 }
22596
22597 /* See its dwarf2loc.h declaration. */
22598
22599 int
22600 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22601 {
22602 struct comp_unit_head cu_header_local;
22603 const struct comp_unit_head *cu_headerp;
22604
22605 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22606
22607 if (cu_headerp->version == 2)
22608 return cu_headerp->addr_size;
22609 else
22610 return cu_headerp->offset_size;
22611 }
22612
22613 /* Return the text offset of the CU. The returned offset comes from
22614 this CU's objfile. If this objfile came from a separate debuginfo
22615 file, then the offset may be different from the corresponding
22616 offset in the parent objfile. */
22617
22618 CORE_ADDR
22619 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22620 {
22621 struct objfile *objfile = per_cu->objfile;
22622
22623 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22624 }
22625
22626 /* Return DWARF version number of PER_CU. */
22627
22628 short
22629 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22630 {
22631 return per_cu->dwarf_version;
22632 }
22633
22634 /* Locate the .debug_info compilation unit from CU's objfile which contains
22635 the DIE at OFFSET. Raises an error on failure. */
22636
22637 static struct dwarf2_per_cu_data *
22638 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22639 unsigned int offset_in_dwz,
22640 struct objfile *objfile)
22641 {
22642 struct dwarf2_per_cu_data *this_cu;
22643 int low, high;
22644 const sect_offset *cu_off;
22645
22646 low = 0;
22647 high = dwarf2_per_objfile->n_comp_units - 1;
22648 while (high > low)
22649 {
22650 struct dwarf2_per_cu_data *mid_cu;
22651 int mid = low + (high - low) / 2;
22652
22653 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22654 cu_off = &mid_cu->sect_off;
22655 if (mid_cu->is_dwz > offset_in_dwz
22656 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22657 high = mid;
22658 else
22659 low = mid + 1;
22660 }
22661 gdb_assert (low == high);
22662 this_cu = dwarf2_per_objfile->all_comp_units[low];
22663 cu_off = &this_cu->sect_off;
22664 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22665 {
22666 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22667 error (_("Dwarf Error: could not find partial DIE containing "
22668 "offset 0x%x [in module %s]"),
22669 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22670
22671 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22672 <= sect_off);
22673 return dwarf2_per_objfile->all_comp_units[low-1];
22674 }
22675 else
22676 {
22677 this_cu = dwarf2_per_objfile->all_comp_units[low];
22678 if (low == dwarf2_per_objfile->n_comp_units - 1
22679 && sect_off >= this_cu->sect_off + this_cu->length)
22680 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22681 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22682 return this_cu;
22683 }
22684 }
22685
22686 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22687
22688 static void
22689 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22690 {
22691 memset (cu, 0, sizeof (*cu));
22692 per_cu->cu = cu;
22693 cu->per_cu = per_cu;
22694 cu->objfile = per_cu->objfile;
22695 obstack_init (&cu->comp_unit_obstack);
22696 }
22697
22698 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22699
22700 static void
22701 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22702 enum language pretend_language)
22703 {
22704 struct attribute *attr;
22705
22706 /* Set the language we're debugging. */
22707 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22708 if (attr)
22709 set_cu_language (DW_UNSND (attr), cu);
22710 else
22711 {
22712 cu->language = pretend_language;
22713 cu->language_defn = language_def (cu->language);
22714 }
22715
22716 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22717 }
22718
22719 /* Release one cached compilation unit, CU. We unlink it from the tree
22720 of compilation units, but we don't remove it from the read_in_chain;
22721 the caller is responsible for that.
22722 NOTE: DATA is a void * because this function is also used as a
22723 cleanup routine. */
22724
22725 static void
22726 free_heap_comp_unit (void *data)
22727 {
22728 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22729
22730 gdb_assert (cu->per_cu != NULL);
22731 cu->per_cu->cu = NULL;
22732 cu->per_cu = NULL;
22733
22734 obstack_free (&cu->comp_unit_obstack, NULL);
22735
22736 xfree (cu);
22737 }
22738
22739 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22740 when we're finished with it. We can't free the pointer itself, but be
22741 sure to unlink it from the cache. Also release any associated storage. */
22742
22743 static void
22744 free_stack_comp_unit (void *data)
22745 {
22746 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22747
22748 gdb_assert (cu->per_cu != NULL);
22749 cu->per_cu->cu = NULL;
22750 cu->per_cu = NULL;
22751
22752 obstack_free (&cu->comp_unit_obstack, NULL);
22753 cu->partial_dies = NULL;
22754 }
22755
22756 /* Free all cached compilation units. */
22757
22758 static void
22759 free_cached_comp_units (void *data)
22760 {
22761 struct dwarf2_per_cu_data *per_cu, **last_chain;
22762
22763 per_cu = dwarf2_per_objfile->read_in_chain;
22764 last_chain = &dwarf2_per_objfile->read_in_chain;
22765 while (per_cu != NULL)
22766 {
22767 struct dwarf2_per_cu_data *next_cu;
22768
22769 next_cu = per_cu->cu->read_in_chain;
22770
22771 free_heap_comp_unit (per_cu->cu);
22772 *last_chain = next_cu;
22773
22774 per_cu = next_cu;
22775 }
22776 }
22777
22778 /* Increase the age counter on each cached compilation unit, and free
22779 any that are too old. */
22780
22781 static void
22782 age_cached_comp_units (void)
22783 {
22784 struct dwarf2_per_cu_data *per_cu, **last_chain;
22785
22786 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22787 per_cu = dwarf2_per_objfile->read_in_chain;
22788 while (per_cu != NULL)
22789 {
22790 per_cu->cu->last_used ++;
22791 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22792 dwarf2_mark (per_cu->cu);
22793 per_cu = per_cu->cu->read_in_chain;
22794 }
22795
22796 per_cu = dwarf2_per_objfile->read_in_chain;
22797 last_chain = &dwarf2_per_objfile->read_in_chain;
22798 while (per_cu != NULL)
22799 {
22800 struct dwarf2_per_cu_data *next_cu;
22801
22802 next_cu = per_cu->cu->read_in_chain;
22803
22804 if (!per_cu->cu->mark)
22805 {
22806 free_heap_comp_unit (per_cu->cu);
22807 *last_chain = next_cu;
22808 }
22809 else
22810 last_chain = &per_cu->cu->read_in_chain;
22811
22812 per_cu = next_cu;
22813 }
22814 }
22815
22816 /* Remove a single compilation unit from the cache. */
22817
22818 static void
22819 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22820 {
22821 struct dwarf2_per_cu_data *per_cu, **last_chain;
22822
22823 per_cu = dwarf2_per_objfile->read_in_chain;
22824 last_chain = &dwarf2_per_objfile->read_in_chain;
22825 while (per_cu != NULL)
22826 {
22827 struct dwarf2_per_cu_data *next_cu;
22828
22829 next_cu = per_cu->cu->read_in_chain;
22830
22831 if (per_cu == target_per_cu)
22832 {
22833 free_heap_comp_unit (per_cu->cu);
22834 per_cu->cu = NULL;
22835 *last_chain = next_cu;
22836 break;
22837 }
22838 else
22839 last_chain = &per_cu->cu->read_in_chain;
22840
22841 per_cu = next_cu;
22842 }
22843 }
22844
22845 /* Release all extra memory associated with OBJFILE. */
22846
22847 void
22848 dwarf2_free_objfile (struct objfile *objfile)
22849 {
22850 dwarf2_per_objfile
22851 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22852 dwarf2_objfile_data_key);
22853
22854 if (dwarf2_per_objfile == NULL)
22855 return;
22856
22857 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22858 free_cached_comp_units (NULL);
22859
22860 if (dwarf2_per_objfile->quick_file_names_table)
22861 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22862
22863 if (dwarf2_per_objfile->line_header_hash)
22864 htab_delete (dwarf2_per_objfile->line_header_hash);
22865
22866 /* Everything else should be on the objfile obstack. */
22867 }
22868
22869 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22870 We store these in a hash table separate from the DIEs, and preserve them
22871 when the DIEs are flushed out of cache.
22872
22873 The CU "per_cu" pointer is needed because offset alone is not enough to
22874 uniquely identify the type. A file may have multiple .debug_types sections,
22875 or the type may come from a DWO file. Furthermore, while it's more logical
22876 to use per_cu->section+offset, with Fission the section with the data is in
22877 the DWO file but we don't know that section at the point we need it.
22878 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22879 because we can enter the lookup routine, get_die_type_at_offset, from
22880 outside this file, and thus won't necessarily have PER_CU->cu.
22881 Fortunately, PER_CU is stable for the life of the objfile. */
22882
22883 struct dwarf2_per_cu_offset_and_type
22884 {
22885 const struct dwarf2_per_cu_data *per_cu;
22886 sect_offset sect_off;
22887 struct type *type;
22888 };
22889
22890 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22891
22892 static hashval_t
22893 per_cu_offset_and_type_hash (const void *item)
22894 {
22895 const struct dwarf2_per_cu_offset_and_type *ofs
22896 = (const struct dwarf2_per_cu_offset_and_type *) item;
22897
22898 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22899 }
22900
22901 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22902
22903 static int
22904 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22905 {
22906 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22907 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22908 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22909 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22910
22911 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22912 && ofs_lhs->sect_off == ofs_rhs->sect_off);
22913 }
22914
22915 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22916 table if necessary. For convenience, return TYPE.
22917
22918 The DIEs reading must have careful ordering to:
22919 * Not cause infite loops trying to read in DIEs as a prerequisite for
22920 reading current DIE.
22921 * Not trying to dereference contents of still incompletely read in types
22922 while reading in other DIEs.
22923 * Enable referencing still incompletely read in types just by a pointer to
22924 the type without accessing its fields.
22925
22926 Therefore caller should follow these rules:
22927 * Try to fetch any prerequisite types we may need to build this DIE type
22928 before building the type and calling set_die_type.
22929 * After building type call set_die_type for current DIE as soon as
22930 possible before fetching more types to complete the current type.
22931 * Make the type as complete as possible before fetching more types. */
22932
22933 static struct type *
22934 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22935 {
22936 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22937 struct objfile *objfile = cu->objfile;
22938 struct attribute *attr;
22939 struct dynamic_prop prop;
22940
22941 /* For Ada types, make sure that the gnat-specific data is always
22942 initialized (if not already set). There are a few types where
22943 we should not be doing so, because the type-specific area is
22944 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22945 where the type-specific area is used to store the floatformat).
22946 But this is not a problem, because the gnat-specific information
22947 is actually not needed for these types. */
22948 if (need_gnat_info (cu)
22949 && TYPE_CODE (type) != TYPE_CODE_FUNC
22950 && TYPE_CODE (type) != TYPE_CODE_FLT
22951 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22952 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22953 && TYPE_CODE (type) != TYPE_CODE_METHOD
22954 && !HAVE_GNAT_AUX_INFO (type))
22955 INIT_GNAT_SPECIFIC (type);
22956
22957 /* Read DW_AT_allocated and set in type. */
22958 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22959 if (attr_form_is_block (attr))
22960 {
22961 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22962 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22963 }
22964 else if (attr != NULL)
22965 {
22966 complaint (&symfile_complaints,
22967 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22968 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22969 to_underlying (die->sect_off));
22970 }
22971
22972 /* Read DW_AT_associated and set in type. */
22973 attr = dwarf2_attr (die, DW_AT_associated, cu);
22974 if (attr_form_is_block (attr))
22975 {
22976 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22977 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22978 }
22979 else if (attr != NULL)
22980 {
22981 complaint (&symfile_complaints,
22982 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22983 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22984 to_underlying (die->sect_off));
22985 }
22986
22987 /* Read DW_AT_data_location and set in type. */
22988 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22989 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22990 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22991
22992 if (dwarf2_per_objfile->die_type_hash == NULL)
22993 {
22994 dwarf2_per_objfile->die_type_hash =
22995 htab_create_alloc_ex (127,
22996 per_cu_offset_and_type_hash,
22997 per_cu_offset_and_type_eq,
22998 NULL,
22999 &objfile->objfile_obstack,
23000 hashtab_obstack_allocate,
23001 dummy_obstack_deallocate);
23002 }
23003
23004 ofs.per_cu = cu->per_cu;
23005 ofs.sect_off = die->sect_off;
23006 ofs.type = type;
23007 slot = (struct dwarf2_per_cu_offset_and_type **)
23008 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23009 if (*slot)
23010 complaint (&symfile_complaints,
23011 _("A problem internal to GDB: DIE 0x%x has type already set"),
23012 to_underlying (die->sect_off));
23013 *slot = XOBNEW (&objfile->objfile_obstack,
23014 struct dwarf2_per_cu_offset_and_type);
23015 **slot = ofs;
23016 return type;
23017 }
23018
23019 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23020 or return NULL if the die does not have a saved type. */
23021
23022 static struct type *
23023 get_die_type_at_offset (sect_offset sect_off,
23024 struct dwarf2_per_cu_data *per_cu)
23025 {
23026 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23027
23028 if (dwarf2_per_objfile->die_type_hash == NULL)
23029 return NULL;
23030
23031 ofs.per_cu = per_cu;
23032 ofs.sect_off = sect_off;
23033 slot = ((struct dwarf2_per_cu_offset_and_type *)
23034 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23035 if (slot)
23036 return slot->type;
23037 else
23038 return NULL;
23039 }
23040
23041 /* Look up the type for DIE in CU in die_type_hash,
23042 or return NULL if DIE does not have a saved type. */
23043
23044 static struct type *
23045 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23046 {
23047 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23048 }
23049
23050 /* Add a dependence relationship from CU to REF_PER_CU. */
23051
23052 static void
23053 dwarf2_add_dependence (struct dwarf2_cu *cu,
23054 struct dwarf2_per_cu_data *ref_per_cu)
23055 {
23056 void **slot;
23057
23058 if (cu->dependencies == NULL)
23059 cu->dependencies
23060 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23061 NULL, &cu->comp_unit_obstack,
23062 hashtab_obstack_allocate,
23063 dummy_obstack_deallocate);
23064
23065 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23066 if (*slot == NULL)
23067 *slot = ref_per_cu;
23068 }
23069
23070 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23071 Set the mark field in every compilation unit in the
23072 cache that we must keep because we are keeping CU. */
23073
23074 static int
23075 dwarf2_mark_helper (void **slot, void *data)
23076 {
23077 struct dwarf2_per_cu_data *per_cu;
23078
23079 per_cu = (struct dwarf2_per_cu_data *) *slot;
23080
23081 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23082 reading of the chain. As such dependencies remain valid it is not much
23083 useful to track and undo them during QUIT cleanups. */
23084 if (per_cu->cu == NULL)
23085 return 1;
23086
23087 if (per_cu->cu->mark)
23088 return 1;
23089 per_cu->cu->mark = 1;
23090
23091 if (per_cu->cu->dependencies != NULL)
23092 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23093
23094 return 1;
23095 }
23096
23097 /* Set the mark field in CU and in every other compilation unit in the
23098 cache that we must keep because we are keeping CU. */
23099
23100 static void
23101 dwarf2_mark (struct dwarf2_cu *cu)
23102 {
23103 if (cu->mark)
23104 return;
23105 cu->mark = 1;
23106 if (cu->dependencies != NULL)
23107 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23108 }
23109
23110 static void
23111 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23112 {
23113 while (per_cu)
23114 {
23115 per_cu->cu->mark = 0;
23116 per_cu = per_cu->cu->read_in_chain;
23117 }
23118 }
23119
23120 /* Trivial hash function for partial_die_info: the hash value of a DIE
23121 is its offset in .debug_info for this objfile. */
23122
23123 static hashval_t
23124 partial_die_hash (const void *item)
23125 {
23126 const struct partial_die_info *part_die
23127 = (const struct partial_die_info *) item;
23128
23129 return to_underlying (part_die->sect_off);
23130 }
23131
23132 /* Trivial comparison function for partial_die_info structures: two DIEs
23133 are equal if they have the same offset. */
23134
23135 static int
23136 partial_die_eq (const void *item_lhs, const void *item_rhs)
23137 {
23138 const struct partial_die_info *part_die_lhs
23139 = (const struct partial_die_info *) item_lhs;
23140 const struct partial_die_info *part_die_rhs
23141 = (const struct partial_die_info *) item_rhs;
23142
23143 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23144 }
23145
23146 static struct cmd_list_element *set_dwarf_cmdlist;
23147 static struct cmd_list_element *show_dwarf_cmdlist;
23148
23149 static void
23150 set_dwarf_cmd (char *args, int from_tty)
23151 {
23152 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23153 gdb_stdout);
23154 }
23155
23156 static void
23157 show_dwarf_cmd (char *args, int from_tty)
23158 {
23159 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23160 }
23161
23162 /* Free data associated with OBJFILE, if necessary. */
23163
23164 static void
23165 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23166 {
23167 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23168 int ix;
23169
23170 /* Make sure we don't accidentally use dwarf2_per_objfile while
23171 cleaning up. */
23172 dwarf2_per_objfile = NULL;
23173
23174 for (ix = 0; ix < data->n_comp_units; ++ix)
23175 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23176
23177 for (ix = 0; ix < data->n_type_units; ++ix)
23178 VEC_free (dwarf2_per_cu_ptr,
23179 data->all_type_units[ix]->per_cu.imported_symtabs);
23180 xfree (data->all_type_units);
23181
23182 VEC_free (dwarf2_section_info_def, data->types);
23183
23184 if (data->dwo_files)
23185 free_dwo_files (data->dwo_files, objfile);
23186 if (data->dwp_file)
23187 gdb_bfd_unref (data->dwp_file->dbfd);
23188
23189 if (data->dwz_file && data->dwz_file->dwz_bfd)
23190 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23191 }
23192
23193 \f
23194 /* The "save gdb-index" command. */
23195
23196 /* The contents of the hash table we create when building the string
23197 table. */
23198 struct strtab_entry
23199 {
23200 offset_type offset;
23201 const char *str;
23202 };
23203
23204 /* Hash function for a strtab_entry.
23205
23206 Function is used only during write_hash_table so no index format backward
23207 compatibility is needed. */
23208
23209 static hashval_t
23210 hash_strtab_entry (const void *e)
23211 {
23212 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23213 return mapped_index_string_hash (INT_MAX, entry->str);
23214 }
23215
23216 /* Equality function for a strtab_entry. */
23217
23218 static int
23219 eq_strtab_entry (const void *a, const void *b)
23220 {
23221 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23222 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23223 return !strcmp (ea->str, eb->str);
23224 }
23225
23226 /* Create a strtab_entry hash table. */
23227
23228 static htab_t
23229 create_strtab (void)
23230 {
23231 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23232 xfree, xcalloc, xfree);
23233 }
23234
23235 /* Add a string to the constant pool. Return the string's offset in
23236 host order. */
23237
23238 static offset_type
23239 add_string (htab_t table, struct obstack *cpool, const char *str)
23240 {
23241 void **slot;
23242 struct strtab_entry entry;
23243 struct strtab_entry *result;
23244
23245 entry.str = str;
23246 slot = htab_find_slot (table, &entry, INSERT);
23247 if (*slot)
23248 result = (struct strtab_entry *) *slot;
23249 else
23250 {
23251 result = XNEW (struct strtab_entry);
23252 result->offset = obstack_object_size (cpool);
23253 result->str = str;
23254 obstack_grow_str0 (cpool, str);
23255 *slot = result;
23256 }
23257 return result->offset;
23258 }
23259
23260 /* An entry in the symbol table. */
23261 struct symtab_index_entry
23262 {
23263 /* The name of the symbol. */
23264 const char *name;
23265 /* The offset of the name in the constant pool. */
23266 offset_type index_offset;
23267 /* A sorted vector of the indices of all the CUs that hold an object
23268 of this name. */
23269 VEC (offset_type) *cu_indices;
23270 };
23271
23272 /* The symbol table. This is a power-of-2-sized hash table. */
23273 struct mapped_symtab
23274 {
23275 offset_type n_elements;
23276 offset_type size;
23277 struct symtab_index_entry **data;
23278 };
23279
23280 /* Hash function for a symtab_index_entry. */
23281
23282 static hashval_t
23283 hash_symtab_entry (const void *e)
23284 {
23285 const struct symtab_index_entry *entry
23286 = (const struct symtab_index_entry *) e;
23287 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23288 sizeof (offset_type) * VEC_length (offset_type,
23289 entry->cu_indices),
23290 0);
23291 }
23292
23293 /* Equality function for a symtab_index_entry. */
23294
23295 static int
23296 eq_symtab_entry (const void *a, const void *b)
23297 {
23298 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23299 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23300 int len = VEC_length (offset_type, ea->cu_indices);
23301 if (len != VEC_length (offset_type, eb->cu_indices))
23302 return 0;
23303 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23304 VEC_address (offset_type, eb->cu_indices),
23305 sizeof (offset_type) * len);
23306 }
23307
23308 /* Destroy a symtab_index_entry. */
23309
23310 static void
23311 delete_symtab_entry (void *p)
23312 {
23313 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23314 VEC_free (offset_type, entry->cu_indices);
23315 xfree (entry);
23316 }
23317
23318 /* Create a hash table holding symtab_index_entry objects. */
23319
23320 static htab_t
23321 create_symbol_hash_table (void)
23322 {
23323 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23324 delete_symtab_entry, xcalloc, xfree);
23325 }
23326
23327 /* Create a new mapped symtab object. */
23328
23329 static struct mapped_symtab *
23330 create_mapped_symtab (void)
23331 {
23332 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23333 symtab->n_elements = 0;
23334 symtab->size = 1024;
23335 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23336 return symtab;
23337 }
23338
23339 /* Destroy a mapped_symtab. */
23340
23341 static void
23342 cleanup_mapped_symtab (void *p)
23343 {
23344 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23345 /* The contents of the array are freed when the other hash table is
23346 destroyed. */
23347 xfree (symtab->data);
23348 xfree (symtab);
23349 }
23350
23351 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23352 the slot.
23353
23354 Function is used only during write_hash_table so no index format backward
23355 compatibility is needed. */
23356
23357 static struct symtab_index_entry **
23358 find_slot (struct mapped_symtab *symtab, const char *name)
23359 {
23360 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23361
23362 index = hash & (symtab->size - 1);
23363 step = ((hash * 17) & (symtab->size - 1)) | 1;
23364
23365 for (;;)
23366 {
23367 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23368 return &symtab->data[index];
23369 index = (index + step) & (symtab->size - 1);
23370 }
23371 }
23372
23373 /* Expand SYMTAB's hash table. */
23374
23375 static void
23376 hash_expand (struct mapped_symtab *symtab)
23377 {
23378 offset_type old_size = symtab->size;
23379 offset_type i;
23380 struct symtab_index_entry **old_entries = symtab->data;
23381
23382 symtab->size *= 2;
23383 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23384
23385 for (i = 0; i < old_size; ++i)
23386 {
23387 if (old_entries[i])
23388 {
23389 struct symtab_index_entry **slot = find_slot (symtab,
23390 old_entries[i]->name);
23391 *slot = old_entries[i];
23392 }
23393 }
23394
23395 xfree (old_entries);
23396 }
23397
23398 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23399 CU_INDEX is the index of the CU in which the symbol appears.
23400 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23401
23402 static void
23403 add_index_entry (struct mapped_symtab *symtab, const char *name,
23404 int is_static, gdb_index_symbol_kind kind,
23405 offset_type cu_index)
23406 {
23407 struct symtab_index_entry **slot;
23408 offset_type cu_index_and_attrs;
23409
23410 ++symtab->n_elements;
23411 if (4 * symtab->n_elements / 3 >= symtab->size)
23412 hash_expand (symtab);
23413
23414 slot = find_slot (symtab, name);
23415 if (!*slot)
23416 {
23417 *slot = XNEW (struct symtab_index_entry);
23418 (*slot)->name = name;
23419 /* index_offset is set later. */
23420 (*slot)->cu_indices = NULL;
23421 }
23422
23423 cu_index_and_attrs = 0;
23424 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23425 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23426 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23427
23428 /* We don't want to record an index value twice as we want to avoid the
23429 duplication.
23430 We process all global symbols and then all static symbols
23431 (which would allow us to avoid the duplication by only having to check
23432 the last entry pushed), but a symbol could have multiple kinds in one CU.
23433 To keep things simple we don't worry about the duplication here and
23434 sort and uniqufy the list after we've processed all symbols. */
23435 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23436 }
23437
23438 /* qsort helper routine for uniquify_cu_indices. */
23439
23440 static int
23441 offset_type_compare (const void *ap, const void *bp)
23442 {
23443 offset_type a = *(offset_type *) ap;
23444 offset_type b = *(offset_type *) bp;
23445
23446 return (a > b) - (b > a);
23447 }
23448
23449 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23450
23451 static void
23452 uniquify_cu_indices (struct mapped_symtab *symtab)
23453 {
23454 int i;
23455
23456 for (i = 0; i < symtab->size; ++i)
23457 {
23458 struct symtab_index_entry *entry = symtab->data[i];
23459
23460 if (entry
23461 && entry->cu_indices != NULL)
23462 {
23463 unsigned int next_to_insert, next_to_check;
23464 offset_type last_value;
23465
23466 qsort (VEC_address (offset_type, entry->cu_indices),
23467 VEC_length (offset_type, entry->cu_indices),
23468 sizeof (offset_type), offset_type_compare);
23469
23470 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23471 next_to_insert = 1;
23472 for (next_to_check = 1;
23473 next_to_check < VEC_length (offset_type, entry->cu_indices);
23474 ++next_to_check)
23475 {
23476 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23477 != last_value)
23478 {
23479 last_value = VEC_index (offset_type, entry->cu_indices,
23480 next_to_check);
23481 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23482 last_value);
23483 ++next_to_insert;
23484 }
23485 }
23486 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23487 }
23488 }
23489 }
23490
23491 /* Add a vector of indices to the constant pool. */
23492
23493 static offset_type
23494 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23495 struct symtab_index_entry *entry)
23496 {
23497 void **slot;
23498
23499 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23500 if (!*slot)
23501 {
23502 offset_type len = VEC_length (offset_type, entry->cu_indices);
23503 offset_type val = MAYBE_SWAP (len);
23504 offset_type iter;
23505 int i;
23506
23507 *slot = entry;
23508 entry->index_offset = obstack_object_size (cpool);
23509
23510 obstack_grow (cpool, &val, sizeof (val));
23511 for (i = 0;
23512 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23513 ++i)
23514 {
23515 val = MAYBE_SWAP (iter);
23516 obstack_grow (cpool, &val, sizeof (val));
23517 }
23518 }
23519 else
23520 {
23521 struct symtab_index_entry *old_entry
23522 = (struct symtab_index_entry *) *slot;
23523 entry->index_offset = old_entry->index_offset;
23524 entry = old_entry;
23525 }
23526 return entry->index_offset;
23527 }
23528
23529 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23530 constant pool entries going into the obstack CPOOL. */
23531
23532 static void
23533 write_hash_table (struct mapped_symtab *symtab,
23534 struct obstack *output, struct obstack *cpool)
23535 {
23536 offset_type i;
23537 htab_t symbol_hash_table;
23538 htab_t str_table;
23539
23540 symbol_hash_table = create_symbol_hash_table ();
23541 str_table = create_strtab ();
23542
23543 /* We add all the index vectors to the constant pool first, to
23544 ensure alignment is ok. */
23545 for (i = 0; i < symtab->size; ++i)
23546 {
23547 if (symtab->data[i])
23548 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23549 }
23550
23551 /* Now write out the hash table. */
23552 for (i = 0; i < symtab->size; ++i)
23553 {
23554 offset_type str_off, vec_off;
23555
23556 if (symtab->data[i])
23557 {
23558 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23559 vec_off = symtab->data[i]->index_offset;
23560 }
23561 else
23562 {
23563 /* While 0 is a valid constant pool index, it is not valid
23564 to have 0 for both offsets. */
23565 str_off = 0;
23566 vec_off = 0;
23567 }
23568
23569 str_off = MAYBE_SWAP (str_off);
23570 vec_off = MAYBE_SWAP (vec_off);
23571
23572 obstack_grow (output, &str_off, sizeof (str_off));
23573 obstack_grow (output, &vec_off, sizeof (vec_off));
23574 }
23575
23576 htab_delete (str_table);
23577 htab_delete (symbol_hash_table);
23578 }
23579
23580 /* Struct to map psymtab to CU index in the index file. */
23581 struct psymtab_cu_index_map
23582 {
23583 struct partial_symtab *psymtab;
23584 unsigned int cu_index;
23585 };
23586
23587 static hashval_t
23588 hash_psymtab_cu_index (const void *item)
23589 {
23590 const struct psymtab_cu_index_map *map
23591 = (const struct psymtab_cu_index_map *) item;
23592
23593 return htab_hash_pointer (map->psymtab);
23594 }
23595
23596 static int
23597 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23598 {
23599 const struct psymtab_cu_index_map *lhs
23600 = (const struct psymtab_cu_index_map *) item_lhs;
23601 const struct psymtab_cu_index_map *rhs
23602 = (const struct psymtab_cu_index_map *) item_rhs;
23603
23604 return lhs->psymtab == rhs->psymtab;
23605 }
23606
23607 /* Helper struct for building the address table. */
23608 struct addrmap_index_data
23609 {
23610 struct objfile *objfile;
23611 struct obstack *addr_obstack;
23612 htab_t cu_index_htab;
23613
23614 /* Non-zero if the previous_* fields are valid.
23615 We can't write an entry until we see the next entry (since it is only then
23616 that we know the end of the entry). */
23617 int previous_valid;
23618 /* Index of the CU in the table of all CUs in the index file. */
23619 unsigned int previous_cu_index;
23620 /* Start address of the CU. */
23621 CORE_ADDR previous_cu_start;
23622 };
23623
23624 /* Write an address entry to OBSTACK. */
23625
23626 static void
23627 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23628 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23629 {
23630 offset_type cu_index_to_write;
23631 gdb_byte addr[8];
23632 CORE_ADDR baseaddr;
23633
23634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23635
23636 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23637 obstack_grow (obstack, addr, 8);
23638 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23639 obstack_grow (obstack, addr, 8);
23640 cu_index_to_write = MAYBE_SWAP (cu_index);
23641 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23642 }
23643
23644 /* Worker function for traversing an addrmap to build the address table. */
23645
23646 static int
23647 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23648 {
23649 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23650 struct partial_symtab *pst = (struct partial_symtab *) obj;
23651
23652 if (data->previous_valid)
23653 add_address_entry (data->objfile, data->addr_obstack,
23654 data->previous_cu_start, start_addr,
23655 data->previous_cu_index);
23656
23657 data->previous_cu_start = start_addr;
23658 if (pst != NULL)
23659 {
23660 struct psymtab_cu_index_map find_map, *map;
23661 find_map.psymtab = pst;
23662 map = ((struct psymtab_cu_index_map *)
23663 htab_find (data->cu_index_htab, &find_map));
23664 gdb_assert (map != NULL);
23665 data->previous_cu_index = map->cu_index;
23666 data->previous_valid = 1;
23667 }
23668 else
23669 data->previous_valid = 0;
23670
23671 return 0;
23672 }
23673
23674 /* Write OBJFILE's address map to OBSTACK.
23675 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23676 in the index file. */
23677
23678 static void
23679 write_address_map (struct objfile *objfile, struct obstack *obstack,
23680 htab_t cu_index_htab)
23681 {
23682 struct addrmap_index_data addrmap_index_data;
23683
23684 /* When writing the address table, we have to cope with the fact that
23685 the addrmap iterator only provides the start of a region; we have to
23686 wait until the next invocation to get the start of the next region. */
23687
23688 addrmap_index_data.objfile = objfile;
23689 addrmap_index_data.addr_obstack = obstack;
23690 addrmap_index_data.cu_index_htab = cu_index_htab;
23691 addrmap_index_data.previous_valid = 0;
23692
23693 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23694 &addrmap_index_data);
23695
23696 /* It's highly unlikely the last entry (end address = 0xff...ff)
23697 is valid, but we should still handle it.
23698 The end address is recorded as the start of the next region, but that
23699 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23700 anyway. */
23701 if (addrmap_index_data.previous_valid)
23702 add_address_entry (objfile, obstack,
23703 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23704 addrmap_index_data.previous_cu_index);
23705 }
23706
23707 /* Return the symbol kind of PSYM. */
23708
23709 static gdb_index_symbol_kind
23710 symbol_kind (struct partial_symbol *psym)
23711 {
23712 domain_enum domain = PSYMBOL_DOMAIN (psym);
23713 enum address_class aclass = PSYMBOL_CLASS (psym);
23714
23715 switch (domain)
23716 {
23717 case VAR_DOMAIN:
23718 switch (aclass)
23719 {
23720 case LOC_BLOCK:
23721 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23722 case LOC_TYPEDEF:
23723 return GDB_INDEX_SYMBOL_KIND_TYPE;
23724 case LOC_COMPUTED:
23725 case LOC_CONST_BYTES:
23726 case LOC_OPTIMIZED_OUT:
23727 case LOC_STATIC:
23728 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23729 case LOC_CONST:
23730 /* Note: It's currently impossible to recognize psyms as enum values
23731 short of reading the type info. For now punt. */
23732 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23733 default:
23734 /* There are other LOC_FOO values that one might want to classify
23735 as variables, but dwarf2read.c doesn't currently use them. */
23736 return GDB_INDEX_SYMBOL_KIND_OTHER;
23737 }
23738 case STRUCT_DOMAIN:
23739 return GDB_INDEX_SYMBOL_KIND_TYPE;
23740 default:
23741 return GDB_INDEX_SYMBOL_KIND_OTHER;
23742 }
23743 }
23744
23745 /* Add a list of partial symbols to SYMTAB. */
23746
23747 static void
23748 write_psymbols (struct mapped_symtab *symtab,
23749 htab_t psyms_seen,
23750 struct partial_symbol **psymp,
23751 int count,
23752 offset_type cu_index,
23753 int is_static)
23754 {
23755 for (; count-- > 0; ++psymp)
23756 {
23757 struct partial_symbol *psym = *psymp;
23758 void **slot;
23759
23760 if (SYMBOL_LANGUAGE (psym) == language_ada)
23761 error (_("Ada is not currently supported by the index"));
23762
23763 /* Only add a given psymbol once. */
23764 slot = htab_find_slot (psyms_seen, psym, INSERT);
23765 if (!*slot)
23766 {
23767 gdb_index_symbol_kind kind = symbol_kind (psym);
23768
23769 *slot = psym;
23770 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23771 is_static, kind, cu_index);
23772 }
23773 }
23774 }
23775
23776 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23777 exception if there is an error. */
23778
23779 static void
23780 write_obstack (FILE *file, struct obstack *obstack)
23781 {
23782 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23783 file)
23784 != obstack_object_size (obstack))
23785 error (_("couldn't data write to file"));
23786 }
23787
23788 /* A helper struct used when iterating over debug_types. */
23789 struct signatured_type_index_data
23790 {
23791 struct objfile *objfile;
23792 struct mapped_symtab *symtab;
23793 struct obstack *types_list;
23794 htab_t psyms_seen;
23795 int cu_index;
23796 };
23797
23798 /* A helper function that writes a single signatured_type to an
23799 obstack. */
23800
23801 static int
23802 write_one_signatured_type (void **slot, void *d)
23803 {
23804 struct signatured_type_index_data *info
23805 = (struct signatured_type_index_data *) d;
23806 struct signatured_type *entry = (struct signatured_type *) *slot;
23807 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23808 gdb_byte val[8];
23809
23810 write_psymbols (info->symtab,
23811 info->psyms_seen,
23812 info->objfile->global_psymbols.list
23813 + psymtab->globals_offset,
23814 psymtab->n_global_syms, info->cu_index,
23815 0);
23816 write_psymbols (info->symtab,
23817 info->psyms_seen,
23818 info->objfile->static_psymbols.list
23819 + psymtab->statics_offset,
23820 psymtab->n_static_syms, info->cu_index,
23821 1);
23822
23823 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23824 to_underlying (entry->per_cu.sect_off));
23825 obstack_grow (info->types_list, val, 8);
23826 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23827 to_underlying (entry->type_offset_in_tu));
23828 obstack_grow (info->types_list, val, 8);
23829 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23830 obstack_grow (info->types_list, val, 8);
23831
23832 ++info->cu_index;
23833
23834 return 1;
23835 }
23836
23837 /* Recurse into all "included" dependencies and write their symbols as
23838 if they appeared in this psymtab. */
23839
23840 static void
23841 recursively_write_psymbols (struct objfile *objfile,
23842 struct partial_symtab *psymtab,
23843 struct mapped_symtab *symtab,
23844 htab_t psyms_seen,
23845 offset_type cu_index)
23846 {
23847 int i;
23848
23849 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23850 if (psymtab->dependencies[i]->user != NULL)
23851 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23852 symtab, psyms_seen, cu_index);
23853
23854 write_psymbols (symtab,
23855 psyms_seen,
23856 objfile->global_psymbols.list + psymtab->globals_offset,
23857 psymtab->n_global_syms, cu_index,
23858 0);
23859 write_psymbols (symtab,
23860 psyms_seen,
23861 objfile->static_psymbols.list + psymtab->statics_offset,
23862 psymtab->n_static_syms, cu_index,
23863 1);
23864 }
23865
23866 /* Create an index file for OBJFILE in the directory DIR. */
23867
23868 static void
23869 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23870 {
23871 struct cleanup *cleanup;
23872 char *filename;
23873 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23874 struct obstack cu_list, types_cu_list;
23875 int i;
23876 FILE *out_file;
23877 struct mapped_symtab *symtab;
23878 offset_type val, size_of_contents, total_len;
23879 struct stat st;
23880 struct psymtab_cu_index_map *psymtab_cu_index_map;
23881
23882 if (dwarf2_per_objfile->using_index)
23883 error (_("Cannot use an index to create the index"));
23884
23885 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23886 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23887
23888 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23889 return;
23890
23891 if (stat (objfile_name (objfile), &st) < 0)
23892 perror_with_name (objfile_name (objfile));
23893
23894 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23895 INDEX_SUFFIX, (char *) NULL);
23896 cleanup = make_cleanup (xfree, filename);
23897
23898 out_file = gdb_fopen_cloexec (filename, "wb");
23899 if (!out_file)
23900 error (_("Can't open `%s' for writing"), filename);
23901
23902 gdb::unlinker unlink_file (filename);
23903
23904 symtab = create_mapped_symtab ();
23905 make_cleanup (cleanup_mapped_symtab, symtab);
23906
23907 obstack_init (&addr_obstack);
23908 make_cleanup_obstack_free (&addr_obstack);
23909
23910 obstack_init (&cu_list);
23911 make_cleanup_obstack_free (&cu_list);
23912
23913 obstack_init (&types_cu_list);
23914 make_cleanup_obstack_free (&types_cu_list);
23915
23916 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23917 htab_eq_pointer,
23918 NULL, xcalloc, xfree));
23919
23920 /* While we're scanning CU's create a table that maps a psymtab pointer
23921 (which is what addrmap records) to its index (which is what is recorded
23922 in the index file). This will later be needed to write the address
23923 table. */
23924 htab_up cu_index_htab (htab_create_alloc (100,
23925 hash_psymtab_cu_index,
23926 eq_psymtab_cu_index,
23927 NULL, xcalloc, xfree));
23928 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23929 dwarf2_per_objfile->n_comp_units);
23930 make_cleanup (xfree, psymtab_cu_index_map);
23931
23932 /* The CU list is already sorted, so we don't need to do additional
23933 work here. Also, the debug_types entries do not appear in
23934 all_comp_units, but only in their own hash table. */
23935 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23936 {
23937 struct dwarf2_per_cu_data *per_cu
23938 = dwarf2_per_objfile->all_comp_units[i];
23939 struct partial_symtab *psymtab = per_cu->v.psymtab;
23940 gdb_byte val[8];
23941 struct psymtab_cu_index_map *map;
23942 void **slot;
23943
23944 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23945 It may be referenced from a local scope but in such case it does not
23946 need to be present in .gdb_index. */
23947 if (psymtab == NULL)
23948 continue;
23949
23950 if (psymtab->user == NULL)
23951 recursively_write_psymbols (objfile, psymtab, symtab,
23952 psyms_seen.get (), i);
23953
23954 map = &psymtab_cu_index_map[i];
23955 map->psymtab = psymtab;
23956 map->cu_index = i;
23957 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23958 gdb_assert (slot != NULL);
23959 gdb_assert (*slot == NULL);
23960 *slot = map;
23961
23962 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23963 to_underlying (per_cu->sect_off));
23964 obstack_grow (&cu_list, val, 8);
23965 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23966 obstack_grow (&cu_list, val, 8);
23967 }
23968
23969 /* Dump the address map. */
23970 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23971
23972 /* Write out the .debug_type entries, if any. */
23973 if (dwarf2_per_objfile->signatured_types)
23974 {
23975 struct signatured_type_index_data sig_data;
23976
23977 sig_data.objfile = objfile;
23978 sig_data.symtab = symtab;
23979 sig_data.types_list = &types_cu_list;
23980 sig_data.psyms_seen = psyms_seen.get ();
23981 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23982 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23983 write_one_signatured_type, &sig_data);
23984 }
23985
23986 /* Now that we've processed all symbols we can shrink their cu_indices
23987 lists. */
23988 uniquify_cu_indices (symtab);
23989
23990 obstack_init (&constant_pool);
23991 make_cleanup_obstack_free (&constant_pool);
23992 obstack_init (&symtab_obstack);
23993 make_cleanup_obstack_free (&symtab_obstack);
23994 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23995
23996 obstack_init (&contents);
23997 make_cleanup_obstack_free (&contents);
23998 size_of_contents = 6 * sizeof (offset_type);
23999 total_len = size_of_contents;
24000
24001 /* The version number. */
24002 val = MAYBE_SWAP (8);
24003 obstack_grow (&contents, &val, sizeof (val));
24004
24005 /* The offset of the CU list from the start of the file. */
24006 val = MAYBE_SWAP (total_len);
24007 obstack_grow (&contents, &val, sizeof (val));
24008 total_len += obstack_object_size (&cu_list);
24009
24010 /* The offset of the types CU list from the start of the file. */
24011 val = MAYBE_SWAP (total_len);
24012 obstack_grow (&contents, &val, sizeof (val));
24013 total_len += obstack_object_size (&types_cu_list);
24014
24015 /* The offset of the address table from the start of the file. */
24016 val = MAYBE_SWAP (total_len);
24017 obstack_grow (&contents, &val, sizeof (val));
24018 total_len += obstack_object_size (&addr_obstack);
24019
24020 /* The offset of the symbol table from the start of the file. */
24021 val = MAYBE_SWAP (total_len);
24022 obstack_grow (&contents, &val, sizeof (val));
24023 total_len += obstack_object_size (&symtab_obstack);
24024
24025 /* The offset of the constant pool from the start of the file. */
24026 val = MAYBE_SWAP (total_len);
24027 obstack_grow (&contents, &val, sizeof (val));
24028 total_len += obstack_object_size (&constant_pool);
24029
24030 gdb_assert (obstack_object_size (&contents) == size_of_contents);
24031
24032 write_obstack (out_file, &contents);
24033 write_obstack (out_file, &cu_list);
24034 write_obstack (out_file, &types_cu_list);
24035 write_obstack (out_file, &addr_obstack);
24036 write_obstack (out_file, &symtab_obstack);
24037 write_obstack (out_file, &constant_pool);
24038
24039 fclose (out_file);
24040
24041 /* We want to keep the file. */
24042 unlink_file.keep ();
24043
24044 do_cleanups (cleanup);
24045 }
24046
24047 /* Implementation of the `save gdb-index' command.
24048
24049 Note that the file format used by this command is documented in the
24050 GDB manual. Any changes here must be documented there. */
24051
24052 static void
24053 save_gdb_index_command (char *arg, int from_tty)
24054 {
24055 struct objfile *objfile;
24056
24057 if (!arg || !*arg)
24058 error (_("usage: save gdb-index DIRECTORY"));
24059
24060 ALL_OBJFILES (objfile)
24061 {
24062 struct stat st;
24063
24064 /* If the objfile does not correspond to an actual file, skip it. */
24065 if (stat (objfile_name (objfile), &st) < 0)
24066 continue;
24067
24068 dwarf2_per_objfile
24069 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24070 dwarf2_objfile_data_key);
24071 if (dwarf2_per_objfile)
24072 {
24073
24074 TRY
24075 {
24076 write_psymtabs_to_index (objfile, arg);
24077 }
24078 CATCH (except, RETURN_MASK_ERROR)
24079 {
24080 exception_fprintf (gdb_stderr, except,
24081 _("Error while writing index for `%s': "),
24082 objfile_name (objfile));
24083 }
24084 END_CATCH
24085 }
24086 }
24087 }
24088
24089 \f
24090
24091 int dwarf_always_disassemble;
24092
24093 static void
24094 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24095 struct cmd_list_element *c, const char *value)
24096 {
24097 fprintf_filtered (file,
24098 _("Whether to always disassemble "
24099 "DWARF expressions is %s.\n"),
24100 value);
24101 }
24102
24103 static void
24104 show_check_physname (struct ui_file *file, int from_tty,
24105 struct cmd_list_element *c, const char *value)
24106 {
24107 fprintf_filtered (file,
24108 _("Whether to check \"physname\" is %s.\n"),
24109 value);
24110 }
24111
24112 void _initialize_dwarf2_read (void);
24113
24114 void
24115 _initialize_dwarf2_read (void)
24116 {
24117 struct cmd_list_element *c;
24118
24119 dwarf2_objfile_data_key
24120 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24121
24122 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24123 Set DWARF specific variables.\n\
24124 Configure DWARF variables such as the cache size"),
24125 &set_dwarf_cmdlist, "maintenance set dwarf ",
24126 0/*allow-unknown*/, &maintenance_set_cmdlist);
24127
24128 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24129 Show DWARF specific variables\n\
24130 Show DWARF variables such as the cache size"),
24131 &show_dwarf_cmdlist, "maintenance show dwarf ",
24132 0/*allow-unknown*/, &maintenance_show_cmdlist);
24133
24134 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24135 &dwarf_max_cache_age, _("\
24136 Set the upper bound on the age of cached DWARF compilation units."), _("\
24137 Show the upper bound on the age of cached DWARF compilation units."), _("\
24138 A higher limit means that cached compilation units will be stored\n\
24139 in memory longer, and more total memory will be used. Zero disables\n\
24140 caching, which can slow down startup."),
24141 NULL,
24142 show_dwarf_max_cache_age,
24143 &set_dwarf_cmdlist,
24144 &show_dwarf_cmdlist);
24145
24146 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24147 &dwarf_always_disassemble, _("\
24148 Set whether `info address' always disassembles DWARF expressions."), _("\
24149 Show whether `info address' always disassembles DWARF expressions."), _("\
24150 When enabled, DWARF expressions are always printed in an assembly-like\n\
24151 syntax. When disabled, expressions will be printed in a more\n\
24152 conversational style, when possible."),
24153 NULL,
24154 show_dwarf_always_disassemble,
24155 &set_dwarf_cmdlist,
24156 &show_dwarf_cmdlist);
24157
24158 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24159 Set debugging of the DWARF reader."), _("\
24160 Show debugging of the DWARF reader."), _("\
24161 When enabled (non-zero), debugging messages are printed during DWARF\n\
24162 reading and symtab expansion. A value of 1 (one) provides basic\n\
24163 information. A value greater than 1 provides more verbose information."),
24164 NULL,
24165 NULL,
24166 &setdebuglist, &showdebuglist);
24167
24168 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24169 Set debugging of the DWARF DIE reader."), _("\
24170 Show debugging of the DWARF DIE reader."), _("\
24171 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24172 The value is the maximum depth to print."),
24173 NULL,
24174 NULL,
24175 &setdebuglist, &showdebuglist);
24176
24177 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24178 Set debugging of the dwarf line reader."), _("\
24179 Show debugging of the dwarf line reader."), _("\
24180 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24181 A value of 1 (one) provides basic information.\n\
24182 A value greater than 1 provides more verbose information."),
24183 NULL,
24184 NULL,
24185 &setdebuglist, &showdebuglist);
24186
24187 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24188 Set cross-checking of \"physname\" code against demangler."), _("\
24189 Show cross-checking of \"physname\" code against demangler."), _("\
24190 When enabled, GDB's internal \"physname\" code is checked against\n\
24191 the demangler."),
24192 NULL, show_check_physname,
24193 &setdebuglist, &showdebuglist);
24194
24195 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24196 no_class, &use_deprecated_index_sections, _("\
24197 Set whether to use deprecated gdb_index sections."), _("\
24198 Show whether to use deprecated gdb_index sections."), _("\
24199 When enabled, deprecated .gdb_index sections are used anyway.\n\
24200 Normally they are ignored either because of a missing feature or\n\
24201 performance issue.\n\
24202 Warning: This option must be enabled before gdb reads the file."),
24203 NULL,
24204 NULL,
24205 &setlist, &showlist);
24206
24207 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24208 _("\
24209 Save a gdb-index file.\n\
24210 Usage: save gdb-index DIRECTORY"),
24211 &save_cmdlist);
24212 set_cmd_completer (c, filename_completer);
24213
24214 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24215 &dwarf2_locexpr_funcs);
24216 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24217 &dwarf2_loclist_funcs);
24218
24219 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24220 &dwarf2_block_frame_base_locexpr_funcs);
24221 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24222 &dwarf2_block_frame_base_loclist_funcs);
24223 }
This page took 0.546329 seconds and 4 git commands to generate.