gdb/dwarf2read.c: rename a field for c++
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2015 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 <sys/stat.h>
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68 #include "f-lang.h"
69 #include "source.h"
70 #include "filestuff.h"
71 #include "build-id.h"
72 #include "namespace.h"
73
74 #include <fcntl.h>
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When == 1, print basic high level tracing messages.
81 When > 1, be more verbose.
82 This is in contrast to the low level DIE reading of dwarf_die_debug. */
83 static unsigned int dwarf_read_debug = 0;
84
85 /* When non-zero, dump DIEs after they are read in. */
86 static unsigned int dwarf_die_debug = 0;
87
88 /* When non-zero, dump line number entries as they are read in. */
89 static unsigned int dwarf_line_debug = 0;
90
91 /* When non-zero, cross-check physname against demangler. */
92 static int check_physname = 0;
93
94 /* When non-zero, do not reject deprecated .gdb_index sections. */
95 static int use_deprecated_index_sections = 0;
96
97 static const struct objfile_data *dwarf2_objfile_data_key;
98
99 /* The "aclass" indices for various kinds of computed DWARF symbols. */
100
101 static int dwarf2_locexpr_index;
102 static int dwarf2_loclist_index;
103 static int dwarf2_locexpr_block_index;
104 static int dwarf2_loclist_block_index;
105
106 /* A descriptor for dwarf sections.
107
108 S.ASECTION, SIZE are typically initialized when the objfile is first
109 scanned. BUFFER, READIN are filled in later when the section is read.
110 If the section contained compressed data then SIZE is updated to record
111 the uncompressed size of the section.
112
113 DWP file format V2 introduces a wrinkle that is easiest to handle by
114 creating the concept of virtual sections contained within a real section.
115 In DWP V2 the sections of the input DWO files are concatenated together
116 into one section, but section offsets are kept relative to the original
117 input section.
118 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
119 the real section this "virtual" section is contained in, and BUFFER,SIZE
120 describe the virtual section. */
121
122 struct dwarf2_section_info
123 {
124 union
125 {
126 /* If this is a real section, the bfd section. */
127 asection *section;
128 /* If this is a virtual section, pointer to the containing ("real")
129 section. */
130 struct dwarf2_section_info *containing_section;
131 } s;
132 /* Pointer to section data, only valid if readin. */
133 const gdb_byte *buffer;
134 /* The size of the section, real or virtual. */
135 bfd_size_type size;
136 /* If this is a virtual section, the offset in the real section.
137 Only valid if is_virtual. */
138 bfd_size_type virtual_offset;
139 /* True if we have tried to read this section. */
140 char readin;
141 /* True if this is a virtual section, False otherwise.
142 This specifies which of s.section and s.containing_section to use. */
143 char is_virtual;
144 };
145
146 typedef struct dwarf2_section_info dwarf2_section_info_def;
147 DEF_VEC_O (dwarf2_section_info_def);
148
149 /* All offsets in the index are of this type. It must be
150 architecture-independent. */
151 typedef uint32_t offset_type;
152
153 DEF_VEC_I (offset_type);
154
155 /* Ensure only legit values are used. */
156 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
157 do { \
158 gdb_assert ((unsigned int) (value) <= 1); \
159 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
160 } while (0)
161
162 /* Ensure only legit values are used. */
163 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
166 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
167 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
168 } while (0)
169
170 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
171 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
172 do { \
173 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
174 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index
180 {
181 /* Index data format version. */
182 int version;
183
184 /* The total length of the buffer. */
185 off_t total_size;
186
187 /* A pointer to the address table data. */
188 const gdb_byte *address_table;
189
190 /* Size of the address table data in bytes. */
191 offset_type address_table_size;
192
193 /* The symbol table, implemented as a hash table. */
194 const offset_type *symbol_table;
195
196 /* Size in slots, each slot is 2 offset_types. */
197 offset_type symbol_table_slots;
198
199 /* A pointer to the constant pool. */
200 const char *constant_pool;
201 };
202
203 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
204 DEF_VEC_P (dwarf2_per_cu_ptr);
205
206 struct tu_stats
207 {
208 int nr_uniq_abbrev_tables;
209 int nr_symtabs;
210 int nr_symtab_sharers;
211 int nr_stmt_less_type_units;
212 int nr_all_type_units_reallocs;
213 };
214
215 /* Collection of data recorded per objfile.
216 This hangs off of dwarf2_objfile_data_key. */
217
218 struct dwarf2_per_objfile
219 {
220 struct dwarf2_section_info info;
221 struct dwarf2_section_info abbrev;
222 struct dwarf2_section_info line;
223 struct dwarf2_section_info loc;
224 struct dwarf2_section_info macinfo;
225 struct dwarf2_section_info macro;
226 struct dwarf2_section_info str;
227 struct dwarf2_section_info ranges;
228 struct dwarf2_section_info addr;
229 struct dwarf2_section_info frame;
230 struct dwarf2_section_info eh_frame;
231 struct dwarf2_section_info gdb_index;
232
233 VEC (dwarf2_section_info_def) *types;
234
235 /* Back link. */
236 struct objfile *objfile;
237
238 /* Table of all the compilation units. This is used to locate
239 the target compilation unit of a particular reference. */
240 struct dwarf2_per_cu_data **all_comp_units;
241
242 /* The number of compilation units in ALL_COMP_UNITS. */
243 int n_comp_units;
244
245 /* The number of .debug_types-related CUs. */
246 int n_type_units;
247
248 /* The number of elements allocated in all_type_units.
249 If there are skeleton-less TUs, we add them to all_type_units lazily. */
250 int n_allocated_type_units;
251
252 /* The .debug_types-related CUs (TUs).
253 This is stored in malloc space because we may realloc it. */
254 struct signatured_type **all_type_units;
255
256 /* Table of struct type_unit_group objects.
257 The hash key is the DW_AT_stmt_list value. */
258 htab_t type_unit_groups;
259
260 /* A table mapping .debug_types signatures to its signatured_type entry.
261 This is NULL if the .debug_types section hasn't been read in yet. */
262 htab_t signatured_types;
263
264 /* Type unit statistics, to see how well the scaling improvements
265 are doing. */
266 struct tu_stats tu_stats;
267
268 /* A chain of compilation units that are currently read in, so that
269 they can be freed later. */
270 struct dwarf2_per_cu_data *read_in_chain;
271
272 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
273 This is NULL if the table hasn't been allocated yet. */
274 htab_t dwo_files;
275
276 /* Non-zero if we've check for whether there is a DWP file. */
277 int dwp_checked;
278
279 /* The DWP file if there is one, or NULL. */
280 struct dwp_file *dwp_file;
281
282 /* The shared '.dwz' file, if one exists. This is used when the
283 original data was compressed using 'dwz -m'. */
284 struct dwz_file *dwz_file;
285
286 /* A flag indicating wether this objfile has a section loaded at a
287 VMA of 0. */
288 int has_section_at_zero;
289
290 /* True if we are using the mapped index,
291 or we are faking it for OBJF_READNOW's sake. */
292 unsigned char using_index;
293
294 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
295 struct mapped_index *index_table;
296
297 /* When using index_table, this keeps track of all quick_file_names entries.
298 TUs typically share line table entries with a CU, so we maintain a
299 separate table of all line table entries to support the sharing.
300 Note that while there can be way more TUs than CUs, we've already
301 sorted all the TUs into "type unit groups", grouped by their
302 DW_AT_stmt_list value. Therefore the only sharing done here is with a
303 CU and its associated TU group if there is one. */
304 htab_t quick_file_names_table;
305
306 /* Set during partial symbol reading, to prevent queueing of full
307 symbols. */
308 int reading_partial_symbols;
309
310 /* Table mapping type DIEs to their struct type *.
311 This is NULL if not allocated yet.
312 The mapping is done via (CU/TU + DIE offset) -> type. */
313 htab_t die_type_hash;
314
315 /* The CUs we recently read. */
316 VEC (dwarf2_per_cu_ptr) *just_read_cus;
317
318 /* Table containing line_header indexed by offset and offset_in_dwz. */
319 htab_t line_header_hash;
320 };
321
322 static struct dwarf2_per_objfile *dwarf2_per_objfile;
323
324 /* Default names of the debugging sections. */
325
326 /* Note that if the debugging section has been compressed, it might
327 have a name like .zdebug_info. */
328
329 static const struct dwarf2_debug_sections dwarf2_elf_names =
330 {
331 { ".debug_info", ".zdebug_info" },
332 { ".debug_abbrev", ".zdebug_abbrev" },
333 { ".debug_line", ".zdebug_line" },
334 { ".debug_loc", ".zdebug_loc" },
335 { ".debug_macinfo", ".zdebug_macinfo" },
336 { ".debug_macro", ".zdebug_macro" },
337 { ".debug_str", ".zdebug_str" },
338 { ".debug_ranges", ".zdebug_ranges" },
339 { ".debug_types", ".zdebug_types" },
340 { ".debug_addr", ".zdebug_addr" },
341 { ".debug_frame", ".zdebug_frame" },
342 { ".eh_frame", NULL },
343 { ".gdb_index", ".zgdb_index" },
344 23
345 };
346
347 /* List of DWO/DWP sections. */
348
349 static const struct dwop_section_names
350 {
351 struct dwarf2_section_names abbrev_dwo;
352 struct dwarf2_section_names info_dwo;
353 struct dwarf2_section_names line_dwo;
354 struct dwarf2_section_names loc_dwo;
355 struct dwarf2_section_names macinfo_dwo;
356 struct dwarf2_section_names macro_dwo;
357 struct dwarf2_section_names str_dwo;
358 struct dwarf2_section_names str_offsets_dwo;
359 struct dwarf2_section_names types_dwo;
360 struct dwarf2_section_names cu_index;
361 struct dwarf2_section_names tu_index;
362 }
363 dwop_section_names =
364 {
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
369 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
370 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
376 };
377
378 /* local data types */
379
380 /* The data in a compilation unit header, after target2host
381 translation, looks like this. */
382 struct comp_unit_head
383 {
384 unsigned int length;
385 short version;
386 unsigned char addr_size;
387 unsigned char signed_addr_p;
388 sect_offset abbrev_offset;
389
390 /* Size of file offsets; either 4 or 8. */
391 unsigned int offset_size;
392
393 /* Size of the length field; either 4 or 12. */
394 unsigned int initial_length_size;
395
396 /* Offset to the first byte of this compilation unit header in the
397 .debug_info section, for resolving relative reference dies. */
398 sect_offset offset;
399
400 /* Offset to first die in this cu from the start of the cu.
401 This will be the first byte following the compilation unit header. */
402 cu_offset first_die_offset;
403 };
404
405 /* Type used for delaying computation of method physnames.
406 See comments for compute_delayed_physnames. */
407 struct delayed_method_info
408 {
409 /* The type to which the method is attached, i.e., its parent class. */
410 struct type *type;
411
412 /* The index of the method in the type's function fieldlists. */
413 int fnfield_index;
414
415 /* The index of the method in the fieldlist. */
416 int index;
417
418 /* The name of the DIE. */
419 const char *name;
420
421 /* The DIE associated with this method. */
422 struct die_info *die;
423 };
424
425 typedef struct delayed_method_info delayed_method_info;
426 DEF_VEC_O (delayed_method_info);
427
428 /* Internal state when decoding a particular compilation unit. */
429 struct dwarf2_cu
430 {
431 /* The objfile containing this compilation unit. */
432 struct objfile *objfile;
433
434 /* The header of the compilation unit. */
435 struct comp_unit_head header;
436
437 /* Base address of this compilation unit. */
438 CORE_ADDR base_address;
439
440 /* Non-zero if base_address has been set. */
441 int base_known;
442
443 /* The language we are debugging. */
444 enum language language;
445 const struct language_defn *language_defn;
446
447 const char *producer;
448
449 /* The generic symbol table building routines have separate lists for
450 file scope symbols and all all other scopes (local scopes). So
451 we need to select the right one to pass to add_symbol_to_list().
452 We do it by keeping a pointer to the correct list in list_in_scope.
453
454 FIXME: The original dwarf code just treated the file scope as the
455 first local scope, and all other local scopes as nested local
456 scopes, and worked fine. Check to see if we really need to
457 distinguish these in buildsym.c. */
458 struct pending **list_in_scope;
459
460 /* The abbrev table for this CU.
461 Normally this points to the abbrev table in the objfile.
462 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
463 struct abbrev_table *abbrev_table;
464
465 /* Hash table holding all the loaded partial DIEs
466 with partial_die->offset.SECT_OFF as hash. */
467 htab_t partial_dies;
468
469 /* Storage for things with the same lifetime as this read-in compilation
470 unit, including partial DIEs. */
471 struct obstack comp_unit_obstack;
472
473 /* When multiple dwarf2_cu structures are living in memory, this field
474 chains them all together, so that they can be released efficiently.
475 We will probably also want a generation counter so that most-recently-used
476 compilation units are cached... */
477 struct dwarf2_per_cu_data *read_in_chain;
478
479 /* Backlink to our per_cu entry. */
480 struct dwarf2_per_cu_data *per_cu;
481
482 /* How many compilation units ago was this CU last referenced? */
483 int last_used;
484
485 /* A hash table of DIE cu_offset for following references with
486 die_info->offset.sect_off as hash. */
487 htab_t die_hash;
488
489 /* Full DIEs if read in. */
490 struct die_info *dies;
491
492 /* A set of pointers to dwarf2_per_cu_data objects for compilation
493 units referenced by this one. Only set during full symbol processing;
494 partial symbol tables do not have dependencies. */
495 htab_t dependencies;
496
497 /* Header data from the line table, during full symbol processing. */
498 struct line_header *line_header;
499
500 /* A list of methods which need to have physnames computed
501 after all type information has been read. */
502 VEC (delayed_method_info) *method_list;
503
504 /* To be copied to symtab->call_site_htab. */
505 htab_t call_site_htab;
506
507 /* Non-NULL if this CU came from a DWO file.
508 There is an invariant here that is important to remember:
509 Except for attributes copied from the top level DIE in the "main"
510 (or "stub") file in preparation for reading the DWO file
511 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
512 Either there isn't a DWO file (in which case this is NULL and the point
513 is moot), or there is and either we're not going to read it (in which
514 case this is NULL) or there is and we are reading it (in which case this
515 is non-NULL). */
516 struct dwo_unit *dwo_unit;
517
518 /* The DW_AT_addr_base attribute if present, zero otherwise
519 (zero is a valid value though).
520 Note this value comes from the Fission stub CU/TU's DIE. */
521 ULONGEST addr_base;
522
523 /* The DW_AT_ranges_base attribute if present, zero otherwise
524 (zero is a valid value though).
525 Note this value comes from the Fission stub CU/TU's DIE.
526 Also note that the value is zero in the non-DWO case so this value can
527 be used without needing to know whether DWO files are in use or not.
528 N.B. This does not apply to DW_AT_ranges appearing in
529 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
530 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
531 DW_AT_ranges_base *would* have to be applied, and we'd have to care
532 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
533 ULONGEST ranges_base;
534
535 /* Mark used when releasing cached dies. */
536 unsigned int mark : 1;
537
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
542 unsigned int has_loclist : 1;
543
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
548 unsigned int checked_producer : 1;
549 unsigned int producer_is_gxx_lt_4_6 : 1;
550 unsigned int producer_is_gcc_lt_4_3 : 1;
551 unsigned int producer_is_icc : 1;
552
553 /* When set, the file that we're processing is known to have
554 debugging info for C++ namespaces. GCC 3.3.x did not produce
555 this information, but later versions do. */
556
557 unsigned int processing_has_namespace_info : 1;
558 };
559
560 /* Persistent data held for a compilation unit, even when not
561 processing it. We put a pointer to this structure in the
562 read_symtab_private field of the psymtab. */
563
564 struct dwarf2_per_cu_data
565 {
566 /* The start offset and length of this compilation unit.
567 NOTE: Unlike comp_unit_head.length, this length includes
568 initial_length_size.
569 If the DIE refers to a DWO file, this is always of the original die,
570 not the DWO file. */
571 sect_offset offset;
572 unsigned int length;
573
574 /* Flag indicating this compilation unit will be read in before
575 any of the current compilation units are processed. */
576 unsigned int queued : 1;
577
578 /* This flag will be set when reading partial DIEs if we need to load
579 absolutely all DIEs for this compilation unit, instead of just the ones
580 we think are interesting. It gets set if we look for a DIE in the
581 hash table and don't find it. */
582 unsigned int load_all_dies : 1;
583
584 /* Non-zero if this CU is from .debug_types.
585 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
586 this is non-zero. */
587 unsigned int is_debug_types : 1;
588
589 /* Non-zero if this CU is from the .dwz file. */
590 unsigned int is_dwz : 1;
591
592 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
593 This flag is only valid if is_debug_types is true.
594 We can't read a CU directly from a DWO file: There are required
595 attributes in the stub. */
596 unsigned int reading_dwo_directly : 1;
597
598 /* Non-zero if the TU has been read.
599 This is used to assist the "Stay in DWO Optimization" for Fission:
600 When reading a DWO, it's faster to read TUs from the DWO instead of
601 fetching them from random other DWOs (due to comdat folding).
602 If the TU has already been read, the optimization is unnecessary
603 (and unwise - we don't want to change where gdb thinks the TU lives
604 "midflight").
605 This flag is only valid if is_debug_types is true. */
606 unsigned int tu_read : 1;
607
608 /* The section this CU/TU lives in.
609 If the DIE refers to a DWO file, this is always the original die,
610 not the DWO file. */
611 struct dwarf2_section_info *section;
612
613 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
614 of the CU cache it gets reset to NULL again. This is left as NULL for
615 dummy CUs (a CU header, but nothing else). */
616 struct dwarf2_cu *cu;
617
618 /* The corresponding objfile.
619 Normally we can get the objfile from dwarf2_per_objfile.
620 However we can enter this file with just a "per_cu" handle. */
621 struct objfile *objfile;
622
623 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
624 is active. Otherwise, the 'psymtab' field is active. */
625 union
626 {
627 /* The partial symbol table associated with this compilation unit,
628 or NULL for unread partial units. */
629 struct partial_symtab *psymtab;
630
631 /* Data needed by the "quick" functions. */
632 struct dwarf2_per_cu_quick_data *quick;
633 } v;
634
635 /* The CUs we import using DW_TAG_imported_unit. This is filled in
636 while reading psymtabs, used to compute the psymtab dependencies,
637 and then cleared. Then it is filled in again while reading full
638 symbols, and only deleted when the objfile is destroyed.
639
640 This is also used to work around a difference between the way gold
641 generates .gdb_index version <=7 and the way gdb does. Arguably this
642 is a gold bug. For symbols coming from TUs, gold records in the index
643 the CU that includes the TU instead of the TU itself. This breaks
644 dw2_lookup_symbol: It assumes that if the index says symbol X lives
645 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
646 will find X. Alas TUs live in their own symtab, so after expanding CU Y
647 we need to look in TU Z to find X. Fortunately, this is akin to
648 DW_TAG_imported_unit, so we just use the same mechanism: For
649 .gdb_index version <=7 this also records the TUs that the CU referred
650 to. Concurrently with this change gdb was modified to emit version 8
651 indices so we only pay a price for gold generated indices.
652 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
653 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
654 };
655
656 /* Entry in the signatured_types hash table. */
657
658 struct signatured_type
659 {
660 /* The "per_cu" object of this type.
661 This struct is used iff per_cu.is_debug_types.
662 N.B.: This is the first member so that it's easy to convert pointers
663 between them. */
664 struct dwarf2_per_cu_data per_cu;
665
666 /* The type's signature. */
667 ULONGEST signature;
668
669 /* Offset in the TU of the type's DIE, as read from the TU header.
670 If this TU is a DWO stub and the definition lives in a DWO file
671 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
672 cu_offset type_offset_in_tu;
673
674 /* Offset in the section of the type's DIE.
675 If the definition lives in a DWO file, this is the offset in the
676 .debug_types.dwo section.
677 The value is zero until the actual value is known.
678 Zero is otherwise not a valid section offset. */
679 sect_offset type_offset_in_section;
680
681 /* Type units are grouped by their DW_AT_stmt_list entry so that they
682 can share them. This points to the containing symtab. */
683 struct type_unit_group *type_unit_group;
684
685 /* The type.
686 The first time we encounter this type we fully read it in and install it
687 in the symbol tables. Subsequent times we only need the type. */
688 struct type *type;
689
690 /* Containing DWO unit.
691 This field is valid iff per_cu.reading_dwo_directly. */
692 struct dwo_unit *dwo_unit;
693 };
694
695 typedef struct signatured_type *sig_type_ptr;
696 DEF_VEC_P (sig_type_ptr);
697
698 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
699 This includes type_unit_group and quick_file_names. */
700
701 struct stmt_list_hash
702 {
703 /* The DWO unit this table is from or NULL if there is none. */
704 struct dwo_unit *dwo_unit;
705
706 /* Offset in .debug_line or .debug_line.dwo. */
707 sect_offset line_offset;
708 };
709
710 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
711 an object of this type. */
712
713 struct type_unit_group
714 {
715 /* dwarf2read.c's main "handle" on a TU symtab.
716 To simplify things we create an artificial CU that "includes" all the
717 type units using this stmt_list so that the rest of the code still has
718 a "per_cu" handle on the symtab.
719 This PER_CU is recognized by having no section. */
720 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
721 struct dwarf2_per_cu_data per_cu;
722
723 /* The TUs that share this DW_AT_stmt_list entry.
724 This is added to while parsing type units to build partial symtabs,
725 and is deleted afterwards and not used again. */
726 VEC (sig_type_ptr) *tus;
727
728 /* The compunit symtab.
729 Type units in a group needn't all be defined in the same source file,
730 so we create an essentially anonymous symtab as the compunit symtab. */
731 struct compunit_symtab *compunit_symtab;
732
733 /* The data used to construct the hash key. */
734 struct stmt_list_hash hash;
735
736 /* The number of symtabs from the line header.
737 The value here must match line_header.num_file_names. */
738 unsigned int num_symtabs;
739
740 /* The symbol tables for this TU (obtained from the files listed in
741 DW_AT_stmt_list).
742 WARNING: The order of entries here must match the order of entries
743 in the line header. After the first TU using this type_unit_group, the
744 line header for the subsequent TUs is recreated from this. This is done
745 because we need to use the same symtabs for each TU using the same
746 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
747 there's no guarantee the line header doesn't have duplicate entries. */
748 struct symtab **symtabs;
749 };
750
751 /* These sections are what may appear in a (real or virtual) DWO file. */
752
753 struct dwo_sections
754 {
755 struct dwarf2_section_info abbrev;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str;
761 struct dwarf2_section_info str_offsets;
762 /* In the case of a virtual DWO file, these two are unused. */
763 struct dwarf2_section_info info;
764 VEC (dwarf2_section_info_def) *types;
765 };
766
767 /* CUs/TUs in DWP/DWO files. */
768
769 struct dwo_unit
770 {
771 /* Backlink to the containing struct dwo_file. */
772 struct dwo_file *dwo_file;
773
774 /* The "id" that distinguishes this CU/TU.
775 .debug_info calls this "dwo_id", .debug_types calls this "signature".
776 Since signatures came first, we stick with it for consistency. */
777 ULONGEST signature;
778
779 /* The section this CU/TU lives in, in the DWO file. */
780 struct dwarf2_section_info *section;
781
782 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
783 sect_offset offset;
784 unsigned int length;
785
786 /* For types, offset in the type's DIE of the type defined by this TU. */
787 cu_offset type_offset_in_tu;
788 };
789
790 /* include/dwarf2.h defines the DWP section codes.
791 It defines a max value but it doesn't define a min value, which we
792 use for error checking, so provide one. */
793
794 enum dwp_v2_section_ids
795 {
796 DW_SECT_MIN = 1
797 };
798
799 /* Data for one DWO file.
800
801 This includes virtual DWO files (a virtual DWO file is a DWO file as it
802 appears in a DWP file). DWP files don't really have DWO files per se -
803 comdat folding of types "loses" the DWO file they came from, and from
804 a high level view DWP files appear to contain a mass of random types.
805 However, to maintain consistency with the non-DWP case we pretend DWP
806 files contain virtual DWO files, and we assign each TU with one virtual
807 DWO file (generally based on the line and abbrev section offsets -
808 a heuristic that seems to work in practice). */
809
810 struct dwo_file
811 {
812 /* The DW_AT_GNU_dwo_name attribute.
813 For virtual DWO files the name is constructed from the section offsets
814 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
815 from related CU+TUs. */
816 const char *dwo_name;
817
818 /* The DW_AT_comp_dir attribute. */
819 const char *comp_dir;
820
821 /* The bfd, when the file is open. Otherwise this is NULL.
822 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
823 bfd *dbfd;
824
825 /* The sections that make up this DWO file.
826 Remember that for virtual DWO files in DWP V2, these are virtual
827 sections (for lack of a better name). */
828 struct dwo_sections sections;
829
830 /* The CU in the file.
831 We only support one because having more than one requires hacking the
832 dwo_name of each to match, which is highly unlikely to happen.
833 Doing this means all TUs can share comp_dir: We also assume that
834 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
835 struct dwo_unit *cu;
836
837 /* Table of TUs in the file.
838 Each element is a struct dwo_unit. */
839 htab_t tus;
840 };
841
842 /* These sections are what may appear in a DWP file. */
843
844 struct dwp_sections
845 {
846 /* These are used by both DWP version 1 and 2. */
847 struct dwarf2_section_info str;
848 struct dwarf2_section_info cu_index;
849 struct dwarf2_section_info tu_index;
850
851 /* These are only used by DWP version 2 files.
852 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
853 sections are referenced by section number, and are not recorded here.
854 In DWP version 2 there is at most one copy of all these sections, each
855 section being (effectively) comprised of the concatenation of all of the
856 individual sections that exist in the version 1 format.
857 To keep the code simple we treat each of these concatenated pieces as a
858 section itself (a virtual section?). */
859 struct dwarf2_section_info abbrev;
860 struct dwarf2_section_info info;
861 struct dwarf2_section_info line;
862 struct dwarf2_section_info loc;
863 struct dwarf2_section_info macinfo;
864 struct dwarf2_section_info macro;
865 struct dwarf2_section_info str_offsets;
866 struct dwarf2_section_info types;
867 };
868
869 /* These sections are what may appear in a virtual DWO file in DWP version 1.
870 A virtual DWO file is a DWO file as it appears in a DWP file. */
871
872 struct virtual_v1_dwo_sections
873 {
874 struct dwarf2_section_info abbrev;
875 struct dwarf2_section_info line;
876 struct dwarf2_section_info loc;
877 struct dwarf2_section_info macinfo;
878 struct dwarf2_section_info macro;
879 struct dwarf2_section_info str_offsets;
880 /* Each DWP hash table entry records one CU or one TU.
881 That is recorded here, and copied to dwo_unit.section. */
882 struct dwarf2_section_info info_or_types;
883 };
884
885 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
886 In version 2, the sections of the DWO files are concatenated together
887 and stored in one section of that name. Thus each ELF section contains
888 several "virtual" sections. */
889
890 struct virtual_v2_dwo_sections
891 {
892 bfd_size_type abbrev_offset;
893 bfd_size_type abbrev_size;
894
895 bfd_size_type line_offset;
896 bfd_size_type line_size;
897
898 bfd_size_type loc_offset;
899 bfd_size_type loc_size;
900
901 bfd_size_type macinfo_offset;
902 bfd_size_type macinfo_size;
903
904 bfd_size_type macro_offset;
905 bfd_size_type macro_size;
906
907 bfd_size_type str_offsets_offset;
908 bfd_size_type str_offsets_size;
909
910 /* Each DWP hash table entry records one CU or one TU.
911 That is recorded here, and copied to dwo_unit.section. */
912 bfd_size_type info_or_types_offset;
913 bfd_size_type info_or_types_size;
914 };
915
916 /* Contents of DWP hash tables. */
917
918 struct dwp_hash_table
919 {
920 uint32_t version, nr_columns;
921 uint32_t nr_units, nr_slots;
922 const gdb_byte *hash_table, *unit_table;
923 union
924 {
925 struct
926 {
927 const gdb_byte *indices;
928 } v1;
929 struct
930 {
931 /* This is indexed by column number and gives the id of the section
932 in that column. */
933 #define MAX_NR_V2_DWO_SECTIONS \
934 (1 /* .debug_info or .debug_types */ \
935 + 1 /* .debug_abbrev */ \
936 + 1 /* .debug_line */ \
937 + 1 /* .debug_loc */ \
938 + 1 /* .debug_str_offsets */ \
939 + 1 /* .debug_macro or .debug_macinfo */)
940 int section_ids[MAX_NR_V2_DWO_SECTIONS];
941 const gdb_byte *offsets;
942 const gdb_byte *sizes;
943 } v2;
944 } section_pool;
945 };
946
947 /* Data for one DWP file. */
948
949 struct dwp_file
950 {
951 /* Name of the file. */
952 const char *name;
953
954 /* File format version. */
955 int version;
956
957 /* The bfd. */
958 bfd *dbfd;
959
960 /* Section info for this file. */
961 struct dwp_sections sections;
962
963 /* Table of CUs in the file. */
964 const struct dwp_hash_table *cus;
965
966 /* Table of TUs in the file. */
967 const struct dwp_hash_table *tus;
968
969 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
970 htab_t loaded_cus;
971 htab_t loaded_tus;
972
973 /* Table to map ELF section numbers to their sections.
974 This is only needed for the DWP V1 file format. */
975 unsigned int num_sections;
976 asection **elf_sections;
977 };
978
979 /* This represents a '.dwz' file. */
980
981 struct dwz_file
982 {
983 /* A dwz file can only contain a few sections. */
984 struct dwarf2_section_info abbrev;
985 struct dwarf2_section_info info;
986 struct dwarf2_section_info str;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info macro;
989 struct dwarf2_section_info gdb_index;
990
991 /* The dwz's BFD. */
992 bfd *dwz_bfd;
993 };
994
995 /* Struct used to pass misc. parameters to read_die_and_children, et
996 al. which are used for both .debug_info and .debug_types dies.
997 All parameters here are unchanging for the life of the call. This
998 struct exists to abstract away the constant parameters of die reading. */
999
1000 struct die_reader_specs
1001 {
1002 /* The bfd of die_section. */
1003 bfd* abfd;
1004
1005 /* The CU of the DIE we are parsing. */
1006 struct dwarf2_cu *cu;
1007
1008 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1009 struct dwo_file *dwo_file;
1010
1011 /* The section the die comes from.
1012 This is either .debug_info or .debug_types, or the .dwo variants. */
1013 struct dwarf2_section_info *die_section;
1014
1015 /* die_section->buffer. */
1016 const gdb_byte *buffer;
1017
1018 /* The end of the buffer. */
1019 const gdb_byte *buffer_end;
1020
1021 /* The value of the DW_AT_comp_dir attribute. */
1022 const char *comp_dir;
1023 };
1024
1025 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1026 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1027 const gdb_byte *info_ptr,
1028 struct die_info *comp_unit_die,
1029 int has_children,
1030 void *data);
1031
1032 struct file_entry
1033 {
1034 const char *name;
1035 unsigned int dir_index;
1036 unsigned int mod_time;
1037 unsigned int length;
1038 /* Non-zero if referenced by the Line Number Program. */
1039 int included_p;
1040 /* The associated symbol table, if any. */
1041 struct symtab *symtab;
1042 };
1043
1044 /* The line number information for a compilation unit (found in the
1045 .debug_line section) begins with a "statement program header",
1046 which contains the following information. */
1047 struct line_header
1048 {
1049 /* Offset of line number information in .debug_line section. */
1050 sect_offset offset;
1051
1052 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1053 unsigned offset_in_dwz : 1;
1054
1055 unsigned int total_length;
1056 unsigned short version;
1057 unsigned int header_length;
1058 unsigned char minimum_instruction_length;
1059 unsigned char maximum_ops_per_instruction;
1060 unsigned char default_is_stmt;
1061 int line_base;
1062 unsigned char line_range;
1063 unsigned char opcode_base;
1064
1065 /* standard_opcode_lengths[i] is the number of operands for the
1066 standard opcode whose value is i. This means that
1067 standard_opcode_lengths[0] is unused, and the last meaningful
1068 element is standard_opcode_lengths[opcode_base - 1]. */
1069 unsigned char *standard_opcode_lengths;
1070
1071 /* The include_directories table. NOTE! These strings are not
1072 allocated with xmalloc; instead, they are pointers into
1073 debug_line_buffer. If you try to free them, `free' will get
1074 indigestion. */
1075 unsigned int num_include_dirs, include_dirs_size;
1076 const char **include_dirs;
1077
1078 /* The file_names table. NOTE! These strings are not allocated
1079 with xmalloc; instead, they are pointers into debug_line_buffer.
1080 Don't try to free them directly. */
1081 unsigned int num_file_names, file_names_size;
1082 struct file_entry *file_names;
1083
1084 /* The start and end of the statement program following this
1085 header. These point into dwarf2_per_objfile->line_buffer. */
1086 const gdb_byte *statement_program_start, *statement_program_end;
1087 };
1088
1089 /* When we construct a partial symbol table entry we only
1090 need this much information. */
1091 struct partial_die_info
1092 {
1093 /* Offset of this DIE. */
1094 sect_offset offset;
1095
1096 /* DWARF-2 tag for this DIE. */
1097 ENUM_BITFIELD(dwarf_tag) tag : 16;
1098
1099 /* Assorted flags describing the data found in this DIE. */
1100 unsigned int has_children : 1;
1101 unsigned int is_external : 1;
1102 unsigned int is_declaration : 1;
1103 unsigned int has_type : 1;
1104 unsigned int has_specification : 1;
1105 unsigned int has_pc_info : 1;
1106 unsigned int may_be_inlined : 1;
1107
1108 /* Flag set if the SCOPE field of this structure has been
1109 computed. */
1110 unsigned int scope_set : 1;
1111
1112 /* Flag set if the DIE has a byte_size attribute. */
1113 unsigned int has_byte_size : 1;
1114
1115 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1116 unsigned int has_const_value : 1;
1117
1118 /* Flag set if any of the DIE's children are template arguments. */
1119 unsigned int has_template_arguments : 1;
1120
1121 /* Flag set if fixup_partial_die has been called on this die. */
1122 unsigned int fixup_called : 1;
1123
1124 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1125 unsigned int is_dwz : 1;
1126
1127 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1128 unsigned int spec_is_dwz : 1;
1129
1130 /* The name of this DIE. Normally the value of DW_AT_name, but
1131 sometimes a default name for unnamed DIEs. */
1132 const char *name;
1133
1134 /* The linkage name, if present. */
1135 const char *linkage_name;
1136
1137 /* The scope to prepend to our children. This is generally
1138 allocated on the comp_unit_obstack, so will disappear
1139 when this compilation unit leaves the cache. */
1140 const char *scope;
1141
1142 /* Some data associated with the partial DIE. The tag determines
1143 which field is live. */
1144 union
1145 {
1146 /* The location description associated with this DIE, if any. */
1147 struct dwarf_block *locdesc;
1148 /* The offset of an import, for DW_TAG_imported_unit. */
1149 sect_offset offset;
1150 } d;
1151
1152 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1153 CORE_ADDR lowpc;
1154 CORE_ADDR highpc;
1155
1156 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1157 DW_AT_sibling, if any. */
1158 /* NOTE: This member isn't strictly necessary, read_partial_die could
1159 return DW_AT_sibling values to its caller load_partial_dies. */
1160 const gdb_byte *sibling;
1161
1162 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1163 DW_AT_specification (or DW_AT_abstract_origin or
1164 DW_AT_extension). */
1165 sect_offset spec_offset;
1166
1167 /* Pointers to this DIE's parent, first child, and next sibling,
1168 if any. */
1169 struct partial_die_info *die_parent, *die_child, *die_sibling;
1170 };
1171
1172 /* This data structure holds the information of an abbrev. */
1173 struct abbrev_info
1174 {
1175 unsigned int number; /* number identifying abbrev */
1176 enum dwarf_tag tag; /* dwarf tag */
1177 unsigned short has_children; /* boolean */
1178 unsigned short num_attrs; /* number of attributes */
1179 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1180 struct abbrev_info *next; /* next in chain */
1181 };
1182
1183 struct attr_abbrev
1184 {
1185 ENUM_BITFIELD(dwarf_attribute) name : 16;
1186 ENUM_BITFIELD(dwarf_form) form : 16;
1187 };
1188
1189 /* Size of abbrev_table.abbrev_hash_table. */
1190 #define ABBREV_HASH_SIZE 121
1191
1192 /* Top level data structure to contain an abbreviation table. */
1193
1194 struct abbrev_table
1195 {
1196 /* Where the abbrev table came from.
1197 This is used as a sanity check when the table is used. */
1198 sect_offset offset;
1199
1200 /* Storage for the abbrev table. */
1201 struct obstack abbrev_obstack;
1202
1203 /* Hash table of abbrevs.
1204 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1205 It could be statically allocated, but the previous code didn't so we
1206 don't either. */
1207 struct abbrev_info **abbrevs;
1208 };
1209
1210 /* Attributes have a name and a value. */
1211 struct attribute
1212 {
1213 ENUM_BITFIELD(dwarf_attribute) name : 16;
1214 ENUM_BITFIELD(dwarf_form) form : 15;
1215
1216 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1217 field should be in u.str (existing only for DW_STRING) but it is kept
1218 here for better struct attribute alignment. */
1219 unsigned int string_is_canonical : 1;
1220
1221 union
1222 {
1223 const char *str;
1224 struct dwarf_block *blk;
1225 ULONGEST unsnd;
1226 LONGEST snd;
1227 CORE_ADDR addr;
1228 ULONGEST signature;
1229 }
1230 u;
1231 };
1232
1233 /* This data structure holds a complete die structure. */
1234 struct die_info
1235 {
1236 /* DWARF-2 tag for this DIE. */
1237 ENUM_BITFIELD(dwarf_tag) tag : 16;
1238
1239 /* Number of attributes */
1240 unsigned char num_attrs;
1241
1242 /* True if we're presently building the full type name for the
1243 type derived from this DIE. */
1244 unsigned char building_fullname : 1;
1245
1246 /* True if this die is in process. PR 16581. */
1247 unsigned char in_process : 1;
1248
1249 /* Abbrev number */
1250 unsigned int abbrev;
1251
1252 /* Offset in .debug_info or .debug_types section. */
1253 sect_offset offset;
1254
1255 /* The dies in a compilation unit form an n-ary tree. PARENT
1256 points to this die's parent; CHILD points to the first child of
1257 this node; and all the children of a given node are chained
1258 together via their SIBLING fields. */
1259 struct die_info *child; /* Its first child, if any. */
1260 struct die_info *sibling; /* Its next sibling, if any. */
1261 struct die_info *parent; /* Its parent, if any. */
1262
1263 /* An array of attributes, with NUM_ATTRS elements. There may be
1264 zero, but it's not common and zero-sized arrays are not
1265 sufficiently portable C. */
1266 struct attribute attrs[1];
1267 };
1268
1269 /* Get at parts of an attribute structure. */
1270
1271 #define DW_STRING(attr) ((attr)->u.str)
1272 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1273 #define DW_UNSND(attr) ((attr)->u.unsnd)
1274 #define DW_BLOCK(attr) ((attr)->u.blk)
1275 #define DW_SND(attr) ((attr)->u.snd)
1276 #define DW_ADDR(attr) ((attr)->u.addr)
1277 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1278
1279 /* Blocks are a bunch of untyped bytes. */
1280 struct dwarf_block
1281 {
1282 size_t size;
1283
1284 /* Valid only if SIZE is not zero. */
1285 const gdb_byte *data;
1286 };
1287
1288 #ifndef ATTR_ALLOC_CHUNK
1289 #define ATTR_ALLOC_CHUNK 4
1290 #endif
1291
1292 /* Allocate fields for structs, unions and enums in this size. */
1293 #ifndef DW_FIELD_ALLOC_CHUNK
1294 #define DW_FIELD_ALLOC_CHUNK 4
1295 #endif
1296
1297 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1298 but this would require a corresponding change in unpack_field_as_long
1299 and friends. */
1300 static int bits_per_byte = 8;
1301
1302 struct nextfield
1303 {
1304 struct nextfield *next;
1305 int accessibility;
1306 int virtuality;
1307 struct field field;
1308 };
1309
1310 struct nextfnfield
1311 {
1312 struct nextfnfield *next;
1313 struct fn_field fnfield;
1314 };
1315
1316 struct fnfieldlist
1317 {
1318 const char *name;
1319 int length;
1320 struct nextfnfield *head;
1321 };
1322
1323 struct typedef_field_list
1324 {
1325 struct typedef_field field;
1326 struct typedef_field_list *next;
1327 };
1328
1329 /* The routines that read and process dies for a C struct or C++ class
1330 pass lists of data member fields and lists of member function fields
1331 in an instance of a field_info structure, as defined below. */
1332 struct field_info
1333 {
1334 /* List of data member and baseclasses fields. */
1335 struct nextfield *fields, *baseclasses;
1336
1337 /* Number of fields (including baseclasses). */
1338 int nfields;
1339
1340 /* Number of baseclasses. */
1341 int nbaseclasses;
1342
1343 /* Set if the accesibility of one of the fields is not public. */
1344 int non_public_fields;
1345
1346 /* Member function fields array, entries are allocated in the order they
1347 are encountered in the object file. */
1348 struct nextfnfield *fnfields;
1349
1350 /* Member function fieldlist array, contains name of possibly overloaded
1351 member function, number of overloaded member functions and a pointer
1352 to the head of the member function field chain. */
1353 struct fnfieldlist *fnfieldlists;
1354
1355 /* Number of entries in the fnfieldlists array. */
1356 int nfnfields;
1357
1358 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1359 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1360 struct typedef_field_list *typedef_field_list;
1361 unsigned typedef_field_list_count;
1362 };
1363
1364 /* One item on the queue of compilation units to read in full symbols
1365 for. */
1366 struct dwarf2_queue_item
1367 {
1368 struct dwarf2_per_cu_data *per_cu;
1369 enum language pretend_language;
1370 struct dwarf2_queue_item *next;
1371 };
1372
1373 /* The current queue. */
1374 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1375
1376 /* Loaded secondary compilation units are kept in memory until they
1377 have not been referenced for the processing of this many
1378 compilation units. Set this to zero to disable caching. Cache
1379 sizes of up to at least twenty will improve startup time for
1380 typical inter-CU-reference binaries, at an obvious memory cost. */
1381 static int dwarf_max_cache_age = 5;
1382 static void
1383 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1384 struct cmd_list_element *c, const char *value)
1385 {
1386 fprintf_filtered (file, _("The upper bound on the age of cached "
1387 "DWARF compilation units is %s.\n"),
1388 value);
1389 }
1390 \f
1391 /* local function prototypes */
1392
1393 static const char *get_section_name (const struct dwarf2_section_info *);
1394
1395 static const char *get_section_file_name (const struct dwarf2_section_info *);
1396
1397 static void dwarf2_locate_sections (bfd *, asection *, void *);
1398
1399 static void dwarf2_find_base_address (struct die_info *die,
1400 struct dwarf2_cu *cu);
1401
1402 static struct partial_symtab *create_partial_symtab
1403 (struct dwarf2_per_cu_data *per_cu, const char *name);
1404
1405 static void dwarf2_build_psymtabs_hard (struct objfile *);
1406
1407 static void scan_partial_symbols (struct partial_die_info *,
1408 CORE_ADDR *, CORE_ADDR *,
1409 int, struct dwarf2_cu *);
1410
1411 static void add_partial_symbol (struct partial_die_info *,
1412 struct dwarf2_cu *);
1413
1414 static void add_partial_namespace (struct partial_die_info *pdi,
1415 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1416 int set_addrmap, struct dwarf2_cu *cu);
1417
1418 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1419 CORE_ADDR *highpc, int set_addrmap,
1420 struct dwarf2_cu *cu);
1421
1422 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1423 struct dwarf2_cu *cu);
1424
1425 static void add_partial_subprogram (struct partial_die_info *pdi,
1426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1427 int need_pc, struct dwarf2_cu *cu);
1428
1429 static void dwarf2_read_symtab (struct partial_symtab *,
1430 struct objfile *);
1431
1432 static void psymtab_to_symtab_1 (struct partial_symtab *);
1433
1434 static struct abbrev_info *abbrev_table_lookup_abbrev
1435 (const struct abbrev_table *, unsigned int);
1436
1437 static struct abbrev_table *abbrev_table_read_table
1438 (struct dwarf2_section_info *, sect_offset);
1439
1440 static void abbrev_table_free (struct abbrev_table *);
1441
1442 static void abbrev_table_free_cleanup (void *);
1443
1444 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1445 struct dwarf2_section_info *);
1446
1447 static void dwarf2_free_abbrev_table (void *);
1448
1449 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1450
1451 static struct partial_die_info *load_partial_dies
1452 (const struct die_reader_specs *, const gdb_byte *, int);
1453
1454 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1455 struct partial_die_info *,
1456 struct abbrev_info *,
1457 unsigned int,
1458 const gdb_byte *);
1459
1460 static struct partial_die_info *find_partial_die (sect_offset, int,
1461 struct dwarf2_cu *);
1462
1463 static void fixup_partial_die (struct partial_die_info *,
1464 struct dwarf2_cu *);
1465
1466 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1467 struct attribute *, struct attr_abbrev *,
1468 const gdb_byte *);
1469
1470 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1471
1472 static int read_1_signed_byte (bfd *, const gdb_byte *);
1473
1474 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1475
1476 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1477
1478 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1479
1480 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1481 unsigned int *);
1482
1483 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1484
1485 static LONGEST read_checked_initial_length_and_offset
1486 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1487 unsigned int *, unsigned int *);
1488
1489 static LONGEST read_offset (bfd *, const gdb_byte *,
1490 const struct comp_unit_head *,
1491 unsigned int *);
1492
1493 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1494
1495 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1496 sect_offset);
1497
1498 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1499
1500 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1501
1502 static const char *read_indirect_string (bfd *, const gdb_byte *,
1503 const struct comp_unit_head *,
1504 unsigned int *);
1505
1506 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1507
1508 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1509
1510 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1511
1512 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1513 const gdb_byte *,
1514 unsigned int *);
1515
1516 static const char *read_str_index (const struct die_reader_specs *reader,
1517 ULONGEST str_index);
1518
1519 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1520
1521 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1522 struct dwarf2_cu *);
1523
1524 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1525 unsigned int);
1526
1527 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1528 struct dwarf2_cu *cu);
1529
1530 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1531 struct dwarf2_cu *cu);
1532
1533 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1534
1535 static struct die_info *die_specification (struct die_info *die,
1536 struct dwarf2_cu **);
1537
1538 static void free_line_header (struct line_header *lh);
1539
1540 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1541 struct dwarf2_cu *cu);
1542
1543 static void dwarf_decode_lines (struct line_header *, const char *,
1544 struct dwarf2_cu *, struct partial_symtab *,
1545 CORE_ADDR, int decode_mapping);
1546
1547 static void dwarf2_start_subfile (const char *, const char *);
1548
1549 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1550 const char *, const char *,
1551 CORE_ADDR);
1552
1553 static struct symbol *new_symbol (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1557 struct dwarf2_cu *, struct symbol *);
1558
1559 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1560 struct dwarf2_cu *);
1561
1562 static void dwarf2_const_value_attr (const struct attribute *attr,
1563 struct type *type,
1564 const char *name,
1565 struct obstack *obstack,
1566 struct dwarf2_cu *cu, LONGEST *value,
1567 const gdb_byte **bytes,
1568 struct dwarf2_locexpr_baton **baton);
1569
1570 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1571
1572 static int need_gnat_info (struct dwarf2_cu *);
1573
1574 static struct type *die_descriptive_type (struct die_info *,
1575 struct dwarf2_cu *);
1576
1577 static void set_descriptive_type (struct type *, struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct type *die_containing_type (struct die_info *,
1581 struct dwarf2_cu *);
1582
1583 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1584 struct dwarf2_cu *);
1585
1586 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1587
1588 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1589
1590 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1591
1592 static char *typename_concat (struct obstack *obs, const char *prefix,
1593 const char *suffix, int physname,
1594 struct dwarf2_cu *cu);
1595
1596 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1597
1598 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1599
1600 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1601
1602 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1603
1604 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1605
1606 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1607 struct dwarf2_cu *, struct partial_symtab *);
1608
1609 static int dwarf2_get_pc_bounds (struct die_info *,
1610 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1611 struct partial_symtab *);
1612
1613 static void get_scope_pc_bounds (struct die_info *,
1614 CORE_ADDR *, CORE_ADDR *,
1615 struct dwarf2_cu *);
1616
1617 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1618 CORE_ADDR, struct dwarf2_cu *);
1619
1620 static void dwarf2_add_field (struct field_info *, struct die_info *,
1621 struct dwarf2_cu *);
1622
1623 static void dwarf2_attach_fields_to_type (struct field_info *,
1624 struct type *, struct dwarf2_cu *);
1625
1626 static void dwarf2_add_member_fn (struct field_info *,
1627 struct die_info *, struct type *,
1628 struct dwarf2_cu *);
1629
1630 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1631 struct type *,
1632 struct dwarf2_cu *);
1633
1634 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1635
1636 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1637
1638 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1639
1640 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1641
1642 static struct using_direct **using_directives (enum language);
1643
1644 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1645
1646 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1647
1648 static struct type *read_module_type (struct die_info *die,
1649 struct dwarf2_cu *cu);
1650
1651 static const char *namespace_name (struct die_info *die,
1652 int *is_anonymous, struct dwarf2_cu *);
1653
1654 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1655
1656 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1657
1658 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1659 struct dwarf2_cu *);
1660
1661 static struct die_info *read_die_and_siblings_1
1662 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1663 struct die_info *);
1664
1665 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1666 const gdb_byte *info_ptr,
1667 const gdb_byte **new_info_ptr,
1668 struct die_info *parent);
1669
1670 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1671 struct die_info **, const gdb_byte *,
1672 int *, int);
1673
1674 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1675 struct die_info **, const gdb_byte *,
1676 int *);
1677
1678 static void process_die (struct die_info *, struct dwarf2_cu *);
1679
1680 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1681 struct obstack *);
1682
1683 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1684
1685 static const char *dwarf2_full_name (const char *name,
1686 struct die_info *die,
1687 struct dwarf2_cu *cu);
1688
1689 static const char *dwarf2_physname (const char *name, struct die_info *die,
1690 struct dwarf2_cu *cu);
1691
1692 static struct die_info *dwarf2_extension (struct die_info *die,
1693 struct dwarf2_cu **);
1694
1695 static const char *dwarf_tag_name (unsigned int);
1696
1697 static const char *dwarf_attr_name (unsigned int);
1698
1699 static const char *dwarf_form_name (unsigned int);
1700
1701 static char *dwarf_bool_name (unsigned int);
1702
1703 static const char *dwarf_type_encoding_name (unsigned int);
1704
1705 static struct die_info *sibling_die (struct die_info *);
1706
1707 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1708
1709 static void dump_die_for_error (struct die_info *);
1710
1711 static void dump_die_1 (struct ui_file *, int level, int max_level,
1712 struct die_info *);
1713
1714 /*static*/ void dump_die (struct die_info *, int max_level);
1715
1716 static void store_in_ref_table (struct die_info *,
1717 struct dwarf2_cu *);
1718
1719 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1720
1721 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1722
1723 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1724 const struct attribute *,
1725 struct dwarf2_cu **);
1726
1727 static struct die_info *follow_die_ref (struct die_info *,
1728 const struct attribute *,
1729 struct dwarf2_cu **);
1730
1731 static struct die_info *follow_die_sig (struct die_info *,
1732 const struct attribute *,
1733 struct dwarf2_cu **);
1734
1735 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1736 struct dwarf2_cu *);
1737
1738 static struct type *get_DW_AT_signature_type (struct die_info *,
1739 const struct attribute *,
1740 struct dwarf2_cu *);
1741
1742 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1743
1744 static void read_signatured_type (struct signatured_type *);
1745
1746 /* memory allocation interface */
1747
1748 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1749
1750 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1751
1752 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1753
1754 static int attr_form_is_block (const struct attribute *);
1755
1756 static int attr_form_is_section_offset (const struct attribute *);
1757
1758 static int attr_form_is_constant (const struct attribute *);
1759
1760 static int attr_form_is_ref (const struct attribute *);
1761
1762 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1763 struct dwarf2_loclist_baton *baton,
1764 const struct attribute *attr);
1765
1766 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1767 struct symbol *sym,
1768 struct dwarf2_cu *cu,
1769 int is_block);
1770
1771 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1772 const gdb_byte *info_ptr,
1773 struct abbrev_info *abbrev);
1774
1775 static void free_stack_comp_unit (void *);
1776
1777 static hashval_t partial_die_hash (const void *item);
1778
1779 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1780
1781 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1782 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1783
1784 static void init_one_comp_unit (struct dwarf2_cu *cu,
1785 struct dwarf2_per_cu_data *per_cu);
1786
1787 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1788 struct die_info *comp_unit_die,
1789 enum language pretend_language);
1790
1791 static void free_heap_comp_unit (void *);
1792
1793 static void free_cached_comp_units (void *);
1794
1795 static void age_cached_comp_units (void);
1796
1797 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1798
1799 static struct type *set_die_type (struct die_info *, struct type *,
1800 struct dwarf2_cu *);
1801
1802 static void create_all_comp_units (struct objfile *);
1803
1804 static int create_all_type_units (struct objfile *);
1805
1806 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1807 enum language);
1808
1809 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1810 enum language);
1811
1812 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1813 enum language);
1814
1815 static void dwarf2_add_dependence (struct dwarf2_cu *,
1816 struct dwarf2_per_cu_data *);
1817
1818 static void dwarf2_mark (struct dwarf2_cu *);
1819
1820 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1821
1822 static struct type *get_die_type_at_offset (sect_offset,
1823 struct dwarf2_per_cu_data *);
1824
1825 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1826
1827 static void dwarf2_release_queue (void *dummy);
1828
1829 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1830 enum language pretend_language);
1831
1832 static void process_queue (void);
1833
1834 static void find_file_and_directory (struct die_info *die,
1835 struct dwarf2_cu *cu,
1836 const char **name, const char **comp_dir);
1837
1838 static char *file_full_name (int file, struct line_header *lh,
1839 const char *comp_dir);
1840
1841 static const gdb_byte *read_and_check_comp_unit_head
1842 (struct comp_unit_head *header,
1843 struct dwarf2_section_info *section,
1844 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1845 int is_debug_types_section);
1846
1847 static void init_cutu_and_read_dies
1848 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1849 int use_existing_cu, int keep,
1850 die_reader_func_ftype *die_reader_func, void *data);
1851
1852 static void init_cutu_and_read_dies_simple
1853 (struct dwarf2_per_cu_data *this_cu,
1854 die_reader_func_ftype *die_reader_func, void *data);
1855
1856 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1857
1858 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1859
1860 static struct dwo_unit *lookup_dwo_unit_in_dwp
1861 (struct dwp_file *dwp_file, const char *comp_dir,
1862 ULONGEST signature, int is_debug_types);
1863
1864 static struct dwp_file *get_dwp_file (void);
1865
1866 static struct dwo_unit *lookup_dwo_comp_unit
1867 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1868
1869 static struct dwo_unit *lookup_dwo_type_unit
1870 (struct signatured_type *, const char *, const char *);
1871
1872 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1873
1874 static void free_dwo_file_cleanup (void *);
1875
1876 static void process_cu_includes (void);
1877
1878 static void check_producer (struct dwarf2_cu *cu);
1879
1880 static void free_line_header_voidp (void *arg);
1881 \f
1882 /* Various complaints about symbol reading that don't abort the process. */
1883
1884 static void
1885 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1886 {
1887 complaint (&symfile_complaints,
1888 _("statement list doesn't fit in .debug_line section"));
1889 }
1890
1891 static void
1892 dwarf2_debug_line_missing_file_complaint (void)
1893 {
1894 complaint (&symfile_complaints,
1895 _(".debug_line section has line data without a file"));
1896 }
1897
1898 static void
1899 dwarf2_debug_line_missing_end_sequence_complaint (void)
1900 {
1901 complaint (&symfile_complaints,
1902 _(".debug_line section has line "
1903 "program sequence without an end"));
1904 }
1905
1906 static void
1907 dwarf2_complex_location_expr_complaint (void)
1908 {
1909 complaint (&symfile_complaints, _("location expression too complex"));
1910 }
1911
1912 static void
1913 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1914 int arg3)
1915 {
1916 complaint (&symfile_complaints,
1917 _("const value length mismatch for '%s', got %d, expected %d"),
1918 arg1, arg2, arg3);
1919 }
1920
1921 static void
1922 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1923 {
1924 complaint (&symfile_complaints,
1925 _("debug info runs off end of %s section"
1926 " [in module %s]"),
1927 get_section_name (section),
1928 get_section_file_name (section));
1929 }
1930
1931 static void
1932 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1933 {
1934 complaint (&symfile_complaints,
1935 _("macro debug info contains a "
1936 "malformed macro definition:\n`%s'"),
1937 arg1);
1938 }
1939
1940 static void
1941 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1942 {
1943 complaint (&symfile_complaints,
1944 _("invalid attribute class or form for '%s' in '%s'"),
1945 arg1, arg2);
1946 }
1947
1948 /* Hash function for line_header_hash. */
1949
1950 static hashval_t
1951 line_header_hash (const struct line_header *ofs)
1952 {
1953 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
1954 }
1955
1956 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1957
1958 static hashval_t
1959 line_header_hash_voidp (const void *item)
1960 {
1961 const struct line_header *ofs = item;
1962
1963 return line_header_hash (ofs);
1964 }
1965
1966 /* Equality function for line_header_hash. */
1967
1968 static int
1969 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1970 {
1971 const struct line_header *ofs_lhs = item_lhs;
1972 const struct line_header *ofs_rhs = item_rhs;
1973
1974 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
1975 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1976 }
1977
1978 \f
1979 #if WORDS_BIGENDIAN
1980
1981 /* Convert VALUE between big- and little-endian. */
1982 static offset_type
1983 byte_swap (offset_type value)
1984 {
1985 offset_type result;
1986
1987 result = (value & 0xff) << 24;
1988 result |= (value & 0xff00) << 8;
1989 result |= (value & 0xff0000) >> 8;
1990 result |= (value & 0xff000000) >> 24;
1991 return result;
1992 }
1993
1994 #define MAYBE_SWAP(V) byte_swap (V)
1995
1996 #else
1997 #define MAYBE_SWAP(V) (V)
1998 #endif /* WORDS_BIGENDIAN */
1999
2000 /* Read the given attribute value as an address, taking the attribute's
2001 form into account. */
2002
2003 static CORE_ADDR
2004 attr_value_as_address (struct attribute *attr)
2005 {
2006 CORE_ADDR addr;
2007
2008 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2009 {
2010 /* Aside from a few clearly defined exceptions, attributes that
2011 contain an address must always be in DW_FORM_addr form.
2012 Unfortunately, some compilers happen to be violating this
2013 requirement by encoding addresses using other forms, such
2014 as DW_FORM_data4 for example. For those broken compilers,
2015 we try to do our best, without any guarantee of success,
2016 to interpret the address correctly. It would also be nice
2017 to generate a complaint, but that would require us to maintain
2018 a list of legitimate cases where a non-address form is allowed,
2019 as well as update callers to pass in at least the CU's DWARF
2020 version. This is more overhead than what we're willing to
2021 expand for a pretty rare case. */
2022 addr = DW_UNSND (attr);
2023 }
2024 else
2025 addr = DW_ADDR (attr);
2026
2027 return addr;
2028 }
2029
2030 /* The suffix for an index file. */
2031 #define INDEX_SUFFIX ".gdb-index"
2032
2033 /* Try to locate the sections we need for DWARF 2 debugging
2034 information and return true if we have enough to do something.
2035 NAMES points to the dwarf2 section names, or is NULL if the standard
2036 ELF names are used. */
2037
2038 int
2039 dwarf2_has_info (struct objfile *objfile,
2040 const struct dwarf2_debug_sections *names)
2041 {
2042 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2043 if (!dwarf2_per_objfile)
2044 {
2045 /* Initialize per-objfile state. */
2046 struct dwarf2_per_objfile *data
2047 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
2048
2049 memset (data, 0, sizeof (*data));
2050 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2051 dwarf2_per_objfile = data;
2052
2053 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2054 (void *) names);
2055 dwarf2_per_objfile->objfile = objfile;
2056 }
2057 return (!dwarf2_per_objfile->info.is_virtual
2058 && dwarf2_per_objfile->info.s.section != NULL
2059 && !dwarf2_per_objfile->abbrev.is_virtual
2060 && dwarf2_per_objfile->abbrev.s.section != NULL);
2061 }
2062
2063 /* Return the containing section of virtual section SECTION. */
2064
2065 static struct dwarf2_section_info *
2066 get_containing_section (const struct dwarf2_section_info *section)
2067 {
2068 gdb_assert (section->is_virtual);
2069 return section->s.containing_section;
2070 }
2071
2072 /* Return the bfd owner of SECTION. */
2073
2074 static struct bfd *
2075 get_section_bfd_owner (const struct dwarf2_section_info *section)
2076 {
2077 if (section->is_virtual)
2078 {
2079 section = get_containing_section (section);
2080 gdb_assert (!section->is_virtual);
2081 }
2082 return section->s.section->owner;
2083 }
2084
2085 /* Return the bfd section of SECTION.
2086 Returns NULL if the section is not present. */
2087
2088 static asection *
2089 get_section_bfd_section (const struct dwarf2_section_info *section)
2090 {
2091 if (section->is_virtual)
2092 {
2093 section = get_containing_section (section);
2094 gdb_assert (!section->is_virtual);
2095 }
2096 return section->s.section;
2097 }
2098
2099 /* Return the name of SECTION. */
2100
2101 static const char *
2102 get_section_name (const struct dwarf2_section_info *section)
2103 {
2104 asection *sectp = get_section_bfd_section (section);
2105
2106 gdb_assert (sectp != NULL);
2107 return bfd_section_name (get_section_bfd_owner (section), sectp);
2108 }
2109
2110 /* Return the name of the file SECTION is in. */
2111
2112 static const char *
2113 get_section_file_name (const struct dwarf2_section_info *section)
2114 {
2115 bfd *abfd = get_section_bfd_owner (section);
2116
2117 return bfd_get_filename (abfd);
2118 }
2119
2120 /* Return the id of SECTION.
2121 Returns 0 if SECTION doesn't exist. */
2122
2123 static int
2124 get_section_id (const struct dwarf2_section_info *section)
2125 {
2126 asection *sectp = get_section_bfd_section (section);
2127
2128 if (sectp == NULL)
2129 return 0;
2130 return sectp->id;
2131 }
2132
2133 /* Return the flags of SECTION.
2134 SECTION (or containing section if this is a virtual section) must exist. */
2135
2136 static int
2137 get_section_flags (const struct dwarf2_section_info *section)
2138 {
2139 asection *sectp = get_section_bfd_section (section);
2140
2141 gdb_assert (sectp != NULL);
2142 return bfd_get_section_flags (sectp->owner, sectp);
2143 }
2144
2145 /* When loading sections, we look either for uncompressed section or for
2146 compressed section names. */
2147
2148 static int
2149 section_is_p (const char *section_name,
2150 const struct dwarf2_section_names *names)
2151 {
2152 if (names->normal != NULL
2153 && strcmp (section_name, names->normal) == 0)
2154 return 1;
2155 if (names->compressed != NULL
2156 && strcmp (section_name, names->compressed) == 0)
2157 return 1;
2158 return 0;
2159 }
2160
2161 /* This function is mapped across the sections and remembers the
2162 offset and size of each of the debugging sections we are interested
2163 in. */
2164
2165 static void
2166 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2167 {
2168 const struct dwarf2_debug_sections *names;
2169 flagword aflag = bfd_get_section_flags (abfd, sectp);
2170
2171 if (vnames == NULL)
2172 names = &dwarf2_elf_names;
2173 else
2174 names = (const struct dwarf2_debug_sections *) vnames;
2175
2176 if ((aflag & SEC_HAS_CONTENTS) == 0)
2177 {
2178 }
2179 else if (section_is_p (sectp->name, &names->info))
2180 {
2181 dwarf2_per_objfile->info.s.section = sectp;
2182 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2183 }
2184 else if (section_is_p (sectp->name, &names->abbrev))
2185 {
2186 dwarf2_per_objfile->abbrev.s.section = sectp;
2187 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2188 }
2189 else if (section_is_p (sectp->name, &names->line))
2190 {
2191 dwarf2_per_objfile->line.s.section = sectp;
2192 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2193 }
2194 else if (section_is_p (sectp->name, &names->loc))
2195 {
2196 dwarf2_per_objfile->loc.s.section = sectp;
2197 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2198 }
2199 else if (section_is_p (sectp->name, &names->macinfo))
2200 {
2201 dwarf2_per_objfile->macinfo.s.section = sectp;
2202 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2203 }
2204 else if (section_is_p (sectp->name, &names->macro))
2205 {
2206 dwarf2_per_objfile->macro.s.section = sectp;
2207 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2208 }
2209 else if (section_is_p (sectp->name, &names->str))
2210 {
2211 dwarf2_per_objfile->str.s.section = sectp;
2212 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2213 }
2214 else if (section_is_p (sectp->name, &names->addr))
2215 {
2216 dwarf2_per_objfile->addr.s.section = sectp;
2217 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2218 }
2219 else if (section_is_p (sectp->name, &names->frame))
2220 {
2221 dwarf2_per_objfile->frame.s.section = sectp;
2222 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2223 }
2224 else if (section_is_p (sectp->name, &names->eh_frame))
2225 {
2226 dwarf2_per_objfile->eh_frame.s.section = sectp;
2227 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2228 }
2229 else if (section_is_p (sectp->name, &names->ranges))
2230 {
2231 dwarf2_per_objfile->ranges.s.section = sectp;
2232 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2233 }
2234 else if (section_is_p (sectp->name, &names->types))
2235 {
2236 struct dwarf2_section_info type_section;
2237
2238 memset (&type_section, 0, sizeof (type_section));
2239 type_section.s.section = sectp;
2240 type_section.size = bfd_get_section_size (sectp);
2241
2242 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2243 &type_section);
2244 }
2245 else if (section_is_p (sectp->name, &names->gdb_index))
2246 {
2247 dwarf2_per_objfile->gdb_index.s.section = sectp;
2248 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2249 }
2250
2251 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2252 && bfd_section_vma (abfd, sectp) == 0)
2253 dwarf2_per_objfile->has_section_at_zero = 1;
2254 }
2255
2256 /* A helper function that decides whether a section is empty,
2257 or not present. */
2258
2259 static int
2260 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2261 {
2262 if (section->is_virtual)
2263 return section->size == 0;
2264 return section->s.section == NULL || section->size == 0;
2265 }
2266
2267 /* Read the contents of the section INFO.
2268 OBJFILE is the main object file, but not necessarily the file where
2269 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2270 of the DWO file.
2271 If the section is compressed, uncompress it before returning. */
2272
2273 static void
2274 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2275 {
2276 asection *sectp;
2277 bfd *abfd;
2278 gdb_byte *buf, *retbuf;
2279
2280 if (info->readin)
2281 return;
2282 info->buffer = NULL;
2283 info->readin = 1;
2284
2285 if (dwarf2_section_empty_p (info))
2286 return;
2287
2288 sectp = get_section_bfd_section (info);
2289
2290 /* If this is a virtual section we need to read in the real one first. */
2291 if (info->is_virtual)
2292 {
2293 struct dwarf2_section_info *containing_section =
2294 get_containing_section (info);
2295
2296 gdb_assert (sectp != NULL);
2297 if ((sectp->flags & SEC_RELOC) != 0)
2298 {
2299 error (_("Dwarf Error: DWP format V2 with relocations is not"
2300 " supported in section %s [in module %s]"),
2301 get_section_name (info), get_section_file_name (info));
2302 }
2303 dwarf2_read_section (objfile, containing_section);
2304 /* Other code should have already caught virtual sections that don't
2305 fit. */
2306 gdb_assert (info->virtual_offset + info->size
2307 <= containing_section->size);
2308 /* If the real section is empty or there was a problem reading the
2309 section we shouldn't get here. */
2310 gdb_assert (containing_section->buffer != NULL);
2311 info->buffer = containing_section->buffer + info->virtual_offset;
2312 return;
2313 }
2314
2315 /* If the section has relocations, we must read it ourselves.
2316 Otherwise we attach it to the BFD. */
2317 if ((sectp->flags & SEC_RELOC) == 0)
2318 {
2319 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2320 return;
2321 }
2322
2323 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2324 info->buffer = buf;
2325
2326 /* When debugging .o files, we may need to apply relocations; see
2327 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2328 We never compress sections in .o files, so we only need to
2329 try this when the section is not compressed. */
2330 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2331 if (retbuf != NULL)
2332 {
2333 info->buffer = retbuf;
2334 return;
2335 }
2336
2337 abfd = get_section_bfd_owner (info);
2338 gdb_assert (abfd != NULL);
2339
2340 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2341 || bfd_bread (buf, info->size, abfd) != info->size)
2342 {
2343 error (_("Dwarf Error: Can't read DWARF data"
2344 " in section %s [in module %s]"),
2345 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2346 }
2347 }
2348
2349 /* A helper function that returns the size of a section in a safe way.
2350 If you are positive that the section has been read before using the
2351 size, then it is safe to refer to the dwarf2_section_info object's
2352 "size" field directly. In other cases, you must call this
2353 function, because for compressed sections the size field is not set
2354 correctly until the section has been read. */
2355
2356 static bfd_size_type
2357 dwarf2_section_size (struct objfile *objfile,
2358 struct dwarf2_section_info *info)
2359 {
2360 if (!info->readin)
2361 dwarf2_read_section (objfile, info);
2362 return info->size;
2363 }
2364
2365 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2366 SECTION_NAME. */
2367
2368 void
2369 dwarf2_get_section_info (struct objfile *objfile,
2370 enum dwarf2_section_enum sect,
2371 asection **sectp, const gdb_byte **bufp,
2372 bfd_size_type *sizep)
2373 {
2374 struct dwarf2_per_objfile *data
2375 = objfile_data (objfile, dwarf2_objfile_data_key);
2376 struct dwarf2_section_info *info;
2377
2378 /* We may see an objfile without any DWARF, in which case we just
2379 return nothing. */
2380 if (data == NULL)
2381 {
2382 *sectp = NULL;
2383 *bufp = NULL;
2384 *sizep = 0;
2385 return;
2386 }
2387 switch (sect)
2388 {
2389 case DWARF2_DEBUG_FRAME:
2390 info = &data->frame;
2391 break;
2392 case DWARF2_EH_FRAME:
2393 info = &data->eh_frame;
2394 break;
2395 default:
2396 gdb_assert_not_reached ("unexpected section");
2397 }
2398
2399 dwarf2_read_section (objfile, info);
2400
2401 *sectp = get_section_bfd_section (info);
2402 *bufp = info->buffer;
2403 *sizep = info->size;
2404 }
2405
2406 /* A helper function to find the sections for a .dwz file. */
2407
2408 static void
2409 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2410 {
2411 struct dwz_file *dwz_file = arg;
2412
2413 /* Note that we only support the standard ELF names, because .dwz
2414 is ELF-only (at the time of writing). */
2415 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2416 {
2417 dwz_file->abbrev.s.section = sectp;
2418 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2419 }
2420 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2421 {
2422 dwz_file->info.s.section = sectp;
2423 dwz_file->info.size = bfd_get_section_size (sectp);
2424 }
2425 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2426 {
2427 dwz_file->str.s.section = sectp;
2428 dwz_file->str.size = bfd_get_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2431 {
2432 dwz_file->line.s.section = sectp;
2433 dwz_file->line.size = bfd_get_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2436 {
2437 dwz_file->macro.s.section = sectp;
2438 dwz_file->macro.size = bfd_get_section_size (sectp);
2439 }
2440 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2441 {
2442 dwz_file->gdb_index.s.section = sectp;
2443 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2444 }
2445 }
2446
2447 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2448 there is no .gnu_debugaltlink section in the file. Error if there
2449 is such a section but the file cannot be found. */
2450
2451 static struct dwz_file *
2452 dwarf2_get_dwz_file (void)
2453 {
2454 bfd *dwz_bfd;
2455 char *data;
2456 struct cleanup *cleanup;
2457 const char *filename;
2458 struct dwz_file *result;
2459 bfd_size_type buildid_len_arg;
2460 size_t buildid_len;
2461 bfd_byte *buildid;
2462
2463 if (dwarf2_per_objfile->dwz_file != NULL)
2464 return dwarf2_per_objfile->dwz_file;
2465
2466 bfd_set_error (bfd_error_no_error);
2467 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2468 &buildid_len_arg, &buildid);
2469 if (data == NULL)
2470 {
2471 if (bfd_get_error () == bfd_error_no_error)
2472 return NULL;
2473 error (_("could not read '.gnu_debugaltlink' section: %s"),
2474 bfd_errmsg (bfd_get_error ()));
2475 }
2476 cleanup = make_cleanup (xfree, data);
2477 make_cleanup (xfree, buildid);
2478
2479 buildid_len = (size_t) buildid_len_arg;
2480
2481 filename = (const char *) data;
2482 if (!IS_ABSOLUTE_PATH (filename))
2483 {
2484 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2485 char *rel;
2486
2487 make_cleanup (xfree, abs);
2488 abs = ldirname (abs);
2489 make_cleanup (xfree, abs);
2490
2491 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2492 make_cleanup (xfree, rel);
2493 filename = rel;
2494 }
2495
2496 /* First try the file name given in the section. If that doesn't
2497 work, try to use the build-id instead. */
2498 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2499 if (dwz_bfd != NULL)
2500 {
2501 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2502 {
2503 gdb_bfd_unref (dwz_bfd);
2504 dwz_bfd = NULL;
2505 }
2506 }
2507
2508 if (dwz_bfd == NULL)
2509 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2510
2511 if (dwz_bfd == NULL)
2512 error (_("could not find '.gnu_debugaltlink' file for %s"),
2513 objfile_name (dwarf2_per_objfile->objfile));
2514
2515 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2516 struct dwz_file);
2517 result->dwz_bfd = dwz_bfd;
2518
2519 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2520
2521 do_cleanups (cleanup);
2522
2523 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, dwz_bfd);
2524 dwarf2_per_objfile->dwz_file = result;
2525 return result;
2526 }
2527 \f
2528 /* DWARF quick_symbols_functions support. */
2529
2530 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2531 unique line tables, so we maintain a separate table of all .debug_line
2532 derived entries to support the sharing.
2533 All the quick functions need is the list of file names. We discard the
2534 line_header when we're done and don't need to record it here. */
2535 struct quick_file_names
2536 {
2537 /* The data used to construct the hash key. */
2538 struct stmt_list_hash hash;
2539
2540 /* The number of entries in file_names, real_names. */
2541 unsigned int num_file_names;
2542
2543 /* The file names from the line table, after being run through
2544 file_full_name. */
2545 const char **file_names;
2546
2547 /* The file names from the line table after being run through
2548 gdb_realpath. These are computed lazily. */
2549 const char **real_names;
2550 };
2551
2552 /* When using the index (and thus not using psymtabs), each CU has an
2553 object of this type. This is used to hold information needed by
2554 the various "quick" methods. */
2555 struct dwarf2_per_cu_quick_data
2556 {
2557 /* The file table. This can be NULL if there was no file table
2558 or it's currently not read in.
2559 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2560 struct quick_file_names *file_names;
2561
2562 /* The corresponding symbol table. This is NULL if symbols for this
2563 CU have not yet been read. */
2564 struct compunit_symtab *compunit_symtab;
2565
2566 /* A temporary mark bit used when iterating over all CUs in
2567 expand_symtabs_matching. */
2568 unsigned int mark : 1;
2569
2570 /* True if we've tried to read the file table and found there isn't one.
2571 There will be no point in trying to read it again next time. */
2572 unsigned int no_file_data : 1;
2573 };
2574
2575 /* Utility hash function for a stmt_list_hash. */
2576
2577 static hashval_t
2578 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2579 {
2580 hashval_t v = 0;
2581
2582 if (stmt_list_hash->dwo_unit != NULL)
2583 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2584 v += stmt_list_hash->line_offset.sect_off;
2585 return v;
2586 }
2587
2588 /* Utility equality function for a stmt_list_hash. */
2589
2590 static int
2591 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2592 const struct stmt_list_hash *rhs)
2593 {
2594 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2595 return 0;
2596 if (lhs->dwo_unit != NULL
2597 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2598 return 0;
2599
2600 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2601 }
2602
2603 /* Hash function for a quick_file_names. */
2604
2605 static hashval_t
2606 hash_file_name_entry (const void *e)
2607 {
2608 const struct quick_file_names *file_data = e;
2609
2610 return hash_stmt_list_entry (&file_data->hash);
2611 }
2612
2613 /* Equality function for a quick_file_names. */
2614
2615 static int
2616 eq_file_name_entry (const void *a, const void *b)
2617 {
2618 const struct quick_file_names *ea = a;
2619 const struct quick_file_names *eb = b;
2620
2621 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2622 }
2623
2624 /* Delete function for a quick_file_names. */
2625
2626 static void
2627 delete_file_name_entry (void *e)
2628 {
2629 struct quick_file_names *file_data = e;
2630 int i;
2631
2632 for (i = 0; i < file_data->num_file_names; ++i)
2633 {
2634 xfree ((void*) file_data->file_names[i]);
2635 if (file_data->real_names)
2636 xfree ((void*) file_data->real_names[i]);
2637 }
2638
2639 /* The space for the struct itself lives on objfile_obstack,
2640 so we don't free it here. */
2641 }
2642
2643 /* Create a quick_file_names hash table. */
2644
2645 static htab_t
2646 create_quick_file_names_table (unsigned int nr_initial_entries)
2647 {
2648 return htab_create_alloc (nr_initial_entries,
2649 hash_file_name_entry, eq_file_name_entry,
2650 delete_file_name_entry, xcalloc, xfree);
2651 }
2652
2653 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2654 have to be created afterwards. You should call age_cached_comp_units after
2655 processing PER_CU->CU. dw2_setup must have been already called. */
2656
2657 static void
2658 load_cu (struct dwarf2_per_cu_data *per_cu)
2659 {
2660 if (per_cu->is_debug_types)
2661 load_full_type_unit (per_cu);
2662 else
2663 load_full_comp_unit (per_cu, language_minimal);
2664
2665 if (per_cu->cu == NULL)
2666 return; /* Dummy CU. */
2667
2668 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2669 }
2670
2671 /* Read in the symbols for PER_CU. */
2672
2673 static void
2674 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2675 {
2676 struct cleanup *back_to;
2677
2678 /* Skip type_unit_groups, reading the type units they contain
2679 is handled elsewhere. */
2680 if (IS_TYPE_UNIT_GROUP (per_cu))
2681 return;
2682
2683 back_to = make_cleanup (dwarf2_release_queue, NULL);
2684
2685 if (dwarf2_per_objfile->using_index
2686 ? per_cu->v.quick->compunit_symtab == NULL
2687 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2688 {
2689 queue_comp_unit (per_cu, language_minimal);
2690 load_cu (per_cu);
2691
2692 /* If we just loaded a CU from a DWO, and we're working with an index
2693 that may badly handle TUs, load all the TUs in that DWO as well.
2694 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2695 if (!per_cu->is_debug_types
2696 && per_cu->cu != NULL
2697 && per_cu->cu->dwo_unit != NULL
2698 && dwarf2_per_objfile->index_table != NULL
2699 && dwarf2_per_objfile->index_table->version <= 7
2700 /* DWP files aren't supported yet. */
2701 && get_dwp_file () == NULL)
2702 queue_and_load_all_dwo_tus (per_cu);
2703 }
2704
2705 process_queue ();
2706
2707 /* Age the cache, releasing compilation units that have not
2708 been used recently. */
2709 age_cached_comp_units ();
2710
2711 do_cleanups (back_to);
2712 }
2713
2714 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2715 the objfile from which this CU came. Returns the resulting symbol
2716 table. */
2717
2718 static struct compunit_symtab *
2719 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2720 {
2721 gdb_assert (dwarf2_per_objfile->using_index);
2722 if (!per_cu->v.quick->compunit_symtab)
2723 {
2724 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2725 increment_reading_symtab ();
2726 dw2_do_instantiate_symtab (per_cu);
2727 process_cu_includes ();
2728 do_cleanups (back_to);
2729 }
2730
2731 return per_cu->v.quick->compunit_symtab;
2732 }
2733
2734 /* Return the CU/TU given its index.
2735
2736 This is intended for loops like:
2737
2738 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2739 + dwarf2_per_objfile->n_type_units); ++i)
2740 {
2741 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2742
2743 ...;
2744 }
2745 */
2746
2747 static struct dwarf2_per_cu_data *
2748 dw2_get_cutu (int index)
2749 {
2750 if (index >= dwarf2_per_objfile->n_comp_units)
2751 {
2752 index -= dwarf2_per_objfile->n_comp_units;
2753 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2754 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2755 }
2756
2757 return dwarf2_per_objfile->all_comp_units[index];
2758 }
2759
2760 /* Return the CU given its index.
2761 This differs from dw2_get_cutu in that it's for when you know INDEX
2762 refers to a CU. */
2763
2764 static struct dwarf2_per_cu_data *
2765 dw2_get_cu (int index)
2766 {
2767 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2768
2769 return dwarf2_per_objfile->all_comp_units[index];
2770 }
2771
2772 /* A helper for create_cus_from_index that handles a given list of
2773 CUs. */
2774
2775 static void
2776 create_cus_from_index_list (struct objfile *objfile,
2777 const gdb_byte *cu_list, offset_type n_elements,
2778 struct dwarf2_section_info *section,
2779 int is_dwz,
2780 int base_offset)
2781 {
2782 offset_type i;
2783
2784 for (i = 0; i < n_elements; i += 2)
2785 {
2786 struct dwarf2_per_cu_data *the_cu;
2787 ULONGEST offset, length;
2788
2789 gdb_static_assert (sizeof (ULONGEST) >= 8);
2790 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2791 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2792 cu_list += 2 * 8;
2793
2794 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2795 struct dwarf2_per_cu_data);
2796 the_cu->offset.sect_off = offset;
2797 the_cu->length = length;
2798 the_cu->objfile = objfile;
2799 the_cu->section = section;
2800 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2801 struct dwarf2_per_cu_quick_data);
2802 the_cu->is_dwz = is_dwz;
2803 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2804 }
2805 }
2806
2807 /* Read the CU list from the mapped index, and use it to create all
2808 the CU objects for this objfile. */
2809
2810 static void
2811 create_cus_from_index (struct objfile *objfile,
2812 const gdb_byte *cu_list, offset_type cu_list_elements,
2813 const gdb_byte *dwz_list, offset_type dwz_elements)
2814 {
2815 struct dwz_file *dwz;
2816
2817 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2818 dwarf2_per_objfile->all_comp_units
2819 = obstack_alloc (&objfile->objfile_obstack,
2820 dwarf2_per_objfile->n_comp_units
2821 * sizeof (struct dwarf2_per_cu_data *));
2822
2823 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2824 &dwarf2_per_objfile->info, 0, 0);
2825
2826 if (dwz_elements == 0)
2827 return;
2828
2829 dwz = dwarf2_get_dwz_file ();
2830 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2831 cu_list_elements / 2);
2832 }
2833
2834 /* Create the signatured type hash table from the index. */
2835
2836 static void
2837 create_signatured_type_table_from_index (struct objfile *objfile,
2838 struct dwarf2_section_info *section,
2839 const gdb_byte *bytes,
2840 offset_type elements)
2841 {
2842 offset_type i;
2843 htab_t sig_types_hash;
2844
2845 dwarf2_per_objfile->n_type_units
2846 = dwarf2_per_objfile->n_allocated_type_units
2847 = elements / 3;
2848 dwarf2_per_objfile->all_type_units
2849 = xmalloc (dwarf2_per_objfile->n_type_units
2850 * sizeof (struct signatured_type *));
2851
2852 sig_types_hash = allocate_signatured_type_table (objfile);
2853
2854 for (i = 0; i < elements; i += 3)
2855 {
2856 struct signatured_type *sig_type;
2857 ULONGEST offset, type_offset_in_tu, signature;
2858 void **slot;
2859
2860 gdb_static_assert (sizeof (ULONGEST) >= 8);
2861 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2862 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2863 BFD_ENDIAN_LITTLE);
2864 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2865 bytes += 3 * 8;
2866
2867 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2868 struct signatured_type);
2869 sig_type->signature = signature;
2870 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2871 sig_type->per_cu.is_debug_types = 1;
2872 sig_type->per_cu.section = section;
2873 sig_type->per_cu.offset.sect_off = offset;
2874 sig_type->per_cu.objfile = objfile;
2875 sig_type->per_cu.v.quick
2876 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2877 struct dwarf2_per_cu_quick_data);
2878
2879 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2880 *slot = sig_type;
2881
2882 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2883 }
2884
2885 dwarf2_per_objfile->signatured_types = sig_types_hash;
2886 }
2887
2888 /* Read the address map data from the mapped index, and use it to
2889 populate the objfile's psymtabs_addrmap. */
2890
2891 static void
2892 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2893 {
2894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2895 const gdb_byte *iter, *end;
2896 struct obstack temp_obstack;
2897 struct addrmap *mutable_map;
2898 struct cleanup *cleanup;
2899 CORE_ADDR baseaddr;
2900
2901 obstack_init (&temp_obstack);
2902 cleanup = make_cleanup_obstack_free (&temp_obstack);
2903 mutable_map = addrmap_create_mutable (&temp_obstack);
2904
2905 iter = index->address_table;
2906 end = iter + index->address_table_size;
2907
2908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2909
2910 while (iter < end)
2911 {
2912 ULONGEST hi, lo, cu_index;
2913 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2914 iter += 8;
2915 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2916 iter += 8;
2917 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2918 iter += 4;
2919
2920 if (lo > hi)
2921 {
2922 complaint (&symfile_complaints,
2923 _(".gdb_index address table has invalid range (%s - %s)"),
2924 hex_string (lo), hex_string (hi));
2925 continue;
2926 }
2927
2928 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2929 {
2930 complaint (&symfile_complaints,
2931 _(".gdb_index address table has invalid CU number %u"),
2932 (unsigned) cu_index);
2933 continue;
2934 }
2935
2936 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
2937 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
2938 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
2939 }
2940
2941 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2942 &objfile->objfile_obstack);
2943 do_cleanups (cleanup);
2944 }
2945
2946 /* The hash function for strings in the mapped index. This is the same as
2947 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2948 implementation. This is necessary because the hash function is tied to the
2949 format of the mapped index file. The hash values do not have to match with
2950 SYMBOL_HASH_NEXT.
2951
2952 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2953
2954 static hashval_t
2955 mapped_index_string_hash (int index_version, const void *p)
2956 {
2957 const unsigned char *str = (const unsigned char *) p;
2958 hashval_t r = 0;
2959 unsigned char c;
2960
2961 while ((c = *str++) != 0)
2962 {
2963 if (index_version >= 5)
2964 c = tolower (c);
2965 r = r * 67 + c - 113;
2966 }
2967
2968 return r;
2969 }
2970
2971 /* Find a slot in the mapped index INDEX for the object named NAME.
2972 If NAME is found, set *VEC_OUT to point to the CU vector in the
2973 constant pool and return 1. If NAME cannot be found, return 0. */
2974
2975 static int
2976 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2977 offset_type **vec_out)
2978 {
2979 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2980 offset_type hash;
2981 offset_type slot, step;
2982 int (*cmp) (const char *, const char *);
2983
2984 if (current_language->la_language == language_cplus
2985 || current_language->la_language == language_java
2986 || current_language->la_language == language_fortran
2987 || current_language->la_language == language_d)
2988 {
2989 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2990 not contain any. */
2991
2992 if (strchr (name, '(') != NULL)
2993 {
2994 char *without_params = cp_remove_params (name);
2995
2996 if (without_params != NULL)
2997 {
2998 make_cleanup (xfree, without_params);
2999 name = without_params;
3000 }
3001 }
3002 }
3003
3004 /* Index version 4 did not support case insensitive searches. But the
3005 indices for case insensitive languages are built in lowercase, therefore
3006 simulate our NAME being searched is also lowercased. */
3007 hash = mapped_index_string_hash ((index->version == 4
3008 && case_sensitivity == case_sensitive_off
3009 ? 5 : index->version),
3010 name);
3011
3012 slot = hash & (index->symbol_table_slots - 1);
3013 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3014 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3015
3016 for (;;)
3017 {
3018 /* Convert a slot number to an offset into the table. */
3019 offset_type i = 2 * slot;
3020 const char *str;
3021 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3022 {
3023 do_cleanups (back_to);
3024 return 0;
3025 }
3026
3027 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3028 if (!cmp (name, str))
3029 {
3030 *vec_out = (offset_type *) (index->constant_pool
3031 + MAYBE_SWAP (index->symbol_table[i + 1]));
3032 do_cleanups (back_to);
3033 return 1;
3034 }
3035
3036 slot = (slot + step) & (index->symbol_table_slots - 1);
3037 }
3038 }
3039
3040 /* A helper function that reads the .gdb_index from SECTION and fills
3041 in MAP. FILENAME is the name of the file containing the section;
3042 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3043 ok to use deprecated sections.
3044
3045 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3046 out parameters that are filled in with information about the CU and
3047 TU lists in the section.
3048
3049 Returns 1 if all went well, 0 otherwise. */
3050
3051 static int
3052 read_index_from_section (struct objfile *objfile,
3053 const char *filename,
3054 int deprecated_ok,
3055 struct dwarf2_section_info *section,
3056 struct mapped_index *map,
3057 const gdb_byte **cu_list,
3058 offset_type *cu_list_elements,
3059 const gdb_byte **types_list,
3060 offset_type *types_list_elements)
3061 {
3062 const gdb_byte *addr;
3063 offset_type version;
3064 offset_type *metadata;
3065 int i;
3066
3067 if (dwarf2_section_empty_p (section))
3068 return 0;
3069
3070 /* Older elfutils strip versions could keep the section in the main
3071 executable while splitting it for the separate debug info file. */
3072 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3073 return 0;
3074
3075 dwarf2_read_section (objfile, section);
3076
3077 addr = section->buffer;
3078 /* Version check. */
3079 version = MAYBE_SWAP (*(offset_type *) addr);
3080 /* Versions earlier than 3 emitted every copy of a psymbol. This
3081 causes the index to behave very poorly for certain requests. Version 3
3082 contained incomplete addrmap. So, it seems better to just ignore such
3083 indices. */
3084 if (version < 4)
3085 {
3086 static int warning_printed = 0;
3087 if (!warning_printed)
3088 {
3089 warning (_("Skipping obsolete .gdb_index section in %s."),
3090 filename);
3091 warning_printed = 1;
3092 }
3093 return 0;
3094 }
3095 /* Index version 4 uses a different hash function than index version
3096 5 and later.
3097
3098 Versions earlier than 6 did not emit psymbols for inlined
3099 functions. Using these files will cause GDB not to be able to
3100 set breakpoints on inlined functions by name, so we ignore these
3101 indices unless the user has done
3102 "set use-deprecated-index-sections on". */
3103 if (version < 6 && !deprecated_ok)
3104 {
3105 static int warning_printed = 0;
3106 if (!warning_printed)
3107 {
3108 warning (_("\
3109 Skipping deprecated .gdb_index section in %s.\n\
3110 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3111 to use the section anyway."),
3112 filename);
3113 warning_printed = 1;
3114 }
3115 return 0;
3116 }
3117 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3118 of the TU (for symbols coming from TUs),
3119 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3120 Plus gold-generated indices can have duplicate entries for global symbols,
3121 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3122 These are just performance bugs, and we can't distinguish gdb-generated
3123 indices from gold-generated ones, so issue no warning here. */
3124
3125 /* Indexes with higher version than the one supported by GDB may be no
3126 longer backward compatible. */
3127 if (version > 8)
3128 return 0;
3129
3130 map->version = version;
3131 map->total_size = section->size;
3132
3133 metadata = (offset_type *) (addr + sizeof (offset_type));
3134
3135 i = 0;
3136 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3137 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3138 / 8);
3139 ++i;
3140
3141 *types_list = addr + MAYBE_SWAP (metadata[i]);
3142 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3143 - MAYBE_SWAP (metadata[i]))
3144 / 8);
3145 ++i;
3146
3147 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3148 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3149 - MAYBE_SWAP (metadata[i]));
3150 ++i;
3151
3152 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3153 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3154 - MAYBE_SWAP (metadata[i]))
3155 / (2 * sizeof (offset_type)));
3156 ++i;
3157
3158 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3159
3160 return 1;
3161 }
3162
3163
3164 /* Read the index file. If everything went ok, initialize the "quick"
3165 elements of all the CUs and return 1. Otherwise, return 0. */
3166
3167 static int
3168 dwarf2_read_index (struct objfile *objfile)
3169 {
3170 struct mapped_index local_map, *map;
3171 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3172 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3173 struct dwz_file *dwz;
3174
3175 if (!read_index_from_section (objfile, objfile_name (objfile),
3176 use_deprecated_index_sections,
3177 &dwarf2_per_objfile->gdb_index, &local_map,
3178 &cu_list, &cu_list_elements,
3179 &types_list, &types_list_elements))
3180 return 0;
3181
3182 /* Don't use the index if it's empty. */
3183 if (local_map.symbol_table_slots == 0)
3184 return 0;
3185
3186 /* If there is a .dwz file, read it so we can get its CU list as
3187 well. */
3188 dwz = dwarf2_get_dwz_file ();
3189 if (dwz != NULL)
3190 {
3191 struct mapped_index dwz_map;
3192 const gdb_byte *dwz_types_ignore;
3193 offset_type dwz_types_elements_ignore;
3194
3195 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3196 1,
3197 &dwz->gdb_index, &dwz_map,
3198 &dwz_list, &dwz_list_elements,
3199 &dwz_types_ignore,
3200 &dwz_types_elements_ignore))
3201 {
3202 warning (_("could not read '.gdb_index' section from %s; skipping"),
3203 bfd_get_filename (dwz->dwz_bfd));
3204 return 0;
3205 }
3206 }
3207
3208 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3209 dwz_list_elements);
3210
3211 if (types_list_elements)
3212 {
3213 struct dwarf2_section_info *section;
3214
3215 /* We can only handle a single .debug_types when we have an
3216 index. */
3217 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3218 return 0;
3219
3220 section = VEC_index (dwarf2_section_info_def,
3221 dwarf2_per_objfile->types, 0);
3222
3223 create_signatured_type_table_from_index (objfile, section, types_list,
3224 types_list_elements);
3225 }
3226
3227 create_addrmap_from_index (objfile, &local_map);
3228
3229 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
3230 *map = local_map;
3231
3232 dwarf2_per_objfile->index_table = map;
3233 dwarf2_per_objfile->using_index = 1;
3234 dwarf2_per_objfile->quick_file_names_table =
3235 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3236
3237 return 1;
3238 }
3239
3240 /* A helper for the "quick" functions which sets the global
3241 dwarf2_per_objfile according to OBJFILE. */
3242
3243 static void
3244 dw2_setup (struct objfile *objfile)
3245 {
3246 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
3247 gdb_assert (dwarf2_per_objfile);
3248 }
3249
3250 /* die_reader_func for dw2_get_file_names. */
3251
3252 static void
3253 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3254 const gdb_byte *info_ptr,
3255 struct die_info *comp_unit_die,
3256 int has_children,
3257 void *data)
3258 {
3259 struct dwarf2_cu *cu = reader->cu;
3260 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3261 struct objfile *objfile = dwarf2_per_objfile->objfile;
3262 struct dwarf2_per_cu_data *lh_cu;
3263 struct line_header *lh;
3264 struct attribute *attr;
3265 int i;
3266 const char *name, *comp_dir;
3267 void **slot;
3268 struct quick_file_names *qfn;
3269 unsigned int line_offset;
3270
3271 gdb_assert (! this_cu->is_debug_types);
3272
3273 /* Our callers never want to match partial units -- instead they
3274 will match the enclosing full CU. */
3275 if (comp_unit_die->tag == DW_TAG_partial_unit)
3276 {
3277 this_cu->v.quick->no_file_data = 1;
3278 return;
3279 }
3280
3281 lh_cu = this_cu;
3282 lh = NULL;
3283 slot = NULL;
3284 line_offset = 0;
3285
3286 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3287 if (attr)
3288 {
3289 struct quick_file_names find_entry;
3290
3291 line_offset = DW_UNSND (attr);
3292
3293 /* We may have already read in this line header (TU line header sharing).
3294 If we have we're done. */
3295 find_entry.hash.dwo_unit = cu->dwo_unit;
3296 find_entry.hash.line_offset.sect_off = line_offset;
3297 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3298 &find_entry, INSERT);
3299 if (*slot != NULL)
3300 {
3301 lh_cu->v.quick->file_names = *slot;
3302 return;
3303 }
3304
3305 lh = dwarf_decode_line_header (line_offset, cu);
3306 }
3307 if (lh == NULL)
3308 {
3309 lh_cu->v.quick->no_file_data = 1;
3310 return;
3311 }
3312
3313 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
3314 qfn->hash.dwo_unit = cu->dwo_unit;
3315 qfn->hash.line_offset.sect_off = line_offset;
3316 gdb_assert (slot != NULL);
3317 *slot = qfn;
3318
3319 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3320
3321 qfn->num_file_names = lh->num_file_names;
3322 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
3323 lh->num_file_names * sizeof (char *));
3324 for (i = 0; i < lh->num_file_names; ++i)
3325 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3326 qfn->real_names = NULL;
3327
3328 free_line_header (lh);
3329
3330 lh_cu->v.quick->file_names = qfn;
3331 }
3332
3333 /* A helper for the "quick" functions which attempts to read the line
3334 table for THIS_CU. */
3335
3336 static struct quick_file_names *
3337 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3338 {
3339 /* This should never be called for TUs. */
3340 gdb_assert (! this_cu->is_debug_types);
3341 /* Nor type unit groups. */
3342 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3343
3344 if (this_cu->v.quick->file_names != NULL)
3345 return this_cu->v.quick->file_names;
3346 /* If we know there is no line data, no point in looking again. */
3347 if (this_cu->v.quick->no_file_data)
3348 return NULL;
3349
3350 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3351
3352 if (this_cu->v.quick->no_file_data)
3353 return NULL;
3354 return this_cu->v.quick->file_names;
3355 }
3356
3357 /* A helper for the "quick" functions which computes and caches the
3358 real path for a given file name from the line table. */
3359
3360 static const char *
3361 dw2_get_real_path (struct objfile *objfile,
3362 struct quick_file_names *qfn, int index)
3363 {
3364 if (qfn->real_names == NULL)
3365 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3366 qfn->num_file_names, const char *);
3367
3368 if (qfn->real_names[index] == NULL)
3369 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3370
3371 return qfn->real_names[index];
3372 }
3373
3374 static struct symtab *
3375 dw2_find_last_source_symtab (struct objfile *objfile)
3376 {
3377 struct compunit_symtab *cust;
3378 int index;
3379
3380 dw2_setup (objfile);
3381 index = dwarf2_per_objfile->n_comp_units - 1;
3382 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3383 if (cust == NULL)
3384 return NULL;
3385 return compunit_primary_filetab (cust);
3386 }
3387
3388 /* Traversal function for dw2_forget_cached_source_info. */
3389
3390 static int
3391 dw2_free_cached_file_names (void **slot, void *info)
3392 {
3393 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3394
3395 if (file_data->real_names)
3396 {
3397 int i;
3398
3399 for (i = 0; i < file_data->num_file_names; ++i)
3400 {
3401 xfree ((void*) file_data->real_names[i]);
3402 file_data->real_names[i] = NULL;
3403 }
3404 }
3405
3406 return 1;
3407 }
3408
3409 static void
3410 dw2_forget_cached_source_info (struct objfile *objfile)
3411 {
3412 dw2_setup (objfile);
3413
3414 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3415 dw2_free_cached_file_names, NULL);
3416 }
3417
3418 /* Helper function for dw2_map_symtabs_matching_filename that expands
3419 the symtabs and calls the iterator. */
3420
3421 static int
3422 dw2_map_expand_apply (struct objfile *objfile,
3423 struct dwarf2_per_cu_data *per_cu,
3424 const char *name, const char *real_path,
3425 int (*callback) (struct symtab *, void *),
3426 void *data)
3427 {
3428 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3429
3430 /* Don't visit already-expanded CUs. */
3431 if (per_cu->v.quick->compunit_symtab)
3432 return 0;
3433
3434 /* This may expand more than one symtab, and we want to iterate over
3435 all of them. */
3436 dw2_instantiate_symtab (per_cu);
3437
3438 return iterate_over_some_symtabs (name, real_path, callback, data,
3439 objfile->compunit_symtabs, last_made);
3440 }
3441
3442 /* Implementation of the map_symtabs_matching_filename method. */
3443
3444 static int
3445 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3446 const char *real_path,
3447 int (*callback) (struct symtab *, void *),
3448 void *data)
3449 {
3450 int i;
3451 const char *name_basename = lbasename (name);
3452
3453 dw2_setup (objfile);
3454
3455 /* The rule is CUs specify all the files, including those used by
3456 any TU, so there's no need to scan TUs here. */
3457
3458 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3459 {
3460 int j;
3461 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3462 struct quick_file_names *file_data;
3463
3464 /* We only need to look at symtabs not already expanded. */
3465 if (per_cu->v.quick->compunit_symtab)
3466 continue;
3467
3468 file_data = dw2_get_file_names (per_cu);
3469 if (file_data == NULL)
3470 continue;
3471
3472 for (j = 0; j < file_data->num_file_names; ++j)
3473 {
3474 const char *this_name = file_data->file_names[j];
3475 const char *this_real_name;
3476
3477 if (compare_filenames_for_search (this_name, name))
3478 {
3479 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3480 callback, data))
3481 return 1;
3482 continue;
3483 }
3484
3485 /* Before we invoke realpath, which can get expensive when many
3486 files are involved, do a quick comparison of the basenames. */
3487 if (! basenames_may_differ
3488 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3489 continue;
3490
3491 this_real_name = dw2_get_real_path (objfile, file_data, j);
3492 if (compare_filenames_for_search (this_real_name, name))
3493 {
3494 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3495 callback, data))
3496 return 1;
3497 continue;
3498 }
3499
3500 if (real_path != NULL)
3501 {
3502 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3503 gdb_assert (IS_ABSOLUTE_PATH (name));
3504 if (this_real_name != NULL
3505 && FILENAME_CMP (real_path, this_real_name) == 0)
3506 {
3507 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3508 callback, data))
3509 return 1;
3510 continue;
3511 }
3512 }
3513 }
3514 }
3515
3516 return 0;
3517 }
3518
3519 /* Struct used to manage iterating over all CUs looking for a symbol. */
3520
3521 struct dw2_symtab_iterator
3522 {
3523 /* The internalized form of .gdb_index. */
3524 struct mapped_index *index;
3525 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3526 int want_specific_block;
3527 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3528 Unused if !WANT_SPECIFIC_BLOCK. */
3529 int block_index;
3530 /* The kind of symbol we're looking for. */
3531 domain_enum domain;
3532 /* The list of CUs from the index entry of the symbol,
3533 or NULL if not found. */
3534 offset_type *vec;
3535 /* The next element in VEC to look at. */
3536 int next;
3537 /* The number of elements in VEC, or zero if there is no match. */
3538 int length;
3539 /* Have we seen a global version of the symbol?
3540 If so we can ignore all further global instances.
3541 This is to work around gold/15646, inefficient gold-generated
3542 indices. */
3543 int global_seen;
3544 };
3545
3546 /* Initialize the index symtab iterator ITER.
3547 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3548 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3549
3550 static void
3551 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3552 struct mapped_index *index,
3553 int want_specific_block,
3554 int block_index,
3555 domain_enum domain,
3556 const char *name)
3557 {
3558 iter->index = index;
3559 iter->want_specific_block = want_specific_block;
3560 iter->block_index = block_index;
3561 iter->domain = domain;
3562 iter->next = 0;
3563 iter->global_seen = 0;
3564
3565 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3566 iter->length = MAYBE_SWAP (*iter->vec);
3567 else
3568 {
3569 iter->vec = NULL;
3570 iter->length = 0;
3571 }
3572 }
3573
3574 /* Return the next matching CU or NULL if there are no more. */
3575
3576 static struct dwarf2_per_cu_data *
3577 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3578 {
3579 for ( ; iter->next < iter->length; ++iter->next)
3580 {
3581 offset_type cu_index_and_attrs =
3582 MAYBE_SWAP (iter->vec[iter->next + 1]);
3583 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3584 struct dwarf2_per_cu_data *per_cu;
3585 int want_static = iter->block_index != GLOBAL_BLOCK;
3586 /* This value is only valid for index versions >= 7. */
3587 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3588 gdb_index_symbol_kind symbol_kind =
3589 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3590 /* Only check the symbol attributes if they're present.
3591 Indices prior to version 7 don't record them,
3592 and indices >= 7 may elide them for certain symbols
3593 (gold does this). */
3594 int attrs_valid =
3595 (iter->index->version >= 7
3596 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3597
3598 /* Don't crash on bad data. */
3599 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3600 + dwarf2_per_objfile->n_type_units))
3601 {
3602 complaint (&symfile_complaints,
3603 _(".gdb_index entry has bad CU index"
3604 " [in module %s]"),
3605 objfile_name (dwarf2_per_objfile->objfile));
3606 continue;
3607 }
3608
3609 per_cu = dw2_get_cutu (cu_index);
3610
3611 /* Skip if already read in. */
3612 if (per_cu->v.quick->compunit_symtab)
3613 continue;
3614
3615 /* Check static vs global. */
3616 if (attrs_valid)
3617 {
3618 if (iter->want_specific_block
3619 && want_static != is_static)
3620 continue;
3621 /* Work around gold/15646. */
3622 if (!is_static && iter->global_seen)
3623 continue;
3624 if (!is_static)
3625 iter->global_seen = 1;
3626 }
3627
3628 /* Only check the symbol's kind if it has one. */
3629 if (attrs_valid)
3630 {
3631 switch (iter->domain)
3632 {
3633 case VAR_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3635 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3636 /* Some types are also in VAR_DOMAIN. */
3637 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3638 continue;
3639 break;
3640 case STRUCT_DOMAIN:
3641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3642 continue;
3643 break;
3644 case LABEL_DOMAIN:
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3646 continue;
3647 break;
3648 default:
3649 break;
3650 }
3651 }
3652
3653 ++iter->next;
3654 return per_cu;
3655 }
3656
3657 return NULL;
3658 }
3659
3660 static struct compunit_symtab *
3661 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3662 const char *name, domain_enum domain)
3663 {
3664 struct compunit_symtab *stab_best = NULL;
3665 struct mapped_index *index;
3666
3667 dw2_setup (objfile);
3668
3669 index = dwarf2_per_objfile->index_table;
3670
3671 /* index is NULL if OBJF_READNOW. */
3672 if (index)
3673 {
3674 struct dw2_symtab_iterator iter;
3675 struct dwarf2_per_cu_data *per_cu;
3676
3677 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3678
3679 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3680 {
3681 struct symbol *sym, *with_opaque = NULL;
3682 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3683 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3684 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3685
3686 sym = block_find_symbol (block, name, domain,
3687 block_find_non_opaque_type_preferred,
3688 &with_opaque);
3689
3690 /* Some caution must be observed with overloaded functions
3691 and methods, since the index will not contain any overload
3692 information (but NAME might contain it). */
3693
3694 if (sym != NULL
3695 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3696 return stab;
3697 if (with_opaque != NULL
3698 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3699 stab_best = stab;
3700
3701 /* Keep looking through other CUs. */
3702 }
3703 }
3704
3705 return stab_best;
3706 }
3707
3708 static void
3709 dw2_print_stats (struct objfile *objfile)
3710 {
3711 int i, total, count;
3712
3713 dw2_setup (objfile);
3714 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3715 count = 0;
3716 for (i = 0; i < total; ++i)
3717 {
3718 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3719
3720 if (!per_cu->v.quick->compunit_symtab)
3721 ++count;
3722 }
3723 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3724 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3725 }
3726
3727 /* This dumps minimal information about the index.
3728 It is called via "mt print objfiles".
3729 One use is to verify .gdb_index has been loaded by the
3730 gdb.dwarf2/gdb-index.exp testcase. */
3731
3732 static void
3733 dw2_dump (struct objfile *objfile)
3734 {
3735 dw2_setup (objfile);
3736 gdb_assert (dwarf2_per_objfile->using_index);
3737 printf_filtered (".gdb_index:");
3738 if (dwarf2_per_objfile->index_table != NULL)
3739 {
3740 printf_filtered (" version %d\n",
3741 dwarf2_per_objfile->index_table->version);
3742 }
3743 else
3744 printf_filtered (" faked for \"readnow\"\n");
3745 printf_filtered ("\n");
3746 }
3747
3748 static void
3749 dw2_relocate (struct objfile *objfile,
3750 const struct section_offsets *new_offsets,
3751 const struct section_offsets *delta)
3752 {
3753 /* There's nothing to relocate here. */
3754 }
3755
3756 static void
3757 dw2_expand_symtabs_for_function (struct objfile *objfile,
3758 const char *func_name)
3759 {
3760 struct mapped_index *index;
3761
3762 dw2_setup (objfile);
3763
3764 index = dwarf2_per_objfile->index_table;
3765
3766 /* index is NULL if OBJF_READNOW. */
3767 if (index)
3768 {
3769 struct dw2_symtab_iterator iter;
3770 struct dwarf2_per_cu_data *per_cu;
3771
3772 /* Note: It doesn't matter what we pass for block_index here. */
3773 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3774 func_name);
3775
3776 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3777 dw2_instantiate_symtab (per_cu);
3778 }
3779 }
3780
3781 static void
3782 dw2_expand_all_symtabs (struct objfile *objfile)
3783 {
3784 int i;
3785
3786 dw2_setup (objfile);
3787
3788 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3789 + dwarf2_per_objfile->n_type_units); ++i)
3790 {
3791 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3792
3793 dw2_instantiate_symtab (per_cu);
3794 }
3795 }
3796
3797 static void
3798 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3799 const char *fullname)
3800 {
3801 int i;
3802
3803 dw2_setup (objfile);
3804
3805 /* We don't need to consider type units here.
3806 This is only called for examining code, e.g. expand_line_sal.
3807 There can be an order of magnitude (or more) more type units
3808 than comp units, and we avoid them if we can. */
3809
3810 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3811 {
3812 int j;
3813 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3814 struct quick_file_names *file_data;
3815
3816 /* We only need to look at symtabs not already expanded. */
3817 if (per_cu->v.quick->compunit_symtab)
3818 continue;
3819
3820 file_data = dw2_get_file_names (per_cu);
3821 if (file_data == NULL)
3822 continue;
3823
3824 for (j = 0; j < file_data->num_file_names; ++j)
3825 {
3826 const char *this_fullname = file_data->file_names[j];
3827
3828 if (filename_cmp (this_fullname, fullname) == 0)
3829 {
3830 dw2_instantiate_symtab (per_cu);
3831 break;
3832 }
3833 }
3834 }
3835 }
3836
3837 static void
3838 dw2_map_matching_symbols (struct objfile *objfile,
3839 const char * name, domain_enum domain,
3840 int global,
3841 int (*callback) (struct block *,
3842 struct symbol *, void *),
3843 void *data, symbol_compare_ftype *match,
3844 symbol_compare_ftype *ordered_compare)
3845 {
3846 /* Currently unimplemented; used for Ada. The function can be called if the
3847 current language is Ada for a non-Ada objfile using GNU index. As Ada
3848 does not look for non-Ada symbols this function should just return. */
3849 }
3850
3851 static void
3852 dw2_expand_symtabs_matching
3853 (struct objfile *objfile,
3854 expand_symtabs_file_matcher_ftype *file_matcher,
3855 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
3856 expand_symtabs_exp_notify_ftype *expansion_notify,
3857 enum search_domain kind,
3858 void *data)
3859 {
3860 int i;
3861 offset_type iter;
3862 struct mapped_index *index;
3863
3864 dw2_setup (objfile);
3865
3866 /* index_table is NULL if OBJF_READNOW. */
3867 if (!dwarf2_per_objfile->index_table)
3868 return;
3869 index = dwarf2_per_objfile->index_table;
3870
3871 if (file_matcher != NULL)
3872 {
3873 struct cleanup *cleanup;
3874 htab_t visited_found, visited_not_found;
3875
3876 visited_found = htab_create_alloc (10,
3877 htab_hash_pointer, htab_eq_pointer,
3878 NULL, xcalloc, xfree);
3879 cleanup = make_cleanup_htab_delete (visited_found);
3880 visited_not_found = htab_create_alloc (10,
3881 htab_hash_pointer, htab_eq_pointer,
3882 NULL, xcalloc, xfree);
3883 make_cleanup_htab_delete (visited_not_found);
3884
3885 /* The rule is CUs specify all the files, including those used by
3886 any TU, so there's no need to scan TUs here. */
3887
3888 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3889 {
3890 int j;
3891 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3892 struct quick_file_names *file_data;
3893 void **slot;
3894
3895 QUIT;
3896
3897 per_cu->v.quick->mark = 0;
3898
3899 /* We only need to look at symtabs not already expanded. */
3900 if (per_cu->v.quick->compunit_symtab)
3901 continue;
3902
3903 file_data = dw2_get_file_names (per_cu);
3904 if (file_data == NULL)
3905 continue;
3906
3907 if (htab_find (visited_not_found, file_data) != NULL)
3908 continue;
3909 else if (htab_find (visited_found, file_data) != NULL)
3910 {
3911 per_cu->v.quick->mark = 1;
3912 continue;
3913 }
3914
3915 for (j = 0; j < file_data->num_file_names; ++j)
3916 {
3917 const char *this_real_name;
3918
3919 if (file_matcher (file_data->file_names[j], data, 0))
3920 {
3921 per_cu->v.quick->mark = 1;
3922 break;
3923 }
3924
3925 /* Before we invoke realpath, which can get expensive when many
3926 files are involved, do a quick comparison of the basenames. */
3927 if (!basenames_may_differ
3928 && !file_matcher (lbasename (file_data->file_names[j]),
3929 data, 1))
3930 continue;
3931
3932 this_real_name = dw2_get_real_path (objfile, file_data, j);
3933 if (file_matcher (this_real_name, data, 0))
3934 {
3935 per_cu->v.quick->mark = 1;
3936 break;
3937 }
3938 }
3939
3940 slot = htab_find_slot (per_cu->v.quick->mark
3941 ? visited_found
3942 : visited_not_found,
3943 file_data, INSERT);
3944 *slot = file_data;
3945 }
3946
3947 do_cleanups (cleanup);
3948 }
3949
3950 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3951 {
3952 offset_type idx = 2 * iter;
3953 const char *name;
3954 offset_type *vec, vec_len, vec_idx;
3955 int global_seen = 0;
3956
3957 QUIT;
3958
3959 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3960 continue;
3961
3962 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3963
3964 if (! (*symbol_matcher) (name, data))
3965 continue;
3966
3967 /* The name was matched, now expand corresponding CUs that were
3968 marked. */
3969 vec = (offset_type *) (index->constant_pool
3970 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3971 vec_len = MAYBE_SWAP (vec[0]);
3972 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3973 {
3974 struct dwarf2_per_cu_data *per_cu;
3975 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3976 /* This value is only valid for index versions >= 7. */
3977 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3978 gdb_index_symbol_kind symbol_kind =
3979 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3980 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3981 /* Only check the symbol attributes if they're present.
3982 Indices prior to version 7 don't record them,
3983 and indices >= 7 may elide them for certain symbols
3984 (gold does this). */
3985 int attrs_valid =
3986 (index->version >= 7
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3988
3989 /* Work around gold/15646. */
3990 if (attrs_valid)
3991 {
3992 if (!is_static && global_seen)
3993 continue;
3994 if (!is_static)
3995 global_seen = 1;
3996 }
3997
3998 /* Only check the symbol's kind if it has one. */
3999 if (attrs_valid)
4000 {
4001 switch (kind)
4002 {
4003 case VARIABLES_DOMAIN:
4004 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4005 continue;
4006 break;
4007 case FUNCTIONS_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4009 continue;
4010 break;
4011 case TYPES_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
4020 /* Don't crash on bad data. */
4021 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4022 + dwarf2_per_objfile->n_type_units))
4023 {
4024 complaint (&symfile_complaints,
4025 _(".gdb_index entry has bad CU index"
4026 " [in module %s]"), objfile_name (objfile));
4027 continue;
4028 }
4029
4030 per_cu = dw2_get_cutu (cu_index);
4031 if (file_matcher == NULL || per_cu->v.quick->mark)
4032 {
4033 int symtab_was_null =
4034 (per_cu->v.quick->compunit_symtab == NULL);
4035
4036 dw2_instantiate_symtab (per_cu);
4037
4038 if (expansion_notify != NULL
4039 && symtab_was_null
4040 && per_cu->v.quick->compunit_symtab != NULL)
4041 {
4042 expansion_notify (per_cu->v.quick->compunit_symtab,
4043 data);
4044 }
4045 }
4046 }
4047 }
4048 }
4049
4050 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4051 symtab. */
4052
4053 static struct compunit_symtab *
4054 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4055 CORE_ADDR pc)
4056 {
4057 int i;
4058
4059 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4060 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4061 return cust;
4062
4063 if (cust->includes == NULL)
4064 return NULL;
4065
4066 for (i = 0; cust->includes[i]; ++i)
4067 {
4068 struct compunit_symtab *s = cust->includes[i];
4069
4070 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4071 if (s != NULL)
4072 return s;
4073 }
4074
4075 return NULL;
4076 }
4077
4078 static struct compunit_symtab *
4079 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4080 struct bound_minimal_symbol msymbol,
4081 CORE_ADDR pc,
4082 struct obj_section *section,
4083 int warn_if_readin)
4084 {
4085 struct dwarf2_per_cu_data *data;
4086 struct compunit_symtab *result;
4087
4088 dw2_setup (objfile);
4089
4090 if (!objfile->psymtabs_addrmap)
4091 return NULL;
4092
4093 data = addrmap_find (objfile->psymtabs_addrmap, pc);
4094 if (!data)
4095 return NULL;
4096
4097 if (warn_if_readin && data->v.quick->compunit_symtab)
4098 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4099 paddress (get_objfile_arch (objfile), pc));
4100
4101 result
4102 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4103 pc);
4104 gdb_assert (result != NULL);
4105 return result;
4106 }
4107
4108 static void
4109 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4110 void *data, int need_fullname)
4111 {
4112 int i;
4113 struct cleanup *cleanup;
4114 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4115 NULL, xcalloc, xfree);
4116
4117 cleanup = make_cleanup_htab_delete (visited);
4118 dw2_setup (objfile);
4119
4120 /* The rule is CUs specify all the files, including those used by
4121 any TU, so there's no need to scan TUs here.
4122 We can ignore file names coming from already-expanded CUs. */
4123
4124 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4125 {
4126 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4127
4128 if (per_cu->v.quick->compunit_symtab)
4129 {
4130 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
4131 INSERT);
4132
4133 *slot = per_cu->v.quick->file_names;
4134 }
4135 }
4136
4137 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4138 {
4139 int j;
4140 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4141 struct quick_file_names *file_data;
4142 void **slot;
4143
4144 /* We only need to look at symtabs not already expanded. */
4145 if (per_cu->v.quick->compunit_symtab)
4146 continue;
4147
4148 file_data = dw2_get_file_names (per_cu);
4149 if (file_data == NULL)
4150 continue;
4151
4152 slot = htab_find_slot (visited, file_data, INSERT);
4153 if (*slot)
4154 {
4155 /* Already visited. */
4156 continue;
4157 }
4158 *slot = file_data;
4159
4160 for (j = 0; j < file_data->num_file_names; ++j)
4161 {
4162 const char *this_real_name;
4163
4164 if (need_fullname)
4165 this_real_name = dw2_get_real_path (objfile, file_data, j);
4166 else
4167 this_real_name = NULL;
4168 (*fun) (file_data->file_names[j], this_real_name, data);
4169 }
4170 }
4171
4172 do_cleanups (cleanup);
4173 }
4174
4175 static int
4176 dw2_has_symbols (struct objfile *objfile)
4177 {
4178 return 1;
4179 }
4180
4181 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4182 {
4183 dw2_has_symbols,
4184 dw2_find_last_source_symtab,
4185 dw2_forget_cached_source_info,
4186 dw2_map_symtabs_matching_filename,
4187 dw2_lookup_symbol,
4188 dw2_print_stats,
4189 dw2_dump,
4190 dw2_relocate,
4191 dw2_expand_symtabs_for_function,
4192 dw2_expand_all_symtabs,
4193 dw2_expand_symtabs_with_fullname,
4194 dw2_map_matching_symbols,
4195 dw2_expand_symtabs_matching,
4196 dw2_find_pc_sect_compunit_symtab,
4197 dw2_map_symbol_filenames
4198 };
4199
4200 /* Initialize for reading DWARF for this objfile. Return 0 if this
4201 file will use psymtabs, or 1 if using the GNU index. */
4202
4203 int
4204 dwarf2_initialize_objfile (struct objfile *objfile)
4205 {
4206 /* If we're about to read full symbols, don't bother with the
4207 indices. In this case we also don't care if some other debug
4208 format is making psymtabs, because they are all about to be
4209 expanded anyway. */
4210 if ((objfile->flags & OBJF_READNOW))
4211 {
4212 int i;
4213
4214 dwarf2_per_objfile->using_index = 1;
4215 create_all_comp_units (objfile);
4216 create_all_type_units (objfile);
4217 dwarf2_per_objfile->quick_file_names_table =
4218 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4219
4220 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4221 + dwarf2_per_objfile->n_type_units); ++i)
4222 {
4223 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4224
4225 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4226 struct dwarf2_per_cu_quick_data);
4227 }
4228
4229 /* Return 1 so that gdb sees the "quick" functions. However,
4230 these functions will be no-ops because we will have expanded
4231 all symtabs. */
4232 return 1;
4233 }
4234
4235 if (dwarf2_read_index (objfile))
4236 return 1;
4237
4238 return 0;
4239 }
4240
4241 \f
4242
4243 /* Build a partial symbol table. */
4244
4245 void
4246 dwarf2_build_psymtabs (struct objfile *objfile)
4247 {
4248
4249 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4250 {
4251 init_psymbol_list (objfile, 1024);
4252 }
4253
4254 TRY
4255 {
4256 /* This isn't really ideal: all the data we allocate on the
4257 objfile's obstack is still uselessly kept around. However,
4258 freeing it seems unsafe. */
4259 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4260
4261 dwarf2_build_psymtabs_hard (objfile);
4262 discard_cleanups (cleanups);
4263 }
4264 CATCH (except, RETURN_MASK_ERROR)
4265 {
4266 exception_print (gdb_stderr, except);
4267 }
4268 END_CATCH
4269 }
4270
4271 /* Return the total length of the CU described by HEADER. */
4272
4273 static unsigned int
4274 get_cu_length (const struct comp_unit_head *header)
4275 {
4276 return header->initial_length_size + header->length;
4277 }
4278
4279 /* Return TRUE if OFFSET is within CU_HEADER. */
4280
4281 static inline int
4282 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4283 {
4284 sect_offset bottom = { cu_header->offset.sect_off };
4285 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4286
4287 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4288 }
4289
4290 /* Find the base address of the compilation unit for range lists and
4291 location lists. It will normally be specified by DW_AT_low_pc.
4292 In DWARF-3 draft 4, the base address could be overridden by
4293 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4294 compilation units with discontinuous ranges. */
4295
4296 static void
4297 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4298 {
4299 struct attribute *attr;
4300
4301 cu->base_known = 0;
4302 cu->base_address = 0;
4303
4304 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4305 if (attr)
4306 {
4307 cu->base_address = attr_value_as_address (attr);
4308 cu->base_known = 1;
4309 }
4310 else
4311 {
4312 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4313 if (attr)
4314 {
4315 cu->base_address = attr_value_as_address (attr);
4316 cu->base_known = 1;
4317 }
4318 }
4319 }
4320
4321 /* Read in the comp unit header information from the debug_info at info_ptr.
4322 NOTE: This leaves members offset, first_die_offset to be filled in
4323 by the caller. */
4324
4325 static const gdb_byte *
4326 read_comp_unit_head (struct comp_unit_head *cu_header,
4327 const gdb_byte *info_ptr, bfd *abfd)
4328 {
4329 int signed_addr;
4330 unsigned int bytes_read;
4331
4332 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4333 cu_header->initial_length_size = bytes_read;
4334 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4335 info_ptr += bytes_read;
4336 cu_header->version = read_2_bytes (abfd, info_ptr);
4337 info_ptr += 2;
4338 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4339 &bytes_read);
4340 info_ptr += bytes_read;
4341 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4342 info_ptr += 1;
4343 signed_addr = bfd_get_sign_extend_vma (abfd);
4344 if (signed_addr < 0)
4345 internal_error (__FILE__, __LINE__,
4346 _("read_comp_unit_head: dwarf from non elf file"));
4347 cu_header->signed_addr_p = signed_addr;
4348
4349 return info_ptr;
4350 }
4351
4352 /* Helper function that returns the proper abbrev section for
4353 THIS_CU. */
4354
4355 static struct dwarf2_section_info *
4356 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4357 {
4358 struct dwarf2_section_info *abbrev;
4359
4360 if (this_cu->is_dwz)
4361 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4362 else
4363 abbrev = &dwarf2_per_objfile->abbrev;
4364
4365 return abbrev;
4366 }
4367
4368 /* Subroutine of read_and_check_comp_unit_head and
4369 read_and_check_type_unit_head to simplify them.
4370 Perform various error checking on the header. */
4371
4372 static void
4373 error_check_comp_unit_head (struct comp_unit_head *header,
4374 struct dwarf2_section_info *section,
4375 struct dwarf2_section_info *abbrev_section)
4376 {
4377 bfd *abfd = get_section_bfd_owner (section);
4378 const char *filename = get_section_file_name (section);
4379
4380 if (header->version != 2 && header->version != 3 && header->version != 4)
4381 error (_("Dwarf Error: wrong version in compilation unit header "
4382 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4383 filename);
4384
4385 if (header->abbrev_offset.sect_off
4386 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4387 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4388 "(offset 0x%lx + 6) [in module %s]"),
4389 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4390 filename);
4391
4392 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4393 avoid potential 32-bit overflow. */
4394 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4395 > section->size)
4396 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4397 "(offset 0x%lx + 0) [in module %s]"),
4398 (long) header->length, (long) header->offset.sect_off,
4399 filename);
4400 }
4401
4402 /* Read in a CU/TU header and perform some basic error checking.
4403 The contents of the header are stored in HEADER.
4404 The result is a pointer to the start of the first DIE. */
4405
4406 static const gdb_byte *
4407 read_and_check_comp_unit_head (struct comp_unit_head *header,
4408 struct dwarf2_section_info *section,
4409 struct dwarf2_section_info *abbrev_section,
4410 const gdb_byte *info_ptr,
4411 int is_debug_types_section)
4412 {
4413 const gdb_byte *beg_of_comp_unit = info_ptr;
4414 bfd *abfd = get_section_bfd_owner (section);
4415
4416 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4417
4418 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4419
4420 /* If we're reading a type unit, skip over the signature and
4421 type_offset fields. */
4422 if (is_debug_types_section)
4423 info_ptr += 8 /*signature*/ + header->offset_size;
4424
4425 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4426
4427 error_check_comp_unit_head (header, section, abbrev_section);
4428
4429 return info_ptr;
4430 }
4431
4432 /* Read in the types comp unit header information from .debug_types entry at
4433 types_ptr. The result is a pointer to one past the end of the header. */
4434
4435 static const gdb_byte *
4436 read_and_check_type_unit_head (struct comp_unit_head *header,
4437 struct dwarf2_section_info *section,
4438 struct dwarf2_section_info *abbrev_section,
4439 const gdb_byte *info_ptr,
4440 ULONGEST *signature,
4441 cu_offset *type_offset_in_tu)
4442 {
4443 const gdb_byte *beg_of_comp_unit = info_ptr;
4444 bfd *abfd = get_section_bfd_owner (section);
4445
4446 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4447
4448 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4449
4450 /* If we're reading a type unit, skip over the signature and
4451 type_offset fields. */
4452 if (signature != NULL)
4453 *signature = read_8_bytes (abfd, info_ptr);
4454 info_ptr += 8;
4455 if (type_offset_in_tu != NULL)
4456 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4457 header->offset_size);
4458 info_ptr += header->offset_size;
4459
4460 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4461
4462 error_check_comp_unit_head (header, section, abbrev_section);
4463
4464 return info_ptr;
4465 }
4466
4467 /* Fetch the abbreviation table offset from a comp or type unit header. */
4468
4469 static sect_offset
4470 read_abbrev_offset (struct dwarf2_section_info *section,
4471 sect_offset offset)
4472 {
4473 bfd *abfd = get_section_bfd_owner (section);
4474 const gdb_byte *info_ptr;
4475 unsigned int length, initial_length_size, offset_size;
4476 sect_offset abbrev_offset;
4477
4478 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4479 info_ptr = section->buffer + offset.sect_off;
4480 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4481 offset_size = initial_length_size == 4 ? 4 : 8;
4482 info_ptr += initial_length_size + 2 /*version*/;
4483 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4484 return abbrev_offset;
4485 }
4486
4487 /* Allocate a new partial symtab for file named NAME and mark this new
4488 partial symtab as being an include of PST. */
4489
4490 static void
4491 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4492 struct objfile *objfile)
4493 {
4494 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4495
4496 if (!IS_ABSOLUTE_PATH (subpst->filename))
4497 {
4498 /* It shares objfile->objfile_obstack. */
4499 subpst->dirname = pst->dirname;
4500 }
4501
4502 subpst->textlow = 0;
4503 subpst->texthigh = 0;
4504
4505 subpst->dependencies = (struct partial_symtab **)
4506 obstack_alloc (&objfile->objfile_obstack,
4507 sizeof (struct partial_symtab *));
4508 subpst->dependencies[0] = pst;
4509 subpst->number_of_dependencies = 1;
4510
4511 subpst->globals_offset = 0;
4512 subpst->n_global_syms = 0;
4513 subpst->statics_offset = 0;
4514 subpst->n_static_syms = 0;
4515 subpst->compunit_symtab = NULL;
4516 subpst->read_symtab = pst->read_symtab;
4517 subpst->readin = 0;
4518
4519 /* No private part is necessary for include psymtabs. This property
4520 can be used to differentiate between such include psymtabs and
4521 the regular ones. */
4522 subpst->read_symtab_private = NULL;
4523 }
4524
4525 /* Read the Line Number Program data and extract the list of files
4526 included by the source file represented by PST. Build an include
4527 partial symtab for each of these included files. */
4528
4529 static void
4530 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4531 struct die_info *die,
4532 struct partial_symtab *pst)
4533 {
4534 struct line_header *lh = NULL;
4535 struct attribute *attr;
4536
4537 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4538 if (attr)
4539 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4540 if (lh == NULL)
4541 return; /* No linetable, so no includes. */
4542
4543 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4544 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
4545
4546 free_line_header (lh);
4547 }
4548
4549 static hashval_t
4550 hash_signatured_type (const void *item)
4551 {
4552 const struct signatured_type *sig_type = item;
4553
4554 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4555 return sig_type->signature;
4556 }
4557
4558 static int
4559 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4560 {
4561 const struct signatured_type *lhs = item_lhs;
4562 const struct signatured_type *rhs = item_rhs;
4563
4564 return lhs->signature == rhs->signature;
4565 }
4566
4567 /* Allocate a hash table for signatured types. */
4568
4569 static htab_t
4570 allocate_signatured_type_table (struct objfile *objfile)
4571 {
4572 return htab_create_alloc_ex (41,
4573 hash_signatured_type,
4574 eq_signatured_type,
4575 NULL,
4576 &objfile->objfile_obstack,
4577 hashtab_obstack_allocate,
4578 dummy_obstack_deallocate);
4579 }
4580
4581 /* A helper function to add a signatured type CU to a table. */
4582
4583 static int
4584 add_signatured_type_cu_to_table (void **slot, void *datum)
4585 {
4586 struct signatured_type *sigt = *slot;
4587 struct signatured_type ***datap = datum;
4588
4589 **datap = sigt;
4590 ++*datap;
4591
4592 return 1;
4593 }
4594
4595 /* Create the hash table of all entries in the .debug_types
4596 (or .debug_types.dwo) section(s).
4597 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4598 otherwise it is NULL.
4599
4600 The result is a pointer to the hash table or NULL if there are no types.
4601
4602 Note: This function processes DWO files only, not DWP files. */
4603
4604 static htab_t
4605 create_debug_types_hash_table (struct dwo_file *dwo_file,
4606 VEC (dwarf2_section_info_def) *types)
4607 {
4608 struct objfile *objfile = dwarf2_per_objfile->objfile;
4609 htab_t types_htab = NULL;
4610 int ix;
4611 struct dwarf2_section_info *section;
4612 struct dwarf2_section_info *abbrev_section;
4613
4614 if (VEC_empty (dwarf2_section_info_def, types))
4615 return NULL;
4616
4617 abbrev_section = (dwo_file != NULL
4618 ? &dwo_file->sections.abbrev
4619 : &dwarf2_per_objfile->abbrev);
4620
4621 if (dwarf_read_debug)
4622 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4623 dwo_file ? ".dwo" : "",
4624 get_section_file_name (abbrev_section));
4625
4626 for (ix = 0;
4627 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4628 ++ix)
4629 {
4630 bfd *abfd;
4631 const gdb_byte *info_ptr, *end_ptr;
4632
4633 dwarf2_read_section (objfile, section);
4634 info_ptr = section->buffer;
4635
4636 if (info_ptr == NULL)
4637 continue;
4638
4639 /* We can't set abfd until now because the section may be empty or
4640 not present, in which case the bfd is unknown. */
4641 abfd = get_section_bfd_owner (section);
4642
4643 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4644 because we don't need to read any dies: the signature is in the
4645 header. */
4646
4647 end_ptr = info_ptr + section->size;
4648 while (info_ptr < end_ptr)
4649 {
4650 sect_offset offset;
4651 cu_offset type_offset_in_tu;
4652 ULONGEST signature;
4653 struct signatured_type *sig_type;
4654 struct dwo_unit *dwo_tu;
4655 void **slot;
4656 const gdb_byte *ptr = info_ptr;
4657 struct comp_unit_head header;
4658 unsigned int length;
4659
4660 offset.sect_off = ptr - section->buffer;
4661
4662 /* We need to read the type's signature in order to build the hash
4663 table, but we don't need anything else just yet. */
4664
4665 ptr = read_and_check_type_unit_head (&header, section,
4666 abbrev_section, ptr,
4667 &signature, &type_offset_in_tu);
4668
4669 length = get_cu_length (&header);
4670
4671 /* Skip dummy type units. */
4672 if (ptr >= info_ptr + length
4673 || peek_abbrev_code (abfd, ptr) == 0)
4674 {
4675 info_ptr += length;
4676 continue;
4677 }
4678
4679 if (types_htab == NULL)
4680 {
4681 if (dwo_file)
4682 types_htab = allocate_dwo_unit_table (objfile);
4683 else
4684 types_htab = allocate_signatured_type_table (objfile);
4685 }
4686
4687 if (dwo_file)
4688 {
4689 sig_type = NULL;
4690 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4691 struct dwo_unit);
4692 dwo_tu->dwo_file = dwo_file;
4693 dwo_tu->signature = signature;
4694 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4695 dwo_tu->section = section;
4696 dwo_tu->offset = offset;
4697 dwo_tu->length = length;
4698 }
4699 else
4700 {
4701 /* N.B.: type_offset is not usable if this type uses a DWO file.
4702 The real type_offset is in the DWO file. */
4703 dwo_tu = NULL;
4704 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4705 struct signatured_type);
4706 sig_type->signature = signature;
4707 sig_type->type_offset_in_tu = type_offset_in_tu;
4708 sig_type->per_cu.objfile = objfile;
4709 sig_type->per_cu.is_debug_types = 1;
4710 sig_type->per_cu.section = section;
4711 sig_type->per_cu.offset = offset;
4712 sig_type->per_cu.length = length;
4713 }
4714
4715 slot = htab_find_slot (types_htab,
4716 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4717 INSERT);
4718 gdb_assert (slot != NULL);
4719 if (*slot != NULL)
4720 {
4721 sect_offset dup_offset;
4722
4723 if (dwo_file)
4724 {
4725 const struct dwo_unit *dup_tu = *slot;
4726
4727 dup_offset = dup_tu->offset;
4728 }
4729 else
4730 {
4731 const struct signatured_type *dup_tu = *slot;
4732
4733 dup_offset = dup_tu->per_cu.offset;
4734 }
4735
4736 complaint (&symfile_complaints,
4737 _("debug type entry at offset 0x%x is duplicate to"
4738 " the entry at offset 0x%x, signature %s"),
4739 offset.sect_off, dup_offset.sect_off,
4740 hex_string (signature));
4741 }
4742 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4743
4744 if (dwarf_read_debug > 1)
4745 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4746 offset.sect_off,
4747 hex_string (signature));
4748
4749 info_ptr += length;
4750 }
4751 }
4752
4753 return types_htab;
4754 }
4755
4756 /* Create the hash table of all entries in the .debug_types section,
4757 and initialize all_type_units.
4758 The result is zero if there is an error (e.g. missing .debug_types section),
4759 otherwise non-zero. */
4760
4761 static int
4762 create_all_type_units (struct objfile *objfile)
4763 {
4764 htab_t types_htab;
4765 struct signatured_type **iter;
4766
4767 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4768 if (types_htab == NULL)
4769 {
4770 dwarf2_per_objfile->signatured_types = NULL;
4771 return 0;
4772 }
4773
4774 dwarf2_per_objfile->signatured_types = types_htab;
4775
4776 dwarf2_per_objfile->n_type_units
4777 = dwarf2_per_objfile->n_allocated_type_units
4778 = htab_elements (types_htab);
4779 dwarf2_per_objfile->all_type_units
4780 = xmalloc (dwarf2_per_objfile->n_type_units
4781 * sizeof (struct signatured_type *));
4782 iter = &dwarf2_per_objfile->all_type_units[0];
4783 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4784 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4785 == dwarf2_per_objfile->n_type_units);
4786
4787 return 1;
4788 }
4789
4790 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4791 If SLOT is non-NULL, it is the entry to use in the hash table.
4792 Otherwise we find one. */
4793
4794 static struct signatured_type *
4795 add_type_unit (ULONGEST sig, void **slot)
4796 {
4797 struct objfile *objfile = dwarf2_per_objfile->objfile;
4798 int n_type_units = dwarf2_per_objfile->n_type_units;
4799 struct signatured_type *sig_type;
4800
4801 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4802 ++n_type_units;
4803 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4804 {
4805 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4806 dwarf2_per_objfile->n_allocated_type_units = 1;
4807 dwarf2_per_objfile->n_allocated_type_units *= 2;
4808 dwarf2_per_objfile->all_type_units
4809 = xrealloc (dwarf2_per_objfile->all_type_units,
4810 dwarf2_per_objfile->n_allocated_type_units
4811 * sizeof (struct signatured_type *));
4812 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4813 }
4814 dwarf2_per_objfile->n_type_units = n_type_units;
4815
4816 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4817 struct signatured_type);
4818 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4819 sig_type->signature = sig;
4820 sig_type->per_cu.is_debug_types = 1;
4821 if (dwarf2_per_objfile->using_index)
4822 {
4823 sig_type->per_cu.v.quick =
4824 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4825 struct dwarf2_per_cu_quick_data);
4826 }
4827
4828 if (slot == NULL)
4829 {
4830 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4831 sig_type, INSERT);
4832 }
4833 gdb_assert (*slot == NULL);
4834 *slot = sig_type;
4835 /* The rest of sig_type must be filled in by the caller. */
4836 return sig_type;
4837 }
4838
4839 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4840 Fill in SIG_ENTRY with DWO_ENTRY. */
4841
4842 static void
4843 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4844 struct signatured_type *sig_entry,
4845 struct dwo_unit *dwo_entry)
4846 {
4847 /* Make sure we're not clobbering something we don't expect to. */
4848 gdb_assert (! sig_entry->per_cu.queued);
4849 gdb_assert (sig_entry->per_cu.cu == NULL);
4850 if (dwarf2_per_objfile->using_index)
4851 {
4852 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4853 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4854 }
4855 else
4856 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4857 gdb_assert (sig_entry->signature == dwo_entry->signature);
4858 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4859 gdb_assert (sig_entry->type_unit_group == NULL);
4860 gdb_assert (sig_entry->dwo_unit == NULL);
4861
4862 sig_entry->per_cu.section = dwo_entry->section;
4863 sig_entry->per_cu.offset = dwo_entry->offset;
4864 sig_entry->per_cu.length = dwo_entry->length;
4865 sig_entry->per_cu.reading_dwo_directly = 1;
4866 sig_entry->per_cu.objfile = objfile;
4867 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4868 sig_entry->dwo_unit = dwo_entry;
4869 }
4870
4871 /* Subroutine of lookup_signatured_type.
4872 If we haven't read the TU yet, create the signatured_type data structure
4873 for a TU to be read in directly from a DWO file, bypassing the stub.
4874 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4875 using .gdb_index, then when reading a CU we want to stay in the DWO file
4876 containing that CU. Otherwise we could end up reading several other DWO
4877 files (due to comdat folding) to process the transitive closure of all the
4878 mentioned TUs, and that can be slow. The current DWO file will have every
4879 type signature that it needs.
4880 We only do this for .gdb_index because in the psymtab case we already have
4881 to read all the DWOs to build the type unit groups. */
4882
4883 static struct signatured_type *
4884 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4885 {
4886 struct objfile *objfile = dwarf2_per_objfile->objfile;
4887 struct dwo_file *dwo_file;
4888 struct dwo_unit find_dwo_entry, *dwo_entry;
4889 struct signatured_type find_sig_entry, *sig_entry;
4890 void **slot;
4891
4892 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4893
4894 /* If TU skeletons have been removed then we may not have read in any
4895 TUs yet. */
4896 if (dwarf2_per_objfile->signatured_types == NULL)
4897 {
4898 dwarf2_per_objfile->signatured_types
4899 = allocate_signatured_type_table (objfile);
4900 }
4901
4902 /* We only ever need to read in one copy of a signatured type.
4903 Use the global signatured_types array to do our own comdat-folding
4904 of types. If this is the first time we're reading this TU, and
4905 the TU has an entry in .gdb_index, replace the recorded data from
4906 .gdb_index with this TU. */
4907
4908 find_sig_entry.signature = sig;
4909 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4910 &find_sig_entry, INSERT);
4911 sig_entry = *slot;
4912
4913 /* We can get here with the TU already read, *or* in the process of being
4914 read. Don't reassign the global entry to point to this DWO if that's
4915 the case. Also note that if the TU is already being read, it may not
4916 have come from a DWO, the program may be a mix of Fission-compiled
4917 code and non-Fission-compiled code. */
4918
4919 /* Have we already tried to read this TU?
4920 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4921 needn't exist in the global table yet). */
4922 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
4923 return sig_entry;
4924
4925 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4926 dwo_unit of the TU itself. */
4927 dwo_file = cu->dwo_unit->dwo_file;
4928
4929 /* Ok, this is the first time we're reading this TU. */
4930 if (dwo_file->tus == NULL)
4931 return NULL;
4932 find_dwo_entry.signature = sig;
4933 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4934 if (dwo_entry == NULL)
4935 return NULL;
4936
4937 /* If the global table doesn't have an entry for this TU, add one. */
4938 if (sig_entry == NULL)
4939 sig_entry = add_type_unit (sig, slot);
4940
4941 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4942 sig_entry->per_cu.tu_read = 1;
4943 return sig_entry;
4944 }
4945
4946 /* Subroutine of lookup_signatured_type.
4947 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4948 then try the DWP file. If the TU stub (skeleton) has been removed then
4949 it won't be in .gdb_index. */
4950
4951 static struct signatured_type *
4952 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4953 {
4954 struct objfile *objfile = dwarf2_per_objfile->objfile;
4955 struct dwp_file *dwp_file = get_dwp_file ();
4956 struct dwo_unit *dwo_entry;
4957 struct signatured_type find_sig_entry, *sig_entry;
4958 void **slot;
4959
4960 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4961 gdb_assert (dwp_file != NULL);
4962
4963 /* If TU skeletons have been removed then we may not have read in any
4964 TUs yet. */
4965 if (dwarf2_per_objfile->signatured_types == NULL)
4966 {
4967 dwarf2_per_objfile->signatured_types
4968 = allocate_signatured_type_table (objfile);
4969 }
4970
4971 find_sig_entry.signature = sig;
4972 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4973 &find_sig_entry, INSERT);
4974 sig_entry = *slot;
4975
4976 /* Have we already tried to read this TU?
4977 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4978 needn't exist in the global table yet). */
4979 if (sig_entry != NULL)
4980 return sig_entry;
4981
4982 if (dwp_file->tus == NULL)
4983 return NULL;
4984 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4985 sig, 1 /* is_debug_types */);
4986 if (dwo_entry == NULL)
4987 return NULL;
4988
4989 sig_entry = add_type_unit (sig, slot);
4990 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4991
4992 return sig_entry;
4993 }
4994
4995 /* Lookup a signature based type for DW_FORM_ref_sig8.
4996 Returns NULL if signature SIG is not present in the table.
4997 It is up to the caller to complain about this. */
4998
4999 static struct signatured_type *
5000 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5001 {
5002 if (cu->dwo_unit
5003 && dwarf2_per_objfile->using_index)
5004 {
5005 /* We're in a DWO/DWP file, and we're using .gdb_index.
5006 These cases require special processing. */
5007 if (get_dwp_file () == NULL)
5008 return lookup_dwo_signatured_type (cu, sig);
5009 else
5010 return lookup_dwp_signatured_type (cu, sig);
5011 }
5012 else
5013 {
5014 struct signatured_type find_entry, *entry;
5015
5016 if (dwarf2_per_objfile->signatured_types == NULL)
5017 return NULL;
5018 find_entry.signature = sig;
5019 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
5020 return entry;
5021 }
5022 }
5023 \f
5024 /* Low level DIE reading support. */
5025
5026 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5027
5028 static void
5029 init_cu_die_reader (struct die_reader_specs *reader,
5030 struct dwarf2_cu *cu,
5031 struct dwarf2_section_info *section,
5032 struct dwo_file *dwo_file)
5033 {
5034 gdb_assert (section->readin && section->buffer != NULL);
5035 reader->abfd = get_section_bfd_owner (section);
5036 reader->cu = cu;
5037 reader->dwo_file = dwo_file;
5038 reader->die_section = section;
5039 reader->buffer = section->buffer;
5040 reader->buffer_end = section->buffer + section->size;
5041 reader->comp_dir = NULL;
5042 }
5043
5044 /* Subroutine of init_cutu_and_read_dies to simplify it.
5045 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5046 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5047 already.
5048
5049 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5050 from it to the DIE in the DWO. If NULL we are skipping the stub.
5051 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5052 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5053 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5054 STUB_COMP_DIR may be non-NULL.
5055 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5056 are filled in with the info of the DIE from the DWO file.
5057 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5058 provided an abbrev table to use.
5059 The result is non-zero if a valid (non-dummy) DIE was found. */
5060
5061 static int
5062 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5063 struct dwo_unit *dwo_unit,
5064 int abbrev_table_provided,
5065 struct die_info *stub_comp_unit_die,
5066 const char *stub_comp_dir,
5067 struct die_reader_specs *result_reader,
5068 const gdb_byte **result_info_ptr,
5069 struct die_info **result_comp_unit_die,
5070 int *result_has_children)
5071 {
5072 struct objfile *objfile = dwarf2_per_objfile->objfile;
5073 struct dwarf2_cu *cu = this_cu->cu;
5074 struct dwarf2_section_info *section;
5075 bfd *abfd;
5076 const gdb_byte *begin_info_ptr, *info_ptr;
5077 ULONGEST signature; /* Or dwo_id. */
5078 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5079 int i,num_extra_attrs;
5080 struct dwarf2_section_info *dwo_abbrev_section;
5081 struct attribute *attr;
5082 struct die_info *comp_unit_die;
5083
5084 /* At most one of these may be provided. */
5085 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5086
5087 /* These attributes aren't processed until later:
5088 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5089 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5090 referenced later. However, these attributes are found in the stub
5091 which we won't have later. In order to not impose this complication
5092 on the rest of the code, we read them here and copy them to the
5093 DWO CU/TU die. */
5094
5095 stmt_list = NULL;
5096 low_pc = NULL;
5097 high_pc = NULL;
5098 ranges = NULL;
5099 comp_dir = NULL;
5100
5101 if (stub_comp_unit_die != NULL)
5102 {
5103 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5104 DWO file. */
5105 if (! this_cu->is_debug_types)
5106 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5107 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5108 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5109 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5110 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5111
5112 /* There should be a DW_AT_addr_base attribute here (if needed).
5113 We need the value before we can process DW_FORM_GNU_addr_index. */
5114 cu->addr_base = 0;
5115 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5116 if (attr)
5117 cu->addr_base = DW_UNSND (attr);
5118
5119 /* There should be a DW_AT_ranges_base attribute here (if needed).
5120 We need the value before we can process DW_AT_ranges. */
5121 cu->ranges_base = 0;
5122 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5123 if (attr)
5124 cu->ranges_base = DW_UNSND (attr);
5125 }
5126 else if (stub_comp_dir != NULL)
5127 {
5128 /* Reconstruct the comp_dir attribute to simplify the code below. */
5129 comp_dir = (struct attribute *)
5130 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
5131 comp_dir->name = DW_AT_comp_dir;
5132 comp_dir->form = DW_FORM_string;
5133 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5134 DW_STRING (comp_dir) = stub_comp_dir;
5135 }
5136
5137 /* Set up for reading the DWO CU/TU. */
5138 cu->dwo_unit = dwo_unit;
5139 section = dwo_unit->section;
5140 dwarf2_read_section (objfile, section);
5141 abfd = get_section_bfd_owner (section);
5142 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5143 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5144 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5145
5146 if (this_cu->is_debug_types)
5147 {
5148 ULONGEST header_signature;
5149 cu_offset type_offset_in_tu;
5150 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5151
5152 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5153 dwo_abbrev_section,
5154 info_ptr,
5155 &header_signature,
5156 &type_offset_in_tu);
5157 /* This is not an assert because it can be caused by bad debug info. */
5158 if (sig_type->signature != header_signature)
5159 {
5160 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5161 " TU at offset 0x%x [in module %s]"),
5162 hex_string (sig_type->signature),
5163 hex_string (header_signature),
5164 dwo_unit->offset.sect_off,
5165 bfd_get_filename (abfd));
5166 }
5167 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5168 /* For DWOs coming from DWP files, we don't know the CU length
5169 nor the type's offset in the TU until now. */
5170 dwo_unit->length = get_cu_length (&cu->header);
5171 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5172
5173 /* Establish the type offset that can be used to lookup the type.
5174 For DWO files, we don't know it until now. */
5175 sig_type->type_offset_in_section.sect_off =
5176 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5177 }
5178 else
5179 {
5180 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5181 dwo_abbrev_section,
5182 info_ptr, 0);
5183 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5184 /* For DWOs coming from DWP files, we don't know the CU length
5185 until now. */
5186 dwo_unit->length = get_cu_length (&cu->header);
5187 }
5188
5189 /* Replace the CU's original abbrev table with the DWO's.
5190 Reminder: We can't read the abbrev table until we've read the header. */
5191 if (abbrev_table_provided)
5192 {
5193 /* Don't free the provided abbrev table, the caller of
5194 init_cutu_and_read_dies owns it. */
5195 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5196 /* Ensure the DWO abbrev table gets freed. */
5197 make_cleanup (dwarf2_free_abbrev_table, cu);
5198 }
5199 else
5200 {
5201 dwarf2_free_abbrev_table (cu);
5202 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5203 /* Leave any existing abbrev table cleanup as is. */
5204 }
5205
5206 /* Read in the die, but leave space to copy over the attributes
5207 from the stub. This has the benefit of simplifying the rest of
5208 the code - all the work to maintain the illusion of a single
5209 DW_TAG_{compile,type}_unit DIE is done here. */
5210 num_extra_attrs = ((stmt_list != NULL)
5211 + (low_pc != NULL)
5212 + (high_pc != NULL)
5213 + (ranges != NULL)
5214 + (comp_dir != NULL));
5215 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5216 result_has_children, num_extra_attrs);
5217
5218 /* Copy over the attributes from the stub to the DIE we just read in. */
5219 comp_unit_die = *result_comp_unit_die;
5220 i = comp_unit_die->num_attrs;
5221 if (stmt_list != NULL)
5222 comp_unit_die->attrs[i++] = *stmt_list;
5223 if (low_pc != NULL)
5224 comp_unit_die->attrs[i++] = *low_pc;
5225 if (high_pc != NULL)
5226 comp_unit_die->attrs[i++] = *high_pc;
5227 if (ranges != NULL)
5228 comp_unit_die->attrs[i++] = *ranges;
5229 if (comp_dir != NULL)
5230 comp_unit_die->attrs[i++] = *comp_dir;
5231 comp_unit_die->num_attrs += num_extra_attrs;
5232
5233 if (dwarf_die_debug)
5234 {
5235 fprintf_unfiltered (gdb_stdlog,
5236 "Read die from %s@0x%x of %s:\n",
5237 get_section_name (section),
5238 (unsigned) (begin_info_ptr - section->buffer),
5239 bfd_get_filename (abfd));
5240 dump_die (comp_unit_die, dwarf_die_debug);
5241 }
5242
5243 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5244 TUs by skipping the stub and going directly to the entry in the DWO file.
5245 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5246 to get it via circuitous means. Blech. */
5247 if (comp_dir != NULL)
5248 result_reader->comp_dir = DW_STRING (comp_dir);
5249
5250 /* Skip dummy compilation units. */
5251 if (info_ptr >= begin_info_ptr + dwo_unit->length
5252 || peek_abbrev_code (abfd, info_ptr) == 0)
5253 return 0;
5254
5255 *result_info_ptr = info_ptr;
5256 return 1;
5257 }
5258
5259 /* Subroutine of init_cutu_and_read_dies to simplify it.
5260 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5261 Returns NULL if the specified DWO unit cannot be found. */
5262
5263 static struct dwo_unit *
5264 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5265 struct die_info *comp_unit_die)
5266 {
5267 struct dwarf2_cu *cu = this_cu->cu;
5268 struct attribute *attr;
5269 ULONGEST signature;
5270 struct dwo_unit *dwo_unit;
5271 const char *comp_dir, *dwo_name;
5272
5273 gdb_assert (cu != NULL);
5274
5275 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5276 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5277 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5278
5279 if (this_cu->is_debug_types)
5280 {
5281 struct signatured_type *sig_type;
5282
5283 /* Since this_cu is the first member of struct signatured_type,
5284 we can go from a pointer to one to a pointer to the other. */
5285 sig_type = (struct signatured_type *) this_cu;
5286 signature = sig_type->signature;
5287 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5288 }
5289 else
5290 {
5291 struct attribute *attr;
5292
5293 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5294 if (! attr)
5295 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5296 " [in module %s]"),
5297 dwo_name, objfile_name (this_cu->objfile));
5298 signature = DW_UNSND (attr);
5299 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5300 signature);
5301 }
5302
5303 return dwo_unit;
5304 }
5305
5306 /* Subroutine of init_cutu_and_read_dies to simplify it.
5307 See it for a description of the parameters.
5308 Read a TU directly from a DWO file, bypassing the stub.
5309
5310 Note: This function could be a little bit simpler if we shared cleanups
5311 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5312 to do, so we keep this function self-contained. Or we could move this
5313 into our caller, but it's complex enough already. */
5314
5315 static void
5316 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5317 int use_existing_cu, int keep,
5318 die_reader_func_ftype *die_reader_func,
5319 void *data)
5320 {
5321 struct dwarf2_cu *cu;
5322 struct signatured_type *sig_type;
5323 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5324 struct die_reader_specs reader;
5325 const gdb_byte *info_ptr;
5326 struct die_info *comp_unit_die;
5327 int has_children;
5328
5329 /* Verify we can do the following downcast, and that we have the
5330 data we need. */
5331 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5332 sig_type = (struct signatured_type *) this_cu;
5333 gdb_assert (sig_type->dwo_unit != NULL);
5334
5335 cleanups = make_cleanup (null_cleanup, NULL);
5336
5337 if (use_existing_cu && this_cu->cu != NULL)
5338 {
5339 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5340 cu = this_cu->cu;
5341 /* There's no need to do the rereading_dwo_cu handling that
5342 init_cutu_and_read_dies does since we don't read the stub. */
5343 }
5344 else
5345 {
5346 /* If !use_existing_cu, this_cu->cu must be NULL. */
5347 gdb_assert (this_cu->cu == NULL);
5348 cu = xmalloc (sizeof (*cu));
5349 init_one_comp_unit (cu, this_cu);
5350 /* If an error occurs while loading, release our storage. */
5351 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5352 }
5353
5354 /* A future optimization, if needed, would be to use an existing
5355 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5356 could share abbrev tables. */
5357
5358 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5359 0 /* abbrev_table_provided */,
5360 NULL /* stub_comp_unit_die */,
5361 sig_type->dwo_unit->dwo_file->comp_dir,
5362 &reader, &info_ptr,
5363 &comp_unit_die, &has_children) == 0)
5364 {
5365 /* Dummy die. */
5366 do_cleanups (cleanups);
5367 return;
5368 }
5369
5370 /* All the "real" work is done here. */
5371 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5372
5373 /* This duplicates the code in init_cutu_and_read_dies,
5374 but the alternative is making the latter more complex.
5375 This function is only for the special case of using DWO files directly:
5376 no point in overly complicating the general case just to handle this. */
5377 if (free_cu_cleanup != NULL)
5378 {
5379 if (keep)
5380 {
5381 /* We've successfully allocated this compilation unit. Let our
5382 caller clean it up when finished with it. */
5383 discard_cleanups (free_cu_cleanup);
5384
5385 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5386 So we have to manually free the abbrev table. */
5387 dwarf2_free_abbrev_table (cu);
5388
5389 /* Link this CU into read_in_chain. */
5390 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5391 dwarf2_per_objfile->read_in_chain = this_cu;
5392 }
5393 else
5394 do_cleanups (free_cu_cleanup);
5395 }
5396
5397 do_cleanups (cleanups);
5398 }
5399
5400 /* Initialize a CU (or TU) and read its DIEs.
5401 If the CU defers to a DWO file, read the DWO file as well.
5402
5403 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5404 Otherwise the table specified in the comp unit header is read in and used.
5405 This is an optimization for when we already have the abbrev table.
5406
5407 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5408 Otherwise, a new CU is allocated with xmalloc.
5409
5410 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5411 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5412
5413 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5414 linker) then DIE_READER_FUNC will not get called. */
5415
5416 static void
5417 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5418 struct abbrev_table *abbrev_table,
5419 int use_existing_cu, int keep,
5420 die_reader_func_ftype *die_reader_func,
5421 void *data)
5422 {
5423 struct objfile *objfile = dwarf2_per_objfile->objfile;
5424 struct dwarf2_section_info *section = this_cu->section;
5425 bfd *abfd = get_section_bfd_owner (section);
5426 struct dwarf2_cu *cu;
5427 const gdb_byte *begin_info_ptr, *info_ptr;
5428 struct die_reader_specs reader;
5429 struct die_info *comp_unit_die;
5430 int has_children;
5431 struct attribute *attr;
5432 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5433 struct signatured_type *sig_type = NULL;
5434 struct dwarf2_section_info *abbrev_section;
5435 /* Non-zero if CU currently points to a DWO file and we need to
5436 reread it. When this happens we need to reread the skeleton die
5437 before we can reread the DWO file (this only applies to CUs, not TUs). */
5438 int rereading_dwo_cu = 0;
5439
5440 if (dwarf_die_debug)
5441 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5442 this_cu->is_debug_types ? "type" : "comp",
5443 this_cu->offset.sect_off);
5444
5445 if (use_existing_cu)
5446 gdb_assert (keep);
5447
5448 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5449 file (instead of going through the stub), short-circuit all of this. */
5450 if (this_cu->reading_dwo_directly)
5451 {
5452 /* Narrow down the scope of possibilities to have to understand. */
5453 gdb_assert (this_cu->is_debug_types);
5454 gdb_assert (abbrev_table == NULL);
5455 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5456 die_reader_func, data);
5457 return;
5458 }
5459
5460 cleanups = make_cleanup (null_cleanup, NULL);
5461
5462 /* This is cheap if the section is already read in. */
5463 dwarf2_read_section (objfile, section);
5464
5465 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5466
5467 abbrev_section = get_abbrev_section_for_cu (this_cu);
5468
5469 if (use_existing_cu && this_cu->cu != NULL)
5470 {
5471 cu = this_cu->cu;
5472 /* If this CU is from a DWO file we need to start over, we need to
5473 refetch the attributes from the skeleton CU.
5474 This could be optimized by retrieving those attributes from when we
5475 were here the first time: the previous comp_unit_die was stored in
5476 comp_unit_obstack. But there's no data yet that we need this
5477 optimization. */
5478 if (cu->dwo_unit != NULL)
5479 rereading_dwo_cu = 1;
5480 }
5481 else
5482 {
5483 /* If !use_existing_cu, this_cu->cu must be NULL. */
5484 gdb_assert (this_cu->cu == NULL);
5485 cu = xmalloc (sizeof (*cu));
5486 init_one_comp_unit (cu, this_cu);
5487 /* If an error occurs while loading, release our storage. */
5488 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5489 }
5490
5491 /* Get the header. */
5492 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5493 {
5494 /* We already have the header, there's no need to read it in again. */
5495 info_ptr += cu->header.first_die_offset.cu_off;
5496 }
5497 else
5498 {
5499 if (this_cu->is_debug_types)
5500 {
5501 ULONGEST signature;
5502 cu_offset type_offset_in_tu;
5503
5504 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5505 abbrev_section, info_ptr,
5506 &signature,
5507 &type_offset_in_tu);
5508
5509 /* Since per_cu is the first member of struct signatured_type,
5510 we can go from a pointer to one to a pointer to the other. */
5511 sig_type = (struct signatured_type *) this_cu;
5512 gdb_assert (sig_type->signature == signature);
5513 gdb_assert (sig_type->type_offset_in_tu.cu_off
5514 == type_offset_in_tu.cu_off);
5515 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5516
5517 /* LENGTH has not been set yet for type units if we're
5518 using .gdb_index. */
5519 this_cu->length = get_cu_length (&cu->header);
5520
5521 /* Establish the type offset that can be used to lookup the type. */
5522 sig_type->type_offset_in_section.sect_off =
5523 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5524 }
5525 else
5526 {
5527 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5528 abbrev_section,
5529 info_ptr, 0);
5530
5531 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5532 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5533 }
5534 }
5535
5536 /* Skip dummy compilation units. */
5537 if (info_ptr >= begin_info_ptr + this_cu->length
5538 || peek_abbrev_code (abfd, info_ptr) == 0)
5539 {
5540 do_cleanups (cleanups);
5541 return;
5542 }
5543
5544 /* If we don't have them yet, read the abbrevs for this compilation unit.
5545 And if we need to read them now, make sure they're freed when we're
5546 done. Note that it's important that if the CU had an abbrev table
5547 on entry we don't free it when we're done: Somewhere up the call stack
5548 it may be in use. */
5549 if (abbrev_table != NULL)
5550 {
5551 gdb_assert (cu->abbrev_table == NULL);
5552 gdb_assert (cu->header.abbrev_offset.sect_off
5553 == abbrev_table->offset.sect_off);
5554 cu->abbrev_table = abbrev_table;
5555 }
5556 else if (cu->abbrev_table == NULL)
5557 {
5558 dwarf2_read_abbrevs (cu, abbrev_section);
5559 make_cleanup (dwarf2_free_abbrev_table, cu);
5560 }
5561 else if (rereading_dwo_cu)
5562 {
5563 dwarf2_free_abbrev_table (cu);
5564 dwarf2_read_abbrevs (cu, abbrev_section);
5565 }
5566
5567 /* Read the top level CU/TU die. */
5568 init_cu_die_reader (&reader, cu, section, NULL);
5569 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5570
5571 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5572 from the DWO file.
5573 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5574 DWO CU, that this test will fail (the attribute will not be present). */
5575 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5576 if (attr)
5577 {
5578 struct dwo_unit *dwo_unit;
5579 struct die_info *dwo_comp_unit_die;
5580
5581 if (has_children)
5582 {
5583 complaint (&symfile_complaints,
5584 _("compilation unit with DW_AT_GNU_dwo_name"
5585 " has children (offset 0x%x) [in module %s]"),
5586 this_cu->offset.sect_off, bfd_get_filename (abfd));
5587 }
5588 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5589 if (dwo_unit != NULL)
5590 {
5591 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5592 abbrev_table != NULL,
5593 comp_unit_die, NULL,
5594 &reader, &info_ptr,
5595 &dwo_comp_unit_die, &has_children) == 0)
5596 {
5597 /* Dummy die. */
5598 do_cleanups (cleanups);
5599 return;
5600 }
5601 comp_unit_die = dwo_comp_unit_die;
5602 }
5603 else
5604 {
5605 /* Yikes, we couldn't find the rest of the DIE, we only have
5606 the stub. A complaint has already been logged. There's
5607 not much more we can do except pass on the stub DIE to
5608 die_reader_func. We don't want to throw an error on bad
5609 debug info. */
5610 }
5611 }
5612
5613 /* All of the above is setup for this call. Yikes. */
5614 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5615
5616 /* Done, clean up. */
5617 if (free_cu_cleanup != NULL)
5618 {
5619 if (keep)
5620 {
5621 /* We've successfully allocated this compilation unit. Let our
5622 caller clean it up when finished with it. */
5623 discard_cleanups (free_cu_cleanup);
5624
5625 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5626 So we have to manually free the abbrev table. */
5627 dwarf2_free_abbrev_table (cu);
5628
5629 /* Link this CU into read_in_chain. */
5630 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5631 dwarf2_per_objfile->read_in_chain = this_cu;
5632 }
5633 else
5634 do_cleanups (free_cu_cleanup);
5635 }
5636
5637 do_cleanups (cleanups);
5638 }
5639
5640 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5641 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5642 to have already done the lookup to find the DWO file).
5643
5644 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5645 THIS_CU->is_debug_types, but nothing else.
5646
5647 We fill in THIS_CU->length.
5648
5649 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5650 linker) then DIE_READER_FUNC will not get called.
5651
5652 THIS_CU->cu is always freed when done.
5653 This is done in order to not leave THIS_CU->cu in a state where we have
5654 to care whether it refers to the "main" CU or the DWO CU. */
5655
5656 static void
5657 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5658 struct dwo_file *dwo_file,
5659 die_reader_func_ftype *die_reader_func,
5660 void *data)
5661 {
5662 struct objfile *objfile = dwarf2_per_objfile->objfile;
5663 struct dwarf2_section_info *section = this_cu->section;
5664 bfd *abfd = get_section_bfd_owner (section);
5665 struct dwarf2_section_info *abbrev_section;
5666 struct dwarf2_cu cu;
5667 const gdb_byte *begin_info_ptr, *info_ptr;
5668 struct die_reader_specs reader;
5669 struct cleanup *cleanups;
5670 struct die_info *comp_unit_die;
5671 int has_children;
5672
5673 if (dwarf_die_debug)
5674 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5675 this_cu->is_debug_types ? "type" : "comp",
5676 this_cu->offset.sect_off);
5677
5678 gdb_assert (this_cu->cu == NULL);
5679
5680 abbrev_section = (dwo_file != NULL
5681 ? &dwo_file->sections.abbrev
5682 : get_abbrev_section_for_cu (this_cu));
5683
5684 /* This is cheap if the section is already read in. */
5685 dwarf2_read_section (objfile, section);
5686
5687 init_one_comp_unit (&cu, this_cu);
5688
5689 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5690
5691 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5692 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5693 abbrev_section, info_ptr,
5694 this_cu->is_debug_types);
5695
5696 this_cu->length = get_cu_length (&cu.header);
5697
5698 /* Skip dummy compilation units. */
5699 if (info_ptr >= begin_info_ptr + this_cu->length
5700 || peek_abbrev_code (abfd, info_ptr) == 0)
5701 {
5702 do_cleanups (cleanups);
5703 return;
5704 }
5705
5706 dwarf2_read_abbrevs (&cu, abbrev_section);
5707 make_cleanup (dwarf2_free_abbrev_table, &cu);
5708
5709 init_cu_die_reader (&reader, &cu, section, dwo_file);
5710 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5711
5712 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5713
5714 do_cleanups (cleanups);
5715 }
5716
5717 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5718 does not lookup the specified DWO file.
5719 This cannot be used to read DWO files.
5720
5721 THIS_CU->cu is always freed when done.
5722 This is done in order to not leave THIS_CU->cu in a state where we have
5723 to care whether it refers to the "main" CU or the DWO CU.
5724 We can revisit this if the data shows there's a performance issue. */
5725
5726 static void
5727 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5728 die_reader_func_ftype *die_reader_func,
5729 void *data)
5730 {
5731 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5732 }
5733 \f
5734 /* Type Unit Groups.
5735
5736 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5737 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5738 so that all types coming from the same compilation (.o file) are grouped
5739 together. A future step could be to put the types in the same symtab as
5740 the CU the types ultimately came from. */
5741
5742 static hashval_t
5743 hash_type_unit_group (const void *item)
5744 {
5745 const struct type_unit_group *tu_group = item;
5746
5747 return hash_stmt_list_entry (&tu_group->hash);
5748 }
5749
5750 static int
5751 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5752 {
5753 const struct type_unit_group *lhs = item_lhs;
5754 const struct type_unit_group *rhs = item_rhs;
5755
5756 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5757 }
5758
5759 /* Allocate a hash table for type unit groups. */
5760
5761 static htab_t
5762 allocate_type_unit_groups_table (void)
5763 {
5764 return htab_create_alloc_ex (3,
5765 hash_type_unit_group,
5766 eq_type_unit_group,
5767 NULL,
5768 &dwarf2_per_objfile->objfile->objfile_obstack,
5769 hashtab_obstack_allocate,
5770 dummy_obstack_deallocate);
5771 }
5772
5773 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5774 partial symtabs. We combine several TUs per psymtab to not let the size
5775 of any one psymtab grow too big. */
5776 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5777 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5778
5779 /* Helper routine for get_type_unit_group.
5780 Create the type_unit_group object used to hold one or more TUs. */
5781
5782 static struct type_unit_group *
5783 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5784 {
5785 struct objfile *objfile = dwarf2_per_objfile->objfile;
5786 struct dwarf2_per_cu_data *per_cu;
5787 struct type_unit_group *tu_group;
5788
5789 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5790 struct type_unit_group);
5791 per_cu = &tu_group->per_cu;
5792 per_cu->objfile = objfile;
5793
5794 if (dwarf2_per_objfile->using_index)
5795 {
5796 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5797 struct dwarf2_per_cu_quick_data);
5798 }
5799 else
5800 {
5801 unsigned int line_offset = line_offset_struct.sect_off;
5802 struct partial_symtab *pst;
5803 char *name;
5804
5805 /* Give the symtab a useful name for debug purposes. */
5806 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5807 name = xstrprintf ("<type_units_%d>",
5808 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5809 else
5810 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5811
5812 pst = create_partial_symtab (per_cu, name);
5813 pst->anonymous = 1;
5814
5815 xfree (name);
5816 }
5817
5818 tu_group->hash.dwo_unit = cu->dwo_unit;
5819 tu_group->hash.line_offset = line_offset_struct;
5820
5821 return tu_group;
5822 }
5823
5824 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5825 STMT_LIST is a DW_AT_stmt_list attribute. */
5826
5827 static struct type_unit_group *
5828 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5829 {
5830 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5831 struct type_unit_group *tu_group;
5832 void **slot;
5833 unsigned int line_offset;
5834 struct type_unit_group type_unit_group_for_lookup;
5835
5836 if (dwarf2_per_objfile->type_unit_groups == NULL)
5837 {
5838 dwarf2_per_objfile->type_unit_groups =
5839 allocate_type_unit_groups_table ();
5840 }
5841
5842 /* Do we need to create a new group, or can we use an existing one? */
5843
5844 if (stmt_list)
5845 {
5846 line_offset = DW_UNSND (stmt_list);
5847 ++tu_stats->nr_symtab_sharers;
5848 }
5849 else
5850 {
5851 /* Ugh, no stmt_list. Rare, but we have to handle it.
5852 We can do various things here like create one group per TU or
5853 spread them over multiple groups to split up the expansion work.
5854 To avoid worst case scenarios (too many groups or too large groups)
5855 we, umm, group them in bunches. */
5856 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5857 | (tu_stats->nr_stmt_less_type_units
5858 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5859 ++tu_stats->nr_stmt_less_type_units;
5860 }
5861
5862 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5863 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5864 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5865 &type_unit_group_for_lookup, INSERT);
5866 if (*slot != NULL)
5867 {
5868 tu_group = *slot;
5869 gdb_assert (tu_group != NULL);
5870 }
5871 else
5872 {
5873 sect_offset line_offset_struct;
5874
5875 line_offset_struct.sect_off = line_offset;
5876 tu_group = create_type_unit_group (cu, line_offset_struct);
5877 *slot = tu_group;
5878 ++tu_stats->nr_symtabs;
5879 }
5880
5881 return tu_group;
5882 }
5883 \f
5884 /* Partial symbol tables. */
5885
5886 /* Create a psymtab named NAME and assign it to PER_CU.
5887
5888 The caller must fill in the following details:
5889 dirname, textlow, texthigh. */
5890
5891 static struct partial_symtab *
5892 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5893 {
5894 struct objfile *objfile = per_cu->objfile;
5895 struct partial_symtab *pst;
5896
5897 pst = start_psymtab_common (objfile, name, 0,
5898 objfile->global_psymbols.next,
5899 objfile->static_psymbols.next);
5900
5901 pst->psymtabs_addrmap_supported = 1;
5902
5903 /* This is the glue that links PST into GDB's symbol API. */
5904 pst->read_symtab_private = per_cu;
5905 pst->read_symtab = dwarf2_read_symtab;
5906 per_cu->v.psymtab = pst;
5907
5908 return pst;
5909 }
5910
5911 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5912 type. */
5913
5914 struct process_psymtab_comp_unit_data
5915 {
5916 /* True if we are reading a DW_TAG_partial_unit. */
5917
5918 int want_partial_unit;
5919
5920 /* The "pretend" language that is used if the CU doesn't declare a
5921 language. */
5922
5923 enum language pretend_language;
5924 };
5925
5926 /* die_reader_func for process_psymtab_comp_unit. */
5927
5928 static void
5929 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5930 const gdb_byte *info_ptr,
5931 struct die_info *comp_unit_die,
5932 int has_children,
5933 void *data)
5934 {
5935 struct dwarf2_cu *cu = reader->cu;
5936 struct objfile *objfile = cu->objfile;
5937 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5938 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5939 CORE_ADDR baseaddr;
5940 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5941 struct partial_symtab *pst;
5942 int has_pc_info;
5943 const char *filename;
5944 struct process_psymtab_comp_unit_data *info = data;
5945
5946 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5947 return;
5948
5949 gdb_assert (! per_cu->is_debug_types);
5950
5951 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5952
5953 cu->list_in_scope = &file_symbols;
5954
5955 /* Allocate a new partial symbol table structure. */
5956 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5957 if (filename == NULL)
5958 filename = "";
5959
5960 pst = create_partial_symtab (per_cu, filename);
5961
5962 /* This must be done before calling dwarf2_build_include_psymtabs. */
5963 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5964
5965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5966
5967 dwarf2_find_base_address (comp_unit_die, cu);
5968
5969 /* Possibly set the default values of LOWPC and HIGHPC from
5970 `DW_AT_ranges'. */
5971 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5972 &best_highpc, cu, pst);
5973 if (has_pc_info == 1 && best_lowpc < best_highpc)
5974 /* Store the contiguous range if it is not empty; it can be empty for
5975 CUs with no code. */
5976 addrmap_set_empty (objfile->psymtabs_addrmap,
5977 gdbarch_adjust_dwarf2_addr (gdbarch,
5978 best_lowpc + baseaddr),
5979 gdbarch_adjust_dwarf2_addr (gdbarch,
5980 best_highpc + baseaddr) - 1,
5981 pst);
5982
5983 /* Check if comp unit has_children.
5984 If so, read the rest of the partial symbols from this comp unit.
5985 If not, there's no more debug_info for this comp unit. */
5986 if (has_children)
5987 {
5988 struct partial_die_info *first_die;
5989 CORE_ADDR lowpc, highpc;
5990
5991 lowpc = ((CORE_ADDR) -1);
5992 highpc = ((CORE_ADDR) 0);
5993
5994 first_die = load_partial_dies (reader, info_ptr, 1);
5995
5996 scan_partial_symbols (first_die, &lowpc, &highpc,
5997 ! has_pc_info, cu);
5998
5999 /* If we didn't find a lowpc, set it to highpc to avoid
6000 complaints from `maint check'. */
6001 if (lowpc == ((CORE_ADDR) -1))
6002 lowpc = highpc;
6003
6004 /* If the compilation unit didn't have an explicit address range,
6005 then use the information extracted from its child dies. */
6006 if (! has_pc_info)
6007 {
6008 best_lowpc = lowpc;
6009 best_highpc = highpc;
6010 }
6011 }
6012 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6013 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6014
6015 end_psymtab_common (objfile, pst);
6016
6017 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6018 {
6019 int i;
6020 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6021 struct dwarf2_per_cu_data *iter;
6022
6023 /* Fill in 'dependencies' here; we fill in 'users' in a
6024 post-pass. */
6025 pst->number_of_dependencies = len;
6026 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6027 len * sizeof (struct symtab *));
6028 for (i = 0;
6029 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6030 i, iter);
6031 ++i)
6032 pst->dependencies[i] = iter->v.psymtab;
6033
6034 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6035 }
6036
6037 /* Get the list of files included in the current compilation unit,
6038 and build a psymtab for each of them. */
6039 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6040
6041 if (dwarf_read_debug)
6042 {
6043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6044
6045 fprintf_unfiltered (gdb_stdlog,
6046 "Psymtab for %s unit @0x%x: %s - %s"
6047 ", %d global, %d static syms\n",
6048 per_cu->is_debug_types ? "type" : "comp",
6049 per_cu->offset.sect_off,
6050 paddress (gdbarch, pst->textlow),
6051 paddress (gdbarch, pst->texthigh),
6052 pst->n_global_syms, pst->n_static_syms);
6053 }
6054 }
6055
6056 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6057 Process compilation unit THIS_CU for a psymtab. */
6058
6059 static void
6060 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6061 int want_partial_unit,
6062 enum language pretend_language)
6063 {
6064 struct process_psymtab_comp_unit_data info;
6065
6066 /* If this compilation unit was already read in, free the
6067 cached copy in order to read it in again. This is
6068 necessary because we skipped some symbols when we first
6069 read in the compilation unit (see load_partial_dies).
6070 This problem could be avoided, but the benefit is unclear. */
6071 if (this_cu->cu != NULL)
6072 free_one_cached_comp_unit (this_cu);
6073
6074 gdb_assert (! this_cu->is_debug_types);
6075 info.want_partial_unit = want_partial_unit;
6076 info.pretend_language = pretend_language;
6077 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6078 process_psymtab_comp_unit_reader,
6079 &info);
6080
6081 /* Age out any secondary CUs. */
6082 age_cached_comp_units ();
6083 }
6084
6085 /* Reader function for build_type_psymtabs. */
6086
6087 static void
6088 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6089 const gdb_byte *info_ptr,
6090 struct die_info *type_unit_die,
6091 int has_children,
6092 void *data)
6093 {
6094 struct objfile *objfile = dwarf2_per_objfile->objfile;
6095 struct dwarf2_cu *cu = reader->cu;
6096 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6097 struct signatured_type *sig_type;
6098 struct type_unit_group *tu_group;
6099 struct attribute *attr;
6100 struct partial_die_info *first_die;
6101 CORE_ADDR lowpc, highpc;
6102 struct partial_symtab *pst;
6103
6104 gdb_assert (data == NULL);
6105 gdb_assert (per_cu->is_debug_types);
6106 sig_type = (struct signatured_type *) per_cu;
6107
6108 if (! has_children)
6109 return;
6110
6111 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6112 tu_group = get_type_unit_group (cu, attr);
6113
6114 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6115
6116 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6117 cu->list_in_scope = &file_symbols;
6118 pst = create_partial_symtab (per_cu, "");
6119 pst->anonymous = 1;
6120
6121 first_die = load_partial_dies (reader, info_ptr, 1);
6122
6123 lowpc = (CORE_ADDR) -1;
6124 highpc = (CORE_ADDR) 0;
6125 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6126
6127 end_psymtab_common (objfile, pst);
6128 }
6129
6130 /* Struct used to sort TUs by their abbreviation table offset. */
6131
6132 struct tu_abbrev_offset
6133 {
6134 struct signatured_type *sig_type;
6135 sect_offset abbrev_offset;
6136 };
6137
6138 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6139
6140 static int
6141 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6142 {
6143 const struct tu_abbrev_offset * const *a = ap;
6144 const struct tu_abbrev_offset * const *b = bp;
6145 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6146 unsigned int boff = (*b)->abbrev_offset.sect_off;
6147
6148 return (aoff > boff) - (aoff < boff);
6149 }
6150
6151 /* Efficiently read all the type units.
6152 This does the bulk of the work for build_type_psymtabs.
6153
6154 The efficiency is because we sort TUs by the abbrev table they use and
6155 only read each abbrev table once. In one program there are 200K TUs
6156 sharing 8K abbrev tables.
6157
6158 The main purpose of this function is to support building the
6159 dwarf2_per_objfile->type_unit_groups table.
6160 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6161 can collapse the search space by grouping them by stmt_list.
6162 The savings can be significant, in the same program from above the 200K TUs
6163 share 8K stmt_list tables.
6164
6165 FUNC is expected to call get_type_unit_group, which will create the
6166 struct type_unit_group if necessary and add it to
6167 dwarf2_per_objfile->type_unit_groups. */
6168
6169 static void
6170 build_type_psymtabs_1 (void)
6171 {
6172 struct objfile *objfile = dwarf2_per_objfile->objfile;
6173 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6174 struct cleanup *cleanups;
6175 struct abbrev_table *abbrev_table;
6176 sect_offset abbrev_offset;
6177 struct tu_abbrev_offset *sorted_by_abbrev;
6178 struct type_unit_group **iter;
6179 int i;
6180
6181 /* It's up to the caller to not call us multiple times. */
6182 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6183
6184 if (dwarf2_per_objfile->n_type_units == 0)
6185 return;
6186
6187 /* TUs typically share abbrev tables, and there can be way more TUs than
6188 abbrev tables. Sort by abbrev table to reduce the number of times we
6189 read each abbrev table in.
6190 Alternatives are to punt or to maintain a cache of abbrev tables.
6191 This is simpler and efficient enough for now.
6192
6193 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6194 symtab to use). Typically TUs with the same abbrev offset have the same
6195 stmt_list value too so in practice this should work well.
6196
6197 The basic algorithm here is:
6198
6199 sort TUs by abbrev table
6200 for each TU with same abbrev table:
6201 read abbrev table if first user
6202 read TU top level DIE
6203 [IWBN if DWO skeletons had DW_AT_stmt_list]
6204 call FUNC */
6205
6206 if (dwarf_read_debug)
6207 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6208
6209 /* Sort in a separate table to maintain the order of all_type_units
6210 for .gdb_index: TU indices directly index all_type_units. */
6211 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6212 dwarf2_per_objfile->n_type_units);
6213 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6214 {
6215 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6216
6217 sorted_by_abbrev[i].sig_type = sig_type;
6218 sorted_by_abbrev[i].abbrev_offset =
6219 read_abbrev_offset (sig_type->per_cu.section,
6220 sig_type->per_cu.offset);
6221 }
6222 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6223 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6224 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6225
6226 abbrev_offset.sect_off = ~(unsigned) 0;
6227 abbrev_table = NULL;
6228 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6229
6230 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6231 {
6232 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6233
6234 /* Switch to the next abbrev table if necessary. */
6235 if (abbrev_table == NULL
6236 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6237 {
6238 if (abbrev_table != NULL)
6239 {
6240 abbrev_table_free (abbrev_table);
6241 /* Reset to NULL in case abbrev_table_read_table throws
6242 an error: abbrev_table_free_cleanup will get called. */
6243 abbrev_table = NULL;
6244 }
6245 abbrev_offset = tu->abbrev_offset;
6246 abbrev_table =
6247 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6248 abbrev_offset);
6249 ++tu_stats->nr_uniq_abbrev_tables;
6250 }
6251
6252 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6253 build_type_psymtabs_reader, NULL);
6254 }
6255
6256 do_cleanups (cleanups);
6257 }
6258
6259 /* Print collected type unit statistics. */
6260
6261 static void
6262 print_tu_stats (void)
6263 {
6264 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6265
6266 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6267 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6268 dwarf2_per_objfile->n_type_units);
6269 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6270 tu_stats->nr_uniq_abbrev_tables);
6271 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6272 tu_stats->nr_symtabs);
6273 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6274 tu_stats->nr_symtab_sharers);
6275 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6276 tu_stats->nr_stmt_less_type_units);
6277 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6278 tu_stats->nr_all_type_units_reallocs);
6279 }
6280
6281 /* Traversal function for build_type_psymtabs. */
6282
6283 static int
6284 build_type_psymtab_dependencies (void **slot, void *info)
6285 {
6286 struct objfile *objfile = dwarf2_per_objfile->objfile;
6287 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6288 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6289 struct partial_symtab *pst = per_cu->v.psymtab;
6290 int len = VEC_length (sig_type_ptr, tu_group->tus);
6291 struct signatured_type *iter;
6292 int i;
6293
6294 gdb_assert (len > 0);
6295 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6296
6297 pst->number_of_dependencies = len;
6298 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6299 len * sizeof (struct psymtab *));
6300 for (i = 0;
6301 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6302 ++i)
6303 {
6304 gdb_assert (iter->per_cu.is_debug_types);
6305 pst->dependencies[i] = iter->per_cu.v.psymtab;
6306 iter->type_unit_group = tu_group;
6307 }
6308
6309 VEC_free (sig_type_ptr, tu_group->tus);
6310
6311 return 1;
6312 }
6313
6314 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6315 Build partial symbol tables for the .debug_types comp-units. */
6316
6317 static void
6318 build_type_psymtabs (struct objfile *objfile)
6319 {
6320 if (! create_all_type_units (objfile))
6321 return;
6322
6323 build_type_psymtabs_1 ();
6324 }
6325
6326 /* Traversal function for process_skeletonless_type_unit.
6327 Read a TU in a DWO file and build partial symbols for it. */
6328
6329 static int
6330 process_skeletonless_type_unit (void **slot, void *info)
6331 {
6332 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6333 struct objfile *objfile = info;
6334 struct signatured_type find_entry, *entry;
6335
6336 /* If this TU doesn't exist in the global table, add it and read it in. */
6337
6338 if (dwarf2_per_objfile->signatured_types == NULL)
6339 {
6340 dwarf2_per_objfile->signatured_types
6341 = allocate_signatured_type_table (objfile);
6342 }
6343
6344 find_entry.signature = dwo_unit->signature;
6345 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6346 INSERT);
6347 /* If we've already seen this type there's nothing to do. What's happening
6348 is we're doing our own version of comdat-folding here. */
6349 if (*slot != NULL)
6350 return 1;
6351
6352 /* This does the job that create_all_type_units would have done for
6353 this TU. */
6354 entry = add_type_unit (dwo_unit->signature, slot);
6355 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6356 *slot = entry;
6357
6358 /* This does the job that build_type_psymtabs_1 would have done. */
6359 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6360 build_type_psymtabs_reader, NULL);
6361
6362 return 1;
6363 }
6364
6365 /* Traversal function for process_skeletonless_type_units. */
6366
6367 static int
6368 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6369 {
6370 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6371
6372 if (dwo_file->tus != NULL)
6373 {
6374 htab_traverse_noresize (dwo_file->tus,
6375 process_skeletonless_type_unit, info);
6376 }
6377
6378 return 1;
6379 }
6380
6381 /* Scan all TUs of DWO files, verifying we've processed them.
6382 This is needed in case a TU was emitted without its skeleton.
6383 Note: This can't be done until we know what all the DWO files are. */
6384
6385 static void
6386 process_skeletonless_type_units (struct objfile *objfile)
6387 {
6388 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6389 if (get_dwp_file () == NULL
6390 && dwarf2_per_objfile->dwo_files != NULL)
6391 {
6392 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6393 process_dwo_file_for_skeletonless_type_units,
6394 objfile);
6395 }
6396 }
6397
6398 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6399
6400 static void
6401 psymtabs_addrmap_cleanup (void *o)
6402 {
6403 struct objfile *objfile = o;
6404
6405 objfile->psymtabs_addrmap = NULL;
6406 }
6407
6408 /* Compute the 'user' field for each psymtab in OBJFILE. */
6409
6410 static void
6411 set_partial_user (struct objfile *objfile)
6412 {
6413 int i;
6414
6415 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6416 {
6417 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6418 struct partial_symtab *pst = per_cu->v.psymtab;
6419 int j;
6420
6421 if (pst == NULL)
6422 continue;
6423
6424 for (j = 0; j < pst->number_of_dependencies; ++j)
6425 {
6426 /* Set the 'user' field only if it is not already set. */
6427 if (pst->dependencies[j]->user == NULL)
6428 pst->dependencies[j]->user = pst;
6429 }
6430 }
6431 }
6432
6433 /* Build the partial symbol table by doing a quick pass through the
6434 .debug_info and .debug_abbrev sections. */
6435
6436 static void
6437 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6438 {
6439 struct cleanup *back_to, *addrmap_cleanup;
6440 struct obstack temp_obstack;
6441 int i;
6442
6443 if (dwarf_read_debug)
6444 {
6445 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6446 objfile_name (objfile));
6447 }
6448
6449 dwarf2_per_objfile->reading_partial_symbols = 1;
6450
6451 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6452
6453 /* Any cached compilation units will be linked by the per-objfile
6454 read_in_chain. Make sure to free them when we're done. */
6455 back_to = make_cleanup (free_cached_comp_units, NULL);
6456
6457 build_type_psymtabs (objfile);
6458
6459 create_all_comp_units (objfile);
6460
6461 /* Create a temporary address map on a temporary obstack. We later
6462 copy this to the final obstack. */
6463 obstack_init (&temp_obstack);
6464 make_cleanup_obstack_free (&temp_obstack);
6465 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6466 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6467
6468 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6469 {
6470 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6471
6472 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6473 }
6474
6475 /* This has to wait until we read the CUs, we need the list of DWOs. */
6476 process_skeletonless_type_units (objfile);
6477
6478 /* Now that all TUs have been processed we can fill in the dependencies. */
6479 if (dwarf2_per_objfile->type_unit_groups != NULL)
6480 {
6481 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6482 build_type_psymtab_dependencies, NULL);
6483 }
6484
6485 if (dwarf_read_debug)
6486 print_tu_stats ();
6487
6488 set_partial_user (objfile);
6489
6490 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6491 &objfile->objfile_obstack);
6492 discard_cleanups (addrmap_cleanup);
6493
6494 do_cleanups (back_to);
6495
6496 if (dwarf_read_debug)
6497 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6498 objfile_name (objfile));
6499 }
6500
6501 /* die_reader_func for load_partial_comp_unit. */
6502
6503 static void
6504 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6505 const gdb_byte *info_ptr,
6506 struct die_info *comp_unit_die,
6507 int has_children,
6508 void *data)
6509 {
6510 struct dwarf2_cu *cu = reader->cu;
6511
6512 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6513
6514 /* Check if comp unit has_children.
6515 If so, read the rest of the partial symbols from this comp unit.
6516 If not, there's no more debug_info for this comp unit. */
6517 if (has_children)
6518 load_partial_dies (reader, info_ptr, 0);
6519 }
6520
6521 /* Load the partial DIEs for a secondary CU into memory.
6522 This is also used when rereading a primary CU with load_all_dies. */
6523
6524 static void
6525 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6526 {
6527 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6528 load_partial_comp_unit_reader, NULL);
6529 }
6530
6531 static void
6532 read_comp_units_from_section (struct objfile *objfile,
6533 struct dwarf2_section_info *section,
6534 unsigned int is_dwz,
6535 int *n_allocated,
6536 int *n_comp_units,
6537 struct dwarf2_per_cu_data ***all_comp_units)
6538 {
6539 const gdb_byte *info_ptr;
6540 bfd *abfd = get_section_bfd_owner (section);
6541
6542 if (dwarf_read_debug)
6543 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6544 get_section_name (section),
6545 get_section_file_name (section));
6546
6547 dwarf2_read_section (objfile, section);
6548
6549 info_ptr = section->buffer;
6550
6551 while (info_ptr < section->buffer + section->size)
6552 {
6553 unsigned int length, initial_length_size;
6554 struct dwarf2_per_cu_data *this_cu;
6555 sect_offset offset;
6556
6557 offset.sect_off = info_ptr - section->buffer;
6558
6559 /* Read just enough information to find out where the next
6560 compilation unit is. */
6561 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6562
6563 /* Save the compilation unit for later lookup. */
6564 this_cu = obstack_alloc (&objfile->objfile_obstack,
6565 sizeof (struct dwarf2_per_cu_data));
6566 memset (this_cu, 0, sizeof (*this_cu));
6567 this_cu->offset = offset;
6568 this_cu->length = length + initial_length_size;
6569 this_cu->is_dwz = is_dwz;
6570 this_cu->objfile = objfile;
6571 this_cu->section = section;
6572
6573 if (*n_comp_units == *n_allocated)
6574 {
6575 *n_allocated *= 2;
6576 *all_comp_units = xrealloc (*all_comp_units,
6577 *n_allocated
6578 * sizeof (struct dwarf2_per_cu_data *));
6579 }
6580 (*all_comp_units)[*n_comp_units] = this_cu;
6581 ++*n_comp_units;
6582
6583 info_ptr = info_ptr + this_cu->length;
6584 }
6585 }
6586
6587 /* Create a list of all compilation units in OBJFILE.
6588 This is only done for -readnow and building partial symtabs. */
6589
6590 static void
6591 create_all_comp_units (struct objfile *objfile)
6592 {
6593 int n_allocated;
6594 int n_comp_units;
6595 struct dwarf2_per_cu_data **all_comp_units;
6596 struct dwz_file *dwz;
6597
6598 n_comp_units = 0;
6599 n_allocated = 10;
6600 all_comp_units = xmalloc (n_allocated
6601 * sizeof (struct dwarf2_per_cu_data *));
6602
6603 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6604 &n_allocated, &n_comp_units, &all_comp_units);
6605
6606 dwz = dwarf2_get_dwz_file ();
6607 if (dwz != NULL)
6608 read_comp_units_from_section (objfile, &dwz->info, 1,
6609 &n_allocated, &n_comp_units,
6610 &all_comp_units);
6611
6612 dwarf2_per_objfile->all_comp_units
6613 = obstack_alloc (&objfile->objfile_obstack,
6614 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6615 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6616 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6617 xfree (all_comp_units);
6618 dwarf2_per_objfile->n_comp_units = n_comp_units;
6619 }
6620
6621 /* Process all loaded DIEs for compilation unit CU, starting at
6622 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6623 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6624 DW_AT_ranges). See the comments of add_partial_subprogram on how
6625 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6626
6627 static void
6628 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6629 CORE_ADDR *highpc, int set_addrmap,
6630 struct dwarf2_cu *cu)
6631 {
6632 struct partial_die_info *pdi;
6633
6634 /* Now, march along the PDI's, descending into ones which have
6635 interesting children but skipping the children of the other ones,
6636 until we reach the end of the compilation unit. */
6637
6638 pdi = first_die;
6639
6640 while (pdi != NULL)
6641 {
6642 fixup_partial_die (pdi, cu);
6643
6644 /* Anonymous namespaces or modules have no name but have interesting
6645 children, so we need to look at them. Ditto for anonymous
6646 enums. */
6647
6648 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6649 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6650 || pdi->tag == DW_TAG_imported_unit)
6651 {
6652 switch (pdi->tag)
6653 {
6654 case DW_TAG_subprogram:
6655 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6656 break;
6657 case DW_TAG_constant:
6658 case DW_TAG_variable:
6659 case DW_TAG_typedef:
6660 case DW_TAG_union_type:
6661 if (!pdi->is_declaration)
6662 {
6663 add_partial_symbol (pdi, cu);
6664 }
6665 break;
6666 case DW_TAG_class_type:
6667 case DW_TAG_interface_type:
6668 case DW_TAG_structure_type:
6669 if (!pdi->is_declaration)
6670 {
6671 add_partial_symbol (pdi, cu);
6672 }
6673 break;
6674 case DW_TAG_enumeration_type:
6675 if (!pdi->is_declaration)
6676 add_partial_enumeration (pdi, cu);
6677 break;
6678 case DW_TAG_base_type:
6679 case DW_TAG_subrange_type:
6680 /* File scope base type definitions are added to the partial
6681 symbol table. */
6682 add_partial_symbol (pdi, cu);
6683 break;
6684 case DW_TAG_namespace:
6685 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6686 break;
6687 case DW_TAG_module:
6688 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6689 break;
6690 case DW_TAG_imported_unit:
6691 {
6692 struct dwarf2_per_cu_data *per_cu;
6693
6694 /* For now we don't handle imported units in type units. */
6695 if (cu->per_cu->is_debug_types)
6696 {
6697 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6698 " supported in type units [in module %s]"),
6699 objfile_name (cu->objfile));
6700 }
6701
6702 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6703 pdi->is_dwz,
6704 cu->objfile);
6705
6706 /* Go read the partial unit, if needed. */
6707 if (per_cu->v.psymtab == NULL)
6708 process_psymtab_comp_unit (per_cu, 1, cu->language);
6709
6710 VEC_safe_push (dwarf2_per_cu_ptr,
6711 cu->per_cu->imported_symtabs, per_cu);
6712 }
6713 break;
6714 case DW_TAG_imported_declaration:
6715 add_partial_symbol (pdi, cu);
6716 break;
6717 default:
6718 break;
6719 }
6720 }
6721
6722 /* If the die has a sibling, skip to the sibling. */
6723
6724 pdi = pdi->die_sibling;
6725 }
6726 }
6727
6728 /* Functions used to compute the fully scoped name of a partial DIE.
6729
6730 Normally, this is simple. For C++, the parent DIE's fully scoped
6731 name is concatenated with "::" and the partial DIE's name. For
6732 Java, the same thing occurs except that "." is used instead of "::".
6733 Enumerators are an exception; they use the scope of their parent
6734 enumeration type, i.e. the name of the enumeration type is not
6735 prepended to the enumerator.
6736
6737 There are two complexities. One is DW_AT_specification; in this
6738 case "parent" means the parent of the target of the specification,
6739 instead of the direct parent of the DIE. The other is compilers
6740 which do not emit DW_TAG_namespace; in this case we try to guess
6741 the fully qualified name of structure types from their members'
6742 linkage names. This must be done using the DIE's children rather
6743 than the children of any DW_AT_specification target. We only need
6744 to do this for structures at the top level, i.e. if the target of
6745 any DW_AT_specification (if any; otherwise the DIE itself) does not
6746 have a parent. */
6747
6748 /* Compute the scope prefix associated with PDI's parent, in
6749 compilation unit CU. The result will be allocated on CU's
6750 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6751 field. NULL is returned if no prefix is necessary. */
6752 static const char *
6753 partial_die_parent_scope (struct partial_die_info *pdi,
6754 struct dwarf2_cu *cu)
6755 {
6756 const char *grandparent_scope;
6757 struct partial_die_info *parent, *real_pdi;
6758
6759 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6760 then this means the parent of the specification DIE. */
6761
6762 real_pdi = pdi;
6763 while (real_pdi->has_specification)
6764 real_pdi = find_partial_die (real_pdi->spec_offset,
6765 real_pdi->spec_is_dwz, cu);
6766
6767 parent = real_pdi->die_parent;
6768 if (parent == NULL)
6769 return NULL;
6770
6771 if (parent->scope_set)
6772 return parent->scope;
6773
6774 fixup_partial_die (parent, cu);
6775
6776 grandparent_scope = partial_die_parent_scope (parent, cu);
6777
6778 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6779 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6780 Work around this problem here. */
6781 if (cu->language == language_cplus
6782 && parent->tag == DW_TAG_namespace
6783 && strcmp (parent->name, "::") == 0
6784 && grandparent_scope == NULL)
6785 {
6786 parent->scope = NULL;
6787 parent->scope_set = 1;
6788 return NULL;
6789 }
6790
6791 if (pdi->tag == DW_TAG_enumerator)
6792 /* Enumerators should not get the name of the enumeration as a prefix. */
6793 parent->scope = grandparent_scope;
6794 else if (parent->tag == DW_TAG_namespace
6795 || parent->tag == DW_TAG_module
6796 || parent->tag == DW_TAG_structure_type
6797 || parent->tag == DW_TAG_class_type
6798 || parent->tag == DW_TAG_interface_type
6799 || parent->tag == DW_TAG_union_type
6800 || parent->tag == DW_TAG_enumeration_type)
6801 {
6802 if (grandparent_scope == NULL)
6803 parent->scope = parent->name;
6804 else
6805 parent->scope = typename_concat (&cu->comp_unit_obstack,
6806 grandparent_scope,
6807 parent->name, 0, cu);
6808 }
6809 else
6810 {
6811 /* FIXME drow/2004-04-01: What should we be doing with
6812 function-local names? For partial symbols, we should probably be
6813 ignoring them. */
6814 complaint (&symfile_complaints,
6815 _("unhandled containing DIE tag %d for DIE at %d"),
6816 parent->tag, pdi->offset.sect_off);
6817 parent->scope = grandparent_scope;
6818 }
6819
6820 parent->scope_set = 1;
6821 return parent->scope;
6822 }
6823
6824 /* Return the fully scoped name associated with PDI, from compilation unit
6825 CU. The result will be allocated with malloc. */
6826
6827 static char *
6828 partial_die_full_name (struct partial_die_info *pdi,
6829 struct dwarf2_cu *cu)
6830 {
6831 const char *parent_scope;
6832
6833 /* If this is a template instantiation, we can not work out the
6834 template arguments from partial DIEs. So, unfortunately, we have
6835 to go through the full DIEs. At least any work we do building
6836 types here will be reused if full symbols are loaded later. */
6837 if (pdi->has_template_arguments)
6838 {
6839 fixup_partial_die (pdi, cu);
6840
6841 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6842 {
6843 struct die_info *die;
6844 struct attribute attr;
6845 struct dwarf2_cu *ref_cu = cu;
6846
6847 /* DW_FORM_ref_addr is using section offset. */
6848 attr.name = 0;
6849 attr.form = DW_FORM_ref_addr;
6850 attr.u.unsnd = pdi->offset.sect_off;
6851 die = follow_die_ref (NULL, &attr, &ref_cu);
6852
6853 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6854 }
6855 }
6856
6857 parent_scope = partial_die_parent_scope (pdi, cu);
6858 if (parent_scope == NULL)
6859 return NULL;
6860 else
6861 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6862 }
6863
6864 static void
6865 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6866 {
6867 struct objfile *objfile = cu->objfile;
6868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6869 CORE_ADDR addr = 0;
6870 const char *actual_name = NULL;
6871 CORE_ADDR baseaddr;
6872 char *built_actual_name;
6873
6874 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6875
6876 built_actual_name = partial_die_full_name (pdi, cu);
6877 if (built_actual_name != NULL)
6878 actual_name = built_actual_name;
6879
6880 if (actual_name == NULL)
6881 actual_name = pdi->name;
6882
6883 switch (pdi->tag)
6884 {
6885 case DW_TAG_subprogram:
6886 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6887 if (pdi->is_external || cu->language == language_ada)
6888 {
6889 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6890 of the global scope. But in Ada, we want to be able to access
6891 nested procedures globally. So all Ada subprograms are stored
6892 in the global scope. */
6893 /* prim_record_minimal_symbol (actual_name, addr, mst_text,
6894 objfile); */
6895 add_psymbol_to_list (actual_name, strlen (actual_name),
6896 built_actual_name != NULL,
6897 VAR_DOMAIN, LOC_BLOCK,
6898 &objfile->global_psymbols,
6899 addr, cu->language, objfile);
6900 }
6901 else
6902 {
6903 /* prim_record_minimal_symbol (actual_name, addr, mst_file_text,
6904 objfile); */
6905 add_psymbol_to_list (actual_name, strlen (actual_name),
6906 built_actual_name != NULL,
6907 VAR_DOMAIN, LOC_BLOCK,
6908 &objfile->static_psymbols,
6909 addr, cu->language, objfile);
6910 }
6911 break;
6912 case DW_TAG_constant:
6913 {
6914 struct psymbol_allocation_list *list;
6915
6916 if (pdi->is_external)
6917 list = &objfile->global_psymbols;
6918 else
6919 list = &objfile->static_psymbols;
6920 add_psymbol_to_list (actual_name, strlen (actual_name),
6921 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6922 list, 0, cu->language, objfile);
6923 }
6924 break;
6925 case DW_TAG_variable:
6926 if (pdi->d.locdesc)
6927 addr = decode_locdesc (pdi->d.locdesc, cu);
6928
6929 if (pdi->d.locdesc
6930 && addr == 0
6931 && !dwarf2_per_objfile->has_section_at_zero)
6932 {
6933 /* A global or static variable may also have been stripped
6934 out by the linker if unused, in which case its address
6935 will be nullified; do not add such variables into partial
6936 symbol table then. */
6937 }
6938 else if (pdi->is_external)
6939 {
6940 /* Global Variable.
6941 Don't enter into the minimal symbol tables as there is
6942 a minimal symbol table entry from the ELF symbols already.
6943 Enter into partial symbol table if it has a location
6944 descriptor or a type.
6945 If the location descriptor is missing, new_symbol will create
6946 a LOC_UNRESOLVED symbol, the address of the variable will then
6947 be determined from the minimal symbol table whenever the variable
6948 is referenced.
6949 The address for the partial symbol table entry is not
6950 used by GDB, but it comes in handy for debugging partial symbol
6951 table building. */
6952
6953 if (pdi->d.locdesc || pdi->has_type)
6954 add_psymbol_to_list (actual_name, strlen (actual_name),
6955 built_actual_name != NULL,
6956 VAR_DOMAIN, LOC_STATIC,
6957 &objfile->global_psymbols,
6958 addr + baseaddr,
6959 cu->language, objfile);
6960 }
6961 else
6962 {
6963 int has_loc = pdi->d.locdesc != NULL;
6964
6965 /* Static Variable. Skip symbols whose value we cannot know (those
6966 without location descriptors or constant values). */
6967 if (!has_loc && !pdi->has_const_value)
6968 {
6969 xfree (built_actual_name);
6970 return;
6971 }
6972
6973 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6974 mst_file_data, objfile); */
6975 add_psymbol_to_list (actual_name, strlen (actual_name),
6976 built_actual_name != NULL,
6977 VAR_DOMAIN, LOC_STATIC,
6978 &objfile->static_psymbols,
6979 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
6980 cu->language, objfile);
6981 }
6982 break;
6983 case DW_TAG_typedef:
6984 case DW_TAG_base_type:
6985 case DW_TAG_subrange_type:
6986 add_psymbol_to_list (actual_name, strlen (actual_name),
6987 built_actual_name != NULL,
6988 VAR_DOMAIN, LOC_TYPEDEF,
6989 &objfile->static_psymbols,
6990 0, cu->language, objfile);
6991 break;
6992 case DW_TAG_imported_declaration:
6993 case DW_TAG_namespace:
6994 add_psymbol_to_list (actual_name, strlen (actual_name),
6995 built_actual_name != NULL,
6996 VAR_DOMAIN, LOC_TYPEDEF,
6997 &objfile->global_psymbols,
6998 0, cu->language, objfile);
6999 break;
7000 case DW_TAG_module:
7001 add_psymbol_to_list (actual_name, strlen (actual_name),
7002 built_actual_name != NULL,
7003 MODULE_DOMAIN, LOC_TYPEDEF,
7004 &objfile->global_psymbols,
7005 0, cu->language, objfile);
7006 break;
7007 case DW_TAG_class_type:
7008 case DW_TAG_interface_type:
7009 case DW_TAG_structure_type:
7010 case DW_TAG_union_type:
7011 case DW_TAG_enumeration_type:
7012 /* Skip external references. The DWARF standard says in the section
7013 about "Structure, Union, and Class Type Entries": "An incomplete
7014 structure, union or class type is represented by a structure,
7015 union or class entry that does not have a byte size attribute
7016 and that has a DW_AT_declaration attribute." */
7017 if (!pdi->has_byte_size && pdi->is_declaration)
7018 {
7019 xfree (built_actual_name);
7020 return;
7021 }
7022
7023 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7024 static vs. global. */
7025 add_psymbol_to_list (actual_name, strlen (actual_name),
7026 built_actual_name != NULL,
7027 STRUCT_DOMAIN, LOC_TYPEDEF,
7028 (cu->language == language_cplus
7029 || cu->language == language_java)
7030 ? &objfile->global_psymbols
7031 : &objfile->static_psymbols,
7032 0, cu->language, objfile);
7033
7034 break;
7035 case DW_TAG_enumerator:
7036 add_psymbol_to_list (actual_name, strlen (actual_name),
7037 built_actual_name != NULL,
7038 VAR_DOMAIN, LOC_CONST,
7039 (cu->language == language_cplus
7040 || cu->language == language_java)
7041 ? &objfile->global_psymbols
7042 : &objfile->static_psymbols,
7043 0, cu->language, objfile);
7044 break;
7045 default:
7046 break;
7047 }
7048
7049 xfree (built_actual_name);
7050 }
7051
7052 /* Read a partial die corresponding to a namespace; also, add a symbol
7053 corresponding to that namespace to the symbol table. NAMESPACE is
7054 the name of the enclosing namespace. */
7055
7056 static void
7057 add_partial_namespace (struct partial_die_info *pdi,
7058 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7059 int set_addrmap, struct dwarf2_cu *cu)
7060 {
7061 /* Add a symbol for the namespace. */
7062
7063 add_partial_symbol (pdi, cu);
7064
7065 /* Now scan partial symbols in that namespace. */
7066
7067 if (pdi->has_children)
7068 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7069 }
7070
7071 /* Read a partial die corresponding to a Fortran module. */
7072
7073 static void
7074 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7075 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7076 {
7077 /* Add a symbol for the namespace. */
7078
7079 add_partial_symbol (pdi, cu);
7080
7081 /* Now scan partial symbols in that module. */
7082
7083 if (pdi->has_children)
7084 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7085 }
7086
7087 /* Read a partial die corresponding to a subprogram and create a partial
7088 symbol for that subprogram. When the CU language allows it, this
7089 routine also defines a partial symbol for each nested subprogram
7090 that this subprogram contains. If SET_ADDRMAP is true, record the
7091 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7092 and highest PC values found in PDI.
7093
7094 PDI may also be a lexical block, in which case we simply search
7095 recursively for subprograms defined inside that lexical block.
7096 Again, this is only performed when the CU language allows this
7097 type of definitions. */
7098
7099 static void
7100 add_partial_subprogram (struct partial_die_info *pdi,
7101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7102 int set_addrmap, struct dwarf2_cu *cu)
7103 {
7104 if (pdi->tag == DW_TAG_subprogram)
7105 {
7106 if (pdi->has_pc_info)
7107 {
7108 if (pdi->lowpc < *lowpc)
7109 *lowpc = pdi->lowpc;
7110 if (pdi->highpc > *highpc)
7111 *highpc = pdi->highpc;
7112 if (set_addrmap)
7113 {
7114 struct objfile *objfile = cu->objfile;
7115 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7116 CORE_ADDR baseaddr;
7117 CORE_ADDR highpc;
7118 CORE_ADDR lowpc;
7119
7120 baseaddr = ANOFFSET (objfile->section_offsets,
7121 SECT_OFF_TEXT (objfile));
7122 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7123 pdi->lowpc + baseaddr);
7124 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7125 pdi->highpc + baseaddr);
7126 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7127 cu->per_cu->v.psymtab);
7128 }
7129 }
7130
7131 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7132 {
7133 if (!pdi->is_declaration)
7134 /* Ignore subprogram DIEs that do not have a name, they are
7135 illegal. Do not emit a complaint at this point, we will
7136 do so when we convert this psymtab into a symtab. */
7137 if (pdi->name)
7138 add_partial_symbol (pdi, cu);
7139 }
7140 }
7141
7142 if (! pdi->has_children)
7143 return;
7144
7145 if (cu->language == language_ada)
7146 {
7147 pdi = pdi->die_child;
7148 while (pdi != NULL)
7149 {
7150 fixup_partial_die (pdi, cu);
7151 if (pdi->tag == DW_TAG_subprogram
7152 || pdi->tag == DW_TAG_lexical_block)
7153 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7154 pdi = pdi->die_sibling;
7155 }
7156 }
7157 }
7158
7159 /* Read a partial die corresponding to an enumeration type. */
7160
7161 static void
7162 add_partial_enumeration (struct partial_die_info *enum_pdi,
7163 struct dwarf2_cu *cu)
7164 {
7165 struct partial_die_info *pdi;
7166
7167 if (enum_pdi->name != NULL)
7168 add_partial_symbol (enum_pdi, cu);
7169
7170 pdi = enum_pdi->die_child;
7171 while (pdi)
7172 {
7173 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7174 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7175 else
7176 add_partial_symbol (pdi, cu);
7177 pdi = pdi->die_sibling;
7178 }
7179 }
7180
7181 /* Return the initial uleb128 in the die at INFO_PTR. */
7182
7183 static unsigned int
7184 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7185 {
7186 unsigned int bytes_read;
7187
7188 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7189 }
7190
7191 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7192 Return the corresponding abbrev, or NULL if the number is zero (indicating
7193 an empty DIE). In either case *BYTES_READ will be set to the length of
7194 the initial number. */
7195
7196 static struct abbrev_info *
7197 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7198 struct dwarf2_cu *cu)
7199 {
7200 bfd *abfd = cu->objfile->obfd;
7201 unsigned int abbrev_number;
7202 struct abbrev_info *abbrev;
7203
7204 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7205
7206 if (abbrev_number == 0)
7207 return NULL;
7208
7209 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7210 if (!abbrev)
7211 {
7212 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7213 " at offset 0x%x [in module %s]"),
7214 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7215 cu->header.offset.sect_off, bfd_get_filename (abfd));
7216 }
7217
7218 return abbrev;
7219 }
7220
7221 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7222 Returns a pointer to the end of a series of DIEs, terminated by an empty
7223 DIE. Any children of the skipped DIEs will also be skipped. */
7224
7225 static const gdb_byte *
7226 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7227 {
7228 struct dwarf2_cu *cu = reader->cu;
7229 struct abbrev_info *abbrev;
7230 unsigned int bytes_read;
7231
7232 while (1)
7233 {
7234 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7235 if (abbrev == NULL)
7236 return info_ptr + bytes_read;
7237 else
7238 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7239 }
7240 }
7241
7242 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7243 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7244 abbrev corresponding to that skipped uleb128 should be passed in
7245 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7246 children. */
7247
7248 static const gdb_byte *
7249 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7250 struct abbrev_info *abbrev)
7251 {
7252 unsigned int bytes_read;
7253 struct attribute attr;
7254 bfd *abfd = reader->abfd;
7255 struct dwarf2_cu *cu = reader->cu;
7256 const gdb_byte *buffer = reader->buffer;
7257 const gdb_byte *buffer_end = reader->buffer_end;
7258 const gdb_byte *start_info_ptr = info_ptr;
7259 unsigned int form, i;
7260
7261 for (i = 0; i < abbrev->num_attrs; i++)
7262 {
7263 /* The only abbrev we care about is DW_AT_sibling. */
7264 if (abbrev->attrs[i].name == DW_AT_sibling)
7265 {
7266 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7267 if (attr.form == DW_FORM_ref_addr)
7268 complaint (&symfile_complaints,
7269 _("ignoring absolute DW_AT_sibling"));
7270 else
7271 {
7272 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7273 const gdb_byte *sibling_ptr = buffer + off;
7274
7275 if (sibling_ptr < info_ptr)
7276 complaint (&symfile_complaints,
7277 _("DW_AT_sibling points backwards"));
7278 else if (sibling_ptr > reader->buffer_end)
7279 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7280 else
7281 return sibling_ptr;
7282 }
7283 }
7284
7285 /* If it isn't DW_AT_sibling, skip this attribute. */
7286 form = abbrev->attrs[i].form;
7287 skip_attribute:
7288 switch (form)
7289 {
7290 case DW_FORM_ref_addr:
7291 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7292 and later it is offset sized. */
7293 if (cu->header.version == 2)
7294 info_ptr += cu->header.addr_size;
7295 else
7296 info_ptr += cu->header.offset_size;
7297 break;
7298 case DW_FORM_GNU_ref_alt:
7299 info_ptr += cu->header.offset_size;
7300 break;
7301 case DW_FORM_addr:
7302 info_ptr += cu->header.addr_size;
7303 break;
7304 case DW_FORM_data1:
7305 case DW_FORM_ref1:
7306 case DW_FORM_flag:
7307 info_ptr += 1;
7308 break;
7309 case DW_FORM_flag_present:
7310 break;
7311 case DW_FORM_data2:
7312 case DW_FORM_ref2:
7313 info_ptr += 2;
7314 break;
7315 case DW_FORM_data4:
7316 case DW_FORM_ref4:
7317 info_ptr += 4;
7318 break;
7319 case DW_FORM_data8:
7320 case DW_FORM_ref8:
7321 case DW_FORM_ref_sig8:
7322 info_ptr += 8;
7323 break;
7324 case DW_FORM_string:
7325 read_direct_string (abfd, info_ptr, &bytes_read);
7326 info_ptr += bytes_read;
7327 break;
7328 case DW_FORM_sec_offset:
7329 case DW_FORM_strp:
7330 case DW_FORM_GNU_strp_alt:
7331 info_ptr += cu->header.offset_size;
7332 break;
7333 case DW_FORM_exprloc:
7334 case DW_FORM_block:
7335 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7336 info_ptr += bytes_read;
7337 break;
7338 case DW_FORM_block1:
7339 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7340 break;
7341 case DW_FORM_block2:
7342 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7343 break;
7344 case DW_FORM_block4:
7345 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7346 break;
7347 case DW_FORM_sdata:
7348 case DW_FORM_udata:
7349 case DW_FORM_ref_udata:
7350 case DW_FORM_GNU_addr_index:
7351 case DW_FORM_GNU_str_index:
7352 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7353 break;
7354 case DW_FORM_indirect:
7355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7356 info_ptr += bytes_read;
7357 /* We need to continue parsing from here, so just go back to
7358 the top. */
7359 goto skip_attribute;
7360
7361 default:
7362 error (_("Dwarf Error: Cannot handle %s "
7363 "in DWARF reader [in module %s]"),
7364 dwarf_form_name (form),
7365 bfd_get_filename (abfd));
7366 }
7367 }
7368
7369 if (abbrev->has_children)
7370 return skip_children (reader, info_ptr);
7371 else
7372 return info_ptr;
7373 }
7374
7375 /* Locate ORIG_PDI's sibling.
7376 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7377
7378 static const gdb_byte *
7379 locate_pdi_sibling (const struct die_reader_specs *reader,
7380 struct partial_die_info *orig_pdi,
7381 const gdb_byte *info_ptr)
7382 {
7383 /* Do we know the sibling already? */
7384
7385 if (orig_pdi->sibling)
7386 return orig_pdi->sibling;
7387
7388 /* Are there any children to deal with? */
7389
7390 if (!orig_pdi->has_children)
7391 return info_ptr;
7392
7393 /* Skip the children the long way. */
7394
7395 return skip_children (reader, info_ptr);
7396 }
7397
7398 /* Expand this partial symbol table into a full symbol table. SELF is
7399 not NULL. */
7400
7401 static void
7402 dwarf2_read_symtab (struct partial_symtab *self,
7403 struct objfile *objfile)
7404 {
7405 if (self->readin)
7406 {
7407 warning (_("bug: psymtab for %s is already read in."),
7408 self->filename);
7409 }
7410 else
7411 {
7412 if (info_verbose)
7413 {
7414 printf_filtered (_("Reading in symbols for %s..."),
7415 self->filename);
7416 gdb_flush (gdb_stdout);
7417 }
7418
7419 /* Restore our global data. */
7420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
7421
7422 /* If this psymtab is constructed from a debug-only objfile, the
7423 has_section_at_zero flag will not necessarily be correct. We
7424 can get the correct value for this flag by looking at the data
7425 associated with the (presumably stripped) associated objfile. */
7426 if (objfile->separate_debug_objfile_backlink)
7427 {
7428 struct dwarf2_per_objfile *dpo_backlink
7429 = objfile_data (objfile->separate_debug_objfile_backlink,
7430 dwarf2_objfile_data_key);
7431
7432 dwarf2_per_objfile->has_section_at_zero
7433 = dpo_backlink->has_section_at_zero;
7434 }
7435
7436 dwarf2_per_objfile->reading_partial_symbols = 0;
7437
7438 psymtab_to_symtab_1 (self);
7439
7440 /* Finish up the debug error message. */
7441 if (info_verbose)
7442 printf_filtered (_("done.\n"));
7443 }
7444
7445 process_cu_includes ();
7446 }
7447 \f
7448 /* Reading in full CUs. */
7449
7450 /* Add PER_CU to the queue. */
7451
7452 static void
7453 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7454 enum language pretend_language)
7455 {
7456 struct dwarf2_queue_item *item;
7457
7458 per_cu->queued = 1;
7459 item = xmalloc (sizeof (*item));
7460 item->per_cu = per_cu;
7461 item->pretend_language = pretend_language;
7462 item->next = NULL;
7463
7464 if (dwarf2_queue == NULL)
7465 dwarf2_queue = item;
7466 else
7467 dwarf2_queue_tail->next = item;
7468
7469 dwarf2_queue_tail = item;
7470 }
7471
7472 /* If PER_CU is not yet queued, add it to the queue.
7473 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7474 dependency.
7475 The result is non-zero if PER_CU was queued, otherwise the result is zero
7476 meaning either PER_CU is already queued or it is already loaded.
7477
7478 N.B. There is an invariant here that if a CU is queued then it is loaded.
7479 The caller is required to load PER_CU if we return non-zero. */
7480
7481 static int
7482 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7483 struct dwarf2_per_cu_data *per_cu,
7484 enum language pretend_language)
7485 {
7486 /* We may arrive here during partial symbol reading, if we need full
7487 DIEs to process an unusual case (e.g. template arguments). Do
7488 not queue PER_CU, just tell our caller to load its DIEs. */
7489 if (dwarf2_per_objfile->reading_partial_symbols)
7490 {
7491 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7492 return 1;
7493 return 0;
7494 }
7495
7496 /* Mark the dependence relation so that we don't flush PER_CU
7497 too early. */
7498 if (dependent_cu != NULL)
7499 dwarf2_add_dependence (dependent_cu, per_cu);
7500
7501 /* If it's already on the queue, we have nothing to do. */
7502 if (per_cu->queued)
7503 return 0;
7504
7505 /* If the compilation unit is already loaded, just mark it as
7506 used. */
7507 if (per_cu->cu != NULL)
7508 {
7509 per_cu->cu->last_used = 0;
7510 return 0;
7511 }
7512
7513 /* Add it to the queue. */
7514 queue_comp_unit (per_cu, pretend_language);
7515
7516 return 1;
7517 }
7518
7519 /* Process the queue. */
7520
7521 static void
7522 process_queue (void)
7523 {
7524 struct dwarf2_queue_item *item, *next_item;
7525
7526 if (dwarf_read_debug)
7527 {
7528 fprintf_unfiltered (gdb_stdlog,
7529 "Expanding one or more symtabs of objfile %s ...\n",
7530 objfile_name (dwarf2_per_objfile->objfile));
7531 }
7532
7533 /* The queue starts out with one item, but following a DIE reference
7534 may load a new CU, adding it to the end of the queue. */
7535 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7536 {
7537 if ((dwarf2_per_objfile->using_index
7538 ? !item->per_cu->v.quick->compunit_symtab
7539 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7540 /* Skip dummy CUs. */
7541 && item->per_cu->cu != NULL)
7542 {
7543 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7544 unsigned int debug_print_threshold;
7545 char buf[100];
7546
7547 if (per_cu->is_debug_types)
7548 {
7549 struct signatured_type *sig_type =
7550 (struct signatured_type *) per_cu;
7551
7552 sprintf (buf, "TU %s at offset 0x%x",
7553 hex_string (sig_type->signature),
7554 per_cu->offset.sect_off);
7555 /* There can be 100s of TUs.
7556 Only print them in verbose mode. */
7557 debug_print_threshold = 2;
7558 }
7559 else
7560 {
7561 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7562 debug_print_threshold = 1;
7563 }
7564
7565 if (dwarf_read_debug >= debug_print_threshold)
7566 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7567
7568 if (per_cu->is_debug_types)
7569 process_full_type_unit (per_cu, item->pretend_language);
7570 else
7571 process_full_comp_unit (per_cu, item->pretend_language);
7572
7573 if (dwarf_read_debug >= debug_print_threshold)
7574 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7575 }
7576
7577 item->per_cu->queued = 0;
7578 next_item = item->next;
7579 xfree (item);
7580 }
7581
7582 dwarf2_queue_tail = NULL;
7583
7584 if (dwarf_read_debug)
7585 {
7586 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7587 objfile_name (dwarf2_per_objfile->objfile));
7588 }
7589 }
7590
7591 /* Free all allocated queue entries. This function only releases anything if
7592 an error was thrown; if the queue was processed then it would have been
7593 freed as we went along. */
7594
7595 static void
7596 dwarf2_release_queue (void *dummy)
7597 {
7598 struct dwarf2_queue_item *item, *last;
7599
7600 item = dwarf2_queue;
7601 while (item)
7602 {
7603 /* Anything still marked queued is likely to be in an
7604 inconsistent state, so discard it. */
7605 if (item->per_cu->queued)
7606 {
7607 if (item->per_cu->cu != NULL)
7608 free_one_cached_comp_unit (item->per_cu);
7609 item->per_cu->queued = 0;
7610 }
7611
7612 last = item;
7613 item = item->next;
7614 xfree (last);
7615 }
7616
7617 dwarf2_queue = dwarf2_queue_tail = NULL;
7618 }
7619
7620 /* Read in full symbols for PST, and anything it depends on. */
7621
7622 static void
7623 psymtab_to_symtab_1 (struct partial_symtab *pst)
7624 {
7625 struct dwarf2_per_cu_data *per_cu;
7626 int i;
7627
7628 if (pst->readin)
7629 return;
7630
7631 for (i = 0; i < pst->number_of_dependencies; i++)
7632 if (!pst->dependencies[i]->readin
7633 && pst->dependencies[i]->user == NULL)
7634 {
7635 /* Inform about additional files that need to be read in. */
7636 if (info_verbose)
7637 {
7638 /* FIXME: i18n: Need to make this a single string. */
7639 fputs_filtered (" ", gdb_stdout);
7640 wrap_here ("");
7641 fputs_filtered ("and ", gdb_stdout);
7642 wrap_here ("");
7643 printf_filtered ("%s...", pst->dependencies[i]->filename);
7644 wrap_here (""); /* Flush output. */
7645 gdb_flush (gdb_stdout);
7646 }
7647 psymtab_to_symtab_1 (pst->dependencies[i]);
7648 }
7649
7650 per_cu = pst->read_symtab_private;
7651
7652 if (per_cu == NULL)
7653 {
7654 /* It's an include file, no symbols to read for it.
7655 Everything is in the parent symtab. */
7656 pst->readin = 1;
7657 return;
7658 }
7659
7660 dw2_do_instantiate_symtab (per_cu);
7661 }
7662
7663 /* Trivial hash function for die_info: the hash value of a DIE
7664 is its offset in .debug_info for this objfile. */
7665
7666 static hashval_t
7667 die_hash (const void *item)
7668 {
7669 const struct die_info *die = item;
7670
7671 return die->offset.sect_off;
7672 }
7673
7674 /* Trivial comparison function for die_info structures: two DIEs
7675 are equal if they have the same offset. */
7676
7677 static int
7678 die_eq (const void *item_lhs, const void *item_rhs)
7679 {
7680 const struct die_info *die_lhs = item_lhs;
7681 const struct die_info *die_rhs = item_rhs;
7682
7683 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7684 }
7685
7686 /* die_reader_func for load_full_comp_unit.
7687 This is identical to read_signatured_type_reader,
7688 but is kept separate for now. */
7689
7690 static void
7691 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7692 const gdb_byte *info_ptr,
7693 struct die_info *comp_unit_die,
7694 int has_children,
7695 void *data)
7696 {
7697 struct dwarf2_cu *cu = reader->cu;
7698 enum language *language_ptr = data;
7699
7700 gdb_assert (cu->die_hash == NULL);
7701 cu->die_hash =
7702 htab_create_alloc_ex (cu->header.length / 12,
7703 die_hash,
7704 die_eq,
7705 NULL,
7706 &cu->comp_unit_obstack,
7707 hashtab_obstack_allocate,
7708 dummy_obstack_deallocate);
7709
7710 if (has_children)
7711 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7712 &info_ptr, comp_unit_die);
7713 cu->dies = comp_unit_die;
7714 /* comp_unit_die is not stored in die_hash, no need. */
7715
7716 /* We try not to read any attributes in this function, because not
7717 all CUs needed for references have been loaded yet, and symbol
7718 table processing isn't initialized. But we have to set the CU language,
7719 or we won't be able to build types correctly.
7720 Similarly, if we do not read the producer, we can not apply
7721 producer-specific interpretation. */
7722 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7723 }
7724
7725 /* Load the DIEs associated with PER_CU into memory. */
7726
7727 static void
7728 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7729 enum language pretend_language)
7730 {
7731 gdb_assert (! this_cu->is_debug_types);
7732
7733 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7734 load_full_comp_unit_reader, &pretend_language);
7735 }
7736
7737 /* Add a DIE to the delayed physname list. */
7738
7739 static void
7740 add_to_method_list (struct type *type, int fnfield_index, int index,
7741 const char *name, struct die_info *die,
7742 struct dwarf2_cu *cu)
7743 {
7744 struct delayed_method_info mi;
7745 mi.type = type;
7746 mi.fnfield_index = fnfield_index;
7747 mi.index = index;
7748 mi.name = name;
7749 mi.die = die;
7750 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7751 }
7752
7753 /* A cleanup for freeing the delayed method list. */
7754
7755 static void
7756 free_delayed_list (void *ptr)
7757 {
7758 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7759 if (cu->method_list != NULL)
7760 {
7761 VEC_free (delayed_method_info, cu->method_list);
7762 cu->method_list = NULL;
7763 }
7764 }
7765
7766 /* Compute the physnames of any methods on the CU's method list.
7767
7768 The computation of method physnames is delayed in order to avoid the
7769 (bad) condition that one of the method's formal parameters is of an as yet
7770 incomplete type. */
7771
7772 static void
7773 compute_delayed_physnames (struct dwarf2_cu *cu)
7774 {
7775 int i;
7776 struct delayed_method_info *mi;
7777 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7778 {
7779 const char *physname;
7780 struct fn_fieldlist *fn_flp
7781 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7782 physname = dwarf2_physname (mi->name, mi->die, cu);
7783 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7784 = physname ? physname : "";
7785 }
7786 }
7787
7788 /* Go objects should be embedded in a DW_TAG_module DIE,
7789 and it's not clear if/how imported objects will appear.
7790 To keep Go support simple until that's worked out,
7791 go back through what we've read and create something usable.
7792 We could do this while processing each DIE, and feels kinda cleaner,
7793 but that way is more invasive.
7794 This is to, for example, allow the user to type "p var" or "b main"
7795 without having to specify the package name, and allow lookups
7796 of module.object to work in contexts that use the expression
7797 parser. */
7798
7799 static void
7800 fixup_go_packaging (struct dwarf2_cu *cu)
7801 {
7802 char *package_name = NULL;
7803 struct pending *list;
7804 int i;
7805
7806 for (list = global_symbols; list != NULL; list = list->next)
7807 {
7808 for (i = 0; i < list->nsyms; ++i)
7809 {
7810 struct symbol *sym = list->symbol[i];
7811
7812 if (SYMBOL_LANGUAGE (sym) == language_go
7813 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7814 {
7815 char *this_package_name = go_symbol_package_name (sym);
7816
7817 if (this_package_name == NULL)
7818 continue;
7819 if (package_name == NULL)
7820 package_name = this_package_name;
7821 else
7822 {
7823 if (strcmp (package_name, this_package_name) != 0)
7824 complaint (&symfile_complaints,
7825 _("Symtab %s has objects from two different Go packages: %s and %s"),
7826 (symbol_symtab (sym) != NULL
7827 ? symtab_to_filename_for_display
7828 (symbol_symtab (sym))
7829 : objfile_name (cu->objfile)),
7830 this_package_name, package_name);
7831 xfree (this_package_name);
7832 }
7833 }
7834 }
7835 }
7836
7837 if (package_name != NULL)
7838 {
7839 struct objfile *objfile = cu->objfile;
7840 const char *saved_package_name
7841 = obstack_copy0 (&objfile->per_bfd->storage_obstack,
7842 package_name,
7843 strlen (package_name));
7844 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7845 saved_package_name, objfile);
7846 struct symbol *sym;
7847
7848 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7849
7850 sym = allocate_symbol (objfile);
7851 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7852 SYMBOL_SET_NAMES (sym, saved_package_name,
7853 strlen (saved_package_name), 0, objfile);
7854 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7855 e.g., "main" finds the "main" module and not C's main(). */
7856 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7858 SYMBOL_TYPE (sym) = type;
7859
7860 add_symbol_to_list (sym, &global_symbols);
7861
7862 xfree (package_name);
7863 }
7864 }
7865
7866 /* Return the symtab for PER_CU. This works properly regardless of
7867 whether we're using the index or psymtabs. */
7868
7869 static struct compunit_symtab *
7870 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7871 {
7872 return (dwarf2_per_objfile->using_index
7873 ? per_cu->v.quick->compunit_symtab
7874 : per_cu->v.psymtab->compunit_symtab);
7875 }
7876
7877 /* A helper function for computing the list of all symbol tables
7878 included by PER_CU. */
7879
7880 static void
7881 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7882 htab_t all_children, htab_t all_type_symtabs,
7883 struct dwarf2_per_cu_data *per_cu,
7884 struct compunit_symtab *immediate_parent)
7885 {
7886 void **slot;
7887 int ix;
7888 struct compunit_symtab *cust;
7889 struct dwarf2_per_cu_data *iter;
7890
7891 slot = htab_find_slot (all_children, per_cu, INSERT);
7892 if (*slot != NULL)
7893 {
7894 /* This inclusion and its children have been processed. */
7895 return;
7896 }
7897
7898 *slot = per_cu;
7899 /* Only add a CU if it has a symbol table. */
7900 cust = get_compunit_symtab (per_cu);
7901 if (cust != NULL)
7902 {
7903 /* If this is a type unit only add its symbol table if we haven't
7904 seen it yet (type unit per_cu's can share symtabs). */
7905 if (per_cu->is_debug_types)
7906 {
7907 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
7908 if (*slot == NULL)
7909 {
7910 *slot = cust;
7911 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7912 if (cust->user == NULL)
7913 cust->user = immediate_parent;
7914 }
7915 }
7916 else
7917 {
7918 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7919 if (cust->user == NULL)
7920 cust->user = immediate_parent;
7921 }
7922 }
7923
7924 for (ix = 0;
7925 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7926 ++ix)
7927 {
7928 recursively_compute_inclusions (result, all_children,
7929 all_type_symtabs, iter, cust);
7930 }
7931 }
7932
7933 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
7934 PER_CU. */
7935
7936 static void
7937 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7938 {
7939 gdb_assert (! per_cu->is_debug_types);
7940
7941 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7942 {
7943 int ix, len;
7944 struct dwarf2_per_cu_data *per_cu_iter;
7945 struct compunit_symtab *compunit_symtab_iter;
7946 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
7947 htab_t all_children, all_type_symtabs;
7948 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
7949
7950 /* If we don't have a symtab, we can just skip this case. */
7951 if (cust == NULL)
7952 return;
7953
7954 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7955 NULL, xcalloc, xfree);
7956 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7957 NULL, xcalloc, xfree);
7958
7959 for (ix = 0;
7960 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7961 ix, per_cu_iter);
7962 ++ix)
7963 {
7964 recursively_compute_inclusions (&result_symtabs, all_children,
7965 all_type_symtabs, per_cu_iter,
7966 cust);
7967 }
7968
7969 /* Now we have a transitive closure of all the included symtabs. */
7970 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7971 cust->includes
7972 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7973 (len + 1) * sizeof (struct compunit_symtab *));
7974 for (ix = 0;
7975 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
7976 compunit_symtab_iter);
7977 ++ix)
7978 cust->includes[ix] = compunit_symtab_iter;
7979 cust->includes[len] = NULL;
7980
7981 VEC_free (compunit_symtab_ptr, result_symtabs);
7982 htab_delete (all_children);
7983 htab_delete (all_type_symtabs);
7984 }
7985 }
7986
7987 /* Compute the 'includes' field for the symtabs of all the CUs we just
7988 read. */
7989
7990 static void
7991 process_cu_includes (void)
7992 {
7993 int ix;
7994 struct dwarf2_per_cu_data *iter;
7995
7996 for (ix = 0;
7997 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7998 ix, iter);
7999 ++ix)
8000 {
8001 if (! iter->is_debug_types)
8002 compute_compunit_symtab_includes (iter);
8003 }
8004
8005 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8006 }
8007
8008 /* Generate full symbol information for PER_CU, whose DIEs have
8009 already been loaded into memory. */
8010
8011 static void
8012 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8013 enum language pretend_language)
8014 {
8015 struct dwarf2_cu *cu = per_cu->cu;
8016 struct objfile *objfile = per_cu->objfile;
8017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8018 CORE_ADDR lowpc, highpc;
8019 struct compunit_symtab *cust;
8020 struct cleanup *back_to, *delayed_list_cleanup;
8021 CORE_ADDR baseaddr;
8022 struct block *static_block;
8023 CORE_ADDR addr;
8024
8025 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8026
8027 buildsym_init ();
8028 back_to = make_cleanup (really_free_pendings, NULL);
8029 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8030
8031 cu->list_in_scope = &file_symbols;
8032
8033 cu->language = pretend_language;
8034 cu->language_defn = language_def (cu->language);
8035
8036 /* Do line number decoding in read_file_scope () */
8037 process_die (cu->dies, cu);
8038
8039 /* For now fudge the Go package. */
8040 if (cu->language == language_go)
8041 fixup_go_packaging (cu);
8042
8043 /* Now that we have processed all the DIEs in the CU, all the types
8044 should be complete, and it should now be safe to compute all of the
8045 physnames. */
8046 compute_delayed_physnames (cu);
8047 do_cleanups (delayed_list_cleanup);
8048
8049 /* Some compilers don't define a DW_AT_high_pc attribute for the
8050 compilation unit. If the DW_AT_high_pc is missing, synthesize
8051 it, by scanning the DIE's below the compilation unit. */
8052 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8053
8054 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8055 static_block = end_symtab_get_static_block (addr, 0, 1);
8056
8057 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8058 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8059 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8060 addrmap to help ensure it has an accurate map of pc values belonging to
8061 this comp unit. */
8062 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8063
8064 cust = end_symtab_from_static_block (static_block,
8065 SECT_OFF_TEXT (objfile), 0);
8066
8067 if (cust != NULL)
8068 {
8069 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8070
8071 /* Set symtab language to language from DW_AT_language. If the
8072 compilation is from a C file generated by language preprocessors, do
8073 not set the language if it was already deduced by start_subfile. */
8074 if (!(cu->language == language_c
8075 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8076 COMPUNIT_FILETABS (cust)->language = cu->language;
8077
8078 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8079 produce DW_AT_location with location lists but it can be possibly
8080 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8081 there were bugs in prologue debug info, fixed later in GCC-4.5
8082 by "unwind info for epilogues" patch (which is not directly related).
8083
8084 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8085 needed, it would be wrong due to missing DW_AT_producer there.
8086
8087 Still one can confuse GDB by using non-standard GCC compilation
8088 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8089 */
8090 if (cu->has_loclist && gcc_4_minor >= 5)
8091 cust->locations_valid = 1;
8092
8093 if (gcc_4_minor >= 5)
8094 cust->epilogue_unwind_valid = 1;
8095
8096 cust->call_site_htab = cu->call_site_htab;
8097 }
8098
8099 if (dwarf2_per_objfile->using_index)
8100 per_cu->v.quick->compunit_symtab = cust;
8101 else
8102 {
8103 struct partial_symtab *pst = per_cu->v.psymtab;
8104 pst->compunit_symtab = cust;
8105 pst->readin = 1;
8106 }
8107
8108 /* Push it for inclusion processing later. */
8109 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8110
8111 do_cleanups (back_to);
8112 }
8113
8114 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8115 already been loaded into memory. */
8116
8117 static void
8118 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8119 enum language pretend_language)
8120 {
8121 struct dwarf2_cu *cu = per_cu->cu;
8122 struct objfile *objfile = per_cu->objfile;
8123 struct compunit_symtab *cust;
8124 struct cleanup *back_to, *delayed_list_cleanup;
8125 struct signatured_type *sig_type;
8126
8127 gdb_assert (per_cu->is_debug_types);
8128 sig_type = (struct signatured_type *) per_cu;
8129
8130 buildsym_init ();
8131 back_to = make_cleanup (really_free_pendings, NULL);
8132 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8133
8134 cu->list_in_scope = &file_symbols;
8135
8136 cu->language = pretend_language;
8137 cu->language_defn = language_def (cu->language);
8138
8139 /* The symbol tables are set up in read_type_unit_scope. */
8140 process_die (cu->dies, cu);
8141
8142 /* For now fudge the Go package. */
8143 if (cu->language == language_go)
8144 fixup_go_packaging (cu);
8145
8146 /* Now that we have processed all the DIEs in the CU, all the types
8147 should be complete, and it should now be safe to compute all of the
8148 physnames. */
8149 compute_delayed_physnames (cu);
8150 do_cleanups (delayed_list_cleanup);
8151
8152 /* TUs share symbol tables.
8153 If this is the first TU to use this symtab, complete the construction
8154 of it with end_expandable_symtab. Otherwise, complete the addition of
8155 this TU's symbols to the existing symtab. */
8156 if (sig_type->type_unit_group->compunit_symtab == NULL)
8157 {
8158 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8159 sig_type->type_unit_group->compunit_symtab = cust;
8160
8161 if (cust != NULL)
8162 {
8163 /* Set symtab language to language from DW_AT_language. If the
8164 compilation is from a C file generated by language preprocessors,
8165 do not set the language if it was already deduced by
8166 start_subfile. */
8167 if (!(cu->language == language_c
8168 && COMPUNIT_FILETABS (cust)->language != language_c))
8169 COMPUNIT_FILETABS (cust)->language = cu->language;
8170 }
8171 }
8172 else
8173 {
8174 augment_type_symtab ();
8175 cust = sig_type->type_unit_group->compunit_symtab;
8176 }
8177
8178 if (dwarf2_per_objfile->using_index)
8179 per_cu->v.quick->compunit_symtab = cust;
8180 else
8181 {
8182 struct partial_symtab *pst = per_cu->v.psymtab;
8183 pst->compunit_symtab = cust;
8184 pst->readin = 1;
8185 }
8186
8187 do_cleanups (back_to);
8188 }
8189
8190 /* Process an imported unit DIE. */
8191
8192 static void
8193 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8194 {
8195 struct attribute *attr;
8196
8197 /* For now we don't handle imported units in type units. */
8198 if (cu->per_cu->is_debug_types)
8199 {
8200 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8201 " supported in type units [in module %s]"),
8202 objfile_name (cu->objfile));
8203 }
8204
8205 attr = dwarf2_attr (die, DW_AT_import, cu);
8206 if (attr != NULL)
8207 {
8208 struct dwarf2_per_cu_data *per_cu;
8209 struct symtab *imported_symtab;
8210 sect_offset offset;
8211 int is_dwz;
8212
8213 offset = dwarf2_get_ref_die_offset (attr);
8214 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8215 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8216
8217 /* If necessary, add it to the queue and load its DIEs. */
8218 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8219 load_full_comp_unit (per_cu, cu->language);
8220
8221 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8222 per_cu);
8223 }
8224 }
8225
8226 /* Reset the in_process bit of a die. */
8227
8228 static void
8229 reset_die_in_process (void *arg)
8230 {
8231 struct die_info *die = arg;
8232
8233 die->in_process = 0;
8234 }
8235
8236 /* Process a die and its children. */
8237
8238 static void
8239 process_die (struct die_info *die, struct dwarf2_cu *cu)
8240 {
8241 struct cleanup *in_process;
8242
8243 /* We should only be processing those not already in process. */
8244 gdb_assert (!die->in_process);
8245
8246 die->in_process = 1;
8247 in_process = make_cleanup (reset_die_in_process,die);
8248
8249 switch (die->tag)
8250 {
8251 case DW_TAG_padding:
8252 break;
8253 case DW_TAG_compile_unit:
8254 case DW_TAG_partial_unit:
8255 read_file_scope (die, cu);
8256 break;
8257 case DW_TAG_type_unit:
8258 read_type_unit_scope (die, cu);
8259 break;
8260 case DW_TAG_subprogram:
8261 case DW_TAG_inlined_subroutine:
8262 read_func_scope (die, cu);
8263 break;
8264 case DW_TAG_lexical_block:
8265 case DW_TAG_try_block:
8266 case DW_TAG_catch_block:
8267 read_lexical_block_scope (die, cu);
8268 break;
8269 case DW_TAG_GNU_call_site:
8270 read_call_site_scope (die, cu);
8271 break;
8272 case DW_TAG_class_type:
8273 case DW_TAG_interface_type:
8274 case DW_TAG_structure_type:
8275 case DW_TAG_union_type:
8276 process_structure_scope (die, cu);
8277 break;
8278 case DW_TAG_enumeration_type:
8279 process_enumeration_scope (die, cu);
8280 break;
8281
8282 /* These dies have a type, but processing them does not create
8283 a symbol or recurse to process the children. Therefore we can
8284 read them on-demand through read_type_die. */
8285 case DW_TAG_subroutine_type:
8286 case DW_TAG_set_type:
8287 case DW_TAG_array_type:
8288 case DW_TAG_pointer_type:
8289 case DW_TAG_ptr_to_member_type:
8290 case DW_TAG_reference_type:
8291 case DW_TAG_string_type:
8292 break;
8293
8294 case DW_TAG_base_type:
8295 case DW_TAG_subrange_type:
8296 case DW_TAG_typedef:
8297 /* Add a typedef symbol for the type definition, if it has a
8298 DW_AT_name. */
8299 new_symbol (die, read_type_die (die, cu), cu);
8300 break;
8301 case DW_TAG_common_block:
8302 read_common_block (die, cu);
8303 break;
8304 case DW_TAG_common_inclusion:
8305 break;
8306 case DW_TAG_namespace:
8307 cu->processing_has_namespace_info = 1;
8308 read_namespace (die, cu);
8309 break;
8310 case DW_TAG_module:
8311 cu->processing_has_namespace_info = 1;
8312 read_module (die, cu);
8313 break;
8314 case DW_TAG_imported_declaration:
8315 cu->processing_has_namespace_info = 1;
8316 if (read_namespace_alias (die, cu))
8317 break;
8318 /* The declaration is not a global namespace alias: fall through. */
8319 case DW_TAG_imported_module:
8320 cu->processing_has_namespace_info = 1;
8321 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8322 || cu->language != language_fortran))
8323 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8324 dwarf_tag_name (die->tag));
8325 read_import_statement (die, cu);
8326 break;
8327
8328 case DW_TAG_imported_unit:
8329 process_imported_unit_die (die, cu);
8330 break;
8331
8332 default:
8333 new_symbol (die, NULL, cu);
8334 break;
8335 }
8336
8337 do_cleanups (in_process);
8338 }
8339 \f
8340 /* DWARF name computation. */
8341
8342 /* A helper function for dwarf2_compute_name which determines whether DIE
8343 needs to have the name of the scope prepended to the name listed in the
8344 die. */
8345
8346 static int
8347 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8348 {
8349 struct attribute *attr;
8350
8351 switch (die->tag)
8352 {
8353 case DW_TAG_namespace:
8354 case DW_TAG_typedef:
8355 case DW_TAG_class_type:
8356 case DW_TAG_interface_type:
8357 case DW_TAG_structure_type:
8358 case DW_TAG_union_type:
8359 case DW_TAG_enumeration_type:
8360 case DW_TAG_enumerator:
8361 case DW_TAG_subprogram:
8362 case DW_TAG_inlined_subroutine:
8363 case DW_TAG_member:
8364 case DW_TAG_imported_declaration:
8365 return 1;
8366
8367 case DW_TAG_variable:
8368 case DW_TAG_constant:
8369 /* We only need to prefix "globally" visible variables. These include
8370 any variable marked with DW_AT_external or any variable that
8371 lives in a namespace. [Variables in anonymous namespaces
8372 require prefixing, but they are not DW_AT_external.] */
8373
8374 if (dwarf2_attr (die, DW_AT_specification, cu))
8375 {
8376 struct dwarf2_cu *spec_cu = cu;
8377
8378 return die_needs_namespace (die_specification (die, &spec_cu),
8379 spec_cu);
8380 }
8381
8382 attr = dwarf2_attr (die, DW_AT_external, cu);
8383 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8384 && die->parent->tag != DW_TAG_module)
8385 return 0;
8386 /* A variable in a lexical block of some kind does not need a
8387 namespace, even though in C++ such variables may be external
8388 and have a mangled name. */
8389 if (die->parent->tag == DW_TAG_lexical_block
8390 || die->parent->tag == DW_TAG_try_block
8391 || die->parent->tag == DW_TAG_catch_block
8392 || die->parent->tag == DW_TAG_subprogram)
8393 return 0;
8394 return 1;
8395
8396 default:
8397 return 0;
8398 }
8399 }
8400
8401 /* Retrieve the last character from a mem_file. */
8402
8403 static void
8404 do_ui_file_peek_last (void *object, const char *buffer, long length)
8405 {
8406 char *last_char_p = (char *) object;
8407
8408 if (length > 0)
8409 *last_char_p = buffer[length - 1];
8410 }
8411
8412 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8413 compute the physname for the object, which include a method's:
8414 - formal parameters (C++/Java),
8415 - receiver type (Go),
8416 - return type (Java).
8417
8418 The term "physname" is a bit confusing.
8419 For C++, for example, it is the demangled name.
8420 For Go, for example, it's the mangled name.
8421
8422 For Ada, return the DIE's linkage name rather than the fully qualified
8423 name. PHYSNAME is ignored..
8424
8425 The result is allocated on the objfile_obstack and canonicalized. */
8426
8427 static const char *
8428 dwarf2_compute_name (const char *name,
8429 struct die_info *die, struct dwarf2_cu *cu,
8430 int physname)
8431 {
8432 struct objfile *objfile = cu->objfile;
8433
8434 if (name == NULL)
8435 name = dwarf2_name (die, cu);
8436
8437 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
8438 compute it by typename_concat inside GDB. */
8439 if (cu->language == language_ada
8440 || (cu->language == language_fortran && physname))
8441 {
8442 /* For Ada unit, we prefer the linkage name over the name, as
8443 the former contains the exported name, which the user expects
8444 to be able to reference. Ideally, we want the user to be able
8445 to reference this entity using either natural or linkage name,
8446 but we haven't started looking at this enhancement yet. */
8447 const char *name;
8448
8449 name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8450 if (name == NULL)
8451 name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8452 if (name != NULL)
8453 return name;
8454 }
8455
8456 /* These are the only languages we know how to qualify names in. */
8457 if (name != NULL
8458 && (cu->language == language_cplus || cu->language == language_java
8459 || cu->language == language_fortran || cu->language == language_d))
8460 {
8461 if (die_needs_namespace (die, cu))
8462 {
8463 long length;
8464 const char *prefix;
8465 struct ui_file *buf;
8466 char *intermediate_name;
8467 const char *canonical_name = NULL;
8468
8469 prefix = determine_prefix (die, cu);
8470 buf = mem_fileopen ();
8471 if (*prefix != '\0')
8472 {
8473 char *prefixed_name = typename_concat (NULL, prefix, name,
8474 physname, cu);
8475
8476 fputs_unfiltered (prefixed_name, buf);
8477 xfree (prefixed_name);
8478 }
8479 else
8480 fputs_unfiltered (name, buf);
8481
8482 /* Template parameters may be specified in the DIE's DW_AT_name, or
8483 as children with DW_TAG_template_type_param or
8484 DW_TAG_value_type_param. If the latter, add them to the name
8485 here. If the name already has template parameters, then
8486 skip this step; some versions of GCC emit both, and
8487 it is more efficient to use the pre-computed name.
8488
8489 Something to keep in mind about this process: it is very
8490 unlikely, or in some cases downright impossible, to produce
8491 something that will match the mangled name of a function.
8492 If the definition of the function has the same debug info,
8493 we should be able to match up with it anyway. But fallbacks
8494 using the minimal symbol, for instance to find a method
8495 implemented in a stripped copy of libstdc++, will not work.
8496 If we do not have debug info for the definition, we will have to
8497 match them up some other way.
8498
8499 When we do name matching there is a related problem with function
8500 templates; two instantiated function templates are allowed to
8501 differ only by their return types, which we do not add here. */
8502
8503 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8504 {
8505 struct attribute *attr;
8506 struct die_info *child;
8507 int first = 1;
8508
8509 die->building_fullname = 1;
8510
8511 for (child = die->child; child != NULL; child = child->sibling)
8512 {
8513 struct type *type;
8514 LONGEST value;
8515 const gdb_byte *bytes;
8516 struct dwarf2_locexpr_baton *baton;
8517 struct value *v;
8518
8519 if (child->tag != DW_TAG_template_type_param
8520 && child->tag != DW_TAG_template_value_param)
8521 continue;
8522
8523 if (first)
8524 {
8525 fputs_unfiltered ("<", buf);
8526 first = 0;
8527 }
8528 else
8529 fputs_unfiltered (", ", buf);
8530
8531 attr = dwarf2_attr (child, DW_AT_type, cu);
8532 if (attr == NULL)
8533 {
8534 complaint (&symfile_complaints,
8535 _("template parameter missing DW_AT_type"));
8536 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8537 continue;
8538 }
8539 type = die_type (child, cu);
8540
8541 if (child->tag == DW_TAG_template_type_param)
8542 {
8543 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
8544 continue;
8545 }
8546
8547 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8548 if (attr == NULL)
8549 {
8550 complaint (&symfile_complaints,
8551 _("template parameter missing "
8552 "DW_AT_const_value"));
8553 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8554 continue;
8555 }
8556
8557 dwarf2_const_value_attr (attr, type, name,
8558 &cu->comp_unit_obstack, cu,
8559 &value, &bytes, &baton);
8560
8561 if (TYPE_NOSIGN (type))
8562 /* GDB prints characters as NUMBER 'CHAR'. If that's
8563 changed, this can use value_print instead. */
8564 c_printchar (value, type, buf);
8565 else
8566 {
8567 struct value_print_options opts;
8568
8569 if (baton != NULL)
8570 v = dwarf2_evaluate_loc_desc (type, NULL,
8571 baton->data,
8572 baton->size,
8573 baton->per_cu);
8574 else if (bytes != NULL)
8575 {
8576 v = allocate_value (type);
8577 memcpy (value_contents_writeable (v), bytes,
8578 TYPE_LENGTH (type));
8579 }
8580 else
8581 v = value_from_longest (type, value);
8582
8583 /* Specify decimal so that we do not depend on
8584 the radix. */
8585 get_formatted_print_options (&opts, 'd');
8586 opts.raw = 1;
8587 value_print (v, buf, &opts);
8588 release_value (v);
8589 value_free (v);
8590 }
8591 }
8592
8593 die->building_fullname = 0;
8594
8595 if (!first)
8596 {
8597 /* Close the argument list, with a space if necessary
8598 (nested templates). */
8599 char last_char = '\0';
8600 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8601 if (last_char == '>')
8602 fputs_unfiltered (" >", buf);
8603 else
8604 fputs_unfiltered (">", buf);
8605 }
8606 }
8607
8608 /* For Java and C++ methods, append formal parameter type
8609 information, if PHYSNAME. */
8610
8611 if (physname && die->tag == DW_TAG_subprogram
8612 && (cu->language == language_cplus
8613 || cu->language == language_java))
8614 {
8615 struct type *type = read_type_die (die, cu);
8616
8617 c_type_print_args (type, buf, 1, cu->language,
8618 &type_print_raw_options);
8619
8620 if (cu->language == language_java)
8621 {
8622 /* For java, we must append the return type to method
8623 names. */
8624 if (die->tag == DW_TAG_subprogram)
8625 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8626 0, 0, &type_print_raw_options);
8627 }
8628 else if (cu->language == language_cplus)
8629 {
8630 /* Assume that an artificial first parameter is
8631 "this", but do not crash if it is not. RealView
8632 marks unnamed (and thus unused) parameters as
8633 artificial; there is no way to differentiate
8634 the two cases. */
8635 if (TYPE_NFIELDS (type) > 0
8636 && TYPE_FIELD_ARTIFICIAL (type, 0)
8637 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8638 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8639 0))))
8640 fputs_unfiltered (" const", buf);
8641 }
8642 }
8643
8644 intermediate_name = ui_file_xstrdup (buf, &length);
8645 ui_file_delete (buf);
8646
8647 if (cu->language == language_cplus)
8648 canonical_name
8649 = dwarf2_canonicalize_name (intermediate_name, cu,
8650 &objfile->per_bfd->storage_obstack);
8651
8652 /* If we only computed INTERMEDIATE_NAME, or if
8653 INTERMEDIATE_NAME is already canonical, then we need to
8654 copy it to the appropriate obstack. */
8655 if (canonical_name == NULL || canonical_name == intermediate_name)
8656 name = obstack_copy0 (&objfile->per_bfd->storage_obstack,
8657 intermediate_name,
8658 strlen (intermediate_name));
8659 else
8660 name = canonical_name;
8661
8662 xfree (intermediate_name);
8663 }
8664 }
8665
8666 return name;
8667 }
8668
8669 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8670 If scope qualifiers are appropriate they will be added. The result
8671 will be allocated on the storage_obstack, or NULL if the DIE does
8672 not have a name. NAME may either be from a previous call to
8673 dwarf2_name or NULL.
8674
8675 The output string will be canonicalized (if C++/Java). */
8676
8677 static const char *
8678 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8679 {
8680 return dwarf2_compute_name (name, die, cu, 0);
8681 }
8682
8683 /* Construct a physname for the given DIE in CU. NAME may either be
8684 from a previous call to dwarf2_name or NULL. The result will be
8685 allocated on the objfile_objstack or NULL if the DIE does not have a
8686 name.
8687
8688 The output string will be canonicalized (if C++/Java). */
8689
8690 static const char *
8691 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8692 {
8693 struct objfile *objfile = cu->objfile;
8694 struct attribute *attr;
8695 const char *retval, *mangled = NULL, *canon = NULL;
8696 struct cleanup *back_to;
8697 int need_copy = 1;
8698
8699 /* In this case dwarf2_compute_name is just a shortcut not building anything
8700 on its own. */
8701 if (!die_needs_namespace (die, cu))
8702 return dwarf2_compute_name (name, die, cu, 1);
8703
8704 back_to = make_cleanup (null_cleanup, NULL);
8705
8706 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8707 if (mangled == NULL)
8708 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8709
8710 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8711 has computed. */
8712 if (mangled != NULL)
8713 {
8714 char *demangled;
8715
8716 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8717 type. It is easier for GDB users to search for such functions as
8718 `name(params)' than `long name(params)'. In such case the minimal
8719 symbol names do not match the full symbol names but for template
8720 functions there is never a need to look up their definition from their
8721 declaration so the only disadvantage remains the minimal symbol
8722 variant `long name(params)' does not have the proper inferior type.
8723 */
8724
8725 if (cu->language == language_go)
8726 {
8727 /* This is a lie, but we already lie to the caller new_symbol_full.
8728 new_symbol_full assumes we return the mangled name.
8729 This just undoes that lie until things are cleaned up. */
8730 demangled = NULL;
8731 }
8732 else
8733 {
8734 demangled = gdb_demangle (mangled,
8735 (DMGL_PARAMS | DMGL_ANSI
8736 | (cu->language == language_java
8737 ? DMGL_JAVA | DMGL_RET_POSTFIX
8738 : DMGL_RET_DROP)));
8739 }
8740 if (demangled)
8741 {
8742 make_cleanup (xfree, demangled);
8743 canon = demangled;
8744 }
8745 else
8746 {
8747 canon = mangled;
8748 need_copy = 0;
8749 }
8750 }
8751
8752 if (canon == NULL || check_physname)
8753 {
8754 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8755
8756 if (canon != NULL && strcmp (physname, canon) != 0)
8757 {
8758 /* It may not mean a bug in GDB. The compiler could also
8759 compute DW_AT_linkage_name incorrectly. But in such case
8760 GDB would need to be bug-to-bug compatible. */
8761
8762 complaint (&symfile_complaints,
8763 _("Computed physname <%s> does not match demangled <%s> "
8764 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8765 physname, canon, mangled, die->offset.sect_off,
8766 objfile_name (objfile));
8767
8768 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8769 is available here - over computed PHYSNAME. It is safer
8770 against both buggy GDB and buggy compilers. */
8771
8772 retval = canon;
8773 }
8774 else
8775 {
8776 retval = physname;
8777 need_copy = 0;
8778 }
8779 }
8780 else
8781 retval = canon;
8782
8783 if (need_copy)
8784 retval = obstack_copy0 (&objfile->per_bfd->storage_obstack,
8785 retval, strlen (retval));
8786
8787 do_cleanups (back_to);
8788 return retval;
8789 }
8790
8791 /* Inspect DIE in CU for a namespace alias. If one exists, record
8792 a new symbol for it.
8793
8794 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8795
8796 static int
8797 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8798 {
8799 struct attribute *attr;
8800
8801 /* If the die does not have a name, this is not a namespace
8802 alias. */
8803 attr = dwarf2_attr (die, DW_AT_name, cu);
8804 if (attr != NULL)
8805 {
8806 int num;
8807 struct die_info *d = die;
8808 struct dwarf2_cu *imported_cu = cu;
8809
8810 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8811 keep inspecting DIEs until we hit the underlying import. */
8812 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8813 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8814 {
8815 attr = dwarf2_attr (d, DW_AT_import, cu);
8816 if (attr == NULL)
8817 break;
8818
8819 d = follow_die_ref (d, attr, &imported_cu);
8820 if (d->tag != DW_TAG_imported_declaration)
8821 break;
8822 }
8823
8824 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8825 {
8826 complaint (&symfile_complaints,
8827 _("DIE at 0x%x has too many recursively imported "
8828 "declarations"), d->offset.sect_off);
8829 return 0;
8830 }
8831
8832 if (attr != NULL)
8833 {
8834 struct type *type;
8835 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8836
8837 type = get_die_type_at_offset (offset, cu->per_cu);
8838 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8839 {
8840 /* This declaration is a global namespace alias. Add
8841 a symbol for it whose type is the aliased namespace. */
8842 new_symbol (die, type, cu);
8843 return 1;
8844 }
8845 }
8846 }
8847
8848 return 0;
8849 }
8850
8851 /* Return the using directives repository (global or local?) to use in the
8852 current context for LANGUAGE.
8853
8854 For Ada, imported declarations can materialize renamings, which *may* be
8855 global. However it is impossible (for now?) in DWARF to distinguish
8856 "external" imported declarations and "static" ones. As all imported
8857 declarations seem to be static in all other languages, make them all CU-wide
8858 global only in Ada. */
8859
8860 static struct using_direct **
8861 using_directives (enum language language)
8862 {
8863 if (language == language_ada && context_stack_depth == 0)
8864 return &global_using_directives;
8865 else
8866 return &local_using_directives;
8867 }
8868
8869 /* Read the import statement specified by the given die and record it. */
8870
8871 static void
8872 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8873 {
8874 struct objfile *objfile = cu->objfile;
8875 struct attribute *import_attr;
8876 struct die_info *imported_die, *child_die;
8877 struct dwarf2_cu *imported_cu;
8878 const char *imported_name;
8879 const char *imported_name_prefix;
8880 const char *canonical_name;
8881 const char *import_alias;
8882 const char *imported_declaration = NULL;
8883 const char *import_prefix;
8884 VEC (const_char_ptr) *excludes = NULL;
8885 struct cleanup *cleanups;
8886
8887 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8888 if (import_attr == NULL)
8889 {
8890 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8891 dwarf_tag_name (die->tag));
8892 return;
8893 }
8894
8895 imported_cu = cu;
8896 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8897 imported_name = dwarf2_name (imported_die, imported_cu);
8898 if (imported_name == NULL)
8899 {
8900 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8901
8902 The import in the following code:
8903 namespace A
8904 {
8905 typedef int B;
8906 }
8907
8908 int main ()
8909 {
8910 using A::B;
8911 B b;
8912 return b;
8913 }
8914
8915 ...
8916 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8917 <52> DW_AT_decl_file : 1
8918 <53> DW_AT_decl_line : 6
8919 <54> DW_AT_import : <0x75>
8920 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8921 <59> DW_AT_name : B
8922 <5b> DW_AT_decl_file : 1
8923 <5c> DW_AT_decl_line : 2
8924 <5d> DW_AT_type : <0x6e>
8925 ...
8926 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8927 <76> DW_AT_byte_size : 4
8928 <77> DW_AT_encoding : 5 (signed)
8929
8930 imports the wrong die ( 0x75 instead of 0x58 ).
8931 This case will be ignored until the gcc bug is fixed. */
8932 return;
8933 }
8934
8935 /* Figure out the local name after import. */
8936 import_alias = dwarf2_name (die, cu);
8937
8938 /* Figure out where the statement is being imported to. */
8939 import_prefix = determine_prefix (die, cu);
8940
8941 /* Figure out what the scope of the imported die is and prepend it
8942 to the name of the imported die. */
8943 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8944
8945 if (imported_die->tag != DW_TAG_namespace
8946 && imported_die->tag != DW_TAG_module)
8947 {
8948 imported_declaration = imported_name;
8949 canonical_name = imported_name_prefix;
8950 }
8951 else if (strlen (imported_name_prefix) > 0)
8952 canonical_name = obconcat (&objfile->objfile_obstack,
8953 imported_name_prefix,
8954 (cu->language == language_d ? "." : "::"),
8955 imported_name, (char *) NULL);
8956 else
8957 canonical_name = imported_name;
8958
8959 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8960
8961 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8962 for (child_die = die->child; child_die && child_die->tag;
8963 child_die = sibling_die (child_die))
8964 {
8965 /* DWARF-4: A Fortran use statement with a “rename list” may be
8966 represented by an imported module entry with an import attribute
8967 referring to the module and owned entries corresponding to those
8968 entities that are renamed as part of being imported. */
8969
8970 if (child_die->tag != DW_TAG_imported_declaration)
8971 {
8972 complaint (&symfile_complaints,
8973 _("child DW_TAG_imported_declaration expected "
8974 "- DIE at 0x%x [in module %s]"),
8975 child_die->offset.sect_off, objfile_name (objfile));
8976 continue;
8977 }
8978
8979 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8980 if (import_attr == NULL)
8981 {
8982 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8983 dwarf_tag_name (child_die->tag));
8984 continue;
8985 }
8986
8987 imported_cu = cu;
8988 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8989 &imported_cu);
8990 imported_name = dwarf2_name (imported_die, imported_cu);
8991 if (imported_name == NULL)
8992 {
8993 complaint (&symfile_complaints,
8994 _("child DW_TAG_imported_declaration has unknown "
8995 "imported name - DIE at 0x%x [in module %s]"),
8996 child_die->offset.sect_off, objfile_name (objfile));
8997 continue;
8998 }
8999
9000 VEC_safe_push (const_char_ptr, excludes, imported_name);
9001
9002 process_die (child_die, cu);
9003 }
9004
9005 add_using_directive (using_directives (cu->language),
9006 import_prefix,
9007 canonical_name,
9008 import_alias,
9009 imported_declaration,
9010 excludes,
9011 0,
9012 &objfile->objfile_obstack);
9013
9014 do_cleanups (cleanups);
9015 }
9016
9017 /* Cleanup function for handle_DW_AT_stmt_list. */
9018
9019 static void
9020 free_cu_line_header (void *arg)
9021 {
9022 struct dwarf2_cu *cu = arg;
9023
9024 free_line_header (cu->line_header);
9025 cu->line_header = NULL;
9026 }
9027
9028 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9029 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9030 this, it was first present in GCC release 4.3.0. */
9031
9032 static int
9033 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9034 {
9035 if (!cu->checked_producer)
9036 check_producer (cu);
9037
9038 return cu->producer_is_gcc_lt_4_3;
9039 }
9040
9041 static void
9042 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9043 const char **name, const char **comp_dir)
9044 {
9045 /* Find the filename. Do not use dwarf2_name here, since the filename
9046 is not a source language identifier. */
9047 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9048 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9049
9050 if (*comp_dir == NULL
9051 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9052 && IS_ABSOLUTE_PATH (*name))
9053 {
9054 char *d = ldirname (*name);
9055
9056 *comp_dir = d;
9057 if (d != NULL)
9058 make_cleanup (xfree, d);
9059 }
9060 if (*comp_dir != NULL)
9061 {
9062 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9063 directory, get rid of it. */
9064 char *cp = strchr (*comp_dir, ':');
9065
9066 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9067 *comp_dir = cp + 1;
9068 }
9069
9070 if (*name == NULL)
9071 *name = "<unknown>";
9072 }
9073
9074 /* Handle DW_AT_stmt_list for a compilation unit.
9075 DIE is the DW_TAG_compile_unit die for CU.
9076 COMP_DIR is the compilation directory. LOWPC is passed to
9077 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9078
9079 static void
9080 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9081 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9082 {
9083 struct objfile *objfile = dwarf2_per_objfile->objfile;
9084 struct attribute *attr;
9085 unsigned int line_offset;
9086 struct line_header line_header_local;
9087 hashval_t line_header_local_hash;
9088 unsigned u;
9089 void **slot;
9090 int decode_mapping;
9091
9092 gdb_assert (! cu->per_cu->is_debug_types);
9093
9094 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9095 if (attr == NULL)
9096 return;
9097
9098 line_offset = DW_UNSND (attr);
9099
9100 /* The line header hash table is only created if needed (it exists to
9101 prevent redundant reading of the line table for partial_units).
9102 If we're given a partial_unit, we'll need it. If we're given a
9103 compile_unit, then use the line header hash table if it's already
9104 created, but don't create one just yet. */
9105
9106 if (dwarf2_per_objfile->line_header_hash == NULL
9107 && die->tag == DW_TAG_partial_unit)
9108 {
9109 dwarf2_per_objfile->line_header_hash
9110 = htab_create_alloc_ex (127, line_header_hash_voidp,
9111 line_header_eq_voidp,
9112 free_line_header_voidp,
9113 &objfile->objfile_obstack,
9114 hashtab_obstack_allocate,
9115 dummy_obstack_deallocate);
9116 }
9117
9118 line_header_local.offset.sect_off = line_offset;
9119 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9120 line_header_local_hash = line_header_hash (&line_header_local);
9121 if (dwarf2_per_objfile->line_header_hash != NULL)
9122 {
9123 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9124 &line_header_local,
9125 line_header_local_hash, NO_INSERT);
9126
9127 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9128 is not present in *SLOT (since if there is something in *SLOT then
9129 it will be for a partial_unit). */
9130 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9131 {
9132 gdb_assert (*slot != NULL);
9133 cu->line_header = *slot;
9134 return;
9135 }
9136 }
9137
9138 /* dwarf_decode_line_header does not yet provide sufficient information.
9139 We always have to call also dwarf_decode_lines for it. */
9140 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9141 if (cu->line_header == NULL)
9142 return;
9143
9144 if (dwarf2_per_objfile->line_header_hash == NULL)
9145 slot = NULL;
9146 else
9147 {
9148 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9149 &line_header_local,
9150 line_header_local_hash, INSERT);
9151 gdb_assert (slot != NULL);
9152 }
9153 if (slot != NULL && *slot == NULL)
9154 {
9155 /* This newly decoded line number information unit will be owned
9156 by line_header_hash hash table. */
9157 *slot = cu->line_header;
9158 }
9159 else
9160 {
9161 /* We cannot free any current entry in (*slot) as that struct line_header
9162 may be already used by multiple CUs. Create only temporary decoded
9163 line_header for this CU - it may happen at most once for each line
9164 number information unit. And if we're not using line_header_hash
9165 then this is what we want as well. */
9166 gdb_assert (die->tag != DW_TAG_partial_unit);
9167 make_cleanup (free_cu_line_header, cu);
9168 }
9169 decode_mapping = (die->tag != DW_TAG_partial_unit);
9170 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9171 decode_mapping);
9172 }
9173
9174 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9175
9176 static void
9177 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9178 {
9179 struct objfile *objfile = dwarf2_per_objfile->objfile;
9180 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9181 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9182 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9183 CORE_ADDR highpc = ((CORE_ADDR) 0);
9184 struct attribute *attr;
9185 const char *name = NULL;
9186 const char *comp_dir = NULL;
9187 struct die_info *child_die;
9188 bfd *abfd = objfile->obfd;
9189 CORE_ADDR baseaddr;
9190
9191 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9192
9193 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9194
9195 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9196 from finish_block. */
9197 if (lowpc == ((CORE_ADDR) -1))
9198 lowpc = highpc;
9199 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9200
9201 find_file_and_directory (die, cu, &name, &comp_dir);
9202
9203 prepare_one_comp_unit (cu, die, cu->language);
9204
9205 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9206 standardised yet. As a workaround for the language detection we fall
9207 back to the DW_AT_producer string. */
9208 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9209 cu->language = language_opencl;
9210
9211 /* Similar hack for Go. */
9212 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9213 set_cu_language (DW_LANG_Go, cu);
9214
9215 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9216
9217 /* Decode line number information if present. We do this before
9218 processing child DIEs, so that the line header table is available
9219 for DW_AT_decl_file. */
9220 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9221
9222 /* Process all dies in compilation unit. */
9223 if (die->child != NULL)
9224 {
9225 child_die = die->child;
9226 while (child_die && child_die->tag)
9227 {
9228 process_die (child_die, cu);
9229 child_die = sibling_die (child_die);
9230 }
9231 }
9232
9233 /* Decode macro information, if present. Dwarf 2 macro information
9234 refers to information in the line number info statement program
9235 header, so we can only read it if we've read the header
9236 successfully. */
9237 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9238 if (attr && cu->line_header)
9239 {
9240 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9241 complaint (&symfile_complaints,
9242 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9243
9244 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9245 }
9246 else
9247 {
9248 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9249 if (attr && cu->line_header)
9250 {
9251 unsigned int macro_offset = DW_UNSND (attr);
9252
9253 dwarf_decode_macros (cu, macro_offset, 0);
9254 }
9255 }
9256
9257 do_cleanups (back_to);
9258 }
9259
9260 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9261 Create the set of symtabs used by this TU, or if this TU is sharing
9262 symtabs with another TU and the symtabs have already been created
9263 then restore those symtabs in the line header.
9264 We don't need the pc/line-number mapping for type units. */
9265
9266 static void
9267 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9268 {
9269 struct objfile *objfile = dwarf2_per_objfile->objfile;
9270 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9271 struct type_unit_group *tu_group;
9272 int first_time;
9273 struct line_header *lh;
9274 struct attribute *attr;
9275 unsigned int i, line_offset;
9276 struct signatured_type *sig_type;
9277
9278 gdb_assert (per_cu->is_debug_types);
9279 sig_type = (struct signatured_type *) per_cu;
9280
9281 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9282
9283 /* If we're using .gdb_index (includes -readnow) then
9284 per_cu->type_unit_group may not have been set up yet. */
9285 if (sig_type->type_unit_group == NULL)
9286 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9287 tu_group = sig_type->type_unit_group;
9288
9289 /* If we've already processed this stmt_list there's no real need to
9290 do it again, we could fake it and just recreate the part we need
9291 (file name,index -> symtab mapping). If data shows this optimization
9292 is useful we can do it then. */
9293 first_time = tu_group->compunit_symtab == NULL;
9294
9295 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9296 debug info. */
9297 lh = NULL;
9298 if (attr != NULL)
9299 {
9300 line_offset = DW_UNSND (attr);
9301 lh = dwarf_decode_line_header (line_offset, cu);
9302 }
9303 if (lh == NULL)
9304 {
9305 if (first_time)
9306 dwarf2_start_symtab (cu, "", NULL, 0);
9307 else
9308 {
9309 gdb_assert (tu_group->symtabs == NULL);
9310 restart_symtab (tu_group->compunit_symtab, "", 0);
9311 }
9312 return;
9313 }
9314
9315 cu->line_header = lh;
9316 make_cleanup (free_cu_line_header, cu);
9317
9318 if (first_time)
9319 {
9320 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9321
9322 tu_group->num_symtabs = lh->num_file_names;
9323 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9324
9325 for (i = 0; i < lh->num_file_names; ++i)
9326 {
9327 const char *dir = NULL;
9328 struct file_entry *fe = &lh->file_names[i];
9329
9330 if (fe->dir_index && lh->include_dirs != NULL)
9331 dir = lh->include_dirs[fe->dir_index - 1];
9332 dwarf2_start_subfile (fe->name, dir);
9333
9334 if (current_subfile->symtab == NULL)
9335 {
9336 /* NOTE: start_subfile will recognize when it's been passed
9337 a file it has already seen. So we can't assume there's a
9338 simple mapping from lh->file_names to subfiles, plus
9339 lh->file_names may contain dups. */
9340 current_subfile->symtab
9341 = allocate_symtab (cust, current_subfile->name);
9342 }
9343
9344 fe->symtab = current_subfile->symtab;
9345 tu_group->symtabs[i] = fe->symtab;
9346 }
9347 }
9348 else
9349 {
9350 restart_symtab (tu_group->compunit_symtab, "", 0);
9351
9352 for (i = 0; i < lh->num_file_names; ++i)
9353 {
9354 struct file_entry *fe = &lh->file_names[i];
9355
9356 fe->symtab = tu_group->symtabs[i];
9357 }
9358 }
9359
9360 /* The main symtab is allocated last. Type units don't have DW_AT_name
9361 so they don't have a "real" (so to speak) symtab anyway.
9362 There is later code that will assign the main symtab to all symbols
9363 that don't have one. We need to handle the case of a symbol with a
9364 missing symtab (DW_AT_decl_file) anyway. */
9365 }
9366
9367 /* Process DW_TAG_type_unit.
9368 For TUs we want to skip the first top level sibling if it's not the
9369 actual type being defined by this TU. In this case the first top
9370 level sibling is there to provide context only. */
9371
9372 static void
9373 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9374 {
9375 struct die_info *child_die;
9376
9377 prepare_one_comp_unit (cu, die, language_minimal);
9378
9379 /* Initialize (or reinitialize) the machinery for building symtabs.
9380 We do this before processing child DIEs, so that the line header table
9381 is available for DW_AT_decl_file. */
9382 setup_type_unit_groups (die, cu);
9383
9384 if (die->child != NULL)
9385 {
9386 child_die = die->child;
9387 while (child_die && child_die->tag)
9388 {
9389 process_die (child_die, cu);
9390 child_die = sibling_die (child_die);
9391 }
9392 }
9393 }
9394 \f
9395 /* DWO/DWP files.
9396
9397 http://gcc.gnu.org/wiki/DebugFission
9398 http://gcc.gnu.org/wiki/DebugFissionDWP
9399
9400 To simplify handling of both DWO files ("object" files with the DWARF info)
9401 and DWP files (a file with the DWOs packaged up into one file), we treat
9402 DWP files as having a collection of virtual DWO files. */
9403
9404 static hashval_t
9405 hash_dwo_file (const void *item)
9406 {
9407 const struct dwo_file *dwo_file = item;
9408 hashval_t hash;
9409
9410 hash = htab_hash_string (dwo_file->dwo_name);
9411 if (dwo_file->comp_dir != NULL)
9412 hash += htab_hash_string (dwo_file->comp_dir);
9413 return hash;
9414 }
9415
9416 static int
9417 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9418 {
9419 const struct dwo_file *lhs = item_lhs;
9420 const struct dwo_file *rhs = item_rhs;
9421
9422 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9423 return 0;
9424 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9425 return lhs->comp_dir == rhs->comp_dir;
9426 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9427 }
9428
9429 /* Allocate a hash table for DWO files. */
9430
9431 static htab_t
9432 allocate_dwo_file_hash_table (void)
9433 {
9434 struct objfile *objfile = dwarf2_per_objfile->objfile;
9435
9436 return htab_create_alloc_ex (41,
9437 hash_dwo_file,
9438 eq_dwo_file,
9439 NULL,
9440 &objfile->objfile_obstack,
9441 hashtab_obstack_allocate,
9442 dummy_obstack_deallocate);
9443 }
9444
9445 /* Lookup DWO file DWO_NAME. */
9446
9447 static void **
9448 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9449 {
9450 struct dwo_file find_entry;
9451 void **slot;
9452
9453 if (dwarf2_per_objfile->dwo_files == NULL)
9454 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9455
9456 memset (&find_entry, 0, sizeof (find_entry));
9457 find_entry.dwo_name = dwo_name;
9458 find_entry.comp_dir = comp_dir;
9459 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9460
9461 return slot;
9462 }
9463
9464 static hashval_t
9465 hash_dwo_unit (const void *item)
9466 {
9467 const struct dwo_unit *dwo_unit = item;
9468
9469 /* This drops the top 32 bits of the id, but is ok for a hash. */
9470 return dwo_unit->signature;
9471 }
9472
9473 static int
9474 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9475 {
9476 const struct dwo_unit *lhs = item_lhs;
9477 const struct dwo_unit *rhs = item_rhs;
9478
9479 /* The signature is assumed to be unique within the DWO file.
9480 So while object file CU dwo_id's always have the value zero,
9481 that's OK, assuming each object file DWO file has only one CU,
9482 and that's the rule for now. */
9483 return lhs->signature == rhs->signature;
9484 }
9485
9486 /* Allocate a hash table for DWO CUs,TUs.
9487 There is one of these tables for each of CUs,TUs for each DWO file. */
9488
9489 static htab_t
9490 allocate_dwo_unit_table (struct objfile *objfile)
9491 {
9492 /* Start out with a pretty small number.
9493 Generally DWO files contain only one CU and maybe some TUs. */
9494 return htab_create_alloc_ex (3,
9495 hash_dwo_unit,
9496 eq_dwo_unit,
9497 NULL,
9498 &objfile->objfile_obstack,
9499 hashtab_obstack_allocate,
9500 dummy_obstack_deallocate);
9501 }
9502
9503 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9504
9505 struct create_dwo_cu_data
9506 {
9507 struct dwo_file *dwo_file;
9508 struct dwo_unit dwo_unit;
9509 };
9510
9511 /* die_reader_func for create_dwo_cu. */
9512
9513 static void
9514 create_dwo_cu_reader (const struct die_reader_specs *reader,
9515 const gdb_byte *info_ptr,
9516 struct die_info *comp_unit_die,
9517 int has_children,
9518 void *datap)
9519 {
9520 struct dwarf2_cu *cu = reader->cu;
9521 struct objfile *objfile = dwarf2_per_objfile->objfile;
9522 sect_offset offset = cu->per_cu->offset;
9523 struct dwarf2_section_info *section = cu->per_cu->section;
9524 struct create_dwo_cu_data *data = datap;
9525 struct dwo_file *dwo_file = data->dwo_file;
9526 struct dwo_unit *dwo_unit = &data->dwo_unit;
9527 struct attribute *attr;
9528
9529 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9530 if (attr == NULL)
9531 {
9532 complaint (&symfile_complaints,
9533 _("Dwarf Error: debug entry at offset 0x%x is missing"
9534 " its dwo_id [in module %s]"),
9535 offset.sect_off, dwo_file->dwo_name);
9536 return;
9537 }
9538
9539 dwo_unit->dwo_file = dwo_file;
9540 dwo_unit->signature = DW_UNSND (attr);
9541 dwo_unit->section = section;
9542 dwo_unit->offset = offset;
9543 dwo_unit->length = cu->per_cu->length;
9544
9545 if (dwarf_read_debug)
9546 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9547 offset.sect_off, hex_string (dwo_unit->signature));
9548 }
9549
9550 /* Create the dwo_unit for the lone CU in DWO_FILE.
9551 Note: This function processes DWO files only, not DWP files. */
9552
9553 static struct dwo_unit *
9554 create_dwo_cu (struct dwo_file *dwo_file)
9555 {
9556 struct objfile *objfile = dwarf2_per_objfile->objfile;
9557 struct dwarf2_section_info *section = &dwo_file->sections.info;
9558 bfd *abfd;
9559 htab_t cu_htab;
9560 const gdb_byte *info_ptr, *end_ptr;
9561 struct create_dwo_cu_data create_dwo_cu_data;
9562 struct dwo_unit *dwo_unit;
9563
9564 dwarf2_read_section (objfile, section);
9565 info_ptr = section->buffer;
9566
9567 if (info_ptr == NULL)
9568 return NULL;
9569
9570 /* We can't set abfd until now because the section may be empty or
9571 not present, in which case section->asection will be NULL. */
9572 abfd = get_section_bfd_owner (section);
9573
9574 if (dwarf_read_debug)
9575 {
9576 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9577 get_section_name (section),
9578 get_section_file_name (section));
9579 }
9580
9581 create_dwo_cu_data.dwo_file = dwo_file;
9582 dwo_unit = NULL;
9583
9584 end_ptr = info_ptr + section->size;
9585 while (info_ptr < end_ptr)
9586 {
9587 struct dwarf2_per_cu_data per_cu;
9588
9589 memset (&create_dwo_cu_data.dwo_unit, 0,
9590 sizeof (create_dwo_cu_data.dwo_unit));
9591 memset (&per_cu, 0, sizeof (per_cu));
9592 per_cu.objfile = objfile;
9593 per_cu.is_debug_types = 0;
9594 per_cu.offset.sect_off = info_ptr - section->buffer;
9595 per_cu.section = section;
9596
9597 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9598 create_dwo_cu_reader,
9599 &create_dwo_cu_data);
9600
9601 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9602 {
9603 /* If we've already found one, complain. We only support one
9604 because having more than one requires hacking the dwo_name of
9605 each to match, which is highly unlikely to happen. */
9606 if (dwo_unit != NULL)
9607 {
9608 complaint (&symfile_complaints,
9609 _("Multiple CUs in DWO file %s [in module %s]"),
9610 dwo_file->dwo_name, objfile_name (objfile));
9611 break;
9612 }
9613
9614 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9615 *dwo_unit = create_dwo_cu_data.dwo_unit;
9616 }
9617
9618 info_ptr += per_cu.length;
9619 }
9620
9621 return dwo_unit;
9622 }
9623
9624 /* DWP file .debug_{cu,tu}_index section format:
9625 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9626
9627 DWP Version 1:
9628
9629 Both index sections have the same format, and serve to map a 64-bit
9630 signature to a set of section numbers. Each section begins with a header,
9631 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9632 indexes, and a pool of 32-bit section numbers. The index sections will be
9633 aligned at 8-byte boundaries in the file.
9634
9635 The index section header consists of:
9636
9637 V, 32 bit version number
9638 -, 32 bits unused
9639 N, 32 bit number of compilation units or type units in the index
9640 M, 32 bit number of slots in the hash table
9641
9642 Numbers are recorded using the byte order of the application binary.
9643
9644 The hash table begins at offset 16 in the section, and consists of an array
9645 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9646 order of the application binary). Unused slots in the hash table are 0.
9647 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9648
9649 The parallel table begins immediately after the hash table
9650 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9651 array of 32-bit indexes (using the byte order of the application binary),
9652 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9653 table contains a 32-bit index into the pool of section numbers. For unused
9654 hash table slots, the corresponding entry in the parallel table will be 0.
9655
9656 The pool of section numbers begins immediately following the hash table
9657 (at offset 16 + 12 * M from the beginning of the section). The pool of
9658 section numbers consists of an array of 32-bit words (using the byte order
9659 of the application binary). Each item in the array is indexed starting
9660 from 0. The hash table entry provides the index of the first section
9661 number in the set. Additional section numbers in the set follow, and the
9662 set is terminated by a 0 entry (section number 0 is not used in ELF).
9663
9664 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9665 section must be the first entry in the set, and the .debug_abbrev.dwo must
9666 be the second entry. Other members of the set may follow in any order.
9667
9668 ---
9669
9670 DWP Version 2:
9671
9672 DWP Version 2 combines all the .debug_info, etc. sections into one,
9673 and the entries in the index tables are now offsets into these sections.
9674 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9675 section.
9676
9677 Index Section Contents:
9678 Header
9679 Hash Table of Signatures dwp_hash_table.hash_table
9680 Parallel Table of Indices dwp_hash_table.unit_table
9681 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9682 Table of Section Sizes dwp_hash_table.v2.sizes
9683
9684 The index section header consists of:
9685
9686 V, 32 bit version number
9687 L, 32 bit number of columns in the table of section offsets
9688 N, 32 bit number of compilation units or type units in the index
9689 M, 32 bit number of slots in the hash table
9690
9691 Numbers are recorded using the byte order of the application binary.
9692
9693 The hash table has the same format as version 1.
9694 The parallel table of indices has the same format as version 1,
9695 except that the entries are origin-1 indices into the table of sections
9696 offsets and the table of section sizes.
9697
9698 The table of offsets begins immediately following the parallel table
9699 (at offset 16 + 12 * M from the beginning of the section). The table is
9700 a two-dimensional array of 32-bit words (using the byte order of the
9701 application binary), with L columns and N+1 rows, in row-major order.
9702 Each row in the array is indexed starting from 0. The first row provides
9703 a key to the remaining rows: each column in this row provides an identifier
9704 for a debug section, and the offsets in the same column of subsequent rows
9705 refer to that section. The section identifiers are:
9706
9707 DW_SECT_INFO 1 .debug_info.dwo
9708 DW_SECT_TYPES 2 .debug_types.dwo
9709 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9710 DW_SECT_LINE 4 .debug_line.dwo
9711 DW_SECT_LOC 5 .debug_loc.dwo
9712 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9713 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9714 DW_SECT_MACRO 8 .debug_macro.dwo
9715
9716 The offsets provided by the CU and TU index sections are the base offsets
9717 for the contributions made by each CU or TU to the corresponding section
9718 in the package file. Each CU and TU header contains an abbrev_offset
9719 field, used to find the abbreviations table for that CU or TU within the
9720 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9721 be interpreted as relative to the base offset given in the index section.
9722 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9723 should be interpreted as relative to the base offset for .debug_line.dwo,
9724 and offsets into other debug sections obtained from DWARF attributes should
9725 also be interpreted as relative to the corresponding base offset.
9726
9727 The table of sizes begins immediately following the table of offsets.
9728 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9729 with L columns and N rows, in row-major order. Each row in the array is
9730 indexed starting from 1 (row 0 is shared by the two tables).
9731
9732 ---
9733
9734 Hash table lookup is handled the same in version 1 and 2:
9735
9736 We assume that N and M will not exceed 2^32 - 1.
9737 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9738
9739 Given a 64-bit compilation unit signature or a type signature S, an entry
9740 in the hash table is located as follows:
9741
9742 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9743 the low-order k bits all set to 1.
9744
9745 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9746
9747 3) If the hash table entry at index H matches the signature, use that
9748 entry. If the hash table entry at index H is unused (all zeroes),
9749 terminate the search: the signature is not present in the table.
9750
9751 4) Let H = (H + H') modulo M. Repeat at Step 3.
9752
9753 Because M > N and H' and M are relatively prime, the search is guaranteed
9754 to stop at an unused slot or find the match. */
9755
9756 /* Create a hash table to map DWO IDs to their CU/TU entry in
9757 .debug_{info,types}.dwo in DWP_FILE.
9758 Returns NULL if there isn't one.
9759 Note: This function processes DWP files only, not DWO files. */
9760
9761 static struct dwp_hash_table *
9762 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9763 {
9764 struct objfile *objfile = dwarf2_per_objfile->objfile;
9765 bfd *dbfd = dwp_file->dbfd;
9766 const gdb_byte *index_ptr, *index_end;
9767 struct dwarf2_section_info *index;
9768 uint32_t version, nr_columns, nr_units, nr_slots;
9769 struct dwp_hash_table *htab;
9770
9771 if (is_debug_types)
9772 index = &dwp_file->sections.tu_index;
9773 else
9774 index = &dwp_file->sections.cu_index;
9775
9776 if (dwarf2_section_empty_p (index))
9777 return NULL;
9778 dwarf2_read_section (objfile, index);
9779
9780 index_ptr = index->buffer;
9781 index_end = index_ptr + index->size;
9782
9783 version = read_4_bytes (dbfd, index_ptr);
9784 index_ptr += 4;
9785 if (version == 2)
9786 nr_columns = read_4_bytes (dbfd, index_ptr);
9787 else
9788 nr_columns = 0;
9789 index_ptr += 4;
9790 nr_units = read_4_bytes (dbfd, index_ptr);
9791 index_ptr += 4;
9792 nr_slots = read_4_bytes (dbfd, index_ptr);
9793 index_ptr += 4;
9794
9795 if (version != 1 && version != 2)
9796 {
9797 error (_("Dwarf Error: unsupported DWP file version (%s)"
9798 " [in module %s]"),
9799 pulongest (version), dwp_file->name);
9800 }
9801 if (nr_slots != (nr_slots & -nr_slots))
9802 {
9803 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9804 " is not power of 2 [in module %s]"),
9805 pulongest (nr_slots), dwp_file->name);
9806 }
9807
9808 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9809 htab->version = version;
9810 htab->nr_columns = nr_columns;
9811 htab->nr_units = nr_units;
9812 htab->nr_slots = nr_slots;
9813 htab->hash_table = index_ptr;
9814 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9815
9816 /* Exit early if the table is empty. */
9817 if (nr_slots == 0 || nr_units == 0
9818 || (version == 2 && nr_columns == 0))
9819 {
9820 /* All must be zero. */
9821 if (nr_slots != 0 || nr_units != 0
9822 || (version == 2 && nr_columns != 0))
9823 {
9824 complaint (&symfile_complaints,
9825 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9826 " all zero [in modules %s]"),
9827 dwp_file->name);
9828 }
9829 return htab;
9830 }
9831
9832 if (version == 1)
9833 {
9834 htab->section_pool.v1.indices =
9835 htab->unit_table + sizeof (uint32_t) * nr_slots;
9836 /* It's harder to decide whether the section is too small in v1.
9837 V1 is deprecated anyway so we punt. */
9838 }
9839 else
9840 {
9841 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9842 int *ids = htab->section_pool.v2.section_ids;
9843 /* Reverse map for error checking. */
9844 int ids_seen[DW_SECT_MAX + 1];
9845 int i;
9846
9847 if (nr_columns < 2)
9848 {
9849 error (_("Dwarf Error: bad DWP hash table, too few columns"
9850 " in section table [in module %s]"),
9851 dwp_file->name);
9852 }
9853 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9854 {
9855 error (_("Dwarf Error: bad DWP hash table, too many columns"
9856 " in section table [in module %s]"),
9857 dwp_file->name);
9858 }
9859 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9860 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9861 for (i = 0; i < nr_columns; ++i)
9862 {
9863 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9864
9865 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9866 {
9867 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9868 " in section table [in module %s]"),
9869 id, dwp_file->name);
9870 }
9871 if (ids_seen[id] != -1)
9872 {
9873 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9874 " id %d in section table [in module %s]"),
9875 id, dwp_file->name);
9876 }
9877 ids_seen[id] = i;
9878 ids[i] = id;
9879 }
9880 /* Must have exactly one info or types section. */
9881 if (((ids_seen[DW_SECT_INFO] != -1)
9882 + (ids_seen[DW_SECT_TYPES] != -1))
9883 != 1)
9884 {
9885 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9886 " DWO info/types section [in module %s]"),
9887 dwp_file->name);
9888 }
9889 /* Must have an abbrev section. */
9890 if (ids_seen[DW_SECT_ABBREV] == -1)
9891 {
9892 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9893 " section [in module %s]"),
9894 dwp_file->name);
9895 }
9896 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9897 htab->section_pool.v2.sizes =
9898 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9899 * nr_units * nr_columns);
9900 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9901 * nr_units * nr_columns))
9902 > index_end)
9903 {
9904 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9905 " [in module %s]"),
9906 dwp_file->name);
9907 }
9908 }
9909
9910 return htab;
9911 }
9912
9913 /* Update SECTIONS with the data from SECTP.
9914
9915 This function is like the other "locate" section routines that are
9916 passed to bfd_map_over_sections, but in this context the sections to
9917 read comes from the DWP V1 hash table, not the full ELF section table.
9918
9919 The result is non-zero for success, or zero if an error was found. */
9920
9921 static int
9922 locate_v1_virtual_dwo_sections (asection *sectp,
9923 struct virtual_v1_dwo_sections *sections)
9924 {
9925 const struct dwop_section_names *names = &dwop_section_names;
9926
9927 if (section_is_p (sectp->name, &names->abbrev_dwo))
9928 {
9929 /* There can be only one. */
9930 if (sections->abbrev.s.section != NULL)
9931 return 0;
9932 sections->abbrev.s.section = sectp;
9933 sections->abbrev.size = bfd_get_section_size (sectp);
9934 }
9935 else if (section_is_p (sectp->name, &names->info_dwo)
9936 || section_is_p (sectp->name, &names->types_dwo))
9937 {
9938 /* There can be only one. */
9939 if (sections->info_or_types.s.section != NULL)
9940 return 0;
9941 sections->info_or_types.s.section = sectp;
9942 sections->info_or_types.size = bfd_get_section_size (sectp);
9943 }
9944 else if (section_is_p (sectp->name, &names->line_dwo))
9945 {
9946 /* There can be only one. */
9947 if (sections->line.s.section != NULL)
9948 return 0;
9949 sections->line.s.section = sectp;
9950 sections->line.size = bfd_get_section_size (sectp);
9951 }
9952 else if (section_is_p (sectp->name, &names->loc_dwo))
9953 {
9954 /* There can be only one. */
9955 if (sections->loc.s.section != NULL)
9956 return 0;
9957 sections->loc.s.section = sectp;
9958 sections->loc.size = bfd_get_section_size (sectp);
9959 }
9960 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9961 {
9962 /* There can be only one. */
9963 if (sections->macinfo.s.section != NULL)
9964 return 0;
9965 sections->macinfo.s.section = sectp;
9966 sections->macinfo.size = bfd_get_section_size (sectp);
9967 }
9968 else if (section_is_p (sectp->name, &names->macro_dwo))
9969 {
9970 /* There can be only one. */
9971 if (sections->macro.s.section != NULL)
9972 return 0;
9973 sections->macro.s.section = sectp;
9974 sections->macro.size = bfd_get_section_size (sectp);
9975 }
9976 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9977 {
9978 /* There can be only one. */
9979 if (sections->str_offsets.s.section != NULL)
9980 return 0;
9981 sections->str_offsets.s.section = sectp;
9982 sections->str_offsets.size = bfd_get_section_size (sectp);
9983 }
9984 else
9985 {
9986 /* No other kind of section is valid. */
9987 return 0;
9988 }
9989
9990 return 1;
9991 }
9992
9993 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9994 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9995 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9996 This is for DWP version 1 files. */
9997
9998 static struct dwo_unit *
9999 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10000 uint32_t unit_index,
10001 const char *comp_dir,
10002 ULONGEST signature, int is_debug_types)
10003 {
10004 struct objfile *objfile = dwarf2_per_objfile->objfile;
10005 const struct dwp_hash_table *dwp_htab =
10006 is_debug_types ? dwp_file->tus : dwp_file->cus;
10007 bfd *dbfd = dwp_file->dbfd;
10008 const char *kind = is_debug_types ? "TU" : "CU";
10009 struct dwo_file *dwo_file;
10010 struct dwo_unit *dwo_unit;
10011 struct virtual_v1_dwo_sections sections;
10012 void **dwo_file_slot;
10013 char *virtual_dwo_name;
10014 struct dwarf2_section_info *cutu;
10015 struct cleanup *cleanups;
10016 int i;
10017
10018 gdb_assert (dwp_file->version == 1);
10019
10020 if (dwarf_read_debug)
10021 {
10022 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10023 kind,
10024 pulongest (unit_index), hex_string (signature),
10025 dwp_file->name);
10026 }
10027
10028 /* Fetch the sections of this DWO unit.
10029 Put a limit on the number of sections we look for so that bad data
10030 doesn't cause us to loop forever. */
10031
10032 #define MAX_NR_V1_DWO_SECTIONS \
10033 (1 /* .debug_info or .debug_types */ \
10034 + 1 /* .debug_abbrev */ \
10035 + 1 /* .debug_line */ \
10036 + 1 /* .debug_loc */ \
10037 + 1 /* .debug_str_offsets */ \
10038 + 1 /* .debug_macro or .debug_macinfo */ \
10039 + 1 /* trailing zero */)
10040
10041 memset (&sections, 0, sizeof (sections));
10042 cleanups = make_cleanup (null_cleanup, 0);
10043
10044 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10045 {
10046 asection *sectp;
10047 uint32_t section_nr =
10048 read_4_bytes (dbfd,
10049 dwp_htab->section_pool.v1.indices
10050 + (unit_index + i) * sizeof (uint32_t));
10051
10052 if (section_nr == 0)
10053 break;
10054 if (section_nr >= dwp_file->num_sections)
10055 {
10056 error (_("Dwarf Error: bad DWP hash table, section number too large"
10057 " [in module %s]"),
10058 dwp_file->name);
10059 }
10060
10061 sectp = dwp_file->elf_sections[section_nr];
10062 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10063 {
10064 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10065 " [in module %s]"),
10066 dwp_file->name);
10067 }
10068 }
10069
10070 if (i < 2
10071 || dwarf2_section_empty_p (&sections.info_or_types)
10072 || dwarf2_section_empty_p (&sections.abbrev))
10073 {
10074 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10075 " [in module %s]"),
10076 dwp_file->name);
10077 }
10078 if (i == MAX_NR_V1_DWO_SECTIONS)
10079 {
10080 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10081 " [in module %s]"),
10082 dwp_file->name);
10083 }
10084
10085 /* It's easier for the rest of the code if we fake a struct dwo_file and
10086 have dwo_unit "live" in that. At least for now.
10087
10088 The DWP file can be made up of a random collection of CUs and TUs.
10089 However, for each CU + set of TUs that came from the same original DWO
10090 file, we can combine them back into a virtual DWO file to save space
10091 (fewer struct dwo_file objects to allocate). Remember that for really
10092 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10093
10094 virtual_dwo_name =
10095 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10096 get_section_id (&sections.abbrev),
10097 get_section_id (&sections.line),
10098 get_section_id (&sections.loc),
10099 get_section_id (&sections.str_offsets));
10100 make_cleanup (xfree, virtual_dwo_name);
10101 /* Can we use an existing virtual DWO file? */
10102 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10103 /* Create one if necessary. */
10104 if (*dwo_file_slot == NULL)
10105 {
10106 if (dwarf_read_debug)
10107 {
10108 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10109 virtual_dwo_name);
10110 }
10111 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10112 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
10113 virtual_dwo_name,
10114 strlen (virtual_dwo_name));
10115 dwo_file->comp_dir = comp_dir;
10116 dwo_file->sections.abbrev = sections.abbrev;
10117 dwo_file->sections.line = sections.line;
10118 dwo_file->sections.loc = sections.loc;
10119 dwo_file->sections.macinfo = sections.macinfo;
10120 dwo_file->sections.macro = sections.macro;
10121 dwo_file->sections.str_offsets = sections.str_offsets;
10122 /* The "str" section is global to the entire DWP file. */
10123 dwo_file->sections.str = dwp_file->sections.str;
10124 /* The info or types section is assigned below to dwo_unit,
10125 there's no need to record it in dwo_file.
10126 Also, we can't simply record type sections in dwo_file because
10127 we record a pointer into the vector in dwo_unit. As we collect more
10128 types we'll grow the vector and eventually have to reallocate space
10129 for it, invalidating all copies of pointers into the previous
10130 contents. */
10131 *dwo_file_slot = dwo_file;
10132 }
10133 else
10134 {
10135 if (dwarf_read_debug)
10136 {
10137 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10138 virtual_dwo_name);
10139 }
10140 dwo_file = *dwo_file_slot;
10141 }
10142 do_cleanups (cleanups);
10143
10144 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10145 dwo_unit->dwo_file = dwo_file;
10146 dwo_unit->signature = signature;
10147 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
10148 sizeof (struct dwarf2_section_info));
10149 *dwo_unit->section = sections.info_or_types;
10150 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10151
10152 return dwo_unit;
10153 }
10154
10155 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10156 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10157 piece within that section used by a TU/CU, return a virtual section
10158 of just that piece. */
10159
10160 static struct dwarf2_section_info
10161 create_dwp_v2_section (struct dwarf2_section_info *section,
10162 bfd_size_type offset, bfd_size_type size)
10163 {
10164 struct dwarf2_section_info result;
10165 asection *sectp;
10166
10167 gdb_assert (section != NULL);
10168 gdb_assert (!section->is_virtual);
10169
10170 memset (&result, 0, sizeof (result));
10171 result.s.containing_section = section;
10172 result.is_virtual = 1;
10173
10174 if (size == 0)
10175 return result;
10176
10177 sectp = get_section_bfd_section (section);
10178
10179 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10180 bounds of the real section. This is a pretty-rare event, so just
10181 flag an error (easier) instead of a warning and trying to cope. */
10182 if (sectp == NULL
10183 || offset + size > bfd_get_section_size (sectp))
10184 {
10185 bfd *abfd = sectp->owner;
10186
10187 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10188 " in section %s [in module %s]"),
10189 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10190 objfile_name (dwarf2_per_objfile->objfile));
10191 }
10192
10193 result.virtual_offset = offset;
10194 result.size = size;
10195 return result;
10196 }
10197
10198 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10199 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10200 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10201 This is for DWP version 2 files. */
10202
10203 static struct dwo_unit *
10204 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10205 uint32_t unit_index,
10206 const char *comp_dir,
10207 ULONGEST signature, int is_debug_types)
10208 {
10209 struct objfile *objfile = dwarf2_per_objfile->objfile;
10210 const struct dwp_hash_table *dwp_htab =
10211 is_debug_types ? dwp_file->tus : dwp_file->cus;
10212 bfd *dbfd = dwp_file->dbfd;
10213 const char *kind = is_debug_types ? "TU" : "CU";
10214 struct dwo_file *dwo_file;
10215 struct dwo_unit *dwo_unit;
10216 struct virtual_v2_dwo_sections sections;
10217 void **dwo_file_slot;
10218 char *virtual_dwo_name;
10219 struct dwarf2_section_info *cutu;
10220 struct cleanup *cleanups;
10221 int i;
10222
10223 gdb_assert (dwp_file->version == 2);
10224
10225 if (dwarf_read_debug)
10226 {
10227 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10228 kind,
10229 pulongest (unit_index), hex_string (signature),
10230 dwp_file->name);
10231 }
10232
10233 /* Fetch the section offsets of this DWO unit. */
10234
10235 memset (&sections, 0, sizeof (sections));
10236 cleanups = make_cleanup (null_cleanup, 0);
10237
10238 for (i = 0; i < dwp_htab->nr_columns; ++i)
10239 {
10240 uint32_t offset = read_4_bytes (dbfd,
10241 dwp_htab->section_pool.v2.offsets
10242 + (((unit_index - 1) * dwp_htab->nr_columns
10243 + i)
10244 * sizeof (uint32_t)));
10245 uint32_t size = read_4_bytes (dbfd,
10246 dwp_htab->section_pool.v2.sizes
10247 + (((unit_index - 1) * dwp_htab->nr_columns
10248 + i)
10249 * sizeof (uint32_t)));
10250
10251 switch (dwp_htab->section_pool.v2.section_ids[i])
10252 {
10253 case DW_SECT_INFO:
10254 case DW_SECT_TYPES:
10255 sections.info_or_types_offset = offset;
10256 sections.info_or_types_size = size;
10257 break;
10258 case DW_SECT_ABBREV:
10259 sections.abbrev_offset = offset;
10260 sections.abbrev_size = size;
10261 break;
10262 case DW_SECT_LINE:
10263 sections.line_offset = offset;
10264 sections.line_size = size;
10265 break;
10266 case DW_SECT_LOC:
10267 sections.loc_offset = offset;
10268 sections.loc_size = size;
10269 break;
10270 case DW_SECT_STR_OFFSETS:
10271 sections.str_offsets_offset = offset;
10272 sections.str_offsets_size = size;
10273 break;
10274 case DW_SECT_MACINFO:
10275 sections.macinfo_offset = offset;
10276 sections.macinfo_size = size;
10277 break;
10278 case DW_SECT_MACRO:
10279 sections.macro_offset = offset;
10280 sections.macro_size = size;
10281 break;
10282 }
10283 }
10284
10285 /* It's easier for the rest of the code if we fake a struct dwo_file and
10286 have dwo_unit "live" in that. At least for now.
10287
10288 The DWP file can be made up of a random collection of CUs and TUs.
10289 However, for each CU + set of TUs that came from the same original DWO
10290 file, we can combine them back into a virtual DWO file to save space
10291 (fewer struct dwo_file objects to allocate). Remember that for really
10292 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10293
10294 virtual_dwo_name =
10295 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10296 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10297 (long) (sections.line_size ? sections.line_offset : 0),
10298 (long) (sections.loc_size ? sections.loc_offset : 0),
10299 (long) (sections.str_offsets_size
10300 ? sections.str_offsets_offset : 0));
10301 make_cleanup (xfree, virtual_dwo_name);
10302 /* Can we use an existing virtual DWO file? */
10303 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10304 /* Create one if necessary. */
10305 if (*dwo_file_slot == NULL)
10306 {
10307 if (dwarf_read_debug)
10308 {
10309 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10310 virtual_dwo_name);
10311 }
10312 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10313 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
10314 virtual_dwo_name,
10315 strlen (virtual_dwo_name));
10316 dwo_file->comp_dir = comp_dir;
10317 dwo_file->sections.abbrev =
10318 create_dwp_v2_section (&dwp_file->sections.abbrev,
10319 sections.abbrev_offset, sections.abbrev_size);
10320 dwo_file->sections.line =
10321 create_dwp_v2_section (&dwp_file->sections.line,
10322 sections.line_offset, sections.line_size);
10323 dwo_file->sections.loc =
10324 create_dwp_v2_section (&dwp_file->sections.loc,
10325 sections.loc_offset, sections.loc_size);
10326 dwo_file->sections.macinfo =
10327 create_dwp_v2_section (&dwp_file->sections.macinfo,
10328 sections.macinfo_offset, sections.macinfo_size);
10329 dwo_file->sections.macro =
10330 create_dwp_v2_section (&dwp_file->sections.macro,
10331 sections.macro_offset, sections.macro_size);
10332 dwo_file->sections.str_offsets =
10333 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10334 sections.str_offsets_offset,
10335 sections.str_offsets_size);
10336 /* The "str" section is global to the entire DWP file. */
10337 dwo_file->sections.str = dwp_file->sections.str;
10338 /* The info or types section is assigned below to dwo_unit,
10339 there's no need to record it in dwo_file.
10340 Also, we can't simply record type sections in dwo_file because
10341 we record a pointer into the vector in dwo_unit. As we collect more
10342 types we'll grow the vector and eventually have to reallocate space
10343 for it, invalidating all copies of pointers into the previous
10344 contents. */
10345 *dwo_file_slot = dwo_file;
10346 }
10347 else
10348 {
10349 if (dwarf_read_debug)
10350 {
10351 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10352 virtual_dwo_name);
10353 }
10354 dwo_file = *dwo_file_slot;
10355 }
10356 do_cleanups (cleanups);
10357
10358 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10359 dwo_unit->dwo_file = dwo_file;
10360 dwo_unit->signature = signature;
10361 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
10362 sizeof (struct dwarf2_section_info));
10363 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10364 ? &dwp_file->sections.types
10365 : &dwp_file->sections.info,
10366 sections.info_or_types_offset,
10367 sections.info_or_types_size);
10368 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10369
10370 return dwo_unit;
10371 }
10372
10373 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10374 Returns NULL if the signature isn't found. */
10375
10376 static struct dwo_unit *
10377 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10378 ULONGEST signature, int is_debug_types)
10379 {
10380 const struct dwp_hash_table *dwp_htab =
10381 is_debug_types ? dwp_file->tus : dwp_file->cus;
10382 bfd *dbfd = dwp_file->dbfd;
10383 uint32_t mask = dwp_htab->nr_slots - 1;
10384 uint32_t hash = signature & mask;
10385 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10386 unsigned int i;
10387 void **slot;
10388 struct dwo_unit find_dwo_cu, *dwo_cu;
10389
10390 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10391 find_dwo_cu.signature = signature;
10392 slot = htab_find_slot (is_debug_types
10393 ? dwp_file->loaded_tus
10394 : dwp_file->loaded_cus,
10395 &find_dwo_cu, INSERT);
10396
10397 if (*slot != NULL)
10398 return *slot;
10399
10400 /* Use a for loop so that we don't loop forever on bad debug info. */
10401 for (i = 0; i < dwp_htab->nr_slots; ++i)
10402 {
10403 ULONGEST signature_in_table;
10404
10405 signature_in_table =
10406 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10407 if (signature_in_table == signature)
10408 {
10409 uint32_t unit_index =
10410 read_4_bytes (dbfd,
10411 dwp_htab->unit_table + hash * sizeof (uint32_t));
10412
10413 if (dwp_file->version == 1)
10414 {
10415 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10416 comp_dir, signature,
10417 is_debug_types);
10418 }
10419 else
10420 {
10421 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10422 comp_dir, signature,
10423 is_debug_types);
10424 }
10425 return *slot;
10426 }
10427 if (signature_in_table == 0)
10428 return NULL;
10429 hash = (hash + hash2) & mask;
10430 }
10431
10432 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10433 " [in module %s]"),
10434 dwp_file->name);
10435 }
10436
10437 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10438 Open the file specified by FILE_NAME and hand it off to BFD for
10439 preliminary analysis. Return a newly initialized bfd *, which
10440 includes a canonicalized copy of FILE_NAME.
10441 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10442 SEARCH_CWD is true if the current directory is to be searched.
10443 It will be searched before debug-file-directory.
10444 If successful, the file is added to the bfd include table of the
10445 objfile's bfd (see gdb_bfd_record_inclusion).
10446 If unable to find/open the file, return NULL.
10447 NOTE: This function is derived from symfile_bfd_open. */
10448
10449 static bfd *
10450 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10451 {
10452 bfd *sym_bfd;
10453 int desc, flags;
10454 char *absolute_name;
10455 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10456 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10457 to debug_file_directory. */
10458 char *search_path;
10459 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10460
10461 if (search_cwd)
10462 {
10463 if (*debug_file_directory != '\0')
10464 search_path = concat (".", dirname_separator_string,
10465 debug_file_directory, NULL);
10466 else
10467 search_path = xstrdup (".");
10468 }
10469 else
10470 search_path = xstrdup (debug_file_directory);
10471
10472 flags = OPF_RETURN_REALPATH;
10473 if (is_dwp)
10474 flags |= OPF_SEARCH_IN_PATH;
10475 desc = openp (search_path, flags, file_name,
10476 O_RDONLY | O_BINARY, &absolute_name);
10477 xfree (search_path);
10478 if (desc < 0)
10479 return NULL;
10480
10481 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
10482 xfree (absolute_name);
10483 if (sym_bfd == NULL)
10484 return NULL;
10485 bfd_set_cacheable (sym_bfd, 1);
10486
10487 if (!bfd_check_format (sym_bfd, bfd_object))
10488 {
10489 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
10490 return NULL;
10491 }
10492
10493 /* Success. Record the bfd as having been included by the objfile's bfd.
10494 This is important because things like demangled_names_hash lives in the
10495 objfile's per_bfd space and may have references to things like symbol
10496 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10497 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd);
10498
10499 return sym_bfd;
10500 }
10501
10502 /* Try to open DWO file FILE_NAME.
10503 COMP_DIR is the DW_AT_comp_dir attribute.
10504 The result is the bfd handle of the file.
10505 If there is a problem finding or opening the file, return NULL.
10506 Upon success, the canonicalized path of the file is stored in the bfd,
10507 same as symfile_bfd_open. */
10508
10509 static bfd *
10510 open_dwo_file (const char *file_name, const char *comp_dir)
10511 {
10512 bfd *abfd;
10513
10514 if (IS_ABSOLUTE_PATH (file_name))
10515 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10516
10517 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10518
10519 if (comp_dir != NULL)
10520 {
10521 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
10522
10523 /* NOTE: If comp_dir is a relative path, this will also try the
10524 search path, which seems useful. */
10525 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
10526 xfree (path_to_try);
10527 if (abfd != NULL)
10528 return abfd;
10529 }
10530
10531 /* That didn't work, try debug-file-directory, which, despite its name,
10532 is a list of paths. */
10533
10534 if (*debug_file_directory == '\0')
10535 return NULL;
10536
10537 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10538 }
10539
10540 /* This function is mapped across the sections and remembers the offset and
10541 size of each of the DWO debugging sections we are interested in. */
10542
10543 static void
10544 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10545 {
10546 struct dwo_sections *dwo_sections = dwo_sections_ptr;
10547 const struct dwop_section_names *names = &dwop_section_names;
10548
10549 if (section_is_p (sectp->name, &names->abbrev_dwo))
10550 {
10551 dwo_sections->abbrev.s.section = sectp;
10552 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10553 }
10554 else if (section_is_p (sectp->name, &names->info_dwo))
10555 {
10556 dwo_sections->info.s.section = sectp;
10557 dwo_sections->info.size = bfd_get_section_size (sectp);
10558 }
10559 else if (section_is_p (sectp->name, &names->line_dwo))
10560 {
10561 dwo_sections->line.s.section = sectp;
10562 dwo_sections->line.size = bfd_get_section_size (sectp);
10563 }
10564 else if (section_is_p (sectp->name, &names->loc_dwo))
10565 {
10566 dwo_sections->loc.s.section = sectp;
10567 dwo_sections->loc.size = bfd_get_section_size (sectp);
10568 }
10569 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10570 {
10571 dwo_sections->macinfo.s.section = sectp;
10572 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10573 }
10574 else if (section_is_p (sectp->name, &names->macro_dwo))
10575 {
10576 dwo_sections->macro.s.section = sectp;
10577 dwo_sections->macro.size = bfd_get_section_size (sectp);
10578 }
10579 else if (section_is_p (sectp->name, &names->str_dwo))
10580 {
10581 dwo_sections->str.s.section = sectp;
10582 dwo_sections->str.size = bfd_get_section_size (sectp);
10583 }
10584 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10585 {
10586 dwo_sections->str_offsets.s.section = sectp;
10587 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10588 }
10589 else if (section_is_p (sectp->name, &names->types_dwo))
10590 {
10591 struct dwarf2_section_info type_section;
10592
10593 memset (&type_section, 0, sizeof (type_section));
10594 type_section.s.section = sectp;
10595 type_section.size = bfd_get_section_size (sectp);
10596 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10597 &type_section);
10598 }
10599 }
10600
10601 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10602 by PER_CU. This is for the non-DWP case.
10603 The result is NULL if DWO_NAME can't be found. */
10604
10605 static struct dwo_file *
10606 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10607 const char *dwo_name, const char *comp_dir)
10608 {
10609 struct objfile *objfile = dwarf2_per_objfile->objfile;
10610 struct dwo_file *dwo_file;
10611 bfd *dbfd;
10612 struct cleanup *cleanups;
10613
10614 dbfd = open_dwo_file (dwo_name, comp_dir);
10615 if (dbfd == NULL)
10616 {
10617 if (dwarf_read_debug)
10618 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10619 return NULL;
10620 }
10621 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10622 dwo_file->dwo_name = dwo_name;
10623 dwo_file->comp_dir = comp_dir;
10624 dwo_file->dbfd = dbfd;
10625
10626 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10627
10628 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
10629
10630 dwo_file->cu = create_dwo_cu (dwo_file);
10631
10632 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10633 dwo_file->sections.types);
10634
10635 discard_cleanups (cleanups);
10636
10637 if (dwarf_read_debug)
10638 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10639
10640 return dwo_file;
10641 }
10642
10643 /* This function is mapped across the sections and remembers the offset and
10644 size of each of the DWP debugging sections common to version 1 and 2 that
10645 we are interested in. */
10646
10647 static void
10648 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10649 void *dwp_file_ptr)
10650 {
10651 struct dwp_file *dwp_file = dwp_file_ptr;
10652 const struct dwop_section_names *names = &dwop_section_names;
10653 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10654
10655 /* Record the ELF section number for later lookup: this is what the
10656 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10657 gdb_assert (elf_section_nr < dwp_file->num_sections);
10658 dwp_file->elf_sections[elf_section_nr] = sectp;
10659
10660 /* Look for specific sections that we need. */
10661 if (section_is_p (sectp->name, &names->str_dwo))
10662 {
10663 dwp_file->sections.str.s.section = sectp;
10664 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10665 }
10666 else if (section_is_p (sectp->name, &names->cu_index))
10667 {
10668 dwp_file->sections.cu_index.s.section = sectp;
10669 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10670 }
10671 else if (section_is_p (sectp->name, &names->tu_index))
10672 {
10673 dwp_file->sections.tu_index.s.section = sectp;
10674 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10675 }
10676 }
10677
10678 /* This function is mapped across the sections and remembers the offset and
10679 size of each of the DWP version 2 debugging sections that we are interested
10680 in. This is split into a separate function because we don't know if we
10681 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10682
10683 static void
10684 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10685 {
10686 struct dwp_file *dwp_file = dwp_file_ptr;
10687 const struct dwop_section_names *names = &dwop_section_names;
10688 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10689
10690 /* Record the ELF section number for later lookup: this is what the
10691 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10692 gdb_assert (elf_section_nr < dwp_file->num_sections);
10693 dwp_file->elf_sections[elf_section_nr] = sectp;
10694
10695 /* Look for specific sections that we need. */
10696 if (section_is_p (sectp->name, &names->abbrev_dwo))
10697 {
10698 dwp_file->sections.abbrev.s.section = sectp;
10699 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10700 }
10701 else if (section_is_p (sectp->name, &names->info_dwo))
10702 {
10703 dwp_file->sections.info.s.section = sectp;
10704 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10705 }
10706 else if (section_is_p (sectp->name, &names->line_dwo))
10707 {
10708 dwp_file->sections.line.s.section = sectp;
10709 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10710 }
10711 else if (section_is_p (sectp->name, &names->loc_dwo))
10712 {
10713 dwp_file->sections.loc.s.section = sectp;
10714 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10715 }
10716 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10717 {
10718 dwp_file->sections.macinfo.s.section = sectp;
10719 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10720 }
10721 else if (section_is_p (sectp->name, &names->macro_dwo))
10722 {
10723 dwp_file->sections.macro.s.section = sectp;
10724 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10725 }
10726 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10727 {
10728 dwp_file->sections.str_offsets.s.section = sectp;
10729 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10730 }
10731 else if (section_is_p (sectp->name, &names->types_dwo))
10732 {
10733 dwp_file->sections.types.s.section = sectp;
10734 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10735 }
10736 }
10737
10738 /* Hash function for dwp_file loaded CUs/TUs. */
10739
10740 static hashval_t
10741 hash_dwp_loaded_cutus (const void *item)
10742 {
10743 const struct dwo_unit *dwo_unit = item;
10744
10745 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10746 return dwo_unit->signature;
10747 }
10748
10749 /* Equality function for dwp_file loaded CUs/TUs. */
10750
10751 static int
10752 eq_dwp_loaded_cutus (const void *a, const void *b)
10753 {
10754 const struct dwo_unit *dua = a;
10755 const struct dwo_unit *dub = b;
10756
10757 return dua->signature == dub->signature;
10758 }
10759
10760 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10761
10762 static htab_t
10763 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10764 {
10765 return htab_create_alloc_ex (3,
10766 hash_dwp_loaded_cutus,
10767 eq_dwp_loaded_cutus,
10768 NULL,
10769 &objfile->objfile_obstack,
10770 hashtab_obstack_allocate,
10771 dummy_obstack_deallocate);
10772 }
10773
10774 /* Try to open DWP file FILE_NAME.
10775 The result is the bfd handle of the file.
10776 If there is a problem finding or opening the file, return NULL.
10777 Upon success, the canonicalized path of the file is stored in the bfd,
10778 same as symfile_bfd_open. */
10779
10780 static bfd *
10781 open_dwp_file (const char *file_name)
10782 {
10783 bfd *abfd;
10784
10785 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10786 if (abfd != NULL)
10787 return abfd;
10788
10789 /* Work around upstream bug 15652.
10790 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10791 [Whether that's a "bug" is debatable, but it is getting in our way.]
10792 We have no real idea where the dwp file is, because gdb's realpath-ing
10793 of the executable's path may have discarded the needed info.
10794 [IWBN if the dwp file name was recorded in the executable, akin to
10795 .gnu_debuglink, but that doesn't exist yet.]
10796 Strip the directory from FILE_NAME and search again. */
10797 if (*debug_file_directory != '\0')
10798 {
10799 /* Don't implicitly search the current directory here.
10800 If the user wants to search "." to handle this case,
10801 it must be added to debug-file-directory. */
10802 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10803 0 /*search_cwd*/);
10804 }
10805
10806 return NULL;
10807 }
10808
10809 /* Initialize the use of the DWP file for the current objfile.
10810 By convention the name of the DWP file is ${objfile}.dwp.
10811 The result is NULL if it can't be found. */
10812
10813 static struct dwp_file *
10814 open_and_init_dwp_file (void)
10815 {
10816 struct objfile *objfile = dwarf2_per_objfile->objfile;
10817 struct dwp_file *dwp_file;
10818 char *dwp_name;
10819 bfd *dbfd;
10820 struct cleanup *cleanups;
10821
10822 /* Try to find first .dwp for the binary file before any symbolic links
10823 resolving. */
10824 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10825 cleanups = make_cleanup (xfree, dwp_name);
10826
10827 dbfd = open_dwp_file (dwp_name);
10828 if (dbfd == NULL
10829 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10830 {
10831 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10832 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10833 make_cleanup (xfree, dwp_name);
10834 dbfd = open_dwp_file (dwp_name);
10835 }
10836
10837 if (dbfd == NULL)
10838 {
10839 if (dwarf_read_debug)
10840 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10841 do_cleanups (cleanups);
10842 return NULL;
10843 }
10844 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10845 dwp_file->name = bfd_get_filename (dbfd);
10846 dwp_file->dbfd = dbfd;
10847 do_cleanups (cleanups);
10848
10849 /* +1: section 0 is unused */
10850 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10851 dwp_file->elf_sections =
10852 OBSTACK_CALLOC (&objfile->objfile_obstack,
10853 dwp_file->num_sections, asection *);
10854
10855 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
10856
10857 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10858
10859 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10860
10861 /* The DWP file version is stored in the hash table. Oh well. */
10862 if (dwp_file->cus->version != dwp_file->tus->version)
10863 {
10864 /* Technically speaking, we should try to limp along, but this is
10865 pretty bizarre. We use pulongest here because that's the established
10866 portability solution (e.g, we cannot use %u for uint32_t). */
10867 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10868 " TU version %s [in DWP file %s]"),
10869 pulongest (dwp_file->cus->version),
10870 pulongest (dwp_file->tus->version), dwp_name);
10871 }
10872 dwp_file->version = dwp_file->cus->version;
10873
10874 if (dwp_file->version == 2)
10875 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10876
10877 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10878 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10879
10880 if (dwarf_read_debug)
10881 {
10882 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10883 fprintf_unfiltered (gdb_stdlog,
10884 " %s CUs, %s TUs\n",
10885 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10886 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10887 }
10888
10889 return dwp_file;
10890 }
10891
10892 /* Wrapper around open_and_init_dwp_file, only open it once. */
10893
10894 static struct dwp_file *
10895 get_dwp_file (void)
10896 {
10897 if (! dwarf2_per_objfile->dwp_checked)
10898 {
10899 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10900 dwarf2_per_objfile->dwp_checked = 1;
10901 }
10902 return dwarf2_per_objfile->dwp_file;
10903 }
10904
10905 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10906 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10907 or in the DWP file for the objfile, referenced by THIS_UNIT.
10908 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10909 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10910
10911 This is called, for example, when wanting to read a variable with a
10912 complex location. Therefore we don't want to do file i/o for every call.
10913 Therefore we don't want to look for a DWO file on every call.
10914 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10915 then we check if we've already seen DWO_NAME, and only THEN do we check
10916 for a DWO file.
10917
10918 The result is a pointer to the dwo_unit object or NULL if we didn't find it
10919 (dwo_id mismatch or couldn't find the DWO/DWP file). */
10920
10921 static struct dwo_unit *
10922 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10923 const char *dwo_name, const char *comp_dir,
10924 ULONGEST signature, int is_debug_types)
10925 {
10926 struct objfile *objfile = dwarf2_per_objfile->objfile;
10927 const char *kind = is_debug_types ? "TU" : "CU";
10928 void **dwo_file_slot;
10929 struct dwo_file *dwo_file;
10930 struct dwp_file *dwp_file;
10931
10932 /* First see if there's a DWP file.
10933 If we have a DWP file but didn't find the DWO inside it, don't
10934 look for the original DWO file. It makes gdb behave differently
10935 depending on whether one is debugging in the build tree. */
10936
10937 dwp_file = get_dwp_file ();
10938 if (dwp_file != NULL)
10939 {
10940 const struct dwp_hash_table *dwp_htab =
10941 is_debug_types ? dwp_file->tus : dwp_file->cus;
10942
10943 if (dwp_htab != NULL)
10944 {
10945 struct dwo_unit *dwo_cutu =
10946 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10947 signature, is_debug_types);
10948
10949 if (dwo_cutu != NULL)
10950 {
10951 if (dwarf_read_debug)
10952 {
10953 fprintf_unfiltered (gdb_stdlog,
10954 "Virtual DWO %s %s found: @%s\n",
10955 kind, hex_string (signature),
10956 host_address_to_string (dwo_cutu));
10957 }
10958 return dwo_cutu;
10959 }
10960 }
10961 }
10962 else
10963 {
10964 /* No DWP file, look for the DWO file. */
10965
10966 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10967 if (*dwo_file_slot == NULL)
10968 {
10969 /* Read in the file and build a table of the CUs/TUs it contains. */
10970 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
10971 }
10972 /* NOTE: This will be NULL if unable to open the file. */
10973 dwo_file = *dwo_file_slot;
10974
10975 if (dwo_file != NULL)
10976 {
10977 struct dwo_unit *dwo_cutu = NULL;
10978
10979 if (is_debug_types && dwo_file->tus)
10980 {
10981 struct dwo_unit find_dwo_cutu;
10982
10983 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10984 find_dwo_cutu.signature = signature;
10985 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
10986 }
10987 else if (!is_debug_types && dwo_file->cu)
10988 {
10989 if (signature == dwo_file->cu->signature)
10990 dwo_cutu = dwo_file->cu;
10991 }
10992
10993 if (dwo_cutu != NULL)
10994 {
10995 if (dwarf_read_debug)
10996 {
10997 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10998 kind, dwo_name, hex_string (signature),
10999 host_address_to_string (dwo_cutu));
11000 }
11001 return dwo_cutu;
11002 }
11003 }
11004 }
11005
11006 /* We didn't find it. This could mean a dwo_id mismatch, or
11007 someone deleted the DWO/DWP file, or the search path isn't set up
11008 correctly to find the file. */
11009
11010 if (dwarf_read_debug)
11011 {
11012 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11013 kind, dwo_name, hex_string (signature));
11014 }
11015
11016 /* This is a warning and not a complaint because it can be caused by
11017 pilot error (e.g., user accidentally deleting the DWO). */
11018 {
11019 /* Print the name of the DWP file if we looked there, helps the user
11020 better diagnose the problem. */
11021 char *dwp_text = NULL;
11022 struct cleanup *cleanups;
11023
11024 if (dwp_file != NULL)
11025 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11026 cleanups = make_cleanup (xfree, dwp_text);
11027
11028 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11029 " [in module %s]"),
11030 kind, dwo_name, hex_string (signature),
11031 dwp_text != NULL ? dwp_text : "",
11032 this_unit->is_debug_types ? "TU" : "CU",
11033 this_unit->offset.sect_off, objfile_name (objfile));
11034
11035 do_cleanups (cleanups);
11036 }
11037 return NULL;
11038 }
11039
11040 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11041 See lookup_dwo_cutu_unit for details. */
11042
11043 static struct dwo_unit *
11044 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11045 const char *dwo_name, const char *comp_dir,
11046 ULONGEST signature)
11047 {
11048 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11049 }
11050
11051 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11052 See lookup_dwo_cutu_unit for details. */
11053
11054 static struct dwo_unit *
11055 lookup_dwo_type_unit (struct signatured_type *this_tu,
11056 const char *dwo_name, const char *comp_dir)
11057 {
11058 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11059 }
11060
11061 /* Traversal function for queue_and_load_all_dwo_tus. */
11062
11063 static int
11064 queue_and_load_dwo_tu (void **slot, void *info)
11065 {
11066 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11067 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11068 ULONGEST signature = dwo_unit->signature;
11069 struct signatured_type *sig_type =
11070 lookup_dwo_signatured_type (per_cu->cu, signature);
11071
11072 if (sig_type != NULL)
11073 {
11074 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11075
11076 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11077 a real dependency of PER_CU on SIG_TYPE. That is detected later
11078 while processing PER_CU. */
11079 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11080 load_full_type_unit (sig_cu);
11081 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11082 }
11083
11084 return 1;
11085 }
11086
11087 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11088 The DWO may have the only definition of the type, though it may not be
11089 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11090 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11091
11092 static void
11093 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11094 {
11095 struct dwo_unit *dwo_unit;
11096 struct dwo_file *dwo_file;
11097
11098 gdb_assert (!per_cu->is_debug_types);
11099 gdb_assert (get_dwp_file () == NULL);
11100 gdb_assert (per_cu->cu != NULL);
11101
11102 dwo_unit = per_cu->cu->dwo_unit;
11103 gdb_assert (dwo_unit != NULL);
11104
11105 dwo_file = dwo_unit->dwo_file;
11106 if (dwo_file->tus != NULL)
11107 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11108 }
11109
11110 /* Free all resources associated with DWO_FILE.
11111 Close the DWO file and munmap the sections.
11112 All memory should be on the objfile obstack. */
11113
11114 static void
11115 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11116 {
11117 int ix;
11118 struct dwarf2_section_info *section;
11119
11120 /* Note: dbfd is NULL for virtual DWO files. */
11121 gdb_bfd_unref (dwo_file->dbfd);
11122
11123 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11124 }
11125
11126 /* Wrapper for free_dwo_file for use in cleanups. */
11127
11128 static void
11129 free_dwo_file_cleanup (void *arg)
11130 {
11131 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11132 struct objfile *objfile = dwarf2_per_objfile->objfile;
11133
11134 free_dwo_file (dwo_file, objfile);
11135 }
11136
11137 /* Traversal function for free_dwo_files. */
11138
11139 static int
11140 free_dwo_file_from_slot (void **slot, void *info)
11141 {
11142 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11143 struct objfile *objfile = (struct objfile *) info;
11144
11145 free_dwo_file (dwo_file, objfile);
11146
11147 return 1;
11148 }
11149
11150 /* Free all resources associated with DWO_FILES. */
11151
11152 static void
11153 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11154 {
11155 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11156 }
11157 \f
11158 /* Read in various DIEs. */
11159
11160 /* qsort helper for inherit_abstract_dies. */
11161
11162 static int
11163 unsigned_int_compar (const void *ap, const void *bp)
11164 {
11165 unsigned int a = *(unsigned int *) ap;
11166 unsigned int b = *(unsigned int *) bp;
11167
11168 return (a > b) - (b > a);
11169 }
11170
11171 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11172 Inherit only the children of the DW_AT_abstract_origin DIE not being
11173 already referenced by DW_AT_abstract_origin from the children of the
11174 current DIE. */
11175
11176 static void
11177 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11178 {
11179 struct die_info *child_die;
11180 unsigned die_children_count;
11181 /* CU offsets which were referenced by children of the current DIE. */
11182 sect_offset *offsets;
11183 sect_offset *offsets_end, *offsetp;
11184 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11185 struct die_info *origin_die;
11186 /* Iterator of the ORIGIN_DIE children. */
11187 struct die_info *origin_child_die;
11188 struct cleanup *cleanups;
11189 struct attribute *attr;
11190 struct dwarf2_cu *origin_cu;
11191 struct pending **origin_previous_list_in_scope;
11192
11193 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11194 if (!attr)
11195 return;
11196
11197 /* Note that following die references may follow to a die in a
11198 different cu. */
11199
11200 origin_cu = cu;
11201 origin_die = follow_die_ref (die, attr, &origin_cu);
11202
11203 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11204 symbols in. */
11205 origin_previous_list_in_scope = origin_cu->list_in_scope;
11206 origin_cu->list_in_scope = cu->list_in_scope;
11207
11208 if (die->tag != origin_die->tag
11209 && !(die->tag == DW_TAG_inlined_subroutine
11210 && origin_die->tag == DW_TAG_subprogram))
11211 complaint (&symfile_complaints,
11212 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11213 die->offset.sect_off, origin_die->offset.sect_off);
11214
11215 child_die = die->child;
11216 die_children_count = 0;
11217 while (child_die && child_die->tag)
11218 {
11219 child_die = sibling_die (child_die);
11220 die_children_count++;
11221 }
11222 offsets = xmalloc (sizeof (*offsets) * die_children_count);
11223 cleanups = make_cleanup (xfree, offsets);
11224
11225 offsets_end = offsets;
11226 for (child_die = die->child;
11227 child_die && child_die->tag;
11228 child_die = sibling_die (child_die))
11229 {
11230 struct die_info *child_origin_die;
11231 struct dwarf2_cu *child_origin_cu;
11232
11233 /* We are trying to process concrete instance entries:
11234 DW_TAG_GNU_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11235 it's not relevant to our analysis here. i.e. detecting DIEs that are
11236 present in the abstract instance but not referenced in the concrete
11237 one. */
11238 if (child_die->tag == DW_TAG_GNU_call_site)
11239 continue;
11240
11241 /* For each CHILD_DIE, find the corresponding child of
11242 ORIGIN_DIE. If there is more than one layer of
11243 DW_AT_abstract_origin, follow them all; there shouldn't be,
11244 but GCC versions at least through 4.4 generate this (GCC PR
11245 40573). */
11246 child_origin_die = child_die;
11247 child_origin_cu = cu;
11248 while (1)
11249 {
11250 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11251 child_origin_cu);
11252 if (attr == NULL)
11253 break;
11254 child_origin_die = follow_die_ref (child_origin_die, attr,
11255 &child_origin_cu);
11256 }
11257
11258 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11259 counterpart may exist. */
11260 if (child_origin_die != child_die)
11261 {
11262 if (child_die->tag != child_origin_die->tag
11263 && !(child_die->tag == DW_TAG_inlined_subroutine
11264 && child_origin_die->tag == DW_TAG_subprogram))
11265 complaint (&symfile_complaints,
11266 _("Child DIE 0x%x and its abstract origin 0x%x have "
11267 "different tags"), child_die->offset.sect_off,
11268 child_origin_die->offset.sect_off);
11269 if (child_origin_die->parent != origin_die)
11270 complaint (&symfile_complaints,
11271 _("Child DIE 0x%x and its abstract origin 0x%x have "
11272 "different parents"), child_die->offset.sect_off,
11273 child_origin_die->offset.sect_off);
11274 else
11275 *offsets_end++ = child_origin_die->offset;
11276 }
11277 }
11278 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11279 unsigned_int_compar);
11280 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11281 if (offsetp[-1].sect_off == offsetp->sect_off)
11282 complaint (&symfile_complaints,
11283 _("Multiple children of DIE 0x%x refer "
11284 "to DIE 0x%x as their abstract origin"),
11285 die->offset.sect_off, offsetp->sect_off);
11286
11287 offsetp = offsets;
11288 origin_child_die = origin_die->child;
11289 while (origin_child_die && origin_child_die->tag)
11290 {
11291 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11292 while (offsetp < offsets_end
11293 && offsetp->sect_off < origin_child_die->offset.sect_off)
11294 offsetp++;
11295 if (offsetp >= offsets_end
11296 || offsetp->sect_off > origin_child_die->offset.sect_off)
11297 {
11298 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11299 Check whether we're already processing ORIGIN_CHILD_DIE.
11300 This can happen with mutually referenced abstract_origins.
11301 PR 16581. */
11302 if (!origin_child_die->in_process)
11303 process_die (origin_child_die, origin_cu);
11304 }
11305 origin_child_die = sibling_die (origin_child_die);
11306 }
11307 origin_cu->list_in_scope = origin_previous_list_in_scope;
11308
11309 do_cleanups (cleanups);
11310 }
11311
11312 static void
11313 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11314 {
11315 struct objfile *objfile = cu->objfile;
11316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11317 struct context_stack *newobj;
11318 CORE_ADDR lowpc;
11319 CORE_ADDR highpc;
11320 struct die_info *child_die;
11321 struct attribute *attr, *call_line, *call_file;
11322 const char *name;
11323 CORE_ADDR baseaddr;
11324 struct block *block;
11325 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11326 VEC (symbolp) *template_args = NULL;
11327 struct template_symbol *templ_func = NULL;
11328
11329 if (inlined_func)
11330 {
11331 /* If we do not have call site information, we can't show the
11332 caller of this inlined function. That's too confusing, so
11333 only use the scope for local variables. */
11334 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11335 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11336 if (call_line == NULL || call_file == NULL)
11337 {
11338 read_lexical_block_scope (die, cu);
11339 return;
11340 }
11341 }
11342
11343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11344
11345 name = dwarf2_name (die, cu);
11346
11347 /* Ignore functions with missing or empty names. These are actually
11348 illegal according to the DWARF standard. */
11349 if (name == NULL)
11350 {
11351 complaint (&symfile_complaints,
11352 _("missing name for subprogram DIE at %d"),
11353 die->offset.sect_off);
11354 return;
11355 }
11356
11357 /* Ignore functions with missing or invalid low and high pc attributes. */
11358 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11359 {
11360 attr = dwarf2_attr (die, DW_AT_external, cu);
11361 if (!attr || !DW_UNSND (attr))
11362 complaint (&symfile_complaints,
11363 _("cannot get low and high bounds "
11364 "for subprogram DIE at %d"),
11365 die->offset.sect_off);
11366 return;
11367 }
11368
11369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11370 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11371
11372 /* If we have any template arguments, then we must allocate a
11373 different sort of symbol. */
11374 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11375 {
11376 if (child_die->tag == DW_TAG_template_type_param
11377 || child_die->tag == DW_TAG_template_value_param)
11378 {
11379 templ_func = allocate_template_symbol (objfile);
11380 templ_func->base.is_cplus_template_function = 1;
11381 break;
11382 }
11383 }
11384
11385 newobj = push_context (0, lowpc);
11386 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11387 (struct symbol *) templ_func);
11388
11389 /* If there is a location expression for DW_AT_frame_base, record
11390 it. */
11391 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11392 if (attr)
11393 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11394
11395 cu->list_in_scope = &local_symbols;
11396
11397 if (die->child != NULL)
11398 {
11399 child_die = die->child;
11400 while (child_die && child_die->tag)
11401 {
11402 if (child_die->tag == DW_TAG_template_type_param
11403 || child_die->tag == DW_TAG_template_value_param)
11404 {
11405 struct symbol *arg = new_symbol (child_die, NULL, cu);
11406
11407 if (arg != NULL)
11408 VEC_safe_push (symbolp, template_args, arg);
11409 }
11410 else
11411 process_die (child_die, cu);
11412 child_die = sibling_die (child_die);
11413 }
11414 }
11415
11416 inherit_abstract_dies (die, cu);
11417
11418 /* If we have a DW_AT_specification, we might need to import using
11419 directives from the context of the specification DIE. See the
11420 comment in determine_prefix. */
11421 if (cu->language == language_cplus
11422 && dwarf2_attr (die, DW_AT_specification, cu))
11423 {
11424 struct dwarf2_cu *spec_cu = cu;
11425 struct die_info *spec_die = die_specification (die, &spec_cu);
11426
11427 while (spec_die)
11428 {
11429 child_die = spec_die->child;
11430 while (child_die && child_die->tag)
11431 {
11432 if (child_die->tag == DW_TAG_imported_module)
11433 process_die (child_die, spec_cu);
11434 child_die = sibling_die (child_die);
11435 }
11436
11437 /* In some cases, GCC generates specification DIEs that
11438 themselves contain DW_AT_specification attributes. */
11439 spec_die = die_specification (spec_die, &spec_cu);
11440 }
11441 }
11442
11443 newobj = pop_context ();
11444 /* Make a block for the local symbols within. */
11445 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11446 lowpc, highpc);
11447
11448 /* For C++, set the block's scope. */
11449 if ((cu->language == language_cplus
11450 || cu->language == language_fortran
11451 || cu->language == language_d)
11452 && cu->processing_has_namespace_info)
11453 block_set_scope (block, determine_prefix (die, cu),
11454 &objfile->objfile_obstack);
11455
11456 /* If we have address ranges, record them. */
11457 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11458
11459 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11460
11461 /* Attach template arguments to function. */
11462 if (! VEC_empty (symbolp, template_args))
11463 {
11464 gdb_assert (templ_func != NULL);
11465
11466 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11467 templ_func->template_arguments
11468 = obstack_alloc (&objfile->objfile_obstack,
11469 (templ_func->n_template_arguments
11470 * sizeof (struct symbol *)));
11471 memcpy (templ_func->template_arguments,
11472 VEC_address (symbolp, template_args),
11473 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11474 VEC_free (symbolp, template_args);
11475 }
11476
11477 /* In C++, we can have functions nested inside functions (e.g., when
11478 a function declares a class that has methods). This means that
11479 when we finish processing a function scope, we may need to go
11480 back to building a containing block's symbol lists. */
11481 local_symbols = newobj->locals;
11482 local_using_directives = newobj->local_using_directives;
11483
11484 /* If we've finished processing a top-level function, subsequent
11485 symbols go in the file symbol list. */
11486 if (outermost_context_p ())
11487 cu->list_in_scope = &file_symbols;
11488 }
11489
11490 /* Process all the DIES contained within a lexical block scope. Start
11491 a new scope, process the dies, and then close the scope. */
11492
11493 static void
11494 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11495 {
11496 struct objfile *objfile = cu->objfile;
11497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11498 struct context_stack *newobj;
11499 CORE_ADDR lowpc, highpc;
11500 struct die_info *child_die;
11501 CORE_ADDR baseaddr;
11502
11503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11504
11505 /* Ignore blocks with missing or invalid low and high pc attributes. */
11506 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11507 as multiple lexical blocks? Handling children in a sane way would
11508 be nasty. Might be easier to properly extend generic blocks to
11509 describe ranges. */
11510 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11511 return;
11512 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11513 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11514
11515 push_context (0, lowpc);
11516 if (die->child != NULL)
11517 {
11518 child_die = die->child;
11519 while (child_die && child_die->tag)
11520 {
11521 process_die (child_die, cu);
11522 child_die = sibling_die (child_die);
11523 }
11524 }
11525 inherit_abstract_dies (die, cu);
11526 newobj = pop_context ();
11527
11528 if (local_symbols != NULL || local_using_directives != NULL)
11529 {
11530 struct block *block
11531 = finish_block (0, &local_symbols, newobj->old_blocks,
11532 newobj->start_addr, highpc);
11533
11534 /* Note that recording ranges after traversing children, as we
11535 do here, means that recording a parent's ranges entails
11536 walking across all its children's ranges as they appear in
11537 the address map, which is quadratic behavior.
11538
11539 It would be nicer to record the parent's ranges before
11540 traversing its children, simply overriding whatever you find
11541 there. But since we don't even decide whether to create a
11542 block until after we've traversed its children, that's hard
11543 to do. */
11544 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11545 }
11546 local_symbols = newobj->locals;
11547 local_using_directives = newobj->local_using_directives;
11548 }
11549
11550 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11551
11552 static void
11553 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11554 {
11555 struct objfile *objfile = cu->objfile;
11556 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11557 CORE_ADDR pc, baseaddr;
11558 struct attribute *attr;
11559 struct call_site *call_site, call_site_local;
11560 void **slot;
11561 int nparams;
11562 struct die_info *child_die;
11563
11564 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11565
11566 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11567 if (!attr)
11568 {
11569 complaint (&symfile_complaints,
11570 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11571 "DIE 0x%x [in module %s]"),
11572 die->offset.sect_off, objfile_name (objfile));
11573 return;
11574 }
11575 pc = attr_value_as_address (attr) + baseaddr;
11576 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11577
11578 if (cu->call_site_htab == NULL)
11579 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11580 NULL, &objfile->objfile_obstack,
11581 hashtab_obstack_allocate, NULL);
11582 call_site_local.pc = pc;
11583 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11584 if (*slot != NULL)
11585 {
11586 complaint (&symfile_complaints,
11587 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11588 "DIE 0x%x [in module %s]"),
11589 paddress (gdbarch, pc), die->offset.sect_off,
11590 objfile_name (objfile));
11591 return;
11592 }
11593
11594 /* Count parameters at the caller. */
11595
11596 nparams = 0;
11597 for (child_die = die->child; child_die && child_die->tag;
11598 child_die = sibling_die (child_die))
11599 {
11600 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11601 {
11602 complaint (&symfile_complaints,
11603 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11604 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11605 child_die->tag, child_die->offset.sect_off,
11606 objfile_name (objfile));
11607 continue;
11608 }
11609
11610 nparams++;
11611 }
11612
11613 call_site = obstack_alloc (&objfile->objfile_obstack,
11614 (sizeof (*call_site)
11615 + (sizeof (*call_site->parameter)
11616 * (nparams - 1))));
11617 *slot = call_site;
11618 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11619 call_site->pc = pc;
11620
11621 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11622 {
11623 struct die_info *func_die;
11624
11625 /* Skip also over DW_TAG_inlined_subroutine. */
11626 for (func_die = die->parent;
11627 func_die && func_die->tag != DW_TAG_subprogram
11628 && func_die->tag != DW_TAG_subroutine_type;
11629 func_die = func_die->parent);
11630
11631 /* DW_AT_GNU_all_call_sites is a superset
11632 of DW_AT_GNU_all_tail_call_sites. */
11633 if (func_die
11634 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11635 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11636 {
11637 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11638 not complete. But keep CALL_SITE for look ups via call_site_htab,
11639 both the initial caller containing the real return address PC and
11640 the final callee containing the current PC of a chain of tail
11641 calls do not need to have the tail call list complete. But any
11642 function candidate for a virtual tail call frame searched via
11643 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11644 determined unambiguously. */
11645 }
11646 else
11647 {
11648 struct type *func_type = NULL;
11649
11650 if (func_die)
11651 func_type = get_die_type (func_die, cu);
11652 if (func_type != NULL)
11653 {
11654 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11655
11656 /* Enlist this call site to the function. */
11657 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11658 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11659 }
11660 else
11661 complaint (&symfile_complaints,
11662 _("Cannot find function owning DW_TAG_GNU_call_site "
11663 "DIE 0x%x [in module %s]"),
11664 die->offset.sect_off, objfile_name (objfile));
11665 }
11666 }
11667
11668 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11669 if (attr == NULL)
11670 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11671 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11672 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11673 /* Keep NULL DWARF_BLOCK. */;
11674 else if (attr_form_is_block (attr))
11675 {
11676 struct dwarf2_locexpr_baton *dlbaton;
11677
11678 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
11679 dlbaton->data = DW_BLOCK (attr)->data;
11680 dlbaton->size = DW_BLOCK (attr)->size;
11681 dlbaton->per_cu = cu->per_cu;
11682
11683 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11684 }
11685 else if (attr_form_is_ref (attr))
11686 {
11687 struct dwarf2_cu *target_cu = cu;
11688 struct die_info *target_die;
11689
11690 target_die = follow_die_ref (die, attr, &target_cu);
11691 gdb_assert (target_cu->objfile == objfile);
11692 if (die_is_declaration (target_die, target_cu))
11693 {
11694 const char *target_physname;
11695
11696 /* Prefer the mangled name; otherwise compute the demangled one. */
11697 target_physname = dwarf2_string_attr (target_die,
11698 DW_AT_linkage_name,
11699 target_cu);
11700 if (target_physname == NULL)
11701 target_physname = dwarf2_string_attr (target_die,
11702 DW_AT_MIPS_linkage_name,
11703 target_cu);
11704 if (target_physname == NULL)
11705 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11706 if (target_physname == NULL)
11707 complaint (&symfile_complaints,
11708 _("DW_AT_GNU_call_site_target target DIE has invalid "
11709 "physname, for referencing DIE 0x%x [in module %s]"),
11710 die->offset.sect_off, objfile_name (objfile));
11711 else
11712 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11713 }
11714 else
11715 {
11716 CORE_ADDR lowpc;
11717
11718 /* DW_AT_entry_pc should be preferred. */
11719 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
11720 complaint (&symfile_complaints,
11721 _("DW_AT_GNU_call_site_target target DIE has invalid "
11722 "low pc, for referencing DIE 0x%x [in module %s]"),
11723 die->offset.sect_off, objfile_name (objfile));
11724 else
11725 {
11726 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11727 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11728 }
11729 }
11730 }
11731 else
11732 complaint (&symfile_complaints,
11733 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11734 "block nor reference, for DIE 0x%x [in module %s]"),
11735 die->offset.sect_off, objfile_name (objfile));
11736
11737 call_site->per_cu = cu->per_cu;
11738
11739 for (child_die = die->child;
11740 child_die && child_die->tag;
11741 child_die = sibling_die (child_die))
11742 {
11743 struct call_site_parameter *parameter;
11744 struct attribute *loc, *origin;
11745
11746 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11747 {
11748 /* Already printed the complaint above. */
11749 continue;
11750 }
11751
11752 gdb_assert (call_site->parameter_count < nparams);
11753 parameter = &call_site->parameter[call_site->parameter_count];
11754
11755 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11756 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11757 register is contained in DW_AT_GNU_call_site_value. */
11758
11759 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11760 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11761 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11762 {
11763 sect_offset offset;
11764
11765 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11766 offset = dwarf2_get_ref_die_offset (origin);
11767 if (!offset_in_cu_p (&cu->header, offset))
11768 {
11769 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11770 binding can be done only inside one CU. Such referenced DIE
11771 therefore cannot be even moved to DW_TAG_partial_unit. */
11772 complaint (&symfile_complaints,
11773 _("DW_AT_abstract_origin offset is not in CU for "
11774 "DW_TAG_GNU_call_site child DIE 0x%x "
11775 "[in module %s]"),
11776 child_die->offset.sect_off, objfile_name (objfile));
11777 continue;
11778 }
11779 parameter->u.param_offset.cu_off = (offset.sect_off
11780 - cu->header.offset.sect_off);
11781 }
11782 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11783 {
11784 complaint (&symfile_complaints,
11785 _("No DW_FORM_block* DW_AT_location for "
11786 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11787 child_die->offset.sect_off, objfile_name (objfile));
11788 continue;
11789 }
11790 else
11791 {
11792 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11793 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11794 if (parameter->u.dwarf_reg != -1)
11795 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11796 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11797 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11798 &parameter->u.fb_offset))
11799 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11800 else
11801 {
11802 complaint (&symfile_complaints,
11803 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11804 "for DW_FORM_block* DW_AT_location is supported for "
11805 "DW_TAG_GNU_call_site child DIE 0x%x "
11806 "[in module %s]"),
11807 child_die->offset.sect_off, objfile_name (objfile));
11808 continue;
11809 }
11810 }
11811
11812 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11813 if (!attr_form_is_block (attr))
11814 {
11815 complaint (&symfile_complaints,
11816 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11817 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11818 child_die->offset.sect_off, objfile_name (objfile));
11819 continue;
11820 }
11821 parameter->value = DW_BLOCK (attr)->data;
11822 parameter->value_size = DW_BLOCK (attr)->size;
11823
11824 /* Parameters are not pre-cleared by memset above. */
11825 parameter->data_value = NULL;
11826 parameter->data_value_size = 0;
11827 call_site->parameter_count++;
11828
11829 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11830 if (attr)
11831 {
11832 if (!attr_form_is_block (attr))
11833 complaint (&symfile_complaints,
11834 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11835 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11836 child_die->offset.sect_off, objfile_name (objfile));
11837 else
11838 {
11839 parameter->data_value = DW_BLOCK (attr)->data;
11840 parameter->data_value_size = DW_BLOCK (attr)->size;
11841 }
11842 }
11843 }
11844 }
11845
11846 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11847 Return 1 if the attributes are present and valid, otherwise, return 0.
11848 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
11849
11850 static int
11851 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11852 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11853 struct partial_symtab *ranges_pst)
11854 {
11855 struct objfile *objfile = cu->objfile;
11856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11857 struct comp_unit_head *cu_header = &cu->header;
11858 bfd *obfd = objfile->obfd;
11859 unsigned int addr_size = cu_header->addr_size;
11860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11861 /* Base address selection entry. */
11862 CORE_ADDR base;
11863 int found_base;
11864 unsigned int dummy;
11865 const gdb_byte *buffer;
11866 CORE_ADDR marker;
11867 int low_set;
11868 CORE_ADDR low = 0;
11869 CORE_ADDR high = 0;
11870 CORE_ADDR baseaddr;
11871
11872 found_base = cu->base_known;
11873 base = cu->base_address;
11874
11875 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11876 if (offset >= dwarf2_per_objfile->ranges.size)
11877 {
11878 complaint (&symfile_complaints,
11879 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11880 offset);
11881 return 0;
11882 }
11883 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11884
11885 /* Read in the largest possible address. */
11886 marker = read_address (obfd, buffer, cu, &dummy);
11887 if ((marker & mask) == mask)
11888 {
11889 /* If we found the largest possible address, then
11890 read the base address. */
11891 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11892 buffer += 2 * addr_size;
11893 offset += 2 * addr_size;
11894 found_base = 1;
11895 }
11896
11897 low_set = 0;
11898
11899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11900
11901 while (1)
11902 {
11903 CORE_ADDR range_beginning, range_end;
11904
11905 range_beginning = read_address (obfd, buffer, cu, &dummy);
11906 buffer += addr_size;
11907 range_end = read_address (obfd, buffer, cu, &dummy);
11908 buffer += addr_size;
11909 offset += 2 * addr_size;
11910
11911 /* An end of list marker is a pair of zero addresses. */
11912 if (range_beginning == 0 && range_end == 0)
11913 /* Found the end of list entry. */
11914 break;
11915
11916 /* Each base address selection entry is a pair of 2 values.
11917 The first is the largest possible address, the second is
11918 the base address. Check for a base address here. */
11919 if ((range_beginning & mask) == mask)
11920 {
11921 /* If we found the largest possible address, then
11922 read the base address. */
11923 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11924 found_base = 1;
11925 continue;
11926 }
11927
11928 if (!found_base)
11929 {
11930 /* We have no valid base address for the ranges
11931 data. */
11932 complaint (&symfile_complaints,
11933 _("Invalid .debug_ranges data (no base address)"));
11934 return 0;
11935 }
11936
11937 if (range_beginning > range_end)
11938 {
11939 /* Inverted range entries are invalid. */
11940 complaint (&symfile_complaints,
11941 _("Invalid .debug_ranges data (inverted range)"));
11942 return 0;
11943 }
11944
11945 /* Empty range entries have no effect. */
11946 if (range_beginning == range_end)
11947 continue;
11948
11949 range_beginning += base;
11950 range_end += base;
11951
11952 /* A not-uncommon case of bad debug info.
11953 Don't pollute the addrmap with bad data. */
11954 if (range_beginning + baseaddr == 0
11955 && !dwarf2_per_objfile->has_section_at_zero)
11956 {
11957 complaint (&symfile_complaints,
11958 _(".debug_ranges entry has start address of zero"
11959 " [in module %s]"), objfile_name (objfile));
11960 continue;
11961 }
11962
11963 if (ranges_pst != NULL)
11964 {
11965 CORE_ADDR lowpc;
11966 CORE_ADDR highpc;
11967
11968 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11969 range_beginning + baseaddr);
11970 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11971 range_end + baseaddr);
11972 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
11973 ranges_pst);
11974 }
11975
11976 /* FIXME: This is recording everything as a low-high
11977 segment of consecutive addresses. We should have a
11978 data structure for discontiguous block ranges
11979 instead. */
11980 if (! low_set)
11981 {
11982 low = range_beginning;
11983 high = range_end;
11984 low_set = 1;
11985 }
11986 else
11987 {
11988 if (range_beginning < low)
11989 low = range_beginning;
11990 if (range_end > high)
11991 high = range_end;
11992 }
11993 }
11994
11995 if (! low_set)
11996 /* If the first entry is an end-of-list marker, the range
11997 describes an empty scope, i.e. no instructions. */
11998 return 0;
11999
12000 if (low_return)
12001 *low_return = low;
12002 if (high_return)
12003 *high_return = high;
12004 return 1;
12005 }
12006
12007 /* Get low and high pc attributes from a die. Return 1 if the attributes
12008 are present and valid, otherwise, return 0. Return -1 if the range is
12009 discontinuous, i.e. derived from DW_AT_ranges information. */
12010
12011 static int
12012 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12013 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12014 struct partial_symtab *pst)
12015 {
12016 struct attribute *attr;
12017 struct attribute *attr_high;
12018 CORE_ADDR low = 0;
12019 CORE_ADDR high = 0;
12020 int ret = 0;
12021
12022 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12023 if (attr_high)
12024 {
12025 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12026 if (attr)
12027 {
12028 low = attr_value_as_address (attr);
12029 high = attr_value_as_address (attr_high);
12030 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12031 high += low;
12032 }
12033 else
12034 /* Found high w/o low attribute. */
12035 return 0;
12036
12037 /* Found consecutive range of addresses. */
12038 ret = 1;
12039 }
12040 else
12041 {
12042 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12043 if (attr != NULL)
12044 {
12045 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12046 We take advantage of the fact that DW_AT_ranges does not appear
12047 in DW_TAG_compile_unit of DWO files. */
12048 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12049 unsigned int ranges_offset = (DW_UNSND (attr)
12050 + (need_ranges_base
12051 ? cu->ranges_base
12052 : 0));
12053
12054 /* Value of the DW_AT_ranges attribute is the offset in the
12055 .debug_ranges section. */
12056 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12057 return 0;
12058 /* Found discontinuous range of addresses. */
12059 ret = -1;
12060 }
12061 }
12062
12063 /* read_partial_die has also the strict LOW < HIGH requirement. */
12064 if (high <= low)
12065 return 0;
12066
12067 /* When using the GNU linker, .gnu.linkonce. sections are used to
12068 eliminate duplicate copies of functions and vtables and such.
12069 The linker will arbitrarily choose one and discard the others.
12070 The AT_*_pc values for such functions refer to local labels in
12071 these sections. If the section from that file was discarded, the
12072 labels are not in the output, so the relocs get a value of 0.
12073 If this is a discarded function, mark the pc bounds as invalid,
12074 so that GDB will ignore it. */
12075 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12076 return 0;
12077
12078 *lowpc = low;
12079 if (highpc)
12080 *highpc = high;
12081 return ret;
12082 }
12083
12084 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12085 its low and high PC addresses. Do nothing if these addresses could not
12086 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12087 and HIGHPC to the high address if greater than HIGHPC. */
12088
12089 static void
12090 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12092 struct dwarf2_cu *cu)
12093 {
12094 CORE_ADDR low, high;
12095 struct die_info *child = die->child;
12096
12097 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
12098 {
12099 *lowpc = min (*lowpc, low);
12100 *highpc = max (*highpc, high);
12101 }
12102
12103 /* If the language does not allow nested subprograms (either inside
12104 subprograms or lexical blocks), we're done. */
12105 if (cu->language != language_ada)
12106 return;
12107
12108 /* Check all the children of the given DIE. If it contains nested
12109 subprograms, then check their pc bounds. Likewise, we need to
12110 check lexical blocks as well, as they may also contain subprogram
12111 definitions. */
12112 while (child && child->tag)
12113 {
12114 if (child->tag == DW_TAG_subprogram
12115 || child->tag == DW_TAG_lexical_block)
12116 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12117 child = sibling_die (child);
12118 }
12119 }
12120
12121 /* Get the low and high pc's represented by the scope DIE, and store
12122 them in *LOWPC and *HIGHPC. If the correct values can't be
12123 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12124
12125 static void
12126 get_scope_pc_bounds (struct die_info *die,
12127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12128 struct dwarf2_cu *cu)
12129 {
12130 CORE_ADDR best_low = (CORE_ADDR) -1;
12131 CORE_ADDR best_high = (CORE_ADDR) 0;
12132 CORE_ADDR current_low, current_high;
12133
12134 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
12135 {
12136 best_low = current_low;
12137 best_high = current_high;
12138 }
12139 else
12140 {
12141 struct die_info *child = die->child;
12142
12143 while (child && child->tag)
12144 {
12145 switch (child->tag) {
12146 case DW_TAG_subprogram:
12147 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12148 break;
12149 case DW_TAG_namespace:
12150 case DW_TAG_module:
12151 /* FIXME: carlton/2004-01-16: Should we do this for
12152 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12153 that current GCC's always emit the DIEs corresponding
12154 to definitions of methods of classes as children of a
12155 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12156 the DIEs giving the declarations, which could be
12157 anywhere). But I don't see any reason why the
12158 standards says that they have to be there. */
12159 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12160
12161 if (current_low != ((CORE_ADDR) -1))
12162 {
12163 best_low = min (best_low, current_low);
12164 best_high = max (best_high, current_high);
12165 }
12166 break;
12167 default:
12168 /* Ignore. */
12169 break;
12170 }
12171
12172 child = sibling_die (child);
12173 }
12174 }
12175
12176 *lowpc = best_low;
12177 *highpc = best_high;
12178 }
12179
12180 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12181 in DIE. */
12182
12183 static void
12184 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12185 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12186 {
12187 struct objfile *objfile = cu->objfile;
12188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12189 struct attribute *attr;
12190 struct attribute *attr_high;
12191
12192 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12193 if (attr_high)
12194 {
12195 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12196 if (attr)
12197 {
12198 CORE_ADDR low = attr_value_as_address (attr);
12199 CORE_ADDR high = attr_value_as_address (attr_high);
12200
12201 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12202 high += low;
12203
12204 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12205 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12206 record_block_range (block, low, high - 1);
12207 }
12208 }
12209
12210 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12211 if (attr)
12212 {
12213 bfd *obfd = objfile->obfd;
12214 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12215 We take advantage of the fact that DW_AT_ranges does not appear
12216 in DW_TAG_compile_unit of DWO files. */
12217 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12218
12219 /* The value of the DW_AT_ranges attribute is the offset of the
12220 address range list in the .debug_ranges section. */
12221 unsigned long offset = (DW_UNSND (attr)
12222 + (need_ranges_base ? cu->ranges_base : 0));
12223 const gdb_byte *buffer;
12224
12225 /* For some target architectures, but not others, the
12226 read_address function sign-extends the addresses it returns.
12227 To recognize base address selection entries, we need a
12228 mask. */
12229 unsigned int addr_size = cu->header.addr_size;
12230 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12231
12232 /* The base address, to which the next pair is relative. Note
12233 that this 'base' is a DWARF concept: most entries in a range
12234 list are relative, to reduce the number of relocs against the
12235 debugging information. This is separate from this function's
12236 'baseaddr' argument, which GDB uses to relocate debugging
12237 information from a shared library based on the address at
12238 which the library was loaded. */
12239 CORE_ADDR base = cu->base_address;
12240 int base_known = cu->base_known;
12241
12242 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12243 if (offset >= dwarf2_per_objfile->ranges.size)
12244 {
12245 complaint (&symfile_complaints,
12246 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12247 offset);
12248 return;
12249 }
12250 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12251
12252 for (;;)
12253 {
12254 unsigned int bytes_read;
12255 CORE_ADDR start, end;
12256
12257 start = read_address (obfd, buffer, cu, &bytes_read);
12258 buffer += bytes_read;
12259 end = read_address (obfd, buffer, cu, &bytes_read);
12260 buffer += bytes_read;
12261
12262 /* Did we find the end of the range list? */
12263 if (start == 0 && end == 0)
12264 break;
12265
12266 /* Did we find a base address selection entry? */
12267 else if ((start & base_select_mask) == base_select_mask)
12268 {
12269 base = end;
12270 base_known = 1;
12271 }
12272
12273 /* We found an ordinary address range. */
12274 else
12275 {
12276 if (!base_known)
12277 {
12278 complaint (&symfile_complaints,
12279 _("Invalid .debug_ranges data "
12280 "(no base address)"));
12281 return;
12282 }
12283
12284 if (start > end)
12285 {
12286 /* Inverted range entries are invalid. */
12287 complaint (&symfile_complaints,
12288 _("Invalid .debug_ranges data "
12289 "(inverted range)"));
12290 return;
12291 }
12292
12293 /* Empty range entries have no effect. */
12294 if (start == end)
12295 continue;
12296
12297 start += base + baseaddr;
12298 end += base + baseaddr;
12299
12300 /* A not-uncommon case of bad debug info.
12301 Don't pollute the addrmap with bad data. */
12302 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
12303 {
12304 complaint (&symfile_complaints,
12305 _(".debug_ranges entry has start address of zero"
12306 " [in module %s]"), objfile_name (objfile));
12307 continue;
12308 }
12309
12310 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12311 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12312 record_block_range (block, start, end - 1);
12313 }
12314 }
12315 }
12316 }
12317
12318 /* Check whether the producer field indicates either of GCC < 4.6, or the
12319 Intel C/C++ compiler, and cache the result in CU. */
12320
12321 static void
12322 check_producer (struct dwarf2_cu *cu)
12323 {
12324 const char *cs;
12325 int major, minor;
12326
12327 if (cu->producer == NULL)
12328 {
12329 /* For unknown compilers expect their behavior is DWARF version
12330 compliant.
12331
12332 GCC started to support .debug_types sections by -gdwarf-4 since
12333 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12334 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12335 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12336 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12337 }
12338 else if (producer_is_gcc (cu->producer, &major, &minor))
12339 {
12340 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12341 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12342 }
12343 else if (startswith (cu->producer, "Intel(R) C"))
12344 cu->producer_is_icc = 1;
12345 else
12346 {
12347 /* For other non-GCC compilers, expect their behavior is DWARF version
12348 compliant. */
12349 }
12350
12351 cu->checked_producer = 1;
12352 }
12353
12354 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12355 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12356 during 4.6.0 experimental. */
12357
12358 static int
12359 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12360 {
12361 if (!cu->checked_producer)
12362 check_producer (cu);
12363
12364 return cu->producer_is_gxx_lt_4_6;
12365 }
12366
12367 /* Return the default accessibility type if it is not overriden by
12368 DW_AT_accessibility. */
12369
12370 static enum dwarf_access_attribute
12371 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12372 {
12373 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12374 {
12375 /* The default DWARF 2 accessibility for members is public, the default
12376 accessibility for inheritance is private. */
12377
12378 if (die->tag != DW_TAG_inheritance)
12379 return DW_ACCESS_public;
12380 else
12381 return DW_ACCESS_private;
12382 }
12383 else
12384 {
12385 /* DWARF 3+ defines the default accessibility a different way. The same
12386 rules apply now for DW_TAG_inheritance as for the members and it only
12387 depends on the container kind. */
12388
12389 if (die->parent->tag == DW_TAG_class_type)
12390 return DW_ACCESS_private;
12391 else
12392 return DW_ACCESS_public;
12393 }
12394 }
12395
12396 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12397 offset. If the attribute was not found return 0, otherwise return
12398 1. If it was found but could not properly be handled, set *OFFSET
12399 to 0. */
12400
12401 static int
12402 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12403 LONGEST *offset)
12404 {
12405 struct attribute *attr;
12406
12407 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12408 if (attr != NULL)
12409 {
12410 *offset = 0;
12411
12412 /* Note that we do not check for a section offset first here.
12413 This is because DW_AT_data_member_location is new in DWARF 4,
12414 so if we see it, we can assume that a constant form is really
12415 a constant and not a section offset. */
12416 if (attr_form_is_constant (attr))
12417 *offset = dwarf2_get_attr_constant_value (attr, 0);
12418 else if (attr_form_is_section_offset (attr))
12419 dwarf2_complex_location_expr_complaint ();
12420 else if (attr_form_is_block (attr))
12421 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12422 else
12423 dwarf2_complex_location_expr_complaint ();
12424
12425 return 1;
12426 }
12427
12428 return 0;
12429 }
12430
12431 /* Add an aggregate field to the field list. */
12432
12433 static void
12434 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12435 struct dwarf2_cu *cu)
12436 {
12437 struct objfile *objfile = cu->objfile;
12438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12439 struct nextfield *new_field;
12440 struct attribute *attr;
12441 struct field *fp;
12442 const char *fieldname = "";
12443
12444 /* Allocate a new field list entry and link it in. */
12445 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
12446 make_cleanup (xfree, new_field);
12447 memset (new_field, 0, sizeof (struct nextfield));
12448
12449 if (die->tag == DW_TAG_inheritance)
12450 {
12451 new_field->next = fip->baseclasses;
12452 fip->baseclasses = new_field;
12453 }
12454 else
12455 {
12456 new_field->next = fip->fields;
12457 fip->fields = new_field;
12458 }
12459 fip->nfields++;
12460
12461 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12462 if (attr)
12463 new_field->accessibility = DW_UNSND (attr);
12464 else
12465 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12466 if (new_field->accessibility != DW_ACCESS_public)
12467 fip->non_public_fields = 1;
12468
12469 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12470 if (attr)
12471 new_field->virtuality = DW_UNSND (attr);
12472 else
12473 new_field->virtuality = DW_VIRTUALITY_none;
12474
12475 fp = &new_field->field;
12476
12477 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12478 {
12479 LONGEST offset;
12480
12481 /* Data member other than a C++ static data member. */
12482
12483 /* Get type of field. */
12484 fp->type = die_type (die, cu);
12485
12486 SET_FIELD_BITPOS (*fp, 0);
12487
12488 /* Get bit size of field (zero if none). */
12489 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12490 if (attr)
12491 {
12492 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12493 }
12494 else
12495 {
12496 FIELD_BITSIZE (*fp) = 0;
12497 }
12498
12499 /* Get bit offset of field. */
12500 if (handle_data_member_location (die, cu, &offset))
12501 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12502 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12503 if (attr)
12504 {
12505 if (gdbarch_bits_big_endian (gdbarch))
12506 {
12507 /* For big endian bits, the DW_AT_bit_offset gives the
12508 additional bit offset from the MSB of the containing
12509 anonymous object to the MSB of the field. We don't
12510 have to do anything special since we don't need to
12511 know the size of the anonymous object. */
12512 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12513 }
12514 else
12515 {
12516 /* For little endian bits, compute the bit offset to the
12517 MSB of the anonymous object, subtract off the number of
12518 bits from the MSB of the field to the MSB of the
12519 object, and then subtract off the number of bits of
12520 the field itself. The result is the bit offset of
12521 the LSB of the field. */
12522 int anonymous_size;
12523 int bit_offset = DW_UNSND (attr);
12524
12525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12526 if (attr)
12527 {
12528 /* The size of the anonymous object containing
12529 the bit field is explicit, so use the
12530 indicated size (in bytes). */
12531 anonymous_size = DW_UNSND (attr);
12532 }
12533 else
12534 {
12535 /* The size of the anonymous object containing
12536 the bit field must be inferred from the type
12537 attribute of the data member containing the
12538 bit field. */
12539 anonymous_size = TYPE_LENGTH (fp->type);
12540 }
12541 SET_FIELD_BITPOS (*fp,
12542 (FIELD_BITPOS (*fp)
12543 + anonymous_size * bits_per_byte
12544 - bit_offset - FIELD_BITSIZE (*fp)));
12545 }
12546 }
12547
12548 /* Get name of field. */
12549 fieldname = dwarf2_name (die, cu);
12550 if (fieldname == NULL)
12551 fieldname = "";
12552
12553 /* The name is already allocated along with this objfile, so we don't
12554 need to duplicate it for the type. */
12555 fp->name = fieldname;
12556
12557 /* Change accessibility for artificial fields (e.g. virtual table
12558 pointer or virtual base class pointer) to private. */
12559 if (dwarf2_attr (die, DW_AT_artificial, cu))
12560 {
12561 FIELD_ARTIFICIAL (*fp) = 1;
12562 new_field->accessibility = DW_ACCESS_private;
12563 fip->non_public_fields = 1;
12564 }
12565 }
12566 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12567 {
12568 /* C++ static member. */
12569
12570 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12571 is a declaration, but all versions of G++ as of this writing
12572 (so through at least 3.2.1) incorrectly generate
12573 DW_TAG_variable tags. */
12574
12575 const char *physname;
12576
12577 /* Get name of field. */
12578 fieldname = dwarf2_name (die, cu);
12579 if (fieldname == NULL)
12580 return;
12581
12582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12583 if (attr
12584 /* Only create a symbol if this is an external value.
12585 new_symbol checks this and puts the value in the global symbol
12586 table, which we want. If it is not external, new_symbol
12587 will try to put the value in cu->list_in_scope which is wrong. */
12588 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12589 {
12590 /* A static const member, not much different than an enum as far as
12591 we're concerned, except that we can support more types. */
12592 new_symbol (die, NULL, cu);
12593 }
12594
12595 /* Get physical name. */
12596 physname = dwarf2_physname (fieldname, die, cu);
12597
12598 /* The name is already allocated along with this objfile, so we don't
12599 need to duplicate it for the type. */
12600 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12601 FIELD_TYPE (*fp) = die_type (die, cu);
12602 FIELD_NAME (*fp) = fieldname;
12603 }
12604 else if (die->tag == DW_TAG_inheritance)
12605 {
12606 LONGEST offset;
12607
12608 /* C++ base class field. */
12609 if (handle_data_member_location (die, cu, &offset))
12610 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12611 FIELD_BITSIZE (*fp) = 0;
12612 FIELD_TYPE (*fp) = die_type (die, cu);
12613 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12614 fip->nbaseclasses++;
12615 }
12616 }
12617
12618 /* Add a typedef defined in the scope of the FIP's class. */
12619
12620 static void
12621 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12622 struct dwarf2_cu *cu)
12623 {
12624 struct objfile *objfile = cu->objfile;
12625 struct typedef_field_list *new_field;
12626 struct attribute *attr;
12627 struct typedef_field *fp;
12628 char *fieldname = "";
12629
12630 /* Allocate a new field list entry and link it in. */
12631 new_field = xzalloc (sizeof (*new_field));
12632 make_cleanup (xfree, new_field);
12633
12634 gdb_assert (die->tag == DW_TAG_typedef);
12635
12636 fp = &new_field->field;
12637
12638 /* Get name of field. */
12639 fp->name = dwarf2_name (die, cu);
12640 if (fp->name == NULL)
12641 return;
12642
12643 fp->type = read_type_die (die, cu);
12644
12645 new_field->next = fip->typedef_field_list;
12646 fip->typedef_field_list = new_field;
12647 fip->typedef_field_list_count++;
12648 }
12649
12650 /* Create the vector of fields, and attach it to the type. */
12651
12652 static void
12653 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12654 struct dwarf2_cu *cu)
12655 {
12656 int nfields = fip->nfields;
12657
12658 /* Record the field count, allocate space for the array of fields,
12659 and create blank accessibility bitfields if necessary. */
12660 TYPE_NFIELDS (type) = nfields;
12661 TYPE_FIELDS (type) = (struct field *)
12662 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12663 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12664
12665 if (fip->non_public_fields && cu->language != language_ada)
12666 {
12667 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12668
12669 TYPE_FIELD_PRIVATE_BITS (type) =
12670 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12671 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12672
12673 TYPE_FIELD_PROTECTED_BITS (type) =
12674 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12675 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12676
12677 TYPE_FIELD_IGNORE_BITS (type) =
12678 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12679 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12680 }
12681
12682 /* If the type has baseclasses, allocate and clear a bit vector for
12683 TYPE_FIELD_VIRTUAL_BITS. */
12684 if (fip->nbaseclasses && cu->language != language_ada)
12685 {
12686 int num_bytes = B_BYTES (fip->nbaseclasses);
12687 unsigned char *pointer;
12688
12689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12690 pointer = TYPE_ALLOC (type, num_bytes);
12691 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12692 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12693 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12694 }
12695
12696 /* Copy the saved-up fields into the field vector. Start from the head of
12697 the list, adding to the tail of the field array, so that they end up in
12698 the same order in the array in which they were added to the list. */
12699 while (nfields-- > 0)
12700 {
12701 struct nextfield *fieldp;
12702
12703 if (fip->fields)
12704 {
12705 fieldp = fip->fields;
12706 fip->fields = fieldp->next;
12707 }
12708 else
12709 {
12710 fieldp = fip->baseclasses;
12711 fip->baseclasses = fieldp->next;
12712 }
12713
12714 TYPE_FIELD (type, nfields) = fieldp->field;
12715 switch (fieldp->accessibility)
12716 {
12717 case DW_ACCESS_private:
12718 if (cu->language != language_ada)
12719 SET_TYPE_FIELD_PRIVATE (type, nfields);
12720 break;
12721
12722 case DW_ACCESS_protected:
12723 if (cu->language != language_ada)
12724 SET_TYPE_FIELD_PROTECTED (type, nfields);
12725 break;
12726
12727 case DW_ACCESS_public:
12728 break;
12729
12730 default:
12731 /* Unknown accessibility. Complain and treat it as public. */
12732 {
12733 complaint (&symfile_complaints, _("unsupported accessibility %d"),
12734 fieldp->accessibility);
12735 }
12736 break;
12737 }
12738 if (nfields < fip->nbaseclasses)
12739 {
12740 switch (fieldp->virtuality)
12741 {
12742 case DW_VIRTUALITY_virtual:
12743 case DW_VIRTUALITY_pure_virtual:
12744 if (cu->language == language_ada)
12745 error (_("unexpected virtuality in component of Ada type"));
12746 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12747 break;
12748 }
12749 }
12750 }
12751 }
12752
12753 /* Return true if this member function is a constructor, false
12754 otherwise. */
12755
12756 static int
12757 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12758 {
12759 const char *fieldname;
12760 const char *type_name;
12761 int len;
12762
12763 if (die->parent == NULL)
12764 return 0;
12765
12766 if (die->parent->tag != DW_TAG_structure_type
12767 && die->parent->tag != DW_TAG_union_type
12768 && die->parent->tag != DW_TAG_class_type)
12769 return 0;
12770
12771 fieldname = dwarf2_name (die, cu);
12772 type_name = dwarf2_name (die->parent, cu);
12773 if (fieldname == NULL || type_name == NULL)
12774 return 0;
12775
12776 len = strlen (fieldname);
12777 return (strncmp (fieldname, type_name, len) == 0
12778 && (type_name[len] == '\0' || type_name[len] == '<'));
12779 }
12780
12781 /* Add a member function to the proper fieldlist. */
12782
12783 static void
12784 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12785 struct type *type, struct dwarf2_cu *cu)
12786 {
12787 struct objfile *objfile = cu->objfile;
12788 struct attribute *attr;
12789 struct fnfieldlist *flp;
12790 int i;
12791 struct fn_field *fnp;
12792 const char *fieldname;
12793 struct nextfnfield *new_fnfield;
12794 struct type *this_type;
12795 enum dwarf_access_attribute accessibility;
12796
12797 if (cu->language == language_ada)
12798 error (_("unexpected member function in Ada type"));
12799
12800 /* Get name of member function. */
12801 fieldname = dwarf2_name (die, cu);
12802 if (fieldname == NULL)
12803 return;
12804
12805 /* Look up member function name in fieldlist. */
12806 for (i = 0; i < fip->nfnfields; i++)
12807 {
12808 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12809 break;
12810 }
12811
12812 /* Create new list element if necessary. */
12813 if (i < fip->nfnfields)
12814 flp = &fip->fnfieldlists[i];
12815 else
12816 {
12817 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12818 {
12819 fip->fnfieldlists = (struct fnfieldlist *)
12820 xrealloc (fip->fnfieldlists,
12821 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
12822 * sizeof (struct fnfieldlist));
12823 if (fip->nfnfields == 0)
12824 make_cleanup (free_current_contents, &fip->fnfieldlists);
12825 }
12826 flp = &fip->fnfieldlists[fip->nfnfields];
12827 flp->name = fieldname;
12828 flp->length = 0;
12829 flp->head = NULL;
12830 i = fip->nfnfields++;
12831 }
12832
12833 /* Create a new member function field and chain it to the field list
12834 entry. */
12835 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
12836 make_cleanup (xfree, new_fnfield);
12837 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12838 new_fnfield->next = flp->head;
12839 flp->head = new_fnfield;
12840 flp->length++;
12841
12842 /* Fill in the member function field info. */
12843 fnp = &new_fnfield->fnfield;
12844
12845 /* Delay processing of the physname until later. */
12846 if (cu->language == language_cplus || cu->language == language_java)
12847 {
12848 add_to_method_list (type, i, flp->length - 1, fieldname,
12849 die, cu);
12850 }
12851 else
12852 {
12853 const char *physname = dwarf2_physname (fieldname, die, cu);
12854 fnp->physname = physname ? physname : "";
12855 }
12856
12857 fnp->type = alloc_type (objfile);
12858 this_type = read_type_die (die, cu);
12859 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
12860 {
12861 int nparams = TYPE_NFIELDS (this_type);
12862
12863 /* TYPE is the domain of this method, and THIS_TYPE is the type
12864 of the method itself (TYPE_CODE_METHOD). */
12865 smash_to_method_type (fnp->type, type,
12866 TYPE_TARGET_TYPE (this_type),
12867 TYPE_FIELDS (this_type),
12868 TYPE_NFIELDS (this_type),
12869 TYPE_VARARGS (this_type));
12870
12871 /* Handle static member functions.
12872 Dwarf2 has no clean way to discern C++ static and non-static
12873 member functions. G++ helps GDB by marking the first
12874 parameter for non-static member functions (which is the this
12875 pointer) as artificial. We obtain this information from
12876 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12877 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12878 fnp->voffset = VOFFSET_STATIC;
12879 }
12880 else
12881 complaint (&symfile_complaints, _("member function type missing for '%s'"),
12882 dwarf2_full_name (fieldname, die, cu));
12883
12884 /* Get fcontext from DW_AT_containing_type if present. */
12885 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12886 fnp->fcontext = die_containing_type (die, cu);
12887
12888 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12889 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12890
12891 /* Get accessibility. */
12892 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12893 if (attr)
12894 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
12895 else
12896 accessibility = dwarf2_default_access_attribute (die, cu);
12897 switch (accessibility)
12898 {
12899 case DW_ACCESS_private:
12900 fnp->is_private = 1;
12901 break;
12902 case DW_ACCESS_protected:
12903 fnp->is_protected = 1;
12904 break;
12905 }
12906
12907 /* Check for artificial methods. */
12908 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12909 if (attr && DW_UNSND (attr) != 0)
12910 fnp->is_artificial = 1;
12911
12912 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12913
12914 /* Get index in virtual function table if it is a virtual member
12915 function. For older versions of GCC, this is an offset in the
12916 appropriate virtual table, as specified by DW_AT_containing_type.
12917 For everyone else, it is an expression to be evaluated relative
12918 to the object address. */
12919
12920 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12921 if (attr)
12922 {
12923 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
12924 {
12925 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12926 {
12927 /* Old-style GCC. */
12928 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12929 }
12930 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12931 || (DW_BLOCK (attr)->size > 1
12932 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12933 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12934 {
12935 struct dwarf_block blk;
12936 int offset;
12937
12938 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
12939 ? 1 : 2);
12940 blk.size = DW_BLOCK (attr)->size - offset;
12941 blk.data = DW_BLOCK (attr)->data + offset;
12942 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12943 if ((fnp->voffset % cu->header.addr_size) != 0)
12944 dwarf2_complex_location_expr_complaint ();
12945 else
12946 fnp->voffset /= cu->header.addr_size;
12947 fnp->voffset += 2;
12948 }
12949 else
12950 dwarf2_complex_location_expr_complaint ();
12951
12952 if (!fnp->fcontext)
12953 {
12954 /* If there is no `this' field and no DW_AT_containing_type,
12955 we cannot actually find a base class context for the
12956 vtable! */
12957 if (TYPE_NFIELDS (this_type) == 0
12958 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12959 {
12960 complaint (&symfile_complaints,
12961 _("cannot determine context for virtual member "
12962 "function \"%s\" (offset %d)"),
12963 fieldname, die->offset.sect_off);
12964 }
12965 else
12966 {
12967 fnp->fcontext
12968 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12969 }
12970 }
12971 }
12972 else if (attr_form_is_section_offset (attr))
12973 {
12974 dwarf2_complex_location_expr_complaint ();
12975 }
12976 else
12977 {
12978 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12979 fieldname);
12980 }
12981 }
12982 else
12983 {
12984 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12985 if (attr && DW_UNSND (attr))
12986 {
12987 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12988 complaint (&symfile_complaints,
12989 _("Member function \"%s\" (offset %d) is virtual "
12990 "but the vtable offset is not specified"),
12991 fieldname, die->offset.sect_off);
12992 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12993 TYPE_CPLUS_DYNAMIC (type) = 1;
12994 }
12995 }
12996 }
12997
12998 /* Create the vector of member function fields, and attach it to the type. */
12999
13000 static void
13001 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13002 struct dwarf2_cu *cu)
13003 {
13004 struct fnfieldlist *flp;
13005 int i;
13006
13007 if (cu->language == language_ada)
13008 error (_("unexpected member functions in Ada type"));
13009
13010 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13011 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13012 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13013
13014 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13015 {
13016 struct nextfnfield *nfp = flp->head;
13017 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13018 int k;
13019
13020 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13021 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13022 fn_flp->fn_fields = (struct fn_field *)
13023 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13024 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13025 fn_flp->fn_fields[k] = nfp->fnfield;
13026 }
13027
13028 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13029 }
13030
13031 /* Returns non-zero if NAME is the name of a vtable member in CU's
13032 language, zero otherwise. */
13033 static int
13034 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13035 {
13036 static const char vptr[] = "_vptr";
13037 static const char vtable[] = "vtable";
13038
13039 /* Look for the C++ and Java forms of the vtable. */
13040 if ((cu->language == language_java
13041 && startswith (name, vtable))
13042 || (startswith (name, vptr)
13043 && is_cplus_marker (name[sizeof (vptr) - 1])))
13044 return 1;
13045
13046 return 0;
13047 }
13048
13049 /* GCC outputs unnamed structures that are really pointers to member
13050 functions, with the ABI-specified layout. If TYPE describes
13051 such a structure, smash it into a member function type.
13052
13053 GCC shouldn't do this; it should just output pointer to member DIEs.
13054 This is GCC PR debug/28767. */
13055
13056 static void
13057 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13058 {
13059 struct type *pfn_type, *self_type, *new_type;
13060
13061 /* Check for a structure with no name and two children. */
13062 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13063 return;
13064
13065 /* Check for __pfn and __delta members. */
13066 if (TYPE_FIELD_NAME (type, 0) == NULL
13067 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13068 || TYPE_FIELD_NAME (type, 1) == NULL
13069 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13070 return;
13071
13072 /* Find the type of the method. */
13073 pfn_type = TYPE_FIELD_TYPE (type, 0);
13074 if (pfn_type == NULL
13075 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13076 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13077 return;
13078
13079 /* Look for the "this" argument. */
13080 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13081 if (TYPE_NFIELDS (pfn_type) == 0
13082 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13083 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13084 return;
13085
13086 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13087 new_type = alloc_type (objfile);
13088 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13089 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13090 TYPE_VARARGS (pfn_type));
13091 smash_to_methodptr_type (type, new_type);
13092 }
13093
13094 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13095 (icc). */
13096
13097 static int
13098 producer_is_icc (struct dwarf2_cu *cu)
13099 {
13100 if (!cu->checked_producer)
13101 check_producer (cu);
13102
13103 return cu->producer_is_icc;
13104 }
13105
13106 /* Called when we find the DIE that starts a structure or union scope
13107 (definition) to create a type for the structure or union. Fill in
13108 the type's name and general properties; the members will not be
13109 processed until process_structure_scope. A symbol table entry for
13110 the type will also not be done until process_structure_scope (assuming
13111 the type has a name).
13112
13113 NOTE: we need to call these functions regardless of whether or not the
13114 DIE has a DW_AT_name attribute, since it might be an anonymous
13115 structure or union. This gets the type entered into our set of
13116 user defined types. */
13117
13118 static struct type *
13119 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13120 {
13121 struct objfile *objfile = cu->objfile;
13122 struct type *type;
13123 struct attribute *attr;
13124 const char *name;
13125
13126 /* If the definition of this type lives in .debug_types, read that type.
13127 Don't follow DW_AT_specification though, that will take us back up
13128 the chain and we want to go down. */
13129 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13130 if (attr)
13131 {
13132 type = get_DW_AT_signature_type (die, attr, cu);
13133
13134 /* The type's CU may not be the same as CU.
13135 Ensure TYPE is recorded with CU in die_type_hash. */
13136 return set_die_type (die, type, cu);
13137 }
13138
13139 type = alloc_type (objfile);
13140 INIT_CPLUS_SPECIFIC (type);
13141
13142 name = dwarf2_name (die, cu);
13143 if (name != NULL)
13144 {
13145 if (cu->language == language_cplus
13146 || cu->language == language_java
13147 || cu->language == language_d)
13148 {
13149 const char *full_name = dwarf2_full_name (name, die, cu);
13150
13151 /* dwarf2_full_name might have already finished building the DIE's
13152 type. If so, there is no need to continue. */
13153 if (get_die_type (die, cu) != NULL)
13154 return get_die_type (die, cu);
13155
13156 TYPE_TAG_NAME (type) = full_name;
13157 if (die->tag == DW_TAG_structure_type
13158 || die->tag == DW_TAG_class_type)
13159 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13160 }
13161 else
13162 {
13163 /* The name is already allocated along with this objfile, so
13164 we don't need to duplicate it for the type. */
13165 TYPE_TAG_NAME (type) = name;
13166 if (die->tag == DW_TAG_class_type)
13167 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13168 }
13169 }
13170
13171 if (die->tag == DW_TAG_structure_type)
13172 {
13173 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13174 }
13175 else if (die->tag == DW_TAG_union_type)
13176 {
13177 TYPE_CODE (type) = TYPE_CODE_UNION;
13178 }
13179 else
13180 {
13181 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13182 }
13183
13184 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13185 TYPE_DECLARED_CLASS (type) = 1;
13186
13187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13188 if (attr)
13189 {
13190 TYPE_LENGTH (type) = DW_UNSND (attr);
13191 }
13192 else
13193 {
13194 TYPE_LENGTH (type) = 0;
13195 }
13196
13197 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13198 {
13199 /* ICC does not output the required DW_AT_declaration
13200 on incomplete types, but gives them a size of zero. */
13201 TYPE_STUB (type) = 1;
13202 }
13203 else
13204 TYPE_STUB_SUPPORTED (type) = 1;
13205
13206 if (die_is_declaration (die, cu))
13207 TYPE_STUB (type) = 1;
13208 else if (attr == NULL && die->child == NULL
13209 && producer_is_realview (cu->producer))
13210 /* RealView does not output the required DW_AT_declaration
13211 on incomplete types. */
13212 TYPE_STUB (type) = 1;
13213
13214 /* We need to add the type field to the die immediately so we don't
13215 infinitely recurse when dealing with pointers to the structure
13216 type within the structure itself. */
13217 set_die_type (die, type, cu);
13218
13219 /* set_die_type should be already done. */
13220 set_descriptive_type (type, die, cu);
13221
13222 return type;
13223 }
13224
13225 /* Finish creating a structure or union type, including filling in
13226 its members and creating a symbol for it. */
13227
13228 static void
13229 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13230 {
13231 struct objfile *objfile = cu->objfile;
13232 struct die_info *child_die;
13233 struct type *type;
13234
13235 type = get_die_type (die, cu);
13236 if (type == NULL)
13237 type = read_structure_type (die, cu);
13238
13239 if (die->child != NULL && ! die_is_declaration (die, cu))
13240 {
13241 struct field_info fi;
13242 VEC (symbolp) *template_args = NULL;
13243 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13244
13245 memset (&fi, 0, sizeof (struct field_info));
13246
13247 child_die = die->child;
13248
13249 while (child_die && child_die->tag)
13250 {
13251 if (child_die->tag == DW_TAG_member
13252 || child_die->tag == DW_TAG_variable)
13253 {
13254 /* NOTE: carlton/2002-11-05: A C++ static data member
13255 should be a DW_TAG_member that is a declaration, but
13256 all versions of G++ as of this writing (so through at
13257 least 3.2.1) incorrectly generate DW_TAG_variable
13258 tags for them instead. */
13259 dwarf2_add_field (&fi, child_die, cu);
13260 }
13261 else if (child_die->tag == DW_TAG_subprogram)
13262 {
13263 /* C++ member function. */
13264 dwarf2_add_member_fn (&fi, child_die, type, cu);
13265 }
13266 else if (child_die->tag == DW_TAG_inheritance)
13267 {
13268 /* C++ base class field. */
13269 dwarf2_add_field (&fi, child_die, cu);
13270 }
13271 else if (child_die->tag == DW_TAG_typedef)
13272 dwarf2_add_typedef (&fi, child_die, cu);
13273 else if (child_die->tag == DW_TAG_template_type_param
13274 || child_die->tag == DW_TAG_template_value_param)
13275 {
13276 struct symbol *arg = new_symbol (child_die, NULL, cu);
13277
13278 if (arg != NULL)
13279 VEC_safe_push (symbolp, template_args, arg);
13280 }
13281
13282 child_die = sibling_die (child_die);
13283 }
13284
13285 /* Attach template arguments to type. */
13286 if (! VEC_empty (symbolp, template_args))
13287 {
13288 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13289 TYPE_N_TEMPLATE_ARGUMENTS (type)
13290 = VEC_length (symbolp, template_args);
13291 TYPE_TEMPLATE_ARGUMENTS (type)
13292 = obstack_alloc (&objfile->objfile_obstack,
13293 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13294 * sizeof (struct symbol *)));
13295 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13296 VEC_address (symbolp, template_args),
13297 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13298 * sizeof (struct symbol *)));
13299 VEC_free (symbolp, template_args);
13300 }
13301
13302 /* Attach fields and member functions to the type. */
13303 if (fi.nfields)
13304 dwarf2_attach_fields_to_type (&fi, type, cu);
13305 if (fi.nfnfields)
13306 {
13307 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13308
13309 /* Get the type which refers to the base class (possibly this
13310 class itself) which contains the vtable pointer for the current
13311 class from the DW_AT_containing_type attribute. This use of
13312 DW_AT_containing_type is a GNU extension. */
13313
13314 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13315 {
13316 struct type *t = die_containing_type (die, cu);
13317
13318 set_type_vptr_basetype (type, t);
13319 if (type == t)
13320 {
13321 int i;
13322
13323 /* Our own class provides vtbl ptr. */
13324 for (i = TYPE_NFIELDS (t) - 1;
13325 i >= TYPE_N_BASECLASSES (t);
13326 --i)
13327 {
13328 const char *fieldname = TYPE_FIELD_NAME (t, i);
13329
13330 if (is_vtable_name (fieldname, cu))
13331 {
13332 set_type_vptr_fieldno (type, i);
13333 break;
13334 }
13335 }
13336
13337 /* Complain if virtual function table field not found. */
13338 if (i < TYPE_N_BASECLASSES (t))
13339 complaint (&symfile_complaints,
13340 _("virtual function table pointer "
13341 "not found when defining class '%s'"),
13342 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13343 "");
13344 }
13345 else
13346 {
13347 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13348 }
13349 }
13350 else if (cu->producer
13351 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13352 {
13353 /* The IBM XLC compiler does not provide direct indication
13354 of the containing type, but the vtable pointer is
13355 always named __vfp. */
13356
13357 int i;
13358
13359 for (i = TYPE_NFIELDS (type) - 1;
13360 i >= TYPE_N_BASECLASSES (type);
13361 --i)
13362 {
13363 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13364 {
13365 set_type_vptr_fieldno (type, i);
13366 set_type_vptr_basetype (type, type);
13367 break;
13368 }
13369 }
13370 }
13371 }
13372
13373 /* Copy fi.typedef_field_list linked list elements content into the
13374 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13375 if (fi.typedef_field_list)
13376 {
13377 int i = fi.typedef_field_list_count;
13378
13379 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13380 TYPE_TYPEDEF_FIELD_ARRAY (type)
13381 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
13382 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13383
13384 /* Reverse the list order to keep the debug info elements order. */
13385 while (--i >= 0)
13386 {
13387 struct typedef_field *dest, *src;
13388
13389 dest = &TYPE_TYPEDEF_FIELD (type, i);
13390 src = &fi.typedef_field_list->field;
13391 fi.typedef_field_list = fi.typedef_field_list->next;
13392 *dest = *src;
13393 }
13394 }
13395
13396 do_cleanups (back_to);
13397
13398 if (HAVE_CPLUS_STRUCT (type))
13399 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
13400 }
13401
13402 quirk_gcc_member_function_pointer (type, objfile);
13403
13404 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13405 snapshots) has been known to create a die giving a declaration
13406 for a class that has, as a child, a die giving a definition for a
13407 nested class. So we have to process our children even if the
13408 current die is a declaration. Normally, of course, a declaration
13409 won't have any children at all. */
13410
13411 child_die = die->child;
13412
13413 while (child_die != NULL && child_die->tag)
13414 {
13415 if (child_die->tag == DW_TAG_member
13416 || child_die->tag == DW_TAG_variable
13417 || child_die->tag == DW_TAG_inheritance
13418 || child_die->tag == DW_TAG_template_value_param
13419 || child_die->tag == DW_TAG_template_type_param)
13420 {
13421 /* Do nothing. */
13422 }
13423 else
13424 process_die (child_die, cu);
13425
13426 child_die = sibling_die (child_die);
13427 }
13428
13429 /* Do not consider external references. According to the DWARF standard,
13430 these DIEs are identified by the fact that they have no byte_size
13431 attribute, and a declaration attribute. */
13432 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13433 || !die_is_declaration (die, cu))
13434 new_symbol (die, type, cu);
13435 }
13436
13437 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13438 update TYPE using some information only available in DIE's children. */
13439
13440 static void
13441 update_enumeration_type_from_children (struct die_info *die,
13442 struct type *type,
13443 struct dwarf2_cu *cu)
13444 {
13445 struct obstack obstack;
13446 struct die_info *child_die;
13447 int unsigned_enum = 1;
13448 int flag_enum = 1;
13449 ULONGEST mask = 0;
13450 struct cleanup *old_chain;
13451
13452 obstack_init (&obstack);
13453 old_chain = make_cleanup_obstack_free (&obstack);
13454
13455 for (child_die = die->child;
13456 child_die != NULL && child_die->tag;
13457 child_die = sibling_die (child_die))
13458 {
13459 struct attribute *attr;
13460 LONGEST value;
13461 const gdb_byte *bytes;
13462 struct dwarf2_locexpr_baton *baton;
13463 const char *name;
13464
13465 if (child_die->tag != DW_TAG_enumerator)
13466 continue;
13467
13468 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13469 if (attr == NULL)
13470 continue;
13471
13472 name = dwarf2_name (child_die, cu);
13473 if (name == NULL)
13474 name = "<anonymous enumerator>";
13475
13476 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13477 &value, &bytes, &baton);
13478 if (value < 0)
13479 {
13480 unsigned_enum = 0;
13481 flag_enum = 0;
13482 }
13483 else if ((mask & value) != 0)
13484 flag_enum = 0;
13485 else
13486 mask |= value;
13487
13488 /* If we already know that the enum type is neither unsigned, nor
13489 a flag type, no need to look at the rest of the enumerates. */
13490 if (!unsigned_enum && !flag_enum)
13491 break;
13492 }
13493
13494 if (unsigned_enum)
13495 TYPE_UNSIGNED (type) = 1;
13496 if (flag_enum)
13497 TYPE_FLAG_ENUM (type) = 1;
13498
13499 do_cleanups (old_chain);
13500 }
13501
13502 /* Given a DW_AT_enumeration_type die, set its type. We do not
13503 complete the type's fields yet, or create any symbols. */
13504
13505 static struct type *
13506 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13507 {
13508 struct objfile *objfile = cu->objfile;
13509 struct type *type;
13510 struct attribute *attr;
13511 const char *name;
13512
13513 /* If the definition of this type lives in .debug_types, read that type.
13514 Don't follow DW_AT_specification though, that will take us back up
13515 the chain and we want to go down. */
13516 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13517 if (attr)
13518 {
13519 type = get_DW_AT_signature_type (die, attr, cu);
13520
13521 /* The type's CU may not be the same as CU.
13522 Ensure TYPE is recorded with CU in die_type_hash. */
13523 return set_die_type (die, type, cu);
13524 }
13525
13526 type = alloc_type (objfile);
13527
13528 TYPE_CODE (type) = TYPE_CODE_ENUM;
13529 name = dwarf2_full_name (NULL, die, cu);
13530 if (name != NULL)
13531 TYPE_TAG_NAME (type) = name;
13532
13533 attr = dwarf2_attr (die, DW_AT_type, cu);
13534 if (attr != NULL)
13535 {
13536 struct type *underlying_type = die_type (die, cu);
13537
13538 TYPE_TARGET_TYPE (type) = underlying_type;
13539 }
13540
13541 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13542 if (attr)
13543 {
13544 TYPE_LENGTH (type) = DW_UNSND (attr);
13545 }
13546 else
13547 {
13548 TYPE_LENGTH (type) = 0;
13549 }
13550
13551 /* The enumeration DIE can be incomplete. In Ada, any type can be
13552 declared as private in the package spec, and then defined only
13553 inside the package body. Such types are known as Taft Amendment
13554 Types. When another package uses such a type, an incomplete DIE
13555 may be generated by the compiler. */
13556 if (die_is_declaration (die, cu))
13557 TYPE_STUB (type) = 1;
13558
13559 /* Finish the creation of this type by using the enum's children.
13560 We must call this even when the underlying type has been provided
13561 so that we can determine if we're looking at a "flag" enum. */
13562 update_enumeration_type_from_children (die, type, cu);
13563
13564 /* If this type has an underlying type that is not a stub, then we
13565 may use its attributes. We always use the "unsigned" attribute
13566 in this situation, because ordinarily we guess whether the type
13567 is unsigned -- but the guess can be wrong and the underlying type
13568 can tell us the reality. However, we defer to a local size
13569 attribute if one exists, because this lets the compiler override
13570 the underlying type if needed. */
13571 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13572 {
13573 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13574 if (TYPE_LENGTH (type) == 0)
13575 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13576 }
13577
13578 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13579
13580 return set_die_type (die, type, cu);
13581 }
13582
13583 /* Given a pointer to a die which begins an enumeration, process all
13584 the dies that define the members of the enumeration, and create the
13585 symbol for the enumeration type.
13586
13587 NOTE: We reverse the order of the element list. */
13588
13589 static void
13590 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13591 {
13592 struct type *this_type;
13593
13594 this_type = get_die_type (die, cu);
13595 if (this_type == NULL)
13596 this_type = read_enumeration_type (die, cu);
13597
13598 if (die->child != NULL)
13599 {
13600 struct die_info *child_die;
13601 struct symbol *sym;
13602 struct field *fields = NULL;
13603 int num_fields = 0;
13604 const char *name;
13605
13606 child_die = die->child;
13607 while (child_die && child_die->tag)
13608 {
13609 if (child_die->tag != DW_TAG_enumerator)
13610 {
13611 process_die (child_die, cu);
13612 }
13613 else
13614 {
13615 name = dwarf2_name (child_die, cu);
13616 if (name)
13617 {
13618 sym = new_symbol (child_die, this_type, cu);
13619
13620 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13621 {
13622 fields = (struct field *)
13623 xrealloc (fields,
13624 (num_fields + DW_FIELD_ALLOC_CHUNK)
13625 * sizeof (struct field));
13626 }
13627
13628 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13629 FIELD_TYPE (fields[num_fields]) = NULL;
13630 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13631 FIELD_BITSIZE (fields[num_fields]) = 0;
13632
13633 num_fields++;
13634 }
13635 }
13636
13637 child_die = sibling_die (child_die);
13638 }
13639
13640 if (num_fields)
13641 {
13642 TYPE_NFIELDS (this_type) = num_fields;
13643 TYPE_FIELDS (this_type) = (struct field *)
13644 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13645 memcpy (TYPE_FIELDS (this_type), fields,
13646 sizeof (struct field) * num_fields);
13647 xfree (fields);
13648 }
13649 }
13650
13651 /* If we are reading an enum from a .debug_types unit, and the enum
13652 is a declaration, and the enum is not the signatured type in the
13653 unit, then we do not want to add a symbol for it. Adding a
13654 symbol would in some cases obscure the true definition of the
13655 enum, giving users an incomplete type when the definition is
13656 actually available. Note that we do not want to do this for all
13657 enums which are just declarations, because C++0x allows forward
13658 enum declarations. */
13659 if (cu->per_cu->is_debug_types
13660 && die_is_declaration (die, cu))
13661 {
13662 struct signatured_type *sig_type;
13663
13664 sig_type = (struct signatured_type *) cu->per_cu;
13665 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13666 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13667 return;
13668 }
13669
13670 new_symbol (die, this_type, cu);
13671 }
13672
13673 /* Extract all information from a DW_TAG_array_type DIE and put it in
13674 the DIE's type field. For now, this only handles one dimensional
13675 arrays. */
13676
13677 static struct type *
13678 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13679 {
13680 struct objfile *objfile = cu->objfile;
13681 struct die_info *child_die;
13682 struct type *type;
13683 struct type *element_type, *range_type, *index_type;
13684 struct type **range_types = NULL;
13685 struct attribute *attr;
13686 int ndim = 0;
13687 struct cleanup *back_to;
13688 const char *name;
13689 unsigned int bit_stride = 0;
13690
13691 element_type = die_type (die, cu);
13692
13693 /* The die_type call above may have already set the type for this DIE. */
13694 type = get_die_type (die, cu);
13695 if (type)
13696 return type;
13697
13698 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13699 if (attr != NULL)
13700 bit_stride = DW_UNSND (attr) * 8;
13701
13702 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13703 if (attr != NULL)
13704 bit_stride = DW_UNSND (attr);
13705
13706 /* Irix 6.2 native cc creates array types without children for
13707 arrays with unspecified length. */
13708 if (die->child == NULL)
13709 {
13710 index_type = objfile_type (objfile)->builtin_int;
13711 range_type = create_static_range_type (NULL, index_type, 0, -1);
13712 type = create_array_type_with_stride (NULL, element_type, range_type,
13713 bit_stride);
13714 return set_die_type (die, type, cu);
13715 }
13716
13717 back_to = make_cleanup (null_cleanup, NULL);
13718 child_die = die->child;
13719 while (child_die && child_die->tag)
13720 {
13721 if (child_die->tag == DW_TAG_subrange_type)
13722 {
13723 struct type *child_type = read_type_die (child_die, cu);
13724
13725 if (child_type != NULL)
13726 {
13727 /* The range type was succesfully read. Save it for the
13728 array type creation. */
13729 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13730 {
13731 range_types = (struct type **)
13732 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13733 * sizeof (struct type *));
13734 if (ndim == 0)
13735 make_cleanup (free_current_contents, &range_types);
13736 }
13737 range_types[ndim++] = child_type;
13738 }
13739 }
13740 child_die = sibling_die (child_die);
13741 }
13742
13743 /* Dwarf2 dimensions are output from left to right, create the
13744 necessary array types in backwards order. */
13745
13746 type = element_type;
13747
13748 if (read_array_order (die, cu) == DW_ORD_col_major)
13749 {
13750 int i = 0;
13751
13752 while (i < ndim)
13753 type = create_array_type_with_stride (NULL, type, range_types[i++],
13754 bit_stride);
13755 }
13756 else
13757 {
13758 while (ndim-- > 0)
13759 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13760 bit_stride);
13761 }
13762
13763 /* Understand Dwarf2 support for vector types (like they occur on
13764 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13765 array type. This is not part of the Dwarf2/3 standard yet, but a
13766 custom vendor extension. The main difference between a regular
13767 array and the vector variant is that vectors are passed by value
13768 to functions. */
13769 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13770 if (attr)
13771 make_vector_type (type);
13772
13773 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13774 implementation may choose to implement triple vectors using this
13775 attribute. */
13776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13777 if (attr)
13778 {
13779 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13780 TYPE_LENGTH (type) = DW_UNSND (attr);
13781 else
13782 complaint (&symfile_complaints,
13783 _("DW_AT_byte_size for array type smaller "
13784 "than the total size of elements"));
13785 }
13786
13787 name = dwarf2_name (die, cu);
13788 if (name)
13789 TYPE_NAME (type) = name;
13790
13791 /* Install the type in the die. */
13792 set_die_type (die, type, cu);
13793
13794 /* set_die_type should be already done. */
13795 set_descriptive_type (type, die, cu);
13796
13797 do_cleanups (back_to);
13798
13799 return type;
13800 }
13801
13802 static enum dwarf_array_dim_ordering
13803 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13804 {
13805 struct attribute *attr;
13806
13807 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13808
13809 if (attr)
13810 return (enum dwarf_array_dim_ordering) DW_SND (attr);
13811
13812 /* GNU F77 is a special case, as at 08/2004 array type info is the
13813 opposite order to the dwarf2 specification, but data is still
13814 laid out as per normal fortran.
13815
13816 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13817 version checking. */
13818
13819 if (cu->language == language_fortran
13820 && cu->producer && strstr (cu->producer, "GNU F77"))
13821 {
13822 return DW_ORD_row_major;
13823 }
13824
13825 switch (cu->language_defn->la_array_ordering)
13826 {
13827 case array_column_major:
13828 return DW_ORD_col_major;
13829 case array_row_major:
13830 default:
13831 return DW_ORD_row_major;
13832 };
13833 }
13834
13835 /* Extract all information from a DW_TAG_set_type DIE and put it in
13836 the DIE's type field. */
13837
13838 static struct type *
13839 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13840 {
13841 struct type *domain_type, *set_type;
13842 struct attribute *attr;
13843
13844 domain_type = die_type (die, cu);
13845
13846 /* The die_type call above may have already set the type for this DIE. */
13847 set_type = get_die_type (die, cu);
13848 if (set_type)
13849 return set_type;
13850
13851 set_type = create_set_type (NULL, domain_type);
13852
13853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13854 if (attr)
13855 TYPE_LENGTH (set_type) = DW_UNSND (attr);
13856
13857 return set_die_type (die, set_type, cu);
13858 }
13859
13860 /* A helper for read_common_block that creates a locexpr baton.
13861 SYM is the symbol which we are marking as computed.
13862 COMMON_DIE is the DIE for the common block.
13863 COMMON_LOC is the location expression attribute for the common
13864 block itself.
13865 MEMBER_LOC is the location expression attribute for the particular
13866 member of the common block that we are processing.
13867 CU is the CU from which the above come. */
13868
13869 static void
13870 mark_common_block_symbol_computed (struct symbol *sym,
13871 struct die_info *common_die,
13872 struct attribute *common_loc,
13873 struct attribute *member_loc,
13874 struct dwarf2_cu *cu)
13875 {
13876 struct objfile *objfile = dwarf2_per_objfile->objfile;
13877 struct dwarf2_locexpr_baton *baton;
13878 gdb_byte *ptr;
13879 unsigned int cu_off;
13880 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13881 LONGEST offset = 0;
13882
13883 gdb_assert (common_loc && member_loc);
13884 gdb_assert (attr_form_is_block (common_loc));
13885 gdb_assert (attr_form_is_block (member_loc)
13886 || attr_form_is_constant (member_loc));
13887
13888 baton = obstack_alloc (&objfile->objfile_obstack,
13889 sizeof (struct dwarf2_locexpr_baton));
13890 baton->per_cu = cu->per_cu;
13891 gdb_assert (baton->per_cu);
13892
13893 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13894
13895 if (attr_form_is_constant (member_loc))
13896 {
13897 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13898 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13899 }
13900 else
13901 baton->size += DW_BLOCK (member_loc)->size;
13902
13903 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
13904 baton->data = ptr;
13905
13906 *ptr++ = DW_OP_call4;
13907 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13908 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13909 ptr += 4;
13910
13911 if (attr_form_is_constant (member_loc))
13912 {
13913 *ptr++ = DW_OP_addr;
13914 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13915 ptr += cu->header.addr_size;
13916 }
13917 else
13918 {
13919 /* We have to copy the data here, because DW_OP_call4 will only
13920 use a DW_AT_location attribute. */
13921 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13922 ptr += DW_BLOCK (member_loc)->size;
13923 }
13924
13925 *ptr++ = DW_OP_plus;
13926 gdb_assert (ptr - baton->data == baton->size);
13927
13928 SYMBOL_LOCATION_BATON (sym) = baton;
13929 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
13930 }
13931
13932 /* Create appropriate locally-scoped variables for all the
13933 DW_TAG_common_block entries. Also create a struct common_block
13934 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13935 is used to sepate the common blocks name namespace from regular
13936 variable names. */
13937
13938 static void
13939 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13940 {
13941 struct attribute *attr;
13942
13943 attr = dwarf2_attr (die, DW_AT_location, cu);
13944 if (attr)
13945 {
13946 /* Support the .debug_loc offsets. */
13947 if (attr_form_is_block (attr))
13948 {
13949 /* Ok. */
13950 }
13951 else if (attr_form_is_section_offset (attr))
13952 {
13953 dwarf2_complex_location_expr_complaint ();
13954 attr = NULL;
13955 }
13956 else
13957 {
13958 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13959 "common block member");
13960 attr = NULL;
13961 }
13962 }
13963
13964 if (die->child != NULL)
13965 {
13966 struct objfile *objfile = cu->objfile;
13967 struct die_info *child_die;
13968 size_t n_entries = 0, size;
13969 struct common_block *common_block;
13970 struct symbol *sym;
13971
13972 for (child_die = die->child;
13973 child_die && child_die->tag;
13974 child_die = sibling_die (child_die))
13975 ++n_entries;
13976
13977 size = (sizeof (struct common_block)
13978 + (n_entries - 1) * sizeof (struct symbol *));
13979 common_block = obstack_alloc (&objfile->objfile_obstack, size);
13980 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13981 common_block->n_entries = 0;
13982
13983 for (child_die = die->child;
13984 child_die && child_die->tag;
13985 child_die = sibling_die (child_die))
13986 {
13987 /* Create the symbol in the DW_TAG_common_block block in the current
13988 symbol scope. */
13989 sym = new_symbol (child_die, NULL, cu);
13990 if (sym != NULL)
13991 {
13992 struct attribute *member_loc;
13993
13994 common_block->contents[common_block->n_entries++] = sym;
13995
13996 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13997 cu);
13998 if (member_loc)
13999 {
14000 /* GDB has handled this for a long time, but it is
14001 not specified by DWARF. It seems to have been
14002 emitted by gfortran at least as recently as:
14003 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14004 complaint (&symfile_complaints,
14005 _("Variable in common block has "
14006 "DW_AT_data_member_location "
14007 "- DIE at 0x%x [in module %s]"),
14008 child_die->offset.sect_off,
14009 objfile_name (cu->objfile));
14010
14011 if (attr_form_is_section_offset (member_loc))
14012 dwarf2_complex_location_expr_complaint ();
14013 else if (attr_form_is_constant (member_loc)
14014 || attr_form_is_block (member_loc))
14015 {
14016 if (attr)
14017 mark_common_block_symbol_computed (sym, die, attr,
14018 member_loc, cu);
14019 }
14020 else
14021 dwarf2_complex_location_expr_complaint ();
14022 }
14023 }
14024 }
14025
14026 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14027 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14028 }
14029 }
14030
14031 /* Create a type for a C++ namespace. */
14032
14033 static struct type *
14034 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14035 {
14036 struct objfile *objfile = cu->objfile;
14037 const char *previous_prefix, *name;
14038 int is_anonymous;
14039 struct type *type;
14040
14041 /* For extensions, reuse the type of the original namespace. */
14042 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14043 {
14044 struct die_info *ext_die;
14045 struct dwarf2_cu *ext_cu = cu;
14046
14047 ext_die = dwarf2_extension (die, &ext_cu);
14048 type = read_type_die (ext_die, ext_cu);
14049
14050 /* EXT_CU may not be the same as CU.
14051 Ensure TYPE is recorded with CU in die_type_hash. */
14052 return set_die_type (die, type, cu);
14053 }
14054
14055 name = namespace_name (die, &is_anonymous, cu);
14056
14057 /* Now build the name of the current namespace. */
14058
14059 previous_prefix = determine_prefix (die, cu);
14060 if (previous_prefix[0] != '\0')
14061 name = typename_concat (&objfile->objfile_obstack,
14062 previous_prefix, name, 0, cu);
14063
14064 /* Create the type. */
14065 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
14066 objfile);
14067 TYPE_NAME (type) = name;
14068 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14069
14070 return set_die_type (die, type, cu);
14071 }
14072
14073 /* Read a namespace scope. */
14074
14075 static void
14076 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14077 {
14078 struct objfile *objfile = cu->objfile;
14079 int is_anonymous;
14080
14081 /* Add a symbol associated to this if we haven't seen the namespace
14082 before. Also, add a using directive if it's an anonymous
14083 namespace. */
14084
14085 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14086 {
14087 struct type *type;
14088
14089 type = read_type_die (die, cu);
14090 new_symbol (die, type, cu);
14091
14092 namespace_name (die, &is_anonymous, cu);
14093 if (is_anonymous)
14094 {
14095 const char *previous_prefix = determine_prefix (die, cu);
14096
14097 add_using_directive (using_directives (cu->language),
14098 previous_prefix, TYPE_NAME (type), NULL,
14099 NULL, NULL, 0, &objfile->objfile_obstack);
14100 }
14101 }
14102
14103 if (die->child != NULL)
14104 {
14105 struct die_info *child_die = die->child;
14106
14107 while (child_die && child_die->tag)
14108 {
14109 process_die (child_die, cu);
14110 child_die = sibling_die (child_die);
14111 }
14112 }
14113 }
14114
14115 /* Read a Fortran module as type. This DIE can be only a declaration used for
14116 imported module. Still we need that type as local Fortran "use ... only"
14117 declaration imports depend on the created type in determine_prefix. */
14118
14119 static struct type *
14120 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14121 {
14122 struct objfile *objfile = cu->objfile;
14123 const char *module_name;
14124 struct type *type;
14125
14126 module_name = dwarf2_name (die, cu);
14127 if (!module_name)
14128 complaint (&symfile_complaints,
14129 _("DW_TAG_module has no name, offset 0x%x"),
14130 die->offset.sect_off);
14131 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
14132
14133 /* determine_prefix uses TYPE_TAG_NAME. */
14134 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14135
14136 return set_die_type (die, type, cu);
14137 }
14138
14139 /* Read a Fortran module. */
14140
14141 static void
14142 read_module (struct die_info *die, struct dwarf2_cu *cu)
14143 {
14144 struct die_info *child_die = die->child;
14145 struct type *type;
14146
14147 type = read_type_die (die, cu);
14148 new_symbol (die, type, cu);
14149
14150 while (child_die && child_die->tag)
14151 {
14152 process_die (child_die, cu);
14153 child_die = sibling_die (child_die);
14154 }
14155 }
14156
14157 /* Return the name of the namespace represented by DIE. Set
14158 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14159 namespace. */
14160
14161 static const char *
14162 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14163 {
14164 struct die_info *current_die;
14165 const char *name = NULL;
14166
14167 /* Loop through the extensions until we find a name. */
14168
14169 for (current_die = die;
14170 current_die != NULL;
14171 current_die = dwarf2_extension (die, &cu))
14172 {
14173 /* We don't use dwarf2_name here so that we can detect the absence
14174 of a name -> anonymous namespace. */
14175 name = dwarf2_string_attr (die, DW_AT_name, cu);
14176
14177 if (name != NULL)
14178 break;
14179 }
14180
14181 /* Is it an anonymous namespace? */
14182
14183 *is_anonymous = (name == NULL);
14184 if (*is_anonymous)
14185 name = CP_ANONYMOUS_NAMESPACE_STR;
14186
14187 return name;
14188 }
14189
14190 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14191 the user defined type vector. */
14192
14193 static struct type *
14194 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14195 {
14196 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14197 struct comp_unit_head *cu_header = &cu->header;
14198 struct type *type;
14199 struct attribute *attr_byte_size;
14200 struct attribute *attr_address_class;
14201 int byte_size, addr_class;
14202 struct type *target_type;
14203
14204 target_type = die_type (die, cu);
14205
14206 /* The die_type call above may have already set the type for this DIE. */
14207 type = get_die_type (die, cu);
14208 if (type)
14209 return type;
14210
14211 type = lookup_pointer_type (target_type);
14212
14213 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14214 if (attr_byte_size)
14215 byte_size = DW_UNSND (attr_byte_size);
14216 else
14217 byte_size = cu_header->addr_size;
14218
14219 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14220 if (attr_address_class)
14221 addr_class = DW_UNSND (attr_address_class);
14222 else
14223 addr_class = DW_ADDR_none;
14224
14225 /* If the pointer size or address class is different than the
14226 default, create a type variant marked as such and set the
14227 length accordingly. */
14228 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14229 {
14230 if (gdbarch_address_class_type_flags_p (gdbarch))
14231 {
14232 int type_flags;
14233
14234 type_flags = gdbarch_address_class_type_flags
14235 (gdbarch, byte_size, addr_class);
14236 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14237 == 0);
14238 type = make_type_with_address_space (type, type_flags);
14239 }
14240 else if (TYPE_LENGTH (type) != byte_size)
14241 {
14242 complaint (&symfile_complaints,
14243 _("invalid pointer size %d"), byte_size);
14244 }
14245 else
14246 {
14247 /* Should we also complain about unhandled address classes? */
14248 }
14249 }
14250
14251 TYPE_LENGTH (type) = byte_size;
14252 return set_die_type (die, type, cu);
14253 }
14254
14255 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14256 the user defined type vector. */
14257
14258 static struct type *
14259 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14260 {
14261 struct type *type;
14262 struct type *to_type;
14263 struct type *domain;
14264
14265 to_type = die_type (die, cu);
14266 domain = die_containing_type (die, cu);
14267
14268 /* The calls above may have already set the type for this DIE. */
14269 type = get_die_type (die, cu);
14270 if (type)
14271 return type;
14272
14273 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14274 type = lookup_methodptr_type (to_type);
14275 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14276 {
14277 struct type *new_type = alloc_type (cu->objfile);
14278
14279 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14280 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14281 TYPE_VARARGS (to_type));
14282 type = lookup_methodptr_type (new_type);
14283 }
14284 else
14285 type = lookup_memberptr_type (to_type, domain);
14286
14287 return set_die_type (die, type, cu);
14288 }
14289
14290 /* Extract all information from a DW_TAG_reference_type DIE and add to
14291 the user defined type vector. */
14292
14293 static struct type *
14294 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14295 {
14296 struct comp_unit_head *cu_header = &cu->header;
14297 struct type *type, *target_type;
14298 struct attribute *attr;
14299
14300 target_type = die_type (die, cu);
14301
14302 /* The die_type call above may have already set the type for this DIE. */
14303 type = get_die_type (die, cu);
14304 if (type)
14305 return type;
14306
14307 type = lookup_reference_type (target_type);
14308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14309 if (attr)
14310 {
14311 TYPE_LENGTH (type) = DW_UNSND (attr);
14312 }
14313 else
14314 {
14315 TYPE_LENGTH (type) = cu_header->addr_size;
14316 }
14317 return set_die_type (die, type, cu);
14318 }
14319
14320 /* Add the given cv-qualifiers to the element type of the array. GCC
14321 outputs DWARF type qualifiers that apply to an array, not the
14322 element type. But GDB relies on the array element type to carry
14323 the cv-qualifiers. This mimics section 6.7.3 of the C99
14324 specification. */
14325
14326 static struct type *
14327 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14328 struct type *base_type, int cnst, int voltl)
14329 {
14330 struct type *el_type, *inner_array;
14331
14332 base_type = copy_type (base_type);
14333 inner_array = base_type;
14334
14335 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14336 {
14337 TYPE_TARGET_TYPE (inner_array) =
14338 copy_type (TYPE_TARGET_TYPE (inner_array));
14339 inner_array = TYPE_TARGET_TYPE (inner_array);
14340 }
14341
14342 el_type = TYPE_TARGET_TYPE (inner_array);
14343 cnst |= TYPE_CONST (el_type);
14344 voltl |= TYPE_VOLATILE (el_type);
14345 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14346
14347 return set_die_type (die, base_type, cu);
14348 }
14349
14350 static struct type *
14351 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14352 {
14353 struct type *base_type, *cv_type;
14354
14355 base_type = die_type (die, cu);
14356
14357 /* The die_type call above may have already set the type for this DIE. */
14358 cv_type = get_die_type (die, cu);
14359 if (cv_type)
14360 return cv_type;
14361
14362 /* In case the const qualifier is applied to an array type, the element type
14363 is so qualified, not the array type (section 6.7.3 of C99). */
14364 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14365 return add_array_cv_type (die, cu, base_type, 1, 0);
14366
14367 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14368 return set_die_type (die, cv_type, cu);
14369 }
14370
14371 static struct type *
14372 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14373 {
14374 struct type *base_type, *cv_type;
14375
14376 base_type = die_type (die, cu);
14377
14378 /* The die_type call above may have already set the type for this DIE. */
14379 cv_type = get_die_type (die, cu);
14380 if (cv_type)
14381 return cv_type;
14382
14383 /* In case the volatile qualifier is applied to an array type, the
14384 element type is so qualified, not the array type (section 6.7.3
14385 of C99). */
14386 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14387 return add_array_cv_type (die, cu, base_type, 0, 1);
14388
14389 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14390 return set_die_type (die, cv_type, cu);
14391 }
14392
14393 /* Handle DW_TAG_restrict_type. */
14394
14395 static struct type *
14396 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14397 {
14398 struct type *base_type, *cv_type;
14399
14400 base_type = die_type (die, cu);
14401
14402 /* The die_type call above may have already set the type for this DIE. */
14403 cv_type = get_die_type (die, cu);
14404 if (cv_type)
14405 return cv_type;
14406
14407 cv_type = make_restrict_type (base_type);
14408 return set_die_type (die, cv_type, cu);
14409 }
14410
14411 /* Handle DW_TAG_atomic_type. */
14412
14413 static struct type *
14414 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14415 {
14416 struct type *base_type, *cv_type;
14417
14418 base_type = die_type (die, cu);
14419
14420 /* The die_type call above may have already set the type for this DIE. */
14421 cv_type = get_die_type (die, cu);
14422 if (cv_type)
14423 return cv_type;
14424
14425 cv_type = make_atomic_type (base_type);
14426 return set_die_type (die, cv_type, cu);
14427 }
14428
14429 /* Extract all information from a DW_TAG_string_type DIE and add to
14430 the user defined type vector. It isn't really a user defined type,
14431 but it behaves like one, with other DIE's using an AT_user_def_type
14432 attribute to reference it. */
14433
14434 static struct type *
14435 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14436 {
14437 struct objfile *objfile = cu->objfile;
14438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14439 struct type *type, *range_type, *index_type, *char_type;
14440 struct attribute *attr;
14441 unsigned int length;
14442
14443 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14444 if (attr)
14445 {
14446 length = DW_UNSND (attr);
14447 }
14448 else
14449 {
14450 /* Check for the DW_AT_byte_size attribute. */
14451 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14452 if (attr)
14453 {
14454 length = DW_UNSND (attr);
14455 }
14456 else
14457 {
14458 length = 1;
14459 }
14460 }
14461
14462 index_type = objfile_type (objfile)->builtin_int;
14463 range_type = create_static_range_type (NULL, index_type, 1, length);
14464 char_type = language_string_char_type (cu->language_defn, gdbarch);
14465 type = create_string_type (NULL, char_type, range_type);
14466
14467 return set_die_type (die, type, cu);
14468 }
14469
14470 /* Assuming that DIE corresponds to a function, returns nonzero
14471 if the function is prototyped. */
14472
14473 static int
14474 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14475 {
14476 struct attribute *attr;
14477
14478 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14479 if (attr && (DW_UNSND (attr) != 0))
14480 return 1;
14481
14482 /* The DWARF standard implies that the DW_AT_prototyped attribute
14483 is only meaninful for C, but the concept also extends to other
14484 languages that allow unprototyped functions (Eg: Objective C).
14485 For all other languages, assume that functions are always
14486 prototyped. */
14487 if (cu->language != language_c
14488 && cu->language != language_objc
14489 && cu->language != language_opencl)
14490 return 1;
14491
14492 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14493 prototyped and unprototyped functions; default to prototyped,
14494 since that is more common in modern code (and RealView warns
14495 about unprototyped functions). */
14496 if (producer_is_realview (cu->producer))
14497 return 1;
14498
14499 return 0;
14500 }
14501
14502 /* Handle DIES due to C code like:
14503
14504 struct foo
14505 {
14506 int (*funcp)(int a, long l);
14507 int b;
14508 };
14509
14510 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14511
14512 static struct type *
14513 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14514 {
14515 struct objfile *objfile = cu->objfile;
14516 struct type *type; /* Type that this function returns. */
14517 struct type *ftype; /* Function that returns above type. */
14518 struct attribute *attr;
14519
14520 type = die_type (die, cu);
14521
14522 /* The die_type call above may have already set the type for this DIE. */
14523 ftype = get_die_type (die, cu);
14524 if (ftype)
14525 return ftype;
14526
14527 ftype = lookup_function_type (type);
14528
14529 if (prototyped_function_p (die, cu))
14530 TYPE_PROTOTYPED (ftype) = 1;
14531
14532 /* Store the calling convention in the type if it's available in
14533 the subroutine die. Otherwise set the calling convention to
14534 the default value DW_CC_normal. */
14535 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14536 if (attr)
14537 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14538 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14539 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14540 else
14541 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14542
14543 /* Record whether the function returns normally to its caller or not
14544 if the DWARF producer set that information. */
14545 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14546 if (attr && (DW_UNSND (attr) != 0))
14547 TYPE_NO_RETURN (ftype) = 1;
14548
14549 /* We need to add the subroutine type to the die immediately so
14550 we don't infinitely recurse when dealing with parameters
14551 declared as the same subroutine type. */
14552 set_die_type (die, ftype, cu);
14553
14554 if (die->child != NULL)
14555 {
14556 struct type *void_type = objfile_type (objfile)->builtin_void;
14557 struct die_info *child_die;
14558 int nparams, iparams;
14559
14560 /* Count the number of parameters.
14561 FIXME: GDB currently ignores vararg functions, but knows about
14562 vararg member functions. */
14563 nparams = 0;
14564 child_die = die->child;
14565 while (child_die && child_die->tag)
14566 {
14567 if (child_die->tag == DW_TAG_formal_parameter)
14568 nparams++;
14569 else if (child_die->tag == DW_TAG_unspecified_parameters)
14570 TYPE_VARARGS (ftype) = 1;
14571 child_die = sibling_die (child_die);
14572 }
14573
14574 /* Allocate storage for parameters and fill them in. */
14575 TYPE_NFIELDS (ftype) = nparams;
14576 TYPE_FIELDS (ftype) = (struct field *)
14577 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14578
14579 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14580 even if we error out during the parameters reading below. */
14581 for (iparams = 0; iparams < nparams; iparams++)
14582 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14583
14584 iparams = 0;
14585 child_die = die->child;
14586 while (child_die && child_die->tag)
14587 {
14588 if (child_die->tag == DW_TAG_formal_parameter)
14589 {
14590 struct type *arg_type;
14591
14592 /* DWARF version 2 has no clean way to discern C++
14593 static and non-static member functions. G++ helps
14594 GDB by marking the first parameter for non-static
14595 member functions (which is the this pointer) as
14596 artificial. We pass this information to
14597 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14598
14599 DWARF version 3 added DW_AT_object_pointer, which GCC
14600 4.5 does not yet generate. */
14601 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14602 if (attr)
14603 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14604 else
14605 {
14606 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14607
14608 /* GCC/43521: In java, the formal parameter
14609 "this" is sometimes not marked with DW_AT_artificial. */
14610 if (cu->language == language_java)
14611 {
14612 const char *name = dwarf2_name (child_die, cu);
14613
14614 if (name && !strcmp (name, "this"))
14615 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
14616 }
14617 }
14618 arg_type = die_type (child_die, cu);
14619
14620 /* RealView does not mark THIS as const, which the testsuite
14621 expects. GCC marks THIS as const in method definitions,
14622 but not in the class specifications (GCC PR 43053). */
14623 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14624 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14625 {
14626 int is_this = 0;
14627 struct dwarf2_cu *arg_cu = cu;
14628 const char *name = dwarf2_name (child_die, cu);
14629
14630 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14631 if (attr)
14632 {
14633 /* If the compiler emits this, use it. */
14634 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14635 is_this = 1;
14636 }
14637 else if (name && strcmp (name, "this") == 0)
14638 /* Function definitions will have the argument names. */
14639 is_this = 1;
14640 else if (name == NULL && iparams == 0)
14641 /* Declarations may not have the names, so like
14642 elsewhere in GDB, assume an artificial first
14643 argument is "this". */
14644 is_this = 1;
14645
14646 if (is_this)
14647 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14648 arg_type, 0);
14649 }
14650
14651 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14652 iparams++;
14653 }
14654 child_die = sibling_die (child_die);
14655 }
14656 }
14657
14658 return ftype;
14659 }
14660
14661 static struct type *
14662 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14663 {
14664 struct objfile *objfile = cu->objfile;
14665 const char *name = NULL;
14666 struct type *this_type, *target_type;
14667
14668 name = dwarf2_full_name (NULL, die, cu);
14669 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
14670 TYPE_FLAG_TARGET_STUB, NULL, objfile);
14671 TYPE_NAME (this_type) = name;
14672 set_die_type (die, this_type, cu);
14673 target_type = die_type (die, cu);
14674 if (target_type != this_type)
14675 TYPE_TARGET_TYPE (this_type) = target_type;
14676 else
14677 {
14678 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14679 spec and cause infinite loops in GDB. */
14680 complaint (&symfile_complaints,
14681 _("Self-referential DW_TAG_typedef "
14682 "- DIE at 0x%x [in module %s]"),
14683 die->offset.sect_off, objfile_name (objfile));
14684 TYPE_TARGET_TYPE (this_type) = NULL;
14685 }
14686 return this_type;
14687 }
14688
14689 /* Find a representation of a given base type and install
14690 it in the TYPE field of the die. */
14691
14692 static struct type *
14693 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14694 {
14695 struct objfile *objfile = cu->objfile;
14696 struct type *type;
14697 struct attribute *attr;
14698 int encoding = 0, size = 0;
14699 const char *name;
14700 enum type_code code = TYPE_CODE_INT;
14701 int type_flags = 0;
14702 struct type *target_type = NULL;
14703
14704 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14705 if (attr)
14706 {
14707 encoding = DW_UNSND (attr);
14708 }
14709 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14710 if (attr)
14711 {
14712 size = DW_UNSND (attr);
14713 }
14714 name = dwarf2_name (die, cu);
14715 if (!name)
14716 {
14717 complaint (&symfile_complaints,
14718 _("DW_AT_name missing from DW_TAG_base_type"));
14719 }
14720
14721 switch (encoding)
14722 {
14723 case DW_ATE_address:
14724 /* Turn DW_ATE_address into a void * pointer. */
14725 code = TYPE_CODE_PTR;
14726 type_flags |= TYPE_FLAG_UNSIGNED;
14727 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
14728 break;
14729 case DW_ATE_boolean:
14730 code = TYPE_CODE_BOOL;
14731 type_flags |= TYPE_FLAG_UNSIGNED;
14732 break;
14733 case DW_ATE_complex_float:
14734 code = TYPE_CODE_COMPLEX;
14735 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
14736 break;
14737 case DW_ATE_decimal_float:
14738 code = TYPE_CODE_DECFLOAT;
14739 break;
14740 case DW_ATE_float:
14741 code = TYPE_CODE_FLT;
14742 break;
14743 case DW_ATE_signed:
14744 break;
14745 case DW_ATE_unsigned:
14746 type_flags |= TYPE_FLAG_UNSIGNED;
14747 if (cu->language == language_fortran
14748 && name
14749 && startswith (name, "character("))
14750 code = TYPE_CODE_CHAR;
14751 break;
14752 case DW_ATE_signed_char:
14753 if (cu->language == language_ada || cu->language == language_m2
14754 || cu->language == language_pascal
14755 || cu->language == language_fortran)
14756 code = TYPE_CODE_CHAR;
14757 break;
14758 case DW_ATE_unsigned_char:
14759 if (cu->language == language_ada || cu->language == language_m2
14760 || cu->language == language_pascal
14761 || cu->language == language_fortran)
14762 code = TYPE_CODE_CHAR;
14763 type_flags |= TYPE_FLAG_UNSIGNED;
14764 break;
14765 case DW_ATE_UTF:
14766 /* We just treat this as an integer and then recognize the
14767 type by name elsewhere. */
14768 break;
14769
14770 default:
14771 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14772 dwarf_type_encoding_name (encoding));
14773 break;
14774 }
14775
14776 type = init_type (code, size, type_flags, NULL, objfile);
14777 TYPE_NAME (type) = name;
14778 TYPE_TARGET_TYPE (type) = target_type;
14779
14780 if (name && strcmp (name, "char") == 0)
14781 TYPE_NOSIGN (type) = 1;
14782
14783 return set_die_type (die, type, cu);
14784 }
14785
14786 /* Parse dwarf attribute if it's a block, reference or constant and put the
14787 resulting value of the attribute into struct bound_prop.
14788 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14789
14790 static int
14791 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14792 struct dwarf2_cu *cu, struct dynamic_prop *prop)
14793 {
14794 struct dwarf2_property_baton *baton;
14795 struct obstack *obstack = &cu->objfile->objfile_obstack;
14796
14797 if (attr == NULL || prop == NULL)
14798 return 0;
14799
14800 if (attr_form_is_block (attr))
14801 {
14802 baton = obstack_alloc (obstack, sizeof (*baton));
14803 baton->referenced_type = NULL;
14804 baton->locexpr.per_cu = cu->per_cu;
14805 baton->locexpr.size = DW_BLOCK (attr)->size;
14806 baton->locexpr.data = DW_BLOCK (attr)->data;
14807 prop->data.baton = baton;
14808 prop->kind = PROP_LOCEXPR;
14809 gdb_assert (prop->data.baton != NULL);
14810 }
14811 else if (attr_form_is_ref (attr))
14812 {
14813 struct dwarf2_cu *target_cu = cu;
14814 struct die_info *target_die;
14815 struct attribute *target_attr;
14816
14817 target_die = follow_die_ref (die, attr, &target_cu);
14818 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
14819 if (target_attr == NULL)
14820 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14821 target_cu);
14822 if (target_attr == NULL)
14823 return 0;
14824
14825 switch (target_attr->name)
14826 {
14827 case DW_AT_location:
14828 if (attr_form_is_section_offset (target_attr))
14829 {
14830 baton = obstack_alloc (obstack, sizeof (*baton));
14831 baton->referenced_type = die_type (target_die, target_cu);
14832 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14833 prop->data.baton = baton;
14834 prop->kind = PROP_LOCLIST;
14835 gdb_assert (prop->data.baton != NULL);
14836 }
14837 else if (attr_form_is_block (target_attr))
14838 {
14839 baton = obstack_alloc (obstack, sizeof (*baton));
14840 baton->referenced_type = die_type (target_die, target_cu);
14841 baton->locexpr.per_cu = cu->per_cu;
14842 baton->locexpr.size = DW_BLOCK (target_attr)->size;
14843 baton->locexpr.data = DW_BLOCK (target_attr)->data;
14844 prop->data.baton = baton;
14845 prop->kind = PROP_LOCEXPR;
14846 gdb_assert (prop->data.baton != NULL);
14847 }
14848 else
14849 {
14850 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14851 "dynamic property");
14852 return 0;
14853 }
14854 break;
14855 case DW_AT_data_member_location:
14856 {
14857 LONGEST offset;
14858
14859 if (!handle_data_member_location (target_die, target_cu,
14860 &offset))
14861 return 0;
14862
14863 baton = obstack_alloc (obstack, sizeof (*baton));
14864 baton->referenced_type = read_type_die (target_die->parent,
14865 target_cu);
14866 baton->offset_info.offset = offset;
14867 baton->offset_info.type = die_type (target_die, target_cu);
14868 prop->data.baton = baton;
14869 prop->kind = PROP_ADDR_OFFSET;
14870 break;
14871 }
14872 }
14873 }
14874 else if (attr_form_is_constant (attr))
14875 {
14876 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
14877 prop->kind = PROP_CONST;
14878 }
14879 else
14880 {
14881 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14882 dwarf2_name (die, cu));
14883 return 0;
14884 }
14885
14886 return 1;
14887 }
14888
14889 /* Read the given DW_AT_subrange DIE. */
14890
14891 static struct type *
14892 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14893 {
14894 struct type *base_type, *orig_base_type;
14895 struct type *range_type;
14896 struct attribute *attr;
14897 struct dynamic_prop low, high;
14898 int low_default_is_valid;
14899 int high_bound_is_count = 0;
14900 const char *name;
14901 LONGEST negative_mask;
14902
14903 orig_base_type = die_type (die, cu);
14904 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14905 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14906 creating the range type, but we use the result of check_typedef
14907 when examining properties of the type. */
14908 base_type = check_typedef (orig_base_type);
14909
14910 /* The die_type call above may have already set the type for this DIE. */
14911 range_type = get_die_type (die, cu);
14912 if (range_type)
14913 return range_type;
14914
14915 low.kind = PROP_CONST;
14916 high.kind = PROP_CONST;
14917 high.data.const_val = 0;
14918
14919 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14920 omitting DW_AT_lower_bound. */
14921 switch (cu->language)
14922 {
14923 case language_c:
14924 case language_cplus:
14925 low.data.const_val = 0;
14926 low_default_is_valid = 1;
14927 break;
14928 case language_fortran:
14929 low.data.const_val = 1;
14930 low_default_is_valid = 1;
14931 break;
14932 case language_d:
14933 case language_java:
14934 case language_objc:
14935 low.data.const_val = 0;
14936 low_default_is_valid = (cu->header.version >= 4);
14937 break;
14938 case language_ada:
14939 case language_m2:
14940 case language_pascal:
14941 low.data.const_val = 1;
14942 low_default_is_valid = (cu->header.version >= 4);
14943 break;
14944 default:
14945 low.data.const_val = 0;
14946 low_default_is_valid = 0;
14947 break;
14948 }
14949
14950 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
14951 if (attr)
14952 attr_to_dynamic_prop (attr, die, cu, &low);
14953 else if (!low_default_is_valid)
14954 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14955 "- DIE at 0x%x [in module %s]"),
14956 die->offset.sect_off, objfile_name (cu->objfile));
14957
14958 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
14959 if (!attr_to_dynamic_prop (attr, die, cu, &high))
14960 {
14961 attr = dwarf2_attr (die, DW_AT_count, cu);
14962 if (attr_to_dynamic_prop (attr, die, cu, &high))
14963 {
14964 /* If bounds are constant do the final calculation here. */
14965 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
14966 high.data.const_val = low.data.const_val + high.data.const_val - 1;
14967 else
14968 high_bound_is_count = 1;
14969 }
14970 }
14971
14972 /* Dwarf-2 specifications explicitly allows to create subrange types
14973 without specifying a base type.
14974 In that case, the base type must be set to the type of
14975 the lower bound, upper bound or count, in that order, if any of these
14976 three attributes references an object that has a type.
14977 If no base type is found, the Dwarf-2 specifications say that
14978 a signed integer type of size equal to the size of an address should
14979 be used.
14980 For the following C code: `extern char gdb_int [];'
14981 GCC produces an empty range DIE.
14982 FIXME: muller/2010-05-28: Possible references to object for low bound,
14983 high bound or count are not yet handled by this code. */
14984 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
14985 {
14986 struct objfile *objfile = cu->objfile;
14987 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14988 int addr_size = gdbarch_addr_bit (gdbarch) /8;
14989 struct type *int_type = objfile_type (objfile)->builtin_int;
14990
14991 /* Test "int", "long int", and "long long int" objfile types,
14992 and select the first one having a size above or equal to the
14993 architecture address size. */
14994 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14995 base_type = int_type;
14996 else
14997 {
14998 int_type = objfile_type (objfile)->builtin_long;
14999 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15000 base_type = int_type;
15001 else
15002 {
15003 int_type = objfile_type (objfile)->builtin_long_long;
15004 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15005 base_type = int_type;
15006 }
15007 }
15008 }
15009
15010 /* Normally, the DWARF producers are expected to use a signed
15011 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15012 But this is unfortunately not always the case, as witnessed
15013 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15014 is used instead. To work around that ambiguity, we treat
15015 the bounds as signed, and thus sign-extend their values, when
15016 the base type is signed. */
15017 negative_mask =
15018 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
15019 if (low.kind == PROP_CONST
15020 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15021 low.data.const_val |= negative_mask;
15022 if (high.kind == PROP_CONST
15023 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15024 high.data.const_val |= negative_mask;
15025
15026 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15027
15028 if (high_bound_is_count)
15029 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15030
15031 /* Ada expects an empty array on no boundary attributes. */
15032 if (attr == NULL && cu->language != language_ada)
15033 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15034
15035 name = dwarf2_name (die, cu);
15036 if (name)
15037 TYPE_NAME (range_type) = name;
15038
15039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15040 if (attr)
15041 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15042
15043 set_die_type (die, range_type, cu);
15044
15045 /* set_die_type should be already done. */
15046 set_descriptive_type (range_type, die, cu);
15047
15048 return range_type;
15049 }
15050
15051 static struct type *
15052 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15053 {
15054 struct type *type;
15055
15056 /* For now, we only support the C meaning of an unspecified type: void. */
15057
15058 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
15059 TYPE_NAME (type) = dwarf2_name (die, cu);
15060
15061 return set_die_type (die, type, cu);
15062 }
15063
15064 /* Read a single die and all its descendents. Set the die's sibling
15065 field to NULL; set other fields in the die correctly, and set all
15066 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15067 location of the info_ptr after reading all of those dies. PARENT
15068 is the parent of the die in question. */
15069
15070 static struct die_info *
15071 read_die_and_children (const struct die_reader_specs *reader,
15072 const gdb_byte *info_ptr,
15073 const gdb_byte **new_info_ptr,
15074 struct die_info *parent)
15075 {
15076 struct die_info *die;
15077 const gdb_byte *cur_ptr;
15078 int has_children;
15079
15080 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15081 if (die == NULL)
15082 {
15083 *new_info_ptr = cur_ptr;
15084 return NULL;
15085 }
15086 store_in_ref_table (die, reader->cu);
15087
15088 if (has_children)
15089 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15090 else
15091 {
15092 die->child = NULL;
15093 *new_info_ptr = cur_ptr;
15094 }
15095
15096 die->sibling = NULL;
15097 die->parent = parent;
15098 return die;
15099 }
15100
15101 /* Read a die, all of its descendents, and all of its siblings; set
15102 all of the fields of all of the dies correctly. Arguments are as
15103 in read_die_and_children. */
15104
15105 static struct die_info *
15106 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15107 const gdb_byte *info_ptr,
15108 const gdb_byte **new_info_ptr,
15109 struct die_info *parent)
15110 {
15111 struct die_info *first_die, *last_sibling;
15112 const gdb_byte *cur_ptr;
15113
15114 cur_ptr = info_ptr;
15115 first_die = last_sibling = NULL;
15116
15117 while (1)
15118 {
15119 struct die_info *die
15120 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15121
15122 if (die == NULL)
15123 {
15124 *new_info_ptr = cur_ptr;
15125 return first_die;
15126 }
15127
15128 if (!first_die)
15129 first_die = die;
15130 else
15131 last_sibling->sibling = die;
15132
15133 last_sibling = die;
15134 }
15135 }
15136
15137 /* Read a die, all of its descendents, and all of its siblings; set
15138 all of the fields of all of the dies correctly. Arguments are as
15139 in read_die_and_children.
15140 This the main entry point for reading a DIE and all its children. */
15141
15142 static struct die_info *
15143 read_die_and_siblings (const struct die_reader_specs *reader,
15144 const gdb_byte *info_ptr,
15145 const gdb_byte **new_info_ptr,
15146 struct die_info *parent)
15147 {
15148 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15149 new_info_ptr, parent);
15150
15151 if (dwarf_die_debug)
15152 {
15153 fprintf_unfiltered (gdb_stdlog,
15154 "Read die from %s@0x%x of %s:\n",
15155 get_section_name (reader->die_section),
15156 (unsigned) (info_ptr - reader->die_section->buffer),
15157 bfd_get_filename (reader->abfd));
15158 dump_die (die, dwarf_die_debug);
15159 }
15160
15161 return die;
15162 }
15163
15164 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15165 attributes.
15166 The caller is responsible for filling in the extra attributes
15167 and updating (*DIEP)->num_attrs.
15168 Set DIEP to point to a newly allocated die with its information,
15169 except for its child, sibling, and parent fields.
15170 Set HAS_CHILDREN to tell whether the die has children or not. */
15171
15172 static const gdb_byte *
15173 read_full_die_1 (const struct die_reader_specs *reader,
15174 struct die_info **diep, const gdb_byte *info_ptr,
15175 int *has_children, int num_extra_attrs)
15176 {
15177 unsigned int abbrev_number, bytes_read, i;
15178 sect_offset offset;
15179 struct abbrev_info *abbrev;
15180 struct die_info *die;
15181 struct dwarf2_cu *cu = reader->cu;
15182 bfd *abfd = reader->abfd;
15183
15184 offset.sect_off = info_ptr - reader->buffer;
15185 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15186 info_ptr += bytes_read;
15187 if (!abbrev_number)
15188 {
15189 *diep = NULL;
15190 *has_children = 0;
15191 return info_ptr;
15192 }
15193
15194 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15195 if (!abbrev)
15196 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15197 abbrev_number,
15198 bfd_get_filename (abfd));
15199
15200 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15201 die->offset = offset;
15202 die->tag = abbrev->tag;
15203 die->abbrev = abbrev_number;
15204
15205 /* Make the result usable.
15206 The caller needs to update num_attrs after adding the extra
15207 attributes. */
15208 die->num_attrs = abbrev->num_attrs;
15209
15210 for (i = 0; i < abbrev->num_attrs; ++i)
15211 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15212 info_ptr);
15213
15214 *diep = die;
15215 *has_children = abbrev->has_children;
15216 return info_ptr;
15217 }
15218
15219 /* Read a die and all its attributes.
15220 Set DIEP to point to a newly allocated die with its information,
15221 except for its child, sibling, and parent fields.
15222 Set HAS_CHILDREN to tell whether the die has children or not. */
15223
15224 static const gdb_byte *
15225 read_full_die (const struct die_reader_specs *reader,
15226 struct die_info **diep, const gdb_byte *info_ptr,
15227 int *has_children)
15228 {
15229 const gdb_byte *result;
15230
15231 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15232
15233 if (dwarf_die_debug)
15234 {
15235 fprintf_unfiltered (gdb_stdlog,
15236 "Read die from %s@0x%x of %s:\n",
15237 get_section_name (reader->die_section),
15238 (unsigned) (info_ptr - reader->die_section->buffer),
15239 bfd_get_filename (reader->abfd));
15240 dump_die (*diep, dwarf_die_debug);
15241 }
15242
15243 return result;
15244 }
15245 \f
15246 /* Abbreviation tables.
15247
15248 In DWARF version 2, the description of the debugging information is
15249 stored in a separate .debug_abbrev section. Before we read any
15250 dies from a section we read in all abbreviations and install them
15251 in a hash table. */
15252
15253 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15254
15255 static struct abbrev_info *
15256 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15257 {
15258 struct abbrev_info *abbrev;
15259
15260 abbrev = (struct abbrev_info *)
15261 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
15262 memset (abbrev, 0, sizeof (struct abbrev_info));
15263 return abbrev;
15264 }
15265
15266 /* Add an abbreviation to the table. */
15267
15268 static void
15269 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15270 unsigned int abbrev_number,
15271 struct abbrev_info *abbrev)
15272 {
15273 unsigned int hash_number;
15274
15275 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15276 abbrev->next = abbrev_table->abbrevs[hash_number];
15277 abbrev_table->abbrevs[hash_number] = abbrev;
15278 }
15279
15280 /* Look up an abbrev in the table.
15281 Returns NULL if the abbrev is not found. */
15282
15283 static struct abbrev_info *
15284 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15285 unsigned int abbrev_number)
15286 {
15287 unsigned int hash_number;
15288 struct abbrev_info *abbrev;
15289
15290 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15291 abbrev = abbrev_table->abbrevs[hash_number];
15292
15293 while (abbrev)
15294 {
15295 if (abbrev->number == abbrev_number)
15296 return abbrev;
15297 abbrev = abbrev->next;
15298 }
15299 return NULL;
15300 }
15301
15302 /* Read in an abbrev table. */
15303
15304 static struct abbrev_table *
15305 abbrev_table_read_table (struct dwarf2_section_info *section,
15306 sect_offset offset)
15307 {
15308 struct objfile *objfile = dwarf2_per_objfile->objfile;
15309 bfd *abfd = get_section_bfd_owner (section);
15310 struct abbrev_table *abbrev_table;
15311 const gdb_byte *abbrev_ptr;
15312 struct abbrev_info *cur_abbrev;
15313 unsigned int abbrev_number, bytes_read, abbrev_name;
15314 unsigned int abbrev_form;
15315 struct attr_abbrev *cur_attrs;
15316 unsigned int allocated_attrs;
15317
15318 abbrev_table = XNEW (struct abbrev_table);
15319 abbrev_table->offset = offset;
15320 obstack_init (&abbrev_table->abbrev_obstack);
15321 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
15322 (ABBREV_HASH_SIZE
15323 * sizeof (struct abbrev_info *)));
15324 memset (abbrev_table->abbrevs, 0,
15325 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15326
15327 dwarf2_read_section (objfile, section);
15328 abbrev_ptr = section->buffer + offset.sect_off;
15329 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15330 abbrev_ptr += bytes_read;
15331
15332 allocated_attrs = ATTR_ALLOC_CHUNK;
15333 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
15334
15335 /* Loop until we reach an abbrev number of 0. */
15336 while (abbrev_number)
15337 {
15338 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15339
15340 /* read in abbrev header */
15341 cur_abbrev->number = abbrev_number;
15342 cur_abbrev->tag
15343 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15344 abbrev_ptr += bytes_read;
15345 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15346 abbrev_ptr += 1;
15347
15348 /* now read in declarations */
15349 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15350 abbrev_ptr += bytes_read;
15351 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15352 abbrev_ptr += bytes_read;
15353 while (abbrev_name)
15354 {
15355 if (cur_abbrev->num_attrs == allocated_attrs)
15356 {
15357 allocated_attrs += ATTR_ALLOC_CHUNK;
15358 cur_attrs
15359 = xrealloc (cur_attrs, (allocated_attrs
15360 * sizeof (struct attr_abbrev)));
15361 }
15362
15363 cur_attrs[cur_abbrev->num_attrs].name
15364 = (enum dwarf_attribute) abbrev_name;
15365 cur_attrs[cur_abbrev->num_attrs++].form
15366 = (enum dwarf_form) abbrev_form;
15367 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15368 abbrev_ptr += bytes_read;
15369 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15370 abbrev_ptr += bytes_read;
15371 }
15372
15373 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
15374 (cur_abbrev->num_attrs
15375 * sizeof (struct attr_abbrev)));
15376 memcpy (cur_abbrev->attrs, cur_attrs,
15377 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15378
15379 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15380
15381 /* Get next abbreviation.
15382 Under Irix6 the abbreviations for a compilation unit are not
15383 always properly terminated with an abbrev number of 0.
15384 Exit loop if we encounter an abbreviation which we have
15385 already read (which means we are about to read the abbreviations
15386 for the next compile unit) or if the end of the abbreviation
15387 table is reached. */
15388 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15389 break;
15390 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15391 abbrev_ptr += bytes_read;
15392 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15393 break;
15394 }
15395
15396 xfree (cur_attrs);
15397 return abbrev_table;
15398 }
15399
15400 /* Free the resources held by ABBREV_TABLE. */
15401
15402 static void
15403 abbrev_table_free (struct abbrev_table *abbrev_table)
15404 {
15405 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15406 xfree (abbrev_table);
15407 }
15408
15409 /* Same as abbrev_table_free but as a cleanup.
15410 We pass in a pointer to the pointer to the table so that we can
15411 set the pointer to NULL when we're done. It also simplifies
15412 build_type_psymtabs_1. */
15413
15414 static void
15415 abbrev_table_free_cleanup (void *table_ptr)
15416 {
15417 struct abbrev_table **abbrev_table_ptr = table_ptr;
15418
15419 if (*abbrev_table_ptr != NULL)
15420 abbrev_table_free (*abbrev_table_ptr);
15421 *abbrev_table_ptr = NULL;
15422 }
15423
15424 /* Read the abbrev table for CU from ABBREV_SECTION. */
15425
15426 static void
15427 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15428 struct dwarf2_section_info *abbrev_section)
15429 {
15430 cu->abbrev_table =
15431 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15432 }
15433
15434 /* Release the memory used by the abbrev table for a compilation unit. */
15435
15436 static void
15437 dwarf2_free_abbrev_table (void *ptr_to_cu)
15438 {
15439 struct dwarf2_cu *cu = ptr_to_cu;
15440
15441 if (cu->abbrev_table != NULL)
15442 abbrev_table_free (cu->abbrev_table);
15443 /* Set this to NULL so that we SEGV if we try to read it later,
15444 and also because free_comp_unit verifies this is NULL. */
15445 cu->abbrev_table = NULL;
15446 }
15447 \f
15448 /* Returns nonzero if TAG represents a type that we might generate a partial
15449 symbol for. */
15450
15451 static int
15452 is_type_tag_for_partial (int tag)
15453 {
15454 switch (tag)
15455 {
15456 #if 0
15457 /* Some types that would be reasonable to generate partial symbols for,
15458 that we don't at present. */
15459 case DW_TAG_array_type:
15460 case DW_TAG_file_type:
15461 case DW_TAG_ptr_to_member_type:
15462 case DW_TAG_set_type:
15463 case DW_TAG_string_type:
15464 case DW_TAG_subroutine_type:
15465 #endif
15466 case DW_TAG_base_type:
15467 case DW_TAG_class_type:
15468 case DW_TAG_interface_type:
15469 case DW_TAG_enumeration_type:
15470 case DW_TAG_structure_type:
15471 case DW_TAG_subrange_type:
15472 case DW_TAG_typedef:
15473 case DW_TAG_union_type:
15474 return 1;
15475 default:
15476 return 0;
15477 }
15478 }
15479
15480 /* Load all DIEs that are interesting for partial symbols into memory. */
15481
15482 static struct partial_die_info *
15483 load_partial_dies (const struct die_reader_specs *reader,
15484 const gdb_byte *info_ptr, int building_psymtab)
15485 {
15486 struct dwarf2_cu *cu = reader->cu;
15487 struct objfile *objfile = cu->objfile;
15488 struct partial_die_info *part_die;
15489 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15490 struct abbrev_info *abbrev;
15491 unsigned int bytes_read;
15492 unsigned int load_all = 0;
15493 int nesting_level = 1;
15494
15495 parent_die = NULL;
15496 last_die = NULL;
15497
15498 gdb_assert (cu->per_cu != NULL);
15499 if (cu->per_cu->load_all_dies)
15500 load_all = 1;
15501
15502 cu->partial_dies
15503 = htab_create_alloc_ex (cu->header.length / 12,
15504 partial_die_hash,
15505 partial_die_eq,
15506 NULL,
15507 &cu->comp_unit_obstack,
15508 hashtab_obstack_allocate,
15509 dummy_obstack_deallocate);
15510
15511 part_die = obstack_alloc (&cu->comp_unit_obstack,
15512 sizeof (struct partial_die_info));
15513
15514 while (1)
15515 {
15516 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15517
15518 /* A NULL abbrev means the end of a series of children. */
15519 if (abbrev == NULL)
15520 {
15521 if (--nesting_level == 0)
15522 {
15523 /* PART_DIE was probably the last thing allocated on the
15524 comp_unit_obstack, so we could call obstack_free
15525 here. We don't do that because the waste is small,
15526 and will be cleaned up when we're done with this
15527 compilation unit. This way, we're also more robust
15528 against other users of the comp_unit_obstack. */
15529 return first_die;
15530 }
15531 info_ptr += bytes_read;
15532 last_die = parent_die;
15533 parent_die = parent_die->die_parent;
15534 continue;
15535 }
15536
15537 /* Check for template arguments. We never save these; if
15538 they're seen, we just mark the parent, and go on our way. */
15539 if (parent_die != NULL
15540 && cu->language == language_cplus
15541 && (abbrev->tag == DW_TAG_template_type_param
15542 || abbrev->tag == DW_TAG_template_value_param))
15543 {
15544 parent_die->has_template_arguments = 1;
15545
15546 if (!load_all)
15547 {
15548 /* We don't need a partial DIE for the template argument. */
15549 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15550 continue;
15551 }
15552 }
15553
15554 /* We only recurse into c++ subprograms looking for template arguments.
15555 Skip their other children. */
15556 if (!load_all
15557 && cu->language == language_cplus
15558 && parent_die != NULL
15559 && parent_die->tag == DW_TAG_subprogram)
15560 {
15561 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15562 continue;
15563 }
15564
15565 /* Check whether this DIE is interesting enough to save. Normally
15566 we would not be interested in members here, but there may be
15567 later variables referencing them via DW_AT_specification (for
15568 static members). */
15569 if (!load_all
15570 && !is_type_tag_for_partial (abbrev->tag)
15571 && abbrev->tag != DW_TAG_constant
15572 && abbrev->tag != DW_TAG_enumerator
15573 && abbrev->tag != DW_TAG_subprogram
15574 && abbrev->tag != DW_TAG_lexical_block
15575 && abbrev->tag != DW_TAG_variable
15576 && abbrev->tag != DW_TAG_namespace
15577 && abbrev->tag != DW_TAG_module
15578 && abbrev->tag != DW_TAG_member
15579 && abbrev->tag != DW_TAG_imported_unit
15580 && abbrev->tag != DW_TAG_imported_declaration)
15581 {
15582 /* Otherwise we skip to the next sibling, if any. */
15583 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15584 continue;
15585 }
15586
15587 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15588 info_ptr);
15589
15590 /* This two-pass algorithm for processing partial symbols has a
15591 high cost in cache pressure. Thus, handle some simple cases
15592 here which cover the majority of C partial symbols. DIEs
15593 which neither have specification tags in them, nor could have
15594 specification tags elsewhere pointing at them, can simply be
15595 processed and discarded.
15596
15597 This segment is also optional; scan_partial_symbols and
15598 add_partial_symbol will handle these DIEs if we chain
15599 them in normally. When compilers which do not emit large
15600 quantities of duplicate debug information are more common,
15601 this code can probably be removed. */
15602
15603 /* Any complete simple types at the top level (pretty much all
15604 of them, for a language without namespaces), can be processed
15605 directly. */
15606 if (parent_die == NULL
15607 && part_die->has_specification == 0
15608 && part_die->is_declaration == 0
15609 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15610 || part_die->tag == DW_TAG_base_type
15611 || part_die->tag == DW_TAG_subrange_type))
15612 {
15613 if (building_psymtab && part_die->name != NULL)
15614 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15615 VAR_DOMAIN, LOC_TYPEDEF,
15616 &objfile->static_psymbols,
15617 0, cu->language, objfile);
15618 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15619 continue;
15620 }
15621
15622 /* The exception for DW_TAG_typedef with has_children above is
15623 a workaround of GCC PR debug/47510. In the case of this complaint
15624 type_name_no_tag_or_error will error on such types later.
15625
15626 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15627 it could not find the child DIEs referenced later, this is checked
15628 above. In correct DWARF DW_TAG_typedef should have no children. */
15629
15630 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15631 complaint (&symfile_complaints,
15632 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15633 "- DIE at 0x%x [in module %s]"),
15634 part_die->offset.sect_off, objfile_name (objfile));
15635
15636 /* If we're at the second level, and we're an enumerator, and
15637 our parent has no specification (meaning possibly lives in a
15638 namespace elsewhere), then we can add the partial symbol now
15639 instead of queueing it. */
15640 if (part_die->tag == DW_TAG_enumerator
15641 && parent_die != NULL
15642 && parent_die->die_parent == NULL
15643 && parent_die->tag == DW_TAG_enumeration_type
15644 && parent_die->has_specification == 0)
15645 {
15646 if (part_die->name == NULL)
15647 complaint (&symfile_complaints,
15648 _("malformed enumerator DIE ignored"));
15649 else if (building_psymtab)
15650 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15651 VAR_DOMAIN, LOC_CONST,
15652 (cu->language == language_cplus
15653 || cu->language == language_java)
15654 ? &objfile->global_psymbols
15655 : &objfile->static_psymbols,
15656 0, cu->language, objfile);
15657
15658 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15659 continue;
15660 }
15661
15662 /* We'll save this DIE so link it in. */
15663 part_die->die_parent = parent_die;
15664 part_die->die_sibling = NULL;
15665 part_die->die_child = NULL;
15666
15667 if (last_die && last_die == parent_die)
15668 last_die->die_child = part_die;
15669 else if (last_die)
15670 last_die->die_sibling = part_die;
15671
15672 last_die = part_die;
15673
15674 if (first_die == NULL)
15675 first_die = part_die;
15676
15677 /* Maybe add the DIE to the hash table. Not all DIEs that we
15678 find interesting need to be in the hash table, because we
15679 also have the parent/sibling/child chains; only those that we
15680 might refer to by offset later during partial symbol reading.
15681
15682 For now this means things that might have be the target of a
15683 DW_AT_specification, DW_AT_abstract_origin, or
15684 DW_AT_extension. DW_AT_extension will refer only to
15685 namespaces; DW_AT_abstract_origin refers to functions (and
15686 many things under the function DIE, but we do not recurse
15687 into function DIEs during partial symbol reading) and
15688 possibly variables as well; DW_AT_specification refers to
15689 declarations. Declarations ought to have the DW_AT_declaration
15690 flag. It happens that GCC forgets to put it in sometimes, but
15691 only for functions, not for types.
15692
15693 Adding more things than necessary to the hash table is harmless
15694 except for the performance cost. Adding too few will result in
15695 wasted time in find_partial_die, when we reread the compilation
15696 unit with load_all_dies set. */
15697
15698 if (load_all
15699 || abbrev->tag == DW_TAG_constant
15700 || abbrev->tag == DW_TAG_subprogram
15701 || abbrev->tag == DW_TAG_variable
15702 || abbrev->tag == DW_TAG_namespace
15703 || part_die->is_declaration)
15704 {
15705 void **slot;
15706
15707 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
15708 part_die->offset.sect_off, INSERT);
15709 *slot = part_die;
15710 }
15711
15712 part_die = obstack_alloc (&cu->comp_unit_obstack,
15713 sizeof (struct partial_die_info));
15714
15715 /* For some DIEs we want to follow their children (if any). For C
15716 we have no reason to follow the children of structures; for other
15717 languages we have to, so that we can get at method physnames
15718 to infer fully qualified class names, for DW_AT_specification,
15719 and for C++ template arguments. For C++, we also look one level
15720 inside functions to find template arguments (if the name of the
15721 function does not already contain the template arguments).
15722
15723 For Ada, we need to scan the children of subprograms and lexical
15724 blocks as well because Ada allows the definition of nested
15725 entities that could be interesting for the debugger, such as
15726 nested subprograms for instance. */
15727 if (last_die->has_children
15728 && (load_all
15729 || last_die->tag == DW_TAG_namespace
15730 || last_die->tag == DW_TAG_module
15731 || last_die->tag == DW_TAG_enumeration_type
15732 || (cu->language == language_cplus
15733 && last_die->tag == DW_TAG_subprogram
15734 && (last_die->name == NULL
15735 || strchr (last_die->name, '<') == NULL))
15736 || (cu->language != language_c
15737 && (last_die->tag == DW_TAG_class_type
15738 || last_die->tag == DW_TAG_interface_type
15739 || last_die->tag == DW_TAG_structure_type
15740 || last_die->tag == DW_TAG_union_type))
15741 || (cu->language == language_ada
15742 && (last_die->tag == DW_TAG_subprogram
15743 || last_die->tag == DW_TAG_lexical_block))))
15744 {
15745 nesting_level++;
15746 parent_die = last_die;
15747 continue;
15748 }
15749
15750 /* Otherwise we skip to the next sibling, if any. */
15751 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
15752
15753 /* Back to the top, do it again. */
15754 }
15755 }
15756
15757 /* Read a minimal amount of information into the minimal die structure. */
15758
15759 static const gdb_byte *
15760 read_partial_die (const struct die_reader_specs *reader,
15761 struct partial_die_info *part_die,
15762 struct abbrev_info *abbrev, unsigned int abbrev_len,
15763 const gdb_byte *info_ptr)
15764 {
15765 struct dwarf2_cu *cu = reader->cu;
15766 struct objfile *objfile = cu->objfile;
15767 const gdb_byte *buffer = reader->buffer;
15768 unsigned int i;
15769 struct attribute attr;
15770 int has_low_pc_attr = 0;
15771 int has_high_pc_attr = 0;
15772 int high_pc_relative = 0;
15773
15774 memset (part_die, 0, sizeof (struct partial_die_info));
15775
15776 part_die->offset.sect_off = info_ptr - buffer;
15777
15778 info_ptr += abbrev_len;
15779
15780 if (abbrev == NULL)
15781 return info_ptr;
15782
15783 part_die->tag = abbrev->tag;
15784 part_die->has_children = abbrev->has_children;
15785
15786 for (i = 0; i < abbrev->num_attrs; ++i)
15787 {
15788 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15789
15790 /* Store the data if it is of an attribute we want to keep in a
15791 partial symbol table. */
15792 switch (attr.name)
15793 {
15794 case DW_AT_name:
15795 switch (part_die->tag)
15796 {
15797 case DW_TAG_compile_unit:
15798 case DW_TAG_partial_unit:
15799 case DW_TAG_type_unit:
15800 /* Compilation units have a DW_AT_name that is a filename, not
15801 a source language identifier. */
15802 case DW_TAG_enumeration_type:
15803 case DW_TAG_enumerator:
15804 /* These tags always have simple identifiers already; no need
15805 to canonicalize them. */
15806 part_die->name = DW_STRING (&attr);
15807 break;
15808 default:
15809 part_die->name
15810 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
15811 &objfile->per_bfd->storage_obstack);
15812 break;
15813 }
15814 break;
15815 case DW_AT_linkage_name:
15816 case DW_AT_MIPS_linkage_name:
15817 /* Note that both forms of linkage name might appear. We
15818 assume they will be the same, and we only store the last
15819 one we see. */
15820 if (cu->language == language_ada)
15821 part_die->name = DW_STRING (&attr);
15822 part_die->linkage_name = DW_STRING (&attr);
15823 break;
15824 case DW_AT_low_pc:
15825 has_low_pc_attr = 1;
15826 part_die->lowpc = attr_value_as_address (&attr);
15827 break;
15828 case DW_AT_high_pc:
15829 has_high_pc_attr = 1;
15830 part_die->highpc = attr_value_as_address (&attr);
15831 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
15832 high_pc_relative = 1;
15833 break;
15834 case DW_AT_location:
15835 /* Support the .debug_loc offsets. */
15836 if (attr_form_is_block (&attr))
15837 {
15838 part_die->d.locdesc = DW_BLOCK (&attr);
15839 }
15840 else if (attr_form_is_section_offset (&attr))
15841 {
15842 dwarf2_complex_location_expr_complaint ();
15843 }
15844 else
15845 {
15846 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15847 "partial symbol information");
15848 }
15849 break;
15850 case DW_AT_external:
15851 part_die->is_external = DW_UNSND (&attr);
15852 break;
15853 case DW_AT_declaration:
15854 part_die->is_declaration = DW_UNSND (&attr);
15855 break;
15856 case DW_AT_type:
15857 part_die->has_type = 1;
15858 break;
15859 case DW_AT_abstract_origin:
15860 case DW_AT_specification:
15861 case DW_AT_extension:
15862 part_die->has_specification = 1;
15863 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
15864 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15865 || cu->per_cu->is_dwz);
15866 break;
15867 case DW_AT_sibling:
15868 /* Ignore absolute siblings, they might point outside of
15869 the current compile unit. */
15870 if (attr.form == DW_FORM_ref_addr)
15871 complaint (&symfile_complaints,
15872 _("ignoring absolute DW_AT_sibling"));
15873 else
15874 {
15875 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15876 const gdb_byte *sibling_ptr = buffer + off;
15877
15878 if (sibling_ptr < info_ptr)
15879 complaint (&symfile_complaints,
15880 _("DW_AT_sibling points backwards"));
15881 else if (sibling_ptr > reader->buffer_end)
15882 dwarf2_section_buffer_overflow_complaint (reader->die_section);
15883 else
15884 part_die->sibling = sibling_ptr;
15885 }
15886 break;
15887 case DW_AT_byte_size:
15888 part_die->has_byte_size = 1;
15889 break;
15890 case DW_AT_const_value:
15891 part_die->has_const_value = 1;
15892 break;
15893 case DW_AT_calling_convention:
15894 /* DWARF doesn't provide a way to identify a program's source-level
15895 entry point. DW_AT_calling_convention attributes are only meant
15896 to describe functions' calling conventions.
15897
15898 However, because it's a necessary piece of information in
15899 Fortran, and because DW_CC_program is the only piece of debugging
15900 information whose definition refers to a 'main program' at all,
15901 several compilers have begun marking Fortran main programs with
15902 DW_CC_program --- even when those functions use the standard
15903 calling conventions.
15904
15905 So until DWARF specifies a way to provide this information and
15906 compilers pick up the new representation, we'll support this
15907 practice. */
15908 if (DW_UNSND (&attr) == DW_CC_program
15909 && cu->language == language_fortran)
15910 set_objfile_main_name (objfile, part_die->name, language_fortran);
15911 break;
15912 case DW_AT_inline:
15913 if (DW_UNSND (&attr) == DW_INL_inlined
15914 || DW_UNSND (&attr) == DW_INL_declared_inlined)
15915 part_die->may_be_inlined = 1;
15916 break;
15917
15918 case DW_AT_import:
15919 if (part_die->tag == DW_TAG_imported_unit)
15920 {
15921 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15922 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15923 || cu->per_cu->is_dwz);
15924 }
15925 break;
15926
15927 default:
15928 break;
15929 }
15930 }
15931
15932 if (high_pc_relative)
15933 part_die->highpc += part_die->lowpc;
15934
15935 if (has_low_pc_attr && has_high_pc_attr)
15936 {
15937 /* When using the GNU linker, .gnu.linkonce. sections are used to
15938 eliminate duplicate copies of functions and vtables and such.
15939 The linker will arbitrarily choose one and discard the others.
15940 The AT_*_pc values for such functions refer to local labels in
15941 these sections. If the section from that file was discarded, the
15942 labels are not in the output, so the relocs get a value of 0.
15943 If this is a discarded function, mark the pc bounds as invalid,
15944 so that GDB will ignore it. */
15945 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15946 {
15947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15948
15949 complaint (&symfile_complaints,
15950 _("DW_AT_low_pc %s is zero "
15951 "for DIE at 0x%x [in module %s]"),
15952 paddress (gdbarch, part_die->lowpc),
15953 part_die->offset.sect_off, objfile_name (objfile));
15954 }
15955 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15956 else if (part_die->lowpc >= part_die->highpc)
15957 {
15958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15959
15960 complaint (&symfile_complaints,
15961 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15962 "for DIE at 0x%x [in module %s]"),
15963 paddress (gdbarch, part_die->lowpc),
15964 paddress (gdbarch, part_die->highpc),
15965 part_die->offset.sect_off, objfile_name (objfile));
15966 }
15967 else
15968 part_die->has_pc_info = 1;
15969 }
15970
15971 return info_ptr;
15972 }
15973
15974 /* Find a cached partial DIE at OFFSET in CU. */
15975
15976 static struct partial_die_info *
15977 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
15978 {
15979 struct partial_die_info *lookup_die = NULL;
15980 struct partial_die_info part_die;
15981
15982 part_die.offset = offset;
15983 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
15984 offset.sect_off);
15985
15986 return lookup_die;
15987 }
15988
15989 /* Find a partial DIE at OFFSET, which may or may not be in CU,
15990 except in the case of .debug_types DIEs which do not reference
15991 outside their CU (they do however referencing other types via
15992 DW_FORM_ref_sig8). */
15993
15994 static struct partial_die_info *
15995 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
15996 {
15997 struct objfile *objfile = cu->objfile;
15998 struct dwarf2_per_cu_data *per_cu = NULL;
15999 struct partial_die_info *pd = NULL;
16000
16001 if (offset_in_dwz == cu->per_cu->is_dwz
16002 && offset_in_cu_p (&cu->header, offset))
16003 {
16004 pd = find_partial_die_in_comp_unit (offset, cu);
16005 if (pd != NULL)
16006 return pd;
16007 /* We missed recording what we needed.
16008 Load all dies and try again. */
16009 per_cu = cu->per_cu;
16010 }
16011 else
16012 {
16013 /* TUs don't reference other CUs/TUs (except via type signatures). */
16014 if (cu->per_cu->is_debug_types)
16015 {
16016 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16017 " external reference to offset 0x%lx [in module %s].\n"),
16018 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16019 bfd_get_filename (objfile->obfd));
16020 }
16021 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16022 objfile);
16023
16024 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16025 load_partial_comp_unit (per_cu);
16026
16027 per_cu->cu->last_used = 0;
16028 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16029 }
16030
16031 /* If we didn't find it, and not all dies have been loaded,
16032 load them all and try again. */
16033
16034 if (pd == NULL && per_cu->load_all_dies == 0)
16035 {
16036 per_cu->load_all_dies = 1;
16037
16038 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16039 THIS_CU->cu may already be in use. So we can't just free it and
16040 replace its DIEs with the ones we read in. Instead, we leave those
16041 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16042 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16043 set. */
16044 load_partial_comp_unit (per_cu);
16045
16046 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16047 }
16048
16049 if (pd == NULL)
16050 internal_error (__FILE__, __LINE__,
16051 _("could not find partial DIE 0x%x "
16052 "in cache [from module %s]\n"),
16053 offset.sect_off, bfd_get_filename (objfile->obfd));
16054 return pd;
16055 }
16056
16057 /* See if we can figure out if the class lives in a namespace. We do
16058 this by looking for a member function; its demangled name will
16059 contain namespace info, if there is any. */
16060
16061 static void
16062 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16063 struct dwarf2_cu *cu)
16064 {
16065 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16066 what template types look like, because the demangler
16067 frequently doesn't give the same name as the debug info. We
16068 could fix this by only using the demangled name to get the
16069 prefix (but see comment in read_structure_type). */
16070
16071 struct partial_die_info *real_pdi;
16072 struct partial_die_info *child_pdi;
16073
16074 /* If this DIE (this DIE's specification, if any) has a parent, then
16075 we should not do this. We'll prepend the parent's fully qualified
16076 name when we create the partial symbol. */
16077
16078 real_pdi = struct_pdi;
16079 while (real_pdi->has_specification)
16080 real_pdi = find_partial_die (real_pdi->spec_offset,
16081 real_pdi->spec_is_dwz, cu);
16082
16083 if (real_pdi->die_parent != NULL)
16084 return;
16085
16086 for (child_pdi = struct_pdi->die_child;
16087 child_pdi != NULL;
16088 child_pdi = child_pdi->die_sibling)
16089 {
16090 if (child_pdi->tag == DW_TAG_subprogram
16091 && child_pdi->linkage_name != NULL)
16092 {
16093 char *actual_class_name
16094 = language_class_name_from_physname (cu->language_defn,
16095 child_pdi->linkage_name);
16096 if (actual_class_name != NULL)
16097 {
16098 struct_pdi->name
16099 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16100 actual_class_name,
16101 strlen (actual_class_name));
16102 xfree (actual_class_name);
16103 }
16104 break;
16105 }
16106 }
16107 }
16108
16109 /* Adjust PART_DIE before generating a symbol for it. This function
16110 may set the is_external flag or change the DIE's name. */
16111
16112 static void
16113 fixup_partial_die (struct partial_die_info *part_die,
16114 struct dwarf2_cu *cu)
16115 {
16116 /* Once we've fixed up a die, there's no point in doing so again.
16117 This also avoids a memory leak if we were to call
16118 guess_partial_die_structure_name multiple times. */
16119 if (part_die->fixup_called)
16120 return;
16121
16122 /* If we found a reference attribute and the DIE has no name, try
16123 to find a name in the referred to DIE. */
16124
16125 if (part_die->name == NULL && part_die->has_specification)
16126 {
16127 struct partial_die_info *spec_die;
16128
16129 spec_die = find_partial_die (part_die->spec_offset,
16130 part_die->spec_is_dwz, cu);
16131
16132 fixup_partial_die (spec_die, cu);
16133
16134 if (spec_die->name)
16135 {
16136 part_die->name = spec_die->name;
16137
16138 /* Copy DW_AT_external attribute if it is set. */
16139 if (spec_die->is_external)
16140 part_die->is_external = spec_die->is_external;
16141 }
16142 }
16143
16144 /* Set default names for some unnamed DIEs. */
16145
16146 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16147 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16148
16149 /* If there is no parent die to provide a namespace, and there are
16150 children, see if we can determine the namespace from their linkage
16151 name. */
16152 if (cu->language == language_cplus
16153 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16154 && part_die->die_parent == NULL
16155 && part_die->has_children
16156 && (part_die->tag == DW_TAG_class_type
16157 || part_die->tag == DW_TAG_structure_type
16158 || part_die->tag == DW_TAG_union_type))
16159 guess_partial_die_structure_name (part_die, cu);
16160
16161 /* GCC might emit a nameless struct or union that has a linkage
16162 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16163 if (part_die->name == NULL
16164 && (part_die->tag == DW_TAG_class_type
16165 || part_die->tag == DW_TAG_interface_type
16166 || part_die->tag == DW_TAG_structure_type
16167 || part_die->tag == DW_TAG_union_type)
16168 && part_die->linkage_name != NULL)
16169 {
16170 char *demangled;
16171
16172 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16173 if (demangled)
16174 {
16175 const char *base;
16176
16177 /* Strip any leading namespaces/classes, keep only the base name.
16178 DW_AT_name for named DIEs does not contain the prefixes. */
16179 base = strrchr (demangled, ':');
16180 if (base && base > demangled && base[-1] == ':')
16181 base++;
16182 else
16183 base = demangled;
16184
16185 part_die->name
16186 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16187 base, strlen (base));
16188 xfree (demangled);
16189 }
16190 }
16191
16192 part_die->fixup_called = 1;
16193 }
16194
16195 /* Read an attribute value described by an attribute form. */
16196
16197 static const gdb_byte *
16198 read_attribute_value (const struct die_reader_specs *reader,
16199 struct attribute *attr, unsigned form,
16200 const gdb_byte *info_ptr)
16201 {
16202 struct dwarf2_cu *cu = reader->cu;
16203 struct objfile *objfile = cu->objfile;
16204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16205 bfd *abfd = reader->abfd;
16206 struct comp_unit_head *cu_header = &cu->header;
16207 unsigned int bytes_read;
16208 struct dwarf_block *blk;
16209
16210 attr->form = (enum dwarf_form) form;
16211 switch (form)
16212 {
16213 case DW_FORM_ref_addr:
16214 if (cu->header.version == 2)
16215 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16216 else
16217 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16218 &cu->header, &bytes_read);
16219 info_ptr += bytes_read;
16220 break;
16221 case DW_FORM_GNU_ref_alt:
16222 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16223 info_ptr += bytes_read;
16224 break;
16225 case DW_FORM_addr:
16226 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16227 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16228 info_ptr += bytes_read;
16229 break;
16230 case DW_FORM_block2:
16231 blk = dwarf_alloc_block (cu);
16232 blk->size = read_2_bytes (abfd, info_ptr);
16233 info_ptr += 2;
16234 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16235 info_ptr += blk->size;
16236 DW_BLOCK (attr) = blk;
16237 break;
16238 case DW_FORM_block4:
16239 blk = dwarf_alloc_block (cu);
16240 blk->size = read_4_bytes (abfd, info_ptr);
16241 info_ptr += 4;
16242 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16243 info_ptr += blk->size;
16244 DW_BLOCK (attr) = blk;
16245 break;
16246 case DW_FORM_data2:
16247 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16248 info_ptr += 2;
16249 break;
16250 case DW_FORM_data4:
16251 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16252 info_ptr += 4;
16253 break;
16254 case DW_FORM_data8:
16255 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16256 info_ptr += 8;
16257 break;
16258 case DW_FORM_sec_offset:
16259 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16260 info_ptr += bytes_read;
16261 break;
16262 case DW_FORM_string:
16263 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16264 DW_STRING_IS_CANONICAL (attr) = 0;
16265 info_ptr += bytes_read;
16266 break;
16267 case DW_FORM_strp:
16268 if (!cu->per_cu->is_dwz)
16269 {
16270 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16271 &bytes_read);
16272 DW_STRING_IS_CANONICAL (attr) = 0;
16273 info_ptr += bytes_read;
16274 break;
16275 }
16276 /* FALLTHROUGH */
16277 case DW_FORM_GNU_strp_alt:
16278 {
16279 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16280 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16281 &bytes_read);
16282
16283 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16284 DW_STRING_IS_CANONICAL (attr) = 0;
16285 info_ptr += bytes_read;
16286 }
16287 break;
16288 case DW_FORM_exprloc:
16289 case DW_FORM_block:
16290 blk = dwarf_alloc_block (cu);
16291 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16292 info_ptr += bytes_read;
16293 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16294 info_ptr += blk->size;
16295 DW_BLOCK (attr) = blk;
16296 break;
16297 case DW_FORM_block1:
16298 blk = dwarf_alloc_block (cu);
16299 blk->size = read_1_byte (abfd, info_ptr);
16300 info_ptr += 1;
16301 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16302 info_ptr += blk->size;
16303 DW_BLOCK (attr) = blk;
16304 break;
16305 case DW_FORM_data1:
16306 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16307 info_ptr += 1;
16308 break;
16309 case DW_FORM_flag:
16310 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16311 info_ptr += 1;
16312 break;
16313 case DW_FORM_flag_present:
16314 DW_UNSND (attr) = 1;
16315 break;
16316 case DW_FORM_sdata:
16317 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16318 info_ptr += bytes_read;
16319 break;
16320 case DW_FORM_udata:
16321 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16322 info_ptr += bytes_read;
16323 break;
16324 case DW_FORM_ref1:
16325 DW_UNSND (attr) = (cu->header.offset.sect_off
16326 + read_1_byte (abfd, info_ptr));
16327 info_ptr += 1;
16328 break;
16329 case DW_FORM_ref2:
16330 DW_UNSND (attr) = (cu->header.offset.sect_off
16331 + read_2_bytes (abfd, info_ptr));
16332 info_ptr += 2;
16333 break;
16334 case DW_FORM_ref4:
16335 DW_UNSND (attr) = (cu->header.offset.sect_off
16336 + read_4_bytes (abfd, info_ptr));
16337 info_ptr += 4;
16338 break;
16339 case DW_FORM_ref8:
16340 DW_UNSND (attr) = (cu->header.offset.sect_off
16341 + read_8_bytes (abfd, info_ptr));
16342 info_ptr += 8;
16343 break;
16344 case DW_FORM_ref_sig8:
16345 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16346 info_ptr += 8;
16347 break;
16348 case DW_FORM_ref_udata:
16349 DW_UNSND (attr) = (cu->header.offset.sect_off
16350 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16351 info_ptr += bytes_read;
16352 break;
16353 case DW_FORM_indirect:
16354 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16355 info_ptr += bytes_read;
16356 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
16357 break;
16358 case DW_FORM_GNU_addr_index:
16359 if (reader->dwo_file == NULL)
16360 {
16361 /* For now flag a hard error.
16362 Later we can turn this into a complaint. */
16363 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16364 dwarf_form_name (form),
16365 bfd_get_filename (abfd));
16366 }
16367 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16368 info_ptr += bytes_read;
16369 break;
16370 case DW_FORM_GNU_str_index:
16371 if (reader->dwo_file == NULL)
16372 {
16373 /* For now flag a hard error.
16374 Later we can turn this into a complaint if warranted. */
16375 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16376 dwarf_form_name (form),
16377 bfd_get_filename (abfd));
16378 }
16379 {
16380 ULONGEST str_index =
16381 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16382
16383 DW_STRING (attr) = read_str_index (reader, str_index);
16384 DW_STRING_IS_CANONICAL (attr) = 0;
16385 info_ptr += bytes_read;
16386 }
16387 break;
16388 default:
16389 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16390 dwarf_form_name (form),
16391 bfd_get_filename (abfd));
16392 }
16393
16394 /* Super hack. */
16395 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16396 attr->form = DW_FORM_GNU_ref_alt;
16397
16398 /* We have seen instances where the compiler tried to emit a byte
16399 size attribute of -1 which ended up being encoded as an unsigned
16400 0xffffffff. Although 0xffffffff is technically a valid size value,
16401 an object of this size seems pretty unlikely so we can relatively
16402 safely treat these cases as if the size attribute was invalid and
16403 treat them as zero by default. */
16404 if (attr->name == DW_AT_byte_size
16405 && form == DW_FORM_data4
16406 && DW_UNSND (attr) >= 0xffffffff)
16407 {
16408 complaint
16409 (&symfile_complaints,
16410 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16411 hex_string (DW_UNSND (attr)));
16412 DW_UNSND (attr) = 0;
16413 }
16414
16415 return info_ptr;
16416 }
16417
16418 /* Read an attribute described by an abbreviated attribute. */
16419
16420 static const gdb_byte *
16421 read_attribute (const struct die_reader_specs *reader,
16422 struct attribute *attr, struct attr_abbrev *abbrev,
16423 const gdb_byte *info_ptr)
16424 {
16425 attr->name = abbrev->name;
16426 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
16427 }
16428
16429 /* Read dwarf information from a buffer. */
16430
16431 static unsigned int
16432 read_1_byte (bfd *abfd, const gdb_byte *buf)
16433 {
16434 return bfd_get_8 (abfd, buf);
16435 }
16436
16437 static int
16438 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16439 {
16440 return bfd_get_signed_8 (abfd, buf);
16441 }
16442
16443 static unsigned int
16444 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16445 {
16446 return bfd_get_16 (abfd, buf);
16447 }
16448
16449 static int
16450 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16451 {
16452 return bfd_get_signed_16 (abfd, buf);
16453 }
16454
16455 static unsigned int
16456 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16457 {
16458 return bfd_get_32 (abfd, buf);
16459 }
16460
16461 static int
16462 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16463 {
16464 return bfd_get_signed_32 (abfd, buf);
16465 }
16466
16467 static ULONGEST
16468 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16469 {
16470 return bfd_get_64 (abfd, buf);
16471 }
16472
16473 static CORE_ADDR
16474 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16475 unsigned int *bytes_read)
16476 {
16477 struct comp_unit_head *cu_header = &cu->header;
16478 CORE_ADDR retval = 0;
16479
16480 if (cu_header->signed_addr_p)
16481 {
16482 switch (cu_header->addr_size)
16483 {
16484 case 2:
16485 retval = bfd_get_signed_16 (abfd, buf);
16486 break;
16487 case 4:
16488 retval = bfd_get_signed_32 (abfd, buf);
16489 break;
16490 case 8:
16491 retval = bfd_get_signed_64 (abfd, buf);
16492 break;
16493 default:
16494 internal_error (__FILE__, __LINE__,
16495 _("read_address: bad switch, signed [in module %s]"),
16496 bfd_get_filename (abfd));
16497 }
16498 }
16499 else
16500 {
16501 switch (cu_header->addr_size)
16502 {
16503 case 2:
16504 retval = bfd_get_16 (abfd, buf);
16505 break;
16506 case 4:
16507 retval = bfd_get_32 (abfd, buf);
16508 break;
16509 case 8:
16510 retval = bfd_get_64 (abfd, buf);
16511 break;
16512 default:
16513 internal_error (__FILE__, __LINE__,
16514 _("read_address: bad switch, "
16515 "unsigned [in module %s]"),
16516 bfd_get_filename (abfd));
16517 }
16518 }
16519
16520 *bytes_read = cu_header->addr_size;
16521 return retval;
16522 }
16523
16524 /* Read the initial length from a section. The (draft) DWARF 3
16525 specification allows the initial length to take up either 4 bytes
16526 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16527 bytes describe the length and all offsets will be 8 bytes in length
16528 instead of 4.
16529
16530 An older, non-standard 64-bit format is also handled by this
16531 function. The older format in question stores the initial length
16532 as an 8-byte quantity without an escape value. Lengths greater
16533 than 2^32 aren't very common which means that the initial 4 bytes
16534 is almost always zero. Since a length value of zero doesn't make
16535 sense for the 32-bit format, this initial zero can be considered to
16536 be an escape value which indicates the presence of the older 64-bit
16537 format. As written, the code can't detect (old format) lengths
16538 greater than 4GB. If it becomes necessary to handle lengths
16539 somewhat larger than 4GB, we could allow other small values (such
16540 as the non-sensical values of 1, 2, and 3) to also be used as
16541 escape values indicating the presence of the old format.
16542
16543 The value returned via bytes_read should be used to increment the
16544 relevant pointer after calling read_initial_length().
16545
16546 [ Note: read_initial_length() and read_offset() are based on the
16547 document entitled "DWARF Debugging Information Format", revision
16548 3, draft 8, dated November 19, 2001. This document was obtained
16549 from:
16550
16551 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16552
16553 This document is only a draft and is subject to change. (So beware.)
16554
16555 Details regarding the older, non-standard 64-bit format were
16556 determined empirically by examining 64-bit ELF files produced by
16557 the SGI toolchain on an IRIX 6.5 machine.
16558
16559 - Kevin, July 16, 2002
16560 ] */
16561
16562 static LONGEST
16563 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16564 {
16565 LONGEST length = bfd_get_32 (abfd, buf);
16566
16567 if (length == 0xffffffff)
16568 {
16569 length = bfd_get_64 (abfd, buf + 4);
16570 *bytes_read = 12;
16571 }
16572 else if (length == 0)
16573 {
16574 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16575 length = bfd_get_64 (abfd, buf);
16576 *bytes_read = 8;
16577 }
16578 else
16579 {
16580 *bytes_read = 4;
16581 }
16582
16583 return length;
16584 }
16585
16586 /* Cover function for read_initial_length.
16587 Returns the length of the object at BUF, and stores the size of the
16588 initial length in *BYTES_READ and stores the size that offsets will be in
16589 *OFFSET_SIZE.
16590 If the initial length size is not equivalent to that specified in
16591 CU_HEADER then issue a complaint.
16592 This is useful when reading non-comp-unit headers. */
16593
16594 static LONGEST
16595 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16596 const struct comp_unit_head *cu_header,
16597 unsigned int *bytes_read,
16598 unsigned int *offset_size)
16599 {
16600 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16601
16602 gdb_assert (cu_header->initial_length_size == 4
16603 || cu_header->initial_length_size == 8
16604 || cu_header->initial_length_size == 12);
16605
16606 if (cu_header->initial_length_size != *bytes_read)
16607 complaint (&symfile_complaints,
16608 _("intermixed 32-bit and 64-bit DWARF sections"));
16609
16610 *offset_size = (*bytes_read == 4) ? 4 : 8;
16611 return length;
16612 }
16613
16614 /* Read an offset from the data stream. The size of the offset is
16615 given by cu_header->offset_size. */
16616
16617 static LONGEST
16618 read_offset (bfd *abfd, const gdb_byte *buf,
16619 const struct comp_unit_head *cu_header,
16620 unsigned int *bytes_read)
16621 {
16622 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16623
16624 *bytes_read = cu_header->offset_size;
16625 return offset;
16626 }
16627
16628 /* Read an offset from the data stream. */
16629
16630 static LONGEST
16631 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16632 {
16633 LONGEST retval = 0;
16634
16635 switch (offset_size)
16636 {
16637 case 4:
16638 retval = bfd_get_32 (abfd, buf);
16639 break;
16640 case 8:
16641 retval = bfd_get_64 (abfd, buf);
16642 break;
16643 default:
16644 internal_error (__FILE__, __LINE__,
16645 _("read_offset_1: bad switch [in module %s]"),
16646 bfd_get_filename (abfd));
16647 }
16648
16649 return retval;
16650 }
16651
16652 static const gdb_byte *
16653 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16654 {
16655 /* If the size of a host char is 8 bits, we can return a pointer
16656 to the buffer, otherwise we have to copy the data to a buffer
16657 allocated on the temporary obstack. */
16658 gdb_assert (HOST_CHAR_BIT == 8);
16659 return buf;
16660 }
16661
16662 static const char *
16663 read_direct_string (bfd *abfd, const gdb_byte *buf,
16664 unsigned int *bytes_read_ptr)
16665 {
16666 /* If the size of a host char is 8 bits, we can return a pointer
16667 to the string, otherwise we have to copy the string to a buffer
16668 allocated on the temporary obstack. */
16669 gdb_assert (HOST_CHAR_BIT == 8);
16670 if (*buf == '\0')
16671 {
16672 *bytes_read_ptr = 1;
16673 return NULL;
16674 }
16675 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16676 return (const char *) buf;
16677 }
16678
16679 static const char *
16680 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
16681 {
16682 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
16683 if (dwarf2_per_objfile->str.buffer == NULL)
16684 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16685 bfd_get_filename (abfd));
16686 if (str_offset >= dwarf2_per_objfile->str.size)
16687 error (_("DW_FORM_strp pointing outside of "
16688 ".debug_str section [in module %s]"),
16689 bfd_get_filename (abfd));
16690 gdb_assert (HOST_CHAR_BIT == 8);
16691 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
16692 return NULL;
16693 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
16694 }
16695
16696 /* Read a string at offset STR_OFFSET in the .debug_str section from
16697 the .dwz file DWZ. Throw an error if the offset is too large. If
16698 the string consists of a single NUL byte, return NULL; otherwise
16699 return a pointer to the string. */
16700
16701 static const char *
16702 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
16703 {
16704 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
16705
16706 if (dwz->str.buffer == NULL)
16707 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
16708 "section [in module %s]"),
16709 bfd_get_filename (dwz->dwz_bfd));
16710 if (str_offset >= dwz->str.size)
16711 error (_("DW_FORM_GNU_strp_alt pointing outside of "
16712 ".debug_str section [in module %s]"),
16713 bfd_get_filename (dwz->dwz_bfd));
16714 gdb_assert (HOST_CHAR_BIT == 8);
16715 if (dwz->str.buffer[str_offset] == '\0')
16716 return NULL;
16717 return (const char *) (dwz->str.buffer + str_offset);
16718 }
16719
16720 static const char *
16721 read_indirect_string (bfd *abfd, const gdb_byte *buf,
16722 const struct comp_unit_head *cu_header,
16723 unsigned int *bytes_read_ptr)
16724 {
16725 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
16726
16727 return read_indirect_string_at_offset (abfd, str_offset);
16728 }
16729
16730 static ULONGEST
16731 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16732 unsigned int *bytes_read_ptr)
16733 {
16734 ULONGEST result;
16735 unsigned int num_read;
16736 int i, shift;
16737 unsigned char byte;
16738
16739 result = 0;
16740 shift = 0;
16741 num_read = 0;
16742 i = 0;
16743 while (1)
16744 {
16745 byte = bfd_get_8 (abfd, buf);
16746 buf++;
16747 num_read++;
16748 result |= ((ULONGEST) (byte & 127) << shift);
16749 if ((byte & 128) == 0)
16750 {
16751 break;
16752 }
16753 shift += 7;
16754 }
16755 *bytes_read_ptr = num_read;
16756 return result;
16757 }
16758
16759 static LONGEST
16760 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16761 unsigned int *bytes_read_ptr)
16762 {
16763 LONGEST result;
16764 int i, shift, num_read;
16765 unsigned char byte;
16766
16767 result = 0;
16768 shift = 0;
16769 num_read = 0;
16770 i = 0;
16771 while (1)
16772 {
16773 byte = bfd_get_8 (abfd, buf);
16774 buf++;
16775 num_read++;
16776 result |= ((LONGEST) (byte & 127) << shift);
16777 shift += 7;
16778 if ((byte & 128) == 0)
16779 {
16780 break;
16781 }
16782 }
16783 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
16784 result |= -(((LONGEST) 1) << shift);
16785 *bytes_read_ptr = num_read;
16786 return result;
16787 }
16788
16789 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16790 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16791 ADDR_SIZE is the size of addresses from the CU header. */
16792
16793 static CORE_ADDR
16794 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16795 {
16796 struct objfile *objfile = dwarf2_per_objfile->objfile;
16797 bfd *abfd = objfile->obfd;
16798 const gdb_byte *info_ptr;
16799
16800 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16801 if (dwarf2_per_objfile->addr.buffer == NULL)
16802 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16803 objfile_name (objfile));
16804 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16805 error (_("DW_FORM_addr_index pointing outside of "
16806 ".debug_addr section [in module %s]"),
16807 objfile_name (objfile));
16808 info_ptr = (dwarf2_per_objfile->addr.buffer
16809 + addr_base + addr_index * addr_size);
16810 if (addr_size == 4)
16811 return bfd_get_32 (abfd, info_ptr);
16812 else
16813 return bfd_get_64 (abfd, info_ptr);
16814 }
16815
16816 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16817
16818 static CORE_ADDR
16819 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16820 {
16821 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16822 }
16823
16824 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16825
16826 static CORE_ADDR
16827 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16828 unsigned int *bytes_read)
16829 {
16830 bfd *abfd = cu->objfile->obfd;
16831 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16832
16833 return read_addr_index (cu, addr_index);
16834 }
16835
16836 /* Data structure to pass results from dwarf2_read_addr_index_reader
16837 back to dwarf2_read_addr_index. */
16838
16839 struct dwarf2_read_addr_index_data
16840 {
16841 ULONGEST addr_base;
16842 int addr_size;
16843 };
16844
16845 /* die_reader_func for dwarf2_read_addr_index. */
16846
16847 static void
16848 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
16849 const gdb_byte *info_ptr,
16850 struct die_info *comp_unit_die,
16851 int has_children,
16852 void *data)
16853 {
16854 struct dwarf2_cu *cu = reader->cu;
16855 struct dwarf2_read_addr_index_data *aidata =
16856 (struct dwarf2_read_addr_index_data *) data;
16857
16858 aidata->addr_base = cu->addr_base;
16859 aidata->addr_size = cu->header.addr_size;
16860 }
16861
16862 /* Given an index in .debug_addr, fetch the value.
16863 NOTE: This can be called during dwarf expression evaluation,
16864 long after the debug information has been read, and thus per_cu->cu
16865 may no longer exist. */
16866
16867 CORE_ADDR
16868 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16869 unsigned int addr_index)
16870 {
16871 struct objfile *objfile = per_cu->objfile;
16872 struct dwarf2_cu *cu = per_cu->cu;
16873 ULONGEST addr_base;
16874 int addr_size;
16875
16876 /* This is intended to be called from outside this file. */
16877 dw2_setup (objfile);
16878
16879 /* We need addr_base and addr_size.
16880 If we don't have PER_CU->cu, we have to get it.
16881 Nasty, but the alternative is storing the needed info in PER_CU,
16882 which at this point doesn't seem justified: it's not clear how frequently
16883 it would get used and it would increase the size of every PER_CU.
16884 Entry points like dwarf2_per_cu_addr_size do a similar thing
16885 so we're not in uncharted territory here.
16886 Alas we need to be a bit more complicated as addr_base is contained
16887 in the DIE.
16888
16889 We don't need to read the entire CU(/TU).
16890 We just need the header and top level die.
16891
16892 IWBN to use the aging mechanism to let us lazily later discard the CU.
16893 For now we skip this optimization. */
16894
16895 if (cu != NULL)
16896 {
16897 addr_base = cu->addr_base;
16898 addr_size = cu->header.addr_size;
16899 }
16900 else
16901 {
16902 struct dwarf2_read_addr_index_data aidata;
16903
16904 /* Note: We can't use init_cutu_and_read_dies_simple here,
16905 we need addr_base. */
16906 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16907 dwarf2_read_addr_index_reader, &aidata);
16908 addr_base = aidata.addr_base;
16909 addr_size = aidata.addr_size;
16910 }
16911
16912 return read_addr_index_1 (addr_index, addr_base, addr_size);
16913 }
16914
16915 /* Given a DW_FORM_GNU_str_index, fetch the string.
16916 This is only used by the Fission support. */
16917
16918 static const char *
16919 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
16920 {
16921 struct objfile *objfile = dwarf2_per_objfile->objfile;
16922 const char *objf_name = objfile_name (objfile);
16923 bfd *abfd = objfile->obfd;
16924 struct dwarf2_cu *cu = reader->cu;
16925 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16926 struct dwarf2_section_info *str_offsets_section =
16927 &reader->dwo_file->sections.str_offsets;
16928 const gdb_byte *info_ptr;
16929 ULONGEST str_offset;
16930 static const char form_name[] = "DW_FORM_GNU_str_index";
16931
16932 dwarf2_read_section (objfile, str_section);
16933 dwarf2_read_section (objfile, str_offsets_section);
16934 if (str_section->buffer == NULL)
16935 error (_("%s used without .debug_str.dwo section"
16936 " in CU at offset 0x%lx [in module %s]"),
16937 form_name, (long) cu->header.offset.sect_off, objf_name);
16938 if (str_offsets_section->buffer == NULL)
16939 error (_("%s used without .debug_str_offsets.dwo section"
16940 " in CU at offset 0x%lx [in module %s]"),
16941 form_name, (long) cu->header.offset.sect_off, objf_name);
16942 if (str_index * cu->header.offset_size >= str_offsets_section->size)
16943 error (_("%s pointing outside of .debug_str_offsets.dwo"
16944 " section in CU at offset 0x%lx [in module %s]"),
16945 form_name, (long) cu->header.offset.sect_off, objf_name);
16946 info_ptr = (str_offsets_section->buffer
16947 + str_index * cu->header.offset_size);
16948 if (cu->header.offset_size == 4)
16949 str_offset = bfd_get_32 (abfd, info_ptr);
16950 else
16951 str_offset = bfd_get_64 (abfd, info_ptr);
16952 if (str_offset >= str_section->size)
16953 error (_("Offset from %s pointing outside of"
16954 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
16955 form_name, (long) cu->header.offset.sect_off, objf_name);
16956 return (const char *) (str_section->buffer + str_offset);
16957 }
16958
16959 /* Return the length of an LEB128 number in BUF. */
16960
16961 static int
16962 leb128_size (const gdb_byte *buf)
16963 {
16964 const gdb_byte *begin = buf;
16965 gdb_byte byte;
16966
16967 while (1)
16968 {
16969 byte = *buf++;
16970 if ((byte & 128) == 0)
16971 return buf - begin;
16972 }
16973 }
16974
16975 static void
16976 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
16977 {
16978 switch (lang)
16979 {
16980 case DW_LANG_C89:
16981 case DW_LANG_C99:
16982 case DW_LANG_C11:
16983 case DW_LANG_C:
16984 case DW_LANG_UPC:
16985 cu->language = language_c;
16986 break;
16987 case DW_LANG_C_plus_plus:
16988 case DW_LANG_C_plus_plus_11:
16989 case DW_LANG_C_plus_plus_14:
16990 cu->language = language_cplus;
16991 break;
16992 case DW_LANG_D:
16993 cu->language = language_d;
16994 break;
16995 case DW_LANG_Fortran77:
16996 case DW_LANG_Fortran90:
16997 case DW_LANG_Fortran95:
16998 case DW_LANG_Fortran03:
16999 case DW_LANG_Fortran08:
17000 cu->language = language_fortran;
17001 break;
17002 case DW_LANG_Go:
17003 cu->language = language_go;
17004 break;
17005 case DW_LANG_Mips_Assembler:
17006 cu->language = language_asm;
17007 break;
17008 case DW_LANG_Java:
17009 cu->language = language_java;
17010 break;
17011 case DW_LANG_Ada83:
17012 case DW_LANG_Ada95:
17013 cu->language = language_ada;
17014 break;
17015 case DW_LANG_Modula2:
17016 cu->language = language_m2;
17017 break;
17018 case DW_LANG_Pascal83:
17019 cu->language = language_pascal;
17020 break;
17021 case DW_LANG_ObjC:
17022 cu->language = language_objc;
17023 break;
17024 case DW_LANG_Cobol74:
17025 case DW_LANG_Cobol85:
17026 default:
17027 cu->language = language_minimal;
17028 break;
17029 }
17030 cu->language_defn = language_def (cu->language);
17031 }
17032
17033 /* Return the named attribute or NULL if not there. */
17034
17035 static struct attribute *
17036 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17037 {
17038 for (;;)
17039 {
17040 unsigned int i;
17041 struct attribute *spec = NULL;
17042
17043 for (i = 0; i < die->num_attrs; ++i)
17044 {
17045 if (die->attrs[i].name == name)
17046 return &die->attrs[i];
17047 if (die->attrs[i].name == DW_AT_specification
17048 || die->attrs[i].name == DW_AT_abstract_origin)
17049 spec = &die->attrs[i];
17050 }
17051
17052 if (!spec)
17053 break;
17054
17055 die = follow_die_ref (die, spec, &cu);
17056 }
17057
17058 return NULL;
17059 }
17060
17061 /* Return the named attribute or NULL if not there,
17062 but do not follow DW_AT_specification, etc.
17063 This is for use in contexts where we're reading .debug_types dies.
17064 Following DW_AT_specification, DW_AT_abstract_origin will take us
17065 back up the chain, and we want to go down. */
17066
17067 static struct attribute *
17068 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17069 {
17070 unsigned int i;
17071
17072 for (i = 0; i < die->num_attrs; ++i)
17073 if (die->attrs[i].name == name)
17074 return &die->attrs[i];
17075
17076 return NULL;
17077 }
17078
17079 /* Return the string associated with a string-typed attribute, or NULL if it
17080 is either not found or is of an incorrect type. */
17081
17082 static const char *
17083 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17084 {
17085 struct attribute *attr;
17086 const char *str = NULL;
17087
17088 attr = dwarf2_attr (die, name, cu);
17089
17090 if (attr != NULL)
17091 {
17092 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_string
17093 || attr->form == DW_FORM_GNU_strp_alt)
17094 str = DW_STRING (attr);
17095 else
17096 complaint (&symfile_complaints,
17097 _("string type expected for attribute %s for "
17098 "DIE at 0x%x in module %s"),
17099 dwarf_attr_name (name), die->offset.sect_off,
17100 objfile_name (cu->objfile));
17101 }
17102
17103 return str;
17104 }
17105
17106 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17107 and holds a non-zero value. This function should only be used for
17108 DW_FORM_flag or DW_FORM_flag_present attributes. */
17109
17110 static int
17111 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17112 {
17113 struct attribute *attr = dwarf2_attr (die, name, cu);
17114
17115 return (attr && DW_UNSND (attr));
17116 }
17117
17118 static int
17119 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17120 {
17121 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17122 which value is non-zero. However, we have to be careful with
17123 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17124 (via dwarf2_flag_true_p) follows this attribute. So we may
17125 end up accidently finding a declaration attribute that belongs
17126 to a different DIE referenced by the specification attribute,
17127 even though the given DIE does not have a declaration attribute. */
17128 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17129 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17130 }
17131
17132 /* Return the die giving the specification for DIE, if there is
17133 one. *SPEC_CU is the CU containing DIE on input, and the CU
17134 containing the return value on output. If there is no
17135 specification, but there is an abstract origin, that is
17136 returned. */
17137
17138 static struct die_info *
17139 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17140 {
17141 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17142 *spec_cu);
17143
17144 if (spec_attr == NULL)
17145 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17146
17147 if (spec_attr == NULL)
17148 return NULL;
17149 else
17150 return follow_die_ref (die, spec_attr, spec_cu);
17151 }
17152
17153 /* Free the line_header structure *LH, and any arrays and strings it
17154 refers to.
17155 NOTE: This is also used as a "cleanup" function. */
17156
17157 static void
17158 free_line_header (struct line_header *lh)
17159 {
17160 if (lh->standard_opcode_lengths)
17161 xfree (lh->standard_opcode_lengths);
17162
17163 /* Remember that all the lh->file_names[i].name pointers are
17164 pointers into debug_line_buffer, and don't need to be freed. */
17165 if (lh->file_names)
17166 xfree (lh->file_names);
17167
17168 /* Similarly for the include directory names. */
17169 if (lh->include_dirs)
17170 xfree (lh->include_dirs);
17171
17172 xfree (lh);
17173 }
17174
17175 /* Stub for free_line_header to match void * callback types. */
17176
17177 static void
17178 free_line_header_voidp (void *arg)
17179 {
17180 struct line_header *lh = arg;
17181
17182 free_line_header (lh);
17183 }
17184
17185 /* Add an entry to LH's include directory table. */
17186
17187 static void
17188 add_include_dir (struct line_header *lh, const char *include_dir)
17189 {
17190 if (dwarf_line_debug >= 2)
17191 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17192 lh->num_include_dirs + 1, include_dir);
17193
17194 /* Grow the array if necessary. */
17195 if (lh->include_dirs_size == 0)
17196 {
17197 lh->include_dirs_size = 1; /* for testing */
17198 lh->include_dirs = xmalloc (lh->include_dirs_size
17199 * sizeof (*lh->include_dirs));
17200 }
17201 else if (lh->num_include_dirs >= lh->include_dirs_size)
17202 {
17203 lh->include_dirs_size *= 2;
17204 lh->include_dirs = xrealloc (lh->include_dirs,
17205 (lh->include_dirs_size
17206 * sizeof (*lh->include_dirs)));
17207 }
17208
17209 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17210 }
17211
17212 /* Add an entry to LH's file name table. */
17213
17214 static void
17215 add_file_name (struct line_header *lh,
17216 const char *name,
17217 unsigned int dir_index,
17218 unsigned int mod_time,
17219 unsigned int length)
17220 {
17221 struct file_entry *fe;
17222
17223 if (dwarf_line_debug >= 2)
17224 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17225 lh->num_file_names + 1, name);
17226
17227 /* Grow the array if necessary. */
17228 if (lh->file_names_size == 0)
17229 {
17230 lh->file_names_size = 1; /* for testing */
17231 lh->file_names = xmalloc (lh->file_names_size
17232 * sizeof (*lh->file_names));
17233 }
17234 else if (lh->num_file_names >= lh->file_names_size)
17235 {
17236 lh->file_names_size *= 2;
17237 lh->file_names = xrealloc (lh->file_names,
17238 (lh->file_names_size
17239 * sizeof (*lh->file_names)));
17240 }
17241
17242 fe = &lh->file_names[lh->num_file_names++];
17243 fe->name = name;
17244 fe->dir_index = dir_index;
17245 fe->mod_time = mod_time;
17246 fe->length = length;
17247 fe->included_p = 0;
17248 fe->symtab = NULL;
17249 }
17250
17251 /* A convenience function to find the proper .debug_line section for a CU. */
17252
17253 static struct dwarf2_section_info *
17254 get_debug_line_section (struct dwarf2_cu *cu)
17255 {
17256 struct dwarf2_section_info *section;
17257
17258 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17259 DWO file. */
17260 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17261 section = &cu->dwo_unit->dwo_file->sections.line;
17262 else if (cu->per_cu->is_dwz)
17263 {
17264 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17265
17266 section = &dwz->line;
17267 }
17268 else
17269 section = &dwarf2_per_objfile->line;
17270
17271 return section;
17272 }
17273
17274 /* Read the statement program header starting at OFFSET in
17275 .debug_line, or .debug_line.dwo. Return a pointer
17276 to a struct line_header, allocated using xmalloc.
17277 Returns NULL if there is a problem reading the header, e.g., if it
17278 has a version we don't understand.
17279
17280 NOTE: the strings in the include directory and file name tables of
17281 the returned object point into the dwarf line section buffer,
17282 and must not be freed. */
17283
17284 static struct line_header *
17285 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17286 {
17287 struct cleanup *back_to;
17288 struct line_header *lh;
17289 const gdb_byte *line_ptr;
17290 unsigned int bytes_read, offset_size;
17291 int i;
17292 const char *cur_dir, *cur_file;
17293 struct dwarf2_section_info *section;
17294 bfd *abfd;
17295
17296 section = get_debug_line_section (cu);
17297 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17298 if (section->buffer == NULL)
17299 {
17300 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17301 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17302 else
17303 complaint (&symfile_complaints, _("missing .debug_line section"));
17304 return 0;
17305 }
17306
17307 /* We can't do this until we know the section is non-empty.
17308 Only then do we know we have such a section. */
17309 abfd = get_section_bfd_owner (section);
17310
17311 /* Make sure that at least there's room for the total_length field.
17312 That could be 12 bytes long, but we're just going to fudge that. */
17313 if (offset + 4 >= section->size)
17314 {
17315 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17316 return 0;
17317 }
17318
17319 lh = xmalloc (sizeof (*lh));
17320 memset (lh, 0, sizeof (*lh));
17321 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17322 (void *) lh);
17323
17324 lh->offset.sect_off = offset;
17325 lh->offset_in_dwz = cu->per_cu->is_dwz;
17326
17327 line_ptr = section->buffer + offset;
17328
17329 /* Read in the header. */
17330 lh->total_length =
17331 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17332 &bytes_read, &offset_size);
17333 line_ptr += bytes_read;
17334 if (line_ptr + lh->total_length > (section->buffer + section->size))
17335 {
17336 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17337 do_cleanups (back_to);
17338 return 0;
17339 }
17340 lh->statement_program_end = line_ptr + lh->total_length;
17341 lh->version = read_2_bytes (abfd, line_ptr);
17342 line_ptr += 2;
17343 if (lh->version > 4)
17344 {
17345 /* This is a version we don't understand. The format could have
17346 changed in ways we don't handle properly so just punt. */
17347 complaint (&symfile_complaints,
17348 _("unsupported version in .debug_line section"));
17349 return NULL;
17350 }
17351 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17352 line_ptr += offset_size;
17353 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17354 line_ptr += 1;
17355 if (lh->version >= 4)
17356 {
17357 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17358 line_ptr += 1;
17359 }
17360 else
17361 lh->maximum_ops_per_instruction = 1;
17362
17363 if (lh->maximum_ops_per_instruction == 0)
17364 {
17365 lh->maximum_ops_per_instruction = 1;
17366 complaint (&symfile_complaints,
17367 _("invalid maximum_ops_per_instruction "
17368 "in `.debug_line' section"));
17369 }
17370
17371 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17372 line_ptr += 1;
17373 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17374 line_ptr += 1;
17375 lh->line_range = read_1_byte (abfd, line_ptr);
17376 line_ptr += 1;
17377 lh->opcode_base = read_1_byte (abfd, line_ptr);
17378 line_ptr += 1;
17379 lh->standard_opcode_lengths
17380 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
17381
17382 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17383 for (i = 1; i < lh->opcode_base; ++i)
17384 {
17385 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17386 line_ptr += 1;
17387 }
17388
17389 /* Read directory table. */
17390 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17391 {
17392 line_ptr += bytes_read;
17393 add_include_dir (lh, cur_dir);
17394 }
17395 line_ptr += bytes_read;
17396
17397 /* Read file name table. */
17398 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17399 {
17400 unsigned int dir_index, mod_time, length;
17401
17402 line_ptr += bytes_read;
17403 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17404 line_ptr += bytes_read;
17405 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17406 line_ptr += bytes_read;
17407 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17408 line_ptr += bytes_read;
17409
17410 add_file_name (lh, cur_file, dir_index, mod_time, length);
17411 }
17412 line_ptr += bytes_read;
17413 lh->statement_program_start = line_ptr;
17414
17415 if (line_ptr > (section->buffer + section->size))
17416 complaint (&symfile_complaints,
17417 _("line number info header doesn't "
17418 "fit in `.debug_line' section"));
17419
17420 discard_cleanups (back_to);
17421 return lh;
17422 }
17423
17424 /* Subroutine of dwarf_decode_lines to simplify it.
17425 Return the file name of the psymtab for included file FILE_INDEX
17426 in line header LH of PST.
17427 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17428 If space for the result is malloc'd, it will be freed by a cleanup.
17429 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17430
17431 The function creates dangling cleanup registration. */
17432
17433 static const char *
17434 psymtab_include_file_name (const struct line_header *lh, int file_index,
17435 const struct partial_symtab *pst,
17436 const char *comp_dir)
17437 {
17438 const struct file_entry fe = lh->file_names [file_index];
17439 const char *include_name = fe.name;
17440 const char *include_name_to_compare = include_name;
17441 const char *dir_name = NULL;
17442 const char *pst_filename;
17443 char *copied_name = NULL;
17444 int file_is_pst;
17445
17446 if (fe.dir_index && lh->include_dirs != NULL)
17447 dir_name = lh->include_dirs[fe.dir_index - 1];
17448
17449 if (!IS_ABSOLUTE_PATH (include_name)
17450 && (dir_name != NULL || comp_dir != NULL))
17451 {
17452 /* Avoid creating a duplicate psymtab for PST.
17453 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17454 Before we do the comparison, however, we need to account
17455 for DIR_NAME and COMP_DIR.
17456 First prepend dir_name (if non-NULL). If we still don't
17457 have an absolute path prepend comp_dir (if non-NULL).
17458 However, the directory we record in the include-file's
17459 psymtab does not contain COMP_DIR (to match the
17460 corresponding symtab(s)).
17461
17462 Example:
17463
17464 bash$ cd /tmp
17465 bash$ gcc -g ./hello.c
17466 include_name = "hello.c"
17467 dir_name = "."
17468 DW_AT_comp_dir = comp_dir = "/tmp"
17469 DW_AT_name = "./hello.c"
17470
17471 */
17472
17473 if (dir_name != NULL)
17474 {
17475 char *tem = concat (dir_name, SLASH_STRING,
17476 include_name, (char *)NULL);
17477
17478 make_cleanup (xfree, tem);
17479 include_name = tem;
17480 include_name_to_compare = include_name;
17481 }
17482 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17483 {
17484 char *tem = concat (comp_dir, SLASH_STRING,
17485 include_name, (char *)NULL);
17486
17487 make_cleanup (xfree, tem);
17488 include_name_to_compare = tem;
17489 }
17490 }
17491
17492 pst_filename = pst->filename;
17493 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17494 {
17495 copied_name = concat (pst->dirname, SLASH_STRING,
17496 pst_filename, (char *)NULL);
17497 pst_filename = copied_name;
17498 }
17499
17500 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
17501
17502 if (copied_name != NULL)
17503 xfree (copied_name);
17504
17505 if (file_is_pst)
17506 return NULL;
17507 return include_name;
17508 }
17509
17510 /* State machine to track the state of the line number program. */
17511
17512 typedef struct
17513 {
17514 /* These are part of the standard DWARF line number state machine. */
17515
17516 unsigned char op_index;
17517 unsigned int file;
17518 unsigned int line;
17519 CORE_ADDR address;
17520 int is_stmt;
17521 unsigned int discriminator;
17522
17523 /* Additional bits of state we need to track. */
17524
17525 /* The last file that we called dwarf2_start_subfile for.
17526 This is only used for TLLs. */
17527 unsigned int last_file;
17528 /* The last file a line number was recorded for. */
17529 struct subfile *last_subfile;
17530
17531 /* The function to call to record a line. */
17532 record_line_ftype *record_line;
17533
17534 /* The last line number that was recorded, used to coalesce
17535 consecutive entries for the same line. This can happen, for
17536 example, when discriminators are present. PR 17276. */
17537 unsigned int last_line;
17538 int line_has_non_zero_discriminator;
17539 } lnp_state_machine;
17540
17541 /* There's a lot of static state to pass to dwarf_record_line.
17542 This keeps it all together. */
17543
17544 typedef struct
17545 {
17546 /* The gdbarch. */
17547 struct gdbarch *gdbarch;
17548
17549 /* The line number header. */
17550 struct line_header *line_header;
17551
17552 /* Non-zero if we're recording lines.
17553 Otherwise we're building partial symtabs and are just interested in
17554 finding include files mentioned by the line number program. */
17555 int record_lines_p;
17556 } lnp_reader_state;
17557
17558 /* Ignore this record_line request. */
17559
17560 static void
17561 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17562 {
17563 return;
17564 }
17565
17566 /* Return non-zero if we should add LINE to the line number table.
17567 LINE is the line to add, LAST_LINE is the last line that was added,
17568 LAST_SUBFILE is the subfile for LAST_LINE.
17569 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17570 had a non-zero discriminator.
17571
17572 We have to be careful in the presence of discriminators.
17573 E.g., for this line:
17574
17575 for (i = 0; i < 100000; i++);
17576
17577 clang can emit four line number entries for that one line,
17578 each with a different discriminator.
17579 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17580
17581 However, we want gdb to coalesce all four entries into one.
17582 Otherwise the user could stepi into the middle of the line and
17583 gdb would get confused about whether the pc really was in the
17584 middle of the line.
17585
17586 Things are further complicated by the fact that two consecutive
17587 line number entries for the same line is a heuristic used by gcc
17588 to denote the end of the prologue. So we can't just discard duplicate
17589 entries, we have to be selective about it. The heuristic we use is
17590 that we only collapse consecutive entries for the same line if at least
17591 one of those entries has a non-zero discriminator. PR 17276.
17592
17593 Note: Addresses in the line number state machine can never go backwards
17594 within one sequence, thus this coalescing is ok. */
17595
17596 static int
17597 dwarf_record_line_p (unsigned int line, unsigned int last_line,
17598 int line_has_non_zero_discriminator,
17599 struct subfile *last_subfile)
17600 {
17601 if (current_subfile != last_subfile)
17602 return 1;
17603 if (line != last_line)
17604 return 1;
17605 /* Same line for the same file that we've seen already.
17606 As a last check, for pr 17276, only record the line if the line
17607 has never had a non-zero discriminator. */
17608 if (!line_has_non_zero_discriminator)
17609 return 1;
17610 return 0;
17611 }
17612
17613 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17614 in the line table of subfile SUBFILE. */
17615
17616 static void
17617 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17618 unsigned int line, CORE_ADDR address,
17619 record_line_ftype p_record_line)
17620 {
17621 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17622
17623 if (dwarf_line_debug)
17624 {
17625 fprintf_unfiltered (gdb_stdlog,
17626 "Recording line %u, file %s, address %s\n",
17627 line, lbasename (subfile->name),
17628 paddress (gdbarch, address));
17629 }
17630
17631 (*p_record_line) (subfile, line, addr);
17632 }
17633
17634 /* Subroutine of dwarf_decode_lines_1 to simplify it.
17635 Mark the end of a set of line number records.
17636 The arguments are the same as for dwarf_record_line_1.
17637 If SUBFILE is NULL the request is ignored. */
17638
17639 static void
17640 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17641 CORE_ADDR address, record_line_ftype p_record_line)
17642 {
17643 if (subfile == NULL)
17644 return;
17645
17646 if (dwarf_line_debug)
17647 {
17648 fprintf_unfiltered (gdb_stdlog,
17649 "Finishing current line, file %s, address %s\n",
17650 lbasename (subfile->name),
17651 paddress (gdbarch, address));
17652 }
17653
17654 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
17655 }
17656
17657 /* Record the line in STATE.
17658 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
17659
17660 static void
17661 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
17662 int end_sequence)
17663 {
17664 const struct line_header *lh = reader->line_header;
17665 unsigned int file, line, discriminator;
17666 int is_stmt;
17667
17668 file = state->file;
17669 line = state->line;
17670 is_stmt = state->is_stmt;
17671 discriminator = state->discriminator;
17672
17673 if (dwarf_line_debug)
17674 {
17675 fprintf_unfiltered (gdb_stdlog,
17676 "Processing actual line %u: file %u,"
17677 " address %s, is_stmt %u, discrim %u\n",
17678 line, file,
17679 paddress (reader->gdbarch, state->address),
17680 is_stmt, discriminator);
17681 }
17682
17683 if (file == 0 || file - 1 >= lh->num_file_names)
17684 dwarf2_debug_line_missing_file_complaint ();
17685 /* For now we ignore lines not starting on an instruction boundary.
17686 But not when processing end_sequence for compatibility with the
17687 previous version of the code. */
17688 else if (state->op_index == 0 || end_sequence)
17689 {
17690 lh->file_names[file - 1].included_p = 1;
17691 if (reader->record_lines_p && is_stmt)
17692 {
17693 if (state->last_subfile != current_subfile || end_sequence)
17694 {
17695 dwarf_finish_line (reader->gdbarch, state->last_subfile,
17696 state->address, state->record_line);
17697 }
17698
17699 if (!end_sequence)
17700 {
17701 if (dwarf_record_line_p (line, state->last_line,
17702 state->line_has_non_zero_discriminator,
17703 state->last_subfile))
17704 {
17705 dwarf_record_line_1 (reader->gdbarch, current_subfile,
17706 line, state->address,
17707 state->record_line);
17708 }
17709 state->last_subfile = current_subfile;
17710 state->last_line = line;
17711 }
17712 }
17713 }
17714 }
17715
17716 /* Initialize STATE for the start of a line number program. */
17717
17718 static void
17719 init_lnp_state_machine (lnp_state_machine *state,
17720 const lnp_reader_state *reader)
17721 {
17722 memset (state, 0, sizeof (*state));
17723
17724 /* Just starting, there is no "last file". */
17725 state->last_file = 0;
17726 state->last_subfile = NULL;
17727
17728 state->record_line = record_line;
17729
17730 state->last_line = 0;
17731 state->line_has_non_zero_discriminator = 0;
17732
17733 /* Initialize these according to the DWARF spec. */
17734 state->op_index = 0;
17735 state->file = 1;
17736 state->line = 1;
17737 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
17738 was a line entry for it so that the backend has a chance to adjust it
17739 and also record it in case it needs it. This is currently used by MIPS
17740 code, cf. `mips_adjust_dwarf2_line'. */
17741 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
17742 state->is_stmt = reader->line_header->default_is_stmt;
17743 state->discriminator = 0;
17744 }
17745
17746 /* Check address and if invalid nop-out the rest of the lines in this
17747 sequence. */
17748
17749 static void
17750 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
17751 const gdb_byte *line_ptr,
17752 CORE_ADDR lowpc, CORE_ADDR address)
17753 {
17754 /* If address < lowpc then it's not a usable value, it's outside the
17755 pc range of the CU. However, we restrict the test to only address
17756 values of zero to preserve GDB's previous behaviour which is to
17757 handle the specific case of a function being GC'd by the linker. */
17758
17759 if (address == 0 && address < lowpc)
17760 {
17761 /* This line table is for a function which has been
17762 GCd by the linker. Ignore it. PR gdb/12528 */
17763
17764 struct objfile *objfile = cu->objfile;
17765 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17766
17767 complaint (&symfile_complaints,
17768 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17769 line_offset, objfile_name (objfile));
17770 state->record_line = noop_record_line;
17771 /* Note: sm.record_line is left as noop_record_line
17772 until we see DW_LNE_end_sequence. */
17773 }
17774 }
17775
17776 /* Subroutine of dwarf_decode_lines to simplify it.
17777 Process the line number information in LH.
17778 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
17779 program in order to set included_p for every referenced header. */
17780
17781 static void
17782 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17783 const int decode_for_pst_p, CORE_ADDR lowpc)
17784 {
17785 const gdb_byte *line_ptr, *extended_end;
17786 const gdb_byte *line_end;
17787 unsigned int bytes_read, extended_len;
17788 unsigned char op_code, extended_op;
17789 CORE_ADDR baseaddr;
17790 struct objfile *objfile = cu->objfile;
17791 bfd *abfd = objfile->obfd;
17792 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17793 /* Non-zero if we're recording line info (as opposed to building partial
17794 symtabs). */
17795 int record_lines_p = !decode_for_pst_p;
17796 /* A collection of things we need to pass to dwarf_record_line. */
17797 lnp_reader_state reader_state;
17798
17799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17800
17801 line_ptr = lh->statement_program_start;
17802 line_end = lh->statement_program_end;
17803
17804 reader_state.gdbarch = gdbarch;
17805 reader_state.line_header = lh;
17806 reader_state.record_lines_p = record_lines_p;
17807
17808 /* Read the statement sequences until there's nothing left. */
17809 while (line_ptr < line_end)
17810 {
17811 /* The DWARF line number program state machine. */
17812 lnp_state_machine state_machine;
17813 int end_sequence = 0;
17814
17815 /* Reset the state machine at the start of each sequence. */
17816 init_lnp_state_machine (&state_machine, &reader_state);
17817
17818 if (record_lines_p && lh->num_file_names >= state_machine.file)
17819 {
17820 /* Start a subfile for the current file of the state machine. */
17821 /* lh->include_dirs and lh->file_names are 0-based, but the
17822 directory and file name numbers in the statement program
17823 are 1-based. */
17824 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
17825 const char *dir = NULL;
17826
17827 if (fe->dir_index && lh->include_dirs != NULL)
17828 dir = lh->include_dirs[fe->dir_index - 1];
17829
17830 dwarf2_start_subfile (fe->name, dir);
17831 }
17832
17833 /* Decode the table. */
17834 while (line_ptr < line_end && !end_sequence)
17835 {
17836 op_code = read_1_byte (abfd, line_ptr);
17837 line_ptr += 1;
17838
17839 if (op_code >= lh->opcode_base)
17840 {
17841 /* Special opcode. */
17842 unsigned char adj_opcode;
17843 CORE_ADDR addr_adj;
17844 int line_delta;
17845
17846 adj_opcode = op_code - lh->opcode_base;
17847 addr_adj = (((state_machine.op_index
17848 + (adj_opcode / lh->line_range))
17849 / lh->maximum_ops_per_instruction)
17850 * lh->minimum_instruction_length);
17851 state_machine.address
17852 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17853 state_machine.op_index = ((state_machine.op_index
17854 + (adj_opcode / lh->line_range))
17855 % lh->maximum_ops_per_instruction);
17856 line_delta = lh->line_base + (adj_opcode % lh->line_range);
17857 state_machine.line += line_delta;
17858 if (line_delta != 0)
17859 state_machine.line_has_non_zero_discriminator
17860 = state_machine.discriminator != 0;
17861
17862 dwarf_record_line (&reader_state, &state_machine, 0);
17863 state_machine.discriminator = 0;
17864 }
17865 else switch (op_code)
17866 {
17867 case DW_LNS_extended_op:
17868 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17869 &bytes_read);
17870 line_ptr += bytes_read;
17871 extended_end = line_ptr + extended_len;
17872 extended_op = read_1_byte (abfd, line_ptr);
17873 line_ptr += 1;
17874 switch (extended_op)
17875 {
17876 case DW_LNE_end_sequence:
17877 state_machine.record_line = record_line;
17878 end_sequence = 1;
17879 break;
17880 case DW_LNE_set_address:
17881 {
17882 CORE_ADDR address
17883 = read_address (abfd, line_ptr, cu, &bytes_read);
17884
17885 line_ptr += bytes_read;
17886 check_line_address (cu, &state_machine, line_ptr,
17887 lowpc, address);
17888 state_machine.op_index = 0;
17889 address += baseaddr;
17890 state_machine.address
17891 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
17892 }
17893 break;
17894 case DW_LNE_define_file:
17895 {
17896 const char *cur_file;
17897 unsigned int dir_index, mod_time, length;
17898
17899 cur_file = read_direct_string (abfd, line_ptr,
17900 &bytes_read);
17901 line_ptr += bytes_read;
17902 dir_index =
17903 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17904 line_ptr += bytes_read;
17905 mod_time =
17906 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17907 line_ptr += bytes_read;
17908 length =
17909 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17910 line_ptr += bytes_read;
17911 add_file_name (lh, cur_file, dir_index, mod_time, length);
17912 }
17913 break;
17914 case DW_LNE_set_discriminator:
17915 /* The discriminator is not interesting to the debugger;
17916 just ignore it. We still need to check its value though:
17917 if there are consecutive entries for the same
17918 (non-prologue) line we want to coalesce them.
17919 PR 17276. */
17920 state_machine.discriminator
17921 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17922 state_machine.line_has_non_zero_discriminator
17923 |= state_machine.discriminator != 0;
17924 line_ptr += bytes_read;
17925 break;
17926 default:
17927 complaint (&symfile_complaints,
17928 _("mangled .debug_line section"));
17929 return;
17930 }
17931 /* Make sure that we parsed the extended op correctly. If e.g.
17932 we expected a different address size than the producer used,
17933 we may have read the wrong number of bytes. */
17934 if (line_ptr != extended_end)
17935 {
17936 complaint (&symfile_complaints,
17937 _("mangled .debug_line section"));
17938 return;
17939 }
17940 break;
17941 case DW_LNS_copy:
17942 dwarf_record_line (&reader_state, &state_machine, 0);
17943 state_machine.discriminator = 0;
17944 break;
17945 case DW_LNS_advance_pc:
17946 {
17947 CORE_ADDR adjust
17948 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17949 CORE_ADDR addr_adj;
17950
17951 addr_adj = (((state_machine.op_index + adjust)
17952 / lh->maximum_ops_per_instruction)
17953 * lh->minimum_instruction_length);
17954 state_machine.address
17955 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17956 state_machine.op_index = ((state_machine.op_index + adjust)
17957 % lh->maximum_ops_per_instruction);
17958 line_ptr += bytes_read;
17959 }
17960 break;
17961 case DW_LNS_advance_line:
17962 {
17963 int line_delta
17964 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
17965
17966 state_machine.line += line_delta;
17967 if (line_delta != 0)
17968 state_machine.line_has_non_zero_discriminator
17969 = state_machine.discriminator != 0;
17970 line_ptr += bytes_read;
17971 }
17972 break;
17973 case DW_LNS_set_file:
17974 {
17975 /* The arrays lh->include_dirs and lh->file_names are
17976 0-based, but the directory and file name numbers in
17977 the statement program are 1-based. */
17978 struct file_entry *fe;
17979 const char *dir = NULL;
17980
17981 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
17982 &bytes_read);
17983 line_ptr += bytes_read;
17984 if (state_machine.file == 0
17985 || state_machine.file - 1 >= lh->num_file_names)
17986 dwarf2_debug_line_missing_file_complaint ();
17987 else
17988 {
17989 fe = &lh->file_names[state_machine.file - 1];
17990 if (fe->dir_index && lh->include_dirs != NULL)
17991 dir = lh->include_dirs[fe->dir_index - 1];
17992 if (record_lines_p)
17993 {
17994 state_machine.last_subfile = current_subfile;
17995 state_machine.line_has_non_zero_discriminator
17996 = state_machine.discriminator != 0;
17997 dwarf2_start_subfile (fe->name, dir);
17998 }
17999 }
18000 }
18001 break;
18002 case DW_LNS_set_column:
18003 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18004 line_ptr += bytes_read;
18005 break;
18006 case DW_LNS_negate_stmt:
18007 state_machine.is_stmt = (!state_machine.is_stmt);
18008 break;
18009 case DW_LNS_set_basic_block:
18010 break;
18011 /* Add to the address register of the state machine the
18012 address increment value corresponding to special opcode
18013 255. I.e., this value is scaled by the minimum
18014 instruction length since special opcode 255 would have
18015 scaled the increment. */
18016 case DW_LNS_const_add_pc:
18017 {
18018 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18019 CORE_ADDR addr_adj;
18020
18021 addr_adj = (((state_machine.op_index + adjust)
18022 / lh->maximum_ops_per_instruction)
18023 * lh->minimum_instruction_length);
18024 state_machine.address
18025 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18026 state_machine.op_index = ((state_machine.op_index + adjust)
18027 % lh->maximum_ops_per_instruction);
18028 }
18029 break;
18030 case DW_LNS_fixed_advance_pc:
18031 {
18032 CORE_ADDR addr_adj;
18033
18034 addr_adj = read_2_bytes (abfd, line_ptr);
18035 state_machine.address
18036 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18037 state_machine.op_index = 0;
18038 line_ptr += 2;
18039 }
18040 break;
18041 default:
18042 {
18043 /* Unknown standard opcode, ignore it. */
18044 int i;
18045
18046 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18047 {
18048 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18049 line_ptr += bytes_read;
18050 }
18051 }
18052 }
18053 }
18054
18055 if (!end_sequence)
18056 dwarf2_debug_line_missing_end_sequence_complaint ();
18057
18058 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18059 in which case we still finish recording the last line). */
18060 dwarf_record_line (&reader_state, &state_machine, 1);
18061 }
18062 }
18063
18064 /* Decode the Line Number Program (LNP) for the given line_header
18065 structure and CU. The actual information extracted and the type
18066 of structures created from the LNP depends on the value of PST.
18067
18068 1. If PST is NULL, then this procedure uses the data from the program
18069 to create all necessary symbol tables, and their linetables.
18070
18071 2. If PST is not NULL, this procedure reads the program to determine
18072 the list of files included by the unit represented by PST, and
18073 builds all the associated partial symbol tables.
18074
18075 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18076 It is used for relative paths in the line table.
18077 NOTE: When processing partial symtabs (pst != NULL),
18078 comp_dir == pst->dirname.
18079
18080 NOTE: It is important that psymtabs have the same file name (via strcmp)
18081 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18082 symtab we don't use it in the name of the psymtabs we create.
18083 E.g. expand_line_sal requires this when finding psymtabs to expand.
18084 A good testcase for this is mb-inline.exp.
18085
18086 LOWPC is the lowest address in CU (or 0 if not known).
18087
18088 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18089 for its PC<->lines mapping information. Otherwise only the filename
18090 table is read in. */
18091
18092 static void
18093 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18094 struct dwarf2_cu *cu, struct partial_symtab *pst,
18095 CORE_ADDR lowpc, int decode_mapping)
18096 {
18097 struct objfile *objfile = cu->objfile;
18098 const int decode_for_pst_p = (pst != NULL);
18099
18100 if (decode_mapping)
18101 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18102
18103 if (decode_for_pst_p)
18104 {
18105 int file_index;
18106
18107 /* Now that we're done scanning the Line Header Program, we can
18108 create the psymtab of each included file. */
18109 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18110 if (lh->file_names[file_index].included_p == 1)
18111 {
18112 const char *include_name =
18113 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18114 if (include_name != NULL)
18115 dwarf2_create_include_psymtab (include_name, pst, objfile);
18116 }
18117 }
18118 else
18119 {
18120 /* Make sure a symtab is created for every file, even files
18121 which contain only variables (i.e. no code with associated
18122 line numbers). */
18123 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18124 int i;
18125
18126 for (i = 0; i < lh->num_file_names; i++)
18127 {
18128 const char *dir = NULL;
18129 struct file_entry *fe;
18130
18131 fe = &lh->file_names[i];
18132 if (fe->dir_index && lh->include_dirs != NULL)
18133 dir = lh->include_dirs[fe->dir_index - 1];
18134 dwarf2_start_subfile (fe->name, dir);
18135
18136 if (current_subfile->symtab == NULL)
18137 {
18138 current_subfile->symtab
18139 = allocate_symtab (cust, current_subfile->name);
18140 }
18141 fe->symtab = current_subfile->symtab;
18142 }
18143 }
18144 }
18145
18146 /* Start a subfile for DWARF. FILENAME is the name of the file and
18147 DIRNAME the name of the source directory which contains FILENAME
18148 or NULL if not known.
18149 This routine tries to keep line numbers from identical absolute and
18150 relative file names in a common subfile.
18151
18152 Using the `list' example from the GDB testsuite, which resides in
18153 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18154 of /srcdir/list0.c yields the following debugging information for list0.c:
18155
18156 DW_AT_name: /srcdir/list0.c
18157 DW_AT_comp_dir: /compdir
18158 files.files[0].name: list0.h
18159 files.files[0].dir: /srcdir
18160 files.files[1].name: list0.c
18161 files.files[1].dir: /srcdir
18162
18163 The line number information for list0.c has to end up in a single
18164 subfile, so that `break /srcdir/list0.c:1' works as expected.
18165 start_subfile will ensure that this happens provided that we pass the
18166 concatenation of files.files[1].dir and files.files[1].name as the
18167 subfile's name. */
18168
18169 static void
18170 dwarf2_start_subfile (const char *filename, const char *dirname)
18171 {
18172 char *copy = NULL;
18173
18174 /* In order not to lose the line information directory,
18175 we concatenate it to the filename when it makes sense.
18176 Note that the Dwarf3 standard says (speaking of filenames in line
18177 information): ``The directory index is ignored for file names
18178 that represent full path names''. Thus ignoring dirname in the
18179 `else' branch below isn't an issue. */
18180
18181 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18182 {
18183 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18184 filename = copy;
18185 }
18186
18187 start_subfile (filename);
18188
18189 if (copy != NULL)
18190 xfree (copy);
18191 }
18192
18193 /* Start a symtab for DWARF.
18194 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18195
18196 static struct compunit_symtab *
18197 dwarf2_start_symtab (struct dwarf2_cu *cu,
18198 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18199 {
18200 struct compunit_symtab *cust
18201 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18202
18203 record_debugformat ("DWARF 2");
18204 record_producer (cu->producer);
18205
18206 /* We assume that we're processing GCC output. */
18207 processing_gcc_compilation = 2;
18208
18209 cu->processing_has_namespace_info = 0;
18210
18211 return cust;
18212 }
18213
18214 static void
18215 var_decode_location (struct attribute *attr, struct symbol *sym,
18216 struct dwarf2_cu *cu)
18217 {
18218 struct objfile *objfile = cu->objfile;
18219 struct comp_unit_head *cu_header = &cu->header;
18220
18221 /* NOTE drow/2003-01-30: There used to be a comment and some special
18222 code here to turn a symbol with DW_AT_external and a
18223 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18224 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18225 with some versions of binutils) where shared libraries could have
18226 relocations against symbols in their debug information - the
18227 minimal symbol would have the right address, but the debug info
18228 would not. It's no longer necessary, because we will explicitly
18229 apply relocations when we read in the debug information now. */
18230
18231 /* A DW_AT_location attribute with no contents indicates that a
18232 variable has been optimized away. */
18233 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18234 {
18235 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18236 return;
18237 }
18238
18239 /* Handle one degenerate form of location expression specially, to
18240 preserve GDB's previous behavior when section offsets are
18241 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18242 then mark this symbol as LOC_STATIC. */
18243
18244 if (attr_form_is_block (attr)
18245 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18246 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18247 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18248 && (DW_BLOCK (attr)->size
18249 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18250 {
18251 unsigned int dummy;
18252
18253 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18254 SYMBOL_VALUE_ADDRESS (sym) =
18255 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18256 else
18257 SYMBOL_VALUE_ADDRESS (sym) =
18258 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18259 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18260 fixup_symbol_section (sym, objfile);
18261 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18262 SYMBOL_SECTION (sym));
18263 return;
18264 }
18265
18266 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18267 expression evaluator, and use LOC_COMPUTED only when necessary
18268 (i.e. when the value of a register or memory location is
18269 referenced, or a thread-local block, etc.). Then again, it might
18270 not be worthwhile. I'm assuming that it isn't unless performance
18271 or memory numbers show me otherwise. */
18272
18273 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18274
18275 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18276 cu->has_loclist = 1;
18277 }
18278
18279 /* Given a pointer to a DWARF information entry, figure out if we need
18280 to make a symbol table entry for it, and if so, create a new entry
18281 and return a pointer to it.
18282 If TYPE is NULL, determine symbol type from the die, otherwise
18283 used the passed type.
18284 If SPACE is not NULL, use it to hold the new symbol. If it is
18285 NULL, allocate a new symbol on the objfile's obstack. */
18286
18287 static struct symbol *
18288 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18289 struct symbol *space)
18290 {
18291 struct objfile *objfile = cu->objfile;
18292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18293 struct symbol *sym = NULL;
18294 const char *name;
18295 struct attribute *attr = NULL;
18296 struct attribute *attr2 = NULL;
18297 CORE_ADDR baseaddr;
18298 struct pending **list_to_add = NULL;
18299
18300 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18301
18302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18303
18304 name = dwarf2_name (die, cu);
18305 if (name)
18306 {
18307 const char *linkagename;
18308 int suppress_add = 0;
18309
18310 if (space)
18311 sym = space;
18312 else
18313 sym = allocate_symbol (objfile);
18314 OBJSTAT (objfile, n_syms++);
18315
18316 /* Cache this symbol's name and the name's demangled form (if any). */
18317 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18318 linkagename = dwarf2_physname (name, die, cu);
18319 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18320
18321 /* Fortran does not have mangling standard and the mangling does differ
18322 between gfortran, iFort etc. */
18323 if (cu->language == language_fortran
18324 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18325 symbol_set_demangled_name (&(sym->ginfo),
18326 dwarf2_full_name (name, die, cu),
18327 NULL);
18328
18329 /* Default assumptions.
18330 Use the passed type or decode it from the die. */
18331 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18332 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18333 if (type != NULL)
18334 SYMBOL_TYPE (sym) = type;
18335 else
18336 SYMBOL_TYPE (sym) = die_type (die, cu);
18337 attr = dwarf2_attr (die,
18338 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18339 cu);
18340 if (attr)
18341 {
18342 SYMBOL_LINE (sym) = DW_UNSND (attr);
18343 }
18344
18345 attr = dwarf2_attr (die,
18346 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18347 cu);
18348 if (attr)
18349 {
18350 int file_index = DW_UNSND (attr);
18351
18352 if (cu->line_header == NULL
18353 || file_index > cu->line_header->num_file_names)
18354 complaint (&symfile_complaints,
18355 _("file index out of range"));
18356 else if (file_index > 0)
18357 {
18358 struct file_entry *fe;
18359
18360 fe = &cu->line_header->file_names[file_index - 1];
18361 symbol_set_symtab (sym, fe->symtab);
18362 }
18363 }
18364
18365 switch (die->tag)
18366 {
18367 case DW_TAG_label:
18368 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18369 if (attr)
18370 {
18371 CORE_ADDR addr;
18372
18373 addr = attr_value_as_address (attr);
18374 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18375 SYMBOL_VALUE_ADDRESS (sym) = addr;
18376 }
18377 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18378 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18379 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18380 add_symbol_to_list (sym, cu->list_in_scope);
18381 break;
18382 case DW_TAG_subprogram:
18383 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18384 finish_block. */
18385 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18387 if ((attr2 && (DW_UNSND (attr2) != 0))
18388 || cu->language == language_ada)
18389 {
18390 /* Subprograms marked external are stored as a global symbol.
18391 Ada subprograms, whether marked external or not, are always
18392 stored as a global symbol, because we want to be able to
18393 access them globally. For instance, we want to be able
18394 to break on a nested subprogram without having to
18395 specify the context. */
18396 list_to_add = &global_symbols;
18397 }
18398 else
18399 {
18400 list_to_add = cu->list_in_scope;
18401 }
18402 break;
18403 case DW_TAG_inlined_subroutine:
18404 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18405 finish_block. */
18406 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18407 SYMBOL_INLINED (sym) = 1;
18408 list_to_add = cu->list_in_scope;
18409 break;
18410 case DW_TAG_template_value_param:
18411 suppress_add = 1;
18412 /* Fall through. */
18413 case DW_TAG_constant:
18414 case DW_TAG_variable:
18415 case DW_TAG_member:
18416 /* Compilation with minimal debug info may result in
18417 variables with missing type entries. Change the
18418 misleading `void' type to something sensible. */
18419 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18420 SYMBOL_TYPE (sym)
18421 = objfile_type (objfile)->nodebug_data_symbol;
18422
18423 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18424 /* In the case of DW_TAG_member, we should only be called for
18425 static const members. */
18426 if (die->tag == DW_TAG_member)
18427 {
18428 /* dwarf2_add_field uses die_is_declaration,
18429 so we do the same. */
18430 gdb_assert (die_is_declaration (die, cu));
18431 gdb_assert (attr);
18432 }
18433 if (attr)
18434 {
18435 dwarf2_const_value (attr, sym, cu);
18436 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18437 if (!suppress_add)
18438 {
18439 if (attr2 && (DW_UNSND (attr2) != 0))
18440 list_to_add = &global_symbols;
18441 else
18442 list_to_add = cu->list_in_scope;
18443 }
18444 break;
18445 }
18446 attr = dwarf2_attr (die, DW_AT_location, cu);
18447 if (attr)
18448 {
18449 var_decode_location (attr, sym, cu);
18450 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18451
18452 /* Fortran explicitly imports any global symbols to the local
18453 scope by DW_TAG_common_block. */
18454 if (cu->language == language_fortran && die->parent
18455 && die->parent->tag == DW_TAG_common_block)
18456 attr2 = NULL;
18457
18458 if (SYMBOL_CLASS (sym) == LOC_STATIC
18459 && SYMBOL_VALUE_ADDRESS (sym) == 0
18460 && !dwarf2_per_objfile->has_section_at_zero)
18461 {
18462 /* When a static variable is eliminated by the linker,
18463 the corresponding debug information is not stripped
18464 out, but the variable address is set to null;
18465 do not add such variables into symbol table. */
18466 }
18467 else if (attr2 && (DW_UNSND (attr2) != 0))
18468 {
18469 /* Workaround gfortran PR debug/40040 - it uses
18470 DW_AT_location for variables in -fPIC libraries which may
18471 get overriden by other libraries/executable and get
18472 a different address. Resolve it by the minimal symbol
18473 which may come from inferior's executable using copy
18474 relocation. Make this workaround only for gfortran as for
18475 other compilers GDB cannot guess the minimal symbol
18476 Fortran mangling kind. */
18477 if (cu->language == language_fortran && die->parent
18478 && die->parent->tag == DW_TAG_module
18479 && cu->producer
18480 && startswith (cu->producer, "GNU Fortran "))
18481 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18482
18483 /* A variable with DW_AT_external is never static,
18484 but it may be block-scoped. */
18485 list_to_add = (cu->list_in_scope == &file_symbols
18486 ? &global_symbols : cu->list_in_scope);
18487 }
18488 else
18489 list_to_add = cu->list_in_scope;
18490 }
18491 else
18492 {
18493 /* We do not know the address of this symbol.
18494 If it is an external symbol and we have type information
18495 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18496 The address of the variable will then be determined from
18497 the minimal symbol table whenever the variable is
18498 referenced. */
18499 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18500
18501 /* Fortran explicitly imports any global symbols to the local
18502 scope by DW_TAG_common_block. */
18503 if (cu->language == language_fortran && die->parent
18504 && die->parent->tag == DW_TAG_common_block)
18505 {
18506 /* SYMBOL_CLASS doesn't matter here because
18507 read_common_block is going to reset it. */
18508 if (!suppress_add)
18509 list_to_add = cu->list_in_scope;
18510 }
18511 else if (attr2 && (DW_UNSND (attr2) != 0)
18512 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
18513 {
18514 /* A variable with DW_AT_external is never static, but it
18515 may be block-scoped. */
18516 list_to_add = (cu->list_in_scope == &file_symbols
18517 ? &global_symbols : cu->list_in_scope);
18518
18519 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18520 }
18521 else if (!die_is_declaration (die, cu))
18522 {
18523 /* Use the default LOC_OPTIMIZED_OUT class. */
18524 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
18525 if (!suppress_add)
18526 list_to_add = cu->list_in_scope;
18527 }
18528 }
18529 break;
18530 case DW_TAG_formal_parameter:
18531 /* If we are inside a function, mark this as an argument. If
18532 not, we might be looking at an argument to an inlined function
18533 when we do not have enough information to show inlined frames;
18534 pretend it's a local variable in that case so that the user can
18535 still see it. */
18536 if (context_stack_depth > 0
18537 && context_stack[context_stack_depth - 1].name != NULL)
18538 SYMBOL_IS_ARGUMENT (sym) = 1;
18539 attr = dwarf2_attr (die, DW_AT_location, cu);
18540 if (attr)
18541 {
18542 var_decode_location (attr, sym, cu);
18543 }
18544 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18545 if (attr)
18546 {
18547 dwarf2_const_value (attr, sym, cu);
18548 }
18549
18550 list_to_add = cu->list_in_scope;
18551 break;
18552 case DW_TAG_unspecified_parameters:
18553 /* From varargs functions; gdb doesn't seem to have any
18554 interest in this information, so just ignore it for now.
18555 (FIXME?) */
18556 break;
18557 case DW_TAG_template_type_param:
18558 suppress_add = 1;
18559 /* Fall through. */
18560 case DW_TAG_class_type:
18561 case DW_TAG_interface_type:
18562 case DW_TAG_structure_type:
18563 case DW_TAG_union_type:
18564 case DW_TAG_set_type:
18565 case DW_TAG_enumeration_type:
18566 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18567 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
18568
18569 {
18570 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
18571 really ever be static objects: otherwise, if you try
18572 to, say, break of a class's method and you're in a file
18573 which doesn't mention that class, it won't work unless
18574 the check for all static symbols in lookup_symbol_aux
18575 saves you. See the OtherFileClass tests in
18576 gdb.c++/namespace.exp. */
18577
18578 if (!suppress_add)
18579 {
18580 list_to_add = (cu->list_in_scope == &file_symbols
18581 && (cu->language == language_cplus
18582 || cu->language == language_java)
18583 ? &global_symbols : cu->list_in_scope);
18584
18585 /* The semantics of C++ state that "struct foo {
18586 ... }" also defines a typedef for "foo". A Java
18587 class declaration also defines a typedef for the
18588 class. */
18589 if (cu->language == language_cplus
18590 || cu->language == language_java
18591 || cu->language == language_ada
18592 || cu->language == language_d)
18593 {
18594 /* The symbol's name is already allocated along
18595 with this objfile, so we don't need to
18596 duplicate it for the type. */
18597 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
18598 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
18599 }
18600 }
18601 }
18602 break;
18603 case DW_TAG_typedef:
18604 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18605 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18606 list_to_add = cu->list_in_scope;
18607 break;
18608 case DW_TAG_base_type:
18609 case DW_TAG_subrange_type:
18610 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18611 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18612 list_to_add = cu->list_in_scope;
18613 break;
18614 case DW_TAG_enumerator:
18615 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18616 if (attr)
18617 {
18618 dwarf2_const_value (attr, sym, cu);
18619 }
18620 {
18621 /* NOTE: carlton/2003-11-10: See comment above in the
18622 DW_TAG_class_type, etc. block. */
18623
18624 list_to_add = (cu->list_in_scope == &file_symbols
18625 && (cu->language == language_cplus
18626 || cu->language == language_java)
18627 ? &global_symbols : cu->list_in_scope);
18628 }
18629 break;
18630 case DW_TAG_imported_declaration:
18631 case DW_TAG_namespace:
18632 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18633 list_to_add = &global_symbols;
18634 break;
18635 case DW_TAG_module:
18636 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18637 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
18638 list_to_add = &global_symbols;
18639 break;
18640 case DW_TAG_common_block:
18641 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
18642 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18643 add_symbol_to_list (sym, cu->list_in_scope);
18644 break;
18645 default:
18646 /* Not a tag we recognize. Hopefully we aren't processing
18647 trash data, but since we must specifically ignore things
18648 we don't recognize, there is nothing else we should do at
18649 this point. */
18650 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
18651 dwarf_tag_name (die->tag));
18652 break;
18653 }
18654
18655 if (suppress_add)
18656 {
18657 sym->hash_next = objfile->template_symbols;
18658 objfile->template_symbols = sym;
18659 list_to_add = NULL;
18660 }
18661
18662 if (list_to_add != NULL)
18663 add_symbol_to_list (sym, list_to_add);
18664
18665 /* For the benefit of old versions of GCC, check for anonymous
18666 namespaces based on the demangled name. */
18667 if (!cu->processing_has_namespace_info
18668 && cu->language == language_cplus)
18669 cp_scan_for_anonymous_namespaces (sym, objfile);
18670 }
18671 return (sym);
18672 }
18673
18674 /* A wrapper for new_symbol_full that always allocates a new symbol. */
18675
18676 static struct symbol *
18677 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18678 {
18679 return new_symbol_full (die, type, cu, NULL);
18680 }
18681
18682 /* Given an attr with a DW_FORM_dataN value in host byte order,
18683 zero-extend it as appropriate for the symbol's type. The DWARF
18684 standard (v4) is not entirely clear about the meaning of using
18685 DW_FORM_dataN for a constant with a signed type, where the type is
18686 wider than the data. The conclusion of a discussion on the DWARF
18687 list was that this is unspecified. We choose to always zero-extend
18688 because that is the interpretation long in use by GCC. */
18689
18690 static gdb_byte *
18691 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
18692 struct dwarf2_cu *cu, LONGEST *value, int bits)
18693 {
18694 struct objfile *objfile = cu->objfile;
18695 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18696 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
18697 LONGEST l = DW_UNSND (attr);
18698
18699 if (bits < sizeof (*value) * 8)
18700 {
18701 l &= ((LONGEST) 1 << bits) - 1;
18702 *value = l;
18703 }
18704 else if (bits == sizeof (*value) * 8)
18705 *value = l;
18706 else
18707 {
18708 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
18709 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18710 return bytes;
18711 }
18712
18713 return NULL;
18714 }
18715
18716 /* Read a constant value from an attribute. Either set *VALUE, or if
18717 the value does not fit in *VALUE, set *BYTES - either already
18718 allocated on the objfile obstack, or newly allocated on OBSTACK,
18719 or, set *BATON, if we translated the constant to a location
18720 expression. */
18721
18722 static void
18723 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
18724 const char *name, struct obstack *obstack,
18725 struct dwarf2_cu *cu,
18726 LONGEST *value, const gdb_byte **bytes,
18727 struct dwarf2_locexpr_baton **baton)
18728 {
18729 struct objfile *objfile = cu->objfile;
18730 struct comp_unit_head *cu_header = &cu->header;
18731 struct dwarf_block *blk;
18732 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
18733 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18734
18735 *value = 0;
18736 *bytes = NULL;
18737 *baton = NULL;
18738
18739 switch (attr->form)
18740 {
18741 case DW_FORM_addr:
18742 case DW_FORM_GNU_addr_index:
18743 {
18744 gdb_byte *data;
18745
18746 if (TYPE_LENGTH (type) != cu_header->addr_size)
18747 dwarf2_const_value_length_mismatch_complaint (name,
18748 cu_header->addr_size,
18749 TYPE_LENGTH (type));
18750 /* Symbols of this form are reasonably rare, so we just
18751 piggyback on the existing location code rather than writing
18752 a new implementation of symbol_computed_ops. */
18753 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
18754 (*baton)->per_cu = cu->per_cu;
18755 gdb_assert ((*baton)->per_cu);
18756
18757 (*baton)->size = 2 + cu_header->addr_size;
18758 data = obstack_alloc (obstack, (*baton)->size);
18759 (*baton)->data = data;
18760
18761 data[0] = DW_OP_addr;
18762 store_unsigned_integer (&data[1], cu_header->addr_size,
18763 byte_order, DW_ADDR (attr));
18764 data[cu_header->addr_size + 1] = DW_OP_stack_value;
18765 }
18766 break;
18767 case DW_FORM_string:
18768 case DW_FORM_strp:
18769 case DW_FORM_GNU_str_index:
18770 case DW_FORM_GNU_strp_alt:
18771 /* DW_STRING is already allocated on the objfile obstack, point
18772 directly to it. */
18773 *bytes = (const gdb_byte *) DW_STRING (attr);
18774 break;
18775 case DW_FORM_block1:
18776 case DW_FORM_block2:
18777 case DW_FORM_block4:
18778 case DW_FORM_block:
18779 case DW_FORM_exprloc:
18780 blk = DW_BLOCK (attr);
18781 if (TYPE_LENGTH (type) != blk->size)
18782 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
18783 TYPE_LENGTH (type));
18784 *bytes = blk->data;
18785 break;
18786
18787 /* The DW_AT_const_value attributes are supposed to carry the
18788 symbol's value "represented as it would be on the target
18789 architecture." By the time we get here, it's already been
18790 converted to host endianness, so we just need to sign- or
18791 zero-extend it as appropriate. */
18792 case DW_FORM_data1:
18793 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
18794 break;
18795 case DW_FORM_data2:
18796 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
18797 break;
18798 case DW_FORM_data4:
18799 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
18800 break;
18801 case DW_FORM_data8:
18802 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
18803 break;
18804
18805 case DW_FORM_sdata:
18806 *value = DW_SND (attr);
18807 break;
18808
18809 case DW_FORM_udata:
18810 *value = DW_UNSND (attr);
18811 break;
18812
18813 default:
18814 complaint (&symfile_complaints,
18815 _("unsupported const value attribute form: '%s'"),
18816 dwarf_form_name (attr->form));
18817 *value = 0;
18818 break;
18819 }
18820 }
18821
18822
18823 /* Copy constant value from an attribute to a symbol. */
18824
18825 static void
18826 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
18827 struct dwarf2_cu *cu)
18828 {
18829 struct objfile *objfile = cu->objfile;
18830 struct comp_unit_head *cu_header = &cu->header;
18831 LONGEST value;
18832 const gdb_byte *bytes;
18833 struct dwarf2_locexpr_baton *baton;
18834
18835 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18836 SYMBOL_PRINT_NAME (sym),
18837 &objfile->objfile_obstack, cu,
18838 &value, &bytes, &baton);
18839
18840 if (baton != NULL)
18841 {
18842 SYMBOL_LOCATION_BATON (sym) = baton;
18843 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
18844 }
18845 else if (bytes != NULL)
18846 {
18847 SYMBOL_VALUE_BYTES (sym) = bytes;
18848 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
18849 }
18850 else
18851 {
18852 SYMBOL_VALUE (sym) = value;
18853 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
18854 }
18855 }
18856
18857 /* Return the type of the die in question using its DW_AT_type attribute. */
18858
18859 static struct type *
18860 die_type (struct die_info *die, struct dwarf2_cu *cu)
18861 {
18862 struct attribute *type_attr;
18863
18864 type_attr = dwarf2_attr (die, DW_AT_type, cu);
18865 if (!type_attr)
18866 {
18867 /* A missing DW_AT_type represents a void type. */
18868 return objfile_type (cu->objfile)->builtin_void;
18869 }
18870
18871 return lookup_die_type (die, type_attr, cu);
18872 }
18873
18874 /* True iff CU's producer generates GNAT Ada auxiliary information
18875 that allows to find parallel types through that information instead
18876 of having to do expensive parallel lookups by type name. */
18877
18878 static int
18879 need_gnat_info (struct dwarf2_cu *cu)
18880 {
18881 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
18882 of GNAT produces this auxiliary information, without any indication
18883 that it is produced. Part of enhancing the FSF version of GNAT
18884 to produce that information will be to put in place an indicator
18885 that we can use in order to determine whether the descriptive type
18886 info is available or not. One suggestion that has been made is
18887 to use a new attribute, attached to the CU die. For now, assume
18888 that the descriptive type info is not available. */
18889 return 0;
18890 }
18891
18892 /* Return the auxiliary type of the die in question using its
18893 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
18894 attribute is not present. */
18895
18896 static struct type *
18897 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18898 {
18899 struct attribute *type_attr;
18900
18901 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18902 if (!type_attr)
18903 return NULL;
18904
18905 return lookup_die_type (die, type_attr, cu);
18906 }
18907
18908 /* If DIE has a descriptive_type attribute, then set the TYPE's
18909 descriptive type accordingly. */
18910
18911 static void
18912 set_descriptive_type (struct type *type, struct die_info *die,
18913 struct dwarf2_cu *cu)
18914 {
18915 struct type *descriptive_type = die_descriptive_type (die, cu);
18916
18917 if (descriptive_type)
18918 {
18919 ALLOCATE_GNAT_AUX_TYPE (type);
18920 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
18921 }
18922 }
18923
18924 /* Return the containing type of the die in question using its
18925 DW_AT_containing_type attribute. */
18926
18927 static struct type *
18928 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
18929 {
18930 struct attribute *type_attr;
18931
18932 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
18933 if (!type_attr)
18934 error (_("Dwarf Error: Problem turning containing type into gdb type "
18935 "[in module %s]"), objfile_name (cu->objfile));
18936
18937 return lookup_die_type (die, type_attr, cu);
18938 }
18939
18940 /* Return an error marker type to use for the ill formed type in DIE/CU. */
18941
18942 static struct type *
18943 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
18944 {
18945 struct objfile *objfile = dwarf2_per_objfile->objfile;
18946 char *message, *saved;
18947
18948 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
18949 objfile_name (objfile),
18950 cu->header.offset.sect_off,
18951 die->offset.sect_off);
18952 saved = obstack_copy0 (&objfile->objfile_obstack,
18953 message, strlen (message));
18954 xfree (message);
18955
18956 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
18957 }
18958
18959 /* Look up the type of DIE in CU using its type attribute ATTR.
18960 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
18961 DW_AT_containing_type.
18962 If there is no type substitute an error marker. */
18963
18964 static struct type *
18965 lookup_die_type (struct die_info *die, const struct attribute *attr,
18966 struct dwarf2_cu *cu)
18967 {
18968 struct objfile *objfile = cu->objfile;
18969 struct type *this_type;
18970
18971 gdb_assert (attr->name == DW_AT_type
18972 || attr->name == DW_AT_GNAT_descriptive_type
18973 || attr->name == DW_AT_containing_type);
18974
18975 /* First see if we have it cached. */
18976
18977 if (attr->form == DW_FORM_GNU_ref_alt)
18978 {
18979 struct dwarf2_per_cu_data *per_cu;
18980 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18981
18982 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
18983 this_type = get_die_type_at_offset (offset, per_cu);
18984 }
18985 else if (attr_form_is_ref (attr))
18986 {
18987 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18988
18989 this_type = get_die_type_at_offset (offset, cu->per_cu);
18990 }
18991 else if (attr->form == DW_FORM_ref_sig8)
18992 {
18993 ULONGEST signature = DW_SIGNATURE (attr);
18994
18995 return get_signatured_type (die, signature, cu);
18996 }
18997 else
18998 {
18999 complaint (&symfile_complaints,
19000 _("Dwarf Error: Bad type attribute %s in DIE"
19001 " at 0x%x [in module %s]"),
19002 dwarf_attr_name (attr->name), die->offset.sect_off,
19003 objfile_name (objfile));
19004 return build_error_marker_type (cu, die);
19005 }
19006
19007 /* If not cached we need to read it in. */
19008
19009 if (this_type == NULL)
19010 {
19011 struct die_info *type_die = NULL;
19012 struct dwarf2_cu *type_cu = cu;
19013
19014 if (attr_form_is_ref (attr))
19015 type_die = follow_die_ref (die, attr, &type_cu);
19016 if (type_die == NULL)
19017 return build_error_marker_type (cu, die);
19018 /* If we find the type now, it's probably because the type came
19019 from an inter-CU reference and the type's CU got expanded before
19020 ours. */
19021 this_type = read_type_die (type_die, type_cu);
19022 }
19023
19024 /* If we still don't have a type use an error marker. */
19025
19026 if (this_type == NULL)
19027 return build_error_marker_type (cu, die);
19028
19029 return this_type;
19030 }
19031
19032 /* Return the type in DIE, CU.
19033 Returns NULL for invalid types.
19034
19035 This first does a lookup in die_type_hash,
19036 and only reads the die in if necessary.
19037
19038 NOTE: This can be called when reading in partial or full symbols. */
19039
19040 static struct type *
19041 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19042 {
19043 struct type *this_type;
19044
19045 this_type = get_die_type (die, cu);
19046 if (this_type)
19047 return this_type;
19048
19049 return read_type_die_1 (die, cu);
19050 }
19051
19052 /* Read the type in DIE, CU.
19053 Returns NULL for invalid types. */
19054
19055 static struct type *
19056 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19057 {
19058 struct type *this_type = NULL;
19059
19060 switch (die->tag)
19061 {
19062 case DW_TAG_class_type:
19063 case DW_TAG_interface_type:
19064 case DW_TAG_structure_type:
19065 case DW_TAG_union_type:
19066 this_type = read_structure_type (die, cu);
19067 break;
19068 case DW_TAG_enumeration_type:
19069 this_type = read_enumeration_type (die, cu);
19070 break;
19071 case DW_TAG_subprogram:
19072 case DW_TAG_subroutine_type:
19073 case DW_TAG_inlined_subroutine:
19074 this_type = read_subroutine_type (die, cu);
19075 break;
19076 case DW_TAG_array_type:
19077 this_type = read_array_type (die, cu);
19078 break;
19079 case DW_TAG_set_type:
19080 this_type = read_set_type (die, cu);
19081 break;
19082 case DW_TAG_pointer_type:
19083 this_type = read_tag_pointer_type (die, cu);
19084 break;
19085 case DW_TAG_ptr_to_member_type:
19086 this_type = read_tag_ptr_to_member_type (die, cu);
19087 break;
19088 case DW_TAG_reference_type:
19089 this_type = read_tag_reference_type (die, cu);
19090 break;
19091 case DW_TAG_const_type:
19092 this_type = read_tag_const_type (die, cu);
19093 break;
19094 case DW_TAG_volatile_type:
19095 this_type = read_tag_volatile_type (die, cu);
19096 break;
19097 case DW_TAG_restrict_type:
19098 this_type = read_tag_restrict_type (die, cu);
19099 break;
19100 case DW_TAG_string_type:
19101 this_type = read_tag_string_type (die, cu);
19102 break;
19103 case DW_TAG_typedef:
19104 this_type = read_typedef (die, cu);
19105 break;
19106 case DW_TAG_subrange_type:
19107 this_type = read_subrange_type (die, cu);
19108 break;
19109 case DW_TAG_base_type:
19110 this_type = read_base_type (die, cu);
19111 break;
19112 case DW_TAG_unspecified_type:
19113 this_type = read_unspecified_type (die, cu);
19114 break;
19115 case DW_TAG_namespace:
19116 this_type = read_namespace_type (die, cu);
19117 break;
19118 case DW_TAG_module:
19119 this_type = read_module_type (die, cu);
19120 break;
19121 case DW_TAG_atomic_type:
19122 this_type = read_tag_atomic_type (die, cu);
19123 break;
19124 default:
19125 complaint (&symfile_complaints,
19126 _("unexpected tag in read_type_die: '%s'"),
19127 dwarf_tag_name (die->tag));
19128 break;
19129 }
19130
19131 return this_type;
19132 }
19133
19134 /* See if we can figure out if the class lives in a namespace. We do
19135 this by looking for a member function; its demangled name will
19136 contain namespace info, if there is any.
19137 Return the computed name or NULL.
19138 Space for the result is allocated on the objfile's obstack.
19139 This is the full-die version of guess_partial_die_structure_name.
19140 In this case we know DIE has no useful parent. */
19141
19142 static char *
19143 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19144 {
19145 struct die_info *spec_die;
19146 struct dwarf2_cu *spec_cu;
19147 struct die_info *child;
19148
19149 spec_cu = cu;
19150 spec_die = die_specification (die, &spec_cu);
19151 if (spec_die != NULL)
19152 {
19153 die = spec_die;
19154 cu = spec_cu;
19155 }
19156
19157 for (child = die->child;
19158 child != NULL;
19159 child = child->sibling)
19160 {
19161 if (child->tag == DW_TAG_subprogram)
19162 {
19163 const char *linkage_name;
19164
19165 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19166 if (linkage_name == NULL)
19167 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19168 cu);
19169 if (linkage_name != NULL)
19170 {
19171 char *actual_name
19172 = language_class_name_from_physname (cu->language_defn,
19173 linkage_name);
19174 char *name = NULL;
19175
19176 if (actual_name != NULL)
19177 {
19178 const char *die_name = dwarf2_name (die, cu);
19179
19180 if (die_name != NULL
19181 && strcmp (die_name, actual_name) != 0)
19182 {
19183 /* Strip off the class name from the full name.
19184 We want the prefix. */
19185 int die_name_len = strlen (die_name);
19186 int actual_name_len = strlen (actual_name);
19187
19188 /* Test for '::' as a sanity check. */
19189 if (actual_name_len > die_name_len + 2
19190 && actual_name[actual_name_len
19191 - die_name_len - 1] == ':')
19192 name =
19193 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19194 actual_name,
19195 actual_name_len - die_name_len - 2);
19196 }
19197 }
19198 xfree (actual_name);
19199 return name;
19200 }
19201 }
19202 }
19203
19204 return NULL;
19205 }
19206
19207 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19208 prefix part in such case. See
19209 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19210
19211 static char *
19212 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19213 {
19214 struct attribute *attr;
19215 char *base;
19216
19217 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19218 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19219 return NULL;
19220
19221 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19222 return NULL;
19223
19224 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19225 if (attr == NULL)
19226 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19227 if (attr == NULL || DW_STRING (attr) == NULL)
19228 return NULL;
19229
19230 /* dwarf2_name had to be already called. */
19231 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19232
19233 /* Strip the base name, keep any leading namespaces/classes. */
19234 base = strrchr (DW_STRING (attr), ':');
19235 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19236 return "";
19237
19238 return obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19239 DW_STRING (attr), &base[-1] - DW_STRING (attr));
19240 }
19241
19242 /* Return the name of the namespace/class that DIE is defined within,
19243 or "" if we can't tell. The caller should not xfree the result.
19244
19245 For example, if we're within the method foo() in the following
19246 code:
19247
19248 namespace N {
19249 class C {
19250 void foo () {
19251 }
19252 };
19253 }
19254
19255 then determine_prefix on foo's die will return "N::C". */
19256
19257 static const char *
19258 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19259 {
19260 struct die_info *parent, *spec_die;
19261 struct dwarf2_cu *spec_cu;
19262 struct type *parent_type;
19263 char *retval;
19264
19265 if (cu->language != language_cplus && cu->language != language_java
19266 && cu->language != language_fortran && cu->language != language_d)
19267 return "";
19268
19269 retval = anonymous_struct_prefix (die, cu);
19270 if (retval)
19271 return retval;
19272
19273 /* We have to be careful in the presence of DW_AT_specification.
19274 For example, with GCC 3.4, given the code
19275
19276 namespace N {
19277 void foo() {
19278 // Definition of N::foo.
19279 }
19280 }
19281
19282 then we'll have a tree of DIEs like this:
19283
19284 1: DW_TAG_compile_unit
19285 2: DW_TAG_namespace // N
19286 3: DW_TAG_subprogram // declaration of N::foo
19287 4: DW_TAG_subprogram // definition of N::foo
19288 DW_AT_specification // refers to die #3
19289
19290 Thus, when processing die #4, we have to pretend that we're in
19291 the context of its DW_AT_specification, namely the contex of die
19292 #3. */
19293 spec_cu = cu;
19294 spec_die = die_specification (die, &spec_cu);
19295 if (spec_die == NULL)
19296 parent = die->parent;
19297 else
19298 {
19299 parent = spec_die->parent;
19300 cu = spec_cu;
19301 }
19302
19303 if (parent == NULL)
19304 return "";
19305 else if (parent->building_fullname)
19306 {
19307 const char *name;
19308 const char *parent_name;
19309
19310 /* It has been seen on RealView 2.2 built binaries,
19311 DW_TAG_template_type_param types actually _defined_ as
19312 children of the parent class:
19313
19314 enum E {};
19315 template class <class Enum> Class{};
19316 Class<enum E> class_e;
19317
19318 1: DW_TAG_class_type (Class)
19319 2: DW_TAG_enumeration_type (E)
19320 3: DW_TAG_enumerator (enum1:0)
19321 3: DW_TAG_enumerator (enum2:1)
19322 ...
19323 2: DW_TAG_template_type_param
19324 DW_AT_type DW_FORM_ref_udata (E)
19325
19326 Besides being broken debug info, it can put GDB into an
19327 infinite loop. Consider:
19328
19329 When we're building the full name for Class<E>, we'll start
19330 at Class, and go look over its template type parameters,
19331 finding E. We'll then try to build the full name of E, and
19332 reach here. We're now trying to build the full name of E,
19333 and look over the parent DIE for containing scope. In the
19334 broken case, if we followed the parent DIE of E, we'd again
19335 find Class, and once again go look at its template type
19336 arguments, etc., etc. Simply don't consider such parent die
19337 as source-level parent of this die (it can't be, the language
19338 doesn't allow it), and break the loop here. */
19339 name = dwarf2_name (die, cu);
19340 parent_name = dwarf2_name (parent, cu);
19341 complaint (&symfile_complaints,
19342 _("template param type '%s' defined within parent '%s'"),
19343 name ? name : "<unknown>",
19344 parent_name ? parent_name : "<unknown>");
19345 return "";
19346 }
19347 else
19348 switch (parent->tag)
19349 {
19350 case DW_TAG_namespace:
19351 parent_type = read_type_die (parent, cu);
19352 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19353 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19354 Work around this problem here. */
19355 if (cu->language == language_cplus
19356 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19357 return "";
19358 /* We give a name to even anonymous namespaces. */
19359 return TYPE_TAG_NAME (parent_type);
19360 case DW_TAG_class_type:
19361 case DW_TAG_interface_type:
19362 case DW_TAG_structure_type:
19363 case DW_TAG_union_type:
19364 case DW_TAG_module:
19365 parent_type = read_type_die (parent, cu);
19366 if (TYPE_TAG_NAME (parent_type) != NULL)
19367 return TYPE_TAG_NAME (parent_type);
19368 else
19369 /* An anonymous structure is only allowed non-static data
19370 members; no typedefs, no member functions, et cetera.
19371 So it does not need a prefix. */
19372 return "";
19373 case DW_TAG_compile_unit:
19374 case DW_TAG_partial_unit:
19375 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19376 if (cu->language == language_cplus
19377 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19378 && die->child != NULL
19379 && (die->tag == DW_TAG_class_type
19380 || die->tag == DW_TAG_structure_type
19381 || die->tag == DW_TAG_union_type))
19382 {
19383 char *name = guess_full_die_structure_name (die, cu);
19384 if (name != NULL)
19385 return name;
19386 }
19387 return "";
19388 case DW_TAG_enumeration_type:
19389 parent_type = read_type_die (parent, cu);
19390 if (TYPE_DECLARED_CLASS (parent_type))
19391 {
19392 if (TYPE_TAG_NAME (parent_type) != NULL)
19393 return TYPE_TAG_NAME (parent_type);
19394 return "";
19395 }
19396 /* Fall through. */
19397 default:
19398 return determine_prefix (parent, cu);
19399 }
19400 }
19401
19402 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19403 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19404 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19405 an obconcat, otherwise allocate storage for the result. The CU argument is
19406 used to determine the language and hence, the appropriate separator. */
19407
19408 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19409
19410 static char *
19411 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19412 int physname, struct dwarf2_cu *cu)
19413 {
19414 const char *lead = "";
19415 const char *sep;
19416
19417 if (suffix == NULL || suffix[0] == '\0'
19418 || prefix == NULL || prefix[0] == '\0')
19419 sep = "";
19420 else if (cu->language == language_java)
19421 sep = ".";
19422 else if (cu->language == language_d)
19423 {
19424 /* For D, the 'main' function could be defined in any module, but it
19425 should never be prefixed. */
19426 if (strcmp (suffix, "D main") == 0)
19427 {
19428 prefix = "";
19429 sep = "";
19430 }
19431 else
19432 sep = ".";
19433 }
19434 else if (cu->language == language_fortran && physname)
19435 {
19436 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19437 DW_AT_MIPS_linkage_name is preferred and used instead. */
19438
19439 lead = "__";
19440 sep = "_MOD_";
19441 }
19442 else
19443 sep = "::";
19444
19445 if (prefix == NULL)
19446 prefix = "";
19447 if (suffix == NULL)
19448 suffix = "";
19449
19450 if (obs == NULL)
19451 {
19452 char *retval
19453 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
19454
19455 strcpy (retval, lead);
19456 strcat (retval, prefix);
19457 strcat (retval, sep);
19458 strcat (retval, suffix);
19459 return retval;
19460 }
19461 else
19462 {
19463 /* We have an obstack. */
19464 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19465 }
19466 }
19467
19468 /* Return sibling of die, NULL if no sibling. */
19469
19470 static struct die_info *
19471 sibling_die (struct die_info *die)
19472 {
19473 return die->sibling;
19474 }
19475
19476 /* Get name of a die, return NULL if not found. */
19477
19478 static const char *
19479 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
19480 struct obstack *obstack)
19481 {
19482 if (name && cu->language == language_cplus)
19483 {
19484 char *canon_name = cp_canonicalize_string (name);
19485
19486 if (canon_name != NULL)
19487 {
19488 if (strcmp (canon_name, name) != 0)
19489 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
19490 xfree (canon_name);
19491 }
19492 }
19493
19494 return name;
19495 }
19496
19497 /* Get name of a die, return NULL if not found.
19498 Anonymous namespaces are converted to their magic string. */
19499
19500 static const char *
19501 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
19502 {
19503 struct attribute *attr;
19504
19505 attr = dwarf2_attr (die, DW_AT_name, cu);
19506 if ((!attr || !DW_STRING (attr))
19507 && die->tag != DW_TAG_namespace
19508 && die->tag != DW_TAG_class_type
19509 && die->tag != DW_TAG_interface_type
19510 && die->tag != DW_TAG_structure_type
19511 && die->tag != DW_TAG_union_type)
19512 return NULL;
19513
19514 switch (die->tag)
19515 {
19516 case DW_TAG_compile_unit:
19517 case DW_TAG_partial_unit:
19518 /* Compilation units have a DW_AT_name that is a filename, not
19519 a source language identifier. */
19520 case DW_TAG_enumeration_type:
19521 case DW_TAG_enumerator:
19522 /* These tags always have simple identifiers already; no need
19523 to canonicalize them. */
19524 return DW_STRING (attr);
19525
19526 case DW_TAG_namespace:
19527 if (attr != NULL && DW_STRING (attr) != NULL)
19528 return DW_STRING (attr);
19529 return CP_ANONYMOUS_NAMESPACE_STR;
19530
19531 case DW_TAG_subprogram:
19532 /* Java constructors will all be named "<init>", so return
19533 the class name when we see this special case. */
19534 if (cu->language == language_java
19535 && DW_STRING (attr) != NULL
19536 && strcmp (DW_STRING (attr), "<init>") == 0)
19537 {
19538 struct dwarf2_cu *spec_cu = cu;
19539 struct die_info *spec_die;
19540
19541 /* GCJ will output '<init>' for Java constructor names.
19542 For this special case, return the name of the parent class. */
19543
19544 /* GCJ may output subprogram DIEs with AT_specification set.
19545 If so, use the name of the specified DIE. */
19546 spec_die = die_specification (die, &spec_cu);
19547 if (spec_die != NULL)
19548 return dwarf2_name (spec_die, spec_cu);
19549
19550 do
19551 {
19552 die = die->parent;
19553 if (die->tag == DW_TAG_class_type)
19554 return dwarf2_name (die, cu);
19555 }
19556 while (die->tag != DW_TAG_compile_unit
19557 && die->tag != DW_TAG_partial_unit);
19558 }
19559 break;
19560
19561 case DW_TAG_class_type:
19562 case DW_TAG_interface_type:
19563 case DW_TAG_structure_type:
19564 case DW_TAG_union_type:
19565 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19566 structures or unions. These were of the form "._%d" in GCC 4.1,
19567 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19568 and GCC 4.4. We work around this problem by ignoring these. */
19569 if (attr && DW_STRING (attr)
19570 && (startswith (DW_STRING (attr), "._")
19571 || startswith (DW_STRING (attr), "<anonymous")))
19572 return NULL;
19573
19574 /* GCC might emit a nameless typedef that has a linkage name. See
19575 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19576 if (!attr || DW_STRING (attr) == NULL)
19577 {
19578 char *demangled = NULL;
19579
19580 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19581 if (attr == NULL)
19582 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19583
19584 if (attr == NULL || DW_STRING (attr) == NULL)
19585 return NULL;
19586
19587 /* Avoid demangling DW_STRING (attr) the second time on a second
19588 call for the same DIE. */
19589 if (!DW_STRING_IS_CANONICAL (attr))
19590 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
19591
19592 if (demangled)
19593 {
19594 char *base;
19595
19596 /* FIXME: we already did this for the partial symbol... */
19597 DW_STRING (attr)
19598 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19599 demangled, strlen (demangled));
19600 DW_STRING_IS_CANONICAL (attr) = 1;
19601 xfree (demangled);
19602
19603 /* Strip any leading namespaces/classes, keep only the base name.
19604 DW_AT_name for named DIEs does not contain the prefixes. */
19605 base = strrchr (DW_STRING (attr), ':');
19606 if (base && base > DW_STRING (attr) && base[-1] == ':')
19607 return &base[1];
19608 else
19609 return DW_STRING (attr);
19610 }
19611 }
19612 break;
19613
19614 default:
19615 break;
19616 }
19617
19618 if (!DW_STRING_IS_CANONICAL (attr))
19619 {
19620 DW_STRING (attr)
19621 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
19622 &cu->objfile->per_bfd->storage_obstack);
19623 DW_STRING_IS_CANONICAL (attr) = 1;
19624 }
19625 return DW_STRING (attr);
19626 }
19627
19628 /* Return the die that this die in an extension of, or NULL if there
19629 is none. *EXT_CU is the CU containing DIE on input, and the CU
19630 containing the return value on output. */
19631
19632 static struct die_info *
19633 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
19634 {
19635 struct attribute *attr;
19636
19637 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
19638 if (attr == NULL)
19639 return NULL;
19640
19641 return follow_die_ref (die, attr, ext_cu);
19642 }
19643
19644 /* Convert a DIE tag into its string name. */
19645
19646 static const char *
19647 dwarf_tag_name (unsigned tag)
19648 {
19649 const char *name = get_DW_TAG_name (tag);
19650
19651 if (name == NULL)
19652 return "DW_TAG_<unknown>";
19653
19654 return name;
19655 }
19656
19657 /* Convert a DWARF attribute code into its string name. */
19658
19659 static const char *
19660 dwarf_attr_name (unsigned attr)
19661 {
19662 const char *name;
19663
19664 #ifdef MIPS /* collides with DW_AT_HP_block_index */
19665 if (attr == DW_AT_MIPS_fde)
19666 return "DW_AT_MIPS_fde";
19667 #else
19668 if (attr == DW_AT_HP_block_index)
19669 return "DW_AT_HP_block_index";
19670 #endif
19671
19672 name = get_DW_AT_name (attr);
19673
19674 if (name == NULL)
19675 return "DW_AT_<unknown>";
19676
19677 return name;
19678 }
19679
19680 /* Convert a DWARF value form code into its string name. */
19681
19682 static const char *
19683 dwarf_form_name (unsigned form)
19684 {
19685 const char *name = get_DW_FORM_name (form);
19686
19687 if (name == NULL)
19688 return "DW_FORM_<unknown>";
19689
19690 return name;
19691 }
19692
19693 static char *
19694 dwarf_bool_name (unsigned mybool)
19695 {
19696 if (mybool)
19697 return "TRUE";
19698 else
19699 return "FALSE";
19700 }
19701
19702 /* Convert a DWARF type code into its string name. */
19703
19704 static const char *
19705 dwarf_type_encoding_name (unsigned enc)
19706 {
19707 const char *name = get_DW_ATE_name (enc);
19708
19709 if (name == NULL)
19710 return "DW_ATE_<unknown>";
19711
19712 return name;
19713 }
19714
19715 static void
19716 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
19717 {
19718 unsigned int i;
19719
19720 print_spaces (indent, f);
19721 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
19722 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
19723
19724 if (die->parent != NULL)
19725 {
19726 print_spaces (indent, f);
19727 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
19728 die->parent->offset.sect_off);
19729 }
19730
19731 print_spaces (indent, f);
19732 fprintf_unfiltered (f, " has children: %s\n",
19733 dwarf_bool_name (die->child != NULL));
19734
19735 print_spaces (indent, f);
19736 fprintf_unfiltered (f, " attributes:\n");
19737
19738 for (i = 0; i < die->num_attrs; ++i)
19739 {
19740 print_spaces (indent, f);
19741 fprintf_unfiltered (f, " %s (%s) ",
19742 dwarf_attr_name (die->attrs[i].name),
19743 dwarf_form_name (die->attrs[i].form));
19744
19745 switch (die->attrs[i].form)
19746 {
19747 case DW_FORM_addr:
19748 case DW_FORM_GNU_addr_index:
19749 fprintf_unfiltered (f, "address: ");
19750 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
19751 break;
19752 case DW_FORM_block2:
19753 case DW_FORM_block4:
19754 case DW_FORM_block:
19755 case DW_FORM_block1:
19756 fprintf_unfiltered (f, "block: size %s",
19757 pulongest (DW_BLOCK (&die->attrs[i])->size));
19758 break;
19759 case DW_FORM_exprloc:
19760 fprintf_unfiltered (f, "expression: size %s",
19761 pulongest (DW_BLOCK (&die->attrs[i])->size));
19762 break;
19763 case DW_FORM_ref_addr:
19764 fprintf_unfiltered (f, "ref address: ");
19765 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19766 break;
19767 case DW_FORM_GNU_ref_alt:
19768 fprintf_unfiltered (f, "alt ref address: ");
19769 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19770 break;
19771 case DW_FORM_ref1:
19772 case DW_FORM_ref2:
19773 case DW_FORM_ref4:
19774 case DW_FORM_ref8:
19775 case DW_FORM_ref_udata:
19776 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
19777 (long) (DW_UNSND (&die->attrs[i])));
19778 break;
19779 case DW_FORM_data1:
19780 case DW_FORM_data2:
19781 case DW_FORM_data4:
19782 case DW_FORM_data8:
19783 case DW_FORM_udata:
19784 case DW_FORM_sdata:
19785 fprintf_unfiltered (f, "constant: %s",
19786 pulongest (DW_UNSND (&die->attrs[i])));
19787 break;
19788 case DW_FORM_sec_offset:
19789 fprintf_unfiltered (f, "section offset: %s",
19790 pulongest (DW_UNSND (&die->attrs[i])));
19791 break;
19792 case DW_FORM_ref_sig8:
19793 fprintf_unfiltered (f, "signature: %s",
19794 hex_string (DW_SIGNATURE (&die->attrs[i])));
19795 break;
19796 case DW_FORM_string:
19797 case DW_FORM_strp:
19798 case DW_FORM_GNU_str_index:
19799 case DW_FORM_GNU_strp_alt:
19800 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
19801 DW_STRING (&die->attrs[i])
19802 ? DW_STRING (&die->attrs[i]) : "",
19803 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
19804 break;
19805 case DW_FORM_flag:
19806 if (DW_UNSND (&die->attrs[i]))
19807 fprintf_unfiltered (f, "flag: TRUE");
19808 else
19809 fprintf_unfiltered (f, "flag: FALSE");
19810 break;
19811 case DW_FORM_flag_present:
19812 fprintf_unfiltered (f, "flag: TRUE");
19813 break;
19814 case DW_FORM_indirect:
19815 /* The reader will have reduced the indirect form to
19816 the "base form" so this form should not occur. */
19817 fprintf_unfiltered (f,
19818 "unexpected attribute form: DW_FORM_indirect");
19819 break;
19820 default:
19821 fprintf_unfiltered (f, "unsupported attribute form: %d.",
19822 die->attrs[i].form);
19823 break;
19824 }
19825 fprintf_unfiltered (f, "\n");
19826 }
19827 }
19828
19829 static void
19830 dump_die_for_error (struct die_info *die)
19831 {
19832 dump_die_shallow (gdb_stderr, 0, die);
19833 }
19834
19835 static void
19836 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
19837 {
19838 int indent = level * 4;
19839
19840 gdb_assert (die != NULL);
19841
19842 if (level >= max_level)
19843 return;
19844
19845 dump_die_shallow (f, indent, die);
19846
19847 if (die->child != NULL)
19848 {
19849 print_spaces (indent, f);
19850 fprintf_unfiltered (f, " Children:");
19851 if (level + 1 < max_level)
19852 {
19853 fprintf_unfiltered (f, "\n");
19854 dump_die_1 (f, level + 1, max_level, die->child);
19855 }
19856 else
19857 {
19858 fprintf_unfiltered (f,
19859 " [not printed, max nesting level reached]\n");
19860 }
19861 }
19862
19863 if (die->sibling != NULL && level > 0)
19864 {
19865 dump_die_1 (f, level, max_level, die->sibling);
19866 }
19867 }
19868
19869 /* This is called from the pdie macro in gdbinit.in.
19870 It's not static so gcc will keep a copy callable from gdb. */
19871
19872 void
19873 dump_die (struct die_info *die, int max_level)
19874 {
19875 dump_die_1 (gdb_stdlog, 0, max_level, die);
19876 }
19877
19878 static void
19879 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
19880 {
19881 void **slot;
19882
19883 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19884 INSERT);
19885
19886 *slot = die;
19887 }
19888
19889 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19890 required kind. */
19891
19892 static sect_offset
19893 dwarf2_get_ref_die_offset (const struct attribute *attr)
19894 {
19895 sect_offset retval = { DW_UNSND (attr) };
19896
19897 if (attr_form_is_ref (attr))
19898 return retval;
19899
19900 retval.sect_off = 0;
19901 complaint (&symfile_complaints,
19902 _("unsupported die ref attribute form: '%s'"),
19903 dwarf_form_name (attr->form));
19904 return retval;
19905 }
19906
19907 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
19908 * the value held by the attribute is not constant. */
19909
19910 static LONGEST
19911 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
19912 {
19913 if (attr->form == DW_FORM_sdata)
19914 return DW_SND (attr);
19915 else if (attr->form == DW_FORM_udata
19916 || attr->form == DW_FORM_data1
19917 || attr->form == DW_FORM_data2
19918 || attr->form == DW_FORM_data4
19919 || attr->form == DW_FORM_data8)
19920 return DW_UNSND (attr);
19921 else
19922 {
19923 complaint (&symfile_complaints,
19924 _("Attribute value is not a constant (%s)"),
19925 dwarf_form_name (attr->form));
19926 return default_value;
19927 }
19928 }
19929
19930 /* Follow reference or signature attribute ATTR of SRC_DIE.
19931 On entry *REF_CU is the CU of SRC_DIE.
19932 On exit *REF_CU is the CU of the result. */
19933
19934 static struct die_info *
19935 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
19936 struct dwarf2_cu **ref_cu)
19937 {
19938 struct die_info *die;
19939
19940 if (attr_form_is_ref (attr))
19941 die = follow_die_ref (src_die, attr, ref_cu);
19942 else if (attr->form == DW_FORM_ref_sig8)
19943 die = follow_die_sig (src_die, attr, ref_cu);
19944 else
19945 {
19946 dump_die_for_error (src_die);
19947 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
19948 objfile_name ((*ref_cu)->objfile));
19949 }
19950
19951 return die;
19952 }
19953
19954 /* Follow reference OFFSET.
19955 On entry *REF_CU is the CU of the source die referencing OFFSET.
19956 On exit *REF_CU is the CU of the result.
19957 Returns NULL if OFFSET is invalid. */
19958
19959 static struct die_info *
19960 follow_die_offset (sect_offset offset, int offset_in_dwz,
19961 struct dwarf2_cu **ref_cu)
19962 {
19963 struct die_info temp_die;
19964 struct dwarf2_cu *target_cu, *cu = *ref_cu;
19965
19966 gdb_assert (cu->per_cu != NULL);
19967
19968 target_cu = cu;
19969
19970 if (cu->per_cu->is_debug_types)
19971 {
19972 /* .debug_types CUs cannot reference anything outside their CU.
19973 If they need to, they have to reference a signatured type via
19974 DW_FORM_ref_sig8. */
19975 if (! offset_in_cu_p (&cu->header, offset))
19976 return NULL;
19977 }
19978 else if (offset_in_dwz != cu->per_cu->is_dwz
19979 || ! offset_in_cu_p (&cu->header, offset))
19980 {
19981 struct dwarf2_per_cu_data *per_cu;
19982
19983 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
19984 cu->objfile);
19985
19986 /* If necessary, add it to the queue and load its DIEs. */
19987 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
19988 load_full_comp_unit (per_cu, cu->language);
19989
19990 target_cu = per_cu->cu;
19991 }
19992 else if (cu->dies == NULL)
19993 {
19994 /* We're loading full DIEs during partial symbol reading. */
19995 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
19996 load_full_comp_unit (cu->per_cu, language_minimal);
19997 }
19998
19999 *ref_cu = target_cu;
20000 temp_die.offset = offset;
20001 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
20002 }
20003
20004 /* Follow reference attribute ATTR of SRC_DIE.
20005 On entry *REF_CU is the CU of SRC_DIE.
20006 On exit *REF_CU is the CU of the result. */
20007
20008 static struct die_info *
20009 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20010 struct dwarf2_cu **ref_cu)
20011 {
20012 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20013 struct dwarf2_cu *cu = *ref_cu;
20014 struct die_info *die;
20015
20016 die = follow_die_offset (offset,
20017 (attr->form == DW_FORM_GNU_ref_alt
20018 || cu->per_cu->is_dwz),
20019 ref_cu);
20020 if (!die)
20021 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20022 "at 0x%x [in module %s]"),
20023 offset.sect_off, src_die->offset.sect_off,
20024 objfile_name (cu->objfile));
20025
20026 return die;
20027 }
20028
20029 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20030 Returned value is intended for DW_OP_call*. Returned
20031 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20032
20033 struct dwarf2_locexpr_baton
20034 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20035 struct dwarf2_per_cu_data *per_cu,
20036 CORE_ADDR (*get_frame_pc) (void *baton),
20037 void *baton)
20038 {
20039 struct dwarf2_cu *cu;
20040 struct die_info *die;
20041 struct attribute *attr;
20042 struct dwarf2_locexpr_baton retval;
20043
20044 dw2_setup (per_cu->objfile);
20045
20046 if (per_cu->cu == NULL)
20047 load_cu (per_cu);
20048 cu = per_cu->cu;
20049 if (cu == NULL)
20050 {
20051 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20052 Instead just throw an error, not much else we can do. */
20053 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20054 offset.sect_off, objfile_name (per_cu->objfile));
20055 }
20056
20057 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20058 if (!die)
20059 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20060 offset.sect_off, objfile_name (per_cu->objfile));
20061
20062 attr = dwarf2_attr (die, DW_AT_location, cu);
20063 if (!attr)
20064 {
20065 /* DWARF: "If there is no such attribute, then there is no effect.".
20066 DATA is ignored if SIZE is 0. */
20067
20068 retval.data = NULL;
20069 retval.size = 0;
20070 }
20071 else if (attr_form_is_section_offset (attr))
20072 {
20073 struct dwarf2_loclist_baton loclist_baton;
20074 CORE_ADDR pc = (*get_frame_pc) (baton);
20075 size_t size;
20076
20077 fill_in_loclist_baton (cu, &loclist_baton, attr);
20078
20079 retval.data = dwarf2_find_location_expression (&loclist_baton,
20080 &size, pc);
20081 retval.size = size;
20082 }
20083 else
20084 {
20085 if (!attr_form_is_block (attr))
20086 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20087 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20088 offset.sect_off, objfile_name (per_cu->objfile));
20089
20090 retval.data = DW_BLOCK (attr)->data;
20091 retval.size = DW_BLOCK (attr)->size;
20092 }
20093 retval.per_cu = cu->per_cu;
20094
20095 age_cached_comp_units ();
20096
20097 return retval;
20098 }
20099
20100 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20101 offset. */
20102
20103 struct dwarf2_locexpr_baton
20104 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20105 struct dwarf2_per_cu_data *per_cu,
20106 CORE_ADDR (*get_frame_pc) (void *baton),
20107 void *baton)
20108 {
20109 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20110
20111 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20112 }
20113
20114 /* Write a constant of a given type as target-ordered bytes into
20115 OBSTACK. */
20116
20117 static const gdb_byte *
20118 write_constant_as_bytes (struct obstack *obstack,
20119 enum bfd_endian byte_order,
20120 struct type *type,
20121 ULONGEST value,
20122 LONGEST *len)
20123 {
20124 gdb_byte *result;
20125
20126 *len = TYPE_LENGTH (type);
20127 result = obstack_alloc (obstack, *len);
20128 store_unsigned_integer (result, *len, byte_order, value);
20129
20130 return result;
20131 }
20132
20133 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20134 pointer to the constant bytes and set LEN to the length of the
20135 data. If memory is needed, allocate it on OBSTACK. If the DIE
20136 does not have a DW_AT_const_value, return NULL. */
20137
20138 const gdb_byte *
20139 dwarf2_fetch_constant_bytes (sect_offset offset,
20140 struct dwarf2_per_cu_data *per_cu,
20141 struct obstack *obstack,
20142 LONGEST *len)
20143 {
20144 struct dwarf2_cu *cu;
20145 struct die_info *die;
20146 struct attribute *attr;
20147 const gdb_byte *result = NULL;
20148 struct type *type;
20149 LONGEST value;
20150 enum bfd_endian byte_order;
20151
20152 dw2_setup (per_cu->objfile);
20153
20154 if (per_cu->cu == NULL)
20155 load_cu (per_cu);
20156 cu = per_cu->cu;
20157 if (cu == NULL)
20158 {
20159 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20160 Instead just throw an error, not much else we can do. */
20161 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20162 offset.sect_off, objfile_name (per_cu->objfile));
20163 }
20164
20165 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20166 if (!die)
20167 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20168 offset.sect_off, objfile_name (per_cu->objfile));
20169
20170
20171 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20172 if (attr == NULL)
20173 return NULL;
20174
20175 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20176 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20177
20178 switch (attr->form)
20179 {
20180 case DW_FORM_addr:
20181 case DW_FORM_GNU_addr_index:
20182 {
20183 gdb_byte *tem;
20184
20185 *len = cu->header.addr_size;
20186 tem = obstack_alloc (obstack, *len);
20187 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20188 result = tem;
20189 }
20190 break;
20191 case DW_FORM_string:
20192 case DW_FORM_strp:
20193 case DW_FORM_GNU_str_index:
20194 case DW_FORM_GNU_strp_alt:
20195 /* DW_STRING is already allocated on the objfile obstack, point
20196 directly to it. */
20197 result = (const gdb_byte *) DW_STRING (attr);
20198 *len = strlen (DW_STRING (attr));
20199 break;
20200 case DW_FORM_block1:
20201 case DW_FORM_block2:
20202 case DW_FORM_block4:
20203 case DW_FORM_block:
20204 case DW_FORM_exprloc:
20205 result = DW_BLOCK (attr)->data;
20206 *len = DW_BLOCK (attr)->size;
20207 break;
20208
20209 /* The DW_AT_const_value attributes are supposed to carry the
20210 symbol's value "represented as it would be on the target
20211 architecture." By the time we get here, it's already been
20212 converted to host endianness, so we just need to sign- or
20213 zero-extend it as appropriate. */
20214 case DW_FORM_data1:
20215 type = die_type (die, cu);
20216 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20217 if (result == NULL)
20218 result = write_constant_as_bytes (obstack, byte_order,
20219 type, value, len);
20220 break;
20221 case DW_FORM_data2:
20222 type = die_type (die, cu);
20223 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20224 if (result == NULL)
20225 result = write_constant_as_bytes (obstack, byte_order,
20226 type, value, len);
20227 break;
20228 case DW_FORM_data4:
20229 type = die_type (die, cu);
20230 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20231 if (result == NULL)
20232 result = write_constant_as_bytes (obstack, byte_order,
20233 type, value, len);
20234 break;
20235 case DW_FORM_data8:
20236 type = die_type (die, cu);
20237 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20238 if (result == NULL)
20239 result = write_constant_as_bytes (obstack, byte_order,
20240 type, value, len);
20241 break;
20242
20243 case DW_FORM_sdata:
20244 type = die_type (die, cu);
20245 result = write_constant_as_bytes (obstack, byte_order,
20246 type, DW_SND (attr), len);
20247 break;
20248
20249 case DW_FORM_udata:
20250 type = die_type (die, cu);
20251 result = write_constant_as_bytes (obstack, byte_order,
20252 type, DW_UNSND (attr), len);
20253 break;
20254
20255 default:
20256 complaint (&symfile_complaints,
20257 _("unsupported const value attribute form: '%s'"),
20258 dwarf_form_name (attr->form));
20259 break;
20260 }
20261
20262 return result;
20263 }
20264
20265 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20266 PER_CU. */
20267
20268 struct type *
20269 dwarf2_get_die_type (cu_offset die_offset,
20270 struct dwarf2_per_cu_data *per_cu)
20271 {
20272 sect_offset die_offset_sect;
20273
20274 dw2_setup (per_cu->objfile);
20275
20276 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20277 return get_die_type_at_offset (die_offset_sect, per_cu);
20278 }
20279
20280 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20281 On entry *REF_CU is the CU of SRC_DIE.
20282 On exit *REF_CU is the CU of the result.
20283 Returns NULL if the referenced DIE isn't found. */
20284
20285 static struct die_info *
20286 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20287 struct dwarf2_cu **ref_cu)
20288 {
20289 struct objfile *objfile = (*ref_cu)->objfile;
20290 struct die_info temp_die;
20291 struct dwarf2_cu *sig_cu;
20292 struct die_info *die;
20293
20294 /* While it might be nice to assert sig_type->type == NULL here,
20295 we can get here for DW_AT_imported_declaration where we need
20296 the DIE not the type. */
20297
20298 /* If necessary, add it to the queue and load its DIEs. */
20299
20300 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20301 read_signatured_type (sig_type);
20302
20303 sig_cu = sig_type->per_cu.cu;
20304 gdb_assert (sig_cu != NULL);
20305 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20306 temp_die.offset = sig_type->type_offset_in_section;
20307 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
20308 temp_die.offset.sect_off);
20309 if (die)
20310 {
20311 /* For .gdb_index version 7 keep track of included TUs.
20312 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20313 if (dwarf2_per_objfile->index_table != NULL
20314 && dwarf2_per_objfile->index_table->version <= 7)
20315 {
20316 VEC_safe_push (dwarf2_per_cu_ptr,
20317 (*ref_cu)->per_cu->imported_symtabs,
20318 sig_cu->per_cu);
20319 }
20320
20321 *ref_cu = sig_cu;
20322 return die;
20323 }
20324
20325 return NULL;
20326 }
20327
20328 /* Follow signatured type referenced by ATTR in SRC_DIE.
20329 On entry *REF_CU is the CU of SRC_DIE.
20330 On exit *REF_CU is the CU of the result.
20331 The result is the DIE of the type.
20332 If the referenced type cannot be found an error is thrown. */
20333
20334 static struct die_info *
20335 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20336 struct dwarf2_cu **ref_cu)
20337 {
20338 ULONGEST signature = DW_SIGNATURE (attr);
20339 struct signatured_type *sig_type;
20340 struct die_info *die;
20341
20342 gdb_assert (attr->form == DW_FORM_ref_sig8);
20343
20344 sig_type = lookup_signatured_type (*ref_cu, signature);
20345 /* sig_type will be NULL if the signatured type is missing from
20346 the debug info. */
20347 if (sig_type == NULL)
20348 {
20349 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20350 " from DIE at 0x%x [in module %s]"),
20351 hex_string (signature), src_die->offset.sect_off,
20352 objfile_name ((*ref_cu)->objfile));
20353 }
20354
20355 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20356 if (die == NULL)
20357 {
20358 dump_die_for_error (src_die);
20359 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20360 " from DIE at 0x%x [in module %s]"),
20361 hex_string (signature), src_die->offset.sect_off,
20362 objfile_name ((*ref_cu)->objfile));
20363 }
20364
20365 return die;
20366 }
20367
20368 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20369 reading in and processing the type unit if necessary. */
20370
20371 static struct type *
20372 get_signatured_type (struct die_info *die, ULONGEST signature,
20373 struct dwarf2_cu *cu)
20374 {
20375 struct signatured_type *sig_type;
20376 struct dwarf2_cu *type_cu;
20377 struct die_info *type_die;
20378 struct type *type;
20379
20380 sig_type = lookup_signatured_type (cu, signature);
20381 /* sig_type will be NULL if the signatured type is missing from
20382 the debug info. */
20383 if (sig_type == NULL)
20384 {
20385 complaint (&symfile_complaints,
20386 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20387 " from DIE at 0x%x [in module %s]"),
20388 hex_string (signature), die->offset.sect_off,
20389 objfile_name (dwarf2_per_objfile->objfile));
20390 return build_error_marker_type (cu, die);
20391 }
20392
20393 /* If we already know the type we're done. */
20394 if (sig_type->type != NULL)
20395 return sig_type->type;
20396
20397 type_cu = cu;
20398 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20399 if (type_die != NULL)
20400 {
20401 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20402 is created. This is important, for example, because for c++ classes
20403 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20404 type = read_type_die (type_die, type_cu);
20405 if (type == NULL)
20406 {
20407 complaint (&symfile_complaints,
20408 _("Dwarf Error: Cannot build signatured type %s"
20409 " referenced from DIE at 0x%x [in module %s]"),
20410 hex_string (signature), die->offset.sect_off,
20411 objfile_name (dwarf2_per_objfile->objfile));
20412 type = build_error_marker_type (cu, die);
20413 }
20414 }
20415 else
20416 {
20417 complaint (&symfile_complaints,
20418 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20419 " from DIE at 0x%x [in module %s]"),
20420 hex_string (signature), die->offset.sect_off,
20421 objfile_name (dwarf2_per_objfile->objfile));
20422 type = build_error_marker_type (cu, die);
20423 }
20424 sig_type->type = type;
20425
20426 return type;
20427 }
20428
20429 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20430 reading in and processing the type unit if necessary. */
20431
20432 static struct type *
20433 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20434 struct dwarf2_cu *cu) /* ARI: editCase function */
20435 {
20436 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20437 if (attr_form_is_ref (attr))
20438 {
20439 struct dwarf2_cu *type_cu = cu;
20440 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20441
20442 return read_type_die (type_die, type_cu);
20443 }
20444 else if (attr->form == DW_FORM_ref_sig8)
20445 {
20446 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20447 }
20448 else
20449 {
20450 complaint (&symfile_complaints,
20451 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20452 " at 0x%x [in module %s]"),
20453 dwarf_form_name (attr->form), die->offset.sect_off,
20454 objfile_name (dwarf2_per_objfile->objfile));
20455 return build_error_marker_type (cu, die);
20456 }
20457 }
20458
20459 /* Load the DIEs associated with type unit PER_CU into memory. */
20460
20461 static void
20462 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
20463 {
20464 struct signatured_type *sig_type;
20465
20466 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20467 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20468
20469 /* We have the per_cu, but we need the signatured_type.
20470 Fortunately this is an easy translation. */
20471 gdb_assert (per_cu->is_debug_types);
20472 sig_type = (struct signatured_type *) per_cu;
20473
20474 gdb_assert (per_cu->cu == NULL);
20475
20476 read_signatured_type (sig_type);
20477
20478 gdb_assert (per_cu->cu != NULL);
20479 }
20480
20481 /* die_reader_func for read_signatured_type.
20482 This is identical to load_full_comp_unit_reader,
20483 but is kept separate for now. */
20484
20485 static void
20486 read_signatured_type_reader (const struct die_reader_specs *reader,
20487 const gdb_byte *info_ptr,
20488 struct die_info *comp_unit_die,
20489 int has_children,
20490 void *data)
20491 {
20492 struct dwarf2_cu *cu = reader->cu;
20493
20494 gdb_assert (cu->die_hash == NULL);
20495 cu->die_hash =
20496 htab_create_alloc_ex (cu->header.length / 12,
20497 die_hash,
20498 die_eq,
20499 NULL,
20500 &cu->comp_unit_obstack,
20501 hashtab_obstack_allocate,
20502 dummy_obstack_deallocate);
20503
20504 if (has_children)
20505 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20506 &info_ptr, comp_unit_die);
20507 cu->dies = comp_unit_die;
20508 /* comp_unit_die is not stored in die_hash, no need. */
20509
20510 /* We try not to read any attributes in this function, because not
20511 all CUs needed for references have been loaded yet, and symbol
20512 table processing isn't initialized. But we have to set the CU language,
20513 or we won't be able to build types correctly.
20514 Similarly, if we do not read the producer, we can not apply
20515 producer-specific interpretation. */
20516 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20517 }
20518
20519 /* Read in a signatured type and build its CU and DIEs.
20520 If the type is a stub for the real type in a DWO file,
20521 read in the real type from the DWO file as well. */
20522
20523 static void
20524 read_signatured_type (struct signatured_type *sig_type)
20525 {
20526 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
20527
20528 gdb_assert (per_cu->is_debug_types);
20529 gdb_assert (per_cu->cu == NULL);
20530
20531 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20532 read_signatured_type_reader, NULL);
20533 sig_type->per_cu.tu_read = 1;
20534 }
20535
20536 /* Decode simple location descriptions.
20537 Given a pointer to a dwarf block that defines a location, compute
20538 the location and return the value.
20539
20540 NOTE drow/2003-11-18: This function is called in two situations
20541 now: for the address of static or global variables (partial symbols
20542 only) and for offsets into structures which are expected to be
20543 (more or less) constant. The partial symbol case should go away,
20544 and only the constant case should remain. That will let this
20545 function complain more accurately. A few special modes are allowed
20546 without complaint for global variables (for instance, global
20547 register values and thread-local values).
20548
20549 A location description containing no operations indicates that the
20550 object is optimized out. The return value is 0 for that case.
20551 FIXME drow/2003-11-16: No callers check for this case any more; soon all
20552 callers will only want a very basic result and this can become a
20553 complaint.
20554
20555 Note that stack[0] is unused except as a default error return. */
20556
20557 static CORE_ADDR
20558 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
20559 {
20560 struct objfile *objfile = cu->objfile;
20561 size_t i;
20562 size_t size = blk->size;
20563 const gdb_byte *data = blk->data;
20564 CORE_ADDR stack[64];
20565 int stacki;
20566 unsigned int bytes_read, unsnd;
20567 gdb_byte op;
20568
20569 i = 0;
20570 stacki = 0;
20571 stack[stacki] = 0;
20572 stack[++stacki] = 0;
20573
20574 while (i < size)
20575 {
20576 op = data[i++];
20577 switch (op)
20578 {
20579 case DW_OP_lit0:
20580 case DW_OP_lit1:
20581 case DW_OP_lit2:
20582 case DW_OP_lit3:
20583 case DW_OP_lit4:
20584 case DW_OP_lit5:
20585 case DW_OP_lit6:
20586 case DW_OP_lit7:
20587 case DW_OP_lit8:
20588 case DW_OP_lit9:
20589 case DW_OP_lit10:
20590 case DW_OP_lit11:
20591 case DW_OP_lit12:
20592 case DW_OP_lit13:
20593 case DW_OP_lit14:
20594 case DW_OP_lit15:
20595 case DW_OP_lit16:
20596 case DW_OP_lit17:
20597 case DW_OP_lit18:
20598 case DW_OP_lit19:
20599 case DW_OP_lit20:
20600 case DW_OP_lit21:
20601 case DW_OP_lit22:
20602 case DW_OP_lit23:
20603 case DW_OP_lit24:
20604 case DW_OP_lit25:
20605 case DW_OP_lit26:
20606 case DW_OP_lit27:
20607 case DW_OP_lit28:
20608 case DW_OP_lit29:
20609 case DW_OP_lit30:
20610 case DW_OP_lit31:
20611 stack[++stacki] = op - DW_OP_lit0;
20612 break;
20613
20614 case DW_OP_reg0:
20615 case DW_OP_reg1:
20616 case DW_OP_reg2:
20617 case DW_OP_reg3:
20618 case DW_OP_reg4:
20619 case DW_OP_reg5:
20620 case DW_OP_reg6:
20621 case DW_OP_reg7:
20622 case DW_OP_reg8:
20623 case DW_OP_reg9:
20624 case DW_OP_reg10:
20625 case DW_OP_reg11:
20626 case DW_OP_reg12:
20627 case DW_OP_reg13:
20628 case DW_OP_reg14:
20629 case DW_OP_reg15:
20630 case DW_OP_reg16:
20631 case DW_OP_reg17:
20632 case DW_OP_reg18:
20633 case DW_OP_reg19:
20634 case DW_OP_reg20:
20635 case DW_OP_reg21:
20636 case DW_OP_reg22:
20637 case DW_OP_reg23:
20638 case DW_OP_reg24:
20639 case DW_OP_reg25:
20640 case DW_OP_reg26:
20641 case DW_OP_reg27:
20642 case DW_OP_reg28:
20643 case DW_OP_reg29:
20644 case DW_OP_reg30:
20645 case DW_OP_reg31:
20646 stack[++stacki] = op - DW_OP_reg0;
20647 if (i < size)
20648 dwarf2_complex_location_expr_complaint ();
20649 break;
20650
20651 case DW_OP_regx:
20652 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20653 i += bytes_read;
20654 stack[++stacki] = unsnd;
20655 if (i < size)
20656 dwarf2_complex_location_expr_complaint ();
20657 break;
20658
20659 case DW_OP_addr:
20660 stack[++stacki] = read_address (objfile->obfd, &data[i],
20661 cu, &bytes_read);
20662 i += bytes_read;
20663 break;
20664
20665 case DW_OP_const1u:
20666 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
20667 i += 1;
20668 break;
20669
20670 case DW_OP_const1s:
20671 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
20672 i += 1;
20673 break;
20674
20675 case DW_OP_const2u:
20676 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
20677 i += 2;
20678 break;
20679
20680 case DW_OP_const2s:
20681 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
20682 i += 2;
20683 break;
20684
20685 case DW_OP_const4u:
20686 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
20687 i += 4;
20688 break;
20689
20690 case DW_OP_const4s:
20691 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
20692 i += 4;
20693 break;
20694
20695 case DW_OP_const8u:
20696 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20697 i += 8;
20698 break;
20699
20700 case DW_OP_constu:
20701 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20702 &bytes_read);
20703 i += bytes_read;
20704 break;
20705
20706 case DW_OP_consts:
20707 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20708 i += bytes_read;
20709 break;
20710
20711 case DW_OP_dup:
20712 stack[stacki + 1] = stack[stacki];
20713 stacki++;
20714 break;
20715
20716 case DW_OP_plus:
20717 stack[stacki - 1] += stack[stacki];
20718 stacki--;
20719 break;
20720
20721 case DW_OP_plus_uconst:
20722 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20723 &bytes_read);
20724 i += bytes_read;
20725 break;
20726
20727 case DW_OP_minus:
20728 stack[stacki - 1] -= stack[stacki];
20729 stacki--;
20730 break;
20731
20732 case DW_OP_deref:
20733 /* If we're not the last op, then we definitely can't encode
20734 this using GDB's address_class enum. This is valid for partial
20735 global symbols, although the variable's address will be bogus
20736 in the psymtab. */
20737 if (i < size)
20738 dwarf2_complex_location_expr_complaint ();
20739 break;
20740
20741 case DW_OP_GNU_push_tls_address:
20742 /* The top of the stack has the offset from the beginning
20743 of the thread control block at which the variable is located. */
20744 /* Nothing should follow this operator, so the top of stack would
20745 be returned. */
20746 /* This is valid for partial global symbols, but the variable's
20747 address will be bogus in the psymtab. Make it always at least
20748 non-zero to not look as a variable garbage collected by linker
20749 which have DW_OP_addr 0. */
20750 if (i < size)
20751 dwarf2_complex_location_expr_complaint ();
20752 stack[stacki]++;
20753 break;
20754
20755 case DW_OP_GNU_uninit:
20756 break;
20757
20758 case DW_OP_GNU_addr_index:
20759 case DW_OP_GNU_const_index:
20760 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20761 &bytes_read);
20762 i += bytes_read;
20763 break;
20764
20765 default:
20766 {
20767 const char *name = get_DW_OP_name (op);
20768
20769 if (name)
20770 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
20771 name);
20772 else
20773 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
20774 op);
20775 }
20776
20777 return (stack[stacki]);
20778 }
20779
20780 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20781 outside of the allocated space. Also enforce minimum>0. */
20782 if (stacki >= ARRAY_SIZE (stack) - 1)
20783 {
20784 complaint (&symfile_complaints,
20785 _("location description stack overflow"));
20786 return 0;
20787 }
20788
20789 if (stacki <= 0)
20790 {
20791 complaint (&symfile_complaints,
20792 _("location description stack underflow"));
20793 return 0;
20794 }
20795 }
20796 return (stack[stacki]);
20797 }
20798
20799 /* memory allocation interface */
20800
20801 static struct dwarf_block *
20802 dwarf_alloc_block (struct dwarf2_cu *cu)
20803 {
20804 struct dwarf_block *blk;
20805
20806 blk = (struct dwarf_block *)
20807 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
20808 return (blk);
20809 }
20810
20811 static struct die_info *
20812 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
20813 {
20814 struct die_info *die;
20815 size_t size = sizeof (struct die_info);
20816
20817 if (num_attrs > 1)
20818 size += (num_attrs - 1) * sizeof (struct attribute);
20819
20820 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
20821 memset (die, 0, sizeof (struct die_info));
20822 return (die);
20823 }
20824
20825 \f
20826 /* Macro support. */
20827
20828 /* Return file name relative to the compilation directory of file number I in
20829 *LH's file name table. The result is allocated using xmalloc; the caller is
20830 responsible for freeing it. */
20831
20832 static char *
20833 file_file_name (int file, struct line_header *lh)
20834 {
20835 /* Is the file number a valid index into the line header's file name
20836 table? Remember that file numbers start with one, not zero. */
20837 if (1 <= file && file <= lh->num_file_names)
20838 {
20839 struct file_entry *fe = &lh->file_names[file - 1];
20840
20841 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20842 || lh->include_dirs == NULL)
20843 return xstrdup (fe->name);
20844 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
20845 fe->name, NULL);
20846 }
20847 else
20848 {
20849 /* The compiler produced a bogus file number. We can at least
20850 record the macro definitions made in the file, even if we
20851 won't be able to find the file by name. */
20852 char fake_name[80];
20853
20854 xsnprintf (fake_name, sizeof (fake_name),
20855 "<bad macro file number %d>", file);
20856
20857 complaint (&symfile_complaints,
20858 _("bad file number in macro information (%d)"),
20859 file);
20860
20861 return xstrdup (fake_name);
20862 }
20863 }
20864
20865 /* Return the full name of file number I in *LH's file name table.
20866 Use COMP_DIR as the name of the current directory of the
20867 compilation. The result is allocated using xmalloc; the caller is
20868 responsible for freeing it. */
20869 static char *
20870 file_full_name (int file, struct line_header *lh, const char *comp_dir)
20871 {
20872 /* Is the file number a valid index into the line header's file name
20873 table? Remember that file numbers start with one, not zero. */
20874 if (1 <= file && file <= lh->num_file_names)
20875 {
20876 char *relative = file_file_name (file, lh);
20877
20878 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
20879 return relative;
20880 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
20881 }
20882 else
20883 return file_file_name (file, lh);
20884 }
20885
20886
20887 static struct macro_source_file *
20888 macro_start_file (int file, int line,
20889 struct macro_source_file *current_file,
20890 struct line_header *lh)
20891 {
20892 /* File name relative to the compilation directory of this source file. */
20893 char *file_name = file_file_name (file, lh);
20894
20895 if (! current_file)
20896 {
20897 /* Note: We don't create a macro table for this compilation unit
20898 at all until we actually get a filename. */
20899 struct macro_table *macro_table = get_macro_table ();
20900
20901 /* If we have no current file, then this must be the start_file
20902 directive for the compilation unit's main source file. */
20903 current_file = macro_set_main (macro_table, file_name);
20904 macro_define_special (macro_table);
20905 }
20906 else
20907 current_file = macro_include (current_file, line, file_name);
20908
20909 xfree (file_name);
20910
20911 return current_file;
20912 }
20913
20914
20915 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
20916 followed by a null byte. */
20917 static char *
20918 copy_string (const char *buf, int len)
20919 {
20920 char *s = xmalloc (len + 1);
20921
20922 memcpy (s, buf, len);
20923 s[len] = '\0';
20924 return s;
20925 }
20926
20927
20928 static const char *
20929 consume_improper_spaces (const char *p, const char *body)
20930 {
20931 if (*p == ' ')
20932 {
20933 complaint (&symfile_complaints,
20934 _("macro definition contains spaces "
20935 "in formal argument list:\n`%s'"),
20936 body);
20937
20938 while (*p == ' ')
20939 p++;
20940 }
20941
20942 return p;
20943 }
20944
20945
20946 static void
20947 parse_macro_definition (struct macro_source_file *file, int line,
20948 const char *body)
20949 {
20950 const char *p;
20951
20952 /* The body string takes one of two forms. For object-like macro
20953 definitions, it should be:
20954
20955 <macro name> " " <definition>
20956
20957 For function-like macro definitions, it should be:
20958
20959 <macro name> "() " <definition>
20960 or
20961 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
20962
20963 Spaces may appear only where explicitly indicated, and in the
20964 <definition>.
20965
20966 The Dwarf 2 spec says that an object-like macro's name is always
20967 followed by a space, but versions of GCC around March 2002 omit
20968 the space when the macro's definition is the empty string.
20969
20970 The Dwarf 2 spec says that there should be no spaces between the
20971 formal arguments in a function-like macro's formal argument list,
20972 but versions of GCC around March 2002 include spaces after the
20973 commas. */
20974
20975
20976 /* Find the extent of the macro name. The macro name is terminated
20977 by either a space or null character (for an object-like macro) or
20978 an opening paren (for a function-like macro). */
20979 for (p = body; *p; p++)
20980 if (*p == ' ' || *p == '(')
20981 break;
20982
20983 if (*p == ' ' || *p == '\0')
20984 {
20985 /* It's an object-like macro. */
20986 int name_len = p - body;
20987 char *name = copy_string (body, name_len);
20988 const char *replacement;
20989
20990 if (*p == ' ')
20991 replacement = body + name_len + 1;
20992 else
20993 {
20994 dwarf2_macro_malformed_definition_complaint (body);
20995 replacement = body + name_len;
20996 }
20997
20998 macro_define_object (file, line, name, replacement);
20999
21000 xfree (name);
21001 }
21002 else if (*p == '(')
21003 {
21004 /* It's a function-like macro. */
21005 char *name = copy_string (body, p - body);
21006 int argc = 0;
21007 int argv_size = 1;
21008 char **argv = xmalloc (argv_size * sizeof (*argv));
21009
21010 p++;
21011
21012 p = consume_improper_spaces (p, body);
21013
21014 /* Parse the formal argument list. */
21015 while (*p && *p != ')')
21016 {
21017 /* Find the extent of the current argument name. */
21018 const char *arg_start = p;
21019
21020 while (*p && *p != ',' && *p != ')' && *p != ' ')
21021 p++;
21022
21023 if (! *p || p == arg_start)
21024 dwarf2_macro_malformed_definition_complaint (body);
21025 else
21026 {
21027 /* Make sure argv has room for the new argument. */
21028 if (argc >= argv_size)
21029 {
21030 argv_size *= 2;
21031 argv = xrealloc (argv, argv_size * sizeof (*argv));
21032 }
21033
21034 argv[argc++] = copy_string (arg_start, p - arg_start);
21035 }
21036
21037 p = consume_improper_spaces (p, body);
21038
21039 /* Consume the comma, if present. */
21040 if (*p == ',')
21041 {
21042 p++;
21043
21044 p = consume_improper_spaces (p, body);
21045 }
21046 }
21047
21048 if (*p == ')')
21049 {
21050 p++;
21051
21052 if (*p == ' ')
21053 /* Perfectly formed definition, no complaints. */
21054 macro_define_function (file, line, name,
21055 argc, (const char **) argv,
21056 p + 1);
21057 else if (*p == '\0')
21058 {
21059 /* Complain, but do define it. */
21060 dwarf2_macro_malformed_definition_complaint (body);
21061 macro_define_function (file, line, name,
21062 argc, (const char **) argv,
21063 p);
21064 }
21065 else
21066 /* Just complain. */
21067 dwarf2_macro_malformed_definition_complaint (body);
21068 }
21069 else
21070 /* Just complain. */
21071 dwarf2_macro_malformed_definition_complaint (body);
21072
21073 xfree (name);
21074 {
21075 int i;
21076
21077 for (i = 0; i < argc; i++)
21078 xfree (argv[i]);
21079 }
21080 xfree (argv);
21081 }
21082 else
21083 dwarf2_macro_malformed_definition_complaint (body);
21084 }
21085
21086 /* Skip some bytes from BYTES according to the form given in FORM.
21087 Returns the new pointer. */
21088
21089 static const gdb_byte *
21090 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21091 enum dwarf_form form,
21092 unsigned int offset_size,
21093 struct dwarf2_section_info *section)
21094 {
21095 unsigned int bytes_read;
21096
21097 switch (form)
21098 {
21099 case DW_FORM_data1:
21100 case DW_FORM_flag:
21101 ++bytes;
21102 break;
21103
21104 case DW_FORM_data2:
21105 bytes += 2;
21106 break;
21107
21108 case DW_FORM_data4:
21109 bytes += 4;
21110 break;
21111
21112 case DW_FORM_data8:
21113 bytes += 8;
21114 break;
21115
21116 case DW_FORM_string:
21117 read_direct_string (abfd, bytes, &bytes_read);
21118 bytes += bytes_read;
21119 break;
21120
21121 case DW_FORM_sec_offset:
21122 case DW_FORM_strp:
21123 case DW_FORM_GNU_strp_alt:
21124 bytes += offset_size;
21125 break;
21126
21127 case DW_FORM_block:
21128 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21129 bytes += bytes_read;
21130 break;
21131
21132 case DW_FORM_block1:
21133 bytes += 1 + read_1_byte (abfd, bytes);
21134 break;
21135 case DW_FORM_block2:
21136 bytes += 2 + read_2_bytes (abfd, bytes);
21137 break;
21138 case DW_FORM_block4:
21139 bytes += 4 + read_4_bytes (abfd, bytes);
21140 break;
21141
21142 case DW_FORM_sdata:
21143 case DW_FORM_udata:
21144 case DW_FORM_GNU_addr_index:
21145 case DW_FORM_GNU_str_index:
21146 bytes = gdb_skip_leb128 (bytes, buffer_end);
21147 if (bytes == NULL)
21148 {
21149 dwarf2_section_buffer_overflow_complaint (section);
21150 return NULL;
21151 }
21152 break;
21153
21154 default:
21155 {
21156 complain:
21157 complaint (&symfile_complaints,
21158 _("invalid form 0x%x in `%s'"),
21159 form, get_section_name (section));
21160 return NULL;
21161 }
21162 }
21163
21164 return bytes;
21165 }
21166
21167 /* A helper for dwarf_decode_macros that handles skipping an unknown
21168 opcode. Returns an updated pointer to the macro data buffer; or,
21169 on error, issues a complaint and returns NULL. */
21170
21171 static const gdb_byte *
21172 skip_unknown_opcode (unsigned int opcode,
21173 const gdb_byte **opcode_definitions,
21174 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21175 bfd *abfd,
21176 unsigned int offset_size,
21177 struct dwarf2_section_info *section)
21178 {
21179 unsigned int bytes_read, i;
21180 unsigned long arg;
21181 const gdb_byte *defn;
21182
21183 if (opcode_definitions[opcode] == NULL)
21184 {
21185 complaint (&symfile_complaints,
21186 _("unrecognized DW_MACFINO opcode 0x%x"),
21187 opcode);
21188 return NULL;
21189 }
21190
21191 defn = opcode_definitions[opcode];
21192 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21193 defn += bytes_read;
21194
21195 for (i = 0; i < arg; ++i)
21196 {
21197 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21198 (enum dwarf_form) defn[i], offset_size,
21199 section);
21200 if (mac_ptr == NULL)
21201 {
21202 /* skip_form_bytes already issued the complaint. */
21203 return NULL;
21204 }
21205 }
21206
21207 return mac_ptr;
21208 }
21209
21210 /* A helper function which parses the header of a macro section.
21211 If the macro section is the extended (for now called "GNU") type,
21212 then this updates *OFFSET_SIZE. Returns a pointer to just after
21213 the header, or issues a complaint and returns NULL on error. */
21214
21215 static const gdb_byte *
21216 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21217 bfd *abfd,
21218 const gdb_byte *mac_ptr,
21219 unsigned int *offset_size,
21220 int section_is_gnu)
21221 {
21222 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21223
21224 if (section_is_gnu)
21225 {
21226 unsigned int version, flags;
21227
21228 version = read_2_bytes (abfd, mac_ptr);
21229 if (version != 4)
21230 {
21231 complaint (&symfile_complaints,
21232 _("unrecognized version `%d' in .debug_macro section"),
21233 version);
21234 return NULL;
21235 }
21236 mac_ptr += 2;
21237
21238 flags = read_1_byte (abfd, mac_ptr);
21239 ++mac_ptr;
21240 *offset_size = (flags & 1) ? 8 : 4;
21241
21242 if ((flags & 2) != 0)
21243 /* We don't need the line table offset. */
21244 mac_ptr += *offset_size;
21245
21246 /* Vendor opcode descriptions. */
21247 if ((flags & 4) != 0)
21248 {
21249 unsigned int i, count;
21250
21251 count = read_1_byte (abfd, mac_ptr);
21252 ++mac_ptr;
21253 for (i = 0; i < count; ++i)
21254 {
21255 unsigned int opcode, bytes_read;
21256 unsigned long arg;
21257
21258 opcode = read_1_byte (abfd, mac_ptr);
21259 ++mac_ptr;
21260 opcode_definitions[opcode] = mac_ptr;
21261 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21262 mac_ptr += bytes_read;
21263 mac_ptr += arg;
21264 }
21265 }
21266 }
21267
21268 return mac_ptr;
21269 }
21270
21271 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21272 including DW_MACRO_GNU_transparent_include. */
21273
21274 static void
21275 dwarf_decode_macro_bytes (bfd *abfd,
21276 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21277 struct macro_source_file *current_file,
21278 struct line_header *lh,
21279 struct dwarf2_section_info *section,
21280 int section_is_gnu, int section_is_dwz,
21281 unsigned int offset_size,
21282 htab_t include_hash)
21283 {
21284 struct objfile *objfile = dwarf2_per_objfile->objfile;
21285 enum dwarf_macro_record_type macinfo_type;
21286 int at_commandline;
21287 const gdb_byte *opcode_definitions[256];
21288
21289 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21290 &offset_size, section_is_gnu);
21291 if (mac_ptr == NULL)
21292 {
21293 /* We already issued a complaint. */
21294 return;
21295 }
21296
21297 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21298 GDB is still reading the definitions from command line. First
21299 DW_MACINFO_start_file will need to be ignored as it was already executed
21300 to create CURRENT_FILE for the main source holding also the command line
21301 definitions. On first met DW_MACINFO_start_file this flag is reset to
21302 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21303
21304 at_commandline = 1;
21305
21306 do
21307 {
21308 /* Do we at least have room for a macinfo type byte? */
21309 if (mac_ptr >= mac_end)
21310 {
21311 dwarf2_section_buffer_overflow_complaint (section);
21312 break;
21313 }
21314
21315 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21316 mac_ptr++;
21317
21318 /* Note that we rely on the fact that the corresponding GNU and
21319 DWARF constants are the same. */
21320 switch (macinfo_type)
21321 {
21322 /* A zero macinfo type indicates the end of the macro
21323 information. */
21324 case 0:
21325 break;
21326
21327 case DW_MACRO_GNU_define:
21328 case DW_MACRO_GNU_undef:
21329 case DW_MACRO_GNU_define_indirect:
21330 case DW_MACRO_GNU_undef_indirect:
21331 case DW_MACRO_GNU_define_indirect_alt:
21332 case DW_MACRO_GNU_undef_indirect_alt:
21333 {
21334 unsigned int bytes_read;
21335 int line;
21336 const char *body;
21337 int is_define;
21338
21339 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21340 mac_ptr += bytes_read;
21341
21342 if (macinfo_type == DW_MACRO_GNU_define
21343 || macinfo_type == DW_MACRO_GNU_undef)
21344 {
21345 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21346 mac_ptr += bytes_read;
21347 }
21348 else
21349 {
21350 LONGEST str_offset;
21351
21352 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21353 mac_ptr += offset_size;
21354
21355 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
21356 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21357 || section_is_dwz)
21358 {
21359 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21360
21361 body = read_indirect_string_from_dwz (dwz, str_offset);
21362 }
21363 else
21364 body = read_indirect_string_at_offset (abfd, str_offset);
21365 }
21366
21367 is_define = (macinfo_type == DW_MACRO_GNU_define
21368 || macinfo_type == DW_MACRO_GNU_define_indirect
21369 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
21370 if (! current_file)
21371 {
21372 /* DWARF violation as no main source is present. */
21373 complaint (&symfile_complaints,
21374 _("debug info with no main source gives macro %s "
21375 "on line %d: %s"),
21376 is_define ? _("definition") : _("undefinition"),
21377 line, body);
21378 break;
21379 }
21380 if ((line == 0 && !at_commandline)
21381 || (line != 0 && at_commandline))
21382 complaint (&symfile_complaints,
21383 _("debug info gives %s macro %s with %s line %d: %s"),
21384 at_commandline ? _("command-line") : _("in-file"),
21385 is_define ? _("definition") : _("undefinition"),
21386 line == 0 ? _("zero") : _("non-zero"), line, body);
21387
21388 if (is_define)
21389 parse_macro_definition (current_file, line, body);
21390 else
21391 {
21392 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
21393 || macinfo_type == DW_MACRO_GNU_undef_indirect
21394 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
21395 macro_undef (current_file, line, body);
21396 }
21397 }
21398 break;
21399
21400 case DW_MACRO_GNU_start_file:
21401 {
21402 unsigned int bytes_read;
21403 int line, file;
21404
21405 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21406 mac_ptr += bytes_read;
21407 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21408 mac_ptr += bytes_read;
21409
21410 if ((line == 0 && !at_commandline)
21411 || (line != 0 && at_commandline))
21412 complaint (&symfile_complaints,
21413 _("debug info gives source %d included "
21414 "from %s at %s line %d"),
21415 file, at_commandline ? _("command-line") : _("file"),
21416 line == 0 ? _("zero") : _("non-zero"), line);
21417
21418 if (at_commandline)
21419 {
21420 /* This DW_MACRO_GNU_start_file was executed in the
21421 pass one. */
21422 at_commandline = 0;
21423 }
21424 else
21425 current_file = macro_start_file (file, line, current_file, lh);
21426 }
21427 break;
21428
21429 case DW_MACRO_GNU_end_file:
21430 if (! current_file)
21431 complaint (&symfile_complaints,
21432 _("macro debug info has an unmatched "
21433 "`close_file' directive"));
21434 else
21435 {
21436 current_file = current_file->included_by;
21437 if (! current_file)
21438 {
21439 enum dwarf_macro_record_type next_type;
21440
21441 /* GCC circa March 2002 doesn't produce the zero
21442 type byte marking the end of the compilation
21443 unit. Complain if it's not there, but exit no
21444 matter what. */
21445
21446 /* Do we at least have room for a macinfo type byte? */
21447 if (mac_ptr >= mac_end)
21448 {
21449 dwarf2_section_buffer_overflow_complaint (section);
21450 return;
21451 }
21452
21453 /* We don't increment mac_ptr here, so this is just
21454 a look-ahead. */
21455 next_type
21456 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21457 mac_ptr);
21458 if (next_type != 0)
21459 complaint (&symfile_complaints,
21460 _("no terminating 0-type entry for "
21461 "macros in `.debug_macinfo' section"));
21462
21463 return;
21464 }
21465 }
21466 break;
21467
21468 case DW_MACRO_GNU_transparent_include:
21469 case DW_MACRO_GNU_transparent_include_alt:
21470 {
21471 LONGEST offset;
21472 void **slot;
21473 bfd *include_bfd = abfd;
21474 struct dwarf2_section_info *include_section = section;
21475 struct dwarf2_section_info alt_section;
21476 const gdb_byte *include_mac_end = mac_end;
21477 int is_dwz = section_is_dwz;
21478 const gdb_byte *new_mac_ptr;
21479
21480 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21481 mac_ptr += offset_size;
21482
21483 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21484 {
21485 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21486
21487 dwarf2_read_section (objfile, &dwz->macro);
21488
21489 include_section = &dwz->macro;
21490 include_bfd = get_section_bfd_owner (include_section);
21491 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21492 is_dwz = 1;
21493 }
21494
21495 new_mac_ptr = include_section->buffer + offset;
21496 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21497
21498 if (*slot != NULL)
21499 {
21500 /* This has actually happened; see
21501 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21502 complaint (&symfile_complaints,
21503 _("recursive DW_MACRO_GNU_transparent_include in "
21504 ".debug_macro section"));
21505 }
21506 else
21507 {
21508 *slot = (void *) new_mac_ptr;
21509
21510 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
21511 include_mac_end, current_file, lh,
21512 section, section_is_gnu, is_dwz,
21513 offset_size, include_hash);
21514
21515 htab_remove_elt (include_hash, (void *) new_mac_ptr);
21516 }
21517 }
21518 break;
21519
21520 case DW_MACINFO_vendor_ext:
21521 if (!section_is_gnu)
21522 {
21523 unsigned int bytes_read;
21524 int constant;
21525
21526 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21527 mac_ptr += bytes_read;
21528 read_direct_string (abfd, mac_ptr, &bytes_read);
21529 mac_ptr += bytes_read;
21530
21531 /* We don't recognize any vendor extensions. */
21532 break;
21533 }
21534 /* FALLTHROUGH */
21535
21536 default:
21537 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21538 mac_ptr, mac_end, abfd, offset_size,
21539 section);
21540 if (mac_ptr == NULL)
21541 return;
21542 break;
21543 }
21544 } while (macinfo_type != 0);
21545 }
21546
21547 static void
21548 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21549 int section_is_gnu)
21550 {
21551 struct objfile *objfile = dwarf2_per_objfile->objfile;
21552 struct line_header *lh = cu->line_header;
21553 bfd *abfd;
21554 const gdb_byte *mac_ptr, *mac_end;
21555 struct macro_source_file *current_file = 0;
21556 enum dwarf_macro_record_type macinfo_type;
21557 unsigned int offset_size = cu->header.offset_size;
21558 const gdb_byte *opcode_definitions[256];
21559 struct cleanup *cleanup;
21560 htab_t include_hash;
21561 void **slot;
21562 struct dwarf2_section_info *section;
21563 const char *section_name;
21564
21565 if (cu->dwo_unit != NULL)
21566 {
21567 if (section_is_gnu)
21568 {
21569 section = &cu->dwo_unit->dwo_file->sections.macro;
21570 section_name = ".debug_macro.dwo";
21571 }
21572 else
21573 {
21574 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21575 section_name = ".debug_macinfo.dwo";
21576 }
21577 }
21578 else
21579 {
21580 if (section_is_gnu)
21581 {
21582 section = &dwarf2_per_objfile->macro;
21583 section_name = ".debug_macro";
21584 }
21585 else
21586 {
21587 section = &dwarf2_per_objfile->macinfo;
21588 section_name = ".debug_macinfo";
21589 }
21590 }
21591
21592 dwarf2_read_section (objfile, section);
21593 if (section->buffer == NULL)
21594 {
21595 complaint (&symfile_complaints, _("missing %s section"), section_name);
21596 return;
21597 }
21598 abfd = get_section_bfd_owner (section);
21599
21600 /* First pass: Find the name of the base filename.
21601 This filename is needed in order to process all macros whose definition
21602 (or undefinition) comes from the command line. These macros are defined
21603 before the first DW_MACINFO_start_file entry, and yet still need to be
21604 associated to the base file.
21605
21606 To determine the base file name, we scan the macro definitions until we
21607 reach the first DW_MACINFO_start_file entry. We then initialize
21608 CURRENT_FILE accordingly so that any macro definition found before the
21609 first DW_MACINFO_start_file can still be associated to the base file. */
21610
21611 mac_ptr = section->buffer + offset;
21612 mac_end = section->buffer + section->size;
21613
21614 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21615 &offset_size, section_is_gnu);
21616 if (mac_ptr == NULL)
21617 {
21618 /* We already issued a complaint. */
21619 return;
21620 }
21621
21622 do
21623 {
21624 /* Do we at least have room for a macinfo type byte? */
21625 if (mac_ptr >= mac_end)
21626 {
21627 /* Complaint is printed during the second pass as GDB will probably
21628 stop the first pass earlier upon finding
21629 DW_MACINFO_start_file. */
21630 break;
21631 }
21632
21633 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21634 mac_ptr++;
21635
21636 /* Note that we rely on the fact that the corresponding GNU and
21637 DWARF constants are the same. */
21638 switch (macinfo_type)
21639 {
21640 /* A zero macinfo type indicates the end of the macro
21641 information. */
21642 case 0:
21643 break;
21644
21645 case DW_MACRO_GNU_define:
21646 case DW_MACRO_GNU_undef:
21647 /* Only skip the data by MAC_PTR. */
21648 {
21649 unsigned int bytes_read;
21650
21651 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21652 mac_ptr += bytes_read;
21653 read_direct_string (abfd, mac_ptr, &bytes_read);
21654 mac_ptr += bytes_read;
21655 }
21656 break;
21657
21658 case DW_MACRO_GNU_start_file:
21659 {
21660 unsigned int bytes_read;
21661 int line, file;
21662
21663 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21664 mac_ptr += bytes_read;
21665 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21666 mac_ptr += bytes_read;
21667
21668 current_file = macro_start_file (file, line, current_file, lh);
21669 }
21670 break;
21671
21672 case DW_MACRO_GNU_end_file:
21673 /* No data to skip by MAC_PTR. */
21674 break;
21675
21676 case DW_MACRO_GNU_define_indirect:
21677 case DW_MACRO_GNU_undef_indirect:
21678 case DW_MACRO_GNU_define_indirect_alt:
21679 case DW_MACRO_GNU_undef_indirect_alt:
21680 {
21681 unsigned int bytes_read;
21682
21683 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21684 mac_ptr += bytes_read;
21685 mac_ptr += offset_size;
21686 }
21687 break;
21688
21689 case DW_MACRO_GNU_transparent_include:
21690 case DW_MACRO_GNU_transparent_include_alt:
21691 /* Note that, according to the spec, a transparent include
21692 chain cannot call DW_MACRO_GNU_start_file. So, we can just
21693 skip this opcode. */
21694 mac_ptr += offset_size;
21695 break;
21696
21697 case DW_MACINFO_vendor_ext:
21698 /* Only skip the data by MAC_PTR. */
21699 if (!section_is_gnu)
21700 {
21701 unsigned int bytes_read;
21702
21703 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21704 mac_ptr += bytes_read;
21705 read_direct_string (abfd, mac_ptr, &bytes_read);
21706 mac_ptr += bytes_read;
21707 }
21708 /* FALLTHROUGH */
21709
21710 default:
21711 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21712 mac_ptr, mac_end, abfd, offset_size,
21713 section);
21714 if (mac_ptr == NULL)
21715 return;
21716 break;
21717 }
21718 } while (macinfo_type != 0 && current_file == NULL);
21719
21720 /* Second pass: Process all entries.
21721
21722 Use the AT_COMMAND_LINE flag to determine whether we are still processing
21723 command-line macro definitions/undefinitions. This flag is unset when we
21724 reach the first DW_MACINFO_start_file entry. */
21725
21726 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
21727 NULL, xcalloc, xfree);
21728 cleanup = make_cleanup_htab_delete (include_hash);
21729 mac_ptr = section->buffer + offset;
21730 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
21731 *slot = (void *) mac_ptr;
21732 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
21733 current_file, lh, section,
21734 section_is_gnu, 0, offset_size, include_hash);
21735 do_cleanups (cleanup);
21736 }
21737
21738 /* Check if the attribute's form is a DW_FORM_block*
21739 if so return true else false. */
21740
21741 static int
21742 attr_form_is_block (const struct attribute *attr)
21743 {
21744 return (attr == NULL ? 0 :
21745 attr->form == DW_FORM_block1
21746 || attr->form == DW_FORM_block2
21747 || attr->form == DW_FORM_block4
21748 || attr->form == DW_FORM_block
21749 || attr->form == DW_FORM_exprloc);
21750 }
21751
21752 /* Return non-zero if ATTR's value is a section offset --- classes
21753 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
21754 You may use DW_UNSND (attr) to retrieve such offsets.
21755
21756 Section 7.5.4, "Attribute Encodings", explains that no attribute
21757 may have a value that belongs to more than one of these classes; it
21758 would be ambiguous if we did, because we use the same forms for all
21759 of them. */
21760
21761 static int
21762 attr_form_is_section_offset (const struct attribute *attr)
21763 {
21764 return (attr->form == DW_FORM_data4
21765 || attr->form == DW_FORM_data8
21766 || attr->form == DW_FORM_sec_offset);
21767 }
21768
21769 /* Return non-zero if ATTR's value falls in the 'constant' class, or
21770 zero otherwise. When this function returns true, you can apply
21771 dwarf2_get_attr_constant_value to it.
21772
21773 However, note that for some attributes you must check
21774 attr_form_is_section_offset before using this test. DW_FORM_data4
21775 and DW_FORM_data8 are members of both the constant class, and of
21776 the classes that contain offsets into other debug sections
21777 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
21778 that, if an attribute's can be either a constant or one of the
21779 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
21780 taken as section offsets, not constants. */
21781
21782 static int
21783 attr_form_is_constant (const struct attribute *attr)
21784 {
21785 switch (attr->form)
21786 {
21787 case DW_FORM_sdata:
21788 case DW_FORM_udata:
21789 case DW_FORM_data1:
21790 case DW_FORM_data2:
21791 case DW_FORM_data4:
21792 case DW_FORM_data8:
21793 return 1;
21794 default:
21795 return 0;
21796 }
21797 }
21798
21799
21800 /* DW_ADDR is always stored already as sect_offset; despite for the forms
21801 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
21802
21803 static int
21804 attr_form_is_ref (const struct attribute *attr)
21805 {
21806 switch (attr->form)
21807 {
21808 case DW_FORM_ref_addr:
21809 case DW_FORM_ref1:
21810 case DW_FORM_ref2:
21811 case DW_FORM_ref4:
21812 case DW_FORM_ref8:
21813 case DW_FORM_ref_udata:
21814 case DW_FORM_GNU_ref_alt:
21815 return 1;
21816 default:
21817 return 0;
21818 }
21819 }
21820
21821 /* Return the .debug_loc section to use for CU.
21822 For DWO files use .debug_loc.dwo. */
21823
21824 static struct dwarf2_section_info *
21825 cu_debug_loc_section (struct dwarf2_cu *cu)
21826 {
21827 if (cu->dwo_unit)
21828 return &cu->dwo_unit->dwo_file->sections.loc;
21829 return &dwarf2_per_objfile->loc;
21830 }
21831
21832 /* A helper function that fills in a dwarf2_loclist_baton. */
21833
21834 static void
21835 fill_in_loclist_baton (struct dwarf2_cu *cu,
21836 struct dwarf2_loclist_baton *baton,
21837 const struct attribute *attr)
21838 {
21839 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21840
21841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
21842
21843 baton->per_cu = cu->per_cu;
21844 gdb_assert (baton->per_cu);
21845 /* We don't know how long the location list is, but make sure we
21846 don't run off the edge of the section. */
21847 baton->size = section->size - DW_UNSND (attr);
21848 baton->data = section->buffer + DW_UNSND (attr);
21849 baton->base_address = cu->base_address;
21850 baton->from_dwo = cu->dwo_unit != NULL;
21851 }
21852
21853 static void
21854 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21855 struct dwarf2_cu *cu, int is_block)
21856 {
21857 struct objfile *objfile = dwarf2_per_objfile->objfile;
21858 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21859
21860 if (attr_form_is_section_offset (attr)
21861 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21862 the section. If so, fall through to the complaint in the
21863 other branch. */
21864 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
21865 {
21866 struct dwarf2_loclist_baton *baton;
21867
21868 baton = obstack_alloc (&objfile->objfile_obstack,
21869 sizeof (struct dwarf2_loclist_baton));
21870
21871 fill_in_loclist_baton (cu, baton, attr);
21872
21873 if (cu->base_known == 0)
21874 complaint (&symfile_complaints,
21875 _("Location list used without "
21876 "specifying the CU base address."));
21877
21878 SYMBOL_ACLASS_INDEX (sym) = (is_block
21879 ? dwarf2_loclist_block_index
21880 : dwarf2_loclist_index);
21881 SYMBOL_LOCATION_BATON (sym) = baton;
21882 }
21883 else
21884 {
21885 struct dwarf2_locexpr_baton *baton;
21886
21887 baton = obstack_alloc (&objfile->objfile_obstack,
21888 sizeof (struct dwarf2_locexpr_baton));
21889 baton->per_cu = cu->per_cu;
21890 gdb_assert (baton->per_cu);
21891
21892 if (attr_form_is_block (attr))
21893 {
21894 /* Note that we're just copying the block's data pointer
21895 here, not the actual data. We're still pointing into the
21896 info_buffer for SYM's objfile; right now we never release
21897 that buffer, but when we do clean up properly this may
21898 need to change. */
21899 baton->size = DW_BLOCK (attr)->size;
21900 baton->data = DW_BLOCK (attr)->data;
21901 }
21902 else
21903 {
21904 dwarf2_invalid_attrib_class_complaint ("location description",
21905 SYMBOL_NATURAL_NAME (sym));
21906 baton->size = 0;
21907 }
21908
21909 SYMBOL_ACLASS_INDEX (sym) = (is_block
21910 ? dwarf2_locexpr_block_index
21911 : dwarf2_locexpr_index);
21912 SYMBOL_LOCATION_BATON (sym) = baton;
21913 }
21914 }
21915
21916 /* Return the OBJFILE associated with the compilation unit CU. If CU
21917 came from a separate debuginfo file, then the master objfile is
21918 returned. */
21919
21920 struct objfile *
21921 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
21922 {
21923 struct objfile *objfile = per_cu->objfile;
21924
21925 /* Return the master objfile, so that we can report and look up the
21926 correct file containing this variable. */
21927 if (objfile->separate_debug_objfile_backlink)
21928 objfile = objfile->separate_debug_objfile_backlink;
21929
21930 return objfile;
21931 }
21932
21933 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
21934 (CU_HEADERP is unused in such case) or prepare a temporary copy at
21935 CU_HEADERP first. */
21936
21937 static const struct comp_unit_head *
21938 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
21939 struct dwarf2_per_cu_data *per_cu)
21940 {
21941 const gdb_byte *info_ptr;
21942
21943 if (per_cu->cu)
21944 return &per_cu->cu->header;
21945
21946 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
21947
21948 memset (cu_headerp, 0, sizeof (*cu_headerp));
21949 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
21950
21951 return cu_headerp;
21952 }
21953
21954 /* Return the address size given in the compilation unit header for CU. */
21955
21956 int
21957 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
21958 {
21959 struct comp_unit_head cu_header_local;
21960 const struct comp_unit_head *cu_headerp;
21961
21962 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21963
21964 return cu_headerp->addr_size;
21965 }
21966
21967 /* Return the offset size given in the compilation unit header for CU. */
21968
21969 int
21970 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
21971 {
21972 struct comp_unit_head cu_header_local;
21973 const struct comp_unit_head *cu_headerp;
21974
21975 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21976
21977 return cu_headerp->offset_size;
21978 }
21979
21980 /* See its dwarf2loc.h declaration. */
21981
21982 int
21983 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
21984 {
21985 struct comp_unit_head cu_header_local;
21986 const struct comp_unit_head *cu_headerp;
21987
21988 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21989
21990 if (cu_headerp->version == 2)
21991 return cu_headerp->addr_size;
21992 else
21993 return cu_headerp->offset_size;
21994 }
21995
21996 /* Return the text offset of the CU. The returned offset comes from
21997 this CU's objfile. If this objfile came from a separate debuginfo
21998 file, then the offset may be different from the corresponding
21999 offset in the parent objfile. */
22000
22001 CORE_ADDR
22002 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22003 {
22004 struct objfile *objfile = per_cu->objfile;
22005
22006 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22007 }
22008
22009 /* Locate the .debug_info compilation unit from CU's objfile which contains
22010 the DIE at OFFSET. Raises an error on failure. */
22011
22012 static struct dwarf2_per_cu_data *
22013 dwarf2_find_containing_comp_unit (sect_offset offset,
22014 unsigned int offset_in_dwz,
22015 struct objfile *objfile)
22016 {
22017 struct dwarf2_per_cu_data *this_cu;
22018 int low, high;
22019 const sect_offset *cu_off;
22020
22021 low = 0;
22022 high = dwarf2_per_objfile->n_comp_units - 1;
22023 while (high > low)
22024 {
22025 struct dwarf2_per_cu_data *mid_cu;
22026 int mid = low + (high - low) / 2;
22027
22028 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22029 cu_off = &mid_cu->offset;
22030 if (mid_cu->is_dwz > offset_in_dwz
22031 || (mid_cu->is_dwz == offset_in_dwz
22032 && cu_off->sect_off >= offset.sect_off))
22033 high = mid;
22034 else
22035 low = mid + 1;
22036 }
22037 gdb_assert (low == high);
22038 this_cu = dwarf2_per_objfile->all_comp_units[low];
22039 cu_off = &this_cu->offset;
22040 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22041 {
22042 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22043 error (_("Dwarf Error: could not find partial DIE containing "
22044 "offset 0x%lx [in module %s]"),
22045 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22046
22047 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22048 <= offset.sect_off);
22049 return dwarf2_per_objfile->all_comp_units[low-1];
22050 }
22051 else
22052 {
22053 this_cu = dwarf2_per_objfile->all_comp_units[low];
22054 if (low == dwarf2_per_objfile->n_comp_units - 1
22055 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22056 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22057 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22058 return this_cu;
22059 }
22060 }
22061
22062 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22063
22064 static void
22065 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22066 {
22067 memset (cu, 0, sizeof (*cu));
22068 per_cu->cu = cu;
22069 cu->per_cu = per_cu;
22070 cu->objfile = per_cu->objfile;
22071 obstack_init (&cu->comp_unit_obstack);
22072 }
22073
22074 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22075
22076 static void
22077 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22078 enum language pretend_language)
22079 {
22080 struct attribute *attr;
22081
22082 /* Set the language we're debugging. */
22083 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22084 if (attr)
22085 set_cu_language (DW_UNSND (attr), cu);
22086 else
22087 {
22088 cu->language = pretend_language;
22089 cu->language_defn = language_def (cu->language);
22090 }
22091
22092 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22093 }
22094
22095 /* Release one cached compilation unit, CU. We unlink it from the tree
22096 of compilation units, but we don't remove it from the read_in_chain;
22097 the caller is responsible for that.
22098 NOTE: DATA is a void * because this function is also used as a
22099 cleanup routine. */
22100
22101 static void
22102 free_heap_comp_unit (void *data)
22103 {
22104 struct dwarf2_cu *cu = data;
22105
22106 gdb_assert (cu->per_cu != NULL);
22107 cu->per_cu->cu = NULL;
22108 cu->per_cu = NULL;
22109
22110 obstack_free (&cu->comp_unit_obstack, NULL);
22111
22112 xfree (cu);
22113 }
22114
22115 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22116 when we're finished with it. We can't free the pointer itself, but be
22117 sure to unlink it from the cache. Also release any associated storage. */
22118
22119 static void
22120 free_stack_comp_unit (void *data)
22121 {
22122 struct dwarf2_cu *cu = data;
22123
22124 gdb_assert (cu->per_cu != NULL);
22125 cu->per_cu->cu = NULL;
22126 cu->per_cu = NULL;
22127
22128 obstack_free (&cu->comp_unit_obstack, NULL);
22129 cu->partial_dies = NULL;
22130 }
22131
22132 /* Free all cached compilation units. */
22133
22134 static void
22135 free_cached_comp_units (void *data)
22136 {
22137 struct dwarf2_per_cu_data *per_cu, **last_chain;
22138
22139 per_cu = dwarf2_per_objfile->read_in_chain;
22140 last_chain = &dwarf2_per_objfile->read_in_chain;
22141 while (per_cu != NULL)
22142 {
22143 struct dwarf2_per_cu_data *next_cu;
22144
22145 next_cu = per_cu->cu->read_in_chain;
22146
22147 free_heap_comp_unit (per_cu->cu);
22148 *last_chain = next_cu;
22149
22150 per_cu = next_cu;
22151 }
22152 }
22153
22154 /* Increase the age counter on each cached compilation unit, and free
22155 any that are too old. */
22156
22157 static void
22158 age_cached_comp_units (void)
22159 {
22160 struct dwarf2_per_cu_data *per_cu, **last_chain;
22161
22162 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22163 per_cu = dwarf2_per_objfile->read_in_chain;
22164 while (per_cu != NULL)
22165 {
22166 per_cu->cu->last_used ++;
22167 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22168 dwarf2_mark (per_cu->cu);
22169 per_cu = per_cu->cu->read_in_chain;
22170 }
22171
22172 per_cu = dwarf2_per_objfile->read_in_chain;
22173 last_chain = &dwarf2_per_objfile->read_in_chain;
22174 while (per_cu != NULL)
22175 {
22176 struct dwarf2_per_cu_data *next_cu;
22177
22178 next_cu = per_cu->cu->read_in_chain;
22179
22180 if (!per_cu->cu->mark)
22181 {
22182 free_heap_comp_unit (per_cu->cu);
22183 *last_chain = next_cu;
22184 }
22185 else
22186 last_chain = &per_cu->cu->read_in_chain;
22187
22188 per_cu = next_cu;
22189 }
22190 }
22191
22192 /* Remove a single compilation unit from the cache. */
22193
22194 static void
22195 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22196 {
22197 struct dwarf2_per_cu_data *per_cu, **last_chain;
22198
22199 per_cu = dwarf2_per_objfile->read_in_chain;
22200 last_chain = &dwarf2_per_objfile->read_in_chain;
22201 while (per_cu != NULL)
22202 {
22203 struct dwarf2_per_cu_data *next_cu;
22204
22205 next_cu = per_cu->cu->read_in_chain;
22206
22207 if (per_cu == target_per_cu)
22208 {
22209 free_heap_comp_unit (per_cu->cu);
22210 per_cu->cu = NULL;
22211 *last_chain = next_cu;
22212 break;
22213 }
22214 else
22215 last_chain = &per_cu->cu->read_in_chain;
22216
22217 per_cu = next_cu;
22218 }
22219 }
22220
22221 /* Release all extra memory associated with OBJFILE. */
22222
22223 void
22224 dwarf2_free_objfile (struct objfile *objfile)
22225 {
22226 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
22227
22228 if (dwarf2_per_objfile == NULL)
22229 return;
22230
22231 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22232 free_cached_comp_units (NULL);
22233
22234 if (dwarf2_per_objfile->quick_file_names_table)
22235 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22236
22237 if (dwarf2_per_objfile->line_header_hash)
22238 htab_delete (dwarf2_per_objfile->line_header_hash);
22239
22240 /* Everything else should be on the objfile obstack. */
22241 }
22242
22243 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22244 We store these in a hash table separate from the DIEs, and preserve them
22245 when the DIEs are flushed out of cache.
22246
22247 The CU "per_cu" pointer is needed because offset alone is not enough to
22248 uniquely identify the type. A file may have multiple .debug_types sections,
22249 or the type may come from a DWO file. Furthermore, while it's more logical
22250 to use per_cu->section+offset, with Fission the section with the data is in
22251 the DWO file but we don't know that section at the point we need it.
22252 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22253 because we can enter the lookup routine, get_die_type_at_offset, from
22254 outside this file, and thus won't necessarily have PER_CU->cu.
22255 Fortunately, PER_CU is stable for the life of the objfile. */
22256
22257 struct dwarf2_per_cu_offset_and_type
22258 {
22259 const struct dwarf2_per_cu_data *per_cu;
22260 sect_offset offset;
22261 struct type *type;
22262 };
22263
22264 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22265
22266 static hashval_t
22267 per_cu_offset_and_type_hash (const void *item)
22268 {
22269 const struct dwarf2_per_cu_offset_and_type *ofs = item;
22270
22271 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22272 }
22273
22274 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22275
22276 static int
22277 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22278 {
22279 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
22280 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
22281
22282 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22283 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22284 }
22285
22286 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22287 table if necessary. For convenience, return TYPE.
22288
22289 The DIEs reading must have careful ordering to:
22290 * Not cause infite loops trying to read in DIEs as a prerequisite for
22291 reading current DIE.
22292 * Not trying to dereference contents of still incompletely read in types
22293 while reading in other DIEs.
22294 * Enable referencing still incompletely read in types just by a pointer to
22295 the type without accessing its fields.
22296
22297 Therefore caller should follow these rules:
22298 * Try to fetch any prerequisite types we may need to build this DIE type
22299 before building the type and calling set_die_type.
22300 * After building type call set_die_type for current DIE as soon as
22301 possible before fetching more types to complete the current type.
22302 * Make the type as complete as possible before fetching more types. */
22303
22304 static struct type *
22305 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22306 {
22307 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22308 struct objfile *objfile = cu->objfile;
22309 struct attribute *attr;
22310 struct dynamic_prop prop;
22311
22312 /* For Ada types, make sure that the gnat-specific data is always
22313 initialized (if not already set). There are a few types where
22314 we should not be doing so, because the type-specific area is
22315 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22316 where the type-specific area is used to store the floatformat).
22317 But this is not a problem, because the gnat-specific information
22318 is actually not needed for these types. */
22319 if (need_gnat_info (cu)
22320 && TYPE_CODE (type) != TYPE_CODE_FUNC
22321 && TYPE_CODE (type) != TYPE_CODE_FLT
22322 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22323 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22324 && TYPE_CODE (type) != TYPE_CODE_METHOD
22325 && !HAVE_GNAT_AUX_INFO (type))
22326 INIT_GNAT_SPECIFIC (type);
22327
22328 /* Read DW_AT_data_location and set in type. */
22329 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22330 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22331 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22332
22333 if (dwarf2_per_objfile->die_type_hash == NULL)
22334 {
22335 dwarf2_per_objfile->die_type_hash =
22336 htab_create_alloc_ex (127,
22337 per_cu_offset_and_type_hash,
22338 per_cu_offset_and_type_eq,
22339 NULL,
22340 &objfile->objfile_obstack,
22341 hashtab_obstack_allocate,
22342 dummy_obstack_deallocate);
22343 }
22344
22345 ofs.per_cu = cu->per_cu;
22346 ofs.offset = die->offset;
22347 ofs.type = type;
22348 slot = (struct dwarf2_per_cu_offset_and_type **)
22349 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22350 if (*slot)
22351 complaint (&symfile_complaints,
22352 _("A problem internal to GDB: DIE 0x%x has type already set"),
22353 die->offset.sect_off);
22354 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
22355 **slot = ofs;
22356 return type;
22357 }
22358
22359 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22360 or return NULL if the die does not have a saved type. */
22361
22362 static struct type *
22363 get_die_type_at_offset (sect_offset offset,
22364 struct dwarf2_per_cu_data *per_cu)
22365 {
22366 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22367
22368 if (dwarf2_per_objfile->die_type_hash == NULL)
22369 return NULL;
22370
22371 ofs.per_cu = per_cu;
22372 ofs.offset = offset;
22373 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
22374 if (slot)
22375 return slot->type;
22376 else
22377 return NULL;
22378 }
22379
22380 /* Look up the type for DIE in CU in die_type_hash,
22381 or return NULL if DIE does not have a saved type. */
22382
22383 static struct type *
22384 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22385 {
22386 return get_die_type_at_offset (die->offset, cu->per_cu);
22387 }
22388
22389 /* Add a dependence relationship from CU to REF_PER_CU. */
22390
22391 static void
22392 dwarf2_add_dependence (struct dwarf2_cu *cu,
22393 struct dwarf2_per_cu_data *ref_per_cu)
22394 {
22395 void **slot;
22396
22397 if (cu->dependencies == NULL)
22398 cu->dependencies
22399 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22400 NULL, &cu->comp_unit_obstack,
22401 hashtab_obstack_allocate,
22402 dummy_obstack_deallocate);
22403
22404 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22405 if (*slot == NULL)
22406 *slot = ref_per_cu;
22407 }
22408
22409 /* Subroutine of dwarf2_mark to pass to htab_traverse.
22410 Set the mark field in every compilation unit in the
22411 cache that we must keep because we are keeping CU. */
22412
22413 static int
22414 dwarf2_mark_helper (void **slot, void *data)
22415 {
22416 struct dwarf2_per_cu_data *per_cu;
22417
22418 per_cu = (struct dwarf2_per_cu_data *) *slot;
22419
22420 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22421 reading of the chain. As such dependencies remain valid it is not much
22422 useful to track and undo them during QUIT cleanups. */
22423 if (per_cu->cu == NULL)
22424 return 1;
22425
22426 if (per_cu->cu->mark)
22427 return 1;
22428 per_cu->cu->mark = 1;
22429
22430 if (per_cu->cu->dependencies != NULL)
22431 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22432
22433 return 1;
22434 }
22435
22436 /* Set the mark field in CU and in every other compilation unit in the
22437 cache that we must keep because we are keeping CU. */
22438
22439 static void
22440 dwarf2_mark (struct dwarf2_cu *cu)
22441 {
22442 if (cu->mark)
22443 return;
22444 cu->mark = 1;
22445 if (cu->dependencies != NULL)
22446 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
22447 }
22448
22449 static void
22450 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22451 {
22452 while (per_cu)
22453 {
22454 per_cu->cu->mark = 0;
22455 per_cu = per_cu->cu->read_in_chain;
22456 }
22457 }
22458
22459 /* Trivial hash function for partial_die_info: the hash value of a DIE
22460 is its offset in .debug_info for this objfile. */
22461
22462 static hashval_t
22463 partial_die_hash (const void *item)
22464 {
22465 const struct partial_die_info *part_die = item;
22466
22467 return part_die->offset.sect_off;
22468 }
22469
22470 /* Trivial comparison function for partial_die_info structures: two DIEs
22471 are equal if they have the same offset. */
22472
22473 static int
22474 partial_die_eq (const void *item_lhs, const void *item_rhs)
22475 {
22476 const struct partial_die_info *part_die_lhs = item_lhs;
22477 const struct partial_die_info *part_die_rhs = item_rhs;
22478
22479 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
22480 }
22481
22482 static struct cmd_list_element *set_dwarf_cmdlist;
22483 static struct cmd_list_element *show_dwarf_cmdlist;
22484
22485 static void
22486 set_dwarf_cmd (char *args, int from_tty)
22487 {
22488 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
22489 gdb_stdout);
22490 }
22491
22492 static void
22493 show_dwarf_cmd (char *args, int from_tty)
22494 {
22495 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
22496 }
22497
22498 /* Free data associated with OBJFILE, if necessary. */
22499
22500 static void
22501 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
22502 {
22503 struct dwarf2_per_objfile *data = d;
22504 int ix;
22505
22506 /* Make sure we don't accidentally use dwarf2_per_objfile while
22507 cleaning up. */
22508 dwarf2_per_objfile = NULL;
22509
22510 for (ix = 0; ix < data->n_comp_units; ++ix)
22511 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
22512
22513 for (ix = 0; ix < data->n_type_units; ++ix)
22514 VEC_free (dwarf2_per_cu_ptr,
22515 data->all_type_units[ix]->per_cu.imported_symtabs);
22516 xfree (data->all_type_units);
22517
22518 VEC_free (dwarf2_section_info_def, data->types);
22519
22520 if (data->dwo_files)
22521 free_dwo_files (data->dwo_files, objfile);
22522 if (data->dwp_file)
22523 gdb_bfd_unref (data->dwp_file->dbfd);
22524
22525 if (data->dwz_file && data->dwz_file->dwz_bfd)
22526 gdb_bfd_unref (data->dwz_file->dwz_bfd);
22527 }
22528
22529 \f
22530 /* The "save gdb-index" command. */
22531
22532 /* The contents of the hash table we create when building the string
22533 table. */
22534 struct strtab_entry
22535 {
22536 offset_type offset;
22537 const char *str;
22538 };
22539
22540 /* Hash function for a strtab_entry.
22541
22542 Function is used only during write_hash_table so no index format backward
22543 compatibility is needed. */
22544
22545 static hashval_t
22546 hash_strtab_entry (const void *e)
22547 {
22548 const struct strtab_entry *entry = e;
22549 return mapped_index_string_hash (INT_MAX, entry->str);
22550 }
22551
22552 /* Equality function for a strtab_entry. */
22553
22554 static int
22555 eq_strtab_entry (const void *a, const void *b)
22556 {
22557 const struct strtab_entry *ea = a;
22558 const struct strtab_entry *eb = b;
22559 return !strcmp (ea->str, eb->str);
22560 }
22561
22562 /* Create a strtab_entry hash table. */
22563
22564 static htab_t
22565 create_strtab (void)
22566 {
22567 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
22568 xfree, xcalloc, xfree);
22569 }
22570
22571 /* Add a string to the constant pool. Return the string's offset in
22572 host order. */
22573
22574 static offset_type
22575 add_string (htab_t table, struct obstack *cpool, const char *str)
22576 {
22577 void **slot;
22578 struct strtab_entry entry;
22579 struct strtab_entry *result;
22580
22581 entry.str = str;
22582 slot = htab_find_slot (table, &entry, INSERT);
22583 if (*slot)
22584 result = *slot;
22585 else
22586 {
22587 result = XNEW (struct strtab_entry);
22588 result->offset = obstack_object_size (cpool);
22589 result->str = str;
22590 obstack_grow_str0 (cpool, str);
22591 *slot = result;
22592 }
22593 return result->offset;
22594 }
22595
22596 /* An entry in the symbol table. */
22597 struct symtab_index_entry
22598 {
22599 /* The name of the symbol. */
22600 const char *name;
22601 /* The offset of the name in the constant pool. */
22602 offset_type index_offset;
22603 /* A sorted vector of the indices of all the CUs that hold an object
22604 of this name. */
22605 VEC (offset_type) *cu_indices;
22606 };
22607
22608 /* The symbol table. This is a power-of-2-sized hash table. */
22609 struct mapped_symtab
22610 {
22611 offset_type n_elements;
22612 offset_type size;
22613 struct symtab_index_entry **data;
22614 };
22615
22616 /* Hash function for a symtab_index_entry. */
22617
22618 static hashval_t
22619 hash_symtab_entry (const void *e)
22620 {
22621 const struct symtab_index_entry *entry = e;
22622 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
22623 sizeof (offset_type) * VEC_length (offset_type,
22624 entry->cu_indices),
22625 0);
22626 }
22627
22628 /* Equality function for a symtab_index_entry. */
22629
22630 static int
22631 eq_symtab_entry (const void *a, const void *b)
22632 {
22633 const struct symtab_index_entry *ea = a;
22634 const struct symtab_index_entry *eb = b;
22635 int len = VEC_length (offset_type, ea->cu_indices);
22636 if (len != VEC_length (offset_type, eb->cu_indices))
22637 return 0;
22638 return !memcmp (VEC_address (offset_type, ea->cu_indices),
22639 VEC_address (offset_type, eb->cu_indices),
22640 sizeof (offset_type) * len);
22641 }
22642
22643 /* Destroy a symtab_index_entry. */
22644
22645 static void
22646 delete_symtab_entry (void *p)
22647 {
22648 struct symtab_index_entry *entry = p;
22649 VEC_free (offset_type, entry->cu_indices);
22650 xfree (entry);
22651 }
22652
22653 /* Create a hash table holding symtab_index_entry objects. */
22654
22655 static htab_t
22656 create_symbol_hash_table (void)
22657 {
22658 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
22659 delete_symtab_entry, xcalloc, xfree);
22660 }
22661
22662 /* Create a new mapped symtab object. */
22663
22664 static struct mapped_symtab *
22665 create_mapped_symtab (void)
22666 {
22667 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
22668 symtab->n_elements = 0;
22669 symtab->size = 1024;
22670 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22671 return symtab;
22672 }
22673
22674 /* Destroy a mapped_symtab. */
22675
22676 static void
22677 cleanup_mapped_symtab (void *p)
22678 {
22679 struct mapped_symtab *symtab = p;
22680 /* The contents of the array are freed when the other hash table is
22681 destroyed. */
22682 xfree (symtab->data);
22683 xfree (symtab);
22684 }
22685
22686 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
22687 the slot.
22688
22689 Function is used only during write_hash_table so no index format backward
22690 compatibility is needed. */
22691
22692 static struct symtab_index_entry **
22693 find_slot (struct mapped_symtab *symtab, const char *name)
22694 {
22695 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
22696
22697 index = hash & (symtab->size - 1);
22698 step = ((hash * 17) & (symtab->size - 1)) | 1;
22699
22700 for (;;)
22701 {
22702 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
22703 return &symtab->data[index];
22704 index = (index + step) & (symtab->size - 1);
22705 }
22706 }
22707
22708 /* Expand SYMTAB's hash table. */
22709
22710 static void
22711 hash_expand (struct mapped_symtab *symtab)
22712 {
22713 offset_type old_size = symtab->size;
22714 offset_type i;
22715 struct symtab_index_entry **old_entries = symtab->data;
22716
22717 symtab->size *= 2;
22718 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22719
22720 for (i = 0; i < old_size; ++i)
22721 {
22722 if (old_entries[i])
22723 {
22724 struct symtab_index_entry **slot = find_slot (symtab,
22725 old_entries[i]->name);
22726 *slot = old_entries[i];
22727 }
22728 }
22729
22730 xfree (old_entries);
22731 }
22732
22733 /* Add an entry to SYMTAB. NAME is the name of the symbol.
22734 CU_INDEX is the index of the CU in which the symbol appears.
22735 IS_STATIC is one if the symbol is static, otherwise zero (global). */
22736
22737 static void
22738 add_index_entry (struct mapped_symtab *symtab, const char *name,
22739 int is_static, gdb_index_symbol_kind kind,
22740 offset_type cu_index)
22741 {
22742 struct symtab_index_entry **slot;
22743 offset_type cu_index_and_attrs;
22744
22745 ++symtab->n_elements;
22746 if (4 * symtab->n_elements / 3 >= symtab->size)
22747 hash_expand (symtab);
22748
22749 slot = find_slot (symtab, name);
22750 if (!*slot)
22751 {
22752 *slot = XNEW (struct symtab_index_entry);
22753 (*slot)->name = name;
22754 /* index_offset is set later. */
22755 (*slot)->cu_indices = NULL;
22756 }
22757
22758 cu_index_and_attrs = 0;
22759 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
22760 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
22761 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
22762
22763 /* We don't want to record an index value twice as we want to avoid the
22764 duplication.
22765 We process all global symbols and then all static symbols
22766 (which would allow us to avoid the duplication by only having to check
22767 the last entry pushed), but a symbol could have multiple kinds in one CU.
22768 To keep things simple we don't worry about the duplication here and
22769 sort and uniqufy the list after we've processed all symbols. */
22770 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
22771 }
22772
22773 /* qsort helper routine for uniquify_cu_indices. */
22774
22775 static int
22776 offset_type_compare (const void *ap, const void *bp)
22777 {
22778 offset_type a = *(offset_type *) ap;
22779 offset_type b = *(offset_type *) bp;
22780
22781 return (a > b) - (b > a);
22782 }
22783
22784 /* Sort and remove duplicates of all symbols' cu_indices lists. */
22785
22786 static void
22787 uniquify_cu_indices (struct mapped_symtab *symtab)
22788 {
22789 int i;
22790
22791 for (i = 0; i < symtab->size; ++i)
22792 {
22793 struct symtab_index_entry *entry = symtab->data[i];
22794
22795 if (entry
22796 && entry->cu_indices != NULL)
22797 {
22798 unsigned int next_to_insert, next_to_check;
22799 offset_type last_value;
22800
22801 qsort (VEC_address (offset_type, entry->cu_indices),
22802 VEC_length (offset_type, entry->cu_indices),
22803 sizeof (offset_type), offset_type_compare);
22804
22805 last_value = VEC_index (offset_type, entry->cu_indices, 0);
22806 next_to_insert = 1;
22807 for (next_to_check = 1;
22808 next_to_check < VEC_length (offset_type, entry->cu_indices);
22809 ++next_to_check)
22810 {
22811 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
22812 != last_value)
22813 {
22814 last_value = VEC_index (offset_type, entry->cu_indices,
22815 next_to_check);
22816 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
22817 last_value);
22818 ++next_to_insert;
22819 }
22820 }
22821 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
22822 }
22823 }
22824 }
22825
22826 /* Add a vector of indices to the constant pool. */
22827
22828 static offset_type
22829 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
22830 struct symtab_index_entry *entry)
22831 {
22832 void **slot;
22833
22834 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
22835 if (!*slot)
22836 {
22837 offset_type len = VEC_length (offset_type, entry->cu_indices);
22838 offset_type val = MAYBE_SWAP (len);
22839 offset_type iter;
22840 int i;
22841
22842 *slot = entry;
22843 entry->index_offset = obstack_object_size (cpool);
22844
22845 obstack_grow (cpool, &val, sizeof (val));
22846 for (i = 0;
22847 VEC_iterate (offset_type, entry->cu_indices, i, iter);
22848 ++i)
22849 {
22850 val = MAYBE_SWAP (iter);
22851 obstack_grow (cpool, &val, sizeof (val));
22852 }
22853 }
22854 else
22855 {
22856 struct symtab_index_entry *old_entry = *slot;
22857 entry->index_offset = old_entry->index_offset;
22858 entry = old_entry;
22859 }
22860 return entry->index_offset;
22861 }
22862
22863 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
22864 constant pool entries going into the obstack CPOOL. */
22865
22866 static void
22867 write_hash_table (struct mapped_symtab *symtab,
22868 struct obstack *output, struct obstack *cpool)
22869 {
22870 offset_type i;
22871 htab_t symbol_hash_table;
22872 htab_t str_table;
22873
22874 symbol_hash_table = create_symbol_hash_table ();
22875 str_table = create_strtab ();
22876
22877 /* We add all the index vectors to the constant pool first, to
22878 ensure alignment is ok. */
22879 for (i = 0; i < symtab->size; ++i)
22880 {
22881 if (symtab->data[i])
22882 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
22883 }
22884
22885 /* Now write out the hash table. */
22886 for (i = 0; i < symtab->size; ++i)
22887 {
22888 offset_type str_off, vec_off;
22889
22890 if (symtab->data[i])
22891 {
22892 str_off = add_string (str_table, cpool, symtab->data[i]->name);
22893 vec_off = symtab->data[i]->index_offset;
22894 }
22895 else
22896 {
22897 /* While 0 is a valid constant pool index, it is not valid
22898 to have 0 for both offsets. */
22899 str_off = 0;
22900 vec_off = 0;
22901 }
22902
22903 str_off = MAYBE_SWAP (str_off);
22904 vec_off = MAYBE_SWAP (vec_off);
22905
22906 obstack_grow (output, &str_off, sizeof (str_off));
22907 obstack_grow (output, &vec_off, sizeof (vec_off));
22908 }
22909
22910 htab_delete (str_table);
22911 htab_delete (symbol_hash_table);
22912 }
22913
22914 /* Struct to map psymtab to CU index in the index file. */
22915 struct psymtab_cu_index_map
22916 {
22917 struct partial_symtab *psymtab;
22918 unsigned int cu_index;
22919 };
22920
22921 static hashval_t
22922 hash_psymtab_cu_index (const void *item)
22923 {
22924 const struct psymtab_cu_index_map *map = item;
22925
22926 return htab_hash_pointer (map->psymtab);
22927 }
22928
22929 static int
22930 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
22931 {
22932 const struct psymtab_cu_index_map *lhs = item_lhs;
22933 const struct psymtab_cu_index_map *rhs = item_rhs;
22934
22935 return lhs->psymtab == rhs->psymtab;
22936 }
22937
22938 /* Helper struct for building the address table. */
22939 struct addrmap_index_data
22940 {
22941 struct objfile *objfile;
22942 struct obstack *addr_obstack;
22943 htab_t cu_index_htab;
22944
22945 /* Non-zero if the previous_* fields are valid.
22946 We can't write an entry until we see the next entry (since it is only then
22947 that we know the end of the entry). */
22948 int previous_valid;
22949 /* Index of the CU in the table of all CUs in the index file. */
22950 unsigned int previous_cu_index;
22951 /* Start address of the CU. */
22952 CORE_ADDR previous_cu_start;
22953 };
22954
22955 /* Write an address entry to OBSTACK. */
22956
22957 static void
22958 add_address_entry (struct objfile *objfile, struct obstack *obstack,
22959 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
22960 {
22961 offset_type cu_index_to_write;
22962 gdb_byte addr[8];
22963 CORE_ADDR baseaddr;
22964
22965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22966
22967 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
22968 obstack_grow (obstack, addr, 8);
22969 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
22970 obstack_grow (obstack, addr, 8);
22971 cu_index_to_write = MAYBE_SWAP (cu_index);
22972 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
22973 }
22974
22975 /* Worker function for traversing an addrmap to build the address table. */
22976
22977 static int
22978 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
22979 {
22980 struct addrmap_index_data *data = datap;
22981 struct partial_symtab *pst = obj;
22982
22983 if (data->previous_valid)
22984 add_address_entry (data->objfile, data->addr_obstack,
22985 data->previous_cu_start, start_addr,
22986 data->previous_cu_index);
22987
22988 data->previous_cu_start = start_addr;
22989 if (pst != NULL)
22990 {
22991 struct psymtab_cu_index_map find_map, *map;
22992 find_map.psymtab = pst;
22993 map = htab_find (data->cu_index_htab, &find_map);
22994 gdb_assert (map != NULL);
22995 data->previous_cu_index = map->cu_index;
22996 data->previous_valid = 1;
22997 }
22998 else
22999 data->previous_valid = 0;
23000
23001 return 0;
23002 }
23003
23004 /* Write OBJFILE's address map to OBSTACK.
23005 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23006 in the index file. */
23007
23008 static void
23009 write_address_map (struct objfile *objfile, struct obstack *obstack,
23010 htab_t cu_index_htab)
23011 {
23012 struct addrmap_index_data addrmap_index_data;
23013
23014 /* When writing the address table, we have to cope with the fact that
23015 the addrmap iterator only provides the start of a region; we have to
23016 wait until the next invocation to get the start of the next region. */
23017
23018 addrmap_index_data.objfile = objfile;
23019 addrmap_index_data.addr_obstack = obstack;
23020 addrmap_index_data.cu_index_htab = cu_index_htab;
23021 addrmap_index_data.previous_valid = 0;
23022
23023 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23024 &addrmap_index_data);
23025
23026 /* It's highly unlikely the last entry (end address = 0xff...ff)
23027 is valid, but we should still handle it.
23028 The end address is recorded as the start of the next region, but that
23029 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23030 anyway. */
23031 if (addrmap_index_data.previous_valid)
23032 add_address_entry (objfile, obstack,
23033 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23034 addrmap_index_data.previous_cu_index);
23035 }
23036
23037 /* Return the symbol kind of PSYM. */
23038
23039 static gdb_index_symbol_kind
23040 symbol_kind (struct partial_symbol *psym)
23041 {
23042 domain_enum domain = PSYMBOL_DOMAIN (psym);
23043 enum address_class aclass = PSYMBOL_CLASS (psym);
23044
23045 switch (domain)
23046 {
23047 case VAR_DOMAIN:
23048 switch (aclass)
23049 {
23050 case LOC_BLOCK:
23051 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23052 case LOC_TYPEDEF:
23053 return GDB_INDEX_SYMBOL_KIND_TYPE;
23054 case LOC_COMPUTED:
23055 case LOC_CONST_BYTES:
23056 case LOC_OPTIMIZED_OUT:
23057 case LOC_STATIC:
23058 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23059 case LOC_CONST:
23060 /* Note: It's currently impossible to recognize psyms as enum values
23061 short of reading the type info. For now punt. */
23062 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23063 default:
23064 /* There are other LOC_FOO values that one might want to classify
23065 as variables, but dwarf2read.c doesn't currently use them. */
23066 return GDB_INDEX_SYMBOL_KIND_OTHER;
23067 }
23068 case STRUCT_DOMAIN:
23069 return GDB_INDEX_SYMBOL_KIND_TYPE;
23070 default:
23071 return GDB_INDEX_SYMBOL_KIND_OTHER;
23072 }
23073 }
23074
23075 /* Add a list of partial symbols to SYMTAB. */
23076
23077 static void
23078 write_psymbols (struct mapped_symtab *symtab,
23079 htab_t psyms_seen,
23080 struct partial_symbol **psymp,
23081 int count,
23082 offset_type cu_index,
23083 int is_static)
23084 {
23085 for (; count-- > 0; ++psymp)
23086 {
23087 struct partial_symbol *psym = *psymp;
23088 void **slot;
23089
23090 if (SYMBOL_LANGUAGE (psym) == language_ada)
23091 error (_("Ada is not currently supported by the index"));
23092
23093 /* Only add a given psymbol once. */
23094 slot = htab_find_slot (psyms_seen, psym, INSERT);
23095 if (!*slot)
23096 {
23097 gdb_index_symbol_kind kind = symbol_kind (psym);
23098
23099 *slot = psym;
23100 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23101 is_static, kind, cu_index);
23102 }
23103 }
23104 }
23105
23106 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23107 exception if there is an error. */
23108
23109 static void
23110 write_obstack (FILE *file, struct obstack *obstack)
23111 {
23112 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23113 file)
23114 != obstack_object_size (obstack))
23115 error (_("couldn't data write to file"));
23116 }
23117
23118 /* Unlink a file if the argument is not NULL. */
23119
23120 static void
23121 unlink_if_set (void *p)
23122 {
23123 char **filename = p;
23124 if (*filename)
23125 unlink (*filename);
23126 }
23127
23128 /* A helper struct used when iterating over debug_types. */
23129 struct signatured_type_index_data
23130 {
23131 struct objfile *objfile;
23132 struct mapped_symtab *symtab;
23133 struct obstack *types_list;
23134 htab_t psyms_seen;
23135 int cu_index;
23136 };
23137
23138 /* A helper function that writes a single signatured_type to an
23139 obstack. */
23140
23141 static int
23142 write_one_signatured_type (void **slot, void *d)
23143 {
23144 struct signatured_type_index_data *info = d;
23145 struct signatured_type *entry = (struct signatured_type *) *slot;
23146 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23147 gdb_byte val[8];
23148
23149 write_psymbols (info->symtab,
23150 info->psyms_seen,
23151 info->objfile->global_psymbols.list
23152 + psymtab->globals_offset,
23153 psymtab->n_global_syms, info->cu_index,
23154 0);
23155 write_psymbols (info->symtab,
23156 info->psyms_seen,
23157 info->objfile->static_psymbols.list
23158 + psymtab->statics_offset,
23159 psymtab->n_static_syms, info->cu_index,
23160 1);
23161
23162 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23163 entry->per_cu.offset.sect_off);
23164 obstack_grow (info->types_list, val, 8);
23165 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23166 entry->type_offset_in_tu.cu_off);
23167 obstack_grow (info->types_list, val, 8);
23168 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23169 obstack_grow (info->types_list, val, 8);
23170
23171 ++info->cu_index;
23172
23173 return 1;
23174 }
23175
23176 /* Recurse into all "included" dependencies and write their symbols as
23177 if they appeared in this psymtab. */
23178
23179 static void
23180 recursively_write_psymbols (struct objfile *objfile,
23181 struct partial_symtab *psymtab,
23182 struct mapped_symtab *symtab,
23183 htab_t psyms_seen,
23184 offset_type cu_index)
23185 {
23186 int i;
23187
23188 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23189 if (psymtab->dependencies[i]->user != NULL)
23190 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23191 symtab, psyms_seen, cu_index);
23192
23193 write_psymbols (symtab,
23194 psyms_seen,
23195 objfile->global_psymbols.list + psymtab->globals_offset,
23196 psymtab->n_global_syms, cu_index,
23197 0);
23198 write_psymbols (symtab,
23199 psyms_seen,
23200 objfile->static_psymbols.list + psymtab->statics_offset,
23201 psymtab->n_static_syms, cu_index,
23202 1);
23203 }
23204
23205 /* Create an index file for OBJFILE in the directory DIR. */
23206
23207 static void
23208 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23209 {
23210 struct cleanup *cleanup;
23211 char *filename, *cleanup_filename;
23212 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23213 struct obstack cu_list, types_cu_list;
23214 int i;
23215 FILE *out_file;
23216 struct mapped_symtab *symtab;
23217 offset_type val, size_of_contents, total_len;
23218 struct stat st;
23219 htab_t psyms_seen;
23220 htab_t cu_index_htab;
23221 struct psymtab_cu_index_map *psymtab_cu_index_map;
23222
23223 if (dwarf2_per_objfile->using_index)
23224 error (_("Cannot use an index to create the index"));
23225
23226 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23227 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23228
23229 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23230 return;
23231
23232 if (stat (objfile_name (objfile), &st) < 0)
23233 perror_with_name (objfile_name (objfile));
23234
23235 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23236 INDEX_SUFFIX, (char *) NULL);
23237 cleanup = make_cleanup (xfree, filename);
23238
23239 out_file = gdb_fopen_cloexec (filename, "wb");
23240 if (!out_file)
23241 error (_("Can't open `%s' for writing"), filename);
23242
23243 cleanup_filename = filename;
23244 make_cleanup (unlink_if_set, &cleanup_filename);
23245
23246 symtab = create_mapped_symtab ();
23247 make_cleanup (cleanup_mapped_symtab, symtab);
23248
23249 obstack_init (&addr_obstack);
23250 make_cleanup_obstack_free (&addr_obstack);
23251
23252 obstack_init (&cu_list);
23253 make_cleanup_obstack_free (&cu_list);
23254
23255 obstack_init (&types_cu_list);
23256 make_cleanup_obstack_free (&types_cu_list);
23257
23258 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
23259 NULL, xcalloc, xfree);
23260 make_cleanup_htab_delete (psyms_seen);
23261
23262 /* While we're scanning CU's create a table that maps a psymtab pointer
23263 (which is what addrmap records) to its index (which is what is recorded
23264 in the index file). This will later be needed to write the address
23265 table. */
23266 cu_index_htab = htab_create_alloc (100,
23267 hash_psymtab_cu_index,
23268 eq_psymtab_cu_index,
23269 NULL, xcalloc, xfree);
23270 make_cleanup_htab_delete (cu_index_htab);
23271 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
23272 xmalloc (sizeof (struct psymtab_cu_index_map)
23273 * dwarf2_per_objfile->n_comp_units);
23274 make_cleanup (xfree, psymtab_cu_index_map);
23275
23276 /* The CU list is already sorted, so we don't need to do additional
23277 work here. Also, the debug_types entries do not appear in
23278 all_comp_units, but only in their own hash table. */
23279 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23280 {
23281 struct dwarf2_per_cu_data *per_cu
23282 = dwarf2_per_objfile->all_comp_units[i];
23283 struct partial_symtab *psymtab = per_cu->v.psymtab;
23284 gdb_byte val[8];
23285 struct psymtab_cu_index_map *map;
23286 void **slot;
23287
23288 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23289 It may be referenced from a local scope but in such case it does not
23290 need to be present in .gdb_index. */
23291 if (psymtab == NULL)
23292 continue;
23293
23294 if (psymtab->user == NULL)
23295 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
23296
23297 map = &psymtab_cu_index_map[i];
23298 map->psymtab = psymtab;
23299 map->cu_index = i;
23300 slot = htab_find_slot (cu_index_htab, map, INSERT);
23301 gdb_assert (slot != NULL);
23302 gdb_assert (*slot == NULL);
23303 *slot = map;
23304
23305 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23306 per_cu->offset.sect_off);
23307 obstack_grow (&cu_list, val, 8);
23308 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23309 obstack_grow (&cu_list, val, 8);
23310 }
23311
23312 /* Dump the address map. */
23313 write_address_map (objfile, &addr_obstack, cu_index_htab);
23314
23315 /* Write out the .debug_type entries, if any. */
23316 if (dwarf2_per_objfile->signatured_types)
23317 {
23318 struct signatured_type_index_data sig_data;
23319
23320 sig_data.objfile = objfile;
23321 sig_data.symtab = symtab;
23322 sig_data.types_list = &types_cu_list;
23323 sig_data.psyms_seen = psyms_seen;
23324 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23325 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23326 write_one_signatured_type, &sig_data);
23327 }
23328
23329 /* Now that we've processed all symbols we can shrink their cu_indices
23330 lists. */
23331 uniquify_cu_indices (symtab);
23332
23333 obstack_init (&constant_pool);
23334 make_cleanup_obstack_free (&constant_pool);
23335 obstack_init (&symtab_obstack);
23336 make_cleanup_obstack_free (&symtab_obstack);
23337 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23338
23339 obstack_init (&contents);
23340 make_cleanup_obstack_free (&contents);
23341 size_of_contents = 6 * sizeof (offset_type);
23342 total_len = size_of_contents;
23343
23344 /* The version number. */
23345 val = MAYBE_SWAP (8);
23346 obstack_grow (&contents, &val, sizeof (val));
23347
23348 /* The offset of the CU list from the start of the file. */
23349 val = MAYBE_SWAP (total_len);
23350 obstack_grow (&contents, &val, sizeof (val));
23351 total_len += obstack_object_size (&cu_list);
23352
23353 /* The offset of the types CU list from the start of the file. */
23354 val = MAYBE_SWAP (total_len);
23355 obstack_grow (&contents, &val, sizeof (val));
23356 total_len += obstack_object_size (&types_cu_list);
23357
23358 /* The offset of the address table from the start of the file. */
23359 val = MAYBE_SWAP (total_len);
23360 obstack_grow (&contents, &val, sizeof (val));
23361 total_len += obstack_object_size (&addr_obstack);
23362
23363 /* The offset of the symbol table from the start of the file. */
23364 val = MAYBE_SWAP (total_len);
23365 obstack_grow (&contents, &val, sizeof (val));
23366 total_len += obstack_object_size (&symtab_obstack);
23367
23368 /* The offset of the constant pool from the start of the file. */
23369 val = MAYBE_SWAP (total_len);
23370 obstack_grow (&contents, &val, sizeof (val));
23371 total_len += obstack_object_size (&constant_pool);
23372
23373 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23374
23375 write_obstack (out_file, &contents);
23376 write_obstack (out_file, &cu_list);
23377 write_obstack (out_file, &types_cu_list);
23378 write_obstack (out_file, &addr_obstack);
23379 write_obstack (out_file, &symtab_obstack);
23380 write_obstack (out_file, &constant_pool);
23381
23382 fclose (out_file);
23383
23384 /* We want to keep the file, so we set cleanup_filename to NULL
23385 here. See unlink_if_set. */
23386 cleanup_filename = NULL;
23387
23388 do_cleanups (cleanup);
23389 }
23390
23391 /* Implementation of the `save gdb-index' command.
23392
23393 Note that the file format used by this command is documented in the
23394 GDB manual. Any changes here must be documented there. */
23395
23396 static void
23397 save_gdb_index_command (char *arg, int from_tty)
23398 {
23399 struct objfile *objfile;
23400
23401 if (!arg || !*arg)
23402 error (_("usage: save gdb-index DIRECTORY"));
23403
23404 ALL_OBJFILES (objfile)
23405 {
23406 struct stat st;
23407
23408 /* If the objfile does not correspond to an actual file, skip it. */
23409 if (stat (objfile_name (objfile), &st) < 0)
23410 continue;
23411
23412 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
23413 if (dwarf2_per_objfile)
23414 {
23415
23416 TRY
23417 {
23418 write_psymtabs_to_index (objfile, arg);
23419 }
23420 CATCH (except, RETURN_MASK_ERROR)
23421 {
23422 exception_fprintf (gdb_stderr, except,
23423 _("Error while writing index for `%s': "),
23424 objfile_name (objfile));
23425 }
23426 END_CATCH
23427 }
23428 }
23429 }
23430
23431 \f
23432
23433 int dwarf_always_disassemble;
23434
23435 static void
23436 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23437 struct cmd_list_element *c, const char *value)
23438 {
23439 fprintf_filtered (file,
23440 _("Whether to always disassemble "
23441 "DWARF expressions is %s.\n"),
23442 value);
23443 }
23444
23445 static void
23446 show_check_physname (struct ui_file *file, int from_tty,
23447 struct cmd_list_element *c, const char *value)
23448 {
23449 fprintf_filtered (file,
23450 _("Whether to check \"physname\" is %s.\n"),
23451 value);
23452 }
23453
23454 void _initialize_dwarf2_read (void);
23455
23456 void
23457 _initialize_dwarf2_read (void)
23458 {
23459 struct cmd_list_element *c;
23460
23461 dwarf2_objfile_data_key
23462 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23463
23464 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23465 Set DWARF specific variables.\n\
23466 Configure DWARF variables such as the cache size"),
23467 &set_dwarf_cmdlist, "maintenance set dwarf ",
23468 0/*allow-unknown*/, &maintenance_set_cmdlist);
23469
23470 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23471 Show DWARF specific variables\n\
23472 Show DWARF variables such as the cache size"),
23473 &show_dwarf_cmdlist, "maintenance show dwarf ",
23474 0/*allow-unknown*/, &maintenance_show_cmdlist);
23475
23476 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23477 &dwarf_max_cache_age, _("\
23478 Set the upper bound on the age of cached DWARF compilation units."), _("\
23479 Show the upper bound on the age of cached DWARF compilation units."), _("\
23480 A higher limit means that cached compilation units will be stored\n\
23481 in memory longer, and more total memory will be used. Zero disables\n\
23482 caching, which can slow down startup."),
23483 NULL,
23484 show_dwarf_max_cache_age,
23485 &set_dwarf_cmdlist,
23486 &show_dwarf_cmdlist);
23487
23488 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
23489 &dwarf_always_disassemble, _("\
23490 Set whether `info address' always disassembles DWARF expressions."), _("\
23491 Show whether `info address' always disassembles DWARF expressions."), _("\
23492 When enabled, DWARF expressions are always printed in an assembly-like\n\
23493 syntax. When disabled, expressions will be printed in a more\n\
23494 conversational style, when possible."),
23495 NULL,
23496 show_dwarf_always_disassemble,
23497 &set_dwarf_cmdlist,
23498 &show_dwarf_cmdlist);
23499
23500 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23501 Set debugging of the DWARF reader."), _("\
23502 Show debugging of the DWARF reader."), _("\
23503 When enabled (non-zero), debugging messages are printed during DWARF\n\
23504 reading and symtab expansion. A value of 1 (one) provides basic\n\
23505 information. A value greater than 1 provides more verbose information."),
23506 NULL,
23507 NULL,
23508 &setdebuglist, &showdebuglist);
23509
23510 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23511 Set debugging of the DWARF DIE reader."), _("\
23512 Show debugging of the DWARF DIE reader."), _("\
23513 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23514 The value is the maximum depth to print."),
23515 NULL,
23516 NULL,
23517 &setdebuglist, &showdebuglist);
23518
23519 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23520 Set debugging of the dwarf line reader."), _("\
23521 Show debugging of the dwarf line reader."), _("\
23522 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23523 A value of 1 (one) provides basic information.\n\
23524 A value greater than 1 provides more verbose information."),
23525 NULL,
23526 NULL,
23527 &setdebuglist, &showdebuglist);
23528
23529 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23530 Set cross-checking of \"physname\" code against demangler."), _("\
23531 Show cross-checking of \"physname\" code against demangler."), _("\
23532 When enabled, GDB's internal \"physname\" code is checked against\n\
23533 the demangler."),
23534 NULL, show_check_physname,
23535 &setdebuglist, &showdebuglist);
23536
23537 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23538 no_class, &use_deprecated_index_sections, _("\
23539 Set whether to use deprecated gdb_index sections."), _("\
23540 Show whether to use deprecated gdb_index sections."), _("\
23541 When enabled, deprecated .gdb_index sections are used anyway.\n\
23542 Normally they are ignored either because of a missing feature or\n\
23543 performance issue.\n\
23544 Warning: This option must be enabled before gdb reads the file."),
23545 NULL,
23546 NULL,
23547 &setlist, &showlist);
23548
23549 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
23550 _("\
23551 Save a gdb-index file.\n\
23552 Usage: save gdb-index DIRECTORY"),
23553 &save_cmdlist);
23554 set_cmd_completer (c, filename_completer);
23555
23556 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23557 &dwarf2_locexpr_funcs);
23558 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23559 &dwarf2_loclist_funcs);
23560
23561 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23562 &dwarf2_block_frame_base_locexpr_funcs);
23563 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23564 &dwarf2_block_frame_base_loclist_funcs);
23565 }
This page took 0.519414 seconds and 5 git commands to generate.