* dwarf2read.c (struct dwarf2_per_cu_data): Move member
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 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 "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
92
93 static const struct objfile_data *dwarf2_objfile_data_key;
94
95 /* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
101
102 struct dwarf2_section_info
103 {
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
107 /* True if we have tried to read this section. */
108 int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127 /* Ensure only legit values are used. */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
142 /* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144 struct mapped_index
145 {
146 /* Index data format version. */
147 int version;
148
149 /* The total length of the buffer. */
150 off_t total_size;
151
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
154
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
160
161 /* Size in slots, each slot is 2 offset_types. */
162 offset_type symbol_table_slots;
163
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166 };
167
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
170
171 /* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
174 struct dwarf2_per_objfile
175 {
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info macro;
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
184 struct dwarf2_section_info addr;
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
187 struct dwarf2_section_info gdb_index;
188
189 VEC (dwarf2_section_info_def) *types;
190
191 /* Back link. */
192 struct objfile *objfile;
193
194 /* Table of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
201 /* The number of .debug_types-related CUs. */
202 int n_type_units;
203
204 /* The .debug_types-related CUs (TUs). */
205 struct signatured_type **all_type_units;
206
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
217
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
253
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
256 unsigned char using_index;
257
258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
259 struct mapped_index *index_table;
260
261 /* When using index_table, this keeps track of all quick_file_names entries.
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
268 htab_t quick_file_names_table;
269
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
273
274 /* Table mapping type DIEs to their struct type *.
275 This is NULL if not allocated yet.
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
281 };
282
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 23
306 };
307
308 /* List of DWO/DWP sections. */
309
310 static const struct dwop_section_names
311 {
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
344 {
345 unsigned int length;
346 short version;
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_offset;
350
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
353
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset offset;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_offset;
364 };
365
366 /* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368 struct delayed_method_info
369 {
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384 };
385
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
388
389 /* Internal state when decoding a particular compilation unit. */
390 struct dwarf2_cu
391 {
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
395 /* The header of the compilation unit. */
396 struct comp_unit_head header;
397
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
408 const char *producer;
409
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
425
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
448 htab_t die_hash;
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base;
495
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
503 unsigned int has_loclist : 1;
504
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
511 unsigned int producer_is_gcc_lt_4_3 : 1;
512 unsigned int producer_is_icc : 1;
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
519 };
520
521 /* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
523 read_symtab_private field of the psymtab. */
524
525 struct dwarf2_per_cu_data
526 {
527 /* The start offset and length of this compilation unit.
528 NOTE: Unlike comp_unit_head.length, this length includes
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
532 sect_offset offset;
533 unsigned int length;
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
537 unsigned int queued : 1;
538
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
545 /* Non-zero if this CU is from .debug_types.
546 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
547 this is non-zero. */
548 unsigned int is_debug_types : 1;
549
550 /* Non-zero if this CU is from the .dwz file. */
551 unsigned int is_dwz : 1;
552
553 /* The section this CU/TU lives in.
554 If the DIE refers to a DWO file, this is always the original die,
555 not the DWO file. */
556 struct dwarf2_section_info *section;
557
558 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
559 of the CU cache it gets reset to NULL again. */
560 struct dwarf2_cu *cu;
561
562 /* The corresponding objfile.
563 Normally we can get the objfile from dwarf2_per_objfile.
564 However we can enter this file with just a "per_cu" handle. */
565 struct objfile *objfile;
566
567 /* When using partial symbol tables, the 'psymtab' field is active.
568 Otherwise the 'quick' field is active. */
569 union
570 {
571 /* The partial symbol table associated with this compilation unit,
572 or NULL for unread partial units. */
573 struct partial_symtab *psymtab;
574
575 /* Data needed by the "quick" functions. */
576 struct dwarf2_per_cu_quick_data *quick;
577 } v;
578
579 /* The CUs we import using DW_TAG_imported_unit. This is filled in
580 while reading psymtabs, used to compute the psymtab dependencies,
581 and then cleared. Then it is filled in again while reading full
582 symbols, and only deleted when the objfile is destroyed.
583
584 This is also used to work around a difference between the way gold
585 generates .gdb_index version <=7 and the way gdb does. Arguably this
586 is a gold bug. For symbols coming from TUs, gold records in the index
587 the CU that includes the TU instead of the TU itself. This breaks
588 dw2_lookup_symbol: It assumes that if the index says symbol X lives
589 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
590 will find X. Alas TUs live in their own symtab, so after expanding CU Y
591 we need to look in TU Z to find X. Fortunately, this is akin to
592 DW_TAG_imported_unit, so we just use the same mechanism: For
593 .gdb_index version <=7 this also records the TUs that the CU referred
594 to. Concurrently with this change gdb was modified to emit version 8
595 indices so we only pay a price for gold generated indices. */
596 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
597 };
598
599 /* Entry in the signatured_types hash table. */
600
601 struct signatured_type
602 {
603 /* The "per_cu" object of this type.
604 N.B.: This is the first member so that it's easy to convert pointers
605 between them. */
606 struct dwarf2_per_cu_data per_cu;
607
608 /* The type's signature. */
609 ULONGEST signature;
610
611 /* Offset in the TU of the type's DIE, as read from the TU header.
612 If this TU is a DWO stub and the definition lives in a DWO file
613 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
614 cu_offset type_offset_in_tu;
615
616 /* Offset in the section of the type's DIE.
617 If the definition lives in a DWO file, this is the offset in the
618 .debug_types.dwo section.
619 The value is zero until the actual value is known.
620 Zero is otherwise not a valid section offset. */
621 sect_offset type_offset_in_section;
622
623 /* Type units are grouped by their DW_AT_stmt_list entry so that they
624 can share them. This points to the containing symtab. */
625 struct type_unit_group *type_unit_group;
626 };
627
628 typedef struct signatured_type *sig_type_ptr;
629 DEF_VEC_P (sig_type_ptr);
630
631 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
632 This includes type_unit_group and quick_file_names. */
633
634 struct stmt_list_hash
635 {
636 /* The DWO unit this table is from or NULL if there is none. */
637 struct dwo_unit *dwo_unit;
638
639 /* Offset in .debug_line or .debug_line.dwo. */
640 sect_offset line_offset;
641 };
642
643 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
644 an object of this type. */
645
646 struct type_unit_group
647 {
648 /* dwarf2read.c's main "handle" on a TU symtab.
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab.
652 This PER_CU is recognized by having no section. */
653 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
654 struct dwarf2_per_cu_data per_cu;
655
656 /* The TUs that share this DW_AT_stmt_list entry.
657 This is added to while parsing type units to build partial symtabs,
658 and is deleted afterwards and not used again. */
659 VEC (sig_type_ptr) *tus;
660
661 /* The primary symtab.
662 Type units in a group needn't all be defined in the same source file,
663 so we create an essentially anonymous symtab as the primary symtab. */
664 struct symtab *primary_symtab;
665
666 /* The data used to construct the hash key. */
667 struct stmt_list_hash hash;
668
669 /* The number of symtabs from the line header.
670 The value here must match line_header.num_file_names. */
671 unsigned int num_symtabs;
672
673 /* The symbol tables for this TU (obtained from the files listed in
674 DW_AT_stmt_list).
675 WARNING: The order of entries here must match the order of entries
676 in the line header. After the first TU using this type_unit_group, the
677 line header for the subsequent TUs is recreated from this. This is done
678 because we need to use the same symtabs for each TU using the same
679 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
680 there's no guarantee the line header doesn't have duplicate entries. */
681 struct symtab **symtabs;
682 };
683
684 /* These sections are what may appear in a DWO file. */
685
686 struct dwo_sections
687 {
688 struct dwarf2_section_info abbrev;
689 struct dwarf2_section_info line;
690 struct dwarf2_section_info loc;
691 struct dwarf2_section_info macinfo;
692 struct dwarf2_section_info macro;
693 struct dwarf2_section_info str;
694 struct dwarf2_section_info str_offsets;
695 /* In the case of a virtual DWO file, these two are unused. */
696 struct dwarf2_section_info info;
697 VEC (dwarf2_section_info_def) *types;
698 };
699
700 /* CUs/TUs in DWP/DWO files. */
701
702 struct dwo_unit
703 {
704 /* Backlink to the containing struct dwo_file. */
705 struct dwo_file *dwo_file;
706
707 /* The "id" that distinguishes this CU/TU.
708 .debug_info calls this "dwo_id", .debug_types calls this "signature".
709 Since signatures came first, we stick with it for consistency. */
710 ULONGEST signature;
711
712 /* The section this CU/TU lives in, in the DWO file. */
713 struct dwarf2_section_info *section;
714
715 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
716 sect_offset offset;
717 unsigned int length;
718
719 /* For types, offset in the type's DIE of the type defined by this TU. */
720 cu_offset type_offset_in_tu;
721 };
722
723 /* Data for one DWO file.
724 This includes virtual DWO files that have been packaged into a
725 DWP file. */
726
727 struct dwo_file
728 {
729 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
733 const char *name;
734
735 /* The bfd, when the file is open. Otherwise this is NULL.
736 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
737 bfd *dbfd;
738
739 /* Section info for this file. */
740 struct dwo_sections sections;
741
742 /* Table of CUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t cus;
745
746 /* Table of TUs in the file.
747 Each element is a struct dwo_unit. */
748 htab_t tus;
749 };
750
751 /* These sections are what may appear in a DWP file. */
752
753 struct dwp_sections
754 {
755 struct dwarf2_section_info str;
756 struct dwarf2_section_info cu_index;
757 struct dwarf2_section_info tu_index;
758 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
759 by section number. We don't need to record them here. */
760 };
761
762 /* These sections are what may appear in a virtual DWO file. */
763
764 struct virtual_dwo_sections
765 {
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 struct dwarf2_section_info info_or_types;
775 };
776
777 /* Contents of DWP hash tables. */
778
779 struct dwp_hash_table
780 {
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table, *section_pool;
783 };
784
785 /* Data for one DWP file. */
786
787 struct dwp_file
788 {
789 /* Name of the file. */
790 const char *name;
791
792 /* The bfd, when the file is open. Otherwise this is NULL. */
793 bfd *dbfd;
794
795 /* Section info for this file. */
796 struct dwp_sections sections;
797
798 /* Table of CUs in the file. */
799 const struct dwp_hash_table *cus;
800
801 /* Table of TUs in the file. */
802 const struct dwp_hash_table *tus;
803
804 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
805 htab_t loaded_cutus;
806
807 /* Table to map ELF section numbers to their sections. */
808 unsigned int num_sections;
809 asection **elf_sections;
810 };
811
812 /* This represents a '.dwz' file. */
813
814 struct dwz_file
815 {
816 /* A dwz file can only contain a few sections. */
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info info;
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info line;
821 struct dwarf2_section_info macro;
822 struct dwarf2_section_info gdb_index;
823
824 /* The dwz's BFD. */
825 bfd *dwz_bfd;
826 };
827
828 /* Struct used to pass misc. parameters to read_die_and_children, et
829 al. which are used for both .debug_info and .debug_types dies.
830 All parameters here are unchanging for the life of the call. This
831 struct exists to abstract away the constant parameters of die reading. */
832
833 struct die_reader_specs
834 {
835 /* die_section->asection->owner. */
836 bfd* abfd;
837
838 /* The CU of the DIE we are parsing. */
839 struct dwarf2_cu *cu;
840
841 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
842 struct dwo_file *dwo_file;
843
844 /* The section the die comes from.
845 This is either .debug_info or .debug_types, or the .dwo variants. */
846 struct dwarf2_section_info *die_section;
847
848 /* die_section->buffer. */
849 gdb_byte *buffer;
850
851 /* The end of the buffer. */
852 const gdb_byte *buffer_end;
853 };
854
855 /* Type of function passed to init_cutu_and_read_dies, et.al. */
856 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
857 gdb_byte *info_ptr,
858 struct die_info *comp_unit_die,
859 int has_children,
860 void *data);
861
862 /* The line number information for a compilation unit (found in the
863 .debug_line section) begins with a "statement program header",
864 which contains the following information. */
865 struct line_header
866 {
867 unsigned int total_length;
868 unsigned short version;
869 unsigned int header_length;
870 unsigned char minimum_instruction_length;
871 unsigned char maximum_ops_per_instruction;
872 unsigned char default_is_stmt;
873 int line_base;
874 unsigned char line_range;
875 unsigned char opcode_base;
876
877 /* standard_opcode_lengths[i] is the number of operands for the
878 standard opcode whose value is i. This means that
879 standard_opcode_lengths[0] is unused, and the last meaningful
880 element is standard_opcode_lengths[opcode_base - 1]. */
881 unsigned char *standard_opcode_lengths;
882
883 /* The include_directories table. NOTE! These strings are not
884 allocated with xmalloc; instead, they are pointers into
885 debug_line_buffer. If you try to free them, `free' will get
886 indigestion. */
887 unsigned int num_include_dirs, include_dirs_size;
888 char **include_dirs;
889
890 /* The file_names table. NOTE! These strings are not allocated
891 with xmalloc; instead, they are pointers into debug_line_buffer.
892 Don't try to free them directly. */
893 unsigned int num_file_names, file_names_size;
894 struct file_entry
895 {
896 char *name;
897 unsigned int dir_index;
898 unsigned int mod_time;
899 unsigned int length;
900 int included_p; /* Non-zero if referenced by the Line Number Program. */
901 struct symtab *symtab; /* The associated symbol table, if any. */
902 } *file_names;
903
904 /* The start and end of the statement program following this
905 header. These point into dwarf2_per_objfile->line_buffer. */
906 gdb_byte *statement_program_start, *statement_program_end;
907 };
908
909 /* When we construct a partial symbol table entry we only
910 need this much information. */
911 struct partial_die_info
912 {
913 /* Offset of this DIE. */
914 sect_offset offset;
915
916 /* DWARF-2 tag for this DIE. */
917 ENUM_BITFIELD(dwarf_tag) tag : 16;
918
919 /* Assorted flags describing the data found in this DIE. */
920 unsigned int has_children : 1;
921 unsigned int is_external : 1;
922 unsigned int is_declaration : 1;
923 unsigned int has_type : 1;
924 unsigned int has_specification : 1;
925 unsigned int has_pc_info : 1;
926 unsigned int may_be_inlined : 1;
927
928 /* Flag set if the SCOPE field of this structure has been
929 computed. */
930 unsigned int scope_set : 1;
931
932 /* Flag set if the DIE has a byte_size attribute. */
933 unsigned int has_byte_size : 1;
934
935 /* Flag set if any of the DIE's children are template arguments. */
936 unsigned int has_template_arguments : 1;
937
938 /* Flag set if fixup_partial_die has been called on this die. */
939 unsigned int fixup_called : 1;
940
941 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
942 unsigned int is_dwz : 1;
943
944 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
945 unsigned int spec_is_dwz : 1;
946
947 /* The name of this DIE. Normally the value of DW_AT_name, but
948 sometimes a default name for unnamed DIEs. */
949 const char *name;
950
951 /* The linkage name, if present. */
952 const char *linkage_name;
953
954 /* The scope to prepend to our children. This is generally
955 allocated on the comp_unit_obstack, so will disappear
956 when this compilation unit leaves the cache. */
957 const char *scope;
958
959 /* Some data associated with the partial DIE. The tag determines
960 which field is live. */
961 union
962 {
963 /* The location description associated with this DIE, if any. */
964 struct dwarf_block *locdesc;
965 /* The offset of an import, for DW_TAG_imported_unit. */
966 sect_offset offset;
967 } d;
968
969 /* If HAS_PC_INFO, the PC range associated with this DIE. */
970 CORE_ADDR lowpc;
971 CORE_ADDR highpc;
972
973 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
974 DW_AT_sibling, if any. */
975 /* NOTE: This member isn't strictly necessary, read_partial_die could
976 return DW_AT_sibling values to its caller load_partial_dies. */
977 gdb_byte *sibling;
978
979 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
980 DW_AT_specification (or DW_AT_abstract_origin or
981 DW_AT_extension). */
982 sect_offset spec_offset;
983
984 /* Pointers to this DIE's parent, first child, and next sibling,
985 if any. */
986 struct partial_die_info *die_parent, *die_child, *die_sibling;
987 };
988
989 /* This data structure holds the information of an abbrev. */
990 struct abbrev_info
991 {
992 unsigned int number; /* number identifying abbrev */
993 enum dwarf_tag tag; /* dwarf tag */
994 unsigned short has_children; /* boolean */
995 unsigned short num_attrs; /* number of attributes */
996 struct attr_abbrev *attrs; /* an array of attribute descriptions */
997 struct abbrev_info *next; /* next in chain */
998 };
999
1000 struct attr_abbrev
1001 {
1002 ENUM_BITFIELD(dwarf_attribute) name : 16;
1003 ENUM_BITFIELD(dwarf_form) form : 16;
1004 };
1005
1006 /* Size of abbrev_table.abbrev_hash_table. */
1007 #define ABBREV_HASH_SIZE 121
1008
1009 /* Top level data structure to contain an abbreviation table. */
1010
1011 struct abbrev_table
1012 {
1013 /* Where the abbrev table came from.
1014 This is used as a sanity check when the table is used. */
1015 sect_offset offset;
1016
1017 /* Storage for the abbrev table. */
1018 struct obstack abbrev_obstack;
1019
1020 /* Hash table of abbrevs.
1021 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1022 It could be statically allocated, but the previous code didn't so we
1023 don't either. */
1024 struct abbrev_info **abbrevs;
1025 };
1026
1027 /* Attributes have a name and a value. */
1028 struct attribute
1029 {
1030 ENUM_BITFIELD(dwarf_attribute) name : 16;
1031 ENUM_BITFIELD(dwarf_form) form : 15;
1032
1033 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1034 field should be in u.str (existing only for DW_STRING) but it is kept
1035 here for better struct attribute alignment. */
1036 unsigned int string_is_canonical : 1;
1037
1038 union
1039 {
1040 const char *str;
1041 struct dwarf_block *blk;
1042 ULONGEST unsnd;
1043 LONGEST snd;
1044 CORE_ADDR addr;
1045 struct signatured_type *signatured_type;
1046 }
1047 u;
1048 };
1049
1050 /* This data structure holds a complete die structure. */
1051 struct die_info
1052 {
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
1062
1063 /* Abbrev number */
1064 unsigned int abbrev;
1065
1066 /* Offset in .debug_info or .debug_types section. */
1067 sect_offset offset;
1068
1069 /* The dies in a compilation unit form an n-ary tree. PARENT
1070 points to this die's parent; CHILD points to the first child of
1071 this node; and all the children of a given node are chained
1072 together via their SIBLING fields. */
1073 struct die_info *child; /* Its first child, if any. */
1074 struct die_info *sibling; /* Its next sibling, if any. */
1075 struct die_info *parent; /* Its parent, if any. */
1076
1077 /* An array of attributes, with NUM_ATTRS elements. There may be
1078 zero, but it's not common and zero-sized arrays are not
1079 sufficiently portable C. */
1080 struct attribute attrs[1];
1081 };
1082
1083 /* Get at parts of an attribute structure. */
1084
1085 #define DW_STRING(attr) ((attr)->u.str)
1086 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1087 #define DW_UNSND(attr) ((attr)->u.unsnd)
1088 #define DW_BLOCK(attr) ((attr)->u.blk)
1089 #define DW_SND(attr) ((attr)->u.snd)
1090 #define DW_ADDR(attr) ((attr)->u.addr)
1091 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1092
1093 /* Blocks are a bunch of untyped bytes. */
1094 struct dwarf_block
1095 {
1096 size_t size;
1097
1098 /* Valid only if SIZE is not zero. */
1099 gdb_byte *data;
1100 };
1101
1102 #ifndef ATTR_ALLOC_CHUNK
1103 #define ATTR_ALLOC_CHUNK 4
1104 #endif
1105
1106 /* Allocate fields for structs, unions and enums in this size. */
1107 #ifndef DW_FIELD_ALLOC_CHUNK
1108 #define DW_FIELD_ALLOC_CHUNK 4
1109 #endif
1110
1111 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1112 but this would require a corresponding change in unpack_field_as_long
1113 and friends. */
1114 static int bits_per_byte = 8;
1115
1116 /* The routines that read and process dies for a C struct or C++ class
1117 pass lists of data member fields and lists of member function fields
1118 in an instance of a field_info structure, as defined below. */
1119 struct field_info
1120 {
1121 /* List of data member and baseclasses fields. */
1122 struct nextfield
1123 {
1124 struct nextfield *next;
1125 int accessibility;
1126 int virtuality;
1127 struct field field;
1128 }
1129 *fields, *baseclasses;
1130
1131 /* Number of fields (including baseclasses). */
1132 int nfields;
1133
1134 /* Number of baseclasses. */
1135 int nbaseclasses;
1136
1137 /* Set if the accesibility of one of the fields is not public. */
1138 int non_public_fields;
1139
1140 /* Member function fields array, entries are allocated in the order they
1141 are encountered in the object file. */
1142 struct nextfnfield
1143 {
1144 struct nextfnfield *next;
1145 struct fn_field fnfield;
1146 }
1147 *fnfields;
1148
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
1152 struct fnfieldlist
1153 {
1154 const char *name;
1155 int length;
1156 struct nextfnfield *head;
1157 }
1158 *fnfieldlists;
1159
1160 /* Number of entries in the fnfieldlists array. */
1161 int nfnfields;
1162
1163 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1164 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1165 struct typedef_field_list
1166 {
1167 struct typedef_field field;
1168 struct typedef_field_list *next;
1169 }
1170 *typedef_field_list;
1171 unsigned typedef_field_list_count;
1172 };
1173
1174 /* One item on the queue of compilation units to read in full symbols
1175 for. */
1176 struct dwarf2_queue_item
1177 {
1178 struct dwarf2_per_cu_data *per_cu;
1179 enum language pretend_language;
1180 struct dwarf2_queue_item *next;
1181 };
1182
1183 /* The current queue. */
1184 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf2_max_cache_age = 5;
1192 static void
1193 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "dwarf2 compilation units is %s.\n"),
1198 value);
1199 }
1200
1201
1202 /* Various complaints about symbol reading that don't abort the process. */
1203
1204 static void
1205 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1206 {
1207 complaint (&symfile_complaints,
1208 _("statement list doesn't fit in .debug_line section"));
1209 }
1210
1211 static void
1212 dwarf2_debug_line_missing_file_complaint (void)
1213 {
1214 complaint (&symfile_complaints,
1215 _(".debug_line section has line data without a file"));
1216 }
1217
1218 static void
1219 dwarf2_debug_line_missing_end_sequence_complaint (void)
1220 {
1221 complaint (&symfile_complaints,
1222 _(".debug_line section has line "
1223 "program sequence without an end"));
1224 }
1225
1226 static void
1227 dwarf2_complex_location_expr_complaint (void)
1228 {
1229 complaint (&symfile_complaints, _("location expression too complex"));
1230 }
1231
1232 static void
1233 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1234 int arg3)
1235 {
1236 complaint (&symfile_complaints,
1237 _("const value length mismatch for '%s', got %d, expected %d"),
1238 arg1, arg2, arg3);
1239 }
1240
1241 static void
1242 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1243 {
1244 complaint (&symfile_complaints,
1245 _("debug info runs off end of %s section"
1246 " [in module %s]"),
1247 section->asection->name,
1248 bfd_get_filename (section->asection->owner));
1249 }
1250
1251 static void
1252 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1253 {
1254 complaint (&symfile_complaints,
1255 _("macro debug info contains a "
1256 "malformed macro definition:\n`%s'"),
1257 arg1);
1258 }
1259
1260 static void
1261 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1262 {
1263 complaint (&symfile_complaints,
1264 _("invalid attribute class or form for '%s' in '%s'"),
1265 arg1, arg2);
1266 }
1267
1268 /* local function prototypes */
1269
1270 static void dwarf2_locate_sections (bfd *, asection *, void *);
1271
1272 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1273 struct objfile *);
1274
1275 static void dwarf2_find_base_address (struct die_info *die,
1276 struct dwarf2_cu *cu);
1277
1278 static struct partial_symtab *create_partial_symtab
1279 (struct dwarf2_per_cu_data *per_cu, const char *name);
1280
1281 static void dwarf2_build_psymtabs_hard (struct objfile *);
1282
1283 static void scan_partial_symbols (struct partial_die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 int, struct dwarf2_cu *);
1286
1287 static void add_partial_symbol (struct partial_die_info *,
1288 struct dwarf2_cu *);
1289
1290 static void add_partial_namespace (struct partial_die_info *pdi,
1291 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1292 int need_pc, struct dwarf2_cu *cu);
1293
1294 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1295 CORE_ADDR *highpc, int need_pc,
1296 struct dwarf2_cu *cu);
1297
1298 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1299 struct dwarf2_cu *cu);
1300
1301 static void add_partial_subprogram (struct partial_die_info *pdi,
1302 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1303 int need_pc, struct dwarf2_cu *cu);
1304
1305 static void dwarf2_read_symtab (struct partial_symtab *,
1306 struct objfile *);
1307
1308 static void psymtab_to_symtab_1 (struct partial_symtab *);
1309
1310 static struct abbrev_info *abbrev_table_lookup_abbrev
1311 (const struct abbrev_table *, unsigned int);
1312
1313 static struct abbrev_table *abbrev_table_read_table
1314 (struct dwarf2_section_info *, sect_offset);
1315
1316 static void abbrev_table_free (struct abbrev_table *);
1317
1318 static void abbrev_table_free_cleanup (void *);
1319
1320 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1321 struct dwarf2_section_info *);
1322
1323 static void dwarf2_free_abbrev_table (void *);
1324
1325 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1326
1327 static struct partial_die_info *load_partial_dies
1328 (const struct die_reader_specs *, gdb_byte *, int);
1329
1330 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1331 struct partial_die_info *,
1332 struct abbrev_info *,
1333 unsigned int,
1334 gdb_byte *);
1335
1336 static struct partial_die_info *find_partial_die (sect_offset, int,
1337 struct dwarf2_cu *);
1338
1339 static void fixup_partial_die (struct partial_die_info *,
1340 struct dwarf2_cu *);
1341
1342 static gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 gdb_byte *);
1345
1346 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1347
1348 static int read_1_signed_byte (bfd *, const gdb_byte *);
1349
1350 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1351
1352 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1353
1354 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1355
1356 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1357 unsigned int *);
1358
1359 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1360
1361 static LONGEST read_checked_initial_length_and_offset
1362 (bfd *, gdb_byte *, const struct comp_unit_head *,
1363 unsigned int *, unsigned int *);
1364
1365 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *);
1367
1368 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1369
1370 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1371 sect_offset);
1372
1373 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1374
1375 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1376
1377 static char *read_indirect_string (bfd *, gdb_byte *,
1378 const struct comp_unit_head *,
1379 unsigned int *);
1380
1381 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1382
1383 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1384
1385 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1386
1387 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1388 unsigned int *);
1389
1390 static char *read_str_index (const struct die_reader_specs *reader,
1391 struct dwarf2_cu *cu, ULONGEST str_index);
1392
1393 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1394
1395 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1396 struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1399 unsigned int);
1400
1401 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1402 struct dwarf2_cu *cu);
1403
1404 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1405
1406 static struct die_info *die_specification (struct die_info *die,
1407 struct dwarf2_cu **);
1408
1409 static void free_line_header (struct line_header *lh);
1410
1411 static void add_file_name (struct line_header *, char *, unsigned int,
1412 unsigned int, unsigned int);
1413
1414 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1415 struct dwarf2_cu *cu);
1416
1417 static void dwarf_decode_lines (struct line_header *, const char *,
1418 struct dwarf2_cu *, struct partial_symtab *,
1419 int);
1420
1421 static void dwarf2_start_subfile (char *, const char *, const char *);
1422
1423 static void dwarf2_start_symtab (struct dwarf2_cu *,
1424 const char *, const char *, CORE_ADDR);
1425
1426 static struct symbol *new_symbol (struct die_info *, struct type *,
1427 struct dwarf2_cu *);
1428
1429 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1430 struct dwarf2_cu *, struct symbol *);
1431
1432 static void dwarf2_const_value (struct attribute *, struct symbol *,
1433 struct dwarf2_cu *);
1434
1435 static void dwarf2_const_value_attr (struct attribute *attr,
1436 struct type *type,
1437 const char *name,
1438 struct obstack *obstack,
1439 struct dwarf2_cu *cu, LONGEST *value,
1440 gdb_byte **bytes,
1441 struct dwarf2_locexpr_baton **baton);
1442
1443 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1444
1445 static int need_gnat_info (struct dwarf2_cu *);
1446
1447 static struct type *die_descriptive_type (struct die_info *,
1448 struct dwarf2_cu *);
1449
1450 static void set_descriptive_type (struct type *, struct die_info *,
1451 struct dwarf2_cu *);
1452
1453 static struct type *die_containing_type (struct die_info *,
1454 struct dwarf2_cu *);
1455
1456 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1457 struct dwarf2_cu *);
1458
1459 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1460
1461 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1462
1463 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1464
1465 static char *typename_concat (struct obstack *obs, const char *prefix,
1466 const char *suffix, int physname,
1467 struct dwarf2_cu *cu);
1468
1469 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1470
1471 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1472
1473 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1474
1475 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1476
1477 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1478
1479 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *, struct partial_symtab *);
1481
1482 static int dwarf2_get_pc_bounds (struct die_info *,
1483 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1484 struct partial_symtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
1517 static struct type *read_module_type (struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
1520 static const char *namespace_name (struct die_info *die,
1521 int *is_anonymous, struct dwarf2_cu *);
1522
1523 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1524
1525 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1526
1527 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1528 struct dwarf2_cu *);
1529
1530 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1531 gdb_byte *info_ptr,
1532 gdb_byte **new_info_ptr,
1533 struct die_info *parent);
1534
1535 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1536 gdb_byte *info_ptr,
1537 gdb_byte **new_info_ptr,
1538 struct die_info *parent);
1539
1540 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1541 struct die_info **, gdb_byte *, int *, int);
1542
1543 static gdb_byte *read_full_die (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *);
1545
1546 static void process_die (struct die_info *, struct dwarf2_cu *);
1547
1548 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1549 struct obstack *);
1550
1551 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_full_name (const char *name,
1554 struct die_info *die,
1555 struct dwarf2_cu *cu);
1556
1557 static const char *dwarf2_physname (const char *name, struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
1560 static struct die_info *dwarf2_extension (struct die_info *die,
1561 struct dwarf2_cu **);
1562
1563 static const char *dwarf_tag_name (unsigned int);
1564
1565 static const char *dwarf_attr_name (unsigned int);
1566
1567 static const char *dwarf_form_name (unsigned int);
1568
1569 static char *dwarf_bool_name (unsigned int);
1570
1571 static const char *dwarf_type_encoding_name (unsigned int);
1572
1573 static struct die_info *sibling_die (struct die_info *);
1574
1575 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1576
1577 static void dump_die_for_error (struct die_info *);
1578
1579 static void dump_die_1 (struct ui_file *, int level, int max_level,
1580 struct die_info *);
1581
1582 /*static*/ void dump_die (struct die_info *, int max_level);
1583
1584 static void store_in_ref_table (struct die_info *,
1585 struct dwarf2_cu *);
1586
1587 static int is_ref_attr (struct attribute *);
1588
1589 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1590
1591 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1592
1593 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1594 struct attribute *,
1595 struct dwarf2_cu **);
1596
1597 static struct die_info *follow_die_ref (struct die_info *,
1598 struct attribute *,
1599 struct dwarf2_cu **);
1600
1601 static struct die_info *follow_die_sig (struct die_info *,
1602 struct attribute *,
1603 struct dwarf2_cu **);
1604
1605 static struct signatured_type *lookup_signatured_type_at_offset
1606 (struct objfile *objfile,
1607 struct dwarf2_section_info *section, sect_offset offset);
1608
1609 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1610
1611 static void read_signatured_type (struct signatured_type *);
1612
1613 static struct type_unit_group *get_type_unit_group
1614 (struct dwarf2_cu *, struct attribute *);
1615
1616 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1617
1618 /* memory allocation interface */
1619
1620 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1621
1622 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1623
1624 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1625 const char *, int);
1626
1627 static int attr_form_is_block (struct attribute *);
1628
1629 static int attr_form_is_section_offset (struct attribute *);
1630
1631 static int attr_form_is_constant (struct attribute *);
1632
1633 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1634 struct dwarf2_loclist_baton *baton,
1635 struct attribute *attr);
1636
1637 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1638 struct symbol *sym,
1639 struct dwarf2_cu *cu,
1640 int is_block);
1641
1642 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1643 gdb_byte *info_ptr,
1644 struct abbrev_info *abbrev);
1645
1646 static void free_stack_comp_unit (void *);
1647
1648 static hashval_t partial_die_hash (const void *item);
1649
1650 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1651
1652 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1653 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1654
1655 static void init_one_comp_unit (struct dwarf2_cu *cu,
1656 struct dwarf2_per_cu_data *per_cu);
1657
1658 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1659 struct die_info *comp_unit_die,
1660 enum language pretend_language);
1661
1662 static void free_heap_comp_unit (void *);
1663
1664 static void free_cached_comp_units (void *);
1665
1666 static void age_cached_comp_units (void);
1667
1668 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1669
1670 static struct type *set_die_type (struct die_info *, struct type *,
1671 struct dwarf2_cu *);
1672
1673 static void create_all_comp_units (struct objfile *);
1674
1675 static int create_all_type_units (struct objfile *);
1676
1677 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1678 enum language);
1679
1680 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1681 enum language);
1682
1683 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1684 enum language);
1685
1686 static void dwarf2_add_dependence (struct dwarf2_cu *,
1687 struct dwarf2_per_cu_data *);
1688
1689 static void dwarf2_mark (struct dwarf2_cu *);
1690
1691 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1692
1693 static struct type *get_die_type_at_offset (sect_offset,
1694 struct dwarf2_per_cu_data *per_cu);
1695
1696 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1697
1698 static void dwarf2_release_queue (void *dummy);
1699
1700 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1701 enum language pretend_language);
1702
1703 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1704 struct dwarf2_per_cu_data *per_cu,
1705 enum language pretend_language);
1706
1707 static void process_queue (void);
1708
1709 static void find_file_and_directory (struct die_info *die,
1710 struct dwarf2_cu *cu,
1711 const char **name, const char **comp_dir);
1712
1713 static char *file_full_name (int file, struct line_header *lh,
1714 const char *comp_dir);
1715
1716 static gdb_byte *read_and_check_comp_unit_head
1717 (struct comp_unit_head *header,
1718 struct dwarf2_section_info *section,
1719 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1720 int is_debug_types_section);
1721
1722 static void init_cutu_and_read_dies
1723 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1724 int use_existing_cu, int keep,
1725 die_reader_func_ftype *die_reader_func, void *data);
1726
1727 static void init_cutu_and_read_dies_simple
1728 (struct dwarf2_per_cu_data *this_cu,
1729 die_reader_func_ftype *die_reader_func, void *data);
1730
1731 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1732
1733 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1734
1735 static struct dwo_unit *lookup_dwo_comp_unit
1736 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1737
1738 static struct dwo_unit *lookup_dwo_type_unit
1739 (struct signatured_type *, const char *, const char *);
1740
1741 static void free_dwo_file_cleanup (void *);
1742
1743 static void process_cu_includes (void);
1744
1745 static void check_producer (struct dwarf2_cu *cu);
1746
1747 #if WORDS_BIGENDIAN
1748
1749 /* Convert VALUE between big- and little-endian. */
1750 static offset_type
1751 byte_swap (offset_type value)
1752 {
1753 offset_type result;
1754
1755 result = (value & 0xff) << 24;
1756 result |= (value & 0xff00) << 8;
1757 result |= (value & 0xff0000) >> 8;
1758 result |= (value & 0xff000000) >> 24;
1759 return result;
1760 }
1761
1762 #define MAYBE_SWAP(V) byte_swap (V)
1763
1764 #else
1765 #define MAYBE_SWAP(V) (V)
1766 #endif /* WORDS_BIGENDIAN */
1767
1768 /* The suffix for an index file. */
1769 #define INDEX_SUFFIX ".gdb-index"
1770
1771 /* Try to locate the sections we need for DWARF 2 debugging
1772 information and return true if we have enough to do something.
1773 NAMES points to the dwarf2 section names, or is NULL if the standard
1774 ELF names are used. */
1775
1776 int
1777 dwarf2_has_info (struct objfile *objfile,
1778 const struct dwarf2_debug_sections *names)
1779 {
1780 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1781 if (!dwarf2_per_objfile)
1782 {
1783 /* Initialize per-objfile state. */
1784 struct dwarf2_per_objfile *data
1785 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1786
1787 memset (data, 0, sizeof (*data));
1788 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1789 dwarf2_per_objfile = data;
1790
1791 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1792 (void *) names);
1793 dwarf2_per_objfile->objfile = objfile;
1794 }
1795 return (dwarf2_per_objfile->info.asection != NULL
1796 && dwarf2_per_objfile->abbrev.asection != NULL);
1797 }
1798
1799 /* When loading sections, we look either for uncompressed section or for
1800 compressed section names. */
1801
1802 static int
1803 section_is_p (const char *section_name,
1804 const struct dwarf2_section_names *names)
1805 {
1806 if (names->normal != NULL
1807 && strcmp (section_name, names->normal) == 0)
1808 return 1;
1809 if (names->compressed != NULL
1810 && strcmp (section_name, names->compressed) == 0)
1811 return 1;
1812 return 0;
1813 }
1814
1815 /* This function is mapped across the sections and remembers the
1816 offset and size of each of the debugging sections we are interested
1817 in. */
1818
1819 static void
1820 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1821 {
1822 const struct dwarf2_debug_sections *names;
1823 flagword aflag = bfd_get_section_flags (abfd, sectp);
1824
1825 if (vnames == NULL)
1826 names = &dwarf2_elf_names;
1827 else
1828 names = (const struct dwarf2_debug_sections *) vnames;
1829
1830 if ((aflag & SEC_HAS_CONTENTS) == 0)
1831 {
1832 }
1833 else if (section_is_p (sectp->name, &names->info))
1834 {
1835 dwarf2_per_objfile->info.asection = sectp;
1836 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1837 }
1838 else if (section_is_p (sectp->name, &names->abbrev))
1839 {
1840 dwarf2_per_objfile->abbrev.asection = sectp;
1841 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1842 }
1843 else if (section_is_p (sectp->name, &names->line))
1844 {
1845 dwarf2_per_objfile->line.asection = sectp;
1846 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1847 }
1848 else if (section_is_p (sectp->name, &names->loc))
1849 {
1850 dwarf2_per_objfile->loc.asection = sectp;
1851 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1852 }
1853 else if (section_is_p (sectp->name, &names->macinfo))
1854 {
1855 dwarf2_per_objfile->macinfo.asection = sectp;
1856 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1857 }
1858 else if (section_is_p (sectp->name, &names->macro))
1859 {
1860 dwarf2_per_objfile->macro.asection = sectp;
1861 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1862 }
1863 else if (section_is_p (sectp->name, &names->str))
1864 {
1865 dwarf2_per_objfile->str.asection = sectp;
1866 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1867 }
1868 else if (section_is_p (sectp->name, &names->addr))
1869 {
1870 dwarf2_per_objfile->addr.asection = sectp;
1871 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1872 }
1873 else if (section_is_p (sectp->name, &names->frame))
1874 {
1875 dwarf2_per_objfile->frame.asection = sectp;
1876 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1877 }
1878 else if (section_is_p (sectp->name, &names->eh_frame))
1879 {
1880 dwarf2_per_objfile->eh_frame.asection = sectp;
1881 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1882 }
1883 else if (section_is_p (sectp->name, &names->ranges))
1884 {
1885 dwarf2_per_objfile->ranges.asection = sectp;
1886 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1887 }
1888 else if (section_is_p (sectp->name, &names->types))
1889 {
1890 struct dwarf2_section_info type_section;
1891
1892 memset (&type_section, 0, sizeof (type_section));
1893 type_section.asection = sectp;
1894 type_section.size = bfd_get_section_size (sectp);
1895
1896 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1897 &type_section);
1898 }
1899 else if (section_is_p (sectp->name, &names->gdb_index))
1900 {
1901 dwarf2_per_objfile->gdb_index.asection = sectp;
1902 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1903 }
1904
1905 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1906 && bfd_section_vma (abfd, sectp) == 0)
1907 dwarf2_per_objfile->has_section_at_zero = 1;
1908 }
1909
1910 /* A helper function that decides whether a section is empty,
1911 or not present. */
1912
1913 static int
1914 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1915 {
1916 return info->asection == NULL || info->size == 0;
1917 }
1918
1919 /* Read the contents of the section INFO.
1920 OBJFILE is the main object file, but not necessarily the file where
1921 the section comes from. E.g., for DWO files INFO->asection->owner
1922 is the bfd of the DWO file.
1923 If the section is compressed, uncompress it before returning. */
1924
1925 static void
1926 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1927 {
1928 asection *sectp = info->asection;
1929 bfd *abfd;
1930 gdb_byte *buf, *retbuf;
1931 unsigned char header[4];
1932
1933 if (info->readin)
1934 return;
1935 info->buffer = NULL;
1936 info->readin = 1;
1937
1938 if (dwarf2_section_empty_p (info))
1939 return;
1940
1941 abfd = sectp->owner;
1942
1943 /* If the section has relocations, we must read it ourselves.
1944 Otherwise we attach it to the BFD. */
1945 if ((sectp->flags & SEC_RELOC) == 0)
1946 {
1947 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1948
1949 /* We have to cast away const here for historical reasons.
1950 Fixing dwarf2read to be const-correct would be quite nice. */
1951 info->buffer = (gdb_byte *) bytes;
1952 return;
1953 }
1954
1955 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1956 info->buffer = buf;
1957
1958 /* When debugging .o files, we may need to apply relocations; see
1959 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1960 We never compress sections in .o files, so we only need to
1961 try this when the section is not compressed. */
1962 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1963 if (retbuf != NULL)
1964 {
1965 info->buffer = retbuf;
1966 return;
1967 }
1968
1969 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1970 || bfd_bread (buf, info->size, abfd) != info->size)
1971 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1972 bfd_get_filename (abfd));
1973 }
1974
1975 /* A helper function that returns the size of a section in a safe way.
1976 If you are positive that the section has been read before using the
1977 size, then it is safe to refer to the dwarf2_section_info object's
1978 "size" field directly. In other cases, you must call this
1979 function, because for compressed sections the size field is not set
1980 correctly until the section has been read. */
1981
1982 static bfd_size_type
1983 dwarf2_section_size (struct objfile *objfile,
1984 struct dwarf2_section_info *info)
1985 {
1986 if (!info->readin)
1987 dwarf2_read_section (objfile, info);
1988 return info->size;
1989 }
1990
1991 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1992 SECTION_NAME. */
1993
1994 void
1995 dwarf2_get_section_info (struct objfile *objfile,
1996 enum dwarf2_section_enum sect,
1997 asection **sectp, gdb_byte **bufp,
1998 bfd_size_type *sizep)
1999 {
2000 struct dwarf2_per_objfile *data
2001 = objfile_data (objfile, dwarf2_objfile_data_key);
2002 struct dwarf2_section_info *info;
2003
2004 /* We may see an objfile without any DWARF, in which case we just
2005 return nothing. */
2006 if (data == NULL)
2007 {
2008 *sectp = NULL;
2009 *bufp = NULL;
2010 *sizep = 0;
2011 return;
2012 }
2013 switch (sect)
2014 {
2015 case DWARF2_DEBUG_FRAME:
2016 info = &data->frame;
2017 break;
2018 case DWARF2_EH_FRAME:
2019 info = &data->eh_frame;
2020 break;
2021 default:
2022 gdb_assert_not_reached ("unexpected section");
2023 }
2024
2025 dwarf2_read_section (objfile, info);
2026
2027 *sectp = info->asection;
2028 *bufp = info->buffer;
2029 *sizep = info->size;
2030 }
2031
2032 /* A helper function to find the sections for a .dwz file. */
2033
2034 static void
2035 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2036 {
2037 struct dwz_file *dwz_file = arg;
2038
2039 /* Note that we only support the standard ELF names, because .dwz
2040 is ELF-only (at the time of writing). */
2041 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2042 {
2043 dwz_file->abbrev.asection = sectp;
2044 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2045 }
2046 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2047 {
2048 dwz_file->info.asection = sectp;
2049 dwz_file->info.size = bfd_get_section_size (sectp);
2050 }
2051 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2052 {
2053 dwz_file->str.asection = sectp;
2054 dwz_file->str.size = bfd_get_section_size (sectp);
2055 }
2056 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2057 {
2058 dwz_file->line.asection = sectp;
2059 dwz_file->line.size = bfd_get_section_size (sectp);
2060 }
2061 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2062 {
2063 dwz_file->macro.asection = sectp;
2064 dwz_file->macro.size = bfd_get_section_size (sectp);
2065 }
2066 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2067 {
2068 dwz_file->gdb_index.asection = sectp;
2069 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2070 }
2071 }
2072
2073 /* Open the separate '.dwz' debug file, if needed. Error if the file
2074 cannot be found. */
2075
2076 static struct dwz_file *
2077 dwarf2_get_dwz_file (void)
2078 {
2079 bfd *abfd, *dwz_bfd;
2080 asection *section;
2081 gdb_byte *data;
2082 struct cleanup *cleanup;
2083 const char *filename;
2084 struct dwz_file *result;
2085
2086 if (dwarf2_per_objfile->dwz_file != NULL)
2087 return dwarf2_per_objfile->dwz_file;
2088
2089 abfd = dwarf2_per_objfile->objfile->obfd;
2090 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2091 if (section == NULL)
2092 error (_("could not find '.gnu_debugaltlink' section"));
2093 if (!bfd_malloc_and_get_section (abfd, section, &data))
2094 error (_("could not read '.gnu_debugaltlink' section: %s"),
2095 bfd_errmsg (bfd_get_error ()));
2096 cleanup = make_cleanup (xfree, data);
2097
2098 filename = data;
2099 if (!IS_ABSOLUTE_PATH (filename))
2100 {
2101 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2102 char *rel;
2103
2104 make_cleanup (xfree, abs);
2105 abs = ldirname (abs);
2106 make_cleanup (xfree, abs);
2107
2108 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2109 make_cleanup (xfree, rel);
2110 filename = rel;
2111 }
2112
2113 /* The format is just a NUL-terminated file name, followed by the
2114 build-id. For now, though, we ignore the build-id. */
2115 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2116 if (dwz_bfd == NULL)
2117 error (_("could not read '%s': %s"), filename,
2118 bfd_errmsg (bfd_get_error ()));
2119
2120 if (!bfd_check_format (dwz_bfd, bfd_object))
2121 {
2122 gdb_bfd_unref (dwz_bfd);
2123 error (_("file '%s' was not usable: %s"), filename,
2124 bfd_errmsg (bfd_get_error ()));
2125 }
2126
2127 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2128 struct dwz_file);
2129 result->dwz_bfd = dwz_bfd;
2130
2131 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2132
2133 do_cleanups (cleanup);
2134
2135 dwarf2_per_objfile->dwz_file = result;
2136 return result;
2137 }
2138 \f
2139 /* DWARF quick_symbols_functions support. */
2140
2141 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2142 unique line tables, so we maintain a separate table of all .debug_line
2143 derived entries to support the sharing.
2144 All the quick functions need is the list of file names. We discard the
2145 line_header when we're done and don't need to record it here. */
2146 struct quick_file_names
2147 {
2148 /* The data used to construct the hash key. */
2149 struct stmt_list_hash hash;
2150
2151 /* The number of entries in file_names, real_names. */
2152 unsigned int num_file_names;
2153
2154 /* The file names from the line table, after being run through
2155 file_full_name. */
2156 const char **file_names;
2157
2158 /* The file names from the line table after being run through
2159 gdb_realpath. These are computed lazily. */
2160 const char **real_names;
2161 };
2162
2163 /* When using the index (and thus not using psymtabs), each CU has an
2164 object of this type. This is used to hold information needed by
2165 the various "quick" methods. */
2166 struct dwarf2_per_cu_quick_data
2167 {
2168 /* The file table. This can be NULL if there was no file table
2169 or it's currently not read in.
2170 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2171 struct quick_file_names *file_names;
2172
2173 /* The corresponding symbol table. This is NULL if symbols for this
2174 CU have not yet been read. */
2175 struct symtab *symtab;
2176
2177 /* A temporary mark bit used when iterating over all CUs in
2178 expand_symtabs_matching. */
2179 unsigned int mark : 1;
2180
2181 /* True if we've tried to read the file table and found there isn't one.
2182 There will be no point in trying to read it again next time. */
2183 unsigned int no_file_data : 1;
2184 };
2185
2186 /* Utility hash function for a stmt_list_hash. */
2187
2188 static hashval_t
2189 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2190 {
2191 hashval_t v = 0;
2192
2193 if (stmt_list_hash->dwo_unit != NULL)
2194 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2195 v += stmt_list_hash->line_offset.sect_off;
2196 return v;
2197 }
2198
2199 /* Utility equality function for a stmt_list_hash. */
2200
2201 static int
2202 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2203 const struct stmt_list_hash *rhs)
2204 {
2205 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2206 return 0;
2207 if (lhs->dwo_unit != NULL
2208 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2209 return 0;
2210
2211 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2212 }
2213
2214 /* Hash function for a quick_file_names. */
2215
2216 static hashval_t
2217 hash_file_name_entry (const void *e)
2218 {
2219 const struct quick_file_names *file_data = e;
2220
2221 return hash_stmt_list_entry (&file_data->hash);
2222 }
2223
2224 /* Equality function for a quick_file_names. */
2225
2226 static int
2227 eq_file_name_entry (const void *a, const void *b)
2228 {
2229 const struct quick_file_names *ea = a;
2230 const struct quick_file_names *eb = b;
2231
2232 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2233 }
2234
2235 /* Delete function for a quick_file_names. */
2236
2237 static void
2238 delete_file_name_entry (void *e)
2239 {
2240 struct quick_file_names *file_data = e;
2241 int i;
2242
2243 for (i = 0; i < file_data->num_file_names; ++i)
2244 {
2245 xfree ((void*) file_data->file_names[i]);
2246 if (file_data->real_names)
2247 xfree ((void*) file_data->real_names[i]);
2248 }
2249
2250 /* The space for the struct itself lives on objfile_obstack,
2251 so we don't free it here. */
2252 }
2253
2254 /* Create a quick_file_names hash table. */
2255
2256 static htab_t
2257 create_quick_file_names_table (unsigned int nr_initial_entries)
2258 {
2259 return htab_create_alloc (nr_initial_entries,
2260 hash_file_name_entry, eq_file_name_entry,
2261 delete_file_name_entry, xcalloc, xfree);
2262 }
2263
2264 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2265 have to be created afterwards. You should call age_cached_comp_units after
2266 processing PER_CU->CU. dw2_setup must have been already called. */
2267
2268 static void
2269 load_cu (struct dwarf2_per_cu_data *per_cu)
2270 {
2271 if (per_cu->is_debug_types)
2272 load_full_type_unit (per_cu);
2273 else
2274 load_full_comp_unit (per_cu, language_minimal);
2275
2276 gdb_assert (per_cu->cu != NULL);
2277
2278 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2279 }
2280
2281 /* Read in the symbols for PER_CU. */
2282
2283 static void
2284 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2285 {
2286 struct cleanup *back_to;
2287
2288 /* Skip type_unit_groups, reading the type units they contain
2289 is handled elsewhere. */
2290 if (IS_TYPE_UNIT_GROUP (per_cu))
2291 return;
2292
2293 back_to = make_cleanup (dwarf2_release_queue, NULL);
2294
2295 if (dwarf2_per_objfile->using_index
2296 ? per_cu->v.quick->symtab == NULL
2297 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2298 {
2299 queue_comp_unit (per_cu, language_minimal);
2300 load_cu (per_cu);
2301 }
2302
2303 process_queue ();
2304
2305 /* Age the cache, releasing compilation units that have not
2306 been used recently. */
2307 age_cached_comp_units ();
2308
2309 do_cleanups (back_to);
2310 }
2311
2312 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2313 the objfile from which this CU came. Returns the resulting symbol
2314 table. */
2315
2316 static struct symtab *
2317 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2318 {
2319 gdb_assert (dwarf2_per_objfile->using_index);
2320 if (!per_cu->v.quick->symtab)
2321 {
2322 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2323 increment_reading_symtab ();
2324 dw2_do_instantiate_symtab (per_cu);
2325 process_cu_includes ();
2326 do_cleanups (back_to);
2327 }
2328 return per_cu->v.quick->symtab;
2329 }
2330
2331 /* Return the CU given its index.
2332
2333 This is intended for loops like:
2334
2335 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2336 + dwarf2_per_objfile->n_type_units); ++i)
2337 {
2338 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2339
2340 ...;
2341 }
2342 */
2343
2344 static struct dwarf2_per_cu_data *
2345 dw2_get_cu (int index)
2346 {
2347 if (index >= dwarf2_per_objfile->n_comp_units)
2348 {
2349 index -= dwarf2_per_objfile->n_comp_units;
2350 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2351 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2352 }
2353
2354 return dwarf2_per_objfile->all_comp_units[index];
2355 }
2356
2357 /* Return the primary CU given its index.
2358 The difference between this function and dw2_get_cu is in the handling
2359 of type units (TUs). Here we return the type_unit_group object.
2360
2361 This is intended for loops like:
2362
2363 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2364 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2365 {
2366 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2367
2368 ...;
2369 }
2370 */
2371
2372 static struct dwarf2_per_cu_data *
2373 dw2_get_primary_cu (int index)
2374 {
2375 if (index >= dwarf2_per_objfile->n_comp_units)
2376 {
2377 index -= dwarf2_per_objfile->n_comp_units;
2378 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2379 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2380 }
2381
2382 return dwarf2_per_objfile->all_comp_units[index];
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (struct objfile *objfile,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz,
2393 int base_offset)
2394 {
2395 offset_type i;
2396
2397 for (i = 0; i < n_elements; i += 2)
2398 {
2399 struct dwarf2_per_cu_data *the_cu;
2400 ULONGEST offset, length;
2401
2402 gdb_static_assert (sizeof (ULONGEST) >= 8);
2403 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2404 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2405 cu_list += 2 * 8;
2406
2407 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2408 struct dwarf2_per_cu_data);
2409 the_cu->offset.sect_off = offset;
2410 the_cu->length = length;
2411 the_cu->objfile = objfile;
2412 the_cu->section = section;
2413 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2414 struct dwarf2_per_cu_quick_data);
2415 the_cu->is_dwz = is_dwz;
2416 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2417 }
2418 }
2419
2420 /* Read the CU list from the mapped index, and use it to create all
2421 the CU objects for this objfile. */
2422
2423 static void
2424 create_cus_from_index (struct objfile *objfile,
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427 {
2428 struct dwz_file *dwz;
2429
2430 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2431 dwarf2_per_objfile->all_comp_units
2432 = obstack_alloc (&objfile->objfile_obstack,
2433 dwarf2_per_objfile->n_comp_units
2434 * sizeof (struct dwarf2_per_cu_data *));
2435
2436 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2437 &dwarf2_per_objfile->info, 0, 0);
2438
2439 if (dwz_elements == 0)
2440 return;
2441
2442 dwz = dwarf2_get_dwz_file ();
2443 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2444 cu_list_elements / 2);
2445 }
2446
2447 /* Create the signatured type hash table from the index. */
2448
2449 static void
2450 create_signatured_type_table_from_index (struct objfile *objfile,
2451 struct dwarf2_section_info *section,
2452 const gdb_byte *bytes,
2453 offset_type elements)
2454 {
2455 offset_type i;
2456 htab_t sig_types_hash;
2457
2458 dwarf2_per_objfile->n_type_units = elements / 3;
2459 dwarf2_per_objfile->all_type_units
2460 = obstack_alloc (&objfile->objfile_obstack,
2461 dwarf2_per_objfile->n_type_units
2462 * sizeof (struct signatured_type *));
2463
2464 sig_types_hash = allocate_signatured_type_table (objfile);
2465
2466 for (i = 0; i < elements; i += 3)
2467 {
2468 struct signatured_type *sig_type;
2469 ULONGEST offset, type_offset_in_tu, signature;
2470 void **slot;
2471
2472 gdb_static_assert (sizeof (ULONGEST) >= 8);
2473 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2474 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2475 BFD_ENDIAN_LITTLE);
2476 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2477 bytes += 3 * 8;
2478
2479 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2480 struct signatured_type);
2481 sig_type->signature = signature;
2482 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2483 sig_type->per_cu.is_debug_types = 1;
2484 sig_type->per_cu.section = section;
2485 sig_type->per_cu.offset.sect_off = offset;
2486 sig_type->per_cu.objfile = objfile;
2487 sig_type->per_cu.v.quick
2488 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2489 struct dwarf2_per_cu_quick_data);
2490
2491 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2492 *slot = sig_type;
2493
2494 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2495 }
2496
2497 dwarf2_per_objfile->signatured_types = sig_types_hash;
2498 }
2499
2500 /* Read the address map data from the mapped index, and use it to
2501 populate the objfile's psymtabs_addrmap. */
2502
2503 static void
2504 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2505 {
2506 const gdb_byte *iter, *end;
2507 struct obstack temp_obstack;
2508 struct addrmap *mutable_map;
2509 struct cleanup *cleanup;
2510 CORE_ADDR baseaddr;
2511
2512 obstack_init (&temp_obstack);
2513 cleanup = make_cleanup_obstack_free (&temp_obstack);
2514 mutable_map = addrmap_create_mutable (&temp_obstack);
2515
2516 iter = index->address_table;
2517 end = iter + index->address_table_size;
2518
2519 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2520
2521 while (iter < end)
2522 {
2523 ULONGEST hi, lo, cu_index;
2524 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2525 iter += 8;
2526 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2527 iter += 8;
2528 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2529 iter += 4;
2530
2531 if (cu_index < dwarf2_per_objfile->n_comp_units)
2532 {
2533 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2534 dw2_get_cu (cu_index));
2535 }
2536 else
2537 {
2538 complaint (&symfile_complaints,
2539 _(".gdb_index address table has invalid CU number %u"),
2540 (unsigned) cu_index);
2541 }
2542 }
2543
2544 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2545 &objfile->objfile_obstack);
2546 do_cleanups (cleanup);
2547 }
2548
2549 /* The hash function for strings in the mapped index. This is the same as
2550 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2551 implementation. This is necessary because the hash function is tied to the
2552 format of the mapped index file. The hash values do not have to match with
2553 SYMBOL_HASH_NEXT.
2554
2555 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2556
2557 static hashval_t
2558 mapped_index_string_hash (int index_version, const void *p)
2559 {
2560 const unsigned char *str = (const unsigned char *) p;
2561 hashval_t r = 0;
2562 unsigned char c;
2563
2564 while ((c = *str++) != 0)
2565 {
2566 if (index_version >= 5)
2567 c = tolower (c);
2568 r = r * 67 + c - 113;
2569 }
2570
2571 return r;
2572 }
2573
2574 /* Find a slot in the mapped index INDEX for the object named NAME.
2575 If NAME is found, set *VEC_OUT to point to the CU vector in the
2576 constant pool and return 1. If NAME cannot be found, return 0. */
2577
2578 static int
2579 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2580 offset_type **vec_out)
2581 {
2582 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2583 offset_type hash;
2584 offset_type slot, step;
2585 int (*cmp) (const char *, const char *);
2586
2587 if (current_language->la_language == language_cplus
2588 || current_language->la_language == language_java
2589 || current_language->la_language == language_fortran)
2590 {
2591 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2592 not contain any. */
2593 const char *paren = strchr (name, '(');
2594
2595 if (paren)
2596 {
2597 char *dup;
2598
2599 dup = xmalloc (paren - name + 1);
2600 memcpy (dup, name, paren - name);
2601 dup[paren - name] = 0;
2602
2603 make_cleanup (xfree, dup);
2604 name = dup;
2605 }
2606 }
2607
2608 /* Index version 4 did not support case insensitive searches. But the
2609 indices for case insensitive languages are built in lowercase, therefore
2610 simulate our NAME being searched is also lowercased. */
2611 hash = mapped_index_string_hash ((index->version == 4
2612 && case_sensitivity == case_sensitive_off
2613 ? 5 : index->version),
2614 name);
2615
2616 slot = hash & (index->symbol_table_slots - 1);
2617 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2618 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2619
2620 for (;;)
2621 {
2622 /* Convert a slot number to an offset into the table. */
2623 offset_type i = 2 * slot;
2624 const char *str;
2625 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2626 {
2627 do_cleanups (back_to);
2628 return 0;
2629 }
2630
2631 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2632 if (!cmp (name, str))
2633 {
2634 *vec_out = (offset_type *) (index->constant_pool
2635 + MAYBE_SWAP (index->symbol_table[i + 1]));
2636 do_cleanups (back_to);
2637 return 1;
2638 }
2639
2640 slot = (slot + step) & (index->symbol_table_slots - 1);
2641 }
2642 }
2643
2644 /* A helper function that reads the .gdb_index from SECTION and fills
2645 in MAP. FILENAME is the name of the file containing the section;
2646 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2647 ok to use deprecated sections.
2648
2649 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2650 out parameters that are filled in with information about the CU and
2651 TU lists in the section.
2652
2653 Returns 1 if all went well, 0 otherwise. */
2654
2655 static int
2656 read_index_from_section (struct objfile *objfile,
2657 const char *filename,
2658 int deprecated_ok,
2659 struct dwarf2_section_info *section,
2660 struct mapped_index *map,
2661 const gdb_byte **cu_list,
2662 offset_type *cu_list_elements,
2663 const gdb_byte **types_list,
2664 offset_type *types_list_elements)
2665 {
2666 char *addr;
2667 offset_type version;
2668 offset_type *metadata;
2669 int i;
2670
2671 if (dwarf2_section_empty_p (section))
2672 return 0;
2673
2674 /* Older elfutils strip versions could keep the section in the main
2675 executable while splitting it for the separate debug info file. */
2676 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2677 return 0;
2678
2679 dwarf2_read_section (objfile, section);
2680
2681 addr = section->buffer;
2682 /* Version check. */
2683 version = MAYBE_SWAP (*(offset_type *) addr);
2684 /* Versions earlier than 3 emitted every copy of a psymbol. This
2685 causes the index to behave very poorly for certain requests. Version 3
2686 contained incomplete addrmap. So, it seems better to just ignore such
2687 indices. */
2688 if (version < 4)
2689 {
2690 static int warning_printed = 0;
2691 if (!warning_printed)
2692 {
2693 warning (_("Skipping obsolete .gdb_index section in %s."),
2694 filename);
2695 warning_printed = 1;
2696 }
2697 return 0;
2698 }
2699 /* Index version 4 uses a different hash function than index version
2700 5 and later.
2701
2702 Versions earlier than 6 did not emit psymbols for inlined
2703 functions. Using these files will cause GDB not to be able to
2704 set breakpoints on inlined functions by name, so we ignore these
2705 indices unless the user has done
2706 "set use-deprecated-index-sections on". */
2707 if (version < 6 && !deprecated_ok)
2708 {
2709 static int warning_printed = 0;
2710 if (!warning_printed)
2711 {
2712 warning (_("\
2713 Skipping deprecated .gdb_index section in %s.\n\
2714 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2715 to use the section anyway."),
2716 filename);
2717 warning_printed = 1;
2718 }
2719 return 0;
2720 }
2721 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2722 of the TU (for symbols coming from TUs). It's just a performance bug, and
2723 we can't distinguish gdb-generated indices from gold-generated ones, so
2724 nothing to do here. */
2725
2726 /* Indexes with higher version than the one supported by GDB may be no
2727 longer backward compatible. */
2728 if (version > 8)
2729 return 0;
2730
2731 map->version = version;
2732 map->total_size = section->size;
2733
2734 metadata = (offset_type *) (addr + sizeof (offset_type));
2735
2736 i = 0;
2737 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2738 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2739 / 8);
2740 ++i;
2741
2742 *types_list = addr + MAYBE_SWAP (metadata[i]);
2743 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2744 - MAYBE_SWAP (metadata[i]))
2745 / 8);
2746 ++i;
2747
2748 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2749 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2750 - MAYBE_SWAP (metadata[i]));
2751 ++i;
2752
2753 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2754 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2755 - MAYBE_SWAP (metadata[i]))
2756 / (2 * sizeof (offset_type)));
2757 ++i;
2758
2759 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2760
2761 return 1;
2762 }
2763
2764
2765 /* Read the index file. If everything went ok, initialize the "quick"
2766 elements of all the CUs and return 1. Otherwise, return 0. */
2767
2768 static int
2769 dwarf2_read_index (struct objfile *objfile)
2770 {
2771 struct mapped_index local_map, *map;
2772 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2773 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2774
2775 if (!read_index_from_section (objfile, objfile->name,
2776 use_deprecated_index_sections,
2777 &dwarf2_per_objfile->gdb_index, &local_map,
2778 &cu_list, &cu_list_elements,
2779 &types_list, &types_list_elements))
2780 return 0;
2781
2782 /* Don't use the index if it's empty. */
2783 if (local_map.symbol_table_slots == 0)
2784 return 0;
2785
2786 /* If there is a .dwz file, read it so we can get its CU list as
2787 well. */
2788 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2789 {
2790 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2791 struct mapped_index dwz_map;
2792 const gdb_byte *dwz_types_ignore;
2793 offset_type dwz_types_elements_ignore;
2794
2795 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2796 1,
2797 &dwz->gdb_index, &dwz_map,
2798 &dwz_list, &dwz_list_elements,
2799 &dwz_types_ignore,
2800 &dwz_types_elements_ignore))
2801 {
2802 warning (_("could not read '.gdb_index' section from %s; skipping"),
2803 bfd_get_filename (dwz->dwz_bfd));
2804 return 0;
2805 }
2806 }
2807
2808 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2809 dwz_list_elements);
2810
2811 if (types_list_elements)
2812 {
2813 struct dwarf2_section_info *section;
2814
2815 /* We can only handle a single .debug_types when we have an
2816 index. */
2817 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2818 return 0;
2819
2820 section = VEC_index (dwarf2_section_info_def,
2821 dwarf2_per_objfile->types, 0);
2822
2823 create_signatured_type_table_from_index (objfile, section, types_list,
2824 types_list_elements);
2825 }
2826
2827 create_addrmap_from_index (objfile, &local_map);
2828
2829 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2830 *map = local_map;
2831
2832 dwarf2_per_objfile->index_table = map;
2833 dwarf2_per_objfile->using_index = 1;
2834 dwarf2_per_objfile->quick_file_names_table =
2835 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2836
2837 return 1;
2838 }
2839
2840 /* A helper for the "quick" functions which sets the global
2841 dwarf2_per_objfile according to OBJFILE. */
2842
2843 static void
2844 dw2_setup (struct objfile *objfile)
2845 {
2846 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2847 gdb_assert (dwarf2_per_objfile);
2848 }
2849
2850 /* die_reader_func for dw2_get_file_names. */
2851
2852 static void
2853 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2854 gdb_byte *info_ptr,
2855 struct die_info *comp_unit_die,
2856 int has_children,
2857 void *data)
2858 {
2859 struct dwarf2_cu *cu = reader->cu;
2860 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2861 struct objfile *objfile = dwarf2_per_objfile->objfile;
2862 struct dwarf2_per_cu_data *lh_cu;
2863 struct line_header *lh;
2864 struct attribute *attr;
2865 int i;
2866 const char *name, *comp_dir;
2867 void **slot;
2868 struct quick_file_names *qfn;
2869 unsigned int line_offset;
2870
2871 gdb_assert (! this_cu->is_debug_types);
2872
2873 /* Our callers never want to match partial units -- instead they
2874 will match the enclosing full CU. */
2875 if (comp_unit_die->tag == DW_TAG_partial_unit)
2876 {
2877 this_cu->v.quick->no_file_data = 1;
2878 return;
2879 }
2880
2881 lh_cu = this_cu;
2882 lh = NULL;
2883 slot = NULL;
2884 line_offset = 0;
2885
2886 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2887 if (attr)
2888 {
2889 struct quick_file_names find_entry;
2890
2891 line_offset = DW_UNSND (attr);
2892
2893 /* We may have already read in this line header (TU line header sharing).
2894 If we have we're done. */
2895 find_entry.hash.dwo_unit = cu->dwo_unit;
2896 find_entry.hash.line_offset.sect_off = line_offset;
2897 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2898 &find_entry, INSERT);
2899 if (*slot != NULL)
2900 {
2901 lh_cu->v.quick->file_names = *slot;
2902 return;
2903 }
2904
2905 lh = dwarf_decode_line_header (line_offset, cu);
2906 }
2907 if (lh == NULL)
2908 {
2909 lh_cu->v.quick->no_file_data = 1;
2910 return;
2911 }
2912
2913 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2914 qfn->hash.dwo_unit = cu->dwo_unit;
2915 qfn->hash.line_offset.sect_off = line_offset;
2916 gdb_assert (slot != NULL);
2917 *slot = qfn;
2918
2919 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2920
2921 qfn->num_file_names = lh->num_file_names;
2922 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2923 lh->num_file_names * sizeof (char *));
2924 for (i = 0; i < lh->num_file_names; ++i)
2925 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2926 qfn->real_names = NULL;
2927
2928 free_line_header (lh);
2929
2930 lh_cu->v.quick->file_names = qfn;
2931 }
2932
2933 /* A helper for the "quick" functions which attempts to read the line
2934 table for THIS_CU. */
2935
2936 static struct quick_file_names *
2937 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2938 {
2939 /* This should never be called for TUs. */
2940 gdb_assert (! this_cu->is_debug_types);
2941 /* Nor type unit groups. */
2942 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2943
2944 if (this_cu->v.quick->file_names != NULL)
2945 return this_cu->v.quick->file_names;
2946 /* If we know there is no line data, no point in looking again. */
2947 if (this_cu->v.quick->no_file_data)
2948 return NULL;
2949
2950 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2951
2952 if (this_cu->v.quick->no_file_data)
2953 return NULL;
2954 return this_cu->v.quick->file_names;
2955 }
2956
2957 /* A helper for the "quick" functions which computes and caches the
2958 real path for a given file name from the line table. */
2959
2960 static const char *
2961 dw2_get_real_path (struct objfile *objfile,
2962 struct quick_file_names *qfn, int index)
2963 {
2964 if (qfn->real_names == NULL)
2965 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2966 qfn->num_file_names, sizeof (char *));
2967
2968 if (qfn->real_names[index] == NULL)
2969 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2970
2971 return qfn->real_names[index];
2972 }
2973
2974 static struct symtab *
2975 dw2_find_last_source_symtab (struct objfile *objfile)
2976 {
2977 int index;
2978
2979 dw2_setup (objfile);
2980 index = dwarf2_per_objfile->n_comp_units - 1;
2981 return dw2_instantiate_symtab (dw2_get_cu (index));
2982 }
2983
2984 /* Traversal function for dw2_forget_cached_source_info. */
2985
2986 static int
2987 dw2_free_cached_file_names (void **slot, void *info)
2988 {
2989 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2990
2991 if (file_data->real_names)
2992 {
2993 int i;
2994
2995 for (i = 0; i < file_data->num_file_names; ++i)
2996 {
2997 xfree ((void*) file_data->real_names[i]);
2998 file_data->real_names[i] = NULL;
2999 }
3000 }
3001
3002 return 1;
3003 }
3004
3005 static void
3006 dw2_forget_cached_source_info (struct objfile *objfile)
3007 {
3008 dw2_setup (objfile);
3009
3010 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3011 dw2_free_cached_file_names, NULL);
3012 }
3013
3014 /* Helper function for dw2_map_symtabs_matching_filename that expands
3015 the symtabs and calls the iterator. */
3016
3017 static int
3018 dw2_map_expand_apply (struct objfile *objfile,
3019 struct dwarf2_per_cu_data *per_cu,
3020 const char *name, const char *real_path,
3021 int (*callback) (struct symtab *, void *),
3022 void *data)
3023 {
3024 struct symtab *last_made = objfile->symtabs;
3025
3026 /* Don't visit already-expanded CUs. */
3027 if (per_cu->v.quick->symtab)
3028 return 0;
3029
3030 /* This may expand more than one symtab, and we want to iterate over
3031 all of them. */
3032 dw2_instantiate_symtab (per_cu);
3033
3034 return iterate_over_some_symtabs (name, real_path, callback, data,
3035 objfile->symtabs, last_made);
3036 }
3037
3038 /* Implementation of the map_symtabs_matching_filename method. */
3039
3040 static int
3041 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3042 const char *real_path,
3043 int (*callback) (struct symtab *, void *),
3044 void *data)
3045 {
3046 int i;
3047 const char *name_basename = lbasename (name);
3048
3049 dw2_setup (objfile);
3050
3051 /* The rule is CUs specify all the files, including those used by
3052 any TU, so there's no need to scan TUs here. */
3053
3054 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3055 {
3056 int j;
3057 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3058 struct quick_file_names *file_data;
3059
3060 /* We only need to look at symtabs not already expanded. */
3061 if (per_cu->v.quick->symtab)
3062 continue;
3063
3064 file_data = dw2_get_file_names (per_cu);
3065 if (file_data == NULL)
3066 continue;
3067
3068 for (j = 0; j < file_data->num_file_names; ++j)
3069 {
3070 const char *this_name = file_data->file_names[j];
3071 const char *this_real_name;
3072
3073 if (compare_filenames_for_search (this_name, name))
3074 {
3075 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3076 callback, data))
3077 return 1;
3078 continue;
3079 }
3080
3081 /* Before we invoke realpath, which can get expensive when many
3082 files are involved, do a quick comparison of the basenames. */
3083 if (! basenames_may_differ
3084 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3085 continue;
3086
3087 this_real_name = dw2_get_real_path (objfile, file_data, j);
3088 if (compare_filenames_for_search (this_real_name, name))
3089 {
3090 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3091 callback, data))
3092 return 1;
3093 continue;
3094 }
3095
3096 if (real_path != NULL)
3097 {
3098 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3099 gdb_assert (IS_ABSOLUTE_PATH (name));
3100 if (this_real_name != NULL
3101 && FILENAME_CMP (real_path, this_real_name) == 0)
3102 {
3103 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3104 callback, data))
3105 return 1;
3106 continue;
3107 }
3108 }
3109 }
3110 }
3111
3112 return 0;
3113 }
3114
3115 /* Struct used to manage iterating over all CUs looking for a symbol. */
3116
3117 struct dw2_symtab_iterator
3118 {
3119 /* The internalized form of .gdb_index. */
3120 struct mapped_index *index;
3121 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3122 int want_specific_block;
3123 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3124 Unused if !WANT_SPECIFIC_BLOCK. */
3125 int block_index;
3126 /* The kind of symbol we're looking for. */
3127 domain_enum domain;
3128 /* The list of CUs from the index entry of the symbol,
3129 or NULL if not found. */
3130 offset_type *vec;
3131 /* The next element in VEC to look at. */
3132 int next;
3133 /* The number of elements in VEC, or zero if there is no match. */
3134 int length;
3135 };
3136
3137 /* Initialize the index symtab iterator ITER.
3138 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3139 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3140
3141 static void
3142 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3143 struct mapped_index *index,
3144 int want_specific_block,
3145 int block_index,
3146 domain_enum domain,
3147 const char *name)
3148 {
3149 iter->index = index;
3150 iter->want_specific_block = want_specific_block;
3151 iter->block_index = block_index;
3152 iter->domain = domain;
3153 iter->next = 0;
3154
3155 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3156 iter->length = MAYBE_SWAP (*iter->vec);
3157 else
3158 {
3159 iter->vec = NULL;
3160 iter->length = 0;
3161 }
3162 }
3163
3164 /* Return the next matching CU or NULL if there are no more. */
3165
3166 static struct dwarf2_per_cu_data *
3167 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3168 {
3169 for ( ; iter->next < iter->length; ++iter->next)
3170 {
3171 offset_type cu_index_and_attrs =
3172 MAYBE_SWAP (iter->vec[iter->next + 1]);
3173 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3174 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3175 int want_static = iter->block_index != GLOBAL_BLOCK;
3176 /* This value is only valid for index versions >= 7. */
3177 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3178 gdb_index_symbol_kind symbol_kind =
3179 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3180 /* Only check the symbol attributes if they're present.
3181 Indices prior to version 7 don't record them,
3182 and indices >= 7 may elide them for certain symbols
3183 (gold does this). */
3184 int attrs_valid =
3185 (iter->index->version >= 7
3186 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3187
3188 /* Skip if already read in. */
3189 if (per_cu->v.quick->symtab)
3190 continue;
3191
3192 if (attrs_valid
3193 && iter->want_specific_block
3194 && want_static != is_static)
3195 continue;
3196
3197 /* Only check the symbol's kind if it has one. */
3198 if (attrs_valid)
3199 {
3200 switch (iter->domain)
3201 {
3202 case VAR_DOMAIN:
3203 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3204 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3205 /* Some types are also in VAR_DOMAIN. */
3206 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3207 continue;
3208 break;
3209 case STRUCT_DOMAIN:
3210 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3211 continue;
3212 break;
3213 case LABEL_DOMAIN:
3214 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3215 continue;
3216 break;
3217 default:
3218 break;
3219 }
3220 }
3221
3222 ++iter->next;
3223 return per_cu;
3224 }
3225
3226 return NULL;
3227 }
3228
3229 static struct symtab *
3230 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3231 const char *name, domain_enum domain)
3232 {
3233 struct symtab *stab_best = NULL;
3234 struct mapped_index *index;
3235
3236 dw2_setup (objfile);
3237
3238 index = dwarf2_per_objfile->index_table;
3239
3240 /* index is NULL if OBJF_READNOW. */
3241 if (index)
3242 {
3243 struct dw2_symtab_iterator iter;
3244 struct dwarf2_per_cu_data *per_cu;
3245
3246 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3247
3248 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3249 {
3250 struct symbol *sym = NULL;
3251 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3252
3253 /* Some caution must be observed with overloaded functions
3254 and methods, since the index will not contain any overload
3255 information (but NAME might contain it). */
3256 if (stab->primary)
3257 {
3258 struct blockvector *bv = BLOCKVECTOR (stab);
3259 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3260
3261 sym = lookup_block_symbol (block, name, domain);
3262 }
3263
3264 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3265 {
3266 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3267 return stab;
3268
3269 stab_best = stab;
3270 }
3271
3272 /* Keep looking through other CUs. */
3273 }
3274 }
3275
3276 return stab_best;
3277 }
3278
3279 static void
3280 dw2_print_stats (struct objfile *objfile)
3281 {
3282 int i, total, count;
3283
3284 dw2_setup (objfile);
3285 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3286 count = 0;
3287 for (i = 0; i < total; ++i)
3288 {
3289 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3290
3291 if (!per_cu->v.quick->symtab)
3292 ++count;
3293 }
3294 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3295 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3296 }
3297
3298 static void
3299 dw2_dump (struct objfile *objfile)
3300 {
3301 /* Nothing worth printing. */
3302 }
3303
3304 static void
3305 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3306 struct section_offsets *delta)
3307 {
3308 /* There's nothing to relocate here. */
3309 }
3310
3311 static void
3312 dw2_expand_symtabs_for_function (struct objfile *objfile,
3313 const char *func_name)
3314 {
3315 struct mapped_index *index;
3316
3317 dw2_setup (objfile);
3318
3319 index = dwarf2_per_objfile->index_table;
3320
3321 /* index is NULL if OBJF_READNOW. */
3322 if (index)
3323 {
3324 struct dw2_symtab_iterator iter;
3325 struct dwarf2_per_cu_data *per_cu;
3326
3327 /* Note: It doesn't matter what we pass for block_index here. */
3328 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3329 func_name);
3330
3331 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3332 dw2_instantiate_symtab (per_cu);
3333 }
3334 }
3335
3336 static void
3337 dw2_expand_all_symtabs (struct objfile *objfile)
3338 {
3339 int i;
3340
3341 dw2_setup (objfile);
3342
3343 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3344 + dwarf2_per_objfile->n_type_units); ++i)
3345 {
3346 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3347
3348 dw2_instantiate_symtab (per_cu);
3349 }
3350 }
3351
3352 static void
3353 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3354 const char *fullname)
3355 {
3356 int i;
3357
3358 dw2_setup (objfile);
3359
3360 /* We don't need to consider type units here.
3361 This is only called for examining code, e.g. expand_line_sal.
3362 There can be an order of magnitude (or more) more type units
3363 than comp units, and we avoid them if we can. */
3364
3365 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3366 {
3367 int j;
3368 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3369 struct quick_file_names *file_data;
3370
3371 /* We only need to look at symtabs not already expanded. */
3372 if (per_cu->v.quick->symtab)
3373 continue;
3374
3375 file_data = dw2_get_file_names (per_cu);
3376 if (file_data == NULL)
3377 continue;
3378
3379 for (j = 0; j < file_data->num_file_names; ++j)
3380 {
3381 const char *this_fullname = file_data->file_names[j];
3382
3383 if (filename_cmp (this_fullname, fullname) == 0)
3384 {
3385 dw2_instantiate_symtab (per_cu);
3386 break;
3387 }
3388 }
3389 }
3390 }
3391
3392 /* A helper function for dw2_find_symbol_file that finds the primary
3393 file name for a given CU. This is a die_reader_func. */
3394
3395 static void
3396 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3397 gdb_byte *info_ptr,
3398 struct die_info *comp_unit_die,
3399 int has_children,
3400 void *data)
3401 {
3402 const char **result_ptr = data;
3403 struct dwarf2_cu *cu = reader->cu;
3404 struct attribute *attr;
3405
3406 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3407 if (attr == NULL)
3408 *result_ptr = NULL;
3409 else
3410 *result_ptr = DW_STRING (attr);
3411 }
3412
3413 static const char *
3414 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3415 {
3416 struct dwarf2_per_cu_data *per_cu;
3417 offset_type *vec;
3418 const char *filename;
3419
3420 dw2_setup (objfile);
3421
3422 /* index_table is NULL if OBJF_READNOW. */
3423 if (!dwarf2_per_objfile->index_table)
3424 {
3425 struct symtab *s;
3426
3427 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3428 {
3429 struct blockvector *bv = BLOCKVECTOR (s);
3430 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3431 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3432
3433 if (sym)
3434 {
3435 /* Only file extension of returned filename is recognized. */
3436 return SYMBOL_SYMTAB (sym)->filename;
3437 }
3438 }
3439 return NULL;
3440 }
3441
3442 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3443 name, &vec))
3444 return NULL;
3445
3446 /* Note that this just looks at the very first one named NAME -- but
3447 actually we are looking for a function. find_main_filename
3448 should be rewritten so that it doesn't require a custom hook. It
3449 could just use the ordinary symbol tables. */
3450 /* vec[0] is the length, which must always be >0. */
3451 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3452
3453 if (per_cu->v.quick->symtab != NULL)
3454 {
3455 /* Only file extension of returned filename is recognized. */
3456 return per_cu->v.quick->symtab->filename;
3457 }
3458
3459 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3460 dw2_get_primary_filename_reader, &filename);
3461
3462 /* Only file extension of returned filename is recognized. */
3463 return filename;
3464 }
3465
3466 static void
3467 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3468 struct objfile *objfile, int global,
3469 int (*callback) (struct block *,
3470 struct symbol *, void *),
3471 void *data, symbol_compare_ftype *match,
3472 symbol_compare_ftype *ordered_compare)
3473 {
3474 /* Currently unimplemented; used for Ada. The function can be called if the
3475 current language is Ada for a non-Ada objfile using GNU index. As Ada
3476 does not look for non-Ada symbols this function should just return. */
3477 }
3478
3479 static void
3480 dw2_expand_symtabs_matching
3481 (struct objfile *objfile,
3482 int (*file_matcher) (const char *, void *, int basenames),
3483 int (*name_matcher) (const char *, void *),
3484 enum search_domain kind,
3485 void *data)
3486 {
3487 int i;
3488 offset_type iter;
3489 struct mapped_index *index;
3490
3491 dw2_setup (objfile);
3492
3493 /* index_table is NULL if OBJF_READNOW. */
3494 if (!dwarf2_per_objfile->index_table)
3495 return;
3496 index = dwarf2_per_objfile->index_table;
3497
3498 if (file_matcher != NULL)
3499 {
3500 struct cleanup *cleanup;
3501 htab_t visited_found, visited_not_found;
3502
3503 visited_found = htab_create_alloc (10,
3504 htab_hash_pointer, htab_eq_pointer,
3505 NULL, xcalloc, xfree);
3506 cleanup = make_cleanup_htab_delete (visited_found);
3507 visited_not_found = htab_create_alloc (10,
3508 htab_hash_pointer, htab_eq_pointer,
3509 NULL, xcalloc, xfree);
3510 make_cleanup_htab_delete (visited_not_found);
3511
3512 /* The rule is CUs specify all the files, including those used by
3513 any TU, so there's no need to scan TUs here. */
3514
3515 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3516 {
3517 int j;
3518 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3519 struct quick_file_names *file_data;
3520 void **slot;
3521
3522 per_cu->v.quick->mark = 0;
3523
3524 /* We only need to look at symtabs not already expanded. */
3525 if (per_cu->v.quick->symtab)
3526 continue;
3527
3528 file_data = dw2_get_file_names (per_cu);
3529 if (file_data == NULL)
3530 continue;
3531
3532 if (htab_find (visited_not_found, file_data) != NULL)
3533 continue;
3534 else if (htab_find (visited_found, file_data) != NULL)
3535 {
3536 per_cu->v.quick->mark = 1;
3537 continue;
3538 }
3539
3540 for (j = 0; j < file_data->num_file_names; ++j)
3541 {
3542 const char *this_real_name;
3543
3544 if (file_matcher (file_data->file_names[j], data, 0))
3545 {
3546 per_cu->v.quick->mark = 1;
3547 break;
3548 }
3549
3550 /* Before we invoke realpath, which can get expensive when many
3551 files are involved, do a quick comparison of the basenames. */
3552 if (!basenames_may_differ
3553 && !file_matcher (lbasename (file_data->file_names[j]),
3554 data, 1))
3555 continue;
3556
3557 this_real_name = dw2_get_real_path (objfile, file_data, j);
3558 if (file_matcher (this_real_name, data, 0))
3559 {
3560 per_cu->v.quick->mark = 1;
3561 break;
3562 }
3563 }
3564
3565 slot = htab_find_slot (per_cu->v.quick->mark
3566 ? visited_found
3567 : visited_not_found,
3568 file_data, INSERT);
3569 *slot = file_data;
3570 }
3571
3572 do_cleanups (cleanup);
3573 }
3574
3575 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3576 {
3577 offset_type idx = 2 * iter;
3578 const char *name;
3579 offset_type *vec, vec_len, vec_idx;
3580
3581 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3582 continue;
3583
3584 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3585
3586 if (! (*name_matcher) (name, data))
3587 continue;
3588
3589 /* The name was matched, now expand corresponding CUs that were
3590 marked. */
3591 vec = (offset_type *) (index->constant_pool
3592 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3593 vec_len = MAYBE_SWAP (vec[0]);
3594 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3595 {
3596 struct dwarf2_per_cu_data *per_cu;
3597 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3598 gdb_index_symbol_kind symbol_kind =
3599 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3600 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3601
3602 /* Don't crash on bad data. */
3603 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3604 + dwarf2_per_objfile->n_type_units))
3605 continue;
3606
3607 /* Only check the symbol's kind if it has one.
3608 Indices prior to version 7 don't record it. */
3609 if (index->version >= 7)
3610 {
3611 switch (kind)
3612 {
3613 case VARIABLES_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3615 continue;
3616 break;
3617 case FUNCTIONS_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3619 continue;
3620 break;
3621 case TYPES_DOMAIN:
3622 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3623 continue;
3624 break;
3625 default:
3626 break;
3627 }
3628 }
3629
3630 per_cu = dw2_get_cu (cu_index);
3631 if (file_matcher == NULL || per_cu->v.quick->mark)
3632 dw2_instantiate_symtab (per_cu);
3633 }
3634 }
3635 }
3636
3637 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3638 symtab. */
3639
3640 static struct symtab *
3641 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3642 {
3643 int i;
3644
3645 if (BLOCKVECTOR (symtab) != NULL
3646 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3647 return symtab;
3648
3649 if (symtab->includes == NULL)
3650 return NULL;
3651
3652 for (i = 0; symtab->includes[i]; ++i)
3653 {
3654 struct symtab *s = symtab->includes[i];
3655
3656 s = recursively_find_pc_sect_symtab (s, pc);
3657 if (s != NULL)
3658 return s;
3659 }
3660
3661 return NULL;
3662 }
3663
3664 static struct symtab *
3665 dw2_find_pc_sect_symtab (struct objfile *objfile,
3666 struct minimal_symbol *msymbol,
3667 CORE_ADDR pc,
3668 struct obj_section *section,
3669 int warn_if_readin)
3670 {
3671 struct dwarf2_per_cu_data *data;
3672 struct symtab *result;
3673
3674 dw2_setup (objfile);
3675
3676 if (!objfile->psymtabs_addrmap)
3677 return NULL;
3678
3679 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3680 if (!data)
3681 return NULL;
3682
3683 if (warn_if_readin && data->v.quick->symtab)
3684 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3685 paddress (get_objfile_arch (objfile), pc));
3686
3687 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3688 gdb_assert (result != NULL);
3689 return result;
3690 }
3691
3692 static void
3693 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3694 void *data, int need_fullname)
3695 {
3696 int i;
3697 struct cleanup *cleanup;
3698 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3699 NULL, xcalloc, xfree);
3700
3701 cleanup = make_cleanup_htab_delete (visited);
3702 dw2_setup (objfile);
3703
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here.
3706 We can ignore file names coming from already-expanded CUs. */
3707
3708 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3709 {
3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711
3712 if (per_cu->v.quick->symtab)
3713 {
3714 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3715 INSERT);
3716
3717 *slot = per_cu->v.quick->file_names;
3718 }
3719 }
3720
3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3722 {
3723 int j;
3724 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3725 struct quick_file_names *file_data;
3726 void **slot;
3727
3728 /* We only need to look at symtabs not already expanded. */
3729 if (per_cu->v.quick->symtab)
3730 continue;
3731
3732 file_data = dw2_get_file_names (per_cu);
3733 if (file_data == NULL)
3734 continue;
3735
3736 slot = htab_find_slot (visited, file_data, INSERT);
3737 if (*slot)
3738 {
3739 /* Already visited. */
3740 continue;
3741 }
3742 *slot = file_data;
3743
3744 for (j = 0; j < file_data->num_file_names; ++j)
3745 {
3746 const char *this_real_name;
3747
3748 if (need_fullname)
3749 this_real_name = dw2_get_real_path (objfile, file_data, j);
3750 else
3751 this_real_name = NULL;
3752 (*fun) (file_data->file_names[j], this_real_name, data);
3753 }
3754 }
3755
3756 do_cleanups (cleanup);
3757 }
3758
3759 static int
3760 dw2_has_symbols (struct objfile *objfile)
3761 {
3762 return 1;
3763 }
3764
3765 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3766 {
3767 dw2_has_symbols,
3768 dw2_find_last_source_symtab,
3769 dw2_forget_cached_source_info,
3770 dw2_map_symtabs_matching_filename,
3771 dw2_lookup_symbol,
3772 dw2_print_stats,
3773 dw2_dump,
3774 dw2_relocate,
3775 dw2_expand_symtabs_for_function,
3776 dw2_expand_all_symtabs,
3777 dw2_expand_symtabs_with_fullname,
3778 dw2_find_symbol_file,
3779 dw2_map_matching_symbols,
3780 dw2_expand_symtabs_matching,
3781 dw2_find_pc_sect_symtab,
3782 dw2_map_symbol_filenames
3783 };
3784
3785 /* Initialize for reading DWARF for this objfile. Return 0 if this
3786 file will use psymtabs, or 1 if using the GNU index. */
3787
3788 int
3789 dwarf2_initialize_objfile (struct objfile *objfile)
3790 {
3791 /* If we're about to read full symbols, don't bother with the
3792 indices. In this case we also don't care if some other debug
3793 format is making psymtabs, because they are all about to be
3794 expanded anyway. */
3795 if ((objfile->flags & OBJF_READNOW))
3796 {
3797 int i;
3798
3799 dwarf2_per_objfile->using_index = 1;
3800 create_all_comp_units (objfile);
3801 create_all_type_units (objfile);
3802 dwarf2_per_objfile->quick_file_names_table =
3803 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3804
3805 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3806 + dwarf2_per_objfile->n_type_units); ++i)
3807 {
3808 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3809
3810 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3811 struct dwarf2_per_cu_quick_data);
3812 }
3813
3814 /* Return 1 so that gdb sees the "quick" functions. However,
3815 these functions will be no-ops because we will have expanded
3816 all symtabs. */
3817 return 1;
3818 }
3819
3820 if (dwarf2_read_index (objfile))
3821 return 1;
3822
3823 return 0;
3824 }
3825
3826 \f
3827
3828 /* Build a partial symbol table. */
3829
3830 void
3831 dwarf2_build_psymtabs (struct objfile *objfile)
3832 {
3833 volatile struct gdb_exception except;
3834
3835 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3836 {
3837 init_psymbol_list (objfile, 1024);
3838 }
3839
3840 TRY_CATCH (except, RETURN_MASK_ERROR)
3841 {
3842 /* This isn't really ideal: all the data we allocate on the
3843 objfile's obstack is still uselessly kept around. However,
3844 freeing it seems unsafe. */
3845 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3846
3847 dwarf2_build_psymtabs_hard (objfile);
3848 discard_cleanups (cleanups);
3849 }
3850 if (except.reason < 0)
3851 exception_print (gdb_stderr, except);
3852 }
3853
3854 /* Return the total length of the CU described by HEADER. */
3855
3856 static unsigned int
3857 get_cu_length (const struct comp_unit_head *header)
3858 {
3859 return header->initial_length_size + header->length;
3860 }
3861
3862 /* Return TRUE if OFFSET is within CU_HEADER. */
3863
3864 static inline int
3865 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3866 {
3867 sect_offset bottom = { cu_header->offset.sect_off };
3868 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3869
3870 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3871 }
3872
3873 /* Find the base address of the compilation unit for range lists and
3874 location lists. It will normally be specified by DW_AT_low_pc.
3875 In DWARF-3 draft 4, the base address could be overridden by
3876 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3877 compilation units with discontinuous ranges. */
3878
3879 static void
3880 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3881 {
3882 struct attribute *attr;
3883
3884 cu->base_known = 0;
3885 cu->base_address = 0;
3886
3887 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3888 if (attr)
3889 {
3890 cu->base_address = DW_ADDR (attr);
3891 cu->base_known = 1;
3892 }
3893 else
3894 {
3895 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3896 if (attr)
3897 {
3898 cu->base_address = DW_ADDR (attr);
3899 cu->base_known = 1;
3900 }
3901 }
3902 }
3903
3904 /* Read in the comp unit header information from the debug_info at info_ptr.
3905 NOTE: This leaves members offset, first_die_offset to be filled in
3906 by the caller. */
3907
3908 static gdb_byte *
3909 read_comp_unit_head (struct comp_unit_head *cu_header,
3910 gdb_byte *info_ptr, bfd *abfd)
3911 {
3912 int signed_addr;
3913 unsigned int bytes_read;
3914
3915 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3916 cu_header->initial_length_size = bytes_read;
3917 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3918 info_ptr += bytes_read;
3919 cu_header->version = read_2_bytes (abfd, info_ptr);
3920 info_ptr += 2;
3921 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3922 &bytes_read);
3923 info_ptr += bytes_read;
3924 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3925 info_ptr += 1;
3926 signed_addr = bfd_get_sign_extend_vma (abfd);
3927 if (signed_addr < 0)
3928 internal_error (__FILE__, __LINE__,
3929 _("read_comp_unit_head: dwarf from non elf file"));
3930 cu_header->signed_addr_p = signed_addr;
3931
3932 return info_ptr;
3933 }
3934
3935 /* Helper function that returns the proper abbrev section for
3936 THIS_CU. */
3937
3938 static struct dwarf2_section_info *
3939 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3940 {
3941 struct dwarf2_section_info *abbrev;
3942
3943 if (this_cu->is_dwz)
3944 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3945 else
3946 abbrev = &dwarf2_per_objfile->abbrev;
3947
3948 return abbrev;
3949 }
3950
3951 /* Subroutine of read_and_check_comp_unit_head and
3952 read_and_check_type_unit_head to simplify them.
3953 Perform various error checking on the header. */
3954
3955 static void
3956 error_check_comp_unit_head (struct comp_unit_head *header,
3957 struct dwarf2_section_info *section,
3958 struct dwarf2_section_info *abbrev_section)
3959 {
3960 bfd *abfd = section->asection->owner;
3961 const char *filename = bfd_get_filename (abfd);
3962
3963 if (header->version != 2 && header->version != 3 && header->version != 4)
3964 error (_("Dwarf Error: wrong version in compilation unit header "
3965 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3966 filename);
3967
3968 if (header->abbrev_offset.sect_off
3969 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3970 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3971 "(offset 0x%lx + 6) [in module %s]"),
3972 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3973 filename);
3974
3975 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3976 avoid potential 32-bit overflow. */
3977 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3978 > section->size)
3979 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3980 "(offset 0x%lx + 0) [in module %s]"),
3981 (long) header->length, (long) header->offset.sect_off,
3982 filename);
3983 }
3984
3985 /* Read in a CU/TU header and perform some basic error checking.
3986 The contents of the header are stored in HEADER.
3987 The result is a pointer to the start of the first DIE. */
3988
3989 static gdb_byte *
3990 read_and_check_comp_unit_head (struct comp_unit_head *header,
3991 struct dwarf2_section_info *section,
3992 struct dwarf2_section_info *abbrev_section,
3993 gdb_byte *info_ptr,
3994 int is_debug_types_section)
3995 {
3996 gdb_byte *beg_of_comp_unit = info_ptr;
3997 bfd *abfd = section->asection->owner;
3998
3999 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4000
4001 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4002
4003 /* If we're reading a type unit, skip over the signature and
4004 type_offset fields. */
4005 if (is_debug_types_section)
4006 info_ptr += 8 /*signature*/ + header->offset_size;
4007
4008 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4009
4010 error_check_comp_unit_head (header, section, abbrev_section);
4011
4012 return info_ptr;
4013 }
4014
4015 /* Read in the types comp unit header information from .debug_types entry at
4016 types_ptr. The result is a pointer to one past the end of the header. */
4017
4018 static gdb_byte *
4019 read_and_check_type_unit_head (struct comp_unit_head *header,
4020 struct dwarf2_section_info *section,
4021 struct dwarf2_section_info *abbrev_section,
4022 gdb_byte *info_ptr,
4023 ULONGEST *signature,
4024 cu_offset *type_offset_in_tu)
4025 {
4026 gdb_byte *beg_of_comp_unit = info_ptr;
4027 bfd *abfd = section->asection->owner;
4028
4029 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4030
4031 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4032
4033 /* If we're reading a type unit, skip over the signature and
4034 type_offset fields. */
4035 if (signature != NULL)
4036 *signature = read_8_bytes (abfd, info_ptr);
4037 info_ptr += 8;
4038 if (type_offset_in_tu != NULL)
4039 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4040 header->offset_size);
4041 info_ptr += header->offset_size;
4042
4043 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4044
4045 error_check_comp_unit_head (header, section, abbrev_section);
4046
4047 return info_ptr;
4048 }
4049
4050 /* Fetch the abbreviation table offset from a comp or type unit header. */
4051
4052 static sect_offset
4053 read_abbrev_offset (struct dwarf2_section_info *section,
4054 sect_offset offset)
4055 {
4056 bfd *abfd = section->asection->owner;
4057 gdb_byte *info_ptr;
4058 unsigned int length, initial_length_size, offset_size;
4059 sect_offset abbrev_offset;
4060
4061 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4062 info_ptr = section->buffer + offset.sect_off;
4063 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4064 offset_size = initial_length_size == 4 ? 4 : 8;
4065 info_ptr += initial_length_size + 2 /*version*/;
4066 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4067 return abbrev_offset;
4068 }
4069
4070 /* Allocate a new partial symtab for file named NAME and mark this new
4071 partial symtab as being an include of PST. */
4072
4073 static void
4074 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4075 struct objfile *objfile)
4076 {
4077 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4078
4079 if (!IS_ABSOLUTE_PATH (subpst->filename))
4080 {
4081 /* It shares objfile->objfile_obstack. */
4082 subpst->dirname = pst->dirname;
4083 }
4084
4085 subpst->section_offsets = pst->section_offsets;
4086 subpst->textlow = 0;
4087 subpst->texthigh = 0;
4088
4089 subpst->dependencies = (struct partial_symtab **)
4090 obstack_alloc (&objfile->objfile_obstack,
4091 sizeof (struct partial_symtab *));
4092 subpst->dependencies[0] = pst;
4093 subpst->number_of_dependencies = 1;
4094
4095 subpst->globals_offset = 0;
4096 subpst->n_global_syms = 0;
4097 subpst->statics_offset = 0;
4098 subpst->n_static_syms = 0;
4099 subpst->symtab = NULL;
4100 subpst->read_symtab = pst->read_symtab;
4101 subpst->readin = 0;
4102
4103 /* No private part is necessary for include psymtabs. This property
4104 can be used to differentiate between such include psymtabs and
4105 the regular ones. */
4106 subpst->read_symtab_private = NULL;
4107 }
4108
4109 /* Read the Line Number Program data and extract the list of files
4110 included by the source file represented by PST. Build an include
4111 partial symtab for each of these included files. */
4112
4113 static void
4114 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4115 struct die_info *die,
4116 struct partial_symtab *pst)
4117 {
4118 struct line_header *lh = NULL;
4119 struct attribute *attr;
4120
4121 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4122 if (attr)
4123 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4124 if (lh == NULL)
4125 return; /* No linetable, so no includes. */
4126
4127 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4128 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4129
4130 free_line_header (lh);
4131 }
4132
4133 static hashval_t
4134 hash_signatured_type (const void *item)
4135 {
4136 const struct signatured_type *sig_type = item;
4137
4138 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4139 return sig_type->signature;
4140 }
4141
4142 static int
4143 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4144 {
4145 const struct signatured_type *lhs = item_lhs;
4146 const struct signatured_type *rhs = item_rhs;
4147
4148 return lhs->signature == rhs->signature;
4149 }
4150
4151 /* Allocate a hash table for signatured types. */
4152
4153 static htab_t
4154 allocate_signatured_type_table (struct objfile *objfile)
4155 {
4156 return htab_create_alloc_ex (41,
4157 hash_signatured_type,
4158 eq_signatured_type,
4159 NULL,
4160 &objfile->objfile_obstack,
4161 hashtab_obstack_allocate,
4162 dummy_obstack_deallocate);
4163 }
4164
4165 /* A helper function to add a signatured type CU to a table. */
4166
4167 static int
4168 add_signatured_type_cu_to_table (void **slot, void *datum)
4169 {
4170 struct signatured_type *sigt = *slot;
4171 struct signatured_type ***datap = datum;
4172
4173 **datap = sigt;
4174 ++*datap;
4175
4176 return 1;
4177 }
4178
4179 /* Create the hash table of all entries in the .debug_types
4180 (or .debug_types.dwo) section(s).
4181 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4182 otherwise it is NULL.
4183
4184 The result is a pointer to the hash table or NULL if there are no types.
4185
4186 Note: This function processes DWO files only, not DWP files. */
4187
4188 static htab_t
4189 create_debug_types_hash_table (struct dwo_file *dwo_file,
4190 VEC (dwarf2_section_info_def) *types)
4191 {
4192 struct objfile *objfile = dwarf2_per_objfile->objfile;
4193 htab_t types_htab = NULL;
4194 int ix;
4195 struct dwarf2_section_info *section;
4196 struct dwarf2_section_info *abbrev_section;
4197
4198 if (VEC_empty (dwarf2_section_info_def, types))
4199 return NULL;
4200
4201 abbrev_section = (dwo_file != NULL
4202 ? &dwo_file->sections.abbrev
4203 : &dwarf2_per_objfile->abbrev);
4204
4205 if (dwarf2_read_debug)
4206 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4207 dwo_file ? ".dwo" : "",
4208 bfd_get_filename (abbrev_section->asection->owner));
4209
4210 for (ix = 0;
4211 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4212 ++ix)
4213 {
4214 bfd *abfd;
4215 gdb_byte *info_ptr, *end_ptr;
4216 struct dwarf2_section_info *abbrev_section;
4217
4218 dwarf2_read_section (objfile, section);
4219 info_ptr = section->buffer;
4220
4221 if (info_ptr == NULL)
4222 continue;
4223
4224 /* We can't set abfd until now because the section may be empty or
4225 not present, in which case section->asection will be NULL. */
4226 abfd = section->asection->owner;
4227
4228 if (dwo_file)
4229 abbrev_section = &dwo_file->sections.abbrev;
4230 else
4231 abbrev_section = &dwarf2_per_objfile->abbrev;
4232
4233 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4234 because we don't need to read any dies: the signature is in the
4235 header. */
4236
4237 end_ptr = info_ptr + section->size;
4238 while (info_ptr < end_ptr)
4239 {
4240 sect_offset offset;
4241 cu_offset type_offset_in_tu;
4242 ULONGEST signature;
4243 struct signatured_type *sig_type;
4244 struct dwo_unit *dwo_tu;
4245 void **slot;
4246 gdb_byte *ptr = info_ptr;
4247 struct comp_unit_head header;
4248 unsigned int length;
4249
4250 offset.sect_off = ptr - section->buffer;
4251
4252 /* We need to read the type's signature in order to build the hash
4253 table, but we don't need anything else just yet. */
4254
4255 ptr = read_and_check_type_unit_head (&header, section,
4256 abbrev_section, ptr,
4257 &signature, &type_offset_in_tu);
4258
4259 length = get_cu_length (&header);
4260
4261 /* Skip dummy type units. */
4262 if (ptr >= info_ptr + length
4263 || peek_abbrev_code (abfd, ptr) == 0)
4264 {
4265 info_ptr += length;
4266 continue;
4267 }
4268
4269 if (types_htab == NULL)
4270 {
4271 if (dwo_file)
4272 types_htab = allocate_dwo_unit_table (objfile);
4273 else
4274 types_htab = allocate_signatured_type_table (objfile);
4275 }
4276
4277 if (dwo_file)
4278 {
4279 sig_type = NULL;
4280 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4281 struct dwo_unit);
4282 dwo_tu->dwo_file = dwo_file;
4283 dwo_tu->signature = signature;
4284 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4285 dwo_tu->section = section;
4286 dwo_tu->offset = offset;
4287 dwo_tu->length = length;
4288 }
4289 else
4290 {
4291 /* N.B.: type_offset is not usable if this type uses a DWO file.
4292 The real type_offset is in the DWO file. */
4293 dwo_tu = NULL;
4294 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4295 struct signatured_type);
4296 sig_type->signature = signature;
4297 sig_type->type_offset_in_tu = type_offset_in_tu;
4298 sig_type->per_cu.objfile = objfile;
4299 sig_type->per_cu.is_debug_types = 1;
4300 sig_type->per_cu.section = section;
4301 sig_type->per_cu.offset = offset;
4302 sig_type->per_cu.length = length;
4303 }
4304
4305 slot = htab_find_slot (types_htab,
4306 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4307 INSERT);
4308 gdb_assert (slot != NULL);
4309 if (*slot != NULL)
4310 {
4311 sect_offset dup_offset;
4312
4313 if (dwo_file)
4314 {
4315 const struct dwo_unit *dup_tu = *slot;
4316
4317 dup_offset = dup_tu->offset;
4318 }
4319 else
4320 {
4321 const struct signatured_type *dup_tu = *slot;
4322
4323 dup_offset = dup_tu->per_cu.offset;
4324 }
4325
4326 complaint (&symfile_complaints,
4327 _("debug type entry at offset 0x%x is duplicate to"
4328 " the entry at offset 0x%x, signature 0x%s"),
4329 offset.sect_off, dup_offset.sect_off,
4330 phex (signature, sizeof (signature)));
4331 }
4332 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4333
4334 if (dwarf2_read_debug)
4335 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4336 offset.sect_off,
4337 phex (signature, sizeof (signature)));
4338
4339 info_ptr += length;
4340 }
4341 }
4342
4343 return types_htab;
4344 }
4345
4346 /* Create the hash table of all entries in the .debug_types section,
4347 and initialize all_type_units.
4348 The result is zero if there is an error (e.g. missing .debug_types section),
4349 otherwise non-zero. */
4350
4351 static int
4352 create_all_type_units (struct objfile *objfile)
4353 {
4354 htab_t types_htab;
4355 struct signatured_type **iter;
4356
4357 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4358 if (types_htab == NULL)
4359 {
4360 dwarf2_per_objfile->signatured_types = NULL;
4361 return 0;
4362 }
4363
4364 dwarf2_per_objfile->signatured_types = types_htab;
4365
4366 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4367 dwarf2_per_objfile->all_type_units
4368 = obstack_alloc (&objfile->objfile_obstack,
4369 dwarf2_per_objfile->n_type_units
4370 * sizeof (struct signatured_type *));
4371 iter = &dwarf2_per_objfile->all_type_units[0];
4372 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4373 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4374 == dwarf2_per_objfile->n_type_units);
4375
4376 return 1;
4377 }
4378
4379 /* Lookup a signature based type for DW_FORM_ref_sig8.
4380 Returns NULL if signature SIG is not present in the table.
4381 It is up to the caller to complain about this. */
4382
4383 static struct signatured_type *
4384 lookup_signatured_type (ULONGEST sig)
4385 {
4386 struct signatured_type find_entry, *entry;
4387
4388 if (dwarf2_per_objfile->signatured_types == NULL)
4389 return NULL;
4390 find_entry.signature = sig;
4391 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4392 return entry;
4393 }
4394 \f
4395 /* Low level DIE reading support. */
4396
4397 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4398
4399 static void
4400 init_cu_die_reader (struct die_reader_specs *reader,
4401 struct dwarf2_cu *cu,
4402 struct dwarf2_section_info *section,
4403 struct dwo_file *dwo_file)
4404 {
4405 gdb_assert (section->readin && section->buffer != NULL);
4406 reader->abfd = section->asection->owner;
4407 reader->cu = cu;
4408 reader->dwo_file = dwo_file;
4409 reader->die_section = section;
4410 reader->buffer = section->buffer;
4411 reader->buffer_end = section->buffer + section->size;
4412 }
4413
4414 /* Subroutine of init_cutu_and_read_dies to simplify it.
4415 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4416 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4417 already.
4418
4419 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4420 from it to the DIE in the DWO. If NULL we are skipping the stub.
4421 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4422 are filled in with the info of the DIE from the DWO file.
4423 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4424 provided an abbrev table to use.
4425 The result is non-zero if a valid (non-dummy) DIE was found. */
4426
4427 static int
4428 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4429 struct dwo_unit *dwo_unit,
4430 int abbrev_table_provided,
4431 struct die_info *stub_comp_unit_die,
4432 struct die_reader_specs *result_reader,
4433 gdb_byte **result_info_ptr,
4434 struct die_info **result_comp_unit_die,
4435 int *result_has_children)
4436 {
4437 struct objfile *objfile = dwarf2_per_objfile->objfile;
4438 struct dwarf2_cu *cu = this_cu->cu;
4439 struct dwarf2_section_info *section;
4440 bfd *abfd;
4441 gdb_byte *begin_info_ptr, *info_ptr;
4442 const char *comp_dir_string;
4443 ULONGEST signature; /* Or dwo_id. */
4444 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4445 int i,num_extra_attrs;
4446 struct dwarf2_section_info *dwo_abbrev_section;
4447 struct attribute *attr;
4448 struct die_info *comp_unit_die;
4449
4450 /* These attributes aren't processed until later:
4451 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4452 However, the attribute is found in the stub which we won't have later.
4453 In order to not impose this complication on the rest of the code,
4454 we read them here and copy them to the DWO CU/TU die. */
4455
4456 stmt_list = NULL;
4457 low_pc = NULL;
4458 high_pc = NULL;
4459 ranges = NULL;
4460 comp_dir = NULL;
4461
4462 if (stub_comp_unit_die != NULL)
4463 {
4464 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4465 DWO file. */
4466 if (! this_cu->is_debug_types)
4467 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4468 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4469 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4470 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4471 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4472
4473 /* There should be a DW_AT_addr_base attribute here (if needed).
4474 We need the value before we can process DW_FORM_GNU_addr_index. */
4475 cu->addr_base = 0;
4476 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4477 if (attr)
4478 cu->addr_base = DW_UNSND (attr);
4479
4480 /* There should be a DW_AT_ranges_base attribute here (if needed).
4481 We need the value before we can process DW_AT_ranges. */
4482 cu->ranges_base = 0;
4483 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4484 if (attr)
4485 cu->ranges_base = DW_UNSND (attr);
4486 }
4487
4488 /* Set up for reading the DWO CU/TU. */
4489 cu->dwo_unit = dwo_unit;
4490 section = dwo_unit->section;
4491 dwarf2_read_section (objfile, section);
4492 abfd = section->asection->owner;
4493 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4494 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4495 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4496
4497 if (this_cu->is_debug_types)
4498 {
4499 ULONGEST header_signature;
4500 cu_offset type_offset_in_tu;
4501 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4502
4503 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4504 dwo_abbrev_section,
4505 info_ptr,
4506 &header_signature,
4507 &type_offset_in_tu);
4508 gdb_assert (sig_type->signature == header_signature);
4509 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4510 /* For DWOs coming from DWP files, we don't know the CU length
4511 nor the type's offset in the TU until now. */
4512 dwo_unit->length = get_cu_length (&cu->header);
4513 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4514
4515 /* Establish the type offset that can be used to lookup the type.
4516 For DWO files, we don't know it until now. */
4517 sig_type->type_offset_in_section.sect_off =
4518 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4519 }
4520 else
4521 {
4522 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4523 dwo_abbrev_section,
4524 info_ptr, 0);
4525 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4526 /* For DWOs coming from DWP files, we don't know the CU length
4527 until now. */
4528 dwo_unit->length = get_cu_length (&cu->header);
4529 }
4530
4531 /* Replace the CU's original abbrev table with the DWO's. */
4532 if (abbrev_table_provided)
4533 {
4534 /* Don't free the provided abbrev table, the caller of
4535 init_cutu_and_read_dies owns it. */
4536 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
4539 else
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4543 }
4544
4545 /* Read in the die, but leave space to copy over the attributes
4546 from the stub. This has the benefit of simplifying the rest of
4547 the code - all the work to maintain the illusion of a single
4548 DW_TAG_{compile,type}_unit DIE is done here. */
4549 num_extra_attrs = ((stmt_list != NULL)
4550 + (low_pc != NULL)
4551 + (high_pc != NULL)
4552 + (ranges != NULL)
4553 + (comp_dir != NULL));
4554 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4555 result_has_children, num_extra_attrs);
4556
4557 /* Copy over the attributes from the stub to the DIE we just read in. */
4558 comp_unit_die = *result_comp_unit_die;
4559 i = comp_unit_die->num_attrs;
4560 if (stmt_list != NULL)
4561 comp_unit_die->attrs[i++] = *stmt_list;
4562 if (low_pc != NULL)
4563 comp_unit_die->attrs[i++] = *low_pc;
4564 if (high_pc != NULL)
4565 comp_unit_die->attrs[i++] = *high_pc;
4566 if (ranges != NULL)
4567 comp_unit_die->attrs[i++] = *ranges;
4568 if (comp_dir != NULL)
4569 comp_unit_die->attrs[i++] = *comp_dir;
4570 comp_unit_die->num_attrs += num_extra_attrs;
4571
4572 /* Skip dummy compilation units. */
4573 if (info_ptr >= begin_info_ptr + dwo_unit->length
4574 || peek_abbrev_code (abfd, info_ptr) == 0)
4575 return 0;
4576
4577 *result_info_ptr = info_ptr;
4578 return 1;
4579 }
4580
4581 /* Subroutine of init_cutu_and_read_dies to simplify it.
4582 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4583 If the specified DWO unit cannot be found an error is thrown. */
4584
4585 static struct dwo_unit *
4586 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4587 struct die_info *comp_unit_die)
4588 {
4589 struct dwarf2_cu *cu = this_cu->cu;
4590 struct attribute *attr;
4591 ULONGEST signature;
4592 struct dwo_unit *dwo_unit;
4593 const char *comp_dir, *dwo_name;
4594
4595 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4596 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4597 gdb_assert (attr != NULL);
4598 dwo_name = DW_STRING (attr);
4599 comp_dir = NULL;
4600 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4601 if (attr)
4602 comp_dir = DW_STRING (attr);
4603
4604 if (this_cu->is_debug_types)
4605 {
4606 struct signatured_type *sig_type;
4607
4608 /* Since this_cu is the first member of struct signatured_type,
4609 we can go from a pointer to one to a pointer to the other. */
4610 sig_type = (struct signatured_type *) this_cu;
4611 signature = sig_type->signature;
4612 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4613 }
4614 else
4615 {
4616 struct attribute *attr;
4617
4618 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4619 if (! attr)
4620 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4621 " [in module %s]"),
4622 dwo_name, this_cu->objfile->name);
4623 signature = DW_UNSND (attr);
4624 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4625 signature);
4626 }
4627
4628 if (dwo_unit == NULL)
4629 {
4630 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4631 " with ID %s [in module %s]"),
4632 this_cu->offset.sect_off,
4633 phex (signature, sizeof (signature)),
4634 this_cu->objfile->name);
4635 }
4636
4637 return dwo_unit;
4638 }
4639
4640 /* Initialize a CU (or TU) and read its DIEs.
4641 If the CU defers to a DWO file, read the DWO file as well.
4642
4643 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4644 Otherwise the table specified in the comp unit header is read in and used.
4645 This is an optimization for when we already have the abbrev table.
4646
4647 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4648 Otherwise, a new CU is allocated with xmalloc.
4649
4650 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4651 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4652
4653 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4654 linker) then DIE_READER_FUNC will not get called. */
4655
4656 static void
4657 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4658 struct abbrev_table *abbrev_table,
4659 int use_existing_cu, int keep,
4660 die_reader_func_ftype *die_reader_func,
4661 void *data)
4662 {
4663 struct objfile *objfile = dwarf2_per_objfile->objfile;
4664 struct dwarf2_section_info *section = this_cu->section;
4665 bfd *abfd = section->asection->owner;
4666 struct dwarf2_cu *cu;
4667 gdb_byte *begin_info_ptr, *info_ptr;
4668 struct die_reader_specs reader;
4669 struct die_info *comp_unit_die;
4670 int has_children;
4671 struct attribute *attr;
4672 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4673 struct signatured_type *sig_type = NULL;
4674 struct dwarf2_section_info *abbrev_section;
4675 /* Non-zero if CU currently points to a DWO file and we need to
4676 reread it. When this happens we need to reread the skeleton die
4677 before we can reread the DWO file. */
4678 int rereading_dwo_cu = 0;
4679
4680 if (dwarf2_die_debug)
4681 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4682 this_cu->is_debug_types ? "type" : "comp",
4683 this_cu->offset.sect_off);
4684
4685 if (use_existing_cu)
4686 gdb_assert (keep);
4687
4688 cleanups = make_cleanup (null_cleanup, NULL);
4689
4690 /* This is cheap if the section is already read in. */
4691 dwarf2_read_section (objfile, section);
4692
4693 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4694
4695 abbrev_section = get_abbrev_section_for_cu (this_cu);
4696
4697 if (use_existing_cu && this_cu->cu != NULL)
4698 {
4699 cu = this_cu->cu;
4700
4701 /* If this CU is from a DWO file we need to start over, we need to
4702 refetch the attributes from the skeleton CU.
4703 This could be optimized by retrieving those attributes from when we
4704 were here the first time: the previous comp_unit_die was stored in
4705 comp_unit_obstack. But there's no data yet that we need this
4706 optimization. */
4707 if (cu->dwo_unit != NULL)
4708 rereading_dwo_cu = 1;
4709 }
4710 else
4711 {
4712 /* If !use_existing_cu, this_cu->cu must be NULL. */
4713 gdb_assert (this_cu->cu == NULL);
4714
4715 cu = xmalloc (sizeof (*cu));
4716 init_one_comp_unit (cu, this_cu);
4717
4718 /* If an error occurs while loading, release our storage. */
4719 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4720 }
4721
4722 /* Get the header. */
4723 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4724 {
4725 /* We already have the header, there's no need to read it in again. */
4726 info_ptr += cu->header.first_die_offset.cu_off;
4727 }
4728 else
4729 {
4730 if (this_cu->is_debug_types)
4731 {
4732 ULONGEST signature;
4733 cu_offset type_offset_in_tu;
4734
4735 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4736 abbrev_section, info_ptr,
4737 &signature,
4738 &type_offset_in_tu);
4739
4740 /* Since per_cu is the first member of struct signatured_type,
4741 we can go from a pointer to one to a pointer to the other. */
4742 sig_type = (struct signatured_type *) this_cu;
4743 gdb_assert (sig_type->signature == signature);
4744 gdb_assert (sig_type->type_offset_in_tu.cu_off
4745 == type_offset_in_tu.cu_off);
4746 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4747
4748 /* LENGTH has not been set yet for type units if we're
4749 using .gdb_index. */
4750 this_cu->length = get_cu_length (&cu->header);
4751
4752 /* Establish the type offset that can be used to lookup the type. */
4753 sig_type->type_offset_in_section.sect_off =
4754 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4755 }
4756 else
4757 {
4758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4759 abbrev_section,
4760 info_ptr, 0);
4761
4762 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4763 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4764 }
4765 }
4766
4767 /* Skip dummy compilation units. */
4768 if (info_ptr >= begin_info_ptr + this_cu->length
4769 || peek_abbrev_code (abfd, info_ptr) == 0)
4770 {
4771 do_cleanups (cleanups);
4772 return;
4773 }
4774
4775 /* If we don't have them yet, read the abbrevs for this compilation unit.
4776 And if we need to read them now, make sure they're freed when we're
4777 done. Note that it's important that if the CU had an abbrev table
4778 on entry we don't free it when we're done: Somewhere up the call stack
4779 it may be in use. */
4780 if (abbrev_table != NULL)
4781 {
4782 gdb_assert (cu->abbrev_table == NULL);
4783 gdb_assert (cu->header.abbrev_offset.sect_off
4784 == abbrev_table->offset.sect_off);
4785 cu->abbrev_table = abbrev_table;
4786 }
4787 else if (cu->abbrev_table == NULL)
4788 {
4789 dwarf2_read_abbrevs (cu, abbrev_section);
4790 make_cleanup (dwarf2_free_abbrev_table, cu);
4791 }
4792 else if (rereading_dwo_cu)
4793 {
4794 dwarf2_free_abbrev_table (cu);
4795 dwarf2_read_abbrevs (cu, abbrev_section);
4796 }
4797
4798 /* Read the top level CU/TU die. */
4799 init_cu_die_reader (&reader, cu, section, NULL);
4800 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4801
4802 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4803 from the DWO file.
4804 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4805 DWO CU, that this test will fail (the attribute will not be present). */
4806 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4807 if (attr)
4808 {
4809 struct dwo_unit *dwo_unit;
4810 struct die_info *dwo_comp_unit_die;
4811
4812 if (has_children)
4813 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4814 " has children (offset 0x%x) [in module %s]"),
4815 this_cu->offset.sect_off, bfd_get_filename (abfd));
4816 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4817 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4818 abbrev_table != NULL,
4819 comp_unit_die,
4820 &reader, &info_ptr,
4821 &dwo_comp_unit_die, &has_children) == 0)
4822 {
4823 /* Dummy die. */
4824 do_cleanups (cleanups);
4825 return;
4826 }
4827 comp_unit_die = dwo_comp_unit_die;
4828 }
4829
4830 /* All of the above is setup for this call. Yikes. */
4831 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4832
4833 /* Done, clean up. */
4834 if (free_cu_cleanup != NULL)
4835 {
4836 if (keep)
4837 {
4838 /* We've successfully allocated this compilation unit. Let our
4839 caller clean it up when finished with it. */
4840 discard_cleanups (free_cu_cleanup);
4841
4842 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4843 So we have to manually free the abbrev table. */
4844 dwarf2_free_abbrev_table (cu);
4845
4846 /* Link this CU into read_in_chain. */
4847 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4848 dwarf2_per_objfile->read_in_chain = this_cu;
4849 }
4850 else
4851 do_cleanups (free_cu_cleanup);
4852 }
4853
4854 do_cleanups (cleanups);
4855 }
4856
4857 /* Read CU/TU THIS_CU in section SECTION,
4858 but do not follow DW_AT_GNU_dwo_name if present.
4859 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4860 to have already done the lookup to find the DWO/DWP file).
4861
4862 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4863 THIS_CU->is_debug_types, but nothing else.
4864
4865 We fill in THIS_CU->length.
4866
4867 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4868 linker) then DIE_READER_FUNC will not get called.
4869
4870 THIS_CU->cu is always freed when done.
4871 This is done in order to not leave THIS_CU->cu in a state where we have
4872 to care whether it refers to the "main" CU or the DWO CU. */
4873
4874 static void
4875 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4876 struct dwarf2_section_info *abbrev_section,
4877 struct dwo_file *dwo_file,
4878 die_reader_func_ftype *die_reader_func,
4879 void *data)
4880 {
4881 struct objfile *objfile = dwarf2_per_objfile->objfile;
4882 struct dwarf2_section_info *section = this_cu->section;
4883 bfd *abfd = section->asection->owner;
4884 struct dwarf2_cu cu;
4885 gdb_byte *begin_info_ptr, *info_ptr;
4886 struct die_reader_specs reader;
4887 struct cleanup *cleanups;
4888 struct die_info *comp_unit_die;
4889 int has_children;
4890
4891 if (dwarf2_die_debug)
4892 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4893 this_cu->is_debug_types ? "type" : "comp",
4894 this_cu->offset.sect_off);
4895
4896 gdb_assert (this_cu->cu == NULL);
4897
4898 /* This is cheap if the section is already read in. */
4899 dwarf2_read_section (objfile, section);
4900
4901 init_one_comp_unit (&cu, this_cu);
4902
4903 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4904
4905 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4906 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4907 abbrev_section, info_ptr,
4908 this_cu->is_debug_types);
4909
4910 this_cu->length = get_cu_length (&cu.header);
4911
4912 /* Skip dummy compilation units. */
4913 if (info_ptr >= begin_info_ptr + this_cu->length
4914 || peek_abbrev_code (abfd, info_ptr) == 0)
4915 {
4916 do_cleanups (cleanups);
4917 return;
4918 }
4919
4920 dwarf2_read_abbrevs (&cu, abbrev_section);
4921 make_cleanup (dwarf2_free_abbrev_table, &cu);
4922
4923 init_cu_die_reader (&reader, &cu, section, dwo_file);
4924 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4925
4926 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4927
4928 do_cleanups (cleanups);
4929 }
4930
4931 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4932 does not lookup the specified DWO file.
4933 This cannot be used to read DWO files.
4934
4935 THIS_CU->cu is always freed when done.
4936 This is done in order to not leave THIS_CU->cu in a state where we have
4937 to care whether it refers to the "main" CU or the DWO CU.
4938 We can revisit this if the data shows there's a performance issue. */
4939
4940 static void
4941 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4942 die_reader_func_ftype *die_reader_func,
4943 void *data)
4944 {
4945 init_cutu_and_read_dies_no_follow (this_cu,
4946 get_abbrev_section_for_cu (this_cu),
4947 NULL,
4948 die_reader_func, data);
4949 }
4950 \f
4951 /* Type Unit Groups.
4952
4953 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4954 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4955 so that all types coming from the same compilation (.o file) are grouped
4956 together. A future step could be to put the types in the same symtab as
4957 the CU the types ultimately came from. */
4958
4959 static hashval_t
4960 hash_type_unit_group (const void *item)
4961 {
4962 const struct type_unit_group *tu_group = item;
4963
4964 return hash_stmt_list_entry (&tu_group->hash);
4965 }
4966
4967 static int
4968 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4969 {
4970 const struct type_unit_group *lhs = item_lhs;
4971 const struct type_unit_group *rhs = item_rhs;
4972
4973 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4974 }
4975
4976 /* Allocate a hash table for type unit groups. */
4977
4978 static htab_t
4979 allocate_type_unit_groups_table (void)
4980 {
4981 return htab_create_alloc_ex (3,
4982 hash_type_unit_group,
4983 eq_type_unit_group,
4984 NULL,
4985 &dwarf2_per_objfile->objfile->objfile_obstack,
4986 hashtab_obstack_allocate,
4987 dummy_obstack_deallocate);
4988 }
4989
4990 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4991 partial symtabs. We combine several TUs per psymtab to not let the size
4992 of any one psymtab grow too big. */
4993 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4994 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4995
4996 /* Helper routine for get_type_unit_group.
4997 Create the type_unit_group object used to hold one or more TUs. */
4998
4999 static struct type_unit_group *
5000 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5001 {
5002 struct objfile *objfile = dwarf2_per_objfile->objfile;
5003 struct dwarf2_per_cu_data *per_cu;
5004 struct type_unit_group *tu_group;
5005
5006 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5007 struct type_unit_group);
5008 per_cu = &tu_group->per_cu;
5009 per_cu->objfile = objfile;
5010
5011 if (dwarf2_per_objfile->using_index)
5012 {
5013 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5014 struct dwarf2_per_cu_quick_data);
5015 }
5016 else
5017 {
5018 unsigned int line_offset = line_offset_struct.sect_off;
5019 struct partial_symtab *pst;
5020 char *name;
5021
5022 /* Give the symtab a useful name for debug purposes. */
5023 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5024 name = xstrprintf ("<type_units_%d>",
5025 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5026 else
5027 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5028
5029 pst = create_partial_symtab (per_cu, name);
5030 pst->anonymous = 1;
5031
5032 xfree (name);
5033 }
5034
5035 tu_group->hash.dwo_unit = cu->dwo_unit;
5036 tu_group->hash.line_offset = line_offset_struct;
5037
5038 return tu_group;
5039 }
5040
5041 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5042 STMT_LIST is a DW_AT_stmt_list attribute. */
5043
5044 static struct type_unit_group *
5045 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5046 {
5047 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5048 struct type_unit_group *tu_group;
5049 void **slot;
5050 unsigned int line_offset;
5051 struct type_unit_group type_unit_group_for_lookup;
5052
5053 if (dwarf2_per_objfile->type_unit_groups == NULL)
5054 {
5055 dwarf2_per_objfile->type_unit_groups =
5056 allocate_type_unit_groups_table ();
5057 }
5058
5059 /* Do we need to create a new group, or can we use an existing one? */
5060
5061 if (stmt_list)
5062 {
5063 line_offset = DW_UNSND (stmt_list);
5064 ++tu_stats->nr_symtab_sharers;
5065 }
5066 else
5067 {
5068 /* Ugh, no stmt_list. Rare, but we have to handle it.
5069 We can do various things here like create one group per TU or
5070 spread them over multiple groups to split up the expansion work.
5071 To avoid worst case scenarios (too many groups or too large groups)
5072 we, umm, group them in bunches. */
5073 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5074 | (tu_stats->nr_stmt_less_type_units
5075 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5076 ++tu_stats->nr_stmt_less_type_units;
5077 }
5078
5079 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5080 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5081 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5082 &type_unit_group_for_lookup, INSERT);
5083 if (*slot != NULL)
5084 {
5085 tu_group = *slot;
5086 gdb_assert (tu_group != NULL);
5087 }
5088 else
5089 {
5090 sect_offset line_offset_struct;
5091
5092 line_offset_struct.sect_off = line_offset;
5093 tu_group = create_type_unit_group (cu, line_offset_struct);
5094 *slot = tu_group;
5095 ++tu_stats->nr_symtabs;
5096 }
5097
5098 return tu_group;
5099 }
5100
5101 /* Struct used to sort TUs by their abbreviation table offset. */
5102
5103 struct tu_abbrev_offset
5104 {
5105 struct signatured_type *sig_type;
5106 sect_offset abbrev_offset;
5107 };
5108
5109 /* Helper routine for build_type_unit_groups, passed to qsort. */
5110
5111 static int
5112 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5113 {
5114 const struct tu_abbrev_offset * const *a = ap;
5115 const struct tu_abbrev_offset * const *b = bp;
5116 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5117 unsigned int boff = (*b)->abbrev_offset.sect_off;
5118
5119 return (aoff > boff) - (aoff < boff);
5120 }
5121
5122 /* A helper function to add a type_unit_group to a table. */
5123
5124 static int
5125 add_type_unit_group_to_table (void **slot, void *datum)
5126 {
5127 struct type_unit_group *tu_group = *slot;
5128 struct type_unit_group ***datap = datum;
5129
5130 **datap = tu_group;
5131 ++*datap;
5132
5133 return 1;
5134 }
5135
5136 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5137 each one passing FUNC,DATA.
5138
5139 The efficiency is because we sort TUs by the abbrev table they use and
5140 only read each abbrev table once. In one program there are 200K TUs
5141 sharing 8K abbrev tables.
5142
5143 The main purpose of this function is to support building the
5144 dwarf2_per_objfile->type_unit_groups table.
5145 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5146 can collapse the search space by grouping them by stmt_list.
5147 The savings can be significant, in the same program from above the 200K TUs
5148 share 8K stmt_list tables.
5149
5150 FUNC is expected to call get_type_unit_group, which will create the
5151 struct type_unit_group if necessary and add it to
5152 dwarf2_per_objfile->type_unit_groups. */
5153
5154 static void
5155 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5156 {
5157 struct objfile *objfile = dwarf2_per_objfile->objfile;
5158 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5159 struct cleanup *cleanups;
5160 struct abbrev_table *abbrev_table;
5161 sect_offset abbrev_offset;
5162 struct tu_abbrev_offset *sorted_by_abbrev;
5163 struct type_unit_group **iter;
5164 int i;
5165
5166 /* It's up to the caller to not call us multiple times. */
5167 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5168
5169 if (dwarf2_per_objfile->n_type_units == 0)
5170 return;
5171
5172 /* TUs typically share abbrev tables, and there can be way more TUs than
5173 abbrev tables. Sort by abbrev table to reduce the number of times we
5174 read each abbrev table in.
5175 Alternatives are to punt or to maintain a cache of abbrev tables.
5176 This is simpler and efficient enough for now.
5177
5178 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5179 symtab to use). Typically TUs with the same abbrev offset have the same
5180 stmt_list value too so in practice this should work well.
5181
5182 The basic algorithm here is:
5183
5184 sort TUs by abbrev table
5185 for each TU with same abbrev table:
5186 read abbrev table if first user
5187 read TU top level DIE
5188 [IWBN if DWO skeletons had DW_AT_stmt_list]
5189 call FUNC */
5190
5191 if (dwarf2_read_debug)
5192 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5193
5194 /* Sort in a separate table to maintain the order of all_type_units
5195 for .gdb_index: TU indices directly index all_type_units. */
5196 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5197 dwarf2_per_objfile->n_type_units);
5198 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5199 {
5200 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5201
5202 sorted_by_abbrev[i].sig_type = sig_type;
5203 sorted_by_abbrev[i].abbrev_offset =
5204 read_abbrev_offset (sig_type->per_cu.section,
5205 sig_type->per_cu.offset);
5206 }
5207 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5208 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5209 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5210
5211 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5212 called any number of times, so we don't reset tu_stats here. */
5213
5214 abbrev_offset.sect_off = ~(unsigned) 0;
5215 abbrev_table = NULL;
5216 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5217
5218 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5219 {
5220 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5221
5222 /* Switch to the next abbrev table if necessary. */
5223 if (abbrev_table == NULL
5224 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5225 {
5226 if (abbrev_table != NULL)
5227 {
5228 abbrev_table_free (abbrev_table);
5229 /* Reset to NULL in case abbrev_table_read_table throws
5230 an error: abbrev_table_free_cleanup will get called. */
5231 abbrev_table = NULL;
5232 }
5233 abbrev_offset = tu->abbrev_offset;
5234 abbrev_table =
5235 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5236 abbrev_offset);
5237 ++tu_stats->nr_uniq_abbrev_tables;
5238 }
5239
5240 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5241 func, data);
5242 }
5243
5244 /* Create a vector of pointers to primary type units to make it easy to
5245 iterate over them and CUs. See dw2_get_primary_cu. */
5246 dwarf2_per_objfile->n_type_unit_groups =
5247 htab_elements (dwarf2_per_objfile->type_unit_groups);
5248 dwarf2_per_objfile->all_type_unit_groups =
5249 obstack_alloc (&objfile->objfile_obstack,
5250 dwarf2_per_objfile->n_type_unit_groups
5251 * sizeof (struct type_unit_group *));
5252 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5253 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5254 add_type_unit_group_to_table, &iter);
5255 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5256 == dwarf2_per_objfile->n_type_unit_groups);
5257
5258 do_cleanups (cleanups);
5259
5260 if (dwarf2_read_debug)
5261 {
5262 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5263 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5264 dwarf2_per_objfile->n_type_units);
5265 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5266 tu_stats->nr_uniq_abbrev_tables);
5267 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5268 tu_stats->nr_symtabs);
5269 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5270 tu_stats->nr_symtab_sharers);
5271 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5272 tu_stats->nr_stmt_less_type_units);
5273 }
5274 }
5275 \f
5276 /* Partial symbol tables. */
5277
5278 /* Create a psymtab named NAME and assign it to PER_CU.
5279
5280 The caller must fill in the following details:
5281 dirname, textlow, texthigh. */
5282
5283 static struct partial_symtab *
5284 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5285 {
5286 struct objfile *objfile = per_cu->objfile;
5287 struct partial_symtab *pst;
5288
5289 pst = start_psymtab_common (objfile, objfile->section_offsets,
5290 name, 0,
5291 objfile->global_psymbols.next,
5292 objfile->static_psymbols.next);
5293
5294 pst->psymtabs_addrmap_supported = 1;
5295
5296 /* This is the glue that links PST into GDB's symbol API. */
5297 pst->read_symtab_private = per_cu;
5298 pst->read_symtab = dwarf2_read_symtab;
5299 per_cu->v.psymtab = pst;
5300
5301 return pst;
5302 }
5303
5304 /* die_reader_func for process_psymtab_comp_unit. */
5305
5306 static void
5307 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5308 gdb_byte *info_ptr,
5309 struct die_info *comp_unit_die,
5310 int has_children,
5311 void *data)
5312 {
5313 struct dwarf2_cu *cu = reader->cu;
5314 struct objfile *objfile = cu->objfile;
5315 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5316 struct attribute *attr;
5317 CORE_ADDR baseaddr;
5318 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5319 struct partial_symtab *pst;
5320 int has_pc_info;
5321 const char *filename;
5322 int *want_partial_unit_ptr = data;
5323
5324 if (comp_unit_die->tag == DW_TAG_partial_unit
5325 && (want_partial_unit_ptr == NULL
5326 || !*want_partial_unit_ptr))
5327 return;
5328
5329 gdb_assert (! per_cu->is_debug_types);
5330
5331 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5332
5333 cu->list_in_scope = &file_symbols;
5334
5335 /* Allocate a new partial symbol table structure. */
5336 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5337 if (attr == NULL || !DW_STRING (attr))
5338 filename = "";
5339 else
5340 filename = DW_STRING (attr);
5341
5342 pst = create_partial_symtab (per_cu, filename);
5343
5344 /* This must be done before calling dwarf2_build_include_psymtabs. */
5345 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5346 if (attr != NULL)
5347 pst->dirname = DW_STRING (attr);
5348
5349 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5350
5351 dwarf2_find_base_address (comp_unit_die, cu);
5352
5353 /* Possibly set the default values of LOWPC and HIGHPC from
5354 `DW_AT_ranges'. */
5355 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5356 &best_highpc, cu, pst);
5357 if (has_pc_info == 1 && best_lowpc < best_highpc)
5358 /* Store the contiguous range if it is not empty; it can be empty for
5359 CUs with no code. */
5360 addrmap_set_empty (objfile->psymtabs_addrmap,
5361 best_lowpc + baseaddr,
5362 best_highpc + baseaddr - 1, pst);
5363
5364 /* Check if comp unit has_children.
5365 If so, read the rest of the partial symbols from this comp unit.
5366 If not, there's no more debug_info for this comp unit. */
5367 if (has_children)
5368 {
5369 struct partial_die_info *first_die;
5370 CORE_ADDR lowpc, highpc;
5371
5372 lowpc = ((CORE_ADDR) -1);
5373 highpc = ((CORE_ADDR) 0);
5374
5375 first_die = load_partial_dies (reader, info_ptr, 1);
5376
5377 scan_partial_symbols (first_die, &lowpc, &highpc,
5378 ! has_pc_info, cu);
5379
5380 /* If we didn't find a lowpc, set it to highpc to avoid
5381 complaints from `maint check'. */
5382 if (lowpc == ((CORE_ADDR) -1))
5383 lowpc = highpc;
5384
5385 /* If the compilation unit didn't have an explicit address range,
5386 then use the information extracted from its child dies. */
5387 if (! has_pc_info)
5388 {
5389 best_lowpc = lowpc;
5390 best_highpc = highpc;
5391 }
5392 }
5393 pst->textlow = best_lowpc + baseaddr;
5394 pst->texthigh = best_highpc + baseaddr;
5395
5396 pst->n_global_syms = objfile->global_psymbols.next -
5397 (objfile->global_psymbols.list + pst->globals_offset);
5398 pst->n_static_syms = objfile->static_psymbols.next -
5399 (objfile->static_psymbols.list + pst->statics_offset);
5400 sort_pst_symbols (objfile, pst);
5401
5402 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5403 {
5404 int i;
5405 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5406 struct dwarf2_per_cu_data *iter;
5407
5408 /* Fill in 'dependencies' here; we fill in 'users' in a
5409 post-pass. */
5410 pst->number_of_dependencies = len;
5411 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5412 len * sizeof (struct symtab *));
5413 for (i = 0;
5414 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5415 i, iter);
5416 ++i)
5417 pst->dependencies[i] = iter->v.psymtab;
5418
5419 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5420 }
5421
5422 /* Get the list of files included in the current compilation unit,
5423 and build a psymtab for each of them. */
5424 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5425
5426 if (dwarf2_read_debug)
5427 {
5428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5429
5430 fprintf_unfiltered (gdb_stdlog,
5431 "Psymtab for %s unit @0x%x: %s - %s"
5432 ", %d global, %d static syms\n",
5433 per_cu->is_debug_types ? "type" : "comp",
5434 per_cu->offset.sect_off,
5435 paddress (gdbarch, pst->textlow),
5436 paddress (gdbarch, pst->texthigh),
5437 pst->n_global_syms, pst->n_static_syms);
5438 }
5439 }
5440
5441 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5442 Process compilation unit THIS_CU for a psymtab. */
5443
5444 static void
5445 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5446 int want_partial_unit)
5447 {
5448 /* If this compilation unit was already read in, free the
5449 cached copy in order to read it in again. This is
5450 necessary because we skipped some symbols when we first
5451 read in the compilation unit (see load_partial_dies).
5452 This problem could be avoided, but the benefit is unclear. */
5453 if (this_cu->cu != NULL)
5454 free_one_cached_comp_unit (this_cu);
5455
5456 gdb_assert (! this_cu->is_debug_types);
5457 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5458 process_psymtab_comp_unit_reader,
5459 &want_partial_unit);
5460
5461 /* Age out any secondary CUs. */
5462 age_cached_comp_units ();
5463 }
5464
5465 /* Reader function for build_type_psymtabs. */
5466
5467 static void
5468 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5469 gdb_byte *info_ptr,
5470 struct die_info *type_unit_die,
5471 int has_children,
5472 void *data)
5473 {
5474 struct objfile *objfile = dwarf2_per_objfile->objfile;
5475 struct dwarf2_cu *cu = reader->cu;
5476 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5477 struct signatured_type *sig_type;
5478 struct type_unit_group *tu_group;
5479 struct attribute *attr;
5480 struct partial_die_info *first_die;
5481 CORE_ADDR lowpc, highpc;
5482 struct partial_symtab *pst;
5483
5484 gdb_assert (data == NULL);
5485 gdb_assert (per_cu->is_debug_types);
5486 sig_type = (struct signatured_type *) per_cu;
5487
5488 if (! has_children)
5489 return;
5490
5491 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5492 tu_group = get_type_unit_group (cu, attr);
5493
5494 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5495
5496 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5497 cu->list_in_scope = &file_symbols;
5498 pst = create_partial_symtab (per_cu, "");
5499 pst->anonymous = 1;
5500
5501 first_die = load_partial_dies (reader, info_ptr, 1);
5502
5503 lowpc = (CORE_ADDR) -1;
5504 highpc = (CORE_ADDR) 0;
5505 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5506
5507 pst->n_global_syms = objfile->global_psymbols.next -
5508 (objfile->global_psymbols.list + pst->globals_offset);
5509 pst->n_static_syms = objfile->static_psymbols.next -
5510 (objfile->static_psymbols.list + pst->statics_offset);
5511 sort_pst_symbols (objfile, pst);
5512 }
5513
5514 /* Traversal function for build_type_psymtabs. */
5515
5516 static int
5517 build_type_psymtab_dependencies (void **slot, void *info)
5518 {
5519 struct objfile *objfile = dwarf2_per_objfile->objfile;
5520 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5521 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5522 struct partial_symtab *pst = per_cu->v.psymtab;
5523 int len = VEC_length (sig_type_ptr, tu_group->tus);
5524 struct signatured_type *iter;
5525 int i;
5526
5527 gdb_assert (len > 0);
5528 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5529
5530 pst->number_of_dependencies = len;
5531 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5532 len * sizeof (struct psymtab *));
5533 for (i = 0;
5534 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5535 ++i)
5536 {
5537 gdb_assert (iter->per_cu.is_debug_types);
5538 pst->dependencies[i] = iter->per_cu.v.psymtab;
5539 iter->type_unit_group = tu_group;
5540 }
5541
5542 VEC_free (sig_type_ptr, tu_group->tus);
5543
5544 return 1;
5545 }
5546
5547 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5548 Build partial symbol tables for the .debug_types comp-units. */
5549
5550 static void
5551 build_type_psymtabs (struct objfile *objfile)
5552 {
5553 if (! create_all_type_units (objfile))
5554 return;
5555
5556 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5557
5558 /* Now that all TUs have been processed we can fill in the dependencies. */
5559 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5560 build_type_psymtab_dependencies, NULL);
5561 }
5562
5563 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5564
5565 static void
5566 psymtabs_addrmap_cleanup (void *o)
5567 {
5568 struct objfile *objfile = o;
5569
5570 objfile->psymtabs_addrmap = NULL;
5571 }
5572
5573 /* Compute the 'user' field for each psymtab in OBJFILE. */
5574
5575 static void
5576 set_partial_user (struct objfile *objfile)
5577 {
5578 int i;
5579
5580 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5581 {
5582 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5583 struct partial_symtab *pst = per_cu->v.psymtab;
5584 int j;
5585
5586 if (pst == NULL)
5587 continue;
5588
5589 for (j = 0; j < pst->number_of_dependencies; ++j)
5590 {
5591 /* Set the 'user' field only if it is not already set. */
5592 if (pst->dependencies[j]->user == NULL)
5593 pst->dependencies[j]->user = pst;
5594 }
5595 }
5596 }
5597
5598 /* Build the partial symbol table by doing a quick pass through the
5599 .debug_info and .debug_abbrev sections. */
5600
5601 static void
5602 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5603 {
5604 struct cleanup *back_to, *addrmap_cleanup;
5605 struct obstack temp_obstack;
5606 int i;
5607
5608 if (dwarf2_read_debug)
5609 {
5610 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5611 objfile->name);
5612 }
5613
5614 dwarf2_per_objfile->reading_partial_symbols = 1;
5615
5616 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5617
5618 /* Any cached compilation units will be linked by the per-objfile
5619 read_in_chain. Make sure to free them when we're done. */
5620 back_to = make_cleanup (free_cached_comp_units, NULL);
5621
5622 build_type_psymtabs (objfile);
5623
5624 create_all_comp_units (objfile);
5625
5626 /* Create a temporary address map on a temporary obstack. We later
5627 copy this to the final obstack. */
5628 obstack_init (&temp_obstack);
5629 make_cleanup_obstack_free (&temp_obstack);
5630 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5631 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5632
5633 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5634 {
5635 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5636
5637 process_psymtab_comp_unit (per_cu, 0);
5638 }
5639
5640 set_partial_user (objfile);
5641
5642 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5643 &objfile->objfile_obstack);
5644 discard_cleanups (addrmap_cleanup);
5645
5646 do_cleanups (back_to);
5647
5648 if (dwarf2_read_debug)
5649 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5650 objfile->name);
5651 }
5652
5653 /* die_reader_func for load_partial_comp_unit. */
5654
5655 static void
5656 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5657 gdb_byte *info_ptr,
5658 struct die_info *comp_unit_die,
5659 int has_children,
5660 void *data)
5661 {
5662 struct dwarf2_cu *cu = reader->cu;
5663
5664 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5665
5666 /* Check if comp unit has_children.
5667 If so, read the rest of the partial symbols from this comp unit.
5668 If not, there's no more debug_info for this comp unit. */
5669 if (has_children)
5670 load_partial_dies (reader, info_ptr, 0);
5671 }
5672
5673 /* Load the partial DIEs for a secondary CU into memory.
5674 This is also used when rereading a primary CU with load_all_dies. */
5675
5676 static void
5677 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5678 {
5679 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5680 load_partial_comp_unit_reader, NULL);
5681 }
5682
5683 static void
5684 read_comp_units_from_section (struct objfile *objfile,
5685 struct dwarf2_section_info *section,
5686 unsigned int is_dwz,
5687 int *n_allocated,
5688 int *n_comp_units,
5689 struct dwarf2_per_cu_data ***all_comp_units)
5690 {
5691 gdb_byte *info_ptr;
5692 bfd *abfd = section->asection->owner;
5693
5694 dwarf2_read_section (objfile, section);
5695
5696 info_ptr = section->buffer;
5697
5698 while (info_ptr < section->buffer + section->size)
5699 {
5700 unsigned int length, initial_length_size;
5701 struct dwarf2_per_cu_data *this_cu;
5702 sect_offset offset;
5703
5704 offset.sect_off = info_ptr - section->buffer;
5705
5706 /* Read just enough information to find out where the next
5707 compilation unit is. */
5708 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5709
5710 /* Save the compilation unit for later lookup. */
5711 this_cu = obstack_alloc (&objfile->objfile_obstack,
5712 sizeof (struct dwarf2_per_cu_data));
5713 memset (this_cu, 0, sizeof (*this_cu));
5714 this_cu->offset = offset;
5715 this_cu->length = length + initial_length_size;
5716 this_cu->is_dwz = is_dwz;
5717 this_cu->objfile = objfile;
5718 this_cu->section = section;
5719
5720 if (*n_comp_units == *n_allocated)
5721 {
5722 *n_allocated *= 2;
5723 *all_comp_units = xrealloc (*all_comp_units,
5724 *n_allocated
5725 * sizeof (struct dwarf2_per_cu_data *));
5726 }
5727 (*all_comp_units)[*n_comp_units] = this_cu;
5728 ++*n_comp_units;
5729
5730 info_ptr = info_ptr + this_cu->length;
5731 }
5732 }
5733
5734 /* Create a list of all compilation units in OBJFILE.
5735 This is only done for -readnow and building partial symtabs. */
5736
5737 static void
5738 create_all_comp_units (struct objfile *objfile)
5739 {
5740 int n_allocated;
5741 int n_comp_units;
5742 struct dwarf2_per_cu_data **all_comp_units;
5743
5744 n_comp_units = 0;
5745 n_allocated = 10;
5746 all_comp_units = xmalloc (n_allocated
5747 * sizeof (struct dwarf2_per_cu_data *));
5748
5749 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5750 &n_allocated, &n_comp_units, &all_comp_units);
5751
5752 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5753 {
5754 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5755
5756 read_comp_units_from_section (objfile, &dwz->info, 1,
5757 &n_allocated, &n_comp_units,
5758 &all_comp_units);
5759 }
5760
5761 dwarf2_per_objfile->all_comp_units
5762 = obstack_alloc (&objfile->objfile_obstack,
5763 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5764 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5765 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5766 xfree (all_comp_units);
5767 dwarf2_per_objfile->n_comp_units = n_comp_units;
5768 }
5769
5770 /* Process all loaded DIEs for compilation unit CU, starting at
5771 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5772 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5773 DW_AT_ranges). If NEED_PC is set, then this function will set
5774 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5775 and record the covered ranges in the addrmap. */
5776
5777 static void
5778 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5779 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5780 {
5781 struct partial_die_info *pdi;
5782
5783 /* Now, march along the PDI's, descending into ones which have
5784 interesting children but skipping the children of the other ones,
5785 until we reach the end of the compilation unit. */
5786
5787 pdi = first_die;
5788
5789 while (pdi != NULL)
5790 {
5791 fixup_partial_die (pdi, cu);
5792
5793 /* Anonymous namespaces or modules have no name but have interesting
5794 children, so we need to look at them. Ditto for anonymous
5795 enums. */
5796
5797 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5798 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5799 || pdi->tag == DW_TAG_imported_unit)
5800 {
5801 switch (pdi->tag)
5802 {
5803 case DW_TAG_subprogram:
5804 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5805 break;
5806 case DW_TAG_constant:
5807 case DW_TAG_variable:
5808 case DW_TAG_typedef:
5809 case DW_TAG_union_type:
5810 if (!pdi->is_declaration)
5811 {
5812 add_partial_symbol (pdi, cu);
5813 }
5814 break;
5815 case DW_TAG_class_type:
5816 case DW_TAG_interface_type:
5817 case DW_TAG_structure_type:
5818 if (!pdi->is_declaration)
5819 {
5820 add_partial_symbol (pdi, cu);
5821 }
5822 break;
5823 case DW_TAG_enumeration_type:
5824 if (!pdi->is_declaration)
5825 add_partial_enumeration (pdi, cu);
5826 break;
5827 case DW_TAG_base_type:
5828 case DW_TAG_subrange_type:
5829 /* File scope base type definitions are added to the partial
5830 symbol table. */
5831 add_partial_symbol (pdi, cu);
5832 break;
5833 case DW_TAG_namespace:
5834 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5835 break;
5836 case DW_TAG_module:
5837 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5838 break;
5839 case DW_TAG_imported_unit:
5840 {
5841 struct dwarf2_per_cu_data *per_cu;
5842
5843 /* For now we don't handle imported units in type units. */
5844 if (cu->per_cu->is_debug_types)
5845 {
5846 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5847 " supported in type units [in module %s]"),
5848 cu->objfile->name);
5849 }
5850
5851 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5852 pdi->is_dwz,
5853 cu->objfile);
5854
5855 /* Go read the partial unit, if needed. */
5856 if (per_cu->v.psymtab == NULL)
5857 process_psymtab_comp_unit (per_cu, 1);
5858
5859 VEC_safe_push (dwarf2_per_cu_ptr,
5860 cu->per_cu->imported_symtabs, per_cu);
5861 }
5862 break;
5863 default:
5864 break;
5865 }
5866 }
5867
5868 /* If the die has a sibling, skip to the sibling. */
5869
5870 pdi = pdi->die_sibling;
5871 }
5872 }
5873
5874 /* Functions used to compute the fully scoped name of a partial DIE.
5875
5876 Normally, this is simple. For C++, the parent DIE's fully scoped
5877 name is concatenated with "::" and the partial DIE's name. For
5878 Java, the same thing occurs except that "." is used instead of "::".
5879 Enumerators are an exception; they use the scope of their parent
5880 enumeration type, i.e. the name of the enumeration type is not
5881 prepended to the enumerator.
5882
5883 There are two complexities. One is DW_AT_specification; in this
5884 case "parent" means the parent of the target of the specification,
5885 instead of the direct parent of the DIE. The other is compilers
5886 which do not emit DW_TAG_namespace; in this case we try to guess
5887 the fully qualified name of structure types from their members'
5888 linkage names. This must be done using the DIE's children rather
5889 than the children of any DW_AT_specification target. We only need
5890 to do this for structures at the top level, i.e. if the target of
5891 any DW_AT_specification (if any; otherwise the DIE itself) does not
5892 have a parent. */
5893
5894 /* Compute the scope prefix associated with PDI's parent, in
5895 compilation unit CU. The result will be allocated on CU's
5896 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5897 field. NULL is returned if no prefix is necessary. */
5898 static const char *
5899 partial_die_parent_scope (struct partial_die_info *pdi,
5900 struct dwarf2_cu *cu)
5901 {
5902 const char *grandparent_scope;
5903 struct partial_die_info *parent, *real_pdi;
5904
5905 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5906 then this means the parent of the specification DIE. */
5907
5908 real_pdi = pdi;
5909 while (real_pdi->has_specification)
5910 real_pdi = find_partial_die (real_pdi->spec_offset,
5911 real_pdi->spec_is_dwz, cu);
5912
5913 parent = real_pdi->die_parent;
5914 if (parent == NULL)
5915 return NULL;
5916
5917 if (parent->scope_set)
5918 return parent->scope;
5919
5920 fixup_partial_die (parent, cu);
5921
5922 grandparent_scope = partial_die_parent_scope (parent, cu);
5923
5924 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5925 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5926 Work around this problem here. */
5927 if (cu->language == language_cplus
5928 && parent->tag == DW_TAG_namespace
5929 && strcmp (parent->name, "::") == 0
5930 && grandparent_scope == NULL)
5931 {
5932 parent->scope = NULL;
5933 parent->scope_set = 1;
5934 return NULL;
5935 }
5936
5937 if (pdi->tag == DW_TAG_enumerator)
5938 /* Enumerators should not get the name of the enumeration as a prefix. */
5939 parent->scope = grandparent_scope;
5940 else if (parent->tag == DW_TAG_namespace
5941 || parent->tag == DW_TAG_module
5942 || parent->tag == DW_TAG_structure_type
5943 || parent->tag == DW_TAG_class_type
5944 || parent->tag == DW_TAG_interface_type
5945 || parent->tag == DW_TAG_union_type
5946 || parent->tag == DW_TAG_enumeration_type)
5947 {
5948 if (grandparent_scope == NULL)
5949 parent->scope = parent->name;
5950 else
5951 parent->scope = typename_concat (&cu->comp_unit_obstack,
5952 grandparent_scope,
5953 parent->name, 0, cu);
5954 }
5955 else
5956 {
5957 /* FIXME drow/2004-04-01: What should we be doing with
5958 function-local names? For partial symbols, we should probably be
5959 ignoring them. */
5960 complaint (&symfile_complaints,
5961 _("unhandled containing DIE tag %d for DIE at %d"),
5962 parent->tag, pdi->offset.sect_off);
5963 parent->scope = grandparent_scope;
5964 }
5965
5966 parent->scope_set = 1;
5967 return parent->scope;
5968 }
5969
5970 /* Return the fully scoped name associated with PDI, from compilation unit
5971 CU. The result will be allocated with malloc. */
5972
5973 static char *
5974 partial_die_full_name (struct partial_die_info *pdi,
5975 struct dwarf2_cu *cu)
5976 {
5977 const char *parent_scope;
5978
5979 /* If this is a template instantiation, we can not work out the
5980 template arguments from partial DIEs. So, unfortunately, we have
5981 to go through the full DIEs. At least any work we do building
5982 types here will be reused if full symbols are loaded later. */
5983 if (pdi->has_template_arguments)
5984 {
5985 fixup_partial_die (pdi, cu);
5986
5987 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5988 {
5989 struct die_info *die;
5990 struct attribute attr;
5991 struct dwarf2_cu *ref_cu = cu;
5992
5993 /* DW_FORM_ref_addr is using section offset. */
5994 attr.name = 0;
5995 attr.form = DW_FORM_ref_addr;
5996 attr.u.unsnd = pdi->offset.sect_off;
5997 die = follow_die_ref (NULL, &attr, &ref_cu);
5998
5999 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6000 }
6001 }
6002
6003 parent_scope = partial_die_parent_scope (pdi, cu);
6004 if (parent_scope == NULL)
6005 return NULL;
6006 else
6007 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6008 }
6009
6010 static void
6011 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6012 {
6013 struct objfile *objfile = cu->objfile;
6014 CORE_ADDR addr = 0;
6015 const char *actual_name = NULL;
6016 CORE_ADDR baseaddr;
6017 char *built_actual_name;
6018
6019 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6020
6021 built_actual_name = partial_die_full_name (pdi, cu);
6022 if (built_actual_name != NULL)
6023 actual_name = built_actual_name;
6024
6025 if (actual_name == NULL)
6026 actual_name = pdi->name;
6027
6028 switch (pdi->tag)
6029 {
6030 case DW_TAG_subprogram:
6031 if (pdi->is_external || cu->language == language_ada)
6032 {
6033 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6034 of the global scope. But in Ada, we want to be able to access
6035 nested procedures globally. So all Ada subprograms are stored
6036 in the global scope. */
6037 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6038 mst_text, objfile); */
6039 add_psymbol_to_list (actual_name, strlen (actual_name),
6040 built_actual_name != NULL,
6041 VAR_DOMAIN, LOC_BLOCK,
6042 &objfile->global_psymbols,
6043 0, pdi->lowpc + baseaddr,
6044 cu->language, objfile);
6045 }
6046 else
6047 {
6048 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6049 mst_file_text, objfile); */
6050 add_psymbol_to_list (actual_name, strlen (actual_name),
6051 built_actual_name != NULL,
6052 VAR_DOMAIN, LOC_BLOCK,
6053 &objfile->static_psymbols,
6054 0, pdi->lowpc + baseaddr,
6055 cu->language, objfile);
6056 }
6057 break;
6058 case DW_TAG_constant:
6059 {
6060 struct psymbol_allocation_list *list;
6061
6062 if (pdi->is_external)
6063 list = &objfile->global_psymbols;
6064 else
6065 list = &objfile->static_psymbols;
6066 add_psymbol_to_list (actual_name, strlen (actual_name),
6067 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6068 list, 0, 0, cu->language, objfile);
6069 }
6070 break;
6071 case DW_TAG_variable:
6072 if (pdi->d.locdesc)
6073 addr = decode_locdesc (pdi->d.locdesc, cu);
6074
6075 if (pdi->d.locdesc
6076 && addr == 0
6077 && !dwarf2_per_objfile->has_section_at_zero)
6078 {
6079 /* A global or static variable may also have been stripped
6080 out by the linker if unused, in which case its address
6081 will be nullified; do not add such variables into partial
6082 symbol table then. */
6083 }
6084 else if (pdi->is_external)
6085 {
6086 /* Global Variable.
6087 Don't enter into the minimal symbol tables as there is
6088 a minimal symbol table entry from the ELF symbols already.
6089 Enter into partial symbol table if it has a location
6090 descriptor or a type.
6091 If the location descriptor is missing, new_symbol will create
6092 a LOC_UNRESOLVED symbol, the address of the variable will then
6093 be determined from the minimal symbol table whenever the variable
6094 is referenced.
6095 The address for the partial symbol table entry is not
6096 used by GDB, but it comes in handy for debugging partial symbol
6097 table building. */
6098
6099 if (pdi->d.locdesc || pdi->has_type)
6100 add_psymbol_to_list (actual_name, strlen (actual_name),
6101 built_actual_name != NULL,
6102 VAR_DOMAIN, LOC_STATIC,
6103 &objfile->global_psymbols,
6104 0, addr + baseaddr,
6105 cu->language, objfile);
6106 }
6107 else
6108 {
6109 /* Static Variable. Skip symbols without location descriptors. */
6110 if (pdi->d.locdesc == NULL)
6111 {
6112 xfree (built_actual_name);
6113 return;
6114 }
6115 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6116 mst_file_data, objfile); */
6117 add_psymbol_to_list (actual_name, strlen (actual_name),
6118 built_actual_name != NULL,
6119 VAR_DOMAIN, LOC_STATIC,
6120 &objfile->static_psymbols,
6121 0, addr + baseaddr,
6122 cu->language, objfile);
6123 }
6124 break;
6125 case DW_TAG_typedef:
6126 case DW_TAG_base_type:
6127 case DW_TAG_subrange_type:
6128 add_psymbol_to_list (actual_name, strlen (actual_name),
6129 built_actual_name != NULL,
6130 VAR_DOMAIN, LOC_TYPEDEF,
6131 &objfile->static_psymbols,
6132 0, (CORE_ADDR) 0, cu->language, objfile);
6133 break;
6134 case DW_TAG_namespace:
6135 add_psymbol_to_list (actual_name, strlen (actual_name),
6136 built_actual_name != NULL,
6137 VAR_DOMAIN, LOC_TYPEDEF,
6138 &objfile->global_psymbols,
6139 0, (CORE_ADDR) 0, cu->language, objfile);
6140 break;
6141 case DW_TAG_class_type:
6142 case DW_TAG_interface_type:
6143 case DW_TAG_structure_type:
6144 case DW_TAG_union_type:
6145 case DW_TAG_enumeration_type:
6146 /* Skip external references. The DWARF standard says in the section
6147 about "Structure, Union, and Class Type Entries": "An incomplete
6148 structure, union or class type is represented by a structure,
6149 union or class entry that does not have a byte size attribute
6150 and that has a DW_AT_declaration attribute." */
6151 if (!pdi->has_byte_size && pdi->is_declaration)
6152 {
6153 xfree (built_actual_name);
6154 return;
6155 }
6156
6157 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6158 static vs. global. */
6159 add_psymbol_to_list (actual_name, strlen (actual_name),
6160 built_actual_name != NULL,
6161 STRUCT_DOMAIN, LOC_TYPEDEF,
6162 (cu->language == language_cplus
6163 || cu->language == language_java)
6164 ? &objfile->global_psymbols
6165 : &objfile->static_psymbols,
6166 0, (CORE_ADDR) 0, cu->language, objfile);
6167
6168 break;
6169 case DW_TAG_enumerator:
6170 add_psymbol_to_list (actual_name, strlen (actual_name),
6171 built_actual_name != NULL,
6172 VAR_DOMAIN, LOC_CONST,
6173 (cu->language == language_cplus
6174 || cu->language == language_java)
6175 ? &objfile->global_psymbols
6176 : &objfile->static_psymbols,
6177 0, (CORE_ADDR) 0, cu->language, objfile);
6178 break;
6179 default:
6180 break;
6181 }
6182
6183 xfree (built_actual_name);
6184 }
6185
6186 /* Read a partial die corresponding to a namespace; also, add a symbol
6187 corresponding to that namespace to the symbol table. NAMESPACE is
6188 the name of the enclosing namespace. */
6189
6190 static void
6191 add_partial_namespace (struct partial_die_info *pdi,
6192 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6193 int need_pc, struct dwarf2_cu *cu)
6194 {
6195 /* Add a symbol for the namespace. */
6196
6197 add_partial_symbol (pdi, cu);
6198
6199 /* Now scan partial symbols in that namespace. */
6200
6201 if (pdi->has_children)
6202 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6203 }
6204
6205 /* Read a partial die corresponding to a Fortran module. */
6206
6207 static void
6208 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6209 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6210 {
6211 /* Now scan partial symbols in that module. */
6212
6213 if (pdi->has_children)
6214 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6215 }
6216
6217 /* Read a partial die corresponding to a subprogram and create a partial
6218 symbol for that subprogram. When the CU language allows it, this
6219 routine also defines a partial symbol for each nested subprogram
6220 that this subprogram contains.
6221
6222 DIE my also be a lexical block, in which case we simply search
6223 recursively for suprograms defined inside that lexical block.
6224 Again, this is only performed when the CU language allows this
6225 type of definitions. */
6226
6227 static void
6228 add_partial_subprogram (struct partial_die_info *pdi,
6229 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6230 int need_pc, struct dwarf2_cu *cu)
6231 {
6232 if (pdi->tag == DW_TAG_subprogram)
6233 {
6234 if (pdi->has_pc_info)
6235 {
6236 if (pdi->lowpc < *lowpc)
6237 *lowpc = pdi->lowpc;
6238 if (pdi->highpc > *highpc)
6239 *highpc = pdi->highpc;
6240 if (need_pc)
6241 {
6242 CORE_ADDR baseaddr;
6243 struct objfile *objfile = cu->objfile;
6244
6245 baseaddr = ANOFFSET (objfile->section_offsets,
6246 SECT_OFF_TEXT (objfile));
6247 addrmap_set_empty (objfile->psymtabs_addrmap,
6248 pdi->lowpc + baseaddr,
6249 pdi->highpc - 1 + baseaddr,
6250 cu->per_cu->v.psymtab);
6251 }
6252 }
6253
6254 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6255 {
6256 if (!pdi->is_declaration)
6257 /* Ignore subprogram DIEs that do not have a name, they are
6258 illegal. Do not emit a complaint at this point, we will
6259 do so when we convert this psymtab into a symtab. */
6260 if (pdi->name)
6261 add_partial_symbol (pdi, cu);
6262 }
6263 }
6264
6265 if (! pdi->has_children)
6266 return;
6267
6268 if (cu->language == language_ada)
6269 {
6270 pdi = pdi->die_child;
6271 while (pdi != NULL)
6272 {
6273 fixup_partial_die (pdi, cu);
6274 if (pdi->tag == DW_TAG_subprogram
6275 || pdi->tag == DW_TAG_lexical_block)
6276 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6277 pdi = pdi->die_sibling;
6278 }
6279 }
6280 }
6281
6282 /* Read a partial die corresponding to an enumeration type. */
6283
6284 static void
6285 add_partial_enumeration (struct partial_die_info *enum_pdi,
6286 struct dwarf2_cu *cu)
6287 {
6288 struct partial_die_info *pdi;
6289
6290 if (enum_pdi->name != NULL)
6291 add_partial_symbol (enum_pdi, cu);
6292
6293 pdi = enum_pdi->die_child;
6294 while (pdi)
6295 {
6296 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6297 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6298 else
6299 add_partial_symbol (pdi, cu);
6300 pdi = pdi->die_sibling;
6301 }
6302 }
6303
6304 /* Return the initial uleb128 in the die at INFO_PTR. */
6305
6306 static unsigned int
6307 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6308 {
6309 unsigned int bytes_read;
6310
6311 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6312 }
6313
6314 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6315 Return the corresponding abbrev, or NULL if the number is zero (indicating
6316 an empty DIE). In either case *BYTES_READ will be set to the length of
6317 the initial number. */
6318
6319 static struct abbrev_info *
6320 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6321 struct dwarf2_cu *cu)
6322 {
6323 bfd *abfd = cu->objfile->obfd;
6324 unsigned int abbrev_number;
6325 struct abbrev_info *abbrev;
6326
6327 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6328
6329 if (abbrev_number == 0)
6330 return NULL;
6331
6332 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6333 if (!abbrev)
6334 {
6335 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6336 abbrev_number, bfd_get_filename (abfd));
6337 }
6338
6339 return abbrev;
6340 }
6341
6342 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6343 Returns a pointer to the end of a series of DIEs, terminated by an empty
6344 DIE. Any children of the skipped DIEs will also be skipped. */
6345
6346 static gdb_byte *
6347 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6348 {
6349 struct dwarf2_cu *cu = reader->cu;
6350 struct abbrev_info *abbrev;
6351 unsigned int bytes_read;
6352
6353 while (1)
6354 {
6355 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6356 if (abbrev == NULL)
6357 return info_ptr + bytes_read;
6358 else
6359 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6360 }
6361 }
6362
6363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6364 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6365 abbrev corresponding to that skipped uleb128 should be passed in
6366 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6367 children. */
6368
6369 static gdb_byte *
6370 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6371 struct abbrev_info *abbrev)
6372 {
6373 unsigned int bytes_read;
6374 struct attribute attr;
6375 bfd *abfd = reader->abfd;
6376 struct dwarf2_cu *cu = reader->cu;
6377 gdb_byte *buffer = reader->buffer;
6378 const gdb_byte *buffer_end = reader->buffer_end;
6379 gdb_byte *start_info_ptr = info_ptr;
6380 unsigned int form, i;
6381
6382 for (i = 0; i < abbrev->num_attrs; i++)
6383 {
6384 /* The only abbrev we care about is DW_AT_sibling. */
6385 if (abbrev->attrs[i].name == DW_AT_sibling)
6386 {
6387 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6388 if (attr.form == DW_FORM_ref_addr)
6389 complaint (&symfile_complaints,
6390 _("ignoring absolute DW_AT_sibling"));
6391 else
6392 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6393 }
6394
6395 /* If it isn't DW_AT_sibling, skip this attribute. */
6396 form = abbrev->attrs[i].form;
6397 skip_attribute:
6398 switch (form)
6399 {
6400 case DW_FORM_ref_addr:
6401 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6402 and later it is offset sized. */
6403 if (cu->header.version == 2)
6404 info_ptr += cu->header.addr_size;
6405 else
6406 info_ptr += cu->header.offset_size;
6407 break;
6408 case DW_FORM_GNU_ref_alt:
6409 info_ptr += cu->header.offset_size;
6410 break;
6411 case DW_FORM_addr:
6412 info_ptr += cu->header.addr_size;
6413 break;
6414 case DW_FORM_data1:
6415 case DW_FORM_ref1:
6416 case DW_FORM_flag:
6417 info_ptr += 1;
6418 break;
6419 case DW_FORM_flag_present:
6420 break;
6421 case DW_FORM_data2:
6422 case DW_FORM_ref2:
6423 info_ptr += 2;
6424 break;
6425 case DW_FORM_data4:
6426 case DW_FORM_ref4:
6427 info_ptr += 4;
6428 break;
6429 case DW_FORM_data8:
6430 case DW_FORM_ref8:
6431 case DW_FORM_ref_sig8:
6432 info_ptr += 8;
6433 break;
6434 case DW_FORM_string:
6435 read_direct_string (abfd, info_ptr, &bytes_read);
6436 info_ptr += bytes_read;
6437 break;
6438 case DW_FORM_sec_offset:
6439 case DW_FORM_strp:
6440 case DW_FORM_GNU_strp_alt:
6441 info_ptr += cu->header.offset_size;
6442 break;
6443 case DW_FORM_exprloc:
6444 case DW_FORM_block:
6445 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6446 info_ptr += bytes_read;
6447 break;
6448 case DW_FORM_block1:
6449 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6450 break;
6451 case DW_FORM_block2:
6452 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6453 break;
6454 case DW_FORM_block4:
6455 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6456 break;
6457 case DW_FORM_sdata:
6458 case DW_FORM_udata:
6459 case DW_FORM_ref_udata:
6460 case DW_FORM_GNU_addr_index:
6461 case DW_FORM_GNU_str_index:
6462 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6463 break;
6464 case DW_FORM_indirect:
6465 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6466 info_ptr += bytes_read;
6467 /* We need to continue parsing from here, so just go back to
6468 the top. */
6469 goto skip_attribute;
6470
6471 default:
6472 error (_("Dwarf Error: Cannot handle %s "
6473 "in DWARF reader [in module %s]"),
6474 dwarf_form_name (form),
6475 bfd_get_filename (abfd));
6476 }
6477 }
6478
6479 if (abbrev->has_children)
6480 return skip_children (reader, info_ptr);
6481 else
6482 return info_ptr;
6483 }
6484
6485 /* Locate ORIG_PDI's sibling.
6486 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6487
6488 static gdb_byte *
6489 locate_pdi_sibling (const struct die_reader_specs *reader,
6490 struct partial_die_info *orig_pdi,
6491 gdb_byte *info_ptr)
6492 {
6493 /* Do we know the sibling already? */
6494
6495 if (orig_pdi->sibling)
6496 return orig_pdi->sibling;
6497
6498 /* Are there any children to deal with? */
6499
6500 if (!orig_pdi->has_children)
6501 return info_ptr;
6502
6503 /* Skip the children the long way. */
6504
6505 return skip_children (reader, info_ptr);
6506 }
6507
6508 /* Expand this partial symbol table into a full symbol table. SELF is
6509 not NULL. */
6510
6511 static void
6512 dwarf2_read_symtab (struct partial_symtab *self,
6513 struct objfile *objfile)
6514 {
6515 if (self->readin)
6516 {
6517 warning (_("bug: psymtab for %s is already read in."),
6518 self->filename);
6519 }
6520 else
6521 {
6522 if (info_verbose)
6523 {
6524 printf_filtered (_("Reading in symbols for %s..."),
6525 self->filename);
6526 gdb_flush (gdb_stdout);
6527 }
6528
6529 /* Restore our global data. */
6530 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6531
6532 /* If this psymtab is constructed from a debug-only objfile, the
6533 has_section_at_zero flag will not necessarily be correct. We
6534 can get the correct value for this flag by looking at the data
6535 associated with the (presumably stripped) associated objfile. */
6536 if (objfile->separate_debug_objfile_backlink)
6537 {
6538 struct dwarf2_per_objfile *dpo_backlink
6539 = objfile_data (objfile->separate_debug_objfile_backlink,
6540 dwarf2_objfile_data_key);
6541
6542 dwarf2_per_objfile->has_section_at_zero
6543 = dpo_backlink->has_section_at_zero;
6544 }
6545
6546 dwarf2_per_objfile->reading_partial_symbols = 0;
6547
6548 psymtab_to_symtab_1 (self);
6549
6550 /* Finish up the debug error message. */
6551 if (info_verbose)
6552 printf_filtered (_("done.\n"));
6553 }
6554
6555 process_cu_includes ();
6556 }
6557 \f
6558 /* Reading in full CUs. */
6559
6560 /* Add PER_CU to the queue. */
6561
6562 static void
6563 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6564 enum language pretend_language)
6565 {
6566 struct dwarf2_queue_item *item;
6567
6568 per_cu->queued = 1;
6569 item = xmalloc (sizeof (*item));
6570 item->per_cu = per_cu;
6571 item->pretend_language = pretend_language;
6572 item->next = NULL;
6573
6574 if (dwarf2_queue == NULL)
6575 dwarf2_queue = item;
6576 else
6577 dwarf2_queue_tail->next = item;
6578
6579 dwarf2_queue_tail = item;
6580 }
6581
6582 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6583 unit and add it to our queue.
6584 The result is non-zero if PER_CU was queued, otherwise the result is zero
6585 meaning either PER_CU is already queued or it is already loaded. */
6586
6587 static int
6588 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6589 struct dwarf2_per_cu_data *per_cu,
6590 enum language pretend_language)
6591 {
6592 /* We may arrive here during partial symbol reading, if we need full
6593 DIEs to process an unusual case (e.g. template arguments). Do
6594 not queue PER_CU, just tell our caller to load its DIEs. */
6595 if (dwarf2_per_objfile->reading_partial_symbols)
6596 {
6597 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6598 return 1;
6599 return 0;
6600 }
6601
6602 /* Mark the dependence relation so that we don't flush PER_CU
6603 too early. */
6604 dwarf2_add_dependence (this_cu, per_cu);
6605
6606 /* If it's already on the queue, we have nothing to do. */
6607 if (per_cu->queued)
6608 return 0;
6609
6610 /* If the compilation unit is already loaded, just mark it as
6611 used. */
6612 if (per_cu->cu != NULL)
6613 {
6614 per_cu->cu->last_used = 0;
6615 return 0;
6616 }
6617
6618 /* Add it to the queue. */
6619 queue_comp_unit (per_cu, pretend_language);
6620
6621 return 1;
6622 }
6623
6624 /* Process the queue. */
6625
6626 static void
6627 process_queue (void)
6628 {
6629 struct dwarf2_queue_item *item, *next_item;
6630
6631 if (dwarf2_read_debug)
6632 {
6633 fprintf_unfiltered (gdb_stdlog,
6634 "Expanding one or more symtabs of objfile %s ...\n",
6635 dwarf2_per_objfile->objfile->name);
6636 }
6637
6638 /* The queue starts out with one item, but following a DIE reference
6639 may load a new CU, adding it to the end of the queue. */
6640 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6641 {
6642 if (dwarf2_per_objfile->using_index
6643 ? !item->per_cu->v.quick->symtab
6644 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6645 {
6646 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6647
6648 if (dwarf2_read_debug)
6649 {
6650 fprintf_unfiltered (gdb_stdlog,
6651 "Expanding symtab of %s at offset 0x%x\n",
6652 per_cu->is_debug_types ? "TU" : "CU",
6653 per_cu->offset.sect_off);
6654 }
6655
6656 if (per_cu->is_debug_types)
6657 process_full_type_unit (per_cu, item->pretend_language);
6658 else
6659 process_full_comp_unit (per_cu, item->pretend_language);
6660
6661 if (dwarf2_read_debug)
6662 {
6663 fprintf_unfiltered (gdb_stdlog,
6664 "Done expanding %s at offset 0x%x\n",
6665 per_cu->is_debug_types ? "TU" : "CU",
6666 per_cu->offset.sect_off);
6667 }
6668 }
6669
6670 item->per_cu->queued = 0;
6671 next_item = item->next;
6672 xfree (item);
6673 }
6674
6675 dwarf2_queue_tail = NULL;
6676
6677 if (dwarf2_read_debug)
6678 {
6679 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6680 dwarf2_per_objfile->objfile->name);
6681 }
6682 }
6683
6684 /* Free all allocated queue entries. This function only releases anything if
6685 an error was thrown; if the queue was processed then it would have been
6686 freed as we went along. */
6687
6688 static void
6689 dwarf2_release_queue (void *dummy)
6690 {
6691 struct dwarf2_queue_item *item, *last;
6692
6693 item = dwarf2_queue;
6694 while (item)
6695 {
6696 /* Anything still marked queued is likely to be in an
6697 inconsistent state, so discard it. */
6698 if (item->per_cu->queued)
6699 {
6700 if (item->per_cu->cu != NULL)
6701 free_one_cached_comp_unit (item->per_cu);
6702 item->per_cu->queued = 0;
6703 }
6704
6705 last = item;
6706 item = item->next;
6707 xfree (last);
6708 }
6709
6710 dwarf2_queue = dwarf2_queue_tail = NULL;
6711 }
6712
6713 /* Read in full symbols for PST, and anything it depends on. */
6714
6715 static void
6716 psymtab_to_symtab_1 (struct partial_symtab *pst)
6717 {
6718 struct dwarf2_per_cu_data *per_cu;
6719 int i;
6720
6721 if (pst->readin)
6722 return;
6723
6724 for (i = 0; i < pst->number_of_dependencies; i++)
6725 if (!pst->dependencies[i]->readin
6726 && pst->dependencies[i]->user == NULL)
6727 {
6728 /* Inform about additional files that need to be read in. */
6729 if (info_verbose)
6730 {
6731 /* FIXME: i18n: Need to make this a single string. */
6732 fputs_filtered (" ", gdb_stdout);
6733 wrap_here ("");
6734 fputs_filtered ("and ", gdb_stdout);
6735 wrap_here ("");
6736 printf_filtered ("%s...", pst->dependencies[i]->filename);
6737 wrap_here (""); /* Flush output. */
6738 gdb_flush (gdb_stdout);
6739 }
6740 psymtab_to_symtab_1 (pst->dependencies[i]);
6741 }
6742
6743 per_cu = pst->read_symtab_private;
6744
6745 if (per_cu == NULL)
6746 {
6747 /* It's an include file, no symbols to read for it.
6748 Everything is in the parent symtab. */
6749 pst->readin = 1;
6750 return;
6751 }
6752
6753 dw2_do_instantiate_symtab (per_cu);
6754 }
6755
6756 /* Trivial hash function for die_info: the hash value of a DIE
6757 is its offset in .debug_info for this objfile. */
6758
6759 static hashval_t
6760 die_hash (const void *item)
6761 {
6762 const struct die_info *die = item;
6763
6764 return die->offset.sect_off;
6765 }
6766
6767 /* Trivial comparison function for die_info structures: two DIEs
6768 are equal if they have the same offset. */
6769
6770 static int
6771 die_eq (const void *item_lhs, const void *item_rhs)
6772 {
6773 const struct die_info *die_lhs = item_lhs;
6774 const struct die_info *die_rhs = item_rhs;
6775
6776 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6777 }
6778
6779 /* die_reader_func for load_full_comp_unit.
6780 This is identical to read_signatured_type_reader,
6781 but is kept separate for now. */
6782
6783 static void
6784 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6785 gdb_byte *info_ptr,
6786 struct die_info *comp_unit_die,
6787 int has_children,
6788 void *data)
6789 {
6790 struct dwarf2_cu *cu = reader->cu;
6791 enum language *language_ptr = data;
6792
6793 gdb_assert (cu->die_hash == NULL);
6794 cu->die_hash =
6795 htab_create_alloc_ex (cu->header.length / 12,
6796 die_hash,
6797 die_eq,
6798 NULL,
6799 &cu->comp_unit_obstack,
6800 hashtab_obstack_allocate,
6801 dummy_obstack_deallocate);
6802
6803 if (has_children)
6804 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6805 &info_ptr, comp_unit_die);
6806 cu->dies = comp_unit_die;
6807 /* comp_unit_die is not stored in die_hash, no need. */
6808
6809 /* We try not to read any attributes in this function, because not
6810 all CUs needed for references have been loaded yet, and symbol
6811 table processing isn't initialized. But we have to set the CU language,
6812 or we won't be able to build types correctly.
6813 Similarly, if we do not read the producer, we can not apply
6814 producer-specific interpretation. */
6815 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6816 }
6817
6818 /* Load the DIEs associated with PER_CU into memory. */
6819
6820 static void
6821 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6822 enum language pretend_language)
6823 {
6824 gdb_assert (! this_cu->is_debug_types);
6825
6826 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6827 load_full_comp_unit_reader, &pretend_language);
6828 }
6829
6830 /* Add a DIE to the delayed physname list. */
6831
6832 static void
6833 add_to_method_list (struct type *type, int fnfield_index, int index,
6834 const char *name, struct die_info *die,
6835 struct dwarf2_cu *cu)
6836 {
6837 struct delayed_method_info mi;
6838 mi.type = type;
6839 mi.fnfield_index = fnfield_index;
6840 mi.index = index;
6841 mi.name = name;
6842 mi.die = die;
6843 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6844 }
6845
6846 /* A cleanup for freeing the delayed method list. */
6847
6848 static void
6849 free_delayed_list (void *ptr)
6850 {
6851 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6852 if (cu->method_list != NULL)
6853 {
6854 VEC_free (delayed_method_info, cu->method_list);
6855 cu->method_list = NULL;
6856 }
6857 }
6858
6859 /* Compute the physnames of any methods on the CU's method list.
6860
6861 The computation of method physnames is delayed in order to avoid the
6862 (bad) condition that one of the method's formal parameters is of an as yet
6863 incomplete type. */
6864
6865 static void
6866 compute_delayed_physnames (struct dwarf2_cu *cu)
6867 {
6868 int i;
6869 struct delayed_method_info *mi;
6870 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6871 {
6872 const char *physname;
6873 struct fn_fieldlist *fn_flp
6874 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6875 physname = dwarf2_physname (mi->name, mi->die, cu);
6876 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6877 }
6878 }
6879
6880 /* Go objects should be embedded in a DW_TAG_module DIE,
6881 and it's not clear if/how imported objects will appear.
6882 To keep Go support simple until that's worked out,
6883 go back through what we've read and create something usable.
6884 We could do this while processing each DIE, and feels kinda cleaner,
6885 but that way is more invasive.
6886 This is to, for example, allow the user to type "p var" or "b main"
6887 without having to specify the package name, and allow lookups
6888 of module.object to work in contexts that use the expression
6889 parser. */
6890
6891 static void
6892 fixup_go_packaging (struct dwarf2_cu *cu)
6893 {
6894 char *package_name = NULL;
6895 struct pending *list;
6896 int i;
6897
6898 for (list = global_symbols; list != NULL; list = list->next)
6899 {
6900 for (i = 0; i < list->nsyms; ++i)
6901 {
6902 struct symbol *sym = list->symbol[i];
6903
6904 if (SYMBOL_LANGUAGE (sym) == language_go
6905 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6906 {
6907 char *this_package_name = go_symbol_package_name (sym);
6908
6909 if (this_package_name == NULL)
6910 continue;
6911 if (package_name == NULL)
6912 package_name = this_package_name;
6913 else
6914 {
6915 if (strcmp (package_name, this_package_name) != 0)
6916 complaint (&symfile_complaints,
6917 _("Symtab %s has objects from two different Go packages: %s and %s"),
6918 (SYMBOL_SYMTAB (sym)
6919 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6920 : cu->objfile->name),
6921 this_package_name, package_name);
6922 xfree (this_package_name);
6923 }
6924 }
6925 }
6926 }
6927
6928 if (package_name != NULL)
6929 {
6930 struct objfile *objfile = cu->objfile;
6931 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6932 package_name,
6933 strlen (package_name));
6934 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6935 saved_package_name, objfile);
6936 struct symbol *sym;
6937
6938 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6939
6940 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6941 SYMBOL_SET_LANGUAGE (sym, language_go);
6942 SYMBOL_SET_NAMES (sym, saved_package_name,
6943 strlen (saved_package_name), 0, objfile);
6944 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6945 e.g., "main" finds the "main" module and not C's main(). */
6946 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6947 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6948 SYMBOL_TYPE (sym) = type;
6949
6950 add_symbol_to_list (sym, &global_symbols);
6951
6952 xfree (package_name);
6953 }
6954 }
6955
6956 /* Return the symtab for PER_CU. This works properly regardless of
6957 whether we're using the index or psymtabs. */
6958
6959 static struct symtab *
6960 get_symtab (struct dwarf2_per_cu_data *per_cu)
6961 {
6962 return (dwarf2_per_objfile->using_index
6963 ? per_cu->v.quick->symtab
6964 : per_cu->v.psymtab->symtab);
6965 }
6966
6967 /* A helper function for computing the list of all symbol tables
6968 included by PER_CU. */
6969
6970 static void
6971 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6972 htab_t all_children,
6973 struct dwarf2_per_cu_data *per_cu)
6974 {
6975 void **slot;
6976 int ix;
6977 struct dwarf2_per_cu_data *iter;
6978
6979 slot = htab_find_slot (all_children, per_cu, INSERT);
6980 if (*slot != NULL)
6981 {
6982 /* This inclusion and its children have been processed. */
6983 return;
6984 }
6985
6986 *slot = per_cu;
6987 /* Only add a CU if it has a symbol table. */
6988 if (get_symtab (per_cu) != NULL)
6989 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6990
6991 for (ix = 0;
6992 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6993 ++ix)
6994 recursively_compute_inclusions (result, all_children, iter);
6995 }
6996
6997 /* Compute the symtab 'includes' fields for the symtab related to
6998 PER_CU. */
6999
7000 static void
7001 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7002 {
7003 gdb_assert (! per_cu->is_debug_types);
7004
7005 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7006 {
7007 int ix, len;
7008 struct dwarf2_per_cu_data *iter;
7009 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7010 htab_t all_children;
7011 struct symtab *symtab = get_symtab (per_cu);
7012
7013 /* If we don't have a symtab, we can just skip this case. */
7014 if (symtab == NULL)
7015 return;
7016
7017 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7018 NULL, xcalloc, xfree);
7019
7020 for (ix = 0;
7021 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7022 ix, iter);
7023 ++ix)
7024 recursively_compute_inclusions (&result_children, all_children, iter);
7025
7026 /* Now we have a transitive closure of all the included CUs, and
7027 for .gdb_index version 7 the included TUs, so we can convert it
7028 to a list of symtabs. */
7029 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7030 symtab->includes
7031 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7032 (len + 1) * sizeof (struct symtab *));
7033 for (ix = 0;
7034 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7035 ++ix)
7036 symtab->includes[ix] = get_symtab (iter);
7037 symtab->includes[len] = NULL;
7038
7039 VEC_free (dwarf2_per_cu_ptr, result_children);
7040 htab_delete (all_children);
7041 }
7042 }
7043
7044 /* Compute the 'includes' field for the symtabs of all the CUs we just
7045 read. */
7046
7047 static void
7048 process_cu_includes (void)
7049 {
7050 int ix;
7051 struct dwarf2_per_cu_data *iter;
7052
7053 for (ix = 0;
7054 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7055 ix, iter);
7056 ++ix)
7057 {
7058 if (! iter->is_debug_types)
7059 compute_symtab_includes (iter);
7060 }
7061
7062 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7063 }
7064
7065 /* Generate full symbol information for PER_CU, whose DIEs have
7066 already been loaded into memory. */
7067
7068 static void
7069 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7070 enum language pretend_language)
7071 {
7072 struct dwarf2_cu *cu = per_cu->cu;
7073 struct objfile *objfile = per_cu->objfile;
7074 CORE_ADDR lowpc, highpc;
7075 struct symtab *symtab;
7076 struct cleanup *back_to, *delayed_list_cleanup;
7077 CORE_ADDR baseaddr;
7078 struct block *static_block;
7079
7080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7081
7082 buildsym_init ();
7083 back_to = make_cleanup (really_free_pendings, NULL);
7084 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7085
7086 cu->list_in_scope = &file_symbols;
7087
7088 cu->language = pretend_language;
7089 cu->language_defn = language_def (cu->language);
7090
7091 /* Do line number decoding in read_file_scope () */
7092 process_die (cu->dies, cu);
7093
7094 /* For now fudge the Go package. */
7095 if (cu->language == language_go)
7096 fixup_go_packaging (cu);
7097
7098 /* Now that we have processed all the DIEs in the CU, all the types
7099 should be complete, and it should now be safe to compute all of the
7100 physnames. */
7101 compute_delayed_physnames (cu);
7102 do_cleanups (delayed_list_cleanup);
7103
7104 /* Some compilers don't define a DW_AT_high_pc attribute for the
7105 compilation unit. If the DW_AT_high_pc is missing, synthesize
7106 it, by scanning the DIE's below the compilation unit. */
7107 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7108
7109 static_block
7110 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7111 per_cu->imported_symtabs != NULL);
7112
7113 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7114 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7115 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7116 addrmap to help ensure it has an accurate map of pc values belonging to
7117 this comp unit. */
7118 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7119
7120 symtab = end_symtab_from_static_block (static_block, objfile,
7121 SECT_OFF_TEXT (objfile), 0);
7122
7123 if (symtab != NULL)
7124 {
7125 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7126
7127 /* Set symtab language to language from DW_AT_language. If the
7128 compilation is from a C file generated by language preprocessors, do
7129 not set the language if it was already deduced by start_subfile. */
7130 if (!(cu->language == language_c && symtab->language != language_c))
7131 symtab->language = cu->language;
7132
7133 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7134 produce DW_AT_location with location lists but it can be possibly
7135 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7136 there were bugs in prologue debug info, fixed later in GCC-4.5
7137 by "unwind info for epilogues" patch (which is not directly related).
7138
7139 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7140 needed, it would be wrong due to missing DW_AT_producer there.
7141
7142 Still one can confuse GDB by using non-standard GCC compilation
7143 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7144 */
7145 if (cu->has_loclist && gcc_4_minor >= 5)
7146 symtab->locations_valid = 1;
7147
7148 if (gcc_4_minor >= 5)
7149 symtab->epilogue_unwind_valid = 1;
7150
7151 symtab->call_site_htab = cu->call_site_htab;
7152 }
7153
7154 if (dwarf2_per_objfile->using_index)
7155 per_cu->v.quick->symtab = symtab;
7156 else
7157 {
7158 struct partial_symtab *pst = per_cu->v.psymtab;
7159 pst->symtab = symtab;
7160 pst->readin = 1;
7161 }
7162
7163 /* Push it for inclusion processing later. */
7164 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7165
7166 do_cleanups (back_to);
7167 }
7168
7169 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7170 already been loaded into memory. */
7171
7172 static void
7173 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7174 enum language pretend_language)
7175 {
7176 struct dwarf2_cu *cu = per_cu->cu;
7177 struct objfile *objfile = per_cu->objfile;
7178 struct symtab *symtab;
7179 struct cleanup *back_to, *delayed_list_cleanup;
7180 struct signatured_type *sig_type;
7181
7182 gdb_assert (per_cu->is_debug_types);
7183 sig_type = (struct signatured_type *) per_cu;
7184
7185 buildsym_init ();
7186 back_to = make_cleanup (really_free_pendings, NULL);
7187 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7188
7189 cu->list_in_scope = &file_symbols;
7190
7191 cu->language = pretend_language;
7192 cu->language_defn = language_def (cu->language);
7193
7194 /* The symbol tables are set up in read_type_unit_scope. */
7195 process_die (cu->dies, cu);
7196
7197 /* For now fudge the Go package. */
7198 if (cu->language == language_go)
7199 fixup_go_packaging (cu);
7200
7201 /* Now that we have processed all the DIEs in the CU, all the types
7202 should be complete, and it should now be safe to compute all of the
7203 physnames. */
7204 compute_delayed_physnames (cu);
7205 do_cleanups (delayed_list_cleanup);
7206
7207 /* TUs share symbol tables.
7208 If this is the first TU to use this symtab, complete the construction
7209 of it with end_expandable_symtab. Otherwise, complete the addition of
7210 this TU's symbols to the existing symtab. */
7211 if (sig_type->type_unit_group->primary_symtab == NULL)
7212 {
7213 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7214 sig_type->type_unit_group->primary_symtab = symtab;
7215
7216 if (symtab != NULL)
7217 {
7218 /* Set symtab language to language from DW_AT_language. If the
7219 compilation is from a C file generated by language preprocessors,
7220 do not set the language if it was already deduced by
7221 start_subfile. */
7222 if (!(cu->language == language_c && symtab->language != language_c))
7223 symtab->language = cu->language;
7224 }
7225 }
7226 else
7227 {
7228 augment_type_symtab (objfile,
7229 sig_type->type_unit_group->primary_symtab);
7230 symtab = sig_type->type_unit_group->primary_symtab;
7231 }
7232
7233 if (dwarf2_per_objfile->using_index)
7234 per_cu->v.quick->symtab = symtab;
7235 else
7236 {
7237 struct partial_symtab *pst = per_cu->v.psymtab;
7238 pst->symtab = symtab;
7239 pst->readin = 1;
7240 }
7241
7242 do_cleanups (back_to);
7243 }
7244
7245 /* Process an imported unit DIE. */
7246
7247 static void
7248 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7249 {
7250 struct attribute *attr;
7251
7252 /* For now we don't handle imported units in type units. */
7253 if (cu->per_cu->is_debug_types)
7254 {
7255 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7256 " supported in type units [in module %s]"),
7257 cu->objfile->name);
7258 }
7259
7260 attr = dwarf2_attr (die, DW_AT_import, cu);
7261 if (attr != NULL)
7262 {
7263 struct dwarf2_per_cu_data *per_cu;
7264 struct symtab *imported_symtab;
7265 sect_offset offset;
7266 int is_dwz;
7267
7268 offset = dwarf2_get_ref_die_offset (attr);
7269 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7270 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7271
7272 /* Queue the unit, if needed. */
7273 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7274 load_full_comp_unit (per_cu, cu->language);
7275
7276 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7277 per_cu);
7278 }
7279 }
7280
7281 /* Process a die and its children. */
7282
7283 static void
7284 process_die (struct die_info *die, struct dwarf2_cu *cu)
7285 {
7286 switch (die->tag)
7287 {
7288 case DW_TAG_padding:
7289 break;
7290 case DW_TAG_compile_unit:
7291 case DW_TAG_partial_unit:
7292 read_file_scope (die, cu);
7293 break;
7294 case DW_TAG_type_unit:
7295 read_type_unit_scope (die, cu);
7296 break;
7297 case DW_TAG_subprogram:
7298 case DW_TAG_inlined_subroutine:
7299 read_func_scope (die, cu);
7300 break;
7301 case DW_TAG_lexical_block:
7302 case DW_TAG_try_block:
7303 case DW_TAG_catch_block:
7304 read_lexical_block_scope (die, cu);
7305 break;
7306 case DW_TAG_GNU_call_site:
7307 read_call_site_scope (die, cu);
7308 break;
7309 case DW_TAG_class_type:
7310 case DW_TAG_interface_type:
7311 case DW_TAG_structure_type:
7312 case DW_TAG_union_type:
7313 process_structure_scope (die, cu);
7314 break;
7315 case DW_TAG_enumeration_type:
7316 process_enumeration_scope (die, cu);
7317 break;
7318
7319 /* These dies have a type, but processing them does not create
7320 a symbol or recurse to process the children. Therefore we can
7321 read them on-demand through read_type_die. */
7322 case DW_TAG_subroutine_type:
7323 case DW_TAG_set_type:
7324 case DW_TAG_array_type:
7325 case DW_TAG_pointer_type:
7326 case DW_TAG_ptr_to_member_type:
7327 case DW_TAG_reference_type:
7328 case DW_TAG_string_type:
7329 break;
7330
7331 case DW_TAG_base_type:
7332 case DW_TAG_subrange_type:
7333 case DW_TAG_typedef:
7334 /* Add a typedef symbol for the type definition, if it has a
7335 DW_AT_name. */
7336 new_symbol (die, read_type_die (die, cu), cu);
7337 break;
7338 case DW_TAG_common_block:
7339 read_common_block (die, cu);
7340 break;
7341 case DW_TAG_common_inclusion:
7342 break;
7343 case DW_TAG_namespace:
7344 cu->processing_has_namespace_info = 1;
7345 read_namespace (die, cu);
7346 break;
7347 case DW_TAG_module:
7348 cu->processing_has_namespace_info = 1;
7349 read_module (die, cu);
7350 break;
7351 case DW_TAG_imported_declaration:
7352 case DW_TAG_imported_module:
7353 cu->processing_has_namespace_info = 1;
7354 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7355 || cu->language != language_fortran))
7356 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7357 dwarf_tag_name (die->tag));
7358 read_import_statement (die, cu);
7359 break;
7360
7361 case DW_TAG_imported_unit:
7362 process_imported_unit_die (die, cu);
7363 break;
7364
7365 default:
7366 new_symbol (die, NULL, cu);
7367 break;
7368 }
7369 }
7370 \f
7371 /* DWARF name computation. */
7372
7373 /* A helper function for dwarf2_compute_name which determines whether DIE
7374 needs to have the name of the scope prepended to the name listed in the
7375 die. */
7376
7377 static int
7378 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7379 {
7380 struct attribute *attr;
7381
7382 switch (die->tag)
7383 {
7384 case DW_TAG_namespace:
7385 case DW_TAG_typedef:
7386 case DW_TAG_class_type:
7387 case DW_TAG_interface_type:
7388 case DW_TAG_structure_type:
7389 case DW_TAG_union_type:
7390 case DW_TAG_enumeration_type:
7391 case DW_TAG_enumerator:
7392 case DW_TAG_subprogram:
7393 case DW_TAG_member:
7394 return 1;
7395
7396 case DW_TAG_variable:
7397 case DW_TAG_constant:
7398 /* We only need to prefix "globally" visible variables. These include
7399 any variable marked with DW_AT_external or any variable that
7400 lives in a namespace. [Variables in anonymous namespaces
7401 require prefixing, but they are not DW_AT_external.] */
7402
7403 if (dwarf2_attr (die, DW_AT_specification, cu))
7404 {
7405 struct dwarf2_cu *spec_cu = cu;
7406
7407 return die_needs_namespace (die_specification (die, &spec_cu),
7408 spec_cu);
7409 }
7410
7411 attr = dwarf2_attr (die, DW_AT_external, cu);
7412 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7413 && die->parent->tag != DW_TAG_module)
7414 return 0;
7415 /* A variable in a lexical block of some kind does not need a
7416 namespace, even though in C++ such variables may be external
7417 and have a mangled name. */
7418 if (die->parent->tag == DW_TAG_lexical_block
7419 || die->parent->tag == DW_TAG_try_block
7420 || die->parent->tag == DW_TAG_catch_block
7421 || die->parent->tag == DW_TAG_subprogram)
7422 return 0;
7423 return 1;
7424
7425 default:
7426 return 0;
7427 }
7428 }
7429
7430 /* Retrieve the last character from a mem_file. */
7431
7432 static void
7433 do_ui_file_peek_last (void *object, const char *buffer, long length)
7434 {
7435 char *last_char_p = (char *) object;
7436
7437 if (length > 0)
7438 *last_char_p = buffer[length - 1];
7439 }
7440
7441 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7442 compute the physname for the object, which include a method's:
7443 - formal parameters (C++/Java),
7444 - receiver type (Go),
7445 - return type (Java).
7446
7447 The term "physname" is a bit confusing.
7448 For C++, for example, it is the demangled name.
7449 For Go, for example, it's the mangled name.
7450
7451 For Ada, return the DIE's linkage name rather than the fully qualified
7452 name. PHYSNAME is ignored..
7453
7454 The result is allocated on the objfile_obstack and canonicalized. */
7455
7456 static const char *
7457 dwarf2_compute_name (const char *name,
7458 struct die_info *die, struct dwarf2_cu *cu,
7459 int physname)
7460 {
7461 struct objfile *objfile = cu->objfile;
7462
7463 if (name == NULL)
7464 name = dwarf2_name (die, cu);
7465
7466 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7467 compute it by typename_concat inside GDB. */
7468 if (cu->language == language_ada
7469 || (cu->language == language_fortran && physname))
7470 {
7471 /* For Ada unit, we prefer the linkage name over the name, as
7472 the former contains the exported name, which the user expects
7473 to be able to reference. Ideally, we want the user to be able
7474 to reference this entity using either natural or linkage name,
7475 but we haven't started looking at this enhancement yet. */
7476 struct attribute *attr;
7477
7478 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7479 if (attr == NULL)
7480 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7481 if (attr && DW_STRING (attr))
7482 return DW_STRING (attr);
7483 }
7484
7485 /* These are the only languages we know how to qualify names in. */
7486 if (name != NULL
7487 && (cu->language == language_cplus || cu->language == language_java
7488 || cu->language == language_fortran))
7489 {
7490 if (die_needs_namespace (die, cu))
7491 {
7492 long length;
7493 const char *prefix;
7494 struct ui_file *buf;
7495
7496 prefix = determine_prefix (die, cu);
7497 buf = mem_fileopen ();
7498 if (*prefix != '\0')
7499 {
7500 char *prefixed_name = typename_concat (NULL, prefix, name,
7501 physname, cu);
7502
7503 fputs_unfiltered (prefixed_name, buf);
7504 xfree (prefixed_name);
7505 }
7506 else
7507 fputs_unfiltered (name, buf);
7508
7509 /* Template parameters may be specified in the DIE's DW_AT_name, or
7510 as children with DW_TAG_template_type_param or
7511 DW_TAG_value_type_param. If the latter, add them to the name
7512 here. If the name already has template parameters, then
7513 skip this step; some versions of GCC emit both, and
7514 it is more efficient to use the pre-computed name.
7515
7516 Something to keep in mind about this process: it is very
7517 unlikely, or in some cases downright impossible, to produce
7518 something that will match the mangled name of a function.
7519 If the definition of the function has the same debug info,
7520 we should be able to match up with it anyway. But fallbacks
7521 using the minimal symbol, for instance to find a method
7522 implemented in a stripped copy of libstdc++, will not work.
7523 If we do not have debug info for the definition, we will have to
7524 match them up some other way.
7525
7526 When we do name matching there is a related problem with function
7527 templates; two instantiated function templates are allowed to
7528 differ only by their return types, which we do not add here. */
7529
7530 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7531 {
7532 struct attribute *attr;
7533 struct die_info *child;
7534 int first = 1;
7535
7536 die->building_fullname = 1;
7537
7538 for (child = die->child; child != NULL; child = child->sibling)
7539 {
7540 struct type *type;
7541 LONGEST value;
7542 gdb_byte *bytes;
7543 struct dwarf2_locexpr_baton *baton;
7544 struct value *v;
7545
7546 if (child->tag != DW_TAG_template_type_param
7547 && child->tag != DW_TAG_template_value_param)
7548 continue;
7549
7550 if (first)
7551 {
7552 fputs_unfiltered ("<", buf);
7553 first = 0;
7554 }
7555 else
7556 fputs_unfiltered (", ", buf);
7557
7558 attr = dwarf2_attr (child, DW_AT_type, cu);
7559 if (attr == NULL)
7560 {
7561 complaint (&symfile_complaints,
7562 _("template parameter missing DW_AT_type"));
7563 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7564 continue;
7565 }
7566 type = die_type (child, cu);
7567
7568 if (child->tag == DW_TAG_template_type_param)
7569 {
7570 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7571 continue;
7572 }
7573
7574 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7575 if (attr == NULL)
7576 {
7577 complaint (&symfile_complaints,
7578 _("template parameter missing "
7579 "DW_AT_const_value"));
7580 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7581 continue;
7582 }
7583
7584 dwarf2_const_value_attr (attr, type, name,
7585 &cu->comp_unit_obstack, cu,
7586 &value, &bytes, &baton);
7587
7588 if (TYPE_NOSIGN (type))
7589 /* GDB prints characters as NUMBER 'CHAR'. If that's
7590 changed, this can use value_print instead. */
7591 c_printchar (value, type, buf);
7592 else
7593 {
7594 struct value_print_options opts;
7595
7596 if (baton != NULL)
7597 v = dwarf2_evaluate_loc_desc (type, NULL,
7598 baton->data,
7599 baton->size,
7600 baton->per_cu);
7601 else if (bytes != NULL)
7602 {
7603 v = allocate_value (type);
7604 memcpy (value_contents_writeable (v), bytes,
7605 TYPE_LENGTH (type));
7606 }
7607 else
7608 v = value_from_longest (type, value);
7609
7610 /* Specify decimal so that we do not depend on
7611 the radix. */
7612 get_formatted_print_options (&opts, 'd');
7613 opts.raw = 1;
7614 value_print (v, buf, &opts);
7615 release_value (v);
7616 value_free (v);
7617 }
7618 }
7619
7620 die->building_fullname = 0;
7621
7622 if (!first)
7623 {
7624 /* Close the argument list, with a space if necessary
7625 (nested templates). */
7626 char last_char = '\0';
7627 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7628 if (last_char == '>')
7629 fputs_unfiltered (" >", buf);
7630 else
7631 fputs_unfiltered (">", buf);
7632 }
7633 }
7634
7635 /* For Java and C++ methods, append formal parameter type
7636 information, if PHYSNAME. */
7637
7638 if (physname && die->tag == DW_TAG_subprogram
7639 && (cu->language == language_cplus
7640 || cu->language == language_java))
7641 {
7642 struct type *type = read_type_die (die, cu);
7643
7644 c_type_print_args (type, buf, 1, cu->language,
7645 &type_print_raw_options);
7646
7647 if (cu->language == language_java)
7648 {
7649 /* For java, we must append the return type to method
7650 names. */
7651 if (die->tag == DW_TAG_subprogram)
7652 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7653 0, 0, &type_print_raw_options);
7654 }
7655 else if (cu->language == language_cplus)
7656 {
7657 /* Assume that an artificial first parameter is
7658 "this", but do not crash if it is not. RealView
7659 marks unnamed (and thus unused) parameters as
7660 artificial; there is no way to differentiate
7661 the two cases. */
7662 if (TYPE_NFIELDS (type) > 0
7663 && TYPE_FIELD_ARTIFICIAL (type, 0)
7664 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7665 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7666 0))))
7667 fputs_unfiltered (" const", buf);
7668 }
7669 }
7670
7671 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7672 &length);
7673 ui_file_delete (buf);
7674
7675 if (cu->language == language_cplus)
7676 {
7677 const char *cname
7678 = dwarf2_canonicalize_name (name, cu,
7679 &objfile->objfile_obstack);
7680
7681 if (cname != NULL)
7682 name = cname;
7683 }
7684 }
7685 }
7686
7687 return name;
7688 }
7689
7690 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7691 If scope qualifiers are appropriate they will be added. The result
7692 will be allocated on the objfile_obstack, or NULL if the DIE does
7693 not have a name. NAME may either be from a previous call to
7694 dwarf2_name or NULL.
7695
7696 The output string will be canonicalized (if C++/Java). */
7697
7698 static const char *
7699 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7700 {
7701 return dwarf2_compute_name (name, die, cu, 0);
7702 }
7703
7704 /* Construct a physname for the given DIE in CU. NAME may either be
7705 from a previous call to dwarf2_name or NULL. The result will be
7706 allocated on the objfile_objstack or NULL if the DIE does not have a
7707 name.
7708
7709 The output string will be canonicalized (if C++/Java). */
7710
7711 static const char *
7712 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7713 {
7714 struct objfile *objfile = cu->objfile;
7715 struct attribute *attr;
7716 const char *retval, *mangled = NULL, *canon = NULL;
7717 struct cleanup *back_to;
7718 int need_copy = 1;
7719
7720 /* In this case dwarf2_compute_name is just a shortcut not building anything
7721 on its own. */
7722 if (!die_needs_namespace (die, cu))
7723 return dwarf2_compute_name (name, die, cu, 1);
7724
7725 back_to = make_cleanup (null_cleanup, NULL);
7726
7727 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7728 if (!attr)
7729 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7730
7731 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7732 has computed. */
7733 if (attr && DW_STRING (attr))
7734 {
7735 char *demangled;
7736
7737 mangled = DW_STRING (attr);
7738
7739 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7740 type. It is easier for GDB users to search for such functions as
7741 `name(params)' than `long name(params)'. In such case the minimal
7742 symbol names do not match the full symbol names but for template
7743 functions there is never a need to look up their definition from their
7744 declaration so the only disadvantage remains the minimal symbol
7745 variant `long name(params)' does not have the proper inferior type.
7746 */
7747
7748 if (cu->language == language_go)
7749 {
7750 /* This is a lie, but we already lie to the caller new_symbol_full.
7751 new_symbol_full assumes we return the mangled name.
7752 This just undoes that lie until things are cleaned up. */
7753 demangled = NULL;
7754 }
7755 else
7756 {
7757 demangled = cplus_demangle (mangled,
7758 (DMGL_PARAMS | DMGL_ANSI
7759 | (cu->language == language_java
7760 ? DMGL_JAVA | DMGL_RET_POSTFIX
7761 : DMGL_RET_DROP)));
7762 }
7763 if (demangled)
7764 {
7765 make_cleanup (xfree, demangled);
7766 canon = demangled;
7767 }
7768 else
7769 {
7770 canon = mangled;
7771 need_copy = 0;
7772 }
7773 }
7774
7775 if (canon == NULL || check_physname)
7776 {
7777 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7778
7779 if (canon != NULL && strcmp (physname, canon) != 0)
7780 {
7781 /* It may not mean a bug in GDB. The compiler could also
7782 compute DW_AT_linkage_name incorrectly. But in such case
7783 GDB would need to be bug-to-bug compatible. */
7784
7785 complaint (&symfile_complaints,
7786 _("Computed physname <%s> does not match demangled <%s> "
7787 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7788 physname, canon, mangled, die->offset.sect_off, objfile->name);
7789
7790 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7791 is available here - over computed PHYSNAME. It is safer
7792 against both buggy GDB and buggy compilers. */
7793
7794 retval = canon;
7795 }
7796 else
7797 {
7798 retval = physname;
7799 need_copy = 0;
7800 }
7801 }
7802 else
7803 retval = canon;
7804
7805 if (need_copy)
7806 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7807
7808 do_cleanups (back_to);
7809 return retval;
7810 }
7811
7812 /* Read the import statement specified by the given die and record it. */
7813
7814 static void
7815 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7816 {
7817 struct objfile *objfile = cu->objfile;
7818 struct attribute *import_attr;
7819 struct die_info *imported_die, *child_die;
7820 struct dwarf2_cu *imported_cu;
7821 const char *imported_name;
7822 const char *imported_name_prefix;
7823 const char *canonical_name;
7824 const char *import_alias;
7825 const char *imported_declaration = NULL;
7826 const char *import_prefix;
7827 VEC (const_char_ptr) *excludes = NULL;
7828 struct cleanup *cleanups;
7829
7830 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7831 if (import_attr == NULL)
7832 {
7833 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7834 dwarf_tag_name (die->tag));
7835 return;
7836 }
7837
7838 imported_cu = cu;
7839 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7840 imported_name = dwarf2_name (imported_die, imported_cu);
7841 if (imported_name == NULL)
7842 {
7843 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7844
7845 The import in the following code:
7846 namespace A
7847 {
7848 typedef int B;
7849 }
7850
7851 int main ()
7852 {
7853 using A::B;
7854 B b;
7855 return b;
7856 }
7857
7858 ...
7859 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7860 <52> DW_AT_decl_file : 1
7861 <53> DW_AT_decl_line : 6
7862 <54> DW_AT_import : <0x75>
7863 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7864 <59> DW_AT_name : B
7865 <5b> DW_AT_decl_file : 1
7866 <5c> DW_AT_decl_line : 2
7867 <5d> DW_AT_type : <0x6e>
7868 ...
7869 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7870 <76> DW_AT_byte_size : 4
7871 <77> DW_AT_encoding : 5 (signed)
7872
7873 imports the wrong die ( 0x75 instead of 0x58 ).
7874 This case will be ignored until the gcc bug is fixed. */
7875 return;
7876 }
7877
7878 /* Figure out the local name after import. */
7879 import_alias = dwarf2_name (die, cu);
7880
7881 /* Figure out where the statement is being imported to. */
7882 import_prefix = determine_prefix (die, cu);
7883
7884 /* Figure out what the scope of the imported die is and prepend it
7885 to the name of the imported die. */
7886 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7887
7888 if (imported_die->tag != DW_TAG_namespace
7889 && imported_die->tag != DW_TAG_module)
7890 {
7891 imported_declaration = imported_name;
7892 canonical_name = imported_name_prefix;
7893 }
7894 else if (strlen (imported_name_prefix) > 0)
7895 canonical_name = obconcat (&objfile->objfile_obstack,
7896 imported_name_prefix, "::", imported_name,
7897 (char *) NULL);
7898 else
7899 canonical_name = imported_name;
7900
7901 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7902
7903 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7904 for (child_die = die->child; child_die && child_die->tag;
7905 child_die = sibling_die (child_die))
7906 {
7907 /* DWARF-4: A Fortran use statement with a “rename list” may be
7908 represented by an imported module entry with an import attribute
7909 referring to the module and owned entries corresponding to those
7910 entities that are renamed as part of being imported. */
7911
7912 if (child_die->tag != DW_TAG_imported_declaration)
7913 {
7914 complaint (&symfile_complaints,
7915 _("child DW_TAG_imported_declaration expected "
7916 "- DIE at 0x%x [in module %s]"),
7917 child_die->offset.sect_off, objfile->name);
7918 continue;
7919 }
7920
7921 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7922 if (import_attr == NULL)
7923 {
7924 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7925 dwarf_tag_name (child_die->tag));
7926 continue;
7927 }
7928
7929 imported_cu = cu;
7930 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7931 &imported_cu);
7932 imported_name = dwarf2_name (imported_die, imported_cu);
7933 if (imported_name == NULL)
7934 {
7935 complaint (&symfile_complaints,
7936 _("child DW_TAG_imported_declaration has unknown "
7937 "imported name - DIE at 0x%x [in module %s]"),
7938 child_die->offset.sect_off, objfile->name);
7939 continue;
7940 }
7941
7942 VEC_safe_push (const_char_ptr, excludes, imported_name);
7943
7944 process_die (child_die, cu);
7945 }
7946
7947 cp_add_using_directive (import_prefix,
7948 canonical_name,
7949 import_alias,
7950 imported_declaration,
7951 excludes,
7952 0,
7953 &objfile->objfile_obstack);
7954
7955 do_cleanups (cleanups);
7956 }
7957
7958 /* Cleanup function for handle_DW_AT_stmt_list. */
7959
7960 static void
7961 free_cu_line_header (void *arg)
7962 {
7963 struct dwarf2_cu *cu = arg;
7964
7965 free_line_header (cu->line_header);
7966 cu->line_header = NULL;
7967 }
7968
7969 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7970 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7971 this, it was first present in GCC release 4.3.0. */
7972
7973 static int
7974 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7975 {
7976 if (!cu->checked_producer)
7977 check_producer (cu);
7978
7979 return cu->producer_is_gcc_lt_4_3;
7980 }
7981
7982 static void
7983 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7984 const char **name, const char **comp_dir)
7985 {
7986 struct attribute *attr;
7987
7988 *name = NULL;
7989 *comp_dir = NULL;
7990
7991 /* Find the filename. Do not use dwarf2_name here, since the filename
7992 is not a source language identifier. */
7993 attr = dwarf2_attr (die, DW_AT_name, cu);
7994 if (attr)
7995 {
7996 *name = DW_STRING (attr);
7997 }
7998
7999 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8000 if (attr)
8001 *comp_dir = DW_STRING (attr);
8002 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8003 && IS_ABSOLUTE_PATH (*name))
8004 {
8005 char *d = ldirname (*name);
8006
8007 *comp_dir = d;
8008 if (d != NULL)
8009 make_cleanup (xfree, d);
8010 }
8011 if (*comp_dir != NULL)
8012 {
8013 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8014 directory, get rid of it. */
8015 char *cp = strchr (*comp_dir, ':');
8016
8017 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8018 *comp_dir = cp + 1;
8019 }
8020
8021 if (*name == NULL)
8022 *name = "<unknown>";
8023 }
8024
8025 /* Handle DW_AT_stmt_list for a compilation unit.
8026 DIE is the DW_TAG_compile_unit die for CU.
8027 COMP_DIR is the compilation directory.
8028 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8029
8030 static void
8031 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8032 const char *comp_dir)
8033 {
8034 struct attribute *attr;
8035
8036 gdb_assert (! cu->per_cu->is_debug_types);
8037
8038 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8039 if (attr)
8040 {
8041 unsigned int line_offset = DW_UNSND (attr);
8042 struct line_header *line_header
8043 = dwarf_decode_line_header (line_offset, cu);
8044
8045 if (line_header)
8046 {
8047 cu->line_header = line_header;
8048 make_cleanup (free_cu_line_header, cu);
8049 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8050 }
8051 }
8052 }
8053
8054 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8055
8056 static void
8057 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8058 {
8059 struct objfile *objfile = dwarf2_per_objfile->objfile;
8060 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8061 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8062 CORE_ADDR highpc = ((CORE_ADDR) 0);
8063 struct attribute *attr;
8064 const char *name = NULL;
8065 const char *comp_dir = NULL;
8066 struct die_info *child_die;
8067 bfd *abfd = objfile->obfd;
8068 CORE_ADDR baseaddr;
8069
8070 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8071
8072 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8073
8074 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8075 from finish_block. */
8076 if (lowpc == ((CORE_ADDR) -1))
8077 lowpc = highpc;
8078 lowpc += baseaddr;
8079 highpc += baseaddr;
8080
8081 find_file_and_directory (die, cu, &name, &comp_dir);
8082
8083 prepare_one_comp_unit (cu, die, cu->language);
8084
8085 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8086 standardised yet. As a workaround for the language detection we fall
8087 back to the DW_AT_producer string. */
8088 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8089 cu->language = language_opencl;
8090
8091 /* Similar hack for Go. */
8092 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8093 set_cu_language (DW_LANG_Go, cu);
8094
8095 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8096
8097 /* Decode line number information if present. We do this before
8098 processing child DIEs, so that the line header table is available
8099 for DW_AT_decl_file. */
8100 handle_DW_AT_stmt_list (die, cu, comp_dir);
8101
8102 /* Process all dies in compilation unit. */
8103 if (die->child != NULL)
8104 {
8105 child_die = die->child;
8106 while (child_die && child_die->tag)
8107 {
8108 process_die (child_die, cu);
8109 child_die = sibling_die (child_die);
8110 }
8111 }
8112
8113 /* Decode macro information, if present. Dwarf 2 macro information
8114 refers to information in the line number info statement program
8115 header, so we can only read it if we've read the header
8116 successfully. */
8117 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8118 if (attr && cu->line_header)
8119 {
8120 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8121 complaint (&symfile_complaints,
8122 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8123
8124 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8125 }
8126 else
8127 {
8128 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8129 if (attr && cu->line_header)
8130 {
8131 unsigned int macro_offset = DW_UNSND (attr);
8132
8133 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8134 }
8135 }
8136
8137 do_cleanups (back_to);
8138 }
8139
8140 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8141 Create the set of symtabs used by this TU, or if this TU is sharing
8142 symtabs with another TU and the symtabs have already been created
8143 then restore those symtabs in the line header.
8144 We don't need the pc/line-number mapping for type units. */
8145
8146 static void
8147 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8148 {
8149 struct objfile *objfile = dwarf2_per_objfile->objfile;
8150 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8151 struct type_unit_group *tu_group;
8152 int first_time;
8153 struct line_header *lh;
8154 struct attribute *attr;
8155 unsigned int i, line_offset;
8156 struct signatured_type *sig_type;
8157
8158 gdb_assert (per_cu->is_debug_types);
8159 sig_type = (struct signatured_type *) per_cu;
8160
8161 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8162
8163 /* If we're using .gdb_index (includes -readnow) then
8164 per_cu->s.type_unit_group may not have been set up yet. */
8165 if (sig_type->type_unit_group == NULL)
8166 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8167 tu_group = sig_type->type_unit_group;
8168
8169 /* If we've already processed this stmt_list there's no real need to
8170 do it again, we could fake it and just recreate the part we need
8171 (file name,index -> symtab mapping). If data shows this optimization
8172 is useful we can do it then. */
8173 first_time = tu_group->primary_symtab == NULL;
8174
8175 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8176 debug info. */
8177 lh = NULL;
8178 if (attr != NULL)
8179 {
8180 line_offset = DW_UNSND (attr);
8181 lh = dwarf_decode_line_header (line_offset, cu);
8182 }
8183 if (lh == NULL)
8184 {
8185 if (first_time)
8186 dwarf2_start_symtab (cu, "", NULL, 0);
8187 else
8188 {
8189 gdb_assert (tu_group->symtabs == NULL);
8190 restart_symtab (0);
8191 }
8192 /* Note: The primary symtab will get allocated at the end. */
8193 return;
8194 }
8195
8196 cu->line_header = lh;
8197 make_cleanup (free_cu_line_header, cu);
8198
8199 if (first_time)
8200 {
8201 dwarf2_start_symtab (cu, "", NULL, 0);
8202
8203 tu_group->num_symtabs = lh->num_file_names;
8204 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8205
8206 for (i = 0; i < lh->num_file_names; ++i)
8207 {
8208 char *dir = NULL;
8209 struct file_entry *fe = &lh->file_names[i];
8210
8211 if (fe->dir_index)
8212 dir = lh->include_dirs[fe->dir_index - 1];
8213 dwarf2_start_subfile (fe->name, dir, NULL);
8214
8215 /* Note: We don't have to watch for the main subfile here, type units
8216 don't have DW_AT_name. */
8217
8218 if (current_subfile->symtab == NULL)
8219 {
8220 /* NOTE: start_subfile will recognize when it's been passed
8221 a file it has already seen. So we can't assume there's a
8222 simple mapping from lh->file_names to subfiles,
8223 lh->file_names may contain dups. */
8224 current_subfile->symtab = allocate_symtab (current_subfile->name,
8225 objfile);
8226 }
8227
8228 fe->symtab = current_subfile->symtab;
8229 tu_group->symtabs[i] = fe->symtab;
8230 }
8231 }
8232 else
8233 {
8234 restart_symtab (0);
8235
8236 for (i = 0; i < lh->num_file_names; ++i)
8237 {
8238 struct file_entry *fe = &lh->file_names[i];
8239
8240 fe->symtab = tu_group->symtabs[i];
8241 }
8242 }
8243
8244 /* The main symtab is allocated last. Type units don't have DW_AT_name
8245 so they don't have a "real" (so to speak) symtab anyway.
8246 There is later code that will assign the main symtab to all symbols
8247 that don't have one. We need to handle the case of a symbol with a
8248 missing symtab (DW_AT_decl_file) anyway. */
8249 }
8250
8251 /* Process DW_TAG_type_unit.
8252 For TUs we want to skip the first top level sibling if it's not the
8253 actual type being defined by this TU. In this case the first top
8254 level sibling is there to provide context only. */
8255
8256 static void
8257 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8258 {
8259 struct die_info *child_die;
8260
8261 prepare_one_comp_unit (cu, die, language_minimal);
8262
8263 /* Initialize (or reinitialize) the machinery for building symtabs.
8264 We do this before processing child DIEs, so that the line header table
8265 is available for DW_AT_decl_file. */
8266 setup_type_unit_groups (die, cu);
8267
8268 if (die->child != NULL)
8269 {
8270 child_die = die->child;
8271 while (child_die && child_die->tag)
8272 {
8273 process_die (child_die, cu);
8274 child_die = sibling_die (child_die);
8275 }
8276 }
8277 }
8278 \f
8279 /* DWO/DWP files.
8280
8281 http://gcc.gnu.org/wiki/DebugFission
8282 http://gcc.gnu.org/wiki/DebugFissionDWP
8283
8284 To simplify handling of both DWO files ("object" files with the DWARF info)
8285 and DWP files (a file with the DWOs packaged up into one file), we treat
8286 DWP files as having a collection of virtual DWO files. */
8287
8288 static hashval_t
8289 hash_dwo_file (const void *item)
8290 {
8291 const struct dwo_file *dwo_file = item;
8292
8293 return htab_hash_string (dwo_file->name);
8294 }
8295
8296 static int
8297 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8298 {
8299 const struct dwo_file *lhs = item_lhs;
8300 const struct dwo_file *rhs = item_rhs;
8301
8302 return strcmp (lhs->name, rhs->name) == 0;
8303 }
8304
8305 /* Allocate a hash table for DWO files. */
8306
8307 static htab_t
8308 allocate_dwo_file_hash_table (void)
8309 {
8310 struct objfile *objfile = dwarf2_per_objfile->objfile;
8311
8312 return htab_create_alloc_ex (41,
8313 hash_dwo_file,
8314 eq_dwo_file,
8315 NULL,
8316 &objfile->objfile_obstack,
8317 hashtab_obstack_allocate,
8318 dummy_obstack_deallocate);
8319 }
8320
8321 /* Lookup DWO file DWO_NAME. */
8322
8323 static void **
8324 lookup_dwo_file_slot (const char *dwo_name)
8325 {
8326 struct dwo_file find_entry;
8327 void **slot;
8328
8329 if (dwarf2_per_objfile->dwo_files == NULL)
8330 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8331
8332 memset (&find_entry, 0, sizeof (find_entry));
8333 find_entry.name = dwo_name;
8334 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8335
8336 return slot;
8337 }
8338
8339 static hashval_t
8340 hash_dwo_unit (const void *item)
8341 {
8342 const struct dwo_unit *dwo_unit = item;
8343
8344 /* This drops the top 32 bits of the id, but is ok for a hash. */
8345 return dwo_unit->signature;
8346 }
8347
8348 static int
8349 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8350 {
8351 const struct dwo_unit *lhs = item_lhs;
8352 const struct dwo_unit *rhs = item_rhs;
8353
8354 /* The signature is assumed to be unique within the DWO file.
8355 So while object file CU dwo_id's always have the value zero,
8356 that's OK, assuming each object file DWO file has only one CU,
8357 and that's the rule for now. */
8358 return lhs->signature == rhs->signature;
8359 }
8360
8361 /* Allocate a hash table for DWO CUs,TUs.
8362 There is one of these tables for each of CUs,TUs for each DWO file. */
8363
8364 static htab_t
8365 allocate_dwo_unit_table (struct objfile *objfile)
8366 {
8367 /* Start out with a pretty small number.
8368 Generally DWO files contain only one CU and maybe some TUs. */
8369 return htab_create_alloc_ex (3,
8370 hash_dwo_unit,
8371 eq_dwo_unit,
8372 NULL,
8373 &objfile->objfile_obstack,
8374 hashtab_obstack_allocate,
8375 dummy_obstack_deallocate);
8376 }
8377
8378 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8379
8380 struct create_dwo_info_table_data
8381 {
8382 struct dwo_file *dwo_file;
8383 htab_t cu_htab;
8384 };
8385
8386 /* die_reader_func for create_dwo_debug_info_hash_table. */
8387
8388 static void
8389 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8390 gdb_byte *info_ptr,
8391 struct die_info *comp_unit_die,
8392 int has_children,
8393 void *datap)
8394 {
8395 struct dwarf2_cu *cu = reader->cu;
8396 struct objfile *objfile = dwarf2_per_objfile->objfile;
8397 sect_offset offset = cu->per_cu->offset;
8398 struct dwarf2_section_info *section = cu->per_cu->section;
8399 struct create_dwo_info_table_data *data = datap;
8400 struct dwo_file *dwo_file = data->dwo_file;
8401 htab_t cu_htab = data->cu_htab;
8402 void **slot;
8403 struct attribute *attr;
8404 struct dwo_unit *dwo_unit;
8405
8406 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8407 if (attr == NULL)
8408 {
8409 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8410 " its dwo_id [in module %s]"),
8411 offset.sect_off, dwo_file->name);
8412 return;
8413 }
8414
8415 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8416 dwo_unit->dwo_file = dwo_file;
8417 dwo_unit->signature = DW_UNSND (attr);
8418 dwo_unit->section = section;
8419 dwo_unit->offset = offset;
8420 dwo_unit->length = cu->per_cu->length;
8421
8422 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8423 gdb_assert (slot != NULL);
8424 if (*slot != NULL)
8425 {
8426 const struct dwo_unit *dup_dwo_unit = *slot;
8427
8428 complaint (&symfile_complaints,
8429 _("debug entry at offset 0x%x is duplicate to the entry at"
8430 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8431 offset.sect_off, dup_dwo_unit->offset.sect_off,
8432 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8433 dwo_file->name);
8434 }
8435 else
8436 *slot = dwo_unit;
8437
8438 if (dwarf2_read_debug)
8439 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8440 offset.sect_off,
8441 phex (dwo_unit->signature,
8442 sizeof (dwo_unit->signature)));
8443 }
8444
8445 /* Create a hash table to map DWO IDs to their CU entry in
8446 .debug_info.dwo in DWO_FILE.
8447 Note: This function processes DWO files only, not DWP files.
8448 Note: A DWO file generally contains one CU, but we don't assume this. */
8449
8450 static htab_t
8451 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8452 {
8453 struct objfile *objfile = dwarf2_per_objfile->objfile;
8454 struct dwarf2_section_info *section = &dwo_file->sections.info;
8455 bfd *abfd;
8456 htab_t cu_htab;
8457 gdb_byte *info_ptr, *end_ptr;
8458 struct create_dwo_info_table_data create_dwo_info_table_data;
8459
8460 dwarf2_read_section (objfile, section);
8461 info_ptr = section->buffer;
8462
8463 if (info_ptr == NULL)
8464 return NULL;
8465
8466 /* We can't set abfd until now because the section may be empty or
8467 not present, in which case section->asection will be NULL. */
8468 abfd = section->asection->owner;
8469
8470 if (dwarf2_read_debug)
8471 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8472 bfd_get_filename (abfd));
8473
8474 cu_htab = allocate_dwo_unit_table (objfile);
8475
8476 create_dwo_info_table_data.dwo_file = dwo_file;
8477 create_dwo_info_table_data.cu_htab = cu_htab;
8478
8479 end_ptr = info_ptr + section->size;
8480 while (info_ptr < end_ptr)
8481 {
8482 struct dwarf2_per_cu_data per_cu;
8483
8484 memset (&per_cu, 0, sizeof (per_cu));
8485 per_cu.objfile = objfile;
8486 per_cu.is_debug_types = 0;
8487 per_cu.offset.sect_off = info_ptr - section->buffer;
8488 per_cu.section = section;
8489
8490 init_cutu_and_read_dies_no_follow (&per_cu,
8491 &dwo_file->sections.abbrev,
8492 dwo_file,
8493 create_dwo_debug_info_hash_table_reader,
8494 &create_dwo_info_table_data);
8495
8496 info_ptr += per_cu.length;
8497 }
8498
8499 return cu_htab;
8500 }
8501
8502 /* DWP file .debug_{cu,tu}_index section format:
8503 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8504
8505 Both index sections have the same format, and serve to map a 64-bit
8506 signature to a set of section numbers. Each section begins with a header,
8507 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8508 indexes, and a pool of 32-bit section numbers. The index sections will be
8509 aligned at 8-byte boundaries in the file.
8510
8511 The index section header contains two unsigned 32-bit values (using the
8512 byte order of the application binary):
8513
8514 N, the number of compilation units or type units in the index
8515 M, the number of slots in the hash table
8516
8517 (We assume that N and M will not exceed 2^32 - 1.)
8518
8519 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8520
8521 The hash table begins at offset 8 in the section, and consists of an array
8522 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8523 order of the application binary). Unused slots in the hash table are 0.
8524 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8525
8526 The parallel table begins immediately after the hash table
8527 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8528 array of 32-bit indexes (using the byte order of the application binary),
8529 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8530 table contains a 32-bit index into the pool of section numbers. For unused
8531 hash table slots, the corresponding entry in the parallel table will be 0.
8532
8533 Given a 64-bit compilation unit signature or a type signature S, an entry
8534 in the hash table is located as follows:
8535
8536 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8537 the low-order k bits all set to 1.
8538
8539 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8540
8541 3) If the hash table entry at index H matches the signature, use that
8542 entry. If the hash table entry at index H is unused (all zeroes),
8543 terminate the search: the signature is not present in the table.
8544
8545 4) Let H = (H + H') modulo M. Repeat at Step 3.
8546
8547 Because M > N and H' and M are relatively prime, the search is guaranteed
8548 to stop at an unused slot or find the match.
8549
8550 The pool of section numbers begins immediately following the hash table
8551 (at offset 8 + 12 * M from the beginning of the section). The pool of
8552 section numbers consists of an array of 32-bit words (using the byte order
8553 of the application binary). Each item in the array is indexed starting
8554 from 0. The hash table entry provides the index of the first section
8555 number in the set. Additional section numbers in the set follow, and the
8556 set is terminated by a 0 entry (section number 0 is not used in ELF).
8557
8558 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8559 section must be the first entry in the set, and the .debug_abbrev.dwo must
8560 be the second entry. Other members of the set may follow in any order. */
8561
8562 /* Create a hash table to map DWO IDs to their CU/TU entry in
8563 .debug_{info,types}.dwo in DWP_FILE.
8564 Returns NULL if there isn't one.
8565 Note: This function processes DWP files only, not DWO files. */
8566
8567 static struct dwp_hash_table *
8568 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8569 {
8570 struct objfile *objfile = dwarf2_per_objfile->objfile;
8571 bfd *dbfd = dwp_file->dbfd;
8572 char *index_ptr, *index_end;
8573 struct dwarf2_section_info *index;
8574 uint32_t version, nr_units, nr_slots;
8575 struct dwp_hash_table *htab;
8576
8577 if (is_debug_types)
8578 index = &dwp_file->sections.tu_index;
8579 else
8580 index = &dwp_file->sections.cu_index;
8581
8582 if (dwarf2_section_empty_p (index))
8583 return NULL;
8584 dwarf2_read_section (objfile, index);
8585
8586 index_ptr = index->buffer;
8587 index_end = index_ptr + index->size;
8588
8589 version = read_4_bytes (dbfd, index_ptr);
8590 index_ptr += 8; /* Skip the unused word. */
8591 nr_units = read_4_bytes (dbfd, index_ptr);
8592 index_ptr += 4;
8593 nr_slots = read_4_bytes (dbfd, index_ptr);
8594 index_ptr += 4;
8595
8596 if (version != 1)
8597 {
8598 error (_("Dwarf Error: unsupported DWP file version (%u)"
8599 " [in module %s]"),
8600 version, dwp_file->name);
8601 }
8602 if (nr_slots != (nr_slots & -nr_slots))
8603 {
8604 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8605 " is not power of 2 [in module %s]"),
8606 nr_slots, dwp_file->name);
8607 }
8608
8609 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8610 htab->nr_units = nr_units;
8611 htab->nr_slots = nr_slots;
8612 htab->hash_table = index_ptr;
8613 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8614 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8615
8616 return htab;
8617 }
8618
8619 /* Update SECTIONS with the data from SECTP.
8620
8621 This function is like the other "locate" section routines that are
8622 passed to bfd_map_over_sections, but in this context the sections to
8623 read comes from the DWP hash table, not the full ELF section table.
8624
8625 The result is non-zero for success, or zero if an error was found. */
8626
8627 static int
8628 locate_virtual_dwo_sections (asection *sectp,
8629 struct virtual_dwo_sections *sections)
8630 {
8631 const struct dwop_section_names *names = &dwop_section_names;
8632
8633 if (section_is_p (sectp->name, &names->abbrev_dwo))
8634 {
8635 /* There can be only one. */
8636 if (sections->abbrev.asection != NULL)
8637 return 0;
8638 sections->abbrev.asection = sectp;
8639 sections->abbrev.size = bfd_get_section_size (sectp);
8640 }
8641 else if (section_is_p (sectp->name, &names->info_dwo)
8642 || section_is_p (sectp->name, &names->types_dwo))
8643 {
8644 /* There can be only one. */
8645 if (sections->info_or_types.asection != NULL)
8646 return 0;
8647 sections->info_or_types.asection = sectp;
8648 sections->info_or_types.size = bfd_get_section_size (sectp);
8649 }
8650 else if (section_is_p (sectp->name, &names->line_dwo))
8651 {
8652 /* There can be only one. */
8653 if (sections->line.asection != NULL)
8654 return 0;
8655 sections->line.asection = sectp;
8656 sections->line.size = bfd_get_section_size (sectp);
8657 }
8658 else if (section_is_p (sectp->name, &names->loc_dwo))
8659 {
8660 /* There can be only one. */
8661 if (sections->loc.asection != NULL)
8662 return 0;
8663 sections->loc.asection = sectp;
8664 sections->loc.size = bfd_get_section_size (sectp);
8665 }
8666 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8667 {
8668 /* There can be only one. */
8669 if (sections->macinfo.asection != NULL)
8670 return 0;
8671 sections->macinfo.asection = sectp;
8672 sections->macinfo.size = bfd_get_section_size (sectp);
8673 }
8674 else if (section_is_p (sectp->name, &names->macro_dwo))
8675 {
8676 /* There can be only one. */
8677 if (sections->macro.asection != NULL)
8678 return 0;
8679 sections->macro.asection = sectp;
8680 sections->macro.size = bfd_get_section_size (sectp);
8681 }
8682 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8683 {
8684 /* There can be only one. */
8685 if (sections->str_offsets.asection != NULL)
8686 return 0;
8687 sections->str_offsets.asection = sectp;
8688 sections->str_offsets.size = bfd_get_section_size (sectp);
8689 }
8690 else
8691 {
8692 /* No other kind of section is valid. */
8693 return 0;
8694 }
8695
8696 return 1;
8697 }
8698
8699 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8700 HTAB is the hash table from the DWP file.
8701 SECTION_INDEX is the index of the DWO in HTAB. */
8702
8703 static struct dwo_unit *
8704 create_dwo_in_dwp (struct dwp_file *dwp_file,
8705 const struct dwp_hash_table *htab,
8706 uint32_t section_index,
8707 ULONGEST signature, int is_debug_types)
8708 {
8709 struct objfile *objfile = dwarf2_per_objfile->objfile;
8710 bfd *dbfd = dwp_file->dbfd;
8711 const char *kind = is_debug_types ? "TU" : "CU";
8712 struct dwo_file *dwo_file;
8713 struct dwo_unit *dwo_unit;
8714 struct virtual_dwo_sections sections;
8715 void **dwo_file_slot;
8716 char *virtual_dwo_name;
8717 struct dwarf2_section_info *cutu;
8718 struct cleanup *cleanups;
8719 int i;
8720
8721 if (dwarf2_read_debug)
8722 {
8723 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8724 kind,
8725 section_index, phex (signature, sizeof (signature)),
8726 dwp_file->name);
8727 }
8728
8729 /* Fetch the sections of this DWO.
8730 Put a limit on the number of sections we look for so that bad data
8731 doesn't cause us to loop forever. */
8732
8733 #define MAX_NR_DWO_SECTIONS \
8734 (1 /* .debug_info or .debug_types */ \
8735 + 1 /* .debug_abbrev */ \
8736 + 1 /* .debug_line */ \
8737 + 1 /* .debug_loc */ \
8738 + 1 /* .debug_str_offsets */ \
8739 + 1 /* .debug_macro */ \
8740 + 1 /* .debug_macinfo */ \
8741 + 1 /* trailing zero */)
8742
8743 memset (&sections, 0, sizeof (sections));
8744 cleanups = make_cleanup (null_cleanup, 0);
8745
8746 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8747 {
8748 asection *sectp;
8749 uint32_t section_nr =
8750 read_4_bytes (dbfd,
8751 htab->section_pool
8752 + (section_index + i) * sizeof (uint32_t));
8753
8754 if (section_nr == 0)
8755 break;
8756 if (section_nr >= dwp_file->num_sections)
8757 {
8758 error (_("Dwarf Error: bad DWP hash table, section number too large"
8759 " [in module %s]"),
8760 dwp_file->name);
8761 }
8762
8763 sectp = dwp_file->elf_sections[section_nr];
8764 if (! locate_virtual_dwo_sections (sectp, &sections))
8765 {
8766 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8767 " [in module %s]"),
8768 dwp_file->name);
8769 }
8770 }
8771
8772 if (i < 2
8773 || sections.info_or_types.asection == NULL
8774 || sections.abbrev.asection == NULL)
8775 {
8776 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8777 " [in module %s]"),
8778 dwp_file->name);
8779 }
8780 if (i == MAX_NR_DWO_SECTIONS)
8781 {
8782 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8783 " [in module %s]"),
8784 dwp_file->name);
8785 }
8786
8787 /* It's easier for the rest of the code if we fake a struct dwo_file and
8788 have dwo_unit "live" in that. At least for now.
8789
8790 The DWP file can be made up of a random collection of CUs and TUs.
8791 However, for each CU + set of TUs that came from the same original DWO
8792 file, we want to combine them back into a virtual DWO file to save space
8793 (fewer struct dwo_file objects to allocated). Remember that for really
8794 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8795
8796 virtual_dwo_name =
8797 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8798 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8799 sections.line.asection ? sections.line.asection->id : 0,
8800 sections.loc.asection ? sections.loc.asection->id : 0,
8801 (sections.str_offsets.asection
8802 ? sections.str_offsets.asection->id
8803 : 0));
8804 make_cleanup (xfree, virtual_dwo_name);
8805 /* Can we use an existing virtual DWO file? */
8806 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8807 /* Create one if necessary. */
8808 if (*dwo_file_slot == NULL)
8809 {
8810 if (dwarf2_read_debug)
8811 {
8812 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8813 virtual_dwo_name);
8814 }
8815 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8816 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8817 virtual_dwo_name,
8818 strlen (virtual_dwo_name));
8819 dwo_file->sections.abbrev = sections.abbrev;
8820 dwo_file->sections.line = sections.line;
8821 dwo_file->sections.loc = sections.loc;
8822 dwo_file->sections.macinfo = sections.macinfo;
8823 dwo_file->sections.macro = sections.macro;
8824 dwo_file->sections.str_offsets = sections.str_offsets;
8825 /* The "str" section is global to the entire DWP file. */
8826 dwo_file->sections.str = dwp_file->sections.str;
8827 /* The info or types section is assigned later to dwo_unit,
8828 there's no need to record it in dwo_file.
8829 Also, we can't simply record type sections in dwo_file because
8830 we record a pointer into the vector in dwo_unit. As we collect more
8831 types we'll grow the vector and eventually have to reallocate space
8832 for it, invalidating all the pointers into the current copy. */
8833 *dwo_file_slot = dwo_file;
8834 }
8835 else
8836 {
8837 if (dwarf2_read_debug)
8838 {
8839 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8840 virtual_dwo_name);
8841 }
8842 dwo_file = *dwo_file_slot;
8843 }
8844 do_cleanups (cleanups);
8845
8846 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8847 dwo_unit->dwo_file = dwo_file;
8848 dwo_unit->signature = signature;
8849 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8850 sizeof (struct dwarf2_section_info));
8851 *dwo_unit->section = sections.info_or_types;
8852 /* offset, length, type_offset_in_tu are set later. */
8853
8854 return dwo_unit;
8855 }
8856
8857 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8858
8859 static struct dwo_unit *
8860 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8861 const struct dwp_hash_table *htab,
8862 ULONGEST signature, int is_debug_types)
8863 {
8864 bfd *dbfd = dwp_file->dbfd;
8865 uint32_t mask = htab->nr_slots - 1;
8866 uint32_t hash = signature & mask;
8867 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8868 unsigned int i;
8869 void **slot;
8870 struct dwo_unit find_dwo_cu, *dwo_cu;
8871
8872 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8873 find_dwo_cu.signature = signature;
8874 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8875
8876 if (*slot != NULL)
8877 return *slot;
8878
8879 /* Use a for loop so that we don't loop forever on bad debug info. */
8880 for (i = 0; i < htab->nr_slots; ++i)
8881 {
8882 ULONGEST signature_in_table;
8883
8884 signature_in_table =
8885 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8886 if (signature_in_table == signature)
8887 {
8888 uint32_t section_index =
8889 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8890
8891 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8892 signature, is_debug_types);
8893 return *slot;
8894 }
8895 if (signature_in_table == 0)
8896 return NULL;
8897 hash = (hash + hash2) & mask;
8898 }
8899
8900 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8901 " [in module %s]"),
8902 dwp_file->name);
8903 }
8904
8905 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8906 Open the file specified by FILE_NAME and hand it off to BFD for
8907 preliminary analysis. Return a newly initialized bfd *, which
8908 includes a canonicalized copy of FILE_NAME.
8909 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8910 In case of trouble, return NULL.
8911 NOTE: This function is derived from symfile_bfd_open. */
8912
8913 static bfd *
8914 try_open_dwop_file (const char *file_name, int is_dwp)
8915 {
8916 bfd *sym_bfd;
8917 int desc, flags;
8918 char *absolute_name;
8919
8920 flags = OPF_TRY_CWD_FIRST;
8921 if (is_dwp)
8922 flags |= OPF_SEARCH_IN_PATH;
8923 desc = openp (debug_file_directory, flags, file_name,
8924 O_RDONLY | O_BINARY, &absolute_name);
8925 if (desc < 0)
8926 return NULL;
8927
8928 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8929 if (!sym_bfd)
8930 {
8931 xfree (absolute_name);
8932 return NULL;
8933 }
8934 xfree (absolute_name);
8935 bfd_set_cacheable (sym_bfd, 1);
8936
8937 if (!bfd_check_format (sym_bfd, bfd_object))
8938 {
8939 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8940 return NULL;
8941 }
8942
8943 return sym_bfd;
8944 }
8945
8946 /* Try to open DWO file FILE_NAME.
8947 COMP_DIR is the DW_AT_comp_dir attribute.
8948 The result is the bfd handle of the file.
8949 If there is a problem finding or opening the file, return NULL.
8950 Upon success, the canonicalized path of the file is stored in the bfd,
8951 same as symfile_bfd_open. */
8952
8953 static bfd *
8954 open_dwo_file (const char *file_name, const char *comp_dir)
8955 {
8956 bfd *abfd;
8957
8958 if (IS_ABSOLUTE_PATH (file_name))
8959 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8960
8961 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8962
8963 if (comp_dir != NULL)
8964 {
8965 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8966
8967 /* NOTE: If comp_dir is a relative path, this will also try the
8968 search path, which seems useful. */
8969 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
8970 xfree (path_to_try);
8971 if (abfd != NULL)
8972 return abfd;
8973 }
8974
8975 /* That didn't work, try debug-file-directory, which, despite its name,
8976 is a list of paths. */
8977
8978 if (*debug_file_directory == '\0')
8979 return NULL;
8980
8981 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8982 }
8983
8984 /* This function is mapped across the sections and remembers the offset and
8985 size of each of the DWO debugging sections we are interested in. */
8986
8987 static void
8988 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8989 {
8990 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8991 const struct dwop_section_names *names = &dwop_section_names;
8992
8993 if (section_is_p (sectp->name, &names->abbrev_dwo))
8994 {
8995 dwo_sections->abbrev.asection = sectp;
8996 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8997 }
8998 else if (section_is_p (sectp->name, &names->info_dwo))
8999 {
9000 dwo_sections->info.asection = sectp;
9001 dwo_sections->info.size = bfd_get_section_size (sectp);
9002 }
9003 else if (section_is_p (sectp->name, &names->line_dwo))
9004 {
9005 dwo_sections->line.asection = sectp;
9006 dwo_sections->line.size = bfd_get_section_size (sectp);
9007 }
9008 else if (section_is_p (sectp->name, &names->loc_dwo))
9009 {
9010 dwo_sections->loc.asection = sectp;
9011 dwo_sections->loc.size = bfd_get_section_size (sectp);
9012 }
9013 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9014 {
9015 dwo_sections->macinfo.asection = sectp;
9016 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9017 }
9018 else if (section_is_p (sectp->name, &names->macro_dwo))
9019 {
9020 dwo_sections->macro.asection = sectp;
9021 dwo_sections->macro.size = bfd_get_section_size (sectp);
9022 }
9023 else if (section_is_p (sectp->name, &names->str_dwo))
9024 {
9025 dwo_sections->str.asection = sectp;
9026 dwo_sections->str.size = bfd_get_section_size (sectp);
9027 }
9028 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9029 {
9030 dwo_sections->str_offsets.asection = sectp;
9031 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9032 }
9033 else if (section_is_p (sectp->name, &names->types_dwo))
9034 {
9035 struct dwarf2_section_info type_section;
9036
9037 memset (&type_section, 0, sizeof (type_section));
9038 type_section.asection = sectp;
9039 type_section.size = bfd_get_section_size (sectp);
9040 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9041 &type_section);
9042 }
9043 }
9044
9045 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9046 by PER_CU.
9047 The result is NULL if DWO_NAME can't be found. */
9048
9049 static struct dwo_file *
9050 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
9051 {
9052 struct objfile *objfile = dwarf2_per_objfile->objfile;
9053 struct dwo_file *dwo_file;
9054 bfd *dbfd;
9055 struct cleanup *cleanups;
9056
9057 dbfd = open_dwo_file (dwo_name, comp_dir);
9058 if (dbfd == NULL)
9059 {
9060 if (dwarf2_read_debug)
9061 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9062 return NULL;
9063 }
9064 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9065 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
9066 dwo_name, strlen (dwo_name));
9067 dwo_file->dbfd = dbfd;
9068
9069 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9070
9071 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9072
9073 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9074
9075 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9076 dwo_file->sections.types);
9077
9078 discard_cleanups (cleanups);
9079
9080 if (dwarf2_read_debug)
9081 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9082
9083 return dwo_file;
9084 }
9085
9086 /* This function is mapped across the sections and remembers the offset and
9087 size of each of the DWP debugging sections we are interested in. */
9088
9089 static void
9090 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9091 {
9092 struct dwp_file *dwp_file = dwp_file_ptr;
9093 const struct dwop_section_names *names = &dwop_section_names;
9094 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9095
9096 /* Record the ELF section number for later lookup: this is what the
9097 .debug_cu_index,.debug_tu_index tables use. */
9098 gdb_assert (elf_section_nr < dwp_file->num_sections);
9099 dwp_file->elf_sections[elf_section_nr] = sectp;
9100
9101 /* Look for specific sections that we need. */
9102 if (section_is_p (sectp->name, &names->str_dwo))
9103 {
9104 dwp_file->sections.str.asection = sectp;
9105 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9106 }
9107 else if (section_is_p (sectp->name, &names->cu_index))
9108 {
9109 dwp_file->sections.cu_index.asection = sectp;
9110 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9111 }
9112 else if (section_is_p (sectp->name, &names->tu_index))
9113 {
9114 dwp_file->sections.tu_index.asection = sectp;
9115 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9116 }
9117 }
9118
9119 /* Hash function for dwp_file loaded CUs/TUs. */
9120
9121 static hashval_t
9122 hash_dwp_loaded_cutus (const void *item)
9123 {
9124 const struct dwo_unit *dwo_unit = item;
9125
9126 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9127 return dwo_unit->signature;
9128 }
9129
9130 /* Equality function for dwp_file loaded CUs/TUs. */
9131
9132 static int
9133 eq_dwp_loaded_cutus (const void *a, const void *b)
9134 {
9135 const struct dwo_unit *dua = a;
9136 const struct dwo_unit *dub = b;
9137
9138 return dua->signature == dub->signature;
9139 }
9140
9141 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9142
9143 static htab_t
9144 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9145 {
9146 return htab_create_alloc_ex (3,
9147 hash_dwp_loaded_cutus,
9148 eq_dwp_loaded_cutus,
9149 NULL,
9150 &objfile->objfile_obstack,
9151 hashtab_obstack_allocate,
9152 dummy_obstack_deallocate);
9153 }
9154
9155 /* Try to open DWP file FILE_NAME.
9156 The result is the bfd handle of the file.
9157 If there is a problem finding or opening the file, return NULL.
9158 Upon success, the canonicalized path of the file is stored in the bfd,
9159 same as symfile_bfd_open. */
9160
9161 static bfd *
9162 open_dwp_file (const char *file_name)
9163 {
9164 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9165 }
9166
9167 /* Initialize the use of the DWP file for the current objfile.
9168 By convention the name of the DWP file is ${objfile}.dwp.
9169 The result is NULL if it can't be found. */
9170
9171 static struct dwp_file *
9172 open_and_init_dwp_file (void)
9173 {
9174 struct objfile *objfile = dwarf2_per_objfile->objfile;
9175 struct dwp_file *dwp_file;
9176 char *dwp_name;
9177 bfd *dbfd;
9178 struct cleanup *cleanups;
9179
9180 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9181 cleanups = make_cleanup (xfree, dwp_name);
9182
9183 dbfd = open_dwp_file (dwp_name);
9184 if (dbfd == NULL)
9185 {
9186 if (dwarf2_read_debug)
9187 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9188 do_cleanups (cleanups);
9189 return NULL;
9190 }
9191 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9192 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9193 dwp_name, strlen (dwp_name));
9194 dwp_file->dbfd = dbfd;
9195 do_cleanups (cleanups);
9196
9197 /* +1: section 0 is unused */
9198 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9199 dwp_file->elf_sections =
9200 OBSTACK_CALLOC (&objfile->objfile_obstack,
9201 dwp_file->num_sections, asection *);
9202
9203 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9204
9205 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9206
9207 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9208
9209 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9210
9211 if (dwarf2_read_debug)
9212 {
9213 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9214 fprintf_unfiltered (gdb_stdlog,
9215 " %u CUs, %u TUs\n",
9216 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9217 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9218 }
9219
9220 return dwp_file;
9221 }
9222
9223 /* Wrapper around open_and_init_dwp_file, only open it once. */
9224
9225 static struct dwp_file *
9226 get_dwp_file (void)
9227 {
9228 if (! dwarf2_per_objfile->dwp_checked)
9229 {
9230 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9231 dwarf2_per_objfile->dwp_checked = 1;
9232 }
9233 return dwarf2_per_objfile->dwp_file;
9234 }
9235
9236 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9237 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9238 or in the DWP file for the objfile, referenced by THIS_UNIT.
9239 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9240 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9241
9242 This is called, for example, when wanting to read a variable with a
9243 complex location. Therefore we don't want to do file i/o for every call.
9244 Therefore we don't want to look for a DWO file on every call.
9245 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9246 then we check if we've already seen DWO_NAME, and only THEN do we check
9247 for a DWO file.
9248
9249 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9250 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9251
9252 static struct dwo_unit *
9253 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9254 const char *dwo_name, const char *comp_dir,
9255 ULONGEST signature, int is_debug_types)
9256 {
9257 struct objfile *objfile = dwarf2_per_objfile->objfile;
9258 const char *kind = is_debug_types ? "TU" : "CU";
9259 void **dwo_file_slot;
9260 struct dwo_file *dwo_file;
9261 struct dwp_file *dwp_file;
9262
9263 /* Have we already read SIGNATURE from a DWP file? */
9264
9265 dwp_file = get_dwp_file ();
9266 if (dwp_file != NULL)
9267 {
9268 const struct dwp_hash_table *dwp_htab =
9269 is_debug_types ? dwp_file->tus : dwp_file->cus;
9270
9271 if (dwp_htab != NULL)
9272 {
9273 struct dwo_unit *dwo_cutu =
9274 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9275
9276 if (dwo_cutu != NULL)
9277 {
9278 if (dwarf2_read_debug)
9279 {
9280 fprintf_unfiltered (gdb_stdlog,
9281 "Virtual DWO %s %s found: @%s\n",
9282 kind, hex_string (signature),
9283 host_address_to_string (dwo_cutu));
9284 }
9285 return dwo_cutu;
9286 }
9287 }
9288 }
9289
9290 /* Have we already seen DWO_NAME? */
9291
9292 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9293 if (*dwo_file_slot == NULL)
9294 {
9295 /* Read in the file and build a table of the DWOs it contains. */
9296 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9297 }
9298 /* NOTE: This will be NULL if unable to open the file. */
9299 dwo_file = *dwo_file_slot;
9300
9301 if (dwo_file != NULL)
9302 {
9303 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9304
9305 if (htab != NULL)
9306 {
9307 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9308
9309 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9310 find_dwo_cutu.signature = signature;
9311 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9312
9313 if (dwo_cutu != NULL)
9314 {
9315 if (dwarf2_read_debug)
9316 {
9317 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9318 kind, dwo_name, hex_string (signature),
9319 host_address_to_string (dwo_cutu));
9320 }
9321 return dwo_cutu;
9322 }
9323 }
9324 }
9325
9326 /* We didn't find it. This could mean a dwo_id mismatch, or
9327 someone deleted the DWO/DWP file, or the search path isn't set up
9328 correctly to find the file. */
9329
9330 if (dwarf2_read_debug)
9331 {
9332 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9333 kind, dwo_name, hex_string (signature));
9334 }
9335
9336 complaint (&symfile_complaints,
9337 _("Could not find DWO %s referenced by CU at offset 0x%x"
9338 " [in module %s]"),
9339 kind, this_unit->offset.sect_off, objfile->name);
9340 return NULL;
9341 }
9342
9343 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9344 See lookup_dwo_cutu_unit for details. */
9345
9346 static struct dwo_unit *
9347 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9348 const char *dwo_name, const char *comp_dir,
9349 ULONGEST signature)
9350 {
9351 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9352 }
9353
9354 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9355 See lookup_dwo_cutu_unit for details. */
9356
9357 static struct dwo_unit *
9358 lookup_dwo_type_unit (struct signatured_type *this_tu,
9359 const char *dwo_name, const char *comp_dir)
9360 {
9361 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9362 }
9363
9364 /* Free all resources associated with DWO_FILE.
9365 Close the DWO file and munmap the sections.
9366 All memory should be on the objfile obstack. */
9367
9368 static void
9369 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9370 {
9371 int ix;
9372 struct dwarf2_section_info *section;
9373
9374 /* Note: dbfd is NULL for virtual DWO files. */
9375 gdb_bfd_unref (dwo_file->dbfd);
9376
9377 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9378 }
9379
9380 /* Wrapper for free_dwo_file for use in cleanups. */
9381
9382 static void
9383 free_dwo_file_cleanup (void *arg)
9384 {
9385 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9386 struct objfile *objfile = dwarf2_per_objfile->objfile;
9387
9388 free_dwo_file (dwo_file, objfile);
9389 }
9390
9391 /* Traversal function for free_dwo_files. */
9392
9393 static int
9394 free_dwo_file_from_slot (void **slot, void *info)
9395 {
9396 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9397 struct objfile *objfile = (struct objfile *) info;
9398
9399 free_dwo_file (dwo_file, objfile);
9400
9401 return 1;
9402 }
9403
9404 /* Free all resources associated with DWO_FILES. */
9405
9406 static void
9407 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9408 {
9409 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9410 }
9411 \f
9412 /* Read in various DIEs. */
9413
9414 /* qsort helper for inherit_abstract_dies. */
9415
9416 static int
9417 unsigned_int_compar (const void *ap, const void *bp)
9418 {
9419 unsigned int a = *(unsigned int *) ap;
9420 unsigned int b = *(unsigned int *) bp;
9421
9422 return (a > b) - (b > a);
9423 }
9424
9425 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9426 Inherit only the children of the DW_AT_abstract_origin DIE not being
9427 already referenced by DW_AT_abstract_origin from the children of the
9428 current DIE. */
9429
9430 static void
9431 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9432 {
9433 struct die_info *child_die;
9434 unsigned die_children_count;
9435 /* CU offsets which were referenced by children of the current DIE. */
9436 sect_offset *offsets;
9437 sect_offset *offsets_end, *offsetp;
9438 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9439 struct die_info *origin_die;
9440 /* Iterator of the ORIGIN_DIE children. */
9441 struct die_info *origin_child_die;
9442 struct cleanup *cleanups;
9443 struct attribute *attr;
9444 struct dwarf2_cu *origin_cu;
9445 struct pending **origin_previous_list_in_scope;
9446
9447 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9448 if (!attr)
9449 return;
9450
9451 /* Note that following die references may follow to a die in a
9452 different cu. */
9453
9454 origin_cu = cu;
9455 origin_die = follow_die_ref (die, attr, &origin_cu);
9456
9457 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9458 symbols in. */
9459 origin_previous_list_in_scope = origin_cu->list_in_scope;
9460 origin_cu->list_in_scope = cu->list_in_scope;
9461
9462 if (die->tag != origin_die->tag
9463 && !(die->tag == DW_TAG_inlined_subroutine
9464 && origin_die->tag == DW_TAG_subprogram))
9465 complaint (&symfile_complaints,
9466 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9467 die->offset.sect_off, origin_die->offset.sect_off);
9468
9469 child_die = die->child;
9470 die_children_count = 0;
9471 while (child_die && child_die->tag)
9472 {
9473 child_die = sibling_die (child_die);
9474 die_children_count++;
9475 }
9476 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9477 cleanups = make_cleanup (xfree, offsets);
9478
9479 offsets_end = offsets;
9480 child_die = die->child;
9481 while (child_die && child_die->tag)
9482 {
9483 /* For each CHILD_DIE, find the corresponding child of
9484 ORIGIN_DIE. If there is more than one layer of
9485 DW_AT_abstract_origin, follow them all; there shouldn't be,
9486 but GCC versions at least through 4.4 generate this (GCC PR
9487 40573). */
9488 struct die_info *child_origin_die = child_die;
9489 struct dwarf2_cu *child_origin_cu = cu;
9490
9491 while (1)
9492 {
9493 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9494 child_origin_cu);
9495 if (attr == NULL)
9496 break;
9497 child_origin_die = follow_die_ref (child_origin_die, attr,
9498 &child_origin_cu);
9499 }
9500
9501 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9502 counterpart may exist. */
9503 if (child_origin_die != child_die)
9504 {
9505 if (child_die->tag != child_origin_die->tag
9506 && !(child_die->tag == DW_TAG_inlined_subroutine
9507 && child_origin_die->tag == DW_TAG_subprogram))
9508 complaint (&symfile_complaints,
9509 _("Child DIE 0x%x and its abstract origin 0x%x have "
9510 "different tags"), child_die->offset.sect_off,
9511 child_origin_die->offset.sect_off);
9512 if (child_origin_die->parent != origin_die)
9513 complaint (&symfile_complaints,
9514 _("Child DIE 0x%x and its abstract origin 0x%x have "
9515 "different parents"), child_die->offset.sect_off,
9516 child_origin_die->offset.sect_off);
9517 else
9518 *offsets_end++ = child_origin_die->offset;
9519 }
9520 child_die = sibling_die (child_die);
9521 }
9522 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9523 unsigned_int_compar);
9524 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9525 if (offsetp[-1].sect_off == offsetp->sect_off)
9526 complaint (&symfile_complaints,
9527 _("Multiple children of DIE 0x%x refer "
9528 "to DIE 0x%x as their abstract origin"),
9529 die->offset.sect_off, offsetp->sect_off);
9530
9531 offsetp = offsets;
9532 origin_child_die = origin_die->child;
9533 while (origin_child_die && origin_child_die->tag)
9534 {
9535 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9536 while (offsetp < offsets_end
9537 && offsetp->sect_off < origin_child_die->offset.sect_off)
9538 offsetp++;
9539 if (offsetp >= offsets_end
9540 || offsetp->sect_off > origin_child_die->offset.sect_off)
9541 {
9542 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9543 process_die (origin_child_die, origin_cu);
9544 }
9545 origin_child_die = sibling_die (origin_child_die);
9546 }
9547 origin_cu->list_in_scope = origin_previous_list_in_scope;
9548
9549 do_cleanups (cleanups);
9550 }
9551
9552 static void
9553 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9554 {
9555 struct objfile *objfile = cu->objfile;
9556 struct context_stack *new;
9557 CORE_ADDR lowpc;
9558 CORE_ADDR highpc;
9559 struct die_info *child_die;
9560 struct attribute *attr, *call_line, *call_file;
9561 const char *name;
9562 CORE_ADDR baseaddr;
9563 struct block *block;
9564 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9565 VEC (symbolp) *template_args = NULL;
9566 struct template_symbol *templ_func = NULL;
9567
9568 if (inlined_func)
9569 {
9570 /* If we do not have call site information, we can't show the
9571 caller of this inlined function. That's too confusing, so
9572 only use the scope for local variables. */
9573 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9574 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9575 if (call_line == NULL || call_file == NULL)
9576 {
9577 read_lexical_block_scope (die, cu);
9578 return;
9579 }
9580 }
9581
9582 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9583
9584 name = dwarf2_name (die, cu);
9585
9586 /* Ignore functions with missing or empty names. These are actually
9587 illegal according to the DWARF standard. */
9588 if (name == NULL)
9589 {
9590 complaint (&symfile_complaints,
9591 _("missing name for subprogram DIE at %d"),
9592 die->offset.sect_off);
9593 return;
9594 }
9595
9596 /* Ignore functions with missing or invalid low and high pc attributes. */
9597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9598 {
9599 attr = dwarf2_attr (die, DW_AT_external, cu);
9600 if (!attr || !DW_UNSND (attr))
9601 complaint (&symfile_complaints,
9602 _("cannot get low and high bounds "
9603 "for subprogram DIE at %d"),
9604 die->offset.sect_off);
9605 return;
9606 }
9607
9608 lowpc += baseaddr;
9609 highpc += baseaddr;
9610
9611 /* If we have any template arguments, then we must allocate a
9612 different sort of symbol. */
9613 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9614 {
9615 if (child_die->tag == DW_TAG_template_type_param
9616 || child_die->tag == DW_TAG_template_value_param)
9617 {
9618 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9619 struct template_symbol);
9620 templ_func->base.is_cplus_template_function = 1;
9621 break;
9622 }
9623 }
9624
9625 new = push_context (0, lowpc);
9626 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9627 (struct symbol *) templ_func);
9628
9629 /* If there is a location expression for DW_AT_frame_base, record
9630 it. */
9631 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9632 if (attr)
9633 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9634
9635 cu->list_in_scope = &local_symbols;
9636
9637 if (die->child != NULL)
9638 {
9639 child_die = die->child;
9640 while (child_die && child_die->tag)
9641 {
9642 if (child_die->tag == DW_TAG_template_type_param
9643 || child_die->tag == DW_TAG_template_value_param)
9644 {
9645 struct symbol *arg = new_symbol (child_die, NULL, cu);
9646
9647 if (arg != NULL)
9648 VEC_safe_push (symbolp, template_args, arg);
9649 }
9650 else
9651 process_die (child_die, cu);
9652 child_die = sibling_die (child_die);
9653 }
9654 }
9655
9656 inherit_abstract_dies (die, cu);
9657
9658 /* If we have a DW_AT_specification, we might need to import using
9659 directives from the context of the specification DIE. See the
9660 comment in determine_prefix. */
9661 if (cu->language == language_cplus
9662 && dwarf2_attr (die, DW_AT_specification, cu))
9663 {
9664 struct dwarf2_cu *spec_cu = cu;
9665 struct die_info *spec_die = die_specification (die, &spec_cu);
9666
9667 while (spec_die)
9668 {
9669 child_die = spec_die->child;
9670 while (child_die && child_die->tag)
9671 {
9672 if (child_die->tag == DW_TAG_imported_module)
9673 process_die (child_die, spec_cu);
9674 child_die = sibling_die (child_die);
9675 }
9676
9677 /* In some cases, GCC generates specification DIEs that
9678 themselves contain DW_AT_specification attributes. */
9679 spec_die = die_specification (spec_die, &spec_cu);
9680 }
9681 }
9682
9683 new = pop_context ();
9684 /* Make a block for the local symbols within. */
9685 block = finish_block (new->name, &local_symbols, new->old_blocks,
9686 lowpc, highpc, objfile);
9687
9688 /* For C++, set the block's scope. */
9689 if ((cu->language == language_cplus || cu->language == language_fortran)
9690 && cu->processing_has_namespace_info)
9691 block_set_scope (block, determine_prefix (die, cu),
9692 &objfile->objfile_obstack);
9693
9694 /* If we have address ranges, record them. */
9695 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9696
9697 /* Attach template arguments to function. */
9698 if (! VEC_empty (symbolp, template_args))
9699 {
9700 gdb_assert (templ_func != NULL);
9701
9702 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9703 templ_func->template_arguments
9704 = obstack_alloc (&objfile->objfile_obstack,
9705 (templ_func->n_template_arguments
9706 * sizeof (struct symbol *)));
9707 memcpy (templ_func->template_arguments,
9708 VEC_address (symbolp, template_args),
9709 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9710 VEC_free (symbolp, template_args);
9711 }
9712
9713 /* In C++, we can have functions nested inside functions (e.g., when
9714 a function declares a class that has methods). This means that
9715 when we finish processing a function scope, we may need to go
9716 back to building a containing block's symbol lists. */
9717 local_symbols = new->locals;
9718 using_directives = new->using_directives;
9719
9720 /* If we've finished processing a top-level function, subsequent
9721 symbols go in the file symbol list. */
9722 if (outermost_context_p ())
9723 cu->list_in_scope = &file_symbols;
9724 }
9725
9726 /* Process all the DIES contained within a lexical block scope. Start
9727 a new scope, process the dies, and then close the scope. */
9728
9729 static void
9730 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9731 {
9732 struct objfile *objfile = cu->objfile;
9733 struct context_stack *new;
9734 CORE_ADDR lowpc, highpc;
9735 struct die_info *child_die;
9736 CORE_ADDR baseaddr;
9737
9738 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9739
9740 /* Ignore blocks with missing or invalid low and high pc attributes. */
9741 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9742 as multiple lexical blocks? Handling children in a sane way would
9743 be nasty. Might be easier to properly extend generic blocks to
9744 describe ranges. */
9745 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9746 return;
9747 lowpc += baseaddr;
9748 highpc += baseaddr;
9749
9750 push_context (0, lowpc);
9751 if (die->child != NULL)
9752 {
9753 child_die = die->child;
9754 while (child_die && child_die->tag)
9755 {
9756 process_die (child_die, cu);
9757 child_die = sibling_die (child_die);
9758 }
9759 }
9760 new = pop_context ();
9761
9762 if (local_symbols != NULL || using_directives != NULL)
9763 {
9764 struct block *block
9765 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9766 highpc, objfile);
9767
9768 /* Note that recording ranges after traversing children, as we
9769 do here, means that recording a parent's ranges entails
9770 walking across all its children's ranges as they appear in
9771 the address map, which is quadratic behavior.
9772
9773 It would be nicer to record the parent's ranges before
9774 traversing its children, simply overriding whatever you find
9775 there. But since we don't even decide whether to create a
9776 block until after we've traversed its children, that's hard
9777 to do. */
9778 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9779 }
9780 local_symbols = new->locals;
9781 using_directives = new->using_directives;
9782 }
9783
9784 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9785
9786 static void
9787 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9788 {
9789 struct objfile *objfile = cu->objfile;
9790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9791 CORE_ADDR pc, baseaddr;
9792 struct attribute *attr;
9793 struct call_site *call_site, call_site_local;
9794 void **slot;
9795 int nparams;
9796 struct die_info *child_die;
9797
9798 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9799
9800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9801 if (!attr)
9802 {
9803 complaint (&symfile_complaints,
9804 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9805 "DIE 0x%x [in module %s]"),
9806 die->offset.sect_off, objfile->name);
9807 return;
9808 }
9809 pc = DW_ADDR (attr) + baseaddr;
9810
9811 if (cu->call_site_htab == NULL)
9812 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9813 NULL, &objfile->objfile_obstack,
9814 hashtab_obstack_allocate, NULL);
9815 call_site_local.pc = pc;
9816 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9817 if (*slot != NULL)
9818 {
9819 complaint (&symfile_complaints,
9820 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9821 "DIE 0x%x [in module %s]"),
9822 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9823 return;
9824 }
9825
9826 /* Count parameters at the caller. */
9827
9828 nparams = 0;
9829 for (child_die = die->child; child_die && child_die->tag;
9830 child_die = sibling_die (child_die))
9831 {
9832 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9833 {
9834 complaint (&symfile_complaints,
9835 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9836 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9837 child_die->tag, child_die->offset.sect_off, objfile->name);
9838 continue;
9839 }
9840
9841 nparams++;
9842 }
9843
9844 call_site = obstack_alloc (&objfile->objfile_obstack,
9845 (sizeof (*call_site)
9846 + (sizeof (*call_site->parameter)
9847 * (nparams - 1))));
9848 *slot = call_site;
9849 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9850 call_site->pc = pc;
9851
9852 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9853 {
9854 struct die_info *func_die;
9855
9856 /* Skip also over DW_TAG_inlined_subroutine. */
9857 for (func_die = die->parent;
9858 func_die && func_die->tag != DW_TAG_subprogram
9859 && func_die->tag != DW_TAG_subroutine_type;
9860 func_die = func_die->parent);
9861
9862 /* DW_AT_GNU_all_call_sites is a superset
9863 of DW_AT_GNU_all_tail_call_sites. */
9864 if (func_die
9865 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9866 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9867 {
9868 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9869 not complete. But keep CALL_SITE for look ups via call_site_htab,
9870 both the initial caller containing the real return address PC and
9871 the final callee containing the current PC of a chain of tail
9872 calls do not need to have the tail call list complete. But any
9873 function candidate for a virtual tail call frame searched via
9874 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9875 determined unambiguously. */
9876 }
9877 else
9878 {
9879 struct type *func_type = NULL;
9880
9881 if (func_die)
9882 func_type = get_die_type (func_die, cu);
9883 if (func_type != NULL)
9884 {
9885 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9886
9887 /* Enlist this call site to the function. */
9888 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9889 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9890 }
9891 else
9892 complaint (&symfile_complaints,
9893 _("Cannot find function owning DW_TAG_GNU_call_site "
9894 "DIE 0x%x [in module %s]"),
9895 die->offset.sect_off, objfile->name);
9896 }
9897 }
9898
9899 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9900 if (attr == NULL)
9901 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9902 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9903 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9904 /* Keep NULL DWARF_BLOCK. */;
9905 else if (attr_form_is_block (attr))
9906 {
9907 struct dwarf2_locexpr_baton *dlbaton;
9908
9909 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9910 dlbaton->data = DW_BLOCK (attr)->data;
9911 dlbaton->size = DW_BLOCK (attr)->size;
9912 dlbaton->per_cu = cu->per_cu;
9913
9914 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9915 }
9916 else if (is_ref_attr (attr))
9917 {
9918 struct dwarf2_cu *target_cu = cu;
9919 struct die_info *target_die;
9920
9921 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9922 gdb_assert (target_cu->objfile == objfile);
9923 if (die_is_declaration (target_die, target_cu))
9924 {
9925 const char *target_physname = NULL;
9926 struct attribute *target_attr;
9927
9928 /* Prefer the mangled name; otherwise compute the demangled one. */
9929 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9930 if (target_attr == NULL)
9931 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9932 target_cu);
9933 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9934 target_physname = DW_STRING (target_attr);
9935 else
9936 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9937 if (target_physname == NULL)
9938 complaint (&symfile_complaints,
9939 _("DW_AT_GNU_call_site_target target DIE has invalid "
9940 "physname, for referencing DIE 0x%x [in module %s]"),
9941 die->offset.sect_off, objfile->name);
9942 else
9943 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9944 }
9945 else
9946 {
9947 CORE_ADDR lowpc;
9948
9949 /* DW_AT_entry_pc should be preferred. */
9950 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9951 complaint (&symfile_complaints,
9952 _("DW_AT_GNU_call_site_target target DIE has invalid "
9953 "low pc, for referencing DIE 0x%x [in module %s]"),
9954 die->offset.sect_off, objfile->name);
9955 else
9956 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9957 }
9958 }
9959 else
9960 complaint (&symfile_complaints,
9961 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9962 "block nor reference, for DIE 0x%x [in module %s]"),
9963 die->offset.sect_off, objfile->name);
9964
9965 call_site->per_cu = cu->per_cu;
9966
9967 for (child_die = die->child;
9968 child_die && child_die->tag;
9969 child_die = sibling_die (child_die))
9970 {
9971 struct call_site_parameter *parameter;
9972 struct attribute *loc, *origin;
9973
9974 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9975 {
9976 /* Already printed the complaint above. */
9977 continue;
9978 }
9979
9980 gdb_assert (call_site->parameter_count < nparams);
9981 parameter = &call_site->parameter[call_site->parameter_count];
9982
9983 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9984 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9985 register is contained in DW_AT_GNU_call_site_value. */
9986
9987 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9988 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9989 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9990 {
9991 sect_offset offset;
9992
9993 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9994 offset = dwarf2_get_ref_die_offset (origin);
9995 if (!offset_in_cu_p (&cu->header, offset))
9996 {
9997 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9998 binding can be done only inside one CU. Such referenced DIE
9999 therefore cannot be even moved to DW_TAG_partial_unit. */
10000 complaint (&symfile_complaints,
10001 _("DW_AT_abstract_origin offset is not in CU for "
10002 "DW_TAG_GNU_call_site child DIE 0x%x "
10003 "[in module %s]"),
10004 child_die->offset.sect_off, objfile->name);
10005 continue;
10006 }
10007 parameter->u.param_offset.cu_off = (offset.sect_off
10008 - cu->header.offset.sect_off);
10009 }
10010 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10011 {
10012 complaint (&symfile_complaints,
10013 _("No DW_FORM_block* DW_AT_location for "
10014 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10015 child_die->offset.sect_off, objfile->name);
10016 continue;
10017 }
10018 else
10019 {
10020 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10021 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10022 if (parameter->u.dwarf_reg != -1)
10023 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10024 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10025 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10026 &parameter->u.fb_offset))
10027 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10028 else
10029 {
10030 complaint (&symfile_complaints,
10031 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10032 "for DW_FORM_block* DW_AT_location is supported for "
10033 "DW_TAG_GNU_call_site child DIE 0x%x "
10034 "[in module %s]"),
10035 child_die->offset.sect_off, objfile->name);
10036 continue;
10037 }
10038 }
10039
10040 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10041 if (!attr_form_is_block (attr))
10042 {
10043 complaint (&symfile_complaints,
10044 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10045 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10046 child_die->offset.sect_off, objfile->name);
10047 continue;
10048 }
10049 parameter->value = DW_BLOCK (attr)->data;
10050 parameter->value_size = DW_BLOCK (attr)->size;
10051
10052 /* Parameters are not pre-cleared by memset above. */
10053 parameter->data_value = NULL;
10054 parameter->data_value_size = 0;
10055 call_site->parameter_count++;
10056
10057 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10058 if (attr)
10059 {
10060 if (!attr_form_is_block (attr))
10061 complaint (&symfile_complaints,
10062 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10063 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10064 child_die->offset.sect_off, objfile->name);
10065 else
10066 {
10067 parameter->data_value = DW_BLOCK (attr)->data;
10068 parameter->data_value_size = DW_BLOCK (attr)->size;
10069 }
10070 }
10071 }
10072 }
10073
10074 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10075 Return 1 if the attributes are present and valid, otherwise, return 0.
10076 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10077
10078 static int
10079 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10080 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10081 struct partial_symtab *ranges_pst)
10082 {
10083 struct objfile *objfile = cu->objfile;
10084 struct comp_unit_head *cu_header = &cu->header;
10085 bfd *obfd = objfile->obfd;
10086 unsigned int addr_size = cu_header->addr_size;
10087 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10088 /* Base address selection entry. */
10089 CORE_ADDR base;
10090 int found_base;
10091 unsigned int dummy;
10092 gdb_byte *buffer;
10093 CORE_ADDR marker;
10094 int low_set;
10095 CORE_ADDR low = 0;
10096 CORE_ADDR high = 0;
10097 CORE_ADDR baseaddr;
10098
10099 found_base = cu->base_known;
10100 base = cu->base_address;
10101
10102 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10103 if (offset >= dwarf2_per_objfile->ranges.size)
10104 {
10105 complaint (&symfile_complaints,
10106 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10107 offset);
10108 return 0;
10109 }
10110 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10111
10112 /* Read in the largest possible address. */
10113 marker = read_address (obfd, buffer, cu, &dummy);
10114 if ((marker & mask) == mask)
10115 {
10116 /* If we found the largest possible address, then
10117 read the base address. */
10118 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10119 buffer += 2 * addr_size;
10120 offset += 2 * addr_size;
10121 found_base = 1;
10122 }
10123
10124 low_set = 0;
10125
10126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10127
10128 while (1)
10129 {
10130 CORE_ADDR range_beginning, range_end;
10131
10132 range_beginning = read_address (obfd, buffer, cu, &dummy);
10133 buffer += addr_size;
10134 range_end = read_address (obfd, buffer, cu, &dummy);
10135 buffer += addr_size;
10136 offset += 2 * addr_size;
10137
10138 /* An end of list marker is a pair of zero addresses. */
10139 if (range_beginning == 0 && range_end == 0)
10140 /* Found the end of list entry. */
10141 break;
10142
10143 /* Each base address selection entry is a pair of 2 values.
10144 The first is the largest possible address, the second is
10145 the base address. Check for a base address here. */
10146 if ((range_beginning & mask) == mask)
10147 {
10148 /* If we found the largest possible address, then
10149 read the base address. */
10150 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10151 found_base = 1;
10152 continue;
10153 }
10154
10155 if (!found_base)
10156 {
10157 /* We have no valid base address for the ranges
10158 data. */
10159 complaint (&symfile_complaints,
10160 _("Invalid .debug_ranges data (no base address)"));
10161 return 0;
10162 }
10163
10164 if (range_beginning > range_end)
10165 {
10166 /* Inverted range entries are invalid. */
10167 complaint (&symfile_complaints,
10168 _("Invalid .debug_ranges data (inverted range)"));
10169 return 0;
10170 }
10171
10172 /* Empty range entries have no effect. */
10173 if (range_beginning == range_end)
10174 continue;
10175
10176 range_beginning += base;
10177 range_end += base;
10178
10179 /* A not-uncommon case of bad debug info.
10180 Don't pollute the addrmap with bad data. */
10181 if (range_beginning + baseaddr == 0
10182 && !dwarf2_per_objfile->has_section_at_zero)
10183 {
10184 complaint (&symfile_complaints,
10185 _(".debug_ranges entry has start address of zero"
10186 " [in module %s]"), objfile->name);
10187 continue;
10188 }
10189
10190 if (ranges_pst != NULL)
10191 addrmap_set_empty (objfile->psymtabs_addrmap,
10192 range_beginning + baseaddr,
10193 range_end - 1 + baseaddr,
10194 ranges_pst);
10195
10196 /* FIXME: This is recording everything as a low-high
10197 segment of consecutive addresses. We should have a
10198 data structure for discontiguous block ranges
10199 instead. */
10200 if (! low_set)
10201 {
10202 low = range_beginning;
10203 high = range_end;
10204 low_set = 1;
10205 }
10206 else
10207 {
10208 if (range_beginning < low)
10209 low = range_beginning;
10210 if (range_end > high)
10211 high = range_end;
10212 }
10213 }
10214
10215 if (! low_set)
10216 /* If the first entry is an end-of-list marker, the range
10217 describes an empty scope, i.e. no instructions. */
10218 return 0;
10219
10220 if (low_return)
10221 *low_return = low;
10222 if (high_return)
10223 *high_return = high;
10224 return 1;
10225 }
10226
10227 /* Get low and high pc attributes from a die. Return 1 if the attributes
10228 are present and valid, otherwise, return 0. Return -1 if the range is
10229 discontinuous, i.e. derived from DW_AT_ranges information. */
10230
10231 static int
10232 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10233 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10234 struct partial_symtab *pst)
10235 {
10236 struct attribute *attr;
10237 struct attribute *attr_high;
10238 CORE_ADDR low = 0;
10239 CORE_ADDR high = 0;
10240 int ret = 0;
10241
10242 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10243 if (attr_high)
10244 {
10245 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10246 if (attr)
10247 {
10248 low = DW_ADDR (attr);
10249 if (attr_high->form == DW_FORM_addr
10250 || attr_high->form == DW_FORM_GNU_addr_index)
10251 high = DW_ADDR (attr_high);
10252 else
10253 high = low + DW_UNSND (attr_high);
10254 }
10255 else
10256 /* Found high w/o low attribute. */
10257 return 0;
10258
10259 /* Found consecutive range of addresses. */
10260 ret = 1;
10261 }
10262 else
10263 {
10264 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10265 if (attr != NULL)
10266 {
10267 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10268 We take advantage of the fact that DW_AT_ranges does not appear
10269 in DW_TAG_compile_unit of DWO files. */
10270 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10271 unsigned int ranges_offset = (DW_UNSND (attr)
10272 + (need_ranges_base
10273 ? cu->ranges_base
10274 : 0));
10275
10276 /* Value of the DW_AT_ranges attribute is the offset in the
10277 .debug_ranges section. */
10278 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10279 return 0;
10280 /* Found discontinuous range of addresses. */
10281 ret = -1;
10282 }
10283 }
10284
10285 /* read_partial_die has also the strict LOW < HIGH requirement. */
10286 if (high <= low)
10287 return 0;
10288
10289 /* When using the GNU linker, .gnu.linkonce. sections are used to
10290 eliminate duplicate copies of functions and vtables and such.
10291 The linker will arbitrarily choose one and discard the others.
10292 The AT_*_pc values for such functions refer to local labels in
10293 these sections. If the section from that file was discarded, the
10294 labels are not in the output, so the relocs get a value of 0.
10295 If this is a discarded function, mark the pc bounds as invalid,
10296 so that GDB will ignore it. */
10297 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10298 return 0;
10299
10300 *lowpc = low;
10301 if (highpc)
10302 *highpc = high;
10303 return ret;
10304 }
10305
10306 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10307 its low and high PC addresses. Do nothing if these addresses could not
10308 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10309 and HIGHPC to the high address if greater than HIGHPC. */
10310
10311 static void
10312 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10313 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10314 struct dwarf2_cu *cu)
10315 {
10316 CORE_ADDR low, high;
10317 struct die_info *child = die->child;
10318
10319 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10320 {
10321 *lowpc = min (*lowpc, low);
10322 *highpc = max (*highpc, high);
10323 }
10324
10325 /* If the language does not allow nested subprograms (either inside
10326 subprograms or lexical blocks), we're done. */
10327 if (cu->language != language_ada)
10328 return;
10329
10330 /* Check all the children of the given DIE. If it contains nested
10331 subprograms, then check their pc bounds. Likewise, we need to
10332 check lexical blocks as well, as they may also contain subprogram
10333 definitions. */
10334 while (child && child->tag)
10335 {
10336 if (child->tag == DW_TAG_subprogram
10337 || child->tag == DW_TAG_lexical_block)
10338 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10339 child = sibling_die (child);
10340 }
10341 }
10342
10343 /* Get the low and high pc's represented by the scope DIE, and store
10344 them in *LOWPC and *HIGHPC. If the correct values can't be
10345 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10346
10347 static void
10348 get_scope_pc_bounds (struct die_info *die,
10349 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10350 struct dwarf2_cu *cu)
10351 {
10352 CORE_ADDR best_low = (CORE_ADDR) -1;
10353 CORE_ADDR best_high = (CORE_ADDR) 0;
10354 CORE_ADDR current_low, current_high;
10355
10356 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10357 {
10358 best_low = current_low;
10359 best_high = current_high;
10360 }
10361 else
10362 {
10363 struct die_info *child = die->child;
10364
10365 while (child && child->tag)
10366 {
10367 switch (child->tag) {
10368 case DW_TAG_subprogram:
10369 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10370 break;
10371 case DW_TAG_namespace:
10372 case DW_TAG_module:
10373 /* FIXME: carlton/2004-01-16: Should we do this for
10374 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10375 that current GCC's always emit the DIEs corresponding
10376 to definitions of methods of classes as children of a
10377 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10378 the DIEs giving the declarations, which could be
10379 anywhere). But I don't see any reason why the
10380 standards says that they have to be there. */
10381 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10382
10383 if (current_low != ((CORE_ADDR) -1))
10384 {
10385 best_low = min (best_low, current_low);
10386 best_high = max (best_high, current_high);
10387 }
10388 break;
10389 default:
10390 /* Ignore. */
10391 break;
10392 }
10393
10394 child = sibling_die (child);
10395 }
10396 }
10397
10398 *lowpc = best_low;
10399 *highpc = best_high;
10400 }
10401
10402 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10403 in DIE. */
10404
10405 static void
10406 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10407 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10408 {
10409 struct objfile *objfile = cu->objfile;
10410 struct attribute *attr;
10411 struct attribute *attr_high;
10412
10413 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10414 if (attr_high)
10415 {
10416 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10417 if (attr)
10418 {
10419 CORE_ADDR low = DW_ADDR (attr);
10420 CORE_ADDR high;
10421 if (attr_high->form == DW_FORM_addr
10422 || attr_high->form == DW_FORM_GNU_addr_index)
10423 high = DW_ADDR (attr_high);
10424 else
10425 high = low + DW_UNSND (attr_high);
10426
10427 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10428 }
10429 }
10430
10431 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10432 if (attr)
10433 {
10434 bfd *obfd = objfile->obfd;
10435 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10436 We take advantage of the fact that DW_AT_ranges does not appear
10437 in DW_TAG_compile_unit of DWO files. */
10438 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10439
10440 /* The value of the DW_AT_ranges attribute is the offset of the
10441 address range list in the .debug_ranges section. */
10442 unsigned long offset = (DW_UNSND (attr)
10443 + (need_ranges_base ? cu->ranges_base : 0));
10444 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10445
10446 /* For some target architectures, but not others, the
10447 read_address function sign-extends the addresses it returns.
10448 To recognize base address selection entries, we need a
10449 mask. */
10450 unsigned int addr_size = cu->header.addr_size;
10451 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10452
10453 /* The base address, to which the next pair is relative. Note
10454 that this 'base' is a DWARF concept: most entries in a range
10455 list are relative, to reduce the number of relocs against the
10456 debugging information. This is separate from this function's
10457 'baseaddr' argument, which GDB uses to relocate debugging
10458 information from a shared library based on the address at
10459 which the library was loaded. */
10460 CORE_ADDR base = cu->base_address;
10461 int base_known = cu->base_known;
10462
10463 gdb_assert (dwarf2_per_objfile->ranges.readin);
10464 if (offset >= dwarf2_per_objfile->ranges.size)
10465 {
10466 complaint (&symfile_complaints,
10467 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10468 offset);
10469 return;
10470 }
10471
10472 for (;;)
10473 {
10474 unsigned int bytes_read;
10475 CORE_ADDR start, end;
10476
10477 start = read_address (obfd, buffer, cu, &bytes_read);
10478 buffer += bytes_read;
10479 end = read_address (obfd, buffer, cu, &bytes_read);
10480 buffer += bytes_read;
10481
10482 /* Did we find the end of the range list? */
10483 if (start == 0 && end == 0)
10484 break;
10485
10486 /* Did we find a base address selection entry? */
10487 else if ((start & base_select_mask) == base_select_mask)
10488 {
10489 base = end;
10490 base_known = 1;
10491 }
10492
10493 /* We found an ordinary address range. */
10494 else
10495 {
10496 if (!base_known)
10497 {
10498 complaint (&symfile_complaints,
10499 _("Invalid .debug_ranges data "
10500 "(no base address)"));
10501 return;
10502 }
10503
10504 if (start > end)
10505 {
10506 /* Inverted range entries are invalid. */
10507 complaint (&symfile_complaints,
10508 _("Invalid .debug_ranges data "
10509 "(inverted range)"));
10510 return;
10511 }
10512
10513 /* Empty range entries have no effect. */
10514 if (start == end)
10515 continue;
10516
10517 start += base + baseaddr;
10518 end += base + baseaddr;
10519
10520 /* A not-uncommon case of bad debug info.
10521 Don't pollute the addrmap with bad data. */
10522 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10523 {
10524 complaint (&symfile_complaints,
10525 _(".debug_ranges entry has start address of zero"
10526 " [in module %s]"), objfile->name);
10527 continue;
10528 }
10529
10530 record_block_range (block, start, end - 1);
10531 }
10532 }
10533 }
10534 }
10535
10536 /* Check whether the producer field indicates either of GCC < 4.6, or the
10537 Intel C/C++ compiler, and cache the result in CU. */
10538
10539 static void
10540 check_producer (struct dwarf2_cu *cu)
10541 {
10542 const char *cs;
10543 int major, minor, release;
10544
10545 if (cu->producer == NULL)
10546 {
10547 /* For unknown compilers expect their behavior is DWARF version
10548 compliant.
10549
10550 GCC started to support .debug_types sections by -gdwarf-4 since
10551 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10552 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10553 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10554 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10555 }
10556 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10557 {
10558 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10559
10560 cs = &cu->producer[strlen ("GNU ")];
10561 while (*cs && !isdigit (*cs))
10562 cs++;
10563 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10564 {
10565 /* Not recognized as GCC. */
10566 }
10567 else
10568 {
10569 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10570 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10571 }
10572 }
10573 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10574 cu->producer_is_icc = 1;
10575 else
10576 {
10577 /* For other non-GCC compilers, expect their behavior is DWARF version
10578 compliant. */
10579 }
10580
10581 cu->checked_producer = 1;
10582 }
10583
10584 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10585 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10586 during 4.6.0 experimental. */
10587
10588 static int
10589 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10590 {
10591 if (!cu->checked_producer)
10592 check_producer (cu);
10593
10594 return cu->producer_is_gxx_lt_4_6;
10595 }
10596
10597 /* Return the default accessibility type if it is not overriden by
10598 DW_AT_accessibility. */
10599
10600 static enum dwarf_access_attribute
10601 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10602 {
10603 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10604 {
10605 /* The default DWARF 2 accessibility for members is public, the default
10606 accessibility for inheritance is private. */
10607
10608 if (die->tag != DW_TAG_inheritance)
10609 return DW_ACCESS_public;
10610 else
10611 return DW_ACCESS_private;
10612 }
10613 else
10614 {
10615 /* DWARF 3+ defines the default accessibility a different way. The same
10616 rules apply now for DW_TAG_inheritance as for the members and it only
10617 depends on the container kind. */
10618
10619 if (die->parent->tag == DW_TAG_class_type)
10620 return DW_ACCESS_private;
10621 else
10622 return DW_ACCESS_public;
10623 }
10624 }
10625
10626 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10627 offset. If the attribute was not found return 0, otherwise return
10628 1. If it was found but could not properly be handled, set *OFFSET
10629 to 0. */
10630
10631 static int
10632 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10633 LONGEST *offset)
10634 {
10635 struct attribute *attr;
10636
10637 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10638 if (attr != NULL)
10639 {
10640 *offset = 0;
10641
10642 /* Note that we do not check for a section offset first here.
10643 This is because DW_AT_data_member_location is new in DWARF 4,
10644 so if we see it, we can assume that a constant form is really
10645 a constant and not a section offset. */
10646 if (attr_form_is_constant (attr))
10647 *offset = dwarf2_get_attr_constant_value (attr, 0);
10648 else if (attr_form_is_section_offset (attr))
10649 dwarf2_complex_location_expr_complaint ();
10650 else if (attr_form_is_block (attr))
10651 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10652 else
10653 dwarf2_complex_location_expr_complaint ();
10654
10655 return 1;
10656 }
10657
10658 return 0;
10659 }
10660
10661 /* Add an aggregate field to the field list. */
10662
10663 static void
10664 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10665 struct dwarf2_cu *cu)
10666 {
10667 struct objfile *objfile = cu->objfile;
10668 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10669 struct nextfield *new_field;
10670 struct attribute *attr;
10671 struct field *fp;
10672 const char *fieldname = "";
10673
10674 /* Allocate a new field list entry and link it in. */
10675 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10676 make_cleanup (xfree, new_field);
10677 memset (new_field, 0, sizeof (struct nextfield));
10678
10679 if (die->tag == DW_TAG_inheritance)
10680 {
10681 new_field->next = fip->baseclasses;
10682 fip->baseclasses = new_field;
10683 }
10684 else
10685 {
10686 new_field->next = fip->fields;
10687 fip->fields = new_field;
10688 }
10689 fip->nfields++;
10690
10691 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10692 if (attr)
10693 new_field->accessibility = DW_UNSND (attr);
10694 else
10695 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10696 if (new_field->accessibility != DW_ACCESS_public)
10697 fip->non_public_fields = 1;
10698
10699 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10700 if (attr)
10701 new_field->virtuality = DW_UNSND (attr);
10702 else
10703 new_field->virtuality = DW_VIRTUALITY_none;
10704
10705 fp = &new_field->field;
10706
10707 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10708 {
10709 LONGEST offset;
10710
10711 /* Data member other than a C++ static data member. */
10712
10713 /* Get type of field. */
10714 fp->type = die_type (die, cu);
10715
10716 SET_FIELD_BITPOS (*fp, 0);
10717
10718 /* Get bit size of field (zero if none). */
10719 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10720 if (attr)
10721 {
10722 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10723 }
10724 else
10725 {
10726 FIELD_BITSIZE (*fp) = 0;
10727 }
10728
10729 /* Get bit offset of field. */
10730 if (handle_data_member_location (die, cu, &offset))
10731 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10732 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10733 if (attr)
10734 {
10735 if (gdbarch_bits_big_endian (gdbarch))
10736 {
10737 /* For big endian bits, the DW_AT_bit_offset gives the
10738 additional bit offset from the MSB of the containing
10739 anonymous object to the MSB of the field. We don't
10740 have to do anything special since we don't need to
10741 know the size of the anonymous object. */
10742 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10743 }
10744 else
10745 {
10746 /* For little endian bits, compute the bit offset to the
10747 MSB of the anonymous object, subtract off the number of
10748 bits from the MSB of the field to the MSB of the
10749 object, and then subtract off the number of bits of
10750 the field itself. The result is the bit offset of
10751 the LSB of the field. */
10752 int anonymous_size;
10753 int bit_offset = DW_UNSND (attr);
10754
10755 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10756 if (attr)
10757 {
10758 /* The size of the anonymous object containing
10759 the bit field is explicit, so use the
10760 indicated size (in bytes). */
10761 anonymous_size = DW_UNSND (attr);
10762 }
10763 else
10764 {
10765 /* The size of the anonymous object containing
10766 the bit field must be inferred from the type
10767 attribute of the data member containing the
10768 bit field. */
10769 anonymous_size = TYPE_LENGTH (fp->type);
10770 }
10771 SET_FIELD_BITPOS (*fp,
10772 (FIELD_BITPOS (*fp)
10773 + anonymous_size * bits_per_byte
10774 - bit_offset - FIELD_BITSIZE (*fp)));
10775 }
10776 }
10777
10778 /* Get name of field. */
10779 fieldname = dwarf2_name (die, cu);
10780 if (fieldname == NULL)
10781 fieldname = "";
10782
10783 /* The name is already allocated along with this objfile, so we don't
10784 need to duplicate it for the type. */
10785 fp->name = fieldname;
10786
10787 /* Change accessibility for artificial fields (e.g. virtual table
10788 pointer or virtual base class pointer) to private. */
10789 if (dwarf2_attr (die, DW_AT_artificial, cu))
10790 {
10791 FIELD_ARTIFICIAL (*fp) = 1;
10792 new_field->accessibility = DW_ACCESS_private;
10793 fip->non_public_fields = 1;
10794 }
10795 }
10796 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10797 {
10798 /* C++ static member. */
10799
10800 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10801 is a declaration, but all versions of G++ as of this writing
10802 (so through at least 3.2.1) incorrectly generate
10803 DW_TAG_variable tags. */
10804
10805 const char *physname;
10806
10807 /* Get name of field. */
10808 fieldname = dwarf2_name (die, cu);
10809 if (fieldname == NULL)
10810 return;
10811
10812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10813 if (attr
10814 /* Only create a symbol if this is an external value.
10815 new_symbol checks this and puts the value in the global symbol
10816 table, which we want. If it is not external, new_symbol
10817 will try to put the value in cu->list_in_scope which is wrong. */
10818 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10819 {
10820 /* A static const member, not much different than an enum as far as
10821 we're concerned, except that we can support more types. */
10822 new_symbol (die, NULL, cu);
10823 }
10824
10825 /* Get physical name. */
10826 physname = dwarf2_physname (fieldname, die, cu);
10827
10828 /* The name is already allocated along with this objfile, so we don't
10829 need to duplicate it for the type. */
10830 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10831 FIELD_TYPE (*fp) = die_type (die, cu);
10832 FIELD_NAME (*fp) = fieldname;
10833 }
10834 else if (die->tag == DW_TAG_inheritance)
10835 {
10836 LONGEST offset;
10837
10838 /* C++ base class field. */
10839 if (handle_data_member_location (die, cu, &offset))
10840 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10841 FIELD_BITSIZE (*fp) = 0;
10842 FIELD_TYPE (*fp) = die_type (die, cu);
10843 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10844 fip->nbaseclasses++;
10845 }
10846 }
10847
10848 /* Add a typedef defined in the scope of the FIP's class. */
10849
10850 static void
10851 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10852 struct dwarf2_cu *cu)
10853 {
10854 struct objfile *objfile = cu->objfile;
10855 struct typedef_field_list *new_field;
10856 struct attribute *attr;
10857 struct typedef_field *fp;
10858 char *fieldname = "";
10859
10860 /* Allocate a new field list entry and link it in. */
10861 new_field = xzalloc (sizeof (*new_field));
10862 make_cleanup (xfree, new_field);
10863
10864 gdb_assert (die->tag == DW_TAG_typedef);
10865
10866 fp = &new_field->field;
10867
10868 /* Get name of field. */
10869 fp->name = dwarf2_name (die, cu);
10870 if (fp->name == NULL)
10871 return;
10872
10873 fp->type = read_type_die (die, cu);
10874
10875 new_field->next = fip->typedef_field_list;
10876 fip->typedef_field_list = new_field;
10877 fip->typedef_field_list_count++;
10878 }
10879
10880 /* Create the vector of fields, and attach it to the type. */
10881
10882 static void
10883 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10884 struct dwarf2_cu *cu)
10885 {
10886 int nfields = fip->nfields;
10887
10888 /* Record the field count, allocate space for the array of fields,
10889 and create blank accessibility bitfields if necessary. */
10890 TYPE_NFIELDS (type) = nfields;
10891 TYPE_FIELDS (type) = (struct field *)
10892 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10893 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10894
10895 if (fip->non_public_fields && cu->language != language_ada)
10896 {
10897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10898
10899 TYPE_FIELD_PRIVATE_BITS (type) =
10900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10901 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10902
10903 TYPE_FIELD_PROTECTED_BITS (type) =
10904 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10905 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10906
10907 TYPE_FIELD_IGNORE_BITS (type) =
10908 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10909 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10910 }
10911
10912 /* If the type has baseclasses, allocate and clear a bit vector for
10913 TYPE_FIELD_VIRTUAL_BITS. */
10914 if (fip->nbaseclasses && cu->language != language_ada)
10915 {
10916 int num_bytes = B_BYTES (fip->nbaseclasses);
10917 unsigned char *pointer;
10918
10919 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10920 pointer = TYPE_ALLOC (type, num_bytes);
10921 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10922 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10923 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10924 }
10925
10926 /* Copy the saved-up fields into the field vector. Start from the head of
10927 the list, adding to the tail of the field array, so that they end up in
10928 the same order in the array in which they were added to the list. */
10929 while (nfields-- > 0)
10930 {
10931 struct nextfield *fieldp;
10932
10933 if (fip->fields)
10934 {
10935 fieldp = fip->fields;
10936 fip->fields = fieldp->next;
10937 }
10938 else
10939 {
10940 fieldp = fip->baseclasses;
10941 fip->baseclasses = fieldp->next;
10942 }
10943
10944 TYPE_FIELD (type, nfields) = fieldp->field;
10945 switch (fieldp->accessibility)
10946 {
10947 case DW_ACCESS_private:
10948 if (cu->language != language_ada)
10949 SET_TYPE_FIELD_PRIVATE (type, nfields);
10950 break;
10951
10952 case DW_ACCESS_protected:
10953 if (cu->language != language_ada)
10954 SET_TYPE_FIELD_PROTECTED (type, nfields);
10955 break;
10956
10957 case DW_ACCESS_public:
10958 break;
10959
10960 default:
10961 /* Unknown accessibility. Complain and treat it as public. */
10962 {
10963 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10964 fieldp->accessibility);
10965 }
10966 break;
10967 }
10968 if (nfields < fip->nbaseclasses)
10969 {
10970 switch (fieldp->virtuality)
10971 {
10972 case DW_VIRTUALITY_virtual:
10973 case DW_VIRTUALITY_pure_virtual:
10974 if (cu->language == language_ada)
10975 error (_("unexpected virtuality in component of Ada type"));
10976 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10977 break;
10978 }
10979 }
10980 }
10981 }
10982
10983 /* Return true if this member function is a constructor, false
10984 otherwise. */
10985
10986 static int
10987 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10988 {
10989 const char *fieldname;
10990 const char *typename;
10991 int len;
10992
10993 if (die->parent == NULL)
10994 return 0;
10995
10996 if (die->parent->tag != DW_TAG_structure_type
10997 && die->parent->tag != DW_TAG_union_type
10998 && die->parent->tag != DW_TAG_class_type)
10999 return 0;
11000
11001 fieldname = dwarf2_name (die, cu);
11002 typename = dwarf2_name (die->parent, cu);
11003 if (fieldname == NULL || typename == NULL)
11004 return 0;
11005
11006 len = strlen (fieldname);
11007 return (strncmp (fieldname, typename, len) == 0
11008 && (typename[len] == '\0' || typename[len] == '<'));
11009 }
11010
11011 /* Add a member function to the proper fieldlist. */
11012
11013 static void
11014 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11015 struct type *type, struct dwarf2_cu *cu)
11016 {
11017 struct objfile *objfile = cu->objfile;
11018 struct attribute *attr;
11019 struct fnfieldlist *flp;
11020 int i;
11021 struct fn_field *fnp;
11022 const char *fieldname;
11023 struct nextfnfield *new_fnfield;
11024 struct type *this_type;
11025 enum dwarf_access_attribute accessibility;
11026
11027 if (cu->language == language_ada)
11028 error (_("unexpected member function in Ada type"));
11029
11030 /* Get name of member function. */
11031 fieldname = dwarf2_name (die, cu);
11032 if (fieldname == NULL)
11033 return;
11034
11035 /* Look up member function name in fieldlist. */
11036 for (i = 0; i < fip->nfnfields; i++)
11037 {
11038 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11039 break;
11040 }
11041
11042 /* Create new list element if necessary. */
11043 if (i < fip->nfnfields)
11044 flp = &fip->fnfieldlists[i];
11045 else
11046 {
11047 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11048 {
11049 fip->fnfieldlists = (struct fnfieldlist *)
11050 xrealloc (fip->fnfieldlists,
11051 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11052 * sizeof (struct fnfieldlist));
11053 if (fip->nfnfields == 0)
11054 make_cleanup (free_current_contents, &fip->fnfieldlists);
11055 }
11056 flp = &fip->fnfieldlists[fip->nfnfields];
11057 flp->name = fieldname;
11058 flp->length = 0;
11059 flp->head = NULL;
11060 i = fip->nfnfields++;
11061 }
11062
11063 /* Create a new member function field and chain it to the field list
11064 entry. */
11065 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11066 make_cleanup (xfree, new_fnfield);
11067 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11068 new_fnfield->next = flp->head;
11069 flp->head = new_fnfield;
11070 flp->length++;
11071
11072 /* Fill in the member function field info. */
11073 fnp = &new_fnfield->fnfield;
11074
11075 /* Delay processing of the physname until later. */
11076 if (cu->language == language_cplus || cu->language == language_java)
11077 {
11078 add_to_method_list (type, i, flp->length - 1, fieldname,
11079 die, cu);
11080 }
11081 else
11082 {
11083 const char *physname = dwarf2_physname (fieldname, die, cu);
11084 fnp->physname = physname ? physname : "";
11085 }
11086
11087 fnp->type = alloc_type (objfile);
11088 this_type = read_type_die (die, cu);
11089 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11090 {
11091 int nparams = TYPE_NFIELDS (this_type);
11092
11093 /* TYPE is the domain of this method, and THIS_TYPE is the type
11094 of the method itself (TYPE_CODE_METHOD). */
11095 smash_to_method_type (fnp->type, type,
11096 TYPE_TARGET_TYPE (this_type),
11097 TYPE_FIELDS (this_type),
11098 TYPE_NFIELDS (this_type),
11099 TYPE_VARARGS (this_type));
11100
11101 /* Handle static member functions.
11102 Dwarf2 has no clean way to discern C++ static and non-static
11103 member functions. G++ helps GDB by marking the first
11104 parameter for non-static member functions (which is the this
11105 pointer) as artificial. We obtain this information from
11106 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11107 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11108 fnp->voffset = VOFFSET_STATIC;
11109 }
11110 else
11111 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11112 dwarf2_full_name (fieldname, die, cu));
11113
11114 /* Get fcontext from DW_AT_containing_type if present. */
11115 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11116 fnp->fcontext = die_containing_type (die, cu);
11117
11118 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11119 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11120
11121 /* Get accessibility. */
11122 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11123 if (attr)
11124 accessibility = DW_UNSND (attr);
11125 else
11126 accessibility = dwarf2_default_access_attribute (die, cu);
11127 switch (accessibility)
11128 {
11129 case DW_ACCESS_private:
11130 fnp->is_private = 1;
11131 break;
11132 case DW_ACCESS_protected:
11133 fnp->is_protected = 1;
11134 break;
11135 }
11136
11137 /* Check for artificial methods. */
11138 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11139 if (attr && DW_UNSND (attr) != 0)
11140 fnp->is_artificial = 1;
11141
11142 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11143
11144 /* Get index in virtual function table if it is a virtual member
11145 function. For older versions of GCC, this is an offset in the
11146 appropriate virtual table, as specified by DW_AT_containing_type.
11147 For everyone else, it is an expression to be evaluated relative
11148 to the object address. */
11149
11150 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11151 if (attr)
11152 {
11153 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11154 {
11155 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11156 {
11157 /* Old-style GCC. */
11158 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11159 }
11160 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11161 || (DW_BLOCK (attr)->size > 1
11162 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11163 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11164 {
11165 struct dwarf_block blk;
11166 int offset;
11167
11168 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11169 ? 1 : 2);
11170 blk.size = DW_BLOCK (attr)->size - offset;
11171 blk.data = DW_BLOCK (attr)->data + offset;
11172 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11173 if ((fnp->voffset % cu->header.addr_size) != 0)
11174 dwarf2_complex_location_expr_complaint ();
11175 else
11176 fnp->voffset /= cu->header.addr_size;
11177 fnp->voffset += 2;
11178 }
11179 else
11180 dwarf2_complex_location_expr_complaint ();
11181
11182 if (!fnp->fcontext)
11183 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11184 }
11185 else if (attr_form_is_section_offset (attr))
11186 {
11187 dwarf2_complex_location_expr_complaint ();
11188 }
11189 else
11190 {
11191 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11192 fieldname);
11193 }
11194 }
11195 else
11196 {
11197 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11198 if (attr && DW_UNSND (attr))
11199 {
11200 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11201 complaint (&symfile_complaints,
11202 _("Member function \"%s\" (offset %d) is virtual "
11203 "but the vtable offset is not specified"),
11204 fieldname, die->offset.sect_off);
11205 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11206 TYPE_CPLUS_DYNAMIC (type) = 1;
11207 }
11208 }
11209 }
11210
11211 /* Create the vector of member function fields, and attach it to the type. */
11212
11213 static void
11214 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11215 struct dwarf2_cu *cu)
11216 {
11217 struct fnfieldlist *flp;
11218 int i;
11219
11220 if (cu->language == language_ada)
11221 error (_("unexpected member functions in Ada type"));
11222
11223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11224 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11225 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11226
11227 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11228 {
11229 struct nextfnfield *nfp = flp->head;
11230 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11231 int k;
11232
11233 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11234 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11235 fn_flp->fn_fields = (struct fn_field *)
11236 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11237 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11238 fn_flp->fn_fields[k] = nfp->fnfield;
11239 }
11240
11241 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11242 }
11243
11244 /* Returns non-zero if NAME is the name of a vtable member in CU's
11245 language, zero otherwise. */
11246 static int
11247 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11248 {
11249 static const char vptr[] = "_vptr";
11250 static const char vtable[] = "vtable";
11251
11252 /* Look for the C++ and Java forms of the vtable. */
11253 if ((cu->language == language_java
11254 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11255 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11256 && is_cplus_marker (name[sizeof (vptr) - 1])))
11257 return 1;
11258
11259 return 0;
11260 }
11261
11262 /* GCC outputs unnamed structures that are really pointers to member
11263 functions, with the ABI-specified layout. If TYPE describes
11264 such a structure, smash it into a member function type.
11265
11266 GCC shouldn't do this; it should just output pointer to member DIEs.
11267 This is GCC PR debug/28767. */
11268
11269 static void
11270 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11271 {
11272 struct type *pfn_type, *domain_type, *new_type;
11273
11274 /* Check for a structure with no name and two children. */
11275 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11276 return;
11277
11278 /* Check for __pfn and __delta members. */
11279 if (TYPE_FIELD_NAME (type, 0) == NULL
11280 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11281 || TYPE_FIELD_NAME (type, 1) == NULL
11282 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11283 return;
11284
11285 /* Find the type of the method. */
11286 pfn_type = TYPE_FIELD_TYPE (type, 0);
11287 if (pfn_type == NULL
11288 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11289 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11290 return;
11291
11292 /* Look for the "this" argument. */
11293 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11294 if (TYPE_NFIELDS (pfn_type) == 0
11295 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11296 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11297 return;
11298
11299 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11300 new_type = alloc_type (objfile);
11301 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11302 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11303 TYPE_VARARGS (pfn_type));
11304 smash_to_methodptr_type (type, new_type);
11305 }
11306
11307 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11308 (icc). */
11309
11310 static int
11311 producer_is_icc (struct dwarf2_cu *cu)
11312 {
11313 if (!cu->checked_producer)
11314 check_producer (cu);
11315
11316 return cu->producer_is_icc;
11317 }
11318
11319 /* Called when we find the DIE that starts a structure or union scope
11320 (definition) to create a type for the structure or union. Fill in
11321 the type's name and general properties; the members will not be
11322 processed until process_structure_type.
11323
11324 NOTE: we need to call these functions regardless of whether or not the
11325 DIE has a DW_AT_name attribute, since it might be an anonymous
11326 structure or union. This gets the type entered into our set of
11327 user defined types.
11328
11329 However, if the structure is incomplete (an opaque struct/union)
11330 then suppress creating a symbol table entry for it since gdb only
11331 wants to find the one with the complete definition. Note that if
11332 it is complete, we just call new_symbol, which does it's own
11333 checking about whether the struct/union is anonymous or not (and
11334 suppresses creating a symbol table entry itself). */
11335
11336 static struct type *
11337 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11338 {
11339 struct objfile *objfile = cu->objfile;
11340 struct type *type;
11341 struct attribute *attr;
11342 const char *name;
11343
11344 /* If the definition of this type lives in .debug_types, read that type.
11345 Don't follow DW_AT_specification though, that will take us back up
11346 the chain and we want to go down. */
11347 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11348 if (attr)
11349 {
11350 struct dwarf2_cu *type_cu = cu;
11351 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11352
11353 /* We could just recurse on read_structure_type, but we need to call
11354 get_die_type to ensure only one type for this DIE is created.
11355 This is important, for example, because for c++ classes we need
11356 TYPE_NAME set which is only done by new_symbol. Blech. */
11357 type = read_type_die (type_die, type_cu);
11358
11359 /* TYPE_CU may not be the same as CU.
11360 Ensure TYPE is recorded in CU's type_hash table. */
11361 return set_die_type (die, type, cu);
11362 }
11363
11364 type = alloc_type (objfile);
11365 INIT_CPLUS_SPECIFIC (type);
11366
11367 name = dwarf2_name (die, cu);
11368 if (name != NULL)
11369 {
11370 if (cu->language == language_cplus
11371 || cu->language == language_java)
11372 {
11373 const char *full_name = dwarf2_full_name (name, die, cu);
11374
11375 /* dwarf2_full_name might have already finished building the DIE's
11376 type. If so, there is no need to continue. */
11377 if (get_die_type (die, cu) != NULL)
11378 return get_die_type (die, cu);
11379
11380 TYPE_TAG_NAME (type) = full_name;
11381 if (die->tag == DW_TAG_structure_type
11382 || die->tag == DW_TAG_class_type)
11383 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11384 }
11385 else
11386 {
11387 /* The name is already allocated along with this objfile, so
11388 we don't need to duplicate it for the type. */
11389 TYPE_TAG_NAME (type) = name;
11390 if (die->tag == DW_TAG_class_type)
11391 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11392 }
11393 }
11394
11395 if (die->tag == DW_TAG_structure_type)
11396 {
11397 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11398 }
11399 else if (die->tag == DW_TAG_union_type)
11400 {
11401 TYPE_CODE (type) = TYPE_CODE_UNION;
11402 }
11403 else
11404 {
11405 TYPE_CODE (type) = TYPE_CODE_CLASS;
11406 }
11407
11408 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11409 TYPE_DECLARED_CLASS (type) = 1;
11410
11411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11412 if (attr)
11413 {
11414 TYPE_LENGTH (type) = DW_UNSND (attr);
11415 }
11416 else
11417 {
11418 TYPE_LENGTH (type) = 0;
11419 }
11420
11421 if (producer_is_icc (cu))
11422 {
11423 /* ICC does not output the required DW_AT_declaration
11424 on incomplete types, but gives them a size of zero. */
11425 }
11426 else
11427 TYPE_STUB_SUPPORTED (type) = 1;
11428
11429 if (die_is_declaration (die, cu))
11430 TYPE_STUB (type) = 1;
11431 else if (attr == NULL && die->child == NULL
11432 && producer_is_realview (cu->producer))
11433 /* RealView does not output the required DW_AT_declaration
11434 on incomplete types. */
11435 TYPE_STUB (type) = 1;
11436
11437 /* We need to add the type field to the die immediately so we don't
11438 infinitely recurse when dealing with pointers to the structure
11439 type within the structure itself. */
11440 set_die_type (die, type, cu);
11441
11442 /* set_die_type should be already done. */
11443 set_descriptive_type (type, die, cu);
11444
11445 return type;
11446 }
11447
11448 /* Finish creating a structure or union type, including filling in
11449 its members and creating a symbol for it. */
11450
11451 static void
11452 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11453 {
11454 struct objfile *objfile = cu->objfile;
11455 struct die_info *child_die = die->child;
11456 struct type *type;
11457
11458 type = get_die_type (die, cu);
11459 if (type == NULL)
11460 type = read_structure_type (die, cu);
11461
11462 if (die->child != NULL && ! die_is_declaration (die, cu))
11463 {
11464 struct field_info fi;
11465 struct die_info *child_die;
11466 VEC (symbolp) *template_args = NULL;
11467 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11468
11469 memset (&fi, 0, sizeof (struct field_info));
11470
11471 child_die = die->child;
11472
11473 while (child_die && child_die->tag)
11474 {
11475 if (child_die->tag == DW_TAG_member
11476 || child_die->tag == DW_TAG_variable)
11477 {
11478 /* NOTE: carlton/2002-11-05: A C++ static data member
11479 should be a DW_TAG_member that is a declaration, but
11480 all versions of G++ as of this writing (so through at
11481 least 3.2.1) incorrectly generate DW_TAG_variable
11482 tags for them instead. */
11483 dwarf2_add_field (&fi, child_die, cu);
11484 }
11485 else if (child_die->tag == DW_TAG_subprogram)
11486 {
11487 /* C++ member function. */
11488 dwarf2_add_member_fn (&fi, child_die, type, cu);
11489 }
11490 else if (child_die->tag == DW_TAG_inheritance)
11491 {
11492 /* C++ base class field. */
11493 dwarf2_add_field (&fi, child_die, cu);
11494 }
11495 else if (child_die->tag == DW_TAG_typedef)
11496 dwarf2_add_typedef (&fi, child_die, cu);
11497 else if (child_die->tag == DW_TAG_template_type_param
11498 || child_die->tag == DW_TAG_template_value_param)
11499 {
11500 struct symbol *arg = new_symbol (child_die, NULL, cu);
11501
11502 if (arg != NULL)
11503 VEC_safe_push (symbolp, template_args, arg);
11504 }
11505
11506 child_die = sibling_die (child_die);
11507 }
11508
11509 /* Attach template arguments to type. */
11510 if (! VEC_empty (symbolp, template_args))
11511 {
11512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11513 TYPE_N_TEMPLATE_ARGUMENTS (type)
11514 = VEC_length (symbolp, template_args);
11515 TYPE_TEMPLATE_ARGUMENTS (type)
11516 = obstack_alloc (&objfile->objfile_obstack,
11517 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11518 * sizeof (struct symbol *)));
11519 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11520 VEC_address (symbolp, template_args),
11521 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11522 * sizeof (struct symbol *)));
11523 VEC_free (symbolp, template_args);
11524 }
11525
11526 /* Attach fields and member functions to the type. */
11527 if (fi.nfields)
11528 dwarf2_attach_fields_to_type (&fi, type, cu);
11529 if (fi.nfnfields)
11530 {
11531 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11532
11533 /* Get the type which refers to the base class (possibly this
11534 class itself) which contains the vtable pointer for the current
11535 class from the DW_AT_containing_type attribute. This use of
11536 DW_AT_containing_type is a GNU extension. */
11537
11538 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11539 {
11540 struct type *t = die_containing_type (die, cu);
11541
11542 TYPE_VPTR_BASETYPE (type) = t;
11543 if (type == t)
11544 {
11545 int i;
11546
11547 /* Our own class provides vtbl ptr. */
11548 for (i = TYPE_NFIELDS (t) - 1;
11549 i >= TYPE_N_BASECLASSES (t);
11550 --i)
11551 {
11552 const char *fieldname = TYPE_FIELD_NAME (t, i);
11553
11554 if (is_vtable_name (fieldname, cu))
11555 {
11556 TYPE_VPTR_FIELDNO (type) = i;
11557 break;
11558 }
11559 }
11560
11561 /* Complain if virtual function table field not found. */
11562 if (i < TYPE_N_BASECLASSES (t))
11563 complaint (&symfile_complaints,
11564 _("virtual function table pointer "
11565 "not found when defining class '%s'"),
11566 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11567 "");
11568 }
11569 else
11570 {
11571 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11572 }
11573 }
11574 else if (cu->producer
11575 && strncmp (cu->producer,
11576 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11577 {
11578 /* The IBM XLC compiler does not provide direct indication
11579 of the containing type, but the vtable pointer is
11580 always named __vfp. */
11581
11582 int i;
11583
11584 for (i = TYPE_NFIELDS (type) - 1;
11585 i >= TYPE_N_BASECLASSES (type);
11586 --i)
11587 {
11588 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11589 {
11590 TYPE_VPTR_FIELDNO (type) = i;
11591 TYPE_VPTR_BASETYPE (type) = type;
11592 break;
11593 }
11594 }
11595 }
11596 }
11597
11598 /* Copy fi.typedef_field_list linked list elements content into the
11599 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11600 if (fi.typedef_field_list)
11601 {
11602 int i = fi.typedef_field_list_count;
11603
11604 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11605 TYPE_TYPEDEF_FIELD_ARRAY (type)
11606 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11607 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11608
11609 /* Reverse the list order to keep the debug info elements order. */
11610 while (--i >= 0)
11611 {
11612 struct typedef_field *dest, *src;
11613
11614 dest = &TYPE_TYPEDEF_FIELD (type, i);
11615 src = &fi.typedef_field_list->field;
11616 fi.typedef_field_list = fi.typedef_field_list->next;
11617 *dest = *src;
11618 }
11619 }
11620
11621 do_cleanups (back_to);
11622
11623 if (HAVE_CPLUS_STRUCT (type))
11624 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11625 }
11626
11627 quirk_gcc_member_function_pointer (type, objfile);
11628
11629 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11630 snapshots) has been known to create a die giving a declaration
11631 for a class that has, as a child, a die giving a definition for a
11632 nested class. So we have to process our children even if the
11633 current die is a declaration. Normally, of course, a declaration
11634 won't have any children at all. */
11635
11636 while (child_die != NULL && child_die->tag)
11637 {
11638 if (child_die->tag == DW_TAG_member
11639 || child_die->tag == DW_TAG_variable
11640 || child_die->tag == DW_TAG_inheritance
11641 || child_die->tag == DW_TAG_template_value_param
11642 || child_die->tag == DW_TAG_template_type_param)
11643 {
11644 /* Do nothing. */
11645 }
11646 else
11647 process_die (child_die, cu);
11648
11649 child_die = sibling_die (child_die);
11650 }
11651
11652 /* Do not consider external references. According to the DWARF standard,
11653 these DIEs are identified by the fact that they have no byte_size
11654 attribute, and a declaration attribute. */
11655 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11656 || !die_is_declaration (die, cu))
11657 new_symbol (die, type, cu);
11658 }
11659
11660 /* Given a DW_AT_enumeration_type die, set its type. We do not
11661 complete the type's fields yet, or create any symbols. */
11662
11663 static struct type *
11664 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11665 {
11666 struct objfile *objfile = cu->objfile;
11667 struct type *type;
11668 struct attribute *attr;
11669 const char *name;
11670
11671 /* If the definition of this type lives in .debug_types, read that type.
11672 Don't follow DW_AT_specification though, that will take us back up
11673 the chain and we want to go down. */
11674 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11675 if (attr)
11676 {
11677 struct dwarf2_cu *type_cu = cu;
11678 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11679
11680 type = read_type_die (type_die, type_cu);
11681
11682 /* TYPE_CU may not be the same as CU.
11683 Ensure TYPE is recorded in CU's type_hash table. */
11684 return set_die_type (die, type, cu);
11685 }
11686
11687 type = alloc_type (objfile);
11688
11689 TYPE_CODE (type) = TYPE_CODE_ENUM;
11690 name = dwarf2_full_name (NULL, die, cu);
11691 if (name != NULL)
11692 TYPE_TAG_NAME (type) = name;
11693
11694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11695 if (attr)
11696 {
11697 TYPE_LENGTH (type) = DW_UNSND (attr);
11698 }
11699 else
11700 {
11701 TYPE_LENGTH (type) = 0;
11702 }
11703
11704 /* The enumeration DIE can be incomplete. In Ada, any type can be
11705 declared as private in the package spec, and then defined only
11706 inside the package body. Such types are known as Taft Amendment
11707 Types. When another package uses such a type, an incomplete DIE
11708 may be generated by the compiler. */
11709 if (die_is_declaration (die, cu))
11710 TYPE_STUB (type) = 1;
11711
11712 return set_die_type (die, type, cu);
11713 }
11714
11715 /* Given a pointer to a die which begins an enumeration, process all
11716 the dies that define the members of the enumeration, and create the
11717 symbol for the enumeration type.
11718
11719 NOTE: We reverse the order of the element list. */
11720
11721 static void
11722 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11723 {
11724 struct type *this_type;
11725
11726 this_type = get_die_type (die, cu);
11727 if (this_type == NULL)
11728 this_type = read_enumeration_type (die, cu);
11729
11730 if (die->child != NULL)
11731 {
11732 struct die_info *child_die;
11733 struct symbol *sym;
11734 struct field *fields = NULL;
11735 int num_fields = 0;
11736 int unsigned_enum = 1;
11737 const char *name;
11738 int flag_enum = 1;
11739 ULONGEST mask = 0;
11740
11741 child_die = die->child;
11742 while (child_die && child_die->tag)
11743 {
11744 if (child_die->tag != DW_TAG_enumerator)
11745 {
11746 process_die (child_die, cu);
11747 }
11748 else
11749 {
11750 name = dwarf2_name (child_die, cu);
11751 if (name)
11752 {
11753 sym = new_symbol (child_die, this_type, cu);
11754 if (SYMBOL_VALUE (sym) < 0)
11755 {
11756 unsigned_enum = 0;
11757 flag_enum = 0;
11758 }
11759 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11760 flag_enum = 0;
11761 else
11762 mask |= SYMBOL_VALUE (sym);
11763
11764 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11765 {
11766 fields = (struct field *)
11767 xrealloc (fields,
11768 (num_fields + DW_FIELD_ALLOC_CHUNK)
11769 * sizeof (struct field));
11770 }
11771
11772 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11773 FIELD_TYPE (fields[num_fields]) = NULL;
11774 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11775 FIELD_BITSIZE (fields[num_fields]) = 0;
11776
11777 num_fields++;
11778 }
11779 }
11780
11781 child_die = sibling_die (child_die);
11782 }
11783
11784 if (num_fields)
11785 {
11786 TYPE_NFIELDS (this_type) = num_fields;
11787 TYPE_FIELDS (this_type) = (struct field *)
11788 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11789 memcpy (TYPE_FIELDS (this_type), fields,
11790 sizeof (struct field) * num_fields);
11791 xfree (fields);
11792 }
11793 if (unsigned_enum)
11794 TYPE_UNSIGNED (this_type) = 1;
11795 if (flag_enum)
11796 TYPE_FLAG_ENUM (this_type) = 1;
11797 }
11798
11799 /* If we are reading an enum from a .debug_types unit, and the enum
11800 is a declaration, and the enum is not the signatured type in the
11801 unit, then we do not want to add a symbol for it. Adding a
11802 symbol would in some cases obscure the true definition of the
11803 enum, giving users an incomplete type when the definition is
11804 actually available. Note that we do not want to do this for all
11805 enums which are just declarations, because C++0x allows forward
11806 enum declarations. */
11807 if (cu->per_cu->is_debug_types
11808 && die_is_declaration (die, cu))
11809 {
11810 struct signatured_type *sig_type;
11811
11812 sig_type
11813 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11814 cu->per_cu->section,
11815 cu->per_cu->offset);
11816 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11817 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11818 return;
11819 }
11820
11821 new_symbol (die, this_type, cu);
11822 }
11823
11824 /* Extract all information from a DW_TAG_array_type DIE and put it in
11825 the DIE's type field. For now, this only handles one dimensional
11826 arrays. */
11827
11828 static struct type *
11829 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11830 {
11831 struct objfile *objfile = cu->objfile;
11832 struct die_info *child_die;
11833 struct type *type;
11834 struct type *element_type, *range_type, *index_type;
11835 struct type **range_types = NULL;
11836 struct attribute *attr;
11837 int ndim = 0;
11838 struct cleanup *back_to;
11839 const char *name;
11840
11841 element_type = die_type (die, cu);
11842
11843 /* The die_type call above may have already set the type for this DIE. */
11844 type = get_die_type (die, cu);
11845 if (type)
11846 return type;
11847
11848 /* Irix 6.2 native cc creates array types without children for
11849 arrays with unspecified length. */
11850 if (die->child == NULL)
11851 {
11852 index_type = objfile_type (objfile)->builtin_int;
11853 range_type = create_range_type (NULL, index_type, 0, -1);
11854 type = create_array_type (NULL, element_type, range_type);
11855 return set_die_type (die, type, cu);
11856 }
11857
11858 back_to = make_cleanup (null_cleanup, NULL);
11859 child_die = die->child;
11860 while (child_die && child_die->tag)
11861 {
11862 if (child_die->tag == DW_TAG_subrange_type)
11863 {
11864 struct type *child_type = read_type_die (child_die, cu);
11865
11866 if (child_type != NULL)
11867 {
11868 /* The range type was succesfully read. Save it for the
11869 array type creation. */
11870 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11871 {
11872 range_types = (struct type **)
11873 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11874 * sizeof (struct type *));
11875 if (ndim == 0)
11876 make_cleanup (free_current_contents, &range_types);
11877 }
11878 range_types[ndim++] = child_type;
11879 }
11880 }
11881 child_die = sibling_die (child_die);
11882 }
11883
11884 /* Dwarf2 dimensions are output from left to right, create the
11885 necessary array types in backwards order. */
11886
11887 type = element_type;
11888
11889 if (read_array_order (die, cu) == DW_ORD_col_major)
11890 {
11891 int i = 0;
11892
11893 while (i < ndim)
11894 type = create_array_type (NULL, type, range_types[i++]);
11895 }
11896 else
11897 {
11898 while (ndim-- > 0)
11899 type = create_array_type (NULL, type, range_types[ndim]);
11900 }
11901
11902 /* Understand Dwarf2 support for vector types (like they occur on
11903 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11904 array type. This is not part of the Dwarf2/3 standard yet, but a
11905 custom vendor extension. The main difference between a regular
11906 array and the vector variant is that vectors are passed by value
11907 to functions. */
11908 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11909 if (attr)
11910 make_vector_type (type);
11911
11912 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11913 implementation may choose to implement triple vectors using this
11914 attribute. */
11915 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11916 if (attr)
11917 {
11918 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11919 TYPE_LENGTH (type) = DW_UNSND (attr);
11920 else
11921 complaint (&symfile_complaints,
11922 _("DW_AT_byte_size for array type smaller "
11923 "than the total size of elements"));
11924 }
11925
11926 name = dwarf2_name (die, cu);
11927 if (name)
11928 TYPE_NAME (type) = name;
11929
11930 /* Install the type in the die. */
11931 set_die_type (die, type, cu);
11932
11933 /* set_die_type should be already done. */
11934 set_descriptive_type (type, die, cu);
11935
11936 do_cleanups (back_to);
11937
11938 return type;
11939 }
11940
11941 static enum dwarf_array_dim_ordering
11942 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11943 {
11944 struct attribute *attr;
11945
11946 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11947
11948 if (attr) return DW_SND (attr);
11949
11950 /* GNU F77 is a special case, as at 08/2004 array type info is the
11951 opposite order to the dwarf2 specification, but data is still
11952 laid out as per normal fortran.
11953
11954 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11955 version checking. */
11956
11957 if (cu->language == language_fortran
11958 && cu->producer && strstr (cu->producer, "GNU F77"))
11959 {
11960 return DW_ORD_row_major;
11961 }
11962
11963 switch (cu->language_defn->la_array_ordering)
11964 {
11965 case array_column_major:
11966 return DW_ORD_col_major;
11967 case array_row_major:
11968 default:
11969 return DW_ORD_row_major;
11970 };
11971 }
11972
11973 /* Extract all information from a DW_TAG_set_type DIE and put it in
11974 the DIE's type field. */
11975
11976 static struct type *
11977 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11978 {
11979 struct type *domain_type, *set_type;
11980 struct attribute *attr;
11981
11982 domain_type = die_type (die, cu);
11983
11984 /* The die_type call above may have already set the type for this DIE. */
11985 set_type = get_die_type (die, cu);
11986 if (set_type)
11987 return set_type;
11988
11989 set_type = create_set_type (NULL, domain_type);
11990
11991 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11992 if (attr)
11993 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11994
11995 return set_die_type (die, set_type, cu);
11996 }
11997
11998 /* A helper for read_common_block that creates a locexpr baton.
11999 SYM is the symbol which we are marking as computed.
12000 COMMON_DIE is the DIE for the common block.
12001 COMMON_LOC is the location expression attribute for the common
12002 block itself.
12003 MEMBER_LOC is the location expression attribute for the particular
12004 member of the common block that we are processing.
12005 CU is the CU from which the above come. */
12006
12007 static void
12008 mark_common_block_symbol_computed (struct symbol *sym,
12009 struct die_info *common_die,
12010 struct attribute *common_loc,
12011 struct attribute *member_loc,
12012 struct dwarf2_cu *cu)
12013 {
12014 struct objfile *objfile = dwarf2_per_objfile->objfile;
12015 struct dwarf2_locexpr_baton *baton;
12016 gdb_byte *ptr;
12017 unsigned int cu_off;
12018 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12019 LONGEST offset = 0;
12020
12021 gdb_assert (common_loc && member_loc);
12022 gdb_assert (attr_form_is_block (common_loc));
12023 gdb_assert (attr_form_is_block (member_loc)
12024 || attr_form_is_constant (member_loc));
12025
12026 baton = obstack_alloc (&objfile->objfile_obstack,
12027 sizeof (struct dwarf2_locexpr_baton));
12028 baton->per_cu = cu->per_cu;
12029 gdb_assert (baton->per_cu);
12030
12031 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12032
12033 if (attr_form_is_constant (member_loc))
12034 {
12035 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12036 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12037 }
12038 else
12039 baton->size += DW_BLOCK (member_loc)->size;
12040
12041 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12042 baton->data = ptr;
12043
12044 *ptr++ = DW_OP_call4;
12045 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12046 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12047 ptr += 4;
12048
12049 if (attr_form_is_constant (member_loc))
12050 {
12051 *ptr++ = DW_OP_addr;
12052 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12053 ptr += cu->header.addr_size;
12054 }
12055 else
12056 {
12057 /* We have to copy the data here, because DW_OP_call4 will only
12058 use a DW_AT_location attribute. */
12059 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12060 ptr += DW_BLOCK (member_loc)->size;
12061 }
12062
12063 *ptr++ = DW_OP_plus;
12064 gdb_assert (ptr - baton->data == baton->size);
12065
12066 SYMBOL_LOCATION_BATON (sym) = baton;
12067 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12068 }
12069
12070 /* Create appropriate locally-scoped variables for all the
12071 DW_TAG_common_block entries. Also create a struct common_block
12072 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12073 is used to sepate the common blocks name namespace from regular
12074 variable names. */
12075
12076 static void
12077 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12078 {
12079 struct attribute *attr;
12080
12081 attr = dwarf2_attr (die, DW_AT_location, cu);
12082 if (attr)
12083 {
12084 /* Support the .debug_loc offsets. */
12085 if (attr_form_is_block (attr))
12086 {
12087 /* Ok. */
12088 }
12089 else if (attr_form_is_section_offset (attr))
12090 {
12091 dwarf2_complex_location_expr_complaint ();
12092 attr = NULL;
12093 }
12094 else
12095 {
12096 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12097 "common block member");
12098 attr = NULL;
12099 }
12100 }
12101
12102 if (die->child != NULL)
12103 {
12104 struct objfile *objfile = cu->objfile;
12105 struct die_info *child_die;
12106 size_t n_entries = 0, size;
12107 struct common_block *common_block;
12108 struct symbol *sym;
12109
12110 for (child_die = die->child;
12111 child_die && child_die->tag;
12112 child_die = sibling_die (child_die))
12113 ++n_entries;
12114
12115 size = (sizeof (struct common_block)
12116 + (n_entries - 1) * sizeof (struct symbol *));
12117 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12118 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12119 common_block->n_entries = 0;
12120
12121 for (child_die = die->child;
12122 child_die && child_die->tag;
12123 child_die = sibling_die (child_die))
12124 {
12125 /* Create the symbol in the DW_TAG_common_block block in the current
12126 symbol scope. */
12127 sym = new_symbol (child_die, NULL, cu);
12128 if (sym != NULL)
12129 {
12130 struct attribute *member_loc;
12131
12132 common_block->contents[common_block->n_entries++] = sym;
12133
12134 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12135 cu);
12136 if (member_loc)
12137 {
12138 /* GDB has handled this for a long time, but it is
12139 not specified by DWARF. It seems to have been
12140 emitted by gfortran at least as recently as:
12141 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12142 complaint (&symfile_complaints,
12143 _("Variable in common block has "
12144 "DW_AT_data_member_location "
12145 "- DIE at 0x%x [in module %s]"),
12146 child_die->offset.sect_off, cu->objfile->name);
12147
12148 if (attr_form_is_section_offset (member_loc))
12149 dwarf2_complex_location_expr_complaint ();
12150 else if (attr_form_is_constant (member_loc)
12151 || attr_form_is_block (member_loc))
12152 {
12153 if (attr)
12154 mark_common_block_symbol_computed (sym, die, attr,
12155 member_loc, cu);
12156 }
12157 else
12158 dwarf2_complex_location_expr_complaint ();
12159 }
12160 }
12161 }
12162
12163 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12164 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12165 }
12166 }
12167
12168 /* Create a type for a C++ namespace. */
12169
12170 static struct type *
12171 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12172 {
12173 struct objfile *objfile = cu->objfile;
12174 const char *previous_prefix, *name;
12175 int is_anonymous;
12176 struct type *type;
12177
12178 /* For extensions, reuse the type of the original namespace. */
12179 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12180 {
12181 struct die_info *ext_die;
12182 struct dwarf2_cu *ext_cu = cu;
12183
12184 ext_die = dwarf2_extension (die, &ext_cu);
12185 type = read_type_die (ext_die, ext_cu);
12186
12187 /* EXT_CU may not be the same as CU.
12188 Ensure TYPE is recorded in CU's type_hash table. */
12189 return set_die_type (die, type, cu);
12190 }
12191
12192 name = namespace_name (die, &is_anonymous, cu);
12193
12194 /* Now build the name of the current namespace. */
12195
12196 previous_prefix = determine_prefix (die, cu);
12197 if (previous_prefix[0] != '\0')
12198 name = typename_concat (&objfile->objfile_obstack,
12199 previous_prefix, name, 0, cu);
12200
12201 /* Create the type. */
12202 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12203 objfile);
12204 TYPE_NAME (type) = name;
12205 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12206
12207 return set_die_type (die, type, cu);
12208 }
12209
12210 /* Read a C++ namespace. */
12211
12212 static void
12213 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12214 {
12215 struct objfile *objfile = cu->objfile;
12216 int is_anonymous;
12217
12218 /* Add a symbol associated to this if we haven't seen the namespace
12219 before. Also, add a using directive if it's an anonymous
12220 namespace. */
12221
12222 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12223 {
12224 struct type *type;
12225
12226 type = read_type_die (die, cu);
12227 new_symbol (die, type, cu);
12228
12229 namespace_name (die, &is_anonymous, cu);
12230 if (is_anonymous)
12231 {
12232 const char *previous_prefix = determine_prefix (die, cu);
12233
12234 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12235 NULL, NULL, 0, &objfile->objfile_obstack);
12236 }
12237 }
12238
12239 if (die->child != NULL)
12240 {
12241 struct die_info *child_die = die->child;
12242
12243 while (child_die && child_die->tag)
12244 {
12245 process_die (child_die, cu);
12246 child_die = sibling_die (child_die);
12247 }
12248 }
12249 }
12250
12251 /* Read a Fortran module as type. This DIE can be only a declaration used for
12252 imported module. Still we need that type as local Fortran "use ... only"
12253 declaration imports depend on the created type in determine_prefix. */
12254
12255 static struct type *
12256 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12257 {
12258 struct objfile *objfile = cu->objfile;
12259 const char *module_name;
12260 struct type *type;
12261
12262 module_name = dwarf2_name (die, cu);
12263 if (!module_name)
12264 complaint (&symfile_complaints,
12265 _("DW_TAG_module has no name, offset 0x%x"),
12266 die->offset.sect_off);
12267 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12268
12269 /* determine_prefix uses TYPE_TAG_NAME. */
12270 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12271
12272 return set_die_type (die, type, cu);
12273 }
12274
12275 /* Read a Fortran module. */
12276
12277 static void
12278 read_module (struct die_info *die, struct dwarf2_cu *cu)
12279 {
12280 struct die_info *child_die = die->child;
12281
12282 while (child_die && child_die->tag)
12283 {
12284 process_die (child_die, cu);
12285 child_die = sibling_die (child_die);
12286 }
12287 }
12288
12289 /* Return the name of the namespace represented by DIE. Set
12290 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12291 namespace. */
12292
12293 static const char *
12294 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12295 {
12296 struct die_info *current_die;
12297 const char *name = NULL;
12298
12299 /* Loop through the extensions until we find a name. */
12300
12301 for (current_die = die;
12302 current_die != NULL;
12303 current_die = dwarf2_extension (die, &cu))
12304 {
12305 name = dwarf2_name (current_die, cu);
12306 if (name != NULL)
12307 break;
12308 }
12309
12310 /* Is it an anonymous namespace? */
12311
12312 *is_anonymous = (name == NULL);
12313 if (*is_anonymous)
12314 name = CP_ANONYMOUS_NAMESPACE_STR;
12315
12316 return name;
12317 }
12318
12319 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12320 the user defined type vector. */
12321
12322 static struct type *
12323 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12324 {
12325 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12326 struct comp_unit_head *cu_header = &cu->header;
12327 struct type *type;
12328 struct attribute *attr_byte_size;
12329 struct attribute *attr_address_class;
12330 int byte_size, addr_class;
12331 struct type *target_type;
12332
12333 target_type = die_type (die, cu);
12334
12335 /* The die_type call above may have already set the type for this DIE. */
12336 type = get_die_type (die, cu);
12337 if (type)
12338 return type;
12339
12340 type = lookup_pointer_type (target_type);
12341
12342 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12343 if (attr_byte_size)
12344 byte_size = DW_UNSND (attr_byte_size);
12345 else
12346 byte_size = cu_header->addr_size;
12347
12348 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12349 if (attr_address_class)
12350 addr_class = DW_UNSND (attr_address_class);
12351 else
12352 addr_class = DW_ADDR_none;
12353
12354 /* If the pointer size or address class is different than the
12355 default, create a type variant marked as such and set the
12356 length accordingly. */
12357 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12358 {
12359 if (gdbarch_address_class_type_flags_p (gdbarch))
12360 {
12361 int type_flags;
12362
12363 type_flags = gdbarch_address_class_type_flags
12364 (gdbarch, byte_size, addr_class);
12365 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12366 == 0);
12367 type = make_type_with_address_space (type, type_flags);
12368 }
12369 else if (TYPE_LENGTH (type) != byte_size)
12370 {
12371 complaint (&symfile_complaints,
12372 _("invalid pointer size %d"), byte_size);
12373 }
12374 else
12375 {
12376 /* Should we also complain about unhandled address classes? */
12377 }
12378 }
12379
12380 TYPE_LENGTH (type) = byte_size;
12381 return set_die_type (die, type, cu);
12382 }
12383
12384 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12385 the user defined type vector. */
12386
12387 static struct type *
12388 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12389 {
12390 struct type *type;
12391 struct type *to_type;
12392 struct type *domain;
12393
12394 to_type = die_type (die, cu);
12395 domain = die_containing_type (die, cu);
12396
12397 /* The calls above may have already set the type for this DIE. */
12398 type = get_die_type (die, cu);
12399 if (type)
12400 return type;
12401
12402 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12403 type = lookup_methodptr_type (to_type);
12404 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12405 {
12406 struct type *new_type = alloc_type (cu->objfile);
12407
12408 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12409 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12410 TYPE_VARARGS (to_type));
12411 type = lookup_methodptr_type (new_type);
12412 }
12413 else
12414 type = lookup_memberptr_type (to_type, domain);
12415
12416 return set_die_type (die, type, cu);
12417 }
12418
12419 /* Extract all information from a DW_TAG_reference_type DIE and add to
12420 the user defined type vector. */
12421
12422 static struct type *
12423 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12424 {
12425 struct comp_unit_head *cu_header = &cu->header;
12426 struct type *type, *target_type;
12427 struct attribute *attr;
12428
12429 target_type = die_type (die, cu);
12430
12431 /* The die_type call above may have already set the type for this DIE. */
12432 type = get_die_type (die, cu);
12433 if (type)
12434 return type;
12435
12436 type = lookup_reference_type (target_type);
12437 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12438 if (attr)
12439 {
12440 TYPE_LENGTH (type) = DW_UNSND (attr);
12441 }
12442 else
12443 {
12444 TYPE_LENGTH (type) = cu_header->addr_size;
12445 }
12446 return set_die_type (die, type, cu);
12447 }
12448
12449 static struct type *
12450 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12451 {
12452 struct type *base_type, *cv_type;
12453
12454 base_type = die_type (die, cu);
12455
12456 /* The die_type call above may have already set the type for this DIE. */
12457 cv_type = get_die_type (die, cu);
12458 if (cv_type)
12459 return cv_type;
12460
12461 /* In case the const qualifier is applied to an array type, the element type
12462 is so qualified, not the array type (section 6.7.3 of C99). */
12463 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12464 {
12465 struct type *el_type, *inner_array;
12466
12467 base_type = copy_type (base_type);
12468 inner_array = base_type;
12469
12470 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12471 {
12472 TYPE_TARGET_TYPE (inner_array) =
12473 copy_type (TYPE_TARGET_TYPE (inner_array));
12474 inner_array = TYPE_TARGET_TYPE (inner_array);
12475 }
12476
12477 el_type = TYPE_TARGET_TYPE (inner_array);
12478 TYPE_TARGET_TYPE (inner_array) =
12479 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12480
12481 return set_die_type (die, base_type, cu);
12482 }
12483
12484 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12485 return set_die_type (die, cv_type, cu);
12486 }
12487
12488 static struct type *
12489 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12490 {
12491 struct type *base_type, *cv_type;
12492
12493 base_type = die_type (die, cu);
12494
12495 /* The die_type call above may have already set the type for this DIE. */
12496 cv_type = get_die_type (die, cu);
12497 if (cv_type)
12498 return cv_type;
12499
12500 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12501 return set_die_type (die, cv_type, cu);
12502 }
12503
12504 /* Handle DW_TAG_restrict_type. */
12505
12506 static struct type *
12507 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12508 {
12509 struct type *base_type, *cv_type;
12510
12511 base_type = die_type (die, cu);
12512
12513 /* The die_type call above may have already set the type for this DIE. */
12514 cv_type = get_die_type (die, cu);
12515 if (cv_type)
12516 return cv_type;
12517
12518 cv_type = make_restrict_type (base_type);
12519 return set_die_type (die, cv_type, cu);
12520 }
12521
12522 /* Extract all information from a DW_TAG_string_type DIE and add to
12523 the user defined type vector. It isn't really a user defined type,
12524 but it behaves like one, with other DIE's using an AT_user_def_type
12525 attribute to reference it. */
12526
12527 static struct type *
12528 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12529 {
12530 struct objfile *objfile = cu->objfile;
12531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12532 struct type *type, *range_type, *index_type, *char_type;
12533 struct attribute *attr;
12534 unsigned int length;
12535
12536 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12537 if (attr)
12538 {
12539 length = DW_UNSND (attr);
12540 }
12541 else
12542 {
12543 /* Check for the DW_AT_byte_size attribute. */
12544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12545 if (attr)
12546 {
12547 length = DW_UNSND (attr);
12548 }
12549 else
12550 {
12551 length = 1;
12552 }
12553 }
12554
12555 index_type = objfile_type (objfile)->builtin_int;
12556 range_type = create_range_type (NULL, index_type, 1, length);
12557 char_type = language_string_char_type (cu->language_defn, gdbarch);
12558 type = create_string_type (NULL, char_type, range_type);
12559
12560 return set_die_type (die, type, cu);
12561 }
12562
12563 /* Handle DIES due to C code like:
12564
12565 struct foo
12566 {
12567 int (*funcp)(int a, long l);
12568 int b;
12569 };
12570
12571 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12572
12573 static struct type *
12574 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12575 {
12576 struct objfile *objfile = cu->objfile;
12577 struct type *type; /* Type that this function returns. */
12578 struct type *ftype; /* Function that returns above type. */
12579 struct attribute *attr;
12580
12581 type = die_type (die, cu);
12582
12583 /* The die_type call above may have already set the type for this DIE. */
12584 ftype = get_die_type (die, cu);
12585 if (ftype)
12586 return ftype;
12587
12588 ftype = lookup_function_type (type);
12589
12590 /* All functions in C++, Pascal and Java have prototypes. */
12591 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12592 if ((attr && (DW_UNSND (attr) != 0))
12593 || cu->language == language_cplus
12594 || cu->language == language_java
12595 || cu->language == language_pascal)
12596 TYPE_PROTOTYPED (ftype) = 1;
12597 else if (producer_is_realview (cu->producer))
12598 /* RealView does not emit DW_AT_prototyped. We can not
12599 distinguish prototyped and unprototyped functions; default to
12600 prototyped, since that is more common in modern code (and
12601 RealView warns about unprototyped functions). */
12602 TYPE_PROTOTYPED (ftype) = 1;
12603
12604 /* Store the calling convention in the type if it's available in
12605 the subroutine die. Otherwise set the calling convention to
12606 the default value DW_CC_normal. */
12607 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12608 if (attr)
12609 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12610 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12611 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12612 else
12613 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12614
12615 /* We need to add the subroutine type to the die immediately so
12616 we don't infinitely recurse when dealing with parameters
12617 declared as the same subroutine type. */
12618 set_die_type (die, ftype, cu);
12619
12620 if (die->child != NULL)
12621 {
12622 struct type *void_type = objfile_type (objfile)->builtin_void;
12623 struct die_info *child_die;
12624 int nparams, iparams;
12625
12626 /* Count the number of parameters.
12627 FIXME: GDB currently ignores vararg functions, but knows about
12628 vararg member functions. */
12629 nparams = 0;
12630 child_die = die->child;
12631 while (child_die && child_die->tag)
12632 {
12633 if (child_die->tag == DW_TAG_formal_parameter)
12634 nparams++;
12635 else if (child_die->tag == DW_TAG_unspecified_parameters)
12636 TYPE_VARARGS (ftype) = 1;
12637 child_die = sibling_die (child_die);
12638 }
12639
12640 /* Allocate storage for parameters and fill them in. */
12641 TYPE_NFIELDS (ftype) = nparams;
12642 TYPE_FIELDS (ftype) = (struct field *)
12643 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12644
12645 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12646 even if we error out during the parameters reading below. */
12647 for (iparams = 0; iparams < nparams; iparams++)
12648 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12649
12650 iparams = 0;
12651 child_die = die->child;
12652 while (child_die && child_die->tag)
12653 {
12654 if (child_die->tag == DW_TAG_formal_parameter)
12655 {
12656 struct type *arg_type;
12657
12658 /* DWARF version 2 has no clean way to discern C++
12659 static and non-static member functions. G++ helps
12660 GDB by marking the first parameter for non-static
12661 member functions (which is the this pointer) as
12662 artificial. We pass this information to
12663 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12664
12665 DWARF version 3 added DW_AT_object_pointer, which GCC
12666 4.5 does not yet generate. */
12667 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12668 if (attr)
12669 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12670 else
12671 {
12672 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12673
12674 /* GCC/43521: In java, the formal parameter
12675 "this" is sometimes not marked with DW_AT_artificial. */
12676 if (cu->language == language_java)
12677 {
12678 const char *name = dwarf2_name (child_die, cu);
12679
12680 if (name && !strcmp (name, "this"))
12681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12682 }
12683 }
12684 arg_type = die_type (child_die, cu);
12685
12686 /* RealView does not mark THIS as const, which the testsuite
12687 expects. GCC marks THIS as const in method definitions,
12688 but not in the class specifications (GCC PR 43053). */
12689 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12690 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12691 {
12692 int is_this = 0;
12693 struct dwarf2_cu *arg_cu = cu;
12694 const char *name = dwarf2_name (child_die, cu);
12695
12696 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12697 if (attr)
12698 {
12699 /* If the compiler emits this, use it. */
12700 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12701 is_this = 1;
12702 }
12703 else if (name && strcmp (name, "this") == 0)
12704 /* Function definitions will have the argument names. */
12705 is_this = 1;
12706 else if (name == NULL && iparams == 0)
12707 /* Declarations may not have the names, so like
12708 elsewhere in GDB, assume an artificial first
12709 argument is "this". */
12710 is_this = 1;
12711
12712 if (is_this)
12713 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12714 arg_type, 0);
12715 }
12716
12717 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12718 iparams++;
12719 }
12720 child_die = sibling_die (child_die);
12721 }
12722 }
12723
12724 return ftype;
12725 }
12726
12727 static struct type *
12728 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12729 {
12730 struct objfile *objfile = cu->objfile;
12731 const char *name = NULL;
12732 struct type *this_type, *target_type;
12733
12734 name = dwarf2_full_name (NULL, die, cu);
12735 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12736 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12737 TYPE_NAME (this_type) = name;
12738 set_die_type (die, this_type, cu);
12739 target_type = die_type (die, cu);
12740 if (target_type != this_type)
12741 TYPE_TARGET_TYPE (this_type) = target_type;
12742 else
12743 {
12744 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12745 spec and cause infinite loops in GDB. */
12746 complaint (&symfile_complaints,
12747 _("Self-referential DW_TAG_typedef "
12748 "- DIE at 0x%x [in module %s]"),
12749 die->offset.sect_off, objfile->name);
12750 TYPE_TARGET_TYPE (this_type) = NULL;
12751 }
12752 return this_type;
12753 }
12754
12755 /* Find a representation of a given base type and install
12756 it in the TYPE field of the die. */
12757
12758 static struct type *
12759 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12760 {
12761 struct objfile *objfile = cu->objfile;
12762 struct type *type;
12763 struct attribute *attr;
12764 int encoding = 0, size = 0;
12765 const char *name;
12766 enum type_code code = TYPE_CODE_INT;
12767 int type_flags = 0;
12768 struct type *target_type = NULL;
12769
12770 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12771 if (attr)
12772 {
12773 encoding = DW_UNSND (attr);
12774 }
12775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12776 if (attr)
12777 {
12778 size = DW_UNSND (attr);
12779 }
12780 name = dwarf2_name (die, cu);
12781 if (!name)
12782 {
12783 complaint (&symfile_complaints,
12784 _("DW_AT_name missing from DW_TAG_base_type"));
12785 }
12786
12787 switch (encoding)
12788 {
12789 case DW_ATE_address:
12790 /* Turn DW_ATE_address into a void * pointer. */
12791 code = TYPE_CODE_PTR;
12792 type_flags |= TYPE_FLAG_UNSIGNED;
12793 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12794 break;
12795 case DW_ATE_boolean:
12796 code = TYPE_CODE_BOOL;
12797 type_flags |= TYPE_FLAG_UNSIGNED;
12798 break;
12799 case DW_ATE_complex_float:
12800 code = TYPE_CODE_COMPLEX;
12801 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12802 break;
12803 case DW_ATE_decimal_float:
12804 code = TYPE_CODE_DECFLOAT;
12805 break;
12806 case DW_ATE_float:
12807 code = TYPE_CODE_FLT;
12808 break;
12809 case DW_ATE_signed:
12810 break;
12811 case DW_ATE_unsigned:
12812 type_flags |= TYPE_FLAG_UNSIGNED;
12813 if (cu->language == language_fortran
12814 && name
12815 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12816 code = TYPE_CODE_CHAR;
12817 break;
12818 case DW_ATE_signed_char:
12819 if (cu->language == language_ada || cu->language == language_m2
12820 || cu->language == language_pascal
12821 || cu->language == language_fortran)
12822 code = TYPE_CODE_CHAR;
12823 break;
12824 case DW_ATE_unsigned_char:
12825 if (cu->language == language_ada || cu->language == language_m2
12826 || cu->language == language_pascal
12827 || cu->language == language_fortran)
12828 code = TYPE_CODE_CHAR;
12829 type_flags |= TYPE_FLAG_UNSIGNED;
12830 break;
12831 case DW_ATE_UTF:
12832 /* We just treat this as an integer and then recognize the
12833 type by name elsewhere. */
12834 break;
12835
12836 default:
12837 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12838 dwarf_type_encoding_name (encoding));
12839 break;
12840 }
12841
12842 type = init_type (code, size, type_flags, NULL, objfile);
12843 TYPE_NAME (type) = name;
12844 TYPE_TARGET_TYPE (type) = target_type;
12845
12846 if (name && strcmp (name, "char") == 0)
12847 TYPE_NOSIGN (type) = 1;
12848
12849 return set_die_type (die, type, cu);
12850 }
12851
12852 /* Read the given DW_AT_subrange DIE. */
12853
12854 static struct type *
12855 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12856 {
12857 struct type *base_type, *orig_base_type;
12858 struct type *range_type;
12859 struct attribute *attr;
12860 LONGEST low, high;
12861 int low_default_is_valid;
12862 const char *name;
12863 LONGEST negative_mask;
12864
12865 orig_base_type = die_type (die, cu);
12866 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12867 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12868 creating the range type, but we use the result of check_typedef
12869 when examining properties of the type. */
12870 base_type = check_typedef (orig_base_type);
12871
12872 /* The die_type call above may have already set the type for this DIE. */
12873 range_type = get_die_type (die, cu);
12874 if (range_type)
12875 return range_type;
12876
12877 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12878 omitting DW_AT_lower_bound. */
12879 switch (cu->language)
12880 {
12881 case language_c:
12882 case language_cplus:
12883 low = 0;
12884 low_default_is_valid = 1;
12885 break;
12886 case language_fortran:
12887 low = 1;
12888 low_default_is_valid = 1;
12889 break;
12890 case language_d:
12891 case language_java:
12892 case language_objc:
12893 low = 0;
12894 low_default_is_valid = (cu->header.version >= 4);
12895 break;
12896 case language_ada:
12897 case language_m2:
12898 case language_pascal:
12899 low = 1;
12900 low_default_is_valid = (cu->header.version >= 4);
12901 break;
12902 default:
12903 low = 0;
12904 low_default_is_valid = 0;
12905 break;
12906 }
12907
12908 /* FIXME: For variable sized arrays either of these could be
12909 a variable rather than a constant value. We'll allow it,
12910 but we don't know how to handle it. */
12911 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12912 if (attr)
12913 low = dwarf2_get_attr_constant_value (attr, low);
12914 else if (!low_default_is_valid)
12915 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12916 "- DIE at 0x%x [in module %s]"),
12917 die->offset.sect_off, cu->objfile->name);
12918
12919 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12920 if (attr)
12921 {
12922 if (attr_form_is_block (attr) || is_ref_attr (attr))
12923 {
12924 /* GCC encodes arrays with unspecified or dynamic length
12925 with a DW_FORM_block1 attribute or a reference attribute.
12926 FIXME: GDB does not yet know how to handle dynamic
12927 arrays properly, treat them as arrays with unspecified
12928 length for now.
12929
12930 FIXME: jimb/2003-09-22: GDB does not really know
12931 how to handle arrays of unspecified length
12932 either; we just represent them as zero-length
12933 arrays. Choose an appropriate upper bound given
12934 the lower bound we've computed above. */
12935 high = low - 1;
12936 }
12937 else
12938 high = dwarf2_get_attr_constant_value (attr, 1);
12939 }
12940 else
12941 {
12942 attr = dwarf2_attr (die, DW_AT_count, cu);
12943 if (attr)
12944 {
12945 int count = dwarf2_get_attr_constant_value (attr, 1);
12946 high = low + count - 1;
12947 }
12948 else
12949 {
12950 /* Unspecified array length. */
12951 high = low - 1;
12952 }
12953 }
12954
12955 /* Dwarf-2 specifications explicitly allows to create subrange types
12956 without specifying a base type.
12957 In that case, the base type must be set to the type of
12958 the lower bound, upper bound or count, in that order, if any of these
12959 three attributes references an object that has a type.
12960 If no base type is found, the Dwarf-2 specifications say that
12961 a signed integer type of size equal to the size of an address should
12962 be used.
12963 For the following C code: `extern char gdb_int [];'
12964 GCC produces an empty range DIE.
12965 FIXME: muller/2010-05-28: Possible references to object for low bound,
12966 high bound or count are not yet handled by this code. */
12967 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12968 {
12969 struct objfile *objfile = cu->objfile;
12970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12971 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12972 struct type *int_type = objfile_type (objfile)->builtin_int;
12973
12974 /* Test "int", "long int", and "long long int" objfile types,
12975 and select the first one having a size above or equal to the
12976 architecture address size. */
12977 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12978 base_type = int_type;
12979 else
12980 {
12981 int_type = objfile_type (objfile)->builtin_long;
12982 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12983 base_type = int_type;
12984 else
12985 {
12986 int_type = objfile_type (objfile)->builtin_long_long;
12987 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12988 base_type = int_type;
12989 }
12990 }
12991 }
12992
12993 negative_mask =
12994 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12995 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12996 low |= negative_mask;
12997 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12998 high |= negative_mask;
12999
13000 range_type = create_range_type (NULL, orig_base_type, low, high);
13001
13002 /* Mark arrays with dynamic length at least as an array of unspecified
13003 length. GDB could check the boundary but before it gets implemented at
13004 least allow accessing the array elements. */
13005 if (attr && attr_form_is_block (attr))
13006 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13007
13008 /* Ada expects an empty array on no boundary attributes. */
13009 if (attr == NULL && cu->language != language_ada)
13010 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13011
13012 name = dwarf2_name (die, cu);
13013 if (name)
13014 TYPE_NAME (range_type) = name;
13015
13016 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13017 if (attr)
13018 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13019
13020 set_die_type (die, range_type, cu);
13021
13022 /* set_die_type should be already done. */
13023 set_descriptive_type (range_type, die, cu);
13024
13025 return range_type;
13026 }
13027
13028 static struct type *
13029 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13030 {
13031 struct type *type;
13032
13033 /* For now, we only support the C meaning of an unspecified type: void. */
13034
13035 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13036 TYPE_NAME (type) = dwarf2_name (die, cu);
13037
13038 return set_die_type (die, type, cu);
13039 }
13040
13041 /* Read a single die and all its descendents. Set the die's sibling
13042 field to NULL; set other fields in the die correctly, and set all
13043 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13044 location of the info_ptr after reading all of those dies. PARENT
13045 is the parent of the die in question. */
13046
13047 static struct die_info *
13048 read_die_and_children (const struct die_reader_specs *reader,
13049 gdb_byte *info_ptr,
13050 gdb_byte **new_info_ptr,
13051 struct die_info *parent)
13052 {
13053 struct die_info *die;
13054 gdb_byte *cur_ptr;
13055 int has_children;
13056
13057 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
13058 if (die == NULL)
13059 {
13060 *new_info_ptr = cur_ptr;
13061 return NULL;
13062 }
13063 store_in_ref_table (die, reader->cu);
13064
13065 if (has_children)
13066 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
13067 else
13068 {
13069 die->child = NULL;
13070 *new_info_ptr = cur_ptr;
13071 }
13072
13073 die->sibling = NULL;
13074 die->parent = parent;
13075 return die;
13076 }
13077
13078 /* Read a die, all of its descendents, and all of its siblings; set
13079 all of the fields of all of the dies correctly. Arguments are as
13080 in read_die_and_children. */
13081
13082 static struct die_info *
13083 read_die_and_siblings (const struct die_reader_specs *reader,
13084 gdb_byte *info_ptr,
13085 gdb_byte **new_info_ptr,
13086 struct die_info *parent)
13087 {
13088 struct die_info *first_die, *last_sibling;
13089 gdb_byte *cur_ptr;
13090
13091 cur_ptr = info_ptr;
13092 first_die = last_sibling = NULL;
13093
13094 while (1)
13095 {
13096 struct die_info *die
13097 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13098
13099 if (die == NULL)
13100 {
13101 *new_info_ptr = cur_ptr;
13102 return first_die;
13103 }
13104
13105 if (!first_die)
13106 first_die = die;
13107 else
13108 last_sibling->sibling = die;
13109
13110 last_sibling = die;
13111 }
13112 }
13113
13114 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13115 attributes.
13116 The caller is responsible for filling in the extra attributes
13117 and updating (*DIEP)->num_attrs.
13118 Set DIEP to point to a newly allocated die with its information,
13119 except for its child, sibling, and parent fields.
13120 Set HAS_CHILDREN to tell whether the die has children or not. */
13121
13122 static gdb_byte *
13123 read_full_die_1 (const struct die_reader_specs *reader,
13124 struct die_info **diep, gdb_byte *info_ptr,
13125 int *has_children, int num_extra_attrs)
13126 {
13127 unsigned int abbrev_number, bytes_read, i;
13128 sect_offset offset;
13129 struct abbrev_info *abbrev;
13130 struct die_info *die;
13131 struct dwarf2_cu *cu = reader->cu;
13132 bfd *abfd = reader->abfd;
13133
13134 offset.sect_off = info_ptr - reader->buffer;
13135 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13136 info_ptr += bytes_read;
13137 if (!abbrev_number)
13138 {
13139 *diep = NULL;
13140 *has_children = 0;
13141 return info_ptr;
13142 }
13143
13144 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13145 if (!abbrev)
13146 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13147 abbrev_number,
13148 bfd_get_filename (abfd));
13149
13150 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13151 die->offset = offset;
13152 die->tag = abbrev->tag;
13153 die->abbrev = abbrev_number;
13154
13155 /* Make the result usable.
13156 The caller needs to update num_attrs after adding the extra
13157 attributes. */
13158 die->num_attrs = abbrev->num_attrs;
13159
13160 for (i = 0; i < abbrev->num_attrs; ++i)
13161 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13162 info_ptr);
13163
13164 *diep = die;
13165 *has_children = abbrev->has_children;
13166 return info_ptr;
13167 }
13168
13169 /* Read a die and all its attributes.
13170 Set DIEP to point to a newly allocated die with its information,
13171 except for its child, sibling, and parent fields.
13172 Set HAS_CHILDREN to tell whether the die has children or not. */
13173
13174 static gdb_byte *
13175 read_full_die (const struct die_reader_specs *reader,
13176 struct die_info **diep, gdb_byte *info_ptr,
13177 int *has_children)
13178 {
13179 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13180 }
13181 \f
13182 /* Abbreviation tables.
13183
13184 In DWARF version 2, the description of the debugging information is
13185 stored in a separate .debug_abbrev section. Before we read any
13186 dies from a section we read in all abbreviations and install them
13187 in a hash table. */
13188
13189 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13190
13191 static struct abbrev_info *
13192 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13193 {
13194 struct abbrev_info *abbrev;
13195
13196 abbrev = (struct abbrev_info *)
13197 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13198 memset (abbrev, 0, sizeof (struct abbrev_info));
13199 return abbrev;
13200 }
13201
13202 /* Add an abbreviation to the table. */
13203
13204 static void
13205 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13206 unsigned int abbrev_number,
13207 struct abbrev_info *abbrev)
13208 {
13209 unsigned int hash_number;
13210
13211 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13212 abbrev->next = abbrev_table->abbrevs[hash_number];
13213 abbrev_table->abbrevs[hash_number] = abbrev;
13214 }
13215
13216 /* Look up an abbrev in the table.
13217 Returns NULL if the abbrev is not found. */
13218
13219 static struct abbrev_info *
13220 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13221 unsigned int abbrev_number)
13222 {
13223 unsigned int hash_number;
13224 struct abbrev_info *abbrev;
13225
13226 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13227 abbrev = abbrev_table->abbrevs[hash_number];
13228
13229 while (abbrev)
13230 {
13231 if (abbrev->number == abbrev_number)
13232 return abbrev;
13233 abbrev = abbrev->next;
13234 }
13235 return NULL;
13236 }
13237
13238 /* Read in an abbrev table. */
13239
13240 static struct abbrev_table *
13241 abbrev_table_read_table (struct dwarf2_section_info *section,
13242 sect_offset offset)
13243 {
13244 struct objfile *objfile = dwarf2_per_objfile->objfile;
13245 bfd *abfd = section->asection->owner;
13246 struct abbrev_table *abbrev_table;
13247 gdb_byte *abbrev_ptr;
13248 struct abbrev_info *cur_abbrev;
13249 unsigned int abbrev_number, bytes_read, abbrev_name;
13250 unsigned int abbrev_form;
13251 struct attr_abbrev *cur_attrs;
13252 unsigned int allocated_attrs;
13253
13254 abbrev_table = XMALLOC (struct abbrev_table);
13255 abbrev_table->offset = offset;
13256 obstack_init (&abbrev_table->abbrev_obstack);
13257 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13258 (ABBREV_HASH_SIZE
13259 * sizeof (struct abbrev_info *)));
13260 memset (abbrev_table->abbrevs, 0,
13261 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13262
13263 dwarf2_read_section (objfile, section);
13264 abbrev_ptr = section->buffer + offset.sect_off;
13265 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13266 abbrev_ptr += bytes_read;
13267
13268 allocated_attrs = ATTR_ALLOC_CHUNK;
13269 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13270
13271 /* Loop until we reach an abbrev number of 0. */
13272 while (abbrev_number)
13273 {
13274 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13275
13276 /* read in abbrev header */
13277 cur_abbrev->number = abbrev_number;
13278 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13279 abbrev_ptr += bytes_read;
13280 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13281 abbrev_ptr += 1;
13282
13283 /* now read in declarations */
13284 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13285 abbrev_ptr += bytes_read;
13286 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13287 abbrev_ptr += bytes_read;
13288 while (abbrev_name)
13289 {
13290 if (cur_abbrev->num_attrs == allocated_attrs)
13291 {
13292 allocated_attrs += ATTR_ALLOC_CHUNK;
13293 cur_attrs
13294 = xrealloc (cur_attrs, (allocated_attrs
13295 * sizeof (struct attr_abbrev)));
13296 }
13297
13298 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13299 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13300 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13301 abbrev_ptr += bytes_read;
13302 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13303 abbrev_ptr += bytes_read;
13304 }
13305
13306 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13307 (cur_abbrev->num_attrs
13308 * sizeof (struct attr_abbrev)));
13309 memcpy (cur_abbrev->attrs, cur_attrs,
13310 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13311
13312 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13313
13314 /* Get next abbreviation.
13315 Under Irix6 the abbreviations for a compilation unit are not
13316 always properly terminated with an abbrev number of 0.
13317 Exit loop if we encounter an abbreviation which we have
13318 already read (which means we are about to read the abbreviations
13319 for the next compile unit) or if the end of the abbreviation
13320 table is reached. */
13321 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13322 break;
13323 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13324 abbrev_ptr += bytes_read;
13325 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13326 break;
13327 }
13328
13329 xfree (cur_attrs);
13330 return abbrev_table;
13331 }
13332
13333 /* Free the resources held by ABBREV_TABLE. */
13334
13335 static void
13336 abbrev_table_free (struct abbrev_table *abbrev_table)
13337 {
13338 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13339 xfree (abbrev_table);
13340 }
13341
13342 /* Same as abbrev_table_free but as a cleanup.
13343 We pass in a pointer to the pointer to the table so that we can
13344 set the pointer to NULL when we're done. It also simplifies
13345 build_type_unit_groups. */
13346
13347 static void
13348 abbrev_table_free_cleanup (void *table_ptr)
13349 {
13350 struct abbrev_table **abbrev_table_ptr = table_ptr;
13351
13352 if (*abbrev_table_ptr != NULL)
13353 abbrev_table_free (*abbrev_table_ptr);
13354 *abbrev_table_ptr = NULL;
13355 }
13356
13357 /* Read the abbrev table for CU from ABBREV_SECTION. */
13358
13359 static void
13360 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13361 struct dwarf2_section_info *abbrev_section)
13362 {
13363 cu->abbrev_table =
13364 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13365 }
13366
13367 /* Release the memory used by the abbrev table for a compilation unit. */
13368
13369 static void
13370 dwarf2_free_abbrev_table (void *ptr_to_cu)
13371 {
13372 struct dwarf2_cu *cu = ptr_to_cu;
13373
13374 abbrev_table_free (cu->abbrev_table);
13375 /* Set this to NULL so that we SEGV if we try to read it later,
13376 and also because free_comp_unit verifies this is NULL. */
13377 cu->abbrev_table = NULL;
13378 }
13379 \f
13380 /* Returns nonzero if TAG represents a type that we might generate a partial
13381 symbol for. */
13382
13383 static int
13384 is_type_tag_for_partial (int tag)
13385 {
13386 switch (tag)
13387 {
13388 #if 0
13389 /* Some types that would be reasonable to generate partial symbols for,
13390 that we don't at present. */
13391 case DW_TAG_array_type:
13392 case DW_TAG_file_type:
13393 case DW_TAG_ptr_to_member_type:
13394 case DW_TAG_set_type:
13395 case DW_TAG_string_type:
13396 case DW_TAG_subroutine_type:
13397 #endif
13398 case DW_TAG_base_type:
13399 case DW_TAG_class_type:
13400 case DW_TAG_interface_type:
13401 case DW_TAG_enumeration_type:
13402 case DW_TAG_structure_type:
13403 case DW_TAG_subrange_type:
13404 case DW_TAG_typedef:
13405 case DW_TAG_union_type:
13406 return 1;
13407 default:
13408 return 0;
13409 }
13410 }
13411
13412 /* Load all DIEs that are interesting for partial symbols into memory. */
13413
13414 static struct partial_die_info *
13415 load_partial_dies (const struct die_reader_specs *reader,
13416 gdb_byte *info_ptr, int building_psymtab)
13417 {
13418 struct dwarf2_cu *cu = reader->cu;
13419 struct objfile *objfile = cu->objfile;
13420 struct partial_die_info *part_die;
13421 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13422 struct abbrev_info *abbrev;
13423 unsigned int bytes_read;
13424 unsigned int load_all = 0;
13425 int nesting_level = 1;
13426
13427 parent_die = NULL;
13428 last_die = NULL;
13429
13430 gdb_assert (cu->per_cu != NULL);
13431 if (cu->per_cu->load_all_dies)
13432 load_all = 1;
13433
13434 cu->partial_dies
13435 = htab_create_alloc_ex (cu->header.length / 12,
13436 partial_die_hash,
13437 partial_die_eq,
13438 NULL,
13439 &cu->comp_unit_obstack,
13440 hashtab_obstack_allocate,
13441 dummy_obstack_deallocate);
13442
13443 part_die = obstack_alloc (&cu->comp_unit_obstack,
13444 sizeof (struct partial_die_info));
13445
13446 while (1)
13447 {
13448 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13449
13450 /* A NULL abbrev means the end of a series of children. */
13451 if (abbrev == NULL)
13452 {
13453 if (--nesting_level == 0)
13454 {
13455 /* PART_DIE was probably the last thing allocated on the
13456 comp_unit_obstack, so we could call obstack_free
13457 here. We don't do that because the waste is small,
13458 and will be cleaned up when we're done with this
13459 compilation unit. This way, we're also more robust
13460 against other users of the comp_unit_obstack. */
13461 return first_die;
13462 }
13463 info_ptr += bytes_read;
13464 last_die = parent_die;
13465 parent_die = parent_die->die_parent;
13466 continue;
13467 }
13468
13469 /* Check for template arguments. We never save these; if
13470 they're seen, we just mark the parent, and go on our way. */
13471 if (parent_die != NULL
13472 && cu->language == language_cplus
13473 && (abbrev->tag == DW_TAG_template_type_param
13474 || abbrev->tag == DW_TAG_template_value_param))
13475 {
13476 parent_die->has_template_arguments = 1;
13477
13478 if (!load_all)
13479 {
13480 /* We don't need a partial DIE for the template argument. */
13481 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13482 continue;
13483 }
13484 }
13485
13486 /* We only recurse into c++ subprograms looking for template arguments.
13487 Skip their other children. */
13488 if (!load_all
13489 && cu->language == language_cplus
13490 && parent_die != NULL
13491 && parent_die->tag == DW_TAG_subprogram)
13492 {
13493 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13494 continue;
13495 }
13496
13497 /* Check whether this DIE is interesting enough to save. Normally
13498 we would not be interested in members here, but there may be
13499 later variables referencing them via DW_AT_specification (for
13500 static members). */
13501 if (!load_all
13502 && !is_type_tag_for_partial (abbrev->tag)
13503 && abbrev->tag != DW_TAG_constant
13504 && abbrev->tag != DW_TAG_enumerator
13505 && abbrev->tag != DW_TAG_subprogram
13506 && abbrev->tag != DW_TAG_lexical_block
13507 && abbrev->tag != DW_TAG_variable
13508 && abbrev->tag != DW_TAG_namespace
13509 && abbrev->tag != DW_TAG_module
13510 && abbrev->tag != DW_TAG_member
13511 && abbrev->tag != DW_TAG_imported_unit)
13512 {
13513 /* Otherwise we skip to the next sibling, if any. */
13514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13515 continue;
13516 }
13517
13518 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13519 info_ptr);
13520
13521 /* This two-pass algorithm for processing partial symbols has a
13522 high cost in cache pressure. Thus, handle some simple cases
13523 here which cover the majority of C partial symbols. DIEs
13524 which neither have specification tags in them, nor could have
13525 specification tags elsewhere pointing at them, can simply be
13526 processed and discarded.
13527
13528 This segment is also optional; scan_partial_symbols and
13529 add_partial_symbol will handle these DIEs if we chain
13530 them in normally. When compilers which do not emit large
13531 quantities of duplicate debug information are more common,
13532 this code can probably be removed. */
13533
13534 /* Any complete simple types at the top level (pretty much all
13535 of them, for a language without namespaces), can be processed
13536 directly. */
13537 if (parent_die == NULL
13538 && part_die->has_specification == 0
13539 && part_die->is_declaration == 0
13540 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13541 || part_die->tag == DW_TAG_base_type
13542 || part_die->tag == DW_TAG_subrange_type))
13543 {
13544 if (building_psymtab && part_die->name != NULL)
13545 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13546 VAR_DOMAIN, LOC_TYPEDEF,
13547 &objfile->static_psymbols,
13548 0, (CORE_ADDR) 0, cu->language, objfile);
13549 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13550 continue;
13551 }
13552
13553 /* The exception for DW_TAG_typedef with has_children above is
13554 a workaround of GCC PR debug/47510. In the case of this complaint
13555 type_name_no_tag_or_error will error on such types later.
13556
13557 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13558 it could not find the child DIEs referenced later, this is checked
13559 above. In correct DWARF DW_TAG_typedef should have no children. */
13560
13561 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13562 complaint (&symfile_complaints,
13563 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13564 "- DIE at 0x%x [in module %s]"),
13565 part_die->offset.sect_off, objfile->name);
13566
13567 /* If we're at the second level, and we're an enumerator, and
13568 our parent has no specification (meaning possibly lives in a
13569 namespace elsewhere), then we can add the partial symbol now
13570 instead of queueing it. */
13571 if (part_die->tag == DW_TAG_enumerator
13572 && parent_die != NULL
13573 && parent_die->die_parent == NULL
13574 && parent_die->tag == DW_TAG_enumeration_type
13575 && parent_die->has_specification == 0)
13576 {
13577 if (part_die->name == NULL)
13578 complaint (&symfile_complaints,
13579 _("malformed enumerator DIE ignored"));
13580 else if (building_psymtab)
13581 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13582 VAR_DOMAIN, LOC_CONST,
13583 (cu->language == language_cplus
13584 || cu->language == language_java)
13585 ? &objfile->global_psymbols
13586 : &objfile->static_psymbols,
13587 0, (CORE_ADDR) 0, cu->language, objfile);
13588
13589 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13590 continue;
13591 }
13592
13593 /* We'll save this DIE so link it in. */
13594 part_die->die_parent = parent_die;
13595 part_die->die_sibling = NULL;
13596 part_die->die_child = NULL;
13597
13598 if (last_die && last_die == parent_die)
13599 last_die->die_child = part_die;
13600 else if (last_die)
13601 last_die->die_sibling = part_die;
13602
13603 last_die = part_die;
13604
13605 if (first_die == NULL)
13606 first_die = part_die;
13607
13608 /* Maybe add the DIE to the hash table. Not all DIEs that we
13609 find interesting need to be in the hash table, because we
13610 also have the parent/sibling/child chains; only those that we
13611 might refer to by offset later during partial symbol reading.
13612
13613 For now this means things that might have be the target of a
13614 DW_AT_specification, DW_AT_abstract_origin, or
13615 DW_AT_extension. DW_AT_extension will refer only to
13616 namespaces; DW_AT_abstract_origin refers to functions (and
13617 many things under the function DIE, but we do not recurse
13618 into function DIEs during partial symbol reading) and
13619 possibly variables as well; DW_AT_specification refers to
13620 declarations. Declarations ought to have the DW_AT_declaration
13621 flag. It happens that GCC forgets to put it in sometimes, but
13622 only for functions, not for types.
13623
13624 Adding more things than necessary to the hash table is harmless
13625 except for the performance cost. Adding too few will result in
13626 wasted time in find_partial_die, when we reread the compilation
13627 unit with load_all_dies set. */
13628
13629 if (load_all
13630 || abbrev->tag == DW_TAG_constant
13631 || abbrev->tag == DW_TAG_subprogram
13632 || abbrev->tag == DW_TAG_variable
13633 || abbrev->tag == DW_TAG_namespace
13634 || part_die->is_declaration)
13635 {
13636 void **slot;
13637
13638 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13639 part_die->offset.sect_off, INSERT);
13640 *slot = part_die;
13641 }
13642
13643 part_die = obstack_alloc (&cu->comp_unit_obstack,
13644 sizeof (struct partial_die_info));
13645
13646 /* For some DIEs we want to follow their children (if any). For C
13647 we have no reason to follow the children of structures; for other
13648 languages we have to, so that we can get at method physnames
13649 to infer fully qualified class names, for DW_AT_specification,
13650 and for C++ template arguments. For C++, we also look one level
13651 inside functions to find template arguments (if the name of the
13652 function does not already contain the template arguments).
13653
13654 For Ada, we need to scan the children of subprograms and lexical
13655 blocks as well because Ada allows the definition of nested
13656 entities that could be interesting for the debugger, such as
13657 nested subprograms for instance. */
13658 if (last_die->has_children
13659 && (load_all
13660 || last_die->tag == DW_TAG_namespace
13661 || last_die->tag == DW_TAG_module
13662 || last_die->tag == DW_TAG_enumeration_type
13663 || (cu->language == language_cplus
13664 && last_die->tag == DW_TAG_subprogram
13665 && (last_die->name == NULL
13666 || strchr (last_die->name, '<') == NULL))
13667 || (cu->language != language_c
13668 && (last_die->tag == DW_TAG_class_type
13669 || last_die->tag == DW_TAG_interface_type
13670 || last_die->tag == DW_TAG_structure_type
13671 || last_die->tag == DW_TAG_union_type))
13672 || (cu->language == language_ada
13673 && (last_die->tag == DW_TAG_subprogram
13674 || last_die->tag == DW_TAG_lexical_block))))
13675 {
13676 nesting_level++;
13677 parent_die = last_die;
13678 continue;
13679 }
13680
13681 /* Otherwise we skip to the next sibling, if any. */
13682 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13683
13684 /* Back to the top, do it again. */
13685 }
13686 }
13687
13688 /* Read a minimal amount of information into the minimal die structure. */
13689
13690 static gdb_byte *
13691 read_partial_die (const struct die_reader_specs *reader,
13692 struct partial_die_info *part_die,
13693 struct abbrev_info *abbrev, unsigned int abbrev_len,
13694 gdb_byte *info_ptr)
13695 {
13696 struct dwarf2_cu *cu = reader->cu;
13697 struct objfile *objfile = cu->objfile;
13698 gdb_byte *buffer = reader->buffer;
13699 unsigned int i;
13700 struct attribute attr;
13701 int has_low_pc_attr = 0;
13702 int has_high_pc_attr = 0;
13703 int high_pc_relative = 0;
13704
13705 memset (part_die, 0, sizeof (struct partial_die_info));
13706
13707 part_die->offset.sect_off = info_ptr - buffer;
13708
13709 info_ptr += abbrev_len;
13710
13711 if (abbrev == NULL)
13712 return info_ptr;
13713
13714 part_die->tag = abbrev->tag;
13715 part_die->has_children = abbrev->has_children;
13716
13717 for (i = 0; i < abbrev->num_attrs; ++i)
13718 {
13719 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13720
13721 /* Store the data if it is of an attribute we want to keep in a
13722 partial symbol table. */
13723 switch (attr.name)
13724 {
13725 case DW_AT_name:
13726 switch (part_die->tag)
13727 {
13728 case DW_TAG_compile_unit:
13729 case DW_TAG_partial_unit:
13730 case DW_TAG_type_unit:
13731 /* Compilation units have a DW_AT_name that is a filename, not
13732 a source language identifier. */
13733 case DW_TAG_enumeration_type:
13734 case DW_TAG_enumerator:
13735 /* These tags always have simple identifiers already; no need
13736 to canonicalize them. */
13737 part_die->name = DW_STRING (&attr);
13738 break;
13739 default:
13740 part_die->name
13741 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13742 &objfile->objfile_obstack);
13743 break;
13744 }
13745 break;
13746 case DW_AT_linkage_name:
13747 case DW_AT_MIPS_linkage_name:
13748 /* Note that both forms of linkage name might appear. We
13749 assume they will be the same, and we only store the last
13750 one we see. */
13751 if (cu->language == language_ada)
13752 part_die->name = DW_STRING (&attr);
13753 part_die->linkage_name = DW_STRING (&attr);
13754 break;
13755 case DW_AT_low_pc:
13756 has_low_pc_attr = 1;
13757 part_die->lowpc = DW_ADDR (&attr);
13758 break;
13759 case DW_AT_high_pc:
13760 has_high_pc_attr = 1;
13761 if (attr.form == DW_FORM_addr
13762 || attr.form == DW_FORM_GNU_addr_index)
13763 part_die->highpc = DW_ADDR (&attr);
13764 else
13765 {
13766 high_pc_relative = 1;
13767 part_die->highpc = DW_UNSND (&attr);
13768 }
13769 break;
13770 case DW_AT_location:
13771 /* Support the .debug_loc offsets. */
13772 if (attr_form_is_block (&attr))
13773 {
13774 part_die->d.locdesc = DW_BLOCK (&attr);
13775 }
13776 else if (attr_form_is_section_offset (&attr))
13777 {
13778 dwarf2_complex_location_expr_complaint ();
13779 }
13780 else
13781 {
13782 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13783 "partial symbol information");
13784 }
13785 break;
13786 case DW_AT_external:
13787 part_die->is_external = DW_UNSND (&attr);
13788 break;
13789 case DW_AT_declaration:
13790 part_die->is_declaration = DW_UNSND (&attr);
13791 break;
13792 case DW_AT_type:
13793 part_die->has_type = 1;
13794 break;
13795 case DW_AT_abstract_origin:
13796 case DW_AT_specification:
13797 case DW_AT_extension:
13798 part_die->has_specification = 1;
13799 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13800 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13801 || cu->per_cu->is_dwz);
13802 break;
13803 case DW_AT_sibling:
13804 /* Ignore absolute siblings, they might point outside of
13805 the current compile unit. */
13806 if (attr.form == DW_FORM_ref_addr)
13807 complaint (&symfile_complaints,
13808 _("ignoring absolute DW_AT_sibling"));
13809 else
13810 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13811 break;
13812 case DW_AT_byte_size:
13813 part_die->has_byte_size = 1;
13814 break;
13815 case DW_AT_calling_convention:
13816 /* DWARF doesn't provide a way to identify a program's source-level
13817 entry point. DW_AT_calling_convention attributes are only meant
13818 to describe functions' calling conventions.
13819
13820 However, because it's a necessary piece of information in
13821 Fortran, and because DW_CC_program is the only piece of debugging
13822 information whose definition refers to a 'main program' at all,
13823 several compilers have begun marking Fortran main programs with
13824 DW_CC_program --- even when those functions use the standard
13825 calling conventions.
13826
13827 So until DWARF specifies a way to provide this information and
13828 compilers pick up the new representation, we'll support this
13829 practice. */
13830 if (DW_UNSND (&attr) == DW_CC_program
13831 && cu->language == language_fortran)
13832 {
13833 set_main_name (part_die->name);
13834
13835 /* As this DIE has a static linkage the name would be difficult
13836 to look up later. */
13837 language_of_main = language_fortran;
13838 }
13839 break;
13840 case DW_AT_inline:
13841 if (DW_UNSND (&attr) == DW_INL_inlined
13842 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13843 part_die->may_be_inlined = 1;
13844 break;
13845
13846 case DW_AT_import:
13847 if (part_die->tag == DW_TAG_imported_unit)
13848 {
13849 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13850 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13851 || cu->per_cu->is_dwz);
13852 }
13853 break;
13854
13855 default:
13856 break;
13857 }
13858 }
13859
13860 if (high_pc_relative)
13861 part_die->highpc += part_die->lowpc;
13862
13863 if (has_low_pc_attr && has_high_pc_attr)
13864 {
13865 /* When using the GNU linker, .gnu.linkonce. sections are used to
13866 eliminate duplicate copies of functions and vtables and such.
13867 The linker will arbitrarily choose one and discard the others.
13868 The AT_*_pc values for such functions refer to local labels in
13869 these sections. If the section from that file was discarded, the
13870 labels are not in the output, so the relocs get a value of 0.
13871 If this is a discarded function, mark the pc bounds as invalid,
13872 so that GDB will ignore it. */
13873 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13874 {
13875 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13876
13877 complaint (&symfile_complaints,
13878 _("DW_AT_low_pc %s is zero "
13879 "for DIE at 0x%x [in module %s]"),
13880 paddress (gdbarch, part_die->lowpc),
13881 part_die->offset.sect_off, objfile->name);
13882 }
13883 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13884 else if (part_die->lowpc >= part_die->highpc)
13885 {
13886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13887
13888 complaint (&symfile_complaints,
13889 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13890 "for DIE at 0x%x [in module %s]"),
13891 paddress (gdbarch, part_die->lowpc),
13892 paddress (gdbarch, part_die->highpc),
13893 part_die->offset.sect_off, objfile->name);
13894 }
13895 else
13896 part_die->has_pc_info = 1;
13897 }
13898
13899 return info_ptr;
13900 }
13901
13902 /* Find a cached partial DIE at OFFSET in CU. */
13903
13904 static struct partial_die_info *
13905 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13906 {
13907 struct partial_die_info *lookup_die = NULL;
13908 struct partial_die_info part_die;
13909
13910 part_die.offset = offset;
13911 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13912 offset.sect_off);
13913
13914 return lookup_die;
13915 }
13916
13917 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13918 except in the case of .debug_types DIEs which do not reference
13919 outside their CU (they do however referencing other types via
13920 DW_FORM_ref_sig8). */
13921
13922 static struct partial_die_info *
13923 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13924 {
13925 struct objfile *objfile = cu->objfile;
13926 struct dwarf2_per_cu_data *per_cu = NULL;
13927 struct partial_die_info *pd = NULL;
13928
13929 if (offset_in_dwz == cu->per_cu->is_dwz
13930 && offset_in_cu_p (&cu->header, offset))
13931 {
13932 pd = find_partial_die_in_comp_unit (offset, cu);
13933 if (pd != NULL)
13934 return pd;
13935 /* We missed recording what we needed.
13936 Load all dies and try again. */
13937 per_cu = cu->per_cu;
13938 }
13939 else
13940 {
13941 /* TUs don't reference other CUs/TUs (except via type signatures). */
13942 if (cu->per_cu->is_debug_types)
13943 {
13944 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13945 " external reference to offset 0x%lx [in module %s].\n"),
13946 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13947 bfd_get_filename (objfile->obfd));
13948 }
13949 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13950 objfile);
13951
13952 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13953 load_partial_comp_unit (per_cu);
13954
13955 per_cu->cu->last_used = 0;
13956 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13957 }
13958
13959 /* If we didn't find it, and not all dies have been loaded,
13960 load them all and try again. */
13961
13962 if (pd == NULL && per_cu->load_all_dies == 0)
13963 {
13964 per_cu->load_all_dies = 1;
13965
13966 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13967 THIS_CU->cu may already be in use. So we can't just free it and
13968 replace its DIEs with the ones we read in. Instead, we leave those
13969 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13970 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13971 set. */
13972 load_partial_comp_unit (per_cu);
13973
13974 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13975 }
13976
13977 if (pd == NULL)
13978 internal_error (__FILE__, __LINE__,
13979 _("could not find partial DIE 0x%x "
13980 "in cache [from module %s]\n"),
13981 offset.sect_off, bfd_get_filename (objfile->obfd));
13982 return pd;
13983 }
13984
13985 /* See if we can figure out if the class lives in a namespace. We do
13986 this by looking for a member function; its demangled name will
13987 contain namespace info, if there is any. */
13988
13989 static void
13990 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13991 struct dwarf2_cu *cu)
13992 {
13993 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13994 what template types look like, because the demangler
13995 frequently doesn't give the same name as the debug info. We
13996 could fix this by only using the demangled name to get the
13997 prefix (but see comment in read_structure_type). */
13998
13999 struct partial_die_info *real_pdi;
14000 struct partial_die_info *child_pdi;
14001
14002 /* If this DIE (this DIE's specification, if any) has a parent, then
14003 we should not do this. We'll prepend the parent's fully qualified
14004 name when we create the partial symbol. */
14005
14006 real_pdi = struct_pdi;
14007 while (real_pdi->has_specification)
14008 real_pdi = find_partial_die (real_pdi->spec_offset,
14009 real_pdi->spec_is_dwz, cu);
14010
14011 if (real_pdi->die_parent != NULL)
14012 return;
14013
14014 for (child_pdi = struct_pdi->die_child;
14015 child_pdi != NULL;
14016 child_pdi = child_pdi->die_sibling)
14017 {
14018 if (child_pdi->tag == DW_TAG_subprogram
14019 && child_pdi->linkage_name != NULL)
14020 {
14021 char *actual_class_name
14022 = language_class_name_from_physname (cu->language_defn,
14023 child_pdi->linkage_name);
14024 if (actual_class_name != NULL)
14025 {
14026 struct_pdi->name
14027 = obstack_copy0 (&cu->objfile->objfile_obstack,
14028 actual_class_name,
14029 strlen (actual_class_name));
14030 xfree (actual_class_name);
14031 }
14032 break;
14033 }
14034 }
14035 }
14036
14037 /* Adjust PART_DIE before generating a symbol for it. This function
14038 may set the is_external flag or change the DIE's name. */
14039
14040 static void
14041 fixup_partial_die (struct partial_die_info *part_die,
14042 struct dwarf2_cu *cu)
14043 {
14044 /* Once we've fixed up a die, there's no point in doing so again.
14045 This also avoids a memory leak if we were to call
14046 guess_partial_die_structure_name multiple times. */
14047 if (part_die->fixup_called)
14048 return;
14049
14050 /* If we found a reference attribute and the DIE has no name, try
14051 to find a name in the referred to DIE. */
14052
14053 if (part_die->name == NULL && part_die->has_specification)
14054 {
14055 struct partial_die_info *spec_die;
14056
14057 spec_die = find_partial_die (part_die->spec_offset,
14058 part_die->spec_is_dwz, cu);
14059
14060 fixup_partial_die (spec_die, cu);
14061
14062 if (spec_die->name)
14063 {
14064 part_die->name = spec_die->name;
14065
14066 /* Copy DW_AT_external attribute if it is set. */
14067 if (spec_die->is_external)
14068 part_die->is_external = spec_die->is_external;
14069 }
14070 }
14071
14072 /* Set default names for some unnamed DIEs. */
14073
14074 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14075 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14076
14077 /* If there is no parent die to provide a namespace, and there are
14078 children, see if we can determine the namespace from their linkage
14079 name. */
14080 if (cu->language == language_cplus
14081 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14082 && part_die->die_parent == NULL
14083 && part_die->has_children
14084 && (part_die->tag == DW_TAG_class_type
14085 || part_die->tag == DW_TAG_structure_type
14086 || part_die->tag == DW_TAG_union_type))
14087 guess_partial_die_structure_name (part_die, cu);
14088
14089 /* GCC might emit a nameless struct or union that has a linkage
14090 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14091 if (part_die->name == NULL
14092 && (part_die->tag == DW_TAG_class_type
14093 || part_die->tag == DW_TAG_interface_type
14094 || part_die->tag == DW_TAG_structure_type
14095 || part_die->tag == DW_TAG_union_type)
14096 && part_die->linkage_name != NULL)
14097 {
14098 char *demangled;
14099
14100 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14101 if (demangled)
14102 {
14103 const char *base;
14104
14105 /* Strip any leading namespaces/classes, keep only the base name.
14106 DW_AT_name for named DIEs does not contain the prefixes. */
14107 base = strrchr (demangled, ':');
14108 if (base && base > demangled && base[-1] == ':')
14109 base++;
14110 else
14111 base = demangled;
14112
14113 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14114 base, strlen (base));
14115 xfree (demangled);
14116 }
14117 }
14118
14119 part_die->fixup_called = 1;
14120 }
14121
14122 /* Read an attribute value described by an attribute form. */
14123
14124 static gdb_byte *
14125 read_attribute_value (const struct die_reader_specs *reader,
14126 struct attribute *attr, unsigned form,
14127 gdb_byte *info_ptr)
14128 {
14129 struct dwarf2_cu *cu = reader->cu;
14130 bfd *abfd = reader->abfd;
14131 struct comp_unit_head *cu_header = &cu->header;
14132 unsigned int bytes_read;
14133 struct dwarf_block *blk;
14134
14135 attr->form = form;
14136 switch (form)
14137 {
14138 case DW_FORM_ref_addr:
14139 if (cu->header.version == 2)
14140 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14141 else
14142 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14143 &cu->header, &bytes_read);
14144 info_ptr += bytes_read;
14145 break;
14146 case DW_FORM_GNU_ref_alt:
14147 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14148 info_ptr += bytes_read;
14149 break;
14150 case DW_FORM_addr:
14151 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14152 info_ptr += bytes_read;
14153 break;
14154 case DW_FORM_block2:
14155 blk = dwarf_alloc_block (cu);
14156 blk->size = read_2_bytes (abfd, info_ptr);
14157 info_ptr += 2;
14158 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14159 info_ptr += blk->size;
14160 DW_BLOCK (attr) = blk;
14161 break;
14162 case DW_FORM_block4:
14163 blk = dwarf_alloc_block (cu);
14164 blk->size = read_4_bytes (abfd, info_ptr);
14165 info_ptr += 4;
14166 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14167 info_ptr += blk->size;
14168 DW_BLOCK (attr) = blk;
14169 break;
14170 case DW_FORM_data2:
14171 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14172 info_ptr += 2;
14173 break;
14174 case DW_FORM_data4:
14175 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14176 info_ptr += 4;
14177 break;
14178 case DW_FORM_data8:
14179 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14180 info_ptr += 8;
14181 break;
14182 case DW_FORM_sec_offset:
14183 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14184 info_ptr += bytes_read;
14185 break;
14186 case DW_FORM_string:
14187 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14188 DW_STRING_IS_CANONICAL (attr) = 0;
14189 info_ptr += bytes_read;
14190 break;
14191 case DW_FORM_strp:
14192 if (!cu->per_cu->is_dwz)
14193 {
14194 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14195 &bytes_read);
14196 DW_STRING_IS_CANONICAL (attr) = 0;
14197 info_ptr += bytes_read;
14198 break;
14199 }
14200 /* FALLTHROUGH */
14201 case DW_FORM_GNU_strp_alt:
14202 {
14203 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14204 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14205 &bytes_read);
14206
14207 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14208 DW_STRING_IS_CANONICAL (attr) = 0;
14209 info_ptr += bytes_read;
14210 }
14211 break;
14212 case DW_FORM_exprloc:
14213 case DW_FORM_block:
14214 blk = dwarf_alloc_block (cu);
14215 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14216 info_ptr += bytes_read;
14217 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14218 info_ptr += blk->size;
14219 DW_BLOCK (attr) = blk;
14220 break;
14221 case DW_FORM_block1:
14222 blk = dwarf_alloc_block (cu);
14223 blk->size = read_1_byte (abfd, info_ptr);
14224 info_ptr += 1;
14225 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14226 info_ptr += blk->size;
14227 DW_BLOCK (attr) = blk;
14228 break;
14229 case DW_FORM_data1:
14230 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14231 info_ptr += 1;
14232 break;
14233 case DW_FORM_flag:
14234 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14235 info_ptr += 1;
14236 break;
14237 case DW_FORM_flag_present:
14238 DW_UNSND (attr) = 1;
14239 break;
14240 case DW_FORM_sdata:
14241 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14242 info_ptr += bytes_read;
14243 break;
14244 case DW_FORM_udata:
14245 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14246 info_ptr += bytes_read;
14247 break;
14248 case DW_FORM_ref1:
14249 DW_UNSND (attr) = (cu->header.offset.sect_off
14250 + read_1_byte (abfd, info_ptr));
14251 info_ptr += 1;
14252 break;
14253 case DW_FORM_ref2:
14254 DW_UNSND (attr) = (cu->header.offset.sect_off
14255 + read_2_bytes (abfd, info_ptr));
14256 info_ptr += 2;
14257 break;
14258 case DW_FORM_ref4:
14259 DW_UNSND (attr) = (cu->header.offset.sect_off
14260 + read_4_bytes (abfd, info_ptr));
14261 info_ptr += 4;
14262 break;
14263 case DW_FORM_ref8:
14264 DW_UNSND (attr) = (cu->header.offset.sect_off
14265 + read_8_bytes (abfd, info_ptr));
14266 info_ptr += 8;
14267 break;
14268 case DW_FORM_ref_sig8:
14269 /* Convert the signature to something we can record in DW_UNSND
14270 for later lookup.
14271 NOTE: This is NULL if the type wasn't found. */
14272 DW_SIGNATURED_TYPE (attr) =
14273 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14274 info_ptr += 8;
14275 break;
14276 case DW_FORM_ref_udata:
14277 DW_UNSND (attr) = (cu->header.offset.sect_off
14278 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14279 info_ptr += bytes_read;
14280 break;
14281 case DW_FORM_indirect:
14282 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14283 info_ptr += bytes_read;
14284 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14285 break;
14286 case DW_FORM_GNU_addr_index:
14287 if (reader->dwo_file == NULL)
14288 {
14289 /* For now flag a hard error.
14290 Later we can turn this into a complaint. */
14291 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14292 dwarf_form_name (form),
14293 bfd_get_filename (abfd));
14294 }
14295 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14296 info_ptr += bytes_read;
14297 break;
14298 case DW_FORM_GNU_str_index:
14299 if (reader->dwo_file == NULL)
14300 {
14301 /* For now flag a hard error.
14302 Later we can turn this into a complaint if warranted. */
14303 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14304 dwarf_form_name (form),
14305 bfd_get_filename (abfd));
14306 }
14307 {
14308 ULONGEST str_index =
14309 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14310
14311 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14312 DW_STRING_IS_CANONICAL (attr) = 0;
14313 info_ptr += bytes_read;
14314 }
14315 break;
14316 default:
14317 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14318 dwarf_form_name (form),
14319 bfd_get_filename (abfd));
14320 }
14321
14322 /* Super hack. */
14323 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14324 attr->form = DW_FORM_GNU_ref_alt;
14325
14326 /* We have seen instances where the compiler tried to emit a byte
14327 size attribute of -1 which ended up being encoded as an unsigned
14328 0xffffffff. Although 0xffffffff is technically a valid size value,
14329 an object of this size seems pretty unlikely so we can relatively
14330 safely treat these cases as if the size attribute was invalid and
14331 treat them as zero by default. */
14332 if (attr->name == DW_AT_byte_size
14333 && form == DW_FORM_data4
14334 && DW_UNSND (attr) >= 0xffffffff)
14335 {
14336 complaint
14337 (&symfile_complaints,
14338 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14339 hex_string (DW_UNSND (attr)));
14340 DW_UNSND (attr) = 0;
14341 }
14342
14343 return info_ptr;
14344 }
14345
14346 /* Read an attribute described by an abbreviated attribute. */
14347
14348 static gdb_byte *
14349 read_attribute (const struct die_reader_specs *reader,
14350 struct attribute *attr, struct attr_abbrev *abbrev,
14351 gdb_byte *info_ptr)
14352 {
14353 attr->name = abbrev->name;
14354 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14355 }
14356
14357 /* Read dwarf information from a buffer. */
14358
14359 static unsigned int
14360 read_1_byte (bfd *abfd, const gdb_byte *buf)
14361 {
14362 return bfd_get_8 (abfd, buf);
14363 }
14364
14365 static int
14366 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14367 {
14368 return bfd_get_signed_8 (abfd, buf);
14369 }
14370
14371 static unsigned int
14372 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14373 {
14374 return bfd_get_16 (abfd, buf);
14375 }
14376
14377 static int
14378 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14379 {
14380 return bfd_get_signed_16 (abfd, buf);
14381 }
14382
14383 static unsigned int
14384 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14385 {
14386 return bfd_get_32 (abfd, buf);
14387 }
14388
14389 static int
14390 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14391 {
14392 return bfd_get_signed_32 (abfd, buf);
14393 }
14394
14395 static ULONGEST
14396 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14397 {
14398 return bfd_get_64 (abfd, buf);
14399 }
14400
14401 static CORE_ADDR
14402 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14403 unsigned int *bytes_read)
14404 {
14405 struct comp_unit_head *cu_header = &cu->header;
14406 CORE_ADDR retval = 0;
14407
14408 if (cu_header->signed_addr_p)
14409 {
14410 switch (cu_header->addr_size)
14411 {
14412 case 2:
14413 retval = bfd_get_signed_16 (abfd, buf);
14414 break;
14415 case 4:
14416 retval = bfd_get_signed_32 (abfd, buf);
14417 break;
14418 case 8:
14419 retval = bfd_get_signed_64 (abfd, buf);
14420 break;
14421 default:
14422 internal_error (__FILE__, __LINE__,
14423 _("read_address: bad switch, signed [in module %s]"),
14424 bfd_get_filename (abfd));
14425 }
14426 }
14427 else
14428 {
14429 switch (cu_header->addr_size)
14430 {
14431 case 2:
14432 retval = bfd_get_16 (abfd, buf);
14433 break;
14434 case 4:
14435 retval = bfd_get_32 (abfd, buf);
14436 break;
14437 case 8:
14438 retval = bfd_get_64 (abfd, buf);
14439 break;
14440 default:
14441 internal_error (__FILE__, __LINE__,
14442 _("read_address: bad switch, "
14443 "unsigned [in module %s]"),
14444 bfd_get_filename (abfd));
14445 }
14446 }
14447
14448 *bytes_read = cu_header->addr_size;
14449 return retval;
14450 }
14451
14452 /* Read the initial length from a section. The (draft) DWARF 3
14453 specification allows the initial length to take up either 4 bytes
14454 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14455 bytes describe the length and all offsets will be 8 bytes in length
14456 instead of 4.
14457
14458 An older, non-standard 64-bit format is also handled by this
14459 function. The older format in question stores the initial length
14460 as an 8-byte quantity without an escape value. Lengths greater
14461 than 2^32 aren't very common which means that the initial 4 bytes
14462 is almost always zero. Since a length value of zero doesn't make
14463 sense for the 32-bit format, this initial zero can be considered to
14464 be an escape value which indicates the presence of the older 64-bit
14465 format. As written, the code can't detect (old format) lengths
14466 greater than 4GB. If it becomes necessary to handle lengths
14467 somewhat larger than 4GB, we could allow other small values (such
14468 as the non-sensical values of 1, 2, and 3) to also be used as
14469 escape values indicating the presence of the old format.
14470
14471 The value returned via bytes_read should be used to increment the
14472 relevant pointer after calling read_initial_length().
14473
14474 [ Note: read_initial_length() and read_offset() are based on the
14475 document entitled "DWARF Debugging Information Format", revision
14476 3, draft 8, dated November 19, 2001. This document was obtained
14477 from:
14478
14479 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14480
14481 This document is only a draft and is subject to change. (So beware.)
14482
14483 Details regarding the older, non-standard 64-bit format were
14484 determined empirically by examining 64-bit ELF files produced by
14485 the SGI toolchain on an IRIX 6.5 machine.
14486
14487 - Kevin, July 16, 2002
14488 ] */
14489
14490 static LONGEST
14491 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14492 {
14493 LONGEST length = bfd_get_32 (abfd, buf);
14494
14495 if (length == 0xffffffff)
14496 {
14497 length = bfd_get_64 (abfd, buf + 4);
14498 *bytes_read = 12;
14499 }
14500 else if (length == 0)
14501 {
14502 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14503 length = bfd_get_64 (abfd, buf);
14504 *bytes_read = 8;
14505 }
14506 else
14507 {
14508 *bytes_read = 4;
14509 }
14510
14511 return length;
14512 }
14513
14514 /* Cover function for read_initial_length.
14515 Returns the length of the object at BUF, and stores the size of the
14516 initial length in *BYTES_READ and stores the size that offsets will be in
14517 *OFFSET_SIZE.
14518 If the initial length size is not equivalent to that specified in
14519 CU_HEADER then issue a complaint.
14520 This is useful when reading non-comp-unit headers. */
14521
14522 static LONGEST
14523 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14524 const struct comp_unit_head *cu_header,
14525 unsigned int *bytes_read,
14526 unsigned int *offset_size)
14527 {
14528 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14529
14530 gdb_assert (cu_header->initial_length_size == 4
14531 || cu_header->initial_length_size == 8
14532 || cu_header->initial_length_size == 12);
14533
14534 if (cu_header->initial_length_size != *bytes_read)
14535 complaint (&symfile_complaints,
14536 _("intermixed 32-bit and 64-bit DWARF sections"));
14537
14538 *offset_size = (*bytes_read == 4) ? 4 : 8;
14539 return length;
14540 }
14541
14542 /* Read an offset from the data stream. The size of the offset is
14543 given by cu_header->offset_size. */
14544
14545 static LONGEST
14546 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14547 unsigned int *bytes_read)
14548 {
14549 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14550
14551 *bytes_read = cu_header->offset_size;
14552 return offset;
14553 }
14554
14555 /* Read an offset from the data stream. */
14556
14557 static LONGEST
14558 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14559 {
14560 LONGEST retval = 0;
14561
14562 switch (offset_size)
14563 {
14564 case 4:
14565 retval = bfd_get_32 (abfd, buf);
14566 break;
14567 case 8:
14568 retval = bfd_get_64 (abfd, buf);
14569 break;
14570 default:
14571 internal_error (__FILE__, __LINE__,
14572 _("read_offset_1: bad switch [in module %s]"),
14573 bfd_get_filename (abfd));
14574 }
14575
14576 return retval;
14577 }
14578
14579 static gdb_byte *
14580 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14581 {
14582 /* If the size of a host char is 8 bits, we can return a pointer
14583 to the buffer, otherwise we have to copy the data to a buffer
14584 allocated on the temporary obstack. */
14585 gdb_assert (HOST_CHAR_BIT == 8);
14586 return buf;
14587 }
14588
14589 static char *
14590 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14591 {
14592 /* If the size of a host char is 8 bits, we can return a pointer
14593 to the string, otherwise we have to copy the string to a buffer
14594 allocated on the temporary obstack. */
14595 gdb_assert (HOST_CHAR_BIT == 8);
14596 if (*buf == '\0')
14597 {
14598 *bytes_read_ptr = 1;
14599 return NULL;
14600 }
14601 *bytes_read_ptr = strlen ((char *) buf) + 1;
14602 return (char *) buf;
14603 }
14604
14605 static char *
14606 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14607 {
14608 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14609 if (dwarf2_per_objfile->str.buffer == NULL)
14610 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14611 bfd_get_filename (abfd));
14612 if (str_offset >= dwarf2_per_objfile->str.size)
14613 error (_("DW_FORM_strp pointing outside of "
14614 ".debug_str section [in module %s]"),
14615 bfd_get_filename (abfd));
14616 gdb_assert (HOST_CHAR_BIT == 8);
14617 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14618 return NULL;
14619 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14620 }
14621
14622 /* Read a string at offset STR_OFFSET in the .debug_str section from
14623 the .dwz file DWZ. Throw an error if the offset is too large. If
14624 the string consists of a single NUL byte, return NULL; otherwise
14625 return a pointer to the string. */
14626
14627 static char *
14628 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14629 {
14630 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14631
14632 if (dwz->str.buffer == NULL)
14633 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14634 "section [in module %s]"),
14635 bfd_get_filename (dwz->dwz_bfd));
14636 if (str_offset >= dwz->str.size)
14637 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14638 ".debug_str section [in module %s]"),
14639 bfd_get_filename (dwz->dwz_bfd));
14640 gdb_assert (HOST_CHAR_BIT == 8);
14641 if (dwz->str.buffer[str_offset] == '\0')
14642 return NULL;
14643 return (char *) (dwz->str.buffer + str_offset);
14644 }
14645
14646 static char *
14647 read_indirect_string (bfd *abfd, gdb_byte *buf,
14648 const struct comp_unit_head *cu_header,
14649 unsigned int *bytes_read_ptr)
14650 {
14651 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14652
14653 return read_indirect_string_at_offset (abfd, str_offset);
14654 }
14655
14656 static ULONGEST
14657 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14658 {
14659 ULONGEST result;
14660 unsigned int num_read;
14661 int i, shift;
14662 unsigned char byte;
14663
14664 result = 0;
14665 shift = 0;
14666 num_read = 0;
14667 i = 0;
14668 while (1)
14669 {
14670 byte = bfd_get_8 (abfd, buf);
14671 buf++;
14672 num_read++;
14673 result |= ((ULONGEST) (byte & 127) << shift);
14674 if ((byte & 128) == 0)
14675 {
14676 break;
14677 }
14678 shift += 7;
14679 }
14680 *bytes_read_ptr = num_read;
14681 return result;
14682 }
14683
14684 static LONGEST
14685 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14686 {
14687 LONGEST result;
14688 int i, shift, num_read;
14689 unsigned char byte;
14690
14691 result = 0;
14692 shift = 0;
14693 num_read = 0;
14694 i = 0;
14695 while (1)
14696 {
14697 byte = bfd_get_8 (abfd, buf);
14698 buf++;
14699 num_read++;
14700 result |= ((LONGEST) (byte & 127) << shift);
14701 shift += 7;
14702 if ((byte & 128) == 0)
14703 {
14704 break;
14705 }
14706 }
14707 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14708 result |= -(((LONGEST) 1) << shift);
14709 *bytes_read_ptr = num_read;
14710 return result;
14711 }
14712
14713 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14714 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14715 ADDR_SIZE is the size of addresses from the CU header. */
14716
14717 static CORE_ADDR
14718 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14719 {
14720 struct objfile *objfile = dwarf2_per_objfile->objfile;
14721 bfd *abfd = objfile->obfd;
14722 const gdb_byte *info_ptr;
14723
14724 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14725 if (dwarf2_per_objfile->addr.buffer == NULL)
14726 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14727 objfile->name);
14728 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14729 error (_("DW_FORM_addr_index pointing outside of "
14730 ".debug_addr section [in module %s]"),
14731 objfile->name);
14732 info_ptr = (dwarf2_per_objfile->addr.buffer
14733 + addr_base + addr_index * addr_size);
14734 if (addr_size == 4)
14735 return bfd_get_32 (abfd, info_ptr);
14736 else
14737 return bfd_get_64 (abfd, info_ptr);
14738 }
14739
14740 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14741
14742 static CORE_ADDR
14743 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14744 {
14745 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14746 }
14747
14748 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14749
14750 static CORE_ADDR
14751 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14752 unsigned int *bytes_read)
14753 {
14754 bfd *abfd = cu->objfile->obfd;
14755 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14756
14757 return read_addr_index (cu, addr_index);
14758 }
14759
14760 /* Data structure to pass results from dwarf2_read_addr_index_reader
14761 back to dwarf2_read_addr_index. */
14762
14763 struct dwarf2_read_addr_index_data
14764 {
14765 ULONGEST addr_base;
14766 int addr_size;
14767 };
14768
14769 /* die_reader_func for dwarf2_read_addr_index. */
14770
14771 static void
14772 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14773 gdb_byte *info_ptr,
14774 struct die_info *comp_unit_die,
14775 int has_children,
14776 void *data)
14777 {
14778 struct dwarf2_cu *cu = reader->cu;
14779 struct dwarf2_read_addr_index_data *aidata =
14780 (struct dwarf2_read_addr_index_data *) data;
14781
14782 aidata->addr_base = cu->addr_base;
14783 aidata->addr_size = cu->header.addr_size;
14784 }
14785
14786 /* Given an index in .debug_addr, fetch the value.
14787 NOTE: This can be called during dwarf expression evaluation,
14788 long after the debug information has been read, and thus per_cu->cu
14789 may no longer exist. */
14790
14791 CORE_ADDR
14792 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14793 unsigned int addr_index)
14794 {
14795 struct objfile *objfile = per_cu->objfile;
14796 struct dwarf2_cu *cu = per_cu->cu;
14797 ULONGEST addr_base;
14798 int addr_size;
14799
14800 /* This is intended to be called from outside this file. */
14801 dw2_setup (objfile);
14802
14803 /* We need addr_base and addr_size.
14804 If we don't have PER_CU->cu, we have to get it.
14805 Nasty, but the alternative is storing the needed info in PER_CU,
14806 which at this point doesn't seem justified: it's not clear how frequently
14807 it would get used and it would increase the size of every PER_CU.
14808 Entry points like dwarf2_per_cu_addr_size do a similar thing
14809 so we're not in uncharted territory here.
14810 Alas we need to be a bit more complicated as addr_base is contained
14811 in the DIE.
14812
14813 We don't need to read the entire CU(/TU).
14814 We just need the header and top level die.
14815
14816 IWBN to use the aging mechanism to let us lazily later discard the CU.
14817 For now we skip this optimization. */
14818
14819 if (cu != NULL)
14820 {
14821 addr_base = cu->addr_base;
14822 addr_size = cu->header.addr_size;
14823 }
14824 else
14825 {
14826 struct dwarf2_read_addr_index_data aidata;
14827
14828 /* Note: We can't use init_cutu_and_read_dies_simple here,
14829 we need addr_base. */
14830 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14831 dwarf2_read_addr_index_reader, &aidata);
14832 addr_base = aidata.addr_base;
14833 addr_size = aidata.addr_size;
14834 }
14835
14836 return read_addr_index_1 (addr_index, addr_base, addr_size);
14837 }
14838
14839 /* Given a DW_AT_str_index, fetch the string. */
14840
14841 static char *
14842 read_str_index (const struct die_reader_specs *reader,
14843 struct dwarf2_cu *cu, ULONGEST str_index)
14844 {
14845 struct objfile *objfile = dwarf2_per_objfile->objfile;
14846 const char *dwo_name = objfile->name;
14847 bfd *abfd = objfile->obfd;
14848 struct dwo_sections *sections = &reader->dwo_file->sections;
14849 gdb_byte *info_ptr;
14850 ULONGEST str_offset;
14851
14852 dwarf2_read_section (objfile, &sections->str);
14853 dwarf2_read_section (objfile, &sections->str_offsets);
14854 if (sections->str.buffer == NULL)
14855 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14856 " in CU at offset 0x%lx [in module %s]"),
14857 (long) cu->header.offset.sect_off, dwo_name);
14858 if (sections->str_offsets.buffer == NULL)
14859 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14860 " in CU at offset 0x%lx [in module %s]"),
14861 (long) cu->header.offset.sect_off, dwo_name);
14862 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14863 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14864 " section in CU at offset 0x%lx [in module %s]"),
14865 (long) cu->header.offset.sect_off, dwo_name);
14866 info_ptr = (sections->str_offsets.buffer
14867 + str_index * cu->header.offset_size);
14868 if (cu->header.offset_size == 4)
14869 str_offset = bfd_get_32 (abfd, info_ptr);
14870 else
14871 str_offset = bfd_get_64 (abfd, info_ptr);
14872 if (str_offset >= sections->str.size)
14873 error (_("Offset from DW_FORM_str_index pointing outside of"
14874 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14875 (long) cu->header.offset.sect_off, dwo_name);
14876 return (char *) (sections->str.buffer + str_offset);
14877 }
14878
14879 /* Return the length of an LEB128 number in BUF. */
14880
14881 static int
14882 leb128_size (const gdb_byte *buf)
14883 {
14884 const gdb_byte *begin = buf;
14885 gdb_byte byte;
14886
14887 while (1)
14888 {
14889 byte = *buf++;
14890 if ((byte & 128) == 0)
14891 return buf - begin;
14892 }
14893 }
14894
14895 static void
14896 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14897 {
14898 switch (lang)
14899 {
14900 case DW_LANG_C89:
14901 case DW_LANG_C99:
14902 case DW_LANG_C:
14903 cu->language = language_c;
14904 break;
14905 case DW_LANG_C_plus_plus:
14906 cu->language = language_cplus;
14907 break;
14908 case DW_LANG_D:
14909 cu->language = language_d;
14910 break;
14911 case DW_LANG_Fortran77:
14912 case DW_LANG_Fortran90:
14913 case DW_LANG_Fortran95:
14914 cu->language = language_fortran;
14915 break;
14916 case DW_LANG_Go:
14917 cu->language = language_go;
14918 break;
14919 case DW_LANG_Mips_Assembler:
14920 cu->language = language_asm;
14921 break;
14922 case DW_LANG_Java:
14923 cu->language = language_java;
14924 break;
14925 case DW_LANG_Ada83:
14926 case DW_LANG_Ada95:
14927 cu->language = language_ada;
14928 break;
14929 case DW_LANG_Modula2:
14930 cu->language = language_m2;
14931 break;
14932 case DW_LANG_Pascal83:
14933 cu->language = language_pascal;
14934 break;
14935 case DW_LANG_ObjC:
14936 cu->language = language_objc;
14937 break;
14938 case DW_LANG_Cobol74:
14939 case DW_LANG_Cobol85:
14940 default:
14941 cu->language = language_minimal;
14942 break;
14943 }
14944 cu->language_defn = language_def (cu->language);
14945 }
14946
14947 /* Return the named attribute or NULL if not there. */
14948
14949 static struct attribute *
14950 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14951 {
14952 for (;;)
14953 {
14954 unsigned int i;
14955 struct attribute *spec = NULL;
14956
14957 for (i = 0; i < die->num_attrs; ++i)
14958 {
14959 if (die->attrs[i].name == name)
14960 return &die->attrs[i];
14961 if (die->attrs[i].name == DW_AT_specification
14962 || die->attrs[i].name == DW_AT_abstract_origin)
14963 spec = &die->attrs[i];
14964 }
14965
14966 if (!spec)
14967 break;
14968
14969 die = follow_die_ref (die, spec, &cu);
14970 }
14971
14972 return NULL;
14973 }
14974
14975 /* Return the named attribute or NULL if not there,
14976 but do not follow DW_AT_specification, etc.
14977 This is for use in contexts where we're reading .debug_types dies.
14978 Following DW_AT_specification, DW_AT_abstract_origin will take us
14979 back up the chain, and we want to go down. */
14980
14981 static struct attribute *
14982 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14983 {
14984 unsigned int i;
14985
14986 for (i = 0; i < die->num_attrs; ++i)
14987 if (die->attrs[i].name == name)
14988 return &die->attrs[i];
14989
14990 return NULL;
14991 }
14992
14993 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14994 and holds a non-zero value. This function should only be used for
14995 DW_FORM_flag or DW_FORM_flag_present attributes. */
14996
14997 static int
14998 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14999 {
15000 struct attribute *attr = dwarf2_attr (die, name, cu);
15001
15002 return (attr && DW_UNSND (attr));
15003 }
15004
15005 static int
15006 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15007 {
15008 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15009 which value is non-zero. However, we have to be careful with
15010 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15011 (via dwarf2_flag_true_p) follows this attribute. So we may
15012 end up accidently finding a declaration attribute that belongs
15013 to a different DIE referenced by the specification attribute,
15014 even though the given DIE does not have a declaration attribute. */
15015 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15016 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15017 }
15018
15019 /* Return the die giving the specification for DIE, if there is
15020 one. *SPEC_CU is the CU containing DIE on input, and the CU
15021 containing the return value on output. If there is no
15022 specification, but there is an abstract origin, that is
15023 returned. */
15024
15025 static struct die_info *
15026 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15027 {
15028 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15029 *spec_cu);
15030
15031 if (spec_attr == NULL)
15032 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15033
15034 if (spec_attr == NULL)
15035 return NULL;
15036 else
15037 return follow_die_ref (die, spec_attr, spec_cu);
15038 }
15039
15040 /* Free the line_header structure *LH, and any arrays and strings it
15041 refers to.
15042 NOTE: This is also used as a "cleanup" function. */
15043
15044 static void
15045 free_line_header (struct line_header *lh)
15046 {
15047 if (lh->standard_opcode_lengths)
15048 xfree (lh->standard_opcode_lengths);
15049
15050 /* Remember that all the lh->file_names[i].name pointers are
15051 pointers into debug_line_buffer, and don't need to be freed. */
15052 if (lh->file_names)
15053 xfree (lh->file_names);
15054
15055 /* Similarly for the include directory names. */
15056 if (lh->include_dirs)
15057 xfree (lh->include_dirs);
15058
15059 xfree (lh);
15060 }
15061
15062 /* Add an entry to LH's include directory table. */
15063
15064 static void
15065 add_include_dir (struct line_header *lh, char *include_dir)
15066 {
15067 /* Grow the array if necessary. */
15068 if (lh->include_dirs_size == 0)
15069 {
15070 lh->include_dirs_size = 1; /* for testing */
15071 lh->include_dirs = xmalloc (lh->include_dirs_size
15072 * sizeof (*lh->include_dirs));
15073 }
15074 else if (lh->num_include_dirs >= lh->include_dirs_size)
15075 {
15076 lh->include_dirs_size *= 2;
15077 lh->include_dirs = xrealloc (lh->include_dirs,
15078 (lh->include_dirs_size
15079 * sizeof (*lh->include_dirs)));
15080 }
15081
15082 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15083 }
15084
15085 /* Add an entry to LH's file name table. */
15086
15087 static void
15088 add_file_name (struct line_header *lh,
15089 char *name,
15090 unsigned int dir_index,
15091 unsigned int mod_time,
15092 unsigned int length)
15093 {
15094 struct file_entry *fe;
15095
15096 /* Grow the array if necessary. */
15097 if (lh->file_names_size == 0)
15098 {
15099 lh->file_names_size = 1; /* for testing */
15100 lh->file_names = xmalloc (lh->file_names_size
15101 * sizeof (*lh->file_names));
15102 }
15103 else if (lh->num_file_names >= lh->file_names_size)
15104 {
15105 lh->file_names_size *= 2;
15106 lh->file_names = xrealloc (lh->file_names,
15107 (lh->file_names_size
15108 * sizeof (*lh->file_names)));
15109 }
15110
15111 fe = &lh->file_names[lh->num_file_names++];
15112 fe->name = name;
15113 fe->dir_index = dir_index;
15114 fe->mod_time = mod_time;
15115 fe->length = length;
15116 fe->included_p = 0;
15117 fe->symtab = NULL;
15118 }
15119
15120 /* A convenience function to find the proper .debug_line section for a
15121 CU. */
15122
15123 static struct dwarf2_section_info *
15124 get_debug_line_section (struct dwarf2_cu *cu)
15125 {
15126 struct dwarf2_section_info *section;
15127
15128 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15129 DWO file. */
15130 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15131 section = &cu->dwo_unit->dwo_file->sections.line;
15132 else if (cu->per_cu->is_dwz)
15133 {
15134 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15135
15136 section = &dwz->line;
15137 }
15138 else
15139 section = &dwarf2_per_objfile->line;
15140
15141 return section;
15142 }
15143
15144 /* Read the statement program header starting at OFFSET in
15145 .debug_line, or .debug_line.dwo. Return a pointer
15146 to a struct line_header, allocated using xmalloc.
15147
15148 NOTE: the strings in the include directory and file name tables of
15149 the returned object point into the dwarf line section buffer,
15150 and must not be freed. */
15151
15152 static struct line_header *
15153 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15154 {
15155 struct cleanup *back_to;
15156 struct line_header *lh;
15157 gdb_byte *line_ptr;
15158 unsigned int bytes_read, offset_size;
15159 int i;
15160 char *cur_dir, *cur_file;
15161 struct dwarf2_section_info *section;
15162 bfd *abfd;
15163
15164 section = get_debug_line_section (cu);
15165 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15166 if (section->buffer == NULL)
15167 {
15168 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15169 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15170 else
15171 complaint (&symfile_complaints, _("missing .debug_line section"));
15172 return 0;
15173 }
15174
15175 /* We can't do this until we know the section is non-empty.
15176 Only then do we know we have such a section. */
15177 abfd = section->asection->owner;
15178
15179 /* Make sure that at least there's room for the total_length field.
15180 That could be 12 bytes long, but we're just going to fudge that. */
15181 if (offset + 4 >= section->size)
15182 {
15183 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15184 return 0;
15185 }
15186
15187 lh = xmalloc (sizeof (*lh));
15188 memset (lh, 0, sizeof (*lh));
15189 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15190 (void *) lh);
15191
15192 line_ptr = section->buffer + offset;
15193
15194 /* Read in the header. */
15195 lh->total_length =
15196 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15197 &bytes_read, &offset_size);
15198 line_ptr += bytes_read;
15199 if (line_ptr + lh->total_length > (section->buffer + section->size))
15200 {
15201 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15202 return 0;
15203 }
15204 lh->statement_program_end = line_ptr + lh->total_length;
15205 lh->version = read_2_bytes (abfd, line_ptr);
15206 line_ptr += 2;
15207 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15208 line_ptr += offset_size;
15209 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15210 line_ptr += 1;
15211 if (lh->version >= 4)
15212 {
15213 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15214 line_ptr += 1;
15215 }
15216 else
15217 lh->maximum_ops_per_instruction = 1;
15218
15219 if (lh->maximum_ops_per_instruction == 0)
15220 {
15221 lh->maximum_ops_per_instruction = 1;
15222 complaint (&symfile_complaints,
15223 _("invalid maximum_ops_per_instruction "
15224 "in `.debug_line' section"));
15225 }
15226
15227 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15228 line_ptr += 1;
15229 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15230 line_ptr += 1;
15231 lh->line_range = read_1_byte (abfd, line_ptr);
15232 line_ptr += 1;
15233 lh->opcode_base = read_1_byte (abfd, line_ptr);
15234 line_ptr += 1;
15235 lh->standard_opcode_lengths
15236 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15237
15238 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15239 for (i = 1; i < lh->opcode_base; ++i)
15240 {
15241 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15242 line_ptr += 1;
15243 }
15244
15245 /* Read directory table. */
15246 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15247 {
15248 line_ptr += bytes_read;
15249 add_include_dir (lh, cur_dir);
15250 }
15251 line_ptr += bytes_read;
15252
15253 /* Read file name table. */
15254 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15255 {
15256 unsigned int dir_index, mod_time, length;
15257
15258 line_ptr += bytes_read;
15259 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15260 line_ptr += bytes_read;
15261 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15262 line_ptr += bytes_read;
15263 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15264 line_ptr += bytes_read;
15265
15266 add_file_name (lh, cur_file, dir_index, mod_time, length);
15267 }
15268 line_ptr += bytes_read;
15269 lh->statement_program_start = line_ptr;
15270
15271 if (line_ptr > (section->buffer + section->size))
15272 complaint (&symfile_complaints,
15273 _("line number info header doesn't "
15274 "fit in `.debug_line' section"));
15275
15276 discard_cleanups (back_to);
15277 return lh;
15278 }
15279
15280 /* Subroutine of dwarf_decode_lines to simplify it.
15281 Return the file name of the psymtab for included file FILE_INDEX
15282 in line header LH of PST.
15283 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15284 If space for the result is malloc'd, it will be freed by a cleanup.
15285 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15286
15287 The function creates dangling cleanup registration. */
15288
15289 static char *
15290 psymtab_include_file_name (const struct line_header *lh, int file_index,
15291 const struct partial_symtab *pst,
15292 const char *comp_dir)
15293 {
15294 const struct file_entry fe = lh->file_names [file_index];
15295 char *include_name = fe.name;
15296 char *include_name_to_compare = include_name;
15297 char *dir_name = NULL;
15298 const char *pst_filename;
15299 char *copied_name = NULL;
15300 int file_is_pst;
15301
15302 if (fe.dir_index)
15303 dir_name = lh->include_dirs[fe.dir_index - 1];
15304
15305 if (!IS_ABSOLUTE_PATH (include_name)
15306 && (dir_name != NULL || comp_dir != NULL))
15307 {
15308 /* Avoid creating a duplicate psymtab for PST.
15309 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15310 Before we do the comparison, however, we need to account
15311 for DIR_NAME and COMP_DIR.
15312 First prepend dir_name (if non-NULL). If we still don't
15313 have an absolute path prepend comp_dir (if non-NULL).
15314 However, the directory we record in the include-file's
15315 psymtab does not contain COMP_DIR (to match the
15316 corresponding symtab(s)).
15317
15318 Example:
15319
15320 bash$ cd /tmp
15321 bash$ gcc -g ./hello.c
15322 include_name = "hello.c"
15323 dir_name = "."
15324 DW_AT_comp_dir = comp_dir = "/tmp"
15325 DW_AT_name = "./hello.c" */
15326
15327 if (dir_name != NULL)
15328 {
15329 include_name = concat (dir_name, SLASH_STRING,
15330 include_name, (char *)NULL);
15331 include_name_to_compare = include_name;
15332 make_cleanup (xfree, include_name);
15333 }
15334 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15335 {
15336 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15337 include_name, (char *)NULL);
15338 }
15339 }
15340
15341 pst_filename = pst->filename;
15342 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15343 {
15344 copied_name = concat (pst->dirname, SLASH_STRING,
15345 pst_filename, (char *)NULL);
15346 pst_filename = copied_name;
15347 }
15348
15349 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15350
15351 if (include_name_to_compare != include_name)
15352 xfree (include_name_to_compare);
15353 if (copied_name != NULL)
15354 xfree (copied_name);
15355
15356 if (file_is_pst)
15357 return NULL;
15358 return include_name;
15359 }
15360
15361 /* Ignore this record_line request. */
15362
15363 static void
15364 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15365 {
15366 return;
15367 }
15368
15369 /* Subroutine of dwarf_decode_lines to simplify it.
15370 Process the line number information in LH. */
15371
15372 static void
15373 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15374 struct dwarf2_cu *cu, struct partial_symtab *pst)
15375 {
15376 gdb_byte *line_ptr, *extended_end;
15377 gdb_byte *line_end;
15378 unsigned int bytes_read, extended_len;
15379 unsigned char op_code, extended_op, adj_opcode;
15380 CORE_ADDR baseaddr;
15381 struct objfile *objfile = cu->objfile;
15382 bfd *abfd = objfile->obfd;
15383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15384 const int decode_for_pst_p = (pst != NULL);
15385 struct subfile *last_subfile = NULL;
15386 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15387 = record_line;
15388
15389 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15390
15391 line_ptr = lh->statement_program_start;
15392 line_end = lh->statement_program_end;
15393
15394 /* Read the statement sequences until there's nothing left. */
15395 while (line_ptr < line_end)
15396 {
15397 /* state machine registers */
15398 CORE_ADDR address = 0;
15399 unsigned int file = 1;
15400 unsigned int line = 1;
15401 unsigned int column = 0;
15402 int is_stmt = lh->default_is_stmt;
15403 int basic_block = 0;
15404 int end_sequence = 0;
15405 CORE_ADDR addr;
15406 unsigned char op_index = 0;
15407
15408 if (!decode_for_pst_p && lh->num_file_names >= file)
15409 {
15410 /* Start a subfile for the current file of the state machine. */
15411 /* lh->include_dirs and lh->file_names are 0-based, but the
15412 directory and file name numbers in the statement program
15413 are 1-based. */
15414 struct file_entry *fe = &lh->file_names[file - 1];
15415 char *dir = NULL;
15416
15417 if (fe->dir_index)
15418 dir = lh->include_dirs[fe->dir_index - 1];
15419
15420 dwarf2_start_subfile (fe->name, dir, comp_dir);
15421 }
15422
15423 /* Decode the table. */
15424 while (!end_sequence)
15425 {
15426 op_code = read_1_byte (abfd, line_ptr);
15427 line_ptr += 1;
15428 if (line_ptr > line_end)
15429 {
15430 dwarf2_debug_line_missing_end_sequence_complaint ();
15431 break;
15432 }
15433
15434 if (op_code >= lh->opcode_base)
15435 {
15436 /* Special operand. */
15437 adj_opcode = op_code - lh->opcode_base;
15438 address += (((op_index + (adj_opcode / lh->line_range))
15439 / lh->maximum_ops_per_instruction)
15440 * lh->minimum_instruction_length);
15441 op_index = ((op_index + (adj_opcode / lh->line_range))
15442 % lh->maximum_ops_per_instruction);
15443 line += lh->line_base + (adj_opcode % lh->line_range);
15444 if (lh->num_file_names < file || file == 0)
15445 dwarf2_debug_line_missing_file_complaint ();
15446 /* For now we ignore lines not starting on an
15447 instruction boundary. */
15448 else if (op_index == 0)
15449 {
15450 lh->file_names[file - 1].included_p = 1;
15451 if (!decode_for_pst_p && is_stmt)
15452 {
15453 if (last_subfile != current_subfile)
15454 {
15455 addr = gdbarch_addr_bits_remove (gdbarch, address);
15456 if (last_subfile)
15457 (*p_record_line) (last_subfile, 0, addr);
15458 last_subfile = current_subfile;
15459 }
15460 /* Append row to matrix using current values. */
15461 addr = gdbarch_addr_bits_remove (gdbarch, address);
15462 (*p_record_line) (current_subfile, line, addr);
15463 }
15464 }
15465 basic_block = 0;
15466 }
15467 else switch (op_code)
15468 {
15469 case DW_LNS_extended_op:
15470 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15471 &bytes_read);
15472 line_ptr += bytes_read;
15473 extended_end = line_ptr + extended_len;
15474 extended_op = read_1_byte (abfd, line_ptr);
15475 line_ptr += 1;
15476 switch (extended_op)
15477 {
15478 case DW_LNE_end_sequence:
15479 p_record_line = record_line;
15480 end_sequence = 1;
15481 break;
15482 case DW_LNE_set_address:
15483 address = read_address (abfd, line_ptr, cu, &bytes_read);
15484
15485 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15486 {
15487 /* This line table is for a function which has been
15488 GCd by the linker. Ignore it. PR gdb/12528 */
15489
15490 long line_offset
15491 = line_ptr - get_debug_line_section (cu)->buffer;
15492
15493 complaint (&symfile_complaints,
15494 _(".debug_line address at offset 0x%lx is 0 "
15495 "[in module %s]"),
15496 line_offset, objfile->name);
15497 p_record_line = noop_record_line;
15498 }
15499
15500 op_index = 0;
15501 line_ptr += bytes_read;
15502 address += baseaddr;
15503 break;
15504 case DW_LNE_define_file:
15505 {
15506 char *cur_file;
15507 unsigned int dir_index, mod_time, length;
15508
15509 cur_file = read_direct_string (abfd, line_ptr,
15510 &bytes_read);
15511 line_ptr += bytes_read;
15512 dir_index =
15513 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15514 line_ptr += bytes_read;
15515 mod_time =
15516 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15517 line_ptr += bytes_read;
15518 length =
15519 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15520 line_ptr += bytes_read;
15521 add_file_name (lh, cur_file, dir_index, mod_time, length);
15522 }
15523 break;
15524 case DW_LNE_set_discriminator:
15525 /* The discriminator is not interesting to the debugger;
15526 just ignore it. */
15527 line_ptr = extended_end;
15528 break;
15529 default:
15530 complaint (&symfile_complaints,
15531 _("mangled .debug_line section"));
15532 return;
15533 }
15534 /* Make sure that we parsed the extended op correctly. If e.g.
15535 we expected a different address size than the producer used,
15536 we may have read the wrong number of bytes. */
15537 if (line_ptr != extended_end)
15538 {
15539 complaint (&symfile_complaints,
15540 _("mangled .debug_line section"));
15541 return;
15542 }
15543 break;
15544 case DW_LNS_copy:
15545 if (lh->num_file_names < file || file == 0)
15546 dwarf2_debug_line_missing_file_complaint ();
15547 else
15548 {
15549 lh->file_names[file - 1].included_p = 1;
15550 if (!decode_for_pst_p && is_stmt)
15551 {
15552 if (last_subfile != current_subfile)
15553 {
15554 addr = gdbarch_addr_bits_remove (gdbarch, address);
15555 if (last_subfile)
15556 (*p_record_line) (last_subfile, 0, addr);
15557 last_subfile = current_subfile;
15558 }
15559 addr = gdbarch_addr_bits_remove (gdbarch, address);
15560 (*p_record_line) (current_subfile, line, addr);
15561 }
15562 }
15563 basic_block = 0;
15564 break;
15565 case DW_LNS_advance_pc:
15566 {
15567 CORE_ADDR adjust
15568 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15569
15570 address += (((op_index + adjust)
15571 / lh->maximum_ops_per_instruction)
15572 * lh->minimum_instruction_length);
15573 op_index = ((op_index + adjust)
15574 % lh->maximum_ops_per_instruction);
15575 line_ptr += bytes_read;
15576 }
15577 break;
15578 case DW_LNS_advance_line:
15579 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15580 line_ptr += bytes_read;
15581 break;
15582 case DW_LNS_set_file:
15583 {
15584 /* The arrays lh->include_dirs and lh->file_names are
15585 0-based, but the directory and file name numbers in
15586 the statement program are 1-based. */
15587 struct file_entry *fe;
15588 char *dir = NULL;
15589
15590 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15591 line_ptr += bytes_read;
15592 if (lh->num_file_names < file || file == 0)
15593 dwarf2_debug_line_missing_file_complaint ();
15594 else
15595 {
15596 fe = &lh->file_names[file - 1];
15597 if (fe->dir_index)
15598 dir = lh->include_dirs[fe->dir_index - 1];
15599 if (!decode_for_pst_p)
15600 {
15601 last_subfile = current_subfile;
15602 dwarf2_start_subfile (fe->name, dir, comp_dir);
15603 }
15604 }
15605 }
15606 break;
15607 case DW_LNS_set_column:
15608 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15609 line_ptr += bytes_read;
15610 break;
15611 case DW_LNS_negate_stmt:
15612 is_stmt = (!is_stmt);
15613 break;
15614 case DW_LNS_set_basic_block:
15615 basic_block = 1;
15616 break;
15617 /* Add to the address register of the state machine the
15618 address increment value corresponding to special opcode
15619 255. I.e., this value is scaled by the minimum
15620 instruction length since special opcode 255 would have
15621 scaled the increment. */
15622 case DW_LNS_const_add_pc:
15623 {
15624 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15625
15626 address += (((op_index + adjust)
15627 / lh->maximum_ops_per_instruction)
15628 * lh->minimum_instruction_length);
15629 op_index = ((op_index + adjust)
15630 % lh->maximum_ops_per_instruction);
15631 }
15632 break;
15633 case DW_LNS_fixed_advance_pc:
15634 address += read_2_bytes (abfd, line_ptr);
15635 op_index = 0;
15636 line_ptr += 2;
15637 break;
15638 default:
15639 {
15640 /* Unknown standard opcode, ignore it. */
15641 int i;
15642
15643 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15644 {
15645 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15646 line_ptr += bytes_read;
15647 }
15648 }
15649 }
15650 }
15651 if (lh->num_file_names < file || file == 0)
15652 dwarf2_debug_line_missing_file_complaint ();
15653 else
15654 {
15655 lh->file_names[file - 1].included_p = 1;
15656 if (!decode_for_pst_p)
15657 {
15658 addr = gdbarch_addr_bits_remove (gdbarch, address);
15659 (*p_record_line) (current_subfile, 0, addr);
15660 }
15661 }
15662 }
15663 }
15664
15665 /* Decode the Line Number Program (LNP) for the given line_header
15666 structure and CU. The actual information extracted and the type
15667 of structures created from the LNP depends on the value of PST.
15668
15669 1. If PST is NULL, then this procedure uses the data from the program
15670 to create all necessary symbol tables, and their linetables.
15671
15672 2. If PST is not NULL, this procedure reads the program to determine
15673 the list of files included by the unit represented by PST, and
15674 builds all the associated partial symbol tables.
15675
15676 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15677 It is used for relative paths in the line table.
15678 NOTE: When processing partial symtabs (pst != NULL),
15679 comp_dir == pst->dirname.
15680
15681 NOTE: It is important that psymtabs have the same file name (via strcmp)
15682 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15683 symtab we don't use it in the name of the psymtabs we create.
15684 E.g. expand_line_sal requires this when finding psymtabs to expand.
15685 A good testcase for this is mb-inline.exp. */
15686
15687 static void
15688 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15689 struct dwarf2_cu *cu, struct partial_symtab *pst,
15690 int want_line_info)
15691 {
15692 struct objfile *objfile = cu->objfile;
15693 const int decode_for_pst_p = (pst != NULL);
15694 struct subfile *first_subfile = current_subfile;
15695
15696 if (want_line_info)
15697 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15698
15699 if (decode_for_pst_p)
15700 {
15701 int file_index;
15702
15703 /* Now that we're done scanning the Line Header Program, we can
15704 create the psymtab of each included file. */
15705 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15706 if (lh->file_names[file_index].included_p == 1)
15707 {
15708 char *include_name =
15709 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15710 if (include_name != NULL)
15711 dwarf2_create_include_psymtab (include_name, pst, objfile);
15712 }
15713 }
15714 else
15715 {
15716 /* Make sure a symtab is created for every file, even files
15717 which contain only variables (i.e. no code with associated
15718 line numbers). */
15719 int i;
15720
15721 for (i = 0; i < lh->num_file_names; i++)
15722 {
15723 char *dir = NULL;
15724 struct file_entry *fe;
15725
15726 fe = &lh->file_names[i];
15727 if (fe->dir_index)
15728 dir = lh->include_dirs[fe->dir_index - 1];
15729 dwarf2_start_subfile (fe->name, dir, comp_dir);
15730
15731 /* Skip the main file; we don't need it, and it must be
15732 allocated last, so that it will show up before the
15733 non-primary symtabs in the objfile's symtab list. */
15734 if (current_subfile == first_subfile)
15735 continue;
15736
15737 if (current_subfile->symtab == NULL)
15738 current_subfile->symtab = allocate_symtab (current_subfile->name,
15739 objfile);
15740 fe->symtab = current_subfile->symtab;
15741 }
15742 }
15743 }
15744
15745 /* Start a subfile for DWARF. FILENAME is the name of the file and
15746 DIRNAME the name of the source directory which contains FILENAME
15747 or NULL if not known. COMP_DIR is the compilation directory for the
15748 linetable's compilation unit or NULL if not known.
15749 This routine tries to keep line numbers from identical absolute and
15750 relative file names in a common subfile.
15751
15752 Using the `list' example from the GDB testsuite, which resides in
15753 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15754 of /srcdir/list0.c yields the following debugging information for list0.c:
15755
15756 DW_AT_name: /srcdir/list0.c
15757 DW_AT_comp_dir: /compdir
15758 files.files[0].name: list0.h
15759 files.files[0].dir: /srcdir
15760 files.files[1].name: list0.c
15761 files.files[1].dir: /srcdir
15762
15763 The line number information for list0.c has to end up in a single
15764 subfile, so that `break /srcdir/list0.c:1' works as expected.
15765 start_subfile will ensure that this happens provided that we pass the
15766 concatenation of files.files[1].dir and files.files[1].name as the
15767 subfile's name. */
15768
15769 static void
15770 dwarf2_start_subfile (char *filename, const char *dirname,
15771 const char *comp_dir)
15772 {
15773 char *fullname;
15774
15775 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15776 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15777 second argument to start_subfile. To be consistent, we do the
15778 same here. In order not to lose the line information directory,
15779 we concatenate it to the filename when it makes sense.
15780 Note that the Dwarf3 standard says (speaking of filenames in line
15781 information): ``The directory index is ignored for file names
15782 that represent full path names''. Thus ignoring dirname in the
15783 `else' branch below isn't an issue. */
15784
15785 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15786 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15787 else
15788 fullname = filename;
15789
15790 start_subfile (fullname, comp_dir);
15791
15792 if (fullname != filename)
15793 xfree (fullname);
15794 }
15795
15796 /* Start a symtab for DWARF.
15797 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15798
15799 static void
15800 dwarf2_start_symtab (struct dwarf2_cu *cu,
15801 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15802 {
15803 start_symtab (name, comp_dir, low_pc);
15804 record_debugformat ("DWARF 2");
15805 record_producer (cu->producer);
15806
15807 /* We assume that we're processing GCC output. */
15808 processing_gcc_compilation = 2;
15809
15810 cu->processing_has_namespace_info = 0;
15811 }
15812
15813 static void
15814 var_decode_location (struct attribute *attr, struct symbol *sym,
15815 struct dwarf2_cu *cu)
15816 {
15817 struct objfile *objfile = cu->objfile;
15818 struct comp_unit_head *cu_header = &cu->header;
15819
15820 /* NOTE drow/2003-01-30: There used to be a comment and some special
15821 code here to turn a symbol with DW_AT_external and a
15822 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15823 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15824 with some versions of binutils) where shared libraries could have
15825 relocations against symbols in their debug information - the
15826 minimal symbol would have the right address, but the debug info
15827 would not. It's no longer necessary, because we will explicitly
15828 apply relocations when we read in the debug information now. */
15829
15830 /* A DW_AT_location attribute with no contents indicates that a
15831 variable has been optimized away. */
15832 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15833 {
15834 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15835 return;
15836 }
15837
15838 /* Handle one degenerate form of location expression specially, to
15839 preserve GDB's previous behavior when section offsets are
15840 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15841 then mark this symbol as LOC_STATIC. */
15842
15843 if (attr_form_is_block (attr)
15844 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15845 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15846 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15847 && (DW_BLOCK (attr)->size
15848 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15849 {
15850 unsigned int dummy;
15851
15852 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15853 SYMBOL_VALUE_ADDRESS (sym) =
15854 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15855 else
15856 SYMBOL_VALUE_ADDRESS (sym) =
15857 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15858 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15859 fixup_symbol_section (sym, objfile);
15860 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15861 SYMBOL_SECTION (sym));
15862 return;
15863 }
15864
15865 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15866 expression evaluator, and use LOC_COMPUTED only when necessary
15867 (i.e. when the value of a register or memory location is
15868 referenced, or a thread-local block, etc.). Then again, it might
15869 not be worthwhile. I'm assuming that it isn't unless performance
15870 or memory numbers show me otherwise. */
15871
15872 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15873
15874 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15875 cu->has_loclist = 1;
15876 }
15877
15878 /* Given a pointer to a DWARF information entry, figure out if we need
15879 to make a symbol table entry for it, and if so, create a new entry
15880 and return a pointer to it.
15881 If TYPE is NULL, determine symbol type from the die, otherwise
15882 used the passed type.
15883 If SPACE is not NULL, use it to hold the new symbol. If it is
15884 NULL, allocate a new symbol on the objfile's obstack. */
15885
15886 static struct symbol *
15887 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15888 struct symbol *space)
15889 {
15890 struct objfile *objfile = cu->objfile;
15891 struct symbol *sym = NULL;
15892 const char *name;
15893 struct attribute *attr = NULL;
15894 struct attribute *attr2 = NULL;
15895 CORE_ADDR baseaddr;
15896 struct pending **list_to_add = NULL;
15897
15898 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15899
15900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15901
15902 name = dwarf2_name (die, cu);
15903 if (name)
15904 {
15905 const char *linkagename;
15906 int suppress_add = 0;
15907
15908 if (space)
15909 sym = space;
15910 else
15911 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15912 OBJSTAT (objfile, n_syms++);
15913
15914 /* Cache this symbol's name and the name's demangled form (if any). */
15915 SYMBOL_SET_LANGUAGE (sym, cu->language);
15916 linkagename = dwarf2_physname (name, die, cu);
15917 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15918
15919 /* Fortran does not have mangling standard and the mangling does differ
15920 between gfortran, iFort etc. */
15921 if (cu->language == language_fortran
15922 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15923 symbol_set_demangled_name (&(sym->ginfo),
15924 dwarf2_full_name (name, die, cu),
15925 NULL);
15926
15927 /* Default assumptions.
15928 Use the passed type or decode it from the die. */
15929 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15930 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15931 if (type != NULL)
15932 SYMBOL_TYPE (sym) = type;
15933 else
15934 SYMBOL_TYPE (sym) = die_type (die, cu);
15935 attr = dwarf2_attr (die,
15936 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15937 cu);
15938 if (attr)
15939 {
15940 SYMBOL_LINE (sym) = DW_UNSND (attr);
15941 }
15942
15943 attr = dwarf2_attr (die,
15944 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15945 cu);
15946 if (attr)
15947 {
15948 int file_index = DW_UNSND (attr);
15949
15950 if (cu->line_header == NULL
15951 || file_index > cu->line_header->num_file_names)
15952 complaint (&symfile_complaints,
15953 _("file index out of range"));
15954 else if (file_index > 0)
15955 {
15956 struct file_entry *fe;
15957
15958 fe = &cu->line_header->file_names[file_index - 1];
15959 SYMBOL_SYMTAB (sym) = fe->symtab;
15960 }
15961 }
15962
15963 switch (die->tag)
15964 {
15965 case DW_TAG_label:
15966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15967 if (attr)
15968 {
15969 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15970 }
15971 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15972 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15973 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15974 add_symbol_to_list (sym, cu->list_in_scope);
15975 break;
15976 case DW_TAG_subprogram:
15977 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15978 finish_block. */
15979 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15981 if ((attr2 && (DW_UNSND (attr2) != 0))
15982 || cu->language == language_ada)
15983 {
15984 /* Subprograms marked external are stored as a global symbol.
15985 Ada subprograms, whether marked external or not, are always
15986 stored as a global symbol, because we want to be able to
15987 access them globally. For instance, we want to be able
15988 to break on a nested subprogram without having to
15989 specify the context. */
15990 list_to_add = &global_symbols;
15991 }
15992 else
15993 {
15994 list_to_add = cu->list_in_scope;
15995 }
15996 break;
15997 case DW_TAG_inlined_subroutine:
15998 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15999 finish_block. */
16000 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16001 SYMBOL_INLINED (sym) = 1;
16002 list_to_add = cu->list_in_scope;
16003 break;
16004 case DW_TAG_template_value_param:
16005 suppress_add = 1;
16006 /* Fall through. */
16007 case DW_TAG_constant:
16008 case DW_TAG_variable:
16009 case DW_TAG_member:
16010 /* Compilation with minimal debug info may result in
16011 variables with missing type entries. Change the
16012 misleading `void' type to something sensible. */
16013 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16014 SYMBOL_TYPE (sym)
16015 = objfile_type (objfile)->nodebug_data_symbol;
16016
16017 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16018 /* In the case of DW_TAG_member, we should only be called for
16019 static const members. */
16020 if (die->tag == DW_TAG_member)
16021 {
16022 /* dwarf2_add_field uses die_is_declaration,
16023 so we do the same. */
16024 gdb_assert (die_is_declaration (die, cu));
16025 gdb_assert (attr);
16026 }
16027 if (attr)
16028 {
16029 dwarf2_const_value (attr, sym, cu);
16030 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16031 if (!suppress_add)
16032 {
16033 if (attr2 && (DW_UNSND (attr2) != 0))
16034 list_to_add = &global_symbols;
16035 else
16036 list_to_add = cu->list_in_scope;
16037 }
16038 break;
16039 }
16040 attr = dwarf2_attr (die, DW_AT_location, cu);
16041 if (attr)
16042 {
16043 var_decode_location (attr, sym, cu);
16044 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16045
16046 /* Fortran explicitly imports any global symbols to the local
16047 scope by DW_TAG_common_block. */
16048 if (cu->language == language_fortran && die->parent
16049 && die->parent->tag == DW_TAG_common_block)
16050 attr2 = NULL;
16051
16052 if (SYMBOL_CLASS (sym) == LOC_STATIC
16053 && SYMBOL_VALUE_ADDRESS (sym) == 0
16054 && !dwarf2_per_objfile->has_section_at_zero)
16055 {
16056 /* When a static variable is eliminated by the linker,
16057 the corresponding debug information is not stripped
16058 out, but the variable address is set to null;
16059 do not add such variables into symbol table. */
16060 }
16061 else if (attr2 && (DW_UNSND (attr2) != 0))
16062 {
16063 /* Workaround gfortran PR debug/40040 - it uses
16064 DW_AT_location for variables in -fPIC libraries which may
16065 get overriden by other libraries/executable and get
16066 a different address. Resolve it by the minimal symbol
16067 which may come from inferior's executable using copy
16068 relocation. Make this workaround only for gfortran as for
16069 other compilers GDB cannot guess the minimal symbol
16070 Fortran mangling kind. */
16071 if (cu->language == language_fortran && die->parent
16072 && die->parent->tag == DW_TAG_module
16073 && cu->producer
16074 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16075 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16076
16077 /* A variable with DW_AT_external is never static,
16078 but it may be block-scoped. */
16079 list_to_add = (cu->list_in_scope == &file_symbols
16080 ? &global_symbols : cu->list_in_scope);
16081 }
16082 else
16083 list_to_add = cu->list_in_scope;
16084 }
16085 else
16086 {
16087 /* We do not know the address of this symbol.
16088 If it is an external symbol and we have type information
16089 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16090 The address of the variable will then be determined from
16091 the minimal symbol table whenever the variable is
16092 referenced. */
16093 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16094
16095 /* Fortran explicitly imports any global symbols to the local
16096 scope by DW_TAG_common_block. */
16097 if (cu->language == language_fortran && die->parent
16098 && die->parent->tag == DW_TAG_common_block)
16099 {
16100 /* SYMBOL_CLASS doesn't matter here because
16101 read_common_block is going to reset it. */
16102 if (!suppress_add)
16103 list_to_add = cu->list_in_scope;
16104 }
16105 else if (attr2 && (DW_UNSND (attr2) != 0)
16106 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16107 {
16108 /* A variable with DW_AT_external is never static, but it
16109 may be block-scoped. */
16110 list_to_add = (cu->list_in_scope == &file_symbols
16111 ? &global_symbols : cu->list_in_scope);
16112
16113 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16114 }
16115 else if (!die_is_declaration (die, cu))
16116 {
16117 /* Use the default LOC_OPTIMIZED_OUT class. */
16118 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16119 if (!suppress_add)
16120 list_to_add = cu->list_in_scope;
16121 }
16122 }
16123 break;
16124 case DW_TAG_formal_parameter:
16125 /* If we are inside a function, mark this as an argument. If
16126 not, we might be looking at an argument to an inlined function
16127 when we do not have enough information to show inlined frames;
16128 pretend it's a local variable in that case so that the user can
16129 still see it. */
16130 if (context_stack_depth > 0
16131 && context_stack[context_stack_depth - 1].name != NULL)
16132 SYMBOL_IS_ARGUMENT (sym) = 1;
16133 attr = dwarf2_attr (die, DW_AT_location, cu);
16134 if (attr)
16135 {
16136 var_decode_location (attr, sym, cu);
16137 }
16138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16139 if (attr)
16140 {
16141 dwarf2_const_value (attr, sym, cu);
16142 }
16143
16144 list_to_add = cu->list_in_scope;
16145 break;
16146 case DW_TAG_unspecified_parameters:
16147 /* From varargs functions; gdb doesn't seem to have any
16148 interest in this information, so just ignore it for now.
16149 (FIXME?) */
16150 break;
16151 case DW_TAG_template_type_param:
16152 suppress_add = 1;
16153 /* Fall through. */
16154 case DW_TAG_class_type:
16155 case DW_TAG_interface_type:
16156 case DW_TAG_structure_type:
16157 case DW_TAG_union_type:
16158 case DW_TAG_set_type:
16159 case DW_TAG_enumeration_type:
16160 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16161 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16162
16163 {
16164 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16165 really ever be static objects: otherwise, if you try
16166 to, say, break of a class's method and you're in a file
16167 which doesn't mention that class, it won't work unless
16168 the check for all static symbols in lookup_symbol_aux
16169 saves you. See the OtherFileClass tests in
16170 gdb.c++/namespace.exp. */
16171
16172 if (!suppress_add)
16173 {
16174 list_to_add = (cu->list_in_scope == &file_symbols
16175 && (cu->language == language_cplus
16176 || cu->language == language_java)
16177 ? &global_symbols : cu->list_in_scope);
16178
16179 /* The semantics of C++ state that "struct foo {
16180 ... }" also defines a typedef for "foo". A Java
16181 class declaration also defines a typedef for the
16182 class. */
16183 if (cu->language == language_cplus
16184 || cu->language == language_java
16185 || cu->language == language_ada)
16186 {
16187 /* The symbol's name is already allocated along
16188 with this objfile, so we don't need to
16189 duplicate it for the type. */
16190 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16191 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16192 }
16193 }
16194 }
16195 break;
16196 case DW_TAG_typedef:
16197 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16198 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16199 list_to_add = cu->list_in_scope;
16200 break;
16201 case DW_TAG_base_type:
16202 case DW_TAG_subrange_type:
16203 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16204 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16205 list_to_add = cu->list_in_scope;
16206 break;
16207 case DW_TAG_enumerator:
16208 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16209 if (attr)
16210 {
16211 dwarf2_const_value (attr, sym, cu);
16212 }
16213 {
16214 /* NOTE: carlton/2003-11-10: See comment above in the
16215 DW_TAG_class_type, etc. block. */
16216
16217 list_to_add = (cu->list_in_scope == &file_symbols
16218 && (cu->language == language_cplus
16219 || cu->language == language_java)
16220 ? &global_symbols : cu->list_in_scope);
16221 }
16222 break;
16223 case DW_TAG_namespace:
16224 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16225 list_to_add = &global_symbols;
16226 break;
16227 case DW_TAG_common_block:
16228 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16229 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16230 add_symbol_to_list (sym, cu->list_in_scope);
16231 break;
16232 default:
16233 /* Not a tag we recognize. Hopefully we aren't processing
16234 trash data, but since we must specifically ignore things
16235 we don't recognize, there is nothing else we should do at
16236 this point. */
16237 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16238 dwarf_tag_name (die->tag));
16239 break;
16240 }
16241
16242 if (suppress_add)
16243 {
16244 sym->hash_next = objfile->template_symbols;
16245 objfile->template_symbols = sym;
16246 list_to_add = NULL;
16247 }
16248
16249 if (list_to_add != NULL)
16250 add_symbol_to_list (sym, list_to_add);
16251
16252 /* For the benefit of old versions of GCC, check for anonymous
16253 namespaces based on the demangled name. */
16254 if (!cu->processing_has_namespace_info
16255 && cu->language == language_cplus)
16256 cp_scan_for_anonymous_namespaces (sym, objfile);
16257 }
16258 return (sym);
16259 }
16260
16261 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16262
16263 static struct symbol *
16264 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16265 {
16266 return new_symbol_full (die, type, cu, NULL);
16267 }
16268
16269 /* Given an attr with a DW_FORM_dataN value in host byte order,
16270 zero-extend it as appropriate for the symbol's type. The DWARF
16271 standard (v4) is not entirely clear about the meaning of using
16272 DW_FORM_dataN for a constant with a signed type, where the type is
16273 wider than the data. The conclusion of a discussion on the DWARF
16274 list was that this is unspecified. We choose to always zero-extend
16275 because that is the interpretation long in use by GCC. */
16276
16277 static gdb_byte *
16278 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16279 const char *name, struct obstack *obstack,
16280 struct dwarf2_cu *cu, LONGEST *value, int bits)
16281 {
16282 struct objfile *objfile = cu->objfile;
16283 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16284 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16285 LONGEST l = DW_UNSND (attr);
16286
16287 if (bits < sizeof (*value) * 8)
16288 {
16289 l &= ((LONGEST) 1 << bits) - 1;
16290 *value = l;
16291 }
16292 else if (bits == sizeof (*value) * 8)
16293 *value = l;
16294 else
16295 {
16296 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16297 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16298 return bytes;
16299 }
16300
16301 return NULL;
16302 }
16303
16304 /* Read a constant value from an attribute. Either set *VALUE, or if
16305 the value does not fit in *VALUE, set *BYTES - either already
16306 allocated on the objfile obstack, or newly allocated on OBSTACK,
16307 or, set *BATON, if we translated the constant to a location
16308 expression. */
16309
16310 static void
16311 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16312 const char *name, struct obstack *obstack,
16313 struct dwarf2_cu *cu,
16314 LONGEST *value, gdb_byte **bytes,
16315 struct dwarf2_locexpr_baton **baton)
16316 {
16317 struct objfile *objfile = cu->objfile;
16318 struct comp_unit_head *cu_header = &cu->header;
16319 struct dwarf_block *blk;
16320 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16321 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16322
16323 *value = 0;
16324 *bytes = NULL;
16325 *baton = NULL;
16326
16327 switch (attr->form)
16328 {
16329 case DW_FORM_addr:
16330 case DW_FORM_GNU_addr_index:
16331 {
16332 gdb_byte *data;
16333
16334 if (TYPE_LENGTH (type) != cu_header->addr_size)
16335 dwarf2_const_value_length_mismatch_complaint (name,
16336 cu_header->addr_size,
16337 TYPE_LENGTH (type));
16338 /* Symbols of this form are reasonably rare, so we just
16339 piggyback on the existing location code rather than writing
16340 a new implementation of symbol_computed_ops. */
16341 *baton = obstack_alloc (&objfile->objfile_obstack,
16342 sizeof (struct dwarf2_locexpr_baton));
16343 (*baton)->per_cu = cu->per_cu;
16344 gdb_assert ((*baton)->per_cu);
16345
16346 (*baton)->size = 2 + cu_header->addr_size;
16347 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16348 (*baton)->data = data;
16349
16350 data[0] = DW_OP_addr;
16351 store_unsigned_integer (&data[1], cu_header->addr_size,
16352 byte_order, DW_ADDR (attr));
16353 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16354 }
16355 break;
16356 case DW_FORM_string:
16357 case DW_FORM_strp:
16358 case DW_FORM_GNU_str_index:
16359 case DW_FORM_GNU_strp_alt:
16360 /* DW_STRING is already allocated on the objfile obstack, point
16361 directly to it. */
16362 *bytes = (gdb_byte *) DW_STRING (attr);
16363 break;
16364 case DW_FORM_block1:
16365 case DW_FORM_block2:
16366 case DW_FORM_block4:
16367 case DW_FORM_block:
16368 case DW_FORM_exprloc:
16369 blk = DW_BLOCK (attr);
16370 if (TYPE_LENGTH (type) != blk->size)
16371 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16372 TYPE_LENGTH (type));
16373 *bytes = blk->data;
16374 break;
16375
16376 /* The DW_AT_const_value attributes are supposed to carry the
16377 symbol's value "represented as it would be on the target
16378 architecture." By the time we get here, it's already been
16379 converted to host endianness, so we just need to sign- or
16380 zero-extend it as appropriate. */
16381 case DW_FORM_data1:
16382 *bytes = dwarf2_const_value_data (attr, type, name,
16383 obstack, cu, value, 8);
16384 break;
16385 case DW_FORM_data2:
16386 *bytes = dwarf2_const_value_data (attr, type, name,
16387 obstack, cu, value, 16);
16388 break;
16389 case DW_FORM_data4:
16390 *bytes = dwarf2_const_value_data (attr, type, name,
16391 obstack, cu, value, 32);
16392 break;
16393 case DW_FORM_data8:
16394 *bytes = dwarf2_const_value_data (attr, type, name,
16395 obstack, cu, value, 64);
16396 break;
16397
16398 case DW_FORM_sdata:
16399 *value = DW_SND (attr);
16400 break;
16401
16402 case DW_FORM_udata:
16403 *value = DW_UNSND (attr);
16404 break;
16405
16406 default:
16407 complaint (&symfile_complaints,
16408 _("unsupported const value attribute form: '%s'"),
16409 dwarf_form_name (attr->form));
16410 *value = 0;
16411 break;
16412 }
16413 }
16414
16415
16416 /* Copy constant value from an attribute to a symbol. */
16417
16418 static void
16419 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16420 struct dwarf2_cu *cu)
16421 {
16422 struct objfile *objfile = cu->objfile;
16423 struct comp_unit_head *cu_header = &cu->header;
16424 LONGEST value;
16425 gdb_byte *bytes;
16426 struct dwarf2_locexpr_baton *baton;
16427
16428 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16429 SYMBOL_PRINT_NAME (sym),
16430 &objfile->objfile_obstack, cu,
16431 &value, &bytes, &baton);
16432
16433 if (baton != NULL)
16434 {
16435 SYMBOL_LOCATION_BATON (sym) = baton;
16436 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16437 }
16438 else if (bytes != NULL)
16439 {
16440 SYMBOL_VALUE_BYTES (sym) = bytes;
16441 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16442 }
16443 else
16444 {
16445 SYMBOL_VALUE (sym) = value;
16446 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16447 }
16448 }
16449
16450 /* Return the type of the die in question using its DW_AT_type attribute. */
16451
16452 static struct type *
16453 die_type (struct die_info *die, struct dwarf2_cu *cu)
16454 {
16455 struct attribute *type_attr;
16456
16457 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16458 if (!type_attr)
16459 {
16460 /* A missing DW_AT_type represents a void type. */
16461 return objfile_type (cu->objfile)->builtin_void;
16462 }
16463
16464 return lookup_die_type (die, type_attr, cu);
16465 }
16466
16467 /* True iff CU's producer generates GNAT Ada auxiliary information
16468 that allows to find parallel types through that information instead
16469 of having to do expensive parallel lookups by type name. */
16470
16471 static int
16472 need_gnat_info (struct dwarf2_cu *cu)
16473 {
16474 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16475 of GNAT produces this auxiliary information, without any indication
16476 that it is produced. Part of enhancing the FSF version of GNAT
16477 to produce that information will be to put in place an indicator
16478 that we can use in order to determine whether the descriptive type
16479 info is available or not. One suggestion that has been made is
16480 to use a new attribute, attached to the CU die. For now, assume
16481 that the descriptive type info is not available. */
16482 return 0;
16483 }
16484
16485 /* Return the auxiliary type of the die in question using its
16486 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16487 attribute is not present. */
16488
16489 static struct type *
16490 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16491 {
16492 struct attribute *type_attr;
16493
16494 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16495 if (!type_attr)
16496 return NULL;
16497
16498 return lookup_die_type (die, type_attr, cu);
16499 }
16500
16501 /* If DIE has a descriptive_type attribute, then set the TYPE's
16502 descriptive type accordingly. */
16503
16504 static void
16505 set_descriptive_type (struct type *type, struct die_info *die,
16506 struct dwarf2_cu *cu)
16507 {
16508 struct type *descriptive_type = die_descriptive_type (die, cu);
16509
16510 if (descriptive_type)
16511 {
16512 ALLOCATE_GNAT_AUX_TYPE (type);
16513 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16514 }
16515 }
16516
16517 /* Return the containing type of the die in question using its
16518 DW_AT_containing_type attribute. */
16519
16520 static struct type *
16521 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16522 {
16523 struct attribute *type_attr;
16524
16525 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16526 if (!type_attr)
16527 error (_("Dwarf Error: Problem turning containing type into gdb type "
16528 "[in module %s]"), cu->objfile->name);
16529
16530 return lookup_die_type (die, type_attr, cu);
16531 }
16532
16533 /* Look up the type of DIE in CU using its type attribute ATTR.
16534 If there is no type substitute an error marker. */
16535
16536 static struct type *
16537 lookup_die_type (struct die_info *die, struct attribute *attr,
16538 struct dwarf2_cu *cu)
16539 {
16540 struct objfile *objfile = cu->objfile;
16541 struct type *this_type;
16542
16543 /* First see if we have it cached. */
16544
16545 if (attr->form == DW_FORM_GNU_ref_alt)
16546 {
16547 struct dwarf2_per_cu_data *per_cu;
16548 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16549
16550 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16551 this_type = get_die_type_at_offset (offset, per_cu);
16552 }
16553 else if (is_ref_attr (attr))
16554 {
16555 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16556
16557 this_type = get_die_type_at_offset (offset, cu->per_cu);
16558 }
16559 else if (attr->form == DW_FORM_ref_sig8)
16560 {
16561 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16562
16563 /* sig_type will be NULL if the signatured type is missing from
16564 the debug info. */
16565 if (sig_type == NULL)
16566 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16567 "at 0x%x [in module %s]"),
16568 die->offset.sect_off, objfile->name);
16569
16570 gdb_assert (sig_type->per_cu.is_debug_types);
16571 /* If we haven't filled in type_offset_in_section yet, then we
16572 haven't read the type in yet. */
16573 this_type = NULL;
16574 if (sig_type->type_offset_in_section.sect_off != 0)
16575 {
16576 this_type =
16577 get_die_type_at_offset (sig_type->type_offset_in_section,
16578 &sig_type->per_cu);
16579 }
16580 }
16581 else
16582 {
16583 dump_die_for_error (die);
16584 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16585 dwarf_attr_name (attr->name), objfile->name);
16586 }
16587
16588 /* If not cached we need to read it in. */
16589
16590 if (this_type == NULL)
16591 {
16592 struct die_info *type_die;
16593 struct dwarf2_cu *type_cu = cu;
16594
16595 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16596 /* If we found the type now, it's probably because the type came
16597 from an inter-CU reference and the type's CU got expanded before
16598 ours. */
16599 this_type = get_die_type (type_die, type_cu);
16600 if (this_type == NULL)
16601 this_type = read_type_die_1 (type_die, type_cu);
16602 }
16603
16604 /* If we still don't have a type use an error marker. */
16605
16606 if (this_type == NULL)
16607 {
16608 char *message, *saved;
16609
16610 /* read_type_die already issued a complaint. */
16611 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16612 objfile->name,
16613 cu->header.offset.sect_off,
16614 die->offset.sect_off);
16615 saved = obstack_copy0 (&objfile->objfile_obstack,
16616 message, strlen (message));
16617 xfree (message);
16618
16619 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16620 }
16621
16622 return this_type;
16623 }
16624
16625 /* Return the type in DIE, CU.
16626 Returns NULL for invalid types.
16627
16628 This first does a lookup in the appropriate type_hash table,
16629 and only reads the die in if necessary.
16630
16631 NOTE: This can be called when reading in partial or full symbols. */
16632
16633 static struct type *
16634 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct type *this_type;
16637
16638 this_type = get_die_type (die, cu);
16639 if (this_type)
16640 return this_type;
16641
16642 return read_type_die_1 (die, cu);
16643 }
16644
16645 /* Read the type in DIE, CU.
16646 Returns NULL for invalid types. */
16647
16648 static struct type *
16649 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16650 {
16651 struct type *this_type = NULL;
16652
16653 switch (die->tag)
16654 {
16655 case DW_TAG_class_type:
16656 case DW_TAG_interface_type:
16657 case DW_TAG_structure_type:
16658 case DW_TAG_union_type:
16659 this_type = read_structure_type (die, cu);
16660 break;
16661 case DW_TAG_enumeration_type:
16662 this_type = read_enumeration_type (die, cu);
16663 break;
16664 case DW_TAG_subprogram:
16665 case DW_TAG_subroutine_type:
16666 case DW_TAG_inlined_subroutine:
16667 this_type = read_subroutine_type (die, cu);
16668 break;
16669 case DW_TAG_array_type:
16670 this_type = read_array_type (die, cu);
16671 break;
16672 case DW_TAG_set_type:
16673 this_type = read_set_type (die, cu);
16674 break;
16675 case DW_TAG_pointer_type:
16676 this_type = read_tag_pointer_type (die, cu);
16677 break;
16678 case DW_TAG_ptr_to_member_type:
16679 this_type = read_tag_ptr_to_member_type (die, cu);
16680 break;
16681 case DW_TAG_reference_type:
16682 this_type = read_tag_reference_type (die, cu);
16683 break;
16684 case DW_TAG_const_type:
16685 this_type = read_tag_const_type (die, cu);
16686 break;
16687 case DW_TAG_volatile_type:
16688 this_type = read_tag_volatile_type (die, cu);
16689 break;
16690 case DW_TAG_restrict_type:
16691 this_type = read_tag_restrict_type (die, cu);
16692 break;
16693 case DW_TAG_string_type:
16694 this_type = read_tag_string_type (die, cu);
16695 break;
16696 case DW_TAG_typedef:
16697 this_type = read_typedef (die, cu);
16698 break;
16699 case DW_TAG_subrange_type:
16700 this_type = read_subrange_type (die, cu);
16701 break;
16702 case DW_TAG_base_type:
16703 this_type = read_base_type (die, cu);
16704 break;
16705 case DW_TAG_unspecified_type:
16706 this_type = read_unspecified_type (die, cu);
16707 break;
16708 case DW_TAG_namespace:
16709 this_type = read_namespace_type (die, cu);
16710 break;
16711 case DW_TAG_module:
16712 this_type = read_module_type (die, cu);
16713 break;
16714 default:
16715 complaint (&symfile_complaints,
16716 _("unexpected tag in read_type_die: '%s'"),
16717 dwarf_tag_name (die->tag));
16718 break;
16719 }
16720
16721 return this_type;
16722 }
16723
16724 /* See if we can figure out if the class lives in a namespace. We do
16725 this by looking for a member function; its demangled name will
16726 contain namespace info, if there is any.
16727 Return the computed name or NULL.
16728 Space for the result is allocated on the objfile's obstack.
16729 This is the full-die version of guess_partial_die_structure_name.
16730 In this case we know DIE has no useful parent. */
16731
16732 static char *
16733 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16734 {
16735 struct die_info *spec_die;
16736 struct dwarf2_cu *spec_cu;
16737 struct die_info *child;
16738
16739 spec_cu = cu;
16740 spec_die = die_specification (die, &spec_cu);
16741 if (spec_die != NULL)
16742 {
16743 die = spec_die;
16744 cu = spec_cu;
16745 }
16746
16747 for (child = die->child;
16748 child != NULL;
16749 child = child->sibling)
16750 {
16751 if (child->tag == DW_TAG_subprogram)
16752 {
16753 struct attribute *attr;
16754
16755 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16756 if (attr == NULL)
16757 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16758 if (attr != NULL)
16759 {
16760 char *actual_name
16761 = language_class_name_from_physname (cu->language_defn,
16762 DW_STRING (attr));
16763 char *name = NULL;
16764
16765 if (actual_name != NULL)
16766 {
16767 const char *die_name = dwarf2_name (die, cu);
16768
16769 if (die_name != NULL
16770 && strcmp (die_name, actual_name) != 0)
16771 {
16772 /* Strip off the class name from the full name.
16773 We want the prefix. */
16774 int die_name_len = strlen (die_name);
16775 int actual_name_len = strlen (actual_name);
16776
16777 /* Test for '::' as a sanity check. */
16778 if (actual_name_len > die_name_len + 2
16779 && actual_name[actual_name_len
16780 - die_name_len - 1] == ':')
16781 name =
16782 obstack_copy0 (&cu->objfile->objfile_obstack,
16783 actual_name,
16784 actual_name_len - die_name_len - 2);
16785 }
16786 }
16787 xfree (actual_name);
16788 return name;
16789 }
16790 }
16791 }
16792
16793 return NULL;
16794 }
16795
16796 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16797 prefix part in such case. See
16798 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16799
16800 static char *
16801 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16802 {
16803 struct attribute *attr;
16804 char *base;
16805
16806 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16807 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16808 return NULL;
16809
16810 attr = dwarf2_attr (die, DW_AT_name, cu);
16811 if (attr != NULL && DW_STRING (attr) != NULL)
16812 return NULL;
16813
16814 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16815 if (attr == NULL)
16816 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16817 if (attr == NULL || DW_STRING (attr) == NULL)
16818 return NULL;
16819
16820 /* dwarf2_name had to be already called. */
16821 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16822
16823 /* Strip the base name, keep any leading namespaces/classes. */
16824 base = strrchr (DW_STRING (attr), ':');
16825 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16826 return "";
16827
16828 return obstack_copy0 (&cu->objfile->objfile_obstack,
16829 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16830 }
16831
16832 /* Return the name of the namespace/class that DIE is defined within,
16833 or "" if we can't tell. The caller should not xfree the result.
16834
16835 For example, if we're within the method foo() in the following
16836 code:
16837
16838 namespace N {
16839 class C {
16840 void foo () {
16841 }
16842 };
16843 }
16844
16845 then determine_prefix on foo's die will return "N::C". */
16846
16847 static const char *
16848 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16849 {
16850 struct die_info *parent, *spec_die;
16851 struct dwarf2_cu *spec_cu;
16852 struct type *parent_type;
16853 char *retval;
16854
16855 if (cu->language != language_cplus && cu->language != language_java
16856 && cu->language != language_fortran)
16857 return "";
16858
16859 retval = anonymous_struct_prefix (die, cu);
16860 if (retval)
16861 return retval;
16862
16863 /* We have to be careful in the presence of DW_AT_specification.
16864 For example, with GCC 3.4, given the code
16865
16866 namespace N {
16867 void foo() {
16868 // Definition of N::foo.
16869 }
16870 }
16871
16872 then we'll have a tree of DIEs like this:
16873
16874 1: DW_TAG_compile_unit
16875 2: DW_TAG_namespace // N
16876 3: DW_TAG_subprogram // declaration of N::foo
16877 4: DW_TAG_subprogram // definition of N::foo
16878 DW_AT_specification // refers to die #3
16879
16880 Thus, when processing die #4, we have to pretend that we're in
16881 the context of its DW_AT_specification, namely the contex of die
16882 #3. */
16883 spec_cu = cu;
16884 spec_die = die_specification (die, &spec_cu);
16885 if (spec_die == NULL)
16886 parent = die->parent;
16887 else
16888 {
16889 parent = spec_die->parent;
16890 cu = spec_cu;
16891 }
16892
16893 if (parent == NULL)
16894 return "";
16895 else if (parent->building_fullname)
16896 {
16897 const char *name;
16898 const char *parent_name;
16899
16900 /* It has been seen on RealView 2.2 built binaries,
16901 DW_TAG_template_type_param types actually _defined_ as
16902 children of the parent class:
16903
16904 enum E {};
16905 template class <class Enum> Class{};
16906 Class<enum E> class_e;
16907
16908 1: DW_TAG_class_type (Class)
16909 2: DW_TAG_enumeration_type (E)
16910 3: DW_TAG_enumerator (enum1:0)
16911 3: DW_TAG_enumerator (enum2:1)
16912 ...
16913 2: DW_TAG_template_type_param
16914 DW_AT_type DW_FORM_ref_udata (E)
16915
16916 Besides being broken debug info, it can put GDB into an
16917 infinite loop. Consider:
16918
16919 When we're building the full name for Class<E>, we'll start
16920 at Class, and go look over its template type parameters,
16921 finding E. We'll then try to build the full name of E, and
16922 reach here. We're now trying to build the full name of E,
16923 and look over the parent DIE for containing scope. In the
16924 broken case, if we followed the parent DIE of E, we'd again
16925 find Class, and once again go look at its template type
16926 arguments, etc., etc. Simply don't consider such parent die
16927 as source-level parent of this die (it can't be, the language
16928 doesn't allow it), and break the loop here. */
16929 name = dwarf2_name (die, cu);
16930 parent_name = dwarf2_name (parent, cu);
16931 complaint (&symfile_complaints,
16932 _("template param type '%s' defined within parent '%s'"),
16933 name ? name : "<unknown>",
16934 parent_name ? parent_name : "<unknown>");
16935 return "";
16936 }
16937 else
16938 switch (parent->tag)
16939 {
16940 case DW_TAG_namespace:
16941 parent_type = read_type_die (parent, cu);
16942 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16943 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16944 Work around this problem here. */
16945 if (cu->language == language_cplus
16946 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16947 return "";
16948 /* We give a name to even anonymous namespaces. */
16949 return TYPE_TAG_NAME (parent_type);
16950 case DW_TAG_class_type:
16951 case DW_TAG_interface_type:
16952 case DW_TAG_structure_type:
16953 case DW_TAG_union_type:
16954 case DW_TAG_module:
16955 parent_type = read_type_die (parent, cu);
16956 if (TYPE_TAG_NAME (parent_type) != NULL)
16957 return TYPE_TAG_NAME (parent_type);
16958 else
16959 /* An anonymous structure is only allowed non-static data
16960 members; no typedefs, no member functions, et cetera.
16961 So it does not need a prefix. */
16962 return "";
16963 case DW_TAG_compile_unit:
16964 case DW_TAG_partial_unit:
16965 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16966 if (cu->language == language_cplus
16967 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16968 && die->child != NULL
16969 && (die->tag == DW_TAG_class_type
16970 || die->tag == DW_TAG_structure_type
16971 || die->tag == DW_TAG_union_type))
16972 {
16973 char *name = guess_full_die_structure_name (die, cu);
16974 if (name != NULL)
16975 return name;
16976 }
16977 return "";
16978 default:
16979 return determine_prefix (parent, cu);
16980 }
16981 }
16982
16983 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16984 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16985 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16986 an obconcat, otherwise allocate storage for the result. The CU argument is
16987 used to determine the language and hence, the appropriate separator. */
16988
16989 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16990
16991 static char *
16992 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16993 int physname, struct dwarf2_cu *cu)
16994 {
16995 const char *lead = "";
16996 const char *sep;
16997
16998 if (suffix == NULL || suffix[0] == '\0'
16999 || prefix == NULL || prefix[0] == '\0')
17000 sep = "";
17001 else if (cu->language == language_java)
17002 sep = ".";
17003 else if (cu->language == language_fortran && physname)
17004 {
17005 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17006 DW_AT_MIPS_linkage_name is preferred and used instead. */
17007
17008 lead = "__";
17009 sep = "_MOD_";
17010 }
17011 else
17012 sep = "::";
17013
17014 if (prefix == NULL)
17015 prefix = "";
17016 if (suffix == NULL)
17017 suffix = "";
17018
17019 if (obs == NULL)
17020 {
17021 char *retval
17022 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17023
17024 strcpy (retval, lead);
17025 strcat (retval, prefix);
17026 strcat (retval, sep);
17027 strcat (retval, suffix);
17028 return retval;
17029 }
17030 else
17031 {
17032 /* We have an obstack. */
17033 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17034 }
17035 }
17036
17037 /* Return sibling of die, NULL if no sibling. */
17038
17039 static struct die_info *
17040 sibling_die (struct die_info *die)
17041 {
17042 return die->sibling;
17043 }
17044
17045 /* Get name of a die, return NULL if not found. */
17046
17047 static const char *
17048 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17049 struct obstack *obstack)
17050 {
17051 if (name && cu->language == language_cplus)
17052 {
17053 char *canon_name = cp_canonicalize_string (name);
17054
17055 if (canon_name != NULL)
17056 {
17057 if (strcmp (canon_name, name) != 0)
17058 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17059 xfree (canon_name);
17060 }
17061 }
17062
17063 return name;
17064 }
17065
17066 /* Get name of a die, return NULL if not found. */
17067
17068 static const char *
17069 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17070 {
17071 struct attribute *attr;
17072
17073 attr = dwarf2_attr (die, DW_AT_name, cu);
17074 if ((!attr || !DW_STRING (attr))
17075 && die->tag != DW_TAG_class_type
17076 && die->tag != DW_TAG_interface_type
17077 && die->tag != DW_TAG_structure_type
17078 && die->tag != DW_TAG_union_type)
17079 return NULL;
17080
17081 switch (die->tag)
17082 {
17083 case DW_TAG_compile_unit:
17084 case DW_TAG_partial_unit:
17085 /* Compilation units have a DW_AT_name that is a filename, not
17086 a source language identifier. */
17087 case DW_TAG_enumeration_type:
17088 case DW_TAG_enumerator:
17089 /* These tags always have simple identifiers already; no need
17090 to canonicalize them. */
17091 return DW_STRING (attr);
17092
17093 case DW_TAG_subprogram:
17094 /* Java constructors will all be named "<init>", so return
17095 the class name when we see this special case. */
17096 if (cu->language == language_java
17097 && DW_STRING (attr) != NULL
17098 && strcmp (DW_STRING (attr), "<init>") == 0)
17099 {
17100 struct dwarf2_cu *spec_cu = cu;
17101 struct die_info *spec_die;
17102
17103 /* GCJ will output '<init>' for Java constructor names.
17104 For this special case, return the name of the parent class. */
17105
17106 /* GCJ may output suprogram DIEs with AT_specification set.
17107 If so, use the name of the specified DIE. */
17108 spec_die = die_specification (die, &spec_cu);
17109 if (spec_die != NULL)
17110 return dwarf2_name (spec_die, spec_cu);
17111
17112 do
17113 {
17114 die = die->parent;
17115 if (die->tag == DW_TAG_class_type)
17116 return dwarf2_name (die, cu);
17117 }
17118 while (die->tag != DW_TAG_compile_unit
17119 && die->tag != DW_TAG_partial_unit);
17120 }
17121 break;
17122
17123 case DW_TAG_class_type:
17124 case DW_TAG_interface_type:
17125 case DW_TAG_structure_type:
17126 case DW_TAG_union_type:
17127 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17128 structures or unions. These were of the form "._%d" in GCC 4.1,
17129 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17130 and GCC 4.4. We work around this problem by ignoring these. */
17131 if (attr && DW_STRING (attr)
17132 && (strncmp (DW_STRING (attr), "._", 2) == 0
17133 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17134 return NULL;
17135
17136 /* GCC might emit a nameless typedef that has a linkage name. See
17137 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17138 if (!attr || DW_STRING (attr) == NULL)
17139 {
17140 char *demangled = NULL;
17141
17142 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17143 if (attr == NULL)
17144 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17145
17146 if (attr == NULL || DW_STRING (attr) == NULL)
17147 return NULL;
17148
17149 /* Avoid demangling DW_STRING (attr) the second time on a second
17150 call for the same DIE. */
17151 if (!DW_STRING_IS_CANONICAL (attr))
17152 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17153
17154 if (demangled)
17155 {
17156 char *base;
17157
17158 /* FIXME: we already did this for the partial symbol... */
17159 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17160 demangled, strlen (demangled));
17161 DW_STRING_IS_CANONICAL (attr) = 1;
17162 xfree (demangled);
17163
17164 /* Strip any leading namespaces/classes, keep only the base name.
17165 DW_AT_name for named DIEs does not contain the prefixes. */
17166 base = strrchr (DW_STRING (attr), ':');
17167 if (base && base > DW_STRING (attr) && base[-1] == ':')
17168 return &base[1];
17169 else
17170 return DW_STRING (attr);
17171 }
17172 }
17173 break;
17174
17175 default:
17176 break;
17177 }
17178
17179 if (!DW_STRING_IS_CANONICAL (attr))
17180 {
17181 DW_STRING (attr)
17182 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17183 &cu->objfile->objfile_obstack);
17184 DW_STRING_IS_CANONICAL (attr) = 1;
17185 }
17186 return DW_STRING (attr);
17187 }
17188
17189 /* Return the die that this die in an extension of, or NULL if there
17190 is none. *EXT_CU is the CU containing DIE on input, and the CU
17191 containing the return value on output. */
17192
17193 static struct die_info *
17194 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17195 {
17196 struct attribute *attr;
17197
17198 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17199 if (attr == NULL)
17200 return NULL;
17201
17202 return follow_die_ref (die, attr, ext_cu);
17203 }
17204
17205 /* Convert a DIE tag into its string name. */
17206
17207 static const char *
17208 dwarf_tag_name (unsigned tag)
17209 {
17210 const char *name = get_DW_TAG_name (tag);
17211
17212 if (name == NULL)
17213 return "DW_TAG_<unknown>";
17214
17215 return name;
17216 }
17217
17218 /* Convert a DWARF attribute code into its string name. */
17219
17220 static const char *
17221 dwarf_attr_name (unsigned attr)
17222 {
17223 const char *name;
17224
17225 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17226 if (attr == DW_AT_MIPS_fde)
17227 return "DW_AT_MIPS_fde";
17228 #else
17229 if (attr == DW_AT_HP_block_index)
17230 return "DW_AT_HP_block_index";
17231 #endif
17232
17233 name = get_DW_AT_name (attr);
17234
17235 if (name == NULL)
17236 return "DW_AT_<unknown>";
17237
17238 return name;
17239 }
17240
17241 /* Convert a DWARF value form code into its string name. */
17242
17243 static const char *
17244 dwarf_form_name (unsigned form)
17245 {
17246 const char *name = get_DW_FORM_name (form);
17247
17248 if (name == NULL)
17249 return "DW_FORM_<unknown>";
17250
17251 return name;
17252 }
17253
17254 static char *
17255 dwarf_bool_name (unsigned mybool)
17256 {
17257 if (mybool)
17258 return "TRUE";
17259 else
17260 return "FALSE";
17261 }
17262
17263 /* Convert a DWARF type code into its string name. */
17264
17265 static const char *
17266 dwarf_type_encoding_name (unsigned enc)
17267 {
17268 const char *name = get_DW_ATE_name (enc);
17269
17270 if (name == NULL)
17271 return "DW_ATE_<unknown>";
17272
17273 return name;
17274 }
17275
17276 static void
17277 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17278 {
17279 unsigned int i;
17280
17281 print_spaces (indent, f);
17282 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17283 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17284
17285 if (die->parent != NULL)
17286 {
17287 print_spaces (indent, f);
17288 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17289 die->parent->offset.sect_off);
17290 }
17291
17292 print_spaces (indent, f);
17293 fprintf_unfiltered (f, " has children: %s\n",
17294 dwarf_bool_name (die->child != NULL));
17295
17296 print_spaces (indent, f);
17297 fprintf_unfiltered (f, " attributes:\n");
17298
17299 for (i = 0; i < die->num_attrs; ++i)
17300 {
17301 print_spaces (indent, f);
17302 fprintf_unfiltered (f, " %s (%s) ",
17303 dwarf_attr_name (die->attrs[i].name),
17304 dwarf_form_name (die->attrs[i].form));
17305
17306 switch (die->attrs[i].form)
17307 {
17308 case DW_FORM_addr:
17309 case DW_FORM_GNU_addr_index:
17310 fprintf_unfiltered (f, "address: ");
17311 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17312 break;
17313 case DW_FORM_block2:
17314 case DW_FORM_block4:
17315 case DW_FORM_block:
17316 case DW_FORM_block1:
17317 fprintf_unfiltered (f, "block: size %s",
17318 pulongest (DW_BLOCK (&die->attrs[i])->size));
17319 break;
17320 case DW_FORM_exprloc:
17321 fprintf_unfiltered (f, "expression: size %s",
17322 pulongest (DW_BLOCK (&die->attrs[i])->size));
17323 break;
17324 case DW_FORM_ref_addr:
17325 fprintf_unfiltered (f, "ref address: ");
17326 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17327 break;
17328 case DW_FORM_GNU_ref_alt:
17329 fprintf_unfiltered (f, "alt ref address: ");
17330 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17331 break;
17332 case DW_FORM_ref1:
17333 case DW_FORM_ref2:
17334 case DW_FORM_ref4:
17335 case DW_FORM_ref8:
17336 case DW_FORM_ref_udata:
17337 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17338 (long) (DW_UNSND (&die->attrs[i])));
17339 break;
17340 case DW_FORM_data1:
17341 case DW_FORM_data2:
17342 case DW_FORM_data4:
17343 case DW_FORM_data8:
17344 case DW_FORM_udata:
17345 case DW_FORM_sdata:
17346 fprintf_unfiltered (f, "constant: %s",
17347 pulongest (DW_UNSND (&die->attrs[i])));
17348 break;
17349 case DW_FORM_sec_offset:
17350 fprintf_unfiltered (f, "section offset: %s",
17351 pulongest (DW_UNSND (&die->attrs[i])));
17352 break;
17353 case DW_FORM_ref_sig8:
17354 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17355 {
17356 struct signatured_type *sig_type =
17357 DW_SIGNATURED_TYPE (&die->attrs[i]);
17358
17359 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17360 hex_string (sig_type->signature),
17361 sig_type->per_cu.offset.sect_off);
17362 }
17363 else
17364 fprintf_unfiltered (f, "signatured type, unknown");
17365 break;
17366 case DW_FORM_string:
17367 case DW_FORM_strp:
17368 case DW_FORM_GNU_str_index:
17369 case DW_FORM_GNU_strp_alt:
17370 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17371 DW_STRING (&die->attrs[i])
17372 ? DW_STRING (&die->attrs[i]) : "",
17373 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17374 break;
17375 case DW_FORM_flag:
17376 if (DW_UNSND (&die->attrs[i]))
17377 fprintf_unfiltered (f, "flag: TRUE");
17378 else
17379 fprintf_unfiltered (f, "flag: FALSE");
17380 break;
17381 case DW_FORM_flag_present:
17382 fprintf_unfiltered (f, "flag: TRUE");
17383 break;
17384 case DW_FORM_indirect:
17385 /* The reader will have reduced the indirect form to
17386 the "base form" so this form should not occur. */
17387 fprintf_unfiltered (f,
17388 "unexpected attribute form: DW_FORM_indirect");
17389 break;
17390 default:
17391 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17392 die->attrs[i].form);
17393 break;
17394 }
17395 fprintf_unfiltered (f, "\n");
17396 }
17397 }
17398
17399 static void
17400 dump_die_for_error (struct die_info *die)
17401 {
17402 dump_die_shallow (gdb_stderr, 0, die);
17403 }
17404
17405 static void
17406 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17407 {
17408 int indent = level * 4;
17409
17410 gdb_assert (die != NULL);
17411
17412 if (level >= max_level)
17413 return;
17414
17415 dump_die_shallow (f, indent, die);
17416
17417 if (die->child != NULL)
17418 {
17419 print_spaces (indent, f);
17420 fprintf_unfiltered (f, " Children:");
17421 if (level + 1 < max_level)
17422 {
17423 fprintf_unfiltered (f, "\n");
17424 dump_die_1 (f, level + 1, max_level, die->child);
17425 }
17426 else
17427 {
17428 fprintf_unfiltered (f,
17429 " [not printed, max nesting level reached]\n");
17430 }
17431 }
17432
17433 if (die->sibling != NULL && level > 0)
17434 {
17435 dump_die_1 (f, level, max_level, die->sibling);
17436 }
17437 }
17438
17439 /* This is called from the pdie macro in gdbinit.in.
17440 It's not static so gcc will keep a copy callable from gdb. */
17441
17442 void
17443 dump_die (struct die_info *die, int max_level)
17444 {
17445 dump_die_1 (gdb_stdlog, 0, max_level, die);
17446 }
17447
17448 static void
17449 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17450 {
17451 void **slot;
17452
17453 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17454 INSERT);
17455
17456 *slot = die;
17457 }
17458
17459 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17460 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17461
17462 static int
17463 is_ref_attr (struct attribute *attr)
17464 {
17465 switch (attr->form)
17466 {
17467 case DW_FORM_ref_addr:
17468 case DW_FORM_ref1:
17469 case DW_FORM_ref2:
17470 case DW_FORM_ref4:
17471 case DW_FORM_ref8:
17472 case DW_FORM_ref_udata:
17473 case DW_FORM_GNU_ref_alt:
17474 return 1;
17475 default:
17476 return 0;
17477 }
17478 }
17479
17480 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17481 required kind. */
17482
17483 static sect_offset
17484 dwarf2_get_ref_die_offset (struct attribute *attr)
17485 {
17486 sect_offset retval = { DW_UNSND (attr) };
17487
17488 if (is_ref_attr (attr))
17489 return retval;
17490
17491 retval.sect_off = 0;
17492 complaint (&symfile_complaints,
17493 _("unsupported die ref attribute form: '%s'"),
17494 dwarf_form_name (attr->form));
17495 return retval;
17496 }
17497
17498 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17499 * the value held by the attribute is not constant. */
17500
17501 static LONGEST
17502 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17503 {
17504 if (attr->form == DW_FORM_sdata)
17505 return DW_SND (attr);
17506 else if (attr->form == DW_FORM_udata
17507 || attr->form == DW_FORM_data1
17508 || attr->form == DW_FORM_data2
17509 || attr->form == DW_FORM_data4
17510 || attr->form == DW_FORM_data8)
17511 return DW_UNSND (attr);
17512 else
17513 {
17514 complaint (&symfile_complaints,
17515 _("Attribute value is not a constant (%s)"),
17516 dwarf_form_name (attr->form));
17517 return default_value;
17518 }
17519 }
17520
17521 /* Follow reference or signature attribute ATTR of SRC_DIE.
17522 On entry *REF_CU is the CU of SRC_DIE.
17523 On exit *REF_CU is the CU of the result. */
17524
17525 static struct die_info *
17526 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17527 struct dwarf2_cu **ref_cu)
17528 {
17529 struct die_info *die;
17530
17531 if (is_ref_attr (attr))
17532 die = follow_die_ref (src_die, attr, ref_cu);
17533 else if (attr->form == DW_FORM_ref_sig8)
17534 die = follow_die_sig (src_die, attr, ref_cu);
17535 else
17536 {
17537 dump_die_for_error (src_die);
17538 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17539 (*ref_cu)->objfile->name);
17540 }
17541
17542 return die;
17543 }
17544
17545 /* Follow reference OFFSET.
17546 On entry *REF_CU is the CU of the source die referencing OFFSET.
17547 On exit *REF_CU is the CU of the result.
17548 Returns NULL if OFFSET is invalid. */
17549
17550 static struct die_info *
17551 follow_die_offset (sect_offset offset, int offset_in_dwz,
17552 struct dwarf2_cu **ref_cu)
17553 {
17554 struct die_info temp_die;
17555 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17556
17557 gdb_assert (cu->per_cu != NULL);
17558
17559 target_cu = cu;
17560
17561 if (cu->per_cu->is_debug_types)
17562 {
17563 /* .debug_types CUs cannot reference anything outside their CU.
17564 If they need to, they have to reference a signatured type via
17565 DW_FORM_ref_sig8. */
17566 if (! offset_in_cu_p (&cu->header, offset))
17567 return NULL;
17568 }
17569 else if (offset_in_dwz != cu->per_cu->is_dwz
17570 || ! offset_in_cu_p (&cu->header, offset))
17571 {
17572 struct dwarf2_per_cu_data *per_cu;
17573
17574 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17575 cu->objfile);
17576
17577 /* If necessary, add it to the queue and load its DIEs. */
17578 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17579 load_full_comp_unit (per_cu, cu->language);
17580
17581 target_cu = per_cu->cu;
17582 }
17583 else if (cu->dies == NULL)
17584 {
17585 /* We're loading full DIEs during partial symbol reading. */
17586 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17587 load_full_comp_unit (cu->per_cu, language_minimal);
17588 }
17589
17590 *ref_cu = target_cu;
17591 temp_die.offset = offset;
17592 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17593 }
17594
17595 /* Follow reference attribute ATTR of SRC_DIE.
17596 On entry *REF_CU is the CU of SRC_DIE.
17597 On exit *REF_CU is the CU of the result. */
17598
17599 static struct die_info *
17600 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17601 struct dwarf2_cu **ref_cu)
17602 {
17603 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17604 struct dwarf2_cu *cu = *ref_cu;
17605 struct die_info *die;
17606
17607 die = follow_die_offset (offset,
17608 (attr->form == DW_FORM_GNU_ref_alt
17609 || cu->per_cu->is_dwz),
17610 ref_cu);
17611 if (!die)
17612 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17613 "at 0x%x [in module %s]"),
17614 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17615
17616 return die;
17617 }
17618
17619 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17620 Returned value is intended for DW_OP_call*. Returned
17621 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17622
17623 struct dwarf2_locexpr_baton
17624 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17625 struct dwarf2_per_cu_data *per_cu,
17626 CORE_ADDR (*get_frame_pc) (void *baton),
17627 void *baton)
17628 {
17629 struct dwarf2_cu *cu;
17630 struct die_info *die;
17631 struct attribute *attr;
17632 struct dwarf2_locexpr_baton retval;
17633
17634 dw2_setup (per_cu->objfile);
17635
17636 if (per_cu->cu == NULL)
17637 load_cu (per_cu);
17638 cu = per_cu->cu;
17639
17640 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17641 if (!die)
17642 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17643 offset.sect_off, per_cu->objfile->name);
17644
17645 attr = dwarf2_attr (die, DW_AT_location, cu);
17646 if (!attr)
17647 {
17648 /* DWARF: "If there is no such attribute, then there is no effect.".
17649 DATA is ignored if SIZE is 0. */
17650
17651 retval.data = NULL;
17652 retval.size = 0;
17653 }
17654 else if (attr_form_is_section_offset (attr))
17655 {
17656 struct dwarf2_loclist_baton loclist_baton;
17657 CORE_ADDR pc = (*get_frame_pc) (baton);
17658 size_t size;
17659
17660 fill_in_loclist_baton (cu, &loclist_baton, attr);
17661
17662 retval.data = dwarf2_find_location_expression (&loclist_baton,
17663 &size, pc);
17664 retval.size = size;
17665 }
17666 else
17667 {
17668 if (!attr_form_is_block (attr))
17669 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17670 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17671 offset.sect_off, per_cu->objfile->name);
17672
17673 retval.data = DW_BLOCK (attr)->data;
17674 retval.size = DW_BLOCK (attr)->size;
17675 }
17676 retval.per_cu = cu->per_cu;
17677
17678 age_cached_comp_units ();
17679
17680 return retval;
17681 }
17682
17683 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17684 offset. */
17685
17686 struct dwarf2_locexpr_baton
17687 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17688 struct dwarf2_per_cu_data *per_cu,
17689 CORE_ADDR (*get_frame_pc) (void *baton),
17690 void *baton)
17691 {
17692 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17693
17694 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17695 }
17696
17697 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17698 PER_CU. */
17699
17700 struct type *
17701 dwarf2_get_die_type (cu_offset die_offset,
17702 struct dwarf2_per_cu_data *per_cu)
17703 {
17704 sect_offset die_offset_sect;
17705
17706 dw2_setup (per_cu->objfile);
17707
17708 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17709 return get_die_type_at_offset (die_offset_sect, per_cu);
17710 }
17711
17712 /* Follow the signature attribute ATTR in SRC_DIE.
17713 On entry *REF_CU is the CU of SRC_DIE.
17714 On exit *REF_CU is the CU of the result. */
17715
17716 static struct die_info *
17717 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17718 struct dwarf2_cu **ref_cu)
17719 {
17720 struct objfile *objfile = (*ref_cu)->objfile;
17721 struct die_info temp_die;
17722 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17723 struct dwarf2_cu *sig_cu;
17724 struct die_info *die;
17725
17726 /* sig_type will be NULL if the signatured type is missing from
17727 the debug info. */
17728 if (sig_type == NULL)
17729 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17730 "at 0x%x [in module %s]"),
17731 src_die->offset.sect_off, objfile->name);
17732
17733 /* If necessary, add it to the queue and load its DIEs. */
17734
17735 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17736 read_signatured_type (sig_type);
17737
17738 gdb_assert (sig_type->per_cu.cu != NULL);
17739
17740 sig_cu = sig_type->per_cu.cu;
17741 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17742 temp_die.offset = sig_type->type_offset_in_section;
17743 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17744 temp_die.offset.sect_off);
17745 if (die)
17746 {
17747 /* For .gdb_index version 7 keep track of included TUs.
17748 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17749 if (dwarf2_per_objfile->index_table != NULL
17750 && dwarf2_per_objfile->index_table->version <= 7)
17751 {
17752 VEC_safe_push (dwarf2_per_cu_ptr,
17753 (*ref_cu)->per_cu->imported_symtabs,
17754 sig_cu->per_cu);
17755 }
17756
17757 *ref_cu = sig_cu;
17758 return die;
17759 }
17760
17761 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17762 "from DIE at 0x%x [in module %s]"),
17763 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17764 }
17765
17766 /* Given an offset of a signatured type, return its signatured_type. */
17767
17768 static struct signatured_type *
17769 lookup_signatured_type_at_offset (struct objfile *objfile,
17770 struct dwarf2_section_info *section,
17771 sect_offset offset)
17772 {
17773 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17774 unsigned int length, initial_length_size;
17775 unsigned int sig_offset;
17776 struct signatured_type find_entry, *sig_type;
17777
17778 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17779 sig_offset = (initial_length_size
17780 + 2 /*version*/
17781 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17782 + 1 /*address_size*/);
17783 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17784 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17785
17786 /* This is only used to lookup previously recorded types.
17787 If we didn't find it, it's our bug. */
17788 gdb_assert (sig_type != NULL);
17789 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17790
17791 return sig_type;
17792 }
17793
17794 /* Load the DIEs associated with type unit PER_CU into memory. */
17795
17796 static void
17797 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17798 {
17799 struct signatured_type *sig_type;
17800
17801 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17802 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17803
17804 /* We have the per_cu, but we need the signatured_type.
17805 Fortunately this is an easy translation. */
17806 gdb_assert (per_cu->is_debug_types);
17807 sig_type = (struct signatured_type *) per_cu;
17808
17809 gdb_assert (per_cu->cu == NULL);
17810
17811 read_signatured_type (sig_type);
17812
17813 gdb_assert (per_cu->cu != NULL);
17814 }
17815
17816 /* die_reader_func for read_signatured_type.
17817 This is identical to load_full_comp_unit_reader,
17818 but is kept separate for now. */
17819
17820 static void
17821 read_signatured_type_reader (const struct die_reader_specs *reader,
17822 gdb_byte *info_ptr,
17823 struct die_info *comp_unit_die,
17824 int has_children,
17825 void *data)
17826 {
17827 struct dwarf2_cu *cu = reader->cu;
17828
17829 gdb_assert (cu->die_hash == NULL);
17830 cu->die_hash =
17831 htab_create_alloc_ex (cu->header.length / 12,
17832 die_hash,
17833 die_eq,
17834 NULL,
17835 &cu->comp_unit_obstack,
17836 hashtab_obstack_allocate,
17837 dummy_obstack_deallocate);
17838
17839 if (has_children)
17840 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17841 &info_ptr, comp_unit_die);
17842 cu->dies = comp_unit_die;
17843 /* comp_unit_die is not stored in die_hash, no need. */
17844
17845 /* We try not to read any attributes in this function, because not
17846 all CUs needed for references have been loaded yet, and symbol
17847 table processing isn't initialized. But we have to set the CU language,
17848 or we won't be able to build types correctly.
17849 Similarly, if we do not read the producer, we can not apply
17850 producer-specific interpretation. */
17851 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17852 }
17853
17854 /* Read in a signatured type and build its CU and DIEs.
17855 If the type is a stub for the real type in a DWO file,
17856 read in the real type from the DWO file as well. */
17857
17858 static void
17859 read_signatured_type (struct signatured_type *sig_type)
17860 {
17861 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17862
17863 gdb_assert (per_cu->is_debug_types);
17864 gdb_assert (per_cu->cu == NULL);
17865
17866 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17867 read_signatured_type_reader, NULL);
17868 }
17869
17870 /* Decode simple location descriptions.
17871 Given a pointer to a dwarf block that defines a location, compute
17872 the location and return the value.
17873
17874 NOTE drow/2003-11-18: This function is called in two situations
17875 now: for the address of static or global variables (partial symbols
17876 only) and for offsets into structures which are expected to be
17877 (more or less) constant. The partial symbol case should go away,
17878 and only the constant case should remain. That will let this
17879 function complain more accurately. A few special modes are allowed
17880 without complaint for global variables (for instance, global
17881 register values and thread-local values).
17882
17883 A location description containing no operations indicates that the
17884 object is optimized out. The return value is 0 for that case.
17885 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17886 callers will only want a very basic result and this can become a
17887 complaint.
17888
17889 Note that stack[0] is unused except as a default error return. */
17890
17891 static CORE_ADDR
17892 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17893 {
17894 struct objfile *objfile = cu->objfile;
17895 size_t i;
17896 size_t size = blk->size;
17897 gdb_byte *data = blk->data;
17898 CORE_ADDR stack[64];
17899 int stacki;
17900 unsigned int bytes_read, unsnd;
17901 gdb_byte op;
17902
17903 i = 0;
17904 stacki = 0;
17905 stack[stacki] = 0;
17906 stack[++stacki] = 0;
17907
17908 while (i < size)
17909 {
17910 op = data[i++];
17911 switch (op)
17912 {
17913 case DW_OP_lit0:
17914 case DW_OP_lit1:
17915 case DW_OP_lit2:
17916 case DW_OP_lit3:
17917 case DW_OP_lit4:
17918 case DW_OP_lit5:
17919 case DW_OP_lit6:
17920 case DW_OP_lit7:
17921 case DW_OP_lit8:
17922 case DW_OP_lit9:
17923 case DW_OP_lit10:
17924 case DW_OP_lit11:
17925 case DW_OP_lit12:
17926 case DW_OP_lit13:
17927 case DW_OP_lit14:
17928 case DW_OP_lit15:
17929 case DW_OP_lit16:
17930 case DW_OP_lit17:
17931 case DW_OP_lit18:
17932 case DW_OP_lit19:
17933 case DW_OP_lit20:
17934 case DW_OP_lit21:
17935 case DW_OP_lit22:
17936 case DW_OP_lit23:
17937 case DW_OP_lit24:
17938 case DW_OP_lit25:
17939 case DW_OP_lit26:
17940 case DW_OP_lit27:
17941 case DW_OP_lit28:
17942 case DW_OP_lit29:
17943 case DW_OP_lit30:
17944 case DW_OP_lit31:
17945 stack[++stacki] = op - DW_OP_lit0;
17946 break;
17947
17948 case DW_OP_reg0:
17949 case DW_OP_reg1:
17950 case DW_OP_reg2:
17951 case DW_OP_reg3:
17952 case DW_OP_reg4:
17953 case DW_OP_reg5:
17954 case DW_OP_reg6:
17955 case DW_OP_reg7:
17956 case DW_OP_reg8:
17957 case DW_OP_reg9:
17958 case DW_OP_reg10:
17959 case DW_OP_reg11:
17960 case DW_OP_reg12:
17961 case DW_OP_reg13:
17962 case DW_OP_reg14:
17963 case DW_OP_reg15:
17964 case DW_OP_reg16:
17965 case DW_OP_reg17:
17966 case DW_OP_reg18:
17967 case DW_OP_reg19:
17968 case DW_OP_reg20:
17969 case DW_OP_reg21:
17970 case DW_OP_reg22:
17971 case DW_OP_reg23:
17972 case DW_OP_reg24:
17973 case DW_OP_reg25:
17974 case DW_OP_reg26:
17975 case DW_OP_reg27:
17976 case DW_OP_reg28:
17977 case DW_OP_reg29:
17978 case DW_OP_reg30:
17979 case DW_OP_reg31:
17980 stack[++stacki] = op - DW_OP_reg0;
17981 if (i < size)
17982 dwarf2_complex_location_expr_complaint ();
17983 break;
17984
17985 case DW_OP_regx:
17986 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17987 i += bytes_read;
17988 stack[++stacki] = unsnd;
17989 if (i < size)
17990 dwarf2_complex_location_expr_complaint ();
17991 break;
17992
17993 case DW_OP_addr:
17994 stack[++stacki] = read_address (objfile->obfd, &data[i],
17995 cu, &bytes_read);
17996 i += bytes_read;
17997 break;
17998
17999 case DW_OP_const1u:
18000 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18001 i += 1;
18002 break;
18003
18004 case DW_OP_const1s:
18005 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18006 i += 1;
18007 break;
18008
18009 case DW_OP_const2u:
18010 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18011 i += 2;
18012 break;
18013
18014 case DW_OP_const2s:
18015 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18016 i += 2;
18017 break;
18018
18019 case DW_OP_const4u:
18020 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18021 i += 4;
18022 break;
18023
18024 case DW_OP_const4s:
18025 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18026 i += 4;
18027 break;
18028
18029 case DW_OP_const8u:
18030 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18031 i += 8;
18032 break;
18033
18034 case DW_OP_constu:
18035 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18036 &bytes_read);
18037 i += bytes_read;
18038 break;
18039
18040 case DW_OP_consts:
18041 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18042 i += bytes_read;
18043 break;
18044
18045 case DW_OP_dup:
18046 stack[stacki + 1] = stack[stacki];
18047 stacki++;
18048 break;
18049
18050 case DW_OP_plus:
18051 stack[stacki - 1] += stack[stacki];
18052 stacki--;
18053 break;
18054
18055 case DW_OP_plus_uconst:
18056 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18057 &bytes_read);
18058 i += bytes_read;
18059 break;
18060
18061 case DW_OP_minus:
18062 stack[stacki - 1] -= stack[stacki];
18063 stacki--;
18064 break;
18065
18066 case DW_OP_deref:
18067 /* If we're not the last op, then we definitely can't encode
18068 this using GDB's address_class enum. This is valid for partial
18069 global symbols, although the variable's address will be bogus
18070 in the psymtab. */
18071 if (i < size)
18072 dwarf2_complex_location_expr_complaint ();
18073 break;
18074
18075 case DW_OP_GNU_push_tls_address:
18076 /* The top of the stack has the offset from the beginning
18077 of the thread control block at which the variable is located. */
18078 /* Nothing should follow this operator, so the top of stack would
18079 be returned. */
18080 /* This is valid for partial global symbols, but the variable's
18081 address will be bogus in the psymtab. Make it always at least
18082 non-zero to not look as a variable garbage collected by linker
18083 which have DW_OP_addr 0. */
18084 if (i < size)
18085 dwarf2_complex_location_expr_complaint ();
18086 stack[stacki]++;
18087 break;
18088
18089 case DW_OP_GNU_uninit:
18090 break;
18091
18092 case DW_OP_GNU_addr_index:
18093 case DW_OP_GNU_const_index:
18094 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18095 &bytes_read);
18096 i += bytes_read;
18097 break;
18098
18099 default:
18100 {
18101 const char *name = get_DW_OP_name (op);
18102
18103 if (name)
18104 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18105 name);
18106 else
18107 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18108 op);
18109 }
18110
18111 return (stack[stacki]);
18112 }
18113
18114 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18115 outside of the allocated space. Also enforce minimum>0. */
18116 if (stacki >= ARRAY_SIZE (stack) - 1)
18117 {
18118 complaint (&symfile_complaints,
18119 _("location description stack overflow"));
18120 return 0;
18121 }
18122
18123 if (stacki <= 0)
18124 {
18125 complaint (&symfile_complaints,
18126 _("location description stack underflow"));
18127 return 0;
18128 }
18129 }
18130 return (stack[stacki]);
18131 }
18132
18133 /* memory allocation interface */
18134
18135 static struct dwarf_block *
18136 dwarf_alloc_block (struct dwarf2_cu *cu)
18137 {
18138 struct dwarf_block *blk;
18139
18140 blk = (struct dwarf_block *)
18141 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18142 return (blk);
18143 }
18144
18145 static struct die_info *
18146 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18147 {
18148 struct die_info *die;
18149 size_t size = sizeof (struct die_info);
18150
18151 if (num_attrs > 1)
18152 size += (num_attrs - 1) * sizeof (struct attribute);
18153
18154 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18155 memset (die, 0, sizeof (struct die_info));
18156 return (die);
18157 }
18158
18159 \f
18160 /* Macro support. */
18161
18162 /* Return file name relative to the compilation directory of file number I in
18163 *LH's file name table. The result is allocated using xmalloc; the caller is
18164 responsible for freeing it. */
18165
18166 static char *
18167 file_file_name (int file, struct line_header *lh)
18168 {
18169 /* Is the file number a valid index into the line header's file name
18170 table? Remember that file numbers start with one, not zero. */
18171 if (1 <= file && file <= lh->num_file_names)
18172 {
18173 struct file_entry *fe = &lh->file_names[file - 1];
18174
18175 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18176 return xstrdup (fe->name);
18177 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18178 fe->name, NULL);
18179 }
18180 else
18181 {
18182 /* The compiler produced a bogus file number. We can at least
18183 record the macro definitions made in the file, even if we
18184 won't be able to find the file by name. */
18185 char fake_name[80];
18186
18187 xsnprintf (fake_name, sizeof (fake_name),
18188 "<bad macro file number %d>", file);
18189
18190 complaint (&symfile_complaints,
18191 _("bad file number in macro information (%d)"),
18192 file);
18193
18194 return xstrdup (fake_name);
18195 }
18196 }
18197
18198 /* Return the full name of file number I in *LH's file name table.
18199 Use COMP_DIR as the name of the current directory of the
18200 compilation. The result is allocated using xmalloc; the caller is
18201 responsible for freeing it. */
18202 static char *
18203 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18204 {
18205 /* Is the file number a valid index into the line header's file name
18206 table? Remember that file numbers start with one, not zero. */
18207 if (1 <= file && file <= lh->num_file_names)
18208 {
18209 char *relative = file_file_name (file, lh);
18210
18211 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18212 return relative;
18213 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18214 }
18215 else
18216 return file_file_name (file, lh);
18217 }
18218
18219
18220 static struct macro_source_file *
18221 macro_start_file (int file, int line,
18222 struct macro_source_file *current_file,
18223 const char *comp_dir,
18224 struct line_header *lh, struct objfile *objfile)
18225 {
18226 /* File name relative to the compilation directory of this source file. */
18227 char *file_name = file_file_name (file, lh);
18228
18229 /* We don't create a macro table for this compilation unit
18230 at all until we actually get a filename. */
18231 if (! pending_macros)
18232 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18233 objfile->per_bfd->macro_cache,
18234 comp_dir);
18235
18236 if (! current_file)
18237 {
18238 /* If we have no current file, then this must be the start_file
18239 directive for the compilation unit's main source file. */
18240 current_file = macro_set_main (pending_macros, file_name);
18241 macro_define_special (pending_macros);
18242 }
18243 else
18244 current_file = macro_include (current_file, line, file_name);
18245
18246 xfree (file_name);
18247
18248 return current_file;
18249 }
18250
18251
18252 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18253 followed by a null byte. */
18254 static char *
18255 copy_string (const char *buf, int len)
18256 {
18257 char *s = xmalloc (len + 1);
18258
18259 memcpy (s, buf, len);
18260 s[len] = '\0';
18261 return s;
18262 }
18263
18264
18265 static const char *
18266 consume_improper_spaces (const char *p, const char *body)
18267 {
18268 if (*p == ' ')
18269 {
18270 complaint (&symfile_complaints,
18271 _("macro definition contains spaces "
18272 "in formal argument list:\n`%s'"),
18273 body);
18274
18275 while (*p == ' ')
18276 p++;
18277 }
18278
18279 return p;
18280 }
18281
18282
18283 static void
18284 parse_macro_definition (struct macro_source_file *file, int line,
18285 const char *body)
18286 {
18287 const char *p;
18288
18289 /* The body string takes one of two forms. For object-like macro
18290 definitions, it should be:
18291
18292 <macro name> " " <definition>
18293
18294 For function-like macro definitions, it should be:
18295
18296 <macro name> "() " <definition>
18297 or
18298 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18299
18300 Spaces may appear only where explicitly indicated, and in the
18301 <definition>.
18302
18303 The Dwarf 2 spec says that an object-like macro's name is always
18304 followed by a space, but versions of GCC around March 2002 omit
18305 the space when the macro's definition is the empty string.
18306
18307 The Dwarf 2 spec says that there should be no spaces between the
18308 formal arguments in a function-like macro's formal argument list,
18309 but versions of GCC around March 2002 include spaces after the
18310 commas. */
18311
18312
18313 /* Find the extent of the macro name. The macro name is terminated
18314 by either a space or null character (for an object-like macro) or
18315 an opening paren (for a function-like macro). */
18316 for (p = body; *p; p++)
18317 if (*p == ' ' || *p == '(')
18318 break;
18319
18320 if (*p == ' ' || *p == '\0')
18321 {
18322 /* It's an object-like macro. */
18323 int name_len = p - body;
18324 char *name = copy_string (body, name_len);
18325 const char *replacement;
18326
18327 if (*p == ' ')
18328 replacement = body + name_len + 1;
18329 else
18330 {
18331 dwarf2_macro_malformed_definition_complaint (body);
18332 replacement = body + name_len;
18333 }
18334
18335 macro_define_object (file, line, name, replacement);
18336
18337 xfree (name);
18338 }
18339 else if (*p == '(')
18340 {
18341 /* It's a function-like macro. */
18342 char *name = copy_string (body, p - body);
18343 int argc = 0;
18344 int argv_size = 1;
18345 char **argv = xmalloc (argv_size * sizeof (*argv));
18346
18347 p++;
18348
18349 p = consume_improper_spaces (p, body);
18350
18351 /* Parse the formal argument list. */
18352 while (*p && *p != ')')
18353 {
18354 /* Find the extent of the current argument name. */
18355 const char *arg_start = p;
18356
18357 while (*p && *p != ',' && *p != ')' && *p != ' ')
18358 p++;
18359
18360 if (! *p || p == arg_start)
18361 dwarf2_macro_malformed_definition_complaint (body);
18362 else
18363 {
18364 /* Make sure argv has room for the new argument. */
18365 if (argc >= argv_size)
18366 {
18367 argv_size *= 2;
18368 argv = xrealloc (argv, argv_size * sizeof (*argv));
18369 }
18370
18371 argv[argc++] = copy_string (arg_start, p - arg_start);
18372 }
18373
18374 p = consume_improper_spaces (p, body);
18375
18376 /* Consume the comma, if present. */
18377 if (*p == ',')
18378 {
18379 p++;
18380
18381 p = consume_improper_spaces (p, body);
18382 }
18383 }
18384
18385 if (*p == ')')
18386 {
18387 p++;
18388
18389 if (*p == ' ')
18390 /* Perfectly formed definition, no complaints. */
18391 macro_define_function (file, line, name,
18392 argc, (const char **) argv,
18393 p + 1);
18394 else if (*p == '\0')
18395 {
18396 /* Complain, but do define it. */
18397 dwarf2_macro_malformed_definition_complaint (body);
18398 macro_define_function (file, line, name,
18399 argc, (const char **) argv,
18400 p);
18401 }
18402 else
18403 /* Just complain. */
18404 dwarf2_macro_malformed_definition_complaint (body);
18405 }
18406 else
18407 /* Just complain. */
18408 dwarf2_macro_malformed_definition_complaint (body);
18409
18410 xfree (name);
18411 {
18412 int i;
18413
18414 for (i = 0; i < argc; i++)
18415 xfree (argv[i]);
18416 }
18417 xfree (argv);
18418 }
18419 else
18420 dwarf2_macro_malformed_definition_complaint (body);
18421 }
18422
18423 /* Skip some bytes from BYTES according to the form given in FORM.
18424 Returns the new pointer. */
18425
18426 static gdb_byte *
18427 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18428 enum dwarf_form form,
18429 unsigned int offset_size,
18430 struct dwarf2_section_info *section)
18431 {
18432 unsigned int bytes_read;
18433
18434 switch (form)
18435 {
18436 case DW_FORM_data1:
18437 case DW_FORM_flag:
18438 ++bytes;
18439 break;
18440
18441 case DW_FORM_data2:
18442 bytes += 2;
18443 break;
18444
18445 case DW_FORM_data4:
18446 bytes += 4;
18447 break;
18448
18449 case DW_FORM_data8:
18450 bytes += 8;
18451 break;
18452
18453 case DW_FORM_string:
18454 read_direct_string (abfd, bytes, &bytes_read);
18455 bytes += bytes_read;
18456 break;
18457
18458 case DW_FORM_sec_offset:
18459 case DW_FORM_strp:
18460 case DW_FORM_GNU_strp_alt:
18461 bytes += offset_size;
18462 break;
18463
18464 case DW_FORM_block:
18465 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18466 bytes += bytes_read;
18467 break;
18468
18469 case DW_FORM_block1:
18470 bytes += 1 + read_1_byte (abfd, bytes);
18471 break;
18472 case DW_FORM_block2:
18473 bytes += 2 + read_2_bytes (abfd, bytes);
18474 break;
18475 case DW_FORM_block4:
18476 bytes += 4 + read_4_bytes (abfd, bytes);
18477 break;
18478
18479 case DW_FORM_sdata:
18480 case DW_FORM_udata:
18481 case DW_FORM_GNU_addr_index:
18482 case DW_FORM_GNU_str_index:
18483 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18484 if (bytes == NULL)
18485 {
18486 dwarf2_section_buffer_overflow_complaint (section);
18487 return NULL;
18488 }
18489 break;
18490
18491 default:
18492 {
18493 complain:
18494 complaint (&symfile_complaints,
18495 _("invalid form 0x%x in `%s'"),
18496 form,
18497 section->asection->name);
18498 return NULL;
18499 }
18500 }
18501
18502 return bytes;
18503 }
18504
18505 /* A helper for dwarf_decode_macros that handles skipping an unknown
18506 opcode. Returns an updated pointer to the macro data buffer; or,
18507 on error, issues a complaint and returns NULL. */
18508
18509 static gdb_byte *
18510 skip_unknown_opcode (unsigned int opcode,
18511 gdb_byte **opcode_definitions,
18512 gdb_byte *mac_ptr, gdb_byte *mac_end,
18513 bfd *abfd,
18514 unsigned int offset_size,
18515 struct dwarf2_section_info *section)
18516 {
18517 unsigned int bytes_read, i;
18518 unsigned long arg;
18519 gdb_byte *defn;
18520
18521 if (opcode_definitions[opcode] == NULL)
18522 {
18523 complaint (&symfile_complaints,
18524 _("unrecognized DW_MACFINO opcode 0x%x"),
18525 opcode);
18526 return NULL;
18527 }
18528
18529 defn = opcode_definitions[opcode];
18530 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18531 defn += bytes_read;
18532
18533 for (i = 0; i < arg; ++i)
18534 {
18535 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18536 section);
18537 if (mac_ptr == NULL)
18538 {
18539 /* skip_form_bytes already issued the complaint. */
18540 return NULL;
18541 }
18542 }
18543
18544 return mac_ptr;
18545 }
18546
18547 /* A helper function which parses the header of a macro section.
18548 If the macro section is the extended (for now called "GNU") type,
18549 then this updates *OFFSET_SIZE. Returns a pointer to just after
18550 the header, or issues a complaint and returns NULL on error. */
18551
18552 static gdb_byte *
18553 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18554 bfd *abfd,
18555 gdb_byte *mac_ptr,
18556 unsigned int *offset_size,
18557 int section_is_gnu)
18558 {
18559 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18560
18561 if (section_is_gnu)
18562 {
18563 unsigned int version, flags;
18564
18565 version = read_2_bytes (abfd, mac_ptr);
18566 if (version != 4)
18567 {
18568 complaint (&symfile_complaints,
18569 _("unrecognized version `%d' in .debug_macro section"),
18570 version);
18571 return NULL;
18572 }
18573 mac_ptr += 2;
18574
18575 flags = read_1_byte (abfd, mac_ptr);
18576 ++mac_ptr;
18577 *offset_size = (flags & 1) ? 8 : 4;
18578
18579 if ((flags & 2) != 0)
18580 /* We don't need the line table offset. */
18581 mac_ptr += *offset_size;
18582
18583 /* Vendor opcode descriptions. */
18584 if ((flags & 4) != 0)
18585 {
18586 unsigned int i, count;
18587
18588 count = read_1_byte (abfd, mac_ptr);
18589 ++mac_ptr;
18590 for (i = 0; i < count; ++i)
18591 {
18592 unsigned int opcode, bytes_read;
18593 unsigned long arg;
18594
18595 opcode = read_1_byte (abfd, mac_ptr);
18596 ++mac_ptr;
18597 opcode_definitions[opcode] = mac_ptr;
18598 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18599 mac_ptr += bytes_read;
18600 mac_ptr += arg;
18601 }
18602 }
18603 }
18604
18605 return mac_ptr;
18606 }
18607
18608 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18609 including DW_MACRO_GNU_transparent_include. */
18610
18611 static void
18612 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18613 struct macro_source_file *current_file,
18614 struct line_header *lh, const char *comp_dir,
18615 struct dwarf2_section_info *section,
18616 int section_is_gnu, int section_is_dwz,
18617 unsigned int offset_size,
18618 struct objfile *objfile,
18619 htab_t include_hash)
18620 {
18621 enum dwarf_macro_record_type macinfo_type;
18622 int at_commandline;
18623 gdb_byte *opcode_definitions[256];
18624
18625 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18626 &offset_size, section_is_gnu);
18627 if (mac_ptr == NULL)
18628 {
18629 /* We already issued a complaint. */
18630 return;
18631 }
18632
18633 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18634 GDB is still reading the definitions from command line. First
18635 DW_MACINFO_start_file will need to be ignored as it was already executed
18636 to create CURRENT_FILE for the main source holding also the command line
18637 definitions. On first met DW_MACINFO_start_file this flag is reset to
18638 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18639
18640 at_commandline = 1;
18641
18642 do
18643 {
18644 /* Do we at least have room for a macinfo type byte? */
18645 if (mac_ptr >= mac_end)
18646 {
18647 dwarf2_section_buffer_overflow_complaint (section);
18648 break;
18649 }
18650
18651 macinfo_type = read_1_byte (abfd, mac_ptr);
18652 mac_ptr++;
18653
18654 /* Note that we rely on the fact that the corresponding GNU and
18655 DWARF constants are the same. */
18656 switch (macinfo_type)
18657 {
18658 /* A zero macinfo type indicates the end of the macro
18659 information. */
18660 case 0:
18661 break;
18662
18663 case DW_MACRO_GNU_define:
18664 case DW_MACRO_GNU_undef:
18665 case DW_MACRO_GNU_define_indirect:
18666 case DW_MACRO_GNU_undef_indirect:
18667 case DW_MACRO_GNU_define_indirect_alt:
18668 case DW_MACRO_GNU_undef_indirect_alt:
18669 {
18670 unsigned int bytes_read;
18671 int line;
18672 char *body;
18673 int is_define;
18674
18675 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18676 mac_ptr += bytes_read;
18677
18678 if (macinfo_type == DW_MACRO_GNU_define
18679 || macinfo_type == DW_MACRO_GNU_undef)
18680 {
18681 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18682 mac_ptr += bytes_read;
18683 }
18684 else
18685 {
18686 LONGEST str_offset;
18687
18688 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18689 mac_ptr += offset_size;
18690
18691 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18692 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18693 || section_is_dwz)
18694 {
18695 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18696
18697 body = read_indirect_string_from_dwz (dwz, str_offset);
18698 }
18699 else
18700 body = read_indirect_string_at_offset (abfd, str_offset);
18701 }
18702
18703 is_define = (macinfo_type == DW_MACRO_GNU_define
18704 || macinfo_type == DW_MACRO_GNU_define_indirect
18705 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18706 if (! current_file)
18707 {
18708 /* DWARF violation as no main source is present. */
18709 complaint (&symfile_complaints,
18710 _("debug info with no main source gives macro %s "
18711 "on line %d: %s"),
18712 is_define ? _("definition") : _("undefinition"),
18713 line, body);
18714 break;
18715 }
18716 if ((line == 0 && !at_commandline)
18717 || (line != 0 && at_commandline))
18718 complaint (&symfile_complaints,
18719 _("debug info gives %s macro %s with %s line %d: %s"),
18720 at_commandline ? _("command-line") : _("in-file"),
18721 is_define ? _("definition") : _("undefinition"),
18722 line == 0 ? _("zero") : _("non-zero"), line, body);
18723
18724 if (is_define)
18725 parse_macro_definition (current_file, line, body);
18726 else
18727 {
18728 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18729 || macinfo_type == DW_MACRO_GNU_undef_indirect
18730 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18731 macro_undef (current_file, line, body);
18732 }
18733 }
18734 break;
18735
18736 case DW_MACRO_GNU_start_file:
18737 {
18738 unsigned int bytes_read;
18739 int line, file;
18740
18741 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18742 mac_ptr += bytes_read;
18743 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18744 mac_ptr += bytes_read;
18745
18746 if ((line == 0 && !at_commandline)
18747 || (line != 0 && at_commandline))
18748 complaint (&symfile_complaints,
18749 _("debug info gives source %d included "
18750 "from %s at %s line %d"),
18751 file, at_commandline ? _("command-line") : _("file"),
18752 line == 0 ? _("zero") : _("non-zero"), line);
18753
18754 if (at_commandline)
18755 {
18756 /* This DW_MACRO_GNU_start_file was executed in the
18757 pass one. */
18758 at_commandline = 0;
18759 }
18760 else
18761 current_file = macro_start_file (file, line,
18762 current_file, comp_dir,
18763 lh, objfile);
18764 }
18765 break;
18766
18767 case DW_MACRO_GNU_end_file:
18768 if (! current_file)
18769 complaint (&symfile_complaints,
18770 _("macro debug info has an unmatched "
18771 "`close_file' directive"));
18772 else
18773 {
18774 current_file = current_file->included_by;
18775 if (! current_file)
18776 {
18777 enum dwarf_macro_record_type next_type;
18778
18779 /* GCC circa March 2002 doesn't produce the zero
18780 type byte marking the end of the compilation
18781 unit. Complain if it's not there, but exit no
18782 matter what. */
18783
18784 /* Do we at least have room for a macinfo type byte? */
18785 if (mac_ptr >= mac_end)
18786 {
18787 dwarf2_section_buffer_overflow_complaint (section);
18788 return;
18789 }
18790
18791 /* We don't increment mac_ptr here, so this is just
18792 a look-ahead. */
18793 next_type = read_1_byte (abfd, mac_ptr);
18794 if (next_type != 0)
18795 complaint (&symfile_complaints,
18796 _("no terminating 0-type entry for "
18797 "macros in `.debug_macinfo' section"));
18798
18799 return;
18800 }
18801 }
18802 break;
18803
18804 case DW_MACRO_GNU_transparent_include:
18805 case DW_MACRO_GNU_transparent_include_alt:
18806 {
18807 LONGEST offset;
18808 void **slot;
18809 bfd *include_bfd = abfd;
18810 struct dwarf2_section_info *include_section = section;
18811 struct dwarf2_section_info alt_section;
18812 gdb_byte *include_mac_end = mac_end;
18813 int is_dwz = section_is_dwz;
18814 gdb_byte *new_mac_ptr;
18815
18816 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18817 mac_ptr += offset_size;
18818
18819 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18820 {
18821 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18822
18823 dwarf2_read_section (dwarf2_per_objfile->objfile,
18824 &dwz->macro);
18825
18826 include_bfd = dwz->macro.asection->owner;
18827 include_section = &dwz->macro;
18828 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18829 is_dwz = 1;
18830 }
18831
18832 new_mac_ptr = include_section->buffer + offset;
18833 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18834
18835 if (*slot != NULL)
18836 {
18837 /* This has actually happened; see
18838 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18839 complaint (&symfile_complaints,
18840 _("recursive DW_MACRO_GNU_transparent_include in "
18841 ".debug_macro section"));
18842 }
18843 else
18844 {
18845 *slot = new_mac_ptr;
18846
18847 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18848 include_mac_end, current_file,
18849 lh, comp_dir,
18850 section, section_is_gnu, is_dwz,
18851 offset_size, objfile, include_hash);
18852
18853 htab_remove_elt (include_hash, new_mac_ptr);
18854 }
18855 }
18856 break;
18857
18858 case DW_MACINFO_vendor_ext:
18859 if (!section_is_gnu)
18860 {
18861 unsigned int bytes_read;
18862 int constant;
18863
18864 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18865 mac_ptr += bytes_read;
18866 read_direct_string (abfd, mac_ptr, &bytes_read);
18867 mac_ptr += bytes_read;
18868
18869 /* We don't recognize any vendor extensions. */
18870 break;
18871 }
18872 /* FALLTHROUGH */
18873
18874 default:
18875 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18876 mac_ptr, mac_end, abfd, offset_size,
18877 section);
18878 if (mac_ptr == NULL)
18879 return;
18880 break;
18881 }
18882 } while (macinfo_type != 0);
18883 }
18884
18885 static void
18886 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18887 const char *comp_dir, int section_is_gnu)
18888 {
18889 struct objfile *objfile = dwarf2_per_objfile->objfile;
18890 struct line_header *lh = cu->line_header;
18891 bfd *abfd;
18892 gdb_byte *mac_ptr, *mac_end;
18893 struct macro_source_file *current_file = 0;
18894 enum dwarf_macro_record_type macinfo_type;
18895 unsigned int offset_size = cu->header.offset_size;
18896 gdb_byte *opcode_definitions[256];
18897 struct cleanup *cleanup;
18898 htab_t include_hash;
18899 void **slot;
18900 struct dwarf2_section_info *section;
18901 const char *section_name;
18902
18903 if (cu->dwo_unit != NULL)
18904 {
18905 if (section_is_gnu)
18906 {
18907 section = &cu->dwo_unit->dwo_file->sections.macro;
18908 section_name = ".debug_macro.dwo";
18909 }
18910 else
18911 {
18912 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18913 section_name = ".debug_macinfo.dwo";
18914 }
18915 }
18916 else
18917 {
18918 if (section_is_gnu)
18919 {
18920 section = &dwarf2_per_objfile->macro;
18921 section_name = ".debug_macro";
18922 }
18923 else
18924 {
18925 section = &dwarf2_per_objfile->macinfo;
18926 section_name = ".debug_macinfo";
18927 }
18928 }
18929
18930 dwarf2_read_section (objfile, section);
18931 if (section->buffer == NULL)
18932 {
18933 complaint (&symfile_complaints, _("missing %s section"), section_name);
18934 return;
18935 }
18936 abfd = section->asection->owner;
18937
18938 /* First pass: Find the name of the base filename.
18939 This filename is needed in order to process all macros whose definition
18940 (or undefinition) comes from the command line. These macros are defined
18941 before the first DW_MACINFO_start_file entry, and yet still need to be
18942 associated to the base file.
18943
18944 To determine the base file name, we scan the macro definitions until we
18945 reach the first DW_MACINFO_start_file entry. We then initialize
18946 CURRENT_FILE accordingly so that any macro definition found before the
18947 first DW_MACINFO_start_file can still be associated to the base file. */
18948
18949 mac_ptr = section->buffer + offset;
18950 mac_end = section->buffer + section->size;
18951
18952 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18953 &offset_size, section_is_gnu);
18954 if (mac_ptr == NULL)
18955 {
18956 /* We already issued a complaint. */
18957 return;
18958 }
18959
18960 do
18961 {
18962 /* Do we at least have room for a macinfo type byte? */
18963 if (mac_ptr >= mac_end)
18964 {
18965 /* Complaint is printed during the second pass as GDB will probably
18966 stop the first pass earlier upon finding
18967 DW_MACINFO_start_file. */
18968 break;
18969 }
18970
18971 macinfo_type = read_1_byte (abfd, mac_ptr);
18972 mac_ptr++;
18973
18974 /* Note that we rely on the fact that the corresponding GNU and
18975 DWARF constants are the same. */
18976 switch (macinfo_type)
18977 {
18978 /* A zero macinfo type indicates the end of the macro
18979 information. */
18980 case 0:
18981 break;
18982
18983 case DW_MACRO_GNU_define:
18984 case DW_MACRO_GNU_undef:
18985 /* Only skip the data by MAC_PTR. */
18986 {
18987 unsigned int bytes_read;
18988
18989 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18990 mac_ptr += bytes_read;
18991 read_direct_string (abfd, mac_ptr, &bytes_read);
18992 mac_ptr += bytes_read;
18993 }
18994 break;
18995
18996 case DW_MACRO_GNU_start_file:
18997 {
18998 unsigned int bytes_read;
18999 int line, file;
19000
19001 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19002 mac_ptr += bytes_read;
19003 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19004 mac_ptr += bytes_read;
19005
19006 current_file = macro_start_file (file, line, current_file,
19007 comp_dir, lh, objfile);
19008 }
19009 break;
19010
19011 case DW_MACRO_GNU_end_file:
19012 /* No data to skip by MAC_PTR. */
19013 break;
19014
19015 case DW_MACRO_GNU_define_indirect:
19016 case DW_MACRO_GNU_undef_indirect:
19017 case DW_MACRO_GNU_define_indirect_alt:
19018 case DW_MACRO_GNU_undef_indirect_alt:
19019 {
19020 unsigned int bytes_read;
19021
19022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19023 mac_ptr += bytes_read;
19024 mac_ptr += offset_size;
19025 }
19026 break;
19027
19028 case DW_MACRO_GNU_transparent_include:
19029 case DW_MACRO_GNU_transparent_include_alt:
19030 /* Note that, according to the spec, a transparent include
19031 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19032 skip this opcode. */
19033 mac_ptr += offset_size;
19034 break;
19035
19036 case DW_MACINFO_vendor_ext:
19037 /* Only skip the data by MAC_PTR. */
19038 if (!section_is_gnu)
19039 {
19040 unsigned int bytes_read;
19041
19042 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19043 mac_ptr += bytes_read;
19044 read_direct_string (abfd, mac_ptr, &bytes_read);
19045 mac_ptr += bytes_read;
19046 }
19047 /* FALLTHROUGH */
19048
19049 default:
19050 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19051 mac_ptr, mac_end, abfd, offset_size,
19052 section);
19053 if (mac_ptr == NULL)
19054 return;
19055 break;
19056 }
19057 } while (macinfo_type != 0 && current_file == NULL);
19058
19059 /* Second pass: Process all entries.
19060
19061 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19062 command-line macro definitions/undefinitions. This flag is unset when we
19063 reach the first DW_MACINFO_start_file entry. */
19064
19065 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19066 NULL, xcalloc, xfree);
19067 cleanup = make_cleanup_htab_delete (include_hash);
19068 mac_ptr = section->buffer + offset;
19069 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19070 *slot = mac_ptr;
19071 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19072 current_file, lh, comp_dir, section,
19073 section_is_gnu, 0,
19074 offset_size, objfile, include_hash);
19075 do_cleanups (cleanup);
19076 }
19077
19078 /* Check if the attribute's form is a DW_FORM_block*
19079 if so return true else false. */
19080
19081 static int
19082 attr_form_is_block (struct attribute *attr)
19083 {
19084 return (attr == NULL ? 0 :
19085 attr->form == DW_FORM_block1
19086 || attr->form == DW_FORM_block2
19087 || attr->form == DW_FORM_block4
19088 || attr->form == DW_FORM_block
19089 || attr->form == DW_FORM_exprloc);
19090 }
19091
19092 /* Return non-zero if ATTR's value is a section offset --- classes
19093 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19094 You may use DW_UNSND (attr) to retrieve such offsets.
19095
19096 Section 7.5.4, "Attribute Encodings", explains that no attribute
19097 may have a value that belongs to more than one of these classes; it
19098 would be ambiguous if we did, because we use the same forms for all
19099 of them. */
19100
19101 static int
19102 attr_form_is_section_offset (struct attribute *attr)
19103 {
19104 return (attr->form == DW_FORM_data4
19105 || attr->form == DW_FORM_data8
19106 || attr->form == DW_FORM_sec_offset);
19107 }
19108
19109 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19110 zero otherwise. When this function returns true, you can apply
19111 dwarf2_get_attr_constant_value to it.
19112
19113 However, note that for some attributes you must check
19114 attr_form_is_section_offset before using this test. DW_FORM_data4
19115 and DW_FORM_data8 are members of both the constant class, and of
19116 the classes that contain offsets into other debug sections
19117 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19118 that, if an attribute's can be either a constant or one of the
19119 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19120 taken as section offsets, not constants. */
19121
19122 static int
19123 attr_form_is_constant (struct attribute *attr)
19124 {
19125 switch (attr->form)
19126 {
19127 case DW_FORM_sdata:
19128 case DW_FORM_udata:
19129 case DW_FORM_data1:
19130 case DW_FORM_data2:
19131 case DW_FORM_data4:
19132 case DW_FORM_data8:
19133 return 1;
19134 default:
19135 return 0;
19136 }
19137 }
19138
19139 /* Return the .debug_loc section to use for CU.
19140 For DWO files use .debug_loc.dwo. */
19141
19142 static struct dwarf2_section_info *
19143 cu_debug_loc_section (struct dwarf2_cu *cu)
19144 {
19145 if (cu->dwo_unit)
19146 return &cu->dwo_unit->dwo_file->sections.loc;
19147 return &dwarf2_per_objfile->loc;
19148 }
19149
19150 /* A helper function that fills in a dwarf2_loclist_baton. */
19151
19152 static void
19153 fill_in_loclist_baton (struct dwarf2_cu *cu,
19154 struct dwarf2_loclist_baton *baton,
19155 struct attribute *attr)
19156 {
19157 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19158
19159 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19160
19161 baton->per_cu = cu->per_cu;
19162 gdb_assert (baton->per_cu);
19163 /* We don't know how long the location list is, but make sure we
19164 don't run off the edge of the section. */
19165 baton->size = section->size - DW_UNSND (attr);
19166 baton->data = section->buffer + DW_UNSND (attr);
19167 baton->base_address = cu->base_address;
19168 baton->from_dwo = cu->dwo_unit != NULL;
19169 }
19170
19171 static void
19172 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19173 struct dwarf2_cu *cu, int is_block)
19174 {
19175 struct objfile *objfile = dwarf2_per_objfile->objfile;
19176 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19177
19178 if (attr_form_is_section_offset (attr)
19179 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19180 the section. If so, fall through to the complaint in the
19181 other branch. */
19182 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19183 {
19184 struct dwarf2_loclist_baton *baton;
19185
19186 baton = obstack_alloc (&objfile->objfile_obstack,
19187 sizeof (struct dwarf2_loclist_baton));
19188
19189 fill_in_loclist_baton (cu, baton, attr);
19190
19191 if (cu->base_known == 0)
19192 complaint (&symfile_complaints,
19193 _("Location list used without "
19194 "specifying the CU base address."));
19195
19196 SYMBOL_ACLASS_INDEX (sym) = (is_block
19197 ? dwarf2_loclist_block_index
19198 : dwarf2_loclist_index);
19199 SYMBOL_LOCATION_BATON (sym) = baton;
19200 }
19201 else
19202 {
19203 struct dwarf2_locexpr_baton *baton;
19204
19205 baton = obstack_alloc (&objfile->objfile_obstack,
19206 sizeof (struct dwarf2_locexpr_baton));
19207 baton->per_cu = cu->per_cu;
19208 gdb_assert (baton->per_cu);
19209
19210 if (attr_form_is_block (attr))
19211 {
19212 /* Note that we're just copying the block's data pointer
19213 here, not the actual data. We're still pointing into the
19214 info_buffer for SYM's objfile; right now we never release
19215 that buffer, but when we do clean up properly this may
19216 need to change. */
19217 baton->size = DW_BLOCK (attr)->size;
19218 baton->data = DW_BLOCK (attr)->data;
19219 }
19220 else
19221 {
19222 dwarf2_invalid_attrib_class_complaint ("location description",
19223 SYMBOL_NATURAL_NAME (sym));
19224 baton->size = 0;
19225 }
19226
19227 SYMBOL_ACLASS_INDEX (sym) = (is_block
19228 ? dwarf2_locexpr_block_index
19229 : dwarf2_locexpr_index);
19230 SYMBOL_LOCATION_BATON (sym) = baton;
19231 }
19232 }
19233
19234 /* Return the OBJFILE associated with the compilation unit CU. If CU
19235 came from a separate debuginfo file, then the master objfile is
19236 returned. */
19237
19238 struct objfile *
19239 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19240 {
19241 struct objfile *objfile = per_cu->objfile;
19242
19243 /* Return the master objfile, so that we can report and look up the
19244 correct file containing this variable. */
19245 if (objfile->separate_debug_objfile_backlink)
19246 objfile = objfile->separate_debug_objfile_backlink;
19247
19248 return objfile;
19249 }
19250
19251 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19252 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19253 CU_HEADERP first. */
19254
19255 static const struct comp_unit_head *
19256 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19257 struct dwarf2_per_cu_data *per_cu)
19258 {
19259 gdb_byte *info_ptr;
19260
19261 if (per_cu->cu)
19262 return &per_cu->cu->header;
19263
19264 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19265
19266 memset (cu_headerp, 0, sizeof (*cu_headerp));
19267 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19268
19269 return cu_headerp;
19270 }
19271
19272 /* Return the address size given in the compilation unit header for CU. */
19273
19274 int
19275 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19276 {
19277 struct comp_unit_head cu_header_local;
19278 const struct comp_unit_head *cu_headerp;
19279
19280 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19281
19282 return cu_headerp->addr_size;
19283 }
19284
19285 /* Return the offset size given in the compilation unit header for CU. */
19286
19287 int
19288 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19289 {
19290 struct comp_unit_head cu_header_local;
19291 const struct comp_unit_head *cu_headerp;
19292
19293 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19294
19295 return cu_headerp->offset_size;
19296 }
19297
19298 /* See its dwarf2loc.h declaration. */
19299
19300 int
19301 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19302 {
19303 struct comp_unit_head cu_header_local;
19304 const struct comp_unit_head *cu_headerp;
19305
19306 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19307
19308 if (cu_headerp->version == 2)
19309 return cu_headerp->addr_size;
19310 else
19311 return cu_headerp->offset_size;
19312 }
19313
19314 /* Return the text offset of the CU. The returned offset comes from
19315 this CU's objfile. If this objfile came from a separate debuginfo
19316 file, then the offset may be different from the corresponding
19317 offset in the parent objfile. */
19318
19319 CORE_ADDR
19320 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19321 {
19322 struct objfile *objfile = per_cu->objfile;
19323
19324 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19325 }
19326
19327 /* Locate the .debug_info compilation unit from CU's objfile which contains
19328 the DIE at OFFSET. Raises an error on failure. */
19329
19330 static struct dwarf2_per_cu_data *
19331 dwarf2_find_containing_comp_unit (sect_offset offset,
19332 unsigned int offset_in_dwz,
19333 struct objfile *objfile)
19334 {
19335 struct dwarf2_per_cu_data *this_cu;
19336 int low, high;
19337 const sect_offset *cu_off;
19338
19339 low = 0;
19340 high = dwarf2_per_objfile->n_comp_units - 1;
19341 while (high > low)
19342 {
19343 struct dwarf2_per_cu_data *mid_cu;
19344 int mid = low + (high - low) / 2;
19345
19346 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19347 cu_off = &mid_cu->offset;
19348 if (mid_cu->is_dwz > offset_in_dwz
19349 || (mid_cu->is_dwz == offset_in_dwz
19350 && cu_off->sect_off >= offset.sect_off))
19351 high = mid;
19352 else
19353 low = mid + 1;
19354 }
19355 gdb_assert (low == high);
19356 this_cu = dwarf2_per_objfile->all_comp_units[low];
19357 cu_off = &this_cu->offset;
19358 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19359 {
19360 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19361 error (_("Dwarf Error: could not find partial DIE containing "
19362 "offset 0x%lx [in module %s]"),
19363 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19364
19365 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19366 <= offset.sect_off);
19367 return dwarf2_per_objfile->all_comp_units[low-1];
19368 }
19369 else
19370 {
19371 this_cu = dwarf2_per_objfile->all_comp_units[low];
19372 if (low == dwarf2_per_objfile->n_comp_units - 1
19373 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19374 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19375 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19376 return this_cu;
19377 }
19378 }
19379
19380 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19381
19382 static void
19383 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19384 {
19385 memset (cu, 0, sizeof (*cu));
19386 per_cu->cu = cu;
19387 cu->per_cu = per_cu;
19388 cu->objfile = per_cu->objfile;
19389 obstack_init (&cu->comp_unit_obstack);
19390 }
19391
19392 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19393
19394 static void
19395 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19396 enum language pretend_language)
19397 {
19398 struct attribute *attr;
19399
19400 /* Set the language we're debugging. */
19401 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19402 if (attr)
19403 set_cu_language (DW_UNSND (attr), cu);
19404 else
19405 {
19406 cu->language = pretend_language;
19407 cu->language_defn = language_def (cu->language);
19408 }
19409
19410 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19411 if (attr)
19412 cu->producer = DW_STRING (attr);
19413 }
19414
19415 /* Release one cached compilation unit, CU. We unlink it from the tree
19416 of compilation units, but we don't remove it from the read_in_chain;
19417 the caller is responsible for that.
19418 NOTE: DATA is a void * because this function is also used as a
19419 cleanup routine. */
19420
19421 static void
19422 free_heap_comp_unit (void *data)
19423 {
19424 struct dwarf2_cu *cu = data;
19425
19426 gdb_assert (cu->per_cu != NULL);
19427 cu->per_cu->cu = NULL;
19428 cu->per_cu = NULL;
19429
19430 obstack_free (&cu->comp_unit_obstack, NULL);
19431
19432 xfree (cu);
19433 }
19434
19435 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19436 when we're finished with it. We can't free the pointer itself, but be
19437 sure to unlink it from the cache. Also release any associated storage. */
19438
19439 static void
19440 free_stack_comp_unit (void *data)
19441 {
19442 struct dwarf2_cu *cu = data;
19443
19444 gdb_assert (cu->per_cu != NULL);
19445 cu->per_cu->cu = NULL;
19446 cu->per_cu = NULL;
19447
19448 obstack_free (&cu->comp_unit_obstack, NULL);
19449 cu->partial_dies = NULL;
19450 }
19451
19452 /* Free all cached compilation units. */
19453
19454 static void
19455 free_cached_comp_units (void *data)
19456 {
19457 struct dwarf2_per_cu_data *per_cu, **last_chain;
19458
19459 per_cu = dwarf2_per_objfile->read_in_chain;
19460 last_chain = &dwarf2_per_objfile->read_in_chain;
19461 while (per_cu != NULL)
19462 {
19463 struct dwarf2_per_cu_data *next_cu;
19464
19465 next_cu = per_cu->cu->read_in_chain;
19466
19467 free_heap_comp_unit (per_cu->cu);
19468 *last_chain = next_cu;
19469
19470 per_cu = next_cu;
19471 }
19472 }
19473
19474 /* Increase the age counter on each cached compilation unit, and free
19475 any that are too old. */
19476
19477 static void
19478 age_cached_comp_units (void)
19479 {
19480 struct dwarf2_per_cu_data *per_cu, **last_chain;
19481
19482 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19483 per_cu = dwarf2_per_objfile->read_in_chain;
19484 while (per_cu != NULL)
19485 {
19486 per_cu->cu->last_used ++;
19487 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19488 dwarf2_mark (per_cu->cu);
19489 per_cu = per_cu->cu->read_in_chain;
19490 }
19491
19492 per_cu = dwarf2_per_objfile->read_in_chain;
19493 last_chain = &dwarf2_per_objfile->read_in_chain;
19494 while (per_cu != NULL)
19495 {
19496 struct dwarf2_per_cu_data *next_cu;
19497
19498 next_cu = per_cu->cu->read_in_chain;
19499
19500 if (!per_cu->cu->mark)
19501 {
19502 free_heap_comp_unit (per_cu->cu);
19503 *last_chain = next_cu;
19504 }
19505 else
19506 last_chain = &per_cu->cu->read_in_chain;
19507
19508 per_cu = next_cu;
19509 }
19510 }
19511
19512 /* Remove a single compilation unit from the cache. */
19513
19514 static void
19515 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19516 {
19517 struct dwarf2_per_cu_data *per_cu, **last_chain;
19518
19519 per_cu = dwarf2_per_objfile->read_in_chain;
19520 last_chain = &dwarf2_per_objfile->read_in_chain;
19521 while (per_cu != NULL)
19522 {
19523 struct dwarf2_per_cu_data *next_cu;
19524
19525 next_cu = per_cu->cu->read_in_chain;
19526
19527 if (per_cu == target_per_cu)
19528 {
19529 free_heap_comp_unit (per_cu->cu);
19530 per_cu->cu = NULL;
19531 *last_chain = next_cu;
19532 break;
19533 }
19534 else
19535 last_chain = &per_cu->cu->read_in_chain;
19536
19537 per_cu = next_cu;
19538 }
19539 }
19540
19541 /* Release all extra memory associated with OBJFILE. */
19542
19543 void
19544 dwarf2_free_objfile (struct objfile *objfile)
19545 {
19546 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19547
19548 if (dwarf2_per_objfile == NULL)
19549 return;
19550
19551 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19552 free_cached_comp_units (NULL);
19553
19554 if (dwarf2_per_objfile->quick_file_names_table)
19555 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19556
19557 /* Everything else should be on the objfile obstack. */
19558 }
19559
19560 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19561 We store these in a hash table separate from the DIEs, and preserve them
19562 when the DIEs are flushed out of cache.
19563
19564 The CU "per_cu" pointer is needed because offset alone is not enough to
19565 uniquely identify the type. A file may have multiple .debug_types sections,
19566 or the type may come from a DWO file. Furthermore, while it's more logical
19567 to use per_cu->section+offset, with Fission the section with the data is in
19568 the DWO file but we don't know that section at the point we need it.
19569 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19570 because we can enter the lookup routine, get_die_type_at_offset, from
19571 outside this file, and thus won't necessarily have PER_CU->cu.
19572 Fortunately, PER_CU is stable for the life of the objfile. */
19573
19574 struct dwarf2_per_cu_offset_and_type
19575 {
19576 const struct dwarf2_per_cu_data *per_cu;
19577 sect_offset offset;
19578 struct type *type;
19579 };
19580
19581 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19582
19583 static hashval_t
19584 per_cu_offset_and_type_hash (const void *item)
19585 {
19586 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19587
19588 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19589 }
19590
19591 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19592
19593 static int
19594 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19595 {
19596 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19597 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19598
19599 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19600 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19601 }
19602
19603 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19604 table if necessary. For convenience, return TYPE.
19605
19606 The DIEs reading must have careful ordering to:
19607 * Not cause infite loops trying to read in DIEs as a prerequisite for
19608 reading current DIE.
19609 * Not trying to dereference contents of still incompletely read in types
19610 while reading in other DIEs.
19611 * Enable referencing still incompletely read in types just by a pointer to
19612 the type without accessing its fields.
19613
19614 Therefore caller should follow these rules:
19615 * Try to fetch any prerequisite types we may need to build this DIE type
19616 before building the type and calling set_die_type.
19617 * After building type call set_die_type for current DIE as soon as
19618 possible before fetching more types to complete the current type.
19619 * Make the type as complete as possible before fetching more types. */
19620
19621 static struct type *
19622 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19623 {
19624 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19625 struct objfile *objfile = cu->objfile;
19626
19627 /* For Ada types, make sure that the gnat-specific data is always
19628 initialized (if not already set). There are a few types where
19629 we should not be doing so, because the type-specific area is
19630 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19631 where the type-specific area is used to store the floatformat).
19632 But this is not a problem, because the gnat-specific information
19633 is actually not needed for these types. */
19634 if (need_gnat_info (cu)
19635 && TYPE_CODE (type) != TYPE_CODE_FUNC
19636 && TYPE_CODE (type) != TYPE_CODE_FLT
19637 && !HAVE_GNAT_AUX_INFO (type))
19638 INIT_GNAT_SPECIFIC (type);
19639
19640 if (dwarf2_per_objfile->die_type_hash == NULL)
19641 {
19642 dwarf2_per_objfile->die_type_hash =
19643 htab_create_alloc_ex (127,
19644 per_cu_offset_and_type_hash,
19645 per_cu_offset_and_type_eq,
19646 NULL,
19647 &objfile->objfile_obstack,
19648 hashtab_obstack_allocate,
19649 dummy_obstack_deallocate);
19650 }
19651
19652 ofs.per_cu = cu->per_cu;
19653 ofs.offset = die->offset;
19654 ofs.type = type;
19655 slot = (struct dwarf2_per_cu_offset_and_type **)
19656 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19657 if (*slot)
19658 complaint (&symfile_complaints,
19659 _("A problem internal to GDB: DIE 0x%x has type already set"),
19660 die->offset.sect_off);
19661 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19662 **slot = ofs;
19663 return type;
19664 }
19665
19666 /* Look up the type for the die at OFFSET in the appropriate type_hash
19667 table, or return NULL if the die does not have a saved type. */
19668
19669 static struct type *
19670 get_die_type_at_offset (sect_offset offset,
19671 struct dwarf2_per_cu_data *per_cu)
19672 {
19673 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19674
19675 if (dwarf2_per_objfile->die_type_hash == NULL)
19676 return NULL;
19677
19678 ofs.per_cu = per_cu;
19679 ofs.offset = offset;
19680 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19681 if (slot)
19682 return slot->type;
19683 else
19684 return NULL;
19685 }
19686
19687 /* Look up the type for DIE in the appropriate type_hash table,
19688 or return NULL if DIE does not have a saved type. */
19689
19690 static struct type *
19691 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19692 {
19693 return get_die_type_at_offset (die->offset, cu->per_cu);
19694 }
19695
19696 /* Add a dependence relationship from CU to REF_PER_CU. */
19697
19698 static void
19699 dwarf2_add_dependence (struct dwarf2_cu *cu,
19700 struct dwarf2_per_cu_data *ref_per_cu)
19701 {
19702 void **slot;
19703
19704 if (cu->dependencies == NULL)
19705 cu->dependencies
19706 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19707 NULL, &cu->comp_unit_obstack,
19708 hashtab_obstack_allocate,
19709 dummy_obstack_deallocate);
19710
19711 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19712 if (*slot == NULL)
19713 *slot = ref_per_cu;
19714 }
19715
19716 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19717 Set the mark field in every compilation unit in the
19718 cache that we must keep because we are keeping CU. */
19719
19720 static int
19721 dwarf2_mark_helper (void **slot, void *data)
19722 {
19723 struct dwarf2_per_cu_data *per_cu;
19724
19725 per_cu = (struct dwarf2_per_cu_data *) *slot;
19726
19727 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19728 reading of the chain. As such dependencies remain valid it is not much
19729 useful to track and undo them during QUIT cleanups. */
19730 if (per_cu->cu == NULL)
19731 return 1;
19732
19733 if (per_cu->cu->mark)
19734 return 1;
19735 per_cu->cu->mark = 1;
19736
19737 if (per_cu->cu->dependencies != NULL)
19738 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19739
19740 return 1;
19741 }
19742
19743 /* Set the mark field in CU and in every other compilation unit in the
19744 cache that we must keep because we are keeping CU. */
19745
19746 static void
19747 dwarf2_mark (struct dwarf2_cu *cu)
19748 {
19749 if (cu->mark)
19750 return;
19751 cu->mark = 1;
19752 if (cu->dependencies != NULL)
19753 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19754 }
19755
19756 static void
19757 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19758 {
19759 while (per_cu)
19760 {
19761 per_cu->cu->mark = 0;
19762 per_cu = per_cu->cu->read_in_chain;
19763 }
19764 }
19765
19766 /* Trivial hash function for partial_die_info: the hash value of a DIE
19767 is its offset in .debug_info for this objfile. */
19768
19769 static hashval_t
19770 partial_die_hash (const void *item)
19771 {
19772 const struct partial_die_info *part_die = item;
19773
19774 return part_die->offset.sect_off;
19775 }
19776
19777 /* Trivial comparison function for partial_die_info structures: two DIEs
19778 are equal if they have the same offset. */
19779
19780 static int
19781 partial_die_eq (const void *item_lhs, const void *item_rhs)
19782 {
19783 const struct partial_die_info *part_die_lhs = item_lhs;
19784 const struct partial_die_info *part_die_rhs = item_rhs;
19785
19786 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19787 }
19788
19789 static struct cmd_list_element *set_dwarf2_cmdlist;
19790 static struct cmd_list_element *show_dwarf2_cmdlist;
19791
19792 static void
19793 set_dwarf2_cmd (char *args, int from_tty)
19794 {
19795 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19796 }
19797
19798 static void
19799 show_dwarf2_cmd (char *args, int from_tty)
19800 {
19801 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19802 }
19803
19804 /* Free data associated with OBJFILE, if necessary. */
19805
19806 static void
19807 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19808 {
19809 struct dwarf2_per_objfile *data = d;
19810 int ix;
19811
19812 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19813 VEC_free (dwarf2_per_cu_ptr,
19814 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19815
19816 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19817 VEC_free (dwarf2_per_cu_ptr,
19818 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19819
19820 VEC_free (dwarf2_section_info_def, data->types);
19821
19822 if (data->dwo_files)
19823 free_dwo_files (data->dwo_files, objfile);
19824 if (data->dwp_file)
19825 gdb_bfd_unref (data->dwp_file->dbfd);
19826
19827 if (data->dwz_file && data->dwz_file->dwz_bfd)
19828 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19829 }
19830
19831 \f
19832 /* The "save gdb-index" command. */
19833
19834 /* The contents of the hash table we create when building the string
19835 table. */
19836 struct strtab_entry
19837 {
19838 offset_type offset;
19839 const char *str;
19840 };
19841
19842 /* Hash function for a strtab_entry.
19843
19844 Function is used only during write_hash_table so no index format backward
19845 compatibility is needed. */
19846
19847 static hashval_t
19848 hash_strtab_entry (const void *e)
19849 {
19850 const struct strtab_entry *entry = e;
19851 return mapped_index_string_hash (INT_MAX, entry->str);
19852 }
19853
19854 /* Equality function for a strtab_entry. */
19855
19856 static int
19857 eq_strtab_entry (const void *a, const void *b)
19858 {
19859 const struct strtab_entry *ea = a;
19860 const struct strtab_entry *eb = b;
19861 return !strcmp (ea->str, eb->str);
19862 }
19863
19864 /* Create a strtab_entry hash table. */
19865
19866 static htab_t
19867 create_strtab (void)
19868 {
19869 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19870 xfree, xcalloc, xfree);
19871 }
19872
19873 /* Add a string to the constant pool. Return the string's offset in
19874 host order. */
19875
19876 static offset_type
19877 add_string (htab_t table, struct obstack *cpool, const char *str)
19878 {
19879 void **slot;
19880 struct strtab_entry entry;
19881 struct strtab_entry *result;
19882
19883 entry.str = str;
19884 slot = htab_find_slot (table, &entry, INSERT);
19885 if (*slot)
19886 result = *slot;
19887 else
19888 {
19889 result = XNEW (struct strtab_entry);
19890 result->offset = obstack_object_size (cpool);
19891 result->str = str;
19892 obstack_grow_str0 (cpool, str);
19893 *slot = result;
19894 }
19895 return result->offset;
19896 }
19897
19898 /* An entry in the symbol table. */
19899 struct symtab_index_entry
19900 {
19901 /* The name of the symbol. */
19902 const char *name;
19903 /* The offset of the name in the constant pool. */
19904 offset_type index_offset;
19905 /* A sorted vector of the indices of all the CUs that hold an object
19906 of this name. */
19907 VEC (offset_type) *cu_indices;
19908 };
19909
19910 /* The symbol table. This is a power-of-2-sized hash table. */
19911 struct mapped_symtab
19912 {
19913 offset_type n_elements;
19914 offset_type size;
19915 struct symtab_index_entry **data;
19916 };
19917
19918 /* Hash function for a symtab_index_entry. */
19919
19920 static hashval_t
19921 hash_symtab_entry (const void *e)
19922 {
19923 const struct symtab_index_entry *entry = e;
19924 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19925 sizeof (offset_type) * VEC_length (offset_type,
19926 entry->cu_indices),
19927 0);
19928 }
19929
19930 /* Equality function for a symtab_index_entry. */
19931
19932 static int
19933 eq_symtab_entry (const void *a, const void *b)
19934 {
19935 const struct symtab_index_entry *ea = a;
19936 const struct symtab_index_entry *eb = b;
19937 int len = VEC_length (offset_type, ea->cu_indices);
19938 if (len != VEC_length (offset_type, eb->cu_indices))
19939 return 0;
19940 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19941 VEC_address (offset_type, eb->cu_indices),
19942 sizeof (offset_type) * len);
19943 }
19944
19945 /* Destroy a symtab_index_entry. */
19946
19947 static void
19948 delete_symtab_entry (void *p)
19949 {
19950 struct symtab_index_entry *entry = p;
19951 VEC_free (offset_type, entry->cu_indices);
19952 xfree (entry);
19953 }
19954
19955 /* Create a hash table holding symtab_index_entry objects. */
19956
19957 static htab_t
19958 create_symbol_hash_table (void)
19959 {
19960 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19961 delete_symtab_entry, xcalloc, xfree);
19962 }
19963
19964 /* Create a new mapped symtab object. */
19965
19966 static struct mapped_symtab *
19967 create_mapped_symtab (void)
19968 {
19969 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19970 symtab->n_elements = 0;
19971 symtab->size = 1024;
19972 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19973 return symtab;
19974 }
19975
19976 /* Destroy a mapped_symtab. */
19977
19978 static void
19979 cleanup_mapped_symtab (void *p)
19980 {
19981 struct mapped_symtab *symtab = p;
19982 /* The contents of the array are freed when the other hash table is
19983 destroyed. */
19984 xfree (symtab->data);
19985 xfree (symtab);
19986 }
19987
19988 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19989 the slot.
19990
19991 Function is used only during write_hash_table so no index format backward
19992 compatibility is needed. */
19993
19994 static struct symtab_index_entry **
19995 find_slot (struct mapped_symtab *symtab, const char *name)
19996 {
19997 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19998
19999 index = hash & (symtab->size - 1);
20000 step = ((hash * 17) & (symtab->size - 1)) | 1;
20001
20002 for (;;)
20003 {
20004 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20005 return &symtab->data[index];
20006 index = (index + step) & (symtab->size - 1);
20007 }
20008 }
20009
20010 /* Expand SYMTAB's hash table. */
20011
20012 static void
20013 hash_expand (struct mapped_symtab *symtab)
20014 {
20015 offset_type old_size = symtab->size;
20016 offset_type i;
20017 struct symtab_index_entry **old_entries = symtab->data;
20018
20019 symtab->size *= 2;
20020 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20021
20022 for (i = 0; i < old_size; ++i)
20023 {
20024 if (old_entries[i])
20025 {
20026 struct symtab_index_entry **slot = find_slot (symtab,
20027 old_entries[i]->name);
20028 *slot = old_entries[i];
20029 }
20030 }
20031
20032 xfree (old_entries);
20033 }
20034
20035 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20036 CU_INDEX is the index of the CU in which the symbol appears.
20037 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20038
20039 static void
20040 add_index_entry (struct mapped_symtab *symtab, const char *name,
20041 int is_static, gdb_index_symbol_kind kind,
20042 offset_type cu_index)
20043 {
20044 struct symtab_index_entry **slot;
20045 offset_type cu_index_and_attrs;
20046
20047 ++symtab->n_elements;
20048 if (4 * symtab->n_elements / 3 >= symtab->size)
20049 hash_expand (symtab);
20050
20051 slot = find_slot (symtab, name);
20052 if (!*slot)
20053 {
20054 *slot = XNEW (struct symtab_index_entry);
20055 (*slot)->name = name;
20056 /* index_offset is set later. */
20057 (*slot)->cu_indices = NULL;
20058 }
20059
20060 cu_index_and_attrs = 0;
20061 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20062 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20063 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20064
20065 /* We don't want to record an index value twice as we want to avoid the
20066 duplication.
20067 We process all global symbols and then all static symbols
20068 (which would allow us to avoid the duplication by only having to check
20069 the last entry pushed), but a symbol could have multiple kinds in one CU.
20070 To keep things simple we don't worry about the duplication here and
20071 sort and uniqufy the list after we've processed all symbols. */
20072 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20073 }
20074
20075 /* qsort helper routine for uniquify_cu_indices. */
20076
20077 static int
20078 offset_type_compare (const void *ap, const void *bp)
20079 {
20080 offset_type a = *(offset_type *) ap;
20081 offset_type b = *(offset_type *) bp;
20082
20083 return (a > b) - (b > a);
20084 }
20085
20086 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20087
20088 static void
20089 uniquify_cu_indices (struct mapped_symtab *symtab)
20090 {
20091 int i;
20092
20093 for (i = 0; i < symtab->size; ++i)
20094 {
20095 struct symtab_index_entry *entry = symtab->data[i];
20096
20097 if (entry
20098 && entry->cu_indices != NULL)
20099 {
20100 unsigned int next_to_insert, next_to_check;
20101 offset_type last_value;
20102
20103 qsort (VEC_address (offset_type, entry->cu_indices),
20104 VEC_length (offset_type, entry->cu_indices),
20105 sizeof (offset_type), offset_type_compare);
20106
20107 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20108 next_to_insert = 1;
20109 for (next_to_check = 1;
20110 next_to_check < VEC_length (offset_type, entry->cu_indices);
20111 ++next_to_check)
20112 {
20113 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20114 != last_value)
20115 {
20116 last_value = VEC_index (offset_type, entry->cu_indices,
20117 next_to_check);
20118 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20119 last_value);
20120 ++next_to_insert;
20121 }
20122 }
20123 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20124 }
20125 }
20126 }
20127
20128 /* Add a vector of indices to the constant pool. */
20129
20130 static offset_type
20131 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20132 struct symtab_index_entry *entry)
20133 {
20134 void **slot;
20135
20136 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20137 if (!*slot)
20138 {
20139 offset_type len = VEC_length (offset_type, entry->cu_indices);
20140 offset_type val = MAYBE_SWAP (len);
20141 offset_type iter;
20142 int i;
20143
20144 *slot = entry;
20145 entry->index_offset = obstack_object_size (cpool);
20146
20147 obstack_grow (cpool, &val, sizeof (val));
20148 for (i = 0;
20149 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20150 ++i)
20151 {
20152 val = MAYBE_SWAP (iter);
20153 obstack_grow (cpool, &val, sizeof (val));
20154 }
20155 }
20156 else
20157 {
20158 struct symtab_index_entry *old_entry = *slot;
20159 entry->index_offset = old_entry->index_offset;
20160 entry = old_entry;
20161 }
20162 return entry->index_offset;
20163 }
20164
20165 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20166 constant pool entries going into the obstack CPOOL. */
20167
20168 static void
20169 write_hash_table (struct mapped_symtab *symtab,
20170 struct obstack *output, struct obstack *cpool)
20171 {
20172 offset_type i;
20173 htab_t symbol_hash_table;
20174 htab_t str_table;
20175
20176 symbol_hash_table = create_symbol_hash_table ();
20177 str_table = create_strtab ();
20178
20179 /* We add all the index vectors to the constant pool first, to
20180 ensure alignment is ok. */
20181 for (i = 0; i < symtab->size; ++i)
20182 {
20183 if (symtab->data[i])
20184 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20185 }
20186
20187 /* Now write out the hash table. */
20188 for (i = 0; i < symtab->size; ++i)
20189 {
20190 offset_type str_off, vec_off;
20191
20192 if (symtab->data[i])
20193 {
20194 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20195 vec_off = symtab->data[i]->index_offset;
20196 }
20197 else
20198 {
20199 /* While 0 is a valid constant pool index, it is not valid
20200 to have 0 for both offsets. */
20201 str_off = 0;
20202 vec_off = 0;
20203 }
20204
20205 str_off = MAYBE_SWAP (str_off);
20206 vec_off = MAYBE_SWAP (vec_off);
20207
20208 obstack_grow (output, &str_off, sizeof (str_off));
20209 obstack_grow (output, &vec_off, sizeof (vec_off));
20210 }
20211
20212 htab_delete (str_table);
20213 htab_delete (symbol_hash_table);
20214 }
20215
20216 /* Struct to map psymtab to CU index in the index file. */
20217 struct psymtab_cu_index_map
20218 {
20219 struct partial_symtab *psymtab;
20220 unsigned int cu_index;
20221 };
20222
20223 static hashval_t
20224 hash_psymtab_cu_index (const void *item)
20225 {
20226 const struct psymtab_cu_index_map *map = item;
20227
20228 return htab_hash_pointer (map->psymtab);
20229 }
20230
20231 static int
20232 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20233 {
20234 const struct psymtab_cu_index_map *lhs = item_lhs;
20235 const struct psymtab_cu_index_map *rhs = item_rhs;
20236
20237 return lhs->psymtab == rhs->psymtab;
20238 }
20239
20240 /* Helper struct for building the address table. */
20241 struct addrmap_index_data
20242 {
20243 struct objfile *objfile;
20244 struct obstack *addr_obstack;
20245 htab_t cu_index_htab;
20246
20247 /* Non-zero if the previous_* fields are valid.
20248 We can't write an entry until we see the next entry (since it is only then
20249 that we know the end of the entry). */
20250 int previous_valid;
20251 /* Index of the CU in the table of all CUs in the index file. */
20252 unsigned int previous_cu_index;
20253 /* Start address of the CU. */
20254 CORE_ADDR previous_cu_start;
20255 };
20256
20257 /* Write an address entry to OBSTACK. */
20258
20259 static void
20260 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20261 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20262 {
20263 offset_type cu_index_to_write;
20264 char addr[8];
20265 CORE_ADDR baseaddr;
20266
20267 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20268
20269 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20270 obstack_grow (obstack, addr, 8);
20271 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20272 obstack_grow (obstack, addr, 8);
20273 cu_index_to_write = MAYBE_SWAP (cu_index);
20274 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20275 }
20276
20277 /* Worker function for traversing an addrmap to build the address table. */
20278
20279 static int
20280 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20281 {
20282 struct addrmap_index_data *data = datap;
20283 struct partial_symtab *pst = obj;
20284
20285 if (data->previous_valid)
20286 add_address_entry (data->objfile, data->addr_obstack,
20287 data->previous_cu_start, start_addr,
20288 data->previous_cu_index);
20289
20290 data->previous_cu_start = start_addr;
20291 if (pst != NULL)
20292 {
20293 struct psymtab_cu_index_map find_map, *map;
20294 find_map.psymtab = pst;
20295 map = htab_find (data->cu_index_htab, &find_map);
20296 gdb_assert (map != NULL);
20297 data->previous_cu_index = map->cu_index;
20298 data->previous_valid = 1;
20299 }
20300 else
20301 data->previous_valid = 0;
20302
20303 return 0;
20304 }
20305
20306 /* Write OBJFILE's address map to OBSTACK.
20307 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20308 in the index file. */
20309
20310 static void
20311 write_address_map (struct objfile *objfile, struct obstack *obstack,
20312 htab_t cu_index_htab)
20313 {
20314 struct addrmap_index_data addrmap_index_data;
20315
20316 /* When writing the address table, we have to cope with the fact that
20317 the addrmap iterator only provides the start of a region; we have to
20318 wait until the next invocation to get the start of the next region. */
20319
20320 addrmap_index_data.objfile = objfile;
20321 addrmap_index_data.addr_obstack = obstack;
20322 addrmap_index_data.cu_index_htab = cu_index_htab;
20323 addrmap_index_data.previous_valid = 0;
20324
20325 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20326 &addrmap_index_data);
20327
20328 /* It's highly unlikely the last entry (end address = 0xff...ff)
20329 is valid, but we should still handle it.
20330 The end address is recorded as the start of the next region, but that
20331 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20332 anyway. */
20333 if (addrmap_index_data.previous_valid)
20334 add_address_entry (objfile, obstack,
20335 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20336 addrmap_index_data.previous_cu_index);
20337 }
20338
20339 /* Return the symbol kind of PSYM. */
20340
20341 static gdb_index_symbol_kind
20342 symbol_kind (struct partial_symbol *psym)
20343 {
20344 domain_enum domain = PSYMBOL_DOMAIN (psym);
20345 enum address_class aclass = PSYMBOL_CLASS (psym);
20346
20347 switch (domain)
20348 {
20349 case VAR_DOMAIN:
20350 switch (aclass)
20351 {
20352 case LOC_BLOCK:
20353 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20354 case LOC_TYPEDEF:
20355 return GDB_INDEX_SYMBOL_KIND_TYPE;
20356 case LOC_COMPUTED:
20357 case LOC_CONST_BYTES:
20358 case LOC_OPTIMIZED_OUT:
20359 case LOC_STATIC:
20360 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20361 case LOC_CONST:
20362 /* Note: It's currently impossible to recognize psyms as enum values
20363 short of reading the type info. For now punt. */
20364 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20365 default:
20366 /* There are other LOC_FOO values that one might want to classify
20367 as variables, but dwarf2read.c doesn't currently use them. */
20368 return GDB_INDEX_SYMBOL_KIND_OTHER;
20369 }
20370 case STRUCT_DOMAIN:
20371 return GDB_INDEX_SYMBOL_KIND_TYPE;
20372 default:
20373 return GDB_INDEX_SYMBOL_KIND_OTHER;
20374 }
20375 }
20376
20377 /* Add a list of partial symbols to SYMTAB. */
20378
20379 static void
20380 write_psymbols (struct mapped_symtab *symtab,
20381 htab_t psyms_seen,
20382 struct partial_symbol **psymp,
20383 int count,
20384 offset_type cu_index,
20385 int is_static)
20386 {
20387 for (; count-- > 0; ++psymp)
20388 {
20389 struct partial_symbol *psym = *psymp;
20390 void **slot;
20391
20392 if (SYMBOL_LANGUAGE (psym) == language_ada)
20393 error (_("Ada is not currently supported by the index"));
20394
20395 /* Only add a given psymbol once. */
20396 slot = htab_find_slot (psyms_seen, psym, INSERT);
20397 if (!*slot)
20398 {
20399 gdb_index_symbol_kind kind = symbol_kind (psym);
20400
20401 *slot = psym;
20402 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20403 is_static, kind, cu_index);
20404 }
20405 }
20406 }
20407
20408 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20409 exception if there is an error. */
20410
20411 static void
20412 write_obstack (FILE *file, struct obstack *obstack)
20413 {
20414 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20415 file)
20416 != obstack_object_size (obstack))
20417 error (_("couldn't data write to file"));
20418 }
20419
20420 /* Unlink a file if the argument is not NULL. */
20421
20422 static void
20423 unlink_if_set (void *p)
20424 {
20425 char **filename = p;
20426 if (*filename)
20427 unlink (*filename);
20428 }
20429
20430 /* A helper struct used when iterating over debug_types. */
20431 struct signatured_type_index_data
20432 {
20433 struct objfile *objfile;
20434 struct mapped_symtab *symtab;
20435 struct obstack *types_list;
20436 htab_t psyms_seen;
20437 int cu_index;
20438 };
20439
20440 /* A helper function that writes a single signatured_type to an
20441 obstack. */
20442
20443 static int
20444 write_one_signatured_type (void **slot, void *d)
20445 {
20446 struct signatured_type_index_data *info = d;
20447 struct signatured_type *entry = (struct signatured_type *) *slot;
20448 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
20449 gdb_byte val[8];
20450
20451 write_psymbols (info->symtab,
20452 info->psyms_seen,
20453 info->objfile->global_psymbols.list
20454 + psymtab->globals_offset,
20455 psymtab->n_global_syms, info->cu_index,
20456 0);
20457 write_psymbols (info->symtab,
20458 info->psyms_seen,
20459 info->objfile->static_psymbols.list
20460 + psymtab->statics_offset,
20461 psymtab->n_static_syms, info->cu_index,
20462 1);
20463
20464 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20465 entry->per_cu.offset.sect_off);
20466 obstack_grow (info->types_list, val, 8);
20467 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20468 entry->type_offset_in_tu.cu_off);
20469 obstack_grow (info->types_list, val, 8);
20470 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20471 obstack_grow (info->types_list, val, 8);
20472
20473 ++info->cu_index;
20474
20475 return 1;
20476 }
20477
20478 /* Recurse into all "included" dependencies and write their symbols as
20479 if they appeared in this psymtab. */
20480
20481 static void
20482 recursively_write_psymbols (struct objfile *objfile,
20483 struct partial_symtab *psymtab,
20484 struct mapped_symtab *symtab,
20485 htab_t psyms_seen,
20486 offset_type cu_index)
20487 {
20488 int i;
20489
20490 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20491 if (psymtab->dependencies[i]->user != NULL)
20492 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20493 symtab, psyms_seen, cu_index);
20494
20495 write_psymbols (symtab,
20496 psyms_seen,
20497 objfile->global_psymbols.list + psymtab->globals_offset,
20498 psymtab->n_global_syms, cu_index,
20499 0);
20500 write_psymbols (symtab,
20501 psyms_seen,
20502 objfile->static_psymbols.list + psymtab->statics_offset,
20503 psymtab->n_static_syms, cu_index,
20504 1);
20505 }
20506
20507 /* Create an index file for OBJFILE in the directory DIR. */
20508
20509 static void
20510 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20511 {
20512 struct cleanup *cleanup;
20513 char *filename, *cleanup_filename;
20514 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20515 struct obstack cu_list, types_cu_list;
20516 int i;
20517 FILE *out_file;
20518 struct mapped_symtab *symtab;
20519 offset_type val, size_of_contents, total_len;
20520 struct stat st;
20521 htab_t psyms_seen;
20522 htab_t cu_index_htab;
20523 struct psymtab_cu_index_map *psymtab_cu_index_map;
20524
20525 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20526 return;
20527
20528 if (dwarf2_per_objfile->using_index)
20529 error (_("Cannot use an index to create the index"));
20530
20531 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20532 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20533
20534 if (stat (objfile->name, &st) < 0)
20535 perror_with_name (objfile->name);
20536
20537 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20538 INDEX_SUFFIX, (char *) NULL);
20539 cleanup = make_cleanup (xfree, filename);
20540
20541 out_file = fopen (filename, "wb");
20542 if (!out_file)
20543 error (_("Can't open `%s' for writing"), filename);
20544
20545 cleanup_filename = filename;
20546 make_cleanup (unlink_if_set, &cleanup_filename);
20547
20548 symtab = create_mapped_symtab ();
20549 make_cleanup (cleanup_mapped_symtab, symtab);
20550
20551 obstack_init (&addr_obstack);
20552 make_cleanup_obstack_free (&addr_obstack);
20553
20554 obstack_init (&cu_list);
20555 make_cleanup_obstack_free (&cu_list);
20556
20557 obstack_init (&types_cu_list);
20558 make_cleanup_obstack_free (&types_cu_list);
20559
20560 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20561 NULL, xcalloc, xfree);
20562 make_cleanup_htab_delete (psyms_seen);
20563
20564 /* While we're scanning CU's create a table that maps a psymtab pointer
20565 (which is what addrmap records) to its index (which is what is recorded
20566 in the index file). This will later be needed to write the address
20567 table. */
20568 cu_index_htab = htab_create_alloc (100,
20569 hash_psymtab_cu_index,
20570 eq_psymtab_cu_index,
20571 NULL, xcalloc, xfree);
20572 make_cleanup_htab_delete (cu_index_htab);
20573 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20574 xmalloc (sizeof (struct psymtab_cu_index_map)
20575 * dwarf2_per_objfile->n_comp_units);
20576 make_cleanup (xfree, psymtab_cu_index_map);
20577
20578 /* The CU list is already sorted, so we don't need to do additional
20579 work here. Also, the debug_types entries do not appear in
20580 all_comp_units, but only in their own hash table. */
20581 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20582 {
20583 struct dwarf2_per_cu_data *per_cu
20584 = dwarf2_per_objfile->all_comp_units[i];
20585 struct partial_symtab *psymtab = per_cu->v.psymtab;
20586 gdb_byte val[8];
20587 struct psymtab_cu_index_map *map;
20588 void **slot;
20589
20590 if (psymtab->user == NULL)
20591 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20592
20593 map = &psymtab_cu_index_map[i];
20594 map->psymtab = psymtab;
20595 map->cu_index = i;
20596 slot = htab_find_slot (cu_index_htab, map, INSERT);
20597 gdb_assert (slot != NULL);
20598 gdb_assert (*slot == NULL);
20599 *slot = map;
20600
20601 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20602 per_cu->offset.sect_off);
20603 obstack_grow (&cu_list, val, 8);
20604 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20605 obstack_grow (&cu_list, val, 8);
20606 }
20607
20608 /* Dump the address map. */
20609 write_address_map (objfile, &addr_obstack, cu_index_htab);
20610
20611 /* Write out the .debug_type entries, if any. */
20612 if (dwarf2_per_objfile->signatured_types)
20613 {
20614 struct signatured_type_index_data sig_data;
20615
20616 sig_data.objfile = objfile;
20617 sig_data.symtab = symtab;
20618 sig_data.types_list = &types_cu_list;
20619 sig_data.psyms_seen = psyms_seen;
20620 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20621 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20622 write_one_signatured_type, &sig_data);
20623 }
20624
20625 /* Now that we've processed all symbols we can shrink their cu_indices
20626 lists. */
20627 uniquify_cu_indices (symtab);
20628
20629 obstack_init (&constant_pool);
20630 make_cleanup_obstack_free (&constant_pool);
20631 obstack_init (&symtab_obstack);
20632 make_cleanup_obstack_free (&symtab_obstack);
20633 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20634
20635 obstack_init (&contents);
20636 make_cleanup_obstack_free (&contents);
20637 size_of_contents = 6 * sizeof (offset_type);
20638 total_len = size_of_contents;
20639
20640 /* The version number. */
20641 val = MAYBE_SWAP (8);
20642 obstack_grow (&contents, &val, sizeof (val));
20643
20644 /* The offset of the CU list from the start of the file. */
20645 val = MAYBE_SWAP (total_len);
20646 obstack_grow (&contents, &val, sizeof (val));
20647 total_len += obstack_object_size (&cu_list);
20648
20649 /* The offset of the types CU list from the start of the file. */
20650 val = MAYBE_SWAP (total_len);
20651 obstack_grow (&contents, &val, sizeof (val));
20652 total_len += obstack_object_size (&types_cu_list);
20653
20654 /* The offset of the address table from the start of the file. */
20655 val = MAYBE_SWAP (total_len);
20656 obstack_grow (&contents, &val, sizeof (val));
20657 total_len += obstack_object_size (&addr_obstack);
20658
20659 /* The offset of the symbol table from the start of the file. */
20660 val = MAYBE_SWAP (total_len);
20661 obstack_grow (&contents, &val, sizeof (val));
20662 total_len += obstack_object_size (&symtab_obstack);
20663
20664 /* The offset of the constant pool from the start of the file. */
20665 val = MAYBE_SWAP (total_len);
20666 obstack_grow (&contents, &val, sizeof (val));
20667 total_len += obstack_object_size (&constant_pool);
20668
20669 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20670
20671 write_obstack (out_file, &contents);
20672 write_obstack (out_file, &cu_list);
20673 write_obstack (out_file, &types_cu_list);
20674 write_obstack (out_file, &addr_obstack);
20675 write_obstack (out_file, &symtab_obstack);
20676 write_obstack (out_file, &constant_pool);
20677
20678 fclose (out_file);
20679
20680 /* We want to keep the file, so we set cleanup_filename to NULL
20681 here. See unlink_if_set. */
20682 cleanup_filename = NULL;
20683
20684 do_cleanups (cleanup);
20685 }
20686
20687 /* Implementation of the `save gdb-index' command.
20688
20689 Note that the file format used by this command is documented in the
20690 GDB manual. Any changes here must be documented there. */
20691
20692 static void
20693 save_gdb_index_command (char *arg, int from_tty)
20694 {
20695 struct objfile *objfile;
20696
20697 if (!arg || !*arg)
20698 error (_("usage: save gdb-index DIRECTORY"));
20699
20700 ALL_OBJFILES (objfile)
20701 {
20702 struct stat st;
20703
20704 /* If the objfile does not correspond to an actual file, skip it. */
20705 if (stat (objfile->name, &st) < 0)
20706 continue;
20707
20708 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20709 if (dwarf2_per_objfile)
20710 {
20711 volatile struct gdb_exception except;
20712
20713 TRY_CATCH (except, RETURN_MASK_ERROR)
20714 {
20715 write_psymtabs_to_index (objfile, arg);
20716 }
20717 if (except.reason < 0)
20718 exception_fprintf (gdb_stderr, except,
20719 _("Error while writing index for `%s': "),
20720 objfile->name);
20721 }
20722 }
20723 }
20724
20725 \f
20726
20727 int dwarf2_always_disassemble;
20728
20729 static void
20730 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20731 struct cmd_list_element *c, const char *value)
20732 {
20733 fprintf_filtered (file,
20734 _("Whether to always disassemble "
20735 "DWARF expressions is %s.\n"),
20736 value);
20737 }
20738
20739 static void
20740 show_check_physname (struct ui_file *file, int from_tty,
20741 struct cmd_list_element *c, const char *value)
20742 {
20743 fprintf_filtered (file,
20744 _("Whether to check \"physname\" is %s.\n"),
20745 value);
20746 }
20747
20748 void _initialize_dwarf2_read (void);
20749
20750 void
20751 _initialize_dwarf2_read (void)
20752 {
20753 struct cmd_list_element *c;
20754
20755 dwarf2_objfile_data_key
20756 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20757
20758 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20759 Set DWARF 2 specific variables.\n\
20760 Configure DWARF 2 variables such as the cache size"),
20761 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20762 0/*allow-unknown*/, &maintenance_set_cmdlist);
20763
20764 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20765 Show DWARF 2 specific variables\n\
20766 Show DWARF 2 variables such as the cache size"),
20767 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20768 0/*allow-unknown*/, &maintenance_show_cmdlist);
20769
20770 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20771 &dwarf2_max_cache_age, _("\
20772 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20773 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20774 A higher limit means that cached compilation units will be stored\n\
20775 in memory longer, and more total memory will be used. Zero disables\n\
20776 caching, which can slow down startup."),
20777 NULL,
20778 show_dwarf2_max_cache_age,
20779 &set_dwarf2_cmdlist,
20780 &show_dwarf2_cmdlist);
20781
20782 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20783 &dwarf2_always_disassemble, _("\
20784 Set whether `info address' always disassembles DWARF expressions."), _("\
20785 Show whether `info address' always disassembles DWARF expressions."), _("\
20786 When enabled, DWARF expressions are always printed in an assembly-like\n\
20787 syntax. When disabled, expressions will be printed in a more\n\
20788 conversational style, when possible."),
20789 NULL,
20790 show_dwarf2_always_disassemble,
20791 &set_dwarf2_cmdlist,
20792 &show_dwarf2_cmdlist);
20793
20794 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20795 Set debugging of the dwarf2 reader."), _("\
20796 Show debugging of the dwarf2 reader."), _("\
20797 When enabled, debugging messages are printed during dwarf2 reading\n\
20798 and symtab expansion."),
20799 NULL,
20800 NULL,
20801 &setdebuglist, &showdebuglist);
20802
20803 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20804 Set debugging of the dwarf2 DIE reader."), _("\
20805 Show debugging of the dwarf2 DIE reader."), _("\
20806 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20807 The value is the maximum depth to print."),
20808 NULL,
20809 NULL,
20810 &setdebuglist, &showdebuglist);
20811
20812 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20813 Set cross-checking of \"physname\" code against demangler."), _("\
20814 Show cross-checking of \"physname\" code against demangler."), _("\
20815 When enabled, GDB's internal \"physname\" code is checked against\n\
20816 the demangler."),
20817 NULL, show_check_physname,
20818 &setdebuglist, &showdebuglist);
20819
20820 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20821 no_class, &use_deprecated_index_sections, _("\
20822 Set whether to use deprecated gdb_index sections."), _("\
20823 Show whether to use deprecated gdb_index sections."), _("\
20824 When enabled, deprecated .gdb_index sections are used anyway.\n\
20825 Normally they are ignored either because of a missing feature or\n\
20826 performance issue.\n\
20827 Warning: This option must be enabled before gdb reads the file."),
20828 NULL,
20829 NULL,
20830 &setlist, &showlist);
20831
20832 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20833 _("\
20834 Save a gdb-index file.\n\
20835 Usage: save gdb-index DIRECTORY"),
20836 &save_cmdlist);
20837 set_cmd_completer (c, filename_completer);
20838
20839 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20840 &dwarf2_locexpr_funcs);
20841 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20842 &dwarf2_loclist_funcs);
20843
20844 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20845 &dwarf2_block_frame_base_locexpr_funcs);
20846 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20847 &dwarf2_block_frame_base_loclist_funcs);
20848 }
This page took 1.343965 seconds and 4 git commands to generate.