gdb/dwarf2read.c: Sanity check DW_AT_sibling values.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72 #include "build-id.h"
73
74 #include <fcntl.h>
75 #include "gdb_string.h"
76 #include "gdb_assert.h"
77 #include <sys/types.h>
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When non-zero, print basic high level tracing messages.
83 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
84 static int dwarf2_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf2_die_debug = 0;
88
89 /* When non-zero, cross-check physname against demangler. */
90 static int check_physname = 0;
91
92 /* When non-zero, do not reject deprecated .gdb_index sections. */
93 static int use_deprecated_index_sections = 0;
94
95 static const struct objfile_data *dwarf2_objfile_data_key;
96
97 /* The "aclass" indices for various kinds of computed DWARF symbols. */
98
99 static int dwarf2_locexpr_index;
100 static int dwarf2_loclist_index;
101 static int dwarf2_locexpr_block_index;
102 static int dwarf2_loclist_block_index;
103
104 /* A descriptor for dwarf sections.
105
106 S.ASECTION, SIZE are typically initialized when the objfile is first
107 scanned. BUFFER, READIN are filled in later when the section is read.
108 If the section contained compressed data then SIZE is updated to record
109 the uncompressed size of the section.
110
111 DWP file format V2 introduces a wrinkle that is easiest to handle by
112 creating the concept of virtual sections contained within a real section.
113 In DWP V2 the sections of the input DWO files are concatenated together
114 into one section, but section offsets are kept relative to the original
115 input section.
116 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
117 the real section this "virtual" section is contained in, and BUFFER,SIZE
118 describe the virtual section. */
119
120 struct dwarf2_section_info
121 {
122 union
123 {
124 /* If this is a real section, the bfd section. */
125 asection *asection;
126 /* If this is a virtual section, pointer to the containing ("real")
127 section. */
128 struct dwarf2_section_info *containing_section;
129 } s;
130 /* Pointer to section data, only valid if readin. */
131 const gdb_byte *buffer;
132 /* The size of the section, real or virtual. */
133 bfd_size_type size;
134 /* If this is a virtual section, the offset in the real section.
135 Only valid if is_virtual. */
136 bfd_size_type virtual_offset;
137 /* True if we have tried to read this section. */
138 char readin;
139 /* True if this is a virtual section, False otherwise.
140 This specifies which of s.asection and s.containing_section to use. */
141 char is_virtual;
142 };
143
144 typedef struct dwarf2_section_info dwarf2_section_info_def;
145 DEF_VEC_O (dwarf2_section_info_def);
146
147 /* All offsets in the index are of this type. It must be
148 architecture-independent. */
149 typedef uint32_t offset_type;
150
151 DEF_VEC_I (offset_type);
152
153 /* Ensure only legit values are used. */
154 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
155 do { \
156 gdb_assert ((unsigned int) (value) <= 1); \
157 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
158 } while (0)
159
160 /* Ensure only legit values are used. */
161 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
162 do { \
163 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
164 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
165 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
166 } while (0)
167
168 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
169 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
172 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175 /* A description of the mapped index. The file format is described in
176 a comment by the code that writes the index. */
177 struct mapped_index
178 {
179 /* Index data format version. */
180 int version;
181
182 /* The total length of the buffer. */
183 off_t total_size;
184
185 /* A pointer to the address table data. */
186 const gdb_byte *address_table;
187
188 /* Size of the address table data in bytes. */
189 offset_type address_table_size;
190
191 /* The symbol table, implemented as a hash table. */
192 const offset_type *symbol_table;
193
194 /* Size in slots, each slot is 2 offset_types. */
195 offset_type symbol_table_slots;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool;
199 };
200
201 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
202 DEF_VEC_P (dwarf2_per_cu_ptr);
203
204 /* Collection of data recorded per objfile.
205 This hangs off of dwarf2_objfile_data_key. */
206
207 struct dwarf2_per_objfile
208 {
209 struct dwarf2_section_info info;
210 struct dwarf2_section_info abbrev;
211 struct dwarf2_section_info line;
212 struct dwarf2_section_info loc;
213 struct dwarf2_section_info macinfo;
214 struct dwarf2_section_info macro;
215 struct dwarf2_section_info str;
216 struct dwarf2_section_info ranges;
217 struct dwarf2_section_info addr;
218 struct dwarf2_section_info frame;
219 struct dwarf2_section_info eh_frame;
220 struct dwarf2_section_info gdb_index;
221
222 VEC (dwarf2_section_info_def) *types;
223
224 /* Back link. */
225 struct objfile *objfile;
226
227 /* Table of all the compilation units. This is used to locate
228 the target compilation unit of a particular reference. */
229 struct dwarf2_per_cu_data **all_comp_units;
230
231 /* The number of compilation units in ALL_COMP_UNITS. */
232 int n_comp_units;
233
234 /* The number of .debug_types-related CUs. */
235 int n_type_units;
236
237 /* The .debug_types-related CUs (TUs).
238 This is stored in malloc space because we may realloc it. */
239 struct signatured_type **all_type_units;
240
241 /* The number of entries in all_type_unit_groups. */
242 int n_type_unit_groups;
243
244 /* Table of type unit groups.
245 This exists to make it easy to iterate over all CUs and TU groups. */
246 struct type_unit_group **all_type_unit_groups;
247
248 /* Table of struct type_unit_group objects.
249 The hash key is the DW_AT_stmt_list value. */
250 htab_t type_unit_groups;
251
252 /* A table mapping .debug_types signatures to its signatured_type entry.
253 This is NULL if the .debug_types section hasn't been read in yet. */
254 htab_t signatured_types;
255
256 /* Type unit statistics, to see how well the scaling improvements
257 are doing. */
258 struct tu_stats
259 {
260 int nr_uniq_abbrev_tables;
261 int nr_symtabs;
262 int nr_symtab_sharers;
263 int nr_stmt_less_type_units;
264 } tu_stats;
265
266 /* A chain of compilation units that are currently read in, so that
267 they can be freed later. */
268 struct dwarf2_per_cu_data *read_in_chain;
269
270 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
271 This is NULL if the table hasn't been allocated yet. */
272 htab_t dwo_files;
273
274 /* Non-zero if we've check for whether there is a DWP file. */
275 int dwp_checked;
276
277 /* The DWP file if there is one, or NULL. */
278 struct dwp_file *dwp_file;
279
280 /* The shared '.dwz' file, if one exists. This is used when the
281 original data was compressed using 'dwz -m'. */
282 struct dwz_file *dwz_file;
283
284 /* A flag indicating wether this objfile has a section loaded at a
285 VMA of 0. */
286 int has_section_at_zero;
287
288 /* True if we are using the mapped index,
289 or we are faking it for OBJF_READNOW's sake. */
290 unsigned char using_index;
291
292 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
293 struct mapped_index *index_table;
294
295 /* When using index_table, this keeps track of all quick_file_names entries.
296 TUs typically share line table entries with a CU, so we maintain a
297 separate table of all line table entries to support the sharing.
298 Note that while there can be way more TUs than CUs, we've already
299 sorted all the TUs into "type unit groups", grouped by their
300 DW_AT_stmt_list value. Therefore the only sharing done here is with a
301 CU and its associated TU group if there is one. */
302 htab_t quick_file_names_table;
303
304 /* Set during partial symbol reading, to prevent queueing of full
305 symbols. */
306 int reading_partial_symbols;
307
308 /* Table mapping type DIEs to their struct type *.
309 This is NULL if not allocated yet.
310 The mapping is done via (CU/TU + DIE offset) -> type. */
311 htab_t die_type_hash;
312
313 /* The CUs we recently read. */
314 VEC (dwarf2_per_cu_ptr) *just_read_cus;
315 };
316
317 static struct dwarf2_per_objfile *dwarf2_per_objfile;
318
319 /* Default names of the debugging sections. */
320
321 /* Note that if the debugging section has been compressed, it might
322 have a name like .zdebug_info. */
323
324 static const struct dwarf2_debug_sections dwarf2_elf_names =
325 {
326 { ".debug_info", ".zdebug_info" },
327 { ".debug_abbrev", ".zdebug_abbrev" },
328 { ".debug_line", ".zdebug_line" },
329 { ".debug_loc", ".zdebug_loc" },
330 { ".debug_macinfo", ".zdebug_macinfo" },
331 { ".debug_macro", ".zdebug_macro" },
332 { ".debug_str", ".zdebug_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_types", ".zdebug_types" },
335 { ".debug_addr", ".zdebug_addr" },
336 { ".debug_frame", ".zdebug_frame" },
337 { ".eh_frame", NULL },
338 { ".gdb_index", ".zgdb_index" },
339 23
340 };
341
342 /* List of DWO/DWP sections. */
343
344 static const struct dwop_section_names
345 {
346 struct dwarf2_section_names abbrev_dwo;
347 struct dwarf2_section_names info_dwo;
348 struct dwarf2_section_names line_dwo;
349 struct dwarf2_section_names loc_dwo;
350 struct dwarf2_section_names macinfo_dwo;
351 struct dwarf2_section_names macro_dwo;
352 struct dwarf2_section_names str_dwo;
353 struct dwarf2_section_names str_offsets_dwo;
354 struct dwarf2_section_names types_dwo;
355 struct dwarf2_section_names cu_index;
356 struct dwarf2_section_names tu_index;
357 }
358 dwop_section_names =
359 {
360 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
361 { ".debug_info.dwo", ".zdebug_info.dwo" },
362 { ".debug_line.dwo", ".zdebug_line.dwo" },
363 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
364 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
365 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
366 { ".debug_str.dwo", ".zdebug_str.dwo" },
367 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
368 { ".debug_types.dwo", ".zdebug_types.dwo" },
369 { ".debug_cu_index", ".zdebug_cu_index" },
370 { ".debug_tu_index", ".zdebug_tu_index" },
371 };
372
373 /* local data types */
374
375 /* The data in a compilation unit header, after target2host
376 translation, looks like this. */
377 struct comp_unit_head
378 {
379 unsigned int length;
380 short version;
381 unsigned char addr_size;
382 unsigned char signed_addr_p;
383 sect_offset abbrev_offset;
384
385 /* Size of file offsets; either 4 or 8. */
386 unsigned int offset_size;
387
388 /* Size of the length field; either 4 or 12. */
389 unsigned int initial_length_size;
390
391 /* Offset to the first byte of this compilation unit header in the
392 .debug_info section, for resolving relative reference dies. */
393 sect_offset offset;
394
395 /* Offset to first die in this cu from the start of the cu.
396 This will be the first byte following the compilation unit header. */
397 cu_offset first_die_offset;
398 };
399
400 /* Type used for delaying computation of method physnames.
401 See comments for compute_delayed_physnames. */
402 struct delayed_method_info
403 {
404 /* The type to which the method is attached, i.e., its parent class. */
405 struct type *type;
406
407 /* The index of the method in the type's function fieldlists. */
408 int fnfield_index;
409
410 /* The index of the method in the fieldlist. */
411 int index;
412
413 /* The name of the DIE. */
414 const char *name;
415
416 /* The DIE associated with this method. */
417 struct die_info *die;
418 };
419
420 typedef struct delayed_method_info delayed_method_info;
421 DEF_VEC_O (delayed_method_info);
422
423 /* Internal state when decoding a particular compilation unit. */
424 struct dwarf2_cu
425 {
426 /* The objfile containing this compilation unit. */
427 struct objfile *objfile;
428
429 /* The header of the compilation unit. */
430 struct comp_unit_head header;
431
432 /* Base address of this compilation unit. */
433 CORE_ADDR base_address;
434
435 /* Non-zero if base_address has been set. */
436 int base_known;
437
438 /* The language we are debugging. */
439 enum language language;
440 const struct language_defn *language_defn;
441
442 const char *producer;
443
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope;
454
455 /* The abbrev table for this CU.
456 Normally this points to the abbrev table in the objfile.
457 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
458 struct abbrev_table *abbrev_table;
459
460 /* Hash table holding all the loaded partial DIEs
461 with partial_die->offset.SECT_OFF as hash. */
462 htab_t partial_dies;
463
464 /* Storage for things with the same lifetime as this read-in compilation
465 unit, including partial DIEs. */
466 struct obstack comp_unit_obstack;
467
468 /* When multiple dwarf2_cu structures are living in memory, this field
469 chains them all together, so that they can be released efficiently.
470 We will probably also want a generation counter so that most-recently-used
471 compilation units are cached... */
472 struct dwarf2_per_cu_data *read_in_chain;
473
474 /* Backlink to our per_cu entry. */
475 struct dwarf2_per_cu_data *per_cu;
476
477 /* How many compilation units ago was this CU last referenced? */
478 int last_used;
479
480 /* A hash table of DIE cu_offset for following references with
481 die_info->offset.sect_off as hash. */
482 htab_t die_hash;
483
484 /* Full DIEs if read in. */
485 struct die_info *dies;
486
487 /* A set of pointers to dwarf2_per_cu_data objects for compilation
488 units referenced by this one. Only set during full symbol processing;
489 partial symbol tables do not have dependencies. */
490 htab_t dependencies;
491
492 /* Header data from the line table, during full symbol processing. */
493 struct line_header *line_header;
494
495 /* A list of methods which need to have physnames computed
496 after all type information has been read. */
497 VEC (delayed_method_info) *method_list;
498
499 /* To be copied to symtab->call_site_htab. */
500 htab_t call_site_htab;
501
502 /* Non-NULL if this CU came from a DWO file.
503 There is an invariant here that is important to remember:
504 Except for attributes copied from the top level DIE in the "main"
505 (or "stub") file in preparation for reading the DWO file
506 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
507 Either there isn't a DWO file (in which case this is NULL and the point
508 is moot), or there is and either we're not going to read it (in which
509 case this is NULL) or there is and we are reading it (in which case this
510 is non-NULL). */
511 struct dwo_unit *dwo_unit;
512
513 /* The DW_AT_addr_base attribute if present, zero otherwise
514 (zero is a valid value though).
515 Note this value comes from the stub CU/TU's DIE. */
516 ULONGEST addr_base;
517
518 /* The DW_AT_ranges_base attribute if present, zero otherwise
519 (zero is a valid value though).
520 Note this value comes from the stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_ranges_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base;
529
530 /* Mark used when releasing cached dies. */
531 unsigned int mark : 1;
532
533 /* This CU references .debug_loc. See the symtab->locations_valid field.
534 This test is imperfect as there may exist optimized debug code not using
535 any location list and still facing inlining issues if handled as
536 unoptimized code. For a future better test see GCC PR other/32998. */
537 unsigned int has_loclist : 1;
538
539 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
540 if all the producer_is_* fields are valid. This information is cached
541 because profiling CU expansion showed excessive time spent in
542 producer_is_gxx_lt_4_6. */
543 unsigned int checked_producer : 1;
544 unsigned int producer_is_gxx_lt_4_6 : 1;
545 unsigned int producer_is_gcc_lt_4_3 : 1;
546 unsigned int producer_is_icc : 1;
547
548 /* When set, the file that we're processing is known to have
549 debugging info for C++ namespaces. GCC 3.3.x did not produce
550 this information, but later versions do. */
551
552 unsigned int processing_has_namespace_info : 1;
553 };
554
555 /* Persistent data held for a compilation unit, even when not
556 processing it. We put a pointer to this structure in the
557 read_symtab_private field of the psymtab. */
558
559 struct dwarf2_per_cu_data
560 {
561 /* The start offset and length of this compilation unit.
562 NOTE: Unlike comp_unit_head.length, this length includes
563 initial_length_size.
564 If the DIE refers to a DWO file, this is always of the original die,
565 not the DWO file. */
566 sect_offset offset;
567 unsigned int length;
568
569 /* Flag indicating this compilation unit will be read in before
570 any of the current compilation units are processed. */
571 unsigned int queued : 1;
572
573 /* This flag will be set when reading partial DIEs if we need to load
574 absolutely all DIEs for this compilation unit, instead of just the ones
575 we think are interesting. It gets set if we look for a DIE in the
576 hash table and don't find it. */
577 unsigned int load_all_dies : 1;
578
579 /* Non-zero if this CU is from .debug_types.
580 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
581 this is non-zero. */
582 unsigned int is_debug_types : 1;
583
584 /* Non-zero if this CU is from the .dwz file. */
585 unsigned int is_dwz : 1;
586
587 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
588 This flag is only valid if is_debug_types is true.
589 We can't read a CU directly from a DWO file: There are required
590 attributes in the stub. */
591 unsigned int reading_dwo_directly : 1;
592
593 /* Non-zero if the TU has been read.
594 This is used to assist the "Stay in DWO Optimization" for Fission:
595 When reading a DWO, it's faster to read TUs from the DWO instead of
596 fetching them from random other DWOs (due to comdat folding).
597 If the TU has already been read, the optimization is unnecessary
598 (and unwise - we don't want to change where gdb thinks the TU lives
599 "midflight").
600 This flag is only valid if is_debug_types is true. */
601 unsigned int tu_read : 1;
602
603 /* The section this CU/TU lives in.
604 If the DIE refers to a DWO file, this is always the original die,
605 not the DWO file. */
606 struct dwarf2_section_info *section;
607
608 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
609 of the CU cache it gets reset to NULL again. */
610 struct dwarf2_cu *cu;
611
612 /* The corresponding objfile.
613 Normally we can get the objfile from dwarf2_per_objfile.
614 However we can enter this file with just a "per_cu" handle. */
615 struct objfile *objfile;
616
617 /* When using partial symbol tables, the 'psymtab' field is active.
618 Otherwise the 'quick' field is active. */
619 union
620 {
621 /* The partial symbol table associated with this compilation unit,
622 or NULL for unread partial units. */
623 struct partial_symtab *psymtab;
624
625 /* Data needed by the "quick" functions. */
626 struct dwarf2_per_cu_quick_data *quick;
627 } v;
628
629 /* The CUs we import using DW_TAG_imported_unit. This is filled in
630 while reading psymtabs, used to compute the psymtab dependencies,
631 and then cleared. Then it is filled in again while reading full
632 symbols, and only deleted when the objfile is destroyed.
633
634 This is also used to work around a difference between the way gold
635 generates .gdb_index version <=7 and the way gdb does. Arguably this
636 is a gold bug. For symbols coming from TUs, gold records in the index
637 the CU that includes the TU instead of the TU itself. This breaks
638 dw2_lookup_symbol: It assumes that if the index says symbol X lives
639 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
640 will find X. Alas TUs live in their own symtab, so after expanding CU Y
641 we need to look in TU Z to find X. Fortunately, this is akin to
642 DW_TAG_imported_unit, so we just use the same mechanism: For
643 .gdb_index version <=7 this also records the TUs that the CU referred
644 to. Concurrently with this change gdb was modified to emit version 8
645 indices so we only pay a price for gold generated indices.
646 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
647 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
648 };
649
650 /* Entry in the signatured_types hash table. */
651
652 struct signatured_type
653 {
654 /* The "per_cu" object of this type.
655 This struct is used iff per_cu.is_debug_types.
656 N.B.: This is the first member so that it's easy to convert pointers
657 between them. */
658 struct dwarf2_per_cu_data per_cu;
659
660 /* The type's signature. */
661 ULONGEST signature;
662
663 /* Offset in the TU of the type's DIE, as read from the TU header.
664 If this TU is a DWO stub and the definition lives in a DWO file
665 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
666 cu_offset type_offset_in_tu;
667
668 /* Offset in the section of the type's DIE.
669 If the definition lives in a DWO file, this is the offset in the
670 .debug_types.dwo section.
671 The value is zero until the actual value is known.
672 Zero is otherwise not a valid section offset. */
673 sect_offset type_offset_in_section;
674
675 /* Type units are grouped by their DW_AT_stmt_list entry so that they
676 can share them. This points to the containing symtab. */
677 struct type_unit_group *type_unit_group;
678
679 /* The type.
680 The first time we encounter this type we fully read it in and install it
681 in the symbol tables. Subsequent times we only need the type. */
682 struct type *type;
683
684 /* Containing DWO unit.
685 This field is valid iff per_cu.reading_dwo_directly. */
686 struct dwo_unit *dwo_unit;
687 };
688
689 typedef struct signatured_type *sig_type_ptr;
690 DEF_VEC_P (sig_type_ptr);
691
692 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
693 This includes type_unit_group and quick_file_names. */
694
695 struct stmt_list_hash
696 {
697 /* The DWO unit this table is from or NULL if there is none. */
698 struct dwo_unit *dwo_unit;
699
700 /* Offset in .debug_line or .debug_line.dwo. */
701 sect_offset line_offset;
702 };
703
704 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
705 an object of this type. */
706
707 struct type_unit_group
708 {
709 /* dwarf2read.c's main "handle" on a TU symtab.
710 To simplify things we create an artificial CU that "includes" all the
711 type units using this stmt_list so that the rest of the code still has
712 a "per_cu" handle on the symtab.
713 This PER_CU is recognized by having no section. */
714 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
715 struct dwarf2_per_cu_data per_cu;
716
717 /* The TUs that share this DW_AT_stmt_list entry.
718 This is added to while parsing type units to build partial symtabs,
719 and is deleted afterwards and not used again. */
720 VEC (sig_type_ptr) *tus;
721
722 /* The primary symtab.
723 Type units in a group needn't all be defined in the same source file,
724 so we create an essentially anonymous symtab as the primary symtab. */
725 struct symtab *primary_symtab;
726
727 /* The data used to construct the hash key. */
728 struct stmt_list_hash hash;
729
730 /* The number of symtabs from the line header.
731 The value here must match line_header.num_file_names. */
732 unsigned int num_symtabs;
733
734 /* The symbol tables for this TU (obtained from the files listed in
735 DW_AT_stmt_list).
736 WARNING: The order of entries here must match the order of entries
737 in the line header. After the first TU using this type_unit_group, the
738 line header for the subsequent TUs is recreated from this. This is done
739 because we need to use the same symtabs for each TU using the same
740 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
741 there's no guarantee the line header doesn't have duplicate entries. */
742 struct symtab **symtabs;
743 };
744
745 /* These sections are what may appear in a (real or virtual) DWO file. */
746
747 struct dwo_sections
748 {
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str;
755 struct dwarf2_section_info str_offsets;
756 /* In the case of a virtual DWO file, these two are unused. */
757 struct dwarf2_section_info info;
758 VEC (dwarf2_section_info_def) *types;
759 };
760
761 /* CUs/TUs in DWP/DWO files. */
762
763 struct dwo_unit
764 {
765 /* Backlink to the containing struct dwo_file. */
766 struct dwo_file *dwo_file;
767
768 /* The "id" that distinguishes this CU/TU.
769 .debug_info calls this "dwo_id", .debug_types calls this "signature".
770 Since signatures came first, we stick with it for consistency. */
771 ULONGEST signature;
772
773 /* The section this CU/TU lives in, in the DWO file. */
774 struct dwarf2_section_info *section;
775
776 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
777 sect_offset offset;
778 unsigned int length;
779
780 /* For types, offset in the type's DIE of the type defined by this TU. */
781 cu_offset type_offset_in_tu;
782 };
783
784 /* include/dwarf2.h defines the DWP section codes.
785 It defines a max value but it doesn't define a min value, which we
786 use for error checking, so provide one. */
787
788 enum dwp_v2_section_ids
789 {
790 DW_SECT_MIN = 1
791 };
792
793 /* Data for one DWO file.
794
795 This includes virtual DWO files (a virtual DWO file is a DWO file as it
796 appears in a DWP file). DWP files don't really have DWO files per se -
797 comdat folding of types "loses" the DWO file they came from, and from
798 a high level view DWP files appear to contain a mass of random types.
799 However, to maintain consistency with the non-DWP case we pretend DWP
800 files contain virtual DWO files, and we assign each TU with one virtual
801 DWO file (generally based on the line and abbrev section offsets -
802 a heuristic that seems to work in practice). */
803
804 struct dwo_file
805 {
806 /* The DW_AT_GNU_dwo_name attribute.
807 For virtual DWO files the name is constructed from the section offsets
808 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
809 from related CU+TUs. */
810 const char *dwo_name;
811
812 /* The DW_AT_comp_dir attribute. */
813 const char *comp_dir;
814
815 /* The bfd, when the file is open. Otherwise this is NULL.
816 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
817 bfd *dbfd;
818
819 /* The sections that make up this DWO file.
820 Remember that for virtual DWO files in DWP V2, these are virtual
821 sections (for lack of a better name). */
822 struct dwo_sections sections;
823
824 /* The CU in the file.
825 We only support one because having more than one requires hacking the
826 dwo_name of each to match, which is highly unlikely to happen.
827 Doing this means all TUs can share comp_dir: We also assume that
828 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
829 struct dwo_unit *cu;
830
831 /* Table of TUs in the file.
832 Each element is a struct dwo_unit. */
833 htab_t tus;
834 };
835
836 /* These sections are what may appear in a DWP file. */
837
838 struct dwp_sections
839 {
840 /* These are used by both DWP version 1 and 2. */
841 struct dwarf2_section_info str;
842 struct dwarf2_section_info cu_index;
843 struct dwarf2_section_info tu_index;
844
845 /* These are only used by DWP version 2 files.
846 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
847 sections are referenced by section number, and are not recorded here.
848 In DWP version 2 there is at most one copy of all these sections, each
849 section being (effectively) comprised of the concatenation of all of the
850 individual sections that exist in the version 1 format.
851 To keep the code simple we treat each of these concatenated pieces as a
852 section itself (a virtual section?). */
853 struct dwarf2_section_info abbrev;
854 struct dwarf2_section_info info;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info loc;
857 struct dwarf2_section_info macinfo;
858 struct dwarf2_section_info macro;
859 struct dwarf2_section_info str_offsets;
860 struct dwarf2_section_info types;
861 };
862
863 /* These sections are what may appear in a virtual DWO file in DWP version 1.
864 A virtual DWO file is a DWO file as it appears in a DWP file. */
865
866 struct virtual_v1_dwo_sections
867 {
868 struct dwarf2_section_info abbrev;
869 struct dwarf2_section_info line;
870 struct dwarf2_section_info loc;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str_offsets;
874 /* Each DWP hash table entry records one CU or one TU.
875 That is recorded here, and copied to dwo_unit.section. */
876 struct dwarf2_section_info info_or_types;
877 };
878
879 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
880 In version 2, the sections of the DWO files are concatenated together
881 and stored in one section of that name. Thus each ELF section contains
882 several "virtual" sections. */
883
884 struct virtual_v2_dwo_sections
885 {
886 bfd_size_type abbrev_offset;
887 bfd_size_type abbrev_size;
888
889 bfd_size_type line_offset;
890 bfd_size_type line_size;
891
892 bfd_size_type loc_offset;
893 bfd_size_type loc_size;
894
895 bfd_size_type macinfo_offset;
896 bfd_size_type macinfo_size;
897
898 bfd_size_type macro_offset;
899 bfd_size_type macro_size;
900
901 bfd_size_type str_offsets_offset;
902 bfd_size_type str_offsets_size;
903
904 /* Each DWP hash table entry records one CU or one TU.
905 That is recorded here, and copied to dwo_unit.section. */
906 bfd_size_type info_or_types_offset;
907 bfd_size_type info_or_types_size;
908 };
909
910 /* Contents of DWP hash tables. */
911
912 struct dwp_hash_table
913 {
914 uint32_t version, nr_columns;
915 uint32_t nr_units, nr_slots;
916 const gdb_byte *hash_table, *unit_table;
917 union
918 {
919 struct
920 {
921 const gdb_byte *indices;
922 } v1;
923 struct
924 {
925 /* This is indexed by column number and gives the id of the section
926 in that column. */
927 #define MAX_NR_V2_DWO_SECTIONS \
928 (1 /* .debug_info or .debug_types */ \
929 + 1 /* .debug_abbrev */ \
930 + 1 /* .debug_line */ \
931 + 1 /* .debug_loc */ \
932 + 1 /* .debug_str_offsets */ \
933 + 1 /* .debug_macro or .debug_macinfo */)
934 int section_ids[MAX_NR_V2_DWO_SECTIONS];
935 const gdb_byte *offsets;
936 const gdb_byte *sizes;
937 } v2;
938 } section_pool;
939 };
940
941 /* Data for one DWP file. */
942
943 struct dwp_file
944 {
945 /* Name of the file. */
946 const char *name;
947
948 /* File format version. */
949 int version;
950
951 /* The bfd. */
952 bfd *dbfd;
953
954 /* Section info for this file. */
955 struct dwp_sections sections;
956
957 /* Table of CUs in the file. */
958 const struct dwp_hash_table *cus;
959
960 /* Table of TUs in the file. */
961 const struct dwp_hash_table *tus;
962
963 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
964 htab_t loaded_cus;
965 htab_t loaded_tus;
966
967 /* Table to map ELF section numbers to their sections.
968 This is only needed for the DWP V1 file format. */
969 unsigned int num_sections;
970 asection **elf_sections;
971 };
972
973 /* This represents a '.dwz' file. */
974
975 struct dwz_file
976 {
977 /* A dwz file can only contain a few sections. */
978 struct dwarf2_section_info abbrev;
979 struct dwarf2_section_info info;
980 struct dwarf2_section_info str;
981 struct dwarf2_section_info line;
982 struct dwarf2_section_info macro;
983 struct dwarf2_section_info gdb_index;
984
985 /* The dwz's BFD. */
986 bfd *dwz_bfd;
987 };
988
989 /* Struct used to pass misc. parameters to read_die_and_children, et
990 al. which are used for both .debug_info and .debug_types dies.
991 All parameters here are unchanging for the life of the call. This
992 struct exists to abstract away the constant parameters of die reading. */
993
994 struct die_reader_specs
995 {
996 /* The bfd of die_section. */
997 bfd* abfd;
998
999 /* The CU of the DIE we are parsing. */
1000 struct dwarf2_cu *cu;
1001
1002 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1003 struct dwo_file *dwo_file;
1004
1005 /* The section the die comes from.
1006 This is either .debug_info or .debug_types, or the .dwo variants. */
1007 struct dwarf2_section_info *die_section;
1008
1009 /* die_section->buffer. */
1010 const gdb_byte *buffer;
1011
1012 /* The end of the buffer. */
1013 const gdb_byte *buffer_end;
1014
1015 /* The value of the DW_AT_comp_dir attribute. */
1016 const char *comp_dir;
1017 };
1018
1019 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1020 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1021 const gdb_byte *info_ptr,
1022 struct die_info *comp_unit_die,
1023 int has_children,
1024 void *data);
1025
1026 /* The line number information for a compilation unit (found in the
1027 .debug_line section) begins with a "statement program header",
1028 which contains the following information. */
1029 struct line_header
1030 {
1031 unsigned int total_length;
1032 unsigned short version;
1033 unsigned int header_length;
1034 unsigned char minimum_instruction_length;
1035 unsigned char maximum_ops_per_instruction;
1036 unsigned char default_is_stmt;
1037 int line_base;
1038 unsigned char line_range;
1039 unsigned char opcode_base;
1040
1041 /* standard_opcode_lengths[i] is the number of operands for the
1042 standard opcode whose value is i. This means that
1043 standard_opcode_lengths[0] is unused, and the last meaningful
1044 element is standard_opcode_lengths[opcode_base - 1]. */
1045 unsigned char *standard_opcode_lengths;
1046
1047 /* The include_directories table. NOTE! These strings are not
1048 allocated with xmalloc; instead, they are pointers into
1049 debug_line_buffer. If you try to free them, `free' will get
1050 indigestion. */
1051 unsigned int num_include_dirs, include_dirs_size;
1052 const char **include_dirs;
1053
1054 /* The file_names table. NOTE! These strings are not allocated
1055 with xmalloc; instead, they are pointers into debug_line_buffer.
1056 Don't try to free them directly. */
1057 unsigned int num_file_names, file_names_size;
1058 struct file_entry
1059 {
1060 const char *name;
1061 unsigned int dir_index;
1062 unsigned int mod_time;
1063 unsigned int length;
1064 int included_p; /* Non-zero if referenced by the Line Number Program. */
1065 struct symtab *symtab; /* The associated symbol table, if any. */
1066 } *file_names;
1067
1068 /* The start and end of the statement program following this
1069 header. These point into dwarf2_per_objfile->line_buffer. */
1070 const gdb_byte *statement_program_start, *statement_program_end;
1071 };
1072
1073 /* When we construct a partial symbol table entry we only
1074 need this much information. */
1075 struct partial_die_info
1076 {
1077 /* Offset of this DIE. */
1078 sect_offset offset;
1079
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
1083 /* Assorted flags describing the data found in this DIE. */
1084 unsigned int has_children : 1;
1085 unsigned int is_external : 1;
1086 unsigned int is_declaration : 1;
1087 unsigned int has_type : 1;
1088 unsigned int has_specification : 1;
1089 unsigned int has_pc_info : 1;
1090 unsigned int may_be_inlined : 1;
1091
1092 /* Flag set if the SCOPE field of this structure has been
1093 computed. */
1094 unsigned int scope_set : 1;
1095
1096 /* Flag set if the DIE has a byte_size attribute. */
1097 unsigned int has_byte_size : 1;
1098
1099 /* Flag set if any of the DIE's children are template arguments. */
1100 unsigned int has_template_arguments : 1;
1101
1102 /* Flag set if fixup_partial_die has been called on this die. */
1103 unsigned int fixup_called : 1;
1104
1105 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1106 unsigned int is_dwz : 1;
1107
1108 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1109 unsigned int spec_is_dwz : 1;
1110
1111 /* The name of this DIE. Normally the value of DW_AT_name, but
1112 sometimes a default name for unnamed DIEs. */
1113 const char *name;
1114
1115 /* The linkage name, if present. */
1116 const char *linkage_name;
1117
1118 /* The scope to prepend to our children. This is generally
1119 allocated on the comp_unit_obstack, so will disappear
1120 when this compilation unit leaves the cache. */
1121 const char *scope;
1122
1123 /* Some data associated with the partial DIE. The tag determines
1124 which field is live. */
1125 union
1126 {
1127 /* The location description associated with this DIE, if any. */
1128 struct dwarf_block *locdesc;
1129 /* The offset of an import, for DW_TAG_imported_unit. */
1130 sect_offset offset;
1131 } d;
1132
1133 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1134 CORE_ADDR lowpc;
1135 CORE_ADDR highpc;
1136
1137 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1138 DW_AT_sibling, if any. */
1139 /* NOTE: This member isn't strictly necessary, read_partial_die could
1140 return DW_AT_sibling values to its caller load_partial_dies. */
1141 const gdb_byte *sibling;
1142
1143 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1144 DW_AT_specification (or DW_AT_abstract_origin or
1145 DW_AT_extension). */
1146 sect_offset spec_offset;
1147
1148 /* Pointers to this DIE's parent, first child, and next sibling,
1149 if any. */
1150 struct partial_die_info *die_parent, *die_child, *die_sibling;
1151 };
1152
1153 /* This data structure holds the information of an abbrev. */
1154 struct abbrev_info
1155 {
1156 unsigned int number; /* number identifying abbrev */
1157 enum dwarf_tag tag; /* dwarf tag */
1158 unsigned short has_children; /* boolean */
1159 unsigned short num_attrs; /* number of attributes */
1160 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1161 struct abbrev_info *next; /* next in chain */
1162 };
1163
1164 struct attr_abbrev
1165 {
1166 ENUM_BITFIELD(dwarf_attribute) name : 16;
1167 ENUM_BITFIELD(dwarf_form) form : 16;
1168 };
1169
1170 /* Size of abbrev_table.abbrev_hash_table. */
1171 #define ABBREV_HASH_SIZE 121
1172
1173 /* Top level data structure to contain an abbreviation table. */
1174
1175 struct abbrev_table
1176 {
1177 /* Where the abbrev table came from.
1178 This is used as a sanity check when the table is used. */
1179 sect_offset offset;
1180
1181 /* Storage for the abbrev table. */
1182 struct obstack abbrev_obstack;
1183
1184 /* Hash table of abbrevs.
1185 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1186 It could be statically allocated, but the previous code didn't so we
1187 don't either. */
1188 struct abbrev_info **abbrevs;
1189 };
1190
1191 /* Attributes have a name and a value. */
1192 struct attribute
1193 {
1194 ENUM_BITFIELD(dwarf_attribute) name : 16;
1195 ENUM_BITFIELD(dwarf_form) form : 15;
1196
1197 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1198 field should be in u.str (existing only for DW_STRING) but it is kept
1199 here for better struct attribute alignment. */
1200 unsigned int string_is_canonical : 1;
1201
1202 union
1203 {
1204 const char *str;
1205 struct dwarf_block *blk;
1206 ULONGEST unsnd;
1207 LONGEST snd;
1208 CORE_ADDR addr;
1209 ULONGEST signature;
1210 }
1211 u;
1212 };
1213
1214 /* This data structure holds a complete die structure. */
1215 struct die_info
1216 {
1217 /* DWARF-2 tag for this DIE. */
1218 ENUM_BITFIELD(dwarf_tag) tag : 16;
1219
1220 /* Number of attributes */
1221 unsigned char num_attrs;
1222
1223 /* True if we're presently building the full type name for the
1224 type derived from this DIE. */
1225 unsigned char building_fullname : 1;
1226
1227 /* Abbrev number */
1228 unsigned int abbrev;
1229
1230 /* Offset in .debug_info or .debug_types section. */
1231 sect_offset offset;
1232
1233 /* The dies in a compilation unit form an n-ary tree. PARENT
1234 points to this die's parent; CHILD points to the first child of
1235 this node; and all the children of a given node are chained
1236 together via their SIBLING fields. */
1237 struct die_info *child; /* Its first child, if any. */
1238 struct die_info *sibling; /* Its next sibling, if any. */
1239 struct die_info *parent; /* Its parent, if any. */
1240
1241 /* An array of attributes, with NUM_ATTRS elements. There may be
1242 zero, but it's not common and zero-sized arrays are not
1243 sufficiently portable C. */
1244 struct attribute attrs[1];
1245 };
1246
1247 /* Get at parts of an attribute structure. */
1248
1249 #define DW_STRING(attr) ((attr)->u.str)
1250 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1251 #define DW_UNSND(attr) ((attr)->u.unsnd)
1252 #define DW_BLOCK(attr) ((attr)->u.blk)
1253 #define DW_SND(attr) ((attr)->u.snd)
1254 #define DW_ADDR(attr) ((attr)->u.addr)
1255 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1256
1257 /* Blocks are a bunch of untyped bytes. */
1258 struct dwarf_block
1259 {
1260 size_t size;
1261
1262 /* Valid only if SIZE is not zero. */
1263 const gdb_byte *data;
1264 };
1265
1266 #ifndef ATTR_ALLOC_CHUNK
1267 #define ATTR_ALLOC_CHUNK 4
1268 #endif
1269
1270 /* Allocate fields for structs, unions and enums in this size. */
1271 #ifndef DW_FIELD_ALLOC_CHUNK
1272 #define DW_FIELD_ALLOC_CHUNK 4
1273 #endif
1274
1275 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1276 but this would require a corresponding change in unpack_field_as_long
1277 and friends. */
1278 static int bits_per_byte = 8;
1279
1280 /* The routines that read and process dies for a C struct or C++ class
1281 pass lists of data member fields and lists of member function fields
1282 in an instance of a field_info structure, as defined below. */
1283 struct field_info
1284 {
1285 /* List of data member and baseclasses fields. */
1286 struct nextfield
1287 {
1288 struct nextfield *next;
1289 int accessibility;
1290 int virtuality;
1291 struct field field;
1292 }
1293 *fields, *baseclasses;
1294
1295 /* Number of fields (including baseclasses). */
1296 int nfields;
1297
1298 /* Number of baseclasses. */
1299 int nbaseclasses;
1300
1301 /* Set if the accesibility of one of the fields is not public. */
1302 int non_public_fields;
1303
1304 /* Member function fields array, entries are allocated in the order they
1305 are encountered in the object file. */
1306 struct nextfnfield
1307 {
1308 struct nextfnfield *next;
1309 struct fn_field fnfield;
1310 }
1311 *fnfields;
1312
1313 /* Member function fieldlist array, contains name of possibly overloaded
1314 member function, number of overloaded member functions and a pointer
1315 to the head of the member function field chain. */
1316 struct fnfieldlist
1317 {
1318 const char *name;
1319 int length;
1320 struct nextfnfield *head;
1321 }
1322 *fnfieldlists;
1323
1324 /* Number of entries in the fnfieldlists array. */
1325 int nfnfields;
1326
1327 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1328 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1329 struct typedef_field_list
1330 {
1331 struct typedef_field field;
1332 struct typedef_field_list *next;
1333 }
1334 *typedef_field_list;
1335 unsigned typedef_field_list_count;
1336 };
1337
1338 /* One item on the queue of compilation units to read in full symbols
1339 for. */
1340 struct dwarf2_queue_item
1341 {
1342 struct dwarf2_per_cu_data *per_cu;
1343 enum language pretend_language;
1344 struct dwarf2_queue_item *next;
1345 };
1346
1347 /* The current queue. */
1348 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1349
1350 /* Loaded secondary compilation units are kept in memory until they
1351 have not been referenced for the processing of this many
1352 compilation units. Set this to zero to disable caching. Cache
1353 sizes of up to at least twenty will improve startup time for
1354 typical inter-CU-reference binaries, at an obvious memory cost. */
1355 static int dwarf2_max_cache_age = 5;
1356 static void
1357 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1358 struct cmd_list_element *c, const char *value)
1359 {
1360 fprintf_filtered (file, _("The upper bound on the age of cached "
1361 "dwarf2 compilation units is %s.\n"),
1362 value);
1363 }
1364 \f
1365 /* local function prototypes */
1366
1367 static const char *get_section_name (const struct dwarf2_section_info *);
1368
1369 static const char *get_section_file_name (const struct dwarf2_section_info *);
1370
1371 static void dwarf2_locate_sections (bfd *, asection *, void *);
1372
1373 static void dwarf2_find_base_address (struct die_info *die,
1374 struct dwarf2_cu *cu);
1375
1376 static struct partial_symtab *create_partial_symtab
1377 (struct dwarf2_per_cu_data *per_cu, const char *name);
1378
1379 static void dwarf2_build_psymtabs_hard (struct objfile *);
1380
1381 static void scan_partial_symbols (struct partial_die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
1383 int, struct dwarf2_cu *);
1384
1385 static void add_partial_symbol (struct partial_die_info *,
1386 struct dwarf2_cu *);
1387
1388 static void add_partial_namespace (struct partial_die_info *pdi,
1389 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1390 int need_pc, struct dwarf2_cu *cu);
1391
1392 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1393 CORE_ADDR *highpc, int need_pc,
1394 struct dwarf2_cu *cu);
1395
1396 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1397 struct dwarf2_cu *cu);
1398
1399 static void add_partial_subprogram (struct partial_die_info *pdi,
1400 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1401 int need_pc, struct dwarf2_cu *cu);
1402
1403 static void dwarf2_read_symtab (struct partial_symtab *,
1404 struct objfile *);
1405
1406 static void psymtab_to_symtab_1 (struct partial_symtab *);
1407
1408 static struct abbrev_info *abbrev_table_lookup_abbrev
1409 (const struct abbrev_table *, unsigned int);
1410
1411 static struct abbrev_table *abbrev_table_read_table
1412 (struct dwarf2_section_info *, sect_offset);
1413
1414 static void abbrev_table_free (struct abbrev_table *);
1415
1416 static void abbrev_table_free_cleanup (void *);
1417
1418 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1419 struct dwarf2_section_info *);
1420
1421 static void dwarf2_free_abbrev_table (void *);
1422
1423 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1424
1425 static struct partial_die_info *load_partial_dies
1426 (const struct die_reader_specs *, const gdb_byte *, int);
1427
1428 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1429 struct partial_die_info *,
1430 struct abbrev_info *,
1431 unsigned int,
1432 const gdb_byte *);
1433
1434 static struct partial_die_info *find_partial_die (sect_offset, int,
1435 struct dwarf2_cu *);
1436
1437 static void fixup_partial_die (struct partial_die_info *,
1438 struct dwarf2_cu *);
1439
1440 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1441 struct attribute *, struct attr_abbrev *,
1442 const gdb_byte *);
1443
1444 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1445
1446 static int read_1_signed_byte (bfd *, const gdb_byte *);
1447
1448 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1449
1450 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1451
1452 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1453
1454 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1455 unsigned int *);
1456
1457 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1458
1459 static LONGEST read_checked_initial_length_and_offset
1460 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1461 unsigned int *, unsigned int *);
1462
1463 static LONGEST read_offset (bfd *, const gdb_byte *,
1464 const struct comp_unit_head *,
1465 unsigned int *);
1466
1467 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1468
1469 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1470 sect_offset);
1471
1472 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1473
1474 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1475
1476 static const char *read_indirect_string (bfd *, const gdb_byte *,
1477 const struct comp_unit_head *,
1478 unsigned int *);
1479
1480 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1481
1482 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1483
1484 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1485
1486 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1487 const gdb_byte *,
1488 unsigned int *);
1489
1490 static const char *read_str_index (const struct die_reader_specs *reader,
1491 struct dwarf2_cu *cu, ULONGEST str_index);
1492
1493 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1494
1495 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1496 struct dwarf2_cu *);
1497
1498 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1499 unsigned int);
1500
1501 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1502 struct dwarf2_cu *cu);
1503
1504 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1505
1506 static struct die_info *die_specification (struct die_info *die,
1507 struct dwarf2_cu **);
1508
1509 static void free_line_header (struct line_header *lh);
1510
1511 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1512 struct dwarf2_cu *cu);
1513
1514 static void dwarf_decode_lines (struct line_header *, const char *,
1515 struct dwarf2_cu *, struct partial_symtab *,
1516 int);
1517
1518 static void dwarf2_start_subfile (const char *, const char *, const char *);
1519
1520 static void dwarf2_start_symtab (struct dwarf2_cu *,
1521 const char *, const char *, CORE_ADDR);
1522
1523 static struct symbol *new_symbol (struct die_info *, struct type *,
1524 struct dwarf2_cu *);
1525
1526 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1527 struct dwarf2_cu *, struct symbol *);
1528
1529 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1530 struct dwarf2_cu *);
1531
1532 static void dwarf2_const_value_attr (const struct attribute *attr,
1533 struct type *type,
1534 const char *name,
1535 struct obstack *obstack,
1536 struct dwarf2_cu *cu, LONGEST *value,
1537 const gdb_byte **bytes,
1538 struct dwarf2_locexpr_baton **baton);
1539
1540 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1541
1542 static int need_gnat_info (struct dwarf2_cu *);
1543
1544 static struct type *die_descriptive_type (struct die_info *,
1545 struct dwarf2_cu *);
1546
1547 static void set_descriptive_type (struct type *, struct die_info *,
1548 struct dwarf2_cu *);
1549
1550 static struct type *die_containing_type (struct die_info *,
1551 struct dwarf2_cu *);
1552
1553 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1554 struct dwarf2_cu *);
1555
1556 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1557
1558 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1559
1560 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1561
1562 static char *typename_concat (struct obstack *obs, const char *prefix,
1563 const char *suffix, int physname,
1564 struct dwarf2_cu *cu);
1565
1566 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1567
1568 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1569
1570 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1571
1572 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1573
1574 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1575
1576 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1577 struct dwarf2_cu *, struct partial_symtab *);
1578
1579 static int dwarf2_get_pc_bounds (struct die_info *,
1580 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1581 struct partial_symtab *);
1582
1583 static void get_scope_pc_bounds (struct die_info *,
1584 CORE_ADDR *, CORE_ADDR *,
1585 struct dwarf2_cu *);
1586
1587 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1588 CORE_ADDR, struct dwarf2_cu *);
1589
1590 static void dwarf2_add_field (struct field_info *, struct die_info *,
1591 struct dwarf2_cu *);
1592
1593 static void dwarf2_attach_fields_to_type (struct field_info *,
1594 struct type *, struct dwarf2_cu *);
1595
1596 static void dwarf2_add_member_fn (struct field_info *,
1597 struct die_info *, struct type *,
1598 struct dwarf2_cu *);
1599
1600 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1601 struct type *,
1602 struct dwarf2_cu *);
1603
1604 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1605
1606 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1607
1608 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1609
1610 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1611
1612 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1613
1614 static struct type *read_module_type (struct die_info *die,
1615 struct dwarf2_cu *cu);
1616
1617 static const char *namespace_name (struct die_info *die,
1618 int *is_anonymous, struct dwarf2_cu *);
1619
1620 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1621
1622 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1623
1624 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1625 struct dwarf2_cu *);
1626
1627 static struct die_info *read_die_and_siblings_1
1628 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1629 struct die_info *);
1630
1631 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1632 const gdb_byte *info_ptr,
1633 const gdb_byte **new_info_ptr,
1634 struct die_info *parent);
1635
1636 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1637 struct die_info **, const gdb_byte *,
1638 int *, int);
1639
1640 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1641 struct die_info **, const gdb_byte *,
1642 int *);
1643
1644 static void process_die (struct die_info *, struct dwarf2_cu *);
1645
1646 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1647 struct obstack *);
1648
1649 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1650
1651 static const char *dwarf2_full_name (const char *name,
1652 struct die_info *die,
1653 struct dwarf2_cu *cu);
1654
1655 static const char *dwarf2_physname (const char *name, struct die_info *die,
1656 struct dwarf2_cu *cu);
1657
1658 static struct die_info *dwarf2_extension (struct die_info *die,
1659 struct dwarf2_cu **);
1660
1661 static const char *dwarf_tag_name (unsigned int);
1662
1663 static const char *dwarf_attr_name (unsigned int);
1664
1665 static const char *dwarf_form_name (unsigned int);
1666
1667 static char *dwarf_bool_name (unsigned int);
1668
1669 static const char *dwarf_type_encoding_name (unsigned int);
1670
1671 static struct die_info *sibling_die (struct die_info *);
1672
1673 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1674
1675 static void dump_die_for_error (struct die_info *);
1676
1677 static void dump_die_1 (struct ui_file *, int level, int max_level,
1678 struct die_info *);
1679
1680 /*static*/ void dump_die (struct die_info *, int max_level);
1681
1682 static void store_in_ref_table (struct die_info *,
1683 struct dwarf2_cu *);
1684
1685 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1686
1687 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1688
1689 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1690 const struct attribute *,
1691 struct dwarf2_cu **);
1692
1693 static struct die_info *follow_die_ref (struct die_info *,
1694 const struct attribute *,
1695 struct dwarf2_cu **);
1696
1697 static struct die_info *follow_die_sig (struct die_info *,
1698 const struct attribute *,
1699 struct dwarf2_cu **);
1700
1701 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1702 struct dwarf2_cu *);
1703
1704 static struct type *get_DW_AT_signature_type (struct die_info *,
1705 const struct attribute *,
1706 struct dwarf2_cu *);
1707
1708 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1709
1710 static void read_signatured_type (struct signatured_type *);
1711
1712 static struct type_unit_group *get_type_unit_group
1713 (struct dwarf2_cu *, const struct attribute *);
1714
1715 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1716
1717 /* memory allocation interface */
1718
1719 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1720
1721 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1722
1723 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1724 const char *, int);
1725
1726 static int attr_form_is_block (const struct attribute *);
1727
1728 static int attr_form_is_section_offset (const struct attribute *);
1729
1730 static int attr_form_is_constant (const struct attribute *);
1731
1732 static int attr_form_is_ref (const struct attribute *);
1733
1734 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1735 struct dwarf2_loclist_baton *baton,
1736 const struct attribute *attr);
1737
1738 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1739 struct symbol *sym,
1740 struct dwarf2_cu *cu,
1741 int is_block);
1742
1743 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1744 const gdb_byte *info_ptr,
1745 struct abbrev_info *abbrev);
1746
1747 static void free_stack_comp_unit (void *);
1748
1749 static hashval_t partial_die_hash (const void *item);
1750
1751 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1752
1753 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1754 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1755
1756 static void init_one_comp_unit (struct dwarf2_cu *cu,
1757 struct dwarf2_per_cu_data *per_cu);
1758
1759 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1760 struct die_info *comp_unit_die,
1761 enum language pretend_language);
1762
1763 static void free_heap_comp_unit (void *);
1764
1765 static void free_cached_comp_units (void *);
1766
1767 static void age_cached_comp_units (void);
1768
1769 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1770
1771 static struct type *set_die_type (struct die_info *, struct type *,
1772 struct dwarf2_cu *);
1773
1774 static void create_all_comp_units (struct objfile *);
1775
1776 static int create_all_type_units (struct objfile *);
1777
1778 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1779 enum language);
1780
1781 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1782 enum language);
1783
1784 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1785 enum language);
1786
1787 static void dwarf2_add_dependence (struct dwarf2_cu *,
1788 struct dwarf2_per_cu_data *);
1789
1790 static void dwarf2_mark (struct dwarf2_cu *);
1791
1792 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1793
1794 static struct type *get_die_type_at_offset (sect_offset,
1795 struct dwarf2_per_cu_data *);
1796
1797 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1798
1799 static void dwarf2_release_queue (void *dummy);
1800
1801 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1802 enum language pretend_language);
1803
1804 static void process_queue (void);
1805
1806 static void find_file_and_directory (struct die_info *die,
1807 struct dwarf2_cu *cu,
1808 const char **name, const char **comp_dir);
1809
1810 static char *file_full_name (int file, struct line_header *lh,
1811 const char *comp_dir);
1812
1813 static const gdb_byte *read_and_check_comp_unit_head
1814 (struct comp_unit_head *header,
1815 struct dwarf2_section_info *section,
1816 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1817 int is_debug_types_section);
1818
1819 static void init_cutu_and_read_dies
1820 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1821 int use_existing_cu, int keep,
1822 die_reader_func_ftype *die_reader_func, void *data);
1823
1824 static void init_cutu_and_read_dies_simple
1825 (struct dwarf2_per_cu_data *this_cu,
1826 die_reader_func_ftype *die_reader_func, void *data);
1827
1828 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1829
1830 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1831
1832 static struct dwo_unit *lookup_dwo_unit_in_dwp
1833 (struct dwp_file *dwp_file, const char *comp_dir,
1834 ULONGEST signature, int is_debug_types);
1835
1836 static struct dwp_file *get_dwp_file (void);
1837
1838 static struct dwo_unit *lookup_dwo_comp_unit
1839 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1840
1841 static struct dwo_unit *lookup_dwo_type_unit
1842 (struct signatured_type *, const char *, const char *);
1843
1844 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1845
1846 static void free_dwo_file_cleanup (void *);
1847
1848 static void process_cu_includes (void);
1849
1850 static void check_producer (struct dwarf2_cu *cu);
1851 \f
1852 /* Various complaints about symbol reading that don't abort the process. */
1853
1854 static void
1855 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1856 {
1857 complaint (&symfile_complaints,
1858 _("statement list doesn't fit in .debug_line section"));
1859 }
1860
1861 static void
1862 dwarf2_debug_line_missing_file_complaint (void)
1863 {
1864 complaint (&symfile_complaints,
1865 _(".debug_line section has line data without a file"));
1866 }
1867
1868 static void
1869 dwarf2_debug_line_missing_end_sequence_complaint (void)
1870 {
1871 complaint (&symfile_complaints,
1872 _(".debug_line section has line "
1873 "program sequence without an end"));
1874 }
1875
1876 static void
1877 dwarf2_complex_location_expr_complaint (void)
1878 {
1879 complaint (&symfile_complaints, _("location expression too complex"));
1880 }
1881
1882 static void
1883 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1884 int arg3)
1885 {
1886 complaint (&symfile_complaints,
1887 _("const value length mismatch for '%s', got %d, expected %d"),
1888 arg1, arg2, arg3);
1889 }
1890
1891 static void
1892 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1893 {
1894 complaint (&symfile_complaints,
1895 _("debug info runs off end of %s section"
1896 " [in module %s]"),
1897 get_section_name (section),
1898 get_section_file_name (section));
1899 }
1900
1901 static void
1902 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1903 {
1904 complaint (&symfile_complaints,
1905 _("macro debug info contains a "
1906 "malformed macro definition:\n`%s'"),
1907 arg1);
1908 }
1909
1910 static void
1911 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1912 {
1913 complaint (&symfile_complaints,
1914 _("invalid attribute class or form for '%s' in '%s'"),
1915 arg1, arg2);
1916 }
1917 \f
1918 #if WORDS_BIGENDIAN
1919
1920 /* Convert VALUE between big- and little-endian. */
1921 static offset_type
1922 byte_swap (offset_type value)
1923 {
1924 offset_type result;
1925
1926 result = (value & 0xff) << 24;
1927 result |= (value & 0xff00) << 8;
1928 result |= (value & 0xff0000) >> 8;
1929 result |= (value & 0xff000000) >> 24;
1930 return result;
1931 }
1932
1933 #define MAYBE_SWAP(V) byte_swap (V)
1934
1935 #else
1936 #define MAYBE_SWAP(V) (V)
1937 #endif /* WORDS_BIGENDIAN */
1938
1939 /* The suffix for an index file. */
1940 #define INDEX_SUFFIX ".gdb-index"
1941
1942 /* Try to locate the sections we need for DWARF 2 debugging
1943 information and return true if we have enough to do something.
1944 NAMES points to the dwarf2 section names, or is NULL if the standard
1945 ELF names are used. */
1946
1947 int
1948 dwarf2_has_info (struct objfile *objfile,
1949 const struct dwarf2_debug_sections *names)
1950 {
1951 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1952 if (!dwarf2_per_objfile)
1953 {
1954 /* Initialize per-objfile state. */
1955 struct dwarf2_per_objfile *data
1956 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1957
1958 memset (data, 0, sizeof (*data));
1959 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1960 dwarf2_per_objfile = data;
1961
1962 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1963 (void *) names);
1964 dwarf2_per_objfile->objfile = objfile;
1965 }
1966 return (!dwarf2_per_objfile->info.is_virtual
1967 && dwarf2_per_objfile->info.s.asection != NULL
1968 && !dwarf2_per_objfile->abbrev.is_virtual
1969 && dwarf2_per_objfile->abbrev.s.asection != NULL);
1970 }
1971
1972 /* Return the containing section of virtual section SECTION. */
1973
1974 static struct dwarf2_section_info *
1975 get_containing_section (const struct dwarf2_section_info *section)
1976 {
1977 gdb_assert (section->is_virtual);
1978 return section->s.containing_section;
1979 }
1980
1981 /* Return the bfd owner of SECTION. */
1982
1983 static struct bfd *
1984 get_section_bfd_owner (const struct dwarf2_section_info *section)
1985 {
1986 if (section->is_virtual)
1987 {
1988 section = get_containing_section (section);
1989 gdb_assert (!section->is_virtual);
1990 }
1991 return section->s.asection->owner;
1992 }
1993
1994 /* Return the bfd section of SECTION.
1995 Returns NULL if the section is not present. */
1996
1997 static asection *
1998 get_section_bfd_section (const struct dwarf2_section_info *section)
1999 {
2000 if (section->is_virtual)
2001 {
2002 section = get_containing_section (section);
2003 gdb_assert (!section->is_virtual);
2004 }
2005 return section->s.asection;
2006 }
2007
2008 /* Return the name of SECTION. */
2009
2010 static const char *
2011 get_section_name (const struct dwarf2_section_info *section)
2012 {
2013 asection *sectp = get_section_bfd_section (section);
2014
2015 gdb_assert (sectp != NULL);
2016 return bfd_section_name (get_section_bfd_owner (section), sectp);
2017 }
2018
2019 /* Return the name of the file SECTION is in. */
2020
2021 static const char *
2022 get_section_file_name (const struct dwarf2_section_info *section)
2023 {
2024 bfd *abfd = get_section_bfd_owner (section);
2025
2026 return bfd_get_filename (abfd);
2027 }
2028
2029 /* Return the id of SECTION.
2030 Returns 0 if SECTION doesn't exist. */
2031
2032 static int
2033 get_section_id (const struct dwarf2_section_info *section)
2034 {
2035 asection *sectp = get_section_bfd_section (section);
2036
2037 if (sectp == NULL)
2038 return 0;
2039 return sectp->id;
2040 }
2041
2042 /* Return the flags of SECTION.
2043 SECTION (or containing section if this is a virtual section) must exist. */
2044
2045 static int
2046 get_section_flags (const struct dwarf2_section_info *section)
2047 {
2048 asection *sectp = get_section_bfd_section (section);
2049
2050 gdb_assert (sectp != NULL);
2051 return bfd_get_section_flags (sectp->owner, sectp);
2052 }
2053
2054 /* When loading sections, we look either for uncompressed section or for
2055 compressed section names. */
2056
2057 static int
2058 section_is_p (const char *section_name,
2059 const struct dwarf2_section_names *names)
2060 {
2061 if (names->normal != NULL
2062 && strcmp (section_name, names->normal) == 0)
2063 return 1;
2064 if (names->compressed != NULL
2065 && strcmp (section_name, names->compressed) == 0)
2066 return 1;
2067 return 0;
2068 }
2069
2070 /* This function is mapped across the sections and remembers the
2071 offset and size of each of the debugging sections we are interested
2072 in. */
2073
2074 static void
2075 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2076 {
2077 const struct dwarf2_debug_sections *names;
2078 flagword aflag = bfd_get_section_flags (abfd, sectp);
2079
2080 if (vnames == NULL)
2081 names = &dwarf2_elf_names;
2082 else
2083 names = (const struct dwarf2_debug_sections *) vnames;
2084
2085 if ((aflag & SEC_HAS_CONTENTS) == 0)
2086 {
2087 }
2088 else if (section_is_p (sectp->name, &names->info))
2089 {
2090 dwarf2_per_objfile->info.s.asection = sectp;
2091 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &names->abbrev))
2094 {
2095 dwarf2_per_objfile->abbrev.s.asection = sectp;
2096 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &names->line))
2099 {
2100 dwarf2_per_objfile->line.s.asection = sectp;
2101 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2102 }
2103 else if (section_is_p (sectp->name, &names->loc))
2104 {
2105 dwarf2_per_objfile->loc.s.asection = sectp;
2106 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2107 }
2108 else if (section_is_p (sectp->name, &names->macinfo))
2109 {
2110 dwarf2_per_objfile->macinfo.s.asection = sectp;
2111 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2112 }
2113 else if (section_is_p (sectp->name, &names->macro))
2114 {
2115 dwarf2_per_objfile->macro.s.asection = sectp;
2116 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2117 }
2118 else if (section_is_p (sectp->name, &names->str))
2119 {
2120 dwarf2_per_objfile->str.s.asection = sectp;
2121 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2122 }
2123 else if (section_is_p (sectp->name, &names->addr))
2124 {
2125 dwarf2_per_objfile->addr.s.asection = sectp;
2126 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2127 }
2128 else if (section_is_p (sectp->name, &names->frame))
2129 {
2130 dwarf2_per_objfile->frame.s.asection = sectp;
2131 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2132 }
2133 else if (section_is_p (sectp->name, &names->eh_frame))
2134 {
2135 dwarf2_per_objfile->eh_frame.s.asection = sectp;
2136 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2137 }
2138 else if (section_is_p (sectp->name, &names->ranges))
2139 {
2140 dwarf2_per_objfile->ranges.s.asection = sectp;
2141 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2142 }
2143 else if (section_is_p (sectp->name, &names->types))
2144 {
2145 struct dwarf2_section_info type_section;
2146
2147 memset (&type_section, 0, sizeof (type_section));
2148 type_section.s.asection = sectp;
2149 type_section.size = bfd_get_section_size (sectp);
2150
2151 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2152 &type_section);
2153 }
2154 else if (section_is_p (sectp->name, &names->gdb_index))
2155 {
2156 dwarf2_per_objfile->gdb_index.s.asection = sectp;
2157 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2158 }
2159
2160 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
2161 && bfd_section_vma (abfd, sectp) == 0)
2162 dwarf2_per_objfile->has_section_at_zero = 1;
2163 }
2164
2165 /* A helper function that decides whether a section is empty,
2166 or not present. */
2167
2168 static int
2169 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2170 {
2171 if (section->is_virtual)
2172 return section->size == 0;
2173 return section->s.asection == NULL || section->size == 0;
2174 }
2175
2176 /* Read the contents of the section INFO.
2177 OBJFILE is the main object file, but not necessarily the file where
2178 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2179 of the DWO file.
2180 If the section is compressed, uncompress it before returning. */
2181
2182 static void
2183 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2184 {
2185 asection *sectp;
2186 bfd *abfd;
2187 gdb_byte *buf, *retbuf;
2188
2189 if (info->readin)
2190 return;
2191 info->buffer = NULL;
2192 info->readin = 1;
2193
2194 if (dwarf2_section_empty_p (info))
2195 return;
2196
2197 sectp = get_section_bfd_section (info);
2198
2199 /* If this is a virtual section we need to read in the real one first. */
2200 if (info->is_virtual)
2201 {
2202 struct dwarf2_section_info *containing_section =
2203 get_containing_section (info);
2204
2205 gdb_assert (sectp != NULL);
2206 if ((sectp->flags & SEC_RELOC) != 0)
2207 {
2208 error (_("Dwarf Error: DWP format V2 with relocations is not"
2209 " supported in section %s [in module %s]"),
2210 get_section_name (info), get_section_file_name (info));
2211 }
2212 dwarf2_read_section (objfile, containing_section);
2213 /* Other code should have already caught virtual sections that don't
2214 fit. */
2215 gdb_assert (info->virtual_offset + info->size
2216 <= containing_section->size);
2217 /* If the real section is empty or there was a problem reading the
2218 section we shouldn't get here. */
2219 gdb_assert (containing_section->buffer != NULL);
2220 info->buffer = containing_section->buffer + info->virtual_offset;
2221 return;
2222 }
2223
2224 /* If the section has relocations, we must read it ourselves.
2225 Otherwise we attach it to the BFD. */
2226 if ((sectp->flags & SEC_RELOC) == 0)
2227 {
2228 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2229 return;
2230 }
2231
2232 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2233 info->buffer = buf;
2234
2235 /* When debugging .o files, we may need to apply relocations; see
2236 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2237 We never compress sections in .o files, so we only need to
2238 try this when the section is not compressed. */
2239 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2240 if (retbuf != NULL)
2241 {
2242 info->buffer = retbuf;
2243 return;
2244 }
2245
2246 abfd = get_section_bfd_owner (info);
2247 gdb_assert (abfd != NULL);
2248
2249 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2250 || bfd_bread (buf, info->size, abfd) != info->size)
2251 {
2252 error (_("Dwarf Error: Can't read DWARF data"
2253 " in section %s [in module %s]"),
2254 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2255 }
2256 }
2257
2258 /* A helper function that returns the size of a section in a safe way.
2259 If you are positive that the section has been read before using the
2260 size, then it is safe to refer to the dwarf2_section_info object's
2261 "size" field directly. In other cases, you must call this
2262 function, because for compressed sections the size field is not set
2263 correctly until the section has been read. */
2264
2265 static bfd_size_type
2266 dwarf2_section_size (struct objfile *objfile,
2267 struct dwarf2_section_info *info)
2268 {
2269 if (!info->readin)
2270 dwarf2_read_section (objfile, info);
2271 return info->size;
2272 }
2273
2274 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2275 SECTION_NAME. */
2276
2277 void
2278 dwarf2_get_section_info (struct objfile *objfile,
2279 enum dwarf2_section_enum sect,
2280 asection **sectp, const gdb_byte **bufp,
2281 bfd_size_type *sizep)
2282 {
2283 struct dwarf2_per_objfile *data
2284 = objfile_data (objfile, dwarf2_objfile_data_key);
2285 struct dwarf2_section_info *info;
2286
2287 /* We may see an objfile without any DWARF, in which case we just
2288 return nothing. */
2289 if (data == NULL)
2290 {
2291 *sectp = NULL;
2292 *bufp = NULL;
2293 *sizep = 0;
2294 return;
2295 }
2296 switch (sect)
2297 {
2298 case DWARF2_DEBUG_FRAME:
2299 info = &data->frame;
2300 break;
2301 case DWARF2_EH_FRAME:
2302 info = &data->eh_frame;
2303 break;
2304 default:
2305 gdb_assert_not_reached ("unexpected section");
2306 }
2307
2308 dwarf2_read_section (objfile, info);
2309
2310 *sectp = get_section_bfd_section (info);
2311 *bufp = info->buffer;
2312 *sizep = info->size;
2313 }
2314
2315 /* A helper function to find the sections for a .dwz file. */
2316
2317 static void
2318 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2319 {
2320 struct dwz_file *dwz_file = arg;
2321
2322 /* Note that we only support the standard ELF names, because .dwz
2323 is ELF-only (at the time of writing). */
2324 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2325 {
2326 dwz_file->abbrev.s.asection = sectp;
2327 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2328 }
2329 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2330 {
2331 dwz_file->info.s.asection = sectp;
2332 dwz_file->info.size = bfd_get_section_size (sectp);
2333 }
2334 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2335 {
2336 dwz_file->str.s.asection = sectp;
2337 dwz_file->str.size = bfd_get_section_size (sectp);
2338 }
2339 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2340 {
2341 dwz_file->line.s.asection = sectp;
2342 dwz_file->line.size = bfd_get_section_size (sectp);
2343 }
2344 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2345 {
2346 dwz_file->macro.s.asection = sectp;
2347 dwz_file->macro.size = bfd_get_section_size (sectp);
2348 }
2349 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2350 {
2351 dwz_file->gdb_index.s.asection = sectp;
2352 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2353 }
2354 }
2355
2356 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2357 there is no .gnu_debugaltlink section in the file. Error if there
2358 is such a section but the file cannot be found. */
2359
2360 static struct dwz_file *
2361 dwarf2_get_dwz_file (void)
2362 {
2363 bfd *dwz_bfd;
2364 char *data;
2365 struct cleanup *cleanup;
2366 const char *filename;
2367 struct dwz_file *result;
2368 bfd_size_type buildid_len_arg;
2369 size_t buildid_len;
2370 bfd_byte *buildid;
2371
2372 if (dwarf2_per_objfile->dwz_file != NULL)
2373 return dwarf2_per_objfile->dwz_file;
2374
2375 bfd_set_error (bfd_error_no_error);
2376 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2377 &buildid_len_arg, &buildid);
2378 if (data == NULL)
2379 {
2380 if (bfd_get_error () == bfd_error_no_error)
2381 return NULL;
2382 error (_("could not read '.gnu_debugaltlink' section: %s"),
2383 bfd_errmsg (bfd_get_error ()));
2384 }
2385 cleanup = make_cleanup (xfree, data);
2386 make_cleanup (xfree, buildid);
2387
2388 buildid_len = (size_t) buildid_len_arg;
2389
2390 filename = (const char *) data;
2391 if (!IS_ABSOLUTE_PATH (filename))
2392 {
2393 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2394 char *rel;
2395
2396 make_cleanup (xfree, abs);
2397 abs = ldirname (abs);
2398 make_cleanup (xfree, abs);
2399
2400 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2401 make_cleanup (xfree, rel);
2402 filename = rel;
2403 }
2404
2405 /* First try the file name given in the section. If that doesn't
2406 work, try to use the build-id instead. */
2407 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2408 if (dwz_bfd != NULL)
2409 {
2410 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2411 {
2412 gdb_bfd_unref (dwz_bfd);
2413 dwz_bfd = NULL;
2414 }
2415 }
2416
2417 if (dwz_bfd == NULL)
2418 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2419
2420 if (dwz_bfd == NULL)
2421 error (_("could not find '.gnu_debugaltlink' file for %s"),
2422 objfile_name (dwarf2_per_objfile->objfile));
2423
2424 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2425 struct dwz_file);
2426 result->dwz_bfd = dwz_bfd;
2427
2428 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2429
2430 do_cleanups (cleanup);
2431
2432 dwarf2_per_objfile->dwz_file = result;
2433 return result;
2434 }
2435 \f
2436 /* DWARF quick_symbols_functions support. */
2437
2438 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2439 unique line tables, so we maintain a separate table of all .debug_line
2440 derived entries to support the sharing.
2441 All the quick functions need is the list of file names. We discard the
2442 line_header when we're done and don't need to record it here. */
2443 struct quick_file_names
2444 {
2445 /* The data used to construct the hash key. */
2446 struct stmt_list_hash hash;
2447
2448 /* The number of entries in file_names, real_names. */
2449 unsigned int num_file_names;
2450
2451 /* The file names from the line table, after being run through
2452 file_full_name. */
2453 const char **file_names;
2454
2455 /* The file names from the line table after being run through
2456 gdb_realpath. These are computed lazily. */
2457 const char **real_names;
2458 };
2459
2460 /* When using the index (and thus not using psymtabs), each CU has an
2461 object of this type. This is used to hold information needed by
2462 the various "quick" methods. */
2463 struct dwarf2_per_cu_quick_data
2464 {
2465 /* The file table. This can be NULL if there was no file table
2466 or it's currently not read in.
2467 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2468 struct quick_file_names *file_names;
2469
2470 /* The corresponding symbol table. This is NULL if symbols for this
2471 CU have not yet been read. */
2472 struct symtab *symtab;
2473
2474 /* A temporary mark bit used when iterating over all CUs in
2475 expand_symtabs_matching. */
2476 unsigned int mark : 1;
2477
2478 /* True if we've tried to read the file table and found there isn't one.
2479 There will be no point in trying to read it again next time. */
2480 unsigned int no_file_data : 1;
2481 };
2482
2483 /* Utility hash function for a stmt_list_hash. */
2484
2485 static hashval_t
2486 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2487 {
2488 hashval_t v = 0;
2489
2490 if (stmt_list_hash->dwo_unit != NULL)
2491 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2492 v += stmt_list_hash->line_offset.sect_off;
2493 return v;
2494 }
2495
2496 /* Utility equality function for a stmt_list_hash. */
2497
2498 static int
2499 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2500 const struct stmt_list_hash *rhs)
2501 {
2502 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2503 return 0;
2504 if (lhs->dwo_unit != NULL
2505 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2506 return 0;
2507
2508 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2509 }
2510
2511 /* Hash function for a quick_file_names. */
2512
2513 static hashval_t
2514 hash_file_name_entry (const void *e)
2515 {
2516 const struct quick_file_names *file_data = e;
2517
2518 return hash_stmt_list_entry (&file_data->hash);
2519 }
2520
2521 /* Equality function for a quick_file_names. */
2522
2523 static int
2524 eq_file_name_entry (const void *a, const void *b)
2525 {
2526 const struct quick_file_names *ea = a;
2527 const struct quick_file_names *eb = b;
2528
2529 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2530 }
2531
2532 /* Delete function for a quick_file_names. */
2533
2534 static void
2535 delete_file_name_entry (void *e)
2536 {
2537 struct quick_file_names *file_data = e;
2538 int i;
2539
2540 for (i = 0; i < file_data->num_file_names; ++i)
2541 {
2542 xfree ((void*) file_data->file_names[i]);
2543 if (file_data->real_names)
2544 xfree ((void*) file_data->real_names[i]);
2545 }
2546
2547 /* The space for the struct itself lives on objfile_obstack,
2548 so we don't free it here. */
2549 }
2550
2551 /* Create a quick_file_names hash table. */
2552
2553 static htab_t
2554 create_quick_file_names_table (unsigned int nr_initial_entries)
2555 {
2556 return htab_create_alloc (nr_initial_entries,
2557 hash_file_name_entry, eq_file_name_entry,
2558 delete_file_name_entry, xcalloc, xfree);
2559 }
2560
2561 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2562 have to be created afterwards. You should call age_cached_comp_units after
2563 processing PER_CU->CU. dw2_setup must have been already called. */
2564
2565 static void
2566 load_cu (struct dwarf2_per_cu_data *per_cu)
2567 {
2568 if (per_cu->is_debug_types)
2569 load_full_type_unit (per_cu);
2570 else
2571 load_full_comp_unit (per_cu, language_minimal);
2572
2573 gdb_assert (per_cu->cu != NULL);
2574
2575 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2576 }
2577
2578 /* Read in the symbols for PER_CU. */
2579
2580 static void
2581 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2582 {
2583 struct cleanup *back_to;
2584
2585 /* Skip type_unit_groups, reading the type units they contain
2586 is handled elsewhere. */
2587 if (IS_TYPE_UNIT_GROUP (per_cu))
2588 return;
2589
2590 back_to = make_cleanup (dwarf2_release_queue, NULL);
2591
2592 if (dwarf2_per_objfile->using_index
2593 ? per_cu->v.quick->symtab == NULL
2594 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2595 {
2596 queue_comp_unit (per_cu, language_minimal);
2597 load_cu (per_cu);
2598
2599 /* If we just loaded a CU from a DWO, and we're working with an index
2600 that may badly handle TUs, load all the TUs in that DWO as well.
2601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2602 if (!per_cu->is_debug_types
2603 && per_cu->cu->dwo_unit != NULL
2604 && dwarf2_per_objfile->index_table != NULL
2605 && dwarf2_per_objfile->index_table->version <= 7
2606 /* DWP files aren't supported yet. */
2607 && get_dwp_file () == NULL)
2608 queue_and_load_all_dwo_tus (per_cu);
2609 }
2610
2611 process_queue ();
2612
2613 /* Age the cache, releasing compilation units that have not
2614 been used recently. */
2615 age_cached_comp_units ();
2616
2617 do_cleanups (back_to);
2618 }
2619
2620 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2621 the objfile from which this CU came. Returns the resulting symbol
2622 table. */
2623
2624 static struct symtab *
2625 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2626 {
2627 gdb_assert (dwarf2_per_objfile->using_index);
2628 if (!per_cu->v.quick->symtab)
2629 {
2630 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2631 increment_reading_symtab ();
2632 dw2_do_instantiate_symtab (per_cu);
2633 process_cu_includes ();
2634 do_cleanups (back_to);
2635 }
2636 return per_cu->v.quick->symtab;
2637 }
2638
2639 /* Return the CU given its index.
2640
2641 This is intended for loops like:
2642
2643 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2644 + dwarf2_per_objfile->n_type_units); ++i)
2645 {
2646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647
2648 ...;
2649 }
2650 */
2651
2652 static struct dwarf2_per_cu_data *
2653 dw2_get_cu (int index)
2654 {
2655 if (index >= dwarf2_per_objfile->n_comp_units)
2656 {
2657 index -= dwarf2_per_objfile->n_comp_units;
2658 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2659 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2660 }
2661
2662 return dwarf2_per_objfile->all_comp_units[index];
2663 }
2664
2665 /* Return the primary CU given its index.
2666 The difference between this function and dw2_get_cu is in the handling
2667 of type units (TUs). Here we return the type_unit_group object.
2668
2669 This is intended for loops like:
2670
2671 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2672 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2673 {
2674 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2675
2676 ...;
2677 }
2678 */
2679
2680 static struct dwarf2_per_cu_data *
2681 dw2_get_primary_cu (int index)
2682 {
2683 if (index >= dwarf2_per_objfile->n_comp_units)
2684 {
2685 index -= dwarf2_per_objfile->n_comp_units;
2686 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2687 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2688 }
2689
2690 return dwarf2_per_objfile->all_comp_units[index];
2691 }
2692
2693 /* A helper for create_cus_from_index that handles a given list of
2694 CUs. */
2695
2696 static void
2697 create_cus_from_index_list (struct objfile *objfile,
2698 const gdb_byte *cu_list, offset_type n_elements,
2699 struct dwarf2_section_info *section,
2700 int is_dwz,
2701 int base_offset)
2702 {
2703 offset_type i;
2704
2705 for (i = 0; i < n_elements; i += 2)
2706 {
2707 struct dwarf2_per_cu_data *the_cu;
2708 ULONGEST offset, length;
2709
2710 gdb_static_assert (sizeof (ULONGEST) >= 8);
2711 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2712 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2713 cu_list += 2 * 8;
2714
2715 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2716 struct dwarf2_per_cu_data);
2717 the_cu->offset.sect_off = offset;
2718 the_cu->length = length;
2719 the_cu->objfile = objfile;
2720 the_cu->section = section;
2721 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2722 struct dwarf2_per_cu_quick_data);
2723 the_cu->is_dwz = is_dwz;
2724 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2725 }
2726 }
2727
2728 /* Read the CU list from the mapped index, and use it to create all
2729 the CU objects for this objfile. */
2730
2731 static void
2732 create_cus_from_index (struct objfile *objfile,
2733 const gdb_byte *cu_list, offset_type cu_list_elements,
2734 const gdb_byte *dwz_list, offset_type dwz_elements)
2735 {
2736 struct dwz_file *dwz;
2737
2738 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2739 dwarf2_per_objfile->all_comp_units
2740 = obstack_alloc (&objfile->objfile_obstack,
2741 dwarf2_per_objfile->n_comp_units
2742 * sizeof (struct dwarf2_per_cu_data *));
2743
2744 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2745 &dwarf2_per_objfile->info, 0, 0);
2746
2747 if (dwz_elements == 0)
2748 return;
2749
2750 dwz = dwarf2_get_dwz_file ();
2751 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2752 cu_list_elements / 2);
2753 }
2754
2755 /* Create the signatured type hash table from the index. */
2756
2757 static void
2758 create_signatured_type_table_from_index (struct objfile *objfile,
2759 struct dwarf2_section_info *section,
2760 const gdb_byte *bytes,
2761 offset_type elements)
2762 {
2763 offset_type i;
2764 htab_t sig_types_hash;
2765
2766 dwarf2_per_objfile->n_type_units = elements / 3;
2767 dwarf2_per_objfile->all_type_units
2768 = xmalloc (dwarf2_per_objfile->n_type_units
2769 * sizeof (struct signatured_type *));
2770
2771 sig_types_hash = allocate_signatured_type_table (objfile);
2772
2773 for (i = 0; i < elements; i += 3)
2774 {
2775 struct signatured_type *sig_type;
2776 ULONGEST offset, type_offset_in_tu, signature;
2777 void **slot;
2778
2779 gdb_static_assert (sizeof (ULONGEST) >= 8);
2780 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2781 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2782 BFD_ENDIAN_LITTLE);
2783 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2784 bytes += 3 * 8;
2785
2786 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2787 struct signatured_type);
2788 sig_type->signature = signature;
2789 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2790 sig_type->per_cu.is_debug_types = 1;
2791 sig_type->per_cu.section = section;
2792 sig_type->per_cu.offset.sect_off = offset;
2793 sig_type->per_cu.objfile = objfile;
2794 sig_type->per_cu.v.quick
2795 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2796 struct dwarf2_per_cu_quick_data);
2797
2798 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2799 *slot = sig_type;
2800
2801 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2802 }
2803
2804 dwarf2_per_objfile->signatured_types = sig_types_hash;
2805 }
2806
2807 /* Read the address map data from the mapped index, and use it to
2808 populate the objfile's psymtabs_addrmap. */
2809
2810 static void
2811 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2812 {
2813 const gdb_byte *iter, *end;
2814 struct obstack temp_obstack;
2815 struct addrmap *mutable_map;
2816 struct cleanup *cleanup;
2817 CORE_ADDR baseaddr;
2818
2819 obstack_init (&temp_obstack);
2820 cleanup = make_cleanup_obstack_free (&temp_obstack);
2821 mutable_map = addrmap_create_mutable (&temp_obstack);
2822
2823 iter = index->address_table;
2824 end = iter + index->address_table_size;
2825
2826 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2827
2828 while (iter < end)
2829 {
2830 ULONGEST hi, lo, cu_index;
2831 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2832 iter += 8;
2833 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2834 iter += 8;
2835 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2836 iter += 4;
2837
2838 if (lo > hi)
2839 {
2840 complaint (&symfile_complaints,
2841 _(".gdb_index address table has invalid range (%s - %s)"),
2842 hex_string (lo), hex_string (hi));
2843 continue;
2844 }
2845
2846 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2847 {
2848 complaint (&symfile_complaints,
2849 _(".gdb_index address table has invalid CU number %u"),
2850 (unsigned) cu_index);
2851 continue;
2852 }
2853
2854 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2855 dw2_get_cu (cu_index));
2856 }
2857
2858 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2859 &objfile->objfile_obstack);
2860 do_cleanups (cleanup);
2861 }
2862
2863 /* The hash function for strings in the mapped index. This is the same as
2864 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2865 implementation. This is necessary because the hash function is tied to the
2866 format of the mapped index file. The hash values do not have to match with
2867 SYMBOL_HASH_NEXT.
2868
2869 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2870
2871 static hashval_t
2872 mapped_index_string_hash (int index_version, const void *p)
2873 {
2874 const unsigned char *str = (const unsigned char *) p;
2875 hashval_t r = 0;
2876 unsigned char c;
2877
2878 while ((c = *str++) != 0)
2879 {
2880 if (index_version >= 5)
2881 c = tolower (c);
2882 r = r * 67 + c - 113;
2883 }
2884
2885 return r;
2886 }
2887
2888 /* Find a slot in the mapped index INDEX for the object named NAME.
2889 If NAME is found, set *VEC_OUT to point to the CU vector in the
2890 constant pool and return 1. If NAME cannot be found, return 0. */
2891
2892 static int
2893 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2894 offset_type **vec_out)
2895 {
2896 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2897 offset_type hash;
2898 offset_type slot, step;
2899 int (*cmp) (const char *, const char *);
2900
2901 if (current_language->la_language == language_cplus
2902 || current_language->la_language == language_java
2903 || current_language->la_language == language_fortran)
2904 {
2905 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2906 not contain any. */
2907 const char *paren = strchr (name, '(');
2908
2909 if (paren)
2910 {
2911 char *dup;
2912
2913 dup = xmalloc (paren - name + 1);
2914 memcpy (dup, name, paren - name);
2915 dup[paren - name] = 0;
2916
2917 make_cleanup (xfree, dup);
2918 name = dup;
2919 }
2920 }
2921
2922 /* Index version 4 did not support case insensitive searches. But the
2923 indices for case insensitive languages are built in lowercase, therefore
2924 simulate our NAME being searched is also lowercased. */
2925 hash = mapped_index_string_hash ((index->version == 4
2926 && case_sensitivity == case_sensitive_off
2927 ? 5 : index->version),
2928 name);
2929
2930 slot = hash & (index->symbol_table_slots - 1);
2931 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2932 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2933
2934 for (;;)
2935 {
2936 /* Convert a slot number to an offset into the table. */
2937 offset_type i = 2 * slot;
2938 const char *str;
2939 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2940 {
2941 do_cleanups (back_to);
2942 return 0;
2943 }
2944
2945 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2946 if (!cmp (name, str))
2947 {
2948 *vec_out = (offset_type *) (index->constant_pool
2949 + MAYBE_SWAP (index->symbol_table[i + 1]));
2950 do_cleanups (back_to);
2951 return 1;
2952 }
2953
2954 slot = (slot + step) & (index->symbol_table_slots - 1);
2955 }
2956 }
2957
2958 /* A helper function that reads the .gdb_index from SECTION and fills
2959 in MAP. FILENAME is the name of the file containing the section;
2960 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2961 ok to use deprecated sections.
2962
2963 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2964 out parameters that are filled in with information about the CU and
2965 TU lists in the section.
2966
2967 Returns 1 if all went well, 0 otherwise. */
2968
2969 static int
2970 read_index_from_section (struct objfile *objfile,
2971 const char *filename,
2972 int deprecated_ok,
2973 struct dwarf2_section_info *section,
2974 struct mapped_index *map,
2975 const gdb_byte **cu_list,
2976 offset_type *cu_list_elements,
2977 const gdb_byte **types_list,
2978 offset_type *types_list_elements)
2979 {
2980 const gdb_byte *addr;
2981 offset_type version;
2982 offset_type *metadata;
2983 int i;
2984
2985 if (dwarf2_section_empty_p (section))
2986 return 0;
2987
2988 /* Older elfutils strip versions could keep the section in the main
2989 executable while splitting it for the separate debug info file. */
2990 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
2991 return 0;
2992
2993 dwarf2_read_section (objfile, section);
2994
2995 addr = section->buffer;
2996 /* Version check. */
2997 version = MAYBE_SWAP (*(offset_type *) addr);
2998 /* Versions earlier than 3 emitted every copy of a psymbol. This
2999 causes the index to behave very poorly for certain requests. Version 3
3000 contained incomplete addrmap. So, it seems better to just ignore such
3001 indices. */
3002 if (version < 4)
3003 {
3004 static int warning_printed = 0;
3005 if (!warning_printed)
3006 {
3007 warning (_("Skipping obsolete .gdb_index section in %s."),
3008 filename);
3009 warning_printed = 1;
3010 }
3011 return 0;
3012 }
3013 /* Index version 4 uses a different hash function than index version
3014 5 and later.
3015
3016 Versions earlier than 6 did not emit psymbols for inlined
3017 functions. Using these files will cause GDB not to be able to
3018 set breakpoints on inlined functions by name, so we ignore these
3019 indices unless the user has done
3020 "set use-deprecated-index-sections on". */
3021 if (version < 6 && !deprecated_ok)
3022 {
3023 static int warning_printed = 0;
3024 if (!warning_printed)
3025 {
3026 warning (_("\
3027 Skipping deprecated .gdb_index section in %s.\n\
3028 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3029 to use the section anyway."),
3030 filename);
3031 warning_printed = 1;
3032 }
3033 return 0;
3034 }
3035 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3036 of the TU (for symbols coming from TUs). It's just a performance bug, and
3037 we can't distinguish gdb-generated indices from gold-generated ones, so
3038 nothing to do here. */
3039
3040 /* Indexes with higher version than the one supported by GDB may be no
3041 longer backward compatible. */
3042 if (version > 8)
3043 return 0;
3044
3045 map->version = version;
3046 map->total_size = section->size;
3047
3048 metadata = (offset_type *) (addr + sizeof (offset_type));
3049
3050 i = 0;
3051 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3052 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3053 / 8);
3054 ++i;
3055
3056 *types_list = addr + MAYBE_SWAP (metadata[i]);
3057 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3058 - MAYBE_SWAP (metadata[i]))
3059 / 8);
3060 ++i;
3061
3062 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3063 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3064 - MAYBE_SWAP (metadata[i]));
3065 ++i;
3066
3067 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3068 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3069 - MAYBE_SWAP (metadata[i]))
3070 / (2 * sizeof (offset_type)));
3071 ++i;
3072
3073 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3074
3075 return 1;
3076 }
3077
3078
3079 /* Read the index file. If everything went ok, initialize the "quick"
3080 elements of all the CUs and return 1. Otherwise, return 0. */
3081
3082 static int
3083 dwarf2_read_index (struct objfile *objfile)
3084 {
3085 struct mapped_index local_map, *map;
3086 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3087 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3088 struct dwz_file *dwz;
3089
3090 if (!read_index_from_section (objfile, objfile_name (objfile),
3091 use_deprecated_index_sections,
3092 &dwarf2_per_objfile->gdb_index, &local_map,
3093 &cu_list, &cu_list_elements,
3094 &types_list, &types_list_elements))
3095 return 0;
3096
3097 /* Don't use the index if it's empty. */
3098 if (local_map.symbol_table_slots == 0)
3099 return 0;
3100
3101 /* If there is a .dwz file, read it so we can get its CU list as
3102 well. */
3103 dwz = dwarf2_get_dwz_file ();
3104 if (dwz != NULL)
3105 {
3106 struct mapped_index dwz_map;
3107 const gdb_byte *dwz_types_ignore;
3108 offset_type dwz_types_elements_ignore;
3109
3110 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3111 1,
3112 &dwz->gdb_index, &dwz_map,
3113 &dwz_list, &dwz_list_elements,
3114 &dwz_types_ignore,
3115 &dwz_types_elements_ignore))
3116 {
3117 warning (_("could not read '.gdb_index' section from %s; skipping"),
3118 bfd_get_filename (dwz->dwz_bfd));
3119 return 0;
3120 }
3121 }
3122
3123 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3124 dwz_list_elements);
3125
3126 if (types_list_elements)
3127 {
3128 struct dwarf2_section_info *section;
3129
3130 /* We can only handle a single .debug_types when we have an
3131 index. */
3132 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3133 return 0;
3134
3135 section = VEC_index (dwarf2_section_info_def,
3136 dwarf2_per_objfile->types, 0);
3137
3138 create_signatured_type_table_from_index (objfile, section, types_list,
3139 types_list_elements);
3140 }
3141
3142 create_addrmap_from_index (objfile, &local_map);
3143
3144 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
3145 *map = local_map;
3146
3147 dwarf2_per_objfile->index_table = map;
3148 dwarf2_per_objfile->using_index = 1;
3149 dwarf2_per_objfile->quick_file_names_table =
3150 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3151
3152 return 1;
3153 }
3154
3155 /* A helper for the "quick" functions which sets the global
3156 dwarf2_per_objfile according to OBJFILE. */
3157
3158 static void
3159 dw2_setup (struct objfile *objfile)
3160 {
3161 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
3162 gdb_assert (dwarf2_per_objfile);
3163 }
3164
3165 /* die_reader_func for dw2_get_file_names. */
3166
3167 static void
3168 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3169 const gdb_byte *info_ptr,
3170 struct die_info *comp_unit_die,
3171 int has_children,
3172 void *data)
3173 {
3174 struct dwarf2_cu *cu = reader->cu;
3175 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3176 struct objfile *objfile = dwarf2_per_objfile->objfile;
3177 struct dwarf2_per_cu_data *lh_cu;
3178 struct line_header *lh;
3179 struct attribute *attr;
3180 int i;
3181 const char *name, *comp_dir;
3182 void **slot;
3183 struct quick_file_names *qfn;
3184 unsigned int line_offset;
3185
3186 gdb_assert (! this_cu->is_debug_types);
3187
3188 /* Our callers never want to match partial units -- instead they
3189 will match the enclosing full CU. */
3190 if (comp_unit_die->tag == DW_TAG_partial_unit)
3191 {
3192 this_cu->v.quick->no_file_data = 1;
3193 return;
3194 }
3195
3196 lh_cu = this_cu;
3197 lh = NULL;
3198 slot = NULL;
3199 line_offset = 0;
3200
3201 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3202 if (attr)
3203 {
3204 struct quick_file_names find_entry;
3205
3206 line_offset = DW_UNSND (attr);
3207
3208 /* We may have already read in this line header (TU line header sharing).
3209 If we have we're done. */
3210 find_entry.hash.dwo_unit = cu->dwo_unit;
3211 find_entry.hash.line_offset.sect_off = line_offset;
3212 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3213 &find_entry, INSERT);
3214 if (*slot != NULL)
3215 {
3216 lh_cu->v.quick->file_names = *slot;
3217 return;
3218 }
3219
3220 lh = dwarf_decode_line_header (line_offset, cu);
3221 }
3222 if (lh == NULL)
3223 {
3224 lh_cu->v.quick->no_file_data = 1;
3225 return;
3226 }
3227
3228 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
3229 qfn->hash.dwo_unit = cu->dwo_unit;
3230 qfn->hash.line_offset.sect_off = line_offset;
3231 gdb_assert (slot != NULL);
3232 *slot = qfn;
3233
3234 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3235
3236 qfn->num_file_names = lh->num_file_names;
3237 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
3238 lh->num_file_names * sizeof (char *));
3239 for (i = 0; i < lh->num_file_names; ++i)
3240 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3241 qfn->real_names = NULL;
3242
3243 free_line_header (lh);
3244
3245 lh_cu->v.quick->file_names = qfn;
3246 }
3247
3248 /* A helper for the "quick" functions which attempts to read the line
3249 table for THIS_CU. */
3250
3251 static struct quick_file_names *
3252 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3253 {
3254 /* This should never be called for TUs. */
3255 gdb_assert (! this_cu->is_debug_types);
3256 /* Nor type unit groups. */
3257 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3258
3259 if (this_cu->v.quick->file_names != NULL)
3260 return this_cu->v.quick->file_names;
3261 /* If we know there is no line data, no point in looking again. */
3262 if (this_cu->v.quick->no_file_data)
3263 return NULL;
3264
3265 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3266
3267 if (this_cu->v.quick->no_file_data)
3268 return NULL;
3269 return this_cu->v.quick->file_names;
3270 }
3271
3272 /* A helper for the "quick" functions which computes and caches the
3273 real path for a given file name from the line table. */
3274
3275 static const char *
3276 dw2_get_real_path (struct objfile *objfile,
3277 struct quick_file_names *qfn, int index)
3278 {
3279 if (qfn->real_names == NULL)
3280 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3281 qfn->num_file_names, char *);
3282
3283 if (qfn->real_names[index] == NULL)
3284 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3285
3286 return qfn->real_names[index];
3287 }
3288
3289 static struct symtab *
3290 dw2_find_last_source_symtab (struct objfile *objfile)
3291 {
3292 int index;
3293
3294 dw2_setup (objfile);
3295 index = dwarf2_per_objfile->n_comp_units - 1;
3296 return dw2_instantiate_symtab (dw2_get_cu (index));
3297 }
3298
3299 /* Traversal function for dw2_forget_cached_source_info. */
3300
3301 static int
3302 dw2_free_cached_file_names (void **slot, void *info)
3303 {
3304 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3305
3306 if (file_data->real_names)
3307 {
3308 int i;
3309
3310 for (i = 0; i < file_data->num_file_names; ++i)
3311 {
3312 xfree ((void*) file_data->real_names[i]);
3313 file_data->real_names[i] = NULL;
3314 }
3315 }
3316
3317 return 1;
3318 }
3319
3320 static void
3321 dw2_forget_cached_source_info (struct objfile *objfile)
3322 {
3323 dw2_setup (objfile);
3324
3325 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3326 dw2_free_cached_file_names, NULL);
3327 }
3328
3329 /* Helper function for dw2_map_symtabs_matching_filename that expands
3330 the symtabs and calls the iterator. */
3331
3332 static int
3333 dw2_map_expand_apply (struct objfile *objfile,
3334 struct dwarf2_per_cu_data *per_cu,
3335 const char *name, const char *real_path,
3336 int (*callback) (struct symtab *, void *),
3337 void *data)
3338 {
3339 struct symtab *last_made = objfile->symtabs;
3340
3341 /* Don't visit already-expanded CUs. */
3342 if (per_cu->v.quick->symtab)
3343 return 0;
3344
3345 /* This may expand more than one symtab, and we want to iterate over
3346 all of them. */
3347 dw2_instantiate_symtab (per_cu);
3348
3349 return iterate_over_some_symtabs (name, real_path, callback, data,
3350 objfile->symtabs, last_made);
3351 }
3352
3353 /* Implementation of the map_symtabs_matching_filename method. */
3354
3355 static int
3356 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3357 const char *real_path,
3358 int (*callback) (struct symtab *, void *),
3359 void *data)
3360 {
3361 int i;
3362 const char *name_basename = lbasename (name);
3363
3364 dw2_setup (objfile);
3365
3366 /* The rule is CUs specify all the files, including those used by
3367 any TU, so there's no need to scan TUs here. */
3368
3369 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3370 {
3371 int j;
3372 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3373 struct quick_file_names *file_data;
3374
3375 /* We only need to look at symtabs not already expanded. */
3376 if (per_cu->v.quick->symtab)
3377 continue;
3378
3379 file_data = dw2_get_file_names (per_cu);
3380 if (file_data == NULL)
3381 continue;
3382
3383 for (j = 0; j < file_data->num_file_names; ++j)
3384 {
3385 const char *this_name = file_data->file_names[j];
3386 const char *this_real_name;
3387
3388 if (compare_filenames_for_search (this_name, name))
3389 {
3390 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3391 callback, data))
3392 return 1;
3393 continue;
3394 }
3395
3396 /* Before we invoke realpath, which can get expensive when many
3397 files are involved, do a quick comparison of the basenames. */
3398 if (! basenames_may_differ
3399 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3400 continue;
3401
3402 this_real_name = dw2_get_real_path (objfile, file_data, j);
3403 if (compare_filenames_for_search (this_real_name, name))
3404 {
3405 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3406 callback, data))
3407 return 1;
3408 continue;
3409 }
3410
3411 if (real_path != NULL)
3412 {
3413 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3414 gdb_assert (IS_ABSOLUTE_PATH (name));
3415 if (this_real_name != NULL
3416 && FILENAME_CMP (real_path, this_real_name) == 0)
3417 {
3418 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3419 callback, data))
3420 return 1;
3421 continue;
3422 }
3423 }
3424 }
3425 }
3426
3427 return 0;
3428 }
3429
3430 /* Struct used to manage iterating over all CUs looking for a symbol. */
3431
3432 struct dw2_symtab_iterator
3433 {
3434 /* The internalized form of .gdb_index. */
3435 struct mapped_index *index;
3436 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3437 int want_specific_block;
3438 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3439 Unused if !WANT_SPECIFIC_BLOCK. */
3440 int block_index;
3441 /* The kind of symbol we're looking for. */
3442 domain_enum domain;
3443 /* The list of CUs from the index entry of the symbol,
3444 or NULL if not found. */
3445 offset_type *vec;
3446 /* The next element in VEC to look at. */
3447 int next;
3448 /* The number of elements in VEC, or zero if there is no match. */
3449 int length;
3450 };
3451
3452 /* Initialize the index symtab iterator ITER.
3453 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3454 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3455
3456 static void
3457 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3458 struct mapped_index *index,
3459 int want_specific_block,
3460 int block_index,
3461 domain_enum domain,
3462 const char *name)
3463 {
3464 iter->index = index;
3465 iter->want_specific_block = want_specific_block;
3466 iter->block_index = block_index;
3467 iter->domain = domain;
3468 iter->next = 0;
3469
3470 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3471 iter->length = MAYBE_SWAP (*iter->vec);
3472 else
3473 {
3474 iter->vec = NULL;
3475 iter->length = 0;
3476 }
3477 }
3478
3479 /* Return the next matching CU or NULL if there are no more. */
3480
3481 static struct dwarf2_per_cu_data *
3482 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3483 {
3484 for ( ; iter->next < iter->length; ++iter->next)
3485 {
3486 offset_type cu_index_and_attrs =
3487 MAYBE_SWAP (iter->vec[iter->next + 1]);
3488 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3489 struct dwarf2_per_cu_data *per_cu;
3490 int want_static = iter->block_index != GLOBAL_BLOCK;
3491 /* This value is only valid for index versions >= 7. */
3492 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3493 gdb_index_symbol_kind symbol_kind =
3494 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3495 /* Only check the symbol attributes if they're present.
3496 Indices prior to version 7 don't record them,
3497 and indices >= 7 may elide them for certain symbols
3498 (gold does this). */
3499 int attrs_valid =
3500 (iter->index->version >= 7
3501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3502
3503 /* Don't crash on bad data. */
3504 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3505 + dwarf2_per_objfile->n_type_units))
3506 {
3507 complaint (&symfile_complaints,
3508 _(".gdb_index entry has bad CU index"
3509 " [in module %s]"),
3510 objfile_name (dwarf2_per_objfile->objfile));
3511 continue;
3512 }
3513
3514 per_cu = dw2_get_cu (cu_index);
3515
3516 /* Skip if already read in. */
3517 if (per_cu->v.quick->symtab)
3518 continue;
3519
3520 if (attrs_valid
3521 && iter->want_specific_block
3522 && want_static != is_static)
3523 continue;
3524
3525 /* Only check the symbol's kind if it has one. */
3526 if (attrs_valid)
3527 {
3528 switch (iter->domain)
3529 {
3530 case VAR_DOMAIN:
3531 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3532 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3533 /* Some types are also in VAR_DOMAIN. */
3534 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3535 continue;
3536 break;
3537 case STRUCT_DOMAIN:
3538 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3539 continue;
3540 break;
3541 case LABEL_DOMAIN:
3542 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3543 continue;
3544 break;
3545 default:
3546 break;
3547 }
3548 }
3549
3550 ++iter->next;
3551 return per_cu;
3552 }
3553
3554 return NULL;
3555 }
3556
3557 static struct symtab *
3558 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3559 const char *name, domain_enum domain)
3560 {
3561 struct symtab *stab_best = NULL;
3562 struct mapped_index *index;
3563
3564 dw2_setup (objfile);
3565
3566 index = dwarf2_per_objfile->index_table;
3567
3568 /* index is NULL if OBJF_READNOW. */
3569 if (index)
3570 {
3571 struct dw2_symtab_iterator iter;
3572 struct dwarf2_per_cu_data *per_cu;
3573
3574 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3575
3576 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3577 {
3578 struct symbol *sym = NULL;
3579 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3580
3581 /* Some caution must be observed with overloaded functions
3582 and methods, since the index will not contain any overload
3583 information (but NAME might contain it). */
3584 if (stab->primary)
3585 {
3586 struct blockvector *bv = BLOCKVECTOR (stab);
3587 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3588
3589 sym = lookup_block_symbol (block, name, domain);
3590 }
3591
3592 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3593 {
3594 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3595 return stab;
3596
3597 stab_best = stab;
3598 }
3599
3600 /* Keep looking through other CUs. */
3601 }
3602 }
3603
3604 return stab_best;
3605 }
3606
3607 static void
3608 dw2_print_stats (struct objfile *objfile)
3609 {
3610 int i, total, count;
3611
3612 dw2_setup (objfile);
3613 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3614 count = 0;
3615 for (i = 0; i < total; ++i)
3616 {
3617 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3618
3619 if (!per_cu->v.quick->symtab)
3620 ++count;
3621 }
3622 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3623 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3624 }
3625
3626 /* This dumps minimal information about the index.
3627 It is called via "mt print objfiles".
3628 One use is to verify .gdb_index has been loaded by the
3629 gdb.dwarf2/gdb-index.exp testcase. */
3630
3631 static void
3632 dw2_dump (struct objfile *objfile)
3633 {
3634 dw2_setup (objfile);
3635 gdb_assert (dwarf2_per_objfile->using_index);
3636 printf_filtered (".gdb_index:");
3637 if (dwarf2_per_objfile->index_table != NULL)
3638 {
3639 printf_filtered (" version %d\n",
3640 dwarf2_per_objfile->index_table->version);
3641 }
3642 else
3643 printf_filtered (" faked for \"readnow\"\n");
3644 printf_filtered ("\n");
3645 }
3646
3647 static void
3648 dw2_relocate (struct objfile *objfile,
3649 const struct section_offsets *new_offsets,
3650 const struct section_offsets *delta)
3651 {
3652 /* There's nothing to relocate here. */
3653 }
3654
3655 static void
3656 dw2_expand_symtabs_for_function (struct objfile *objfile,
3657 const char *func_name)
3658 {
3659 struct mapped_index *index;
3660
3661 dw2_setup (objfile);
3662
3663 index = dwarf2_per_objfile->index_table;
3664
3665 /* index is NULL if OBJF_READNOW. */
3666 if (index)
3667 {
3668 struct dw2_symtab_iterator iter;
3669 struct dwarf2_per_cu_data *per_cu;
3670
3671 /* Note: It doesn't matter what we pass for block_index here. */
3672 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3673 func_name);
3674
3675 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3676 dw2_instantiate_symtab (per_cu);
3677 }
3678 }
3679
3680 static void
3681 dw2_expand_all_symtabs (struct objfile *objfile)
3682 {
3683 int i;
3684
3685 dw2_setup (objfile);
3686
3687 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3688 + dwarf2_per_objfile->n_type_units); ++i)
3689 {
3690 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3691
3692 dw2_instantiate_symtab (per_cu);
3693 }
3694 }
3695
3696 static void
3697 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3698 const char *fullname)
3699 {
3700 int i;
3701
3702 dw2_setup (objfile);
3703
3704 /* We don't need to consider type units here.
3705 This is only called for examining code, e.g. expand_line_sal.
3706 There can be an order of magnitude (or more) more type units
3707 than comp units, and we avoid them if we can. */
3708
3709 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3710 {
3711 int j;
3712 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3713 struct quick_file_names *file_data;
3714
3715 /* We only need to look at symtabs not already expanded. */
3716 if (per_cu->v.quick->symtab)
3717 continue;
3718
3719 file_data = dw2_get_file_names (per_cu);
3720 if (file_data == NULL)
3721 continue;
3722
3723 for (j = 0; j < file_data->num_file_names; ++j)
3724 {
3725 const char *this_fullname = file_data->file_names[j];
3726
3727 if (filename_cmp (this_fullname, fullname) == 0)
3728 {
3729 dw2_instantiate_symtab (per_cu);
3730 break;
3731 }
3732 }
3733 }
3734 }
3735
3736 static void
3737 dw2_map_matching_symbols (struct objfile *objfile,
3738 const char * name, domain_enum namespace,
3739 int global,
3740 int (*callback) (struct block *,
3741 struct symbol *, void *),
3742 void *data, symbol_compare_ftype *match,
3743 symbol_compare_ftype *ordered_compare)
3744 {
3745 /* Currently unimplemented; used for Ada. The function can be called if the
3746 current language is Ada for a non-Ada objfile using GNU index. As Ada
3747 does not look for non-Ada symbols this function should just return. */
3748 }
3749
3750 static void
3751 dw2_expand_symtabs_matching
3752 (struct objfile *objfile,
3753 int (*file_matcher) (const char *, void *, int basenames),
3754 int (*name_matcher) (const char *, void *),
3755 enum search_domain kind,
3756 void *data)
3757 {
3758 int i;
3759 offset_type iter;
3760 struct mapped_index *index;
3761
3762 dw2_setup (objfile);
3763
3764 /* index_table is NULL if OBJF_READNOW. */
3765 if (!dwarf2_per_objfile->index_table)
3766 return;
3767 index = dwarf2_per_objfile->index_table;
3768
3769 if (file_matcher != NULL)
3770 {
3771 struct cleanup *cleanup;
3772 htab_t visited_found, visited_not_found;
3773
3774 visited_found = htab_create_alloc (10,
3775 htab_hash_pointer, htab_eq_pointer,
3776 NULL, xcalloc, xfree);
3777 cleanup = make_cleanup_htab_delete (visited_found);
3778 visited_not_found = htab_create_alloc (10,
3779 htab_hash_pointer, htab_eq_pointer,
3780 NULL, xcalloc, xfree);
3781 make_cleanup_htab_delete (visited_not_found);
3782
3783 /* The rule is CUs specify all the files, including those used by
3784 any TU, so there's no need to scan TUs here. */
3785
3786 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3787 {
3788 int j;
3789 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3790 struct quick_file_names *file_data;
3791 void **slot;
3792
3793 per_cu->v.quick->mark = 0;
3794
3795 /* We only need to look at symtabs not already expanded. */
3796 if (per_cu->v.quick->symtab)
3797 continue;
3798
3799 file_data = dw2_get_file_names (per_cu);
3800 if (file_data == NULL)
3801 continue;
3802
3803 if (htab_find (visited_not_found, file_data) != NULL)
3804 continue;
3805 else if (htab_find (visited_found, file_data) != NULL)
3806 {
3807 per_cu->v.quick->mark = 1;
3808 continue;
3809 }
3810
3811 for (j = 0; j < file_data->num_file_names; ++j)
3812 {
3813 const char *this_real_name;
3814
3815 if (file_matcher (file_data->file_names[j], data, 0))
3816 {
3817 per_cu->v.quick->mark = 1;
3818 break;
3819 }
3820
3821 /* Before we invoke realpath, which can get expensive when many
3822 files are involved, do a quick comparison of the basenames. */
3823 if (!basenames_may_differ
3824 && !file_matcher (lbasename (file_data->file_names[j]),
3825 data, 1))
3826 continue;
3827
3828 this_real_name = dw2_get_real_path (objfile, file_data, j);
3829 if (file_matcher (this_real_name, data, 0))
3830 {
3831 per_cu->v.quick->mark = 1;
3832 break;
3833 }
3834 }
3835
3836 slot = htab_find_slot (per_cu->v.quick->mark
3837 ? visited_found
3838 : visited_not_found,
3839 file_data, INSERT);
3840 *slot = file_data;
3841 }
3842
3843 do_cleanups (cleanup);
3844 }
3845
3846 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3847 {
3848 offset_type idx = 2 * iter;
3849 const char *name;
3850 offset_type *vec, vec_len, vec_idx;
3851
3852 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3853 continue;
3854
3855 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3856
3857 if (! (*name_matcher) (name, data))
3858 continue;
3859
3860 /* The name was matched, now expand corresponding CUs that were
3861 marked. */
3862 vec = (offset_type *) (index->constant_pool
3863 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3864 vec_len = MAYBE_SWAP (vec[0]);
3865 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3866 {
3867 struct dwarf2_per_cu_data *per_cu;
3868 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3869 gdb_index_symbol_kind symbol_kind =
3870 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3871 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3872 /* Only check the symbol attributes if they're present.
3873 Indices prior to version 7 don't record them,
3874 and indices >= 7 may elide them for certain symbols
3875 (gold does this). */
3876 int attrs_valid =
3877 (index->version >= 7
3878 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3879
3880 /* Only check the symbol's kind if it has one. */
3881 if (attrs_valid)
3882 {
3883 switch (kind)
3884 {
3885 case VARIABLES_DOMAIN:
3886 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3887 continue;
3888 break;
3889 case FUNCTIONS_DOMAIN:
3890 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3891 continue;
3892 break;
3893 case TYPES_DOMAIN:
3894 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900 }
3901
3902 /* Don't crash on bad data. */
3903 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3904 + dwarf2_per_objfile->n_type_units))
3905 {
3906 complaint (&symfile_complaints,
3907 _(".gdb_index entry has bad CU index"
3908 " [in module %s]"), objfile_name (objfile));
3909 continue;
3910 }
3911
3912 per_cu = dw2_get_cu (cu_index);
3913 if (file_matcher == NULL || per_cu->v.quick->mark)
3914 dw2_instantiate_symtab (per_cu);
3915 }
3916 }
3917 }
3918
3919 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3920 symtab. */
3921
3922 static struct symtab *
3923 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3924 {
3925 int i;
3926
3927 if (BLOCKVECTOR (symtab) != NULL
3928 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3929 return symtab;
3930
3931 if (symtab->includes == NULL)
3932 return NULL;
3933
3934 for (i = 0; symtab->includes[i]; ++i)
3935 {
3936 struct symtab *s = symtab->includes[i];
3937
3938 s = recursively_find_pc_sect_symtab (s, pc);
3939 if (s != NULL)
3940 return s;
3941 }
3942
3943 return NULL;
3944 }
3945
3946 static struct symtab *
3947 dw2_find_pc_sect_symtab (struct objfile *objfile,
3948 struct minimal_symbol *msymbol,
3949 CORE_ADDR pc,
3950 struct obj_section *section,
3951 int warn_if_readin)
3952 {
3953 struct dwarf2_per_cu_data *data;
3954 struct symtab *result;
3955
3956 dw2_setup (objfile);
3957
3958 if (!objfile->psymtabs_addrmap)
3959 return NULL;
3960
3961 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3962 if (!data)
3963 return NULL;
3964
3965 if (warn_if_readin && data->v.quick->symtab)
3966 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3967 paddress (get_objfile_arch (objfile), pc));
3968
3969 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3970 gdb_assert (result != NULL);
3971 return result;
3972 }
3973
3974 static void
3975 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3976 void *data, int need_fullname)
3977 {
3978 int i;
3979 struct cleanup *cleanup;
3980 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3981 NULL, xcalloc, xfree);
3982
3983 cleanup = make_cleanup_htab_delete (visited);
3984 dw2_setup (objfile);
3985
3986 /* The rule is CUs specify all the files, including those used by
3987 any TU, so there's no need to scan TUs here.
3988 We can ignore file names coming from already-expanded CUs. */
3989
3990 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3991 {
3992 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3993
3994 if (per_cu->v.quick->symtab)
3995 {
3996 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3997 INSERT);
3998
3999 *slot = per_cu->v.quick->file_names;
4000 }
4001 }
4002
4003 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4004 {
4005 int j;
4006 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
4007 struct quick_file_names *file_data;
4008 void **slot;
4009
4010 /* We only need to look at symtabs not already expanded. */
4011 if (per_cu->v.quick->symtab)
4012 continue;
4013
4014 file_data = dw2_get_file_names (per_cu);
4015 if (file_data == NULL)
4016 continue;
4017
4018 slot = htab_find_slot (visited, file_data, INSERT);
4019 if (*slot)
4020 {
4021 /* Already visited. */
4022 continue;
4023 }
4024 *slot = file_data;
4025
4026 for (j = 0; j < file_data->num_file_names; ++j)
4027 {
4028 const char *this_real_name;
4029
4030 if (need_fullname)
4031 this_real_name = dw2_get_real_path (objfile, file_data, j);
4032 else
4033 this_real_name = NULL;
4034 (*fun) (file_data->file_names[j], this_real_name, data);
4035 }
4036 }
4037
4038 do_cleanups (cleanup);
4039 }
4040
4041 static int
4042 dw2_has_symbols (struct objfile *objfile)
4043 {
4044 return 1;
4045 }
4046
4047 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4048 {
4049 dw2_has_symbols,
4050 dw2_find_last_source_symtab,
4051 dw2_forget_cached_source_info,
4052 dw2_map_symtabs_matching_filename,
4053 dw2_lookup_symbol,
4054 dw2_print_stats,
4055 dw2_dump,
4056 dw2_relocate,
4057 dw2_expand_symtabs_for_function,
4058 dw2_expand_all_symtabs,
4059 dw2_expand_symtabs_with_fullname,
4060 dw2_map_matching_symbols,
4061 dw2_expand_symtabs_matching,
4062 dw2_find_pc_sect_symtab,
4063 dw2_map_symbol_filenames
4064 };
4065
4066 /* Initialize for reading DWARF for this objfile. Return 0 if this
4067 file will use psymtabs, or 1 if using the GNU index. */
4068
4069 int
4070 dwarf2_initialize_objfile (struct objfile *objfile)
4071 {
4072 /* If we're about to read full symbols, don't bother with the
4073 indices. In this case we also don't care if some other debug
4074 format is making psymtabs, because they are all about to be
4075 expanded anyway. */
4076 if ((objfile->flags & OBJF_READNOW))
4077 {
4078 int i;
4079
4080 dwarf2_per_objfile->using_index = 1;
4081 create_all_comp_units (objfile);
4082 create_all_type_units (objfile);
4083 dwarf2_per_objfile->quick_file_names_table =
4084 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4085
4086 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4087 + dwarf2_per_objfile->n_type_units); ++i)
4088 {
4089 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4090
4091 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4092 struct dwarf2_per_cu_quick_data);
4093 }
4094
4095 /* Return 1 so that gdb sees the "quick" functions. However,
4096 these functions will be no-ops because we will have expanded
4097 all symtabs. */
4098 return 1;
4099 }
4100
4101 if (dwarf2_read_index (objfile))
4102 return 1;
4103
4104 return 0;
4105 }
4106
4107 \f
4108
4109 /* Build a partial symbol table. */
4110
4111 void
4112 dwarf2_build_psymtabs (struct objfile *objfile)
4113 {
4114 volatile struct gdb_exception except;
4115
4116 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4117 {
4118 init_psymbol_list (objfile, 1024);
4119 }
4120
4121 TRY_CATCH (except, RETURN_MASK_ERROR)
4122 {
4123 /* This isn't really ideal: all the data we allocate on the
4124 objfile's obstack is still uselessly kept around. However,
4125 freeing it seems unsafe. */
4126 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4127
4128 dwarf2_build_psymtabs_hard (objfile);
4129 discard_cleanups (cleanups);
4130 }
4131 if (except.reason < 0)
4132 exception_print (gdb_stderr, except);
4133 }
4134
4135 /* Return the total length of the CU described by HEADER. */
4136
4137 static unsigned int
4138 get_cu_length (const struct comp_unit_head *header)
4139 {
4140 return header->initial_length_size + header->length;
4141 }
4142
4143 /* Return TRUE if OFFSET is within CU_HEADER. */
4144
4145 static inline int
4146 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4147 {
4148 sect_offset bottom = { cu_header->offset.sect_off };
4149 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4150
4151 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4152 }
4153
4154 /* Find the base address of the compilation unit for range lists and
4155 location lists. It will normally be specified by DW_AT_low_pc.
4156 In DWARF-3 draft 4, the base address could be overridden by
4157 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4158 compilation units with discontinuous ranges. */
4159
4160 static void
4161 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4162 {
4163 struct attribute *attr;
4164
4165 cu->base_known = 0;
4166 cu->base_address = 0;
4167
4168 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4169 if (attr)
4170 {
4171 cu->base_address = DW_ADDR (attr);
4172 cu->base_known = 1;
4173 }
4174 else
4175 {
4176 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4177 if (attr)
4178 {
4179 cu->base_address = DW_ADDR (attr);
4180 cu->base_known = 1;
4181 }
4182 }
4183 }
4184
4185 /* Read in the comp unit header information from the debug_info at info_ptr.
4186 NOTE: This leaves members offset, first_die_offset to be filled in
4187 by the caller. */
4188
4189 static const gdb_byte *
4190 read_comp_unit_head (struct comp_unit_head *cu_header,
4191 const gdb_byte *info_ptr, bfd *abfd)
4192 {
4193 int signed_addr;
4194 unsigned int bytes_read;
4195
4196 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4197 cu_header->initial_length_size = bytes_read;
4198 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4199 info_ptr += bytes_read;
4200 cu_header->version = read_2_bytes (abfd, info_ptr);
4201 info_ptr += 2;
4202 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4203 &bytes_read);
4204 info_ptr += bytes_read;
4205 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4206 info_ptr += 1;
4207 signed_addr = bfd_get_sign_extend_vma (abfd);
4208 if (signed_addr < 0)
4209 internal_error (__FILE__, __LINE__,
4210 _("read_comp_unit_head: dwarf from non elf file"));
4211 cu_header->signed_addr_p = signed_addr;
4212
4213 return info_ptr;
4214 }
4215
4216 /* Helper function that returns the proper abbrev section for
4217 THIS_CU. */
4218
4219 static struct dwarf2_section_info *
4220 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4221 {
4222 struct dwarf2_section_info *abbrev;
4223
4224 if (this_cu->is_dwz)
4225 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4226 else
4227 abbrev = &dwarf2_per_objfile->abbrev;
4228
4229 return abbrev;
4230 }
4231
4232 /* Subroutine of read_and_check_comp_unit_head and
4233 read_and_check_type_unit_head to simplify them.
4234 Perform various error checking on the header. */
4235
4236 static void
4237 error_check_comp_unit_head (struct comp_unit_head *header,
4238 struct dwarf2_section_info *section,
4239 struct dwarf2_section_info *abbrev_section)
4240 {
4241 bfd *abfd = get_section_bfd_owner (section);
4242 const char *filename = get_section_file_name (section);
4243
4244 if (header->version != 2 && header->version != 3 && header->version != 4)
4245 error (_("Dwarf Error: wrong version in compilation unit header "
4246 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4247 filename);
4248
4249 if (header->abbrev_offset.sect_off
4250 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4251 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4252 "(offset 0x%lx + 6) [in module %s]"),
4253 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4254 filename);
4255
4256 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4257 avoid potential 32-bit overflow. */
4258 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4259 > section->size)
4260 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4261 "(offset 0x%lx + 0) [in module %s]"),
4262 (long) header->length, (long) header->offset.sect_off,
4263 filename);
4264 }
4265
4266 /* Read in a CU/TU header and perform some basic error checking.
4267 The contents of the header are stored in HEADER.
4268 The result is a pointer to the start of the first DIE. */
4269
4270 static const gdb_byte *
4271 read_and_check_comp_unit_head (struct comp_unit_head *header,
4272 struct dwarf2_section_info *section,
4273 struct dwarf2_section_info *abbrev_section,
4274 const gdb_byte *info_ptr,
4275 int is_debug_types_section)
4276 {
4277 const gdb_byte *beg_of_comp_unit = info_ptr;
4278 bfd *abfd = get_section_bfd_owner (section);
4279
4280 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4281
4282 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4283
4284 /* If we're reading a type unit, skip over the signature and
4285 type_offset fields. */
4286 if (is_debug_types_section)
4287 info_ptr += 8 /*signature*/ + header->offset_size;
4288
4289 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4290
4291 error_check_comp_unit_head (header, section, abbrev_section);
4292
4293 return info_ptr;
4294 }
4295
4296 /* Read in the types comp unit header information from .debug_types entry at
4297 types_ptr. The result is a pointer to one past the end of the header. */
4298
4299 static const gdb_byte *
4300 read_and_check_type_unit_head (struct comp_unit_head *header,
4301 struct dwarf2_section_info *section,
4302 struct dwarf2_section_info *abbrev_section,
4303 const gdb_byte *info_ptr,
4304 ULONGEST *signature,
4305 cu_offset *type_offset_in_tu)
4306 {
4307 const gdb_byte *beg_of_comp_unit = info_ptr;
4308 bfd *abfd = get_section_bfd_owner (section);
4309
4310 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4311
4312 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4313
4314 /* If we're reading a type unit, skip over the signature and
4315 type_offset fields. */
4316 if (signature != NULL)
4317 *signature = read_8_bytes (abfd, info_ptr);
4318 info_ptr += 8;
4319 if (type_offset_in_tu != NULL)
4320 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4321 header->offset_size);
4322 info_ptr += header->offset_size;
4323
4324 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4325
4326 error_check_comp_unit_head (header, section, abbrev_section);
4327
4328 return info_ptr;
4329 }
4330
4331 /* Fetch the abbreviation table offset from a comp or type unit header. */
4332
4333 static sect_offset
4334 read_abbrev_offset (struct dwarf2_section_info *section,
4335 sect_offset offset)
4336 {
4337 bfd *abfd = get_section_bfd_owner (section);
4338 const gdb_byte *info_ptr;
4339 unsigned int length, initial_length_size, offset_size;
4340 sect_offset abbrev_offset;
4341
4342 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4343 info_ptr = section->buffer + offset.sect_off;
4344 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4345 offset_size = initial_length_size == 4 ? 4 : 8;
4346 info_ptr += initial_length_size + 2 /*version*/;
4347 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4348 return abbrev_offset;
4349 }
4350
4351 /* Allocate a new partial symtab for file named NAME and mark this new
4352 partial symtab as being an include of PST. */
4353
4354 static void
4355 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4356 struct objfile *objfile)
4357 {
4358 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4359
4360 if (!IS_ABSOLUTE_PATH (subpst->filename))
4361 {
4362 /* It shares objfile->objfile_obstack. */
4363 subpst->dirname = pst->dirname;
4364 }
4365
4366 subpst->section_offsets = pst->section_offsets;
4367 subpst->textlow = 0;
4368 subpst->texthigh = 0;
4369
4370 subpst->dependencies = (struct partial_symtab **)
4371 obstack_alloc (&objfile->objfile_obstack,
4372 sizeof (struct partial_symtab *));
4373 subpst->dependencies[0] = pst;
4374 subpst->number_of_dependencies = 1;
4375
4376 subpst->globals_offset = 0;
4377 subpst->n_global_syms = 0;
4378 subpst->statics_offset = 0;
4379 subpst->n_static_syms = 0;
4380 subpst->symtab = NULL;
4381 subpst->read_symtab = pst->read_symtab;
4382 subpst->readin = 0;
4383
4384 /* No private part is necessary for include psymtabs. This property
4385 can be used to differentiate between such include psymtabs and
4386 the regular ones. */
4387 subpst->read_symtab_private = NULL;
4388 }
4389
4390 /* Read the Line Number Program data and extract the list of files
4391 included by the source file represented by PST. Build an include
4392 partial symtab for each of these included files. */
4393
4394 static void
4395 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4396 struct die_info *die,
4397 struct partial_symtab *pst)
4398 {
4399 struct line_header *lh = NULL;
4400 struct attribute *attr;
4401
4402 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4403 if (attr)
4404 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4405 if (lh == NULL)
4406 return; /* No linetable, so no includes. */
4407
4408 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4409 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4410
4411 free_line_header (lh);
4412 }
4413
4414 static hashval_t
4415 hash_signatured_type (const void *item)
4416 {
4417 const struct signatured_type *sig_type = item;
4418
4419 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4420 return sig_type->signature;
4421 }
4422
4423 static int
4424 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4425 {
4426 const struct signatured_type *lhs = item_lhs;
4427 const struct signatured_type *rhs = item_rhs;
4428
4429 return lhs->signature == rhs->signature;
4430 }
4431
4432 /* Allocate a hash table for signatured types. */
4433
4434 static htab_t
4435 allocate_signatured_type_table (struct objfile *objfile)
4436 {
4437 return htab_create_alloc_ex (41,
4438 hash_signatured_type,
4439 eq_signatured_type,
4440 NULL,
4441 &objfile->objfile_obstack,
4442 hashtab_obstack_allocate,
4443 dummy_obstack_deallocate);
4444 }
4445
4446 /* A helper function to add a signatured type CU to a table. */
4447
4448 static int
4449 add_signatured_type_cu_to_table (void **slot, void *datum)
4450 {
4451 struct signatured_type *sigt = *slot;
4452 struct signatured_type ***datap = datum;
4453
4454 **datap = sigt;
4455 ++*datap;
4456
4457 return 1;
4458 }
4459
4460 /* Create the hash table of all entries in the .debug_types
4461 (or .debug_types.dwo) section(s).
4462 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4463 otherwise it is NULL.
4464
4465 The result is a pointer to the hash table or NULL if there are no types.
4466
4467 Note: This function processes DWO files only, not DWP files. */
4468
4469 static htab_t
4470 create_debug_types_hash_table (struct dwo_file *dwo_file,
4471 VEC (dwarf2_section_info_def) *types)
4472 {
4473 struct objfile *objfile = dwarf2_per_objfile->objfile;
4474 htab_t types_htab = NULL;
4475 int ix;
4476 struct dwarf2_section_info *section;
4477 struct dwarf2_section_info *abbrev_section;
4478
4479 if (VEC_empty (dwarf2_section_info_def, types))
4480 return NULL;
4481
4482 abbrev_section = (dwo_file != NULL
4483 ? &dwo_file->sections.abbrev
4484 : &dwarf2_per_objfile->abbrev);
4485
4486 if (dwarf2_read_debug)
4487 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4488 dwo_file ? ".dwo" : "",
4489 get_section_file_name (abbrev_section));
4490
4491 for (ix = 0;
4492 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4493 ++ix)
4494 {
4495 bfd *abfd;
4496 const gdb_byte *info_ptr, *end_ptr;
4497
4498 dwarf2_read_section (objfile, section);
4499 info_ptr = section->buffer;
4500
4501 if (info_ptr == NULL)
4502 continue;
4503
4504 /* We can't set abfd until now because the section may be empty or
4505 not present, in which case the bfd is unknown. */
4506 abfd = get_section_bfd_owner (section);
4507
4508 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4509 because we don't need to read any dies: the signature is in the
4510 header. */
4511
4512 end_ptr = info_ptr + section->size;
4513 while (info_ptr < end_ptr)
4514 {
4515 sect_offset offset;
4516 cu_offset type_offset_in_tu;
4517 ULONGEST signature;
4518 struct signatured_type *sig_type;
4519 struct dwo_unit *dwo_tu;
4520 void **slot;
4521 const gdb_byte *ptr = info_ptr;
4522 struct comp_unit_head header;
4523 unsigned int length;
4524
4525 offset.sect_off = ptr - section->buffer;
4526
4527 /* We need to read the type's signature in order to build the hash
4528 table, but we don't need anything else just yet. */
4529
4530 ptr = read_and_check_type_unit_head (&header, section,
4531 abbrev_section, ptr,
4532 &signature, &type_offset_in_tu);
4533
4534 length = get_cu_length (&header);
4535
4536 /* Skip dummy type units. */
4537 if (ptr >= info_ptr + length
4538 || peek_abbrev_code (abfd, ptr) == 0)
4539 {
4540 info_ptr += length;
4541 continue;
4542 }
4543
4544 if (types_htab == NULL)
4545 {
4546 if (dwo_file)
4547 types_htab = allocate_dwo_unit_table (objfile);
4548 else
4549 types_htab = allocate_signatured_type_table (objfile);
4550 }
4551
4552 if (dwo_file)
4553 {
4554 sig_type = NULL;
4555 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4556 struct dwo_unit);
4557 dwo_tu->dwo_file = dwo_file;
4558 dwo_tu->signature = signature;
4559 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4560 dwo_tu->section = section;
4561 dwo_tu->offset = offset;
4562 dwo_tu->length = length;
4563 }
4564 else
4565 {
4566 /* N.B.: type_offset is not usable if this type uses a DWO file.
4567 The real type_offset is in the DWO file. */
4568 dwo_tu = NULL;
4569 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4570 struct signatured_type);
4571 sig_type->signature = signature;
4572 sig_type->type_offset_in_tu = type_offset_in_tu;
4573 sig_type->per_cu.objfile = objfile;
4574 sig_type->per_cu.is_debug_types = 1;
4575 sig_type->per_cu.section = section;
4576 sig_type->per_cu.offset = offset;
4577 sig_type->per_cu.length = length;
4578 }
4579
4580 slot = htab_find_slot (types_htab,
4581 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4582 INSERT);
4583 gdb_assert (slot != NULL);
4584 if (*slot != NULL)
4585 {
4586 sect_offset dup_offset;
4587
4588 if (dwo_file)
4589 {
4590 const struct dwo_unit *dup_tu = *slot;
4591
4592 dup_offset = dup_tu->offset;
4593 }
4594 else
4595 {
4596 const struct signatured_type *dup_tu = *slot;
4597
4598 dup_offset = dup_tu->per_cu.offset;
4599 }
4600
4601 complaint (&symfile_complaints,
4602 _("debug type entry at offset 0x%x is duplicate to"
4603 " the entry at offset 0x%x, signature %s"),
4604 offset.sect_off, dup_offset.sect_off,
4605 hex_string (signature));
4606 }
4607 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4608
4609 if (dwarf2_read_debug)
4610 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4611 offset.sect_off,
4612 hex_string (signature));
4613
4614 info_ptr += length;
4615 }
4616 }
4617
4618 return types_htab;
4619 }
4620
4621 /* Create the hash table of all entries in the .debug_types section,
4622 and initialize all_type_units.
4623 The result is zero if there is an error (e.g. missing .debug_types section),
4624 otherwise non-zero. */
4625
4626 static int
4627 create_all_type_units (struct objfile *objfile)
4628 {
4629 htab_t types_htab;
4630 struct signatured_type **iter;
4631
4632 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4633 if (types_htab == NULL)
4634 {
4635 dwarf2_per_objfile->signatured_types = NULL;
4636 return 0;
4637 }
4638
4639 dwarf2_per_objfile->signatured_types = types_htab;
4640
4641 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4642 dwarf2_per_objfile->all_type_units
4643 = xmalloc (dwarf2_per_objfile->n_type_units
4644 * sizeof (struct signatured_type *));
4645 iter = &dwarf2_per_objfile->all_type_units[0];
4646 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4647 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4648 == dwarf2_per_objfile->n_type_units);
4649
4650 return 1;
4651 }
4652
4653 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4654 Fill in SIG_ENTRY with DWO_ENTRY. */
4655
4656 static void
4657 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4658 struct signatured_type *sig_entry,
4659 struct dwo_unit *dwo_entry)
4660 {
4661 /* Make sure we're not clobbering something we don't expect to. */
4662 gdb_assert (! sig_entry->per_cu.queued);
4663 gdb_assert (sig_entry->per_cu.cu == NULL);
4664 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4665 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4666 gdb_assert (sig_entry->signature == dwo_entry->signature);
4667 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4668 gdb_assert (sig_entry->type_unit_group == NULL);
4669 gdb_assert (sig_entry->dwo_unit == NULL);
4670
4671 sig_entry->per_cu.section = dwo_entry->section;
4672 sig_entry->per_cu.offset = dwo_entry->offset;
4673 sig_entry->per_cu.length = dwo_entry->length;
4674 sig_entry->per_cu.reading_dwo_directly = 1;
4675 sig_entry->per_cu.objfile = objfile;
4676 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4677 sig_entry->dwo_unit = dwo_entry;
4678 }
4679
4680 /* Subroutine of lookup_signatured_type.
4681 If we haven't read the TU yet, create the signatured_type data structure
4682 for a TU to be read in directly from a DWO file, bypassing the stub.
4683 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4684 using .gdb_index, then when reading a CU we want to stay in the DWO file
4685 containing that CU. Otherwise we could end up reading several other DWO
4686 files (due to comdat folding) to process the transitive closure of all the
4687 mentioned TUs, and that can be slow. The current DWO file will have every
4688 type signature that it needs.
4689 We only do this for .gdb_index because in the psymtab case we already have
4690 to read all the DWOs to build the type unit groups. */
4691
4692 static struct signatured_type *
4693 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4694 {
4695 struct objfile *objfile = dwarf2_per_objfile->objfile;
4696 struct dwo_file *dwo_file;
4697 struct dwo_unit find_dwo_entry, *dwo_entry;
4698 struct signatured_type find_sig_entry, *sig_entry;
4699
4700 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4701
4702 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4703 dwo_unit of the TU itself. */
4704 dwo_file = cu->dwo_unit->dwo_file;
4705
4706 /* We only ever need to read in one copy of a signatured type.
4707 Just use the global signatured_types array. If this is the first time
4708 we're reading this type, replace the recorded data from .gdb_index with
4709 this TU. */
4710
4711 if (dwarf2_per_objfile->signatured_types == NULL)
4712 return NULL;
4713 find_sig_entry.signature = sig;
4714 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4715 if (sig_entry == NULL)
4716 return NULL;
4717
4718 /* We can get here with the TU already read, *or* in the process of being
4719 read. Don't reassign it if that's the case. Also note that if the TU is
4720 already being read, it may not have come from a DWO, the program may be
4721 a mix of Fission-compiled code and non-Fission-compiled code. */
4722 /* Have we already tried to read this TU? */
4723 if (sig_entry->per_cu.tu_read)
4724 return sig_entry;
4725
4726 /* Ok, this is the first time we're reading this TU. */
4727 if (dwo_file->tus == NULL)
4728 return NULL;
4729 find_dwo_entry.signature = sig;
4730 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4731 if (dwo_entry == NULL)
4732 return NULL;
4733
4734 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4735 sig_entry->per_cu.tu_read = 1;
4736 return sig_entry;
4737 }
4738
4739 /* Subroutine of lookup_dwp_signatured_type.
4740 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4741
4742 static struct signatured_type *
4743 add_type_unit (ULONGEST sig)
4744 {
4745 struct objfile *objfile = dwarf2_per_objfile->objfile;
4746 int n_type_units = dwarf2_per_objfile->n_type_units;
4747 struct signatured_type *sig_type;
4748 void **slot;
4749
4750 ++n_type_units;
4751 dwarf2_per_objfile->all_type_units =
4752 xrealloc (dwarf2_per_objfile->all_type_units,
4753 n_type_units * sizeof (struct signatured_type *));
4754 dwarf2_per_objfile->n_type_units = n_type_units;
4755 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4756 struct signatured_type);
4757 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4758 sig_type->signature = sig;
4759 sig_type->per_cu.is_debug_types = 1;
4760 sig_type->per_cu.v.quick =
4761 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4762 struct dwarf2_per_cu_quick_data);
4763 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4764 sig_type, INSERT);
4765 gdb_assert (*slot == NULL);
4766 *slot = sig_type;
4767 /* The rest of sig_type must be filled in by the caller. */
4768 return sig_type;
4769 }
4770
4771 /* Subroutine of lookup_signatured_type.
4772 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4773 then try the DWP file.
4774 Normally this "can't happen", but if there's a bug in signature
4775 generation and/or the DWP file is built incorrectly, it can happen.
4776 Using the type directly from the DWP file means we don't have the stub
4777 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4778 not critical. [Eventually the stub may go away for type units anyway.] */
4779
4780 static struct signatured_type *
4781 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4782 {
4783 struct objfile *objfile = dwarf2_per_objfile->objfile;
4784 struct dwp_file *dwp_file = get_dwp_file ();
4785 struct dwo_unit *dwo_entry;
4786 struct signatured_type find_sig_entry, *sig_entry;
4787
4788 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4789 gdb_assert (dwp_file != NULL);
4790
4791 if (dwarf2_per_objfile->signatured_types != NULL)
4792 {
4793 find_sig_entry.signature = sig;
4794 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4795 &find_sig_entry);
4796 if (sig_entry != NULL)
4797 return sig_entry;
4798 }
4799
4800 /* This is the "shouldn't happen" case.
4801 Try the DWP file and hope for the best. */
4802 if (dwp_file->tus == NULL)
4803 return NULL;
4804 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4805 sig, 1 /* is_debug_types */);
4806 if (dwo_entry == NULL)
4807 return NULL;
4808
4809 sig_entry = add_type_unit (sig);
4810 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4811
4812 /* The caller will signal a complaint if we return NULL.
4813 Here we don't return NULL but we still want to complain. */
4814 complaint (&symfile_complaints,
4815 _("Bad type signature %s referenced by %s at 0x%x,"
4816 " coping by using copy in DWP [in module %s]"),
4817 hex_string (sig),
4818 cu->per_cu->is_debug_types ? "TU" : "CU",
4819 cu->per_cu->offset.sect_off,
4820 objfile_name (objfile));
4821
4822 return sig_entry;
4823 }
4824
4825 /* Lookup a signature based type for DW_FORM_ref_sig8.
4826 Returns NULL if signature SIG is not present in the table.
4827 It is up to the caller to complain about this. */
4828
4829 static struct signatured_type *
4830 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4831 {
4832 if (cu->dwo_unit
4833 && dwarf2_per_objfile->using_index)
4834 {
4835 /* We're in a DWO/DWP file, and we're using .gdb_index.
4836 These cases require special processing. */
4837 if (get_dwp_file () == NULL)
4838 return lookup_dwo_signatured_type (cu, sig);
4839 else
4840 return lookup_dwp_signatured_type (cu, sig);
4841 }
4842 else
4843 {
4844 struct signatured_type find_entry, *entry;
4845
4846 if (dwarf2_per_objfile->signatured_types == NULL)
4847 return NULL;
4848 find_entry.signature = sig;
4849 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4850 return entry;
4851 }
4852 }
4853 \f
4854 /* Low level DIE reading support. */
4855
4856 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4857
4858 static void
4859 init_cu_die_reader (struct die_reader_specs *reader,
4860 struct dwarf2_cu *cu,
4861 struct dwarf2_section_info *section,
4862 struct dwo_file *dwo_file)
4863 {
4864 gdb_assert (section->readin && section->buffer != NULL);
4865 reader->abfd = get_section_bfd_owner (section);
4866 reader->cu = cu;
4867 reader->dwo_file = dwo_file;
4868 reader->die_section = section;
4869 reader->buffer = section->buffer;
4870 reader->buffer_end = section->buffer + section->size;
4871 reader->comp_dir = NULL;
4872 }
4873
4874 /* Subroutine of init_cutu_and_read_dies to simplify it.
4875 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4876 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4877 already.
4878
4879 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4880 from it to the DIE in the DWO. If NULL we are skipping the stub.
4881 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4882 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4883 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4884 COMP_DIR must be non-NULL.
4885 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4886 are filled in with the info of the DIE from the DWO file.
4887 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4888 provided an abbrev table to use.
4889 The result is non-zero if a valid (non-dummy) DIE was found. */
4890
4891 static int
4892 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4893 struct dwo_unit *dwo_unit,
4894 int abbrev_table_provided,
4895 struct die_info *stub_comp_unit_die,
4896 const char *stub_comp_dir,
4897 struct die_reader_specs *result_reader,
4898 const gdb_byte **result_info_ptr,
4899 struct die_info **result_comp_unit_die,
4900 int *result_has_children)
4901 {
4902 struct objfile *objfile = dwarf2_per_objfile->objfile;
4903 struct dwarf2_cu *cu = this_cu->cu;
4904 struct dwarf2_section_info *section;
4905 bfd *abfd;
4906 const gdb_byte *begin_info_ptr, *info_ptr;
4907 const char *comp_dir_string;
4908 ULONGEST signature; /* Or dwo_id. */
4909 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4910 int i,num_extra_attrs;
4911 struct dwarf2_section_info *dwo_abbrev_section;
4912 struct attribute *attr;
4913 struct attribute comp_dir_attr;
4914 struct die_info *comp_unit_die;
4915
4916 /* Both can't be provided. */
4917 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4918
4919 /* These attributes aren't processed until later:
4920 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4921 However, the attribute is found in the stub which we won't have later.
4922 In order to not impose this complication on the rest of the code,
4923 we read them here and copy them to the DWO CU/TU die. */
4924
4925 stmt_list = NULL;
4926 low_pc = NULL;
4927 high_pc = NULL;
4928 ranges = NULL;
4929 comp_dir = NULL;
4930
4931 if (stub_comp_unit_die != NULL)
4932 {
4933 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4934 DWO file. */
4935 if (! this_cu->is_debug_types)
4936 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4937 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4938 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4939 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4940 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4941
4942 /* There should be a DW_AT_addr_base attribute here (if needed).
4943 We need the value before we can process DW_FORM_GNU_addr_index. */
4944 cu->addr_base = 0;
4945 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4946 if (attr)
4947 cu->addr_base = DW_UNSND (attr);
4948
4949 /* There should be a DW_AT_ranges_base attribute here (if needed).
4950 We need the value before we can process DW_AT_ranges. */
4951 cu->ranges_base = 0;
4952 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4953 if (attr)
4954 cu->ranges_base = DW_UNSND (attr);
4955 }
4956 else if (stub_comp_dir != NULL)
4957 {
4958 /* Reconstruct the comp_dir attribute to simplify the code below. */
4959 comp_dir = (struct attribute *)
4960 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4961 comp_dir->name = DW_AT_comp_dir;
4962 comp_dir->form = DW_FORM_string;
4963 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4964 DW_STRING (comp_dir) = stub_comp_dir;
4965 }
4966
4967 /* Set up for reading the DWO CU/TU. */
4968 cu->dwo_unit = dwo_unit;
4969 section = dwo_unit->section;
4970 dwarf2_read_section (objfile, section);
4971 abfd = get_section_bfd_owner (section);
4972 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4973 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4974 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4975
4976 if (this_cu->is_debug_types)
4977 {
4978 ULONGEST header_signature;
4979 cu_offset type_offset_in_tu;
4980 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4981
4982 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4983 dwo_abbrev_section,
4984 info_ptr,
4985 &header_signature,
4986 &type_offset_in_tu);
4987 /* This is not an assert because it can be caused by bad debug info. */
4988 if (sig_type->signature != header_signature)
4989 {
4990 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4991 " TU at offset 0x%x [in module %s]"),
4992 hex_string (sig_type->signature),
4993 hex_string (header_signature),
4994 dwo_unit->offset.sect_off,
4995 bfd_get_filename (abfd));
4996 }
4997 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4998 /* For DWOs coming from DWP files, we don't know the CU length
4999 nor the type's offset in the TU until now. */
5000 dwo_unit->length = get_cu_length (&cu->header);
5001 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5002
5003 /* Establish the type offset that can be used to lookup the type.
5004 For DWO files, we don't know it until now. */
5005 sig_type->type_offset_in_section.sect_off =
5006 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5007 }
5008 else
5009 {
5010 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5011 dwo_abbrev_section,
5012 info_ptr, 0);
5013 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5014 /* For DWOs coming from DWP files, we don't know the CU length
5015 until now. */
5016 dwo_unit->length = get_cu_length (&cu->header);
5017 }
5018
5019 /* Replace the CU's original abbrev table with the DWO's.
5020 Reminder: We can't read the abbrev table until we've read the header. */
5021 if (abbrev_table_provided)
5022 {
5023 /* Don't free the provided abbrev table, the caller of
5024 init_cutu_and_read_dies owns it. */
5025 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5026 /* Ensure the DWO abbrev table gets freed. */
5027 make_cleanup (dwarf2_free_abbrev_table, cu);
5028 }
5029 else
5030 {
5031 dwarf2_free_abbrev_table (cu);
5032 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5033 /* Leave any existing abbrev table cleanup as is. */
5034 }
5035
5036 /* Read in the die, but leave space to copy over the attributes
5037 from the stub. This has the benefit of simplifying the rest of
5038 the code - all the work to maintain the illusion of a single
5039 DW_TAG_{compile,type}_unit DIE is done here. */
5040 num_extra_attrs = ((stmt_list != NULL)
5041 + (low_pc != NULL)
5042 + (high_pc != NULL)
5043 + (ranges != NULL)
5044 + (comp_dir != NULL));
5045 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5046 result_has_children, num_extra_attrs);
5047
5048 /* Copy over the attributes from the stub to the DIE we just read in. */
5049 comp_unit_die = *result_comp_unit_die;
5050 i = comp_unit_die->num_attrs;
5051 if (stmt_list != NULL)
5052 comp_unit_die->attrs[i++] = *stmt_list;
5053 if (low_pc != NULL)
5054 comp_unit_die->attrs[i++] = *low_pc;
5055 if (high_pc != NULL)
5056 comp_unit_die->attrs[i++] = *high_pc;
5057 if (ranges != NULL)
5058 comp_unit_die->attrs[i++] = *ranges;
5059 if (comp_dir != NULL)
5060 comp_unit_die->attrs[i++] = *comp_dir;
5061 comp_unit_die->num_attrs += num_extra_attrs;
5062
5063 if (dwarf2_die_debug)
5064 {
5065 fprintf_unfiltered (gdb_stdlog,
5066 "Read die from %s@0x%x of %s:\n",
5067 get_section_name (section),
5068 (unsigned) (begin_info_ptr - section->buffer),
5069 bfd_get_filename (abfd));
5070 dump_die (comp_unit_die, dwarf2_die_debug);
5071 }
5072
5073 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5074 TUs by skipping the stub and going directly to the entry in the DWO file.
5075 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5076 to get it via circuitous means. Blech. */
5077 if (comp_dir != NULL)
5078 result_reader->comp_dir = DW_STRING (comp_dir);
5079
5080 /* Skip dummy compilation units. */
5081 if (info_ptr >= begin_info_ptr + dwo_unit->length
5082 || peek_abbrev_code (abfd, info_ptr) == 0)
5083 return 0;
5084
5085 *result_info_ptr = info_ptr;
5086 return 1;
5087 }
5088
5089 /* Subroutine of init_cutu_and_read_dies to simplify it.
5090 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5091 Returns NULL if the specified DWO unit cannot be found. */
5092
5093 static struct dwo_unit *
5094 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5095 struct die_info *comp_unit_die)
5096 {
5097 struct dwarf2_cu *cu = this_cu->cu;
5098 struct attribute *attr;
5099 ULONGEST signature;
5100 struct dwo_unit *dwo_unit;
5101 const char *comp_dir, *dwo_name;
5102
5103 gdb_assert (cu != NULL);
5104
5105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5106 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5107 gdb_assert (attr != NULL);
5108 dwo_name = DW_STRING (attr);
5109 comp_dir = NULL;
5110 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5111 if (attr)
5112 comp_dir = DW_STRING (attr);
5113
5114 if (this_cu->is_debug_types)
5115 {
5116 struct signatured_type *sig_type;
5117
5118 /* Since this_cu is the first member of struct signatured_type,
5119 we can go from a pointer to one to a pointer to the other. */
5120 sig_type = (struct signatured_type *) this_cu;
5121 signature = sig_type->signature;
5122 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5123 }
5124 else
5125 {
5126 struct attribute *attr;
5127
5128 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5129 if (! attr)
5130 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5131 " [in module %s]"),
5132 dwo_name, objfile_name (this_cu->objfile));
5133 signature = DW_UNSND (attr);
5134 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5135 signature);
5136 }
5137
5138 return dwo_unit;
5139 }
5140
5141 /* Subroutine of init_cutu_and_read_dies to simplify it.
5142 Read a TU directly from a DWO file, bypassing the stub. */
5143
5144 static void
5145 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
5146 die_reader_func_ftype *die_reader_func,
5147 void *data)
5148 {
5149 struct dwarf2_cu *cu;
5150 struct signatured_type *sig_type;
5151 struct cleanup *cleanups, *free_cu_cleanup;
5152 struct die_reader_specs reader;
5153 const gdb_byte *info_ptr;
5154 struct die_info *comp_unit_die;
5155 int has_children;
5156
5157 /* Verify we can do the following downcast, and that we have the
5158 data we need. */
5159 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5160 sig_type = (struct signatured_type *) this_cu;
5161 gdb_assert (sig_type->dwo_unit != NULL);
5162
5163 cleanups = make_cleanup (null_cleanup, NULL);
5164
5165 gdb_assert (this_cu->cu == NULL);
5166 cu = xmalloc (sizeof (*cu));
5167 init_one_comp_unit (cu, this_cu);
5168 /* If an error occurs while loading, release our storage. */
5169 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5170
5171 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5172 0 /* abbrev_table_provided */,
5173 NULL /* stub_comp_unit_die */,
5174 sig_type->dwo_unit->dwo_file->comp_dir,
5175 &reader, &info_ptr,
5176 &comp_unit_die, &has_children) == 0)
5177 {
5178 /* Dummy die. */
5179 do_cleanups (cleanups);
5180 return;
5181 }
5182
5183 /* All the "real" work is done here. */
5184 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5185
5186 /* This duplicates some code in init_cutu_and_read_dies,
5187 but the alternative is making the latter more complex.
5188 This function is only for the special case of using DWO files directly:
5189 no point in overly complicating the general case just to handle this. */
5190 if (keep)
5191 {
5192 /* We've successfully allocated this compilation unit. Let our
5193 caller clean it up when finished with it. */
5194 discard_cleanups (free_cu_cleanup);
5195
5196 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5197 So we have to manually free the abbrev table. */
5198 dwarf2_free_abbrev_table (cu);
5199
5200 /* Link this CU into read_in_chain. */
5201 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5202 dwarf2_per_objfile->read_in_chain = this_cu;
5203 }
5204 else
5205 do_cleanups (free_cu_cleanup);
5206
5207 do_cleanups (cleanups);
5208 }
5209
5210 /* Initialize a CU (or TU) and read its DIEs.
5211 If the CU defers to a DWO file, read the DWO file as well.
5212
5213 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5214 Otherwise the table specified in the comp unit header is read in and used.
5215 This is an optimization for when we already have the abbrev table.
5216
5217 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5218 Otherwise, a new CU is allocated with xmalloc.
5219
5220 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5221 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5222
5223 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5224 linker) then DIE_READER_FUNC will not get called. */
5225
5226 static void
5227 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5228 struct abbrev_table *abbrev_table,
5229 int use_existing_cu, int keep,
5230 die_reader_func_ftype *die_reader_func,
5231 void *data)
5232 {
5233 struct objfile *objfile = dwarf2_per_objfile->objfile;
5234 struct dwarf2_section_info *section = this_cu->section;
5235 bfd *abfd = get_section_bfd_owner (section);
5236 struct dwarf2_cu *cu;
5237 const gdb_byte *begin_info_ptr, *info_ptr;
5238 struct die_reader_specs reader;
5239 struct die_info *comp_unit_die;
5240 int has_children;
5241 struct attribute *attr;
5242 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5243 struct signatured_type *sig_type = NULL;
5244 struct dwarf2_section_info *abbrev_section;
5245 /* Non-zero if CU currently points to a DWO file and we need to
5246 reread it. When this happens we need to reread the skeleton die
5247 before we can reread the DWO file (this only applies to CUs, not TUs). */
5248 int rereading_dwo_cu = 0;
5249
5250 if (dwarf2_die_debug)
5251 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5252 this_cu->is_debug_types ? "type" : "comp",
5253 this_cu->offset.sect_off);
5254
5255 if (use_existing_cu)
5256 gdb_assert (keep);
5257
5258 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5259 file (instead of going through the stub), short-circuit all of this. */
5260 if (this_cu->reading_dwo_directly)
5261 {
5262 /* Narrow down the scope of possibilities to have to understand. */
5263 gdb_assert (this_cu->is_debug_types);
5264 gdb_assert (abbrev_table == NULL);
5265 gdb_assert (!use_existing_cu);
5266 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5267 return;
5268 }
5269
5270 cleanups = make_cleanup (null_cleanup, NULL);
5271
5272 /* This is cheap if the section is already read in. */
5273 dwarf2_read_section (objfile, section);
5274
5275 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5276
5277 abbrev_section = get_abbrev_section_for_cu (this_cu);
5278
5279 if (use_existing_cu && this_cu->cu != NULL)
5280 {
5281 cu = this_cu->cu;
5282
5283 /* If this CU is from a DWO file we need to start over, we need to
5284 refetch the attributes from the skeleton CU.
5285 This could be optimized by retrieving those attributes from when we
5286 were here the first time: the previous comp_unit_die was stored in
5287 comp_unit_obstack. But there's no data yet that we need this
5288 optimization. */
5289 if (cu->dwo_unit != NULL)
5290 rereading_dwo_cu = 1;
5291 }
5292 else
5293 {
5294 /* If !use_existing_cu, this_cu->cu must be NULL. */
5295 gdb_assert (this_cu->cu == NULL);
5296
5297 cu = xmalloc (sizeof (*cu));
5298 init_one_comp_unit (cu, this_cu);
5299
5300 /* If an error occurs while loading, release our storage. */
5301 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5302 }
5303
5304 /* Get the header. */
5305 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5306 {
5307 /* We already have the header, there's no need to read it in again. */
5308 info_ptr += cu->header.first_die_offset.cu_off;
5309 }
5310 else
5311 {
5312 if (this_cu->is_debug_types)
5313 {
5314 ULONGEST signature;
5315 cu_offset type_offset_in_tu;
5316
5317 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5318 abbrev_section, info_ptr,
5319 &signature,
5320 &type_offset_in_tu);
5321
5322 /* Since per_cu is the first member of struct signatured_type,
5323 we can go from a pointer to one to a pointer to the other. */
5324 sig_type = (struct signatured_type *) this_cu;
5325 gdb_assert (sig_type->signature == signature);
5326 gdb_assert (sig_type->type_offset_in_tu.cu_off
5327 == type_offset_in_tu.cu_off);
5328 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5329
5330 /* LENGTH has not been set yet for type units if we're
5331 using .gdb_index. */
5332 this_cu->length = get_cu_length (&cu->header);
5333
5334 /* Establish the type offset that can be used to lookup the type. */
5335 sig_type->type_offset_in_section.sect_off =
5336 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5337 }
5338 else
5339 {
5340 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5341 abbrev_section,
5342 info_ptr, 0);
5343
5344 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5345 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5346 }
5347 }
5348
5349 /* Skip dummy compilation units. */
5350 if (info_ptr >= begin_info_ptr + this_cu->length
5351 || peek_abbrev_code (abfd, info_ptr) == 0)
5352 {
5353 do_cleanups (cleanups);
5354 return;
5355 }
5356
5357 /* If we don't have them yet, read the abbrevs for this compilation unit.
5358 And if we need to read them now, make sure they're freed when we're
5359 done. Note that it's important that if the CU had an abbrev table
5360 on entry we don't free it when we're done: Somewhere up the call stack
5361 it may be in use. */
5362 if (abbrev_table != NULL)
5363 {
5364 gdb_assert (cu->abbrev_table == NULL);
5365 gdb_assert (cu->header.abbrev_offset.sect_off
5366 == abbrev_table->offset.sect_off);
5367 cu->abbrev_table = abbrev_table;
5368 }
5369 else if (cu->abbrev_table == NULL)
5370 {
5371 dwarf2_read_abbrevs (cu, abbrev_section);
5372 make_cleanup (dwarf2_free_abbrev_table, cu);
5373 }
5374 else if (rereading_dwo_cu)
5375 {
5376 dwarf2_free_abbrev_table (cu);
5377 dwarf2_read_abbrevs (cu, abbrev_section);
5378 }
5379
5380 /* Read the top level CU/TU die. */
5381 init_cu_die_reader (&reader, cu, section, NULL);
5382 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5383
5384 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5385 from the DWO file.
5386 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5387 DWO CU, that this test will fail (the attribute will not be present). */
5388 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5389 if (attr)
5390 {
5391 struct dwo_unit *dwo_unit;
5392 struct die_info *dwo_comp_unit_die;
5393
5394 if (has_children)
5395 {
5396 complaint (&symfile_complaints,
5397 _("compilation unit with DW_AT_GNU_dwo_name"
5398 " has children (offset 0x%x) [in module %s]"),
5399 this_cu->offset.sect_off, bfd_get_filename (abfd));
5400 }
5401 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5402 if (dwo_unit != NULL)
5403 {
5404 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5405 abbrev_table != NULL,
5406 comp_unit_die, NULL,
5407 &reader, &info_ptr,
5408 &dwo_comp_unit_die, &has_children) == 0)
5409 {
5410 /* Dummy die. */
5411 do_cleanups (cleanups);
5412 return;
5413 }
5414 comp_unit_die = dwo_comp_unit_die;
5415 }
5416 else
5417 {
5418 /* Yikes, we couldn't find the rest of the DIE, we only have
5419 the stub. A complaint has already been logged. There's
5420 not much more we can do except pass on the stub DIE to
5421 die_reader_func. We don't want to throw an error on bad
5422 debug info. */
5423 }
5424 }
5425
5426 /* All of the above is setup for this call. Yikes. */
5427 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5428
5429 /* Done, clean up. */
5430 if (free_cu_cleanup != NULL)
5431 {
5432 if (keep)
5433 {
5434 /* We've successfully allocated this compilation unit. Let our
5435 caller clean it up when finished with it. */
5436 discard_cleanups (free_cu_cleanup);
5437
5438 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5439 So we have to manually free the abbrev table. */
5440 dwarf2_free_abbrev_table (cu);
5441
5442 /* Link this CU into read_in_chain. */
5443 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5444 dwarf2_per_objfile->read_in_chain = this_cu;
5445 }
5446 else
5447 do_cleanups (free_cu_cleanup);
5448 }
5449
5450 do_cleanups (cleanups);
5451 }
5452
5453 /* Read CU/TU THIS_CU in section SECTION,
5454 but do not follow DW_AT_GNU_dwo_name if present.
5455 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5456 to have already done the lookup to find the DWO/DWP file).
5457
5458 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5459 THIS_CU->is_debug_types, but nothing else.
5460
5461 We fill in THIS_CU->length.
5462
5463 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5464 linker) then DIE_READER_FUNC will not get called.
5465
5466 THIS_CU->cu is always freed when done.
5467 This is done in order to not leave THIS_CU->cu in a state where we have
5468 to care whether it refers to the "main" CU or the DWO CU. */
5469
5470 static void
5471 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5472 struct dwarf2_section_info *abbrev_section,
5473 struct dwo_file *dwo_file,
5474 die_reader_func_ftype *die_reader_func,
5475 void *data)
5476 {
5477 struct objfile *objfile = dwarf2_per_objfile->objfile;
5478 struct dwarf2_section_info *section = this_cu->section;
5479 bfd *abfd = get_section_bfd_owner (section);
5480 struct dwarf2_cu cu;
5481 const gdb_byte *begin_info_ptr, *info_ptr;
5482 struct die_reader_specs reader;
5483 struct cleanup *cleanups;
5484 struct die_info *comp_unit_die;
5485 int has_children;
5486
5487 if (dwarf2_die_debug)
5488 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5489 this_cu->is_debug_types ? "type" : "comp",
5490 this_cu->offset.sect_off);
5491
5492 gdb_assert (this_cu->cu == NULL);
5493
5494 /* This is cheap if the section is already read in. */
5495 dwarf2_read_section (objfile, section);
5496
5497 init_one_comp_unit (&cu, this_cu);
5498
5499 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5500
5501 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5502 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5503 abbrev_section, info_ptr,
5504 this_cu->is_debug_types);
5505
5506 this_cu->length = get_cu_length (&cu.header);
5507
5508 /* Skip dummy compilation units. */
5509 if (info_ptr >= begin_info_ptr + this_cu->length
5510 || peek_abbrev_code (abfd, info_ptr) == 0)
5511 {
5512 do_cleanups (cleanups);
5513 return;
5514 }
5515
5516 dwarf2_read_abbrevs (&cu, abbrev_section);
5517 make_cleanup (dwarf2_free_abbrev_table, &cu);
5518
5519 init_cu_die_reader (&reader, &cu, section, dwo_file);
5520 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5521
5522 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5523
5524 do_cleanups (cleanups);
5525 }
5526
5527 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5528 does not lookup the specified DWO file.
5529 This cannot be used to read DWO files.
5530
5531 THIS_CU->cu is always freed when done.
5532 This is done in order to not leave THIS_CU->cu in a state where we have
5533 to care whether it refers to the "main" CU or the DWO CU.
5534 We can revisit this if the data shows there's a performance issue. */
5535
5536 static void
5537 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5538 die_reader_func_ftype *die_reader_func,
5539 void *data)
5540 {
5541 init_cutu_and_read_dies_no_follow (this_cu,
5542 get_abbrev_section_for_cu (this_cu),
5543 NULL,
5544 die_reader_func, data);
5545 }
5546 \f
5547 /* Type Unit Groups.
5548
5549 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5550 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5551 so that all types coming from the same compilation (.o file) are grouped
5552 together. A future step could be to put the types in the same symtab as
5553 the CU the types ultimately came from. */
5554
5555 static hashval_t
5556 hash_type_unit_group (const void *item)
5557 {
5558 const struct type_unit_group *tu_group = item;
5559
5560 return hash_stmt_list_entry (&tu_group->hash);
5561 }
5562
5563 static int
5564 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5565 {
5566 const struct type_unit_group *lhs = item_lhs;
5567 const struct type_unit_group *rhs = item_rhs;
5568
5569 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5570 }
5571
5572 /* Allocate a hash table for type unit groups. */
5573
5574 static htab_t
5575 allocate_type_unit_groups_table (void)
5576 {
5577 return htab_create_alloc_ex (3,
5578 hash_type_unit_group,
5579 eq_type_unit_group,
5580 NULL,
5581 &dwarf2_per_objfile->objfile->objfile_obstack,
5582 hashtab_obstack_allocate,
5583 dummy_obstack_deallocate);
5584 }
5585
5586 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5587 partial symtabs. We combine several TUs per psymtab to not let the size
5588 of any one psymtab grow too big. */
5589 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5590 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5591
5592 /* Helper routine for get_type_unit_group.
5593 Create the type_unit_group object used to hold one or more TUs. */
5594
5595 static struct type_unit_group *
5596 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5597 {
5598 struct objfile *objfile = dwarf2_per_objfile->objfile;
5599 struct dwarf2_per_cu_data *per_cu;
5600 struct type_unit_group *tu_group;
5601
5602 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5603 struct type_unit_group);
5604 per_cu = &tu_group->per_cu;
5605 per_cu->objfile = objfile;
5606
5607 if (dwarf2_per_objfile->using_index)
5608 {
5609 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5610 struct dwarf2_per_cu_quick_data);
5611 }
5612 else
5613 {
5614 unsigned int line_offset = line_offset_struct.sect_off;
5615 struct partial_symtab *pst;
5616 char *name;
5617
5618 /* Give the symtab a useful name for debug purposes. */
5619 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5620 name = xstrprintf ("<type_units_%d>",
5621 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5622 else
5623 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5624
5625 pst = create_partial_symtab (per_cu, name);
5626 pst->anonymous = 1;
5627
5628 xfree (name);
5629 }
5630
5631 tu_group->hash.dwo_unit = cu->dwo_unit;
5632 tu_group->hash.line_offset = line_offset_struct;
5633
5634 return tu_group;
5635 }
5636
5637 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5638 STMT_LIST is a DW_AT_stmt_list attribute. */
5639
5640 static struct type_unit_group *
5641 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5642 {
5643 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5644 struct type_unit_group *tu_group;
5645 void **slot;
5646 unsigned int line_offset;
5647 struct type_unit_group type_unit_group_for_lookup;
5648
5649 if (dwarf2_per_objfile->type_unit_groups == NULL)
5650 {
5651 dwarf2_per_objfile->type_unit_groups =
5652 allocate_type_unit_groups_table ();
5653 }
5654
5655 /* Do we need to create a new group, or can we use an existing one? */
5656
5657 if (stmt_list)
5658 {
5659 line_offset = DW_UNSND (stmt_list);
5660 ++tu_stats->nr_symtab_sharers;
5661 }
5662 else
5663 {
5664 /* Ugh, no stmt_list. Rare, but we have to handle it.
5665 We can do various things here like create one group per TU or
5666 spread them over multiple groups to split up the expansion work.
5667 To avoid worst case scenarios (too many groups or too large groups)
5668 we, umm, group them in bunches. */
5669 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5670 | (tu_stats->nr_stmt_less_type_units
5671 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5672 ++tu_stats->nr_stmt_less_type_units;
5673 }
5674
5675 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5676 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5677 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5678 &type_unit_group_for_lookup, INSERT);
5679 if (*slot != NULL)
5680 {
5681 tu_group = *slot;
5682 gdb_assert (tu_group != NULL);
5683 }
5684 else
5685 {
5686 sect_offset line_offset_struct;
5687
5688 line_offset_struct.sect_off = line_offset;
5689 tu_group = create_type_unit_group (cu, line_offset_struct);
5690 *slot = tu_group;
5691 ++tu_stats->nr_symtabs;
5692 }
5693
5694 return tu_group;
5695 }
5696
5697 /* Struct used to sort TUs by their abbreviation table offset. */
5698
5699 struct tu_abbrev_offset
5700 {
5701 struct signatured_type *sig_type;
5702 sect_offset abbrev_offset;
5703 };
5704
5705 /* Helper routine for build_type_unit_groups, passed to qsort. */
5706
5707 static int
5708 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5709 {
5710 const struct tu_abbrev_offset * const *a = ap;
5711 const struct tu_abbrev_offset * const *b = bp;
5712 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5713 unsigned int boff = (*b)->abbrev_offset.sect_off;
5714
5715 return (aoff > boff) - (aoff < boff);
5716 }
5717
5718 /* A helper function to add a type_unit_group to a table. */
5719
5720 static int
5721 add_type_unit_group_to_table (void **slot, void *datum)
5722 {
5723 struct type_unit_group *tu_group = *slot;
5724 struct type_unit_group ***datap = datum;
5725
5726 **datap = tu_group;
5727 ++*datap;
5728
5729 return 1;
5730 }
5731
5732 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5733 each one passing FUNC,DATA.
5734
5735 The efficiency is because we sort TUs by the abbrev table they use and
5736 only read each abbrev table once. In one program there are 200K TUs
5737 sharing 8K abbrev tables.
5738
5739 The main purpose of this function is to support building the
5740 dwarf2_per_objfile->type_unit_groups table.
5741 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5742 can collapse the search space by grouping them by stmt_list.
5743 The savings can be significant, in the same program from above the 200K TUs
5744 share 8K stmt_list tables.
5745
5746 FUNC is expected to call get_type_unit_group, which will create the
5747 struct type_unit_group if necessary and add it to
5748 dwarf2_per_objfile->type_unit_groups. */
5749
5750 static void
5751 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5752 {
5753 struct objfile *objfile = dwarf2_per_objfile->objfile;
5754 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5755 struct cleanup *cleanups;
5756 struct abbrev_table *abbrev_table;
5757 sect_offset abbrev_offset;
5758 struct tu_abbrev_offset *sorted_by_abbrev;
5759 struct type_unit_group **iter;
5760 int i;
5761
5762 /* It's up to the caller to not call us multiple times. */
5763 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5764
5765 if (dwarf2_per_objfile->n_type_units == 0)
5766 return;
5767
5768 /* TUs typically share abbrev tables, and there can be way more TUs than
5769 abbrev tables. Sort by abbrev table to reduce the number of times we
5770 read each abbrev table in.
5771 Alternatives are to punt or to maintain a cache of abbrev tables.
5772 This is simpler and efficient enough for now.
5773
5774 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5775 symtab to use). Typically TUs with the same abbrev offset have the same
5776 stmt_list value too so in practice this should work well.
5777
5778 The basic algorithm here is:
5779
5780 sort TUs by abbrev table
5781 for each TU with same abbrev table:
5782 read abbrev table if first user
5783 read TU top level DIE
5784 [IWBN if DWO skeletons had DW_AT_stmt_list]
5785 call FUNC */
5786
5787 if (dwarf2_read_debug)
5788 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5789
5790 /* Sort in a separate table to maintain the order of all_type_units
5791 for .gdb_index: TU indices directly index all_type_units. */
5792 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5793 dwarf2_per_objfile->n_type_units);
5794 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5795 {
5796 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5797
5798 sorted_by_abbrev[i].sig_type = sig_type;
5799 sorted_by_abbrev[i].abbrev_offset =
5800 read_abbrev_offset (sig_type->per_cu.section,
5801 sig_type->per_cu.offset);
5802 }
5803 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5804 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5805 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5806
5807 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5808 called any number of times, so we don't reset tu_stats here. */
5809
5810 abbrev_offset.sect_off = ~(unsigned) 0;
5811 abbrev_table = NULL;
5812 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5813
5814 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5815 {
5816 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5817
5818 /* Switch to the next abbrev table if necessary. */
5819 if (abbrev_table == NULL
5820 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5821 {
5822 if (abbrev_table != NULL)
5823 {
5824 abbrev_table_free (abbrev_table);
5825 /* Reset to NULL in case abbrev_table_read_table throws
5826 an error: abbrev_table_free_cleanup will get called. */
5827 abbrev_table = NULL;
5828 }
5829 abbrev_offset = tu->abbrev_offset;
5830 abbrev_table =
5831 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5832 abbrev_offset);
5833 ++tu_stats->nr_uniq_abbrev_tables;
5834 }
5835
5836 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5837 func, data);
5838 }
5839
5840 /* type_unit_groups can be NULL if there is an error in the debug info.
5841 Just create an empty table so the rest of gdb doesn't have to watch
5842 for this error case. */
5843 if (dwarf2_per_objfile->type_unit_groups == NULL)
5844 {
5845 dwarf2_per_objfile->type_unit_groups =
5846 allocate_type_unit_groups_table ();
5847 dwarf2_per_objfile->n_type_unit_groups = 0;
5848 }
5849
5850 /* Create a vector of pointers to primary type units to make it easy to
5851 iterate over them and CUs. See dw2_get_primary_cu. */
5852 dwarf2_per_objfile->n_type_unit_groups =
5853 htab_elements (dwarf2_per_objfile->type_unit_groups);
5854 dwarf2_per_objfile->all_type_unit_groups =
5855 obstack_alloc (&objfile->objfile_obstack,
5856 dwarf2_per_objfile->n_type_unit_groups
5857 * sizeof (struct type_unit_group *));
5858 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5859 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5860 add_type_unit_group_to_table, &iter);
5861 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5862 == dwarf2_per_objfile->n_type_unit_groups);
5863
5864 do_cleanups (cleanups);
5865
5866 if (dwarf2_read_debug)
5867 {
5868 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5869 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5870 dwarf2_per_objfile->n_type_units);
5871 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5872 tu_stats->nr_uniq_abbrev_tables);
5873 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5874 tu_stats->nr_symtabs);
5875 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5876 tu_stats->nr_symtab_sharers);
5877 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5878 tu_stats->nr_stmt_less_type_units);
5879 }
5880 }
5881 \f
5882 /* Partial symbol tables. */
5883
5884 /* Create a psymtab named NAME and assign it to PER_CU.
5885
5886 The caller must fill in the following details:
5887 dirname, textlow, texthigh. */
5888
5889 static struct partial_symtab *
5890 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5891 {
5892 struct objfile *objfile = per_cu->objfile;
5893 struct partial_symtab *pst;
5894
5895 pst = start_psymtab_common (objfile, objfile->section_offsets,
5896 name, 0,
5897 objfile->global_psymbols.next,
5898 objfile->static_psymbols.next);
5899
5900 pst->psymtabs_addrmap_supported = 1;
5901
5902 /* This is the glue that links PST into GDB's symbol API. */
5903 pst->read_symtab_private = per_cu;
5904 pst->read_symtab = dwarf2_read_symtab;
5905 per_cu->v.psymtab = pst;
5906
5907 return pst;
5908 }
5909
5910 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5911 type. */
5912
5913 struct process_psymtab_comp_unit_data
5914 {
5915 /* True if we are reading a DW_TAG_partial_unit. */
5916
5917 int want_partial_unit;
5918
5919 /* The "pretend" language that is used if the CU doesn't declare a
5920 language. */
5921
5922 enum language pretend_language;
5923 };
5924
5925 /* die_reader_func for process_psymtab_comp_unit. */
5926
5927 static void
5928 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5929 const gdb_byte *info_ptr,
5930 struct die_info *comp_unit_die,
5931 int has_children,
5932 void *data)
5933 {
5934 struct dwarf2_cu *cu = reader->cu;
5935 struct objfile *objfile = cu->objfile;
5936 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5937 struct attribute *attr;
5938 CORE_ADDR baseaddr;
5939 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5940 struct partial_symtab *pst;
5941 int has_pc_info;
5942 const char *filename;
5943 struct process_psymtab_comp_unit_data *info = data;
5944
5945 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5946 return;
5947
5948 gdb_assert (! per_cu->is_debug_types);
5949
5950 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5951
5952 cu->list_in_scope = &file_symbols;
5953
5954 /* Allocate a new partial symbol table structure. */
5955 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5956 if (attr == NULL || !DW_STRING (attr))
5957 filename = "";
5958 else
5959 filename = DW_STRING (attr);
5960
5961 pst = create_partial_symtab (per_cu, filename);
5962
5963 /* This must be done before calling dwarf2_build_include_psymtabs. */
5964 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5965 if (attr != NULL)
5966 pst->dirname = DW_STRING (attr);
5967
5968 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5969
5970 dwarf2_find_base_address (comp_unit_die, cu);
5971
5972 /* Possibly set the default values of LOWPC and HIGHPC from
5973 `DW_AT_ranges'. */
5974 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5975 &best_highpc, cu, pst);
5976 if (has_pc_info == 1 && best_lowpc < best_highpc)
5977 /* Store the contiguous range if it is not empty; it can be empty for
5978 CUs with no code. */
5979 addrmap_set_empty (objfile->psymtabs_addrmap,
5980 best_lowpc + baseaddr,
5981 best_highpc + baseaddr - 1, 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 = best_lowpc + baseaddr;
6013 pst->texthigh = best_highpc + baseaddr;
6014
6015 pst->n_global_syms = objfile->global_psymbols.next -
6016 (objfile->global_psymbols.list + pst->globals_offset);
6017 pst->n_static_syms = objfile->static_psymbols.next -
6018 (objfile->static_psymbols.list + pst->statics_offset);
6019 sort_pst_symbols (objfile, pst);
6020
6021 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6022 {
6023 int i;
6024 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6025 struct dwarf2_per_cu_data *iter;
6026
6027 /* Fill in 'dependencies' here; we fill in 'users' in a
6028 post-pass. */
6029 pst->number_of_dependencies = len;
6030 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6031 len * sizeof (struct symtab *));
6032 for (i = 0;
6033 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6034 i, iter);
6035 ++i)
6036 pst->dependencies[i] = iter->v.psymtab;
6037
6038 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6039 }
6040
6041 /* Get the list of files included in the current compilation unit,
6042 and build a psymtab for each of them. */
6043 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6044
6045 if (dwarf2_read_debug)
6046 {
6047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6048
6049 fprintf_unfiltered (gdb_stdlog,
6050 "Psymtab for %s unit @0x%x: %s - %s"
6051 ", %d global, %d static syms\n",
6052 per_cu->is_debug_types ? "type" : "comp",
6053 per_cu->offset.sect_off,
6054 paddress (gdbarch, pst->textlow),
6055 paddress (gdbarch, pst->texthigh),
6056 pst->n_global_syms, pst->n_static_syms);
6057 }
6058 }
6059
6060 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6061 Process compilation unit THIS_CU for a psymtab. */
6062
6063 static void
6064 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6065 int want_partial_unit,
6066 enum language pretend_language)
6067 {
6068 struct process_psymtab_comp_unit_data info;
6069
6070 /* If this compilation unit was already read in, free the
6071 cached copy in order to read it in again. This is
6072 necessary because we skipped some symbols when we first
6073 read in the compilation unit (see load_partial_dies).
6074 This problem could be avoided, but the benefit is unclear. */
6075 if (this_cu->cu != NULL)
6076 free_one_cached_comp_unit (this_cu);
6077
6078 gdb_assert (! this_cu->is_debug_types);
6079 info.want_partial_unit = want_partial_unit;
6080 info.pretend_language = pretend_language;
6081 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6082 process_psymtab_comp_unit_reader,
6083 &info);
6084
6085 /* Age out any secondary CUs. */
6086 age_cached_comp_units ();
6087 }
6088
6089 /* Reader function for build_type_psymtabs. */
6090
6091 static void
6092 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6093 const gdb_byte *info_ptr,
6094 struct die_info *type_unit_die,
6095 int has_children,
6096 void *data)
6097 {
6098 struct objfile *objfile = dwarf2_per_objfile->objfile;
6099 struct dwarf2_cu *cu = reader->cu;
6100 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6101 struct signatured_type *sig_type;
6102 struct type_unit_group *tu_group;
6103 struct attribute *attr;
6104 struct partial_die_info *first_die;
6105 CORE_ADDR lowpc, highpc;
6106 struct partial_symtab *pst;
6107
6108 gdb_assert (data == NULL);
6109 gdb_assert (per_cu->is_debug_types);
6110 sig_type = (struct signatured_type *) per_cu;
6111
6112 if (! has_children)
6113 return;
6114
6115 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6116 tu_group = get_type_unit_group (cu, attr);
6117
6118 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6119
6120 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6121 cu->list_in_scope = &file_symbols;
6122 pst = create_partial_symtab (per_cu, "");
6123 pst->anonymous = 1;
6124
6125 first_die = load_partial_dies (reader, info_ptr, 1);
6126
6127 lowpc = (CORE_ADDR) -1;
6128 highpc = (CORE_ADDR) 0;
6129 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6130
6131 pst->n_global_syms = objfile->global_psymbols.next -
6132 (objfile->global_psymbols.list + pst->globals_offset);
6133 pst->n_static_syms = objfile->static_psymbols.next -
6134 (objfile->static_psymbols.list + pst->statics_offset);
6135 sort_pst_symbols (objfile, pst);
6136 }
6137
6138 /* Traversal function for build_type_psymtabs. */
6139
6140 static int
6141 build_type_psymtab_dependencies (void **slot, void *info)
6142 {
6143 struct objfile *objfile = dwarf2_per_objfile->objfile;
6144 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6145 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6146 struct partial_symtab *pst = per_cu->v.psymtab;
6147 int len = VEC_length (sig_type_ptr, tu_group->tus);
6148 struct signatured_type *iter;
6149 int i;
6150
6151 gdb_assert (len > 0);
6152 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6153
6154 pst->number_of_dependencies = len;
6155 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6156 len * sizeof (struct psymtab *));
6157 for (i = 0;
6158 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6159 ++i)
6160 {
6161 gdb_assert (iter->per_cu.is_debug_types);
6162 pst->dependencies[i] = iter->per_cu.v.psymtab;
6163 iter->type_unit_group = tu_group;
6164 }
6165
6166 VEC_free (sig_type_ptr, tu_group->tus);
6167
6168 return 1;
6169 }
6170
6171 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6172 Build partial symbol tables for the .debug_types comp-units. */
6173
6174 static void
6175 build_type_psymtabs (struct objfile *objfile)
6176 {
6177 if (! create_all_type_units (objfile))
6178 return;
6179
6180 build_type_unit_groups (build_type_psymtabs_reader, NULL);
6181
6182 /* Now that all TUs have been processed we can fill in the dependencies. */
6183 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6184 build_type_psymtab_dependencies, NULL);
6185 }
6186
6187 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6188
6189 static void
6190 psymtabs_addrmap_cleanup (void *o)
6191 {
6192 struct objfile *objfile = o;
6193
6194 objfile->psymtabs_addrmap = NULL;
6195 }
6196
6197 /* Compute the 'user' field for each psymtab in OBJFILE. */
6198
6199 static void
6200 set_partial_user (struct objfile *objfile)
6201 {
6202 int i;
6203
6204 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6205 {
6206 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6207 struct partial_symtab *pst = per_cu->v.psymtab;
6208 int j;
6209
6210 if (pst == NULL)
6211 continue;
6212
6213 for (j = 0; j < pst->number_of_dependencies; ++j)
6214 {
6215 /* Set the 'user' field only if it is not already set. */
6216 if (pst->dependencies[j]->user == NULL)
6217 pst->dependencies[j]->user = pst;
6218 }
6219 }
6220 }
6221
6222 /* Build the partial symbol table by doing a quick pass through the
6223 .debug_info and .debug_abbrev sections. */
6224
6225 static void
6226 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6227 {
6228 struct cleanup *back_to, *addrmap_cleanup;
6229 struct obstack temp_obstack;
6230 int i;
6231
6232 if (dwarf2_read_debug)
6233 {
6234 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6235 objfile_name (objfile));
6236 }
6237
6238 dwarf2_per_objfile->reading_partial_symbols = 1;
6239
6240 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6241
6242 /* Any cached compilation units will be linked by the per-objfile
6243 read_in_chain. Make sure to free them when we're done. */
6244 back_to = make_cleanup (free_cached_comp_units, NULL);
6245
6246 build_type_psymtabs (objfile);
6247
6248 create_all_comp_units (objfile);
6249
6250 /* Create a temporary address map on a temporary obstack. We later
6251 copy this to the final obstack. */
6252 obstack_init (&temp_obstack);
6253 make_cleanup_obstack_free (&temp_obstack);
6254 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6255 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6256
6257 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6258 {
6259 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6260
6261 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6262 }
6263
6264 set_partial_user (objfile);
6265
6266 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6267 &objfile->objfile_obstack);
6268 discard_cleanups (addrmap_cleanup);
6269
6270 do_cleanups (back_to);
6271
6272 if (dwarf2_read_debug)
6273 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6274 objfile_name (objfile));
6275 }
6276
6277 /* die_reader_func for load_partial_comp_unit. */
6278
6279 static void
6280 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6281 const gdb_byte *info_ptr,
6282 struct die_info *comp_unit_die,
6283 int has_children,
6284 void *data)
6285 {
6286 struct dwarf2_cu *cu = reader->cu;
6287
6288 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6289
6290 /* Check if comp unit has_children.
6291 If so, read the rest of the partial symbols from this comp unit.
6292 If not, there's no more debug_info for this comp unit. */
6293 if (has_children)
6294 load_partial_dies (reader, info_ptr, 0);
6295 }
6296
6297 /* Load the partial DIEs for a secondary CU into memory.
6298 This is also used when rereading a primary CU with load_all_dies. */
6299
6300 static void
6301 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6302 {
6303 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6304 load_partial_comp_unit_reader, NULL);
6305 }
6306
6307 static void
6308 read_comp_units_from_section (struct objfile *objfile,
6309 struct dwarf2_section_info *section,
6310 unsigned int is_dwz,
6311 int *n_allocated,
6312 int *n_comp_units,
6313 struct dwarf2_per_cu_data ***all_comp_units)
6314 {
6315 const gdb_byte *info_ptr;
6316 bfd *abfd = get_section_bfd_owner (section);
6317
6318 if (dwarf2_read_debug)
6319 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6320 get_section_name (section),
6321 get_section_file_name (section));
6322
6323 dwarf2_read_section (objfile, section);
6324
6325 info_ptr = section->buffer;
6326
6327 while (info_ptr < section->buffer + section->size)
6328 {
6329 unsigned int length, initial_length_size;
6330 struct dwarf2_per_cu_data *this_cu;
6331 sect_offset offset;
6332
6333 offset.sect_off = info_ptr - section->buffer;
6334
6335 /* Read just enough information to find out where the next
6336 compilation unit is. */
6337 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6338
6339 /* Save the compilation unit for later lookup. */
6340 this_cu = obstack_alloc (&objfile->objfile_obstack,
6341 sizeof (struct dwarf2_per_cu_data));
6342 memset (this_cu, 0, sizeof (*this_cu));
6343 this_cu->offset = offset;
6344 this_cu->length = length + initial_length_size;
6345 this_cu->is_dwz = is_dwz;
6346 this_cu->objfile = objfile;
6347 this_cu->section = section;
6348
6349 if (*n_comp_units == *n_allocated)
6350 {
6351 *n_allocated *= 2;
6352 *all_comp_units = xrealloc (*all_comp_units,
6353 *n_allocated
6354 * sizeof (struct dwarf2_per_cu_data *));
6355 }
6356 (*all_comp_units)[*n_comp_units] = this_cu;
6357 ++*n_comp_units;
6358
6359 info_ptr = info_ptr + this_cu->length;
6360 }
6361 }
6362
6363 /* Create a list of all compilation units in OBJFILE.
6364 This is only done for -readnow and building partial symtabs. */
6365
6366 static void
6367 create_all_comp_units (struct objfile *objfile)
6368 {
6369 int n_allocated;
6370 int n_comp_units;
6371 struct dwarf2_per_cu_data **all_comp_units;
6372 struct dwz_file *dwz;
6373
6374 n_comp_units = 0;
6375 n_allocated = 10;
6376 all_comp_units = xmalloc (n_allocated
6377 * sizeof (struct dwarf2_per_cu_data *));
6378
6379 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6380 &n_allocated, &n_comp_units, &all_comp_units);
6381
6382 dwz = dwarf2_get_dwz_file ();
6383 if (dwz != NULL)
6384 read_comp_units_from_section (objfile, &dwz->info, 1,
6385 &n_allocated, &n_comp_units,
6386 &all_comp_units);
6387
6388 dwarf2_per_objfile->all_comp_units
6389 = obstack_alloc (&objfile->objfile_obstack,
6390 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6391 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6392 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6393 xfree (all_comp_units);
6394 dwarf2_per_objfile->n_comp_units = n_comp_units;
6395 }
6396
6397 /* Process all loaded DIEs for compilation unit CU, starting at
6398 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6399 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6400 DW_AT_ranges). If NEED_PC is set, then this function will set
6401 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6402 and record the covered ranges in the addrmap. */
6403
6404 static void
6405 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6406 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6407 {
6408 struct partial_die_info *pdi;
6409
6410 /* Now, march along the PDI's, descending into ones which have
6411 interesting children but skipping the children of the other ones,
6412 until we reach the end of the compilation unit. */
6413
6414 pdi = first_die;
6415
6416 while (pdi != NULL)
6417 {
6418 fixup_partial_die (pdi, cu);
6419
6420 /* Anonymous namespaces or modules have no name but have interesting
6421 children, so we need to look at them. Ditto for anonymous
6422 enums. */
6423
6424 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6425 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6426 || pdi->tag == DW_TAG_imported_unit)
6427 {
6428 switch (pdi->tag)
6429 {
6430 case DW_TAG_subprogram:
6431 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6432 break;
6433 case DW_TAG_constant:
6434 case DW_TAG_variable:
6435 case DW_TAG_typedef:
6436 case DW_TAG_union_type:
6437 if (!pdi->is_declaration)
6438 {
6439 add_partial_symbol (pdi, cu);
6440 }
6441 break;
6442 case DW_TAG_class_type:
6443 case DW_TAG_interface_type:
6444 case DW_TAG_structure_type:
6445 if (!pdi->is_declaration)
6446 {
6447 add_partial_symbol (pdi, cu);
6448 }
6449 break;
6450 case DW_TAG_enumeration_type:
6451 if (!pdi->is_declaration)
6452 add_partial_enumeration (pdi, cu);
6453 break;
6454 case DW_TAG_base_type:
6455 case DW_TAG_subrange_type:
6456 /* File scope base type definitions are added to the partial
6457 symbol table. */
6458 add_partial_symbol (pdi, cu);
6459 break;
6460 case DW_TAG_namespace:
6461 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6462 break;
6463 case DW_TAG_module:
6464 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6465 break;
6466 case DW_TAG_imported_unit:
6467 {
6468 struct dwarf2_per_cu_data *per_cu;
6469
6470 /* For now we don't handle imported units in type units. */
6471 if (cu->per_cu->is_debug_types)
6472 {
6473 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6474 " supported in type units [in module %s]"),
6475 objfile_name (cu->objfile));
6476 }
6477
6478 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6479 pdi->is_dwz,
6480 cu->objfile);
6481
6482 /* Go read the partial unit, if needed. */
6483 if (per_cu->v.psymtab == NULL)
6484 process_psymtab_comp_unit (per_cu, 1, cu->language);
6485
6486 VEC_safe_push (dwarf2_per_cu_ptr,
6487 cu->per_cu->imported_symtabs, per_cu);
6488 }
6489 break;
6490 default:
6491 break;
6492 }
6493 }
6494
6495 /* If the die has a sibling, skip to the sibling. */
6496
6497 pdi = pdi->die_sibling;
6498 }
6499 }
6500
6501 /* Functions used to compute the fully scoped name of a partial DIE.
6502
6503 Normally, this is simple. For C++, the parent DIE's fully scoped
6504 name is concatenated with "::" and the partial DIE's name. For
6505 Java, the same thing occurs except that "." is used instead of "::".
6506 Enumerators are an exception; they use the scope of their parent
6507 enumeration type, i.e. the name of the enumeration type is not
6508 prepended to the enumerator.
6509
6510 There are two complexities. One is DW_AT_specification; in this
6511 case "parent" means the parent of the target of the specification,
6512 instead of the direct parent of the DIE. The other is compilers
6513 which do not emit DW_TAG_namespace; in this case we try to guess
6514 the fully qualified name of structure types from their members'
6515 linkage names. This must be done using the DIE's children rather
6516 than the children of any DW_AT_specification target. We only need
6517 to do this for structures at the top level, i.e. if the target of
6518 any DW_AT_specification (if any; otherwise the DIE itself) does not
6519 have a parent. */
6520
6521 /* Compute the scope prefix associated with PDI's parent, in
6522 compilation unit CU. The result will be allocated on CU's
6523 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6524 field. NULL is returned if no prefix is necessary. */
6525 static const char *
6526 partial_die_parent_scope (struct partial_die_info *pdi,
6527 struct dwarf2_cu *cu)
6528 {
6529 const char *grandparent_scope;
6530 struct partial_die_info *parent, *real_pdi;
6531
6532 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6533 then this means the parent of the specification DIE. */
6534
6535 real_pdi = pdi;
6536 while (real_pdi->has_specification)
6537 real_pdi = find_partial_die (real_pdi->spec_offset,
6538 real_pdi->spec_is_dwz, cu);
6539
6540 parent = real_pdi->die_parent;
6541 if (parent == NULL)
6542 return NULL;
6543
6544 if (parent->scope_set)
6545 return parent->scope;
6546
6547 fixup_partial_die (parent, cu);
6548
6549 grandparent_scope = partial_die_parent_scope (parent, cu);
6550
6551 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6552 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6553 Work around this problem here. */
6554 if (cu->language == language_cplus
6555 && parent->tag == DW_TAG_namespace
6556 && strcmp (parent->name, "::") == 0
6557 && grandparent_scope == NULL)
6558 {
6559 parent->scope = NULL;
6560 parent->scope_set = 1;
6561 return NULL;
6562 }
6563
6564 if (pdi->tag == DW_TAG_enumerator)
6565 /* Enumerators should not get the name of the enumeration as a prefix. */
6566 parent->scope = grandparent_scope;
6567 else if (parent->tag == DW_TAG_namespace
6568 || parent->tag == DW_TAG_module
6569 || parent->tag == DW_TAG_structure_type
6570 || parent->tag == DW_TAG_class_type
6571 || parent->tag == DW_TAG_interface_type
6572 || parent->tag == DW_TAG_union_type
6573 || parent->tag == DW_TAG_enumeration_type)
6574 {
6575 if (grandparent_scope == NULL)
6576 parent->scope = parent->name;
6577 else
6578 parent->scope = typename_concat (&cu->comp_unit_obstack,
6579 grandparent_scope,
6580 parent->name, 0, cu);
6581 }
6582 else
6583 {
6584 /* FIXME drow/2004-04-01: What should we be doing with
6585 function-local names? For partial symbols, we should probably be
6586 ignoring them. */
6587 complaint (&symfile_complaints,
6588 _("unhandled containing DIE tag %d for DIE at %d"),
6589 parent->tag, pdi->offset.sect_off);
6590 parent->scope = grandparent_scope;
6591 }
6592
6593 parent->scope_set = 1;
6594 return parent->scope;
6595 }
6596
6597 /* Return the fully scoped name associated with PDI, from compilation unit
6598 CU. The result will be allocated with malloc. */
6599
6600 static char *
6601 partial_die_full_name (struct partial_die_info *pdi,
6602 struct dwarf2_cu *cu)
6603 {
6604 const char *parent_scope;
6605
6606 /* If this is a template instantiation, we can not work out the
6607 template arguments from partial DIEs. So, unfortunately, we have
6608 to go through the full DIEs. At least any work we do building
6609 types here will be reused if full symbols are loaded later. */
6610 if (pdi->has_template_arguments)
6611 {
6612 fixup_partial_die (pdi, cu);
6613
6614 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6615 {
6616 struct die_info *die;
6617 struct attribute attr;
6618 struct dwarf2_cu *ref_cu = cu;
6619
6620 /* DW_FORM_ref_addr is using section offset. */
6621 attr.name = 0;
6622 attr.form = DW_FORM_ref_addr;
6623 attr.u.unsnd = pdi->offset.sect_off;
6624 die = follow_die_ref (NULL, &attr, &ref_cu);
6625
6626 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6627 }
6628 }
6629
6630 parent_scope = partial_die_parent_scope (pdi, cu);
6631 if (parent_scope == NULL)
6632 return NULL;
6633 else
6634 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6635 }
6636
6637 static void
6638 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6639 {
6640 struct objfile *objfile = cu->objfile;
6641 CORE_ADDR addr = 0;
6642 const char *actual_name = NULL;
6643 CORE_ADDR baseaddr;
6644 char *built_actual_name;
6645
6646 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6647
6648 built_actual_name = partial_die_full_name (pdi, cu);
6649 if (built_actual_name != NULL)
6650 actual_name = built_actual_name;
6651
6652 if (actual_name == NULL)
6653 actual_name = pdi->name;
6654
6655 switch (pdi->tag)
6656 {
6657 case DW_TAG_subprogram:
6658 if (pdi->is_external || cu->language == language_ada)
6659 {
6660 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6661 of the global scope. But in Ada, we want to be able to access
6662 nested procedures globally. So all Ada subprograms are stored
6663 in the global scope. */
6664 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6665 mst_text, objfile); */
6666 add_psymbol_to_list (actual_name, strlen (actual_name),
6667 built_actual_name != NULL,
6668 VAR_DOMAIN, LOC_BLOCK,
6669 &objfile->global_psymbols,
6670 0, pdi->lowpc + baseaddr,
6671 cu->language, objfile);
6672 }
6673 else
6674 {
6675 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6676 mst_file_text, objfile); */
6677 add_psymbol_to_list (actual_name, strlen (actual_name),
6678 built_actual_name != NULL,
6679 VAR_DOMAIN, LOC_BLOCK,
6680 &objfile->static_psymbols,
6681 0, pdi->lowpc + baseaddr,
6682 cu->language, objfile);
6683 }
6684 break;
6685 case DW_TAG_constant:
6686 {
6687 struct psymbol_allocation_list *list;
6688
6689 if (pdi->is_external)
6690 list = &objfile->global_psymbols;
6691 else
6692 list = &objfile->static_psymbols;
6693 add_psymbol_to_list (actual_name, strlen (actual_name),
6694 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6695 list, 0, 0, cu->language, objfile);
6696 }
6697 break;
6698 case DW_TAG_variable:
6699 if (pdi->d.locdesc)
6700 addr = decode_locdesc (pdi->d.locdesc, cu);
6701
6702 if (pdi->d.locdesc
6703 && addr == 0
6704 && !dwarf2_per_objfile->has_section_at_zero)
6705 {
6706 /* A global or static variable may also have been stripped
6707 out by the linker if unused, in which case its address
6708 will be nullified; do not add such variables into partial
6709 symbol table then. */
6710 }
6711 else if (pdi->is_external)
6712 {
6713 /* Global Variable.
6714 Don't enter into the minimal symbol tables as there is
6715 a minimal symbol table entry from the ELF symbols already.
6716 Enter into partial symbol table if it has a location
6717 descriptor or a type.
6718 If the location descriptor is missing, new_symbol will create
6719 a LOC_UNRESOLVED symbol, the address of the variable will then
6720 be determined from the minimal symbol table whenever the variable
6721 is referenced.
6722 The address for the partial symbol table entry is not
6723 used by GDB, but it comes in handy for debugging partial symbol
6724 table building. */
6725
6726 if (pdi->d.locdesc || pdi->has_type)
6727 add_psymbol_to_list (actual_name, strlen (actual_name),
6728 built_actual_name != NULL,
6729 VAR_DOMAIN, LOC_STATIC,
6730 &objfile->global_psymbols,
6731 0, addr + baseaddr,
6732 cu->language, objfile);
6733 }
6734 else
6735 {
6736 /* Static Variable. Skip symbols without location descriptors. */
6737 if (pdi->d.locdesc == NULL)
6738 {
6739 xfree (built_actual_name);
6740 return;
6741 }
6742 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6743 mst_file_data, objfile); */
6744 add_psymbol_to_list (actual_name, strlen (actual_name),
6745 built_actual_name != NULL,
6746 VAR_DOMAIN, LOC_STATIC,
6747 &objfile->static_psymbols,
6748 0, addr + baseaddr,
6749 cu->language, objfile);
6750 }
6751 break;
6752 case DW_TAG_typedef:
6753 case DW_TAG_base_type:
6754 case DW_TAG_subrange_type:
6755 add_psymbol_to_list (actual_name, strlen (actual_name),
6756 built_actual_name != NULL,
6757 VAR_DOMAIN, LOC_TYPEDEF,
6758 &objfile->static_psymbols,
6759 0, (CORE_ADDR) 0, cu->language, objfile);
6760 break;
6761 case DW_TAG_namespace:
6762 add_psymbol_to_list (actual_name, strlen (actual_name),
6763 built_actual_name != NULL,
6764 VAR_DOMAIN, LOC_TYPEDEF,
6765 &objfile->global_psymbols,
6766 0, (CORE_ADDR) 0, cu->language, objfile);
6767 break;
6768 case DW_TAG_class_type:
6769 case DW_TAG_interface_type:
6770 case DW_TAG_structure_type:
6771 case DW_TAG_union_type:
6772 case DW_TAG_enumeration_type:
6773 /* Skip external references. The DWARF standard says in the section
6774 about "Structure, Union, and Class Type Entries": "An incomplete
6775 structure, union or class type is represented by a structure,
6776 union or class entry that does not have a byte size attribute
6777 and that has a DW_AT_declaration attribute." */
6778 if (!pdi->has_byte_size && pdi->is_declaration)
6779 {
6780 xfree (built_actual_name);
6781 return;
6782 }
6783
6784 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6785 static vs. global. */
6786 add_psymbol_to_list (actual_name, strlen (actual_name),
6787 built_actual_name != NULL,
6788 STRUCT_DOMAIN, LOC_TYPEDEF,
6789 (cu->language == language_cplus
6790 || cu->language == language_java)
6791 ? &objfile->global_psymbols
6792 : &objfile->static_psymbols,
6793 0, (CORE_ADDR) 0, cu->language, objfile);
6794
6795 break;
6796 case DW_TAG_enumerator:
6797 add_psymbol_to_list (actual_name, strlen (actual_name),
6798 built_actual_name != NULL,
6799 VAR_DOMAIN, LOC_CONST,
6800 (cu->language == language_cplus
6801 || cu->language == language_java)
6802 ? &objfile->global_psymbols
6803 : &objfile->static_psymbols,
6804 0, (CORE_ADDR) 0, cu->language, objfile);
6805 break;
6806 default:
6807 break;
6808 }
6809
6810 xfree (built_actual_name);
6811 }
6812
6813 /* Read a partial die corresponding to a namespace; also, add a symbol
6814 corresponding to that namespace to the symbol table. NAMESPACE is
6815 the name of the enclosing namespace. */
6816
6817 static void
6818 add_partial_namespace (struct partial_die_info *pdi,
6819 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6820 int need_pc, struct dwarf2_cu *cu)
6821 {
6822 /* Add a symbol for the namespace. */
6823
6824 add_partial_symbol (pdi, cu);
6825
6826 /* Now scan partial symbols in that namespace. */
6827
6828 if (pdi->has_children)
6829 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6830 }
6831
6832 /* Read a partial die corresponding to a Fortran module. */
6833
6834 static void
6835 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6836 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6837 {
6838 /* Now scan partial symbols in that module. */
6839
6840 if (pdi->has_children)
6841 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6842 }
6843
6844 /* Read a partial die corresponding to a subprogram and create a partial
6845 symbol for that subprogram. When the CU language allows it, this
6846 routine also defines a partial symbol for each nested subprogram
6847 that this subprogram contains.
6848
6849 DIE my also be a lexical block, in which case we simply search
6850 recursively for suprograms defined inside that lexical block.
6851 Again, this is only performed when the CU language allows this
6852 type of definitions. */
6853
6854 static void
6855 add_partial_subprogram (struct partial_die_info *pdi,
6856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6857 int need_pc, struct dwarf2_cu *cu)
6858 {
6859 if (pdi->tag == DW_TAG_subprogram)
6860 {
6861 if (pdi->has_pc_info)
6862 {
6863 if (pdi->lowpc < *lowpc)
6864 *lowpc = pdi->lowpc;
6865 if (pdi->highpc > *highpc)
6866 *highpc = pdi->highpc;
6867 if (need_pc)
6868 {
6869 CORE_ADDR baseaddr;
6870 struct objfile *objfile = cu->objfile;
6871
6872 baseaddr = ANOFFSET (objfile->section_offsets,
6873 SECT_OFF_TEXT (objfile));
6874 addrmap_set_empty (objfile->psymtabs_addrmap,
6875 pdi->lowpc + baseaddr,
6876 pdi->highpc - 1 + baseaddr,
6877 cu->per_cu->v.psymtab);
6878 }
6879 }
6880
6881 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6882 {
6883 if (!pdi->is_declaration)
6884 /* Ignore subprogram DIEs that do not have a name, they are
6885 illegal. Do not emit a complaint at this point, we will
6886 do so when we convert this psymtab into a symtab. */
6887 if (pdi->name)
6888 add_partial_symbol (pdi, cu);
6889 }
6890 }
6891
6892 if (! pdi->has_children)
6893 return;
6894
6895 if (cu->language == language_ada)
6896 {
6897 pdi = pdi->die_child;
6898 while (pdi != NULL)
6899 {
6900 fixup_partial_die (pdi, cu);
6901 if (pdi->tag == DW_TAG_subprogram
6902 || pdi->tag == DW_TAG_lexical_block)
6903 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6904 pdi = pdi->die_sibling;
6905 }
6906 }
6907 }
6908
6909 /* Read a partial die corresponding to an enumeration type. */
6910
6911 static void
6912 add_partial_enumeration (struct partial_die_info *enum_pdi,
6913 struct dwarf2_cu *cu)
6914 {
6915 struct partial_die_info *pdi;
6916
6917 if (enum_pdi->name != NULL)
6918 add_partial_symbol (enum_pdi, cu);
6919
6920 pdi = enum_pdi->die_child;
6921 while (pdi)
6922 {
6923 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6924 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6925 else
6926 add_partial_symbol (pdi, cu);
6927 pdi = pdi->die_sibling;
6928 }
6929 }
6930
6931 /* Return the initial uleb128 in the die at INFO_PTR. */
6932
6933 static unsigned int
6934 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6935 {
6936 unsigned int bytes_read;
6937
6938 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6939 }
6940
6941 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6942 Return the corresponding abbrev, or NULL if the number is zero (indicating
6943 an empty DIE). In either case *BYTES_READ will be set to the length of
6944 the initial number. */
6945
6946 static struct abbrev_info *
6947 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6948 struct dwarf2_cu *cu)
6949 {
6950 bfd *abfd = cu->objfile->obfd;
6951 unsigned int abbrev_number;
6952 struct abbrev_info *abbrev;
6953
6954 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6955
6956 if (abbrev_number == 0)
6957 return NULL;
6958
6959 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6960 if (!abbrev)
6961 {
6962 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6963 abbrev_number, bfd_get_filename (abfd));
6964 }
6965
6966 return abbrev;
6967 }
6968
6969 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6970 Returns a pointer to the end of a series of DIEs, terminated by an empty
6971 DIE. Any children of the skipped DIEs will also be skipped. */
6972
6973 static const gdb_byte *
6974 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6975 {
6976 struct dwarf2_cu *cu = reader->cu;
6977 struct abbrev_info *abbrev;
6978 unsigned int bytes_read;
6979
6980 while (1)
6981 {
6982 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6983 if (abbrev == NULL)
6984 return info_ptr + bytes_read;
6985 else
6986 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6987 }
6988 }
6989
6990 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6991 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6992 abbrev corresponding to that skipped uleb128 should be passed in
6993 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6994 children. */
6995
6996 static const gdb_byte *
6997 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6998 struct abbrev_info *abbrev)
6999 {
7000 unsigned int bytes_read;
7001 struct attribute attr;
7002 bfd *abfd = reader->abfd;
7003 struct dwarf2_cu *cu = reader->cu;
7004 const gdb_byte *buffer = reader->buffer;
7005 const gdb_byte *buffer_end = reader->buffer_end;
7006 const gdb_byte *start_info_ptr = info_ptr;
7007 unsigned int form, i;
7008
7009 for (i = 0; i < abbrev->num_attrs; i++)
7010 {
7011 /* The only abbrev we care about is DW_AT_sibling. */
7012 if (abbrev->attrs[i].name == DW_AT_sibling)
7013 {
7014 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7015 if (attr.form == DW_FORM_ref_addr)
7016 complaint (&symfile_complaints,
7017 _("ignoring absolute DW_AT_sibling"));
7018 else
7019 {
7020 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7021 const gdb_byte *sibling_ptr = buffer + off;
7022
7023 if (sibling_ptr < info_ptr)
7024 complaint (&symfile_complaints,
7025 _("DW_AT_sibling points backwards"));
7026 else
7027 return sibling_ptr;
7028 }
7029 }
7030
7031 /* If it isn't DW_AT_sibling, skip this attribute. */
7032 form = abbrev->attrs[i].form;
7033 skip_attribute:
7034 switch (form)
7035 {
7036 case DW_FORM_ref_addr:
7037 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7038 and later it is offset sized. */
7039 if (cu->header.version == 2)
7040 info_ptr += cu->header.addr_size;
7041 else
7042 info_ptr += cu->header.offset_size;
7043 break;
7044 case DW_FORM_GNU_ref_alt:
7045 info_ptr += cu->header.offset_size;
7046 break;
7047 case DW_FORM_addr:
7048 info_ptr += cu->header.addr_size;
7049 break;
7050 case DW_FORM_data1:
7051 case DW_FORM_ref1:
7052 case DW_FORM_flag:
7053 info_ptr += 1;
7054 break;
7055 case DW_FORM_flag_present:
7056 break;
7057 case DW_FORM_data2:
7058 case DW_FORM_ref2:
7059 info_ptr += 2;
7060 break;
7061 case DW_FORM_data4:
7062 case DW_FORM_ref4:
7063 info_ptr += 4;
7064 break;
7065 case DW_FORM_data8:
7066 case DW_FORM_ref8:
7067 case DW_FORM_ref_sig8:
7068 info_ptr += 8;
7069 break;
7070 case DW_FORM_string:
7071 read_direct_string (abfd, info_ptr, &bytes_read);
7072 info_ptr += bytes_read;
7073 break;
7074 case DW_FORM_sec_offset:
7075 case DW_FORM_strp:
7076 case DW_FORM_GNU_strp_alt:
7077 info_ptr += cu->header.offset_size;
7078 break;
7079 case DW_FORM_exprloc:
7080 case DW_FORM_block:
7081 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7082 info_ptr += bytes_read;
7083 break;
7084 case DW_FORM_block1:
7085 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7086 break;
7087 case DW_FORM_block2:
7088 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7089 break;
7090 case DW_FORM_block4:
7091 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7092 break;
7093 case DW_FORM_sdata:
7094 case DW_FORM_udata:
7095 case DW_FORM_ref_udata:
7096 case DW_FORM_GNU_addr_index:
7097 case DW_FORM_GNU_str_index:
7098 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7099 break;
7100 case DW_FORM_indirect:
7101 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7102 info_ptr += bytes_read;
7103 /* We need to continue parsing from here, so just go back to
7104 the top. */
7105 goto skip_attribute;
7106
7107 default:
7108 error (_("Dwarf Error: Cannot handle %s "
7109 "in DWARF reader [in module %s]"),
7110 dwarf_form_name (form),
7111 bfd_get_filename (abfd));
7112 }
7113 }
7114
7115 if (abbrev->has_children)
7116 return skip_children (reader, info_ptr);
7117 else
7118 return info_ptr;
7119 }
7120
7121 /* Locate ORIG_PDI's sibling.
7122 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7123
7124 static const gdb_byte *
7125 locate_pdi_sibling (const struct die_reader_specs *reader,
7126 struct partial_die_info *orig_pdi,
7127 const gdb_byte *info_ptr)
7128 {
7129 /* Do we know the sibling already? */
7130
7131 if (orig_pdi->sibling)
7132 return orig_pdi->sibling;
7133
7134 /* Are there any children to deal with? */
7135
7136 if (!orig_pdi->has_children)
7137 return info_ptr;
7138
7139 /* Skip the children the long way. */
7140
7141 return skip_children (reader, info_ptr);
7142 }
7143
7144 /* Expand this partial symbol table into a full symbol table. SELF is
7145 not NULL. */
7146
7147 static void
7148 dwarf2_read_symtab (struct partial_symtab *self,
7149 struct objfile *objfile)
7150 {
7151 if (self->readin)
7152 {
7153 warning (_("bug: psymtab for %s is already read in."),
7154 self->filename);
7155 }
7156 else
7157 {
7158 if (info_verbose)
7159 {
7160 printf_filtered (_("Reading in symbols for %s..."),
7161 self->filename);
7162 gdb_flush (gdb_stdout);
7163 }
7164
7165 /* Restore our global data. */
7166 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
7167
7168 /* If this psymtab is constructed from a debug-only objfile, the
7169 has_section_at_zero flag will not necessarily be correct. We
7170 can get the correct value for this flag by looking at the data
7171 associated with the (presumably stripped) associated objfile. */
7172 if (objfile->separate_debug_objfile_backlink)
7173 {
7174 struct dwarf2_per_objfile *dpo_backlink
7175 = objfile_data (objfile->separate_debug_objfile_backlink,
7176 dwarf2_objfile_data_key);
7177
7178 dwarf2_per_objfile->has_section_at_zero
7179 = dpo_backlink->has_section_at_zero;
7180 }
7181
7182 dwarf2_per_objfile->reading_partial_symbols = 0;
7183
7184 psymtab_to_symtab_1 (self);
7185
7186 /* Finish up the debug error message. */
7187 if (info_verbose)
7188 printf_filtered (_("done.\n"));
7189 }
7190
7191 process_cu_includes ();
7192 }
7193 \f
7194 /* Reading in full CUs. */
7195
7196 /* Add PER_CU to the queue. */
7197
7198 static void
7199 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7200 enum language pretend_language)
7201 {
7202 struct dwarf2_queue_item *item;
7203
7204 per_cu->queued = 1;
7205 item = xmalloc (sizeof (*item));
7206 item->per_cu = per_cu;
7207 item->pretend_language = pretend_language;
7208 item->next = NULL;
7209
7210 if (dwarf2_queue == NULL)
7211 dwarf2_queue = item;
7212 else
7213 dwarf2_queue_tail->next = item;
7214
7215 dwarf2_queue_tail = item;
7216 }
7217
7218 /* If PER_CU is not yet queued, add it to the queue.
7219 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7220 dependency.
7221 The result is non-zero if PER_CU was queued, otherwise the result is zero
7222 meaning either PER_CU is already queued or it is already loaded.
7223
7224 N.B. There is an invariant here that if a CU is queued then it is loaded.
7225 The caller is required to load PER_CU if we return non-zero. */
7226
7227 static int
7228 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7229 struct dwarf2_per_cu_data *per_cu,
7230 enum language pretend_language)
7231 {
7232 /* We may arrive here during partial symbol reading, if we need full
7233 DIEs to process an unusual case (e.g. template arguments). Do
7234 not queue PER_CU, just tell our caller to load its DIEs. */
7235 if (dwarf2_per_objfile->reading_partial_symbols)
7236 {
7237 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7238 return 1;
7239 return 0;
7240 }
7241
7242 /* Mark the dependence relation so that we don't flush PER_CU
7243 too early. */
7244 if (dependent_cu != NULL)
7245 dwarf2_add_dependence (dependent_cu, per_cu);
7246
7247 /* If it's already on the queue, we have nothing to do. */
7248 if (per_cu->queued)
7249 return 0;
7250
7251 /* If the compilation unit is already loaded, just mark it as
7252 used. */
7253 if (per_cu->cu != NULL)
7254 {
7255 per_cu->cu->last_used = 0;
7256 return 0;
7257 }
7258
7259 /* Add it to the queue. */
7260 queue_comp_unit (per_cu, pretend_language);
7261
7262 return 1;
7263 }
7264
7265 /* Process the queue. */
7266
7267 static void
7268 process_queue (void)
7269 {
7270 struct dwarf2_queue_item *item, *next_item;
7271
7272 if (dwarf2_read_debug)
7273 {
7274 fprintf_unfiltered (gdb_stdlog,
7275 "Expanding one or more symtabs of objfile %s ...\n",
7276 objfile_name (dwarf2_per_objfile->objfile));
7277 }
7278
7279 /* The queue starts out with one item, but following a DIE reference
7280 may load a new CU, adding it to the end of the queue. */
7281 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7282 {
7283 if (dwarf2_per_objfile->using_index
7284 ? !item->per_cu->v.quick->symtab
7285 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7286 {
7287 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7288 char buf[100];
7289
7290 if (per_cu->is_debug_types)
7291 {
7292 struct signatured_type *sig_type =
7293 (struct signatured_type *) per_cu;
7294
7295 sprintf (buf, "TU %s at offset 0x%x",
7296 hex_string (sig_type->signature), per_cu->offset.sect_off);
7297 }
7298 else
7299 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7300
7301 if (dwarf2_read_debug)
7302 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7303
7304 if (per_cu->is_debug_types)
7305 process_full_type_unit (per_cu, item->pretend_language);
7306 else
7307 process_full_comp_unit (per_cu, item->pretend_language);
7308
7309 if (dwarf2_read_debug)
7310 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7311 }
7312
7313 item->per_cu->queued = 0;
7314 next_item = item->next;
7315 xfree (item);
7316 }
7317
7318 dwarf2_queue_tail = NULL;
7319
7320 if (dwarf2_read_debug)
7321 {
7322 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7323 objfile_name (dwarf2_per_objfile->objfile));
7324 }
7325 }
7326
7327 /* Free all allocated queue entries. This function only releases anything if
7328 an error was thrown; if the queue was processed then it would have been
7329 freed as we went along. */
7330
7331 static void
7332 dwarf2_release_queue (void *dummy)
7333 {
7334 struct dwarf2_queue_item *item, *last;
7335
7336 item = dwarf2_queue;
7337 while (item)
7338 {
7339 /* Anything still marked queued is likely to be in an
7340 inconsistent state, so discard it. */
7341 if (item->per_cu->queued)
7342 {
7343 if (item->per_cu->cu != NULL)
7344 free_one_cached_comp_unit (item->per_cu);
7345 item->per_cu->queued = 0;
7346 }
7347
7348 last = item;
7349 item = item->next;
7350 xfree (last);
7351 }
7352
7353 dwarf2_queue = dwarf2_queue_tail = NULL;
7354 }
7355
7356 /* Read in full symbols for PST, and anything it depends on. */
7357
7358 static void
7359 psymtab_to_symtab_1 (struct partial_symtab *pst)
7360 {
7361 struct dwarf2_per_cu_data *per_cu;
7362 int i;
7363
7364 if (pst->readin)
7365 return;
7366
7367 for (i = 0; i < pst->number_of_dependencies; i++)
7368 if (!pst->dependencies[i]->readin
7369 && pst->dependencies[i]->user == NULL)
7370 {
7371 /* Inform about additional files that need to be read in. */
7372 if (info_verbose)
7373 {
7374 /* FIXME: i18n: Need to make this a single string. */
7375 fputs_filtered (" ", gdb_stdout);
7376 wrap_here ("");
7377 fputs_filtered ("and ", gdb_stdout);
7378 wrap_here ("");
7379 printf_filtered ("%s...", pst->dependencies[i]->filename);
7380 wrap_here (""); /* Flush output. */
7381 gdb_flush (gdb_stdout);
7382 }
7383 psymtab_to_symtab_1 (pst->dependencies[i]);
7384 }
7385
7386 per_cu = pst->read_symtab_private;
7387
7388 if (per_cu == NULL)
7389 {
7390 /* It's an include file, no symbols to read for it.
7391 Everything is in the parent symtab. */
7392 pst->readin = 1;
7393 return;
7394 }
7395
7396 dw2_do_instantiate_symtab (per_cu);
7397 }
7398
7399 /* Trivial hash function for die_info: the hash value of a DIE
7400 is its offset in .debug_info for this objfile. */
7401
7402 static hashval_t
7403 die_hash (const void *item)
7404 {
7405 const struct die_info *die = item;
7406
7407 return die->offset.sect_off;
7408 }
7409
7410 /* Trivial comparison function for die_info structures: two DIEs
7411 are equal if they have the same offset. */
7412
7413 static int
7414 die_eq (const void *item_lhs, const void *item_rhs)
7415 {
7416 const struct die_info *die_lhs = item_lhs;
7417 const struct die_info *die_rhs = item_rhs;
7418
7419 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7420 }
7421
7422 /* die_reader_func for load_full_comp_unit.
7423 This is identical to read_signatured_type_reader,
7424 but is kept separate for now. */
7425
7426 static void
7427 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7428 const gdb_byte *info_ptr,
7429 struct die_info *comp_unit_die,
7430 int has_children,
7431 void *data)
7432 {
7433 struct dwarf2_cu *cu = reader->cu;
7434 enum language *language_ptr = data;
7435
7436 gdb_assert (cu->die_hash == NULL);
7437 cu->die_hash =
7438 htab_create_alloc_ex (cu->header.length / 12,
7439 die_hash,
7440 die_eq,
7441 NULL,
7442 &cu->comp_unit_obstack,
7443 hashtab_obstack_allocate,
7444 dummy_obstack_deallocate);
7445
7446 if (has_children)
7447 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7448 &info_ptr, comp_unit_die);
7449 cu->dies = comp_unit_die;
7450 /* comp_unit_die is not stored in die_hash, no need. */
7451
7452 /* We try not to read any attributes in this function, because not
7453 all CUs needed for references have been loaded yet, and symbol
7454 table processing isn't initialized. But we have to set the CU language,
7455 or we won't be able to build types correctly.
7456 Similarly, if we do not read the producer, we can not apply
7457 producer-specific interpretation. */
7458 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7459 }
7460
7461 /* Load the DIEs associated with PER_CU into memory. */
7462
7463 static void
7464 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7465 enum language pretend_language)
7466 {
7467 gdb_assert (! this_cu->is_debug_types);
7468
7469 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7470 load_full_comp_unit_reader, &pretend_language);
7471 }
7472
7473 /* Add a DIE to the delayed physname list. */
7474
7475 static void
7476 add_to_method_list (struct type *type, int fnfield_index, int index,
7477 const char *name, struct die_info *die,
7478 struct dwarf2_cu *cu)
7479 {
7480 struct delayed_method_info mi;
7481 mi.type = type;
7482 mi.fnfield_index = fnfield_index;
7483 mi.index = index;
7484 mi.name = name;
7485 mi.die = die;
7486 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7487 }
7488
7489 /* A cleanup for freeing the delayed method list. */
7490
7491 static void
7492 free_delayed_list (void *ptr)
7493 {
7494 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7495 if (cu->method_list != NULL)
7496 {
7497 VEC_free (delayed_method_info, cu->method_list);
7498 cu->method_list = NULL;
7499 }
7500 }
7501
7502 /* Compute the physnames of any methods on the CU's method list.
7503
7504 The computation of method physnames is delayed in order to avoid the
7505 (bad) condition that one of the method's formal parameters is of an as yet
7506 incomplete type. */
7507
7508 static void
7509 compute_delayed_physnames (struct dwarf2_cu *cu)
7510 {
7511 int i;
7512 struct delayed_method_info *mi;
7513 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7514 {
7515 const char *physname;
7516 struct fn_fieldlist *fn_flp
7517 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7518 physname = dwarf2_physname (mi->name, mi->die, cu);
7519 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7520 }
7521 }
7522
7523 /* Go objects should be embedded in a DW_TAG_module DIE,
7524 and it's not clear if/how imported objects will appear.
7525 To keep Go support simple until that's worked out,
7526 go back through what we've read and create something usable.
7527 We could do this while processing each DIE, and feels kinda cleaner,
7528 but that way is more invasive.
7529 This is to, for example, allow the user to type "p var" or "b main"
7530 without having to specify the package name, and allow lookups
7531 of module.object to work in contexts that use the expression
7532 parser. */
7533
7534 static void
7535 fixup_go_packaging (struct dwarf2_cu *cu)
7536 {
7537 char *package_name = NULL;
7538 struct pending *list;
7539 int i;
7540
7541 for (list = global_symbols; list != NULL; list = list->next)
7542 {
7543 for (i = 0; i < list->nsyms; ++i)
7544 {
7545 struct symbol *sym = list->symbol[i];
7546
7547 if (SYMBOL_LANGUAGE (sym) == language_go
7548 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7549 {
7550 char *this_package_name = go_symbol_package_name (sym);
7551
7552 if (this_package_name == NULL)
7553 continue;
7554 if (package_name == NULL)
7555 package_name = this_package_name;
7556 else
7557 {
7558 if (strcmp (package_name, this_package_name) != 0)
7559 complaint (&symfile_complaints,
7560 _("Symtab %s has objects from two different Go packages: %s and %s"),
7561 (SYMBOL_SYMTAB (sym)
7562 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7563 : objfile_name (cu->objfile)),
7564 this_package_name, package_name);
7565 xfree (this_package_name);
7566 }
7567 }
7568 }
7569 }
7570
7571 if (package_name != NULL)
7572 {
7573 struct objfile *objfile = cu->objfile;
7574 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7575 package_name,
7576 strlen (package_name));
7577 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7578 saved_package_name, objfile);
7579 struct symbol *sym;
7580
7581 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7582
7583 sym = allocate_symbol (objfile);
7584 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7585 SYMBOL_SET_NAMES (sym, saved_package_name,
7586 strlen (saved_package_name), 0, objfile);
7587 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7588 e.g., "main" finds the "main" module and not C's main(). */
7589 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7591 SYMBOL_TYPE (sym) = type;
7592
7593 add_symbol_to_list (sym, &global_symbols);
7594
7595 xfree (package_name);
7596 }
7597 }
7598
7599 /* Return the symtab for PER_CU. This works properly regardless of
7600 whether we're using the index or psymtabs. */
7601
7602 static struct symtab *
7603 get_symtab (struct dwarf2_per_cu_data *per_cu)
7604 {
7605 return (dwarf2_per_objfile->using_index
7606 ? per_cu->v.quick->symtab
7607 : per_cu->v.psymtab->symtab);
7608 }
7609
7610 /* A helper function for computing the list of all symbol tables
7611 included by PER_CU. */
7612
7613 static void
7614 recursively_compute_inclusions (VEC (symtab_ptr) **result,
7615 htab_t all_children, htab_t all_type_symtabs,
7616 struct dwarf2_per_cu_data *per_cu,
7617 struct symtab *immediate_parent)
7618 {
7619 void **slot;
7620 int ix;
7621 struct symtab *symtab;
7622 struct dwarf2_per_cu_data *iter;
7623
7624 slot = htab_find_slot (all_children, per_cu, INSERT);
7625 if (*slot != NULL)
7626 {
7627 /* This inclusion and its children have been processed. */
7628 return;
7629 }
7630
7631 *slot = per_cu;
7632 /* Only add a CU if it has a symbol table. */
7633 symtab = get_symtab (per_cu);
7634 if (symtab != NULL)
7635 {
7636 /* If this is a type unit only add its symbol table if we haven't
7637 seen it yet (type unit per_cu's can share symtabs). */
7638 if (per_cu->is_debug_types)
7639 {
7640 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7641 if (*slot == NULL)
7642 {
7643 *slot = symtab;
7644 VEC_safe_push (symtab_ptr, *result, symtab);
7645 if (symtab->user == NULL)
7646 symtab->user = immediate_parent;
7647 }
7648 }
7649 else
7650 {
7651 VEC_safe_push (symtab_ptr, *result, symtab);
7652 if (symtab->user == NULL)
7653 symtab->user = immediate_parent;
7654 }
7655 }
7656
7657 for (ix = 0;
7658 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7659 ++ix)
7660 {
7661 recursively_compute_inclusions (result, all_children,
7662 all_type_symtabs, iter, symtab);
7663 }
7664 }
7665
7666 /* Compute the symtab 'includes' fields for the symtab related to
7667 PER_CU. */
7668
7669 static void
7670 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7671 {
7672 gdb_assert (! per_cu->is_debug_types);
7673
7674 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7675 {
7676 int ix, len;
7677 struct dwarf2_per_cu_data *per_cu_iter;
7678 struct symtab *symtab_iter;
7679 VEC (symtab_ptr) *result_symtabs = NULL;
7680 htab_t all_children, all_type_symtabs;
7681 struct symtab *symtab = get_symtab (per_cu);
7682
7683 /* If we don't have a symtab, we can just skip this case. */
7684 if (symtab == NULL)
7685 return;
7686
7687 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7688 NULL, xcalloc, xfree);
7689 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7690 NULL, xcalloc, xfree);
7691
7692 for (ix = 0;
7693 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7694 ix, per_cu_iter);
7695 ++ix)
7696 {
7697 recursively_compute_inclusions (&result_symtabs, all_children,
7698 all_type_symtabs, per_cu_iter,
7699 symtab);
7700 }
7701
7702 /* Now we have a transitive closure of all the included symtabs. */
7703 len = VEC_length (symtab_ptr, result_symtabs);
7704 symtab->includes
7705 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7706 (len + 1) * sizeof (struct symtab *));
7707 for (ix = 0;
7708 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
7709 ++ix)
7710 symtab->includes[ix] = symtab_iter;
7711 symtab->includes[len] = NULL;
7712
7713 VEC_free (symtab_ptr, result_symtabs);
7714 htab_delete (all_children);
7715 htab_delete (all_type_symtabs);
7716 }
7717 }
7718
7719 /* Compute the 'includes' field for the symtabs of all the CUs we just
7720 read. */
7721
7722 static void
7723 process_cu_includes (void)
7724 {
7725 int ix;
7726 struct dwarf2_per_cu_data *iter;
7727
7728 for (ix = 0;
7729 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7730 ix, iter);
7731 ++ix)
7732 {
7733 if (! iter->is_debug_types)
7734 compute_symtab_includes (iter);
7735 }
7736
7737 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7738 }
7739
7740 /* Generate full symbol information for PER_CU, whose DIEs have
7741 already been loaded into memory. */
7742
7743 static void
7744 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7745 enum language pretend_language)
7746 {
7747 struct dwarf2_cu *cu = per_cu->cu;
7748 struct objfile *objfile = per_cu->objfile;
7749 CORE_ADDR lowpc, highpc;
7750 struct symtab *symtab;
7751 struct cleanup *back_to, *delayed_list_cleanup;
7752 CORE_ADDR baseaddr;
7753 struct block *static_block;
7754
7755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7756
7757 buildsym_init ();
7758 back_to = make_cleanup (really_free_pendings, NULL);
7759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7760
7761 cu->list_in_scope = &file_symbols;
7762
7763 cu->language = pretend_language;
7764 cu->language_defn = language_def (cu->language);
7765
7766 /* Do line number decoding in read_file_scope () */
7767 process_die (cu->dies, cu);
7768
7769 /* For now fudge the Go package. */
7770 if (cu->language == language_go)
7771 fixup_go_packaging (cu);
7772
7773 /* Now that we have processed all the DIEs in the CU, all the types
7774 should be complete, and it should now be safe to compute all of the
7775 physnames. */
7776 compute_delayed_physnames (cu);
7777 do_cleanups (delayed_list_cleanup);
7778
7779 /* Some compilers don't define a DW_AT_high_pc attribute for the
7780 compilation unit. If the DW_AT_high_pc is missing, synthesize
7781 it, by scanning the DIE's below the compilation unit. */
7782 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7783
7784 static_block
7785 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7786
7787 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7788 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7789 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7790 addrmap to help ensure it has an accurate map of pc values belonging to
7791 this comp unit. */
7792 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7793
7794 symtab = end_symtab_from_static_block (static_block, objfile,
7795 SECT_OFF_TEXT (objfile), 0);
7796
7797 if (symtab != NULL)
7798 {
7799 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7800
7801 /* Set symtab language to language from DW_AT_language. If the
7802 compilation is from a C file generated by language preprocessors, do
7803 not set the language if it was already deduced by start_subfile. */
7804 if (!(cu->language == language_c && symtab->language != language_c))
7805 symtab->language = cu->language;
7806
7807 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7808 produce DW_AT_location with location lists but it can be possibly
7809 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7810 there were bugs in prologue debug info, fixed later in GCC-4.5
7811 by "unwind info for epilogues" patch (which is not directly related).
7812
7813 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7814 needed, it would be wrong due to missing DW_AT_producer there.
7815
7816 Still one can confuse GDB by using non-standard GCC compilation
7817 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7818 */
7819 if (cu->has_loclist && gcc_4_minor >= 5)
7820 symtab->locations_valid = 1;
7821
7822 if (gcc_4_minor >= 5)
7823 symtab->epilogue_unwind_valid = 1;
7824
7825 symtab->call_site_htab = cu->call_site_htab;
7826 }
7827
7828 if (dwarf2_per_objfile->using_index)
7829 per_cu->v.quick->symtab = symtab;
7830 else
7831 {
7832 struct partial_symtab *pst = per_cu->v.psymtab;
7833 pst->symtab = symtab;
7834 pst->readin = 1;
7835 }
7836
7837 /* Push it for inclusion processing later. */
7838 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7839
7840 do_cleanups (back_to);
7841 }
7842
7843 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7844 already been loaded into memory. */
7845
7846 static void
7847 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7848 enum language pretend_language)
7849 {
7850 struct dwarf2_cu *cu = per_cu->cu;
7851 struct objfile *objfile = per_cu->objfile;
7852 struct symtab *symtab;
7853 struct cleanup *back_to, *delayed_list_cleanup;
7854 struct signatured_type *sig_type;
7855
7856 gdb_assert (per_cu->is_debug_types);
7857 sig_type = (struct signatured_type *) per_cu;
7858
7859 buildsym_init ();
7860 back_to = make_cleanup (really_free_pendings, NULL);
7861 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7862
7863 cu->list_in_scope = &file_symbols;
7864
7865 cu->language = pretend_language;
7866 cu->language_defn = language_def (cu->language);
7867
7868 /* The symbol tables are set up in read_type_unit_scope. */
7869 process_die (cu->dies, cu);
7870
7871 /* For now fudge the Go package. */
7872 if (cu->language == language_go)
7873 fixup_go_packaging (cu);
7874
7875 /* Now that we have processed all the DIEs in the CU, all the types
7876 should be complete, and it should now be safe to compute all of the
7877 physnames. */
7878 compute_delayed_physnames (cu);
7879 do_cleanups (delayed_list_cleanup);
7880
7881 /* TUs share symbol tables.
7882 If this is the first TU to use this symtab, complete the construction
7883 of it with end_expandable_symtab. Otherwise, complete the addition of
7884 this TU's symbols to the existing symtab. */
7885 if (sig_type->type_unit_group->primary_symtab == NULL)
7886 {
7887 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7888 sig_type->type_unit_group->primary_symtab = symtab;
7889
7890 if (symtab != NULL)
7891 {
7892 /* Set symtab language to language from DW_AT_language. If the
7893 compilation is from a C file generated by language preprocessors,
7894 do not set the language if it was already deduced by
7895 start_subfile. */
7896 if (!(cu->language == language_c && symtab->language != language_c))
7897 symtab->language = cu->language;
7898 }
7899 }
7900 else
7901 {
7902 augment_type_symtab (objfile,
7903 sig_type->type_unit_group->primary_symtab);
7904 symtab = sig_type->type_unit_group->primary_symtab;
7905 }
7906
7907 if (dwarf2_per_objfile->using_index)
7908 per_cu->v.quick->symtab = symtab;
7909 else
7910 {
7911 struct partial_symtab *pst = per_cu->v.psymtab;
7912 pst->symtab = symtab;
7913 pst->readin = 1;
7914 }
7915
7916 do_cleanups (back_to);
7917 }
7918
7919 /* Process an imported unit DIE. */
7920
7921 static void
7922 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7923 {
7924 struct attribute *attr;
7925
7926 /* For now we don't handle imported units in type units. */
7927 if (cu->per_cu->is_debug_types)
7928 {
7929 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7930 " supported in type units [in module %s]"),
7931 objfile_name (cu->objfile));
7932 }
7933
7934 attr = dwarf2_attr (die, DW_AT_import, cu);
7935 if (attr != NULL)
7936 {
7937 struct dwarf2_per_cu_data *per_cu;
7938 struct symtab *imported_symtab;
7939 sect_offset offset;
7940 int is_dwz;
7941
7942 offset = dwarf2_get_ref_die_offset (attr);
7943 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7944 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7945
7946 /* If necessary, add it to the queue and load its DIEs. */
7947 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7948 load_full_comp_unit (per_cu, cu->language);
7949
7950 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7951 per_cu);
7952 }
7953 }
7954
7955 /* Process a die and its children. */
7956
7957 static void
7958 process_die (struct die_info *die, struct dwarf2_cu *cu)
7959 {
7960 switch (die->tag)
7961 {
7962 case DW_TAG_padding:
7963 break;
7964 case DW_TAG_compile_unit:
7965 case DW_TAG_partial_unit:
7966 read_file_scope (die, cu);
7967 break;
7968 case DW_TAG_type_unit:
7969 read_type_unit_scope (die, cu);
7970 break;
7971 case DW_TAG_subprogram:
7972 case DW_TAG_inlined_subroutine:
7973 read_func_scope (die, cu);
7974 break;
7975 case DW_TAG_lexical_block:
7976 case DW_TAG_try_block:
7977 case DW_TAG_catch_block:
7978 read_lexical_block_scope (die, cu);
7979 break;
7980 case DW_TAG_GNU_call_site:
7981 read_call_site_scope (die, cu);
7982 break;
7983 case DW_TAG_class_type:
7984 case DW_TAG_interface_type:
7985 case DW_TAG_structure_type:
7986 case DW_TAG_union_type:
7987 process_structure_scope (die, cu);
7988 break;
7989 case DW_TAG_enumeration_type:
7990 process_enumeration_scope (die, cu);
7991 break;
7992
7993 /* These dies have a type, but processing them does not create
7994 a symbol or recurse to process the children. Therefore we can
7995 read them on-demand through read_type_die. */
7996 case DW_TAG_subroutine_type:
7997 case DW_TAG_set_type:
7998 case DW_TAG_array_type:
7999 case DW_TAG_pointer_type:
8000 case DW_TAG_ptr_to_member_type:
8001 case DW_TAG_reference_type:
8002 case DW_TAG_string_type:
8003 break;
8004
8005 case DW_TAG_base_type:
8006 case DW_TAG_subrange_type:
8007 case DW_TAG_typedef:
8008 /* Add a typedef symbol for the type definition, if it has a
8009 DW_AT_name. */
8010 new_symbol (die, read_type_die (die, cu), cu);
8011 break;
8012 case DW_TAG_common_block:
8013 read_common_block (die, cu);
8014 break;
8015 case DW_TAG_common_inclusion:
8016 break;
8017 case DW_TAG_namespace:
8018 cu->processing_has_namespace_info = 1;
8019 read_namespace (die, cu);
8020 break;
8021 case DW_TAG_module:
8022 cu->processing_has_namespace_info = 1;
8023 read_module (die, cu);
8024 break;
8025 case DW_TAG_imported_declaration:
8026 case DW_TAG_imported_module:
8027 cu->processing_has_namespace_info = 1;
8028 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8029 || cu->language != language_fortran))
8030 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8031 dwarf_tag_name (die->tag));
8032 read_import_statement (die, cu);
8033 break;
8034
8035 case DW_TAG_imported_unit:
8036 process_imported_unit_die (die, cu);
8037 break;
8038
8039 default:
8040 new_symbol (die, NULL, cu);
8041 break;
8042 }
8043 }
8044 \f
8045 /* DWARF name computation. */
8046
8047 /* A helper function for dwarf2_compute_name which determines whether DIE
8048 needs to have the name of the scope prepended to the name listed in the
8049 die. */
8050
8051 static int
8052 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8053 {
8054 struct attribute *attr;
8055
8056 switch (die->tag)
8057 {
8058 case DW_TAG_namespace:
8059 case DW_TAG_typedef:
8060 case DW_TAG_class_type:
8061 case DW_TAG_interface_type:
8062 case DW_TAG_structure_type:
8063 case DW_TAG_union_type:
8064 case DW_TAG_enumeration_type:
8065 case DW_TAG_enumerator:
8066 case DW_TAG_subprogram:
8067 case DW_TAG_member:
8068 return 1;
8069
8070 case DW_TAG_variable:
8071 case DW_TAG_constant:
8072 /* We only need to prefix "globally" visible variables. These include
8073 any variable marked with DW_AT_external or any variable that
8074 lives in a namespace. [Variables in anonymous namespaces
8075 require prefixing, but they are not DW_AT_external.] */
8076
8077 if (dwarf2_attr (die, DW_AT_specification, cu))
8078 {
8079 struct dwarf2_cu *spec_cu = cu;
8080
8081 return die_needs_namespace (die_specification (die, &spec_cu),
8082 spec_cu);
8083 }
8084
8085 attr = dwarf2_attr (die, DW_AT_external, cu);
8086 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8087 && die->parent->tag != DW_TAG_module)
8088 return 0;
8089 /* A variable in a lexical block of some kind does not need a
8090 namespace, even though in C++ such variables may be external
8091 and have a mangled name. */
8092 if (die->parent->tag == DW_TAG_lexical_block
8093 || die->parent->tag == DW_TAG_try_block
8094 || die->parent->tag == DW_TAG_catch_block
8095 || die->parent->tag == DW_TAG_subprogram)
8096 return 0;
8097 return 1;
8098
8099 default:
8100 return 0;
8101 }
8102 }
8103
8104 /* Retrieve the last character from a mem_file. */
8105
8106 static void
8107 do_ui_file_peek_last (void *object, const char *buffer, long length)
8108 {
8109 char *last_char_p = (char *) object;
8110
8111 if (length > 0)
8112 *last_char_p = buffer[length - 1];
8113 }
8114
8115 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8116 compute the physname for the object, which include a method's:
8117 - formal parameters (C++/Java),
8118 - receiver type (Go),
8119 - return type (Java).
8120
8121 The term "physname" is a bit confusing.
8122 For C++, for example, it is the demangled name.
8123 For Go, for example, it's the mangled name.
8124
8125 For Ada, return the DIE's linkage name rather than the fully qualified
8126 name. PHYSNAME is ignored..
8127
8128 The result is allocated on the objfile_obstack and canonicalized. */
8129
8130 static const char *
8131 dwarf2_compute_name (const char *name,
8132 struct die_info *die, struct dwarf2_cu *cu,
8133 int physname)
8134 {
8135 struct objfile *objfile = cu->objfile;
8136
8137 if (name == NULL)
8138 name = dwarf2_name (die, cu);
8139
8140 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
8141 compute it by typename_concat inside GDB. */
8142 if (cu->language == language_ada
8143 || (cu->language == language_fortran && physname))
8144 {
8145 /* For Ada unit, we prefer the linkage name over the name, as
8146 the former contains the exported name, which the user expects
8147 to be able to reference. Ideally, we want the user to be able
8148 to reference this entity using either natural or linkage name,
8149 but we haven't started looking at this enhancement yet. */
8150 struct attribute *attr;
8151
8152 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8153 if (attr == NULL)
8154 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8155 if (attr && DW_STRING (attr))
8156 return DW_STRING (attr);
8157 }
8158
8159 /* These are the only languages we know how to qualify names in. */
8160 if (name != NULL
8161 && (cu->language == language_cplus || cu->language == language_java
8162 || cu->language == language_fortran))
8163 {
8164 if (die_needs_namespace (die, cu))
8165 {
8166 long length;
8167 const char *prefix;
8168 struct ui_file *buf;
8169
8170 prefix = determine_prefix (die, cu);
8171 buf = mem_fileopen ();
8172 if (*prefix != '\0')
8173 {
8174 char *prefixed_name = typename_concat (NULL, prefix, name,
8175 physname, cu);
8176
8177 fputs_unfiltered (prefixed_name, buf);
8178 xfree (prefixed_name);
8179 }
8180 else
8181 fputs_unfiltered (name, buf);
8182
8183 /* Template parameters may be specified in the DIE's DW_AT_name, or
8184 as children with DW_TAG_template_type_param or
8185 DW_TAG_value_type_param. If the latter, add them to the name
8186 here. If the name already has template parameters, then
8187 skip this step; some versions of GCC emit both, and
8188 it is more efficient to use the pre-computed name.
8189
8190 Something to keep in mind about this process: it is very
8191 unlikely, or in some cases downright impossible, to produce
8192 something that will match the mangled name of a function.
8193 If the definition of the function has the same debug info,
8194 we should be able to match up with it anyway. But fallbacks
8195 using the minimal symbol, for instance to find a method
8196 implemented in a stripped copy of libstdc++, will not work.
8197 If we do not have debug info for the definition, we will have to
8198 match them up some other way.
8199
8200 When we do name matching there is a related problem with function
8201 templates; two instantiated function templates are allowed to
8202 differ only by their return types, which we do not add here. */
8203
8204 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8205 {
8206 struct attribute *attr;
8207 struct die_info *child;
8208 int first = 1;
8209
8210 die->building_fullname = 1;
8211
8212 for (child = die->child; child != NULL; child = child->sibling)
8213 {
8214 struct type *type;
8215 LONGEST value;
8216 const gdb_byte *bytes;
8217 struct dwarf2_locexpr_baton *baton;
8218 struct value *v;
8219
8220 if (child->tag != DW_TAG_template_type_param
8221 && child->tag != DW_TAG_template_value_param)
8222 continue;
8223
8224 if (first)
8225 {
8226 fputs_unfiltered ("<", buf);
8227 first = 0;
8228 }
8229 else
8230 fputs_unfiltered (", ", buf);
8231
8232 attr = dwarf2_attr (child, DW_AT_type, cu);
8233 if (attr == NULL)
8234 {
8235 complaint (&symfile_complaints,
8236 _("template parameter missing DW_AT_type"));
8237 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8238 continue;
8239 }
8240 type = die_type (child, cu);
8241
8242 if (child->tag == DW_TAG_template_type_param)
8243 {
8244 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
8245 continue;
8246 }
8247
8248 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8249 if (attr == NULL)
8250 {
8251 complaint (&symfile_complaints,
8252 _("template parameter missing "
8253 "DW_AT_const_value"));
8254 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8255 continue;
8256 }
8257
8258 dwarf2_const_value_attr (attr, type, name,
8259 &cu->comp_unit_obstack, cu,
8260 &value, &bytes, &baton);
8261
8262 if (TYPE_NOSIGN (type))
8263 /* GDB prints characters as NUMBER 'CHAR'. If that's
8264 changed, this can use value_print instead. */
8265 c_printchar (value, type, buf);
8266 else
8267 {
8268 struct value_print_options opts;
8269
8270 if (baton != NULL)
8271 v = dwarf2_evaluate_loc_desc (type, NULL,
8272 baton->data,
8273 baton->size,
8274 baton->per_cu);
8275 else if (bytes != NULL)
8276 {
8277 v = allocate_value (type);
8278 memcpy (value_contents_writeable (v), bytes,
8279 TYPE_LENGTH (type));
8280 }
8281 else
8282 v = value_from_longest (type, value);
8283
8284 /* Specify decimal so that we do not depend on
8285 the radix. */
8286 get_formatted_print_options (&opts, 'd');
8287 opts.raw = 1;
8288 value_print (v, buf, &opts);
8289 release_value (v);
8290 value_free (v);
8291 }
8292 }
8293
8294 die->building_fullname = 0;
8295
8296 if (!first)
8297 {
8298 /* Close the argument list, with a space if necessary
8299 (nested templates). */
8300 char last_char = '\0';
8301 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8302 if (last_char == '>')
8303 fputs_unfiltered (" >", buf);
8304 else
8305 fputs_unfiltered (">", buf);
8306 }
8307 }
8308
8309 /* For Java and C++ methods, append formal parameter type
8310 information, if PHYSNAME. */
8311
8312 if (physname && die->tag == DW_TAG_subprogram
8313 && (cu->language == language_cplus
8314 || cu->language == language_java))
8315 {
8316 struct type *type = read_type_die (die, cu);
8317
8318 c_type_print_args (type, buf, 1, cu->language,
8319 &type_print_raw_options);
8320
8321 if (cu->language == language_java)
8322 {
8323 /* For java, we must append the return type to method
8324 names. */
8325 if (die->tag == DW_TAG_subprogram)
8326 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8327 0, 0, &type_print_raw_options);
8328 }
8329 else if (cu->language == language_cplus)
8330 {
8331 /* Assume that an artificial first parameter is
8332 "this", but do not crash if it is not. RealView
8333 marks unnamed (and thus unused) parameters as
8334 artificial; there is no way to differentiate
8335 the two cases. */
8336 if (TYPE_NFIELDS (type) > 0
8337 && TYPE_FIELD_ARTIFICIAL (type, 0)
8338 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8339 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8340 0))))
8341 fputs_unfiltered (" const", buf);
8342 }
8343 }
8344
8345 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8346 &length);
8347 ui_file_delete (buf);
8348
8349 if (cu->language == language_cplus)
8350 {
8351 const char *cname
8352 = dwarf2_canonicalize_name (name, cu,
8353 &objfile->objfile_obstack);
8354
8355 if (cname != NULL)
8356 name = cname;
8357 }
8358 }
8359 }
8360
8361 return name;
8362 }
8363
8364 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8365 If scope qualifiers are appropriate they will be added. The result
8366 will be allocated on the objfile_obstack, or NULL if the DIE does
8367 not have a name. NAME may either be from a previous call to
8368 dwarf2_name or NULL.
8369
8370 The output string will be canonicalized (if C++/Java). */
8371
8372 static const char *
8373 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8374 {
8375 return dwarf2_compute_name (name, die, cu, 0);
8376 }
8377
8378 /* Construct a physname for the given DIE in CU. NAME may either be
8379 from a previous call to dwarf2_name or NULL. The result will be
8380 allocated on the objfile_objstack or NULL if the DIE does not have a
8381 name.
8382
8383 The output string will be canonicalized (if C++/Java). */
8384
8385 static const char *
8386 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8387 {
8388 struct objfile *objfile = cu->objfile;
8389 struct attribute *attr;
8390 const char *retval, *mangled = NULL, *canon = NULL;
8391 struct cleanup *back_to;
8392 int need_copy = 1;
8393
8394 /* In this case dwarf2_compute_name is just a shortcut not building anything
8395 on its own. */
8396 if (!die_needs_namespace (die, cu))
8397 return dwarf2_compute_name (name, die, cu, 1);
8398
8399 back_to = make_cleanup (null_cleanup, NULL);
8400
8401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8402 if (!attr)
8403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8404
8405 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8406 has computed. */
8407 if (attr && DW_STRING (attr))
8408 {
8409 char *demangled;
8410
8411 mangled = DW_STRING (attr);
8412
8413 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8414 type. It is easier for GDB users to search for such functions as
8415 `name(params)' than `long name(params)'. In such case the minimal
8416 symbol names do not match the full symbol names but for template
8417 functions there is never a need to look up their definition from their
8418 declaration so the only disadvantage remains the minimal symbol
8419 variant `long name(params)' does not have the proper inferior type.
8420 */
8421
8422 if (cu->language == language_go)
8423 {
8424 /* This is a lie, but we already lie to the caller new_symbol_full.
8425 new_symbol_full assumes we return the mangled name.
8426 This just undoes that lie until things are cleaned up. */
8427 demangled = NULL;
8428 }
8429 else
8430 {
8431 demangled = gdb_demangle (mangled,
8432 (DMGL_PARAMS | DMGL_ANSI
8433 | (cu->language == language_java
8434 ? DMGL_JAVA | DMGL_RET_POSTFIX
8435 : DMGL_RET_DROP)));
8436 }
8437 if (demangled)
8438 {
8439 make_cleanup (xfree, demangled);
8440 canon = demangled;
8441 }
8442 else
8443 {
8444 canon = mangled;
8445 need_copy = 0;
8446 }
8447 }
8448
8449 if (canon == NULL || check_physname)
8450 {
8451 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8452
8453 if (canon != NULL && strcmp (physname, canon) != 0)
8454 {
8455 /* It may not mean a bug in GDB. The compiler could also
8456 compute DW_AT_linkage_name incorrectly. But in such case
8457 GDB would need to be bug-to-bug compatible. */
8458
8459 complaint (&symfile_complaints,
8460 _("Computed physname <%s> does not match demangled <%s> "
8461 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8462 physname, canon, mangled, die->offset.sect_off,
8463 objfile_name (objfile));
8464
8465 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8466 is available here - over computed PHYSNAME. It is safer
8467 against both buggy GDB and buggy compilers. */
8468
8469 retval = canon;
8470 }
8471 else
8472 {
8473 retval = physname;
8474 need_copy = 0;
8475 }
8476 }
8477 else
8478 retval = canon;
8479
8480 if (need_copy)
8481 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8482
8483 do_cleanups (back_to);
8484 return retval;
8485 }
8486
8487 /* Read the import statement specified by the given die and record it. */
8488
8489 static void
8490 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8491 {
8492 struct objfile *objfile = cu->objfile;
8493 struct attribute *import_attr;
8494 struct die_info *imported_die, *child_die;
8495 struct dwarf2_cu *imported_cu;
8496 const char *imported_name;
8497 const char *imported_name_prefix;
8498 const char *canonical_name;
8499 const char *import_alias;
8500 const char *imported_declaration = NULL;
8501 const char *import_prefix;
8502 VEC (const_char_ptr) *excludes = NULL;
8503 struct cleanup *cleanups;
8504
8505 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8506 if (import_attr == NULL)
8507 {
8508 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8509 dwarf_tag_name (die->tag));
8510 return;
8511 }
8512
8513 imported_cu = cu;
8514 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8515 imported_name = dwarf2_name (imported_die, imported_cu);
8516 if (imported_name == NULL)
8517 {
8518 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8519
8520 The import in the following code:
8521 namespace A
8522 {
8523 typedef int B;
8524 }
8525
8526 int main ()
8527 {
8528 using A::B;
8529 B b;
8530 return b;
8531 }
8532
8533 ...
8534 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8535 <52> DW_AT_decl_file : 1
8536 <53> DW_AT_decl_line : 6
8537 <54> DW_AT_import : <0x75>
8538 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8539 <59> DW_AT_name : B
8540 <5b> DW_AT_decl_file : 1
8541 <5c> DW_AT_decl_line : 2
8542 <5d> DW_AT_type : <0x6e>
8543 ...
8544 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8545 <76> DW_AT_byte_size : 4
8546 <77> DW_AT_encoding : 5 (signed)
8547
8548 imports the wrong die ( 0x75 instead of 0x58 ).
8549 This case will be ignored until the gcc bug is fixed. */
8550 return;
8551 }
8552
8553 /* Figure out the local name after import. */
8554 import_alias = dwarf2_name (die, cu);
8555
8556 /* Figure out where the statement is being imported to. */
8557 import_prefix = determine_prefix (die, cu);
8558
8559 /* Figure out what the scope of the imported die is and prepend it
8560 to the name of the imported die. */
8561 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8562
8563 if (imported_die->tag != DW_TAG_namespace
8564 && imported_die->tag != DW_TAG_module)
8565 {
8566 imported_declaration = imported_name;
8567 canonical_name = imported_name_prefix;
8568 }
8569 else if (strlen (imported_name_prefix) > 0)
8570 canonical_name = obconcat (&objfile->objfile_obstack,
8571 imported_name_prefix, "::", imported_name,
8572 (char *) NULL);
8573 else
8574 canonical_name = imported_name;
8575
8576 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8577
8578 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8579 for (child_die = die->child; child_die && child_die->tag;
8580 child_die = sibling_die (child_die))
8581 {
8582 /* DWARF-4: A Fortran use statement with a “rename list” may be
8583 represented by an imported module entry with an import attribute
8584 referring to the module and owned entries corresponding to those
8585 entities that are renamed as part of being imported. */
8586
8587 if (child_die->tag != DW_TAG_imported_declaration)
8588 {
8589 complaint (&symfile_complaints,
8590 _("child DW_TAG_imported_declaration expected "
8591 "- DIE at 0x%x [in module %s]"),
8592 child_die->offset.sect_off, objfile_name (objfile));
8593 continue;
8594 }
8595
8596 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8597 if (import_attr == NULL)
8598 {
8599 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8600 dwarf_tag_name (child_die->tag));
8601 continue;
8602 }
8603
8604 imported_cu = cu;
8605 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8606 &imported_cu);
8607 imported_name = dwarf2_name (imported_die, imported_cu);
8608 if (imported_name == NULL)
8609 {
8610 complaint (&symfile_complaints,
8611 _("child DW_TAG_imported_declaration has unknown "
8612 "imported name - DIE at 0x%x [in module %s]"),
8613 child_die->offset.sect_off, objfile_name (objfile));
8614 continue;
8615 }
8616
8617 VEC_safe_push (const_char_ptr, excludes, imported_name);
8618
8619 process_die (child_die, cu);
8620 }
8621
8622 cp_add_using_directive (import_prefix,
8623 canonical_name,
8624 import_alias,
8625 imported_declaration,
8626 excludes,
8627 0,
8628 &objfile->objfile_obstack);
8629
8630 do_cleanups (cleanups);
8631 }
8632
8633 /* Cleanup function for handle_DW_AT_stmt_list. */
8634
8635 static void
8636 free_cu_line_header (void *arg)
8637 {
8638 struct dwarf2_cu *cu = arg;
8639
8640 free_line_header (cu->line_header);
8641 cu->line_header = NULL;
8642 }
8643
8644 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8645 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8646 this, it was first present in GCC release 4.3.0. */
8647
8648 static int
8649 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8650 {
8651 if (!cu->checked_producer)
8652 check_producer (cu);
8653
8654 return cu->producer_is_gcc_lt_4_3;
8655 }
8656
8657 static void
8658 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8659 const char **name, const char **comp_dir)
8660 {
8661 struct attribute *attr;
8662
8663 *name = NULL;
8664 *comp_dir = NULL;
8665
8666 /* Find the filename. Do not use dwarf2_name here, since the filename
8667 is not a source language identifier. */
8668 attr = dwarf2_attr (die, DW_AT_name, cu);
8669 if (attr)
8670 {
8671 *name = DW_STRING (attr);
8672 }
8673
8674 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8675 if (attr)
8676 *comp_dir = DW_STRING (attr);
8677 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8678 && IS_ABSOLUTE_PATH (*name))
8679 {
8680 char *d = ldirname (*name);
8681
8682 *comp_dir = d;
8683 if (d != NULL)
8684 make_cleanup (xfree, d);
8685 }
8686 if (*comp_dir != NULL)
8687 {
8688 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8689 directory, get rid of it. */
8690 char *cp = strchr (*comp_dir, ':');
8691
8692 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8693 *comp_dir = cp + 1;
8694 }
8695
8696 if (*name == NULL)
8697 *name = "<unknown>";
8698 }
8699
8700 /* Handle DW_AT_stmt_list for a compilation unit.
8701 DIE is the DW_TAG_compile_unit die for CU.
8702 COMP_DIR is the compilation directory.
8703 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8704
8705 static void
8706 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8707 const char *comp_dir) /* ARI: editCase function */
8708 {
8709 struct attribute *attr;
8710
8711 gdb_assert (! cu->per_cu->is_debug_types);
8712
8713 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8714 if (attr)
8715 {
8716 unsigned int line_offset = DW_UNSND (attr);
8717 struct line_header *line_header
8718 = dwarf_decode_line_header (line_offset, cu);
8719
8720 if (line_header)
8721 {
8722 cu->line_header = line_header;
8723 make_cleanup (free_cu_line_header, cu);
8724 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8725 }
8726 }
8727 }
8728
8729 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8730
8731 static void
8732 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8733 {
8734 struct objfile *objfile = dwarf2_per_objfile->objfile;
8735 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8736 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8737 CORE_ADDR highpc = ((CORE_ADDR) 0);
8738 struct attribute *attr;
8739 const char *name = NULL;
8740 const char *comp_dir = NULL;
8741 struct die_info *child_die;
8742 bfd *abfd = objfile->obfd;
8743 CORE_ADDR baseaddr;
8744
8745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8746
8747 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8748
8749 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8750 from finish_block. */
8751 if (lowpc == ((CORE_ADDR) -1))
8752 lowpc = highpc;
8753 lowpc += baseaddr;
8754 highpc += baseaddr;
8755
8756 find_file_and_directory (die, cu, &name, &comp_dir);
8757
8758 prepare_one_comp_unit (cu, die, cu->language);
8759
8760 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8761 standardised yet. As a workaround for the language detection we fall
8762 back to the DW_AT_producer string. */
8763 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8764 cu->language = language_opencl;
8765
8766 /* Similar hack for Go. */
8767 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8768 set_cu_language (DW_LANG_Go, cu);
8769
8770 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8771
8772 /* Decode line number information if present. We do this before
8773 processing child DIEs, so that the line header table is available
8774 for DW_AT_decl_file. */
8775 handle_DW_AT_stmt_list (die, cu, comp_dir);
8776
8777 /* Process all dies in compilation unit. */
8778 if (die->child != NULL)
8779 {
8780 child_die = die->child;
8781 while (child_die && child_die->tag)
8782 {
8783 process_die (child_die, cu);
8784 child_die = sibling_die (child_die);
8785 }
8786 }
8787
8788 /* Decode macro information, if present. Dwarf 2 macro information
8789 refers to information in the line number info statement program
8790 header, so we can only read it if we've read the header
8791 successfully. */
8792 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8793 if (attr && cu->line_header)
8794 {
8795 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8796 complaint (&symfile_complaints,
8797 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8798
8799 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8800 }
8801 else
8802 {
8803 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8804 if (attr && cu->line_header)
8805 {
8806 unsigned int macro_offset = DW_UNSND (attr);
8807
8808 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8809 }
8810 }
8811
8812 do_cleanups (back_to);
8813 }
8814
8815 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8816 Create the set of symtabs used by this TU, or if this TU is sharing
8817 symtabs with another TU and the symtabs have already been created
8818 then restore those symtabs in the line header.
8819 We don't need the pc/line-number mapping for type units. */
8820
8821 static void
8822 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8823 {
8824 struct objfile *objfile = dwarf2_per_objfile->objfile;
8825 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8826 struct type_unit_group *tu_group;
8827 int first_time;
8828 struct line_header *lh;
8829 struct attribute *attr;
8830 unsigned int i, line_offset;
8831 struct signatured_type *sig_type;
8832
8833 gdb_assert (per_cu->is_debug_types);
8834 sig_type = (struct signatured_type *) per_cu;
8835
8836 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8837
8838 /* If we're using .gdb_index (includes -readnow) then
8839 per_cu->type_unit_group may not have been set up yet. */
8840 if (sig_type->type_unit_group == NULL)
8841 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8842 tu_group = sig_type->type_unit_group;
8843
8844 /* If we've already processed this stmt_list there's no real need to
8845 do it again, we could fake it and just recreate the part we need
8846 (file name,index -> symtab mapping). If data shows this optimization
8847 is useful we can do it then. */
8848 first_time = tu_group->primary_symtab == NULL;
8849
8850 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8851 debug info. */
8852 lh = NULL;
8853 if (attr != NULL)
8854 {
8855 line_offset = DW_UNSND (attr);
8856 lh = dwarf_decode_line_header (line_offset, cu);
8857 }
8858 if (lh == NULL)
8859 {
8860 if (first_time)
8861 dwarf2_start_symtab (cu, "", NULL, 0);
8862 else
8863 {
8864 gdb_assert (tu_group->symtabs == NULL);
8865 restart_symtab (0);
8866 }
8867 /* Note: The primary symtab will get allocated at the end. */
8868 return;
8869 }
8870
8871 cu->line_header = lh;
8872 make_cleanup (free_cu_line_header, cu);
8873
8874 if (first_time)
8875 {
8876 dwarf2_start_symtab (cu, "", NULL, 0);
8877
8878 tu_group->num_symtabs = lh->num_file_names;
8879 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8880
8881 for (i = 0; i < lh->num_file_names; ++i)
8882 {
8883 const char *dir = NULL;
8884 struct file_entry *fe = &lh->file_names[i];
8885
8886 if (fe->dir_index)
8887 dir = lh->include_dirs[fe->dir_index - 1];
8888 dwarf2_start_subfile (fe->name, dir, NULL);
8889
8890 /* Note: We don't have to watch for the main subfile here, type units
8891 don't have DW_AT_name. */
8892
8893 if (current_subfile->symtab == NULL)
8894 {
8895 /* NOTE: start_subfile will recognize when it's been passed
8896 a file it has already seen. So we can't assume there's a
8897 simple mapping from lh->file_names to subfiles,
8898 lh->file_names may contain dups. */
8899 current_subfile->symtab = allocate_symtab (current_subfile->name,
8900 objfile);
8901 }
8902
8903 fe->symtab = current_subfile->symtab;
8904 tu_group->symtabs[i] = fe->symtab;
8905 }
8906 }
8907 else
8908 {
8909 restart_symtab (0);
8910
8911 for (i = 0; i < lh->num_file_names; ++i)
8912 {
8913 struct file_entry *fe = &lh->file_names[i];
8914
8915 fe->symtab = tu_group->symtabs[i];
8916 }
8917 }
8918
8919 /* The main symtab is allocated last. Type units don't have DW_AT_name
8920 so they don't have a "real" (so to speak) symtab anyway.
8921 There is later code that will assign the main symtab to all symbols
8922 that don't have one. We need to handle the case of a symbol with a
8923 missing symtab (DW_AT_decl_file) anyway. */
8924 }
8925
8926 /* Process DW_TAG_type_unit.
8927 For TUs we want to skip the first top level sibling if it's not the
8928 actual type being defined by this TU. In this case the first top
8929 level sibling is there to provide context only. */
8930
8931 static void
8932 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8933 {
8934 struct die_info *child_die;
8935
8936 prepare_one_comp_unit (cu, die, language_minimal);
8937
8938 /* Initialize (or reinitialize) the machinery for building symtabs.
8939 We do this before processing child DIEs, so that the line header table
8940 is available for DW_AT_decl_file. */
8941 setup_type_unit_groups (die, cu);
8942
8943 if (die->child != NULL)
8944 {
8945 child_die = die->child;
8946 while (child_die && child_die->tag)
8947 {
8948 process_die (child_die, cu);
8949 child_die = sibling_die (child_die);
8950 }
8951 }
8952 }
8953 \f
8954 /* DWO/DWP files.
8955
8956 http://gcc.gnu.org/wiki/DebugFission
8957 http://gcc.gnu.org/wiki/DebugFissionDWP
8958
8959 To simplify handling of both DWO files ("object" files with the DWARF info)
8960 and DWP files (a file with the DWOs packaged up into one file), we treat
8961 DWP files as having a collection of virtual DWO files. */
8962
8963 static hashval_t
8964 hash_dwo_file (const void *item)
8965 {
8966 const struct dwo_file *dwo_file = item;
8967 hashval_t hash;
8968
8969 hash = htab_hash_string (dwo_file->dwo_name);
8970 if (dwo_file->comp_dir != NULL)
8971 hash += htab_hash_string (dwo_file->comp_dir);
8972 return hash;
8973 }
8974
8975 static int
8976 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8977 {
8978 const struct dwo_file *lhs = item_lhs;
8979 const struct dwo_file *rhs = item_rhs;
8980
8981 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8982 return 0;
8983 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8984 return lhs->comp_dir == rhs->comp_dir;
8985 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8986 }
8987
8988 /* Allocate a hash table for DWO files. */
8989
8990 static htab_t
8991 allocate_dwo_file_hash_table (void)
8992 {
8993 struct objfile *objfile = dwarf2_per_objfile->objfile;
8994
8995 return htab_create_alloc_ex (41,
8996 hash_dwo_file,
8997 eq_dwo_file,
8998 NULL,
8999 &objfile->objfile_obstack,
9000 hashtab_obstack_allocate,
9001 dummy_obstack_deallocate);
9002 }
9003
9004 /* Lookup DWO file DWO_NAME. */
9005
9006 static void **
9007 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9008 {
9009 struct dwo_file find_entry;
9010 void **slot;
9011
9012 if (dwarf2_per_objfile->dwo_files == NULL)
9013 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9014
9015 memset (&find_entry, 0, sizeof (find_entry));
9016 find_entry.dwo_name = dwo_name;
9017 find_entry.comp_dir = comp_dir;
9018 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9019
9020 return slot;
9021 }
9022
9023 static hashval_t
9024 hash_dwo_unit (const void *item)
9025 {
9026 const struct dwo_unit *dwo_unit = item;
9027
9028 /* This drops the top 32 bits of the id, but is ok for a hash. */
9029 return dwo_unit->signature;
9030 }
9031
9032 static int
9033 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9034 {
9035 const struct dwo_unit *lhs = item_lhs;
9036 const struct dwo_unit *rhs = item_rhs;
9037
9038 /* The signature is assumed to be unique within the DWO file.
9039 So while object file CU dwo_id's always have the value zero,
9040 that's OK, assuming each object file DWO file has only one CU,
9041 and that's the rule for now. */
9042 return lhs->signature == rhs->signature;
9043 }
9044
9045 /* Allocate a hash table for DWO CUs,TUs.
9046 There is one of these tables for each of CUs,TUs for each DWO file. */
9047
9048 static htab_t
9049 allocate_dwo_unit_table (struct objfile *objfile)
9050 {
9051 /* Start out with a pretty small number.
9052 Generally DWO files contain only one CU and maybe some TUs. */
9053 return htab_create_alloc_ex (3,
9054 hash_dwo_unit,
9055 eq_dwo_unit,
9056 NULL,
9057 &objfile->objfile_obstack,
9058 hashtab_obstack_allocate,
9059 dummy_obstack_deallocate);
9060 }
9061
9062 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9063
9064 struct create_dwo_cu_data
9065 {
9066 struct dwo_file *dwo_file;
9067 struct dwo_unit dwo_unit;
9068 };
9069
9070 /* die_reader_func for create_dwo_cu. */
9071
9072 static void
9073 create_dwo_cu_reader (const struct die_reader_specs *reader,
9074 const gdb_byte *info_ptr,
9075 struct die_info *comp_unit_die,
9076 int has_children,
9077 void *datap)
9078 {
9079 struct dwarf2_cu *cu = reader->cu;
9080 struct objfile *objfile = dwarf2_per_objfile->objfile;
9081 sect_offset offset = cu->per_cu->offset;
9082 struct dwarf2_section_info *section = cu->per_cu->section;
9083 struct create_dwo_cu_data *data = datap;
9084 struct dwo_file *dwo_file = data->dwo_file;
9085 struct dwo_unit *dwo_unit = &data->dwo_unit;
9086 struct attribute *attr;
9087
9088 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9089 if (attr == NULL)
9090 {
9091 complaint (&symfile_complaints,
9092 _("Dwarf Error: debug entry at offset 0x%x is missing"
9093 " its dwo_id [in module %s]"),
9094 offset.sect_off, dwo_file->dwo_name);
9095 return;
9096 }
9097
9098 dwo_unit->dwo_file = dwo_file;
9099 dwo_unit->signature = DW_UNSND (attr);
9100 dwo_unit->section = section;
9101 dwo_unit->offset = offset;
9102 dwo_unit->length = cu->per_cu->length;
9103
9104 if (dwarf2_read_debug)
9105 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9106 offset.sect_off, hex_string (dwo_unit->signature));
9107 }
9108
9109 /* Create the dwo_unit for the lone CU in DWO_FILE.
9110 Note: This function processes DWO files only, not DWP files. */
9111
9112 static struct dwo_unit *
9113 create_dwo_cu (struct dwo_file *dwo_file)
9114 {
9115 struct objfile *objfile = dwarf2_per_objfile->objfile;
9116 struct dwarf2_section_info *section = &dwo_file->sections.info;
9117 bfd *abfd;
9118 htab_t cu_htab;
9119 const gdb_byte *info_ptr, *end_ptr;
9120 struct create_dwo_cu_data create_dwo_cu_data;
9121 struct dwo_unit *dwo_unit;
9122
9123 dwarf2_read_section (objfile, section);
9124 info_ptr = section->buffer;
9125
9126 if (info_ptr == NULL)
9127 return NULL;
9128
9129 /* We can't set abfd until now because the section may be empty or
9130 not present, in which case section->asection will be NULL. */
9131 abfd = get_section_bfd_owner (section);
9132
9133 if (dwarf2_read_debug)
9134 {
9135 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9136 get_section_name (section),
9137 get_section_file_name (section));
9138 }
9139
9140 create_dwo_cu_data.dwo_file = dwo_file;
9141 dwo_unit = NULL;
9142
9143 end_ptr = info_ptr + section->size;
9144 while (info_ptr < end_ptr)
9145 {
9146 struct dwarf2_per_cu_data per_cu;
9147
9148 memset (&create_dwo_cu_data.dwo_unit, 0,
9149 sizeof (create_dwo_cu_data.dwo_unit));
9150 memset (&per_cu, 0, sizeof (per_cu));
9151 per_cu.objfile = objfile;
9152 per_cu.is_debug_types = 0;
9153 per_cu.offset.sect_off = info_ptr - section->buffer;
9154 per_cu.section = section;
9155
9156 init_cutu_and_read_dies_no_follow (&per_cu,
9157 &dwo_file->sections.abbrev,
9158 dwo_file,
9159 create_dwo_cu_reader,
9160 &create_dwo_cu_data);
9161
9162 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9163 {
9164 /* If we've already found one, complain. We only support one
9165 because having more than one requires hacking the dwo_name of
9166 each to match, which is highly unlikely to happen. */
9167 if (dwo_unit != NULL)
9168 {
9169 complaint (&symfile_complaints,
9170 _("Multiple CUs in DWO file %s [in module %s]"),
9171 dwo_file->dwo_name, objfile_name (objfile));
9172 break;
9173 }
9174
9175 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9176 *dwo_unit = create_dwo_cu_data.dwo_unit;
9177 }
9178
9179 info_ptr += per_cu.length;
9180 }
9181
9182 return dwo_unit;
9183 }
9184
9185 /* DWP file .debug_{cu,tu}_index section format:
9186 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9187
9188 DWP Version 1:
9189
9190 Both index sections have the same format, and serve to map a 64-bit
9191 signature to a set of section numbers. Each section begins with a header,
9192 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9193 indexes, and a pool of 32-bit section numbers. The index sections will be
9194 aligned at 8-byte boundaries in the file.
9195
9196 The index section header consists of:
9197
9198 V, 32 bit version number
9199 -, 32 bits unused
9200 N, 32 bit number of compilation units or type units in the index
9201 M, 32 bit number of slots in the hash table
9202
9203 Numbers are recorded using the byte order of the application binary.
9204
9205 The hash table begins at offset 16 in the section, and consists of an array
9206 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9207 order of the application binary). Unused slots in the hash table are 0.
9208 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9209
9210 The parallel table begins immediately after the hash table
9211 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9212 array of 32-bit indexes (using the byte order of the application binary),
9213 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9214 table contains a 32-bit index into the pool of section numbers. For unused
9215 hash table slots, the corresponding entry in the parallel table will be 0.
9216
9217 The pool of section numbers begins immediately following the hash table
9218 (at offset 16 + 12 * M from the beginning of the section). The pool of
9219 section numbers consists of an array of 32-bit words (using the byte order
9220 of the application binary). Each item in the array is indexed starting
9221 from 0. The hash table entry provides the index of the first section
9222 number in the set. Additional section numbers in the set follow, and the
9223 set is terminated by a 0 entry (section number 0 is not used in ELF).
9224
9225 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9226 section must be the first entry in the set, and the .debug_abbrev.dwo must
9227 be the second entry. Other members of the set may follow in any order.
9228
9229 ---
9230
9231 DWP Version 2:
9232
9233 DWP Version 2 combines all the .debug_info, etc. sections into one,
9234 and the entries in the index tables are now offsets into these sections.
9235 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9236 section.
9237
9238 Index Section Contents:
9239 Header
9240 Hash Table of Signatures dwp_hash_table.hash_table
9241 Parallel Table of Indices dwp_hash_table.unit_table
9242 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9243 Table of Section Sizes dwp_hash_table.v2.sizes
9244
9245 The index section header consists of:
9246
9247 V, 32 bit version number
9248 L, 32 bit number of columns in the table of section offsets
9249 N, 32 bit number of compilation units or type units in the index
9250 M, 32 bit number of slots in the hash table
9251
9252 Numbers are recorded using the byte order of the application binary.
9253
9254 The hash table has the same format as version 1.
9255 The parallel table of indices has the same format as version 1,
9256 except that the entries are origin-1 indices into the table of sections
9257 offsets and the table of section sizes.
9258
9259 The table of offsets begins immediately following the parallel table
9260 (at offset 16 + 12 * M from the beginning of the section). The table is
9261 a two-dimensional array of 32-bit words (using the byte order of the
9262 application binary), with L columns and N+1 rows, in row-major order.
9263 Each row in the array is indexed starting from 0. The first row provides
9264 a key to the remaining rows: each column in this row provides an identifier
9265 for a debug section, and the offsets in the same column of subsequent rows
9266 refer to that section. The section identifiers are:
9267
9268 DW_SECT_INFO 1 .debug_info.dwo
9269 DW_SECT_TYPES 2 .debug_types.dwo
9270 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9271 DW_SECT_LINE 4 .debug_line.dwo
9272 DW_SECT_LOC 5 .debug_loc.dwo
9273 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9274 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9275 DW_SECT_MACRO 8 .debug_macro.dwo
9276
9277 The offsets provided by the CU and TU index sections are the base offsets
9278 for the contributions made by each CU or TU to the corresponding section
9279 in the package file. Each CU and TU header contains an abbrev_offset
9280 field, used to find the abbreviations table for that CU or TU within the
9281 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9282 be interpreted as relative to the base offset given in the index section.
9283 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9284 should be interpreted as relative to the base offset for .debug_line.dwo,
9285 and offsets into other debug sections obtained from DWARF attributes should
9286 also be interpreted as relative to the corresponding base offset.
9287
9288 The table of sizes begins immediately following the table of offsets.
9289 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9290 with L columns and N rows, in row-major order. Each row in the array is
9291 indexed starting from 1 (row 0 is shared by the two tables).
9292
9293 ---
9294
9295 Hash table lookup is handled the same in version 1 and 2:
9296
9297 We assume that N and M will not exceed 2^32 - 1.
9298 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9299
9300 Given a 64-bit compilation unit signature or a type signature S, an entry
9301 in the hash table is located as follows:
9302
9303 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9304 the low-order k bits all set to 1.
9305
9306 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9307
9308 3) If the hash table entry at index H matches the signature, use that
9309 entry. If the hash table entry at index H is unused (all zeroes),
9310 terminate the search: the signature is not present in the table.
9311
9312 4) Let H = (H + H') modulo M. Repeat at Step 3.
9313
9314 Because M > N and H' and M are relatively prime, the search is guaranteed
9315 to stop at an unused slot or find the match. */
9316
9317 /* Create a hash table to map DWO IDs to their CU/TU entry in
9318 .debug_{info,types}.dwo in DWP_FILE.
9319 Returns NULL if there isn't one.
9320 Note: This function processes DWP files only, not DWO files. */
9321
9322 static struct dwp_hash_table *
9323 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9324 {
9325 struct objfile *objfile = dwarf2_per_objfile->objfile;
9326 bfd *dbfd = dwp_file->dbfd;
9327 const gdb_byte *index_ptr, *index_end;
9328 struct dwarf2_section_info *index;
9329 uint32_t version, nr_columns, nr_units, nr_slots;
9330 struct dwp_hash_table *htab;
9331
9332 if (is_debug_types)
9333 index = &dwp_file->sections.tu_index;
9334 else
9335 index = &dwp_file->sections.cu_index;
9336
9337 if (dwarf2_section_empty_p (index))
9338 return NULL;
9339 dwarf2_read_section (objfile, index);
9340
9341 index_ptr = index->buffer;
9342 index_end = index_ptr + index->size;
9343
9344 version = read_4_bytes (dbfd, index_ptr);
9345 index_ptr += 4;
9346 if (version == 2)
9347 nr_columns = read_4_bytes (dbfd, index_ptr);
9348 else
9349 nr_columns = 0;
9350 index_ptr += 4;
9351 nr_units = read_4_bytes (dbfd, index_ptr);
9352 index_ptr += 4;
9353 nr_slots = read_4_bytes (dbfd, index_ptr);
9354 index_ptr += 4;
9355
9356 if (version != 1 && version != 2)
9357 {
9358 error (_("Dwarf Error: unsupported DWP file version (%s)"
9359 " [in module %s]"),
9360 pulongest (version), dwp_file->name);
9361 }
9362 if (nr_slots != (nr_slots & -nr_slots))
9363 {
9364 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9365 " is not power of 2 [in module %s]"),
9366 pulongest (nr_slots), dwp_file->name);
9367 }
9368
9369 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9370 htab->version = version;
9371 htab->nr_columns = nr_columns;
9372 htab->nr_units = nr_units;
9373 htab->nr_slots = nr_slots;
9374 htab->hash_table = index_ptr;
9375 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9376
9377 /* Exit early if the table is empty. */
9378 if (nr_slots == 0 || nr_units == 0
9379 || (version == 2 && nr_columns == 0))
9380 {
9381 /* All must be zero. */
9382 if (nr_slots != 0 || nr_units != 0
9383 || (version == 2 && nr_columns != 0))
9384 {
9385 complaint (&symfile_complaints,
9386 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9387 " all zero [in modules %s]"),
9388 dwp_file->name);
9389 }
9390 return htab;
9391 }
9392
9393 if (version == 1)
9394 {
9395 htab->section_pool.v1.indices =
9396 htab->unit_table + sizeof (uint32_t) * nr_slots;
9397 /* It's harder to decide whether the section is too small in v1.
9398 V1 is deprecated anyway so we punt. */
9399 }
9400 else
9401 {
9402 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9403 int *ids = htab->section_pool.v2.section_ids;
9404 /* Reverse map for error checking. */
9405 int ids_seen[DW_SECT_MAX + 1];
9406 int i;
9407
9408 if (nr_columns < 2)
9409 {
9410 error (_("Dwarf Error: bad DWP hash table, too few columns"
9411 " in section table [in module %s]"),
9412 dwp_file->name);
9413 }
9414 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9415 {
9416 error (_("Dwarf Error: bad DWP hash table, too many columns"
9417 " in section table [in module %s]"),
9418 dwp_file->name);
9419 }
9420 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9421 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9422 for (i = 0; i < nr_columns; ++i)
9423 {
9424 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9425
9426 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9427 {
9428 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9429 " in section table [in module %s]"),
9430 id, dwp_file->name);
9431 }
9432 if (ids_seen[id] != -1)
9433 {
9434 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9435 " id %d in section table [in module %s]"),
9436 id, dwp_file->name);
9437 }
9438 ids_seen[id] = i;
9439 ids[i] = id;
9440 }
9441 /* Must have exactly one info or types section. */
9442 if (((ids_seen[DW_SECT_INFO] != -1)
9443 + (ids_seen[DW_SECT_TYPES] != -1))
9444 != 1)
9445 {
9446 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9447 " DWO info/types section [in module %s]"),
9448 dwp_file->name);
9449 }
9450 /* Must have an abbrev section. */
9451 if (ids_seen[DW_SECT_ABBREV] == -1)
9452 {
9453 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9454 " section [in module %s]"),
9455 dwp_file->name);
9456 }
9457 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9458 htab->section_pool.v2.sizes =
9459 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9460 * nr_units * nr_columns);
9461 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9462 * nr_units * nr_columns))
9463 > index_end)
9464 {
9465 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9466 " [in module %s]"),
9467 dwp_file->name);
9468 }
9469 }
9470
9471 return htab;
9472 }
9473
9474 /* Update SECTIONS with the data from SECTP.
9475
9476 This function is like the other "locate" section routines that are
9477 passed to bfd_map_over_sections, but in this context the sections to
9478 read comes from the DWP V1 hash table, not the full ELF section table.
9479
9480 The result is non-zero for success, or zero if an error was found. */
9481
9482 static int
9483 locate_v1_virtual_dwo_sections (asection *sectp,
9484 struct virtual_v1_dwo_sections *sections)
9485 {
9486 const struct dwop_section_names *names = &dwop_section_names;
9487
9488 if (section_is_p (sectp->name, &names->abbrev_dwo))
9489 {
9490 /* There can be only one. */
9491 if (sections->abbrev.s.asection != NULL)
9492 return 0;
9493 sections->abbrev.s.asection = sectp;
9494 sections->abbrev.size = bfd_get_section_size (sectp);
9495 }
9496 else if (section_is_p (sectp->name, &names->info_dwo)
9497 || section_is_p (sectp->name, &names->types_dwo))
9498 {
9499 /* There can be only one. */
9500 if (sections->info_or_types.s.asection != NULL)
9501 return 0;
9502 sections->info_or_types.s.asection = sectp;
9503 sections->info_or_types.size = bfd_get_section_size (sectp);
9504 }
9505 else if (section_is_p (sectp->name, &names->line_dwo))
9506 {
9507 /* There can be only one. */
9508 if (sections->line.s.asection != NULL)
9509 return 0;
9510 sections->line.s.asection = sectp;
9511 sections->line.size = bfd_get_section_size (sectp);
9512 }
9513 else if (section_is_p (sectp->name, &names->loc_dwo))
9514 {
9515 /* There can be only one. */
9516 if (sections->loc.s.asection != NULL)
9517 return 0;
9518 sections->loc.s.asection = sectp;
9519 sections->loc.size = bfd_get_section_size (sectp);
9520 }
9521 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9522 {
9523 /* There can be only one. */
9524 if (sections->macinfo.s.asection != NULL)
9525 return 0;
9526 sections->macinfo.s.asection = sectp;
9527 sections->macinfo.size = bfd_get_section_size (sectp);
9528 }
9529 else if (section_is_p (sectp->name, &names->macro_dwo))
9530 {
9531 /* There can be only one. */
9532 if (sections->macro.s.asection != NULL)
9533 return 0;
9534 sections->macro.s.asection = sectp;
9535 sections->macro.size = bfd_get_section_size (sectp);
9536 }
9537 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9538 {
9539 /* There can be only one. */
9540 if (sections->str_offsets.s.asection != NULL)
9541 return 0;
9542 sections->str_offsets.s.asection = sectp;
9543 sections->str_offsets.size = bfd_get_section_size (sectp);
9544 }
9545 else
9546 {
9547 /* No other kind of section is valid. */
9548 return 0;
9549 }
9550
9551 return 1;
9552 }
9553
9554 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9555 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9556 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9557 This is for DWP version 1 files. */
9558
9559 static struct dwo_unit *
9560 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
9561 uint32_t unit_index,
9562 const char *comp_dir,
9563 ULONGEST signature, int is_debug_types)
9564 {
9565 struct objfile *objfile = dwarf2_per_objfile->objfile;
9566 const struct dwp_hash_table *dwp_htab =
9567 is_debug_types ? dwp_file->tus : dwp_file->cus;
9568 bfd *dbfd = dwp_file->dbfd;
9569 const char *kind = is_debug_types ? "TU" : "CU";
9570 struct dwo_file *dwo_file;
9571 struct dwo_unit *dwo_unit;
9572 struct virtual_v1_dwo_sections sections;
9573 void **dwo_file_slot;
9574 char *virtual_dwo_name;
9575 struct dwarf2_section_info *cutu;
9576 struct cleanup *cleanups;
9577 int i;
9578
9579 gdb_assert (dwp_file->version == 1);
9580
9581 if (dwarf2_read_debug)
9582 {
9583 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
9584 kind,
9585 pulongest (unit_index), hex_string (signature),
9586 dwp_file->name);
9587 }
9588
9589 /* Fetch the sections of this DWO unit.
9590 Put a limit on the number of sections we look for so that bad data
9591 doesn't cause us to loop forever. */
9592
9593 #define MAX_NR_V1_DWO_SECTIONS \
9594 (1 /* .debug_info or .debug_types */ \
9595 + 1 /* .debug_abbrev */ \
9596 + 1 /* .debug_line */ \
9597 + 1 /* .debug_loc */ \
9598 + 1 /* .debug_str_offsets */ \
9599 + 1 /* .debug_macro or .debug_macinfo */ \
9600 + 1 /* trailing zero */)
9601
9602 memset (&sections, 0, sizeof (sections));
9603 cleanups = make_cleanup (null_cleanup, 0);
9604
9605 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
9606 {
9607 asection *sectp;
9608 uint32_t section_nr =
9609 read_4_bytes (dbfd,
9610 dwp_htab->section_pool.v1.indices
9611 + (unit_index + i) * sizeof (uint32_t));
9612
9613 if (section_nr == 0)
9614 break;
9615 if (section_nr >= dwp_file->num_sections)
9616 {
9617 error (_("Dwarf Error: bad DWP hash table, section number too large"
9618 " [in module %s]"),
9619 dwp_file->name);
9620 }
9621
9622 sectp = dwp_file->elf_sections[section_nr];
9623 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
9624 {
9625 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9626 " [in module %s]"),
9627 dwp_file->name);
9628 }
9629 }
9630
9631 if (i < 2
9632 || dwarf2_section_empty_p (&sections.info_or_types)
9633 || dwarf2_section_empty_p (&sections.abbrev))
9634 {
9635 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9636 " [in module %s]"),
9637 dwp_file->name);
9638 }
9639 if (i == MAX_NR_V1_DWO_SECTIONS)
9640 {
9641 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9642 " [in module %s]"),
9643 dwp_file->name);
9644 }
9645
9646 /* It's easier for the rest of the code if we fake a struct dwo_file and
9647 have dwo_unit "live" in that. At least for now.
9648
9649 The DWP file can be made up of a random collection of CUs and TUs.
9650 However, for each CU + set of TUs that came from the same original DWO
9651 file, we can combine them back into a virtual DWO file to save space
9652 (fewer struct dwo_file objects to allocate). Remember that for really
9653 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9654
9655 virtual_dwo_name =
9656 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9657 get_section_id (&sections.abbrev),
9658 get_section_id (&sections.line),
9659 get_section_id (&sections.loc),
9660 get_section_id (&sections.str_offsets));
9661 make_cleanup (xfree, virtual_dwo_name);
9662 /* Can we use an existing virtual DWO file? */
9663 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9664 /* Create one if necessary. */
9665 if (*dwo_file_slot == NULL)
9666 {
9667 if (dwarf2_read_debug)
9668 {
9669 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9670 virtual_dwo_name);
9671 }
9672 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9673 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9674 virtual_dwo_name,
9675 strlen (virtual_dwo_name));
9676 dwo_file->comp_dir = comp_dir;
9677 dwo_file->sections.abbrev = sections.abbrev;
9678 dwo_file->sections.line = sections.line;
9679 dwo_file->sections.loc = sections.loc;
9680 dwo_file->sections.macinfo = sections.macinfo;
9681 dwo_file->sections.macro = sections.macro;
9682 dwo_file->sections.str_offsets = sections.str_offsets;
9683 /* The "str" section is global to the entire DWP file. */
9684 dwo_file->sections.str = dwp_file->sections.str;
9685 /* The info or types section is assigned below to dwo_unit,
9686 there's no need to record it in dwo_file.
9687 Also, we can't simply record type sections in dwo_file because
9688 we record a pointer into the vector in dwo_unit. As we collect more
9689 types we'll grow the vector and eventually have to reallocate space
9690 for it, invalidating all copies of pointers into the previous
9691 contents. */
9692 *dwo_file_slot = dwo_file;
9693 }
9694 else
9695 {
9696 if (dwarf2_read_debug)
9697 {
9698 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9699 virtual_dwo_name);
9700 }
9701 dwo_file = *dwo_file_slot;
9702 }
9703 do_cleanups (cleanups);
9704
9705 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9706 dwo_unit->dwo_file = dwo_file;
9707 dwo_unit->signature = signature;
9708 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9709 sizeof (struct dwarf2_section_info));
9710 *dwo_unit->section = sections.info_or_types;
9711 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9712
9713 return dwo_unit;
9714 }
9715
9716 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
9717 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
9718 piece within that section used by a TU/CU, return a virtual section
9719 of just that piece. */
9720
9721 static struct dwarf2_section_info
9722 create_dwp_v2_section (struct dwarf2_section_info *section,
9723 bfd_size_type offset, bfd_size_type size)
9724 {
9725 struct dwarf2_section_info result;
9726 asection *sectp;
9727
9728 gdb_assert (section != NULL);
9729 gdb_assert (!section->is_virtual);
9730
9731 memset (&result, 0, sizeof (result));
9732 result.s.containing_section = section;
9733 result.is_virtual = 1;
9734
9735 if (size == 0)
9736 return result;
9737
9738 sectp = get_section_bfd_section (section);
9739
9740 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
9741 bounds of the real section. This is a pretty-rare event, so just
9742 flag an error (easier) instead of a warning and trying to cope. */
9743 if (sectp == NULL
9744 || offset + size > bfd_get_section_size (sectp))
9745 {
9746 bfd *abfd = sectp->owner;
9747
9748 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
9749 " in section %s [in module %s]"),
9750 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
9751 objfile_name (dwarf2_per_objfile->objfile));
9752 }
9753
9754 result.virtual_offset = offset;
9755 result.size = size;
9756 return result;
9757 }
9758
9759 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9760 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9761 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9762 This is for DWP version 2 files. */
9763
9764 static struct dwo_unit *
9765 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
9766 uint32_t unit_index,
9767 const char *comp_dir,
9768 ULONGEST signature, int is_debug_types)
9769 {
9770 struct objfile *objfile = dwarf2_per_objfile->objfile;
9771 const struct dwp_hash_table *dwp_htab =
9772 is_debug_types ? dwp_file->tus : dwp_file->cus;
9773 bfd *dbfd = dwp_file->dbfd;
9774 const char *kind = is_debug_types ? "TU" : "CU";
9775 struct dwo_file *dwo_file;
9776 struct dwo_unit *dwo_unit;
9777 struct virtual_v2_dwo_sections sections;
9778 void **dwo_file_slot;
9779 char *virtual_dwo_name;
9780 struct dwarf2_section_info *cutu;
9781 struct cleanup *cleanups;
9782 int i;
9783
9784 gdb_assert (dwp_file->version == 2);
9785
9786 if (dwarf2_read_debug)
9787 {
9788 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
9789 kind,
9790 pulongest (unit_index), hex_string (signature),
9791 dwp_file->name);
9792 }
9793
9794 /* Fetch the section offsets of this DWO unit. */
9795
9796 memset (&sections, 0, sizeof (sections));
9797 cleanups = make_cleanup (null_cleanup, 0);
9798
9799 for (i = 0; i < dwp_htab->nr_columns; ++i)
9800 {
9801 uint32_t offset = read_4_bytes (dbfd,
9802 dwp_htab->section_pool.v2.offsets
9803 + (((unit_index - 1) * dwp_htab->nr_columns
9804 + i)
9805 * sizeof (uint32_t)));
9806 uint32_t size = read_4_bytes (dbfd,
9807 dwp_htab->section_pool.v2.sizes
9808 + (((unit_index - 1) * dwp_htab->nr_columns
9809 + i)
9810 * sizeof (uint32_t)));
9811
9812 switch (dwp_htab->section_pool.v2.section_ids[i])
9813 {
9814 case DW_SECT_INFO:
9815 case DW_SECT_TYPES:
9816 sections.info_or_types_offset = offset;
9817 sections.info_or_types_size = size;
9818 break;
9819 case DW_SECT_ABBREV:
9820 sections.abbrev_offset = offset;
9821 sections.abbrev_size = size;
9822 break;
9823 case DW_SECT_LINE:
9824 sections.line_offset = offset;
9825 sections.line_size = size;
9826 break;
9827 case DW_SECT_LOC:
9828 sections.loc_offset = offset;
9829 sections.loc_size = size;
9830 break;
9831 case DW_SECT_STR_OFFSETS:
9832 sections.str_offsets_offset = offset;
9833 sections.str_offsets_size = size;
9834 break;
9835 case DW_SECT_MACINFO:
9836 sections.macinfo_offset = offset;
9837 sections.macinfo_size = size;
9838 break;
9839 case DW_SECT_MACRO:
9840 sections.macro_offset = offset;
9841 sections.macro_size = size;
9842 break;
9843 }
9844 }
9845
9846 /* It's easier for the rest of the code if we fake a struct dwo_file and
9847 have dwo_unit "live" in that. At least for now.
9848
9849 The DWP file can be made up of a random collection of CUs and TUs.
9850 However, for each CU + set of TUs that came from the same original DWO
9851 file, we can combine them back into a virtual DWO file to save space
9852 (fewer struct dwo_file objects to allocate). Remember that for really
9853 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9854
9855 virtual_dwo_name =
9856 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
9857 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
9858 (long) (sections.line_size ? sections.line_offset : 0),
9859 (long) (sections.loc_size ? sections.loc_offset : 0),
9860 (long) (sections.str_offsets_size
9861 ? sections.str_offsets_offset : 0));
9862 make_cleanup (xfree, virtual_dwo_name);
9863 /* Can we use an existing virtual DWO file? */
9864 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9865 /* Create one if necessary. */
9866 if (*dwo_file_slot == NULL)
9867 {
9868 if (dwarf2_read_debug)
9869 {
9870 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9871 virtual_dwo_name);
9872 }
9873 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9874 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9875 virtual_dwo_name,
9876 strlen (virtual_dwo_name));
9877 dwo_file->comp_dir = comp_dir;
9878 dwo_file->sections.abbrev =
9879 create_dwp_v2_section (&dwp_file->sections.abbrev,
9880 sections.abbrev_offset, sections.abbrev_size);
9881 dwo_file->sections.line =
9882 create_dwp_v2_section (&dwp_file->sections.line,
9883 sections.line_offset, sections.line_size);
9884 dwo_file->sections.loc =
9885 create_dwp_v2_section (&dwp_file->sections.loc,
9886 sections.loc_offset, sections.loc_size);
9887 dwo_file->sections.macinfo =
9888 create_dwp_v2_section (&dwp_file->sections.macinfo,
9889 sections.macinfo_offset, sections.macinfo_size);
9890 dwo_file->sections.macro =
9891 create_dwp_v2_section (&dwp_file->sections.macro,
9892 sections.macro_offset, sections.macro_size);
9893 dwo_file->sections.str_offsets =
9894 create_dwp_v2_section (&dwp_file->sections.str_offsets,
9895 sections.str_offsets_offset,
9896 sections.str_offsets_size);
9897 /* The "str" section is global to the entire DWP file. */
9898 dwo_file->sections.str = dwp_file->sections.str;
9899 /* The info or types section is assigned below to dwo_unit,
9900 there's no need to record it in dwo_file.
9901 Also, we can't simply record type sections in dwo_file because
9902 we record a pointer into the vector in dwo_unit. As we collect more
9903 types we'll grow the vector and eventually have to reallocate space
9904 for it, invalidating all copies of pointers into the previous
9905 contents. */
9906 *dwo_file_slot = dwo_file;
9907 }
9908 else
9909 {
9910 if (dwarf2_read_debug)
9911 {
9912 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9913 virtual_dwo_name);
9914 }
9915 dwo_file = *dwo_file_slot;
9916 }
9917 do_cleanups (cleanups);
9918
9919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9920 dwo_unit->dwo_file = dwo_file;
9921 dwo_unit->signature = signature;
9922 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9923 sizeof (struct dwarf2_section_info));
9924 *dwo_unit->section = create_dwp_v2_section (is_debug_types
9925 ? &dwp_file->sections.types
9926 : &dwp_file->sections.info,
9927 sections.info_or_types_offset,
9928 sections.info_or_types_size);
9929 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9930
9931 return dwo_unit;
9932 }
9933
9934 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9935 Returns NULL if the signature isn't found. */
9936
9937 static struct dwo_unit *
9938 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
9939 ULONGEST signature, int is_debug_types)
9940 {
9941 const struct dwp_hash_table *dwp_htab =
9942 is_debug_types ? dwp_file->tus : dwp_file->cus;
9943 bfd *dbfd = dwp_file->dbfd;
9944 uint32_t mask = dwp_htab->nr_slots - 1;
9945 uint32_t hash = signature & mask;
9946 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9947 unsigned int i;
9948 void **slot;
9949 struct dwo_unit find_dwo_cu, *dwo_cu;
9950
9951 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9952 find_dwo_cu.signature = signature;
9953 slot = htab_find_slot (is_debug_types
9954 ? dwp_file->loaded_tus
9955 : dwp_file->loaded_cus,
9956 &find_dwo_cu, INSERT);
9957
9958 if (*slot != NULL)
9959 return *slot;
9960
9961 /* Use a for loop so that we don't loop forever on bad debug info. */
9962 for (i = 0; i < dwp_htab->nr_slots; ++i)
9963 {
9964 ULONGEST signature_in_table;
9965
9966 signature_in_table =
9967 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9968 if (signature_in_table == signature)
9969 {
9970 uint32_t unit_index =
9971 read_4_bytes (dbfd,
9972 dwp_htab->unit_table + hash * sizeof (uint32_t));
9973
9974 if (dwp_file->version == 1)
9975 {
9976 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
9977 comp_dir, signature,
9978 is_debug_types);
9979 }
9980 else
9981 {
9982 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
9983 comp_dir, signature,
9984 is_debug_types);
9985 }
9986 return *slot;
9987 }
9988 if (signature_in_table == 0)
9989 return NULL;
9990 hash = (hash + hash2) & mask;
9991 }
9992
9993 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9994 " [in module %s]"),
9995 dwp_file->name);
9996 }
9997
9998 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9999 Open the file specified by FILE_NAME and hand it off to BFD for
10000 preliminary analysis. Return a newly initialized bfd *, which
10001 includes a canonicalized copy of FILE_NAME.
10002 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10003 SEARCH_CWD is true if the current directory is to be searched.
10004 It will be searched before debug-file-directory.
10005 If unable to find/open the file, return NULL.
10006 NOTE: This function is derived from symfile_bfd_open. */
10007
10008 static bfd *
10009 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10010 {
10011 bfd *sym_bfd;
10012 int desc, flags;
10013 char *absolute_name;
10014 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10015 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10016 to debug_file_directory. */
10017 char *search_path;
10018 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10019
10020 if (search_cwd)
10021 {
10022 if (*debug_file_directory != '\0')
10023 search_path = concat (".", dirname_separator_string,
10024 debug_file_directory, NULL);
10025 else
10026 search_path = xstrdup (".");
10027 }
10028 else
10029 search_path = xstrdup (debug_file_directory);
10030
10031 flags = OPF_RETURN_REALPATH;
10032 if (is_dwp)
10033 flags |= OPF_SEARCH_IN_PATH;
10034 desc = openp (search_path, flags, file_name,
10035 O_RDONLY | O_BINARY, &absolute_name);
10036 xfree (search_path);
10037 if (desc < 0)
10038 return NULL;
10039
10040 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
10041 xfree (absolute_name);
10042 if (sym_bfd == NULL)
10043 return NULL;
10044 bfd_set_cacheable (sym_bfd, 1);
10045
10046 if (!bfd_check_format (sym_bfd, bfd_object))
10047 {
10048 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
10049 return NULL;
10050 }
10051
10052 return sym_bfd;
10053 }
10054
10055 /* Try to open DWO file FILE_NAME.
10056 COMP_DIR is the DW_AT_comp_dir attribute.
10057 The result is the bfd handle of the file.
10058 If there is a problem finding or opening the file, return NULL.
10059 Upon success, the canonicalized path of the file is stored in the bfd,
10060 same as symfile_bfd_open. */
10061
10062 static bfd *
10063 open_dwo_file (const char *file_name, const char *comp_dir)
10064 {
10065 bfd *abfd;
10066
10067 if (IS_ABSOLUTE_PATH (file_name))
10068 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10069
10070 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10071
10072 if (comp_dir != NULL)
10073 {
10074 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
10075
10076 /* NOTE: If comp_dir is a relative path, this will also try the
10077 search path, which seems useful. */
10078 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
10079 xfree (path_to_try);
10080 if (abfd != NULL)
10081 return abfd;
10082 }
10083
10084 /* That didn't work, try debug-file-directory, which, despite its name,
10085 is a list of paths. */
10086
10087 if (*debug_file_directory == '\0')
10088 return NULL;
10089
10090 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10091 }
10092
10093 /* This function is mapped across the sections and remembers the offset and
10094 size of each of the DWO debugging sections we are interested in. */
10095
10096 static void
10097 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10098 {
10099 struct dwo_sections *dwo_sections = dwo_sections_ptr;
10100 const struct dwop_section_names *names = &dwop_section_names;
10101
10102 if (section_is_p (sectp->name, &names->abbrev_dwo))
10103 {
10104 dwo_sections->abbrev.s.asection = sectp;
10105 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10106 }
10107 else if (section_is_p (sectp->name, &names->info_dwo))
10108 {
10109 dwo_sections->info.s.asection = sectp;
10110 dwo_sections->info.size = bfd_get_section_size (sectp);
10111 }
10112 else if (section_is_p (sectp->name, &names->line_dwo))
10113 {
10114 dwo_sections->line.s.asection = sectp;
10115 dwo_sections->line.size = bfd_get_section_size (sectp);
10116 }
10117 else if (section_is_p (sectp->name, &names->loc_dwo))
10118 {
10119 dwo_sections->loc.s.asection = sectp;
10120 dwo_sections->loc.size = bfd_get_section_size (sectp);
10121 }
10122 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10123 {
10124 dwo_sections->macinfo.s.asection = sectp;
10125 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10126 }
10127 else if (section_is_p (sectp->name, &names->macro_dwo))
10128 {
10129 dwo_sections->macro.s.asection = sectp;
10130 dwo_sections->macro.size = bfd_get_section_size (sectp);
10131 }
10132 else if (section_is_p (sectp->name, &names->str_dwo))
10133 {
10134 dwo_sections->str.s.asection = sectp;
10135 dwo_sections->str.size = bfd_get_section_size (sectp);
10136 }
10137 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10138 {
10139 dwo_sections->str_offsets.s.asection = sectp;
10140 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10141 }
10142 else if (section_is_p (sectp->name, &names->types_dwo))
10143 {
10144 struct dwarf2_section_info type_section;
10145
10146 memset (&type_section, 0, sizeof (type_section));
10147 type_section.s.asection = sectp;
10148 type_section.size = bfd_get_section_size (sectp);
10149 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10150 &type_section);
10151 }
10152 }
10153
10154 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10155 by PER_CU. This is for the non-DWP case.
10156 The result is NULL if DWO_NAME can't be found. */
10157
10158 static struct dwo_file *
10159 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10160 const char *dwo_name, const char *comp_dir)
10161 {
10162 struct objfile *objfile = dwarf2_per_objfile->objfile;
10163 struct dwo_file *dwo_file;
10164 bfd *dbfd;
10165 struct cleanup *cleanups;
10166
10167 dbfd = open_dwo_file (dwo_name, comp_dir);
10168 if (dbfd == NULL)
10169 {
10170 if (dwarf2_read_debug)
10171 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10172 return NULL;
10173 }
10174 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10175 dwo_file->dwo_name = dwo_name;
10176 dwo_file->comp_dir = comp_dir;
10177 dwo_file->dbfd = dbfd;
10178
10179 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10180
10181 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
10182
10183 dwo_file->cu = create_dwo_cu (dwo_file);
10184
10185 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10186 dwo_file->sections.types);
10187
10188 discard_cleanups (cleanups);
10189
10190 if (dwarf2_read_debug)
10191 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10192
10193 return dwo_file;
10194 }
10195
10196 /* This function is mapped across the sections and remembers the offset and
10197 size of each of the DWP debugging sections common to version 1 and 2 that
10198 we are interested in. */
10199
10200 static void
10201 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10202 void *dwp_file_ptr)
10203 {
10204 struct dwp_file *dwp_file = dwp_file_ptr;
10205 const struct dwop_section_names *names = &dwop_section_names;
10206 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10207
10208 /* Record the ELF section number for later lookup: this is what the
10209 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10210 gdb_assert (elf_section_nr < dwp_file->num_sections);
10211 dwp_file->elf_sections[elf_section_nr] = sectp;
10212
10213 /* Look for specific sections that we need. */
10214 if (section_is_p (sectp->name, &names->str_dwo))
10215 {
10216 dwp_file->sections.str.s.asection = sectp;
10217 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10218 }
10219 else if (section_is_p (sectp->name, &names->cu_index))
10220 {
10221 dwp_file->sections.cu_index.s.asection = sectp;
10222 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10223 }
10224 else if (section_is_p (sectp->name, &names->tu_index))
10225 {
10226 dwp_file->sections.tu_index.s.asection = sectp;
10227 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10228 }
10229 }
10230
10231 /* This function is mapped across the sections and remembers the offset and
10232 size of each of the DWP version 2 debugging sections that we are interested
10233 in. This is split into a separate function because we don't know if we
10234 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10235
10236 static void
10237 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10238 {
10239 struct dwp_file *dwp_file = dwp_file_ptr;
10240 const struct dwop_section_names *names = &dwop_section_names;
10241 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10242
10243 /* Record the ELF section number for later lookup: this is what the
10244 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10245 gdb_assert (elf_section_nr < dwp_file->num_sections);
10246 dwp_file->elf_sections[elf_section_nr] = sectp;
10247
10248 /* Look for specific sections that we need. */
10249 if (section_is_p (sectp->name, &names->abbrev_dwo))
10250 {
10251 dwp_file->sections.abbrev.s.asection = sectp;
10252 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10253 }
10254 else if (section_is_p (sectp->name, &names->info_dwo))
10255 {
10256 dwp_file->sections.info.s.asection = sectp;
10257 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10258 }
10259 else if (section_is_p (sectp->name, &names->line_dwo))
10260 {
10261 dwp_file->sections.line.s.asection = sectp;
10262 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10263 }
10264 else if (section_is_p (sectp->name, &names->loc_dwo))
10265 {
10266 dwp_file->sections.loc.s.asection = sectp;
10267 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10268 }
10269 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10270 {
10271 dwp_file->sections.macinfo.s.asection = sectp;
10272 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10273 }
10274 else if (section_is_p (sectp->name, &names->macro_dwo))
10275 {
10276 dwp_file->sections.macro.s.asection = sectp;
10277 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10278 }
10279 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10280 {
10281 dwp_file->sections.str_offsets.s.asection = sectp;
10282 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10283 }
10284 else if (section_is_p (sectp->name, &names->types_dwo))
10285 {
10286 dwp_file->sections.types.s.asection = sectp;
10287 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10288 }
10289 }
10290
10291 /* Hash function for dwp_file loaded CUs/TUs. */
10292
10293 static hashval_t
10294 hash_dwp_loaded_cutus (const void *item)
10295 {
10296 const struct dwo_unit *dwo_unit = item;
10297
10298 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10299 return dwo_unit->signature;
10300 }
10301
10302 /* Equality function for dwp_file loaded CUs/TUs. */
10303
10304 static int
10305 eq_dwp_loaded_cutus (const void *a, const void *b)
10306 {
10307 const struct dwo_unit *dua = a;
10308 const struct dwo_unit *dub = b;
10309
10310 return dua->signature == dub->signature;
10311 }
10312
10313 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10314
10315 static htab_t
10316 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10317 {
10318 return htab_create_alloc_ex (3,
10319 hash_dwp_loaded_cutus,
10320 eq_dwp_loaded_cutus,
10321 NULL,
10322 &objfile->objfile_obstack,
10323 hashtab_obstack_allocate,
10324 dummy_obstack_deallocate);
10325 }
10326
10327 /* Try to open DWP file FILE_NAME.
10328 The result is the bfd handle of the file.
10329 If there is a problem finding or opening the file, return NULL.
10330 Upon success, the canonicalized path of the file is stored in the bfd,
10331 same as symfile_bfd_open. */
10332
10333 static bfd *
10334 open_dwp_file (const char *file_name)
10335 {
10336 bfd *abfd;
10337
10338 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10339 if (abfd != NULL)
10340 return abfd;
10341
10342 /* Work around upstream bug 15652.
10343 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10344 [Whether that's a "bug" is debatable, but it is getting in our way.]
10345 We have no real idea where the dwp file is, because gdb's realpath-ing
10346 of the executable's path may have discarded the needed info.
10347 [IWBN if the dwp file name was recorded in the executable, akin to
10348 .gnu_debuglink, but that doesn't exist yet.]
10349 Strip the directory from FILE_NAME and search again. */
10350 if (*debug_file_directory != '\0')
10351 {
10352 /* Don't implicitly search the current directory here.
10353 If the user wants to search "." to handle this case,
10354 it must be added to debug-file-directory. */
10355 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10356 0 /*search_cwd*/);
10357 }
10358
10359 return NULL;
10360 }
10361
10362 /* Initialize the use of the DWP file for the current objfile.
10363 By convention the name of the DWP file is ${objfile}.dwp.
10364 The result is NULL if it can't be found. */
10365
10366 static struct dwp_file *
10367 open_and_init_dwp_file (void)
10368 {
10369 struct objfile *objfile = dwarf2_per_objfile->objfile;
10370 struct dwp_file *dwp_file;
10371 char *dwp_name;
10372 bfd *dbfd;
10373 struct cleanup *cleanups;
10374
10375 /* Try to find first .dwp for the binary file before any symbolic links
10376 resolving. */
10377 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10378 cleanups = make_cleanup (xfree, dwp_name);
10379
10380 dbfd = open_dwp_file (dwp_name);
10381 if (dbfd == NULL
10382 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10383 {
10384 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10385 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10386 make_cleanup (xfree, dwp_name);
10387 dbfd = open_dwp_file (dwp_name);
10388 }
10389
10390 if (dbfd == NULL)
10391 {
10392 if (dwarf2_read_debug)
10393 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10394 do_cleanups (cleanups);
10395 return NULL;
10396 }
10397 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10398 dwp_file->name = bfd_get_filename (dbfd);
10399 dwp_file->dbfd = dbfd;
10400 do_cleanups (cleanups);
10401
10402 /* +1: section 0 is unused */
10403 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10404 dwp_file->elf_sections =
10405 OBSTACK_CALLOC (&objfile->objfile_obstack,
10406 dwp_file->num_sections, asection *);
10407
10408 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
10409
10410 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10411
10412 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10413
10414 /* The DWP file version is stored in the hash table. Oh well. */
10415 if (dwp_file->cus->version != dwp_file->tus->version)
10416 {
10417 /* Technically speaking, we should try to limp along, but this is
10418 pretty bizarre. */
10419 error (_("Dwarf Error: DWP file CU version %d doesn't match"
10420 " TU version %d [in DWP file %s]"),
10421 dwp_file->cus->version, dwp_file->tus->version, dwp_name);
10422 }
10423 dwp_file->version = dwp_file->cus->version;
10424
10425 if (dwp_file->version == 2)
10426 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10427
10428 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10429 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10430
10431 if (dwarf2_read_debug)
10432 {
10433 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10434 fprintf_unfiltered (gdb_stdlog,
10435 " %s CUs, %s TUs\n",
10436 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10437 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10438 }
10439
10440 return dwp_file;
10441 }
10442
10443 /* Wrapper around open_and_init_dwp_file, only open it once. */
10444
10445 static struct dwp_file *
10446 get_dwp_file (void)
10447 {
10448 if (! dwarf2_per_objfile->dwp_checked)
10449 {
10450 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10451 dwarf2_per_objfile->dwp_checked = 1;
10452 }
10453 return dwarf2_per_objfile->dwp_file;
10454 }
10455
10456 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10457 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10458 or in the DWP file for the objfile, referenced by THIS_UNIT.
10459 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10460 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10461
10462 This is called, for example, when wanting to read a variable with a
10463 complex location. Therefore we don't want to do file i/o for every call.
10464 Therefore we don't want to look for a DWO file on every call.
10465 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10466 then we check if we've already seen DWO_NAME, and only THEN do we check
10467 for a DWO file.
10468
10469 The result is a pointer to the dwo_unit object or NULL if we didn't find it
10470 (dwo_id mismatch or couldn't find the DWO/DWP file). */
10471
10472 static struct dwo_unit *
10473 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10474 const char *dwo_name, const char *comp_dir,
10475 ULONGEST signature, int is_debug_types)
10476 {
10477 struct objfile *objfile = dwarf2_per_objfile->objfile;
10478 const char *kind = is_debug_types ? "TU" : "CU";
10479 void **dwo_file_slot;
10480 struct dwo_file *dwo_file;
10481 struct dwp_file *dwp_file;
10482
10483 /* First see if there's a DWP file.
10484 If we have a DWP file but didn't find the DWO inside it, don't
10485 look for the original DWO file. It makes gdb behave differently
10486 depending on whether one is debugging in the build tree. */
10487
10488 dwp_file = get_dwp_file ();
10489 if (dwp_file != NULL)
10490 {
10491 const struct dwp_hash_table *dwp_htab =
10492 is_debug_types ? dwp_file->tus : dwp_file->cus;
10493
10494 if (dwp_htab != NULL)
10495 {
10496 struct dwo_unit *dwo_cutu =
10497 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10498 signature, is_debug_types);
10499
10500 if (dwo_cutu != NULL)
10501 {
10502 if (dwarf2_read_debug)
10503 {
10504 fprintf_unfiltered (gdb_stdlog,
10505 "Virtual DWO %s %s found: @%s\n",
10506 kind, hex_string (signature),
10507 host_address_to_string (dwo_cutu));
10508 }
10509 return dwo_cutu;
10510 }
10511 }
10512 }
10513 else
10514 {
10515 /* No DWP file, look for the DWO file. */
10516
10517 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10518 if (*dwo_file_slot == NULL)
10519 {
10520 /* Read in the file and build a table of the CUs/TUs it contains. */
10521 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
10522 }
10523 /* NOTE: This will be NULL if unable to open the file. */
10524 dwo_file = *dwo_file_slot;
10525
10526 if (dwo_file != NULL)
10527 {
10528 struct dwo_unit *dwo_cutu = NULL;
10529
10530 if (is_debug_types && dwo_file->tus)
10531 {
10532 struct dwo_unit find_dwo_cutu;
10533
10534 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10535 find_dwo_cutu.signature = signature;
10536 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
10537 }
10538 else if (!is_debug_types && dwo_file->cu)
10539 {
10540 if (signature == dwo_file->cu->signature)
10541 dwo_cutu = dwo_file->cu;
10542 }
10543
10544 if (dwo_cutu != NULL)
10545 {
10546 if (dwarf2_read_debug)
10547 {
10548 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10549 kind, dwo_name, hex_string (signature),
10550 host_address_to_string (dwo_cutu));
10551 }
10552 return dwo_cutu;
10553 }
10554 }
10555 }
10556
10557 /* We didn't find it. This could mean a dwo_id mismatch, or
10558 someone deleted the DWO/DWP file, or the search path isn't set up
10559 correctly to find the file. */
10560
10561 if (dwarf2_read_debug)
10562 {
10563 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
10564 kind, dwo_name, hex_string (signature));
10565 }
10566
10567 /* This is a warning and not a complaint because it can be caused by
10568 pilot error (e.g., user accidentally deleting the DWO). */
10569 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
10570 " [in module %s]"),
10571 kind, dwo_name, hex_string (signature),
10572 this_unit->is_debug_types ? "TU" : "CU",
10573 this_unit->offset.sect_off, objfile_name (objfile));
10574 return NULL;
10575 }
10576
10577 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
10578 See lookup_dwo_cutu_unit for details. */
10579
10580 static struct dwo_unit *
10581 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
10582 const char *dwo_name, const char *comp_dir,
10583 ULONGEST signature)
10584 {
10585 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
10586 }
10587
10588 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
10589 See lookup_dwo_cutu_unit for details. */
10590
10591 static struct dwo_unit *
10592 lookup_dwo_type_unit (struct signatured_type *this_tu,
10593 const char *dwo_name, const char *comp_dir)
10594 {
10595 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
10596 }
10597
10598 /* Traversal function for queue_and_load_all_dwo_tus. */
10599
10600 static int
10601 queue_and_load_dwo_tu (void **slot, void *info)
10602 {
10603 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
10604 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
10605 ULONGEST signature = dwo_unit->signature;
10606 struct signatured_type *sig_type =
10607 lookup_dwo_signatured_type (per_cu->cu, signature);
10608
10609 if (sig_type != NULL)
10610 {
10611 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
10612
10613 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
10614 a real dependency of PER_CU on SIG_TYPE. That is detected later
10615 while processing PER_CU. */
10616 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
10617 load_full_type_unit (sig_cu);
10618 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
10619 }
10620
10621 return 1;
10622 }
10623
10624 /* Queue all TUs contained in the DWO of PER_CU to be read in.
10625 The DWO may have the only definition of the type, though it may not be
10626 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
10627 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
10628
10629 static void
10630 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
10631 {
10632 struct dwo_unit *dwo_unit;
10633 struct dwo_file *dwo_file;
10634
10635 gdb_assert (!per_cu->is_debug_types);
10636 gdb_assert (get_dwp_file () == NULL);
10637 gdb_assert (per_cu->cu != NULL);
10638
10639 dwo_unit = per_cu->cu->dwo_unit;
10640 gdb_assert (dwo_unit != NULL);
10641
10642 dwo_file = dwo_unit->dwo_file;
10643 if (dwo_file->tus != NULL)
10644 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
10645 }
10646
10647 /* Free all resources associated with DWO_FILE.
10648 Close the DWO file and munmap the sections.
10649 All memory should be on the objfile obstack. */
10650
10651 static void
10652 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
10653 {
10654 int ix;
10655 struct dwarf2_section_info *section;
10656
10657 /* Note: dbfd is NULL for virtual DWO files. */
10658 gdb_bfd_unref (dwo_file->dbfd);
10659
10660 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
10661 }
10662
10663 /* Wrapper for free_dwo_file for use in cleanups. */
10664
10665 static void
10666 free_dwo_file_cleanup (void *arg)
10667 {
10668 struct dwo_file *dwo_file = (struct dwo_file *) arg;
10669 struct objfile *objfile = dwarf2_per_objfile->objfile;
10670
10671 free_dwo_file (dwo_file, objfile);
10672 }
10673
10674 /* Traversal function for free_dwo_files. */
10675
10676 static int
10677 free_dwo_file_from_slot (void **slot, void *info)
10678 {
10679 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
10680 struct objfile *objfile = (struct objfile *) info;
10681
10682 free_dwo_file (dwo_file, objfile);
10683
10684 return 1;
10685 }
10686
10687 /* Free all resources associated with DWO_FILES. */
10688
10689 static void
10690 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
10691 {
10692 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
10693 }
10694 \f
10695 /* Read in various DIEs. */
10696
10697 /* qsort helper for inherit_abstract_dies. */
10698
10699 static int
10700 unsigned_int_compar (const void *ap, const void *bp)
10701 {
10702 unsigned int a = *(unsigned int *) ap;
10703 unsigned int b = *(unsigned int *) bp;
10704
10705 return (a > b) - (b > a);
10706 }
10707
10708 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
10709 Inherit only the children of the DW_AT_abstract_origin DIE not being
10710 already referenced by DW_AT_abstract_origin from the children of the
10711 current DIE. */
10712
10713 static void
10714 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
10715 {
10716 struct die_info *child_die;
10717 unsigned die_children_count;
10718 /* CU offsets which were referenced by children of the current DIE. */
10719 sect_offset *offsets;
10720 sect_offset *offsets_end, *offsetp;
10721 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
10722 struct die_info *origin_die;
10723 /* Iterator of the ORIGIN_DIE children. */
10724 struct die_info *origin_child_die;
10725 struct cleanup *cleanups;
10726 struct attribute *attr;
10727 struct dwarf2_cu *origin_cu;
10728 struct pending **origin_previous_list_in_scope;
10729
10730 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10731 if (!attr)
10732 return;
10733
10734 /* Note that following die references may follow to a die in a
10735 different cu. */
10736
10737 origin_cu = cu;
10738 origin_die = follow_die_ref (die, attr, &origin_cu);
10739
10740 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10741 symbols in. */
10742 origin_previous_list_in_scope = origin_cu->list_in_scope;
10743 origin_cu->list_in_scope = cu->list_in_scope;
10744
10745 if (die->tag != origin_die->tag
10746 && !(die->tag == DW_TAG_inlined_subroutine
10747 && origin_die->tag == DW_TAG_subprogram))
10748 complaint (&symfile_complaints,
10749 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
10750 die->offset.sect_off, origin_die->offset.sect_off);
10751
10752 child_die = die->child;
10753 die_children_count = 0;
10754 while (child_die && child_die->tag)
10755 {
10756 child_die = sibling_die (child_die);
10757 die_children_count++;
10758 }
10759 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10760 cleanups = make_cleanup (xfree, offsets);
10761
10762 offsets_end = offsets;
10763 child_die = die->child;
10764 while (child_die && child_die->tag)
10765 {
10766 /* For each CHILD_DIE, find the corresponding child of
10767 ORIGIN_DIE. If there is more than one layer of
10768 DW_AT_abstract_origin, follow them all; there shouldn't be,
10769 but GCC versions at least through 4.4 generate this (GCC PR
10770 40573). */
10771 struct die_info *child_origin_die = child_die;
10772 struct dwarf2_cu *child_origin_cu = cu;
10773
10774 while (1)
10775 {
10776 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10777 child_origin_cu);
10778 if (attr == NULL)
10779 break;
10780 child_origin_die = follow_die_ref (child_origin_die, attr,
10781 &child_origin_cu);
10782 }
10783
10784 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10785 counterpart may exist. */
10786 if (child_origin_die != child_die)
10787 {
10788 if (child_die->tag != child_origin_die->tag
10789 && !(child_die->tag == DW_TAG_inlined_subroutine
10790 && child_origin_die->tag == DW_TAG_subprogram))
10791 complaint (&symfile_complaints,
10792 _("Child DIE 0x%x and its abstract origin 0x%x have "
10793 "different tags"), child_die->offset.sect_off,
10794 child_origin_die->offset.sect_off);
10795 if (child_origin_die->parent != origin_die)
10796 complaint (&symfile_complaints,
10797 _("Child DIE 0x%x and its abstract origin 0x%x have "
10798 "different parents"), child_die->offset.sect_off,
10799 child_origin_die->offset.sect_off);
10800 else
10801 *offsets_end++ = child_origin_die->offset;
10802 }
10803 child_die = sibling_die (child_die);
10804 }
10805 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10806 unsigned_int_compar);
10807 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
10808 if (offsetp[-1].sect_off == offsetp->sect_off)
10809 complaint (&symfile_complaints,
10810 _("Multiple children of DIE 0x%x refer "
10811 "to DIE 0x%x as their abstract origin"),
10812 die->offset.sect_off, offsetp->sect_off);
10813
10814 offsetp = offsets;
10815 origin_child_die = origin_die->child;
10816 while (origin_child_die && origin_child_die->tag)
10817 {
10818 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
10819 while (offsetp < offsets_end
10820 && offsetp->sect_off < origin_child_die->offset.sect_off)
10821 offsetp++;
10822 if (offsetp >= offsets_end
10823 || offsetp->sect_off > origin_child_die->offset.sect_off)
10824 {
10825 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
10826 process_die (origin_child_die, origin_cu);
10827 }
10828 origin_child_die = sibling_die (origin_child_die);
10829 }
10830 origin_cu->list_in_scope = origin_previous_list_in_scope;
10831
10832 do_cleanups (cleanups);
10833 }
10834
10835 static void
10836 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10837 {
10838 struct objfile *objfile = cu->objfile;
10839 struct context_stack *new;
10840 CORE_ADDR lowpc;
10841 CORE_ADDR highpc;
10842 struct die_info *child_die;
10843 struct attribute *attr, *call_line, *call_file;
10844 const char *name;
10845 CORE_ADDR baseaddr;
10846 struct block *block;
10847 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10848 VEC (symbolp) *template_args = NULL;
10849 struct template_symbol *templ_func = NULL;
10850
10851 if (inlined_func)
10852 {
10853 /* If we do not have call site information, we can't show the
10854 caller of this inlined function. That's too confusing, so
10855 only use the scope for local variables. */
10856 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10857 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10858 if (call_line == NULL || call_file == NULL)
10859 {
10860 read_lexical_block_scope (die, cu);
10861 return;
10862 }
10863 }
10864
10865 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10866
10867 name = dwarf2_name (die, cu);
10868
10869 /* Ignore functions with missing or empty names. These are actually
10870 illegal according to the DWARF standard. */
10871 if (name == NULL)
10872 {
10873 complaint (&symfile_complaints,
10874 _("missing name for subprogram DIE at %d"),
10875 die->offset.sect_off);
10876 return;
10877 }
10878
10879 /* Ignore functions with missing or invalid low and high pc attributes. */
10880 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10881 {
10882 attr = dwarf2_attr (die, DW_AT_external, cu);
10883 if (!attr || !DW_UNSND (attr))
10884 complaint (&symfile_complaints,
10885 _("cannot get low and high bounds "
10886 "for subprogram DIE at %d"),
10887 die->offset.sect_off);
10888 return;
10889 }
10890
10891 lowpc += baseaddr;
10892 highpc += baseaddr;
10893
10894 /* If we have any template arguments, then we must allocate a
10895 different sort of symbol. */
10896 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10897 {
10898 if (child_die->tag == DW_TAG_template_type_param
10899 || child_die->tag == DW_TAG_template_value_param)
10900 {
10901 templ_func = allocate_template_symbol (objfile);
10902 templ_func->base.is_cplus_template_function = 1;
10903 break;
10904 }
10905 }
10906
10907 new = push_context (0, lowpc);
10908 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10909 (struct symbol *) templ_func);
10910
10911 /* If there is a location expression for DW_AT_frame_base, record
10912 it. */
10913 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10914 if (attr)
10915 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10916
10917 cu->list_in_scope = &local_symbols;
10918
10919 if (die->child != NULL)
10920 {
10921 child_die = die->child;
10922 while (child_die && child_die->tag)
10923 {
10924 if (child_die->tag == DW_TAG_template_type_param
10925 || child_die->tag == DW_TAG_template_value_param)
10926 {
10927 struct symbol *arg = new_symbol (child_die, NULL, cu);
10928
10929 if (arg != NULL)
10930 VEC_safe_push (symbolp, template_args, arg);
10931 }
10932 else
10933 process_die (child_die, cu);
10934 child_die = sibling_die (child_die);
10935 }
10936 }
10937
10938 inherit_abstract_dies (die, cu);
10939
10940 /* If we have a DW_AT_specification, we might need to import using
10941 directives from the context of the specification DIE. See the
10942 comment in determine_prefix. */
10943 if (cu->language == language_cplus
10944 && dwarf2_attr (die, DW_AT_specification, cu))
10945 {
10946 struct dwarf2_cu *spec_cu = cu;
10947 struct die_info *spec_die = die_specification (die, &spec_cu);
10948
10949 while (spec_die)
10950 {
10951 child_die = spec_die->child;
10952 while (child_die && child_die->tag)
10953 {
10954 if (child_die->tag == DW_TAG_imported_module)
10955 process_die (child_die, spec_cu);
10956 child_die = sibling_die (child_die);
10957 }
10958
10959 /* In some cases, GCC generates specification DIEs that
10960 themselves contain DW_AT_specification attributes. */
10961 spec_die = die_specification (spec_die, &spec_cu);
10962 }
10963 }
10964
10965 new = pop_context ();
10966 /* Make a block for the local symbols within. */
10967 block = finish_block (new->name, &local_symbols, new->old_blocks,
10968 lowpc, highpc, objfile);
10969
10970 /* For C++, set the block's scope. */
10971 if ((cu->language == language_cplus || cu->language == language_fortran)
10972 && cu->processing_has_namespace_info)
10973 block_set_scope (block, determine_prefix (die, cu),
10974 &objfile->objfile_obstack);
10975
10976 /* If we have address ranges, record them. */
10977 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10978
10979 /* Attach template arguments to function. */
10980 if (! VEC_empty (symbolp, template_args))
10981 {
10982 gdb_assert (templ_func != NULL);
10983
10984 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10985 templ_func->template_arguments
10986 = obstack_alloc (&objfile->objfile_obstack,
10987 (templ_func->n_template_arguments
10988 * sizeof (struct symbol *)));
10989 memcpy (templ_func->template_arguments,
10990 VEC_address (symbolp, template_args),
10991 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10992 VEC_free (symbolp, template_args);
10993 }
10994
10995 /* In C++, we can have functions nested inside functions (e.g., when
10996 a function declares a class that has methods). This means that
10997 when we finish processing a function scope, we may need to go
10998 back to building a containing block's symbol lists. */
10999 local_symbols = new->locals;
11000 using_directives = new->using_directives;
11001
11002 /* If we've finished processing a top-level function, subsequent
11003 symbols go in the file symbol list. */
11004 if (outermost_context_p ())
11005 cu->list_in_scope = &file_symbols;
11006 }
11007
11008 /* Process all the DIES contained within a lexical block scope. Start
11009 a new scope, process the dies, and then close the scope. */
11010
11011 static void
11012 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11013 {
11014 struct objfile *objfile = cu->objfile;
11015 struct context_stack *new;
11016 CORE_ADDR lowpc, highpc;
11017 struct die_info *child_die;
11018 CORE_ADDR baseaddr;
11019
11020 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11021
11022 /* Ignore blocks with missing or invalid low and high pc attributes. */
11023 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11024 as multiple lexical blocks? Handling children in a sane way would
11025 be nasty. Might be easier to properly extend generic blocks to
11026 describe ranges. */
11027 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11028 return;
11029 lowpc += baseaddr;
11030 highpc += baseaddr;
11031
11032 push_context (0, lowpc);
11033 if (die->child != NULL)
11034 {
11035 child_die = die->child;
11036 while (child_die && child_die->tag)
11037 {
11038 process_die (child_die, cu);
11039 child_die = sibling_die (child_die);
11040 }
11041 }
11042 new = pop_context ();
11043
11044 if (local_symbols != NULL || using_directives != NULL)
11045 {
11046 struct block *block
11047 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
11048 highpc, objfile);
11049
11050 /* Note that recording ranges after traversing children, as we
11051 do here, means that recording a parent's ranges entails
11052 walking across all its children's ranges as they appear in
11053 the address map, which is quadratic behavior.
11054
11055 It would be nicer to record the parent's ranges before
11056 traversing its children, simply overriding whatever you find
11057 there. But since we don't even decide whether to create a
11058 block until after we've traversed its children, that's hard
11059 to do. */
11060 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11061 }
11062 local_symbols = new->locals;
11063 using_directives = new->using_directives;
11064 }
11065
11066 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11067
11068 static void
11069 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11070 {
11071 struct objfile *objfile = cu->objfile;
11072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11073 CORE_ADDR pc, baseaddr;
11074 struct attribute *attr;
11075 struct call_site *call_site, call_site_local;
11076 void **slot;
11077 int nparams;
11078 struct die_info *child_die;
11079
11080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11081
11082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11083 if (!attr)
11084 {
11085 complaint (&symfile_complaints,
11086 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11087 "DIE 0x%x [in module %s]"),
11088 die->offset.sect_off, objfile_name (objfile));
11089 return;
11090 }
11091 pc = DW_ADDR (attr) + baseaddr;
11092
11093 if (cu->call_site_htab == NULL)
11094 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11095 NULL, &objfile->objfile_obstack,
11096 hashtab_obstack_allocate, NULL);
11097 call_site_local.pc = pc;
11098 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11099 if (*slot != NULL)
11100 {
11101 complaint (&symfile_complaints,
11102 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11103 "DIE 0x%x [in module %s]"),
11104 paddress (gdbarch, pc), die->offset.sect_off,
11105 objfile_name (objfile));
11106 return;
11107 }
11108
11109 /* Count parameters at the caller. */
11110
11111 nparams = 0;
11112 for (child_die = die->child; child_die && child_die->tag;
11113 child_die = sibling_die (child_die))
11114 {
11115 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11116 {
11117 complaint (&symfile_complaints,
11118 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11119 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11120 child_die->tag, child_die->offset.sect_off,
11121 objfile_name (objfile));
11122 continue;
11123 }
11124
11125 nparams++;
11126 }
11127
11128 call_site = obstack_alloc (&objfile->objfile_obstack,
11129 (sizeof (*call_site)
11130 + (sizeof (*call_site->parameter)
11131 * (nparams - 1))));
11132 *slot = call_site;
11133 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11134 call_site->pc = pc;
11135
11136 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11137 {
11138 struct die_info *func_die;
11139
11140 /* Skip also over DW_TAG_inlined_subroutine. */
11141 for (func_die = die->parent;
11142 func_die && func_die->tag != DW_TAG_subprogram
11143 && func_die->tag != DW_TAG_subroutine_type;
11144 func_die = func_die->parent);
11145
11146 /* DW_AT_GNU_all_call_sites is a superset
11147 of DW_AT_GNU_all_tail_call_sites. */
11148 if (func_die
11149 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11150 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11151 {
11152 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11153 not complete. But keep CALL_SITE for look ups via call_site_htab,
11154 both the initial caller containing the real return address PC and
11155 the final callee containing the current PC of a chain of tail
11156 calls do not need to have the tail call list complete. But any
11157 function candidate for a virtual tail call frame searched via
11158 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11159 determined unambiguously. */
11160 }
11161 else
11162 {
11163 struct type *func_type = NULL;
11164
11165 if (func_die)
11166 func_type = get_die_type (func_die, cu);
11167 if (func_type != NULL)
11168 {
11169 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11170
11171 /* Enlist this call site to the function. */
11172 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11173 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11174 }
11175 else
11176 complaint (&symfile_complaints,
11177 _("Cannot find function owning DW_TAG_GNU_call_site "
11178 "DIE 0x%x [in module %s]"),
11179 die->offset.sect_off, objfile_name (objfile));
11180 }
11181 }
11182
11183 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11184 if (attr == NULL)
11185 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11186 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11187 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11188 /* Keep NULL DWARF_BLOCK. */;
11189 else if (attr_form_is_block (attr))
11190 {
11191 struct dwarf2_locexpr_baton *dlbaton;
11192
11193 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
11194 dlbaton->data = DW_BLOCK (attr)->data;
11195 dlbaton->size = DW_BLOCK (attr)->size;
11196 dlbaton->per_cu = cu->per_cu;
11197
11198 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11199 }
11200 else if (attr_form_is_ref (attr))
11201 {
11202 struct dwarf2_cu *target_cu = cu;
11203 struct die_info *target_die;
11204
11205 target_die = follow_die_ref (die, attr, &target_cu);
11206 gdb_assert (target_cu->objfile == objfile);
11207 if (die_is_declaration (target_die, target_cu))
11208 {
11209 const char *target_physname = NULL;
11210 struct attribute *target_attr;
11211
11212 /* Prefer the mangled name; otherwise compute the demangled one. */
11213 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
11214 if (target_attr == NULL)
11215 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
11216 target_cu);
11217 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
11218 target_physname = DW_STRING (target_attr);
11219 else
11220 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11221 if (target_physname == NULL)
11222 complaint (&symfile_complaints,
11223 _("DW_AT_GNU_call_site_target target DIE has invalid "
11224 "physname, for referencing DIE 0x%x [in module %s]"),
11225 die->offset.sect_off, objfile_name (objfile));
11226 else
11227 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11228 }
11229 else
11230 {
11231 CORE_ADDR lowpc;
11232
11233 /* DW_AT_entry_pc should be preferred. */
11234 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
11235 complaint (&symfile_complaints,
11236 _("DW_AT_GNU_call_site_target target DIE has invalid "
11237 "low pc, for referencing DIE 0x%x [in module %s]"),
11238 die->offset.sect_off, objfile_name (objfile));
11239 else
11240 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
11241 }
11242 }
11243 else
11244 complaint (&symfile_complaints,
11245 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11246 "block nor reference, for DIE 0x%x [in module %s]"),
11247 die->offset.sect_off, objfile_name (objfile));
11248
11249 call_site->per_cu = cu->per_cu;
11250
11251 for (child_die = die->child;
11252 child_die && child_die->tag;
11253 child_die = sibling_die (child_die))
11254 {
11255 struct call_site_parameter *parameter;
11256 struct attribute *loc, *origin;
11257
11258 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11259 {
11260 /* Already printed the complaint above. */
11261 continue;
11262 }
11263
11264 gdb_assert (call_site->parameter_count < nparams);
11265 parameter = &call_site->parameter[call_site->parameter_count];
11266
11267 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11268 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11269 register is contained in DW_AT_GNU_call_site_value. */
11270
11271 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11272 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11273 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11274 {
11275 sect_offset offset;
11276
11277 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11278 offset = dwarf2_get_ref_die_offset (origin);
11279 if (!offset_in_cu_p (&cu->header, offset))
11280 {
11281 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11282 binding can be done only inside one CU. Such referenced DIE
11283 therefore cannot be even moved to DW_TAG_partial_unit. */
11284 complaint (&symfile_complaints,
11285 _("DW_AT_abstract_origin offset is not in CU for "
11286 "DW_TAG_GNU_call_site child DIE 0x%x "
11287 "[in module %s]"),
11288 child_die->offset.sect_off, objfile_name (objfile));
11289 continue;
11290 }
11291 parameter->u.param_offset.cu_off = (offset.sect_off
11292 - cu->header.offset.sect_off);
11293 }
11294 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11295 {
11296 complaint (&symfile_complaints,
11297 _("No DW_FORM_block* DW_AT_location for "
11298 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11299 child_die->offset.sect_off, objfile_name (objfile));
11300 continue;
11301 }
11302 else
11303 {
11304 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11305 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11306 if (parameter->u.dwarf_reg != -1)
11307 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11308 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11309 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11310 &parameter->u.fb_offset))
11311 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11312 else
11313 {
11314 complaint (&symfile_complaints,
11315 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11316 "for DW_FORM_block* DW_AT_location is supported for "
11317 "DW_TAG_GNU_call_site child DIE 0x%x "
11318 "[in module %s]"),
11319 child_die->offset.sect_off, objfile_name (objfile));
11320 continue;
11321 }
11322 }
11323
11324 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11325 if (!attr_form_is_block (attr))
11326 {
11327 complaint (&symfile_complaints,
11328 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11329 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11330 child_die->offset.sect_off, objfile_name (objfile));
11331 continue;
11332 }
11333 parameter->value = DW_BLOCK (attr)->data;
11334 parameter->value_size = DW_BLOCK (attr)->size;
11335
11336 /* Parameters are not pre-cleared by memset above. */
11337 parameter->data_value = NULL;
11338 parameter->data_value_size = 0;
11339 call_site->parameter_count++;
11340
11341 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11342 if (attr)
11343 {
11344 if (!attr_form_is_block (attr))
11345 complaint (&symfile_complaints,
11346 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11347 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11348 child_die->offset.sect_off, objfile_name (objfile));
11349 else
11350 {
11351 parameter->data_value = DW_BLOCK (attr)->data;
11352 parameter->data_value_size = DW_BLOCK (attr)->size;
11353 }
11354 }
11355 }
11356 }
11357
11358 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11359 Return 1 if the attributes are present and valid, otherwise, return 0.
11360 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
11361
11362 static int
11363 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11364 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11365 struct partial_symtab *ranges_pst)
11366 {
11367 struct objfile *objfile = cu->objfile;
11368 struct comp_unit_head *cu_header = &cu->header;
11369 bfd *obfd = objfile->obfd;
11370 unsigned int addr_size = cu_header->addr_size;
11371 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11372 /* Base address selection entry. */
11373 CORE_ADDR base;
11374 int found_base;
11375 unsigned int dummy;
11376 const gdb_byte *buffer;
11377 CORE_ADDR marker;
11378 int low_set;
11379 CORE_ADDR low = 0;
11380 CORE_ADDR high = 0;
11381 CORE_ADDR baseaddr;
11382
11383 found_base = cu->base_known;
11384 base = cu->base_address;
11385
11386 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11387 if (offset >= dwarf2_per_objfile->ranges.size)
11388 {
11389 complaint (&symfile_complaints,
11390 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11391 offset);
11392 return 0;
11393 }
11394 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11395
11396 /* Read in the largest possible address. */
11397 marker = read_address (obfd, buffer, cu, &dummy);
11398 if ((marker & mask) == mask)
11399 {
11400 /* If we found the largest possible address, then
11401 read the base address. */
11402 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11403 buffer += 2 * addr_size;
11404 offset += 2 * addr_size;
11405 found_base = 1;
11406 }
11407
11408 low_set = 0;
11409
11410 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11411
11412 while (1)
11413 {
11414 CORE_ADDR range_beginning, range_end;
11415
11416 range_beginning = read_address (obfd, buffer, cu, &dummy);
11417 buffer += addr_size;
11418 range_end = read_address (obfd, buffer, cu, &dummy);
11419 buffer += addr_size;
11420 offset += 2 * addr_size;
11421
11422 /* An end of list marker is a pair of zero addresses. */
11423 if (range_beginning == 0 && range_end == 0)
11424 /* Found the end of list entry. */
11425 break;
11426
11427 /* Each base address selection entry is a pair of 2 values.
11428 The first is the largest possible address, the second is
11429 the base address. Check for a base address here. */
11430 if ((range_beginning & mask) == mask)
11431 {
11432 /* If we found the largest possible address, then
11433 read the base address. */
11434 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11435 found_base = 1;
11436 continue;
11437 }
11438
11439 if (!found_base)
11440 {
11441 /* We have no valid base address for the ranges
11442 data. */
11443 complaint (&symfile_complaints,
11444 _("Invalid .debug_ranges data (no base address)"));
11445 return 0;
11446 }
11447
11448 if (range_beginning > range_end)
11449 {
11450 /* Inverted range entries are invalid. */
11451 complaint (&symfile_complaints,
11452 _("Invalid .debug_ranges data (inverted range)"));
11453 return 0;
11454 }
11455
11456 /* Empty range entries have no effect. */
11457 if (range_beginning == range_end)
11458 continue;
11459
11460 range_beginning += base;
11461 range_end += base;
11462
11463 /* A not-uncommon case of bad debug info.
11464 Don't pollute the addrmap with bad data. */
11465 if (range_beginning + baseaddr == 0
11466 && !dwarf2_per_objfile->has_section_at_zero)
11467 {
11468 complaint (&symfile_complaints,
11469 _(".debug_ranges entry has start address of zero"
11470 " [in module %s]"), objfile_name (objfile));
11471 continue;
11472 }
11473
11474 if (ranges_pst != NULL)
11475 addrmap_set_empty (objfile->psymtabs_addrmap,
11476 range_beginning + baseaddr,
11477 range_end - 1 + baseaddr,
11478 ranges_pst);
11479
11480 /* FIXME: This is recording everything as a low-high
11481 segment of consecutive addresses. We should have a
11482 data structure for discontiguous block ranges
11483 instead. */
11484 if (! low_set)
11485 {
11486 low = range_beginning;
11487 high = range_end;
11488 low_set = 1;
11489 }
11490 else
11491 {
11492 if (range_beginning < low)
11493 low = range_beginning;
11494 if (range_end > high)
11495 high = range_end;
11496 }
11497 }
11498
11499 if (! low_set)
11500 /* If the first entry is an end-of-list marker, the range
11501 describes an empty scope, i.e. no instructions. */
11502 return 0;
11503
11504 if (low_return)
11505 *low_return = low;
11506 if (high_return)
11507 *high_return = high;
11508 return 1;
11509 }
11510
11511 /* Get low and high pc attributes from a die. Return 1 if the attributes
11512 are present and valid, otherwise, return 0. Return -1 if the range is
11513 discontinuous, i.e. derived from DW_AT_ranges information. */
11514
11515 static int
11516 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
11517 CORE_ADDR *highpc, struct dwarf2_cu *cu,
11518 struct partial_symtab *pst)
11519 {
11520 struct attribute *attr;
11521 struct attribute *attr_high;
11522 CORE_ADDR low = 0;
11523 CORE_ADDR high = 0;
11524 int ret = 0;
11525
11526 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11527 if (attr_high)
11528 {
11529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11530 if (attr)
11531 {
11532 low = DW_ADDR (attr);
11533 if (attr_high->form == DW_FORM_addr
11534 || attr_high->form == DW_FORM_GNU_addr_index)
11535 high = DW_ADDR (attr_high);
11536 else
11537 high = low + DW_UNSND (attr_high);
11538 }
11539 else
11540 /* Found high w/o low attribute. */
11541 return 0;
11542
11543 /* Found consecutive range of addresses. */
11544 ret = 1;
11545 }
11546 else
11547 {
11548 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11549 if (attr != NULL)
11550 {
11551 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11552 We take advantage of the fact that DW_AT_ranges does not appear
11553 in DW_TAG_compile_unit of DWO files. */
11554 int need_ranges_base = die->tag != DW_TAG_compile_unit;
11555 unsigned int ranges_offset = (DW_UNSND (attr)
11556 + (need_ranges_base
11557 ? cu->ranges_base
11558 : 0));
11559
11560 /* Value of the DW_AT_ranges attribute is the offset in the
11561 .debug_ranges section. */
11562 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
11563 return 0;
11564 /* Found discontinuous range of addresses. */
11565 ret = -1;
11566 }
11567 }
11568
11569 /* read_partial_die has also the strict LOW < HIGH requirement. */
11570 if (high <= low)
11571 return 0;
11572
11573 /* When using the GNU linker, .gnu.linkonce. sections are used to
11574 eliminate duplicate copies of functions and vtables and such.
11575 The linker will arbitrarily choose one and discard the others.
11576 The AT_*_pc values for such functions refer to local labels in
11577 these sections. If the section from that file was discarded, the
11578 labels are not in the output, so the relocs get a value of 0.
11579 If this is a discarded function, mark the pc bounds as invalid,
11580 so that GDB will ignore it. */
11581 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
11582 return 0;
11583
11584 *lowpc = low;
11585 if (highpc)
11586 *highpc = high;
11587 return ret;
11588 }
11589
11590 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11591 its low and high PC addresses. Do nothing if these addresses could not
11592 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11593 and HIGHPC to the high address if greater than HIGHPC. */
11594
11595 static void
11596 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11597 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11598 struct dwarf2_cu *cu)
11599 {
11600 CORE_ADDR low, high;
11601 struct die_info *child = die->child;
11602
11603 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
11604 {
11605 *lowpc = min (*lowpc, low);
11606 *highpc = max (*highpc, high);
11607 }
11608
11609 /* If the language does not allow nested subprograms (either inside
11610 subprograms or lexical blocks), we're done. */
11611 if (cu->language != language_ada)
11612 return;
11613
11614 /* Check all the children of the given DIE. If it contains nested
11615 subprograms, then check their pc bounds. Likewise, we need to
11616 check lexical blocks as well, as they may also contain subprogram
11617 definitions. */
11618 while (child && child->tag)
11619 {
11620 if (child->tag == DW_TAG_subprogram
11621 || child->tag == DW_TAG_lexical_block)
11622 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11623 child = sibling_die (child);
11624 }
11625 }
11626
11627 /* Get the low and high pc's represented by the scope DIE, and store
11628 them in *LOWPC and *HIGHPC. If the correct values can't be
11629 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11630
11631 static void
11632 get_scope_pc_bounds (struct die_info *die,
11633 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11634 struct dwarf2_cu *cu)
11635 {
11636 CORE_ADDR best_low = (CORE_ADDR) -1;
11637 CORE_ADDR best_high = (CORE_ADDR) 0;
11638 CORE_ADDR current_low, current_high;
11639
11640 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
11641 {
11642 best_low = current_low;
11643 best_high = current_high;
11644 }
11645 else
11646 {
11647 struct die_info *child = die->child;
11648
11649 while (child && child->tag)
11650 {
11651 switch (child->tag) {
11652 case DW_TAG_subprogram:
11653 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11654 break;
11655 case DW_TAG_namespace:
11656 case DW_TAG_module:
11657 /* FIXME: carlton/2004-01-16: Should we do this for
11658 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11659 that current GCC's always emit the DIEs corresponding
11660 to definitions of methods of classes as children of a
11661 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11662 the DIEs giving the declarations, which could be
11663 anywhere). But I don't see any reason why the
11664 standards says that they have to be there. */
11665 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11666
11667 if (current_low != ((CORE_ADDR) -1))
11668 {
11669 best_low = min (best_low, current_low);
11670 best_high = max (best_high, current_high);
11671 }
11672 break;
11673 default:
11674 /* Ignore. */
11675 break;
11676 }
11677
11678 child = sibling_die (child);
11679 }
11680 }
11681
11682 *lowpc = best_low;
11683 *highpc = best_high;
11684 }
11685
11686 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11687 in DIE. */
11688
11689 static void
11690 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11691 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11692 {
11693 struct objfile *objfile = cu->objfile;
11694 struct attribute *attr;
11695 struct attribute *attr_high;
11696
11697 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11698 if (attr_high)
11699 {
11700 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11701 if (attr)
11702 {
11703 CORE_ADDR low = DW_ADDR (attr);
11704 CORE_ADDR high;
11705 if (attr_high->form == DW_FORM_addr
11706 || attr_high->form == DW_FORM_GNU_addr_index)
11707 high = DW_ADDR (attr_high);
11708 else
11709 high = low + DW_UNSND (attr_high);
11710
11711 record_block_range (block, baseaddr + low, baseaddr + high - 1);
11712 }
11713 }
11714
11715 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11716 if (attr)
11717 {
11718 bfd *obfd = objfile->obfd;
11719 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11720 We take advantage of the fact that DW_AT_ranges does not appear
11721 in DW_TAG_compile_unit of DWO files. */
11722 int need_ranges_base = die->tag != DW_TAG_compile_unit;
11723
11724 /* The value of the DW_AT_ranges attribute is the offset of the
11725 address range list in the .debug_ranges section. */
11726 unsigned long offset = (DW_UNSND (attr)
11727 + (need_ranges_base ? cu->ranges_base : 0));
11728 const gdb_byte *buffer;
11729
11730 /* For some target architectures, but not others, the
11731 read_address function sign-extends the addresses it returns.
11732 To recognize base address selection entries, we need a
11733 mask. */
11734 unsigned int addr_size = cu->header.addr_size;
11735 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11736
11737 /* The base address, to which the next pair is relative. Note
11738 that this 'base' is a DWARF concept: most entries in a range
11739 list are relative, to reduce the number of relocs against the
11740 debugging information. This is separate from this function's
11741 'baseaddr' argument, which GDB uses to relocate debugging
11742 information from a shared library based on the address at
11743 which the library was loaded. */
11744 CORE_ADDR base = cu->base_address;
11745 int base_known = cu->base_known;
11746
11747 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11748 if (offset >= dwarf2_per_objfile->ranges.size)
11749 {
11750 complaint (&symfile_complaints,
11751 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11752 offset);
11753 return;
11754 }
11755 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11756
11757 for (;;)
11758 {
11759 unsigned int bytes_read;
11760 CORE_ADDR start, end;
11761
11762 start = read_address (obfd, buffer, cu, &bytes_read);
11763 buffer += bytes_read;
11764 end = read_address (obfd, buffer, cu, &bytes_read);
11765 buffer += bytes_read;
11766
11767 /* Did we find the end of the range list? */
11768 if (start == 0 && end == 0)
11769 break;
11770
11771 /* Did we find a base address selection entry? */
11772 else if ((start & base_select_mask) == base_select_mask)
11773 {
11774 base = end;
11775 base_known = 1;
11776 }
11777
11778 /* We found an ordinary address range. */
11779 else
11780 {
11781 if (!base_known)
11782 {
11783 complaint (&symfile_complaints,
11784 _("Invalid .debug_ranges data "
11785 "(no base address)"));
11786 return;
11787 }
11788
11789 if (start > end)
11790 {
11791 /* Inverted range entries are invalid. */
11792 complaint (&symfile_complaints,
11793 _("Invalid .debug_ranges data "
11794 "(inverted range)"));
11795 return;
11796 }
11797
11798 /* Empty range entries have no effect. */
11799 if (start == end)
11800 continue;
11801
11802 start += base + baseaddr;
11803 end += base + baseaddr;
11804
11805 /* A not-uncommon case of bad debug info.
11806 Don't pollute the addrmap with bad data. */
11807 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11808 {
11809 complaint (&symfile_complaints,
11810 _(".debug_ranges entry has start address of zero"
11811 " [in module %s]"), objfile_name (objfile));
11812 continue;
11813 }
11814
11815 record_block_range (block, start, end - 1);
11816 }
11817 }
11818 }
11819 }
11820
11821 /* Check whether the producer field indicates either of GCC < 4.6, or the
11822 Intel C/C++ compiler, and cache the result in CU. */
11823
11824 static void
11825 check_producer (struct dwarf2_cu *cu)
11826 {
11827 const char *cs;
11828 int major, minor, release;
11829
11830 if (cu->producer == NULL)
11831 {
11832 /* For unknown compilers expect their behavior is DWARF version
11833 compliant.
11834
11835 GCC started to support .debug_types sections by -gdwarf-4 since
11836 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11837 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11838 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11839 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11840 }
11841 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11842 {
11843 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11844
11845 cs = &cu->producer[strlen ("GNU ")];
11846 while (*cs && !isdigit (*cs))
11847 cs++;
11848 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11849 {
11850 /* Not recognized as GCC. */
11851 }
11852 else
11853 {
11854 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11855 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11856 }
11857 }
11858 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11859 cu->producer_is_icc = 1;
11860 else
11861 {
11862 /* For other non-GCC compilers, expect their behavior is DWARF version
11863 compliant. */
11864 }
11865
11866 cu->checked_producer = 1;
11867 }
11868
11869 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11870 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11871 during 4.6.0 experimental. */
11872
11873 static int
11874 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11875 {
11876 if (!cu->checked_producer)
11877 check_producer (cu);
11878
11879 return cu->producer_is_gxx_lt_4_6;
11880 }
11881
11882 /* Return the default accessibility type if it is not overriden by
11883 DW_AT_accessibility. */
11884
11885 static enum dwarf_access_attribute
11886 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11887 {
11888 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11889 {
11890 /* The default DWARF 2 accessibility for members is public, the default
11891 accessibility for inheritance is private. */
11892
11893 if (die->tag != DW_TAG_inheritance)
11894 return DW_ACCESS_public;
11895 else
11896 return DW_ACCESS_private;
11897 }
11898 else
11899 {
11900 /* DWARF 3+ defines the default accessibility a different way. The same
11901 rules apply now for DW_TAG_inheritance as for the members and it only
11902 depends on the container kind. */
11903
11904 if (die->parent->tag == DW_TAG_class_type)
11905 return DW_ACCESS_private;
11906 else
11907 return DW_ACCESS_public;
11908 }
11909 }
11910
11911 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11912 offset. If the attribute was not found return 0, otherwise return
11913 1. If it was found but could not properly be handled, set *OFFSET
11914 to 0. */
11915
11916 static int
11917 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11918 LONGEST *offset)
11919 {
11920 struct attribute *attr;
11921
11922 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11923 if (attr != NULL)
11924 {
11925 *offset = 0;
11926
11927 /* Note that we do not check for a section offset first here.
11928 This is because DW_AT_data_member_location is new in DWARF 4,
11929 so if we see it, we can assume that a constant form is really
11930 a constant and not a section offset. */
11931 if (attr_form_is_constant (attr))
11932 *offset = dwarf2_get_attr_constant_value (attr, 0);
11933 else if (attr_form_is_section_offset (attr))
11934 dwarf2_complex_location_expr_complaint ();
11935 else if (attr_form_is_block (attr))
11936 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11937 else
11938 dwarf2_complex_location_expr_complaint ();
11939
11940 return 1;
11941 }
11942
11943 return 0;
11944 }
11945
11946 /* Add an aggregate field to the field list. */
11947
11948 static void
11949 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11950 struct dwarf2_cu *cu)
11951 {
11952 struct objfile *objfile = cu->objfile;
11953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11954 struct nextfield *new_field;
11955 struct attribute *attr;
11956 struct field *fp;
11957 const char *fieldname = "";
11958
11959 /* Allocate a new field list entry and link it in. */
11960 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11961 make_cleanup (xfree, new_field);
11962 memset (new_field, 0, sizeof (struct nextfield));
11963
11964 if (die->tag == DW_TAG_inheritance)
11965 {
11966 new_field->next = fip->baseclasses;
11967 fip->baseclasses = new_field;
11968 }
11969 else
11970 {
11971 new_field->next = fip->fields;
11972 fip->fields = new_field;
11973 }
11974 fip->nfields++;
11975
11976 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11977 if (attr)
11978 new_field->accessibility = DW_UNSND (attr);
11979 else
11980 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11981 if (new_field->accessibility != DW_ACCESS_public)
11982 fip->non_public_fields = 1;
11983
11984 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11985 if (attr)
11986 new_field->virtuality = DW_UNSND (attr);
11987 else
11988 new_field->virtuality = DW_VIRTUALITY_none;
11989
11990 fp = &new_field->field;
11991
11992 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11993 {
11994 LONGEST offset;
11995
11996 /* Data member other than a C++ static data member. */
11997
11998 /* Get type of field. */
11999 fp->type = die_type (die, cu);
12000
12001 SET_FIELD_BITPOS (*fp, 0);
12002
12003 /* Get bit size of field (zero if none). */
12004 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12005 if (attr)
12006 {
12007 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12008 }
12009 else
12010 {
12011 FIELD_BITSIZE (*fp) = 0;
12012 }
12013
12014 /* Get bit offset of field. */
12015 if (handle_data_member_location (die, cu, &offset))
12016 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12017 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12018 if (attr)
12019 {
12020 if (gdbarch_bits_big_endian (gdbarch))
12021 {
12022 /* For big endian bits, the DW_AT_bit_offset gives the
12023 additional bit offset from the MSB of the containing
12024 anonymous object to the MSB of the field. We don't
12025 have to do anything special since we don't need to
12026 know the size of the anonymous object. */
12027 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12028 }
12029 else
12030 {
12031 /* For little endian bits, compute the bit offset to the
12032 MSB of the anonymous object, subtract off the number of
12033 bits from the MSB of the field to the MSB of the
12034 object, and then subtract off the number of bits of
12035 the field itself. The result is the bit offset of
12036 the LSB of the field. */
12037 int anonymous_size;
12038 int bit_offset = DW_UNSND (attr);
12039
12040 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12041 if (attr)
12042 {
12043 /* The size of the anonymous object containing
12044 the bit field is explicit, so use the
12045 indicated size (in bytes). */
12046 anonymous_size = DW_UNSND (attr);
12047 }
12048 else
12049 {
12050 /* The size of the anonymous object containing
12051 the bit field must be inferred from the type
12052 attribute of the data member containing the
12053 bit field. */
12054 anonymous_size = TYPE_LENGTH (fp->type);
12055 }
12056 SET_FIELD_BITPOS (*fp,
12057 (FIELD_BITPOS (*fp)
12058 + anonymous_size * bits_per_byte
12059 - bit_offset - FIELD_BITSIZE (*fp)));
12060 }
12061 }
12062
12063 /* Get name of field. */
12064 fieldname = dwarf2_name (die, cu);
12065 if (fieldname == NULL)
12066 fieldname = "";
12067
12068 /* The name is already allocated along with this objfile, so we don't
12069 need to duplicate it for the type. */
12070 fp->name = fieldname;
12071
12072 /* Change accessibility for artificial fields (e.g. virtual table
12073 pointer or virtual base class pointer) to private. */
12074 if (dwarf2_attr (die, DW_AT_artificial, cu))
12075 {
12076 FIELD_ARTIFICIAL (*fp) = 1;
12077 new_field->accessibility = DW_ACCESS_private;
12078 fip->non_public_fields = 1;
12079 }
12080 }
12081 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12082 {
12083 /* C++ static member. */
12084
12085 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12086 is a declaration, but all versions of G++ as of this writing
12087 (so through at least 3.2.1) incorrectly generate
12088 DW_TAG_variable tags. */
12089
12090 const char *physname;
12091
12092 /* Get name of field. */
12093 fieldname = dwarf2_name (die, cu);
12094 if (fieldname == NULL)
12095 return;
12096
12097 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12098 if (attr
12099 /* Only create a symbol if this is an external value.
12100 new_symbol checks this and puts the value in the global symbol
12101 table, which we want. If it is not external, new_symbol
12102 will try to put the value in cu->list_in_scope which is wrong. */
12103 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12104 {
12105 /* A static const member, not much different than an enum as far as
12106 we're concerned, except that we can support more types. */
12107 new_symbol (die, NULL, cu);
12108 }
12109
12110 /* Get physical name. */
12111 physname = dwarf2_physname (fieldname, die, cu);
12112
12113 /* The name is already allocated along with this objfile, so we don't
12114 need to duplicate it for the type. */
12115 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12116 FIELD_TYPE (*fp) = die_type (die, cu);
12117 FIELD_NAME (*fp) = fieldname;
12118 }
12119 else if (die->tag == DW_TAG_inheritance)
12120 {
12121 LONGEST offset;
12122
12123 /* C++ base class field. */
12124 if (handle_data_member_location (die, cu, &offset))
12125 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12126 FIELD_BITSIZE (*fp) = 0;
12127 FIELD_TYPE (*fp) = die_type (die, cu);
12128 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12129 fip->nbaseclasses++;
12130 }
12131 }
12132
12133 /* Add a typedef defined in the scope of the FIP's class. */
12134
12135 static void
12136 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12137 struct dwarf2_cu *cu)
12138 {
12139 struct objfile *objfile = cu->objfile;
12140 struct typedef_field_list *new_field;
12141 struct attribute *attr;
12142 struct typedef_field *fp;
12143 char *fieldname = "";
12144
12145 /* Allocate a new field list entry and link it in. */
12146 new_field = xzalloc (sizeof (*new_field));
12147 make_cleanup (xfree, new_field);
12148
12149 gdb_assert (die->tag == DW_TAG_typedef);
12150
12151 fp = &new_field->field;
12152
12153 /* Get name of field. */
12154 fp->name = dwarf2_name (die, cu);
12155 if (fp->name == NULL)
12156 return;
12157
12158 fp->type = read_type_die (die, cu);
12159
12160 new_field->next = fip->typedef_field_list;
12161 fip->typedef_field_list = new_field;
12162 fip->typedef_field_list_count++;
12163 }
12164
12165 /* Create the vector of fields, and attach it to the type. */
12166
12167 static void
12168 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12169 struct dwarf2_cu *cu)
12170 {
12171 int nfields = fip->nfields;
12172
12173 /* Record the field count, allocate space for the array of fields,
12174 and create blank accessibility bitfields if necessary. */
12175 TYPE_NFIELDS (type) = nfields;
12176 TYPE_FIELDS (type) = (struct field *)
12177 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12178 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12179
12180 if (fip->non_public_fields && cu->language != language_ada)
12181 {
12182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12183
12184 TYPE_FIELD_PRIVATE_BITS (type) =
12185 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12186 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12187
12188 TYPE_FIELD_PROTECTED_BITS (type) =
12189 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12190 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12191
12192 TYPE_FIELD_IGNORE_BITS (type) =
12193 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12194 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12195 }
12196
12197 /* If the type has baseclasses, allocate and clear a bit vector for
12198 TYPE_FIELD_VIRTUAL_BITS. */
12199 if (fip->nbaseclasses && cu->language != language_ada)
12200 {
12201 int num_bytes = B_BYTES (fip->nbaseclasses);
12202 unsigned char *pointer;
12203
12204 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12205 pointer = TYPE_ALLOC (type, num_bytes);
12206 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12207 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12208 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12209 }
12210
12211 /* Copy the saved-up fields into the field vector. Start from the head of
12212 the list, adding to the tail of the field array, so that they end up in
12213 the same order in the array in which they were added to the list. */
12214 while (nfields-- > 0)
12215 {
12216 struct nextfield *fieldp;
12217
12218 if (fip->fields)
12219 {
12220 fieldp = fip->fields;
12221 fip->fields = fieldp->next;
12222 }
12223 else
12224 {
12225 fieldp = fip->baseclasses;
12226 fip->baseclasses = fieldp->next;
12227 }
12228
12229 TYPE_FIELD (type, nfields) = fieldp->field;
12230 switch (fieldp->accessibility)
12231 {
12232 case DW_ACCESS_private:
12233 if (cu->language != language_ada)
12234 SET_TYPE_FIELD_PRIVATE (type, nfields);
12235 break;
12236
12237 case DW_ACCESS_protected:
12238 if (cu->language != language_ada)
12239 SET_TYPE_FIELD_PROTECTED (type, nfields);
12240 break;
12241
12242 case DW_ACCESS_public:
12243 break;
12244
12245 default:
12246 /* Unknown accessibility. Complain and treat it as public. */
12247 {
12248 complaint (&symfile_complaints, _("unsupported accessibility %d"),
12249 fieldp->accessibility);
12250 }
12251 break;
12252 }
12253 if (nfields < fip->nbaseclasses)
12254 {
12255 switch (fieldp->virtuality)
12256 {
12257 case DW_VIRTUALITY_virtual:
12258 case DW_VIRTUALITY_pure_virtual:
12259 if (cu->language == language_ada)
12260 error (_("unexpected virtuality in component of Ada type"));
12261 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12262 break;
12263 }
12264 }
12265 }
12266 }
12267
12268 /* Return true if this member function is a constructor, false
12269 otherwise. */
12270
12271 static int
12272 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12273 {
12274 const char *fieldname;
12275 const char *typename;
12276 int len;
12277
12278 if (die->parent == NULL)
12279 return 0;
12280
12281 if (die->parent->tag != DW_TAG_structure_type
12282 && die->parent->tag != DW_TAG_union_type
12283 && die->parent->tag != DW_TAG_class_type)
12284 return 0;
12285
12286 fieldname = dwarf2_name (die, cu);
12287 typename = dwarf2_name (die->parent, cu);
12288 if (fieldname == NULL || typename == NULL)
12289 return 0;
12290
12291 len = strlen (fieldname);
12292 return (strncmp (fieldname, typename, len) == 0
12293 && (typename[len] == '\0' || typename[len] == '<'));
12294 }
12295
12296 /* Add a member function to the proper fieldlist. */
12297
12298 static void
12299 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12300 struct type *type, struct dwarf2_cu *cu)
12301 {
12302 struct objfile *objfile = cu->objfile;
12303 struct attribute *attr;
12304 struct fnfieldlist *flp;
12305 int i;
12306 struct fn_field *fnp;
12307 const char *fieldname;
12308 struct nextfnfield *new_fnfield;
12309 struct type *this_type;
12310 enum dwarf_access_attribute accessibility;
12311
12312 if (cu->language == language_ada)
12313 error (_("unexpected member function in Ada type"));
12314
12315 /* Get name of member function. */
12316 fieldname = dwarf2_name (die, cu);
12317 if (fieldname == NULL)
12318 return;
12319
12320 /* Look up member function name in fieldlist. */
12321 for (i = 0; i < fip->nfnfields; i++)
12322 {
12323 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12324 break;
12325 }
12326
12327 /* Create new list element if necessary. */
12328 if (i < fip->nfnfields)
12329 flp = &fip->fnfieldlists[i];
12330 else
12331 {
12332 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12333 {
12334 fip->fnfieldlists = (struct fnfieldlist *)
12335 xrealloc (fip->fnfieldlists,
12336 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
12337 * sizeof (struct fnfieldlist));
12338 if (fip->nfnfields == 0)
12339 make_cleanup (free_current_contents, &fip->fnfieldlists);
12340 }
12341 flp = &fip->fnfieldlists[fip->nfnfields];
12342 flp->name = fieldname;
12343 flp->length = 0;
12344 flp->head = NULL;
12345 i = fip->nfnfields++;
12346 }
12347
12348 /* Create a new member function field and chain it to the field list
12349 entry. */
12350 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
12351 make_cleanup (xfree, new_fnfield);
12352 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12353 new_fnfield->next = flp->head;
12354 flp->head = new_fnfield;
12355 flp->length++;
12356
12357 /* Fill in the member function field info. */
12358 fnp = &new_fnfield->fnfield;
12359
12360 /* Delay processing of the physname until later. */
12361 if (cu->language == language_cplus || cu->language == language_java)
12362 {
12363 add_to_method_list (type, i, flp->length - 1, fieldname,
12364 die, cu);
12365 }
12366 else
12367 {
12368 const char *physname = dwarf2_physname (fieldname, die, cu);
12369 fnp->physname = physname ? physname : "";
12370 }
12371
12372 fnp->type = alloc_type (objfile);
12373 this_type = read_type_die (die, cu);
12374 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
12375 {
12376 int nparams = TYPE_NFIELDS (this_type);
12377
12378 /* TYPE is the domain of this method, and THIS_TYPE is the type
12379 of the method itself (TYPE_CODE_METHOD). */
12380 smash_to_method_type (fnp->type, type,
12381 TYPE_TARGET_TYPE (this_type),
12382 TYPE_FIELDS (this_type),
12383 TYPE_NFIELDS (this_type),
12384 TYPE_VARARGS (this_type));
12385
12386 /* Handle static member functions.
12387 Dwarf2 has no clean way to discern C++ static and non-static
12388 member functions. G++ helps GDB by marking the first
12389 parameter for non-static member functions (which is the this
12390 pointer) as artificial. We obtain this information from
12391 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12392 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12393 fnp->voffset = VOFFSET_STATIC;
12394 }
12395 else
12396 complaint (&symfile_complaints, _("member function type missing for '%s'"),
12397 dwarf2_full_name (fieldname, die, cu));
12398
12399 /* Get fcontext from DW_AT_containing_type if present. */
12400 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12401 fnp->fcontext = die_containing_type (die, cu);
12402
12403 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12404 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12405
12406 /* Get accessibility. */
12407 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12408 if (attr)
12409 accessibility = DW_UNSND (attr);
12410 else
12411 accessibility = dwarf2_default_access_attribute (die, cu);
12412 switch (accessibility)
12413 {
12414 case DW_ACCESS_private:
12415 fnp->is_private = 1;
12416 break;
12417 case DW_ACCESS_protected:
12418 fnp->is_protected = 1;
12419 break;
12420 }
12421
12422 /* Check for artificial methods. */
12423 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12424 if (attr && DW_UNSND (attr) != 0)
12425 fnp->is_artificial = 1;
12426
12427 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12428
12429 /* Get index in virtual function table if it is a virtual member
12430 function. For older versions of GCC, this is an offset in the
12431 appropriate virtual table, as specified by DW_AT_containing_type.
12432 For everyone else, it is an expression to be evaluated relative
12433 to the object address. */
12434
12435 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12436 if (attr)
12437 {
12438 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
12439 {
12440 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12441 {
12442 /* Old-style GCC. */
12443 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12444 }
12445 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12446 || (DW_BLOCK (attr)->size > 1
12447 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12448 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12449 {
12450 struct dwarf_block blk;
12451 int offset;
12452
12453 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
12454 ? 1 : 2);
12455 blk.size = DW_BLOCK (attr)->size - offset;
12456 blk.data = DW_BLOCK (attr)->data + offset;
12457 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12458 if ((fnp->voffset % cu->header.addr_size) != 0)
12459 dwarf2_complex_location_expr_complaint ();
12460 else
12461 fnp->voffset /= cu->header.addr_size;
12462 fnp->voffset += 2;
12463 }
12464 else
12465 dwarf2_complex_location_expr_complaint ();
12466
12467 if (!fnp->fcontext)
12468 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12469 }
12470 else if (attr_form_is_section_offset (attr))
12471 {
12472 dwarf2_complex_location_expr_complaint ();
12473 }
12474 else
12475 {
12476 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12477 fieldname);
12478 }
12479 }
12480 else
12481 {
12482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12483 if (attr && DW_UNSND (attr))
12484 {
12485 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12486 complaint (&symfile_complaints,
12487 _("Member function \"%s\" (offset %d) is virtual "
12488 "but the vtable offset is not specified"),
12489 fieldname, die->offset.sect_off);
12490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12491 TYPE_CPLUS_DYNAMIC (type) = 1;
12492 }
12493 }
12494 }
12495
12496 /* Create the vector of member function fields, and attach it to the type. */
12497
12498 static void
12499 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12500 struct dwarf2_cu *cu)
12501 {
12502 struct fnfieldlist *flp;
12503 int i;
12504
12505 if (cu->language == language_ada)
12506 error (_("unexpected member functions in Ada type"));
12507
12508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12509 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12510 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
12511
12512 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
12513 {
12514 struct nextfnfield *nfp = flp->head;
12515 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12516 int k;
12517
12518 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
12519 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
12520 fn_flp->fn_fields = (struct fn_field *)
12521 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
12522 for (k = flp->length; (k--, nfp); nfp = nfp->next)
12523 fn_flp->fn_fields[k] = nfp->fnfield;
12524 }
12525
12526 TYPE_NFN_FIELDS (type) = fip->nfnfields;
12527 }
12528
12529 /* Returns non-zero if NAME is the name of a vtable member in CU's
12530 language, zero otherwise. */
12531 static int
12532 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12533 {
12534 static const char vptr[] = "_vptr";
12535 static const char vtable[] = "vtable";
12536
12537 /* Look for the C++ and Java forms of the vtable. */
12538 if ((cu->language == language_java
12539 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
12540 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
12541 && is_cplus_marker (name[sizeof (vptr) - 1])))
12542 return 1;
12543
12544 return 0;
12545 }
12546
12547 /* GCC outputs unnamed structures that are really pointers to member
12548 functions, with the ABI-specified layout. If TYPE describes
12549 such a structure, smash it into a member function type.
12550
12551 GCC shouldn't do this; it should just output pointer to member DIEs.
12552 This is GCC PR debug/28767. */
12553
12554 static void
12555 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12556 {
12557 struct type *pfn_type, *domain_type, *new_type;
12558
12559 /* Check for a structure with no name and two children. */
12560 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
12561 return;
12562
12563 /* Check for __pfn and __delta members. */
12564 if (TYPE_FIELD_NAME (type, 0) == NULL
12565 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
12566 || TYPE_FIELD_NAME (type, 1) == NULL
12567 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
12568 return;
12569
12570 /* Find the type of the method. */
12571 pfn_type = TYPE_FIELD_TYPE (type, 0);
12572 if (pfn_type == NULL
12573 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
12574 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
12575 return;
12576
12577 /* Look for the "this" argument. */
12578 pfn_type = TYPE_TARGET_TYPE (pfn_type);
12579 if (TYPE_NFIELDS (pfn_type) == 0
12580 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
12581 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
12582 return;
12583
12584 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
12585 new_type = alloc_type (objfile);
12586 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
12587 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
12588 TYPE_VARARGS (pfn_type));
12589 smash_to_methodptr_type (type, new_type);
12590 }
12591
12592 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
12593 (icc). */
12594
12595 static int
12596 producer_is_icc (struct dwarf2_cu *cu)
12597 {
12598 if (!cu->checked_producer)
12599 check_producer (cu);
12600
12601 return cu->producer_is_icc;
12602 }
12603
12604 /* Called when we find the DIE that starts a structure or union scope
12605 (definition) to create a type for the structure or union. Fill in
12606 the type's name and general properties; the members will not be
12607 processed until process_structure_scope.
12608
12609 NOTE: we need to call these functions regardless of whether or not the
12610 DIE has a DW_AT_name attribute, since it might be an anonymous
12611 structure or union. This gets the type entered into our set of
12612 user defined types.
12613
12614 However, if the structure is incomplete (an opaque struct/union)
12615 then suppress creating a symbol table entry for it since gdb only
12616 wants to find the one with the complete definition. Note that if
12617 it is complete, we just call new_symbol, which does it's own
12618 checking about whether the struct/union is anonymous or not (and
12619 suppresses creating a symbol table entry itself). */
12620
12621 static struct type *
12622 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12623 {
12624 struct objfile *objfile = cu->objfile;
12625 struct type *type;
12626 struct attribute *attr;
12627 const char *name;
12628
12629 /* If the definition of this type lives in .debug_types, read that type.
12630 Don't follow DW_AT_specification though, that will take us back up
12631 the chain and we want to go down. */
12632 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12633 if (attr)
12634 {
12635 type = get_DW_AT_signature_type (die, attr, cu);
12636
12637 /* The type's CU may not be the same as CU.
12638 Ensure TYPE is recorded with CU in die_type_hash. */
12639 return set_die_type (die, type, cu);
12640 }
12641
12642 type = alloc_type (objfile);
12643 INIT_CPLUS_SPECIFIC (type);
12644
12645 name = dwarf2_name (die, cu);
12646 if (name != NULL)
12647 {
12648 if (cu->language == language_cplus
12649 || cu->language == language_java)
12650 {
12651 const char *full_name = dwarf2_full_name (name, die, cu);
12652
12653 /* dwarf2_full_name might have already finished building the DIE's
12654 type. If so, there is no need to continue. */
12655 if (get_die_type (die, cu) != NULL)
12656 return get_die_type (die, cu);
12657
12658 TYPE_TAG_NAME (type) = full_name;
12659 if (die->tag == DW_TAG_structure_type
12660 || die->tag == DW_TAG_class_type)
12661 TYPE_NAME (type) = TYPE_TAG_NAME (type);
12662 }
12663 else
12664 {
12665 /* The name is already allocated along with this objfile, so
12666 we don't need to duplicate it for the type. */
12667 TYPE_TAG_NAME (type) = name;
12668 if (die->tag == DW_TAG_class_type)
12669 TYPE_NAME (type) = TYPE_TAG_NAME (type);
12670 }
12671 }
12672
12673 if (die->tag == DW_TAG_structure_type)
12674 {
12675 TYPE_CODE (type) = TYPE_CODE_STRUCT;
12676 }
12677 else if (die->tag == DW_TAG_union_type)
12678 {
12679 TYPE_CODE (type) = TYPE_CODE_UNION;
12680 }
12681 else
12682 {
12683 TYPE_CODE (type) = TYPE_CODE_CLASS;
12684 }
12685
12686 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
12687 TYPE_DECLARED_CLASS (type) = 1;
12688
12689 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12690 if (attr)
12691 {
12692 TYPE_LENGTH (type) = DW_UNSND (attr);
12693 }
12694 else
12695 {
12696 TYPE_LENGTH (type) = 0;
12697 }
12698
12699 if (producer_is_icc (cu))
12700 {
12701 /* ICC does not output the required DW_AT_declaration
12702 on incomplete types, but gives them a size of zero. */
12703 }
12704 else
12705 TYPE_STUB_SUPPORTED (type) = 1;
12706
12707 if (die_is_declaration (die, cu))
12708 TYPE_STUB (type) = 1;
12709 else if (attr == NULL && die->child == NULL
12710 && producer_is_realview (cu->producer))
12711 /* RealView does not output the required DW_AT_declaration
12712 on incomplete types. */
12713 TYPE_STUB (type) = 1;
12714
12715 /* We need to add the type field to the die immediately so we don't
12716 infinitely recurse when dealing with pointers to the structure
12717 type within the structure itself. */
12718 set_die_type (die, type, cu);
12719
12720 /* set_die_type should be already done. */
12721 set_descriptive_type (type, die, cu);
12722
12723 return type;
12724 }
12725
12726 /* Finish creating a structure or union type, including filling in
12727 its members and creating a symbol for it. */
12728
12729 static void
12730 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12731 {
12732 struct objfile *objfile = cu->objfile;
12733 struct die_info *child_die = die->child;
12734 struct type *type;
12735
12736 type = get_die_type (die, cu);
12737 if (type == NULL)
12738 type = read_structure_type (die, cu);
12739
12740 if (die->child != NULL && ! die_is_declaration (die, cu))
12741 {
12742 struct field_info fi;
12743 struct die_info *child_die;
12744 VEC (symbolp) *template_args = NULL;
12745 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
12746
12747 memset (&fi, 0, sizeof (struct field_info));
12748
12749 child_die = die->child;
12750
12751 while (child_die && child_die->tag)
12752 {
12753 if (child_die->tag == DW_TAG_member
12754 || child_die->tag == DW_TAG_variable)
12755 {
12756 /* NOTE: carlton/2002-11-05: A C++ static data member
12757 should be a DW_TAG_member that is a declaration, but
12758 all versions of G++ as of this writing (so through at
12759 least 3.2.1) incorrectly generate DW_TAG_variable
12760 tags for them instead. */
12761 dwarf2_add_field (&fi, child_die, cu);
12762 }
12763 else if (child_die->tag == DW_TAG_subprogram)
12764 {
12765 /* C++ member function. */
12766 dwarf2_add_member_fn (&fi, child_die, type, cu);
12767 }
12768 else if (child_die->tag == DW_TAG_inheritance)
12769 {
12770 /* C++ base class field. */
12771 dwarf2_add_field (&fi, child_die, cu);
12772 }
12773 else if (child_die->tag == DW_TAG_typedef)
12774 dwarf2_add_typedef (&fi, child_die, cu);
12775 else if (child_die->tag == DW_TAG_template_type_param
12776 || child_die->tag == DW_TAG_template_value_param)
12777 {
12778 struct symbol *arg = new_symbol (child_die, NULL, cu);
12779
12780 if (arg != NULL)
12781 VEC_safe_push (symbolp, template_args, arg);
12782 }
12783
12784 child_die = sibling_die (child_die);
12785 }
12786
12787 /* Attach template arguments to type. */
12788 if (! VEC_empty (symbolp, template_args))
12789 {
12790 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12791 TYPE_N_TEMPLATE_ARGUMENTS (type)
12792 = VEC_length (symbolp, template_args);
12793 TYPE_TEMPLATE_ARGUMENTS (type)
12794 = obstack_alloc (&objfile->objfile_obstack,
12795 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12796 * sizeof (struct symbol *)));
12797 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12798 VEC_address (symbolp, template_args),
12799 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12800 * sizeof (struct symbol *)));
12801 VEC_free (symbolp, template_args);
12802 }
12803
12804 /* Attach fields and member functions to the type. */
12805 if (fi.nfields)
12806 dwarf2_attach_fields_to_type (&fi, type, cu);
12807 if (fi.nfnfields)
12808 {
12809 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12810
12811 /* Get the type which refers to the base class (possibly this
12812 class itself) which contains the vtable pointer for the current
12813 class from the DW_AT_containing_type attribute. This use of
12814 DW_AT_containing_type is a GNU extension. */
12815
12816 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12817 {
12818 struct type *t = die_containing_type (die, cu);
12819
12820 TYPE_VPTR_BASETYPE (type) = t;
12821 if (type == t)
12822 {
12823 int i;
12824
12825 /* Our own class provides vtbl ptr. */
12826 for (i = TYPE_NFIELDS (t) - 1;
12827 i >= TYPE_N_BASECLASSES (t);
12828 --i)
12829 {
12830 const char *fieldname = TYPE_FIELD_NAME (t, i);
12831
12832 if (is_vtable_name (fieldname, cu))
12833 {
12834 TYPE_VPTR_FIELDNO (type) = i;
12835 break;
12836 }
12837 }
12838
12839 /* Complain if virtual function table field not found. */
12840 if (i < TYPE_N_BASECLASSES (t))
12841 complaint (&symfile_complaints,
12842 _("virtual function table pointer "
12843 "not found when defining class '%s'"),
12844 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12845 "");
12846 }
12847 else
12848 {
12849 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12850 }
12851 }
12852 else if (cu->producer
12853 && strncmp (cu->producer,
12854 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12855 {
12856 /* The IBM XLC compiler does not provide direct indication
12857 of the containing type, but the vtable pointer is
12858 always named __vfp. */
12859
12860 int i;
12861
12862 for (i = TYPE_NFIELDS (type) - 1;
12863 i >= TYPE_N_BASECLASSES (type);
12864 --i)
12865 {
12866 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12867 {
12868 TYPE_VPTR_FIELDNO (type) = i;
12869 TYPE_VPTR_BASETYPE (type) = type;
12870 break;
12871 }
12872 }
12873 }
12874 }
12875
12876 /* Copy fi.typedef_field_list linked list elements content into the
12877 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12878 if (fi.typedef_field_list)
12879 {
12880 int i = fi.typedef_field_list_count;
12881
12882 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12883 TYPE_TYPEDEF_FIELD_ARRAY (type)
12884 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12885 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12886
12887 /* Reverse the list order to keep the debug info elements order. */
12888 while (--i >= 0)
12889 {
12890 struct typedef_field *dest, *src;
12891
12892 dest = &TYPE_TYPEDEF_FIELD (type, i);
12893 src = &fi.typedef_field_list->field;
12894 fi.typedef_field_list = fi.typedef_field_list->next;
12895 *dest = *src;
12896 }
12897 }
12898
12899 do_cleanups (back_to);
12900
12901 if (HAVE_CPLUS_STRUCT (type))
12902 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12903 }
12904
12905 quirk_gcc_member_function_pointer (type, objfile);
12906
12907 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12908 snapshots) has been known to create a die giving a declaration
12909 for a class that has, as a child, a die giving a definition for a
12910 nested class. So we have to process our children even if the
12911 current die is a declaration. Normally, of course, a declaration
12912 won't have any children at all. */
12913
12914 while (child_die != NULL && child_die->tag)
12915 {
12916 if (child_die->tag == DW_TAG_member
12917 || child_die->tag == DW_TAG_variable
12918 || child_die->tag == DW_TAG_inheritance
12919 || child_die->tag == DW_TAG_template_value_param
12920 || child_die->tag == DW_TAG_template_type_param)
12921 {
12922 /* Do nothing. */
12923 }
12924 else
12925 process_die (child_die, cu);
12926
12927 child_die = sibling_die (child_die);
12928 }
12929
12930 /* Do not consider external references. According to the DWARF standard,
12931 these DIEs are identified by the fact that they have no byte_size
12932 attribute, and a declaration attribute. */
12933 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12934 || !die_is_declaration (die, cu))
12935 new_symbol (die, type, cu);
12936 }
12937
12938 /* Given a DW_AT_enumeration_type die, set its type. We do not
12939 complete the type's fields yet, or create any symbols. */
12940
12941 static struct type *
12942 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12943 {
12944 struct objfile *objfile = cu->objfile;
12945 struct type *type;
12946 struct attribute *attr;
12947 const char *name;
12948
12949 /* If the definition of this type lives in .debug_types, read that type.
12950 Don't follow DW_AT_specification though, that will take us back up
12951 the chain and we want to go down. */
12952 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12953 if (attr)
12954 {
12955 type = get_DW_AT_signature_type (die, attr, cu);
12956
12957 /* The type's CU may not be the same as CU.
12958 Ensure TYPE is recorded with CU in die_type_hash. */
12959 return set_die_type (die, type, cu);
12960 }
12961
12962 type = alloc_type (objfile);
12963
12964 TYPE_CODE (type) = TYPE_CODE_ENUM;
12965 name = dwarf2_full_name (NULL, die, cu);
12966 if (name != NULL)
12967 TYPE_TAG_NAME (type) = name;
12968
12969 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12970 if (attr)
12971 {
12972 TYPE_LENGTH (type) = DW_UNSND (attr);
12973 }
12974 else
12975 {
12976 TYPE_LENGTH (type) = 0;
12977 }
12978
12979 /* The enumeration DIE can be incomplete. In Ada, any type can be
12980 declared as private in the package spec, and then defined only
12981 inside the package body. Such types are known as Taft Amendment
12982 Types. When another package uses such a type, an incomplete DIE
12983 may be generated by the compiler. */
12984 if (die_is_declaration (die, cu))
12985 TYPE_STUB (type) = 1;
12986
12987 return set_die_type (die, type, cu);
12988 }
12989
12990 /* Given a pointer to a die which begins an enumeration, process all
12991 the dies that define the members of the enumeration, and create the
12992 symbol for the enumeration type.
12993
12994 NOTE: We reverse the order of the element list. */
12995
12996 static void
12997 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12998 {
12999 struct type *this_type;
13000
13001 this_type = get_die_type (die, cu);
13002 if (this_type == NULL)
13003 this_type = read_enumeration_type (die, cu);
13004
13005 if (die->child != NULL)
13006 {
13007 struct die_info *child_die;
13008 struct symbol *sym;
13009 struct field *fields = NULL;
13010 int num_fields = 0;
13011 int unsigned_enum = 1;
13012 const char *name;
13013 int flag_enum = 1;
13014 ULONGEST mask = 0;
13015
13016 child_die = die->child;
13017 while (child_die && child_die->tag)
13018 {
13019 if (child_die->tag != DW_TAG_enumerator)
13020 {
13021 process_die (child_die, cu);
13022 }
13023 else
13024 {
13025 name = dwarf2_name (child_die, cu);
13026 if (name)
13027 {
13028 sym = new_symbol (child_die, this_type, cu);
13029 if (SYMBOL_VALUE (sym) < 0)
13030 {
13031 unsigned_enum = 0;
13032 flag_enum = 0;
13033 }
13034 else if ((mask & SYMBOL_VALUE (sym)) != 0)
13035 flag_enum = 0;
13036 else
13037 mask |= SYMBOL_VALUE (sym);
13038
13039 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13040 {
13041 fields = (struct field *)
13042 xrealloc (fields,
13043 (num_fields + DW_FIELD_ALLOC_CHUNK)
13044 * sizeof (struct field));
13045 }
13046
13047 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13048 FIELD_TYPE (fields[num_fields]) = NULL;
13049 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13050 FIELD_BITSIZE (fields[num_fields]) = 0;
13051
13052 num_fields++;
13053 }
13054 }
13055
13056 child_die = sibling_die (child_die);
13057 }
13058
13059 if (num_fields)
13060 {
13061 TYPE_NFIELDS (this_type) = num_fields;
13062 TYPE_FIELDS (this_type) = (struct field *)
13063 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13064 memcpy (TYPE_FIELDS (this_type), fields,
13065 sizeof (struct field) * num_fields);
13066 xfree (fields);
13067 }
13068 if (unsigned_enum)
13069 TYPE_UNSIGNED (this_type) = 1;
13070 if (flag_enum)
13071 TYPE_FLAG_ENUM (this_type) = 1;
13072 }
13073
13074 /* If we are reading an enum from a .debug_types unit, and the enum
13075 is a declaration, and the enum is not the signatured type in the
13076 unit, then we do not want to add a symbol for it. Adding a
13077 symbol would in some cases obscure the true definition of the
13078 enum, giving users an incomplete type when the definition is
13079 actually available. Note that we do not want to do this for all
13080 enums which are just declarations, because C++0x allows forward
13081 enum declarations. */
13082 if (cu->per_cu->is_debug_types
13083 && die_is_declaration (die, cu))
13084 {
13085 struct signatured_type *sig_type;
13086
13087 sig_type = (struct signatured_type *) cu->per_cu;
13088 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13089 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13090 return;
13091 }
13092
13093 new_symbol (die, this_type, cu);
13094 }
13095
13096 /* Extract all information from a DW_TAG_array_type DIE and put it in
13097 the DIE's type field. For now, this only handles one dimensional
13098 arrays. */
13099
13100 static struct type *
13101 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13102 {
13103 struct objfile *objfile = cu->objfile;
13104 struct die_info *child_die;
13105 struct type *type;
13106 struct type *element_type, *range_type, *index_type;
13107 struct type **range_types = NULL;
13108 struct attribute *attr;
13109 int ndim = 0;
13110 struct cleanup *back_to;
13111 const char *name;
13112
13113 element_type = die_type (die, cu);
13114
13115 /* The die_type call above may have already set the type for this DIE. */
13116 type = get_die_type (die, cu);
13117 if (type)
13118 return type;
13119
13120 /* Irix 6.2 native cc creates array types without children for
13121 arrays with unspecified length. */
13122 if (die->child == NULL)
13123 {
13124 index_type = objfile_type (objfile)->builtin_int;
13125 range_type = create_range_type (NULL, index_type, 0, -1);
13126 type = create_array_type (NULL, element_type, range_type);
13127 return set_die_type (die, type, cu);
13128 }
13129
13130 back_to = make_cleanup (null_cleanup, NULL);
13131 child_die = die->child;
13132 while (child_die && child_die->tag)
13133 {
13134 if (child_die->tag == DW_TAG_subrange_type)
13135 {
13136 struct type *child_type = read_type_die (child_die, cu);
13137
13138 if (child_type != NULL)
13139 {
13140 /* The range type was succesfully read. Save it for the
13141 array type creation. */
13142 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13143 {
13144 range_types = (struct type **)
13145 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13146 * sizeof (struct type *));
13147 if (ndim == 0)
13148 make_cleanup (free_current_contents, &range_types);
13149 }
13150 range_types[ndim++] = child_type;
13151 }
13152 }
13153 child_die = sibling_die (child_die);
13154 }
13155
13156 /* Dwarf2 dimensions are output from left to right, create the
13157 necessary array types in backwards order. */
13158
13159 type = element_type;
13160
13161 if (read_array_order (die, cu) == DW_ORD_col_major)
13162 {
13163 int i = 0;
13164
13165 while (i < ndim)
13166 type = create_array_type (NULL, type, range_types[i++]);
13167 }
13168 else
13169 {
13170 while (ndim-- > 0)
13171 type = create_array_type (NULL, type, range_types[ndim]);
13172 }
13173
13174 /* Understand Dwarf2 support for vector types (like they occur on
13175 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13176 array type. This is not part of the Dwarf2/3 standard yet, but a
13177 custom vendor extension. The main difference between a regular
13178 array and the vector variant is that vectors are passed by value
13179 to functions. */
13180 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13181 if (attr)
13182 make_vector_type (type);
13183
13184 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13185 implementation may choose to implement triple vectors using this
13186 attribute. */
13187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13188 if (attr)
13189 {
13190 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13191 TYPE_LENGTH (type) = DW_UNSND (attr);
13192 else
13193 complaint (&symfile_complaints,
13194 _("DW_AT_byte_size for array type smaller "
13195 "than the total size of elements"));
13196 }
13197
13198 name = dwarf2_name (die, cu);
13199 if (name)
13200 TYPE_NAME (type) = name;
13201
13202 /* Install the type in the die. */
13203 set_die_type (die, type, cu);
13204
13205 /* set_die_type should be already done. */
13206 set_descriptive_type (type, die, cu);
13207
13208 do_cleanups (back_to);
13209
13210 return type;
13211 }
13212
13213 static enum dwarf_array_dim_ordering
13214 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13215 {
13216 struct attribute *attr;
13217
13218 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13219
13220 if (attr) return DW_SND (attr);
13221
13222 /* GNU F77 is a special case, as at 08/2004 array type info is the
13223 opposite order to the dwarf2 specification, but data is still
13224 laid out as per normal fortran.
13225
13226 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13227 version checking. */
13228
13229 if (cu->language == language_fortran
13230 && cu->producer && strstr (cu->producer, "GNU F77"))
13231 {
13232 return DW_ORD_row_major;
13233 }
13234
13235 switch (cu->language_defn->la_array_ordering)
13236 {
13237 case array_column_major:
13238 return DW_ORD_col_major;
13239 case array_row_major:
13240 default:
13241 return DW_ORD_row_major;
13242 };
13243 }
13244
13245 /* Extract all information from a DW_TAG_set_type DIE and put it in
13246 the DIE's type field. */
13247
13248 static struct type *
13249 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13250 {
13251 struct type *domain_type, *set_type;
13252 struct attribute *attr;
13253
13254 domain_type = die_type (die, cu);
13255
13256 /* The die_type call above may have already set the type for this DIE. */
13257 set_type = get_die_type (die, cu);
13258 if (set_type)
13259 return set_type;
13260
13261 set_type = create_set_type (NULL, domain_type);
13262
13263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13264 if (attr)
13265 TYPE_LENGTH (set_type) = DW_UNSND (attr);
13266
13267 return set_die_type (die, set_type, cu);
13268 }
13269
13270 /* A helper for read_common_block that creates a locexpr baton.
13271 SYM is the symbol which we are marking as computed.
13272 COMMON_DIE is the DIE for the common block.
13273 COMMON_LOC is the location expression attribute for the common
13274 block itself.
13275 MEMBER_LOC is the location expression attribute for the particular
13276 member of the common block that we are processing.
13277 CU is the CU from which the above come. */
13278
13279 static void
13280 mark_common_block_symbol_computed (struct symbol *sym,
13281 struct die_info *common_die,
13282 struct attribute *common_loc,
13283 struct attribute *member_loc,
13284 struct dwarf2_cu *cu)
13285 {
13286 struct objfile *objfile = dwarf2_per_objfile->objfile;
13287 struct dwarf2_locexpr_baton *baton;
13288 gdb_byte *ptr;
13289 unsigned int cu_off;
13290 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13291 LONGEST offset = 0;
13292
13293 gdb_assert (common_loc && member_loc);
13294 gdb_assert (attr_form_is_block (common_loc));
13295 gdb_assert (attr_form_is_block (member_loc)
13296 || attr_form_is_constant (member_loc));
13297
13298 baton = obstack_alloc (&objfile->objfile_obstack,
13299 sizeof (struct dwarf2_locexpr_baton));
13300 baton->per_cu = cu->per_cu;
13301 gdb_assert (baton->per_cu);
13302
13303 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13304
13305 if (attr_form_is_constant (member_loc))
13306 {
13307 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13308 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13309 }
13310 else
13311 baton->size += DW_BLOCK (member_loc)->size;
13312
13313 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
13314 baton->data = ptr;
13315
13316 *ptr++ = DW_OP_call4;
13317 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13318 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13319 ptr += 4;
13320
13321 if (attr_form_is_constant (member_loc))
13322 {
13323 *ptr++ = DW_OP_addr;
13324 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13325 ptr += cu->header.addr_size;
13326 }
13327 else
13328 {
13329 /* We have to copy the data here, because DW_OP_call4 will only
13330 use a DW_AT_location attribute. */
13331 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13332 ptr += DW_BLOCK (member_loc)->size;
13333 }
13334
13335 *ptr++ = DW_OP_plus;
13336 gdb_assert (ptr - baton->data == baton->size);
13337
13338 SYMBOL_LOCATION_BATON (sym) = baton;
13339 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
13340 }
13341
13342 /* Create appropriate locally-scoped variables for all the
13343 DW_TAG_common_block entries. Also create a struct common_block
13344 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13345 is used to sepate the common blocks name namespace from regular
13346 variable names. */
13347
13348 static void
13349 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13350 {
13351 struct attribute *attr;
13352
13353 attr = dwarf2_attr (die, DW_AT_location, cu);
13354 if (attr)
13355 {
13356 /* Support the .debug_loc offsets. */
13357 if (attr_form_is_block (attr))
13358 {
13359 /* Ok. */
13360 }
13361 else if (attr_form_is_section_offset (attr))
13362 {
13363 dwarf2_complex_location_expr_complaint ();
13364 attr = NULL;
13365 }
13366 else
13367 {
13368 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13369 "common block member");
13370 attr = NULL;
13371 }
13372 }
13373
13374 if (die->child != NULL)
13375 {
13376 struct objfile *objfile = cu->objfile;
13377 struct die_info *child_die;
13378 size_t n_entries = 0, size;
13379 struct common_block *common_block;
13380 struct symbol *sym;
13381
13382 for (child_die = die->child;
13383 child_die && child_die->tag;
13384 child_die = sibling_die (child_die))
13385 ++n_entries;
13386
13387 size = (sizeof (struct common_block)
13388 + (n_entries - 1) * sizeof (struct symbol *));
13389 common_block = obstack_alloc (&objfile->objfile_obstack, size);
13390 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13391 common_block->n_entries = 0;
13392
13393 for (child_die = die->child;
13394 child_die && child_die->tag;
13395 child_die = sibling_die (child_die))
13396 {
13397 /* Create the symbol in the DW_TAG_common_block block in the current
13398 symbol scope. */
13399 sym = new_symbol (child_die, NULL, cu);
13400 if (sym != NULL)
13401 {
13402 struct attribute *member_loc;
13403
13404 common_block->contents[common_block->n_entries++] = sym;
13405
13406 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13407 cu);
13408 if (member_loc)
13409 {
13410 /* GDB has handled this for a long time, but it is
13411 not specified by DWARF. It seems to have been
13412 emitted by gfortran at least as recently as:
13413 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13414 complaint (&symfile_complaints,
13415 _("Variable in common block has "
13416 "DW_AT_data_member_location "
13417 "- DIE at 0x%x [in module %s]"),
13418 child_die->offset.sect_off,
13419 objfile_name (cu->objfile));
13420
13421 if (attr_form_is_section_offset (member_loc))
13422 dwarf2_complex_location_expr_complaint ();
13423 else if (attr_form_is_constant (member_loc)
13424 || attr_form_is_block (member_loc))
13425 {
13426 if (attr)
13427 mark_common_block_symbol_computed (sym, die, attr,
13428 member_loc, cu);
13429 }
13430 else
13431 dwarf2_complex_location_expr_complaint ();
13432 }
13433 }
13434 }
13435
13436 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
13437 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
13438 }
13439 }
13440
13441 /* Create a type for a C++ namespace. */
13442
13443 static struct type *
13444 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
13445 {
13446 struct objfile *objfile = cu->objfile;
13447 const char *previous_prefix, *name;
13448 int is_anonymous;
13449 struct type *type;
13450
13451 /* For extensions, reuse the type of the original namespace. */
13452 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
13453 {
13454 struct die_info *ext_die;
13455 struct dwarf2_cu *ext_cu = cu;
13456
13457 ext_die = dwarf2_extension (die, &ext_cu);
13458 type = read_type_die (ext_die, ext_cu);
13459
13460 /* EXT_CU may not be the same as CU.
13461 Ensure TYPE is recorded with CU in die_type_hash. */
13462 return set_die_type (die, type, cu);
13463 }
13464
13465 name = namespace_name (die, &is_anonymous, cu);
13466
13467 /* Now build the name of the current namespace. */
13468
13469 previous_prefix = determine_prefix (die, cu);
13470 if (previous_prefix[0] != '\0')
13471 name = typename_concat (&objfile->objfile_obstack,
13472 previous_prefix, name, 0, cu);
13473
13474 /* Create the type. */
13475 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
13476 objfile);
13477 TYPE_NAME (type) = name;
13478 TYPE_TAG_NAME (type) = TYPE_NAME (type);
13479
13480 return set_die_type (die, type, cu);
13481 }
13482
13483 /* Read a C++ namespace. */
13484
13485 static void
13486 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
13487 {
13488 struct objfile *objfile = cu->objfile;
13489 int is_anonymous;
13490
13491 /* Add a symbol associated to this if we haven't seen the namespace
13492 before. Also, add a using directive if it's an anonymous
13493 namespace. */
13494
13495 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
13496 {
13497 struct type *type;
13498
13499 type = read_type_die (die, cu);
13500 new_symbol (die, type, cu);
13501
13502 namespace_name (die, &is_anonymous, cu);
13503 if (is_anonymous)
13504 {
13505 const char *previous_prefix = determine_prefix (die, cu);
13506
13507 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13508 NULL, NULL, 0, &objfile->objfile_obstack);
13509 }
13510 }
13511
13512 if (die->child != NULL)
13513 {
13514 struct die_info *child_die = die->child;
13515
13516 while (child_die && child_die->tag)
13517 {
13518 process_die (child_die, cu);
13519 child_die = sibling_die (child_die);
13520 }
13521 }
13522 }
13523
13524 /* Read a Fortran module as type. This DIE can be only a declaration used for
13525 imported module. Still we need that type as local Fortran "use ... only"
13526 declaration imports depend on the created type in determine_prefix. */
13527
13528 static struct type *
13529 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
13530 {
13531 struct objfile *objfile = cu->objfile;
13532 const char *module_name;
13533 struct type *type;
13534
13535 module_name = dwarf2_name (die, cu);
13536 if (!module_name)
13537 complaint (&symfile_complaints,
13538 _("DW_TAG_module has no name, offset 0x%x"),
13539 die->offset.sect_off);
13540 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
13541
13542 /* determine_prefix uses TYPE_TAG_NAME. */
13543 TYPE_TAG_NAME (type) = TYPE_NAME (type);
13544
13545 return set_die_type (die, type, cu);
13546 }
13547
13548 /* Read a Fortran module. */
13549
13550 static void
13551 read_module (struct die_info *die, struct dwarf2_cu *cu)
13552 {
13553 struct die_info *child_die = die->child;
13554
13555 while (child_die && child_die->tag)
13556 {
13557 process_die (child_die, cu);
13558 child_die = sibling_die (child_die);
13559 }
13560 }
13561
13562 /* Return the name of the namespace represented by DIE. Set
13563 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
13564 namespace. */
13565
13566 static const char *
13567 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
13568 {
13569 struct die_info *current_die;
13570 const char *name = NULL;
13571
13572 /* Loop through the extensions until we find a name. */
13573
13574 for (current_die = die;
13575 current_die != NULL;
13576 current_die = dwarf2_extension (die, &cu))
13577 {
13578 name = dwarf2_name (current_die, cu);
13579 if (name != NULL)
13580 break;
13581 }
13582
13583 /* Is it an anonymous namespace? */
13584
13585 *is_anonymous = (name == NULL);
13586 if (*is_anonymous)
13587 name = CP_ANONYMOUS_NAMESPACE_STR;
13588
13589 return name;
13590 }
13591
13592 /* Extract all information from a DW_TAG_pointer_type DIE and add to
13593 the user defined type vector. */
13594
13595 static struct type *
13596 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
13597 {
13598 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
13599 struct comp_unit_head *cu_header = &cu->header;
13600 struct type *type;
13601 struct attribute *attr_byte_size;
13602 struct attribute *attr_address_class;
13603 int byte_size, addr_class;
13604 struct type *target_type;
13605
13606 target_type = die_type (die, cu);
13607
13608 /* The die_type call above may have already set the type for this DIE. */
13609 type = get_die_type (die, cu);
13610 if (type)
13611 return type;
13612
13613 type = lookup_pointer_type (target_type);
13614
13615 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
13616 if (attr_byte_size)
13617 byte_size = DW_UNSND (attr_byte_size);
13618 else
13619 byte_size = cu_header->addr_size;
13620
13621 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
13622 if (attr_address_class)
13623 addr_class = DW_UNSND (attr_address_class);
13624 else
13625 addr_class = DW_ADDR_none;
13626
13627 /* If the pointer size or address class is different than the
13628 default, create a type variant marked as such and set the
13629 length accordingly. */
13630 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
13631 {
13632 if (gdbarch_address_class_type_flags_p (gdbarch))
13633 {
13634 int type_flags;
13635
13636 type_flags = gdbarch_address_class_type_flags
13637 (gdbarch, byte_size, addr_class);
13638 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
13639 == 0);
13640 type = make_type_with_address_space (type, type_flags);
13641 }
13642 else if (TYPE_LENGTH (type) != byte_size)
13643 {
13644 complaint (&symfile_complaints,
13645 _("invalid pointer size %d"), byte_size);
13646 }
13647 else
13648 {
13649 /* Should we also complain about unhandled address classes? */
13650 }
13651 }
13652
13653 TYPE_LENGTH (type) = byte_size;
13654 return set_die_type (die, type, cu);
13655 }
13656
13657 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
13658 the user defined type vector. */
13659
13660 static struct type *
13661 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
13662 {
13663 struct type *type;
13664 struct type *to_type;
13665 struct type *domain;
13666
13667 to_type = die_type (die, cu);
13668 domain = die_containing_type (die, cu);
13669
13670 /* The calls above may have already set the type for this DIE. */
13671 type = get_die_type (die, cu);
13672 if (type)
13673 return type;
13674
13675 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
13676 type = lookup_methodptr_type (to_type);
13677 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
13678 {
13679 struct type *new_type = alloc_type (cu->objfile);
13680
13681 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
13682 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
13683 TYPE_VARARGS (to_type));
13684 type = lookup_methodptr_type (new_type);
13685 }
13686 else
13687 type = lookup_memberptr_type (to_type, domain);
13688
13689 return set_die_type (die, type, cu);
13690 }
13691
13692 /* Extract all information from a DW_TAG_reference_type DIE and add to
13693 the user defined type vector. */
13694
13695 static struct type *
13696 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
13697 {
13698 struct comp_unit_head *cu_header = &cu->header;
13699 struct type *type, *target_type;
13700 struct attribute *attr;
13701
13702 target_type = die_type (die, cu);
13703
13704 /* The die_type call above may have already set the type for this DIE. */
13705 type = get_die_type (die, cu);
13706 if (type)
13707 return type;
13708
13709 type = lookup_reference_type (target_type);
13710 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13711 if (attr)
13712 {
13713 TYPE_LENGTH (type) = DW_UNSND (attr);
13714 }
13715 else
13716 {
13717 TYPE_LENGTH (type) = cu_header->addr_size;
13718 }
13719 return set_die_type (die, type, cu);
13720 }
13721
13722 static struct type *
13723 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
13724 {
13725 struct type *base_type, *cv_type;
13726
13727 base_type = die_type (die, cu);
13728
13729 /* The die_type call above may have already set the type for this DIE. */
13730 cv_type = get_die_type (die, cu);
13731 if (cv_type)
13732 return cv_type;
13733
13734 /* In case the const qualifier is applied to an array type, the element type
13735 is so qualified, not the array type (section 6.7.3 of C99). */
13736 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
13737 {
13738 struct type *el_type, *inner_array;
13739
13740 base_type = copy_type (base_type);
13741 inner_array = base_type;
13742
13743 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13744 {
13745 TYPE_TARGET_TYPE (inner_array) =
13746 copy_type (TYPE_TARGET_TYPE (inner_array));
13747 inner_array = TYPE_TARGET_TYPE (inner_array);
13748 }
13749
13750 el_type = TYPE_TARGET_TYPE (inner_array);
13751 TYPE_TARGET_TYPE (inner_array) =
13752 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13753
13754 return set_die_type (die, base_type, cu);
13755 }
13756
13757 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13758 return set_die_type (die, cv_type, cu);
13759 }
13760
13761 static struct type *
13762 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
13763 {
13764 struct type *base_type, *cv_type;
13765
13766 base_type = die_type (die, cu);
13767
13768 /* The die_type call above may have already set the type for this DIE. */
13769 cv_type = get_die_type (die, cu);
13770 if (cv_type)
13771 return cv_type;
13772
13773 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13774 return set_die_type (die, cv_type, cu);
13775 }
13776
13777 /* Handle DW_TAG_restrict_type. */
13778
13779 static struct type *
13780 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13781 {
13782 struct type *base_type, *cv_type;
13783
13784 base_type = die_type (die, cu);
13785
13786 /* The die_type call above may have already set the type for this DIE. */
13787 cv_type = get_die_type (die, cu);
13788 if (cv_type)
13789 return cv_type;
13790
13791 cv_type = make_restrict_type (base_type);
13792 return set_die_type (die, cv_type, cu);
13793 }
13794
13795 /* Extract all information from a DW_TAG_string_type DIE and add to
13796 the user defined type vector. It isn't really a user defined type,
13797 but it behaves like one, with other DIE's using an AT_user_def_type
13798 attribute to reference it. */
13799
13800 static struct type *
13801 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
13802 {
13803 struct objfile *objfile = cu->objfile;
13804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13805 struct type *type, *range_type, *index_type, *char_type;
13806 struct attribute *attr;
13807 unsigned int length;
13808
13809 attr = dwarf2_attr (die, DW_AT_string_length, cu);
13810 if (attr)
13811 {
13812 length = DW_UNSND (attr);
13813 }
13814 else
13815 {
13816 /* Check for the DW_AT_byte_size attribute. */
13817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13818 if (attr)
13819 {
13820 length = DW_UNSND (attr);
13821 }
13822 else
13823 {
13824 length = 1;
13825 }
13826 }
13827
13828 index_type = objfile_type (objfile)->builtin_int;
13829 range_type = create_range_type (NULL, index_type, 1, length);
13830 char_type = language_string_char_type (cu->language_defn, gdbarch);
13831 type = create_string_type (NULL, char_type, range_type);
13832
13833 return set_die_type (die, type, cu);
13834 }
13835
13836 /* Assuming that DIE corresponds to a function, returns nonzero
13837 if the function is prototyped. */
13838
13839 static int
13840 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13841 {
13842 struct attribute *attr;
13843
13844 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13845 if (attr && (DW_UNSND (attr) != 0))
13846 return 1;
13847
13848 /* The DWARF standard implies that the DW_AT_prototyped attribute
13849 is only meaninful for C, but the concept also extends to other
13850 languages that allow unprototyped functions (Eg: Objective C).
13851 For all other languages, assume that functions are always
13852 prototyped. */
13853 if (cu->language != language_c
13854 && cu->language != language_objc
13855 && cu->language != language_opencl)
13856 return 1;
13857
13858 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13859 prototyped and unprototyped functions; default to prototyped,
13860 since that is more common in modern code (and RealView warns
13861 about unprototyped functions). */
13862 if (producer_is_realview (cu->producer))
13863 return 1;
13864
13865 return 0;
13866 }
13867
13868 /* Handle DIES due to C code like:
13869
13870 struct foo
13871 {
13872 int (*funcp)(int a, long l);
13873 int b;
13874 };
13875
13876 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13877
13878 static struct type *
13879 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13880 {
13881 struct objfile *objfile = cu->objfile;
13882 struct type *type; /* Type that this function returns. */
13883 struct type *ftype; /* Function that returns above type. */
13884 struct attribute *attr;
13885
13886 type = die_type (die, cu);
13887
13888 /* The die_type call above may have already set the type for this DIE. */
13889 ftype = get_die_type (die, cu);
13890 if (ftype)
13891 return ftype;
13892
13893 ftype = lookup_function_type (type);
13894
13895 if (prototyped_function_p (die, cu))
13896 TYPE_PROTOTYPED (ftype) = 1;
13897
13898 /* Store the calling convention in the type if it's available in
13899 the subroutine die. Otherwise set the calling convention to
13900 the default value DW_CC_normal. */
13901 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13902 if (attr)
13903 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13904 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13905 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13906 else
13907 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13908
13909 /* We need to add the subroutine type to the die immediately so
13910 we don't infinitely recurse when dealing with parameters
13911 declared as the same subroutine type. */
13912 set_die_type (die, ftype, cu);
13913
13914 if (die->child != NULL)
13915 {
13916 struct type *void_type = objfile_type (objfile)->builtin_void;
13917 struct die_info *child_die;
13918 int nparams, iparams;
13919
13920 /* Count the number of parameters.
13921 FIXME: GDB currently ignores vararg functions, but knows about
13922 vararg member functions. */
13923 nparams = 0;
13924 child_die = die->child;
13925 while (child_die && child_die->tag)
13926 {
13927 if (child_die->tag == DW_TAG_formal_parameter)
13928 nparams++;
13929 else if (child_die->tag == DW_TAG_unspecified_parameters)
13930 TYPE_VARARGS (ftype) = 1;
13931 child_die = sibling_die (child_die);
13932 }
13933
13934 /* Allocate storage for parameters and fill them in. */
13935 TYPE_NFIELDS (ftype) = nparams;
13936 TYPE_FIELDS (ftype) = (struct field *)
13937 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13938
13939 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13940 even if we error out during the parameters reading below. */
13941 for (iparams = 0; iparams < nparams; iparams++)
13942 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13943
13944 iparams = 0;
13945 child_die = die->child;
13946 while (child_die && child_die->tag)
13947 {
13948 if (child_die->tag == DW_TAG_formal_parameter)
13949 {
13950 struct type *arg_type;
13951
13952 /* DWARF version 2 has no clean way to discern C++
13953 static and non-static member functions. G++ helps
13954 GDB by marking the first parameter for non-static
13955 member functions (which is the this pointer) as
13956 artificial. We pass this information to
13957 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13958
13959 DWARF version 3 added DW_AT_object_pointer, which GCC
13960 4.5 does not yet generate. */
13961 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13962 if (attr)
13963 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13964 else
13965 {
13966 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13967
13968 /* GCC/43521: In java, the formal parameter
13969 "this" is sometimes not marked with DW_AT_artificial. */
13970 if (cu->language == language_java)
13971 {
13972 const char *name = dwarf2_name (child_die, cu);
13973
13974 if (name && !strcmp (name, "this"))
13975 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13976 }
13977 }
13978 arg_type = die_type (child_die, cu);
13979
13980 /* RealView does not mark THIS as const, which the testsuite
13981 expects. GCC marks THIS as const in method definitions,
13982 but not in the class specifications (GCC PR 43053). */
13983 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13984 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13985 {
13986 int is_this = 0;
13987 struct dwarf2_cu *arg_cu = cu;
13988 const char *name = dwarf2_name (child_die, cu);
13989
13990 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13991 if (attr)
13992 {
13993 /* If the compiler emits this, use it. */
13994 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13995 is_this = 1;
13996 }
13997 else if (name && strcmp (name, "this") == 0)
13998 /* Function definitions will have the argument names. */
13999 is_this = 1;
14000 else if (name == NULL && iparams == 0)
14001 /* Declarations may not have the names, so like
14002 elsewhere in GDB, assume an artificial first
14003 argument is "this". */
14004 is_this = 1;
14005
14006 if (is_this)
14007 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14008 arg_type, 0);
14009 }
14010
14011 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14012 iparams++;
14013 }
14014 child_die = sibling_die (child_die);
14015 }
14016 }
14017
14018 return ftype;
14019 }
14020
14021 static struct type *
14022 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14023 {
14024 struct objfile *objfile = cu->objfile;
14025 const char *name = NULL;
14026 struct type *this_type, *target_type;
14027
14028 name = dwarf2_full_name (NULL, die, cu);
14029 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
14030 TYPE_FLAG_TARGET_STUB, NULL, objfile);
14031 TYPE_NAME (this_type) = name;
14032 set_die_type (die, this_type, cu);
14033 target_type = die_type (die, cu);
14034 if (target_type != this_type)
14035 TYPE_TARGET_TYPE (this_type) = target_type;
14036 else
14037 {
14038 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14039 spec and cause infinite loops in GDB. */
14040 complaint (&symfile_complaints,
14041 _("Self-referential DW_TAG_typedef "
14042 "- DIE at 0x%x [in module %s]"),
14043 die->offset.sect_off, objfile_name (objfile));
14044 TYPE_TARGET_TYPE (this_type) = NULL;
14045 }
14046 return this_type;
14047 }
14048
14049 /* Find a representation of a given base type and install
14050 it in the TYPE field of the die. */
14051
14052 static struct type *
14053 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14054 {
14055 struct objfile *objfile = cu->objfile;
14056 struct type *type;
14057 struct attribute *attr;
14058 int encoding = 0, size = 0;
14059 const char *name;
14060 enum type_code code = TYPE_CODE_INT;
14061 int type_flags = 0;
14062 struct type *target_type = NULL;
14063
14064 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14065 if (attr)
14066 {
14067 encoding = DW_UNSND (attr);
14068 }
14069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14070 if (attr)
14071 {
14072 size = DW_UNSND (attr);
14073 }
14074 name = dwarf2_name (die, cu);
14075 if (!name)
14076 {
14077 complaint (&symfile_complaints,
14078 _("DW_AT_name missing from DW_TAG_base_type"));
14079 }
14080
14081 switch (encoding)
14082 {
14083 case DW_ATE_address:
14084 /* Turn DW_ATE_address into a void * pointer. */
14085 code = TYPE_CODE_PTR;
14086 type_flags |= TYPE_FLAG_UNSIGNED;
14087 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
14088 break;
14089 case DW_ATE_boolean:
14090 code = TYPE_CODE_BOOL;
14091 type_flags |= TYPE_FLAG_UNSIGNED;
14092 break;
14093 case DW_ATE_complex_float:
14094 code = TYPE_CODE_COMPLEX;
14095 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
14096 break;
14097 case DW_ATE_decimal_float:
14098 code = TYPE_CODE_DECFLOAT;
14099 break;
14100 case DW_ATE_float:
14101 code = TYPE_CODE_FLT;
14102 break;
14103 case DW_ATE_signed:
14104 break;
14105 case DW_ATE_unsigned:
14106 type_flags |= TYPE_FLAG_UNSIGNED;
14107 if (cu->language == language_fortran
14108 && name
14109 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
14110 code = TYPE_CODE_CHAR;
14111 break;
14112 case DW_ATE_signed_char:
14113 if (cu->language == language_ada || cu->language == language_m2
14114 || cu->language == language_pascal
14115 || cu->language == language_fortran)
14116 code = TYPE_CODE_CHAR;
14117 break;
14118 case DW_ATE_unsigned_char:
14119 if (cu->language == language_ada || cu->language == language_m2
14120 || cu->language == language_pascal
14121 || cu->language == language_fortran)
14122 code = TYPE_CODE_CHAR;
14123 type_flags |= TYPE_FLAG_UNSIGNED;
14124 break;
14125 case DW_ATE_UTF:
14126 /* We just treat this as an integer and then recognize the
14127 type by name elsewhere. */
14128 break;
14129
14130 default:
14131 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14132 dwarf_type_encoding_name (encoding));
14133 break;
14134 }
14135
14136 type = init_type (code, size, type_flags, NULL, objfile);
14137 TYPE_NAME (type) = name;
14138 TYPE_TARGET_TYPE (type) = target_type;
14139
14140 if (name && strcmp (name, "char") == 0)
14141 TYPE_NOSIGN (type) = 1;
14142
14143 return set_die_type (die, type, cu);
14144 }
14145
14146 /* Read the given DW_AT_subrange DIE. */
14147
14148 static struct type *
14149 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14150 {
14151 struct type *base_type, *orig_base_type;
14152 struct type *range_type;
14153 struct attribute *attr;
14154 LONGEST low, high;
14155 int low_default_is_valid;
14156 const char *name;
14157 LONGEST negative_mask;
14158
14159 orig_base_type = die_type (die, cu);
14160 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14161 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14162 creating the range type, but we use the result of check_typedef
14163 when examining properties of the type. */
14164 base_type = check_typedef (orig_base_type);
14165
14166 /* The die_type call above may have already set the type for this DIE. */
14167 range_type = get_die_type (die, cu);
14168 if (range_type)
14169 return range_type;
14170
14171 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14172 omitting DW_AT_lower_bound. */
14173 switch (cu->language)
14174 {
14175 case language_c:
14176 case language_cplus:
14177 low = 0;
14178 low_default_is_valid = 1;
14179 break;
14180 case language_fortran:
14181 low = 1;
14182 low_default_is_valid = 1;
14183 break;
14184 case language_d:
14185 case language_java:
14186 case language_objc:
14187 low = 0;
14188 low_default_is_valid = (cu->header.version >= 4);
14189 break;
14190 case language_ada:
14191 case language_m2:
14192 case language_pascal:
14193 low = 1;
14194 low_default_is_valid = (cu->header.version >= 4);
14195 break;
14196 default:
14197 low = 0;
14198 low_default_is_valid = 0;
14199 break;
14200 }
14201
14202 /* FIXME: For variable sized arrays either of these could be
14203 a variable rather than a constant value. We'll allow it,
14204 but we don't know how to handle it. */
14205 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
14206 if (attr)
14207 low = dwarf2_get_attr_constant_value (attr, low);
14208 else if (!low_default_is_valid)
14209 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14210 "- DIE at 0x%x [in module %s]"),
14211 die->offset.sect_off, objfile_name (cu->objfile));
14212
14213 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
14214 if (attr)
14215 {
14216 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
14217 {
14218 /* GCC encodes arrays with unspecified or dynamic length
14219 with a DW_FORM_block1 attribute or a reference attribute.
14220 FIXME: GDB does not yet know how to handle dynamic
14221 arrays properly, treat them as arrays with unspecified
14222 length for now.
14223
14224 FIXME: jimb/2003-09-22: GDB does not really know
14225 how to handle arrays of unspecified length
14226 either; we just represent them as zero-length
14227 arrays. Choose an appropriate upper bound given
14228 the lower bound we've computed above. */
14229 high = low - 1;
14230 }
14231 else
14232 high = dwarf2_get_attr_constant_value (attr, 1);
14233 }
14234 else
14235 {
14236 attr = dwarf2_attr (die, DW_AT_count, cu);
14237 if (attr)
14238 {
14239 int count = dwarf2_get_attr_constant_value (attr, 1);
14240 high = low + count - 1;
14241 }
14242 else
14243 {
14244 /* Unspecified array length. */
14245 high = low - 1;
14246 }
14247 }
14248
14249 /* Dwarf-2 specifications explicitly allows to create subrange types
14250 without specifying a base type.
14251 In that case, the base type must be set to the type of
14252 the lower bound, upper bound or count, in that order, if any of these
14253 three attributes references an object that has a type.
14254 If no base type is found, the Dwarf-2 specifications say that
14255 a signed integer type of size equal to the size of an address should
14256 be used.
14257 For the following C code: `extern char gdb_int [];'
14258 GCC produces an empty range DIE.
14259 FIXME: muller/2010-05-28: Possible references to object for low bound,
14260 high bound or count are not yet handled by this code. */
14261 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
14262 {
14263 struct objfile *objfile = cu->objfile;
14264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14265 int addr_size = gdbarch_addr_bit (gdbarch) /8;
14266 struct type *int_type = objfile_type (objfile)->builtin_int;
14267
14268 /* Test "int", "long int", and "long long int" objfile types,
14269 and select the first one having a size above or equal to the
14270 architecture address size. */
14271 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14272 base_type = int_type;
14273 else
14274 {
14275 int_type = objfile_type (objfile)->builtin_long;
14276 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14277 base_type = int_type;
14278 else
14279 {
14280 int_type = objfile_type (objfile)->builtin_long_long;
14281 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14282 base_type = int_type;
14283 }
14284 }
14285 }
14286
14287 negative_mask =
14288 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
14289 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
14290 low |= negative_mask;
14291 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
14292 high |= negative_mask;
14293
14294 range_type = create_range_type (NULL, orig_base_type, low, high);
14295
14296 /* Mark arrays with dynamic length at least as an array of unspecified
14297 length. GDB could check the boundary but before it gets implemented at
14298 least allow accessing the array elements. */
14299 if (attr && attr_form_is_block (attr))
14300 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14301
14302 /* Ada expects an empty array on no boundary attributes. */
14303 if (attr == NULL && cu->language != language_ada)
14304 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14305
14306 name = dwarf2_name (die, cu);
14307 if (name)
14308 TYPE_NAME (range_type) = name;
14309
14310 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14311 if (attr)
14312 TYPE_LENGTH (range_type) = DW_UNSND (attr);
14313
14314 set_die_type (die, range_type, cu);
14315
14316 /* set_die_type should be already done. */
14317 set_descriptive_type (range_type, die, cu);
14318
14319 return range_type;
14320 }
14321
14322 static struct type *
14323 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
14324 {
14325 struct type *type;
14326
14327 /* For now, we only support the C meaning of an unspecified type: void. */
14328
14329 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
14330 TYPE_NAME (type) = dwarf2_name (die, cu);
14331
14332 return set_die_type (die, type, cu);
14333 }
14334
14335 /* Read a single die and all its descendents. Set the die's sibling
14336 field to NULL; set other fields in the die correctly, and set all
14337 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
14338 location of the info_ptr after reading all of those dies. PARENT
14339 is the parent of the die in question. */
14340
14341 static struct die_info *
14342 read_die_and_children (const struct die_reader_specs *reader,
14343 const gdb_byte *info_ptr,
14344 const gdb_byte **new_info_ptr,
14345 struct die_info *parent)
14346 {
14347 struct die_info *die;
14348 const gdb_byte *cur_ptr;
14349 int has_children;
14350
14351 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
14352 if (die == NULL)
14353 {
14354 *new_info_ptr = cur_ptr;
14355 return NULL;
14356 }
14357 store_in_ref_table (die, reader->cu);
14358
14359 if (has_children)
14360 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
14361 else
14362 {
14363 die->child = NULL;
14364 *new_info_ptr = cur_ptr;
14365 }
14366
14367 die->sibling = NULL;
14368 die->parent = parent;
14369 return die;
14370 }
14371
14372 /* Read a die, all of its descendents, and all of its siblings; set
14373 all of the fields of all of the dies correctly. Arguments are as
14374 in read_die_and_children. */
14375
14376 static struct die_info *
14377 read_die_and_siblings_1 (const struct die_reader_specs *reader,
14378 const gdb_byte *info_ptr,
14379 const gdb_byte **new_info_ptr,
14380 struct die_info *parent)
14381 {
14382 struct die_info *first_die, *last_sibling;
14383 const gdb_byte *cur_ptr;
14384
14385 cur_ptr = info_ptr;
14386 first_die = last_sibling = NULL;
14387
14388 while (1)
14389 {
14390 struct die_info *die
14391 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
14392
14393 if (die == NULL)
14394 {
14395 *new_info_ptr = cur_ptr;
14396 return first_die;
14397 }
14398
14399 if (!first_die)
14400 first_die = die;
14401 else
14402 last_sibling->sibling = die;
14403
14404 last_sibling = die;
14405 }
14406 }
14407
14408 /* Read a die, all of its descendents, and all of its siblings; set
14409 all of the fields of all of the dies correctly. Arguments are as
14410 in read_die_and_children.
14411 This the main entry point for reading a DIE and all its children. */
14412
14413 static struct die_info *
14414 read_die_and_siblings (const struct die_reader_specs *reader,
14415 const gdb_byte *info_ptr,
14416 const gdb_byte **new_info_ptr,
14417 struct die_info *parent)
14418 {
14419 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
14420 new_info_ptr, parent);
14421
14422 if (dwarf2_die_debug)
14423 {
14424 fprintf_unfiltered (gdb_stdlog,
14425 "Read die from %s@0x%x of %s:\n",
14426 get_section_name (reader->die_section),
14427 (unsigned) (info_ptr - reader->die_section->buffer),
14428 bfd_get_filename (reader->abfd));
14429 dump_die (die, dwarf2_die_debug);
14430 }
14431
14432 return die;
14433 }
14434
14435 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
14436 attributes.
14437 The caller is responsible for filling in the extra attributes
14438 and updating (*DIEP)->num_attrs.
14439 Set DIEP to point to a newly allocated die with its information,
14440 except for its child, sibling, and parent fields.
14441 Set HAS_CHILDREN to tell whether the die has children or not. */
14442
14443 static const gdb_byte *
14444 read_full_die_1 (const struct die_reader_specs *reader,
14445 struct die_info **diep, const gdb_byte *info_ptr,
14446 int *has_children, int num_extra_attrs)
14447 {
14448 unsigned int abbrev_number, bytes_read, i;
14449 sect_offset offset;
14450 struct abbrev_info *abbrev;
14451 struct die_info *die;
14452 struct dwarf2_cu *cu = reader->cu;
14453 bfd *abfd = reader->abfd;
14454
14455 offset.sect_off = info_ptr - reader->buffer;
14456 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14457 info_ptr += bytes_read;
14458 if (!abbrev_number)
14459 {
14460 *diep = NULL;
14461 *has_children = 0;
14462 return info_ptr;
14463 }
14464
14465 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
14466 if (!abbrev)
14467 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
14468 abbrev_number,
14469 bfd_get_filename (abfd));
14470
14471 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
14472 die->offset = offset;
14473 die->tag = abbrev->tag;
14474 die->abbrev = abbrev_number;
14475
14476 /* Make the result usable.
14477 The caller needs to update num_attrs after adding the extra
14478 attributes. */
14479 die->num_attrs = abbrev->num_attrs;
14480
14481 for (i = 0; i < abbrev->num_attrs; ++i)
14482 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
14483 info_ptr);
14484
14485 *diep = die;
14486 *has_children = abbrev->has_children;
14487 return info_ptr;
14488 }
14489
14490 /* Read a die and all its attributes.
14491 Set DIEP to point to a newly allocated die with its information,
14492 except for its child, sibling, and parent fields.
14493 Set HAS_CHILDREN to tell whether the die has children or not. */
14494
14495 static const gdb_byte *
14496 read_full_die (const struct die_reader_specs *reader,
14497 struct die_info **diep, const gdb_byte *info_ptr,
14498 int *has_children)
14499 {
14500 const gdb_byte *result;
14501
14502 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
14503
14504 if (dwarf2_die_debug)
14505 {
14506 fprintf_unfiltered (gdb_stdlog,
14507 "Read die from %s@0x%x of %s:\n",
14508 get_section_name (reader->die_section),
14509 (unsigned) (info_ptr - reader->die_section->buffer),
14510 bfd_get_filename (reader->abfd));
14511 dump_die (*diep, dwarf2_die_debug);
14512 }
14513
14514 return result;
14515 }
14516 \f
14517 /* Abbreviation tables.
14518
14519 In DWARF version 2, the description of the debugging information is
14520 stored in a separate .debug_abbrev section. Before we read any
14521 dies from a section we read in all abbreviations and install them
14522 in a hash table. */
14523
14524 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
14525
14526 static struct abbrev_info *
14527 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
14528 {
14529 struct abbrev_info *abbrev;
14530
14531 abbrev = (struct abbrev_info *)
14532 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
14533 memset (abbrev, 0, sizeof (struct abbrev_info));
14534 return abbrev;
14535 }
14536
14537 /* Add an abbreviation to the table. */
14538
14539 static void
14540 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
14541 unsigned int abbrev_number,
14542 struct abbrev_info *abbrev)
14543 {
14544 unsigned int hash_number;
14545
14546 hash_number = abbrev_number % ABBREV_HASH_SIZE;
14547 abbrev->next = abbrev_table->abbrevs[hash_number];
14548 abbrev_table->abbrevs[hash_number] = abbrev;
14549 }
14550
14551 /* Look up an abbrev in the table.
14552 Returns NULL if the abbrev is not found. */
14553
14554 static struct abbrev_info *
14555 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
14556 unsigned int abbrev_number)
14557 {
14558 unsigned int hash_number;
14559 struct abbrev_info *abbrev;
14560
14561 hash_number = abbrev_number % ABBREV_HASH_SIZE;
14562 abbrev = abbrev_table->abbrevs[hash_number];
14563
14564 while (abbrev)
14565 {
14566 if (abbrev->number == abbrev_number)
14567 return abbrev;
14568 abbrev = abbrev->next;
14569 }
14570 return NULL;
14571 }
14572
14573 /* Read in an abbrev table. */
14574
14575 static struct abbrev_table *
14576 abbrev_table_read_table (struct dwarf2_section_info *section,
14577 sect_offset offset)
14578 {
14579 struct objfile *objfile = dwarf2_per_objfile->objfile;
14580 bfd *abfd = get_section_bfd_owner (section);
14581 struct abbrev_table *abbrev_table;
14582 const gdb_byte *abbrev_ptr;
14583 struct abbrev_info *cur_abbrev;
14584 unsigned int abbrev_number, bytes_read, abbrev_name;
14585 unsigned int abbrev_form;
14586 struct attr_abbrev *cur_attrs;
14587 unsigned int allocated_attrs;
14588
14589 abbrev_table = XMALLOC (struct abbrev_table);
14590 abbrev_table->offset = offset;
14591 obstack_init (&abbrev_table->abbrev_obstack);
14592 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
14593 (ABBREV_HASH_SIZE
14594 * sizeof (struct abbrev_info *)));
14595 memset (abbrev_table->abbrevs, 0,
14596 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
14597
14598 dwarf2_read_section (objfile, section);
14599 abbrev_ptr = section->buffer + offset.sect_off;
14600 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14601 abbrev_ptr += bytes_read;
14602
14603 allocated_attrs = ATTR_ALLOC_CHUNK;
14604 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
14605
14606 /* Loop until we reach an abbrev number of 0. */
14607 while (abbrev_number)
14608 {
14609 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
14610
14611 /* read in abbrev header */
14612 cur_abbrev->number = abbrev_number;
14613 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14614 abbrev_ptr += bytes_read;
14615 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
14616 abbrev_ptr += 1;
14617
14618 /* now read in declarations */
14619 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14620 abbrev_ptr += bytes_read;
14621 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14622 abbrev_ptr += bytes_read;
14623 while (abbrev_name)
14624 {
14625 if (cur_abbrev->num_attrs == allocated_attrs)
14626 {
14627 allocated_attrs += ATTR_ALLOC_CHUNK;
14628 cur_attrs
14629 = xrealloc (cur_attrs, (allocated_attrs
14630 * sizeof (struct attr_abbrev)));
14631 }
14632
14633 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
14634 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
14635 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14636 abbrev_ptr += bytes_read;
14637 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14638 abbrev_ptr += bytes_read;
14639 }
14640
14641 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
14642 (cur_abbrev->num_attrs
14643 * sizeof (struct attr_abbrev)));
14644 memcpy (cur_abbrev->attrs, cur_attrs,
14645 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
14646
14647 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
14648
14649 /* Get next abbreviation.
14650 Under Irix6 the abbreviations for a compilation unit are not
14651 always properly terminated with an abbrev number of 0.
14652 Exit loop if we encounter an abbreviation which we have
14653 already read (which means we are about to read the abbreviations
14654 for the next compile unit) or if the end of the abbreviation
14655 table is reached. */
14656 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
14657 break;
14658 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14659 abbrev_ptr += bytes_read;
14660 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
14661 break;
14662 }
14663
14664 xfree (cur_attrs);
14665 return abbrev_table;
14666 }
14667
14668 /* Free the resources held by ABBREV_TABLE. */
14669
14670 static void
14671 abbrev_table_free (struct abbrev_table *abbrev_table)
14672 {
14673 obstack_free (&abbrev_table->abbrev_obstack, NULL);
14674 xfree (abbrev_table);
14675 }
14676
14677 /* Same as abbrev_table_free but as a cleanup.
14678 We pass in a pointer to the pointer to the table so that we can
14679 set the pointer to NULL when we're done. It also simplifies
14680 build_type_unit_groups. */
14681
14682 static void
14683 abbrev_table_free_cleanup (void *table_ptr)
14684 {
14685 struct abbrev_table **abbrev_table_ptr = table_ptr;
14686
14687 if (*abbrev_table_ptr != NULL)
14688 abbrev_table_free (*abbrev_table_ptr);
14689 *abbrev_table_ptr = NULL;
14690 }
14691
14692 /* Read the abbrev table for CU from ABBREV_SECTION. */
14693
14694 static void
14695 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
14696 struct dwarf2_section_info *abbrev_section)
14697 {
14698 cu->abbrev_table =
14699 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
14700 }
14701
14702 /* Release the memory used by the abbrev table for a compilation unit. */
14703
14704 static void
14705 dwarf2_free_abbrev_table (void *ptr_to_cu)
14706 {
14707 struct dwarf2_cu *cu = ptr_to_cu;
14708
14709 if (cu->abbrev_table != NULL)
14710 abbrev_table_free (cu->abbrev_table);
14711 /* Set this to NULL so that we SEGV if we try to read it later,
14712 and also because free_comp_unit verifies this is NULL. */
14713 cu->abbrev_table = NULL;
14714 }
14715 \f
14716 /* Returns nonzero if TAG represents a type that we might generate a partial
14717 symbol for. */
14718
14719 static int
14720 is_type_tag_for_partial (int tag)
14721 {
14722 switch (tag)
14723 {
14724 #if 0
14725 /* Some types that would be reasonable to generate partial symbols for,
14726 that we don't at present. */
14727 case DW_TAG_array_type:
14728 case DW_TAG_file_type:
14729 case DW_TAG_ptr_to_member_type:
14730 case DW_TAG_set_type:
14731 case DW_TAG_string_type:
14732 case DW_TAG_subroutine_type:
14733 #endif
14734 case DW_TAG_base_type:
14735 case DW_TAG_class_type:
14736 case DW_TAG_interface_type:
14737 case DW_TAG_enumeration_type:
14738 case DW_TAG_structure_type:
14739 case DW_TAG_subrange_type:
14740 case DW_TAG_typedef:
14741 case DW_TAG_union_type:
14742 return 1;
14743 default:
14744 return 0;
14745 }
14746 }
14747
14748 /* Load all DIEs that are interesting for partial symbols into memory. */
14749
14750 static struct partial_die_info *
14751 load_partial_dies (const struct die_reader_specs *reader,
14752 const gdb_byte *info_ptr, int building_psymtab)
14753 {
14754 struct dwarf2_cu *cu = reader->cu;
14755 struct objfile *objfile = cu->objfile;
14756 struct partial_die_info *part_die;
14757 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14758 struct abbrev_info *abbrev;
14759 unsigned int bytes_read;
14760 unsigned int load_all = 0;
14761 int nesting_level = 1;
14762
14763 parent_die = NULL;
14764 last_die = NULL;
14765
14766 gdb_assert (cu->per_cu != NULL);
14767 if (cu->per_cu->load_all_dies)
14768 load_all = 1;
14769
14770 cu->partial_dies
14771 = htab_create_alloc_ex (cu->header.length / 12,
14772 partial_die_hash,
14773 partial_die_eq,
14774 NULL,
14775 &cu->comp_unit_obstack,
14776 hashtab_obstack_allocate,
14777 dummy_obstack_deallocate);
14778
14779 part_die = obstack_alloc (&cu->comp_unit_obstack,
14780 sizeof (struct partial_die_info));
14781
14782 while (1)
14783 {
14784 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14785
14786 /* A NULL abbrev means the end of a series of children. */
14787 if (abbrev == NULL)
14788 {
14789 if (--nesting_level == 0)
14790 {
14791 /* PART_DIE was probably the last thing allocated on the
14792 comp_unit_obstack, so we could call obstack_free
14793 here. We don't do that because the waste is small,
14794 and will be cleaned up when we're done with this
14795 compilation unit. This way, we're also more robust
14796 against other users of the comp_unit_obstack. */
14797 return first_die;
14798 }
14799 info_ptr += bytes_read;
14800 last_die = parent_die;
14801 parent_die = parent_die->die_parent;
14802 continue;
14803 }
14804
14805 /* Check for template arguments. We never save these; if
14806 they're seen, we just mark the parent, and go on our way. */
14807 if (parent_die != NULL
14808 && cu->language == language_cplus
14809 && (abbrev->tag == DW_TAG_template_type_param
14810 || abbrev->tag == DW_TAG_template_value_param))
14811 {
14812 parent_die->has_template_arguments = 1;
14813
14814 if (!load_all)
14815 {
14816 /* We don't need a partial DIE for the template argument. */
14817 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14818 continue;
14819 }
14820 }
14821
14822 /* We only recurse into c++ subprograms looking for template arguments.
14823 Skip their other children. */
14824 if (!load_all
14825 && cu->language == language_cplus
14826 && parent_die != NULL
14827 && parent_die->tag == DW_TAG_subprogram)
14828 {
14829 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14830 continue;
14831 }
14832
14833 /* Check whether this DIE is interesting enough to save. Normally
14834 we would not be interested in members here, but there may be
14835 later variables referencing them via DW_AT_specification (for
14836 static members). */
14837 if (!load_all
14838 && !is_type_tag_for_partial (abbrev->tag)
14839 && abbrev->tag != DW_TAG_constant
14840 && abbrev->tag != DW_TAG_enumerator
14841 && abbrev->tag != DW_TAG_subprogram
14842 && abbrev->tag != DW_TAG_lexical_block
14843 && abbrev->tag != DW_TAG_variable
14844 && abbrev->tag != DW_TAG_namespace
14845 && abbrev->tag != DW_TAG_module
14846 && abbrev->tag != DW_TAG_member
14847 && abbrev->tag != DW_TAG_imported_unit)
14848 {
14849 /* Otherwise we skip to the next sibling, if any. */
14850 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14851 continue;
14852 }
14853
14854 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14855 info_ptr);
14856
14857 /* This two-pass algorithm for processing partial symbols has a
14858 high cost in cache pressure. Thus, handle some simple cases
14859 here which cover the majority of C partial symbols. DIEs
14860 which neither have specification tags in them, nor could have
14861 specification tags elsewhere pointing at them, can simply be
14862 processed and discarded.
14863
14864 This segment is also optional; scan_partial_symbols and
14865 add_partial_symbol will handle these DIEs if we chain
14866 them in normally. When compilers which do not emit large
14867 quantities of duplicate debug information are more common,
14868 this code can probably be removed. */
14869
14870 /* Any complete simple types at the top level (pretty much all
14871 of them, for a language without namespaces), can be processed
14872 directly. */
14873 if (parent_die == NULL
14874 && part_die->has_specification == 0
14875 && part_die->is_declaration == 0
14876 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14877 || part_die->tag == DW_TAG_base_type
14878 || part_die->tag == DW_TAG_subrange_type))
14879 {
14880 if (building_psymtab && part_die->name != NULL)
14881 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14882 VAR_DOMAIN, LOC_TYPEDEF,
14883 &objfile->static_psymbols,
14884 0, (CORE_ADDR) 0, cu->language, objfile);
14885 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14886 continue;
14887 }
14888
14889 /* The exception for DW_TAG_typedef with has_children above is
14890 a workaround of GCC PR debug/47510. In the case of this complaint
14891 type_name_no_tag_or_error will error on such types later.
14892
14893 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14894 it could not find the child DIEs referenced later, this is checked
14895 above. In correct DWARF DW_TAG_typedef should have no children. */
14896
14897 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14898 complaint (&symfile_complaints,
14899 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14900 "- DIE at 0x%x [in module %s]"),
14901 part_die->offset.sect_off, objfile_name (objfile));
14902
14903 /* If we're at the second level, and we're an enumerator, and
14904 our parent has no specification (meaning possibly lives in a
14905 namespace elsewhere), then we can add the partial symbol now
14906 instead of queueing it. */
14907 if (part_die->tag == DW_TAG_enumerator
14908 && parent_die != NULL
14909 && parent_die->die_parent == NULL
14910 && parent_die->tag == DW_TAG_enumeration_type
14911 && parent_die->has_specification == 0)
14912 {
14913 if (part_die->name == NULL)
14914 complaint (&symfile_complaints,
14915 _("malformed enumerator DIE ignored"));
14916 else if (building_psymtab)
14917 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14918 VAR_DOMAIN, LOC_CONST,
14919 (cu->language == language_cplus
14920 || cu->language == language_java)
14921 ? &objfile->global_psymbols
14922 : &objfile->static_psymbols,
14923 0, (CORE_ADDR) 0, cu->language, objfile);
14924
14925 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14926 continue;
14927 }
14928
14929 /* We'll save this DIE so link it in. */
14930 part_die->die_parent = parent_die;
14931 part_die->die_sibling = NULL;
14932 part_die->die_child = NULL;
14933
14934 if (last_die && last_die == parent_die)
14935 last_die->die_child = part_die;
14936 else if (last_die)
14937 last_die->die_sibling = part_die;
14938
14939 last_die = part_die;
14940
14941 if (first_die == NULL)
14942 first_die = part_die;
14943
14944 /* Maybe add the DIE to the hash table. Not all DIEs that we
14945 find interesting need to be in the hash table, because we
14946 also have the parent/sibling/child chains; only those that we
14947 might refer to by offset later during partial symbol reading.
14948
14949 For now this means things that might have be the target of a
14950 DW_AT_specification, DW_AT_abstract_origin, or
14951 DW_AT_extension. DW_AT_extension will refer only to
14952 namespaces; DW_AT_abstract_origin refers to functions (and
14953 many things under the function DIE, but we do not recurse
14954 into function DIEs during partial symbol reading) and
14955 possibly variables as well; DW_AT_specification refers to
14956 declarations. Declarations ought to have the DW_AT_declaration
14957 flag. It happens that GCC forgets to put it in sometimes, but
14958 only for functions, not for types.
14959
14960 Adding more things than necessary to the hash table is harmless
14961 except for the performance cost. Adding too few will result in
14962 wasted time in find_partial_die, when we reread the compilation
14963 unit with load_all_dies set. */
14964
14965 if (load_all
14966 || abbrev->tag == DW_TAG_constant
14967 || abbrev->tag == DW_TAG_subprogram
14968 || abbrev->tag == DW_TAG_variable
14969 || abbrev->tag == DW_TAG_namespace
14970 || part_die->is_declaration)
14971 {
14972 void **slot;
14973
14974 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14975 part_die->offset.sect_off, INSERT);
14976 *slot = part_die;
14977 }
14978
14979 part_die = obstack_alloc (&cu->comp_unit_obstack,
14980 sizeof (struct partial_die_info));
14981
14982 /* For some DIEs we want to follow their children (if any). For C
14983 we have no reason to follow the children of structures; for other
14984 languages we have to, so that we can get at method physnames
14985 to infer fully qualified class names, for DW_AT_specification,
14986 and for C++ template arguments. For C++, we also look one level
14987 inside functions to find template arguments (if the name of the
14988 function does not already contain the template arguments).
14989
14990 For Ada, we need to scan the children of subprograms and lexical
14991 blocks as well because Ada allows the definition of nested
14992 entities that could be interesting for the debugger, such as
14993 nested subprograms for instance. */
14994 if (last_die->has_children
14995 && (load_all
14996 || last_die->tag == DW_TAG_namespace
14997 || last_die->tag == DW_TAG_module
14998 || last_die->tag == DW_TAG_enumeration_type
14999 || (cu->language == language_cplus
15000 && last_die->tag == DW_TAG_subprogram
15001 && (last_die->name == NULL
15002 || strchr (last_die->name, '<') == NULL))
15003 || (cu->language != language_c
15004 && (last_die->tag == DW_TAG_class_type
15005 || last_die->tag == DW_TAG_interface_type
15006 || last_die->tag == DW_TAG_structure_type
15007 || last_die->tag == DW_TAG_union_type))
15008 || (cu->language == language_ada
15009 && (last_die->tag == DW_TAG_subprogram
15010 || last_die->tag == DW_TAG_lexical_block))))
15011 {
15012 nesting_level++;
15013 parent_die = last_die;
15014 continue;
15015 }
15016
15017 /* Otherwise we skip to the next sibling, if any. */
15018 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
15019
15020 /* Back to the top, do it again. */
15021 }
15022 }
15023
15024 /* Read a minimal amount of information into the minimal die structure. */
15025
15026 static const gdb_byte *
15027 read_partial_die (const struct die_reader_specs *reader,
15028 struct partial_die_info *part_die,
15029 struct abbrev_info *abbrev, unsigned int abbrev_len,
15030 const gdb_byte *info_ptr)
15031 {
15032 struct dwarf2_cu *cu = reader->cu;
15033 struct objfile *objfile = cu->objfile;
15034 const gdb_byte *buffer = reader->buffer;
15035 unsigned int i;
15036 struct attribute attr;
15037 int has_low_pc_attr = 0;
15038 int has_high_pc_attr = 0;
15039 int high_pc_relative = 0;
15040
15041 memset (part_die, 0, sizeof (struct partial_die_info));
15042
15043 part_die->offset.sect_off = info_ptr - buffer;
15044
15045 info_ptr += abbrev_len;
15046
15047 if (abbrev == NULL)
15048 return info_ptr;
15049
15050 part_die->tag = abbrev->tag;
15051 part_die->has_children = abbrev->has_children;
15052
15053 for (i = 0; i < abbrev->num_attrs; ++i)
15054 {
15055 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15056
15057 /* Store the data if it is of an attribute we want to keep in a
15058 partial symbol table. */
15059 switch (attr.name)
15060 {
15061 case DW_AT_name:
15062 switch (part_die->tag)
15063 {
15064 case DW_TAG_compile_unit:
15065 case DW_TAG_partial_unit:
15066 case DW_TAG_type_unit:
15067 /* Compilation units have a DW_AT_name that is a filename, not
15068 a source language identifier. */
15069 case DW_TAG_enumeration_type:
15070 case DW_TAG_enumerator:
15071 /* These tags always have simple identifiers already; no need
15072 to canonicalize them. */
15073 part_die->name = DW_STRING (&attr);
15074 break;
15075 default:
15076 part_die->name
15077 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
15078 &objfile->objfile_obstack);
15079 break;
15080 }
15081 break;
15082 case DW_AT_linkage_name:
15083 case DW_AT_MIPS_linkage_name:
15084 /* Note that both forms of linkage name might appear. We
15085 assume they will be the same, and we only store the last
15086 one we see. */
15087 if (cu->language == language_ada)
15088 part_die->name = DW_STRING (&attr);
15089 part_die->linkage_name = DW_STRING (&attr);
15090 break;
15091 case DW_AT_low_pc:
15092 has_low_pc_attr = 1;
15093 part_die->lowpc = DW_ADDR (&attr);
15094 break;
15095 case DW_AT_high_pc:
15096 has_high_pc_attr = 1;
15097 if (attr.form == DW_FORM_addr
15098 || attr.form == DW_FORM_GNU_addr_index)
15099 part_die->highpc = DW_ADDR (&attr);
15100 else
15101 {
15102 high_pc_relative = 1;
15103 part_die->highpc = DW_UNSND (&attr);
15104 }
15105 break;
15106 case DW_AT_location:
15107 /* Support the .debug_loc offsets. */
15108 if (attr_form_is_block (&attr))
15109 {
15110 part_die->d.locdesc = DW_BLOCK (&attr);
15111 }
15112 else if (attr_form_is_section_offset (&attr))
15113 {
15114 dwarf2_complex_location_expr_complaint ();
15115 }
15116 else
15117 {
15118 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15119 "partial symbol information");
15120 }
15121 break;
15122 case DW_AT_external:
15123 part_die->is_external = DW_UNSND (&attr);
15124 break;
15125 case DW_AT_declaration:
15126 part_die->is_declaration = DW_UNSND (&attr);
15127 break;
15128 case DW_AT_type:
15129 part_die->has_type = 1;
15130 break;
15131 case DW_AT_abstract_origin:
15132 case DW_AT_specification:
15133 case DW_AT_extension:
15134 part_die->has_specification = 1;
15135 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
15136 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15137 || cu->per_cu->is_dwz);
15138 break;
15139 case DW_AT_sibling:
15140 /* Ignore absolute siblings, they might point outside of
15141 the current compile unit. */
15142 if (attr.form == DW_FORM_ref_addr)
15143 complaint (&symfile_complaints,
15144 _("ignoring absolute DW_AT_sibling"));
15145 else
15146 {
15147 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15148 const gdb_byte *sibling_ptr = buffer + off;
15149
15150 if (sibling_ptr < info_ptr)
15151 complaint (&symfile_complaints,
15152 _("DW_AT_sibling points backwards"));
15153 else
15154 part_die->sibling = sibling_ptr;
15155 }
15156 break;
15157 case DW_AT_byte_size:
15158 part_die->has_byte_size = 1;
15159 break;
15160 case DW_AT_calling_convention:
15161 /* DWARF doesn't provide a way to identify a program's source-level
15162 entry point. DW_AT_calling_convention attributes are only meant
15163 to describe functions' calling conventions.
15164
15165 However, because it's a necessary piece of information in
15166 Fortran, and because DW_CC_program is the only piece of debugging
15167 information whose definition refers to a 'main program' at all,
15168 several compilers have begun marking Fortran main programs with
15169 DW_CC_program --- even when those functions use the standard
15170 calling conventions.
15171
15172 So until DWARF specifies a way to provide this information and
15173 compilers pick up the new representation, we'll support this
15174 practice. */
15175 if (DW_UNSND (&attr) == DW_CC_program
15176 && cu->language == language_fortran)
15177 {
15178 set_main_name (part_die->name);
15179
15180 /* As this DIE has a static linkage the name would be difficult
15181 to look up later. */
15182 language_of_main = language_fortran;
15183 }
15184 break;
15185 case DW_AT_inline:
15186 if (DW_UNSND (&attr) == DW_INL_inlined
15187 || DW_UNSND (&attr) == DW_INL_declared_inlined)
15188 part_die->may_be_inlined = 1;
15189 break;
15190
15191 case DW_AT_import:
15192 if (part_die->tag == DW_TAG_imported_unit)
15193 {
15194 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15195 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15196 || cu->per_cu->is_dwz);
15197 }
15198 break;
15199
15200 default:
15201 break;
15202 }
15203 }
15204
15205 if (high_pc_relative)
15206 part_die->highpc += part_die->lowpc;
15207
15208 if (has_low_pc_attr && has_high_pc_attr)
15209 {
15210 /* When using the GNU linker, .gnu.linkonce. sections are used to
15211 eliminate duplicate copies of functions and vtables and such.
15212 The linker will arbitrarily choose one and discard the others.
15213 The AT_*_pc values for such functions refer to local labels in
15214 these sections. If the section from that file was discarded, the
15215 labels are not in the output, so the relocs get a value of 0.
15216 If this is a discarded function, mark the pc bounds as invalid,
15217 so that GDB will ignore it. */
15218 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15219 {
15220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15221
15222 complaint (&symfile_complaints,
15223 _("DW_AT_low_pc %s is zero "
15224 "for DIE at 0x%x [in module %s]"),
15225 paddress (gdbarch, part_die->lowpc),
15226 part_die->offset.sect_off, objfile_name (objfile));
15227 }
15228 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15229 else if (part_die->lowpc >= part_die->highpc)
15230 {
15231 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15232
15233 complaint (&symfile_complaints,
15234 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15235 "for DIE at 0x%x [in module %s]"),
15236 paddress (gdbarch, part_die->lowpc),
15237 paddress (gdbarch, part_die->highpc),
15238 part_die->offset.sect_off, objfile_name (objfile));
15239 }
15240 else
15241 part_die->has_pc_info = 1;
15242 }
15243
15244 return info_ptr;
15245 }
15246
15247 /* Find a cached partial DIE at OFFSET in CU. */
15248
15249 static struct partial_die_info *
15250 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
15251 {
15252 struct partial_die_info *lookup_die = NULL;
15253 struct partial_die_info part_die;
15254
15255 part_die.offset = offset;
15256 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
15257 offset.sect_off);
15258
15259 return lookup_die;
15260 }
15261
15262 /* Find a partial DIE at OFFSET, which may or may not be in CU,
15263 except in the case of .debug_types DIEs which do not reference
15264 outside their CU (they do however referencing other types via
15265 DW_FORM_ref_sig8). */
15266
15267 static struct partial_die_info *
15268 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
15269 {
15270 struct objfile *objfile = cu->objfile;
15271 struct dwarf2_per_cu_data *per_cu = NULL;
15272 struct partial_die_info *pd = NULL;
15273
15274 if (offset_in_dwz == cu->per_cu->is_dwz
15275 && offset_in_cu_p (&cu->header, offset))
15276 {
15277 pd = find_partial_die_in_comp_unit (offset, cu);
15278 if (pd != NULL)
15279 return pd;
15280 /* We missed recording what we needed.
15281 Load all dies and try again. */
15282 per_cu = cu->per_cu;
15283 }
15284 else
15285 {
15286 /* TUs don't reference other CUs/TUs (except via type signatures). */
15287 if (cu->per_cu->is_debug_types)
15288 {
15289 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
15290 " external reference to offset 0x%lx [in module %s].\n"),
15291 (long) cu->header.offset.sect_off, (long) offset.sect_off,
15292 bfd_get_filename (objfile->obfd));
15293 }
15294 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
15295 objfile);
15296
15297 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
15298 load_partial_comp_unit (per_cu);
15299
15300 per_cu->cu->last_used = 0;
15301 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15302 }
15303
15304 /* If we didn't find it, and not all dies have been loaded,
15305 load them all and try again. */
15306
15307 if (pd == NULL && per_cu->load_all_dies == 0)
15308 {
15309 per_cu->load_all_dies = 1;
15310
15311 /* This is nasty. When we reread the DIEs, somewhere up the call chain
15312 THIS_CU->cu may already be in use. So we can't just free it and
15313 replace its DIEs with the ones we read in. Instead, we leave those
15314 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
15315 and clobber THIS_CU->cu->partial_dies with the hash table for the new
15316 set. */
15317 load_partial_comp_unit (per_cu);
15318
15319 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15320 }
15321
15322 if (pd == NULL)
15323 internal_error (__FILE__, __LINE__,
15324 _("could not find partial DIE 0x%x "
15325 "in cache [from module %s]\n"),
15326 offset.sect_off, bfd_get_filename (objfile->obfd));
15327 return pd;
15328 }
15329
15330 /* See if we can figure out if the class lives in a namespace. We do
15331 this by looking for a member function; its demangled name will
15332 contain namespace info, if there is any. */
15333
15334 static void
15335 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
15336 struct dwarf2_cu *cu)
15337 {
15338 /* NOTE: carlton/2003-10-07: Getting the info this way changes
15339 what template types look like, because the demangler
15340 frequently doesn't give the same name as the debug info. We
15341 could fix this by only using the demangled name to get the
15342 prefix (but see comment in read_structure_type). */
15343
15344 struct partial_die_info *real_pdi;
15345 struct partial_die_info *child_pdi;
15346
15347 /* If this DIE (this DIE's specification, if any) has a parent, then
15348 we should not do this. We'll prepend the parent's fully qualified
15349 name when we create the partial symbol. */
15350
15351 real_pdi = struct_pdi;
15352 while (real_pdi->has_specification)
15353 real_pdi = find_partial_die (real_pdi->spec_offset,
15354 real_pdi->spec_is_dwz, cu);
15355
15356 if (real_pdi->die_parent != NULL)
15357 return;
15358
15359 for (child_pdi = struct_pdi->die_child;
15360 child_pdi != NULL;
15361 child_pdi = child_pdi->die_sibling)
15362 {
15363 if (child_pdi->tag == DW_TAG_subprogram
15364 && child_pdi->linkage_name != NULL)
15365 {
15366 char *actual_class_name
15367 = language_class_name_from_physname (cu->language_defn,
15368 child_pdi->linkage_name);
15369 if (actual_class_name != NULL)
15370 {
15371 struct_pdi->name
15372 = obstack_copy0 (&cu->objfile->objfile_obstack,
15373 actual_class_name,
15374 strlen (actual_class_name));
15375 xfree (actual_class_name);
15376 }
15377 break;
15378 }
15379 }
15380 }
15381
15382 /* Adjust PART_DIE before generating a symbol for it. This function
15383 may set the is_external flag or change the DIE's name. */
15384
15385 static void
15386 fixup_partial_die (struct partial_die_info *part_die,
15387 struct dwarf2_cu *cu)
15388 {
15389 /* Once we've fixed up a die, there's no point in doing so again.
15390 This also avoids a memory leak if we were to call
15391 guess_partial_die_structure_name multiple times. */
15392 if (part_die->fixup_called)
15393 return;
15394
15395 /* If we found a reference attribute and the DIE has no name, try
15396 to find a name in the referred to DIE. */
15397
15398 if (part_die->name == NULL && part_die->has_specification)
15399 {
15400 struct partial_die_info *spec_die;
15401
15402 spec_die = find_partial_die (part_die->spec_offset,
15403 part_die->spec_is_dwz, cu);
15404
15405 fixup_partial_die (spec_die, cu);
15406
15407 if (spec_die->name)
15408 {
15409 part_die->name = spec_die->name;
15410
15411 /* Copy DW_AT_external attribute if it is set. */
15412 if (spec_die->is_external)
15413 part_die->is_external = spec_die->is_external;
15414 }
15415 }
15416
15417 /* Set default names for some unnamed DIEs. */
15418
15419 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
15420 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
15421
15422 /* If there is no parent die to provide a namespace, and there are
15423 children, see if we can determine the namespace from their linkage
15424 name. */
15425 if (cu->language == language_cplus
15426 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15427 && part_die->die_parent == NULL
15428 && part_die->has_children
15429 && (part_die->tag == DW_TAG_class_type
15430 || part_die->tag == DW_TAG_structure_type
15431 || part_die->tag == DW_TAG_union_type))
15432 guess_partial_die_structure_name (part_die, cu);
15433
15434 /* GCC might emit a nameless struct or union that has a linkage
15435 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15436 if (part_die->name == NULL
15437 && (part_die->tag == DW_TAG_class_type
15438 || part_die->tag == DW_TAG_interface_type
15439 || part_die->tag == DW_TAG_structure_type
15440 || part_die->tag == DW_TAG_union_type)
15441 && part_die->linkage_name != NULL)
15442 {
15443 char *demangled;
15444
15445 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
15446 if (demangled)
15447 {
15448 const char *base;
15449
15450 /* Strip any leading namespaces/classes, keep only the base name.
15451 DW_AT_name for named DIEs does not contain the prefixes. */
15452 base = strrchr (demangled, ':');
15453 if (base && base > demangled && base[-1] == ':')
15454 base++;
15455 else
15456 base = demangled;
15457
15458 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
15459 base, strlen (base));
15460 xfree (demangled);
15461 }
15462 }
15463
15464 part_die->fixup_called = 1;
15465 }
15466
15467 /* Read an attribute value described by an attribute form. */
15468
15469 static const gdb_byte *
15470 read_attribute_value (const struct die_reader_specs *reader,
15471 struct attribute *attr, unsigned form,
15472 const gdb_byte *info_ptr)
15473 {
15474 struct dwarf2_cu *cu = reader->cu;
15475 bfd *abfd = reader->abfd;
15476 struct comp_unit_head *cu_header = &cu->header;
15477 unsigned int bytes_read;
15478 struct dwarf_block *blk;
15479
15480 attr->form = form;
15481 switch (form)
15482 {
15483 case DW_FORM_ref_addr:
15484 if (cu->header.version == 2)
15485 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
15486 else
15487 DW_UNSND (attr) = read_offset (abfd, info_ptr,
15488 &cu->header, &bytes_read);
15489 info_ptr += bytes_read;
15490 break;
15491 case DW_FORM_GNU_ref_alt:
15492 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15493 info_ptr += bytes_read;
15494 break;
15495 case DW_FORM_addr:
15496 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
15497 info_ptr += bytes_read;
15498 break;
15499 case DW_FORM_block2:
15500 blk = dwarf_alloc_block (cu);
15501 blk->size = read_2_bytes (abfd, info_ptr);
15502 info_ptr += 2;
15503 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15504 info_ptr += blk->size;
15505 DW_BLOCK (attr) = blk;
15506 break;
15507 case DW_FORM_block4:
15508 blk = dwarf_alloc_block (cu);
15509 blk->size = read_4_bytes (abfd, info_ptr);
15510 info_ptr += 4;
15511 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15512 info_ptr += blk->size;
15513 DW_BLOCK (attr) = blk;
15514 break;
15515 case DW_FORM_data2:
15516 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
15517 info_ptr += 2;
15518 break;
15519 case DW_FORM_data4:
15520 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
15521 info_ptr += 4;
15522 break;
15523 case DW_FORM_data8:
15524 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
15525 info_ptr += 8;
15526 break;
15527 case DW_FORM_sec_offset:
15528 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15529 info_ptr += bytes_read;
15530 break;
15531 case DW_FORM_string:
15532 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
15533 DW_STRING_IS_CANONICAL (attr) = 0;
15534 info_ptr += bytes_read;
15535 break;
15536 case DW_FORM_strp:
15537 if (!cu->per_cu->is_dwz)
15538 {
15539 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
15540 &bytes_read);
15541 DW_STRING_IS_CANONICAL (attr) = 0;
15542 info_ptr += bytes_read;
15543 break;
15544 }
15545 /* FALLTHROUGH */
15546 case DW_FORM_GNU_strp_alt:
15547 {
15548 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15549 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
15550 &bytes_read);
15551
15552 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
15553 DW_STRING_IS_CANONICAL (attr) = 0;
15554 info_ptr += bytes_read;
15555 }
15556 break;
15557 case DW_FORM_exprloc:
15558 case DW_FORM_block:
15559 blk = dwarf_alloc_block (cu);
15560 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15561 info_ptr += bytes_read;
15562 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15563 info_ptr += blk->size;
15564 DW_BLOCK (attr) = blk;
15565 break;
15566 case DW_FORM_block1:
15567 blk = dwarf_alloc_block (cu);
15568 blk->size = read_1_byte (abfd, info_ptr);
15569 info_ptr += 1;
15570 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15571 info_ptr += blk->size;
15572 DW_BLOCK (attr) = blk;
15573 break;
15574 case DW_FORM_data1:
15575 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15576 info_ptr += 1;
15577 break;
15578 case DW_FORM_flag:
15579 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15580 info_ptr += 1;
15581 break;
15582 case DW_FORM_flag_present:
15583 DW_UNSND (attr) = 1;
15584 break;
15585 case DW_FORM_sdata:
15586 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
15587 info_ptr += bytes_read;
15588 break;
15589 case DW_FORM_udata:
15590 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15591 info_ptr += bytes_read;
15592 break;
15593 case DW_FORM_ref1:
15594 DW_UNSND (attr) = (cu->header.offset.sect_off
15595 + read_1_byte (abfd, info_ptr));
15596 info_ptr += 1;
15597 break;
15598 case DW_FORM_ref2:
15599 DW_UNSND (attr) = (cu->header.offset.sect_off
15600 + read_2_bytes (abfd, info_ptr));
15601 info_ptr += 2;
15602 break;
15603 case DW_FORM_ref4:
15604 DW_UNSND (attr) = (cu->header.offset.sect_off
15605 + read_4_bytes (abfd, info_ptr));
15606 info_ptr += 4;
15607 break;
15608 case DW_FORM_ref8:
15609 DW_UNSND (attr) = (cu->header.offset.sect_off
15610 + read_8_bytes (abfd, info_ptr));
15611 info_ptr += 8;
15612 break;
15613 case DW_FORM_ref_sig8:
15614 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
15615 info_ptr += 8;
15616 break;
15617 case DW_FORM_ref_udata:
15618 DW_UNSND (attr) = (cu->header.offset.sect_off
15619 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
15620 info_ptr += bytes_read;
15621 break;
15622 case DW_FORM_indirect:
15623 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15624 info_ptr += bytes_read;
15625 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
15626 break;
15627 case DW_FORM_GNU_addr_index:
15628 if (reader->dwo_file == NULL)
15629 {
15630 /* For now flag a hard error.
15631 Later we can turn this into a complaint. */
15632 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15633 dwarf_form_name (form),
15634 bfd_get_filename (abfd));
15635 }
15636 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
15637 info_ptr += bytes_read;
15638 break;
15639 case DW_FORM_GNU_str_index:
15640 if (reader->dwo_file == NULL)
15641 {
15642 /* For now flag a hard error.
15643 Later we can turn this into a complaint if warranted. */
15644 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15645 dwarf_form_name (form),
15646 bfd_get_filename (abfd));
15647 }
15648 {
15649 ULONGEST str_index =
15650 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15651
15652 DW_STRING (attr) = read_str_index (reader, cu, str_index);
15653 DW_STRING_IS_CANONICAL (attr) = 0;
15654 info_ptr += bytes_read;
15655 }
15656 break;
15657 default:
15658 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
15659 dwarf_form_name (form),
15660 bfd_get_filename (abfd));
15661 }
15662
15663 /* Super hack. */
15664 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
15665 attr->form = DW_FORM_GNU_ref_alt;
15666
15667 /* We have seen instances where the compiler tried to emit a byte
15668 size attribute of -1 which ended up being encoded as an unsigned
15669 0xffffffff. Although 0xffffffff is technically a valid size value,
15670 an object of this size seems pretty unlikely so we can relatively
15671 safely treat these cases as if the size attribute was invalid and
15672 treat them as zero by default. */
15673 if (attr->name == DW_AT_byte_size
15674 && form == DW_FORM_data4
15675 && DW_UNSND (attr) >= 0xffffffff)
15676 {
15677 complaint
15678 (&symfile_complaints,
15679 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
15680 hex_string (DW_UNSND (attr)));
15681 DW_UNSND (attr) = 0;
15682 }
15683
15684 return info_ptr;
15685 }
15686
15687 /* Read an attribute described by an abbreviated attribute. */
15688
15689 static const gdb_byte *
15690 read_attribute (const struct die_reader_specs *reader,
15691 struct attribute *attr, struct attr_abbrev *abbrev,
15692 const gdb_byte *info_ptr)
15693 {
15694 attr->name = abbrev->name;
15695 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
15696 }
15697
15698 /* Read dwarf information from a buffer. */
15699
15700 static unsigned int
15701 read_1_byte (bfd *abfd, const gdb_byte *buf)
15702 {
15703 return bfd_get_8 (abfd, buf);
15704 }
15705
15706 static int
15707 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
15708 {
15709 return bfd_get_signed_8 (abfd, buf);
15710 }
15711
15712 static unsigned int
15713 read_2_bytes (bfd *abfd, const gdb_byte *buf)
15714 {
15715 return bfd_get_16 (abfd, buf);
15716 }
15717
15718 static int
15719 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
15720 {
15721 return bfd_get_signed_16 (abfd, buf);
15722 }
15723
15724 static unsigned int
15725 read_4_bytes (bfd *abfd, const gdb_byte *buf)
15726 {
15727 return bfd_get_32 (abfd, buf);
15728 }
15729
15730 static int
15731 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
15732 {
15733 return bfd_get_signed_32 (abfd, buf);
15734 }
15735
15736 static ULONGEST
15737 read_8_bytes (bfd *abfd, const gdb_byte *buf)
15738 {
15739 return bfd_get_64 (abfd, buf);
15740 }
15741
15742 static CORE_ADDR
15743 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
15744 unsigned int *bytes_read)
15745 {
15746 struct comp_unit_head *cu_header = &cu->header;
15747 CORE_ADDR retval = 0;
15748
15749 if (cu_header->signed_addr_p)
15750 {
15751 switch (cu_header->addr_size)
15752 {
15753 case 2:
15754 retval = bfd_get_signed_16 (abfd, buf);
15755 break;
15756 case 4:
15757 retval = bfd_get_signed_32 (abfd, buf);
15758 break;
15759 case 8:
15760 retval = bfd_get_signed_64 (abfd, buf);
15761 break;
15762 default:
15763 internal_error (__FILE__, __LINE__,
15764 _("read_address: bad switch, signed [in module %s]"),
15765 bfd_get_filename (abfd));
15766 }
15767 }
15768 else
15769 {
15770 switch (cu_header->addr_size)
15771 {
15772 case 2:
15773 retval = bfd_get_16 (abfd, buf);
15774 break;
15775 case 4:
15776 retval = bfd_get_32 (abfd, buf);
15777 break;
15778 case 8:
15779 retval = bfd_get_64 (abfd, buf);
15780 break;
15781 default:
15782 internal_error (__FILE__, __LINE__,
15783 _("read_address: bad switch, "
15784 "unsigned [in module %s]"),
15785 bfd_get_filename (abfd));
15786 }
15787 }
15788
15789 *bytes_read = cu_header->addr_size;
15790 return retval;
15791 }
15792
15793 /* Read the initial length from a section. The (draft) DWARF 3
15794 specification allows the initial length to take up either 4 bytes
15795 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15796 bytes describe the length and all offsets will be 8 bytes in length
15797 instead of 4.
15798
15799 An older, non-standard 64-bit format is also handled by this
15800 function. The older format in question stores the initial length
15801 as an 8-byte quantity without an escape value. Lengths greater
15802 than 2^32 aren't very common which means that the initial 4 bytes
15803 is almost always zero. Since a length value of zero doesn't make
15804 sense for the 32-bit format, this initial zero can be considered to
15805 be an escape value which indicates the presence of the older 64-bit
15806 format. As written, the code can't detect (old format) lengths
15807 greater than 4GB. If it becomes necessary to handle lengths
15808 somewhat larger than 4GB, we could allow other small values (such
15809 as the non-sensical values of 1, 2, and 3) to also be used as
15810 escape values indicating the presence of the old format.
15811
15812 The value returned via bytes_read should be used to increment the
15813 relevant pointer after calling read_initial_length().
15814
15815 [ Note: read_initial_length() and read_offset() are based on the
15816 document entitled "DWARF Debugging Information Format", revision
15817 3, draft 8, dated November 19, 2001. This document was obtained
15818 from:
15819
15820 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
15821
15822 This document is only a draft and is subject to change. (So beware.)
15823
15824 Details regarding the older, non-standard 64-bit format were
15825 determined empirically by examining 64-bit ELF files produced by
15826 the SGI toolchain on an IRIX 6.5 machine.
15827
15828 - Kevin, July 16, 2002
15829 ] */
15830
15831 static LONGEST
15832 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
15833 {
15834 LONGEST length = bfd_get_32 (abfd, buf);
15835
15836 if (length == 0xffffffff)
15837 {
15838 length = bfd_get_64 (abfd, buf + 4);
15839 *bytes_read = 12;
15840 }
15841 else if (length == 0)
15842 {
15843 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
15844 length = bfd_get_64 (abfd, buf);
15845 *bytes_read = 8;
15846 }
15847 else
15848 {
15849 *bytes_read = 4;
15850 }
15851
15852 return length;
15853 }
15854
15855 /* Cover function for read_initial_length.
15856 Returns the length of the object at BUF, and stores the size of the
15857 initial length in *BYTES_READ and stores the size that offsets will be in
15858 *OFFSET_SIZE.
15859 If the initial length size is not equivalent to that specified in
15860 CU_HEADER then issue a complaint.
15861 This is useful when reading non-comp-unit headers. */
15862
15863 static LONGEST
15864 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15865 const struct comp_unit_head *cu_header,
15866 unsigned int *bytes_read,
15867 unsigned int *offset_size)
15868 {
15869 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15870
15871 gdb_assert (cu_header->initial_length_size == 4
15872 || cu_header->initial_length_size == 8
15873 || cu_header->initial_length_size == 12);
15874
15875 if (cu_header->initial_length_size != *bytes_read)
15876 complaint (&symfile_complaints,
15877 _("intermixed 32-bit and 64-bit DWARF sections"));
15878
15879 *offset_size = (*bytes_read == 4) ? 4 : 8;
15880 return length;
15881 }
15882
15883 /* Read an offset from the data stream. The size of the offset is
15884 given by cu_header->offset_size. */
15885
15886 static LONGEST
15887 read_offset (bfd *abfd, const gdb_byte *buf,
15888 const struct comp_unit_head *cu_header,
15889 unsigned int *bytes_read)
15890 {
15891 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15892
15893 *bytes_read = cu_header->offset_size;
15894 return offset;
15895 }
15896
15897 /* Read an offset from the data stream. */
15898
15899 static LONGEST
15900 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15901 {
15902 LONGEST retval = 0;
15903
15904 switch (offset_size)
15905 {
15906 case 4:
15907 retval = bfd_get_32 (abfd, buf);
15908 break;
15909 case 8:
15910 retval = bfd_get_64 (abfd, buf);
15911 break;
15912 default:
15913 internal_error (__FILE__, __LINE__,
15914 _("read_offset_1: bad switch [in module %s]"),
15915 bfd_get_filename (abfd));
15916 }
15917
15918 return retval;
15919 }
15920
15921 static const gdb_byte *
15922 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15923 {
15924 /* If the size of a host char is 8 bits, we can return a pointer
15925 to the buffer, otherwise we have to copy the data to a buffer
15926 allocated on the temporary obstack. */
15927 gdb_assert (HOST_CHAR_BIT == 8);
15928 return buf;
15929 }
15930
15931 static const char *
15932 read_direct_string (bfd *abfd, const gdb_byte *buf,
15933 unsigned int *bytes_read_ptr)
15934 {
15935 /* If the size of a host char is 8 bits, we can return a pointer
15936 to the string, otherwise we have to copy the string to a buffer
15937 allocated on the temporary obstack. */
15938 gdb_assert (HOST_CHAR_BIT == 8);
15939 if (*buf == '\0')
15940 {
15941 *bytes_read_ptr = 1;
15942 return NULL;
15943 }
15944 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15945 return (const char *) buf;
15946 }
15947
15948 static const char *
15949 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15950 {
15951 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15952 if (dwarf2_per_objfile->str.buffer == NULL)
15953 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15954 bfd_get_filename (abfd));
15955 if (str_offset >= dwarf2_per_objfile->str.size)
15956 error (_("DW_FORM_strp pointing outside of "
15957 ".debug_str section [in module %s]"),
15958 bfd_get_filename (abfd));
15959 gdb_assert (HOST_CHAR_BIT == 8);
15960 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15961 return NULL;
15962 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15963 }
15964
15965 /* Read a string at offset STR_OFFSET in the .debug_str section from
15966 the .dwz file DWZ. Throw an error if the offset is too large. If
15967 the string consists of a single NUL byte, return NULL; otherwise
15968 return a pointer to the string. */
15969
15970 static const char *
15971 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15972 {
15973 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15974
15975 if (dwz->str.buffer == NULL)
15976 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15977 "section [in module %s]"),
15978 bfd_get_filename (dwz->dwz_bfd));
15979 if (str_offset >= dwz->str.size)
15980 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15981 ".debug_str section [in module %s]"),
15982 bfd_get_filename (dwz->dwz_bfd));
15983 gdb_assert (HOST_CHAR_BIT == 8);
15984 if (dwz->str.buffer[str_offset] == '\0')
15985 return NULL;
15986 return (const char *) (dwz->str.buffer + str_offset);
15987 }
15988
15989 static const char *
15990 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15991 const struct comp_unit_head *cu_header,
15992 unsigned int *bytes_read_ptr)
15993 {
15994 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15995
15996 return read_indirect_string_at_offset (abfd, str_offset);
15997 }
15998
15999 static ULONGEST
16000 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16001 unsigned int *bytes_read_ptr)
16002 {
16003 ULONGEST result;
16004 unsigned int num_read;
16005 int i, shift;
16006 unsigned char byte;
16007
16008 result = 0;
16009 shift = 0;
16010 num_read = 0;
16011 i = 0;
16012 while (1)
16013 {
16014 byte = bfd_get_8 (abfd, buf);
16015 buf++;
16016 num_read++;
16017 result |= ((ULONGEST) (byte & 127) << shift);
16018 if ((byte & 128) == 0)
16019 {
16020 break;
16021 }
16022 shift += 7;
16023 }
16024 *bytes_read_ptr = num_read;
16025 return result;
16026 }
16027
16028 static LONGEST
16029 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16030 unsigned int *bytes_read_ptr)
16031 {
16032 LONGEST result;
16033 int i, shift, num_read;
16034 unsigned char byte;
16035
16036 result = 0;
16037 shift = 0;
16038 num_read = 0;
16039 i = 0;
16040 while (1)
16041 {
16042 byte = bfd_get_8 (abfd, buf);
16043 buf++;
16044 num_read++;
16045 result |= ((LONGEST) (byte & 127) << shift);
16046 shift += 7;
16047 if ((byte & 128) == 0)
16048 {
16049 break;
16050 }
16051 }
16052 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
16053 result |= -(((LONGEST) 1) << shift);
16054 *bytes_read_ptr = num_read;
16055 return result;
16056 }
16057
16058 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16059 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16060 ADDR_SIZE is the size of addresses from the CU header. */
16061
16062 static CORE_ADDR
16063 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16064 {
16065 struct objfile *objfile = dwarf2_per_objfile->objfile;
16066 bfd *abfd = objfile->obfd;
16067 const gdb_byte *info_ptr;
16068
16069 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16070 if (dwarf2_per_objfile->addr.buffer == NULL)
16071 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16072 objfile_name (objfile));
16073 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16074 error (_("DW_FORM_addr_index pointing outside of "
16075 ".debug_addr section [in module %s]"),
16076 objfile_name (objfile));
16077 info_ptr = (dwarf2_per_objfile->addr.buffer
16078 + addr_base + addr_index * addr_size);
16079 if (addr_size == 4)
16080 return bfd_get_32 (abfd, info_ptr);
16081 else
16082 return bfd_get_64 (abfd, info_ptr);
16083 }
16084
16085 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16086
16087 static CORE_ADDR
16088 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16089 {
16090 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16091 }
16092
16093 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16094
16095 static CORE_ADDR
16096 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16097 unsigned int *bytes_read)
16098 {
16099 bfd *abfd = cu->objfile->obfd;
16100 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16101
16102 return read_addr_index (cu, addr_index);
16103 }
16104
16105 /* Data structure to pass results from dwarf2_read_addr_index_reader
16106 back to dwarf2_read_addr_index. */
16107
16108 struct dwarf2_read_addr_index_data
16109 {
16110 ULONGEST addr_base;
16111 int addr_size;
16112 };
16113
16114 /* die_reader_func for dwarf2_read_addr_index. */
16115
16116 static void
16117 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
16118 const gdb_byte *info_ptr,
16119 struct die_info *comp_unit_die,
16120 int has_children,
16121 void *data)
16122 {
16123 struct dwarf2_cu *cu = reader->cu;
16124 struct dwarf2_read_addr_index_data *aidata =
16125 (struct dwarf2_read_addr_index_data *) data;
16126
16127 aidata->addr_base = cu->addr_base;
16128 aidata->addr_size = cu->header.addr_size;
16129 }
16130
16131 /* Given an index in .debug_addr, fetch the value.
16132 NOTE: This can be called during dwarf expression evaluation,
16133 long after the debug information has been read, and thus per_cu->cu
16134 may no longer exist. */
16135
16136 CORE_ADDR
16137 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16138 unsigned int addr_index)
16139 {
16140 struct objfile *objfile = per_cu->objfile;
16141 struct dwarf2_cu *cu = per_cu->cu;
16142 ULONGEST addr_base;
16143 int addr_size;
16144
16145 /* This is intended to be called from outside this file. */
16146 dw2_setup (objfile);
16147
16148 /* We need addr_base and addr_size.
16149 If we don't have PER_CU->cu, we have to get it.
16150 Nasty, but the alternative is storing the needed info in PER_CU,
16151 which at this point doesn't seem justified: it's not clear how frequently
16152 it would get used and it would increase the size of every PER_CU.
16153 Entry points like dwarf2_per_cu_addr_size do a similar thing
16154 so we're not in uncharted territory here.
16155 Alas we need to be a bit more complicated as addr_base is contained
16156 in the DIE.
16157
16158 We don't need to read the entire CU(/TU).
16159 We just need the header and top level die.
16160
16161 IWBN to use the aging mechanism to let us lazily later discard the CU.
16162 For now we skip this optimization. */
16163
16164 if (cu != NULL)
16165 {
16166 addr_base = cu->addr_base;
16167 addr_size = cu->header.addr_size;
16168 }
16169 else
16170 {
16171 struct dwarf2_read_addr_index_data aidata;
16172
16173 /* Note: We can't use init_cutu_and_read_dies_simple here,
16174 we need addr_base. */
16175 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16176 dwarf2_read_addr_index_reader, &aidata);
16177 addr_base = aidata.addr_base;
16178 addr_size = aidata.addr_size;
16179 }
16180
16181 return read_addr_index_1 (addr_index, addr_base, addr_size);
16182 }
16183
16184 /* Given a DW_FORM_GNU_str_index, fetch the string.
16185 This is only used by the Fission support. */
16186
16187 static const char *
16188 read_str_index (const struct die_reader_specs *reader,
16189 struct dwarf2_cu *cu, ULONGEST str_index)
16190 {
16191 struct objfile *objfile = dwarf2_per_objfile->objfile;
16192 const char *dwo_name = objfile_name (objfile);
16193 bfd *abfd = objfile->obfd;
16194 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16195 struct dwarf2_section_info *str_offsets_section =
16196 &reader->dwo_file->sections.str_offsets;
16197 const gdb_byte *info_ptr;
16198 ULONGEST str_offset;
16199 static const char form_name[] = "DW_FORM_GNU_str_index";
16200
16201 dwarf2_read_section (objfile, str_section);
16202 dwarf2_read_section (objfile, str_offsets_section);
16203 if (str_section->buffer == NULL)
16204 error (_("%s used without .debug_str.dwo section"
16205 " in CU at offset 0x%lx [in module %s]"),
16206 form_name, (long) cu->header.offset.sect_off, dwo_name);
16207 if (str_offsets_section->buffer == NULL)
16208 error (_("%s used without .debug_str_offsets.dwo section"
16209 " in CU at offset 0x%lx [in module %s]"),
16210 form_name, (long) cu->header.offset.sect_off, dwo_name);
16211 if (str_index * cu->header.offset_size >= str_offsets_section->size)
16212 error (_("%s pointing outside of .debug_str_offsets.dwo"
16213 " section in CU at offset 0x%lx [in module %s]"),
16214 form_name, (long) cu->header.offset.sect_off, dwo_name);
16215 info_ptr = (str_offsets_section->buffer
16216 + str_index * cu->header.offset_size);
16217 if (cu->header.offset_size == 4)
16218 str_offset = bfd_get_32 (abfd, info_ptr);
16219 else
16220 str_offset = bfd_get_64 (abfd, info_ptr);
16221 if (str_offset >= str_section->size)
16222 error (_("Offset from %s pointing outside of"
16223 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
16224 form_name, (long) cu->header.offset.sect_off, dwo_name);
16225 return (const char *) (str_section->buffer + str_offset);
16226 }
16227
16228 /* Return the length of an LEB128 number in BUF. */
16229
16230 static int
16231 leb128_size (const gdb_byte *buf)
16232 {
16233 const gdb_byte *begin = buf;
16234 gdb_byte byte;
16235
16236 while (1)
16237 {
16238 byte = *buf++;
16239 if ((byte & 128) == 0)
16240 return buf - begin;
16241 }
16242 }
16243
16244 static void
16245 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
16246 {
16247 switch (lang)
16248 {
16249 case DW_LANG_C89:
16250 case DW_LANG_C99:
16251 case DW_LANG_C:
16252 case DW_LANG_UPC:
16253 cu->language = language_c;
16254 break;
16255 case DW_LANG_C_plus_plus:
16256 cu->language = language_cplus;
16257 break;
16258 case DW_LANG_D:
16259 cu->language = language_d;
16260 break;
16261 case DW_LANG_Fortran77:
16262 case DW_LANG_Fortran90:
16263 case DW_LANG_Fortran95:
16264 cu->language = language_fortran;
16265 break;
16266 case DW_LANG_Go:
16267 cu->language = language_go;
16268 break;
16269 case DW_LANG_Mips_Assembler:
16270 cu->language = language_asm;
16271 break;
16272 case DW_LANG_Java:
16273 cu->language = language_java;
16274 break;
16275 case DW_LANG_Ada83:
16276 case DW_LANG_Ada95:
16277 cu->language = language_ada;
16278 break;
16279 case DW_LANG_Modula2:
16280 cu->language = language_m2;
16281 break;
16282 case DW_LANG_Pascal83:
16283 cu->language = language_pascal;
16284 break;
16285 case DW_LANG_ObjC:
16286 cu->language = language_objc;
16287 break;
16288 case DW_LANG_Cobol74:
16289 case DW_LANG_Cobol85:
16290 default:
16291 cu->language = language_minimal;
16292 break;
16293 }
16294 cu->language_defn = language_def (cu->language);
16295 }
16296
16297 /* Return the named attribute or NULL if not there. */
16298
16299 static struct attribute *
16300 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
16301 {
16302 for (;;)
16303 {
16304 unsigned int i;
16305 struct attribute *spec = NULL;
16306
16307 for (i = 0; i < die->num_attrs; ++i)
16308 {
16309 if (die->attrs[i].name == name)
16310 return &die->attrs[i];
16311 if (die->attrs[i].name == DW_AT_specification
16312 || die->attrs[i].name == DW_AT_abstract_origin)
16313 spec = &die->attrs[i];
16314 }
16315
16316 if (!spec)
16317 break;
16318
16319 die = follow_die_ref (die, spec, &cu);
16320 }
16321
16322 return NULL;
16323 }
16324
16325 /* Return the named attribute or NULL if not there,
16326 but do not follow DW_AT_specification, etc.
16327 This is for use in contexts where we're reading .debug_types dies.
16328 Following DW_AT_specification, DW_AT_abstract_origin will take us
16329 back up the chain, and we want to go down. */
16330
16331 static struct attribute *
16332 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
16333 {
16334 unsigned int i;
16335
16336 for (i = 0; i < die->num_attrs; ++i)
16337 if (die->attrs[i].name == name)
16338 return &die->attrs[i];
16339
16340 return NULL;
16341 }
16342
16343 /* Return non-zero iff the attribute NAME is defined for the given DIE,
16344 and holds a non-zero value. This function should only be used for
16345 DW_FORM_flag or DW_FORM_flag_present attributes. */
16346
16347 static int
16348 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
16349 {
16350 struct attribute *attr = dwarf2_attr (die, name, cu);
16351
16352 return (attr && DW_UNSND (attr));
16353 }
16354
16355 static int
16356 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
16357 {
16358 /* A DIE is a declaration if it has a DW_AT_declaration attribute
16359 which value is non-zero. However, we have to be careful with
16360 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
16361 (via dwarf2_flag_true_p) follows this attribute. So we may
16362 end up accidently finding a declaration attribute that belongs
16363 to a different DIE referenced by the specification attribute,
16364 even though the given DIE does not have a declaration attribute. */
16365 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
16366 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
16367 }
16368
16369 /* Return the die giving the specification for DIE, if there is
16370 one. *SPEC_CU is the CU containing DIE on input, and the CU
16371 containing the return value on output. If there is no
16372 specification, but there is an abstract origin, that is
16373 returned. */
16374
16375 static struct die_info *
16376 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
16377 {
16378 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
16379 *spec_cu);
16380
16381 if (spec_attr == NULL)
16382 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
16383
16384 if (spec_attr == NULL)
16385 return NULL;
16386 else
16387 return follow_die_ref (die, spec_attr, spec_cu);
16388 }
16389
16390 /* Free the line_header structure *LH, and any arrays and strings it
16391 refers to.
16392 NOTE: This is also used as a "cleanup" function. */
16393
16394 static void
16395 free_line_header (struct line_header *lh)
16396 {
16397 if (lh->standard_opcode_lengths)
16398 xfree (lh->standard_opcode_lengths);
16399
16400 /* Remember that all the lh->file_names[i].name pointers are
16401 pointers into debug_line_buffer, and don't need to be freed. */
16402 if (lh->file_names)
16403 xfree (lh->file_names);
16404
16405 /* Similarly for the include directory names. */
16406 if (lh->include_dirs)
16407 xfree (lh->include_dirs);
16408
16409 xfree (lh);
16410 }
16411
16412 /* Add an entry to LH's include directory table. */
16413
16414 static void
16415 add_include_dir (struct line_header *lh, const char *include_dir)
16416 {
16417 /* Grow the array if necessary. */
16418 if (lh->include_dirs_size == 0)
16419 {
16420 lh->include_dirs_size = 1; /* for testing */
16421 lh->include_dirs = xmalloc (lh->include_dirs_size
16422 * sizeof (*lh->include_dirs));
16423 }
16424 else if (lh->num_include_dirs >= lh->include_dirs_size)
16425 {
16426 lh->include_dirs_size *= 2;
16427 lh->include_dirs = xrealloc (lh->include_dirs,
16428 (lh->include_dirs_size
16429 * sizeof (*lh->include_dirs)));
16430 }
16431
16432 lh->include_dirs[lh->num_include_dirs++] = include_dir;
16433 }
16434
16435 /* Add an entry to LH's file name table. */
16436
16437 static void
16438 add_file_name (struct line_header *lh,
16439 const char *name,
16440 unsigned int dir_index,
16441 unsigned int mod_time,
16442 unsigned int length)
16443 {
16444 struct file_entry *fe;
16445
16446 /* Grow the array if necessary. */
16447 if (lh->file_names_size == 0)
16448 {
16449 lh->file_names_size = 1; /* for testing */
16450 lh->file_names = xmalloc (lh->file_names_size
16451 * sizeof (*lh->file_names));
16452 }
16453 else if (lh->num_file_names >= lh->file_names_size)
16454 {
16455 lh->file_names_size *= 2;
16456 lh->file_names = xrealloc (lh->file_names,
16457 (lh->file_names_size
16458 * sizeof (*lh->file_names)));
16459 }
16460
16461 fe = &lh->file_names[lh->num_file_names++];
16462 fe->name = name;
16463 fe->dir_index = dir_index;
16464 fe->mod_time = mod_time;
16465 fe->length = length;
16466 fe->included_p = 0;
16467 fe->symtab = NULL;
16468 }
16469
16470 /* A convenience function to find the proper .debug_line section for a
16471 CU. */
16472
16473 static struct dwarf2_section_info *
16474 get_debug_line_section (struct dwarf2_cu *cu)
16475 {
16476 struct dwarf2_section_info *section;
16477
16478 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
16479 DWO file. */
16480 if (cu->dwo_unit && cu->per_cu->is_debug_types)
16481 section = &cu->dwo_unit->dwo_file->sections.line;
16482 else if (cu->per_cu->is_dwz)
16483 {
16484 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16485
16486 section = &dwz->line;
16487 }
16488 else
16489 section = &dwarf2_per_objfile->line;
16490
16491 return section;
16492 }
16493
16494 /* Read the statement program header starting at OFFSET in
16495 .debug_line, or .debug_line.dwo. Return a pointer
16496 to a struct line_header, allocated using xmalloc.
16497
16498 NOTE: the strings in the include directory and file name tables of
16499 the returned object point into the dwarf line section buffer,
16500 and must not be freed. */
16501
16502 static struct line_header *
16503 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
16504 {
16505 struct cleanup *back_to;
16506 struct line_header *lh;
16507 const gdb_byte *line_ptr;
16508 unsigned int bytes_read, offset_size;
16509 int i;
16510 const char *cur_dir, *cur_file;
16511 struct dwarf2_section_info *section;
16512 bfd *abfd;
16513
16514 section = get_debug_line_section (cu);
16515 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16516 if (section->buffer == NULL)
16517 {
16518 if (cu->dwo_unit && cu->per_cu->is_debug_types)
16519 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
16520 else
16521 complaint (&symfile_complaints, _("missing .debug_line section"));
16522 return 0;
16523 }
16524
16525 /* We can't do this until we know the section is non-empty.
16526 Only then do we know we have such a section. */
16527 abfd = get_section_bfd_owner (section);
16528
16529 /* Make sure that at least there's room for the total_length field.
16530 That could be 12 bytes long, but we're just going to fudge that. */
16531 if (offset + 4 >= section->size)
16532 {
16533 dwarf2_statement_list_fits_in_line_number_section_complaint ();
16534 return 0;
16535 }
16536
16537 lh = xmalloc (sizeof (*lh));
16538 memset (lh, 0, sizeof (*lh));
16539 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
16540 (void *) lh);
16541
16542 line_ptr = section->buffer + offset;
16543
16544 /* Read in the header. */
16545 lh->total_length =
16546 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
16547 &bytes_read, &offset_size);
16548 line_ptr += bytes_read;
16549 if (line_ptr + lh->total_length > (section->buffer + section->size))
16550 {
16551 dwarf2_statement_list_fits_in_line_number_section_complaint ();
16552 do_cleanups (back_to);
16553 return 0;
16554 }
16555 lh->statement_program_end = line_ptr + lh->total_length;
16556 lh->version = read_2_bytes (abfd, line_ptr);
16557 line_ptr += 2;
16558 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
16559 line_ptr += offset_size;
16560 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
16561 line_ptr += 1;
16562 if (lh->version >= 4)
16563 {
16564 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
16565 line_ptr += 1;
16566 }
16567 else
16568 lh->maximum_ops_per_instruction = 1;
16569
16570 if (lh->maximum_ops_per_instruction == 0)
16571 {
16572 lh->maximum_ops_per_instruction = 1;
16573 complaint (&symfile_complaints,
16574 _("invalid maximum_ops_per_instruction "
16575 "in `.debug_line' section"));
16576 }
16577
16578 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
16579 line_ptr += 1;
16580 lh->line_base = read_1_signed_byte (abfd, line_ptr);
16581 line_ptr += 1;
16582 lh->line_range = read_1_byte (abfd, line_ptr);
16583 line_ptr += 1;
16584 lh->opcode_base = read_1_byte (abfd, line_ptr);
16585 line_ptr += 1;
16586 lh->standard_opcode_lengths
16587 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
16588
16589 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
16590 for (i = 1; i < lh->opcode_base; ++i)
16591 {
16592 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
16593 line_ptr += 1;
16594 }
16595
16596 /* Read directory table. */
16597 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
16598 {
16599 line_ptr += bytes_read;
16600 add_include_dir (lh, cur_dir);
16601 }
16602 line_ptr += bytes_read;
16603
16604 /* Read file name table. */
16605 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
16606 {
16607 unsigned int dir_index, mod_time, length;
16608
16609 line_ptr += bytes_read;
16610 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16611 line_ptr += bytes_read;
16612 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16613 line_ptr += bytes_read;
16614 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16615 line_ptr += bytes_read;
16616
16617 add_file_name (lh, cur_file, dir_index, mod_time, length);
16618 }
16619 line_ptr += bytes_read;
16620 lh->statement_program_start = line_ptr;
16621
16622 if (line_ptr > (section->buffer + section->size))
16623 complaint (&symfile_complaints,
16624 _("line number info header doesn't "
16625 "fit in `.debug_line' section"));
16626
16627 discard_cleanups (back_to);
16628 return lh;
16629 }
16630
16631 /* Subroutine of dwarf_decode_lines to simplify it.
16632 Return the file name of the psymtab for included file FILE_INDEX
16633 in line header LH of PST.
16634 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16635 If space for the result is malloc'd, it will be freed by a cleanup.
16636 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
16637
16638 The function creates dangling cleanup registration. */
16639
16640 static const char *
16641 psymtab_include_file_name (const struct line_header *lh, int file_index,
16642 const struct partial_symtab *pst,
16643 const char *comp_dir)
16644 {
16645 const struct file_entry fe = lh->file_names [file_index];
16646 const char *include_name = fe.name;
16647 const char *include_name_to_compare = include_name;
16648 const char *dir_name = NULL;
16649 const char *pst_filename;
16650 char *copied_name = NULL;
16651 int file_is_pst;
16652
16653 if (fe.dir_index)
16654 dir_name = lh->include_dirs[fe.dir_index - 1];
16655
16656 if (!IS_ABSOLUTE_PATH (include_name)
16657 && (dir_name != NULL || comp_dir != NULL))
16658 {
16659 /* Avoid creating a duplicate psymtab for PST.
16660 We do this by comparing INCLUDE_NAME and PST_FILENAME.
16661 Before we do the comparison, however, we need to account
16662 for DIR_NAME and COMP_DIR.
16663 First prepend dir_name (if non-NULL). If we still don't
16664 have an absolute path prepend comp_dir (if non-NULL).
16665 However, the directory we record in the include-file's
16666 psymtab does not contain COMP_DIR (to match the
16667 corresponding symtab(s)).
16668
16669 Example:
16670
16671 bash$ cd /tmp
16672 bash$ gcc -g ./hello.c
16673 include_name = "hello.c"
16674 dir_name = "."
16675 DW_AT_comp_dir = comp_dir = "/tmp"
16676 DW_AT_name = "./hello.c" */
16677
16678 if (dir_name != NULL)
16679 {
16680 char *tem = concat (dir_name, SLASH_STRING,
16681 include_name, (char *)NULL);
16682
16683 make_cleanup (xfree, tem);
16684 include_name = tem;
16685 include_name_to_compare = include_name;
16686 }
16687 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
16688 {
16689 char *tem = concat (comp_dir, SLASH_STRING,
16690 include_name, (char *)NULL);
16691
16692 make_cleanup (xfree, tem);
16693 include_name_to_compare = tem;
16694 }
16695 }
16696
16697 pst_filename = pst->filename;
16698 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
16699 {
16700 copied_name = concat (pst->dirname, SLASH_STRING,
16701 pst_filename, (char *)NULL);
16702 pst_filename = copied_name;
16703 }
16704
16705 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
16706
16707 if (copied_name != NULL)
16708 xfree (copied_name);
16709
16710 if (file_is_pst)
16711 return NULL;
16712 return include_name;
16713 }
16714
16715 /* Ignore this record_line request. */
16716
16717 static void
16718 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
16719 {
16720 return;
16721 }
16722
16723 /* Subroutine of dwarf_decode_lines to simplify it.
16724 Process the line number information in LH. */
16725
16726 static void
16727 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
16728 struct dwarf2_cu *cu, struct partial_symtab *pst)
16729 {
16730 const gdb_byte *line_ptr, *extended_end;
16731 const gdb_byte *line_end;
16732 unsigned int bytes_read, extended_len;
16733 unsigned char op_code, extended_op, adj_opcode;
16734 CORE_ADDR baseaddr;
16735 struct objfile *objfile = cu->objfile;
16736 bfd *abfd = objfile->obfd;
16737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16738 const int decode_for_pst_p = (pst != NULL);
16739 struct subfile *last_subfile = NULL;
16740 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
16741 = record_line;
16742
16743 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16744
16745 line_ptr = lh->statement_program_start;
16746 line_end = lh->statement_program_end;
16747
16748 /* Read the statement sequences until there's nothing left. */
16749 while (line_ptr < line_end)
16750 {
16751 /* state machine registers */
16752 CORE_ADDR address = 0;
16753 unsigned int file = 1;
16754 unsigned int line = 1;
16755 unsigned int column = 0;
16756 int is_stmt = lh->default_is_stmt;
16757 int basic_block = 0;
16758 int end_sequence = 0;
16759 CORE_ADDR addr;
16760 unsigned char op_index = 0;
16761
16762 if (!decode_for_pst_p && lh->num_file_names >= file)
16763 {
16764 /* Start a subfile for the current file of the state machine. */
16765 /* lh->include_dirs and lh->file_names are 0-based, but the
16766 directory and file name numbers in the statement program
16767 are 1-based. */
16768 struct file_entry *fe = &lh->file_names[file - 1];
16769 const char *dir = NULL;
16770
16771 if (fe->dir_index)
16772 dir = lh->include_dirs[fe->dir_index - 1];
16773
16774 dwarf2_start_subfile (fe->name, dir, comp_dir);
16775 }
16776
16777 /* Decode the table. */
16778 while (!end_sequence)
16779 {
16780 op_code = read_1_byte (abfd, line_ptr);
16781 line_ptr += 1;
16782 if (line_ptr > line_end)
16783 {
16784 dwarf2_debug_line_missing_end_sequence_complaint ();
16785 break;
16786 }
16787
16788 if (op_code >= lh->opcode_base)
16789 {
16790 /* Special operand. */
16791 adj_opcode = op_code - lh->opcode_base;
16792 address += (((op_index + (adj_opcode / lh->line_range))
16793 / lh->maximum_ops_per_instruction)
16794 * lh->minimum_instruction_length);
16795 op_index = ((op_index + (adj_opcode / lh->line_range))
16796 % lh->maximum_ops_per_instruction);
16797 line += lh->line_base + (adj_opcode % lh->line_range);
16798 if (lh->num_file_names < file || file == 0)
16799 dwarf2_debug_line_missing_file_complaint ();
16800 /* For now we ignore lines not starting on an
16801 instruction boundary. */
16802 else if (op_index == 0)
16803 {
16804 lh->file_names[file - 1].included_p = 1;
16805 if (!decode_for_pst_p && is_stmt)
16806 {
16807 if (last_subfile != current_subfile)
16808 {
16809 addr = gdbarch_addr_bits_remove (gdbarch, address);
16810 if (last_subfile)
16811 (*p_record_line) (last_subfile, 0, addr);
16812 last_subfile = current_subfile;
16813 }
16814 /* Append row to matrix using current values. */
16815 addr = gdbarch_addr_bits_remove (gdbarch, address);
16816 (*p_record_line) (current_subfile, line, addr);
16817 }
16818 }
16819 basic_block = 0;
16820 }
16821 else switch (op_code)
16822 {
16823 case DW_LNS_extended_op:
16824 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16825 &bytes_read);
16826 line_ptr += bytes_read;
16827 extended_end = line_ptr + extended_len;
16828 extended_op = read_1_byte (abfd, line_ptr);
16829 line_ptr += 1;
16830 switch (extended_op)
16831 {
16832 case DW_LNE_end_sequence:
16833 p_record_line = record_line;
16834 end_sequence = 1;
16835 break;
16836 case DW_LNE_set_address:
16837 address = read_address (abfd, line_ptr, cu, &bytes_read);
16838
16839 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16840 {
16841 /* This line table is for a function which has been
16842 GCd by the linker. Ignore it. PR gdb/12528 */
16843
16844 long line_offset
16845 = line_ptr - get_debug_line_section (cu)->buffer;
16846
16847 complaint (&symfile_complaints,
16848 _(".debug_line address at offset 0x%lx is 0 "
16849 "[in module %s]"),
16850 line_offset, objfile_name (objfile));
16851 p_record_line = noop_record_line;
16852 }
16853
16854 op_index = 0;
16855 line_ptr += bytes_read;
16856 address += baseaddr;
16857 break;
16858 case DW_LNE_define_file:
16859 {
16860 const char *cur_file;
16861 unsigned int dir_index, mod_time, length;
16862
16863 cur_file = read_direct_string (abfd, line_ptr,
16864 &bytes_read);
16865 line_ptr += bytes_read;
16866 dir_index =
16867 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16868 line_ptr += bytes_read;
16869 mod_time =
16870 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16871 line_ptr += bytes_read;
16872 length =
16873 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16874 line_ptr += bytes_read;
16875 add_file_name (lh, cur_file, dir_index, mod_time, length);
16876 }
16877 break;
16878 case DW_LNE_set_discriminator:
16879 /* The discriminator is not interesting to the debugger;
16880 just ignore it. */
16881 line_ptr = extended_end;
16882 break;
16883 default:
16884 complaint (&symfile_complaints,
16885 _("mangled .debug_line section"));
16886 return;
16887 }
16888 /* Make sure that we parsed the extended op correctly. If e.g.
16889 we expected a different address size than the producer used,
16890 we may have read the wrong number of bytes. */
16891 if (line_ptr != extended_end)
16892 {
16893 complaint (&symfile_complaints,
16894 _("mangled .debug_line section"));
16895 return;
16896 }
16897 break;
16898 case DW_LNS_copy:
16899 if (lh->num_file_names < file || file == 0)
16900 dwarf2_debug_line_missing_file_complaint ();
16901 else
16902 {
16903 lh->file_names[file - 1].included_p = 1;
16904 if (!decode_for_pst_p && is_stmt)
16905 {
16906 if (last_subfile != current_subfile)
16907 {
16908 addr = gdbarch_addr_bits_remove (gdbarch, address);
16909 if (last_subfile)
16910 (*p_record_line) (last_subfile, 0, addr);
16911 last_subfile = current_subfile;
16912 }
16913 addr = gdbarch_addr_bits_remove (gdbarch, address);
16914 (*p_record_line) (current_subfile, line, addr);
16915 }
16916 }
16917 basic_block = 0;
16918 break;
16919 case DW_LNS_advance_pc:
16920 {
16921 CORE_ADDR adjust
16922 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16923
16924 address += (((op_index + adjust)
16925 / lh->maximum_ops_per_instruction)
16926 * lh->minimum_instruction_length);
16927 op_index = ((op_index + adjust)
16928 % lh->maximum_ops_per_instruction);
16929 line_ptr += bytes_read;
16930 }
16931 break;
16932 case DW_LNS_advance_line:
16933 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16934 line_ptr += bytes_read;
16935 break;
16936 case DW_LNS_set_file:
16937 {
16938 /* The arrays lh->include_dirs and lh->file_names are
16939 0-based, but the directory and file name numbers in
16940 the statement program are 1-based. */
16941 struct file_entry *fe;
16942 const char *dir = NULL;
16943
16944 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16945 line_ptr += bytes_read;
16946 if (lh->num_file_names < file || file == 0)
16947 dwarf2_debug_line_missing_file_complaint ();
16948 else
16949 {
16950 fe = &lh->file_names[file - 1];
16951 if (fe->dir_index)
16952 dir = lh->include_dirs[fe->dir_index - 1];
16953 if (!decode_for_pst_p)
16954 {
16955 last_subfile = current_subfile;
16956 dwarf2_start_subfile (fe->name, dir, comp_dir);
16957 }
16958 }
16959 }
16960 break;
16961 case DW_LNS_set_column:
16962 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16963 line_ptr += bytes_read;
16964 break;
16965 case DW_LNS_negate_stmt:
16966 is_stmt = (!is_stmt);
16967 break;
16968 case DW_LNS_set_basic_block:
16969 basic_block = 1;
16970 break;
16971 /* Add to the address register of the state machine the
16972 address increment value corresponding to special opcode
16973 255. I.e., this value is scaled by the minimum
16974 instruction length since special opcode 255 would have
16975 scaled the increment. */
16976 case DW_LNS_const_add_pc:
16977 {
16978 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16979
16980 address += (((op_index + adjust)
16981 / lh->maximum_ops_per_instruction)
16982 * lh->minimum_instruction_length);
16983 op_index = ((op_index + adjust)
16984 % lh->maximum_ops_per_instruction);
16985 }
16986 break;
16987 case DW_LNS_fixed_advance_pc:
16988 address += read_2_bytes (abfd, line_ptr);
16989 op_index = 0;
16990 line_ptr += 2;
16991 break;
16992 default:
16993 {
16994 /* Unknown standard opcode, ignore it. */
16995 int i;
16996
16997 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16998 {
16999 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17000 line_ptr += bytes_read;
17001 }
17002 }
17003 }
17004 }
17005 if (lh->num_file_names < file || file == 0)
17006 dwarf2_debug_line_missing_file_complaint ();
17007 else
17008 {
17009 lh->file_names[file - 1].included_p = 1;
17010 if (!decode_for_pst_p)
17011 {
17012 addr = gdbarch_addr_bits_remove (gdbarch, address);
17013 (*p_record_line) (current_subfile, 0, addr);
17014 }
17015 }
17016 }
17017 }
17018
17019 /* Decode the Line Number Program (LNP) for the given line_header
17020 structure and CU. The actual information extracted and the type
17021 of structures created from the LNP depends on the value of PST.
17022
17023 1. If PST is NULL, then this procedure uses the data from the program
17024 to create all necessary symbol tables, and their linetables.
17025
17026 2. If PST is not NULL, this procedure reads the program to determine
17027 the list of files included by the unit represented by PST, and
17028 builds all the associated partial symbol tables.
17029
17030 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17031 It is used for relative paths in the line table.
17032 NOTE: When processing partial symtabs (pst != NULL),
17033 comp_dir == pst->dirname.
17034
17035 NOTE: It is important that psymtabs have the same file name (via strcmp)
17036 as the corresponding symtab. Since COMP_DIR is not used in the name of the
17037 symtab we don't use it in the name of the psymtabs we create.
17038 E.g. expand_line_sal requires this when finding psymtabs to expand.
17039 A good testcase for this is mb-inline.exp. */
17040
17041 static void
17042 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
17043 struct dwarf2_cu *cu, struct partial_symtab *pst,
17044 int want_line_info)
17045 {
17046 struct objfile *objfile = cu->objfile;
17047 const int decode_for_pst_p = (pst != NULL);
17048 struct subfile *first_subfile = current_subfile;
17049
17050 if (want_line_info)
17051 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
17052
17053 if (decode_for_pst_p)
17054 {
17055 int file_index;
17056
17057 /* Now that we're done scanning the Line Header Program, we can
17058 create the psymtab of each included file. */
17059 for (file_index = 0; file_index < lh->num_file_names; file_index++)
17060 if (lh->file_names[file_index].included_p == 1)
17061 {
17062 const char *include_name =
17063 psymtab_include_file_name (lh, file_index, pst, comp_dir);
17064 if (include_name != NULL)
17065 dwarf2_create_include_psymtab (include_name, pst, objfile);
17066 }
17067 }
17068 else
17069 {
17070 /* Make sure a symtab is created for every file, even files
17071 which contain only variables (i.e. no code with associated
17072 line numbers). */
17073 int i;
17074
17075 for (i = 0; i < lh->num_file_names; i++)
17076 {
17077 const char *dir = NULL;
17078 struct file_entry *fe;
17079
17080 fe = &lh->file_names[i];
17081 if (fe->dir_index)
17082 dir = lh->include_dirs[fe->dir_index - 1];
17083 dwarf2_start_subfile (fe->name, dir, comp_dir);
17084
17085 /* Skip the main file; we don't need it, and it must be
17086 allocated last, so that it will show up before the
17087 non-primary symtabs in the objfile's symtab list. */
17088 if (current_subfile == first_subfile)
17089 continue;
17090
17091 if (current_subfile->symtab == NULL)
17092 current_subfile->symtab = allocate_symtab (current_subfile->name,
17093 objfile);
17094 fe->symtab = current_subfile->symtab;
17095 }
17096 }
17097 }
17098
17099 /* Start a subfile for DWARF. FILENAME is the name of the file and
17100 DIRNAME the name of the source directory which contains FILENAME
17101 or NULL if not known. COMP_DIR is the compilation directory for the
17102 linetable's compilation unit or NULL if not known.
17103 This routine tries to keep line numbers from identical absolute and
17104 relative file names in a common subfile.
17105
17106 Using the `list' example from the GDB testsuite, which resides in
17107 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
17108 of /srcdir/list0.c yields the following debugging information for list0.c:
17109
17110 DW_AT_name: /srcdir/list0.c
17111 DW_AT_comp_dir: /compdir
17112 files.files[0].name: list0.h
17113 files.files[0].dir: /srcdir
17114 files.files[1].name: list0.c
17115 files.files[1].dir: /srcdir
17116
17117 The line number information for list0.c has to end up in a single
17118 subfile, so that `break /srcdir/list0.c:1' works as expected.
17119 start_subfile will ensure that this happens provided that we pass the
17120 concatenation of files.files[1].dir and files.files[1].name as the
17121 subfile's name. */
17122
17123 static void
17124 dwarf2_start_subfile (const char *filename, const char *dirname,
17125 const char *comp_dir)
17126 {
17127 char *copy = NULL;
17128
17129 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
17130 `start_symtab' will always pass the contents of DW_AT_comp_dir as
17131 second argument to start_subfile. To be consistent, we do the
17132 same here. In order not to lose the line information directory,
17133 we concatenate it to the filename when it makes sense.
17134 Note that the Dwarf3 standard says (speaking of filenames in line
17135 information): ``The directory index is ignored for file names
17136 that represent full path names''. Thus ignoring dirname in the
17137 `else' branch below isn't an issue. */
17138
17139 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
17140 {
17141 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
17142 filename = copy;
17143 }
17144
17145 start_subfile (filename, comp_dir);
17146
17147 if (copy != NULL)
17148 xfree (copy);
17149 }
17150
17151 /* Start a symtab for DWARF.
17152 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
17153
17154 static void
17155 dwarf2_start_symtab (struct dwarf2_cu *cu,
17156 const char *name, const char *comp_dir, CORE_ADDR low_pc)
17157 {
17158 start_symtab (name, comp_dir, low_pc);
17159 record_debugformat ("DWARF 2");
17160 record_producer (cu->producer);
17161
17162 /* We assume that we're processing GCC output. */
17163 processing_gcc_compilation = 2;
17164
17165 cu->processing_has_namespace_info = 0;
17166 }
17167
17168 static void
17169 var_decode_location (struct attribute *attr, struct symbol *sym,
17170 struct dwarf2_cu *cu)
17171 {
17172 struct objfile *objfile = cu->objfile;
17173 struct comp_unit_head *cu_header = &cu->header;
17174
17175 /* NOTE drow/2003-01-30: There used to be a comment and some special
17176 code here to turn a symbol with DW_AT_external and a
17177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
17178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
17179 with some versions of binutils) where shared libraries could have
17180 relocations against symbols in their debug information - the
17181 minimal symbol would have the right address, but the debug info
17182 would not. It's no longer necessary, because we will explicitly
17183 apply relocations when we read in the debug information now. */
17184
17185 /* A DW_AT_location attribute with no contents indicates that a
17186 variable has been optimized away. */
17187 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
17188 {
17189 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
17190 return;
17191 }
17192
17193 /* Handle one degenerate form of location expression specially, to
17194 preserve GDB's previous behavior when section offsets are
17195 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
17196 then mark this symbol as LOC_STATIC. */
17197
17198 if (attr_form_is_block (attr)
17199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
17200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
17201 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
17202 && (DW_BLOCK (attr)->size
17203 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
17204 {
17205 unsigned int dummy;
17206
17207 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
17208 SYMBOL_VALUE_ADDRESS (sym) =
17209 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
17210 else
17211 SYMBOL_VALUE_ADDRESS (sym) =
17212 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
17213 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
17214 fixup_symbol_section (sym, objfile);
17215 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
17216 SYMBOL_SECTION (sym));
17217 return;
17218 }
17219
17220 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
17221 expression evaluator, and use LOC_COMPUTED only when necessary
17222 (i.e. when the value of a register or memory location is
17223 referenced, or a thread-local block, etc.). Then again, it might
17224 not be worthwhile. I'm assuming that it isn't unless performance
17225 or memory numbers show me otherwise. */
17226
17227 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
17228
17229 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
17230 cu->has_loclist = 1;
17231 }
17232
17233 /* Given a pointer to a DWARF information entry, figure out if we need
17234 to make a symbol table entry for it, and if so, create a new entry
17235 and return a pointer to it.
17236 If TYPE is NULL, determine symbol type from the die, otherwise
17237 used the passed type.
17238 If SPACE is not NULL, use it to hold the new symbol. If it is
17239 NULL, allocate a new symbol on the objfile's obstack. */
17240
17241 static struct symbol *
17242 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
17243 struct symbol *space)
17244 {
17245 struct objfile *objfile = cu->objfile;
17246 struct symbol *sym = NULL;
17247 const char *name;
17248 struct attribute *attr = NULL;
17249 struct attribute *attr2 = NULL;
17250 CORE_ADDR baseaddr;
17251 struct pending **list_to_add = NULL;
17252
17253 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
17254
17255 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17256
17257 name = dwarf2_name (die, cu);
17258 if (name)
17259 {
17260 const char *linkagename;
17261 int suppress_add = 0;
17262
17263 if (space)
17264 sym = space;
17265 else
17266 sym = allocate_symbol (objfile);
17267 OBJSTAT (objfile, n_syms++);
17268
17269 /* Cache this symbol's name and the name's demangled form (if any). */
17270 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
17271 linkagename = dwarf2_physname (name, die, cu);
17272 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
17273
17274 /* Fortran does not have mangling standard and the mangling does differ
17275 between gfortran, iFort etc. */
17276 if (cu->language == language_fortran
17277 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
17278 symbol_set_demangled_name (&(sym->ginfo),
17279 dwarf2_full_name (name, die, cu),
17280 NULL);
17281
17282 /* Default assumptions.
17283 Use the passed type or decode it from the die. */
17284 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17285 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
17286 if (type != NULL)
17287 SYMBOL_TYPE (sym) = type;
17288 else
17289 SYMBOL_TYPE (sym) = die_type (die, cu);
17290 attr = dwarf2_attr (die,
17291 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
17292 cu);
17293 if (attr)
17294 {
17295 SYMBOL_LINE (sym) = DW_UNSND (attr);
17296 }
17297
17298 attr = dwarf2_attr (die,
17299 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
17300 cu);
17301 if (attr)
17302 {
17303 int file_index = DW_UNSND (attr);
17304
17305 if (cu->line_header == NULL
17306 || file_index > cu->line_header->num_file_names)
17307 complaint (&symfile_complaints,
17308 _("file index out of range"));
17309 else if (file_index > 0)
17310 {
17311 struct file_entry *fe;
17312
17313 fe = &cu->line_header->file_names[file_index - 1];
17314 SYMBOL_SYMTAB (sym) = fe->symtab;
17315 }
17316 }
17317
17318 switch (die->tag)
17319 {
17320 case DW_TAG_label:
17321 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
17322 if (attr)
17323 {
17324 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
17325 }
17326 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
17327 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
17328 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
17329 add_symbol_to_list (sym, cu->list_in_scope);
17330 break;
17331 case DW_TAG_subprogram:
17332 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17333 finish_block. */
17334 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
17335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
17336 if ((attr2 && (DW_UNSND (attr2) != 0))
17337 || cu->language == language_ada)
17338 {
17339 /* Subprograms marked external are stored as a global symbol.
17340 Ada subprograms, whether marked external or not, are always
17341 stored as a global symbol, because we want to be able to
17342 access them globally. For instance, we want to be able
17343 to break on a nested subprogram without having to
17344 specify the context. */
17345 list_to_add = &global_symbols;
17346 }
17347 else
17348 {
17349 list_to_add = cu->list_in_scope;
17350 }
17351 break;
17352 case DW_TAG_inlined_subroutine:
17353 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17354 finish_block. */
17355 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
17356 SYMBOL_INLINED (sym) = 1;
17357 list_to_add = cu->list_in_scope;
17358 break;
17359 case DW_TAG_template_value_param:
17360 suppress_add = 1;
17361 /* Fall through. */
17362 case DW_TAG_constant:
17363 case DW_TAG_variable:
17364 case DW_TAG_member:
17365 /* Compilation with minimal debug info may result in
17366 variables with missing type entries. Change the
17367 misleading `void' type to something sensible. */
17368 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
17369 SYMBOL_TYPE (sym)
17370 = objfile_type (objfile)->nodebug_data_symbol;
17371
17372 attr = dwarf2_attr (die, DW_AT_const_value, cu);
17373 /* In the case of DW_TAG_member, we should only be called for
17374 static const members. */
17375 if (die->tag == DW_TAG_member)
17376 {
17377 /* dwarf2_add_field uses die_is_declaration,
17378 so we do the same. */
17379 gdb_assert (die_is_declaration (die, cu));
17380 gdb_assert (attr);
17381 }
17382 if (attr)
17383 {
17384 dwarf2_const_value (attr, sym, cu);
17385 attr2 = dwarf2_attr (die, DW_AT_external, cu);
17386 if (!suppress_add)
17387 {
17388 if (attr2 && (DW_UNSND (attr2) != 0))
17389 list_to_add = &global_symbols;
17390 else
17391 list_to_add = cu->list_in_scope;
17392 }
17393 break;
17394 }
17395 attr = dwarf2_attr (die, DW_AT_location, cu);
17396 if (attr)
17397 {
17398 var_decode_location (attr, sym, cu);
17399 attr2 = dwarf2_attr (die, DW_AT_external, cu);
17400
17401 /* Fortran explicitly imports any global symbols to the local
17402 scope by DW_TAG_common_block. */
17403 if (cu->language == language_fortran && die->parent
17404 && die->parent->tag == DW_TAG_common_block)
17405 attr2 = NULL;
17406
17407 if (SYMBOL_CLASS (sym) == LOC_STATIC
17408 && SYMBOL_VALUE_ADDRESS (sym) == 0
17409 && !dwarf2_per_objfile->has_section_at_zero)
17410 {
17411 /* When a static variable is eliminated by the linker,
17412 the corresponding debug information is not stripped
17413 out, but the variable address is set to null;
17414 do not add such variables into symbol table. */
17415 }
17416 else if (attr2 && (DW_UNSND (attr2) != 0))
17417 {
17418 /* Workaround gfortran PR debug/40040 - it uses
17419 DW_AT_location for variables in -fPIC libraries which may
17420 get overriden by other libraries/executable and get
17421 a different address. Resolve it by the minimal symbol
17422 which may come from inferior's executable using copy
17423 relocation. Make this workaround only for gfortran as for
17424 other compilers GDB cannot guess the minimal symbol
17425 Fortran mangling kind. */
17426 if (cu->language == language_fortran && die->parent
17427 && die->parent->tag == DW_TAG_module
17428 && cu->producer
17429 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
17430 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
17431
17432 /* A variable with DW_AT_external is never static,
17433 but it may be block-scoped. */
17434 list_to_add = (cu->list_in_scope == &file_symbols
17435 ? &global_symbols : cu->list_in_scope);
17436 }
17437 else
17438 list_to_add = cu->list_in_scope;
17439 }
17440 else
17441 {
17442 /* We do not know the address of this symbol.
17443 If it is an external symbol and we have type information
17444 for it, enter the symbol as a LOC_UNRESOLVED symbol.
17445 The address of the variable will then be determined from
17446 the minimal symbol table whenever the variable is
17447 referenced. */
17448 attr2 = dwarf2_attr (die, DW_AT_external, cu);
17449
17450 /* Fortran explicitly imports any global symbols to the local
17451 scope by DW_TAG_common_block. */
17452 if (cu->language == language_fortran && die->parent
17453 && die->parent->tag == DW_TAG_common_block)
17454 {
17455 /* SYMBOL_CLASS doesn't matter here because
17456 read_common_block is going to reset it. */
17457 if (!suppress_add)
17458 list_to_add = cu->list_in_scope;
17459 }
17460 else if (attr2 && (DW_UNSND (attr2) != 0)
17461 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
17462 {
17463 /* A variable with DW_AT_external is never static, but it
17464 may be block-scoped. */
17465 list_to_add = (cu->list_in_scope == &file_symbols
17466 ? &global_symbols : cu->list_in_scope);
17467
17468 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
17469 }
17470 else if (!die_is_declaration (die, cu))
17471 {
17472 /* Use the default LOC_OPTIMIZED_OUT class. */
17473 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
17474 if (!suppress_add)
17475 list_to_add = cu->list_in_scope;
17476 }
17477 }
17478 break;
17479 case DW_TAG_formal_parameter:
17480 /* If we are inside a function, mark this as an argument. If
17481 not, we might be looking at an argument to an inlined function
17482 when we do not have enough information to show inlined frames;
17483 pretend it's a local variable in that case so that the user can
17484 still see it. */
17485 if (context_stack_depth > 0
17486 && context_stack[context_stack_depth - 1].name != NULL)
17487 SYMBOL_IS_ARGUMENT (sym) = 1;
17488 attr = dwarf2_attr (die, DW_AT_location, cu);
17489 if (attr)
17490 {
17491 var_decode_location (attr, sym, cu);
17492 }
17493 attr = dwarf2_attr (die, DW_AT_const_value, cu);
17494 if (attr)
17495 {
17496 dwarf2_const_value (attr, sym, cu);
17497 }
17498
17499 list_to_add = cu->list_in_scope;
17500 break;
17501 case DW_TAG_unspecified_parameters:
17502 /* From varargs functions; gdb doesn't seem to have any
17503 interest in this information, so just ignore it for now.
17504 (FIXME?) */
17505 break;
17506 case DW_TAG_template_type_param:
17507 suppress_add = 1;
17508 /* Fall through. */
17509 case DW_TAG_class_type:
17510 case DW_TAG_interface_type:
17511 case DW_TAG_structure_type:
17512 case DW_TAG_union_type:
17513 case DW_TAG_set_type:
17514 case DW_TAG_enumeration_type:
17515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
17517
17518 {
17519 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
17520 really ever be static objects: otherwise, if you try
17521 to, say, break of a class's method and you're in a file
17522 which doesn't mention that class, it won't work unless
17523 the check for all static symbols in lookup_symbol_aux
17524 saves you. See the OtherFileClass tests in
17525 gdb.c++/namespace.exp. */
17526
17527 if (!suppress_add)
17528 {
17529 list_to_add = (cu->list_in_scope == &file_symbols
17530 && (cu->language == language_cplus
17531 || cu->language == language_java)
17532 ? &global_symbols : cu->list_in_scope);
17533
17534 /* The semantics of C++ state that "struct foo {
17535 ... }" also defines a typedef for "foo". A Java
17536 class declaration also defines a typedef for the
17537 class. */
17538 if (cu->language == language_cplus
17539 || cu->language == language_java
17540 || cu->language == language_ada)
17541 {
17542 /* The symbol's name is already allocated along
17543 with this objfile, so we don't need to
17544 duplicate it for the type. */
17545 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
17546 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
17547 }
17548 }
17549 }
17550 break;
17551 case DW_TAG_typedef:
17552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17553 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17554 list_to_add = cu->list_in_scope;
17555 break;
17556 case DW_TAG_base_type:
17557 case DW_TAG_subrange_type:
17558 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17559 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17560 list_to_add = cu->list_in_scope;
17561 break;
17562 case DW_TAG_enumerator:
17563 attr = dwarf2_attr (die, DW_AT_const_value, cu);
17564 if (attr)
17565 {
17566 dwarf2_const_value (attr, sym, cu);
17567 }
17568 {
17569 /* NOTE: carlton/2003-11-10: See comment above in the
17570 DW_TAG_class_type, etc. block. */
17571
17572 list_to_add = (cu->list_in_scope == &file_symbols
17573 && (cu->language == language_cplus
17574 || cu->language == language_java)
17575 ? &global_symbols : cu->list_in_scope);
17576 }
17577 break;
17578 case DW_TAG_namespace:
17579 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17580 list_to_add = &global_symbols;
17581 break;
17582 case DW_TAG_common_block:
17583 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
17584 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
17585 add_symbol_to_list (sym, cu->list_in_scope);
17586 break;
17587 default:
17588 /* Not a tag we recognize. Hopefully we aren't processing
17589 trash data, but since we must specifically ignore things
17590 we don't recognize, there is nothing else we should do at
17591 this point. */
17592 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
17593 dwarf_tag_name (die->tag));
17594 break;
17595 }
17596
17597 if (suppress_add)
17598 {
17599 sym->hash_next = objfile->template_symbols;
17600 objfile->template_symbols = sym;
17601 list_to_add = NULL;
17602 }
17603
17604 if (list_to_add != NULL)
17605 add_symbol_to_list (sym, list_to_add);
17606
17607 /* For the benefit of old versions of GCC, check for anonymous
17608 namespaces based on the demangled name. */
17609 if (!cu->processing_has_namespace_info
17610 && cu->language == language_cplus)
17611 cp_scan_for_anonymous_namespaces (sym, objfile);
17612 }
17613 return (sym);
17614 }
17615
17616 /* A wrapper for new_symbol_full that always allocates a new symbol. */
17617
17618 static struct symbol *
17619 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17620 {
17621 return new_symbol_full (die, type, cu, NULL);
17622 }
17623
17624 /* Given an attr with a DW_FORM_dataN value in host byte order,
17625 zero-extend it as appropriate for the symbol's type. The DWARF
17626 standard (v4) is not entirely clear about the meaning of using
17627 DW_FORM_dataN for a constant with a signed type, where the type is
17628 wider than the data. The conclusion of a discussion on the DWARF
17629 list was that this is unspecified. We choose to always zero-extend
17630 because that is the interpretation long in use by GCC. */
17631
17632 static gdb_byte *
17633 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
17634 struct dwarf2_cu *cu, LONGEST *value, int bits)
17635 {
17636 struct objfile *objfile = cu->objfile;
17637 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
17638 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
17639 LONGEST l = DW_UNSND (attr);
17640
17641 if (bits < sizeof (*value) * 8)
17642 {
17643 l &= ((LONGEST) 1 << bits) - 1;
17644 *value = l;
17645 }
17646 else if (bits == sizeof (*value) * 8)
17647 *value = l;
17648 else
17649 {
17650 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
17651 store_unsigned_integer (bytes, bits / 8, byte_order, l);
17652 return bytes;
17653 }
17654
17655 return NULL;
17656 }
17657
17658 /* Read a constant value from an attribute. Either set *VALUE, or if
17659 the value does not fit in *VALUE, set *BYTES - either already
17660 allocated on the objfile obstack, or newly allocated on OBSTACK,
17661 or, set *BATON, if we translated the constant to a location
17662 expression. */
17663
17664 static void
17665 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
17666 const char *name, struct obstack *obstack,
17667 struct dwarf2_cu *cu,
17668 LONGEST *value, const gdb_byte **bytes,
17669 struct dwarf2_locexpr_baton **baton)
17670 {
17671 struct objfile *objfile = cu->objfile;
17672 struct comp_unit_head *cu_header = &cu->header;
17673 struct dwarf_block *blk;
17674 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
17675 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
17676
17677 *value = 0;
17678 *bytes = NULL;
17679 *baton = NULL;
17680
17681 switch (attr->form)
17682 {
17683 case DW_FORM_addr:
17684 case DW_FORM_GNU_addr_index:
17685 {
17686 gdb_byte *data;
17687
17688 if (TYPE_LENGTH (type) != cu_header->addr_size)
17689 dwarf2_const_value_length_mismatch_complaint (name,
17690 cu_header->addr_size,
17691 TYPE_LENGTH (type));
17692 /* Symbols of this form are reasonably rare, so we just
17693 piggyback on the existing location code rather than writing
17694 a new implementation of symbol_computed_ops. */
17695 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
17696 (*baton)->per_cu = cu->per_cu;
17697 gdb_assert ((*baton)->per_cu);
17698
17699 (*baton)->size = 2 + cu_header->addr_size;
17700 data = obstack_alloc (obstack, (*baton)->size);
17701 (*baton)->data = data;
17702
17703 data[0] = DW_OP_addr;
17704 store_unsigned_integer (&data[1], cu_header->addr_size,
17705 byte_order, DW_ADDR (attr));
17706 data[cu_header->addr_size + 1] = DW_OP_stack_value;
17707 }
17708 break;
17709 case DW_FORM_string:
17710 case DW_FORM_strp:
17711 case DW_FORM_GNU_str_index:
17712 case DW_FORM_GNU_strp_alt:
17713 /* DW_STRING is already allocated on the objfile obstack, point
17714 directly to it. */
17715 *bytes = (const gdb_byte *) DW_STRING (attr);
17716 break;
17717 case DW_FORM_block1:
17718 case DW_FORM_block2:
17719 case DW_FORM_block4:
17720 case DW_FORM_block:
17721 case DW_FORM_exprloc:
17722 blk = DW_BLOCK (attr);
17723 if (TYPE_LENGTH (type) != blk->size)
17724 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
17725 TYPE_LENGTH (type));
17726 *bytes = blk->data;
17727 break;
17728
17729 /* The DW_AT_const_value attributes are supposed to carry the
17730 symbol's value "represented as it would be on the target
17731 architecture." By the time we get here, it's already been
17732 converted to host endianness, so we just need to sign- or
17733 zero-extend it as appropriate. */
17734 case DW_FORM_data1:
17735 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
17736 break;
17737 case DW_FORM_data2:
17738 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
17739 break;
17740 case DW_FORM_data4:
17741 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
17742 break;
17743 case DW_FORM_data8:
17744 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
17745 break;
17746
17747 case DW_FORM_sdata:
17748 *value = DW_SND (attr);
17749 break;
17750
17751 case DW_FORM_udata:
17752 *value = DW_UNSND (attr);
17753 break;
17754
17755 default:
17756 complaint (&symfile_complaints,
17757 _("unsupported const value attribute form: '%s'"),
17758 dwarf_form_name (attr->form));
17759 *value = 0;
17760 break;
17761 }
17762 }
17763
17764
17765 /* Copy constant value from an attribute to a symbol. */
17766
17767 static void
17768 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
17769 struct dwarf2_cu *cu)
17770 {
17771 struct objfile *objfile = cu->objfile;
17772 struct comp_unit_head *cu_header = &cu->header;
17773 LONGEST value;
17774 const gdb_byte *bytes;
17775 struct dwarf2_locexpr_baton *baton;
17776
17777 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17778 SYMBOL_PRINT_NAME (sym),
17779 &objfile->objfile_obstack, cu,
17780 &value, &bytes, &baton);
17781
17782 if (baton != NULL)
17783 {
17784 SYMBOL_LOCATION_BATON (sym) = baton;
17785 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17786 }
17787 else if (bytes != NULL)
17788 {
17789 SYMBOL_VALUE_BYTES (sym) = bytes;
17790 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
17791 }
17792 else
17793 {
17794 SYMBOL_VALUE (sym) = value;
17795 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
17796 }
17797 }
17798
17799 /* Return the type of the die in question using its DW_AT_type attribute. */
17800
17801 static struct type *
17802 die_type (struct die_info *die, struct dwarf2_cu *cu)
17803 {
17804 struct attribute *type_attr;
17805
17806 type_attr = dwarf2_attr (die, DW_AT_type, cu);
17807 if (!type_attr)
17808 {
17809 /* A missing DW_AT_type represents a void type. */
17810 return objfile_type (cu->objfile)->builtin_void;
17811 }
17812
17813 return lookup_die_type (die, type_attr, cu);
17814 }
17815
17816 /* True iff CU's producer generates GNAT Ada auxiliary information
17817 that allows to find parallel types through that information instead
17818 of having to do expensive parallel lookups by type name. */
17819
17820 static int
17821 need_gnat_info (struct dwarf2_cu *cu)
17822 {
17823 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17824 of GNAT produces this auxiliary information, without any indication
17825 that it is produced. Part of enhancing the FSF version of GNAT
17826 to produce that information will be to put in place an indicator
17827 that we can use in order to determine whether the descriptive type
17828 info is available or not. One suggestion that has been made is
17829 to use a new attribute, attached to the CU die. For now, assume
17830 that the descriptive type info is not available. */
17831 return 0;
17832 }
17833
17834 /* Return the auxiliary type of the die in question using its
17835 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17836 attribute is not present. */
17837
17838 static struct type *
17839 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17840 {
17841 struct attribute *type_attr;
17842
17843 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17844 if (!type_attr)
17845 return NULL;
17846
17847 return lookup_die_type (die, type_attr, cu);
17848 }
17849
17850 /* If DIE has a descriptive_type attribute, then set the TYPE's
17851 descriptive type accordingly. */
17852
17853 static void
17854 set_descriptive_type (struct type *type, struct die_info *die,
17855 struct dwarf2_cu *cu)
17856 {
17857 struct type *descriptive_type = die_descriptive_type (die, cu);
17858
17859 if (descriptive_type)
17860 {
17861 ALLOCATE_GNAT_AUX_TYPE (type);
17862 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17863 }
17864 }
17865
17866 /* Return the containing type of the die in question using its
17867 DW_AT_containing_type attribute. */
17868
17869 static struct type *
17870 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17871 {
17872 struct attribute *type_attr;
17873
17874 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17875 if (!type_attr)
17876 error (_("Dwarf Error: Problem turning containing type into gdb type "
17877 "[in module %s]"), objfile_name (cu->objfile));
17878
17879 return lookup_die_type (die, type_attr, cu);
17880 }
17881
17882 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17883
17884 static struct type *
17885 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17886 {
17887 struct objfile *objfile = dwarf2_per_objfile->objfile;
17888 char *message, *saved;
17889
17890 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17891 objfile_name (objfile),
17892 cu->header.offset.sect_off,
17893 die->offset.sect_off);
17894 saved = obstack_copy0 (&objfile->objfile_obstack,
17895 message, strlen (message));
17896 xfree (message);
17897
17898 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17899 }
17900
17901 /* Look up the type of DIE in CU using its type attribute ATTR.
17902 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17903 DW_AT_containing_type.
17904 If there is no type substitute an error marker. */
17905
17906 static struct type *
17907 lookup_die_type (struct die_info *die, const struct attribute *attr,
17908 struct dwarf2_cu *cu)
17909 {
17910 struct objfile *objfile = cu->objfile;
17911 struct type *this_type;
17912
17913 gdb_assert (attr->name == DW_AT_type
17914 || attr->name == DW_AT_GNAT_descriptive_type
17915 || attr->name == DW_AT_containing_type);
17916
17917 /* First see if we have it cached. */
17918
17919 if (attr->form == DW_FORM_GNU_ref_alt)
17920 {
17921 struct dwarf2_per_cu_data *per_cu;
17922 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17923
17924 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17925 this_type = get_die_type_at_offset (offset, per_cu);
17926 }
17927 else if (attr_form_is_ref (attr))
17928 {
17929 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17930
17931 this_type = get_die_type_at_offset (offset, cu->per_cu);
17932 }
17933 else if (attr->form == DW_FORM_ref_sig8)
17934 {
17935 ULONGEST signature = DW_SIGNATURE (attr);
17936
17937 return get_signatured_type (die, signature, cu);
17938 }
17939 else
17940 {
17941 complaint (&symfile_complaints,
17942 _("Dwarf Error: Bad type attribute %s in DIE"
17943 " at 0x%x [in module %s]"),
17944 dwarf_attr_name (attr->name), die->offset.sect_off,
17945 objfile_name (objfile));
17946 return build_error_marker_type (cu, die);
17947 }
17948
17949 /* If not cached we need to read it in. */
17950
17951 if (this_type == NULL)
17952 {
17953 struct die_info *type_die = NULL;
17954 struct dwarf2_cu *type_cu = cu;
17955
17956 if (attr_form_is_ref (attr))
17957 type_die = follow_die_ref (die, attr, &type_cu);
17958 if (type_die == NULL)
17959 return build_error_marker_type (cu, die);
17960 /* If we find the type now, it's probably because the type came
17961 from an inter-CU reference and the type's CU got expanded before
17962 ours. */
17963 this_type = read_type_die (type_die, type_cu);
17964 }
17965
17966 /* If we still don't have a type use an error marker. */
17967
17968 if (this_type == NULL)
17969 return build_error_marker_type (cu, die);
17970
17971 return this_type;
17972 }
17973
17974 /* Return the type in DIE, CU.
17975 Returns NULL for invalid types.
17976
17977 This first does a lookup in die_type_hash,
17978 and only reads the die in if necessary.
17979
17980 NOTE: This can be called when reading in partial or full symbols. */
17981
17982 static struct type *
17983 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17984 {
17985 struct type *this_type;
17986
17987 this_type = get_die_type (die, cu);
17988 if (this_type)
17989 return this_type;
17990
17991 return read_type_die_1 (die, cu);
17992 }
17993
17994 /* Read the type in DIE, CU.
17995 Returns NULL for invalid types. */
17996
17997 static struct type *
17998 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17999 {
18000 struct type *this_type = NULL;
18001
18002 switch (die->tag)
18003 {
18004 case DW_TAG_class_type:
18005 case DW_TAG_interface_type:
18006 case DW_TAG_structure_type:
18007 case DW_TAG_union_type:
18008 this_type = read_structure_type (die, cu);
18009 break;
18010 case DW_TAG_enumeration_type:
18011 this_type = read_enumeration_type (die, cu);
18012 break;
18013 case DW_TAG_subprogram:
18014 case DW_TAG_subroutine_type:
18015 case DW_TAG_inlined_subroutine:
18016 this_type = read_subroutine_type (die, cu);
18017 break;
18018 case DW_TAG_array_type:
18019 this_type = read_array_type (die, cu);
18020 break;
18021 case DW_TAG_set_type:
18022 this_type = read_set_type (die, cu);
18023 break;
18024 case DW_TAG_pointer_type:
18025 this_type = read_tag_pointer_type (die, cu);
18026 break;
18027 case DW_TAG_ptr_to_member_type:
18028 this_type = read_tag_ptr_to_member_type (die, cu);
18029 break;
18030 case DW_TAG_reference_type:
18031 this_type = read_tag_reference_type (die, cu);
18032 break;
18033 case DW_TAG_const_type:
18034 this_type = read_tag_const_type (die, cu);
18035 break;
18036 case DW_TAG_volatile_type:
18037 this_type = read_tag_volatile_type (die, cu);
18038 break;
18039 case DW_TAG_restrict_type:
18040 this_type = read_tag_restrict_type (die, cu);
18041 break;
18042 case DW_TAG_string_type:
18043 this_type = read_tag_string_type (die, cu);
18044 break;
18045 case DW_TAG_typedef:
18046 this_type = read_typedef (die, cu);
18047 break;
18048 case DW_TAG_subrange_type:
18049 this_type = read_subrange_type (die, cu);
18050 break;
18051 case DW_TAG_base_type:
18052 this_type = read_base_type (die, cu);
18053 break;
18054 case DW_TAG_unspecified_type:
18055 this_type = read_unspecified_type (die, cu);
18056 break;
18057 case DW_TAG_namespace:
18058 this_type = read_namespace_type (die, cu);
18059 break;
18060 case DW_TAG_module:
18061 this_type = read_module_type (die, cu);
18062 break;
18063 default:
18064 complaint (&symfile_complaints,
18065 _("unexpected tag in read_type_die: '%s'"),
18066 dwarf_tag_name (die->tag));
18067 break;
18068 }
18069
18070 return this_type;
18071 }
18072
18073 /* See if we can figure out if the class lives in a namespace. We do
18074 this by looking for a member function; its demangled name will
18075 contain namespace info, if there is any.
18076 Return the computed name or NULL.
18077 Space for the result is allocated on the objfile's obstack.
18078 This is the full-die version of guess_partial_die_structure_name.
18079 In this case we know DIE has no useful parent. */
18080
18081 static char *
18082 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
18083 {
18084 struct die_info *spec_die;
18085 struct dwarf2_cu *spec_cu;
18086 struct die_info *child;
18087
18088 spec_cu = cu;
18089 spec_die = die_specification (die, &spec_cu);
18090 if (spec_die != NULL)
18091 {
18092 die = spec_die;
18093 cu = spec_cu;
18094 }
18095
18096 for (child = die->child;
18097 child != NULL;
18098 child = child->sibling)
18099 {
18100 if (child->tag == DW_TAG_subprogram)
18101 {
18102 struct attribute *attr;
18103
18104 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
18105 if (attr == NULL)
18106 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
18107 if (attr != NULL)
18108 {
18109 char *actual_name
18110 = language_class_name_from_physname (cu->language_defn,
18111 DW_STRING (attr));
18112 char *name = NULL;
18113
18114 if (actual_name != NULL)
18115 {
18116 const char *die_name = dwarf2_name (die, cu);
18117
18118 if (die_name != NULL
18119 && strcmp (die_name, actual_name) != 0)
18120 {
18121 /* Strip off the class name from the full name.
18122 We want the prefix. */
18123 int die_name_len = strlen (die_name);
18124 int actual_name_len = strlen (actual_name);
18125
18126 /* Test for '::' as a sanity check. */
18127 if (actual_name_len > die_name_len + 2
18128 && actual_name[actual_name_len
18129 - die_name_len - 1] == ':')
18130 name =
18131 obstack_copy0 (&cu->objfile->objfile_obstack,
18132 actual_name,
18133 actual_name_len - die_name_len - 2);
18134 }
18135 }
18136 xfree (actual_name);
18137 return name;
18138 }
18139 }
18140 }
18141
18142 return NULL;
18143 }
18144
18145 /* GCC might emit a nameless typedef that has a linkage name. Determine the
18146 prefix part in such case. See
18147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18148
18149 static char *
18150 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
18151 {
18152 struct attribute *attr;
18153 char *base;
18154
18155 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
18156 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
18157 return NULL;
18158
18159 attr = dwarf2_attr (die, DW_AT_name, cu);
18160 if (attr != NULL && DW_STRING (attr) != NULL)
18161 return NULL;
18162
18163 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18164 if (attr == NULL)
18165 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18166 if (attr == NULL || DW_STRING (attr) == NULL)
18167 return NULL;
18168
18169 /* dwarf2_name had to be already called. */
18170 gdb_assert (DW_STRING_IS_CANONICAL (attr));
18171
18172 /* Strip the base name, keep any leading namespaces/classes. */
18173 base = strrchr (DW_STRING (attr), ':');
18174 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
18175 return "";
18176
18177 return obstack_copy0 (&cu->objfile->objfile_obstack,
18178 DW_STRING (attr), &base[-1] - DW_STRING (attr));
18179 }
18180
18181 /* Return the name of the namespace/class that DIE is defined within,
18182 or "" if we can't tell. The caller should not xfree the result.
18183
18184 For example, if we're within the method foo() in the following
18185 code:
18186
18187 namespace N {
18188 class C {
18189 void foo () {
18190 }
18191 };
18192 }
18193
18194 then determine_prefix on foo's die will return "N::C". */
18195
18196 static const char *
18197 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
18198 {
18199 struct die_info *parent, *spec_die;
18200 struct dwarf2_cu *spec_cu;
18201 struct type *parent_type;
18202 char *retval;
18203
18204 if (cu->language != language_cplus && cu->language != language_java
18205 && cu->language != language_fortran)
18206 return "";
18207
18208 retval = anonymous_struct_prefix (die, cu);
18209 if (retval)
18210 return retval;
18211
18212 /* We have to be careful in the presence of DW_AT_specification.
18213 For example, with GCC 3.4, given the code
18214
18215 namespace N {
18216 void foo() {
18217 // Definition of N::foo.
18218 }
18219 }
18220
18221 then we'll have a tree of DIEs like this:
18222
18223 1: DW_TAG_compile_unit
18224 2: DW_TAG_namespace // N
18225 3: DW_TAG_subprogram // declaration of N::foo
18226 4: DW_TAG_subprogram // definition of N::foo
18227 DW_AT_specification // refers to die #3
18228
18229 Thus, when processing die #4, we have to pretend that we're in
18230 the context of its DW_AT_specification, namely the contex of die
18231 #3. */
18232 spec_cu = cu;
18233 spec_die = die_specification (die, &spec_cu);
18234 if (spec_die == NULL)
18235 parent = die->parent;
18236 else
18237 {
18238 parent = spec_die->parent;
18239 cu = spec_cu;
18240 }
18241
18242 if (parent == NULL)
18243 return "";
18244 else if (parent->building_fullname)
18245 {
18246 const char *name;
18247 const char *parent_name;
18248
18249 /* It has been seen on RealView 2.2 built binaries,
18250 DW_TAG_template_type_param types actually _defined_ as
18251 children of the parent class:
18252
18253 enum E {};
18254 template class <class Enum> Class{};
18255 Class<enum E> class_e;
18256
18257 1: DW_TAG_class_type (Class)
18258 2: DW_TAG_enumeration_type (E)
18259 3: DW_TAG_enumerator (enum1:0)
18260 3: DW_TAG_enumerator (enum2:1)
18261 ...
18262 2: DW_TAG_template_type_param
18263 DW_AT_type DW_FORM_ref_udata (E)
18264
18265 Besides being broken debug info, it can put GDB into an
18266 infinite loop. Consider:
18267
18268 When we're building the full name for Class<E>, we'll start
18269 at Class, and go look over its template type parameters,
18270 finding E. We'll then try to build the full name of E, and
18271 reach here. We're now trying to build the full name of E,
18272 and look over the parent DIE for containing scope. In the
18273 broken case, if we followed the parent DIE of E, we'd again
18274 find Class, and once again go look at its template type
18275 arguments, etc., etc. Simply don't consider such parent die
18276 as source-level parent of this die (it can't be, the language
18277 doesn't allow it), and break the loop here. */
18278 name = dwarf2_name (die, cu);
18279 parent_name = dwarf2_name (parent, cu);
18280 complaint (&symfile_complaints,
18281 _("template param type '%s' defined within parent '%s'"),
18282 name ? name : "<unknown>",
18283 parent_name ? parent_name : "<unknown>");
18284 return "";
18285 }
18286 else
18287 switch (parent->tag)
18288 {
18289 case DW_TAG_namespace:
18290 parent_type = read_type_die (parent, cu);
18291 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
18292 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
18293 Work around this problem here. */
18294 if (cu->language == language_cplus
18295 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
18296 return "";
18297 /* We give a name to even anonymous namespaces. */
18298 return TYPE_TAG_NAME (parent_type);
18299 case DW_TAG_class_type:
18300 case DW_TAG_interface_type:
18301 case DW_TAG_structure_type:
18302 case DW_TAG_union_type:
18303 case DW_TAG_module:
18304 parent_type = read_type_die (parent, cu);
18305 if (TYPE_TAG_NAME (parent_type) != NULL)
18306 return TYPE_TAG_NAME (parent_type);
18307 else
18308 /* An anonymous structure is only allowed non-static data
18309 members; no typedefs, no member functions, et cetera.
18310 So it does not need a prefix. */
18311 return "";
18312 case DW_TAG_compile_unit:
18313 case DW_TAG_partial_unit:
18314 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
18315 if (cu->language == language_cplus
18316 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18317 && die->child != NULL
18318 && (die->tag == DW_TAG_class_type
18319 || die->tag == DW_TAG_structure_type
18320 || die->tag == DW_TAG_union_type))
18321 {
18322 char *name = guess_full_die_structure_name (die, cu);
18323 if (name != NULL)
18324 return name;
18325 }
18326 return "";
18327 default:
18328 return determine_prefix (parent, cu);
18329 }
18330 }
18331
18332 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
18333 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
18334 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
18335 an obconcat, otherwise allocate storage for the result. The CU argument is
18336 used to determine the language and hence, the appropriate separator. */
18337
18338 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
18339
18340 static char *
18341 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
18342 int physname, struct dwarf2_cu *cu)
18343 {
18344 const char *lead = "";
18345 const char *sep;
18346
18347 if (suffix == NULL || suffix[0] == '\0'
18348 || prefix == NULL || prefix[0] == '\0')
18349 sep = "";
18350 else if (cu->language == language_java)
18351 sep = ".";
18352 else if (cu->language == language_fortran && physname)
18353 {
18354 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
18355 DW_AT_MIPS_linkage_name is preferred and used instead. */
18356
18357 lead = "__";
18358 sep = "_MOD_";
18359 }
18360 else
18361 sep = "::";
18362
18363 if (prefix == NULL)
18364 prefix = "";
18365 if (suffix == NULL)
18366 suffix = "";
18367
18368 if (obs == NULL)
18369 {
18370 char *retval
18371 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
18372
18373 strcpy (retval, lead);
18374 strcat (retval, prefix);
18375 strcat (retval, sep);
18376 strcat (retval, suffix);
18377 return retval;
18378 }
18379 else
18380 {
18381 /* We have an obstack. */
18382 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
18383 }
18384 }
18385
18386 /* Return sibling of die, NULL if no sibling. */
18387
18388 static struct die_info *
18389 sibling_die (struct die_info *die)
18390 {
18391 return die->sibling;
18392 }
18393
18394 /* Get name of a die, return NULL if not found. */
18395
18396 static const char *
18397 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
18398 struct obstack *obstack)
18399 {
18400 if (name && cu->language == language_cplus)
18401 {
18402 char *canon_name = cp_canonicalize_string (name);
18403
18404 if (canon_name != NULL)
18405 {
18406 if (strcmp (canon_name, name) != 0)
18407 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
18408 xfree (canon_name);
18409 }
18410 }
18411
18412 return name;
18413 }
18414
18415 /* Get name of a die, return NULL if not found. */
18416
18417 static const char *
18418 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
18419 {
18420 struct attribute *attr;
18421
18422 attr = dwarf2_attr (die, DW_AT_name, cu);
18423 if ((!attr || !DW_STRING (attr))
18424 && die->tag != DW_TAG_class_type
18425 && die->tag != DW_TAG_interface_type
18426 && die->tag != DW_TAG_structure_type
18427 && die->tag != DW_TAG_union_type)
18428 return NULL;
18429
18430 switch (die->tag)
18431 {
18432 case DW_TAG_compile_unit:
18433 case DW_TAG_partial_unit:
18434 /* Compilation units have a DW_AT_name that is a filename, not
18435 a source language identifier. */
18436 case DW_TAG_enumeration_type:
18437 case DW_TAG_enumerator:
18438 /* These tags always have simple identifiers already; no need
18439 to canonicalize them. */
18440 return DW_STRING (attr);
18441
18442 case DW_TAG_subprogram:
18443 /* Java constructors will all be named "<init>", so return
18444 the class name when we see this special case. */
18445 if (cu->language == language_java
18446 && DW_STRING (attr) != NULL
18447 && strcmp (DW_STRING (attr), "<init>") == 0)
18448 {
18449 struct dwarf2_cu *spec_cu = cu;
18450 struct die_info *spec_die;
18451
18452 /* GCJ will output '<init>' for Java constructor names.
18453 For this special case, return the name of the parent class. */
18454
18455 /* GCJ may output suprogram DIEs with AT_specification set.
18456 If so, use the name of the specified DIE. */
18457 spec_die = die_specification (die, &spec_cu);
18458 if (spec_die != NULL)
18459 return dwarf2_name (spec_die, spec_cu);
18460
18461 do
18462 {
18463 die = die->parent;
18464 if (die->tag == DW_TAG_class_type)
18465 return dwarf2_name (die, cu);
18466 }
18467 while (die->tag != DW_TAG_compile_unit
18468 && die->tag != DW_TAG_partial_unit);
18469 }
18470 break;
18471
18472 case DW_TAG_class_type:
18473 case DW_TAG_interface_type:
18474 case DW_TAG_structure_type:
18475 case DW_TAG_union_type:
18476 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
18477 structures or unions. These were of the form "._%d" in GCC 4.1,
18478 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
18479 and GCC 4.4. We work around this problem by ignoring these. */
18480 if (attr && DW_STRING (attr)
18481 && (strncmp (DW_STRING (attr), "._", 2) == 0
18482 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
18483 return NULL;
18484
18485 /* GCC might emit a nameless typedef that has a linkage name. See
18486 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18487 if (!attr || DW_STRING (attr) == NULL)
18488 {
18489 char *demangled = NULL;
18490
18491 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18492 if (attr == NULL)
18493 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18494
18495 if (attr == NULL || DW_STRING (attr) == NULL)
18496 return NULL;
18497
18498 /* Avoid demangling DW_STRING (attr) the second time on a second
18499 call for the same DIE. */
18500 if (!DW_STRING_IS_CANONICAL (attr))
18501 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
18502
18503 if (demangled)
18504 {
18505 char *base;
18506
18507 /* FIXME: we already did this for the partial symbol... */
18508 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
18509 demangled, strlen (demangled));
18510 DW_STRING_IS_CANONICAL (attr) = 1;
18511 xfree (demangled);
18512
18513 /* Strip any leading namespaces/classes, keep only the base name.
18514 DW_AT_name for named DIEs does not contain the prefixes. */
18515 base = strrchr (DW_STRING (attr), ':');
18516 if (base && base > DW_STRING (attr) && base[-1] == ':')
18517 return &base[1];
18518 else
18519 return DW_STRING (attr);
18520 }
18521 }
18522 break;
18523
18524 default:
18525 break;
18526 }
18527
18528 if (!DW_STRING_IS_CANONICAL (attr))
18529 {
18530 DW_STRING (attr)
18531 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
18532 &cu->objfile->objfile_obstack);
18533 DW_STRING_IS_CANONICAL (attr) = 1;
18534 }
18535 return DW_STRING (attr);
18536 }
18537
18538 /* Return the die that this die in an extension of, or NULL if there
18539 is none. *EXT_CU is the CU containing DIE on input, and the CU
18540 containing the return value on output. */
18541
18542 static struct die_info *
18543 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
18544 {
18545 struct attribute *attr;
18546
18547 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
18548 if (attr == NULL)
18549 return NULL;
18550
18551 return follow_die_ref (die, attr, ext_cu);
18552 }
18553
18554 /* Convert a DIE tag into its string name. */
18555
18556 static const char *
18557 dwarf_tag_name (unsigned tag)
18558 {
18559 const char *name = get_DW_TAG_name (tag);
18560
18561 if (name == NULL)
18562 return "DW_TAG_<unknown>";
18563
18564 return name;
18565 }
18566
18567 /* Convert a DWARF attribute code into its string name. */
18568
18569 static const char *
18570 dwarf_attr_name (unsigned attr)
18571 {
18572 const char *name;
18573
18574 #ifdef MIPS /* collides with DW_AT_HP_block_index */
18575 if (attr == DW_AT_MIPS_fde)
18576 return "DW_AT_MIPS_fde";
18577 #else
18578 if (attr == DW_AT_HP_block_index)
18579 return "DW_AT_HP_block_index";
18580 #endif
18581
18582 name = get_DW_AT_name (attr);
18583
18584 if (name == NULL)
18585 return "DW_AT_<unknown>";
18586
18587 return name;
18588 }
18589
18590 /* Convert a DWARF value form code into its string name. */
18591
18592 static const char *
18593 dwarf_form_name (unsigned form)
18594 {
18595 const char *name = get_DW_FORM_name (form);
18596
18597 if (name == NULL)
18598 return "DW_FORM_<unknown>";
18599
18600 return name;
18601 }
18602
18603 static char *
18604 dwarf_bool_name (unsigned mybool)
18605 {
18606 if (mybool)
18607 return "TRUE";
18608 else
18609 return "FALSE";
18610 }
18611
18612 /* Convert a DWARF type code into its string name. */
18613
18614 static const char *
18615 dwarf_type_encoding_name (unsigned enc)
18616 {
18617 const char *name = get_DW_ATE_name (enc);
18618
18619 if (name == NULL)
18620 return "DW_ATE_<unknown>";
18621
18622 return name;
18623 }
18624
18625 static void
18626 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
18627 {
18628 unsigned int i;
18629
18630 print_spaces (indent, f);
18631 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
18632 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
18633
18634 if (die->parent != NULL)
18635 {
18636 print_spaces (indent, f);
18637 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
18638 die->parent->offset.sect_off);
18639 }
18640
18641 print_spaces (indent, f);
18642 fprintf_unfiltered (f, " has children: %s\n",
18643 dwarf_bool_name (die->child != NULL));
18644
18645 print_spaces (indent, f);
18646 fprintf_unfiltered (f, " attributes:\n");
18647
18648 for (i = 0; i < die->num_attrs; ++i)
18649 {
18650 print_spaces (indent, f);
18651 fprintf_unfiltered (f, " %s (%s) ",
18652 dwarf_attr_name (die->attrs[i].name),
18653 dwarf_form_name (die->attrs[i].form));
18654
18655 switch (die->attrs[i].form)
18656 {
18657 case DW_FORM_addr:
18658 case DW_FORM_GNU_addr_index:
18659 fprintf_unfiltered (f, "address: ");
18660 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
18661 break;
18662 case DW_FORM_block2:
18663 case DW_FORM_block4:
18664 case DW_FORM_block:
18665 case DW_FORM_block1:
18666 fprintf_unfiltered (f, "block: size %s",
18667 pulongest (DW_BLOCK (&die->attrs[i])->size));
18668 break;
18669 case DW_FORM_exprloc:
18670 fprintf_unfiltered (f, "expression: size %s",
18671 pulongest (DW_BLOCK (&die->attrs[i])->size));
18672 break;
18673 case DW_FORM_ref_addr:
18674 fprintf_unfiltered (f, "ref address: ");
18675 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18676 break;
18677 case DW_FORM_GNU_ref_alt:
18678 fprintf_unfiltered (f, "alt ref address: ");
18679 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18680 break;
18681 case DW_FORM_ref1:
18682 case DW_FORM_ref2:
18683 case DW_FORM_ref4:
18684 case DW_FORM_ref8:
18685 case DW_FORM_ref_udata:
18686 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
18687 (long) (DW_UNSND (&die->attrs[i])));
18688 break;
18689 case DW_FORM_data1:
18690 case DW_FORM_data2:
18691 case DW_FORM_data4:
18692 case DW_FORM_data8:
18693 case DW_FORM_udata:
18694 case DW_FORM_sdata:
18695 fprintf_unfiltered (f, "constant: %s",
18696 pulongest (DW_UNSND (&die->attrs[i])));
18697 break;
18698 case DW_FORM_sec_offset:
18699 fprintf_unfiltered (f, "section offset: %s",
18700 pulongest (DW_UNSND (&die->attrs[i])));
18701 break;
18702 case DW_FORM_ref_sig8:
18703 fprintf_unfiltered (f, "signature: %s",
18704 hex_string (DW_SIGNATURE (&die->attrs[i])));
18705 break;
18706 case DW_FORM_string:
18707 case DW_FORM_strp:
18708 case DW_FORM_GNU_str_index:
18709 case DW_FORM_GNU_strp_alt:
18710 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
18711 DW_STRING (&die->attrs[i])
18712 ? DW_STRING (&die->attrs[i]) : "",
18713 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
18714 break;
18715 case DW_FORM_flag:
18716 if (DW_UNSND (&die->attrs[i]))
18717 fprintf_unfiltered (f, "flag: TRUE");
18718 else
18719 fprintf_unfiltered (f, "flag: FALSE");
18720 break;
18721 case DW_FORM_flag_present:
18722 fprintf_unfiltered (f, "flag: TRUE");
18723 break;
18724 case DW_FORM_indirect:
18725 /* The reader will have reduced the indirect form to
18726 the "base form" so this form should not occur. */
18727 fprintf_unfiltered (f,
18728 "unexpected attribute form: DW_FORM_indirect");
18729 break;
18730 default:
18731 fprintf_unfiltered (f, "unsupported attribute form: %d.",
18732 die->attrs[i].form);
18733 break;
18734 }
18735 fprintf_unfiltered (f, "\n");
18736 }
18737 }
18738
18739 static void
18740 dump_die_for_error (struct die_info *die)
18741 {
18742 dump_die_shallow (gdb_stderr, 0, die);
18743 }
18744
18745 static void
18746 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
18747 {
18748 int indent = level * 4;
18749
18750 gdb_assert (die != NULL);
18751
18752 if (level >= max_level)
18753 return;
18754
18755 dump_die_shallow (f, indent, die);
18756
18757 if (die->child != NULL)
18758 {
18759 print_spaces (indent, f);
18760 fprintf_unfiltered (f, " Children:");
18761 if (level + 1 < max_level)
18762 {
18763 fprintf_unfiltered (f, "\n");
18764 dump_die_1 (f, level + 1, max_level, die->child);
18765 }
18766 else
18767 {
18768 fprintf_unfiltered (f,
18769 " [not printed, max nesting level reached]\n");
18770 }
18771 }
18772
18773 if (die->sibling != NULL && level > 0)
18774 {
18775 dump_die_1 (f, level, max_level, die->sibling);
18776 }
18777 }
18778
18779 /* This is called from the pdie macro in gdbinit.in.
18780 It's not static so gcc will keep a copy callable from gdb. */
18781
18782 void
18783 dump_die (struct die_info *die, int max_level)
18784 {
18785 dump_die_1 (gdb_stdlog, 0, max_level, die);
18786 }
18787
18788 static void
18789 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
18790 {
18791 void **slot;
18792
18793 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18794 INSERT);
18795
18796 *slot = die;
18797 }
18798
18799 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18800 required kind. */
18801
18802 static sect_offset
18803 dwarf2_get_ref_die_offset (const struct attribute *attr)
18804 {
18805 sect_offset retval = { DW_UNSND (attr) };
18806
18807 if (attr_form_is_ref (attr))
18808 return retval;
18809
18810 retval.sect_off = 0;
18811 complaint (&symfile_complaints,
18812 _("unsupported die ref attribute form: '%s'"),
18813 dwarf_form_name (attr->form));
18814 return retval;
18815 }
18816
18817 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18818 * the value held by the attribute is not constant. */
18819
18820 static LONGEST
18821 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
18822 {
18823 if (attr->form == DW_FORM_sdata)
18824 return DW_SND (attr);
18825 else if (attr->form == DW_FORM_udata
18826 || attr->form == DW_FORM_data1
18827 || attr->form == DW_FORM_data2
18828 || attr->form == DW_FORM_data4
18829 || attr->form == DW_FORM_data8)
18830 return DW_UNSND (attr);
18831 else
18832 {
18833 complaint (&symfile_complaints,
18834 _("Attribute value is not a constant (%s)"),
18835 dwarf_form_name (attr->form));
18836 return default_value;
18837 }
18838 }
18839
18840 /* Follow reference or signature attribute ATTR of SRC_DIE.
18841 On entry *REF_CU is the CU of SRC_DIE.
18842 On exit *REF_CU is the CU of the result. */
18843
18844 static struct die_info *
18845 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18846 struct dwarf2_cu **ref_cu)
18847 {
18848 struct die_info *die;
18849
18850 if (attr_form_is_ref (attr))
18851 die = follow_die_ref (src_die, attr, ref_cu);
18852 else if (attr->form == DW_FORM_ref_sig8)
18853 die = follow_die_sig (src_die, attr, ref_cu);
18854 else
18855 {
18856 dump_die_for_error (src_die);
18857 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18858 objfile_name ((*ref_cu)->objfile));
18859 }
18860
18861 return die;
18862 }
18863
18864 /* Follow reference OFFSET.
18865 On entry *REF_CU is the CU of the source die referencing OFFSET.
18866 On exit *REF_CU is the CU of the result.
18867 Returns NULL if OFFSET is invalid. */
18868
18869 static struct die_info *
18870 follow_die_offset (sect_offset offset, int offset_in_dwz,
18871 struct dwarf2_cu **ref_cu)
18872 {
18873 struct die_info temp_die;
18874 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18875
18876 gdb_assert (cu->per_cu != NULL);
18877
18878 target_cu = cu;
18879
18880 if (cu->per_cu->is_debug_types)
18881 {
18882 /* .debug_types CUs cannot reference anything outside their CU.
18883 If they need to, they have to reference a signatured type via
18884 DW_FORM_ref_sig8. */
18885 if (! offset_in_cu_p (&cu->header, offset))
18886 return NULL;
18887 }
18888 else if (offset_in_dwz != cu->per_cu->is_dwz
18889 || ! offset_in_cu_p (&cu->header, offset))
18890 {
18891 struct dwarf2_per_cu_data *per_cu;
18892
18893 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18894 cu->objfile);
18895
18896 /* If necessary, add it to the queue and load its DIEs. */
18897 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18898 load_full_comp_unit (per_cu, cu->language);
18899
18900 target_cu = per_cu->cu;
18901 }
18902 else if (cu->dies == NULL)
18903 {
18904 /* We're loading full DIEs during partial symbol reading. */
18905 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18906 load_full_comp_unit (cu->per_cu, language_minimal);
18907 }
18908
18909 *ref_cu = target_cu;
18910 temp_die.offset = offset;
18911 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18912 }
18913
18914 /* Follow reference attribute ATTR of SRC_DIE.
18915 On entry *REF_CU is the CU of SRC_DIE.
18916 On exit *REF_CU is the CU of the result. */
18917
18918 static struct die_info *
18919 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18920 struct dwarf2_cu **ref_cu)
18921 {
18922 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18923 struct dwarf2_cu *cu = *ref_cu;
18924 struct die_info *die;
18925
18926 die = follow_die_offset (offset,
18927 (attr->form == DW_FORM_GNU_ref_alt
18928 || cu->per_cu->is_dwz),
18929 ref_cu);
18930 if (!die)
18931 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18932 "at 0x%x [in module %s]"),
18933 offset.sect_off, src_die->offset.sect_off,
18934 objfile_name (cu->objfile));
18935
18936 return die;
18937 }
18938
18939 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18940 Returned value is intended for DW_OP_call*. Returned
18941 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18942
18943 struct dwarf2_locexpr_baton
18944 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18945 struct dwarf2_per_cu_data *per_cu,
18946 CORE_ADDR (*get_frame_pc) (void *baton),
18947 void *baton)
18948 {
18949 struct dwarf2_cu *cu;
18950 struct die_info *die;
18951 struct attribute *attr;
18952 struct dwarf2_locexpr_baton retval;
18953
18954 dw2_setup (per_cu->objfile);
18955
18956 if (per_cu->cu == NULL)
18957 load_cu (per_cu);
18958 cu = per_cu->cu;
18959
18960 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18961 if (!die)
18962 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18963 offset.sect_off, objfile_name (per_cu->objfile));
18964
18965 attr = dwarf2_attr (die, DW_AT_location, cu);
18966 if (!attr)
18967 {
18968 /* DWARF: "If there is no such attribute, then there is no effect.".
18969 DATA is ignored if SIZE is 0. */
18970
18971 retval.data = NULL;
18972 retval.size = 0;
18973 }
18974 else if (attr_form_is_section_offset (attr))
18975 {
18976 struct dwarf2_loclist_baton loclist_baton;
18977 CORE_ADDR pc = (*get_frame_pc) (baton);
18978 size_t size;
18979
18980 fill_in_loclist_baton (cu, &loclist_baton, attr);
18981
18982 retval.data = dwarf2_find_location_expression (&loclist_baton,
18983 &size, pc);
18984 retval.size = size;
18985 }
18986 else
18987 {
18988 if (!attr_form_is_block (attr))
18989 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18990 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18991 offset.sect_off, objfile_name (per_cu->objfile));
18992
18993 retval.data = DW_BLOCK (attr)->data;
18994 retval.size = DW_BLOCK (attr)->size;
18995 }
18996 retval.per_cu = cu->per_cu;
18997
18998 age_cached_comp_units ();
18999
19000 return retval;
19001 }
19002
19003 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
19004 offset. */
19005
19006 struct dwarf2_locexpr_baton
19007 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
19008 struct dwarf2_per_cu_data *per_cu,
19009 CORE_ADDR (*get_frame_pc) (void *baton),
19010 void *baton)
19011 {
19012 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
19013
19014 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
19015 }
19016
19017 /* Write a constant of a given type as target-ordered bytes into
19018 OBSTACK. */
19019
19020 static const gdb_byte *
19021 write_constant_as_bytes (struct obstack *obstack,
19022 enum bfd_endian byte_order,
19023 struct type *type,
19024 ULONGEST value,
19025 LONGEST *len)
19026 {
19027 gdb_byte *result;
19028
19029 *len = TYPE_LENGTH (type);
19030 result = obstack_alloc (obstack, *len);
19031 store_unsigned_integer (result, *len, byte_order, value);
19032
19033 return result;
19034 }
19035
19036 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
19037 pointer to the constant bytes and set LEN to the length of the
19038 data. If memory is needed, allocate it on OBSTACK. If the DIE
19039 does not have a DW_AT_const_value, return NULL. */
19040
19041 const gdb_byte *
19042 dwarf2_fetch_constant_bytes (sect_offset offset,
19043 struct dwarf2_per_cu_data *per_cu,
19044 struct obstack *obstack,
19045 LONGEST *len)
19046 {
19047 struct dwarf2_cu *cu;
19048 struct die_info *die;
19049 struct attribute *attr;
19050 const gdb_byte *result = NULL;
19051 struct type *type;
19052 LONGEST value;
19053 enum bfd_endian byte_order;
19054
19055 dw2_setup (per_cu->objfile);
19056
19057 if (per_cu->cu == NULL)
19058 load_cu (per_cu);
19059 cu = per_cu->cu;
19060
19061 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
19062 if (!die)
19063 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
19064 offset.sect_off, objfile_name (per_cu->objfile));
19065
19066
19067 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19068 if (attr == NULL)
19069 return NULL;
19070
19071 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
19072 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19073
19074 switch (attr->form)
19075 {
19076 case DW_FORM_addr:
19077 case DW_FORM_GNU_addr_index:
19078 {
19079 gdb_byte *tem;
19080
19081 *len = cu->header.addr_size;
19082 tem = obstack_alloc (obstack, *len);
19083 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
19084 result = tem;
19085 }
19086 break;
19087 case DW_FORM_string:
19088 case DW_FORM_strp:
19089 case DW_FORM_GNU_str_index:
19090 case DW_FORM_GNU_strp_alt:
19091 /* DW_STRING is already allocated on the objfile obstack, point
19092 directly to it. */
19093 result = (const gdb_byte *) DW_STRING (attr);
19094 *len = strlen (DW_STRING (attr));
19095 break;
19096 case DW_FORM_block1:
19097 case DW_FORM_block2:
19098 case DW_FORM_block4:
19099 case DW_FORM_block:
19100 case DW_FORM_exprloc:
19101 result = DW_BLOCK (attr)->data;
19102 *len = DW_BLOCK (attr)->size;
19103 break;
19104
19105 /* The DW_AT_const_value attributes are supposed to carry the
19106 symbol's value "represented as it would be on the target
19107 architecture." By the time we get here, it's already been
19108 converted to host endianness, so we just need to sign- or
19109 zero-extend it as appropriate. */
19110 case DW_FORM_data1:
19111 type = die_type (die, cu);
19112 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
19113 if (result == NULL)
19114 result = write_constant_as_bytes (obstack, byte_order,
19115 type, value, len);
19116 break;
19117 case DW_FORM_data2:
19118 type = die_type (die, cu);
19119 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
19120 if (result == NULL)
19121 result = write_constant_as_bytes (obstack, byte_order,
19122 type, value, len);
19123 break;
19124 case DW_FORM_data4:
19125 type = die_type (die, cu);
19126 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
19127 if (result == NULL)
19128 result = write_constant_as_bytes (obstack, byte_order,
19129 type, value, len);
19130 break;
19131 case DW_FORM_data8:
19132 type = die_type (die, cu);
19133 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
19134 if (result == NULL)
19135 result = write_constant_as_bytes (obstack, byte_order,
19136 type, value, len);
19137 break;
19138
19139 case DW_FORM_sdata:
19140 type = die_type (die, cu);
19141 result = write_constant_as_bytes (obstack, byte_order,
19142 type, DW_SND (attr), len);
19143 break;
19144
19145 case DW_FORM_udata:
19146 type = die_type (die, cu);
19147 result = write_constant_as_bytes (obstack, byte_order,
19148 type, DW_UNSND (attr), len);
19149 break;
19150
19151 default:
19152 complaint (&symfile_complaints,
19153 _("unsupported const value attribute form: '%s'"),
19154 dwarf_form_name (attr->form));
19155 break;
19156 }
19157
19158 return result;
19159 }
19160
19161 /* Return the type of the DIE at DIE_OFFSET in the CU named by
19162 PER_CU. */
19163
19164 struct type *
19165 dwarf2_get_die_type (cu_offset die_offset,
19166 struct dwarf2_per_cu_data *per_cu)
19167 {
19168 sect_offset die_offset_sect;
19169
19170 dw2_setup (per_cu->objfile);
19171
19172 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
19173 return get_die_type_at_offset (die_offset_sect, per_cu);
19174 }
19175
19176 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
19177 On entry *REF_CU is the CU of SRC_DIE.
19178 On exit *REF_CU is the CU of the result.
19179 Returns NULL if the referenced DIE isn't found. */
19180
19181 static struct die_info *
19182 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
19183 struct dwarf2_cu **ref_cu)
19184 {
19185 struct objfile *objfile = (*ref_cu)->objfile;
19186 struct die_info temp_die;
19187 struct dwarf2_cu *sig_cu;
19188 struct die_info *die;
19189
19190 /* While it might be nice to assert sig_type->type == NULL here,
19191 we can get here for DW_AT_imported_declaration where we need
19192 the DIE not the type. */
19193
19194 /* If necessary, add it to the queue and load its DIEs. */
19195
19196 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
19197 read_signatured_type (sig_type);
19198
19199 sig_cu = sig_type->per_cu.cu;
19200 gdb_assert (sig_cu != NULL);
19201 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
19202 temp_die.offset = sig_type->type_offset_in_section;
19203 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
19204 temp_die.offset.sect_off);
19205 if (die)
19206 {
19207 /* For .gdb_index version 7 keep track of included TUs.
19208 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
19209 if (dwarf2_per_objfile->index_table != NULL
19210 && dwarf2_per_objfile->index_table->version <= 7)
19211 {
19212 VEC_safe_push (dwarf2_per_cu_ptr,
19213 (*ref_cu)->per_cu->imported_symtabs,
19214 sig_cu->per_cu);
19215 }
19216
19217 *ref_cu = sig_cu;
19218 return die;
19219 }
19220
19221 return NULL;
19222 }
19223
19224 /* Follow signatured type referenced by ATTR in SRC_DIE.
19225 On entry *REF_CU is the CU of SRC_DIE.
19226 On exit *REF_CU is the CU of the result.
19227 The result is the DIE of the type.
19228 If the referenced type cannot be found an error is thrown. */
19229
19230 static struct die_info *
19231 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
19232 struct dwarf2_cu **ref_cu)
19233 {
19234 ULONGEST signature = DW_SIGNATURE (attr);
19235 struct signatured_type *sig_type;
19236 struct die_info *die;
19237
19238 gdb_assert (attr->form == DW_FORM_ref_sig8);
19239
19240 sig_type = lookup_signatured_type (*ref_cu, signature);
19241 /* sig_type will be NULL if the signatured type is missing from
19242 the debug info. */
19243 if (sig_type == NULL)
19244 {
19245 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
19246 " from DIE at 0x%x [in module %s]"),
19247 hex_string (signature), src_die->offset.sect_off,
19248 objfile_name ((*ref_cu)->objfile));
19249 }
19250
19251 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
19252 if (die == NULL)
19253 {
19254 dump_die_for_error (src_die);
19255 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
19256 " from DIE at 0x%x [in module %s]"),
19257 hex_string (signature), src_die->offset.sect_off,
19258 objfile_name ((*ref_cu)->objfile));
19259 }
19260
19261 return die;
19262 }
19263
19264 /* Get the type specified by SIGNATURE referenced in DIE/CU,
19265 reading in and processing the type unit if necessary. */
19266
19267 static struct type *
19268 get_signatured_type (struct die_info *die, ULONGEST signature,
19269 struct dwarf2_cu *cu)
19270 {
19271 struct signatured_type *sig_type;
19272 struct dwarf2_cu *type_cu;
19273 struct die_info *type_die;
19274 struct type *type;
19275
19276 sig_type = lookup_signatured_type (cu, signature);
19277 /* sig_type will be NULL if the signatured type is missing from
19278 the debug info. */
19279 if (sig_type == NULL)
19280 {
19281 complaint (&symfile_complaints,
19282 _("Dwarf Error: Cannot find signatured DIE %s referenced"
19283 " from DIE at 0x%x [in module %s]"),
19284 hex_string (signature), die->offset.sect_off,
19285 objfile_name (dwarf2_per_objfile->objfile));
19286 return build_error_marker_type (cu, die);
19287 }
19288
19289 /* If we already know the type we're done. */
19290 if (sig_type->type != NULL)
19291 return sig_type->type;
19292
19293 type_cu = cu;
19294 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
19295 if (type_die != NULL)
19296 {
19297 /* N.B. We need to call get_die_type to ensure only one type for this DIE
19298 is created. This is important, for example, because for c++ classes
19299 we need TYPE_NAME set which is only done by new_symbol. Blech. */
19300 type = read_type_die (type_die, type_cu);
19301 if (type == NULL)
19302 {
19303 complaint (&symfile_complaints,
19304 _("Dwarf Error: Cannot build signatured type %s"
19305 " referenced from DIE at 0x%x [in module %s]"),
19306 hex_string (signature), die->offset.sect_off,
19307 objfile_name (dwarf2_per_objfile->objfile));
19308 type = build_error_marker_type (cu, die);
19309 }
19310 }
19311 else
19312 {
19313 complaint (&symfile_complaints,
19314 _("Dwarf Error: Problem reading signatured DIE %s referenced"
19315 " from DIE at 0x%x [in module %s]"),
19316 hex_string (signature), die->offset.sect_off,
19317 objfile_name (dwarf2_per_objfile->objfile));
19318 type = build_error_marker_type (cu, die);
19319 }
19320 sig_type->type = type;
19321
19322 return type;
19323 }
19324
19325 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
19326 reading in and processing the type unit if necessary. */
19327
19328 static struct type *
19329 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
19330 struct dwarf2_cu *cu) /* ARI: editCase function */
19331 {
19332 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
19333 if (attr_form_is_ref (attr))
19334 {
19335 struct dwarf2_cu *type_cu = cu;
19336 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
19337
19338 return read_type_die (type_die, type_cu);
19339 }
19340 else if (attr->form == DW_FORM_ref_sig8)
19341 {
19342 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
19343 }
19344 else
19345 {
19346 complaint (&symfile_complaints,
19347 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
19348 " at 0x%x [in module %s]"),
19349 dwarf_form_name (attr->form), die->offset.sect_off,
19350 objfile_name (dwarf2_per_objfile->objfile));
19351 return build_error_marker_type (cu, die);
19352 }
19353 }
19354
19355 /* Load the DIEs associated with type unit PER_CU into memory. */
19356
19357 static void
19358 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
19359 {
19360 struct signatured_type *sig_type;
19361
19362 /* Caller is responsible for ensuring type_unit_groups don't get here. */
19363 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
19364
19365 /* We have the per_cu, but we need the signatured_type.
19366 Fortunately this is an easy translation. */
19367 gdb_assert (per_cu->is_debug_types);
19368 sig_type = (struct signatured_type *) per_cu;
19369
19370 gdb_assert (per_cu->cu == NULL);
19371
19372 read_signatured_type (sig_type);
19373
19374 gdb_assert (per_cu->cu != NULL);
19375 }
19376
19377 /* die_reader_func for read_signatured_type.
19378 This is identical to load_full_comp_unit_reader,
19379 but is kept separate for now. */
19380
19381 static void
19382 read_signatured_type_reader (const struct die_reader_specs *reader,
19383 const gdb_byte *info_ptr,
19384 struct die_info *comp_unit_die,
19385 int has_children,
19386 void *data)
19387 {
19388 struct dwarf2_cu *cu = reader->cu;
19389
19390 gdb_assert (cu->die_hash == NULL);
19391 cu->die_hash =
19392 htab_create_alloc_ex (cu->header.length / 12,
19393 die_hash,
19394 die_eq,
19395 NULL,
19396 &cu->comp_unit_obstack,
19397 hashtab_obstack_allocate,
19398 dummy_obstack_deallocate);
19399
19400 if (has_children)
19401 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
19402 &info_ptr, comp_unit_die);
19403 cu->dies = comp_unit_die;
19404 /* comp_unit_die is not stored in die_hash, no need. */
19405
19406 /* We try not to read any attributes in this function, because not
19407 all CUs needed for references have been loaded yet, and symbol
19408 table processing isn't initialized. But we have to set the CU language,
19409 or we won't be able to build types correctly.
19410 Similarly, if we do not read the producer, we can not apply
19411 producer-specific interpretation. */
19412 prepare_one_comp_unit (cu, cu->dies, language_minimal);
19413 }
19414
19415 /* Read in a signatured type and build its CU and DIEs.
19416 If the type is a stub for the real type in a DWO file,
19417 read in the real type from the DWO file as well. */
19418
19419 static void
19420 read_signatured_type (struct signatured_type *sig_type)
19421 {
19422 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
19423
19424 gdb_assert (per_cu->is_debug_types);
19425 gdb_assert (per_cu->cu == NULL);
19426
19427 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
19428 read_signatured_type_reader, NULL);
19429 sig_type->per_cu.tu_read = 1;
19430 }
19431
19432 /* Decode simple location descriptions.
19433 Given a pointer to a dwarf block that defines a location, compute
19434 the location and return the value.
19435
19436 NOTE drow/2003-11-18: This function is called in two situations
19437 now: for the address of static or global variables (partial symbols
19438 only) and for offsets into structures which are expected to be
19439 (more or less) constant. The partial symbol case should go away,
19440 and only the constant case should remain. That will let this
19441 function complain more accurately. A few special modes are allowed
19442 without complaint for global variables (for instance, global
19443 register values and thread-local values).
19444
19445 A location description containing no operations indicates that the
19446 object is optimized out. The return value is 0 for that case.
19447 FIXME drow/2003-11-16: No callers check for this case any more; soon all
19448 callers will only want a very basic result and this can become a
19449 complaint.
19450
19451 Note that stack[0] is unused except as a default error return. */
19452
19453 static CORE_ADDR
19454 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
19455 {
19456 struct objfile *objfile = cu->objfile;
19457 size_t i;
19458 size_t size = blk->size;
19459 const gdb_byte *data = blk->data;
19460 CORE_ADDR stack[64];
19461 int stacki;
19462 unsigned int bytes_read, unsnd;
19463 gdb_byte op;
19464
19465 i = 0;
19466 stacki = 0;
19467 stack[stacki] = 0;
19468 stack[++stacki] = 0;
19469
19470 while (i < size)
19471 {
19472 op = data[i++];
19473 switch (op)
19474 {
19475 case DW_OP_lit0:
19476 case DW_OP_lit1:
19477 case DW_OP_lit2:
19478 case DW_OP_lit3:
19479 case DW_OP_lit4:
19480 case DW_OP_lit5:
19481 case DW_OP_lit6:
19482 case DW_OP_lit7:
19483 case DW_OP_lit8:
19484 case DW_OP_lit9:
19485 case DW_OP_lit10:
19486 case DW_OP_lit11:
19487 case DW_OP_lit12:
19488 case DW_OP_lit13:
19489 case DW_OP_lit14:
19490 case DW_OP_lit15:
19491 case DW_OP_lit16:
19492 case DW_OP_lit17:
19493 case DW_OP_lit18:
19494 case DW_OP_lit19:
19495 case DW_OP_lit20:
19496 case DW_OP_lit21:
19497 case DW_OP_lit22:
19498 case DW_OP_lit23:
19499 case DW_OP_lit24:
19500 case DW_OP_lit25:
19501 case DW_OP_lit26:
19502 case DW_OP_lit27:
19503 case DW_OP_lit28:
19504 case DW_OP_lit29:
19505 case DW_OP_lit30:
19506 case DW_OP_lit31:
19507 stack[++stacki] = op - DW_OP_lit0;
19508 break;
19509
19510 case DW_OP_reg0:
19511 case DW_OP_reg1:
19512 case DW_OP_reg2:
19513 case DW_OP_reg3:
19514 case DW_OP_reg4:
19515 case DW_OP_reg5:
19516 case DW_OP_reg6:
19517 case DW_OP_reg7:
19518 case DW_OP_reg8:
19519 case DW_OP_reg9:
19520 case DW_OP_reg10:
19521 case DW_OP_reg11:
19522 case DW_OP_reg12:
19523 case DW_OP_reg13:
19524 case DW_OP_reg14:
19525 case DW_OP_reg15:
19526 case DW_OP_reg16:
19527 case DW_OP_reg17:
19528 case DW_OP_reg18:
19529 case DW_OP_reg19:
19530 case DW_OP_reg20:
19531 case DW_OP_reg21:
19532 case DW_OP_reg22:
19533 case DW_OP_reg23:
19534 case DW_OP_reg24:
19535 case DW_OP_reg25:
19536 case DW_OP_reg26:
19537 case DW_OP_reg27:
19538 case DW_OP_reg28:
19539 case DW_OP_reg29:
19540 case DW_OP_reg30:
19541 case DW_OP_reg31:
19542 stack[++stacki] = op - DW_OP_reg0;
19543 if (i < size)
19544 dwarf2_complex_location_expr_complaint ();
19545 break;
19546
19547 case DW_OP_regx:
19548 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
19549 i += bytes_read;
19550 stack[++stacki] = unsnd;
19551 if (i < size)
19552 dwarf2_complex_location_expr_complaint ();
19553 break;
19554
19555 case DW_OP_addr:
19556 stack[++stacki] = read_address (objfile->obfd, &data[i],
19557 cu, &bytes_read);
19558 i += bytes_read;
19559 break;
19560
19561 case DW_OP_const1u:
19562 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
19563 i += 1;
19564 break;
19565
19566 case DW_OP_const1s:
19567 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
19568 i += 1;
19569 break;
19570
19571 case DW_OP_const2u:
19572 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
19573 i += 2;
19574 break;
19575
19576 case DW_OP_const2s:
19577 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
19578 i += 2;
19579 break;
19580
19581 case DW_OP_const4u:
19582 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
19583 i += 4;
19584 break;
19585
19586 case DW_OP_const4s:
19587 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
19588 i += 4;
19589 break;
19590
19591 case DW_OP_const8u:
19592 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
19593 i += 8;
19594 break;
19595
19596 case DW_OP_constu:
19597 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
19598 &bytes_read);
19599 i += bytes_read;
19600 break;
19601
19602 case DW_OP_consts:
19603 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
19604 i += bytes_read;
19605 break;
19606
19607 case DW_OP_dup:
19608 stack[stacki + 1] = stack[stacki];
19609 stacki++;
19610 break;
19611
19612 case DW_OP_plus:
19613 stack[stacki - 1] += stack[stacki];
19614 stacki--;
19615 break;
19616
19617 case DW_OP_plus_uconst:
19618 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
19619 &bytes_read);
19620 i += bytes_read;
19621 break;
19622
19623 case DW_OP_minus:
19624 stack[stacki - 1] -= stack[stacki];
19625 stacki--;
19626 break;
19627
19628 case DW_OP_deref:
19629 /* If we're not the last op, then we definitely can't encode
19630 this using GDB's address_class enum. This is valid for partial
19631 global symbols, although the variable's address will be bogus
19632 in the psymtab. */
19633 if (i < size)
19634 dwarf2_complex_location_expr_complaint ();
19635 break;
19636
19637 case DW_OP_GNU_push_tls_address:
19638 /* The top of the stack has the offset from the beginning
19639 of the thread control block at which the variable is located. */
19640 /* Nothing should follow this operator, so the top of stack would
19641 be returned. */
19642 /* This is valid for partial global symbols, but the variable's
19643 address will be bogus in the psymtab. Make it always at least
19644 non-zero to not look as a variable garbage collected by linker
19645 which have DW_OP_addr 0. */
19646 if (i < size)
19647 dwarf2_complex_location_expr_complaint ();
19648 stack[stacki]++;
19649 break;
19650
19651 case DW_OP_GNU_uninit:
19652 break;
19653
19654 case DW_OP_GNU_addr_index:
19655 case DW_OP_GNU_const_index:
19656 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
19657 &bytes_read);
19658 i += bytes_read;
19659 break;
19660
19661 default:
19662 {
19663 const char *name = get_DW_OP_name (op);
19664
19665 if (name)
19666 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
19667 name);
19668 else
19669 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
19670 op);
19671 }
19672
19673 return (stack[stacki]);
19674 }
19675
19676 /* Enforce maximum stack depth of SIZE-1 to avoid writing
19677 outside of the allocated space. Also enforce minimum>0. */
19678 if (stacki >= ARRAY_SIZE (stack) - 1)
19679 {
19680 complaint (&symfile_complaints,
19681 _("location description stack overflow"));
19682 return 0;
19683 }
19684
19685 if (stacki <= 0)
19686 {
19687 complaint (&symfile_complaints,
19688 _("location description stack underflow"));
19689 return 0;
19690 }
19691 }
19692 return (stack[stacki]);
19693 }
19694
19695 /* memory allocation interface */
19696
19697 static struct dwarf_block *
19698 dwarf_alloc_block (struct dwarf2_cu *cu)
19699 {
19700 struct dwarf_block *blk;
19701
19702 blk = (struct dwarf_block *)
19703 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
19704 return (blk);
19705 }
19706
19707 static struct die_info *
19708 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
19709 {
19710 struct die_info *die;
19711 size_t size = sizeof (struct die_info);
19712
19713 if (num_attrs > 1)
19714 size += (num_attrs - 1) * sizeof (struct attribute);
19715
19716 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
19717 memset (die, 0, sizeof (struct die_info));
19718 return (die);
19719 }
19720
19721 \f
19722 /* Macro support. */
19723
19724 /* Return file name relative to the compilation directory of file number I in
19725 *LH's file name table. The result is allocated using xmalloc; the caller is
19726 responsible for freeing it. */
19727
19728 static char *
19729 file_file_name (int file, struct line_header *lh)
19730 {
19731 /* Is the file number a valid index into the line header's file name
19732 table? Remember that file numbers start with one, not zero. */
19733 if (1 <= file && file <= lh->num_file_names)
19734 {
19735 struct file_entry *fe = &lh->file_names[file - 1];
19736
19737 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
19738 return xstrdup (fe->name);
19739 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
19740 fe->name, NULL);
19741 }
19742 else
19743 {
19744 /* The compiler produced a bogus file number. We can at least
19745 record the macro definitions made in the file, even if we
19746 won't be able to find the file by name. */
19747 char fake_name[80];
19748
19749 xsnprintf (fake_name, sizeof (fake_name),
19750 "<bad macro file number %d>", file);
19751
19752 complaint (&symfile_complaints,
19753 _("bad file number in macro information (%d)"),
19754 file);
19755
19756 return xstrdup (fake_name);
19757 }
19758 }
19759
19760 /* Return the full name of file number I in *LH's file name table.
19761 Use COMP_DIR as the name of the current directory of the
19762 compilation. The result is allocated using xmalloc; the caller is
19763 responsible for freeing it. */
19764 static char *
19765 file_full_name (int file, struct line_header *lh, const char *comp_dir)
19766 {
19767 /* Is the file number a valid index into the line header's file name
19768 table? Remember that file numbers start with one, not zero. */
19769 if (1 <= file && file <= lh->num_file_names)
19770 {
19771 char *relative = file_file_name (file, lh);
19772
19773 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19774 return relative;
19775 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19776 }
19777 else
19778 return file_file_name (file, lh);
19779 }
19780
19781
19782 static struct macro_source_file *
19783 macro_start_file (int file, int line,
19784 struct macro_source_file *current_file,
19785 const char *comp_dir,
19786 struct line_header *lh, struct objfile *objfile)
19787 {
19788 /* File name relative to the compilation directory of this source file. */
19789 char *file_name = file_file_name (file, lh);
19790
19791 if (! current_file)
19792 {
19793 /* Note: We don't create a macro table for this compilation unit
19794 at all until we actually get a filename. */
19795 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19796
19797 /* If we have no current file, then this must be the start_file
19798 directive for the compilation unit's main source file. */
19799 current_file = macro_set_main (macro_table, file_name);
19800 macro_define_special (macro_table);
19801 }
19802 else
19803 current_file = macro_include (current_file, line, file_name);
19804
19805 xfree (file_name);
19806
19807 return current_file;
19808 }
19809
19810
19811 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19812 followed by a null byte. */
19813 static char *
19814 copy_string (const char *buf, int len)
19815 {
19816 char *s = xmalloc (len + 1);
19817
19818 memcpy (s, buf, len);
19819 s[len] = '\0';
19820 return s;
19821 }
19822
19823
19824 static const char *
19825 consume_improper_spaces (const char *p, const char *body)
19826 {
19827 if (*p == ' ')
19828 {
19829 complaint (&symfile_complaints,
19830 _("macro definition contains spaces "
19831 "in formal argument list:\n`%s'"),
19832 body);
19833
19834 while (*p == ' ')
19835 p++;
19836 }
19837
19838 return p;
19839 }
19840
19841
19842 static void
19843 parse_macro_definition (struct macro_source_file *file, int line,
19844 const char *body)
19845 {
19846 const char *p;
19847
19848 /* The body string takes one of two forms. For object-like macro
19849 definitions, it should be:
19850
19851 <macro name> " " <definition>
19852
19853 For function-like macro definitions, it should be:
19854
19855 <macro name> "() " <definition>
19856 or
19857 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19858
19859 Spaces may appear only where explicitly indicated, and in the
19860 <definition>.
19861
19862 The Dwarf 2 spec says that an object-like macro's name is always
19863 followed by a space, but versions of GCC around March 2002 omit
19864 the space when the macro's definition is the empty string.
19865
19866 The Dwarf 2 spec says that there should be no spaces between the
19867 formal arguments in a function-like macro's formal argument list,
19868 but versions of GCC around March 2002 include spaces after the
19869 commas. */
19870
19871
19872 /* Find the extent of the macro name. The macro name is terminated
19873 by either a space or null character (for an object-like macro) or
19874 an opening paren (for a function-like macro). */
19875 for (p = body; *p; p++)
19876 if (*p == ' ' || *p == '(')
19877 break;
19878
19879 if (*p == ' ' || *p == '\0')
19880 {
19881 /* It's an object-like macro. */
19882 int name_len = p - body;
19883 char *name = copy_string (body, name_len);
19884 const char *replacement;
19885
19886 if (*p == ' ')
19887 replacement = body + name_len + 1;
19888 else
19889 {
19890 dwarf2_macro_malformed_definition_complaint (body);
19891 replacement = body + name_len;
19892 }
19893
19894 macro_define_object (file, line, name, replacement);
19895
19896 xfree (name);
19897 }
19898 else if (*p == '(')
19899 {
19900 /* It's a function-like macro. */
19901 char *name = copy_string (body, p - body);
19902 int argc = 0;
19903 int argv_size = 1;
19904 char **argv = xmalloc (argv_size * sizeof (*argv));
19905
19906 p++;
19907
19908 p = consume_improper_spaces (p, body);
19909
19910 /* Parse the formal argument list. */
19911 while (*p && *p != ')')
19912 {
19913 /* Find the extent of the current argument name. */
19914 const char *arg_start = p;
19915
19916 while (*p && *p != ',' && *p != ')' && *p != ' ')
19917 p++;
19918
19919 if (! *p || p == arg_start)
19920 dwarf2_macro_malformed_definition_complaint (body);
19921 else
19922 {
19923 /* Make sure argv has room for the new argument. */
19924 if (argc >= argv_size)
19925 {
19926 argv_size *= 2;
19927 argv = xrealloc (argv, argv_size * sizeof (*argv));
19928 }
19929
19930 argv[argc++] = copy_string (arg_start, p - arg_start);
19931 }
19932
19933 p = consume_improper_spaces (p, body);
19934
19935 /* Consume the comma, if present. */
19936 if (*p == ',')
19937 {
19938 p++;
19939
19940 p = consume_improper_spaces (p, body);
19941 }
19942 }
19943
19944 if (*p == ')')
19945 {
19946 p++;
19947
19948 if (*p == ' ')
19949 /* Perfectly formed definition, no complaints. */
19950 macro_define_function (file, line, name,
19951 argc, (const char **) argv,
19952 p + 1);
19953 else if (*p == '\0')
19954 {
19955 /* Complain, but do define it. */
19956 dwarf2_macro_malformed_definition_complaint (body);
19957 macro_define_function (file, line, name,
19958 argc, (const char **) argv,
19959 p);
19960 }
19961 else
19962 /* Just complain. */
19963 dwarf2_macro_malformed_definition_complaint (body);
19964 }
19965 else
19966 /* Just complain. */
19967 dwarf2_macro_malformed_definition_complaint (body);
19968
19969 xfree (name);
19970 {
19971 int i;
19972
19973 for (i = 0; i < argc; i++)
19974 xfree (argv[i]);
19975 }
19976 xfree (argv);
19977 }
19978 else
19979 dwarf2_macro_malformed_definition_complaint (body);
19980 }
19981
19982 /* Skip some bytes from BYTES according to the form given in FORM.
19983 Returns the new pointer. */
19984
19985 static const gdb_byte *
19986 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19987 enum dwarf_form form,
19988 unsigned int offset_size,
19989 struct dwarf2_section_info *section)
19990 {
19991 unsigned int bytes_read;
19992
19993 switch (form)
19994 {
19995 case DW_FORM_data1:
19996 case DW_FORM_flag:
19997 ++bytes;
19998 break;
19999
20000 case DW_FORM_data2:
20001 bytes += 2;
20002 break;
20003
20004 case DW_FORM_data4:
20005 bytes += 4;
20006 break;
20007
20008 case DW_FORM_data8:
20009 bytes += 8;
20010 break;
20011
20012 case DW_FORM_string:
20013 read_direct_string (abfd, bytes, &bytes_read);
20014 bytes += bytes_read;
20015 break;
20016
20017 case DW_FORM_sec_offset:
20018 case DW_FORM_strp:
20019 case DW_FORM_GNU_strp_alt:
20020 bytes += offset_size;
20021 break;
20022
20023 case DW_FORM_block:
20024 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
20025 bytes += bytes_read;
20026 break;
20027
20028 case DW_FORM_block1:
20029 bytes += 1 + read_1_byte (abfd, bytes);
20030 break;
20031 case DW_FORM_block2:
20032 bytes += 2 + read_2_bytes (abfd, bytes);
20033 break;
20034 case DW_FORM_block4:
20035 bytes += 4 + read_4_bytes (abfd, bytes);
20036 break;
20037
20038 case DW_FORM_sdata:
20039 case DW_FORM_udata:
20040 case DW_FORM_GNU_addr_index:
20041 case DW_FORM_GNU_str_index:
20042 bytes = gdb_skip_leb128 (bytes, buffer_end);
20043 if (bytes == NULL)
20044 {
20045 dwarf2_section_buffer_overflow_complaint (section);
20046 return NULL;
20047 }
20048 break;
20049
20050 default:
20051 {
20052 complain:
20053 complaint (&symfile_complaints,
20054 _("invalid form 0x%x in `%s'"),
20055 form, get_section_name (section));
20056 return NULL;
20057 }
20058 }
20059
20060 return bytes;
20061 }
20062
20063 /* A helper for dwarf_decode_macros that handles skipping an unknown
20064 opcode. Returns an updated pointer to the macro data buffer; or,
20065 on error, issues a complaint and returns NULL. */
20066
20067 static const gdb_byte *
20068 skip_unknown_opcode (unsigned int opcode,
20069 const gdb_byte **opcode_definitions,
20070 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
20071 bfd *abfd,
20072 unsigned int offset_size,
20073 struct dwarf2_section_info *section)
20074 {
20075 unsigned int bytes_read, i;
20076 unsigned long arg;
20077 const gdb_byte *defn;
20078
20079 if (opcode_definitions[opcode] == NULL)
20080 {
20081 complaint (&symfile_complaints,
20082 _("unrecognized DW_MACFINO opcode 0x%x"),
20083 opcode);
20084 return NULL;
20085 }
20086
20087 defn = opcode_definitions[opcode];
20088 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
20089 defn += bytes_read;
20090
20091 for (i = 0; i < arg; ++i)
20092 {
20093 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
20094 section);
20095 if (mac_ptr == NULL)
20096 {
20097 /* skip_form_bytes already issued the complaint. */
20098 return NULL;
20099 }
20100 }
20101
20102 return mac_ptr;
20103 }
20104
20105 /* A helper function which parses the header of a macro section.
20106 If the macro section is the extended (for now called "GNU") type,
20107 then this updates *OFFSET_SIZE. Returns a pointer to just after
20108 the header, or issues a complaint and returns NULL on error. */
20109
20110 static const gdb_byte *
20111 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
20112 bfd *abfd,
20113 const gdb_byte *mac_ptr,
20114 unsigned int *offset_size,
20115 int section_is_gnu)
20116 {
20117 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
20118
20119 if (section_is_gnu)
20120 {
20121 unsigned int version, flags;
20122
20123 version = read_2_bytes (abfd, mac_ptr);
20124 if (version != 4)
20125 {
20126 complaint (&symfile_complaints,
20127 _("unrecognized version `%d' in .debug_macro section"),
20128 version);
20129 return NULL;
20130 }
20131 mac_ptr += 2;
20132
20133 flags = read_1_byte (abfd, mac_ptr);
20134 ++mac_ptr;
20135 *offset_size = (flags & 1) ? 8 : 4;
20136
20137 if ((flags & 2) != 0)
20138 /* We don't need the line table offset. */
20139 mac_ptr += *offset_size;
20140
20141 /* Vendor opcode descriptions. */
20142 if ((flags & 4) != 0)
20143 {
20144 unsigned int i, count;
20145
20146 count = read_1_byte (abfd, mac_ptr);
20147 ++mac_ptr;
20148 for (i = 0; i < count; ++i)
20149 {
20150 unsigned int opcode, bytes_read;
20151 unsigned long arg;
20152
20153 opcode = read_1_byte (abfd, mac_ptr);
20154 ++mac_ptr;
20155 opcode_definitions[opcode] = mac_ptr;
20156 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20157 mac_ptr += bytes_read;
20158 mac_ptr += arg;
20159 }
20160 }
20161 }
20162
20163 return mac_ptr;
20164 }
20165
20166 /* A helper for dwarf_decode_macros that handles the GNU extensions,
20167 including DW_MACRO_GNU_transparent_include. */
20168
20169 static void
20170 dwarf_decode_macro_bytes (bfd *abfd,
20171 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
20172 struct macro_source_file *current_file,
20173 struct line_header *lh, const char *comp_dir,
20174 struct dwarf2_section_info *section,
20175 int section_is_gnu, int section_is_dwz,
20176 unsigned int offset_size,
20177 struct objfile *objfile,
20178 htab_t include_hash)
20179 {
20180 enum dwarf_macro_record_type macinfo_type;
20181 int at_commandline;
20182 const gdb_byte *opcode_definitions[256];
20183
20184 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20185 &offset_size, section_is_gnu);
20186 if (mac_ptr == NULL)
20187 {
20188 /* We already issued a complaint. */
20189 return;
20190 }
20191
20192 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
20193 GDB is still reading the definitions from command line. First
20194 DW_MACINFO_start_file will need to be ignored as it was already executed
20195 to create CURRENT_FILE for the main source holding also the command line
20196 definitions. On first met DW_MACINFO_start_file this flag is reset to
20197 normally execute all the remaining DW_MACINFO_start_file macinfos. */
20198
20199 at_commandline = 1;
20200
20201 do
20202 {
20203 /* Do we at least have room for a macinfo type byte? */
20204 if (mac_ptr >= mac_end)
20205 {
20206 dwarf2_section_buffer_overflow_complaint (section);
20207 break;
20208 }
20209
20210 macinfo_type = read_1_byte (abfd, mac_ptr);
20211 mac_ptr++;
20212
20213 /* Note that we rely on the fact that the corresponding GNU and
20214 DWARF constants are the same. */
20215 switch (macinfo_type)
20216 {
20217 /* A zero macinfo type indicates the end of the macro
20218 information. */
20219 case 0:
20220 break;
20221
20222 case DW_MACRO_GNU_define:
20223 case DW_MACRO_GNU_undef:
20224 case DW_MACRO_GNU_define_indirect:
20225 case DW_MACRO_GNU_undef_indirect:
20226 case DW_MACRO_GNU_define_indirect_alt:
20227 case DW_MACRO_GNU_undef_indirect_alt:
20228 {
20229 unsigned int bytes_read;
20230 int line;
20231 const char *body;
20232 int is_define;
20233
20234 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20235 mac_ptr += bytes_read;
20236
20237 if (macinfo_type == DW_MACRO_GNU_define
20238 || macinfo_type == DW_MACRO_GNU_undef)
20239 {
20240 body = read_direct_string (abfd, mac_ptr, &bytes_read);
20241 mac_ptr += bytes_read;
20242 }
20243 else
20244 {
20245 LONGEST str_offset;
20246
20247 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
20248 mac_ptr += offset_size;
20249
20250 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
20251 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
20252 || section_is_dwz)
20253 {
20254 struct dwz_file *dwz = dwarf2_get_dwz_file ();
20255
20256 body = read_indirect_string_from_dwz (dwz, str_offset);
20257 }
20258 else
20259 body = read_indirect_string_at_offset (abfd, str_offset);
20260 }
20261
20262 is_define = (macinfo_type == DW_MACRO_GNU_define
20263 || macinfo_type == DW_MACRO_GNU_define_indirect
20264 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
20265 if (! current_file)
20266 {
20267 /* DWARF violation as no main source is present. */
20268 complaint (&symfile_complaints,
20269 _("debug info with no main source gives macro %s "
20270 "on line %d: %s"),
20271 is_define ? _("definition") : _("undefinition"),
20272 line, body);
20273 break;
20274 }
20275 if ((line == 0 && !at_commandline)
20276 || (line != 0 && at_commandline))
20277 complaint (&symfile_complaints,
20278 _("debug info gives %s macro %s with %s line %d: %s"),
20279 at_commandline ? _("command-line") : _("in-file"),
20280 is_define ? _("definition") : _("undefinition"),
20281 line == 0 ? _("zero") : _("non-zero"), line, body);
20282
20283 if (is_define)
20284 parse_macro_definition (current_file, line, body);
20285 else
20286 {
20287 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
20288 || macinfo_type == DW_MACRO_GNU_undef_indirect
20289 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
20290 macro_undef (current_file, line, body);
20291 }
20292 }
20293 break;
20294
20295 case DW_MACRO_GNU_start_file:
20296 {
20297 unsigned int bytes_read;
20298 int line, file;
20299
20300 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20301 mac_ptr += bytes_read;
20302 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20303 mac_ptr += bytes_read;
20304
20305 if ((line == 0 && !at_commandline)
20306 || (line != 0 && at_commandline))
20307 complaint (&symfile_complaints,
20308 _("debug info gives source %d included "
20309 "from %s at %s line %d"),
20310 file, at_commandline ? _("command-line") : _("file"),
20311 line == 0 ? _("zero") : _("non-zero"), line);
20312
20313 if (at_commandline)
20314 {
20315 /* This DW_MACRO_GNU_start_file was executed in the
20316 pass one. */
20317 at_commandline = 0;
20318 }
20319 else
20320 current_file = macro_start_file (file, line,
20321 current_file, comp_dir,
20322 lh, objfile);
20323 }
20324 break;
20325
20326 case DW_MACRO_GNU_end_file:
20327 if (! current_file)
20328 complaint (&symfile_complaints,
20329 _("macro debug info has an unmatched "
20330 "`close_file' directive"));
20331 else
20332 {
20333 current_file = current_file->included_by;
20334 if (! current_file)
20335 {
20336 enum dwarf_macro_record_type next_type;
20337
20338 /* GCC circa March 2002 doesn't produce the zero
20339 type byte marking the end of the compilation
20340 unit. Complain if it's not there, but exit no
20341 matter what. */
20342
20343 /* Do we at least have room for a macinfo type byte? */
20344 if (mac_ptr >= mac_end)
20345 {
20346 dwarf2_section_buffer_overflow_complaint (section);
20347 return;
20348 }
20349
20350 /* We don't increment mac_ptr here, so this is just
20351 a look-ahead. */
20352 next_type = read_1_byte (abfd, mac_ptr);
20353 if (next_type != 0)
20354 complaint (&symfile_complaints,
20355 _("no terminating 0-type entry for "
20356 "macros in `.debug_macinfo' section"));
20357
20358 return;
20359 }
20360 }
20361 break;
20362
20363 case DW_MACRO_GNU_transparent_include:
20364 case DW_MACRO_GNU_transparent_include_alt:
20365 {
20366 LONGEST offset;
20367 void **slot;
20368 bfd *include_bfd = abfd;
20369 struct dwarf2_section_info *include_section = section;
20370 struct dwarf2_section_info alt_section;
20371 const gdb_byte *include_mac_end = mac_end;
20372 int is_dwz = section_is_dwz;
20373 const gdb_byte *new_mac_ptr;
20374
20375 offset = read_offset_1 (abfd, mac_ptr, offset_size);
20376 mac_ptr += offset_size;
20377
20378 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
20379 {
20380 struct dwz_file *dwz = dwarf2_get_dwz_file ();
20381
20382 dwarf2_read_section (dwarf2_per_objfile->objfile,
20383 &dwz->macro);
20384
20385 include_section = &dwz->macro;
20386 include_bfd = get_section_bfd_owner (include_section);
20387 include_mac_end = dwz->macro.buffer + dwz->macro.size;
20388 is_dwz = 1;
20389 }
20390
20391 new_mac_ptr = include_section->buffer + offset;
20392 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
20393
20394 if (*slot != NULL)
20395 {
20396 /* This has actually happened; see
20397 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
20398 complaint (&symfile_complaints,
20399 _("recursive DW_MACRO_GNU_transparent_include in "
20400 ".debug_macro section"));
20401 }
20402 else
20403 {
20404 *slot = (void *) new_mac_ptr;
20405
20406 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
20407 include_mac_end, current_file,
20408 lh, comp_dir,
20409 section, section_is_gnu, is_dwz,
20410 offset_size, objfile, include_hash);
20411
20412 htab_remove_elt (include_hash, (void *) new_mac_ptr);
20413 }
20414 }
20415 break;
20416
20417 case DW_MACINFO_vendor_ext:
20418 if (!section_is_gnu)
20419 {
20420 unsigned int bytes_read;
20421 int constant;
20422
20423 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20424 mac_ptr += bytes_read;
20425 read_direct_string (abfd, mac_ptr, &bytes_read);
20426 mac_ptr += bytes_read;
20427
20428 /* We don't recognize any vendor extensions. */
20429 break;
20430 }
20431 /* FALLTHROUGH */
20432
20433 default:
20434 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
20435 mac_ptr, mac_end, abfd, offset_size,
20436 section);
20437 if (mac_ptr == NULL)
20438 return;
20439 break;
20440 }
20441 } while (macinfo_type != 0);
20442 }
20443
20444 static void
20445 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
20446 const char *comp_dir, int section_is_gnu)
20447 {
20448 struct objfile *objfile = dwarf2_per_objfile->objfile;
20449 struct line_header *lh = cu->line_header;
20450 bfd *abfd;
20451 const gdb_byte *mac_ptr, *mac_end;
20452 struct macro_source_file *current_file = 0;
20453 enum dwarf_macro_record_type macinfo_type;
20454 unsigned int offset_size = cu->header.offset_size;
20455 const gdb_byte *opcode_definitions[256];
20456 struct cleanup *cleanup;
20457 htab_t include_hash;
20458 void **slot;
20459 struct dwarf2_section_info *section;
20460 const char *section_name;
20461
20462 if (cu->dwo_unit != NULL)
20463 {
20464 if (section_is_gnu)
20465 {
20466 section = &cu->dwo_unit->dwo_file->sections.macro;
20467 section_name = ".debug_macro.dwo";
20468 }
20469 else
20470 {
20471 section = &cu->dwo_unit->dwo_file->sections.macinfo;
20472 section_name = ".debug_macinfo.dwo";
20473 }
20474 }
20475 else
20476 {
20477 if (section_is_gnu)
20478 {
20479 section = &dwarf2_per_objfile->macro;
20480 section_name = ".debug_macro";
20481 }
20482 else
20483 {
20484 section = &dwarf2_per_objfile->macinfo;
20485 section_name = ".debug_macinfo";
20486 }
20487 }
20488
20489 dwarf2_read_section (objfile, section);
20490 if (section->buffer == NULL)
20491 {
20492 complaint (&symfile_complaints, _("missing %s section"), section_name);
20493 return;
20494 }
20495 abfd = get_section_bfd_owner (section);
20496
20497 /* First pass: Find the name of the base filename.
20498 This filename is needed in order to process all macros whose definition
20499 (or undefinition) comes from the command line. These macros are defined
20500 before the first DW_MACINFO_start_file entry, and yet still need to be
20501 associated to the base file.
20502
20503 To determine the base file name, we scan the macro definitions until we
20504 reach the first DW_MACINFO_start_file entry. We then initialize
20505 CURRENT_FILE accordingly so that any macro definition found before the
20506 first DW_MACINFO_start_file can still be associated to the base file. */
20507
20508 mac_ptr = section->buffer + offset;
20509 mac_end = section->buffer + section->size;
20510
20511 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20512 &offset_size, section_is_gnu);
20513 if (mac_ptr == NULL)
20514 {
20515 /* We already issued a complaint. */
20516 return;
20517 }
20518
20519 do
20520 {
20521 /* Do we at least have room for a macinfo type byte? */
20522 if (mac_ptr >= mac_end)
20523 {
20524 /* Complaint is printed during the second pass as GDB will probably
20525 stop the first pass earlier upon finding
20526 DW_MACINFO_start_file. */
20527 break;
20528 }
20529
20530 macinfo_type = read_1_byte (abfd, mac_ptr);
20531 mac_ptr++;
20532
20533 /* Note that we rely on the fact that the corresponding GNU and
20534 DWARF constants are the same. */
20535 switch (macinfo_type)
20536 {
20537 /* A zero macinfo type indicates the end of the macro
20538 information. */
20539 case 0:
20540 break;
20541
20542 case DW_MACRO_GNU_define:
20543 case DW_MACRO_GNU_undef:
20544 /* Only skip the data by MAC_PTR. */
20545 {
20546 unsigned int bytes_read;
20547
20548 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20549 mac_ptr += bytes_read;
20550 read_direct_string (abfd, mac_ptr, &bytes_read);
20551 mac_ptr += bytes_read;
20552 }
20553 break;
20554
20555 case DW_MACRO_GNU_start_file:
20556 {
20557 unsigned int bytes_read;
20558 int line, file;
20559
20560 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20561 mac_ptr += bytes_read;
20562 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20563 mac_ptr += bytes_read;
20564
20565 current_file = macro_start_file (file, line, current_file,
20566 comp_dir, lh, objfile);
20567 }
20568 break;
20569
20570 case DW_MACRO_GNU_end_file:
20571 /* No data to skip by MAC_PTR. */
20572 break;
20573
20574 case DW_MACRO_GNU_define_indirect:
20575 case DW_MACRO_GNU_undef_indirect:
20576 case DW_MACRO_GNU_define_indirect_alt:
20577 case DW_MACRO_GNU_undef_indirect_alt:
20578 {
20579 unsigned int bytes_read;
20580
20581 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20582 mac_ptr += bytes_read;
20583 mac_ptr += offset_size;
20584 }
20585 break;
20586
20587 case DW_MACRO_GNU_transparent_include:
20588 case DW_MACRO_GNU_transparent_include_alt:
20589 /* Note that, according to the spec, a transparent include
20590 chain cannot call DW_MACRO_GNU_start_file. So, we can just
20591 skip this opcode. */
20592 mac_ptr += offset_size;
20593 break;
20594
20595 case DW_MACINFO_vendor_ext:
20596 /* Only skip the data by MAC_PTR. */
20597 if (!section_is_gnu)
20598 {
20599 unsigned int bytes_read;
20600
20601 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20602 mac_ptr += bytes_read;
20603 read_direct_string (abfd, mac_ptr, &bytes_read);
20604 mac_ptr += bytes_read;
20605 }
20606 /* FALLTHROUGH */
20607
20608 default:
20609 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
20610 mac_ptr, mac_end, abfd, offset_size,
20611 section);
20612 if (mac_ptr == NULL)
20613 return;
20614 break;
20615 }
20616 } while (macinfo_type != 0 && current_file == NULL);
20617
20618 /* Second pass: Process all entries.
20619
20620 Use the AT_COMMAND_LINE flag to determine whether we are still processing
20621 command-line macro definitions/undefinitions. This flag is unset when we
20622 reach the first DW_MACINFO_start_file entry. */
20623
20624 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
20625 NULL, xcalloc, xfree);
20626 cleanup = make_cleanup_htab_delete (include_hash);
20627 mac_ptr = section->buffer + offset;
20628 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
20629 *slot = (void *) mac_ptr;
20630 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
20631 current_file, lh, comp_dir, section,
20632 section_is_gnu, 0,
20633 offset_size, objfile, include_hash);
20634 do_cleanups (cleanup);
20635 }
20636
20637 /* Check if the attribute's form is a DW_FORM_block*
20638 if so return true else false. */
20639
20640 static int
20641 attr_form_is_block (const struct attribute *attr)
20642 {
20643 return (attr == NULL ? 0 :
20644 attr->form == DW_FORM_block1
20645 || attr->form == DW_FORM_block2
20646 || attr->form == DW_FORM_block4
20647 || attr->form == DW_FORM_block
20648 || attr->form == DW_FORM_exprloc);
20649 }
20650
20651 /* Return non-zero if ATTR's value is a section offset --- classes
20652 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
20653 You may use DW_UNSND (attr) to retrieve such offsets.
20654
20655 Section 7.5.4, "Attribute Encodings", explains that no attribute
20656 may have a value that belongs to more than one of these classes; it
20657 would be ambiguous if we did, because we use the same forms for all
20658 of them. */
20659
20660 static int
20661 attr_form_is_section_offset (const struct attribute *attr)
20662 {
20663 return (attr->form == DW_FORM_data4
20664 || attr->form == DW_FORM_data8
20665 || attr->form == DW_FORM_sec_offset);
20666 }
20667
20668 /* Return non-zero if ATTR's value falls in the 'constant' class, or
20669 zero otherwise. When this function returns true, you can apply
20670 dwarf2_get_attr_constant_value to it.
20671
20672 However, note that for some attributes you must check
20673 attr_form_is_section_offset before using this test. DW_FORM_data4
20674 and DW_FORM_data8 are members of both the constant class, and of
20675 the classes that contain offsets into other debug sections
20676 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
20677 that, if an attribute's can be either a constant or one of the
20678 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
20679 taken as section offsets, not constants. */
20680
20681 static int
20682 attr_form_is_constant (const struct attribute *attr)
20683 {
20684 switch (attr->form)
20685 {
20686 case DW_FORM_sdata:
20687 case DW_FORM_udata:
20688 case DW_FORM_data1:
20689 case DW_FORM_data2:
20690 case DW_FORM_data4:
20691 case DW_FORM_data8:
20692 return 1;
20693 default:
20694 return 0;
20695 }
20696 }
20697
20698
20699 /* DW_ADDR is always stored already as sect_offset; despite for the forms
20700 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
20701
20702 static int
20703 attr_form_is_ref (const struct attribute *attr)
20704 {
20705 switch (attr->form)
20706 {
20707 case DW_FORM_ref_addr:
20708 case DW_FORM_ref1:
20709 case DW_FORM_ref2:
20710 case DW_FORM_ref4:
20711 case DW_FORM_ref8:
20712 case DW_FORM_ref_udata:
20713 case DW_FORM_GNU_ref_alt:
20714 return 1;
20715 default:
20716 return 0;
20717 }
20718 }
20719
20720 /* Return the .debug_loc section to use for CU.
20721 For DWO files use .debug_loc.dwo. */
20722
20723 static struct dwarf2_section_info *
20724 cu_debug_loc_section (struct dwarf2_cu *cu)
20725 {
20726 if (cu->dwo_unit)
20727 return &cu->dwo_unit->dwo_file->sections.loc;
20728 return &dwarf2_per_objfile->loc;
20729 }
20730
20731 /* A helper function that fills in a dwarf2_loclist_baton. */
20732
20733 static void
20734 fill_in_loclist_baton (struct dwarf2_cu *cu,
20735 struct dwarf2_loclist_baton *baton,
20736 const struct attribute *attr)
20737 {
20738 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20739
20740 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20741
20742 baton->per_cu = cu->per_cu;
20743 gdb_assert (baton->per_cu);
20744 /* We don't know how long the location list is, but make sure we
20745 don't run off the edge of the section. */
20746 baton->size = section->size - DW_UNSND (attr);
20747 baton->data = section->buffer + DW_UNSND (attr);
20748 baton->base_address = cu->base_address;
20749 baton->from_dwo = cu->dwo_unit != NULL;
20750 }
20751
20752 static void
20753 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
20754 struct dwarf2_cu *cu, int is_block)
20755 {
20756 struct objfile *objfile = dwarf2_per_objfile->objfile;
20757 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20758
20759 if (attr_form_is_section_offset (attr)
20760 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
20761 the section. If so, fall through to the complaint in the
20762 other branch. */
20763 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
20764 {
20765 struct dwarf2_loclist_baton *baton;
20766
20767 baton = obstack_alloc (&objfile->objfile_obstack,
20768 sizeof (struct dwarf2_loclist_baton));
20769
20770 fill_in_loclist_baton (cu, baton, attr);
20771
20772 if (cu->base_known == 0)
20773 complaint (&symfile_complaints,
20774 _("Location list used without "
20775 "specifying the CU base address."));
20776
20777 SYMBOL_ACLASS_INDEX (sym) = (is_block
20778 ? dwarf2_loclist_block_index
20779 : dwarf2_loclist_index);
20780 SYMBOL_LOCATION_BATON (sym) = baton;
20781 }
20782 else
20783 {
20784 struct dwarf2_locexpr_baton *baton;
20785
20786 baton = obstack_alloc (&objfile->objfile_obstack,
20787 sizeof (struct dwarf2_locexpr_baton));
20788 baton->per_cu = cu->per_cu;
20789 gdb_assert (baton->per_cu);
20790
20791 if (attr_form_is_block (attr))
20792 {
20793 /* Note that we're just copying the block's data pointer
20794 here, not the actual data. We're still pointing into the
20795 info_buffer for SYM's objfile; right now we never release
20796 that buffer, but when we do clean up properly this may
20797 need to change. */
20798 baton->size = DW_BLOCK (attr)->size;
20799 baton->data = DW_BLOCK (attr)->data;
20800 }
20801 else
20802 {
20803 dwarf2_invalid_attrib_class_complaint ("location description",
20804 SYMBOL_NATURAL_NAME (sym));
20805 baton->size = 0;
20806 }
20807
20808 SYMBOL_ACLASS_INDEX (sym) = (is_block
20809 ? dwarf2_locexpr_block_index
20810 : dwarf2_locexpr_index);
20811 SYMBOL_LOCATION_BATON (sym) = baton;
20812 }
20813 }
20814
20815 /* Return the OBJFILE associated with the compilation unit CU. If CU
20816 came from a separate debuginfo file, then the master objfile is
20817 returned. */
20818
20819 struct objfile *
20820 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20821 {
20822 struct objfile *objfile = per_cu->objfile;
20823
20824 /* Return the master objfile, so that we can report and look up the
20825 correct file containing this variable. */
20826 if (objfile->separate_debug_objfile_backlink)
20827 objfile = objfile->separate_debug_objfile_backlink;
20828
20829 return objfile;
20830 }
20831
20832 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20833 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20834 CU_HEADERP first. */
20835
20836 static const struct comp_unit_head *
20837 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20838 struct dwarf2_per_cu_data *per_cu)
20839 {
20840 const gdb_byte *info_ptr;
20841
20842 if (per_cu->cu)
20843 return &per_cu->cu->header;
20844
20845 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20846
20847 memset (cu_headerp, 0, sizeof (*cu_headerp));
20848 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20849
20850 return cu_headerp;
20851 }
20852
20853 /* Return the address size given in the compilation unit header for CU. */
20854
20855 int
20856 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20857 {
20858 struct comp_unit_head cu_header_local;
20859 const struct comp_unit_head *cu_headerp;
20860
20861 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20862
20863 return cu_headerp->addr_size;
20864 }
20865
20866 /* Return the offset size given in the compilation unit header for CU. */
20867
20868 int
20869 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20870 {
20871 struct comp_unit_head cu_header_local;
20872 const struct comp_unit_head *cu_headerp;
20873
20874 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20875
20876 return cu_headerp->offset_size;
20877 }
20878
20879 /* See its dwarf2loc.h declaration. */
20880
20881 int
20882 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20883 {
20884 struct comp_unit_head cu_header_local;
20885 const struct comp_unit_head *cu_headerp;
20886
20887 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20888
20889 if (cu_headerp->version == 2)
20890 return cu_headerp->addr_size;
20891 else
20892 return cu_headerp->offset_size;
20893 }
20894
20895 /* Return the text offset of the CU. The returned offset comes from
20896 this CU's objfile. If this objfile came from a separate debuginfo
20897 file, then the offset may be different from the corresponding
20898 offset in the parent objfile. */
20899
20900 CORE_ADDR
20901 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20902 {
20903 struct objfile *objfile = per_cu->objfile;
20904
20905 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20906 }
20907
20908 /* Locate the .debug_info compilation unit from CU's objfile which contains
20909 the DIE at OFFSET. Raises an error on failure. */
20910
20911 static struct dwarf2_per_cu_data *
20912 dwarf2_find_containing_comp_unit (sect_offset offset,
20913 unsigned int offset_in_dwz,
20914 struct objfile *objfile)
20915 {
20916 struct dwarf2_per_cu_data *this_cu;
20917 int low, high;
20918 const sect_offset *cu_off;
20919
20920 low = 0;
20921 high = dwarf2_per_objfile->n_comp_units - 1;
20922 while (high > low)
20923 {
20924 struct dwarf2_per_cu_data *mid_cu;
20925 int mid = low + (high - low) / 2;
20926
20927 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20928 cu_off = &mid_cu->offset;
20929 if (mid_cu->is_dwz > offset_in_dwz
20930 || (mid_cu->is_dwz == offset_in_dwz
20931 && cu_off->sect_off >= offset.sect_off))
20932 high = mid;
20933 else
20934 low = mid + 1;
20935 }
20936 gdb_assert (low == high);
20937 this_cu = dwarf2_per_objfile->all_comp_units[low];
20938 cu_off = &this_cu->offset;
20939 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20940 {
20941 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20942 error (_("Dwarf Error: could not find partial DIE containing "
20943 "offset 0x%lx [in module %s]"),
20944 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20945
20946 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20947 <= offset.sect_off);
20948 return dwarf2_per_objfile->all_comp_units[low-1];
20949 }
20950 else
20951 {
20952 this_cu = dwarf2_per_objfile->all_comp_units[low];
20953 if (low == dwarf2_per_objfile->n_comp_units - 1
20954 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20955 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20956 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20957 return this_cu;
20958 }
20959 }
20960
20961 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20962
20963 static void
20964 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20965 {
20966 memset (cu, 0, sizeof (*cu));
20967 per_cu->cu = cu;
20968 cu->per_cu = per_cu;
20969 cu->objfile = per_cu->objfile;
20970 obstack_init (&cu->comp_unit_obstack);
20971 }
20972
20973 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20974
20975 static void
20976 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20977 enum language pretend_language)
20978 {
20979 struct attribute *attr;
20980
20981 /* Set the language we're debugging. */
20982 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20983 if (attr)
20984 set_cu_language (DW_UNSND (attr), cu);
20985 else
20986 {
20987 cu->language = pretend_language;
20988 cu->language_defn = language_def (cu->language);
20989 }
20990
20991 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20992 if (attr)
20993 cu->producer = DW_STRING (attr);
20994 }
20995
20996 /* Release one cached compilation unit, CU. We unlink it from the tree
20997 of compilation units, but we don't remove it from the read_in_chain;
20998 the caller is responsible for that.
20999 NOTE: DATA is a void * because this function is also used as a
21000 cleanup routine. */
21001
21002 static void
21003 free_heap_comp_unit (void *data)
21004 {
21005 struct dwarf2_cu *cu = data;
21006
21007 gdb_assert (cu->per_cu != NULL);
21008 cu->per_cu->cu = NULL;
21009 cu->per_cu = NULL;
21010
21011 obstack_free (&cu->comp_unit_obstack, NULL);
21012
21013 xfree (cu);
21014 }
21015
21016 /* This cleanup function is passed the address of a dwarf2_cu on the stack
21017 when we're finished with it. We can't free the pointer itself, but be
21018 sure to unlink it from the cache. Also release any associated storage. */
21019
21020 static void
21021 free_stack_comp_unit (void *data)
21022 {
21023 struct dwarf2_cu *cu = data;
21024
21025 gdb_assert (cu->per_cu != NULL);
21026 cu->per_cu->cu = NULL;
21027 cu->per_cu = NULL;
21028
21029 obstack_free (&cu->comp_unit_obstack, NULL);
21030 cu->partial_dies = NULL;
21031 }
21032
21033 /* Free all cached compilation units. */
21034
21035 static void
21036 free_cached_comp_units (void *data)
21037 {
21038 struct dwarf2_per_cu_data *per_cu, **last_chain;
21039
21040 per_cu = dwarf2_per_objfile->read_in_chain;
21041 last_chain = &dwarf2_per_objfile->read_in_chain;
21042 while (per_cu != NULL)
21043 {
21044 struct dwarf2_per_cu_data *next_cu;
21045
21046 next_cu = per_cu->cu->read_in_chain;
21047
21048 free_heap_comp_unit (per_cu->cu);
21049 *last_chain = next_cu;
21050
21051 per_cu = next_cu;
21052 }
21053 }
21054
21055 /* Increase the age counter on each cached compilation unit, and free
21056 any that are too old. */
21057
21058 static void
21059 age_cached_comp_units (void)
21060 {
21061 struct dwarf2_per_cu_data *per_cu, **last_chain;
21062
21063 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
21064 per_cu = dwarf2_per_objfile->read_in_chain;
21065 while (per_cu != NULL)
21066 {
21067 per_cu->cu->last_used ++;
21068 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
21069 dwarf2_mark (per_cu->cu);
21070 per_cu = per_cu->cu->read_in_chain;
21071 }
21072
21073 per_cu = dwarf2_per_objfile->read_in_chain;
21074 last_chain = &dwarf2_per_objfile->read_in_chain;
21075 while (per_cu != NULL)
21076 {
21077 struct dwarf2_per_cu_data *next_cu;
21078
21079 next_cu = per_cu->cu->read_in_chain;
21080
21081 if (!per_cu->cu->mark)
21082 {
21083 free_heap_comp_unit (per_cu->cu);
21084 *last_chain = next_cu;
21085 }
21086 else
21087 last_chain = &per_cu->cu->read_in_chain;
21088
21089 per_cu = next_cu;
21090 }
21091 }
21092
21093 /* Remove a single compilation unit from the cache. */
21094
21095 static void
21096 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
21097 {
21098 struct dwarf2_per_cu_data *per_cu, **last_chain;
21099
21100 per_cu = dwarf2_per_objfile->read_in_chain;
21101 last_chain = &dwarf2_per_objfile->read_in_chain;
21102 while (per_cu != NULL)
21103 {
21104 struct dwarf2_per_cu_data *next_cu;
21105
21106 next_cu = per_cu->cu->read_in_chain;
21107
21108 if (per_cu == target_per_cu)
21109 {
21110 free_heap_comp_unit (per_cu->cu);
21111 per_cu->cu = NULL;
21112 *last_chain = next_cu;
21113 break;
21114 }
21115 else
21116 last_chain = &per_cu->cu->read_in_chain;
21117
21118 per_cu = next_cu;
21119 }
21120 }
21121
21122 /* Release all extra memory associated with OBJFILE. */
21123
21124 void
21125 dwarf2_free_objfile (struct objfile *objfile)
21126 {
21127 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21128
21129 if (dwarf2_per_objfile == NULL)
21130 return;
21131
21132 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
21133 free_cached_comp_units (NULL);
21134
21135 if (dwarf2_per_objfile->quick_file_names_table)
21136 htab_delete (dwarf2_per_objfile->quick_file_names_table);
21137
21138 /* Everything else should be on the objfile obstack. */
21139 }
21140
21141 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21142 We store these in a hash table separate from the DIEs, and preserve them
21143 when the DIEs are flushed out of cache.
21144
21145 The CU "per_cu" pointer is needed because offset alone is not enough to
21146 uniquely identify the type. A file may have multiple .debug_types sections,
21147 or the type may come from a DWO file. Furthermore, while it's more logical
21148 to use per_cu->section+offset, with Fission the section with the data is in
21149 the DWO file but we don't know that section at the point we need it.
21150 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21151 because we can enter the lookup routine, get_die_type_at_offset, from
21152 outside this file, and thus won't necessarily have PER_CU->cu.
21153 Fortunately, PER_CU is stable for the life of the objfile. */
21154
21155 struct dwarf2_per_cu_offset_and_type
21156 {
21157 const struct dwarf2_per_cu_data *per_cu;
21158 sect_offset offset;
21159 struct type *type;
21160 };
21161
21162 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21163
21164 static hashval_t
21165 per_cu_offset_and_type_hash (const void *item)
21166 {
21167 const struct dwarf2_per_cu_offset_and_type *ofs = item;
21168
21169 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
21170 }
21171
21172 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21173
21174 static int
21175 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21176 {
21177 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
21178 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
21179
21180 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21181 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
21182 }
21183
21184 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21185 table if necessary. For convenience, return TYPE.
21186
21187 The DIEs reading must have careful ordering to:
21188 * Not cause infite loops trying to read in DIEs as a prerequisite for
21189 reading current DIE.
21190 * Not trying to dereference contents of still incompletely read in types
21191 while reading in other DIEs.
21192 * Enable referencing still incompletely read in types just by a pointer to
21193 the type without accessing its fields.
21194
21195 Therefore caller should follow these rules:
21196 * Try to fetch any prerequisite types we may need to build this DIE type
21197 before building the type and calling set_die_type.
21198 * After building type call set_die_type for current DIE as soon as
21199 possible before fetching more types to complete the current type.
21200 * Make the type as complete as possible before fetching more types. */
21201
21202 static struct type *
21203 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21204 {
21205 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21206 struct objfile *objfile = cu->objfile;
21207
21208 /* For Ada types, make sure that the gnat-specific data is always
21209 initialized (if not already set). There are a few types where
21210 we should not be doing so, because the type-specific area is
21211 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21212 where the type-specific area is used to store the floatformat).
21213 But this is not a problem, because the gnat-specific information
21214 is actually not needed for these types. */
21215 if (need_gnat_info (cu)
21216 && TYPE_CODE (type) != TYPE_CODE_FUNC
21217 && TYPE_CODE (type) != TYPE_CODE_FLT
21218 && !HAVE_GNAT_AUX_INFO (type))
21219 INIT_GNAT_SPECIFIC (type);
21220
21221 if (dwarf2_per_objfile->die_type_hash == NULL)
21222 {
21223 dwarf2_per_objfile->die_type_hash =
21224 htab_create_alloc_ex (127,
21225 per_cu_offset_and_type_hash,
21226 per_cu_offset_and_type_eq,
21227 NULL,
21228 &objfile->objfile_obstack,
21229 hashtab_obstack_allocate,
21230 dummy_obstack_deallocate);
21231 }
21232
21233 ofs.per_cu = cu->per_cu;
21234 ofs.offset = die->offset;
21235 ofs.type = type;
21236 slot = (struct dwarf2_per_cu_offset_and_type **)
21237 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
21238 if (*slot)
21239 complaint (&symfile_complaints,
21240 _("A problem internal to GDB: DIE 0x%x has type already set"),
21241 die->offset.sect_off);
21242 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
21243 **slot = ofs;
21244 return type;
21245 }
21246
21247 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
21248 or return NULL if the die does not have a saved type. */
21249
21250 static struct type *
21251 get_die_type_at_offset (sect_offset offset,
21252 struct dwarf2_per_cu_data *per_cu)
21253 {
21254 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21255
21256 if (dwarf2_per_objfile->die_type_hash == NULL)
21257 return NULL;
21258
21259 ofs.per_cu = per_cu;
21260 ofs.offset = offset;
21261 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
21262 if (slot)
21263 return slot->type;
21264 else
21265 return NULL;
21266 }
21267
21268 /* Look up the type for DIE in CU in die_type_hash,
21269 or return NULL if DIE does not have a saved type. */
21270
21271 static struct type *
21272 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21273 {
21274 return get_die_type_at_offset (die->offset, cu->per_cu);
21275 }
21276
21277 /* Add a dependence relationship from CU to REF_PER_CU. */
21278
21279 static void
21280 dwarf2_add_dependence (struct dwarf2_cu *cu,
21281 struct dwarf2_per_cu_data *ref_per_cu)
21282 {
21283 void **slot;
21284
21285 if (cu->dependencies == NULL)
21286 cu->dependencies
21287 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
21288 NULL, &cu->comp_unit_obstack,
21289 hashtab_obstack_allocate,
21290 dummy_obstack_deallocate);
21291
21292 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
21293 if (*slot == NULL)
21294 *slot = ref_per_cu;
21295 }
21296
21297 /* Subroutine of dwarf2_mark to pass to htab_traverse.
21298 Set the mark field in every compilation unit in the
21299 cache that we must keep because we are keeping CU. */
21300
21301 static int
21302 dwarf2_mark_helper (void **slot, void *data)
21303 {
21304 struct dwarf2_per_cu_data *per_cu;
21305
21306 per_cu = (struct dwarf2_per_cu_data *) *slot;
21307
21308 /* cu->dependencies references may not yet have been ever read if QUIT aborts
21309 reading of the chain. As such dependencies remain valid it is not much
21310 useful to track and undo them during QUIT cleanups. */
21311 if (per_cu->cu == NULL)
21312 return 1;
21313
21314 if (per_cu->cu->mark)
21315 return 1;
21316 per_cu->cu->mark = 1;
21317
21318 if (per_cu->cu->dependencies != NULL)
21319 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
21320
21321 return 1;
21322 }
21323
21324 /* Set the mark field in CU and in every other compilation unit in the
21325 cache that we must keep because we are keeping CU. */
21326
21327 static void
21328 dwarf2_mark (struct dwarf2_cu *cu)
21329 {
21330 if (cu->mark)
21331 return;
21332 cu->mark = 1;
21333 if (cu->dependencies != NULL)
21334 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
21335 }
21336
21337 static void
21338 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
21339 {
21340 while (per_cu)
21341 {
21342 per_cu->cu->mark = 0;
21343 per_cu = per_cu->cu->read_in_chain;
21344 }
21345 }
21346
21347 /* Trivial hash function for partial_die_info: the hash value of a DIE
21348 is its offset in .debug_info for this objfile. */
21349
21350 static hashval_t
21351 partial_die_hash (const void *item)
21352 {
21353 const struct partial_die_info *part_die = item;
21354
21355 return part_die->offset.sect_off;
21356 }
21357
21358 /* Trivial comparison function for partial_die_info structures: two DIEs
21359 are equal if they have the same offset. */
21360
21361 static int
21362 partial_die_eq (const void *item_lhs, const void *item_rhs)
21363 {
21364 const struct partial_die_info *part_die_lhs = item_lhs;
21365 const struct partial_die_info *part_die_rhs = item_rhs;
21366
21367 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
21368 }
21369
21370 static struct cmd_list_element *set_dwarf2_cmdlist;
21371 static struct cmd_list_element *show_dwarf2_cmdlist;
21372
21373 static void
21374 set_dwarf2_cmd (char *args, int from_tty)
21375 {
21376 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
21377 }
21378
21379 static void
21380 show_dwarf2_cmd (char *args, int from_tty)
21381 {
21382 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
21383 }
21384
21385 /* Free data associated with OBJFILE, if necessary. */
21386
21387 static void
21388 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
21389 {
21390 struct dwarf2_per_objfile *data = d;
21391 int ix;
21392
21393 /* Make sure we don't accidentally use dwarf2_per_objfile while
21394 cleaning up. */
21395 dwarf2_per_objfile = NULL;
21396
21397 for (ix = 0; ix < data->n_comp_units; ++ix)
21398 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
21399
21400 for (ix = 0; ix < data->n_type_units; ++ix)
21401 VEC_free (dwarf2_per_cu_ptr,
21402 data->all_type_units[ix]->per_cu.imported_symtabs);
21403 xfree (data->all_type_units);
21404
21405 VEC_free (dwarf2_section_info_def, data->types);
21406
21407 if (data->dwo_files)
21408 free_dwo_files (data->dwo_files, objfile);
21409 if (data->dwp_file)
21410 gdb_bfd_unref (data->dwp_file->dbfd);
21411
21412 if (data->dwz_file && data->dwz_file->dwz_bfd)
21413 gdb_bfd_unref (data->dwz_file->dwz_bfd);
21414 }
21415
21416 \f
21417 /* The "save gdb-index" command. */
21418
21419 /* The contents of the hash table we create when building the string
21420 table. */
21421 struct strtab_entry
21422 {
21423 offset_type offset;
21424 const char *str;
21425 };
21426
21427 /* Hash function for a strtab_entry.
21428
21429 Function is used only during write_hash_table so no index format backward
21430 compatibility is needed. */
21431
21432 static hashval_t
21433 hash_strtab_entry (const void *e)
21434 {
21435 const struct strtab_entry *entry = e;
21436 return mapped_index_string_hash (INT_MAX, entry->str);
21437 }
21438
21439 /* Equality function for a strtab_entry. */
21440
21441 static int
21442 eq_strtab_entry (const void *a, const void *b)
21443 {
21444 const struct strtab_entry *ea = a;
21445 const struct strtab_entry *eb = b;
21446 return !strcmp (ea->str, eb->str);
21447 }
21448
21449 /* Create a strtab_entry hash table. */
21450
21451 static htab_t
21452 create_strtab (void)
21453 {
21454 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
21455 xfree, xcalloc, xfree);
21456 }
21457
21458 /* Add a string to the constant pool. Return the string's offset in
21459 host order. */
21460
21461 static offset_type
21462 add_string (htab_t table, struct obstack *cpool, const char *str)
21463 {
21464 void **slot;
21465 struct strtab_entry entry;
21466 struct strtab_entry *result;
21467
21468 entry.str = str;
21469 slot = htab_find_slot (table, &entry, INSERT);
21470 if (*slot)
21471 result = *slot;
21472 else
21473 {
21474 result = XNEW (struct strtab_entry);
21475 result->offset = obstack_object_size (cpool);
21476 result->str = str;
21477 obstack_grow_str0 (cpool, str);
21478 *slot = result;
21479 }
21480 return result->offset;
21481 }
21482
21483 /* An entry in the symbol table. */
21484 struct symtab_index_entry
21485 {
21486 /* The name of the symbol. */
21487 const char *name;
21488 /* The offset of the name in the constant pool. */
21489 offset_type index_offset;
21490 /* A sorted vector of the indices of all the CUs that hold an object
21491 of this name. */
21492 VEC (offset_type) *cu_indices;
21493 };
21494
21495 /* The symbol table. This is a power-of-2-sized hash table. */
21496 struct mapped_symtab
21497 {
21498 offset_type n_elements;
21499 offset_type size;
21500 struct symtab_index_entry **data;
21501 };
21502
21503 /* Hash function for a symtab_index_entry. */
21504
21505 static hashval_t
21506 hash_symtab_entry (const void *e)
21507 {
21508 const struct symtab_index_entry *entry = e;
21509 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
21510 sizeof (offset_type) * VEC_length (offset_type,
21511 entry->cu_indices),
21512 0);
21513 }
21514
21515 /* Equality function for a symtab_index_entry. */
21516
21517 static int
21518 eq_symtab_entry (const void *a, const void *b)
21519 {
21520 const struct symtab_index_entry *ea = a;
21521 const struct symtab_index_entry *eb = b;
21522 int len = VEC_length (offset_type, ea->cu_indices);
21523 if (len != VEC_length (offset_type, eb->cu_indices))
21524 return 0;
21525 return !memcmp (VEC_address (offset_type, ea->cu_indices),
21526 VEC_address (offset_type, eb->cu_indices),
21527 sizeof (offset_type) * len);
21528 }
21529
21530 /* Destroy a symtab_index_entry. */
21531
21532 static void
21533 delete_symtab_entry (void *p)
21534 {
21535 struct symtab_index_entry *entry = p;
21536 VEC_free (offset_type, entry->cu_indices);
21537 xfree (entry);
21538 }
21539
21540 /* Create a hash table holding symtab_index_entry objects. */
21541
21542 static htab_t
21543 create_symbol_hash_table (void)
21544 {
21545 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
21546 delete_symtab_entry, xcalloc, xfree);
21547 }
21548
21549 /* Create a new mapped symtab object. */
21550
21551 static struct mapped_symtab *
21552 create_mapped_symtab (void)
21553 {
21554 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
21555 symtab->n_elements = 0;
21556 symtab->size = 1024;
21557 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21558 return symtab;
21559 }
21560
21561 /* Destroy a mapped_symtab. */
21562
21563 static void
21564 cleanup_mapped_symtab (void *p)
21565 {
21566 struct mapped_symtab *symtab = p;
21567 /* The contents of the array are freed when the other hash table is
21568 destroyed. */
21569 xfree (symtab->data);
21570 xfree (symtab);
21571 }
21572
21573 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
21574 the slot.
21575
21576 Function is used only during write_hash_table so no index format backward
21577 compatibility is needed. */
21578
21579 static struct symtab_index_entry **
21580 find_slot (struct mapped_symtab *symtab, const char *name)
21581 {
21582 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
21583
21584 index = hash & (symtab->size - 1);
21585 step = ((hash * 17) & (symtab->size - 1)) | 1;
21586
21587 for (;;)
21588 {
21589 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
21590 return &symtab->data[index];
21591 index = (index + step) & (symtab->size - 1);
21592 }
21593 }
21594
21595 /* Expand SYMTAB's hash table. */
21596
21597 static void
21598 hash_expand (struct mapped_symtab *symtab)
21599 {
21600 offset_type old_size = symtab->size;
21601 offset_type i;
21602 struct symtab_index_entry **old_entries = symtab->data;
21603
21604 symtab->size *= 2;
21605 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21606
21607 for (i = 0; i < old_size; ++i)
21608 {
21609 if (old_entries[i])
21610 {
21611 struct symtab_index_entry **slot = find_slot (symtab,
21612 old_entries[i]->name);
21613 *slot = old_entries[i];
21614 }
21615 }
21616
21617 xfree (old_entries);
21618 }
21619
21620 /* Add an entry to SYMTAB. NAME is the name of the symbol.
21621 CU_INDEX is the index of the CU in which the symbol appears.
21622 IS_STATIC is one if the symbol is static, otherwise zero (global). */
21623
21624 static void
21625 add_index_entry (struct mapped_symtab *symtab, const char *name,
21626 int is_static, gdb_index_symbol_kind kind,
21627 offset_type cu_index)
21628 {
21629 struct symtab_index_entry **slot;
21630 offset_type cu_index_and_attrs;
21631
21632 ++symtab->n_elements;
21633 if (4 * symtab->n_elements / 3 >= symtab->size)
21634 hash_expand (symtab);
21635
21636 slot = find_slot (symtab, name);
21637 if (!*slot)
21638 {
21639 *slot = XNEW (struct symtab_index_entry);
21640 (*slot)->name = name;
21641 /* index_offset is set later. */
21642 (*slot)->cu_indices = NULL;
21643 }
21644
21645 cu_index_and_attrs = 0;
21646 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
21647 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
21648 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
21649
21650 /* We don't want to record an index value twice as we want to avoid the
21651 duplication.
21652 We process all global symbols and then all static symbols
21653 (which would allow us to avoid the duplication by only having to check
21654 the last entry pushed), but a symbol could have multiple kinds in one CU.
21655 To keep things simple we don't worry about the duplication here and
21656 sort and uniqufy the list after we've processed all symbols. */
21657 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
21658 }
21659
21660 /* qsort helper routine for uniquify_cu_indices. */
21661
21662 static int
21663 offset_type_compare (const void *ap, const void *bp)
21664 {
21665 offset_type a = *(offset_type *) ap;
21666 offset_type b = *(offset_type *) bp;
21667
21668 return (a > b) - (b > a);
21669 }
21670
21671 /* Sort and remove duplicates of all symbols' cu_indices lists. */
21672
21673 static void
21674 uniquify_cu_indices (struct mapped_symtab *symtab)
21675 {
21676 int i;
21677
21678 for (i = 0; i < symtab->size; ++i)
21679 {
21680 struct symtab_index_entry *entry = symtab->data[i];
21681
21682 if (entry
21683 && entry->cu_indices != NULL)
21684 {
21685 unsigned int next_to_insert, next_to_check;
21686 offset_type last_value;
21687
21688 qsort (VEC_address (offset_type, entry->cu_indices),
21689 VEC_length (offset_type, entry->cu_indices),
21690 sizeof (offset_type), offset_type_compare);
21691
21692 last_value = VEC_index (offset_type, entry->cu_indices, 0);
21693 next_to_insert = 1;
21694 for (next_to_check = 1;
21695 next_to_check < VEC_length (offset_type, entry->cu_indices);
21696 ++next_to_check)
21697 {
21698 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
21699 != last_value)
21700 {
21701 last_value = VEC_index (offset_type, entry->cu_indices,
21702 next_to_check);
21703 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
21704 last_value);
21705 ++next_to_insert;
21706 }
21707 }
21708 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
21709 }
21710 }
21711 }
21712
21713 /* Add a vector of indices to the constant pool. */
21714
21715 static offset_type
21716 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
21717 struct symtab_index_entry *entry)
21718 {
21719 void **slot;
21720
21721 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
21722 if (!*slot)
21723 {
21724 offset_type len = VEC_length (offset_type, entry->cu_indices);
21725 offset_type val = MAYBE_SWAP (len);
21726 offset_type iter;
21727 int i;
21728
21729 *slot = entry;
21730 entry->index_offset = obstack_object_size (cpool);
21731
21732 obstack_grow (cpool, &val, sizeof (val));
21733 for (i = 0;
21734 VEC_iterate (offset_type, entry->cu_indices, i, iter);
21735 ++i)
21736 {
21737 val = MAYBE_SWAP (iter);
21738 obstack_grow (cpool, &val, sizeof (val));
21739 }
21740 }
21741 else
21742 {
21743 struct symtab_index_entry *old_entry = *slot;
21744 entry->index_offset = old_entry->index_offset;
21745 entry = old_entry;
21746 }
21747 return entry->index_offset;
21748 }
21749
21750 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21751 constant pool entries going into the obstack CPOOL. */
21752
21753 static void
21754 write_hash_table (struct mapped_symtab *symtab,
21755 struct obstack *output, struct obstack *cpool)
21756 {
21757 offset_type i;
21758 htab_t symbol_hash_table;
21759 htab_t str_table;
21760
21761 symbol_hash_table = create_symbol_hash_table ();
21762 str_table = create_strtab ();
21763
21764 /* We add all the index vectors to the constant pool first, to
21765 ensure alignment is ok. */
21766 for (i = 0; i < symtab->size; ++i)
21767 {
21768 if (symtab->data[i])
21769 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
21770 }
21771
21772 /* Now write out the hash table. */
21773 for (i = 0; i < symtab->size; ++i)
21774 {
21775 offset_type str_off, vec_off;
21776
21777 if (symtab->data[i])
21778 {
21779 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21780 vec_off = symtab->data[i]->index_offset;
21781 }
21782 else
21783 {
21784 /* While 0 is a valid constant pool index, it is not valid
21785 to have 0 for both offsets. */
21786 str_off = 0;
21787 vec_off = 0;
21788 }
21789
21790 str_off = MAYBE_SWAP (str_off);
21791 vec_off = MAYBE_SWAP (vec_off);
21792
21793 obstack_grow (output, &str_off, sizeof (str_off));
21794 obstack_grow (output, &vec_off, sizeof (vec_off));
21795 }
21796
21797 htab_delete (str_table);
21798 htab_delete (symbol_hash_table);
21799 }
21800
21801 /* Struct to map psymtab to CU index in the index file. */
21802 struct psymtab_cu_index_map
21803 {
21804 struct partial_symtab *psymtab;
21805 unsigned int cu_index;
21806 };
21807
21808 static hashval_t
21809 hash_psymtab_cu_index (const void *item)
21810 {
21811 const struct psymtab_cu_index_map *map = item;
21812
21813 return htab_hash_pointer (map->psymtab);
21814 }
21815
21816 static int
21817 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21818 {
21819 const struct psymtab_cu_index_map *lhs = item_lhs;
21820 const struct psymtab_cu_index_map *rhs = item_rhs;
21821
21822 return lhs->psymtab == rhs->psymtab;
21823 }
21824
21825 /* Helper struct for building the address table. */
21826 struct addrmap_index_data
21827 {
21828 struct objfile *objfile;
21829 struct obstack *addr_obstack;
21830 htab_t cu_index_htab;
21831
21832 /* Non-zero if the previous_* fields are valid.
21833 We can't write an entry until we see the next entry (since it is only then
21834 that we know the end of the entry). */
21835 int previous_valid;
21836 /* Index of the CU in the table of all CUs in the index file. */
21837 unsigned int previous_cu_index;
21838 /* Start address of the CU. */
21839 CORE_ADDR previous_cu_start;
21840 };
21841
21842 /* Write an address entry to OBSTACK. */
21843
21844 static void
21845 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21846 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21847 {
21848 offset_type cu_index_to_write;
21849 gdb_byte addr[8];
21850 CORE_ADDR baseaddr;
21851
21852 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21853
21854 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21855 obstack_grow (obstack, addr, 8);
21856 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21857 obstack_grow (obstack, addr, 8);
21858 cu_index_to_write = MAYBE_SWAP (cu_index);
21859 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21860 }
21861
21862 /* Worker function for traversing an addrmap to build the address table. */
21863
21864 static int
21865 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21866 {
21867 struct addrmap_index_data *data = datap;
21868 struct partial_symtab *pst = obj;
21869
21870 if (data->previous_valid)
21871 add_address_entry (data->objfile, data->addr_obstack,
21872 data->previous_cu_start, start_addr,
21873 data->previous_cu_index);
21874
21875 data->previous_cu_start = start_addr;
21876 if (pst != NULL)
21877 {
21878 struct psymtab_cu_index_map find_map, *map;
21879 find_map.psymtab = pst;
21880 map = htab_find (data->cu_index_htab, &find_map);
21881 gdb_assert (map != NULL);
21882 data->previous_cu_index = map->cu_index;
21883 data->previous_valid = 1;
21884 }
21885 else
21886 data->previous_valid = 0;
21887
21888 return 0;
21889 }
21890
21891 /* Write OBJFILE's address map to OBSTACK.
21892 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21893 in the index file. */
21894
21895 static void
21896 write_address_map (struct objfile *objfile, struct obstack *obstack,
21897 htab_t cu_index_htab)
21898 {
21899 struct addrmap_index_data addrmap_index_data;
21900
21901 /* When writing the address table, we have to cope with the fact that
21902 the addrmap iterator only provides the start of a region; we have to
21903 wait until the next invocation to get the start of the next region. */
21904
21905 addrmap_index_data.objfile = objfile;
21906 addrmap_index_data.addr_obstack = obstack;
21907 addrmap_index_data.cu_index_htab = cu_index_htab;
21908 addrmap_index_data.previous_valid = 0;
21909
21910 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21911 &addrmap_index_data);
21912
21913 /* It's highly unlikely the last entry (end address = 0xff...ff)
21914 is valid, but we should still handle it.
21915 The end address is recorded as the start of the next region, but that
21916 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21917 anyway. */
21918 if (addrmap_index_data.previous_valid)
21919 add_address_entry (objfile, obstack,
21920 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21921 addrmap_index_data.previous_cu_index);
21922 }
21923
21924 /* Return the symbol kind of PSYM. */
21925
21926 static gdb_index_symbol_kind
21927 symbol_kind (struct partial_symbol *psym)
21928 {
21929 domain_enum domain = PSYMBOL_DOMAIN (psym);
21930 enum address_class aclass = PSYMBOL_CLASS (psym);
21931
21932 switch (domain)
21933 {
21934 case VAR_DOMAIN:
21935 switch (aclass)
21936 {
21937 case LOC_BLOCK:
21938 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21939 case LOC_TYPEDEF:
21940 return GDB_INDEX_SYMBOL_KIND_TYPE;
21941 case LOC_COMPUTED:
21942 case LOC_CONST_BYTES:
21943 case LOC_OPTIMIZED_OUT:
21944 case LOC_STATIC:
21945 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21946 case LOC_CONST:
21947 /* Note: It's currently impossible to recognize psyms as enum values
21948 short of reading the type info. For now punt. */
21949 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21950 default:
21951 /* There are other LOC_FOO values that one might want to classify
21952 as variables, but dwarf2read.c doesn't currently use them. */
21953 return GDB_INDEX_SYMBOL_KIND_OTHER;
21954 }
21955 case STRUCT_DOMAIN:
21956 return GDB_INDEX_SYMBOL_KIND_TYPE;
21957 default:
21958 return GDB_INDEX_SYMBOL_KIND_OTHER;
21959 }
21960 }
21961
21962 /* Add a list of partial symbols to SYMTAB. */
21963
21964 static void
21965 write_psymbols (struct mapped_symtab *symtab,
21966 htab_t psyms_seen,
21967 struct partial_symbol **psymp,
21968 int count,
21969 offset_type cu_index,
21970 int is_static)
21971 {
21972 for (; count-- > 0; ++psymp)
21973 {
21974 struct partial_symbol *psym = *psymp;
21975 void **slot;
21976
21977 if (SYMBOL_LANGUAGE (psym) == language_ada)
21978 error (_("Ada is not currently supported by the index"));
21979
21980 /* Only add a given psymbol once. */
21981 slot = htab_find_slot (psyms_seen, psym, INSERT);
21982 if (!*slot)
21983 {
21984 gdb_index_symbol_kind kind = symbol_kind (psym);
21985
21986 *slot = psym;
21987 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21988 is_static, kind, cu_index);
21989 }
21990 }
21991 }
21992
21993 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21994 exception if there is an error. */
21995
21996 static void
21997 write_obstack (FILE *file, struct obstack *obstack)
21998 {
21999 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
22000 file)
22001 != obstack_object_size (obstack))
22002 error (_("couldn't data write to file"));
22003 }
22004
22005 /* Unlink a file if the argument is not NULL. */
22006
22007 static void
22008 unlink_if_set (void *p)
22009 {
22010 char **filename = p;
22011 if (*filename)
22012 unlink (*filename);
22013 }
22014
22015 /* A helper struct used when iterating over debug_types. */
22016 struct signatured_type_index_data
22017 {
22018 struct objfile *objfile;
22019 struct mapped_symtab *symtab;
22020 struct obstack *types_list;
22021 htab_t psyms_seen;
22022 int cu_index;
22023 };
22024
22025 /* A helper function that writes a single signatured_type to an
22026 obstack. */
22027
22028 static int
22029 write_one_signatured_type (void **slot, void *d)
22030 {
22031 struct signatured_type_index_data *info = d;
22032 struct signatured_type *entry = (struct signatured_type *) *slot;
22033 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
22034 gdb_byte val[8];
22035
22036 write_psymbols (info->symtab,
22037 info->psyms_seen,
22038 info->objfile->global_psymbols.list
22039 + psymtab->globals_offset,
22040 psymtab->n_global_syms, info->cu_index,
22041 0);
22042 write_psymbols (info->symtab,
22043 info->psyms_seen,
22044 info->objfile->static_psymbols.list
22045 + psymtab->statics_offset,
22046 psymtab->n_static_syms, info->cu_index,
22047 1);
22048
22049 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22050 entry->per_cu.offset.sect_off);
22051 obstack_grow (info->types_list, val, 8);
22052 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22053 entry->type_offset_in_tu.cu_off);
22054 obstack_grow (info->types_list, val, 8);
22055 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
22056 obstack_grow (info->types_list, val, 8);
22057
22058 ++info->cu_index;
22059
22060 return 1;
22061 }
22062
22063 /* Recurse into all "included" dependencies and write their symbols as
22064 if they appeared in this psymtab. */
22065
22066 static void
22067 recursively_write_psymbols (struct objfile *objfile,
22068 struct partial_symtab *psymtab,
22069 struct mapped_symtab *symtab,
22070 htab_t psyms_seen,
22071 offset_type cu_index)
22072 {
22073 int i;
22074
22075 for (i = 0; i < psymtab->number_of_dependencies; ++i)
22076 if (psymtab->dependencies[i]->user != NULL)
22077 recursively_write_psymbols (objfile, psymtab->dependencies[i],
22078 symtab, psyms_seen, cu_index);
22079
22080 write_psymbols (symtab,
22081 psyms_seen,
22082 objfile->global_psymbols.list + psymtab->globals_offset,
22083 psymtab->n_global_syms, cu_index,
22084 0);
22085 write_psymbols (symtab,
22086 psyms_seen,
22087 objfile->static_psymbols.list + psymtab->statics_offset,
22088 psymtab->n_static_syms, cu_index,
22089 1);
22090 }
22091
22092 /* Create an index file for OBJFILE in the directory DIR. */
22093
22094 static void
22095 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
22096 {
22097 struct cleanup *cleanup;
22098 char *filename, *cleanup_filename;
22099 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
22100 struct obstack cu_list, types_cu_list;
22101 int i;
22102 FILE *out_file;
22103 struct mapped_symtab *symtab;
22104 offset_type val, size_of_contents, total_len;
22105 struct stat st;
22106 htab_t psyms_seen;
22107 htab_t cu_index_htab;
22108 struct psymtab_cu_index_map *psymtab_cu_index_map;
22109
22110 if (dwarf2_per_objfile->using_index)
22111 error (_("Cannot use an index to create the index"));
22112
22113 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
22114 error (_("Cannot make an index when the file has multiple .debug_types sections"));
22115
22116 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
22117 return;
22118
22119 if (stat (objfile_name (objfile), &st) < 0)
22120 perror_with_name (objfile_name (objfile));
22121
22122 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
22123 INDEX_SUFFIX, (char *) NULL);
22124 cleanup = make_cleanup (xfree, filename);
22125
22126 out_file = gdb_fopen_cloexec (filename, "wb");
22127 if (!out_file)
22128 error (_("Can't open `%s' for writing"), filename);
22129
22130 cleanup_filename = filename;
22131 make_cleanup (unlink_if_set, &cleanup_filename);
22132
22133 symtab = create_mapped_symtab ();
22134 make_cleanup (cleanup_mapped_symtab, symtab);
22135
22136 obstack_init (&addr_obstack);
22137 make_cleanup_obstack_free (&addr_obstack);
22138
22139 obstack_init (&cu_list);
22140 make_cleanup_obstack_free (&cu_list);
22141
22142 obstack_init (&types_cu_list);
22143 make_cleanup_obstack_free (&types_cu_list);
22144
22145 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
22146 NULL, xcalloc, xfree);
22147 make_cleanup_htab_delete (psyms_seen);
22148
22149 /* While we're scanning CU's create a table that maps a psymtab pointer
22150 (which is what addrmap records) to its index (which is what is recorded
22151 in the index file). This will later be needed to write the address
22152 table. */
22153 cu_index_htab = htab_create_alloc (100,
22154 hash_psymtab_cu_index,
22155 eq_psymtab_cu_index,
22156 NULL, xcalloc, xfree);
22157 make_cleanup_htab_delete (cu_index_htab);
22158 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
22159 xmalloc (sizeof (struct psymtab_cu_index_map)
22160 * dwarf2_per_objfile->n_comp_units);
22161 make_cleanup (xfree, psymtab_cu_index_map);
22162
22163 /* The CU list is already sorted, so we don't need to do additional
22164 work here. Also, the debug_types entries do not appear in
22165 all_comp_units, but only in their own hash table. */
22166 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
22167 {
22168 struct dwarf2_per_cu_data *per_cu
22169 = dwarf2_per_objfile->all_comp_units[i];
22170 struct partial_symtab *psymtab = per_cu->v.psymtab;
22171 gdb_byte val[8];
22172 struct psymtab_cu_index_map *map;
22173 void **slot;
22174
22175 /* CU of a shared file from 'dwz -m' may be unused by this main file.
22176 It may be referenced from a local scope but in such case it does not
22177 need to be present in .gdb_index. */
22178 if (psymtab == NULL)
22179 continue;
22180
22181 if (psymtab->user == NULL)
22182 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
22183
22184 map = &psymtab_cu_index_map[i];
22185 map->psymtab = psymtab;
22186 map->cu_index = i;
22187 slot = htab_find_slot (cu_index_htab, map, INSERT);
22188 gdb_assert (slot != NULL);
22189 gdb_assert (*slot == NULL);
22190 *slot = map;
22191
22192 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22193 per_cu->offset.sect_off);
22194 obstack_grow (&cu_list, val, 8);
22195 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
22196 obstack_grow (&cu_list, val, 8);
22197 }
22198
22199 /* Dump the address map. */
22200 write_address_map (objfile, &addr_obstack, cu_index_htab);
22201
22202 /* Write out the .debug_type entries, if any. */
22203 if (dwarf2_per_objfile->signatured_types)
22204 {
22205 struct signatured_type_index_data sig_data;
22206
22207 sig_data.objfile = objfile;
22208 sig_data.symtab = symtab;
22209 sig_data.types_list = &types_cu_list;
22210 sig_data.psyms_seen = psyms_seen;
22211 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
22212 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
22213 write_one_signatured_type, &sig_data);
22214 }
22215
22216 /* Now that we've processed all symbols we can shrink their cu_indices
22217 lists. */
22218 uniquify_cu_indices (symtab);
22219
22220 obstack_init (&constant_pool);
22221 make_cleanup_obstack_free (&constant_pool);
22222 obstack_init (&symtab_obstack);
22223 make_cleanup_obstack_free (&symtab_obstack);
22224 write_hash_table (symtab, &symtab_obstack, &constant_pool);
22225
22226 obstack_init (&contents);
22227 make_cleanup_obstack_free (&contents);
22228 size_of_contents = 6 * sizeof (offset_type);
22229 total_len = size_of_contents;
22230
22231 /* The version number. */
22232 val = MAYBE_SWAP (8);
22233 obstack_grow (&contents, &val, sizeof (val));
22234
22235 /* The offset of the CU list from the start of the file. */
22236 val = MAYBE_SWAP (total_len);
22237 obstack_grow (&contents, &val, sizeof (val));
22238 total_len += obstack_object_size (&cu_list);
22239
22240 /* The offset of the types CU list from the start of the file. */
22241 val = MAYBE_SWAP (total_len);
22242 obstack_grow (&contents, &val, sizeof (val));
22243 total_len += obstack_object_size (&types_cu_list);
22244
22245 /* The offset of the address table from the start of the file. */
22246 val = MAYBE_SWAP (total_len);
22247 obstack_grow (&contents, &val, sizeof (val));
22248 total_len += obstack_object_size (&addr_obstack);
22249
22250 /* The offset of the symbol table from the start of the file. */
22251 val = MAYBE_SWAP (total_len);
22252 obstack_grow (&contents, &val, sizeof (val));
22253 total_len += obstack_object_size (&symtab_obstack);
22254
22255 /* The offset of the constant pool from the start of the file. */
22256 val = MAYBE_SWAP (total_len);
22257 obstack_grow (&contents, &val, sizeof (val));
22258 total_len += obstack_object_size (&constant_pool);
22259
22260 gdb_assert (obstack_object_size (&contents) == size_of_contents);
22261
22262 write_obstack (out_file, &contents);
22263 write_obstack (out_file, &cu_list);
22264 write_obstack (out_file, &types_cu_list);
22265 write_obstack (out_file, &addr_obstack);
22266 write_obstack (out_file, &symtab_obstack);
22267 write_obstack (out_file, &constant_pool);
22268
22269 fclose (out_file);
22270
22271 /* We want to keep the file, so we set cleanup_filename to NULL
22272 here. See unlink_if_set. */
22273 cleanup_filename = NULL;
22274
22275 do_cleanups (cleanup);
22276 }
22277
22278 /* Implementation of the `save gdb-index' command.
22279
22280 Note that the file format used by this command is documented in the
22281 GDB manual. Any changes here must be documented there. */
22282
22283 static void
22284 save_gdb_index_command (char *arg, int from_tty)
22285 {
22286 struct objfile *objfile;
22287
22288 if (!arg || !*arg)
22289 error (_("usage: save gdb-index DIRECTORY"));
22290
22291 ALL_OBJFILES (objfile)
22292 {
22293 struct stat st;
22294
22295 /* If the objfile does not correspond to an actual file, skip it. */
22296 if (stat (objfile_name (objfile), &st) < 0)
22297 continue;
22298
22299 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
22300 if (dwarf2_per_objfile)
22301 {
22302 volatile struct gdb_exception except;
22303
22304 TRY_CATCH (except, RETURN_MASK_ERROR)
22305 {
22306 write_psymtabs_to_index (objfile, arg);
22307 }
22308 if (except.reason < 0)
22309 exception_fprintf (gdb_stderr, except,
22310 _("Error while writing index for `%s': "),
22311 objfile_name (objfile));
22312 }
22313 }
22314 }
22315
22316 \f
22317
22318 int dwarf2_always_disassemble;
22319
22320 static void
22321 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
22322 struct cmd_list_element *c, const char *value)
22323 {
22324 fprintf_filtered (file,
22325 _("Whether to always disassemble "
22326 "DWARF expressions is %s.\n"),
22327 value);
22328 }
22329
22330 static void
22331 show_check_physname (struct ui_file *file, int from_tty,
22332 struct cmd_list_element *c, const char *value)
22333 {
22334 fprintf_filtered (file,
22335 _("Whether to check \"physname\" is %s.\n"),
22336 value);
22337 }
22338
22339 void _initialize_dwarf2_read (void);
22340
22341 void
22342 _initialize_dwarf2_read (void)
22343 {
22344 struct cmd_list_element *c;
22345
22346 dwarf2_objfile_data_key
22347 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
22348
22349 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
22350 Set DWARF 2 specific variables.\n\
22351 Configure DWARF 2 variables such as the cache size"),
22352 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
22353 0/*allow-unknown*/, &maintenance_set_cmdlist);
22354
22355 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
22356 Show DWARF 2 specific variables\n\
22357 Show DWARF 2 variables such as the cache size"),
22358 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
22359 0/*allow-unknown*/, &maintenance_show_cmdlist);
22360
22361 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
22362 &dwarf2_max_cache_age, _("\
22363 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
22364 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
22365 A higher limit means that cached compilation units will be stored\n\
22366 in memory longer, and more total memory will be used. Zero disables\n\
22367 caching, which can slow down startup."),
22368 NULL,
22369 show_dwarf2_max_cache_age,
22370 &set_dwarf2_cmdlist,
22371 &show_dwarf2_cmdlist);
22372
22373 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
22374 &dwarf2_always_disassemble, _("\
22375 Set whether `info address' always disassembles DWARF expressions."), _("\
22376 Show whether `info address' always disassembles DWARF expressions."), _("\
22377 When enabled, DWARF expressions are always printed in an assembly-like\n\
22378 syntax. When disabled, expressions will be printed in a more\n\
22379 conversational style, when possible."),
22380 NULL,
22381 show_dwarf2_always_disassemble,
22382 &set_dwarf2_cmdlist,
22383 &show_dwarf2_cmdlist);
22384
22385 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
22386 Set debugging of the dwarf2 reader."), _("\
22387 Show debugging of the dwarf2 reader."), _("\
22388 When enabled, debugging messages are printed during dwarf2 reading\n\
22389 and symtab expansion."),
22390 NULL,
22391 NULL,
22392 &setdebuglist, &showdebuglist);
22393
22394 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
22395 Set debugging of the dwarf2 DIE reader."), _("\
22396 Show debugging of the dwarf2 DIE reader."), _("\
22397 When enabled (non-zero), DIEs are dumped after they are read in.\n\
22398 The value is the maximum depth to print."),
22399 NULL,
22400 NULL,
22401 &setdebuglist, &showdebuglist);
22402
22403 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
22404 Set cross-checking of \"physname\" code against demangler."), _("\
22405 Show cross-checking of \"physname\" code against demangler."), _("\
22406 When enabled, GDB's internal \"physname\" code is checked against\n\
22407 the demangler."),
22408 NULL, show_check_physname,
22409 &setdebuglist, &showdebuglist);
22410
22411 add_setshow_boolean_cmd ("use-deprecated-index-sections",
22412 no_class, &use_deprecated_index_sections, _("\
22413 Set whether to use deprecated gdb_index sections."), _("\
22414 Show whether to use deprecated gdb_index sections."), _("\
22415 When enabled, deprecated .gdb_index sections are used anyway.\n\
22416 Normally they are ignored either because of a missing feature or\n\
22417 performance issue.\n\
22418 Warning: This option must be enabled before gdb reads the file."),
22419 NULL,
22420 NULL,
22421 &setlist, &showlist);
22422
22423 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
22424 _("\
22425 Save a gdb-index file.\n\
22426 Usage: save gdb-index DIRECTORY"),
22427 &save_cmdlist);
22428 set_cmd_completer (c, filename_completer);
22429
22430 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
22431 &dwarf2_locexpr_funcs);
22432 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
22433 &dwarf2_loclist_funcs);
22434
22435 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
22436 &dwarf2_block_frame_base_locexpr_funcs);
22437 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
22438 &dwarf2_block_frame_base_loclist_funcs);
22439 }
This page took 0.461065 seconds and 5 git commands to generate.