* dwarf2read.c (fixup_go_packaging): Save package name
[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
71 #include <fcntl.h>
72 #include "gdb_string.h"
73 #include "gdb_assert.h"
74 #include <sys/types.h>
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 /* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81 static int dwarf2_read_debug = 0;
82
83 /* When non-zero, dump DIEs after they are read in. */
84 static unsigned int dwarf2_die_debug = 0;
85
86 /* When non-zero, cross-check physname against demangler. */
87 static int check_physname = 0;
88
89 /* When non-zero, do not reject deprecated .gdb_index sections. */
90 static int use_deprecated_index_sections = 0;
91
92 /* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96 static int processing_has_namespace_info;
97
98 static const struct objfile_data *dwarf2_objfile_data_key;
99
100 struct dwarf2_section_info
101 {
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
105 /* True if we have tried to read this section. */
106 int readin;
107 };
108
109 typedef struct dwarf2_section_info dwarf2_section_info_def;
110 DEF_VEC_O (dwarf2_section_info_def);
111
112 /* All offsets in the index are of this type. It must be
113 architecture-independent. */
114 typedef uint32_t offset_type;
115
116 DEF_VEC_I (offset_type);
117
118 /* Ensure only legit values are used. */
119 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125 /* Ensure only legit values are used. */
126 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
134 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
140 /* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142 struct mapped_index
143 {
144 /* Index data format version. */
145 int version;
146
147 /* The total length of the buffer. */
148 off_t total_size;
149
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
152
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
155
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
158
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_table_slots;
161
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164 };
165
166 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167 DEF_VEC_P (dwarf2_per_cu_ptr);
168
169 /* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
172 struct dwarf2_per_objfile
173 {
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
179 struct dwarf2_section_info macro;
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
182 struct dwarf2_section_info addr;
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
185 struct dwarf2_section_info gdb_index;
186
187 VEC (dwarf2_section_info_def) *types;
188
189 /* Back link. */
190 struct objfile *objfile;
191
192 /* Table of all the compilation units. This is used to locate
193 the target compilation unit of a particular reference. */
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
199 /* The number of .debug_types-related CUs. */
200 int n_type_units;
201
202 /* The .debug_types-related CUs (TUs). */
203 struct signatured_type **all_type_units;
204
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
215
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
251
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
254 unsigned char using_index;
255
256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
257 struct mapped_index *index_table;
258
259 /* When using index_table, this keeps track of all quick_file_names entries.
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
266 htab_t quick_file_names_table;
267
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
271
272 /* Table mapping type DIEs to their struct type *.
273 This is NULL if not allocated yet.
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
279 };
280
281 static struct dwarf2_per_objfile *dwarf2_per_objfile;
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
295 { ".debug_macro", ".zdebug_macro" },
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
299 { ".debug_addr", ".zdebug_addr" },
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
302 { ".gdb_index", ".zgdb_index" },
303 23
304 };
305
306 /* List of DWO/DWP sections. */
307
308 static const struct dwop_section_names
309 {
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
321 }
322 dwop_section_names =
323 {
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335 };
336
337 /* local data types */
338
339 /* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341 struct comp_unit_head
342 {
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_offset;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
357 sect_offset offset;
358
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
361 cu_offset first_die_offset;
362 };
363
364 /* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366 struct delayed_method_info
367 {
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382 };
383
384 typedef struct delayed_method_info delayed_method_info;
385 DEF_VEC_O (delayed_method_info);
386
387 /* Internal state when decoding a particular compilation unit. */
388 struct dwarf2_cu
389 {
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header;
395
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
406 const char *producer;
407
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
423
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
446 htab_t die_hash;
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_ranges_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
492 ULONGEST ranges_base;
493
494 /* Mark used when releasing cached dies. */
495 unsigned int mark : 1;
496
497 /* This CU references .debug_loc. See the symtab->locations_valid field.
498 This test is imperfect as there may exist optimized debug code not using
499 any location list and still facing inlining issues if handled as
500 unoptimized code. For a future better test see GCC PR other/32998. */
501 unsigned int has_loclist : 1;
502
503 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
504 if all the producer_is_* fields are valid. This information is cached
505 because profiling CU expansion showed excessive time spent in
506 producer_is_gxx_lt_4_6. */
507 unsigned int checked_producer : 1;
508 unsigned int producer_is_gxx_lt_4_6 : 1;
509 unsigned int producer_is_gcc_lt_4_3 : 1;
510 unsigned int producer_is_icc : 1;
511 };
512
513 /* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
515 read_symtab_private field of the psymtab. */
516
517 struct dwarf2_per_cu_data
518 {
519 /* The start offset and length of this compilation unit.
520 NOTE: Unlike comp_unit_head.length, this length includes
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
524 sect_offset offset;
525 unsigned int length;
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
529 unsigned int queued : 1;
530
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
547
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
550 struct dwarf2_cu *cu;
551
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
562 or NULL for unread partial units. */
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
568
569 union
570 {
571 /* The CUs we import using DW_TAG_imported_unit. This is filled in
572 while reading psymtabs, used to compute the psymtab dependencies,
573 and then cleared. Then it is filled in again while reading full
574 symbols, and only deleted when the objfile is destroyed. */
575 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
576
577 /* Type units are grouped by their DW_AT_stmt_list entry so that they
578 can share them. If this is a TU, this points to the containing
579 symtab. */
580 struct type_unit_group *type_unit_group;
581 } s;
582 };
583
584 /* Entry in the signatured_types hash table. */
585
586 struct signatured_type
587 {
588 /* The "per_cu" object of this type.
589 N.B.: This is the first member so that it's easy to convert pointers
590 between them. */
591 struct dwarf2_per_cu_data per_cu;
592
593 /* The type's signature. */
594 ULONGEST signature;
595
596 /* Offset in the TU of the type's DIE, as read from the TU header.
597 If the definition lives in a DWO file, this value is unusable. */
598 cu_offset type_offset_in_tu;
599
600 /* Offset in the section of the type's DIE.
601 If the definition lives in a DWO file, this is the offset in the
602 .debug_types.dwo section.
603 The value is zero until the actual value is known.
604 Zero is otherwise not a valid section offset. */
605 sect_offset type_offset_in_section;
606 };
607
608 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611 struct stmt_list_hash
612 {
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_offset;
618 };
619
620 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623 struct type_unit_group
624 {
625 /* dwarf2read.c's main "handle" on the symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
631 struct dwarf2_per_cu_data per_cu;
632
633 union
634 {
635 /* The TUs that share this DW_AT_stmt_list entry.
636 This is added to while parsing type units to build partial symtabs,
637 and is deleted afterwards and not used again. */
638 VEC (dwarf2_per_cu_ptr) *tus;
639
640 /* When reading the line table in "quick" functions, we need a real TU.
641 Any will do, we know they all share the same DW_AT_stmt_list entry.
642 For simplicity's sake, we pick the first one. */
643 struct dwarf2_per_cu_data *first_tu;
644 } t;
645
646 /* The primary symtab.
647 Type units in a group needn't all be defined in the same source file,
648 so we create an essentially anonymous symtab as the primary symtab. */
649 struct symtab *primary_symtab;
650
651 /* The data used to construct the hash key. */
652 struct stmt_list_hash hash;
653
654 /* The number of symtabs from the line header.
655 The value here must match line_header.num_file_names. */
656 unsigned int num_symtabs;
657
658 /* The symbol tables for this TU (obtained from the files listed in
659 DW_AT_stmt_list).
660 WARNING: The order of entries here must match the order of entries
661 in the line header. After the first TU using this type_unit_group, the
662 line header for the subsequent TUs is recreated from this. This is done
663 because we need to use the same symtabs for each TU using the same
664 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
665 there's no guarantee the line header doesn't have duplicate entries. */
666 struct symtab **symtabs;
667 };
668
669 /* These sections are what may appear in a DWO file. */
670
671 struct dwo_sections
672 {
673 struct dwarf2_section_info abbrev;
674 struct dwarf2_section_info line;
675 struct dwarf2_section_info loc;
676 struct dwarf2_section_info macinfo;
677 struct dwarf2_section_info macro;
678 struct dwarf2_section_info str;
679 struct dwarf2_section_info str_offsets;
680 /* In the case of a virtual DWO file, these two are unused. */
681 struct dwarf2_section_info info;
682 VEC (dwarf2_section_info_def) *types;
683 };
684
685 /* Common bits of DWO CUs/TUs. */
686
687 struct dwo_unit
688 {
689 /* Backlink to the containing struct dwo_file. */
690 struct dwo_file *dwo_file;
691
692 /* The "id" that distinguishes this CU/TU.
693 .debug_info calls this "dwo_id", .debug_types calls this "signature".
694 Since signatures came first, we stick with it for consistency. */
695 ULONGEST signature;
696
697 /* The section this CU/TU lives in, in the DWO file. */
698 struct dwarf2_section_info *info_or_types_section;
699
700 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
701 sect_offset offset;
702 unsigned int length;
703
704 /* For types, offset in the type's DIE of the type defined by this TU. */
705 cu_offset type_offset_in_tu;
706 };
707
708 /* Data for one DWO file.
709 This includes virtual DWO files that have been packaged into a
710 DWP file. */
711
712 struct dwo_file
713 {
714 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *name;
719
720 /* The bfd, when the file is open. Otherwise this is NULL.
721 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
722 bfd *dbfd;
723
724 /* Section info for this file. */
725 struct dwo_sections sections;
726
727 /* Table of CUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_t cus;
730
731 /* Table of TUs in the file.
732 Each element is a struct dwo_unit. */
733 htab_t tus;
734 };
735
736 /* These sections are what may appear in a DWP file. */
737
738 struct dwp_sections
739 {
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
744 by section number. We don't need to record them here. */
745 };
746
747 /* These sections are what may appear in a virtual DWO file. */
748
749 struct virtual_dwo_sections
750 {
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info line;
753 struct dwarf2_section_info loc;
754 struct dwarf2_section_info macinfo;
755 struct dwarf2_section_info macro;
756 struct dwarf2_section_info str_offsets;
757 /* Each DWP hash table entry records one CU or one TU.
758 That is recorded here, and copied to dwo_unit.info_or_types_section. */
759 struct dwarf2_section_info info_or_types;
760 };
761
762 /* Contents of DWP hash tables. */
763
764 struct dwp_hash_table
765 {
766 uint32_t nr_units, nr_slots;
767 const gdb_byte *hash_table, *unit_table, *section_pool;
768 };
769
770 /* Data for one DWP file. */
771
772 struct dwp_file
773 {
774 /* Name of the file. */
775 const char *name;
776
777 /* The bfd, when the file is open. Otherwise this is NULL. */
778 bfd *dbfd;
779
780 /* Section info for this file. */
781 struct dwp_sections sections;
782
783 /* Table of CUs in the file. */
784 const struct dwp_hash_table *cus;
785
786 /* Table of TUs in the file. */
787 const struct dwp_hash_table *tus;
788
789 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
790 htab_t loaded_cutus;
791
792 /* Table to map ELF section numbers to their sections. */
793 unsigned int num_sections;
794 asection **elf_sections;
795 };
796
797 /* This represents a '.dwz' file. */
798
799 struct dwz_file
800 {
801 /* A dwz file can only contain a few sections. */
802 struct dwarf2_section_info abbrev;
803 struct dwarf2_section_info info;
804 struct dwarf2_section_info str;
805 struct dwarf2_section_info line;
806 struct dwarf2_section_info macro;
807 struct dwarf2_section_info gdb_index;
808
809 /* The dwz's BFD. */
810 bfd *dwz_bfd;
811 };
812
813 /* Struct used to pass misc. parameters to read_die_and_children, et
814 al. which are used for both .debug_info and .debug_types dies.
815 All parameters here are unchanging for the life of the call. This
816 struct exists to abstract away the constant parameters of die reading. */
817
818 struct die_reader_specs
819 {
820 /* die_section->asection->owner. */
821 bfd* abfd;
822
823 /* The CU of the DIE we are parsing. */
824 struct dwarf2_cu *cu;
825
826 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
827 struct dwo_file *dwo_file;
828
829 /* The section the die comes from.
830 This is either .debug_info or .debug_types, or the .dwo variants. */
831 struct dwarf2_section_info *die_section;
832
833 /* die_section->buffer. */
834 gdb_byte *buffer;
835
836 /* The end of the buffer. */
837 const gdb_byte *buffer_end;
838 };
839
840 /* Type of function passed to init_cutu_and_read_dies, et.al. */
841 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
842 gdb_byte *info_ptr,
843 struct die_info *comp_unit_die,
844 int has_children,
845 void *data);
846
847 /* The line number information for a compilation unit (found in the
848 .debug_line section) begins with a "statement program header",
849 which contains the following information. */
850 struct line_header
851 {
852 unsigned int total_length;
853 unsigned short version;
854 unsigned int header_length;
855 unsigned char minimum_instruction_length;
856 unsigned char maximum_ops_per_instruction;
857 unsigned char default_is_stmt;
858 int line_base;
859 unsigned char line_range;
860 unsigned char opcode_base;
861
862 /* standard_opcode_lengths[i] is the number of operands for the
863 standard opcode whose value is i. This means that
864 standard_opcode_lengths[0] is unused, and the last meaningful
865 element is standard_opcode_lengths[opcode_base - 1]. */
866 unsigned char *standard_opcode_lengths;
867
868 /* The include_directories table. NOTE! These strings are not
869 allocated with xmalloc; instead, they are pointers into
870 debug_line_buffer. If you try to free them, `free' will get
871 indigestion. */
872 unsigned int num_include_dirs, include_dirs_size;
873 char **include_dirs;
874
875 /* The file_names table. NOTE! These strings are not allocated
876 with xmalloc; instead, they are pointers into debug_line_buffer.
877 Don't try to free them directly. */
878 unsigned int num_file_names, file_names_size;
879 struct file_entry
880 {
881 char *name;
882 unsigned int dir_index;
883 unsigned int mod_time;
884 unsigned int length;
885 int included_p; /* Non-zero if referenced by the Line Number Program. */
886 struct symtab *symtab; /* The associated symbol table, if any. */
887 } *file_names;
888
889 /* The start and end of the statement program following this
890 header. These point into dwarf2_per_objfile->line_buffer. */
891 gdb_byte *statement_program_start, *statement_program_end;
892 };
893
894 /* When we construct a partial symbol table entry we only
895 need this much information. */
896 struct partial_die_info
897 {
898 /* Offset of this DIE. */
899 sect_offset offset;
900
901 /* DWARF-2 tag for this DIE. */
902 ENUM_BITFIELD(dwarf_tag) tag : 16;
903
904 /* Assorted flags describing the data found in this DIE. */
905 unsigned int has_children : 1;
906 unsigned int is_external : 1;
907 unsigned int is_declaration : 1;
908 unsigned int has_type : 1;
909 unsigned int has_specification : 1;
910 unsigned int has_pc_info : 1;
911 unsigned int may_be_inlined : 1;
912
913 /* Flag set if the SCOPE field of this structure has been
914 computed. */
915 unsigned int scope_set : 1;
916
917 /* Flag set if the DIE has a byte_size attribute. */
918 unsigned int has_byte_size : 1;
919
920 /* Flag set if any of the DIE's children are template arguments. */
921 unsigned int has_template_arguments : 1;
922
923 /* Flag set if fixup_partial_die has been called on this die. */
924 unsigned int fixup_called : 1;
925
926 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
927 unsigned int is_dwz : 1;
928
929 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
930 unsigned int spec_is_dwz : 1;
931
932 /* The name of this DIE. Normally the value of DW_AT_name, but
933 sometimes a default name for unnamed DIEs. */
934 const char *name;
935
936 /* The linkage name, if present. */
937 const char *linkage_name;
938
939 /* The scope to prepend to our children. This is generally
940 allocated on the comp_unit_obstack, so will disappear
941 when this compilation unit leaves the cache. */
942 const char *scope;
943
944 /* Some data associated with the partial DIE. The tag determines
945 which field is live. */
946 union
947 {
948 /* The location description associated with this DIE, if any. */
949 struct dwarf_block *locdesc;
950 /* The offset of an import, for DW_TAG_imported_unit. */
951 sect_offset offset;
952 } d;
953
954 /* If HAS_PC_INFO, the PC range associated with this DIE. */
955 CORE_ADDR lowpc;
956 CORE_ADDR highpc;
957
958 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
959 DW_AT_sibling, if any. */
960 /* NOTE: This member isn't strictly necessary, read_partial_die could
961 return DW_AT_sibling values to its caller load_partial_dies. */
962 gdb_byte *sibling;
963
964 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
965 DW_AT_specification (or DW_AT_abstract_origin or
966 DW_AT_extension). */
967 sect_offset spec_offset;
968
969 /* Pointers to this DIE's parent, first child, and next sibling,
970 if any. */
971 struct partial_die_info *die_parent, *die_child, *die_sibling;
972 };
973
974 /* This data structure holds the information of an abbrev. */
975 struct abbrev_info
976 {
977 unsigned int number; /* number identifying abbrev */
978 enum dwarf_tag tag; /* dwarf tag */
979 unsigned short has_children; /* boolean */
980 unsigned short num_attrs; /* number of attributes */
981 struct attr_abbrev *attrs; /* an array of attribute descriptions */
982 struct abbrev_info *next; /* next in chain */
983 };
984
985 struct attr_abbrev
986 {
987 ENUM_BITFIELD(dwarf_attribute) name : 16;
988 ENUM_BITFIELD(dwarf_form) form : 16;
989 };
990
991 /* Size of abbrev_table.abbrev_hash_table. */
992 #define ABBREV_HASH_SIZE 121
993
994 /* Top level data structure to contain an abbreviation table. */
995
996 struct abbrev_table
997 {
998 /* Where the abbrev table came from.
999 This is used as a sanity check when the table is used. */
1000 sect_offset offset;
1001
1002 /* Storage for the abbrev table. */
1003 struct obstack abbrev_obstack;
1004
1005 /* Hash table of abbrevs.
1006 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1007 It could be statically allocated, but the previous code didn't so we
1008 don't either. */
1009 struct abbrev_info **abbrevs;
1010 };
1011
1012 /* Attributes have a name and a value. */
1013 struct attribute
1014 {
1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
1016 ENUM_BITFIELD(dwarf_form) form : 15;
1017
1018 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1019 field should be in u.str (existing only for DW_STRING) but it is kept
1020 here for better struct attribute alignment. */
1021 unsigned int string_is_canonical : 1;
1022
1023 union
1024 {
1025 const char *str;
1026 struct dwarf_block *blk;
1027 ULONGEST unsnd;
1028 LONGEST snd;
1029 CORE_ADDR addr;
1030 struct signatured_type *signatured_type;
1031 }
1032 u;
1033 };
1034
1035 /* This data structure holds a complete die structure. */
1036 struct die_info
1037 {
1038 /* DWARF-2 tag for this DIE. */
1039 ENUM_BITFIELD(dwarf_tag) tag : 16;
1040
1041 /* Number of attributes */
1042 unsigned char num_attrs;
1043
1044 /* True if we're presently building the full type name for the
1045 type derived from this DIE. */
1046 unsigned char building_fullname : 1;
1047
1048 /* Abbrev number */
1049 unsigned int abbrev;
1050
1051 /* Offset in .debug_info or .debug_types section. */
1052 sect_offset offset;
1053
1054 /* The dies in a compilation unit form an n-ary tree. PARENT
1055 points to this die's parent; CHILD points to the first child of
1056 this node; and all the children of a given node are chained
1057 together via their SIBLING fields. */
1058 struct die_info *child; /* Its first child, if any. */
1059 struct die_info *sibling; /* Its next sibling, if any. */
1060 struct die_info *parent; /* Its parent, if any. */
1061
1062 /* An array of attributes, with NUM_ATTRS elements. There may be
1063 zero, but it's not common and zero-sized arrays are not
1064 sufficiently portable C. */
1065 struct attribute attrs[1];
1066 };
1067
1068 /* Get at parts of an attribute structure. */
1069
1070 #define DW_STRING(attr) ((attr)->u.str)
1071 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1072 #define DW_UNSND(attr) ((attr)->u.unsnd)
1073 #define DW_BLOCK(attr) ((attr)->u.blk)
1074 #define DW_SND(attr) ((attr)->u.snd)
1075 #define DW_ADDR(attr) ((attr)->u.addr)
1076 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1077
1078 /* Blocks are a bunch of untyped bytes. */
1079 struct dwarf_block
1080 {
1081 size_t size;
1082
1083 /* Valid only if SIZE is not zero. */
1084 gdb_byte *data;
1085 };
1086
1087 #ifndef ATTR_ALLOC_CHUNK
1088 #define ATTR_ALLOC_CHUNK 4
1089 #endif
1090
1091 /* Allocate fields for structs, unions and enums in this size. */
1092 #ifndef DW_FIELD_ALLOC_CHUNK
1093 #define DW_FIELD_ALLOC_CHUNK 4
1094 #endif
1095
1096 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099 static int bits_per_byte = 8;
1100
1101 /* The routines that read and process dies for a C struct or C++ class
1102 pass lists of data member fields and lists of member function fields
1103 in an instance of a field_info structure, as defined below. */
1104 struct field_info
1105 {
1106 /* List of data member and baseclasses fields. */
1107 struct nextfield
1108 {
1109 struct nextfield *next;
1110 int accessibility;
1111 int virtuality;
1112 struct field field;
1113 }
1114 *fields, *baseclasses;
1115
1116 /* Number of fields (including baseclasses). */
1117 int nfields;
1118
1119 /* Number of baseclasses. */
1120 int nbaseclasses;
1121
1122 /* Set if the accesibility of one of the fields is not public. */
1123 int non_public_fields;
1124
1125 /* Member function fields array, entries are allocated in the order they
1126 are encountered in the object file. */
1127 struct nextfnfield
1128 {
1129 struct nextfnfield *next;
1130 struct fn_field fnfield;
1131 }
1132 *fnfields;
1133
1134 /* Member function fieldlist array, contains name of possibly overloaded
1135 member function, number of overloaded member functions and a pointer
1136 to the head of the member function field chain. */
1137 struct fnfieldlist
1138 {
1139 const char *name;
1140 int length;
1141 struct nextfnfield *head;
1142 }
1143 *fnfieldlists;
1144
1145 /* Number of entries in the fnfieldlists array. */
1146 int nfnfields;
1147
1148 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1149 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1150 struct typedef_field_list
1151 {
1152 struct typedef_field field;
1153 struct typedef_field_list *next;
1154 }
1155 *typedef_field_list;
1156 unsigned typedef_field_list_count;
1157 };
1158
1159 /* One item on the queue of compilation units to read in full symbols
1160 for. */
1161 struct dwarf2_queue_item
1162 {
1163 struct dwarf2_per_cu_data *per_cu;
1164 enum language pretend_language;
1165 struct dwarf2_queue_item *next;
1166 };
1167
1168 /* The current queue. */
1169 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1170
1171 /* Loaded secondary compilation units are kept in memory until they
1172 have not been referenced for the processing of this many
1173 compilation units. Set this to zero to disable caching. Cache
1174 sizes of up to at least twenty will improve startup time for
1175 typical inter-CU-reference binaries, at an obvious memory cost. */
1176 static int dwarf2_max_cache_age = 5;
1177 static void
1178 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1179 struct cmd_list_element *c, const char *value)
1180 {
1181 fprintf_filtered (file, _("The upper bound on the age of cached "
1182 "dwarf2 compilation units is %s.\n"),
1183 value);
1184 }
1185
1186
1187 /* Various complaints about symbol reading that don't abort the process. */
1188
1189 static void
1190 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1191 {
1192 complaint (&symfile_complaints,
1193 _("statement list doesn't fit in .debug_line section"));
1194 }
1195
1196 static void
1197 dwarf2_debug_line_missing_file_complaint (void)
1198 {
1199 complaint (&symfile_complaints,
1200 _(".debug_line section has line data without a file"));
1201 }
1202
1203 static void
1204 dwarf2_debug_line_missing_end_sequence_complaint (void)
1205 {
1206 complaint (&symfile_complaints,
1207 _(".debug_line section has line "
1208 "program sequence without an end"));
1209 }
1210
1211 static void
1212 dwarf2_complex_location_expr_complaint (void)
1213 {
1214 complaint (&symfile_complaints, _("location expression too complex"));
1215 }
1216
1217 static void
1218 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1219 int arg3)
1220 {
1221 complaint (&symfile_complaints,
1222 _("const value length mismatch for '%s', got %d, expected %d"),
1223 arg1, arg2, arg3);
1224 }
1225
1226 static void
1227 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1228 {
1229 complaint (&symfile_complaints,
1230 _("debug info runs off end of %s section"
1231 " [in module %s]"),
1232 section->asection->name,
1233 bfd_get_filename (section->asection->owner));
1234 }
1235
1236 static void
1237 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1238 {
1239 complaint (&symfile_complaints,
1240 _("macro debug info contains a "
1241 "malformed macro definition:\n`%s'"),
1242 arg1);
1243 }
1244
1245 static void
1246 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1247 {
1248 complaint (&symfile_complaints,
1249 _("invalid attribute class or form for '%s' in '%s'"),
1250 arg1, arg2);
1251 }
1252
1253 /* local function prototypes */
1254
1255 static void dwarf2_locate_sections (bfd *, asection *, void *);
1256
1257 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1258 struct objfile *);
1259
1260 static void dwarf2_find_base_address (struct die_info *die,
1261 struct dwarf2_cu *cu);
1262
1263 static void dwarf2_build_psymtabs_hard (struct objfile *);
1264
1265 static void scan_partial_symbols (struct partial_die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
1267 int, struct dwarf2_cu *);
1268
1269 static void add_partial_symbol (struct partial_die_info *,
1270 struct dwarf2_cu *);
1271
1272 static void add_partial_namespace (struct partial_die_info *pdi,
1273 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1274 int need_pc, struct dwarf2_cu *cu);
1275
1276 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1277 CORE_ADDR *highpc, int need_pc,
1278 struct dwarf2_cu *cu);
1279
1280 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1281 struct dwarf2_cu *cu);
1282
1283 static void add_partial_subprogram (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1285 int need_pc, struct dwarf2_cu *cu);
1286
1287 static void dwarf2_read_symtab (struct partial_symtab *,
1288 struct objfile *);
1289
1290 static void psymtab_to_symtab_1 (struct partial_symtab *);
1291
1292 static struct abbrev_info *abbrev_table_lookup_abbrev
1293 (const struct abbrev_table *, unsigned int);
1294
1295 static struct abbrev_table *abbrev_table_read_table
1296 (struct dwarf2_section_info *, sect_offset);
1297
1298 static void abbrev_table_free (struct abbrev_table *);
1299
1300 static void abbrev_table_free_cleanup (void *);
1301
1302 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1303 struct dwarf2_section_info *);
1304
1305 static void dwarf2_free_abbrev_table (void *);
1306
1307 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1308
1309 static struct partial_die_info *load_partial_dies
1310 (const struct die_reader_specs *, gdb_byte *, int);
1311
1312 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1313 struct partial_die_info *,
1314 struct abbrev_info *,
1315 unsigned int,
1316 gdb_byte *);
1317
1318 static struct partial_die_info *find_partial_die (sect_offset, int,
1319 struct dwarf2_cu *);
1320
1321 static void fixup_partial_die (struct partial_die_info *,
1322 struct dwarf2_cu *);
1323
1324 static gdb_byte *read_attribute (const struct die_reader_specs *,
1325 struct attribute *, struct attr_abbrev *,
1326 gdb_byte *);
1327
1328 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1329
1330 static int read_1_signed_byte (bfd *, const gdb_byte *);
1331
1332 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1333
1334 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1335
1336 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1337
1338 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1339 unsigned int *);
1340
1341 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1342
1343 static LONGEST read_checked_initial_length_and_offset
1344 (bfd *, gdb_byte *, const struct comp_unit_head *,
1345 unsigned int *, unsigned int *);
1346
1347 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1348 unsigned int *);
1349
1350 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1351
1352 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1353 sect_offset);
1354
1355 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1356
1357 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1358
1359 static char *read_indirect_string (bfd *, gdb_byte *,
1360 const struct comp_unit_head *,
1361 unsigned int *);
1362
1363 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1364
1365 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1366
1367 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1368
1369 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1370 unsigned int *);
1371
1372 static char *read_str_index (const struct die_reader_specs *reader,
1373 struct dwarf2_cu *cu, ULONGEST str_index);
1374
1375 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1376
1377 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1378 struct dwarf2_cu *);
1379
1380 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1381 unsigned int);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static void free_line_header (struct line_header *lh);
1392
1393 static void add_file_name (struct line_header *, char *, unsigned int,
1394 unsigned int, unsigned int);
1395
1396 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1397 struct dwarf2_cu *cu);
1398
1399 static void dwarf_decode_lines (struct line_header *, const char *,
1400 struct dwarf2_cu *, struct partial_symtab *,
1401 int);
1402
1403 static void dwarf2_start_subfile (char *, const char *, const char *);
1404
1405 static void dwarf2_start_symtab (struct dwarf2_cu *,
1406 const char *, const char *, CORE_ADDR);
1407
1408 static struct symbol *new_symbol (struct die_info *, struct type *,
1409 struct dwarf2_cu *);
1410
1411 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1412 struct dwarf2_cu *, struct symbol *);
1413
1414 static void dwarf2_const_value (struct attribute *, struct symbol *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_const_value_attr (struct attribute *attr,
1418 struct type *type,
1419 const char *name,
1420 struct obstack *obstack,
1421 struct dwarf2_cu *cu, LONGEST *value,
1422 gdb_byte **bytes,
1423 struct dwarf2_locexpr_baton **baton);
1424
1425 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1426
1427 static int need_gnat_info (struct dwarf2_cu *);
1428
1429 static struct type *die_descriptive_type (struct die_info *,
1430 struct dwarf2_cu *);
1431
1432 static void set_descriptive_type (struct type *, struct die_info *,
1433 struct dwarf2_cu *);
1434
1435 static struct type *die_containing_type (struct die_info *,
1436 struct dwarf2_cu *);
1437
1438 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1439 struct dwarf2_cu *);
1440
1441 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1442
1443 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1444
1445 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1446
1447 static char *typename_concat (struct obstack *obs, const char *prefix,
1448 const char *suffix, int physname,
1449 struct dwarf2_cu *cu);
1450
1451 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1452
1453 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1454
1455 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1456
1457 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1458
1459 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1460
1461 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1462 struct dwarf2_cu *, struct partial_symtab *);
1463
1464 static int dwarf2_get_pc_bounds (struct die_info *,
1465 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1466 struct partial_symtab *);
1467
1468 static void get_scope_pc_bounds (struct die_info *,
1469 CORE_ADDR *, CORE_ADDR *,
1470 struct dwarf2_cu *);
1471
1472 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1473 CORE_ADDR, struct dwarf2_cu *);
1474
1475 static void dwarf2_add_field (struct field_info *, struct die_info *,
1476 struct dwarf2_cu *);
1477
1478 static void dwarf2_attach_fields_to_type (struct field_info *,
1479 struct type *, struct dwarf2_cu *);
1480
1481 static void dwarf2_add_member_fn (struct field_info *,
1482 struct die_info *, struct type *,
1483 struct dwarf2_cu *);
1484
1485 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1486 struct type *,
1487 struct dwarf2_cu *);
1488
1489 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1490
1491 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1492
1493 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1494
1495 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1496
1497 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1498
1499 static struct type *read_module_type (struct die_info *die,
1500 struct dwarf2_cu *cu);
1501
1502 static const char *namespace_name (struct die_info *die,
1503 int *is_anonymous, struct dwarf2_cu *);
1504
1505 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1506
1507 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1508
1509 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1510 struct dwarf2_cu *);
1511
1512 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1513 gdb_byte *info_ptr,
1514 gdb_byte **new_info_ptr,
1515 struct die_info *parent);
1516
1517 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1518 gdb_byte *info_ptr,
1519 gdb_byte **new_info_ptr,
1520 struct die_info *parent);
1521
1522 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1523 struct die_info **, gdb_byte *, int *, int);
1524
1525 static gdb_byte *read_full_die (const struct die_reader_specs *,
1526 struct die_info **, gdb_byte *, int *);
1527
1528 static void process_die (struct die_info *, struct dwarf2_cu *);
1529
1530 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1531 struct obstack *);
1532
1533 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1534
1535 static const char *dwarf2_full_name (const char *name,
1536 struct die_info *die,
1537 struct dwarf2_cu *cu);
1538
1539 static struct die_info *dwarf2_extension (struct die_info *die,
1540 struct dwarf2_cu **);
1541
1542 static const char *dwarf_tag_name (unsigned int);
1543
1544 static const char *dwarf_attr_name (unsigned int);
1545
1546 static const char *dwarf_form_name (unsigned int);
1547
1548 static char *dwarf_bool_name (unsigned int);
1549
1550 static const char *dwarf_type_encoding_name (unsigned int);
1551
1552 static struct die_info *sibling_die (struct die_info *);
1553
1554 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1555
1556 static void dump_die_for_error (struct die_info *);
1557
1558 static void dump_die_1 (struct ui_file *, int level, int max_level,
1559 struct die_info *);
1560
1561 /*static*/ void dump_die (struct die_info *, int max_level);
1562
1563 static void store_in_ref_table (struct die_info *,
1564 struct dwarf2_cu *);
1565
1566 static int is_ref_attr (struct attribute *);
1567
1568 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1569
1570 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1571
1572 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1573 struct attribute *,
1574 struct dwarf2_cu **);
1575
1576 static struct die_info *follow_die_ref (struct die_info *,
1577 struct attribute *,
1578 struct dwarf2_cu **);
1579
1580 static struct die_info *follow_die_sig (struct die_info *,
1581 struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct signatured_type *lookup_signatured_type_at_offset
1585 (struct objfile *objfile,
1586 struct dwarf2_section_info *section, sect_offset offset);
1587
1588 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1589
1590 static void read_signatured_type (struct signatured_type *);
1591
1592 static struct type_unit_group *get_type_unit_group
1593 (struct dwarf2_cu *, struct attribute *);
1594
1595 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1596
1597 /* memory allocation interface */
1598
1599 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1600
1601 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1602
1603 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1604 const char *, int);
1605
1606 static int attr_form_is_block (struct attribute *);
1607
1608 static int attr_form_is_section_offset (struct attribute *);
1609
1610 static int attr_form_is_constant (struct attribute *);
1611
1612 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1613 struct dwarf2_loclist_baton *baton,
1614 struct attribute *attr);
1615
1616 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1617 struct symbol *sym,
1618 struct dwarf2_cu *cu);
1619
1620 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1621 gdb_byte *info_ptr,
1622 struct abbrev_info *abbrev);
1623
1624 static void free_stack_comp_unit (void *);
1625
1626 static hashval_t partial_die_hash (const void *item);
1627
1628 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1629
1630 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1631 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1632
1633 static void init_one_comp_unit (struct dwarf2_cu *cu,
1634 struct dwarf2_per_cu_data *per_cu);
1635
1636 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
1639
1640 static void free_heap_comp_unit (void *);
1641
1642 static void free_cached_comp_units (void *);
1643
1644 static void age_cached_comp_units (void);
1645
1646 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1647
1648 static struct type *set_die_type (struct die_info *, struct type *,
1649 struct dwarf2_cu *);
1650
1651 static void create_all_comp_units (struct objfile *);
1652
1653 static int create_all_type_units (struct objfile *);
1654
1655 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
1658 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1659 enum language);
1660
1661 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1662 enum language);
1663
1664 static void dwarf2_add_dependence (struct dwarf2_cu *,
1665 struct dwarf2_per_cu_data *);
1666
1667 static void dwarf2_mark (struct dwarf2_cu *);
1668
1669 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1670
1671 static struct type *get_die_type_at_offset (sect_offset,
1672 struct dwarf2_per_cu_data *per_cu);
1673
1674 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1675
1676 static void dwarf2_release_queue (void *dummy);
1677
1678 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1679 enum language pretend_language);
1680
1681 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1682 struct dwarf2_per_cu_data *per_cu,
1683 enum language pretend_language);
1684
1685 static void process_queue (void);
1686
1687 static void find_file_and_directory (struct die_info *die,
1688 struct dwarf2_cu *cu,
1689 const char **name, const char **comp_dir);
1690
1691 static char *file_full_name (int file, struct line_header *lh,
1692 const char *comp_dir);
1693
1694 static gdb_byte *read_and_check_comp_unit_head
1695 (struct comp_unit_head *header,
1696 struct dwarf2_section_info *section,
1697 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1698 int is_debug_types_section);
1699
1700 static void init_cutu_and_read_dies
1701 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1702 int use_existing_cu, int keep,
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
1705 static void init_cutu_and_read_dies_simple
1706 (struct dwarf2_per_cu_data *this_cu,
1707 die_reader_func_ftype *die_reader_func, void *data);
1708
1709 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1710
1711 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1712
1713 static struct dwo_unit *lookup_dwo_comp_unit
1714 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1715
1716 static struct dwo_unit *lookup_dwo_type_unit
1717 (struct signatured_type *, const char *, const char *);
1718
1719 static void free_dwo_file_cleanup (void *);
1720
1721 static void process_cu_includes (void);
1722
1723 static void check_producer (struct dwarf2_cu *cu);
1724
1725 #if WORDS_BIGENDIAN
1726
1727 /* Convert VALUE between big- and little-endian. */
1728 static offset_type
1729 byte_swap (offset_type value)
1730 {
1731 offset_type result;
1732
1733 result = (value & 0xff) << 24;
1734 result |= (value & 0xff00) << 8;
1735 result |= (value & 0xff0000) >> 8;
1736 result |= (value & 0xff000000) >> 24;
1737 return result;
1738 }
1739
1740 #define MAYBE_SWAP(V) byte_swap (V)
1741
1742 #else
1743 #define MAYBE_SWAP(V) (V)
1744 #endif /* WORDS_BIGENDIAN */
1745
1746 /* The suffix for an index file. */
1747 #define INDEX_SUFFIX ".gdb-index"
1748
1749 static const char *dwarf2_physname (const char *name, struct die_info *die,
1750 struct dwarf2_cu *cu);
1751
1752 /* Try to locate the sections we need for DWARF 2 debugging
1753 information and return true if we have enough to do something.
1754 NAMES points to the dwarf2 section names, or is NULL if the standard
1755 ELF names are used. */
1756
1757 int
1758 dwarf2_has_info (struct objfile *objfile,
1759 const struct dwarf2_debug_sections *names)
1760 {
1761 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1762 if (!dwarf2_per_objfile)
1763 {
1764 /* Initialize per-objfile state. */
1765 struct dwarf2_per_objfile *data
1766 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1767
1768 memset (data, 0, sizeof (*data));
1769 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1770 dwarf2_per_objfile = data;
1771
1772 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1773 (void *) names);
1774 dwarf2_per_objfile->objfile = objfile;
1775 }
1776 return (dwarf2_per_objfile->info.asection != NULL
1777 && dwarf2_per_objfile->abbrev.asection != NULL);
1778 }
1779
1780 /* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
1782
1783 static int
1784 section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
1786 {
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
1794 }
1795
1796 /* This function is mapped across the sections and remembers the
1797 offset and size of each of the debugging sections we are interested
1798 in. */
1799
1800 static void
1801 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1802 {
1803 const struct dwarf2_debug_sections *names;
1804 flagword aflag = bfd_get_section_flags (abfd, sectp);
1805
1806 if (vnames == NULL)
1807 names = &dwarf2_elf_names;
1808 else
1809 names = (const struct dwarf2_debug_sections *) vnames;
1810
1811 if ((aflag & SEC_HAS_CONTENTS) == 0)
1812 {
1813 }
1814 else if (section_is_p (sectp->name, &names->info))
1815 {
1816 dwarf2_per_objfile->info.asection = sectp;
1817 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1818 }
1819 else if (section_is_p (sectp->name, &names->abbrev))
1820 {
1821 dwarf2_per_objfile->abbrev.asection = sectp;
1822 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1823 }
1824 else if (section_is_p (sectp->name, &names->line))
1825 {
1826 dwarf2_per_objfile->line.asection = sectp;
1827 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1828 }
1829 else if (section_is_p (sectp->name, &names->loc))
1830 {
1831 dwarf2_per_objfile->loc.asection = sectp;
1832 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1833 }
1834 else if (section_is_p (sectp->name, &names->macinfo))
1835 {
1836 dwarf2_per_objfile->macinfo.asection = sectp;
1837 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1838 }
1839 else if (section_is_p (sectp->name, &names->macro))
1840 {
1841 dwarf2_per_objfile->macro.asection = sectp;
1842 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1843 }
1844 else if (section_is_p (sectp->name, &names->str))
1845 {
1846 dwarf2_per_objfile->str.asection = sectp;
1847 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1848 }
1849 else if (section_is_p (sectp->name, &names->addr))
1850 {
1851 dwarf2_per_objfile->addr.asection = sectp;
1852 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1853 }
1854 else if (section_is_p (sectp->name, &names->frame))
1855 {
1856 dwarf2_per_objfile->frame.asection = sectp;
1857 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1858 }
1859 else if (section_is_p (sectp->name, &names->eh_frame))
1860 {
1861 dwarf2_per_objfile->eh_frame.asection = sectp;
1862 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1863 }
1864 else if (section_is_p (sectp->name, &names->ranges))
1865 {
1866 dwarf2_per_objfile->ranges.asection = sectp;
1867 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1868 }
1869 else if (section_is_p (sectp->name, &names->types))
1870 {
1871 struct dwarf2_section_info type_section;
1872
1873 memset (&type_section, 0, sizeof (type_section));
1874 type_section.asection = sectp;
1875 type_section.size = bfd_get_section_size (sectp);
1876
1877 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1878 &type_section);
1879 }
1880 else if (section_is_p (sectp->name, &names->gdb_index))
1881 {
1882 dwarf2_per_objfile->gdb_index.asection = sectp;
1883 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1884 }
1885
1886 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1887 && bfd_section_vma (abfd, sectp) == 0)
1888 dwarf2_per_objfile->has_section_at_zero = 1;
1889 }
1890
1891 /* A helper function that decides whether a section is empty,
1892 or not present. */
1893
1894 static int
1895 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1896 {
1897 return info->asection == NULL || info->size == 0;
1898 }
1899
1900 /* Read the contents of the section INFO.
1901 OBJFILE is the main object file, but not necessarily the file where
1902 the section comes from. E.g., for DWO files INFO->asection->owner
1903 is the bfd of the DWO file.
1904 If the section is compressed, uncompress it before returning. */
1905
1906 static void
1907 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1908 {
1909 asection *sectp = info->asection;
1910 bfd *abfd;
1911 gdb_byte *buf, *retbuf;
1912 unsigned char header[4];
1913
1914 if (info->readin)
1915 return;
1916 info->buffer = NULL;
1917 info->readin = 1;
1918
1919 if (dwarf2_section_empty_p (info))
1920 return;
1921
1922 abfd = sectp->owner;
1923
1924 /* If the section has relocations, we must read it ourselves.
1925 Otherwise we attach it to the BFD. */
1926 if ((sectp->flags & SEC_RELOC) == 0)
1927 {
1928 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1929
1930 /* We have to cast away const here for historical reasons.
1931 Fixing dwarf2read to be const-correct would be quite nice. */
1932 info->buffer = (gdb_byte *) bytes;
1933 return;
1934 }
1935
1936 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1937 info->buffer = buf;
1938
1939 /* When debugging .o files, we may need to apply relocations; see
1940 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1941 We never compress sections in .o files, so we only need to
1942 try this when the section is not compressed. */
1943 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1944 if (retbuf != NULL)
1945 {
1946 info->buffer = retbuf;
1947 return;
1948 }
1949
1950 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1951 || bfd_bread (buf, info->size, abfd) != info->size)
1952 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1953 bfd_get_filename (abfd));
1954 }
1955
1956 /* A helper function that returns the size of a section in a safe way.
1957 If you are positive that the section has been read before using the
1958 size, then it is safe to refer to the dwarf2_section_info object's
1959 "size" field directly. In other cases, you must call this
1960 function, because for compressed sections the size field is not set
1961 correctly until the section has been read. */
1962
1963 static bfd_size_type
1964 dwarf2_section_size (struct objfile *objfile,
1965 struct dwarf2_section_info *info)
1966 {
1967 if (!info->readin)
1968 dwarf2_read_section (objfile, info);
1969 return info->size;
1970 }
1971
1972 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1973 SECTION_NAME. */
1974
1975 void
1976 dwarf2_get_section_info (struct objfile *objfile,
1977 enum dwarf2_section_enum sect,
1978 asection **sectp, gdb_byte **bufp,
1979 bfd_size_type *sizep)
1980 {
1981 struct dwarf2_per_objfile *data
1982 = objfile_data (objfile, dwarf2_objfile_data_key);
1983 struct dwarf2_section_info *info;
1984
1985 /* We may see an objfile without any DWARF, in which case we just
1986 return nothing. */
1987 if (data == NULL)
1988 {
1989 *sectp = NULL;
1990 *bufp = NULL;
1991 *sizep = 0;
1992 return;
1993 }
1994 switch (sect)
1995 {
1996 case DWARF2_DEBUG_FRAME:
1997 info = &data->frame;
1998 break;
1999 case DWARF2_EH_FRAME:
2000 info = &data->eh_frame;
2001 break;
2002 default:
2003 gdb_assert_not_reached ("unexpected section");
2004 }
2005
2006 dwarf2_read_section (objfile, info);
2007
2008 *sectp = info->asection;
2009 *bufp = info->buffer;
2010 *sizep = info->size;
2011 }
2012
2013 /* A helper function to find the sections for a .dwz file. */
2014
2015 static void
2016 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2017 {
2018 struct dwz_file *dwz_file = arg;
2019
2020 /* Note that we only support the standard ELF names, because .dwz
2021 is ELF-only (at the time of writing). */
2022 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2023 {
2024 dwz_file->abbrev.asection = sectp;
2025 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2028 {
2029 dwz_file->info.asection = sectp;
2030 dwz_file->info.size = bfd_get_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2033 {
2034 dwz_file->str.asection = sectp;
2035 dwz_file->str.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2038 {
2039 dwz_file->line.asection = sectp;
2040 dwz_file->line.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2043 {
2044 dwz_file->macro.asection = sectp;
2045 dwz_file->macro.size = bfd_get_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2048 {
2049 dwz_file->gdb_index.asection = sectp;
2050 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2051 }
2052 }
2053
2054 /* Open the separate '.dwz' debug file, if needed. Error if the file
2055 cannot be found. */
2056
2057 static struct dwz_file *
2058 dwarf2_get_dwz_file (void)
2059 {
2060 bfd *abfd, *dwz_bfd;
2061 asection *section;
2062 gdb_byte *data;
2063 struct cleanup *cleanup;
2064 const char *filename;
2065 struct dwz_file *result;
2066
2067 if (dwarf2_per_objfile->dwz_file != NULL)
2068 return dwarf2_per_objfile->dwz_file;
2069
2070 abfd = dwarf2_per_objfile->objfile->obfd;
2071 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2072 if (section == NULL)
2073 error (_("could not find '.gnu_debugaltlink' section"));
2074 if (!bfd_malloc_and_get_section (abfd, section, &data))
2075 error (_("could not read '.gnu_debugaltlink' section: %s"),
2076 bfd_errmsg (bfd_get_error ()));
2077 cleanup = make_cleanup (xfree, data);
2078
2079 filename = data;
2080 if (!IS_ABSOLUTE_PATH (filename))
2081 {
2082 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2083 char *rel;
2084
2085 make_cleanup (xfree, abs);
2086 abs = ldirname (abs);
2087 make_cleanup (xfree, abs);
2088
2089 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2090 make_cleanup (xfree, rel);
2091 filename = rel;
2092 }
2093
2094 /* The format is just a NUL-terminated file name, followed by the
2095 build-id. For now, though, we ignore the build-id. */
2096 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2097 if (dwz_bfd == NULL)
2098 error (_("could not read '%s': %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100
2101 if (!bfd_check_format (dwz_bfd, bfd_object))
2102 {
2103 gdb_bfd_unref (dwz_bfd);
2104 error (_("file '%s' was not usable: %s"), filename,
2105 bfd_errmsg (bfd_get_error ()));
2106 }
2107
2108 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2109 struct dwz_file);
2110 result->dwz_bfd = dwz_bfd;
2111
2112 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2113
2114 do_cleanups (cleanup);
2115
2116 dwarf2_per_objfile->dwz_file = result;
2117 return result;
2118 }
2119 \f
2120 /* DWARF quick_symbols_functions support. */
2121
2122 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2123 unique line tables, so we maintain a separate table of all .debug_line
2124 derived entries to support the sharing.
2125 All the quick functions need is the list of file names. We discard the
2126 line_header when we're done and don't need to record it here. */
2127 struct quick_file_names
2128 {
2129 /* The data used to construct the hash key. */
2130 struct stmt_list_hash hash;
2131
2132 /* The number of entries in file_names, real_names. */
2133 unsigned int num_file_names;
2134
2135 /* The file names from the line table, after being run through
2136 file_full_name. */
2137 const char **file_names;
2138
2139 /* The file names from the line table after being run through
2140 gdb_realpath. These are computed lazily. */
2141 const char **real_names;
2142 };
2143
2144 /* When using the index (and thus not using psymtabs), each CU has an
2145 object of this type. This is used to hold information needed by
2146 the various "quick" methods. */
2147 struct dwarf2_per_cu_quick_data
2148 {
2149 /* The file table. This can be NULL if there was no file table
2150 or it's currently not read in.
2151 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2152 struct quick_file_names *file_names;
2153
2154 /* The corresponding symbol table. This is NULL if symbols for this
2155 CU have not yet been read. */
2156 struct symtab *symtab;
2157
2158 /* A temporary mark bit used when iterating over all CUs in
2159 expand_symtabs_matching. */
2160 unsigned int mark : 1;
2161
2162 /* True if we've tried to read the file table and found there isn't one.
2163 There will be no point in trying to read it again next time. */
2164 unsigned int no_file_data : 1;
2165 };
2166
2167 /* Utility hash function for a stmt_list_hash. */
2168
2169 static hashval_t
2170 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2171 {
2172 hashval_t v = 0;
2173
2174 if (stmt_list_hash->dwo_unit != NULL)
2175 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2176 v += stmt_list_hash->line_offset.sect_off;
2177 return v;
2178 }
2179
2180 /* Utility equality function for a stmt_list_hash. */
2181
2182 static int
2183 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2184 const struct stmt_list_hash *rhs)
2185 {
2186 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2187 return 0;
2188 if (lhs->dwo_unit != NULL
2189 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2190 return 0;
2191
2192 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2193 }
2194
2195 /* Hash function for a quick_file_names. */
2196
2197 static hashval_t
2198 hash_file_name_entry (const void *e)
2199 {
2200 const struct quick_file_names *file_data = e;
2201
2202 return hash_stmt_list_entry (&file_data->hash);
2203 }
2204
2205 /* Equality function for a quick_file_names. */
2206
2207 static int
2208 eq_file_name_entry (const void *a, const void *b)
2209 {
2210 const struct quick_file_names *ea = a;
2211 const struct quick_file_names *eb = b;
2212
2213 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2214 }
2215
2216 /* Delete function for a quick_file_names. */
2217
2218 static void
2219 delete_file_name_entry (void *e)
2220 {
2221 struct quick_file_names *file_data = e;
2222 int i;
2223
2224 for (i = 0; i < file_data->num_file_names; ++i)
2225 {
2226 xfree ((void*) file_data->file_names[i]);
2227 if (file_data->real_names)
2228 xfree ((void*) file_data->real_names[i]);
2229 }
2230
2231 /* The space for the struct itself lives on objfile_obstack,
2232 so we don't free it here. */
2233 }
2234
2235 /* Create a quick_file_names hash table. */
2236
2237 static htab_t
2238 create_quick_file_names_table (unsigned int nr_initial_entries)
2239 {
2240 return htab_create_alloc (nr_initial_entries,
2241 hash_file_name_entry, eq_file_name_entry,
2242 delete_file_name_entry, xcalloc, xfree);
2243 }
2244
2245 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2246 have to be created afterwards. You should call age_cached_comp_units after
2247 processing PER_CU->CU. dw2_setup must have been already called. */
2248
2249 static void
2250 load_cu (struct dwarf2_per_cu_data *per_cu)
2251 {
2252 if (per_cu->is_debug_types)
2253 load_full_type_unit (per_cu);
2254 else
2255 load_full_comp_unit (per_cu, language_minimal);
2256
2257 gdb_assert (per_cu->cu != NULL);
2258
2259 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2260 }
2261
2262 /* Read in the symbols for PER_CU. */
2263
2264 static void
2265 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2266 {
2267 struct cleanup *back_to;
2268
2269 /* Skip type_unit_groups, reading the type units they contain
2270 is handled elsewhere. */
2271 if (IS_TYPE_UNIT_GROUP (per_cu))
2272 return;
2273
2274 back_to = make_cleanup (dwarf2_release_queue, NULL);
2275
2276 if (dwarf2_per_objfile->using_index
2277 ? per_cu->v.quick->symtab == NULL
2278 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2279 {
2280 queue_comp_unit (per_cu, language_minimal);
2281 load_cu (per_cu);
2282 }
2283
2284 process_queue ();
2285
2286 /* Age the cache, releasing compilation units that have not
2287 been used recently. */
2288 age_cached_comp_units ();
2289
2290 do_cleanups (back_to);
2291 }
2292
2293 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2294 the objfile from which this CU came. Returns the resulting symbol
2295 table. */
2296
2297 static struct symtab *
2298 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2299 {
2300 gdb_assert (dwarf2_per_objfile->using_index);
2301 if (!per_cu->v.quick->symtab)
2302 {
2303 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2304 increment_reading_symtab ();
2305 dw2_do_instantiate_symtab (per_cu);
2306 process_cu_includes ();
2307 do_cleanups (back_to);
2308 }
2309 return per_cu->v.quick->symtab;
2310 }
2311
2312 /* Return the CU given its index.
2313
2314 This is intended for loops like:
2315
2316 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2317 + dwarf2_per_objfile->n_type_units); ++i)
2318 {
2319 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2320
2321 ...;
2322 }
2323 */
2324
2325 static struct dwarf2_per_cu_data *
2326 dw2_get_cu (int index)
2327 {
2328 if (index >= dwarf2_per_objfile->n_comp_units)
2329 {
2330 index -= dwarf2_per_objfile->n_comp_units;
2331 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2332 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2333 }
2334
2335 return dwarf2_per_objfile->all_comp_units[index];
2336 }
2337
2338 /* Return the primary CU given its index.
2339 The difference between this function and dw2_get_cu is in the handling
2340 of type units (TUs). Here we return the type_unit_group object.
2341
2342 This is intended for loops like:
2343
2344 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2345 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2348
2349 ...;
2350 }
2351 */
2352
2353 static struct dwarf2_per_cu_data *
2354 dw2_get_primary_cu (int index)
2355 {
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
2358 index -= dwarf2_per_objfile->n_comp_units;
2359 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2360 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2361 }
2362
2363 return dwarf2_per_objfile->all_comp_units[index];
2364 }
2365
2366 /* A helper for create_cus_from_index that handles a given list of
2367 CUs. */
2368
2369 static void
2370 create_cus_from_index_list (struct objfile *objfile,
2371 const gdb_byte *cu_list, offset_type n_elements,
2372 struct dwarf2_section_info *section,
2373 int is_dwz,
2374 int base_offset)
2375 {
2376 offset_type i;
2377
2378 for (i = 0; i < n_elements; i += 2)
2379 {
2380 struct dwarf2_per_cu_data *the_cu;
2381 ULONGEST offset, length;
2382
2383 gdb_static_assert (sizeof (ULONGEST) >= 8);
2384 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2385 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2386 cu_list += 2 * 8;
2387
2388 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2389 struct dwarf2_per_cu_data);
2390 the_cu->offset.sect_off = offset;
2391 the_cu->length = length;
2392 the_cu->objfile = objfile;
2393 the_cu->info_or_types_section = section;
2394 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395 struct dwarf2_per_cu_quick_data);
2396 the_cu->is_dwz = is_dwz;
2397 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2398 }
2399 }
2400
2401 /* Read the CU list from the mapped index, and use it to create all
2402 the CU objects for this objfile. */
2403
2404 static void
2405 create_cus_from_index (struct objfile *objfile,
2406 const gdb_byte *cu_list, offset_type cu_list_elements,
2407 const gdb_byte *dwz_list, offset_type dwz_elements)
2408 {
2409 struct dwz_file *dwz;
2410
2411 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2412 dwarf2_per_objfile->all_comp_units
2413 = obstack_alloc (&objfile->objfile_obstack,
2414 dwarf2_per_objfile->n_comp_units
2415 * sizeof (struct dwarf2_per_cu_data *));
2416
2417 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2418 &dwarf2_per_objfile->info, 0, 0);
2419
2420 if (dwz_elements == 0)
2421 return;
2422
2423 dwz = dwarf2_get_dwz_file ();
2424 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2425 cu_list_elements / 2);
2426 }
2427
2428 /* Create the signatured type hash table from the index. */
2429
2430 static void
2431 create_signatured_type_table_from_index (struct objfile *objfile,
2432 struct dwarf2_section_info *section,
2433 const gdb_byte *bytes,
2434 offset_type elements)
2435 {
2436 offset_type i;
2437 htab_t sig_types_hash;
2438
2439 dwarf2_per_objfile->n_type_units = elements / 3;
2440 dwarf2_per_objfile->all_type_units
2441 = obstack_alloc (&objfile->objfile_obstack,
2442 dwarf2_per_objfile->n_type_units
2443 * sizeof (struct signatured_type *));
2444
2445 sig_types_hash = allocate_signatured_type_table (objfile);
2446
2447 for (i = 0; i < elements; i += 3)
2448 {
2449 struct signatured_type *sig_type;
2450 ULONGEST offset, type_offset_in_tu, signature;
2451 void **slot;
2452
2453 gdb_static_assert (sizeof (ULONGEST) >= 8);
2454 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2456 BFD_ENDIAN_LITTLE);
2457 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2458 bytes += 3 * 8;
2459
2460 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2461 struct signatured_type);
2462 sig_type->signature = signature;
2463 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2464 sig_type->per_cu.is_debug_types = 1;
2465 sig_type->per_cu.info_or_types_section = section;
2466 sig_type->per_cu.offset.sect_off = offset;
2467 sig_type->per_cu.objfile = objfile;
2468 sig_type->per_cu.v.quick
2469 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2470 struct dwarf2_per_cu_quick_data);
2471
2472 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2473 *slot = sig_type;
2474
2475 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2476 }
2477
2478 dwarf2_per_objfile->signatured_types = sig_types_hash;
2479 }
2480
2481 /* Read the address map data from the mapped index, and use it to
2482 populate the objfile's psymtabs_addrmap. */
2483
2484 static void
2485 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2486 {
2487 const gdb_byte *iter, *end;
2488 struct obstack temp_obstack;
2489 struct addrmap *mutable_map;
2490 struct cleanup *cleanup;
2491 CORE_ADDR baseaddr;
2492
2493 obstack_init (&temp_obstack);
2494 cleanup = make_cleanup_obstack_free (&temp_obstack);
2495 mutable_map = addrmap_create_mutable (&temp_obstack);
2496
2497 iter = index->address_table;
2498 end = iter + index->address_table_size;
2499
2500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2501
2502 while (iter < end)
2503 {
2504 ULONGEST hi, lo, cu_index;
2505 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2506 iter += 8;
2507 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2508 iter += 8;
2509 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2510 iter += 4;
2511
2512 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2513 dw2_get_cu (cu_index));
2514 }
2515
2516 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2517 &objfile->objfile_obstack);
2518 do_cleanups (cleanup);
2519 }
2520
2521 /* The hash function for strings in the mapped index. This is the same as
2522 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2523 implementation. This is necessary because the hash function is tied to the
2524 format of the mapped index file. The hash values do not have to match with
2525 SYMBOL_HASH_NEXT.
2526
2527 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2528
2529 static hashval_t
2530 mapped_index_string_hash (int index_version, const void *p)
2531 {
2532 const unsigned char *str = (const unsigned char *) p;
2533 hashval_t r = 0;
2534 unsigned char c;
2535
2536 while ((c = *str++) != 0)
2537 {
2538 if (index_version >= 5)
2539 c = tolower (c);
2540 r = r * 67 + c - 113;
2541 }
2542
2543 return r;
2544 }
2545
2546 /* Find a slot in the mapped index INDEX for the object named NAME.
2547 If NAME is found, set *VEC_OUT to point to the CU vector in the
2548 constant pool and return 1. If NAME cannot be found, return 0. */
2549
2550 static int
2551 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2552 offset_type **vec_out)
2553 {
2554 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2555 offset_type hash;
2556 offset_type slot, step;
2557 int (*cmp) (const char *, const char *);
2558
2559 if (current_language->la_language == language_cplus
2560 || current_language->la_language == language_java
2561 || current_language->la_language == language_fortran)
2562 {
2563 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2564 not contain any. */
2565 const char *paren = strchr (name, '(');
2566
2567 if (paren)
2568 {
2569 char *dup;
2570
2571 dup = xmalloc (paren - name + 1);
2572 memcpy (dup, name, paren - name);
2573 dup[paren - name] = 0;
2574
2575 make_cleanup (xfree, dup);
2576 name = dup;
2577 }
2578 }
2579
2580 /* Index version 4 did not support case insensitive searches. But the
2581 indices for case insensitive languages are built in lowercase, therefore
2582 simulate our NAME being searched is also lowercased. */
2583 hash = mapped_index_string_hash ((index->version == 4
2584 && case_sensitivity == case_sensitive_off
2585 ? 5 : index->version),
2586 name);
2587
2588 slot = hash & (index->symbol_table_slots - 1);
2589 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2590 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2591
2592 for (;;)
2593 {
2594 /* Convert a slot number to an offset into the table. */
2595 offset_type i = 2 * slot;
2596 const char *str;
2597 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2598 {
2599 do_cleanups (back_to);
2600 return 0;
2601 }
2602
2603 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2604 if (!cmp (name, str))
2605 {
2606 *vec_out = (offset_type *) (index->constant_pool
2607 + MAYBE_SWAP (index->symbol_table[i + 1]));
2608 do_cleanups (back_to);
2609 return 1;
2610 }
2611
2612 slot = (slot + step) & (index->symbol_table_slots - 1);
2613 }
2614 }
2615
2616 /* A helper function that reads the .gdb_index from SECTION and fills
2617 in MAP. FILENAME is the name of the file containing the section;
2618 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2619 ok to use deprecated sections.
2620
2621 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2622 out parameters that are filled in with information about the CU and
2623 TU lists in the section.
2624
2625 Returns 1 if all went well, 0 otherwise. */
2626
2627 static int
2628 read_index_from_section (struct objfile *objfile,
2629 const char *filename,
2630 int deprecated_ok,
2631 struct dwarf2_section_info *section,
2632 struct mapped_index *map,
2633 const gdb_byte **cu_list,
2634 offset_type *cu_list_elements,
2635 const gdb_byte **types_list,
2636 offset_type *types_list_elements)
2637 {
2638 char *addr;
2639 offset_type version;
2640 offset_type *metadata;
2641 int i;
2642
2643 if (dwarf2_section_empty_p (section))
2644 return 0;
2645
2646 /* Older elfutils strip versions could keep the section in the main
2647 executable while splitting it for the separate debug info file. */
2648 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2649 return 0;
2650
2651 dwarf2_read_section (objfile, section);
2652
2653 addr = section->buffer;
2654 /* Version check. */
2655 version = MAYBE_SWAP (*(offset_type *) addr);
2656 /* Versions earlier than 3 emitted every copy of a psymbol. This
2657 causes the index to behave very poorly for certain requests. Version 3
2658 contained incomplete addrmap. So, it seems better to just ignore such
2659 indices. */
2660 if (version < 4)
2661 {
2662 static int warning_printed = 0;
2663 if (!warning_printed)
2664 {
2665 warning (_("Skipping obsolete .gdb_index section in %s."),
2666 filename);
2667 warning_printed = 1;
2668 }
2669 return 0;
2670 }
2671 /* Index version 4 uses a different hash function than index version
2672 5 and later.
2673
2674 Versions earlier than 6 did not emit psymbols for inlined
2675 functions. Using these files will cause GDB not to be able to
2676 set breakpoints on inlined functions by name, so we ignore these
2677 indices unless the user has done
2678 "set use-deprecated-index-sections on". */
2679 if (version < 6 && !deprecated_ok)
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
2684 warning (_("\
2685 Skipping deprecated .gdb_index section in %s.\n\
2686 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2687 to use the section anyway."),
2688 filename);
2689 warning_printed = 1;
2690 }
2691 return 0;
2692 }
2693 /* Indexes with higher version than the one supported by GDB may be no
2694 longer backward compatible. */
2695 if (version > 7)
2696 return 0;
2697
2698 map->version = version;
2699 map->total_size = section->size;
2700
2701 metadata = (offset_type *) (addr + sizeof (offset_type));
2702
2703 i = 0;
2704 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2705 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2706 / 8);
2707 ++i;
2708
2709 *types_list = addr + MAYBE_SWAP (metadata[i]);
2710 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2711 - MAYBE_SWAP (metadata[i]))
2712 / 8);
2713 ++i;
2714
2715 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2716 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2717 - MAYBE_SWAP (metadata[i]));
2718 ++i;
2719
2720 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2721 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2722 - MAYBE_SWAP (metadata[i]))
2723 / (2 * sizeof (offset_type)));
2724 ++i;
2725
2726 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2727
2728 return 1;
2729 }
2730
2731
2732 /* Read the index file. If everything went ok, initialize the "quick"
2733 elements of all the CUs and return 1. Otherwise, return 0. */
2734
2735 static int
2736 dwarf2_read_index (struct objfile *objfile)
2737 {
2738 struct mapped_index local_map, *map;
2739 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2740 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2741
2742 if (!read_index_from_section (objfile, objfile->name,
2743 use_deprecated_index_sections,
2744 &dwarf2_per_objfile->gdb_index, &local_map,
2745 &cu_list, &cu_list_elements,
2746 &types_list, &types_list_elements))
2747 return 0;
2748
2749 /* Don't use the index if it's empty. */
2750 if (local_map.symbol_table_slots == 0)
2751 return 0;
2752
2753 /* If there is a .dwz file, read it so we can get its CU list as
2754 well. */
2755 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2756 {
2757 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2758 struct mapped_index dwz_map;
2759 const gdb_byte *dwz_types_ignore;
2760 offset_type dwz_types_elements_ignore;
2761
2762 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2763 1,
2764 &dwz->gdb_index, &dwz_map,
2765 &dwz_list, &dwz_list_elements,
2766 &dwz_types_ignore,
2767 &dwz_types_elements_ignore))
2768 {
2769 warning (_("could not read '.gdb_index' section from %s; skipping"),
2770 bfd_get_filename (dwz->dwz_bfd));
2771 return 0;
2772 }
2773 }
2774
2775 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2776 dwz_list_elements);
2777
2778 if (types_list_elements)
2779 {
2780 struct dwarf2_section_info *section;
2781
2782 /* We can only handle a single .debug_types when we have an
2783 index. */
2784 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2785 return 0;
2786
2787 section = VEC_index (dwarf2_section_info_def,
2788 dwarf2_per_objfile->types, 0);
2789
2790 create_signatured_type_table_from_index (objfile, section, types_list,
2791 types_list_elements);
2792 }
2793
2794 create_addrmap_from_index (objfile, &local_map);
2795
2796 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2797 *map = local_map;
2798
2799 dwarf2_per_objfile->index_table = map;
2800 dwarf2_per_objfile->using_index = 1;
2801 dwarf2_per_objfile->quick_file_names_table =
2802 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2803
2804 return 1;
2805 }
2806
2807 /* A helper for the "quick" functions which sets the global
2808 dwarf2_per_objfile according to OBJFILE. */
2809
2810 static void
2811 dw2_setup (struct objfile *objfile)
2812 {
2813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2814 gdb_assert (dwarf2_per_objfile);
2815 }
2816
2817 /* die_reader_func for dw2_get_file_names. */
2818
2819 static void
2820 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *comp_unit_die,
2823 int has_children,
2824 void *data)
2825 {
2826 struct dwarf2_cu *cu = reader->cu;
2827 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2828 struct objfile *objfile = dwarf2_per_objfile->objfile;
2829 struct dwarf2_per_cu_data *lh_cu;
2830 struct line_header *lh;
2831 struct attribute *attr;
2832 int i;
2833 const char *name, *comp_dir;
2834 void **slot;
2835 struct quick_file_names *qfn;
2836 unsigned int line_offset;
2837
2838 /* Our callers never want to match partial units -- instead they
2839 will match the enclosing full CU. */
2840 if (comp_unit_die->tag == DW_TAG_partial_unit)
2841 {
2842 this_cu->v.quick->no_file_data = 1;
2843 return;
2844 }
2845
2846 /* If we're reading the line header for TUs, store it in the "per_cu"
2847 for tu_group. */
2848 if (this_cu->is_debug_types)
2849 {
2850 struct type_unit_group *tu_group = data;
2851
2852 gdb_assert (tu_group != NULL);
2853 lh_cu = &tu_group->per_cu;
2854 }
2855 else
2856 lh_cu = this_cu;
2857
2858 lh = NULL;
2859 slot = NULL;
2860 line_offset = 0;
2861
2862 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2863 if (attr)
2864 {
2865 struct quick_file_names find_entry;
2866
2867 line_offset = DW_UNSND (attr);
2868
2869 /* We may have already read in this line header (TU line header sharing).
2870 If we have we're done. */
2871 find_entry.hash.dwo_unit = cu->dwo_unit;
2872 find_entry.hash.line_offset.sect_off = line_offset;
2873 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2874 &find_entry, INSERT);
2875 if (*slot != NULL)
2876 {
2877 lh_cu->v.quick->file_names = *slot;
2878 return;
2879 }
2880
2881 lh = dwarf_decode_line_header (line_offset, cu);
2882 }
2883 if (lh == NULL)
2884 {
2885 lh_cu->v.quick->no_file_data = 1;
2886 return;
2887 }
2888
2889 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2890 qfn->hash.dwo_unit = cu->dwo_unit;
2891 qfn->hash.line_offset.sect_off = line_offset;
2892 gdb_assert (slot != NULL);
2893 *slot = qfn;
2894
2895 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2896
2897 qfn->num_file_names = lh->num_file_names;
2898 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2899 lh->num_file_names * sizeof (char *));
2900 for (i = 0; i < lh->num_file_names; ++i)
2901 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2902 qfn->real_names = NULL;
2903
2904 free_line_header (lh);
2905
2906 lh_cu->v.quick->file_names = qfn;
2907 }
2908
2909 /* A helper for the "quick" functions which attempts to read the line
2910 table for THIS_CU. */
2911
2912 static struct quick_file_names *
2913 dw2_get_file_names (struct objfile *objfile,
2914 struct dwarf2_per_cu_data *this_cu)
2915 {
2916 /* For TUs this should only be called on the parent group. */
2917 if (this_cu->is_debug_types)
2918 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2919
2920 if (this_cu->v.quick->file_names != NULL)
2921 return this_cu->v.quick->file_names;
2922 /* If we know there is no line data, no point in looking again. */
2923 if (this_cu->v.quick->no_file_data)
2924 return NULL;
2925
2926 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2927 in the stub for CUs, there's is no need to lookup the DWO file.
2928 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2929 DWO file. */
2930 if (this_cu->is_debug_types)
2931 {
2932 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2933
2934 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2935 dw2_get_file_names_reader, tu_group);
2936 }
2937 else
2938 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2939
2940 if (this_cu->v.quick->no_file_data)
2941 return NULL;
2942 return this_cu->v.quick->file_names;
2943 }
2944
2945 /* A helper for the "quick" functions which computes and caches the
2946 real path for a given file name from the line table. */
2947
2948 static const char *
2949 dw2_get_real_path (struct objfile *objfile,
2950 struct quick_file_names *qfn, int index)
2951 {
2952 if (qfn->real_names == NULL)
2953 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2954 qfn->num_file_names, sizeof (char *));
2955
2956 if (qfn->real_names[index] == NULL)
2957 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2958
2959 return qfn->real_names[index];
2960 }
2961
2962 static struct symtab *
2963 dw2_find_last_source_symtab (struct objfile *objfile)
2964 {
2965 int index;
2966
2967 dw2_setup (objfile);
2968 index = dwarf2_per_objfile->n_comp_units - 1;
2969 return dw2_instantiate_symtab (dw2_get_cu (index));
2970 }
2971
2972 /* Traversal function for dw2_forget_cached_source_info. */
2973
2974 static int
2975 dw2_free_cached_file_names (void **slot, void *info)
2976 {
2977 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2978
2979 if (file_data->real_names)
2980 {
2981 int i;
2982
2983 for (i = 0; i < file_data->num_file_names; ++i)
2984 {
2985 xfree ((void*) file_data->real_names[i]);
2986 file_data->real_names[i] = NULL;
2987 }
2988 }
2989
2990 return 1;
2991 }
2992
2993 static void
2994 dw2_forget_cached_source_info (struct objfile *objfile)
2995 {
2996 dw2_setup (objfile);
2997
2998 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2999 dw2_free_cached_file_names, NULL);
3000 }
3001
3002 /* Helper function for dw2_map_symtabs_matching_filename that expands
3003 the symtabs and calls the iterator. */
3004
3005 static int
3006 dw2_map_expand_apply (struct objfile *objfile,
3007 struct dwarf2_per_cu_data *per_cu,
3008 const char *name,
3009 const char *full_path, const char *real_path,
3010 int (*callback) (struct symtab *, void *),
3011 void *data)
3012 {
3013 struct symtab *last_made = objfile->symtabs;
3014
3015 /* Don't visit already-expanded CUs. */
3016 if (per_cu->v.quick->symtab)
3017 return 0;
3018
3019 /* This may expand more than one symtab, and we want to iterate over
3020 all of them. */
3021 dw2_instantiate_symtab (per_cu);
3022
3023 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3024 objfile->symtabs, last_made);
3025 }
3026
3027 /* Implementation of the map_symtabs_matching_filename method. */
3028
3029 static int
3030 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3031 const char *full_path, const char *real_path,
3032 int (*callback) (struct symtab *, void *),
3033 void *data)
3034 {
3035 int i;
3036 const char *name_basename = lbasename (name);
3037 int is_abs = IS_ABSOLUTE_PATH (name);
3038
3039 dw2_setup (objfile);
3040
3041 /* The rule is CUs specify all the files, including those used by
3042 any TU, so there's no need to scan TUs here. */
3043
3044 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3045 {
3046 int j;
3047 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3048 struct quick_file_names *file_data;
3049
3050 /* We only need to look at symtabs not already expanded. */
3051 if (per_cu->v.quick->symtab)
3052 continue;
3053
3054 file_data = dw2_get_file_names (objfile, per_cu);
3055 if (file_data == NULL)
3056 continue;
3057
3058 for (j = 0; j < file_data->num_file_names; ++j)
3059 {
3060 const char *this_name = file_data->file_names[j];
3061
3062 if (FILENAME_CMP (name, this_name) == 0
3063 || (!is_abs && compare_filenames_for_search (this_name, name)))
3064 {
3065 if (dw2_map_expand_apply (objfile, per_cu,
3066 name, full_path, real_path,
3067 callback, data))
3068 return 1;
3069 }
3070
3071 /* Before we invoke realpath, which can get expensive when many
3072 files are involved, do a quick comparison of the basenames. */
3073 if (! basenames_may_differ
3074 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3075 continue;
3076
3077 if (full_path != NULL)
3078 {
3079 const char *this_real_name = dw2_get_real_path (objfile,
3080 file_data, j);
3081
3082 if (this_real_name != NULL
3083 && (FILENAME_CMP (full_path, this_real_name) == 0
3084 || (!is_abs
3085 && compare_filenames_for_search (this_real_name,
3086 name))))
3087 {
3088 if (dw2_map_expand_apply (objfile, per_cu,
3089 name, full_path, real_path,
3090 callback, data))
3091 return 1;
3092 }
3093 }
3094
3095 if (real_path != NULL)
3096 {
3097 const char *this_real_name = dw2_get_real_path (objfile,
3098 file_data, j);
3099
3100 if (this_real_name != NULL
3101 && (FILENAME_CMP (real_path, this_real_name) == 0
3102 || (!is_abs
3103 && compare_filenames_for_search (this_real_name,
3104 name))))
3105 {
3106 if (dw2_map_expand_apply (objfile, per_cu,
3107 name, full_path, real_path,
3108 callback, data))
3109 return 1;
3110 }
3111 }
3112 }
3113 }
3114
3115 return 0;
3116 }
3117
3118 /* Struct used to manage iterating over all CUs looking for a symbol. */
3119
3120 struct dw2_symtab_iterator
3121 {
3122 /* The internalized form of .gdb_index. */
3123 struct mapped_index *index;
3124 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3125 int want_specific_block;
3126 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3127 Unused if !WANT_SPECIFIC_BLOCK. */
3128 int block_index;
3129 /* The kind of symbol we're looking for. */
3130 domain_enum domain;
3131 /* The list of CUs from the index entry of the symbol,
3132 or NULL if not found. */
3133 offset_type *vec;
3134 /* The next element in VEC to look at. */
3135 int next;
3136 /* The number of elements in VEC, or zero if there is no match. */
3137 int length;
3138 };
3139
3140 /* Initialize the index symtab iterator ITER.
3141 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3142 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3143
3144 static void
3145 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3146 struct mapped_index *index,
3147 int want_specific_block,
3148 int block_index,
3149 domain_enum domain,
3150 const char *name)
3151 {
3152 iter->index = index;
3153 iter->want_specific_block = want_specific_block;
3154 iter->block_index = block_index;
3155 iter->domain = domain;
3156 iter->next = 0;
3157
3158 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3159 iter->length = MAYBE_SWAP (*iter->vec);
3160 else
3161 {
3162 iter->vec = NULL;
3163 iter->length = 0;
3164 }
3165 }
3166
3167 /* Return the next matching CU or NULL if there are no more. */
3168
3169 static struct dwarf2_per_cu_data *
3170 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3171 {
3172 for ( ; iter->next < iter->length; ++iter->next)
3173 {
3174 offset_type cu_index_and_attrs =
3175 MAYBE_SWAP (iter->vec[iter->next + 1]);
3176 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3177 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3178 int want_static = iter->block_index != GLOBAL_BLOCK;
3179 /* This value is only valid for index versions >= 7. */
3180 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3181 gdb_index_symbol_kind symbol_kind =
3182 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3183 /* Only check the symbol attributes if they're present.
3184 Indices prior to version 7 don't record them,
3185 and indices >= 7 may elide them for certain symbols
3186 (gold does this). */
3187 int attrs_valid =
3188 (iter->index->version >= 7
3189 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3190
3191 /* Skip if already read in. */
3192 if (per_cu->v.quick->symtab)
3193 continue;
3194
3195 if (attrs_valid
3196 && iter->want_specific_block
3197 && want_static != is_static)
3198 continue;
3199
3200 /* Only check the symbol's kind if it has one. */
3201 if (attrs_valid)
3202 {
3203 switch (iter->domain)
3204 {
3205 case VAR_DOMAIN:
3206 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3207 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3208 /* Some types are also in VAR_DOMAIN. */
3209 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3210 continue;
3211 break;
3212 case STRUCT_DOMAIN:
3213 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3214 continue;
3215 break;
3216 case LABEL_DOMAIN:
3217 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3218 continue;
3219 break;
3220 default:
3221 break;
3222 }
3223 }
3224
3225 ++iter->next;
3226 return per_cu;
3227 }
3228
3229 return NULL;
3230 }
3231
3232 static struct symtab *
3233 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3234 const char *name, domain_enum domain)
3235 {
3236 struct symtab *stab_best = NULL;
3237 struct mapped_index *index;
3238
3239 dw2_setup (objfile);
3240
3241 index = dwarf2_per_objfile->index_table;
3242
3243 /* index is NULL if OBJF_READNOW. */
3244 if (index)
3245 {
3246 struct dw2_symtab_iterator iter;
3247 struct dwarf2_per_cu_data *per_cu;
3248
3249 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3250
3251 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3252 {
3253 struct symbol *sym = NULL;
3254 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3255
3256 /* Some caution must be observed with overloaded functions
3257 and methods, since the index will not contain any overload
3258 information (but NAME might contain it). */
3259 if (stab->primary)
3260 {
3261 struct blockvector *bv = BLOCKVECTOR (stab);
3262 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3263
3264 sym = lookup_block_symbol (block, name, domain);
3265 }
3266
3267 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3268 {
3269 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3270 return stab;
3271
3272 stab_best = stab;
3273 }
3274
3275 /* Keep looking through other CUs. */
3276 }
3277 }
3278
3279 return stab_best;
3280 }
3281
3282 static void
3283 dw2_print_stats (struct objfile *objfile)
3284 {
3285 int i, count;
3286
3287 dw2_setup (objfile);
3288 count = 0;
3289 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3290 + dwarf2_per_objfile->n_type_units); ++i)
3291 {
3292 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3293
3294 if (!per_cu->v.quick->symtab)
3295 ++count;
3296 }
3297 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3298 }
3299
3300 static void
3301 dw2_dump (struct objfile *objfile)
3302 {
3303 /* Nothing worth printing. */
3304 }
3305
3306 static void
3307 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3308 struct section_offsets *delta)
3309 {
3310 /* There's nothing to relocate here. */
3311 }
3312
3313 static void
3314 dw2_expand_symtabs_for_function (struct objfile *objfile,
3315 const char *func_name)
3316 {
3317 struct mapped_index *index;
3318
3319 dw2_setup (objfile);
3320
3321 index = dwarf2_per_objfile->index_table;
3322
3323 /* index is NULL if OBJF_READNOW. */
3324 if (index)
3325 {
3326 struct dw2_symtab_iterator iter;
3327 struct dwarf2_per_cu_data *per_cu;
3328
3329 /* Note: It doesn't matter what we pass for block_index here. */
3330 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3331 func_name);
3332
3333 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3334 dw2_instantiate_symtab (per_cu);
3335 }
3336 }
3337
3338 static void
3339 dw2_expand_all_symtabs (struct objfile *objfile)
3340 {
3341 int i;
3342
3343 dw2_setup (objfile);
3344
3345 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3346 + dwarf2_per_objfile->n_type_units); ++i)
3347 {
3348 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3349
3350 dw2_instantiate_symtab (per_cu);
3351 }
3352 }
3353
3354 static void
3355 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3356 const char *filename)
3357 {
3358 int i;
3359
3360 dw2_setup (objfile);
3361
3362 /* We don't need to consider type units here.
3363 This is only called for examining code, e.g. expand_line_sal.
3364 There can be an order of magnitude (or more) more type units
3365 than comp units, and we avoid them if we can. */
3366
3367 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3368 {
3369 int j;
3370 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3371 struct quick_file_names *file_data;
3372
3373 /* We only need to look at symtabs not already expanded. */
3374 if (per_cu->v.quick->symtab)
3375 continue;
3376
3377 file_data = dw2_get_file_names (objfile, per_cu);
3378 if (file_data == NULL)
3379 continue;
3380
3381 for (j = 0; j < file_data->num_file_names; ++j)
3382 {
3383 const char *this_name = file_data->file_names[j];
3384 if (FILENAME_CMP (this_name, filename) == 0)
3385 {
3386 dw2_instantiate_symtab (per_cu);
3387 break;
3388 }
3389 }
3390 }
3391 }
3392
3393 /* A helper function for dw2_find_symbol_file that finds the primary
3394 file name for a given CU. This is a die_reader_func. */
3395
3396 static void
3397 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3398 gdb_byte *info_ptr,
3399 struct die_info *comp_unit_die,
3400 int has_children,
3401 void *data)
3402 {
3403 const char **result_ptr = data;
3404 struct dwarf2_cu *cu = reader->cu;
3405 struct attribute *attr;
3406
3407 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3408 if (attr == NULL)
3409 *result_ptr = NULL;
3410 else
3411 *result_ptr = DW_STRING (attr);
3412 }
3413
3414 static const char *
3415 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3416 {
3417 struct dwarf2_per_cu_data *per_cu;
3418 offset_type *vec;
3419 const char *filename;
3420
3421 dw2_setup (objfile);
3422
3423 /* index_table is NULL if OBJF_READNOW. */
3424 if (!dwarf2_per_objfile->index_table)
3425 {
3426 struct symtab *s;
3427
3428 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3429 {
3430 struct blockvector *bv = BLOCKVECTOR (s);
3431 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3432 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3433
3434 if (sym)
3435 return SYMBOL_SYMTAB (sym)->filename;
3436 }
3437 return NULL;
3438 }
3439
3440 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3441 name, &vec))
3442 return NULL;
3443
3444 /* Note that this just looks at the very first one named NAME -- but
3445 actually we are looking for a function. find_main_filename
3446 should be rewritten so that it doesn't require a custom hook. It
3447 could just use the ordinary symbol tables. */
3448 /* vec[0] is the length, which must always be >0. */
3449 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3450
3451 if (per_cu->v.quick->symtab != NULL)
3452 return per_cu->v.quick->symtab->filename;
3453
3454 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3455 dw2_get_primary_filename_reader, &filename);
3456
3457 return filename;
3458 }
3459
3460 static void
3461 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3462 struct objfile *objfile, int global,
3463 int (*callback) (struct block *,
3464 struct symbol *, void *),
3465 void *data, symbol_compare_ftype *match,
3466 symbol_compare_ftype *ordered_compare)
3467 {
3468 /* Currently unimplemented; used for Ada. The function can be called if the
3469 current language is Ada for a non-Ada objfile using GNU index. As Ada
3470 does not look for non-Ada symbols this function should just return. */
3471 }
3472
3473 static void
3474 dw2_expand_symtabs_matching
3475 (struct objfile *objfile,
3476 int (*file_matcher) (const char *, void *),
3477 int (*name_matcher) (const char *, void *),
3478 enum search_domain kind,
3479 void *data)
3480 {
3481 int i;
3482 offset_type iter;
3483 struct mapped_index *index;
3484
3485 dw2_setup (objfile);
3486
3487 /* index_table is NULL if OBJF_READNOW. */
3488 if (!dwarf2_per_objfile->index_table)
3489 return;
3490 index = dwarf2_per_objfile->index_table;
3491
3492 if (file_matcher != NULL)
3493 {
3494 struct cleanup *cleanup;
3495 htab_t visited_found, visited_not_found;
3496
3497 visited_found = htab_create_alloc (10,
3498 htab_hash_pointer, htab_eq_pointer,
3499 NULL, xcalloc, xfree);
3500 cleanup = make_cleanup_htab_delete (visited_found);
3501 visited_not_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 make_cleanup_htab_delete (visited_not_found);
3505
3506 /* The rule is CUs specify all the files, including those used by
3507 any TU, so there's no need to scan TUs here. */
3508
3509 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3510 {
3511 int j;
3512 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3513 struct quick_file_names *file_data;
3514 void **slot;
3515
3516 per_cu->v.quick->mark = 0;
3517
3518 /* We only need to look at symtabs not already expanded. */
3519 if (per_cu->v.quick->symtab)
3520 continue;
3521
3522 file_data = dw2_get_file_names (objfile, per_cu);
3523 if (file_data == NULL)
3524 continue;
3525
3526 if (htab_find (visited_not_found, file_data) != NULL)
3527 continue;
3528 else if (htab_find (visited_found, file_data) != NULL)
3529 {
3530 per_cu->v.quick->mark = 1;
3531 continue;
3532 }
3533
3534 for (j = 0; j < file_data->num_file_names; ++j)
3535 {
3536 if (file_matcher (file_data->file_names[j], data))
3537 {
3538 per_cu->v.quick->mark = 1;
3539 break;
3540 }
3541 }
3542
3543 slot = htab_find_slot (per_cu->v.quick->mark
3544 ? visited_found
3545 : visited_not_found,
3546 file_data, INSERT);
3547 *slot = file_data;
3548 }
3549
3550 do_cleanups (cleanup);
3551 }
3552
3553 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3554 {
3555 offset_type idx = 2 * iter;
3556 const char *name;
3557 offset_type *vec, vec_len, vec_idx;
3558
3559 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3560 continue;
3561
3562 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3563
3564 if (! (*name_matcher) (name, data))
3565 continue;
3566
3567 /* The name was matched, now expand corresponding CUs that were
3568 marked. */
3569 vec = (offset_type *) (index->constant_pool
3570 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3571 vec_len = MAYBE_SWAP (vec[0]);
3572 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3573 {
3574 struct dwarf2_per_cu_data *per_cu;
3575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3576 gdb_index_symbol_kind symbol_kind =
3577 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3578 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3579
3580 /* Don't crash on bad data. */
3581 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3582 + dwarf2_per_objfile->n_type_units))
3583 continue;
3584
3585 /* Only check the symbol's kind if it has one.
3586 Indices prior to version 7 don't record it. */
3587 if (index->version >= 7)
3588 {
3589 switch (kind)
3590 {
3591 case VARIABLES_DOMAIN:
3592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3593 continue;
3594 break;
3595 case FUNCTIONS_DOMAIN:
3596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3597 continue;
3598 break;
3599 case TYPES_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3601 continue;
3602 break;
3603 default:
3604 break;
3605 }
3606 }
3607
3608 per_cu = dw2_get_cu (cu_index);
3609 if (file_matcher == NULL || per_cu->v.quick->mark)
3610 dw2_instantiate_symtab (per_cu);
3611 }
3612 }
3613 }
3614
3615 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3616 symtab. */
3617
3618 static struct symtab *
3619 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3620 {
3621 int i;
3622
3623 if (BLOCKVECTOR (symtab) != NULL
3624 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3625 return symtab;
3626
3627 if (symtab->includes == NULL)
3628 return NULL;
3629
3630 for (i = 0; symtab->includes[i]; ++i)
3631 {
3632 struct symtab *s = symtab->includes[i];
3633
3634 s = recursively_find_pc_sect_symtab (s, pc);
3635 if (s != NULL)
3636 return s;
3637 }
3638
3639 return NULL;
3640 }
3641
3642 static struct symtab *
3643 dw2_find_pc_sect_symtab (struct objfile *objfile,
3644 struct minimal_symbol *msymbol,
3645 CORE_ADDR pc,
3646 struct obj_section *section,
3647 int warn_if_readin)
3648 {
3649 struct dwarf2_per_cu_data *data;
3650 struct symtab *result;
3651
3652 dw2_setup (objfile);
3653
3654 if (!objfile->psymtabs_addrmap)
3655 return NULL;
3656
3657 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3658 if (!data)
3659 return NULL;
3660
3661 if (warn_if_readin && data->v.quick->symtab)
3662 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3663 paddress (get_objfile_arch (objfile), pc));
3664
3665 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3666 gdb_assert (result != NULL);
3667 return result;
3668 }
3669
3670 static void
3671 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3672 void *data, int need_fullname)
3673 {
3674 int i;
3675 struct cleanup *cleanup;
3676 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3677 NULL, xcalloc, xfree);
3678
3679 cleanup = make_cleanup_htab_delete (visited);
3680 dw2_setup (objfile);
3681
3682 /* The rule is CUs specify all the files, including those used by
3683 any TU, so there's no need to scan TUs here.
3684 We can ignore file names coming from already-expanded CUs. */
3685
3686 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3687 {
3688 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3689
3690 if (per_cu->v.quick->symtab)
3691 {
3692 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3693 INSERT);
3694
3695 *slot = per_cu->v.quick->file_names;
3696 }
3697 }
3698
3699 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3700 {
3701 int j;
3702 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3703 struct quick_file_names *file_data;
3704 void **slot;
3705
3706 /* We only need to look at symtabs not already expanded. */
3707 if (per_cu->v.quick->symtab)
3708 continue;
3709
3710 file_data = dw2_get_file_names (objfile, per_cu);
3711 if (file_data == NULL)
3712 continue;
3713
3714 slot = htab_find_slot (visited, file_data, INSERT);
3715 if (*slot)
3716 {
3717 /* Already visited. */
3718 continue;
3719 }
3720 *slot = file_data;
3721
3722 for (j = 0; j < file_data->num_file_names; ++j)
3723 {
3724 const char *this_real_name;
3725
3726 if (need_fullname)
3727 this_real_name = dw2_get_real_path (objfile, file_data, j);
3728 else
3729 this_real_name = NULL;
3730 (*fun) (file_data->file_names[j], this_real_name, data);
3731 }
3732 }
3733
3734 do_cleanups (cleanup);
3735 }
3736
3737 static int
3738 dw2_has_symbols (struct objfile *objfile)
3739 {
3740 return 1;
3741 }
3742
3743 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3744 {
3745 dw2_has_symbols,
3746 dw2_find_last_source_symtab,
3747 dw2_forget_cached_source_info,
3748 dw2_map_symtabs_matching_filename,
3749 dw2_lookup_symbol,
3750 dw2_print_stats,
3751 dw2_dump,
3752 dw2_relocate,
3753 dw2_expand_symtabs_for_function,
3754 dw2_expand_all_symtabs,
3755 dw2_expand_symtabs_with_filename,
3756 dw2_find_symbol_file,
3757 dw2_map_matching_symbols,
3758 dw2_expand_symtabs_matching,
3759 dw2_find_pc_sect_symtab,
3760 dw2_map_symbol_filenames
3761 };
3762
3763 /* Initialize for reading DWARF for this objfile. Return 0 if this
3764 file will use psymtabs, or 1 if using the GNU index. */
3765
3766 int
3767 dwarf2_initialize_objfile (struct objfile *objfile)
3768 {
3769 /* If we're about to read full symbols, don't bother with the
3770 indices. In this case we also don't care if some other debug
3771 format is making psymtabs, because they are all about to be
3772 expanded anyway. */
3773 if ((objfile->flags & OBJF_READNOW))
3774 {
3775 int i;
3776
3777 dwarf2_per_objfile->using_index = 1;
3778 create_all_comp_units (objfile);
3779 create_all_type_units (objfile);
3780 dwarf2_per_objfile->quick_file_names_table =
3781 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3782
3783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3784 + dwarf2_per_objfile->n_type_units); ++i)
3785 {
3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3787
3788 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3789 struct dwarf2_per_cu_quick_data);
3790 }
3791
3792 /* Return 1 so that gdb sees the "quick" functions. However,
3793 these functions will be no-ops because we will have expanded
3794 all symtabs. */
3795 return 1;
3796 }
3797
3798 if (dwarf2_read_index (objfile))
3799 return 1;
3800
3801 return 0;
3802 }
3803
3804 \f
3805
3806 /* Build a partial symbol table. */
3807
3808 void
3809 dwarf2_build_psymtabs (struct objfile *objfile)
3810 {
3811 volatile struct gdb_exception except;
3812
3813 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3814 {
3815 init_psymbol_list (objfile, 1024);
3816 }
3817
3818 TRY_CATCH (except, RETURN_MASK_ERROR)
3819 {
3820 /* This isn't really ideal: all the data we allocate on the
3821 objfile's obstack is still uselessly kept around. However,
3822 freeing it seems unsafe. */
3823 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3824
3825 dwarf2_build_psymtabs_hard (objfile);
3826 discard_cleanups (cleanups);
3827 }
3828 if (except.reason < 0)
3829 exception_print (gdb_stderr, except);
3830 }
3831
3832 /* Return the total length of the CU described by HEADER. */
3833
3834 static unsigned int
3835 get_cu_length (const struct comp_unit_head *header)
3836 {
3837 return header->initial_length_size + header->length;
3838 }
3839
3840 /* Return TRUE if OFFSET is within CU_HEADER. */
3841
3842 static inline int
3843 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3844 {
3845 sect_offset bottom = { cu_header->offset.sect_off };
3846 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3847
3848 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3849 }
3850
3851 /* Find the base address of the compilation unit for range lists and
3852 location lists. It will normally be specified by DW_AT_low_pc.
3853 In DWARF-3 draft 4, the base address could be overridden by
3854 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3855 compilation units with discontinuous ranges. */
3856
3857 static void
3858 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3859 {
3860 struct attribute *attr;
3861
3862 cu->base_known = 0;
3863 cu->base_address = 0;
3864
3865 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3866 if (attr)
3867 {
3868 cu->base_address = DW_ADDR (attr);
3869 cu->base_known = 1;
3870 }
3871 else
3872 {
3873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3874 if (attr)
3875 {
3876 cu->base_address = DW_ADDR (attr);
3877 cu->base_known = 1;
3878 }
3879 }
3880 }
3881
3882 /* Read in the comp unit header information from the debug_info at info_ptr.
3883 NOTE: This leaves members offset, first_die_offset to be filled in
3884 by the caller. */
3885
3886 static gdb_byte *
3887 read_comp_unit_head (struct comp_unit_head *cu_header,
3888 gdb_byte *info_ptr, bfd *abfd)
3889 {
3890 int signed_addr;
3891 unsigned int bytes_read;
3892
3893 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3894 cu_header->initial_length_size = bytes_read;
3895 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3896 info_ptr += bytes_read;
3897 cu_header->version = read_2_bytes (abfd, info_ptr);
3898 info_ptr += 2;
3899 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3900 &bytes_read);
3901 info_ptr += bytes_read;
3902 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3903 info_ptr += 1;
3904 signed_addr = bfd_get_sign_extend_vma (abfd);
3905 if (signed_addr < 0)
3906 internal_error (__FILE__, __LINE__,
3907 _("read_comp_unit_head: dwarf from non elf file"));
3908 cu_header->signed_addr_p = signed_addr;
3909
3910 return info_ptr;
3911 }
3912
3913 /* Helper function that returns the proper abbrev section for
3914 THIS_CU. */
3915
3916 static struct dwarf2_section_info *
3917 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3918 {
3919 struct dwarf2_section_info *abbrev;
3920
3921 if (this_cu->is_dwz)
3922 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3923 else
3924 abbrev = &dwarf2_per_objfile->abbrev;
3925
3926 return abbrev;
3927 }
3928
3929 /* Subroutine of read_and_check_comp_unit_head and
3930 read_and_check_type_unit_head to simplify them.
3931 Perform various error checking on the header. */
3932
3933 static void
3934 error_check_comp_unit_head (struct comp_unit_head *header,
3935 struct dwarf2_section_info *section,
3936 struct dwarf2_section_info *abbrev_section)
3937 {
3938 bfd *abfd = section->asection->owner;
3939 const char *filename = bfd_get_filename (abfd);
3940
3941 if (header->version != 2 && header->version != 3 && header->version != 4)
3942 error (_("Dwarf Error: wrong version in compilation unit header "
3943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3944 filename);
3945
3946 if (header->abbrev_offset.sect_off
3947 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3948 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3949 "(offset 0x%lx + 6) [in module %s]"),
3950 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3951 filename);
3952
3953 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3954 avoid potential 32-bit overflow. */
3955 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3956 > section->size)
3957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3958 "(offset 0x%lx + 0) [in module %s]"),
3959 (long) header->length, (long) header->offset.sect_off,
3960 filename);
3961 }
3962
3963 /* Read in a CU/TU header and perform some basic error checking.
3964 The contents of the header are stored in HEADER.
3965 The result is a pointer to the start of the first DIE. */
3966
3967 static gdb_byte *
3968 read_and_check_comp_unit_head (struct comp_unit_head *header,
3969 struct dwarf2_section_info *section,
3970 struct dwarf2_section_info *abbrev_section,
3971 gdb_byte *info_ptr,
3972 int is_debug_types_section)
3973 {
3974 gdb_byte *beg_of_comp_unit = info_ptr;
3975 bfd *abfd = section->asection->owner;
3976
3977 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3978
3979 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3980
3981 /* If we're reading a type unit, skip over the signature and
3982 type_offset fields. */
3983 if (is_debug_types_section)
3984 info_ptr += 8 /*signature*/ + header->offset_size;
3985
3986 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3987
3988 error_check_comp_unit_head (header, section, abbrev_section);
3989
3990 return info_ptr;
3991 }
3992
3993 /* Read in the types comp unit header information from .debug_types entry at
3994 types_ptr. The result is a pointer to one past the end of the header. */
3995
3996 static gdb_byte *
3997 read_and_check_type_unit_head (struct comp_unit_head *header,
3998 struct dwarf2_section_info *section,
3999 struct dwarf2_section_info *abbrev_section,
4000 gdb_byte *info_ptr,
4001 ULONGEST *signature,
4002 cu_offset *type_offset_in_tu)
4003 {
4004 gdb_byte *beg_of_comp_unit = info_ptr;
4005 bfd *abfd = section->asection->owner;
4006
4007 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4008
4009 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4010
4011 /* If we're reading a type unit, skip over the signature and
4012 type_offset fields. */
4013 if (signature != NULL)
4014 *signature = read_8_bytes (abfd, info_ptr);
4015 info_ptr += 8;
4016 if (type_offset_in_tu != NULL)
4017 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4018 header->offset_size);
4019 info_ptr += header->offset_size;
4020
4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4022
4023 error_check_comp_unit_head (header, section, abbrev_section);
4024
4025 return info_ptr;
4026 }
4027
4028 /* Fetch the abbreviation table offset from a comp or type unit header. */
4029
4030 static sect_offset
4031 read_abbrev_offset (struct dwarf2_section_info *section,
4032 sect_offset offset)
4033 {
4034 bfd *abfd = section->asection->owner;
4035 gdb_byte *info_ptr;
4036 unsigned int length, initial_length_size, offset_size;
4037 sect_offset abbrev_offset;
4038
4039 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4040 info_ptr = section->buffer + offset.sect_off;
4041 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4042 offset_size = initial_length_size == 4 ? 4 : 8;
4043 info_ptr += initial_length_size + 2 /*version*/;
4044 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4045 return abbrev_offset;
4046 }
4047
4048 /* Allocate a new partial symtab for file named NAME and mark this new
4049 partial symtab as being an include of PST. */
4050
4051 static void
4052 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4053 struct objfile *objfile)
4054 {
4055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4056
4057 subpst->section_offsets = pst->section_offsets;
4058 subpst->textlow = 0;
4059 subpst->texthigh = 0;
4060
4061 subpst->dependencies = (struct partial_symtab **)
4062 obstack_alloc (&objfile->objfile_obstack,
4063 sizeof (struct partial_symtab *));
4064 subpst->dependencies[0] = pst;
4065 subpst->number_of_dependencies = 1;
4066
4067 subpst->globals_offset = 0;
4068 subpst->n_global_syms = 0;
4069 subpst->statics_offset = 0;
4070 subpst->n_static_syms = 0;
4071 subpst->symtab = NULL;
4072 subpst->read_symtab = pst->read_symtab;
4073 subpst->readin = 0;
4074
4075 /* No private part is necessary for include psymtabs. This property
4076 can be used to differentiate between such include psymtabs and
4077 the regular ones. */
4078 subpst->read_symtab_private = NULL;
4079 }
4080
4081 /* Read the Line Number Program data and extract the list of files
4082 included by the source file represented by PST. Build an include
4083 partial symtab for each of these included files. */
4084
4085 static void
4086 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4087 struct die_info *die,
4088 struct partial_symtab *pst)
4089 {
4090 struct line_header *lh = NULL;
4091 struct attribute *attr;
4092
4093 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4094 if (attr)
4095 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4096 if (lh == NULL)
4097 return; /* No linetable, so no includes. */
4098
4099 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4100 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4101
4102 free_line_header (lh);
4103 }
4104
4105 static hashval_t
4106 hash_signatured_type (const void *item)
4107 {
4108 const struct signatured_type *sig_type = item;
4109
4110 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4111 return sig_type->signature;
4112 }
4113
4114 static int
4115 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4116 {
4117 const struct signatured_type *lhs = item_lhs;
4118 const struct signatured_type *rhs = item_rhs;
4119
4120 return lhs->signature == rhs->signature;
4121 }
4122
4123 /* Allocate a hash table for signatured types. */
4124
4125 static htab_t
4126 allocate_signatured_type_table (struct objfile *objfile)
4127 {
4128 return htab_create_alloc_ex (41,
4129 hash_signatured_type,
4130 eq_signatured_type,
4131 NULL,
4132 &objfile->objfile_obstack,
4133 hashtab_obstack_allocate,
4134 dummy_obstack_deallocate);
4135 }
4136
4137 /* A helper function to add a signatured type CU to a table. */
4138
4139 static int
4140 add_signatured_type_cu_to_table (void **slot, void *datum)
4141 {
4142 struct signatured_type *sigt = *slot;
4143 struct signatured_type ***datap = datum;
4144
4145 **datap = sigt;
4146 ++*datap;
4147
4148 return 1;
4149 }
4150
4151 /* Create the hash table of all entries in the .debug_types section.
4152 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4153 NULL otherwise.
4154 Note: This function processes DWO files only, not DWP files.
4155 The result is a pointer to the hash table or NULL if there are
4156 no types. */
4157
4158 static htab_t
4159 create_debug_types_hash_table (struct dwo_file *dwo_file,
4160 VEC (dwarf2_section_info_def) *types)
4161 {
4162 struct objfile *objfile = dwarf2_per_objfile->objfile;
4163 htab_t types_htab = NULL;
4164 int ix;
4165 struct dwarf2_section_info *section;
4166 struct dwarf2_section_info *abbrev_section;
4167
4168 if (VEC_empty (dwarf2_section_info_def, types))
4169 return NULL;
4170
4171 abbrev_section = (dwo_file != NULL
4172 ? &dwo_file->sections.abbrev
4173 : &dwarf2_per_objfile->abbrev);
4174
4175 if (dwarf2_read_debug)
4176 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4177 dwo_file ? ".dwo" : "",
4178 bfd_get_filename (abbrev_section->asection->owner));
4179
4180 for (ix = 0;
4181 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4182 ++ix)
4183 {
4184 bfd *abfd;
4185 gdb_byte *info_ptr, *end_ptr;
4186 struct dwarf2_section_info *abbrev_section;
4187
4188 dwarf2_read_section (objfile, section);
4189 info_ptr = section->buffer;
4190
4191 if (info_ptr == NULL)
4192 continue;
4193
4194 /* We can't set abfd until now because the section may be empty or
4195 not present, in which case section->asection will be NULL. */
4196 abfd = section->asection->owner;
4197
4198 if (dwo_file)
4199 abbrev_section = &dwo_file->sections.abbrev;
4200 else
4201 abbrev_section = &dwarf2_per_objfile->abbrev;
4202
4203 if (types_htab == NULL)
4204 {
4205 if (dwo_file)
4206 types_htab = allocate_dwo_unit_table (objfile);
4207 else
4208 types_htab = allocate_signatured_type_table (objfile);
4209 }
4210
4211 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4212 because we don't need to read any dies: the signature is in the
4213 header. */
4214
4215 end_ptr = info_ptr + section->size;
4216 while (info_ptr < end_ptr)
4217 {
4218 sect_offset offset;
4219 cu_offset type_offset_in_tu;
4220 ULONGEST signature;
4221 struct signatured_type *sig_type;
4222 struct dwo_unit *dwo_tu;
4223 void **slot;
4224 gdb_byte *ptr = info_ptr;
4225 struct comp_unit_head header;
4226 unsigned int length;
4227
4228 offset.sect_off = ptr - section->buffer;
4229
4230 /* We need to read the type's signature in order to build the hash
4231 table, but we don't need anything else just yet. */
4232
4233 ptr = read_and_check_type_unit_head (&header, section,
4234 abbrev_section, ptr,
4235 &signature, &type_offset_in_tu);
4236
4237 length = get_cu_length (&header);
4238
4239 /* Skip dummy type units. */
4240 if (ptr >= info_ptr + length
4241 || peek_abbrev_code (abfd, ptr) == 0)
4242 {
4243 info_ptr += length;
4244 continue;
4245 }
4246
4247 if (dwo_file)
4248 {
4249 sig_type = NULL;
4250 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4251 struct dwo_unit);
4252 dwo_tu->dwo_file = dwo_file;
4253 dwo_tu->signature = signature;
4254 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4255 dwo_tu->info_or_types_section = section;
4256 dwo_tu->offset = offset;
4257 dwo_tu->length = length;
4258 }
4259 else
4260 {
4261 /* N.B.: type_offset is not usable if this type uses a DWO file.
4262 The real type_offset is in the DWO file. */
4263 dwo_tu = NULL;
4264 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4265 struct signatured_type);
4266 sig_type->signature = signature;
4267 sig_type->type_offset_in_tu = type_offset_in_tu;
4268 sig_type->per_cu.objfile = objfile;
4269 sig_type->per_cu.is_debug_types = 1;
4270 sig_type->per_cu.info_or_types_section = section;
4271 sig_type->per_cu.offset = offset;
4272 sig_type->per_cu.length = length;
4273 }
4274
4275 slot = htab_find_slot (types_htab,
4276 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4277 INSERT);
4278 gdb_assert (slot != NULL);
4279 if (*slot != NULL)
4280 {
4281 sect_offset dup_offset;
4282
4283 if (dwo_file)
4284 {
4285 const struct dwo_unit *dup_tu = *slot;
4286
4287 dup_offset = dup_tu->offset;
4288 }
4289 else
4290 {
4291 const struct signatured_type *dup_tu = *slot;
4292
4293 dup_offset = dup_tu->per_cu.offset;
4294 }
4295
4296 complaint (&symfile_complaints,
4297 _("debug type entry at offset 0x%x is duplicate to the "
4298 "entry at offset 0x%x, signature 0x%s"),
4299 offset.sect_off, dup_offset.sect_off,
4300 phex (signature, sizeof (signature)));
4301 }
4302 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4303
4304 if (dwarf2_read_debug)
4305 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4306 offset.sect_off,
4307 phex (signature, sizeof (signature)));
4308
4309 info_ptr += length;
4310 }
4311 }
4312
4313 return types_htab;
4314 }
4315
4316 /* Create the hash table of all entries in the .debug_types section,
4317 and initialize all_type_units.
4318 The result is zero if there is an error (e.g. missing .debug_types section),
4319 otherwise non-zero. */
4320
4321 static int
4322 create_all_type_units (struct objfile *objfile)
4323 {
4324 htab_t types_htab;
4325 struct signatured_type **iter;
4326
4327 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4328 if (types_htab == NULL)
4329 {
4330 dwarf2_per_objfile->signatured_types = NULL;
4331 return 0;
4332 }
4333
4334 dwarf2_per_objfile->signatured_types = types_htab;
4335
4336 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4337 dwarf2_per_objfile->all_type_units
4338 = obstack_alloc (&objfile->objfile_obstack,
4339 dwarf2_per_objfile->n_type_units
4340 * sizeof (struct signatured_type *));
4341 iter = &dwarf2_per_objfile->all_type_units[0];
4342 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4343 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4344 == dwarf2_per_objfile->n_type_units);
4345
4346 return 1;
4347 }
4348
4349 /* Lookup a signature based type for DW_FORM_ref_sig8.
4350 Returns NULL if signature SIG is not present in the table. */
4351
4352 static struct signatured_type *
4353 lookup_signatured_type (ULONGEST sig)
4354 {
4355 struct signatured_type find_entry, *entry;
4356
4357 if (dwarf2_per_objfile->signatured_types == NULL)
4358 {
4359 complaint (&symfile_complaints,
4360 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4361 return NULL;
4362 }
4363
4364 find_entry.signature = sig;
4365 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4366 return entry;
4367 }
4368 \f
4369 /* Low level DIE reading support. */
4370
4371 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4372
4373 static void
4374 init_cu_die_reader (struct die_reader_specs *reader,
4375 struct dwarf2_cu *cu,
4376 struct dwarf2_section_info *section,
4377 struct dwo_file *dwo_file)
4378 {
4379 gdb_assert (section->readin && section->buffer != NULL);
4380 reader->abfd = section->asection->owner;
4381 reader->cu = cu;
4382 reader->dwo_file = dwo_file;
4383 reader->die_section = section;
4384 reader->buffer = section->buffer;
4385 reader->buffer_end = section->buffer + section->size;
4386 }
4387
4388 /* Initialize a CU (or TU) and read its DIEs.
4389 If the CU defers to a DWO file, read the DWO file as well.
4390
4391 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4392 Otherwise the table specified in the comp unit header is read in and used.
4393 This is an optimization for when we already have the abbrev table.
4394
4395 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4396 Otherwise, a new CU is allocated with xmalloc.
4397
4398 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4399 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4400
4401 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4402 linker) then DIE_READER_FUNC will not get called. */
4403
4404 static void
4405 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4406 struct abbrev_table *abbrev_table,
4407 int use_existing_cu, int keep,
4408 die_reader_func_ftype *die_reader_func,
4409 void *data)
4410 {
4411 struct objfile *objfile = dwarf2_per_objfile->objfile;
4412 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4413 bfd *abfd = section->asection->owner;
4414 struct dwarf2_cu *cu;
4415 gdb_byte *begin_info_ptr, *info_ptr;
4416 struct die_reader_specs reader;
4417 struct die_info *comp_unit_die;
4418 int has_children;
4419 struct attribute *attr;
4420 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4421 struct signatured_type *sig_type = NULL;
4422 struct dwarf2_section_info *abbrev_section;
4423 /* Non-zero if CU currently points to a DWO file and we need to
4424 reread it. When this happens we need to reread the skeleton die
4425 before we can reread the DWO file. */
4426 int rereading_dwo_cu = 0;
4427
4428 if (dwarf2_die_debug)
4429 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4430 this_cu->is_debug_types ? "type" : "comp",
4431 this_cu->offset.sect_off);
4432
4433 if (use_existing_cu)
4434 gdb_assert (keep);
4435
4436 cleanups = make_cleanup (null_cleanup, NULL);
4437
4438 /* This is cheap if the section is already read in. */
4439 dwarf2_read_section (objfile, section);
4440
4441 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4442
4443 abbrev_section = get_abbrev_section_for_cu (this_cu);
4444
4445 if (use_existing_cu && this_cu->cu != NULL)
4446 {
4447 cu = this_cu->cu;
4448
4449 /* If this CU is from a DWO file we need to start over, we need to
4450 refetch the attributes from the skeleton CU.
4451 This could be optimized by retrieving those attributes from when we
4452 were here the first time: the previous comp_unit_die was stored in
4453 comp_unit_obstack. But there's no data yet that we need this
4454 optimization. */
4455 if (cu->dwo_unit != NULL)
4456 rereading_dwo_cu = 1;
4457 }
4458 else
4459 {
4460 /* If !use_existing_cu, this_cu->cu must be NULL. */
4461 gdb_assert (this_cu->cu == NULL);
4462
4463 cu = xmalloc (sizeof (*cu));
4464 init_one_comp_unit (cu, this_cu);
4465
4466 /* If an error occurs while loading, release our storage. */
4467 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4468 }
4469
4470 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4471 {
4472 /* We already have the header, there's no need to read it in again. */
4473 info_ptr += cu->header.first_die_offset.cu_off;
4474 }
4475 else
4476 {
4477 if (this_cu->is_debug_types)
4478 {
4479 ULONGEST signature;
4480 cu_offset type_offset_in_tu;
4481
4482 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4483 abbrev_section, info_ptr,
4484 &signature,
4485 &type_offset_in_tu);
4486
4487 /* Since per_cu is the first member of struct signatured_type,
4488 we can go from a pointer to one to a pointer to the other. */
4489 sig_type = (struct signatured_type *) this_cu;
4490 gdb_assert (sig_type->signature == signature);
4491 gdb_assert (sig_type->type_offset_in_tu.cu_off
4492 == type_offset_in_tu.cu_off);
4493 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4494
4495 /* LENGTH has not been set yet for type units if we're
4496 using .gdb_index. */
4497 this_cu->length = get_cu_length (&cu->header);
4498
4499 /* Establish the type offset that can be used to lookup the type. */
4500 sig_type->type_offset_in_section.sect_off =
4501 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4502 }
4503 else
4504 {
4505 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4506 abbrev_section,
4507 info_ptr, 0);
4508
4509 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4510 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4511 }
4512 }
4513
4514 /* Skip dummy compilation units. */
4515 if (info_ptr >= begin_info_ptr + this_cu->length
4516 || peek_abbrev_code (abfd, info_ptr) == 0)
4517 {
4518 do_cleanups (cleanups);
4519 return;
4520 }
4521
4522 /* If we don't have them yet, read the abbrevs for this compilation unit.
4523 And if we need to read them now, make sure they're freed when we're
4524 done. Note that it's important that if the CU had an abbrev table
4525 on entry we don't free it when we're done: Somewhere up the call stack
4526 it may be in use. */
4527 if (abbrev_table != NULL)
4528 {
4529 gdb_assert (cu->abbrev_table == NULL);
4530 gdb_assert (cu->header.abbrev_offset.sect_off
4531 == abbrev_table->offset.sect_off);
4532 cu->abbrev_table = abbrev_table;
4533 }
4534 else if (cu->abbrev_table == NULL)
4535 {
4536 dwarf2_read_abbrevs (cu, abbrev_section);
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
4539 else if (rereading_dwo_cu)
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, abbrev_section);
4543 }
4544
4545 /* Read the top level CU/TU die. */
4546 init_cu_die_reader (&reader, cu, section, NULL);
4547 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4548
4549 /* If we have a DWO stub, process it and then read in the DWO file.
4550 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4551 a DWO CU, that this test will fail. */
4552 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4553 if (attr)
4554 {
4555 const char *dwo_name = DW_STRING (attr);
4556 const char *comp_dir_string;
4557 struct dwo_unit *dwo_unit;
4558 ULONGEST signature; /* Or dwo_id. */
4559 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4560 int i,num_extra_attrs;
4561 struct dwarf2_section_info *dwo_abbrev_section;
4562
4563 if (has_children)
4564 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4565 " has children (offset 0x%x) [in module %s]"),
4566 this_cu->offset.sect_off, bfd_get_filename (abfd));
4567
4568 /* These attributes aren't processed until later:
4569 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4570 However, the attribute is found in the stub which we won't have later.
4571 In order to not impose this complication on the rest of the code,
4572 we read them here and copy them to the DWO CU/TU die. */
4573
4574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4575 DWO file. */
4576 stmt_list = NULL;
4577 if (! this_cu->is_debug_types)
4578 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4579 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4580 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4581 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4582 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4583
4584 /* There should be a DW_AT_addr_base attribute here (if needed).
4585 We need the value before we can process DW_FORM_GNU_addr_index. */
4586 cu->addr_base = 0;
4587 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4588 if (attr)
4589 cu->addr_base = DW_UNSND (attr);
4590
4591 /* There should be a DW_AT_ranges_base attribute here (if needed).
4592 We need the value before we can process DW_AT_ranges. */
4593 cu->ranges_base = 0;
4594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4595 if (attr)
4596 cu->ranges_base = DW_UNSND (attr);
4597
4598 if (this_cu->is_debug_types)
4599 {
4600 gdb_assert (sig_type != NULL);
4601 signature = sig_type->signature;
4602 }
4603 else
4604 {
4605 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4606 if (! attr)
4607 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4608 dwo_name);
4609 signature = DW_UNSND (attr);
4610 }
4611
4612 /* We may need the comp_dir in order to find the DWO file. */
4613 comp_dir_string = NULL;
4614 if (comp_dir)
4615 comp_dir_string = DW_STRING (comp_dir);
4616
4617 if (this_cu->is_debug_types)
4618 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4619 else
4620 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4621 signature);
4622
4623 if (dwo_unit == NULL)
4624 {
4625 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4626 " with ID %s [in module %s]"),
4627 this_cu->offset.sect_off,
4628 phex (signature, sizeof (signature)),
4629 objfile->name);
4630 }
4631
4632 /* Set up for reading the DWO CU/TU. */
4633 cu->dwo_unit = dwo_unit;
4634 section = dwo_unit->info_or_types_section;
4635 dwarf2_read_section (objfile, section);
4636 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4637 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4638 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4639
4640 if (this_cu->is_debug_types)
4641 {
4642 ULONGEST signature;
4643 cu_offset type_offset_in_tu;
4644
4645 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4646 dwo_abbrev_section,
4647 info_ptr,
4648 &signature,
4649 &type_offset_in_tu);
4650 gdb_assert (sig_type->signature == signature);
4651 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4652 /* For DWOs coming from DWP files, we don't know the CU length
4653 nor the type's offset in the TU until now. */
4654 dwo_unit->length = get_cu_length (&cu->header);
4655 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4656
4657 /* Establish the type offset that can be used to lookup the type.
4658 For DWO files, we don't know it until now. */
4659 sig_type->type_offset_in_section.sect_off =
4660 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4661 }
4662 else
4663 {
4664 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4665 dwo_abbrev_section,
4666 info_ptr, 0);
4667 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4668 /* For DWOs coming from DWP files, we don't know the CU length
4669 until now. */
4670 dwo_unit->length = get_cu_length (&cu->header);
4671 }
4672
4673 /* Discard the original CU's abbrev table, and read the DWO's. */
4674 if (abbrev_table == NULL)
4675 {
4676 dwarf2_free_abbrev_table (cu);
4677 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4678 }
4679 else
4680 {
4681 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4682 make_cleanup (dwarf2_free_abbrev_table, cu);
4683 }
4684
4685 /* Read in the die, but leave space to copy over the attributes
4686 from the stub. This has the benefit of simplifying the rest of
4687 the code - all the real work is done here. */
4688 num_extra_attrs = ((stmt_list != NULL)
4689 + (low_pc != NULL)
4690 + (high_pc != NULL)
4691 + (ranges != NULL)
4692 + (comp_dir != NULL));
4693 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4694 &has_children, num_extra_attrs);
4695
4696 /* Copy over the attributes from the stub to the DWO die. */
4697 i = comp_unit_die->num_attrs;
4698 if (stmt_list != NULL)
4699 comp_unit_die->attrs[i++] = *stmt_list;
4700 if (low_pc != NULL)
4701 comp_unit_die->attrs[i++] = *low_pc;
4702 if (high_pc != NULL)
4703 comp_unit_die->attrs[i++] = *high_pc;
4704 if (ranges != NULL)
4705 comp_unit_die->attrs[i++] = *ranges;
4706 if (comp_dir != NULL)
4707 comp_unit_die->attrs[i++] = *comp_dir;
4708 comp_unit_die->num_attrs += num_extra_attrs;
4709
4710 /* Skip dummy compilation units. */
4711 if (info_ptr >= begin_info_ptr + dwo_unit->length
4712 || peek_abbrev_code (abfd, info_ptr) == 0)
4713 {
4714 do_cleanups (cleanups);
4715 return;
4716 }
4717 }
4718
4719 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4720
4721 if (free_cu_cleanup != NULL)
4722 {
4723 if (keep)
4724 {
4725 /* We've successfully allocated this compilation unit. Let our
4726 caller clean it up when finished with it. */
4727 discard_cleanups (free_cu_cleanup);
4728
4729 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4730 So we have to manually free the abbrev table. */
4731 dwarf2_free_abbrev_table (cu);
4732
4733 /* Link this CU into read_in_chain. */
4734 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4735 dwarf2_per_objfile->read_in_chain = this_cu;
4736 }
4737 else
4738 do_cleanups (free_cu_cleanup);
4739 }
4740
4741 do_cleanups (cleanups);
4742 }
4743
4744 /* Read CU/TU THIS_CU in section SECTION,
4745 but do not follow DW_AT_GNU_dwo_name if present.
4746 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4747 to have already done the lookup to find the DWO/DWP file).
4748
4749 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4750 THIS_CU->is_debug_types, but nothing else.
4751
4752 We fill in THIS_CU->length.
4753
4754 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4755 linker) then DIE_READER_FUNC will not get called.
4756
4757 THIS_CU->cu is always freed when done.
4758 This is done in order to not leave THIS_CU->cu in a state where we have
4759 to care whether it refers to the "main" CU or the DWO CU. */
4760
4761 static void
4762 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4763 struct dwarf2_section_info *abbrev_section,
4764 struct dwo_file *dwo_file,
4765 die_reader_func_ftype *die_reader_func,
4766 void *data)
4767 {
4768 struct objfile *objfile = dwarf2_per_objfile->objfile;
4769 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4770 bfd *abfd = section->asection->owner;
4771 struct dwarf2_cu cu;
4772 gdb_byte *begin_info_ptr, *info_ptr;
4773 struct die_reader_specs reader;
4774 struct cleanup *cleanups;
4775 struct die_info *comp_unit_die;
4776 int has_children;
4777
4778 if (dwarf2_die_debug)
4779 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4780 this_cu->is_debug_types ? "type" : "comp",
4781 this_cu->offset.sect_off);
4782
4783 gdb_assert (this_cu->cu == NULL);
4784
4785 /* This is cheap if the section is already read in. */
4786 dwarf2_read_section (objfile, section);
4787
4788 init_one_comp_unit (&cu, this_cu);
4789
4790 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4791
4792 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4793 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4794 abbrev_section, info_ptr,
4795 this_cu->is_debug_types);
4796
4797 this_cu->length = get_cu_length (&cu.header);
4798
4799 /* Skip dummy compilation units. */
4800 if (info_ptr >= begin_info_ptr + this_cu->length
4801 || peek_abbrev_code (abfd, info_ptr) == 0)
4802 {
4803 do_cleanups (cleanups);
4804 return;
4805 }
4806
4807 dwarf2_read_abbrevs (&cu, abbrev_section);
4808 make_cleanup (dwarf2_free_abbrev_table, &cu);
4809
4810 init_cu_die_reader (&reader, &cu, section, dwo_file);
4811 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4812
4813 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4814
4815 do_cleanups (cleanups);
4816 }
4817
4818 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4819 does not lookup the specified DWO file.
4820 This cannot be used to read DWO files.
4821
4822 THIS_CU->cu is always freed when done.
4823 This is done in order to not leave THIS_CU->cu in a state where we have
4824 to care whether it refers to the "main" CU or the DWO CU.
4825 We can revisit this if the data shows there's a performance issue. */
4826
4827 static void
4828 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4829 die_reader_func_ftype *die_reader_func,
4830 void *data)
4831 {
4832 init_cutu_and_read_dies_no_follow (this_cu,
4833 get_abbrev_section_for_cu (this_cu),
4834 NULL,
4835 die_reader_func, data);
4836 }
4837
4838 /* Create a psymtab named NAME and assign it to PER_CU.
4839
4840 The caller must fill in the following details:
4841 dirname, textlow, texthigh. */
4842
4843 static struct partial_symtab *
4844 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4845 {
4846 struct objfile *objfile = per_cu->objfile;
4847 struct partial_symtab *pst;
4848
4849 pst = start_psymtab_common (objfile, objfile->section_offsets,
4850 name, 0,
4851 objfile->global_psymbols.next,
4852 objfile->static_psymbols.next);
4853
4854 pst->psymtabs_addrmap_supported = 1;
4855
4856 /* This is the glue that links PST into GDB's symbol API. */
4857 pst->read_symtab_private = per_cu;
4858 pst->read_symtab = dwarf2_read_symtab;
4859 per_cu->v.psymtab = pst;
4860
4861 return pst;
4862 }
4863
4864 /* die_reader_func for process_psymtab_comp_unit. */
4865
4866 static void
4867 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4868 gdb_byte *info_ptr,
4869 struct die_info *comp_unit_die,
4870 int has_children,
4871 void *data)
4872 {
4873 struct dwarf2_cu *cu = reader->cu;
4874 struct objfile *objfile = cu->objfile;
4875 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4876 struct attribute *attr;
4877 CORE_ADDR baseaddr;
4878 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4879 struct partial_symtab *pst;
4880 int has_pc_info;
4881 const char *filename;
4882 int *want_partial_unit_ptr = data;
4883
4884 if (comp_unit_die->tag == DW_TAG_partial_unit
4885 && (want_partial_unit_ptr == NULL
4886 || !*want_partial_unit_ptr))
4887 return;
4888
4889 gdb_assert (! per_cu->is_debug_types);
4890
4891 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4892
4893 cu->list_in_scope = &file_symbols;
4894
4895 /* Allocate a new partial symbol table structure. */
4896 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4897 if (attr == NULL || !DW_STRING (attr))
4898 filename = "";
4899 else
4900 filename = DW_STRING (attr);
4901
4902 pst = create_partial_symtab (per_cu, filename);
4903
4904 /* This must be done before calling dwarf2_build_include_psymtabs. */
4905 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4906 if (attr != NULL)
4907 pst->dirname = DW_STRING (attr);
4908
4909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4910
4911 dwarf2_find_base_address (comp_unit_die, cu);
4912
4913 /* Possibly set the default values of LOWPC and HIGHPC from
4914 `DW_AT_ranges'. */
4915 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4916 &best_highpc, cu, pst);
4917 if (has_pc_info == 1 && best_lowpc < best_highpc)
4918 /* Store the contiguous range if it is not empty; it can be empty for
4919 CUs with no code. */
4920 addrmap_set_empty (objfile->psymtabs_addrmap,
4921 best_lowpc + baseaddr,
4922 best_highpc + baseaddr - 1, pst);
4923
4924 /* Check if comp unit has_children.
4925 If so, read the rest of the partial symbols from this comp unit.
4926 If not, there's no more debug_info for this comp unit. */
4927 if (has_children)
4928 {
4929 struct partial_die_info *first_die;
4930 CORE_ADDR lowpc, highpc;
4931
4932 lowpc = ((CORE_ADDR) -1);
4933 highpc = ((CORE_ADDR) 0);
4934
4935 first_die = load_partial_dies (reader, info_ptr, 1);
4936
4937 scan_partial_symbols (first_die, &lowpc, &highpc,
4938 ! has_pc_info, cu);
4939
4940 /* If we didn't find a lowpc, set it to highpc to avoid
4941 complaints from `maint check'. */
4942 if (lowpc == ((CORE_ADDR) -1))
4943 lowpc = highpc;
4944
4945 /* If the compilation unit didn't have an explicit address range,
4946 then use the information extracted from its child dies. */
4947 if (! has_pc_info)
4948 {
4949 best_lowpc = lowpc;
4950 best_highpc = highpc;
4951 }
4952 }
4953 pst->textlow = best_lowpc + baseaddr;
4954 pst->texthigh = best_highpc + baseaddr;
4955
4956 pst->n_global_syms = objfile->global_psymbols.next -
4957 (objfile->global_psymbols.list + pst->globals_offset);
4958 pst->n_static_syms = objfile->static_psymbols.next -
4959 (objfile->static_psymbols.list + pst->statics_offset);
4960 sort_pst_symbols (objfile, pst);
4961
4962 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4963 {
4964 int i;
4965 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4966 struct dwarf2_per_cu_data *iter;
4967
4968 /* Fill in 'dependencies' here; we fill in 'users' in a
4969 post-pass. */
4970 pst->number_of_dependencies = len;
4971 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4972 len * sizeof (struct symtab *));
4973 for (i = 0;
4974 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4975 i, iter);
4976 ++i)
4977 pst->dependencies[i] = iter->v.psymtab;
4978
4979 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4980 }
4981
4982 /* Get the list of files included in the current compilation unit,
4983 and build a psymtab for each of them. */
4984 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4985
4986 if (dwarf2_read_debug)
4987 {
4988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4989
4990 fprintf_unfiltered (gdb_stdlog,
4991 "Psymtab for %s unit @0x%x: %s - %s"
4992 ", %d global, %d static syms\n",
4993 per_cu->is_debug_types ? "type" : "comp",
4994 per_cu->offset.sect_off,
4995 paddress (gdbarch, pst->textlow),
4996 paddress (gdbarch, pst->texthigh),
4997 pst->n_global_syms, pst->n_static_syms);
4998 }
4999 }
5000
5001 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5002 Process compilation unit THIS_CU for a psymtab. */
5003
5004 static void
5005 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5006 int want_partial_unit)
5007 {
5008 /* If this compilation unit was already read in, free the
5009 cached copy in order to read it in again. This is
5010 necessary because we skipped some symbols when we first
5011 read in the compilation unit (see load_partial_dies).
5012 This problem could be avoided, but the benefit is unclear. */
5013 if (this_cu->cu != NULL)
5014 free_one_cached_comp_unit (this_cu);
5015
5016 gdb_assert (! this_cu->is_debug_types);
5017 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5018 process_psymtab_comp_unit_reader,
5019 &want_partial_unit);
5020
5021 /* Age out any secondary CUs. */
5022 age_cached_comp_units ();
5023 }
5024
5025 static hashval_t
5026 hash_type_unit_group (const void *item)
5027 {
5028 const struct type_unit_group *tu_group = item;
5029
5030 return hash_stmt_list_entry (&tu_group->hash);
5031 }
5032
5033 static int
5034 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5035 {
5036 const struct type_unit_group *lhs = item_lhs;
5037 const struct type_unit_group *rhs = item_rhs;
5038
5039 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5040 }
5041
5042 /* Allocate a hash table for type unit groups. */
5043
5044 static htab_t
5045 allocate_type_unit_groups_table (void)
5046 {
5047 return htab_create_alloc_ex (3,
5048 hash_type_unit_group,
5049 eq_type_unit_group,
5050 NULL,
5051 &dwarf2_per_objfile->objfile->objfile_obstack,
5052 hashtab_obstack_allocate,
5053 dummy_obstack_deallocate);
5054 }
5055
5056 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5057 partial symtabs. We combine several TUs per psymtab to not let the size
5058 of any one psymtab grow too big. */
5059 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5060 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5061
5062 /* Helper routine for get_type_unit_group.
5063 Create the type_unit_group object used to hold one or more TUs. */
5064
5065 static struct type_unit_group *
5066 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5067 {
5068 struct objfile *objfile = dwarf2_per_objfile->objfile;
5069 struct dwarf2_per_cu_data *per_cu;
5070 struct type_unit_group *tu_group;
5071
5072 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5073 struct type_unit_group);
5074 per_cu = &tu_group->per_cu;
5075 per_cu->objfile = objfile;
5076 per_cu->is_debug_types = 1;
5077 per_cu->s.type_unit_group = tu_group;
5078
5079 if (dwarf2_per_objfile->using_index)
5080 {
5081 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5082 struct dwarf2_per_cu_quick_data);
5083 tu_group->t.first_tu = cu->per_cu;
5084 }
5085 else
5086 {
5087 unsigned int line_offset = line_offset_struct.sect_off;
5088 struct partial_symtab *pst;
5089 char *name;
5090
5091 /* Give the symtab a useful name for debug purposes. */
5092 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5093 name = xstrprintf ("<type_units_%d>",
5094 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5095 else
5096 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5097
5098 pst = create_partial_symtab (per_cu, name);
5099 pst->anonymous = 1;
5100
5101 xfree (name);
5102 }
5103
5104 tu_group->hash.dwo_unit = cu->dwo_unit;
5105 tu_group->hash.line_offset = line_offset_struct;
5106
5107 return tu_group;
5108 }
5109
5110 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5111 STMT_LIST is a DW_AT_stmt_list attribute. */
5112
5113 static struct type_unit_group *
5114 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5115 {
5116 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5117 struct type_unit_group *tu_group;
5118 void **slot;
5119 unsigned int line_offset;
5120 struct type_unit_group type_unit_group_for_lookup;
5121
5122 if (dwarf2_per_objfile->type_unit_groups == NULL)
5123 {
5124 dwarf2_per_objfile->type_unit_groups =
5125 allocate_type_unit_groups_table ();
5126 }
5127
5128 /* Do we need to create a new group, or can we use an existing one? */
5129
5130 if (stmt_list)
5131 {
5132 line_offset = DW_UNSND (stmt_list);
5133 ++tu_stats->nr_symtab_sharers;
5134 }
5135 else
5136 {
5137 /* Ugh, no stmt_list. Rare, but we have to handle it.
5138 We can do various things here like create one group per TU or
5139 spread them over multiple groups to split up the expansion work.
5140 To avoid worst case scenarios (too many groups or too large groups)
5141 we, umm, group them in bunches. */
5142 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5143 | (tu_stats->nr_stmt_less_type_units
5144 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5145 ++tu_stats->nr_stmt_less_type_units;
5146 }
5147
5148 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5149 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5150 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5151 &type_unit_group_for_lookup, INSERT);
5152 if (*slot != NULL)
5153 {
5154 tu_group = *slot;
5155 gdb_assert (tu_group != NULL);
5156 }
5157 else
5158 {
5159 sect_offset line_offset_struct;
5160
5161 line_offset_struct.sect_off = line_offset;
5162 tu_group = create_type_unit_group (cu, line_offset_struct);
5163 *slot = tu_group;
5164 ++tu_stats->nr_symtabs;
5165 }
5166
5167 return tu_group;
5168 }
5169
5170 /* Struct used to sort TUs by their abbreviation table offset. */
5171
5172 struct tu_abbrev_offset
5173 {
5174 struct signatured_type *sig_type;
5175 sect_offset abbrev_offset;
5176 };
5177
5178 /* Helper routine for build_type_unit_groups, passed to qsort. */
5179
5180 static int
5181 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5182 {
5183 const struct tu_abbrev_offset * const *a = ap;
5184 const struct tu_abbrev_offset * const *b = bp;
5185 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5186 unsigned int boff = (*b)->abbrev_offset.sect_off;
5187
5188 return (aoff > boff) - (aoff < boff);
5189 }
5190
5191 /* A helper function to add a type_unit_group to a table. */
5192
5193 static int
5194 add_type_unit_group_to_table (void **slot, void *datum)
5195 {
5196 struct type_unit_group *tu_group = *slot;
5197 struct type_unit_group ***datap = datum;
5198
5199 **datap = tu_group;
5200 ++*datap;
5201
5202 return 1;
5203 }
5204
5205 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5206 each one passing FUNC,DATA.
5207
5208 The efficiency is because we sort TUs by the abbrev table they use and
5209 only read each abbrev table once. In one program there are 200K TUs
5210 sharing 8K abbrev tables.
5211
5212 The main purpose of this function is to support building the
5213 dwarf2_per_objfile->type_unit_groups table.
5214 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5215 can collapse the search space by grouping them by stmt_list.
5216 The savings can be significant, in the same program from above the 200K TUs
5217 share 8K stmt_list tables.
5218
5219 FUNC is expected to call get_type_unit_group, which will create the
5220 struct type_unit_group if necessary and add it to
5221 dwarf2_per_objfile->type_unit_groups. */
5222
5223 static void
5224 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5225 {
5226 struct objfile *objfile = dwarf2_per_objfile->objfile;
5227 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5228 struct cleanup *cleanups;
5229 struct abbrev_table *abbrev_table;
5230 sect_offset abbrev_offset;
5231 struct tu_abbrev_offset *sorted_by_abbrev;
5232 struct type_unit_group **iter;
5233 int i;
5234
5235 /* It's up to the caller to not call us multiple times. */
5236 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5237
5238 if (dwarf2_per_objfile->n_type_units == 0)
5239 return;
5240
5241 /* TUs typically share abbrev tables, and there can be way more TUs than
5242 abbrev tables. Sort by abbrev table to reduce the number of times we
5243 read each abbrev table in.
5244 Alternatives are to punt or to maintain a cache of abbrev tables.
5245 This is simpler and efficient enough for now.
5246
5247 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5248 symtab to use). Typically TUs with the same abbrev offset have the same
5249 stmt_list value too so in practice this should work well.
5250
5251 The basic algorithm here is:
5252
5253 sort TUs by abbrev table
5254 for each TU with same abbrev table:
5255 read abbrev table if first user
5256 read TU top level DIE
5257 [IWBN if DWO skeletons had DW_AT_stmt_list]
5258 call FUNC */
5259
5260 if (dwarf2_read_debug)
5261 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5262
5263 /* Sort in a separate table to maintain the order of all_type_units
5264 for .gdb_index: TU indices directly index all_type_units. */
5265 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5266 dwarf2_per_objfile->n_type_units);
5267 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5268 {
5269 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5270
5271 sorted_by_abbrev[i].sig_type = sig_type;
5272 sorted_by_abbrev[i].abbrev_offset =
5273 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5274 sig_type->per_cu.offset);
5275 }
5276 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5277 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5278 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5279
5280 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5281 called any number of times, so we don't reset tu_stats here. */
5282
5283 abbrev_offset.sect_off = ~(unsigned) 0;
5284 abbrev_table = NULL;
5285 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5286
5287 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5288 {
5289 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5290
5291 /* Switch to the next abbrev table if necessary. */
5292 if (abbrev_table == NULL
5293 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5294 {
5295 if (abbrev_table != NULL)
5296 {
5297 abbrev_table_free (abbrev_table);
5298 /* Reset to NULL in case abbrev_table_read_table throws
5299 an error: abbrev_table_free_cleanup will get called. */
5300 abbrev_table = NULL;
5301 }
5302 abbrev_offset = tu->abbrev_offset;
5303 abbrev_table =
5304 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5305 abbrev_offset);
5306 ++tu_stats->nr_uniq_abbrev_tables;
5307 }
5308
5309 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5310 func, data);
5311 }
5312
5313 /* Create a vector of pointers to primary type units to make it easy to
5314 iterate over them and CUs. See dw2_get_primary_cu. */
5315 dwarf2_per_objfile->n_type_unit_groups =
5316 htab_elements (dwarf2_per_objfile->type_unit_groups);
5317 dwarf2_per_objfile->all_type_unit_groups =
5318 obstack_alloc (&objfile->objfile_obstack,
5319 dwarf2_per_objfile->n_type_unit_groups
5320 * sizeof (struct type_unit_group *));
5321 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5322 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5323 add_type_unit_group_to_table, &iter);
5324 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5325 == dwarf2_per_objfile->n_type_unit_groups);
5326
5327 do_cleanups (cleanups);
5328
5329 if (dwarf2_read_debug)
5330 {
5331 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5332 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5333 dwarf2_per_objfile->n_type_units);
5334 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5335 tu_stats->nr_uniq_abbrev_tables);
5336 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5337 tu_stats->nr_symtabs);
5338 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5339 tu_stats->nr_symtab_sharers);
5340 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5341 tu_stats->nr_stmt_less_type_units);
5342 }
5343 }
5344
5345 /* Reader function for build_type_psymtabs. */
5346
5347 static void
5348 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5349 gdb_byte *info_ptr,
5350 struct die_info *type_unit_die,
5351 int has_children,
5352 void *data)
5353 {
5354 struct objfile *objfile = dwarf2_per_objfile->objfile;
5355 struct dwarf2_cu *cu = reader->cu;
5356 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5357 struct type_unit_group *tu_group;
5358 struct attribute *attr;
5359 struct partial_die_info *first_die;
5360 CORE_ADDR lowpc, highpc;
5361 struct partial_symtab *pst;
5362
5363 gdb_assert (data == NULL);
5364
5365 if (! has_children)
5366 return;
5367
5368 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5369 tu_group = get_type_unit_group (cu, attr);
5370
5371 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5372
5373 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5374 cu->list_in_scope = &file_symbols;
5375 pst = create_partial_symtab (per_cu, "");
5376 pst->anonymous = 1;
5377
5378 first_die = load_partial_dies (reader, info_ptr, 1);
5379
5380 lowpc = (CORE_ADDR) -1;
5381 highpc = (CORE_ADDR) 0;
5382 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5383
5384 pst->n_global_syms = objfile->global_psymbols.next -
5385 (objfile->global_psymbols.list + pst->globals_offset);
5386 pst->n_static_syms = objfile->static_psymbols.next -
5387 (objfile->static_psymbols.list + pst->statics_offset);
5388 sort_pst_symbols (objfile, pst);
5389 }
5390
5391 /* Traversal function for build_type_psymtabs. */
5392
5393 static int
5394 build_type_psymtab_dependencies (void **slot, void *info)
5395 {
5396 struct objfile *objfile = dwarf2_per_objfile->objfile;
5397 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5398 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5399 struct partial_symtab *pst = per_cu->v.psymtab;
5400 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5401 struct dwarf2_per_cu_data *iter;
5402 int i;
5403
5404 gdb_assert (len > 0);
5405
5406 pst->number_of_dependencies = len;
5407 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5408 len * sizeof (struct psymtab *));
5409 for (i = 0;
5410 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5411 ++i)
5412 {
5413 pst->dependencies[i] = iter->v.psymtab;
5414 iter->s.type_unit_group = tu_group;
5415 }
5416
5417 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5418
5419 return 1;
5420 }
5421
5422 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5423 Build partial symbol tables for the .debug_types comp-units. */
5424
5425 static void
5426 build_type_psymtabs (struct objfile *objfile)
5427 {
5428 if (! create_all_type_units (objfile))
5429 return;
5430
5431 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5432
5433 /* Now that all TUs have been processed we can fill in the dependencies. */
5434 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5435 build_type_psymtab_dependencies, NULL);
5436 }
5437
5438 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5439
5440 static void
5441 psymtabs_addrmap_cleanup (void *o)
5442 {
5443 struct objfile *objfile = o;
5444
5445 objfile->psymtabs_addrmap = NULL;
5446 }
5447
5448 /* Compute the 'user' field for each psymtab in OBJFILE. */
5449
5450 static void
5451 set_partial_user (struct objfile *objfile)
5452 {
5453 int i;
5454
5455 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5456 {
5457 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5458 struct partial_symtab *pst = per_cu->v.psymtab;
5459 int j;
5460
5461 if (pst == NULL)
5462 continue;
5463
5464 for (j = 0; j < pst->number_of_dependencies; ++j)
5465 {
5466 /* Set the 'user' field only if it is not already set. */
5467 if (pst->dependencies[j]->user == NULL)
5468 pst->dependencies[j]->user = pst;
5469 }
5470 }
5471 }
5472
5473 /* Build the partial symbol table by doing a quick pass through the
5474 .debug_info and .debug_abbrev sections. */
5475
5476 static void
5477 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5478 {
5479 struct cleanup *back_to, *addrmap_cleanup;
5480 struct obstack temp_obstack;
5481 int i;
5482
5483 if (dwarf2_read_debug)
5484 {
5485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5486 objfile->name);
5487 }
5488
5489 dwarf2_per_objfile->reading_partial_symbols = 1;
5490
5491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5492
5493 /* Any cached compilation units will be linked by the per-objfile
5494 read_in_chain. Make sure to free them when we're done. */
5495 back_to = make_cleanup (free_cached_comp_units, NULL);
5496
5497 build_type_psymtabs (objfile);
5498
5499 create_all_comp_units (objfile);
5500
5501 /* Create a temporary address map on a temporary obstack. We later
5502 copy this to the final obstack. */
5503 obstack_init (&temp_obstack);
5504 make_cleanup_obstack_free (&temp_obstack);
5505 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5506 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5507
5508 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5509 {
5510 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5511
5512 process_psymtab_comp_unit (per_cu, 0);
5513 }
5514
5515 set_partial_user (objfile);
5516
5517 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5518 &objfile->objfile_obstack);
5519 discard_cleanups (addrmap_cleanup);
5520
5521 do_cleanups (back_to);
5522
5523 if (dwarf2_read_debug)
5524 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5525 objfile->name);
5526 }
5527
5528 /* die_reader_func for load_partial_comp_unit. */
5529
5530 static void
5531 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5532 gdb_byte *info_ptr,
5533 struct die_info *comp_unit_die,
5534 int has_children,
5535 void *data)
5536 {
5537 struct dwarf2_cu *cu = reader->cu;
5538
5539 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5540
5541 /* Check if comp unit has_children.
5542 If so, read the rest of the partial symbols from this comp unit.
5543 If not, there's no more debug_info for this comp unit. */
5544 if (has_children)
5545 load_partial_dies (reader, info_ptr, 0);
5546 }
5547
5548 /* Load the partial DIEs for a secondary CU into memory.
5549 This is also used when rereading a primary CU with load_all_dies. */
5550
5551 static void
5552 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5553 {
5554 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5555 load_partial_comp_unit_reader, NULL);
5556 }
5557
5558 static void
5559 read_comp_units_from_section (struct objfile *objfile,
5560 struct dwarf2_section_info *section,
5561 unsigned int is_dwz,
5562 int *n_allocated,
5563 int *n_comp_units,
5564 struct dwarf2_per_cu_data ***all_comp_units)
5565 {
5566 gdb_byte *info_ptr;
5567 bfd *abfd = section->asection->owner;
5568
5569 dwarf2_read_section (objfile, section);
5570
5571 info_ptr = section->buffer;
5572
5573 while (info_ptr < section->buffer + section->size)
5574 {
5575 unsigned int length, initial_length_size;
5576 struct dwarf2_per_cu_data *this_cu;
5577 sect_offset offset;
5578
5579 offset.sect_off = info_ptr - section->buffer;
5580
5581 /* Read just enough information to find out where the next
5582 compilation unit is. */
5583 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5584
5585 /* Save the compilation unit for later lookup. */
5586 this_cu = obstack_alloc (&objfile->objfile_obstack,
5587 sizeof (struct dwarf2_per_cu_data));
5588 memset (this_cu, 0, sizeof (*this_cu));
5589 this_cu->offset = offset;
5590 this_cu->length = length + initial_length_size;
5591 this_cu->is_dwz = is_dwz;
5592 this_cu->objfile = objfile;
5593 this_cu->info_or_types_section = section;
5594
5595 if (*n_comp_units == *n_allocated)
5596 {
5597 *n_allocated *= 2;
5598 *all_comp_units = xrealloc (*all_comp_units,
5599 *n_allocated
5600 * sizeof (struct dwarf2_per_cu_data *));
5601 }
5602 (*all_comp_units)[*n_comp_units] = this_cu;
5603 ++*n_comp_units;
5604
5605 info_ptr = info_ptr + this_cu->length;
5606 }
5607 }
5608
5609 /* Create a list of all compilation units in OBJFILE.
5610 This is only done for -readnow and building partial symtabs. */
5611
5612 static void
5613 create_all_comp_units (struct objfile *objfile)
5614 {
5615 int n_allocated;
5616 int n_comp_units;
5617 struct dwarf2_per_cu_data **all_comp_units;
5618
5619 n_comp_units = 0;
5620 n_allocated = 10;
5621 all_comp_units = xmalloc (n_allocated
5622 * sizeof (struct dwarf2_per_cu_data *));
5623
5624 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5625 &n_allocated, &n_comp_units, &all_comp_units);
5626
5627 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5628 {
5629 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5630
5631 read_comp_units_from_section (objfile, &dwz->info, 1,
5632 &n_allocated, &n_comp_units,
5633 &all_comp_units);
5634 }
5635
5636 dwarf2_per_objfile->all_comp_units
5637 = obstack_alloc (&objfile->objfile_obstack,
5638 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5639 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5640 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5641 xfree (all_comp_units);
5642 dwarf2_per_objfile->n_comp_units = n_comp_units;
5643 }
5644
5645 /* Process all loaded DIEs for compilation unit CU, starting at
5646 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5647 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5648 DW_AT_ranges). If NEED_PC is set, then this function will set
5649 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5650 and record the covered ranges in the addrmap. */
5651
5652 static void
5653 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5654 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5655 {
5656 struct partial_die_info *pdi;
5657
5658 /* Now, march along the PDI's, descending into ones which have
5659 interesting children but skipping the children of the other ones,
5660 until we reach the end of the compilation unit. */
5661
5662 pdi = first_die;
5663
5664 while (pdi != NULL)
5665 {
5666 fixup_partial_die (pdi, cu);
5667
5668 /* Anonymous namespaces or modules have no name but have interesting
5669 children, so we need to look at them. Ditto for anonymous
5670 enums. */
5671
5672 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5673 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5674 || pdi->tag == DW_TAG_imported_unit)
5675 {
5676 switch (pdi->tag)
5677 {
5678 case DW_TAG_subprogram:
5679 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5680 break;
5681 case DW_TAG_constant:
5682 case DW_TAG_variable:
5683 case DW_TAG_typedef:
5684 case DW_TAG_union_type:
5685 if (!pdi->is_declaration)
5686 {
5687 add_partial_symbol (pdi, cu);
5688 }
5689 break;
5690 case DW_TAG_class_type:
5691 case DW_TAG_interface_type:
5692 case DW_TAG_structure_type:
5693 if (!pdi->is_declaration)
5694 {
5695 add_partial_symbol (pdi, cu);
5696 }
5697 break;
5698 case DW_TAG_enumeration_type:
5699 if (!pdi->is_declaration)
5700 add_partial_enumeration (pdi, cu);
5701 break;
5702 case DW_TAG_base_type:
5703 case DW_TAG_subrange_type:
5704 /* File scope base type definitions are added to the partial
5705 symbol table. */
5706 add_partial_symbol (pdi, cu);
5707 break;
5708 case DW_TAG_namespace:
5709 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5710 break;
5711 case DW_TAG_module:
5712 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5713 break;
5714 case DW_TAG_imported_unit:
5715 {
5716 struct dwarf2_per_cu_data *per_cu;
5717
5718 /* For now we don't handle imported units in type units. */
5719 if (cu->per_cu->is_debug_types)
5720 {
5721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5722 " supported in type units [in module %s]"),
5723 cu->objfile->name);
5724 }
5725
5726 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5727 pdi->is_dwz,
5728 cu->objfile);
5729
5730 /* Go read the partial unit, if needed. */
5731 if (per_cu->v.psymtab == NULL)
5732 process_psymtab_comp_unit (per_cu, 1);
5733
5734 VEC_safe_push (dwarf2_per_cu_ptr,
5735 cu->per_cu->s.imported_symtabs, per_cu);
5736 }
5737 break;
5738 default:
5739 break;
5740 }
5741 }
5742
5743 /* If the die has a sibling, skip to the sibling. */
5744
5745 pdi = pdi->die_sibling;
5746 }
5747 }
5748
5749 /* Functions used to compute the fully scoped name of a partial DIE.
5750
5751 Normally, this is simple. For C++, the parent DIE's fully scoped
5752 name is concatenated with "::" and the partial DIE's name. For
5753 Java, the same thing occurs except that "." is used instead of "::".
5754 Enumerators are an exception; they use the scope of their parent
5755 enumeration type, i.e. the name of the enumeration type is not
5756 prepended to the enumerator.
5757
5758 There are two complexities. One is DW_AT_specification; in this
5759 case "parent" means the parent of the target of the specification,
5760 instead of the direct parent of the DIE. The other is compilers
5761 which do not emit DW_TAG_namespace; in this case we try to guess
5762 the fully qualified name of structure types from their members'
5763 linkage names. This must be done using the DIE's children rather
5764 than the children of any DW_AT_specification target. We only need
5765 to do this for structures at the top level, i.e. if the target of
5766 any DW_AT_specification (if any; otherwise the DIE itself) does not
5767 have a parent. */
5768
5769 /* Compute the scope prefix associated with PDI's parent, in
5770 compilation unit CU. The result will be allocated on CU's
5771 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5772 field. NULL is returned if no prefix is necessary. */
5773 static const char *
5774 partial_die_parent_scope (struct partial_die_info *pdi,
5775 struct dwarf2_cu *cu)
5776 {
5777 const char *grandparent_scope;
5778 struct partial_die_info *parent, *real_pdi;
5779
5780 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5781 then this means the parent of the specification DIE. */
5782
5783 real_pdi = pdi;
5784 while (real_pdi->has_specification)
5785 real_pdi = find_partial_die (real_pdi->spec_offset,
5786 real_pdi->spec_is_dwz, cu);
5787
5788 parent = real_pdi->die_parent;
5789 if (parent == NULL)
5790 return NULL;
5791
5792 if (parent->scope_set)
5793 return parent->scope;
5794
5795 fixup_partial_die (parent, cu);
5796
5797 grandparent_scope = partial_die_parent_scope (parent, cu);
5798
5799 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5800 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5801 Work around this problem here. */
5802 if (cu->language == language_cplus
5803 && parent->tag == DW_TAG_namespace
5804 && strcmp (parent->name, "::") == 0
5805 && grandparent_scope == NULL)
5806 {
5807 parent->scope = NULL;
5808 parent->scope_set = 1;
5809 return NULL;
5810 }
5811
5812 if (pdi->tag == DW_TAG_enumerator)
5813 /* Enumerators should not get the name of the enumeration as a prefix. */
5814 parent->scope = grandparent_scope;
5815 else if (parent->tag == DW_TAG_namespace
5816 || parent->tag == DW_TAG_module
5817 || parent->tag == DW_TAG_structure_type
5818 || parent->tag == DW_TAG_class_type
5819 || parent->tag == DW_TAG_interface_type
5820 || parent->tag == DW_TAG_union_type
5821 || parent->tag == DW_TAG_enumeration_type)
5822 {
5823 if (grandparent_scope == NULL)
5824 parent->scope = parent->name;
5825 else
5826 parent->scope = typename_concat (&cu->comp_unit_obstack,
5827 grandparent_scope,
5828 parent->name, 0, cu);
5829 }
5830 else
5831 {
5832 /* FIXME drow/2004-04-01: What should we be doing with
5833 function-local names? For partial symbols, we should probably be
5834 ignoring them. */
5835 complaint (&symfile_complaints,
5836 _("unhandled containing DIE tag %d for DIE at %d"),
5837 parent->tag, pdi->offset.sect_off);
5838 parent->scope = grandparent_scope;
5839 }
5840
5841 parent->scope_set = 1;
5842 return parent->scope;
5843 }
5844
5845 /* Return the fully scoped name associated with PDI, from compilation unit
5846 CU. The result will be allocated with malloc. */
5847
5848 static char *
5849 partial_die_full_name (struct partial_die_info *pdi,
5850 struct dwarf2_cu *cu)
5851 {
5852 const char *parent_scope;
5853
5854 /* If this is a template instantiation, we can not work out the
5855 template arguments from partial DIEs. So, unfortunately, we have
5856 to go through the full DIEs. At least any work we do building
5857 types here will be reused if full symbols are loaded later. */
5858 if (pdi->has_template_arguments)
5859 {
5860 fixup_partial_die (pdi, cu);
5861
5862 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5863 {
5864 struct die_info *die;
5865 struct attribute attr;
5866 struct dwarf2_cu *ref_cu = cu;
5867
5868 /* DW_FORM_ref_addr is using section offset. */
5869 attr.name = 0;
5870 attr.form = DW_FORM_ref_addr;
5871 attr.u.unsnd = pdi->offset.sect_off;
5872 die = follow_die_ref (NULL, &attr, &ref_cu);
5873
5874 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5875 }
5876 }
5877
5878 parent_scope = partial_die_parent_scope (pdi, cu);
5879 if (parent_scope == NULL)
5880 return NULL;
5881 else
5882 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5883 }
5884
5885 static void
5886 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5887 {
5888 struct objfile *objfile = cu->objfile;
5889 CORE_ADDR addr = 0;
5890 const char *actual_name = NULL;
5891 CORE_ADDR baseaddr;
5892 char *built_actual_name;
5893
5894 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5895
5896 built_actual_name = partial_die_full_name (pdi, cu);
5897 if (built_actual_name != NULL)
5898 actual_name = built_actual_name;
5899
5900 if (actual_name == NULL)
5901 actual_name = pdi->name;
5902
5903 switch (pdi->tag)
5904 {
5905 case DW_TAG_subprogram:
5906 if (pdi->is_external || cu->language == language_ada)
5907 {
5908 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5909 of the global scope. But in Ada, we want to be able to access
5910 nested procedures globally. So all Ada subprograms are stored
5911 in the global scope. */
5912 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5913 mst_text, objfile); */
5914 add_psymbol_to_list (actual_name, strlen (actual_name),
5915 built_actual_name != NULL,
5916 VAR_DOMAIN, LOC_BLOCK,
5917 &objfile->global_psymbols,
5918 0, pdi->lowpc + baseaddr,
5919 cu->language, objfile);
5920 }
5921 else
5922 {
5923 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5924 mst_file_text, objfile); */
5925 add_psymbol_to_list (actual_name, strlen (actual_name),
5926 built_actual_name != NULL,
5927 VAR_DOMAIN, LOC_BLOCK,
5928 &objfile->static_psymbols,
5929 0, pdi->lowpc + baseaddr,
5930 cu->language, objfile);
5931 }
5932 break;
5933 case DW_TAG_constant:
5934 {
5935 struct psymbol_allocation_list *list;
5936
5937 if (pdi->is_external)
5938 list = &objfile->global_psymbols;
5939 else
5940 list = &objfile->static_psymbols;
5941 add_psymbol_to_list (actual_name, strlen (actual_name),
5942 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5943 list, 0, 0, cu->language, objfile);
5944 }
5945 break;
5946 case DW_TAG_variable:
5947 if (pdi->d.locdesc)
5948 addr = decode_locdesc (pdi->d.locdesc, cu);
5949
5950 if (pdi->d.locdesc
5951 && addr == 0
5952 && !dwarf2_per_objfile->has_section_at_zero)
5953 {
5954 /* A global or static variable may also have been stripped
5955 out by the linker if unused, in which case its address
5956 will be nullified; do not add such variables into partial
5957 symbol table then. */
5958 }
5959 else if (pdi->is_external)
5960 {
5961 /* Global Variable.
5962 Don't enter into the minimal symbol tables as there is
5963 a minimal symbol table entry from the ELF symbols already.
5964 Enter into partial symbol table if it has a location
5965 descriptor or a type.
5966 If the location descriptor is missing, new_symbol will create
5967 a LOC_UNRESOLVED symbol, the address of the variable will then
5968 be determined from the minimal symbol table whenever the variable
5969 is referenced.
5970 The address for the partial symbol table entry is not
5971 used by GDB, but it comes in handy for debugging partial symbol
5972 table building. */
5973
5974 if (pdi->d.locdesc || pdi->has_type)
5975 add_psymbol_to_list (actual_name, strlen (actual_name),
5976 built_actual_name != NULL,
5977 VAR_DOMAIN, LOC_STATIC,
5978 &objfile->global_psymbols,
5979 0, addr + baseaddr,
5980 cu->language, objfile);
5981 }
5982 else
5983 {
5984 /* Static Variable. Skip symbols without location descriptors. */
5985 if (pdi->d.locdesc == NULL)
5986 {
5987 xfree (built_actual_name);
5988 return;
5989 }
5990 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5991 mst_file_data, objfile); */
5992 add_psymbol_to_list (actual_name, strlen (actual_name),
5993 built_actual_name != NULL,
5994 VAR_DOMAIN, LOC_STATIC,
5995 &objfile->static_psymbols,
5996 0, addr + baseaddr,
5997 cu->language, objfile);
5998 }
5999 break;
6000 case DW_TAG_typedef:
6001 case DW_TAG_base_type:
6002 case DW_TAG_subrange_type:
6003 add_psymbol_to_list (actual_name, strlen (actual_name),
6004 built_actual_name != NULL,
6005 VAR_DOMAIN, LOC_TYPEDEF,
6006 &objfile->static_psymbols,
6007 0, (CORE_ADDR) 0, cu->language, objfile);
6008 break;
6009 case DW_TAG_namespace:
6010 add_psymbol_to_list (actual_name, strlen (actual_name),
6011 built_actual_name != NULL,
6012 VAR_DOMAIN, LOC_TYPEDEF,
6013 &objfile->global_psymbols,
6014 0, (CORE_ADDR) 0, cu->language, objfile);
6015 break;
6016 case DW_TAG_class_type:
6017 case DW_TAG_interface_type:
6018 case DW_TAG_structure_type:
6019 case DW_TAG_union_type:
6020 case DW_TAG_enumeration_type:
6021 /* Skip external references. The DWARF standard says in the section
6022 about "Structure, Union, and Class Type Entries": "An incomplete
6023 structure, union or class type is represented by a structure,
6024 union or class entry that does not have a byte size attribute
6025 and that has a DW_AT_declaration attribute." */
6026 if (!pdi->has_byte_size && pdi->is_declaration)
6027 {
6028 xfree (built_actual_name);
6029 return;
6030 }
6031
6032 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6033 static vs. global. */
6034 add_psymbol_to_list (actual_name, strlen (actual_name),
6035 built_actual_name != NULL,
6036 STRUCT_DOMAIN, LOC_TYPEDEF,
6037 (cu->language == language_cplus
6038 || cu->language == language_java)
6039 ? &objfile->global_psymbols
6040 : &objfile->static_psymbols,
6041 0, (CORE_ADDR) 0, cu->language, objfile);
6042
6043 break;
6044 case DW_TAG_enumerator:
6045 add_psymbol_to_list (actual_name, strlen (actual_name),
6046 built_actual_name != NULL,
6047 VAR_DOMAIN, LOC_CONST,
6048 (cu->language == language_cplus
6049 || cu->language == language_java)
6050 ? &objfile->global_psymbols
6051 : &objfile->static_psymbols,
6052 0, (CORE_ADDR) 0, cu->language, objfile);
6053 break;
6054 default:
6055 break;
6056 }
6057
6058 xfree (built_actual_name);
6059 }
6060
6061 /* Read a partial die corresponding to a namespace; also, add a symbol
6062 corresponding to that namespace to the symbol table. NAMESPACE is
6063 the name of the enclosing namespace. */
6064
6065 static void
6066 add_partial_namespace (struct partial_die_info *pdi,
6067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6068 int need_pc, struct dwarf2_cu *cu)
6069 {
6070 /* Add a symbol for the namespace. */
6071
6072 add_partial_symbol (pdi, cu);
6073
6074 /* Now scan partial symbols in that namespace. */
6075
6076 if (pdi->has_children)
6077 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6078 }
6079
6080 /* Read a partial die corresponding to a Fortran module. */
6081
6082 static void
6083 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6084 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6085 {
6086 /* Now scan partial symbols in that module. */
6087
6088 if (pdi->has_children)
6089 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6090 }
6091
6092 /* Read a partial die corresponding to a subprogram and create a partial
6093 symbol for that subprogram. When the CU language allows it, this
6094 routine also defines a partial symbol for each nested subprogram
6095 that this subprogram contains.
6096
6097 DIE my also be a lexical block, in which case we simply search
6098 recursively for suprograms defined inside that lexical block.
6099 Again, this is only performed when the CU language allows this
6100 type of definitions. */
6101
6102 static void
6103 add_partial_subprogram (struct partial_die_info *pdi,
6104 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6105 int need_pc, struct dwarf2_cu *cu)
6106 {
6107 if (pdi->tag == DW_TAG_subprogram)
6108 {
6109 if (pdi->has_pc_info)
6110 {
6111 if (pdi->lowpc < *lowpc)
6112 *lowpc = pdi->lowpc;
6113 if (pdi->highpc > *highpc)
6114 *highpc = pdi->highpc;
6115 if (need_pc)
6116 {
6117 CORE_ADDR baseaddr;
6118 struct objfile *objfile = cu->objfile;
6119
6120 baseaddr = ANOFFSET (objfile->section_offsets,
6121 SECT_OFF_TEXT (objfile));
6122 addrmap_set_empty (objfile->psymtabs_addrmap,
6123 pdi->lowpc + baseaddr,
6124 pdi->highpc - 1 + baseaddr,
6125 cu->per_cu->v.psymtab);
6126 }
6127 }
6128
6129 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6130 {
6131 if (!pdi->is_declaration)
6132 /* Ignore subprogram DIEs that do not have a name, they are
6133 illegal. Do not emit a complaint at this point, we will
6134 do so when we convert this psymtab into a symtab. */
6135 if (pdi->name)
6136 add_partial_symbol (pdi, cu);
6137 }
6138 }
6139
6140 if (! pdi->has_children)
6141 return;
6142
6143 if (cu->language == language_ada)
6144 {
6145 pdi = pdi->die_child;
6146 while (pdi != NULL)
6147 {
6148 fixup_partial_die (pdi, cu);
6149 if (pdi->tag == DW_TAG_subprogram
6150 || pdi->tag == DW_TAG_lexical_block)
6151 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6152 pdi = pdi->die_sibling;
6153 }
6154 }
6155 }
6156
6157 /* Read a partial die corresponding to an enumeration type. */
6158
6159 static void
6160 add_partial_enumeration (struct partial_die_info *enum_pdi,
6161 struct dwarf2_cu *cu)
6162 {
6163 struct partial_die_info *pdi;
6164
6165 if (enum_pdi->name != NULL)
6166 add_partial_symbol (enum_pdi, cu);
6167
6168 pdi = enum_pdi->die_child;
6169 while (pdi)
6170 {
6171 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6172 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6173 else
6174 add_partial_symbol (pdi, cu);
6175 pdi = pdi->die_sibling;
6176 }
6177 }
6178
6179 /* Return the initial uleb128 in the die at INFO_PTR. */
6180
6181 static unsigned int
6182 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6183 {
6184 unsigned int bytes_read;
6185
6186 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6187 }
6188
6189 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6190 Return the corresponding abbrev, or NULL if the number is zero (indicating
6191 an empty DIE). In either case *BYTES_READ will be set to the length of
6192 the initial number. */
6193
6194 static struct abbrev_info *
6195 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6196 struct dwarf2_cu *cu)
6197 {
6198 bfd *abfd = cu->objfile->obfd;
6199 unsigned int abbrev_number;
6200 struct abbrev_info *abbrev;
6201
6202 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6203
6204 if (abbrev_number == 0)
6205 return NULL;
6206
6207 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6208 if (!abbrev)
6209 {
6210 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6211 abbrev_number, bfd_get_filename (abfd));
6212 }
6213
6214 return abbrev;
6215 }
6216
6217 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6218 Returns a pointer to the end of a series of DIEs, terminated by an empty
6219 DIE. Any children of the skipped DIEs will also be skipped. */
6220
6221 static gdb_byte *
6222 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6223 {
6224 struct dwarf2_cu *cu = reader->cu;
6225 struct abbrev_info *abbrev;
6226 unsigned int bytes_read;
6227
6228 while (1)
6229 {
6230 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6231 if (abbrev == NULL)
6232 return info_ptr + bytes_read;
6233 else
6234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6235 }
6236 }
6237
6238 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6239 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6240 abbrev corresponding to that skipped uleb128 should be passed in
6241 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6242 children. */
6243
6244 static gdb_byte *
6245 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6246 struct abbrev_info *abbrev)
6247 {
6248 unsigned int bytes_read;
6249 struct attribute attr;
6250 bfd *abfd = reader->abfd;
6251 struct dwarf2_cu *cu = reader->cu;
6252 gdb_byte *buffer = reader->buffer;
6253 const gdb_byte *buffer_end = reader->buffer_end;
6254 gdb_byte *start_info_ptr = info_ptr;
6255 unsigned int form, i;
6256
6257 for (i = 0; i < abbrev->num_attrs; i++)
6258 {
6259 /* The only abbrev we care about is DW_AT_sibling. */
6260 if (abbrev->attrs[i].name == DW_AT_sibling)
6261 {
6262 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6263 if (attr.form == DW_FORM_ref_addr)
6264 complaint (&symfile_complaints,
6265 _("ignoring absolute DW_AT_sibling"));
6266 else
6267 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6268 }
6269
6270 /* If it isn't DW_AT_sibling, skip this attribute. */
6271 form = abbrev->attrs[i].form;
6272 skip_attribute:
6273 switch (form)
6274 {
6275 case DW_FORM_ref_addr:
6276 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6277 and later it is offset sized. */
6278 if (cu->header.version == 2)
6279 info_ptr += cu->header.addr_size;
6280 else
6281 info_ptr += cu->header.offset_size;
6282 break;
6283 case DW_FORM_GNU_ref_alt:
6284 info_ptr += cu->header.offset_size;
6285 break;
6286 case DW_FORM_addr:
6287 info_ptr += cu->header.addr_size;
6288 break;
6289 case DW_FORM_data1:
6290 case DW_FORM_ref1:
6291 case DW_FORM_flag:
6292 info_ptr += 1;
6293 break;
6294 case DW_FORM_flag_present:
6295 break;
6296 case DW_FORM_data2:
6297 case DW_FORM_ref2:
6298 info_ptr += 2;
6299 break;
6300 case DW_FORM_data4:
6301 case DW_FORM_ref4:
6302 info_ptr += 4;
6303 break;
6304 case DW_FORM_data8:
6305 case DW_FORM_ref8:
6306 case DW_FORM_ref_sig8:
6307 info_ptr += 8;
6308 break;
6309 case DW_FORM_string:
6310 read_direct_string (abfd, info_ptr, &bytes_read);
6311 info_ptr += bytes_read;
6312 break;
6313 case DW_FORM_sec_offset:
6314 case DW_FORM_strp:
6315 case DW_FORM_GNU_strp_alt:
6316 info_ptr += cu->header.offset_size;
6317 break;
6318 case DW_FORM_exprloc:
6319 case DW_FORM_block:
6320 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6321 info_ptr += bytes_read;
6322 break;
6323 case DW_FORM_block1:
6324 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6325 break;
6326 case DW_FORM_block2:
6327 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6328 break;
6329 case DW_FORM_block4:
6330 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6331 break;
6332 case DW_FORM_sdata:
6333 case DW_FORM_udata:
6334 case DW_FORM_ref_udata:
6335 case DW_FORM_GNU_addr_index:
6336 case DW_FORM_GNU_str_index:
6337 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6338 break;
6339 case DW_FORM_indirect:
6340 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6341 info_ptr += bytes_read;
6342 /* We need to continue parsing from here, so just go back to
6343 the top. */
6344 goto skip_attribute;
6345
6346 default:
6347 error (_("Dwarf Error: Cannot handle %s "
6348 "in DWARF reader [in module %s]"),
6349 dwarf_form_name (form),
6350 bfd_get_filename (abfd));
6351 }
6352 }
6353
6354 if (abbrev->has_children)
6355 return skip_children (reader, info_ptr);
6356 else
6357 return info_ptr;
6358 }
6359
6360 /* Locate ORIG_PDI's sibling.
6361 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6362
6363 static gdb_byte *
6364 locate_pdi_sibling (const struct die_reader_specs *reader,
6365 struct partial_die_info *orig_pdi,
6366 gdb_byte *info_ptr)
6367 {
6368 /* Do we know the sibling already? */
6369
6370 if (orig_pdi->sibling)
6371 return orig_pdi->sibling;
6372
6373 /* Are there any children to deal with? */
6374
6375 if (!orig_pdi->has_children)
6376 return info_ptr;
6377
6378 /* Skip the children the long way. */
6379
6380 return skip_children (reader, info_ptr);
6381 }
6382
6383 /* Expand this partial symbol table into a full symbol table. SELF is
6384 not NULL. */
6385
6386 static void
6387 dwarf2_read_symtab (struct partial_symtab *self,
6388 struct objfile *objfile)
6389 {
6390 if (self->readin)
6391 {
6392 warning (_("bug: psymtab for %s is already read in."),
6393 self->filename);
6394 }
6395 else
6396 {
6397 if (info_verbose)
6398 {
6399 printf_filtered (_("Reading in symbols for %s..."),
6400 self->filename);
6401 gdb_flush (gdb_stdout);
6402 }
6403
6404 /* Restore our global data. */
6405 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6406
6407 /* If this psymtab is constructed from a debug-only objfile, the
6408 has_section_at_zero flag will not necessarily be correct. We
6409 can get the correct value for this flag by looking at the data
6410 associated with the (presumably stripped) associated objfile. */
6411 if (objfile->separate_debug_objfile_backlink)
6412 {
6413 struct dwarf2_per_objfile *dpo_backlink
6414 = objfile_data (objfile->separate_debug_objfile_backlink,
6415 dwarf2_objfile_data_key);
6416
6417 dwarf2_per_objfile->has_section_at_zero
6418 = dpo_backlink->has_section_at_zero;
6419 }
6420
6421 dwarf2_per_objfile->reading_partial_symbols = 0;
6422
6423 psymtab_to_symtab_1 (self);
6424
6425 /* Finish up the debug error message. */
6426 if (info_verbose)
6427 printf_filtered (_("done.\n"));
6428 }
6429
6430 process_cu_includes ();
6431 }
6432 \f
6433 /* Reading in full CUs. */
6434
6435 /* Add PER_CU to the queue. */
6436
6437 static void
6438 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6439 enum language pretend_language)
6440 {
6441 struct dwarf2_queue_item *item;
6442
6443 per_cu->queued = 1;
6444 item = xmalloc (sizeof (*item));
6445 item->per_cu = per_cu;
6446 item->pretend_language = pretend_language;
6447 item->next = NULL;
6448
6449 if (dwarf2_queue == NULL)
6450 dwarf2_queue = item;
6451 else
6452 dwarf2_queue_tail->next = item;
6453
6454 dwarf2_queue_tail = item;
6455 }
6456
6457 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6458 unit and add it to our queue.
6459 The result is non-zero if PER_CU was queued, otherwise the result is zero
6460 meaning either PER_CU is already queued or it is already loaded. */
6461
6462 static int
6463 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6464 struct dwarf2_per_cu_data *per_cu,
6465 enum language pretend_language)
6466 {
6467 /* We may arrive here during partial symbol reading, if we need full
6468 DIEs to process an unusual case (e.g. template arguments). Do
6469 not queue PER_CU, just tell our caller to load its DIEs. */
6470 if (dwarf2_per_objfile->reading_partial_symbols)
6471 {
6472 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6473 return 1;
6474 return 0;
6475 }
6476
6477 /* Mark the dependence relation so that we don't flush PER_CU
6478 too early. */
6479 dwarf2_add_dependence (this_cu, per_cu);
6480
6481 /* If it's already on the queue, we have nothing to do. */
6482 if (per_cu->queued)
6483 return 0;
6484
6485 /* If the compilation unit is already loaded, just mark it as
6486 used. */
6487 if (per_cu->cu != NULL)
6488 {
6489 per_cu->cu->last_used = 0;
6490 return 0;
6491 }
6492
6493 /* Add it to the queue. */
6494 queue_comp_unit (per_cu, pretend_language);
6495
6496 return 1;
6497 }
6498
6499 /* Process the queue. */
6500
6501 static void
6502 process_queue (void)
6503 {
6504 struct dwarf2_queue_item *item, *next_item;
6505
6506 if (dwarf2_read_debug)
6507 {
6508 fprintf_unfiltered (gdb_stdlog,
6509 "Expanding one or more symtabs of objfile %s ...\n",
6510 dwarf2_per_objfile->objfile->name);
6511 }
6512
6513 /* The queue starts out with one item, but following a DIE reference
6514 may load a new CU, adding it to the end of the queue. */
6515 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6516 {
6517 if (dwarf2_per_objfile->using_index
6518 ? !item->per_cu->v.quick->symtab
6519 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6520 {
6521 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6522
6523 if (dwarf2_read_debug)
6524 {
6525 fprintf_unfiltered (gdb_stdlog,
6526 "Expanding symtab of %s at offset 0x%x\n",
6527 per_cu->is_debug_types ? "TU" : "CU",
6528 per_cu->offset.sect_off);
6529 }
6530
6531 if (per_cu->is_debug_types)
6532 process_full_type_unit (per_cu, item->pretend_language);
6533 else
6534 process_full_comp_unit (per_cu, item->pretend_language);
6535
6536 if (dwarf2_read_debug)
6537 {
6538 fprintf_unfiltered (gdb_stdlog,
6539 "Done expanding %s at offset 0x%x\n",
6540 per_cu->is_debug_types ? "TU" : "CU",
6541 per_cu->offset.sect_off);
6542 }
6543 }
6544
6545 item->per_cu->queued = 0;
6546 next_item = item->next;
6547 xfree (item);
6548 }
6549
6550 dwarf2_queue_tail = NULL;
6551
6552 if (dwarf2_read_debug)
6553 {
6554 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6555 dwarf2_per_objfile->objfile->name);
6556 }
6557 }
6558
6559 /* Free all allocated queue entries. This function only releases anything if
6560 an error was thrown; if the queue was processed then it would have been
6561 freed as we went along. */
6562
6563 static void
6564 dwarf2_release_queue (void *dummy)
6565 {
6566 struct dwarf2_queue_item *item, *last;
6567
6568 item = dwarf2_queue;
6569 while (item)
6570 {
6571 /* Anything still marked queued is likely to be in an
6572 inconsistent state, so discard it. */
6573 if (item->per_cu->queued)
6574 {
6575 if (item->per_cu->cu != NULL)
6576 free_one_cached_comp_unit (item->per_cu);
6577 item->per_cu->queued = 0;
6578 }
6579
6580 last = item;
6581 item = item->next;
6582 xfree (last);
6583 }
6584
6585 dwarf2_queue = dwarf2_queue_tail = NULL;
6586 }
6587
6588 /* Read in full symbols for PST, and anything it depends on. */
6589
6590 static void
6591 psymtab_to_symtab_1 (struct partial_symtab *pst)
6592 {
6593 struct dwarf2_per_cu_data *per_cu;
6594 int i;
6595
6596 if (pst->readin)
6597 return;
6598
6599 for (i = 0; i < pst->number_of_dependencies; i++)
6600 if (!pst->dependencies[i]->readin
6601 && pst->dependencies[i]->user == NULL)
6602 {
6603 /* Inform about additional files that need to be read in. */
6604 if (info_verbose)
6605 {
6606 /* FIXME: i18n: Need to make this a single string. */
6607 fputs_filtered (" ", gdb_stdout);
6608 wrap_here ("");
6609 fputs_filtered ("and ", gdb_stdout);
6610 wrap_here ("");
6611 printf_filtered ("%s...", pst->dependencies[i]->filename);
6612 wrap_here (""); /* Flush output. */
6613 gdb_flush (gdb_stdout);
6614 }
6615 psymtab_to_symtab_1 (pst->dependencies[i]);
6616 }
6617
6618 per_cu = pst->read_symtab_private;
6619
6620 if (per_cu == NULL)
6621 {
6622 /* It's an include file, no symbols to read for it.
6623 Everything is in the parent symtab. */
6624 pst->readin = 1;
6625 return;
6626 }
6627
6628 dw2_do_instantiate_symtab (per_cu);
6629 }
6630
6631 /* Trivial hash function for die_info: the hash value of a DIE
6632 is its offset in .debug_info for this objfile. */
6633
6634 static hashval_t
6635 die_hash (const void *item)
6636 {
6637 const struct die_info *die = item;
6638
6639 return die->offset.sect_off;
6640 }
6641
6642 /* Trivial comparison function for die_info structures: two DIEs
6643 are equal if they have the same offset. */
6644
6645 static int
6646 die_eq (const void *item_lhs, const void *item_rhs)
6647 {
6648 const struct die_info *die_lhs = item_lhs;
6649 const struct die_info *die_rhs = item_rhs;
6650
6651 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6652 }
6653
6654 /* die_reader_func for load_full_comp_unit.
6655 This is identical to read_signatured_type_reader,
6656 but is kept separate for now. */
6657
6658 static void
6659 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6660 gdb_byte *info_ptr,
6661 struct die_info *comp_unit_die,
6662 int has_children,
6663 void *data)
6664 {
6665 struct dwarf2_cu *cu = reader->cu;
6666 enum language *language_ptr = data;
6667
6668 gdb_assert (cu->die_hash == NULL);
6669 cu->die_hash =
6670 htab_create_alloc_ex (cu->header.length / 12,
6671 die_hash,
6672 die_eq,
6673 NULL,
6674 &cu->comp_unit_obstack,
6675 hashtab_obstack_allocate,
6676 dummy_obstack_deallocate);
6677
6678 if (has_children)
6679 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6680 &info_ptr, comp_unit_die);
6681 cu->dies = comp_unit_die;
6682 /* comp_unit_die is not stored in die_hash, no need. */
6683
6684 /* We try not to read any attributes in this function, because not
6685 all CUs needed for references have been loaded yet, and symbol
6686 table processing isn't initialized. But we have to set the CU language,
6687 or we won't be able to build types correctly.
6688 Similarly, if we do not read the producer, we can not apply
6689 producer-specific interpretation. */
6690 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6691 }
6692
6693 /* Load the DIEs associated with PER_CU into memory. */
6694
6695 static void
6696 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6697 enum language pretend_language)
6698 {
6699 gdb_assert (! this_cu->is_debug_types);
6700
6701 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6702 load_full_comp_unit_reader, &pretend_language);
6703 }
6704
6705 /* Add a DIE to the delayed physname list. */
6706
6707 static void
6708 add_to_method_list (struct type *type, int fnfield_index, int index,
6709 const char *name, struct die_info *die,
6710 struct dwarf2_cu *cu)
6711 {
6712 struct delayed_method_info mi;
6713 mi.type = type;
6714 mi.fnfield_index = fnfield_index;
6715 mi.index = index;
6716 mi.name = name;
6717 mi.die = die;
6718 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6719 }
6720
6721 /* A cleanup for freeing the delayed method list. */
6722
6723 static void
6724 free_delayed_list (void *ptr)
6725 {
6726 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6727 if (cu->method_list != NULL)
6728 {
6729 VEC_free (delayed_method_info, cu->method_list);
6730 cu->method_list = NULL;
6731 }
6732 }
6733
6734 /* Compute the physnames of any methods on the CU's method list.
6735
6736 The computation of method physnames is delayed in order to avoid the
6737 (bad) condition that one of the method's formal parameters is of an as yet
6738 incomplete type. */
6739
6740 static void
6741 compute_delayed_physnames (struct dwarf2_cu *cu)
6742 {
6743 int i;
6744 struct delayed_method_info *mi;
6745 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6746 {
6747 const char *physname;
6748 struct fn_fieldlist *fn_flp
6749 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6750 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6751 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6752 }
6753 }
6754
6755 /* Go objects should be embedded in a DW_TAG_module DIE,
6756 and it's not clear if/how imported objects will appear.
6757 To keep Go support simple until that's worked out,
6758 go back through what we've read and create something usable.
6759 We could do this while processing each DIE, and feels kinda cleaner,
6760 but that way is more invasive.
6761 This is to, for example, allow the user to type "p var" or "b main"
6762 without having to specify the package name, and allow lookups
6763 of module.object to work in contexts that use the expression
6764 parser. */
6765
6766 static void
6767 fixup_go_packaging (struct dwarf2_cu *cu)
6768 {
6769 char *package_name = NULL;
6770 struct pending *list;
6771 int i;
6772
6773 for (list = global_symbols; list != NULL; list = list->next)
6774 {
6775 for (i = 0; i < list->nsyms; ++i)
6776 {
6777 struct symbol *sym = list->symbol[i];
6778
6779 if (SYMBOL_LANGUAGE (sym) == language_go
6780 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6781 {
6782 char *this_package_name = go_symbol_package_name (sym);
6783
6784 if (this_package_name == NULL)
6785 continue;
6786 if (package_name == NULL)
6787 package_name = this_package_name;
6788 else
6789 {
6790 if (strcmp (package_name, this_package_name) != 0)
6791 complaint (&symfile_complaints,
6792 _("Symtab %s has objects from two different Go packages: %s and %s"),
6793 (SYMBOL_SYMTAB (sym)
6794 ? SYMBOL_SYMTAB (sym)->filename
6795 : cu->objfile->name),
6796 this_package_name, package_name);
6797 xfree (this_package_name);
6798 }
6799 }
6800 }
6801 }
6802
6803 if (package_name != NULL)
6804 {
6805 struct objfile *objfile = cu->objfile;
6806 const char *saved_package_name = obsavestring (package_name,
6807 strlen (package_name),
6808 &objfile->objfile_obstack);
6809 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6810 saved_package_name, objfile);
6811 struct symbol *sym;
6812
6813 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6814
6815 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6816 SYMBOL_SET_LANGUAGE (sym, language_go);
6817 SYMBOL_SET_NAMES (sym, saved_package_name,
6818 strlen (saved_package_name), 0, objfile);
6819 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6820 e.g., "main" finds the "main" module and not C's main(). */
6821 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6822 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6823 SYMBOL_TYPE (sym) = type;
6824
6825 add_symbol_to_list (sym, &global_symbols);
6826
6827 xfree (package_name);
6828 }
6829 }
6830
6831 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6832
6833 /* Return the symtab for PER_CU. This works properly regardless of
6834 whether we're using the index or psymtabs. */
6835
6836 static struct symtab *
6837 get_symtab (struct dwarf2_per_cu_data *per_cu)
6838 {
6839 return (dwarf2_per_objfile->using_index
6840 ? per_cu->v.quick->symtab
6841 : per_cu->v.psymtab->symtab);
6842 }
6843
6844 /* A helper function for computing the list of all symbol tables
6845 included by PER_CU. */
6846
6847 static void
6848 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6849 htab_t all_children,
6850 struct dwarf2_per_cu_data *per_cu)
6851 {
6852 void **slot;
6853 int ix;
6854 struct dwarf2_per_cu_data *iter;
6855
6856 slot = htab_find_slot (all_children, per_cu, INSERT);
6857 if (*slot != NULL)
6858 {
6859 /* This inclusion and its children have been processed. */
6860 return;
6861 }
6862
6863 *slot = per_cu;
6864 /* Only add a CU if it has a symbol table. */
6865 if (get_symtab (per_cu) != NULL)
6866 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6867
6868 for (ix = 0;
6869 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6870 ++ix)
6871 recursively_compute_inclusions (result, all_children, iter);
6872 }
6873
6874 /* Compute the symtab 'includes' fields for the symtab related to
6875 PER_CU. */
6876
6877 static void
6878 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6879 {
6880 gdb_assert (! per_cu->is_debug_types);
6881
6882 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6883 {
6884 int ix, len;
6885 struct dwarf2_per_cu_data *iter;
6886 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6887 htab_t all_children;
6888 struct symtab *symtab = get_symtab (per_cu);
6889
6890 /* If we don't have a symtab, we can just skip this case. */
6891 if (symtab == NULL)
6892 return;
6893
6894 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6895 NULL, xcalloc, xfree);
6896
6897 for (ix = 0;
6898 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6899 ix, iter);
6900 ++ix)
6901 recursively_compute_inclusions (&result_children, all_children, iter);
6902
6903 /* Now we have a transitive closure of all the included CUs, so
6904 we can convert it to a list of symtabs. */
6905 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6906 symtab->includes
6907 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6908 (len + 1) * sizeof (struct symtab *));
6909 for (ix = 0;
6910 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6911 ++ix)
6912 symtab->includes[ix] = get_symtab (iter);
6913 symtab->includes[len] = NULL;
6914
6915 VEC_free (dwarf2_per_cu_ptr, result_children);
6916 htab_delete (all_children);
6917 }
6918 }
6919
6920 /* Compute the 'includes' field for the symtabs of all the CUs we just
6921 read. */
6922
6923 static void
6924 process_cu_includes (void)
6925 {
6926 int ix;
6927 struct dwarf2_per_cu_data *iter;
6928
6929 for (ix = 0;
6930 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6931 ix, iter);
6932 ++ix)
6933 {
6934 if (! iter->is_debug_types)
6935 compute_symtab_includes (iter);
6936 }
6937
6938 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6939 }
6940
6941 /* Generate full symbol information for PER_CU, whose DIEs have
6942 already been loaded into memory. */
6943
6944 static void
6945 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6946 enum language pretend_language)
6947 {
6948 struct dwarf2_cu *cu = per_cu->cu;
6949 struct objfile *objfile = per_cu->objfile;
6950 CORE_ADDR lowpc, highpc;
6951 struct symtab *symtab;
6952 struct cleanup *back_to, *delayed_list_cleanup;
6953 CORE_ADDR baseaddr;
6954 struct block *static_block;
6955
6956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6957
6958 buildsym_init ();
6959 back_to = make_cleanup (really_free_pendings, NULL);
6960 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6961
6962 cu->list_in_scope = &file_symbols;
6963
6964 cu->language = pretend_language;
6965 cu->language_defn = language_def (cu->language);
6966
6967 /* Do line number decoding in read_file_scope () */
6968 process_die (cu->dies, cu);
6969
6970 /* For now fudge the Go package. */
6971 if (cu->language == language_go)
6972 fixup_go_packaging (cu);
6973
6974 /* Now that we have processed all the DIEs in the CU, all the types
6975 should be complete, and it should now be safe to compute all of the
6976 physnames. */
6977 compute_delayed_physnames (cu);
6978 do_cleanups (delayed_list_cleanup);
6979
6980 /* Some compilers don't define a DW_AT_high_pc attribute for the
6981 compilation unit. If the DW_AT_high_pc is missing, synthesize
6982 it, by scanning the DIE's below the compilation unit. */
6983 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6984
6985 static_block
6986 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6987 per_cu->s.imported_symtabs != NULL);
6988
6989 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6990 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6991 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6992 addrmap to help ensure it has an accurate map of pc values belonging to
6993 this comp unit. */
6994 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6995
6996 symtab = end_symtab_from_static_block (static_block, objfile,
6997 SECT_OFF_TEXT (objfile), 0);
6998
6999 if (symtab != NULL)
7000 {
7001 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7002
7003 /* Set symtab language to language from DW_AT_language. If the
7004 compilation is from a C file generated by language preprocessors, do
7005 not set the language if it was already deduced by start_subfile. */
7006 if (!(cu->language == language_c && symtab->language != language_c))
7007 symtab->language = cu->language;
7008
7009 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7010 produce DW_AT_location with location lists but it can be possibly
7011 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7012 there were bugs in prologue debug info, fixed later in GCC-4.5
7013 by "unwind info for epilogues" patch (which is not directly related).
7014
7015 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7016 needed, it would be wrong due to missing DW_AT_producer there.
7017
7018 Still one can confuse GDB by using non-standard GCC compilation
7019 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7020 */
7021 if (cu->has_loclist && gcc_4_minor >= 5)
7022 symtab->locations_valid = 1;
7023
7024 if (gcc_4_minor >= 5)
7025 symtab->epilogue_unwind_valid = 1;
7026
7027 symtab->call_site_htab = cu->call_site_htab;
7028 }
7029
7030 if (dwarf2_per_objfile->using_index)
7031 per_cu->v.quick->symtab = symtab;
7032 else
7033 {
7034 struct partial_symtab *pst = per_cu->v.psymtab;
7035 pst->symtab = symtab;
7036 pst->readin = 1;
7037 }
7038
7039 /* Push it for inclusion processing later. */
7040 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7041
7042 do_cleanups (back_to);
7043 }
7044
7045 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7046 already been loaded into memory. */
7047
7048 static void
7049 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7050 enum language pretend_language)
7051 {
7052 struct dwarf2_cu *cu = per_cu->cu;
7053 struct objfile *objfile = per_cu->objfile;
7054 struct symtab *symtab;
7055 struct cleanup *back_to, *delayed_list_cleanup;
7056
7057 buildsym_init ();
7058 back_to = make_cleanup (really_free_pendings, NULL);
7059 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7060
7061 cu->list_in_scope = &file_symbols;
7062
7063 cu->language = pretend_language;
7064 cu->language_defn = language_def (cu->language);
7065
7066 /* The symbol tables are set up in read_type_unit_scope. */
7067 process_die (cu->dies, cu);
7068
7069 /* For now fudge the Go package. */
7070 if (cu->language == language_go)
7071 fixup_go_packaging (cu);
7072
7073 /* Now that we have processed all the DIEs in the CU, all the types
7074 should be complete, and it should now be safe to compute all of the
7075 physnames. */
7076 compute_delayed_physnames (cu);
7077 do_cleanups (delayed_list_cleanup);
7078
7079 /* TUs share symbol tables.
7080 If this is the first TU to use this symtab, complete the construction
7081 of it with end_expandable_symtab. Otherwise, complete the addition of
7082 this TU's symbols to the existing symtab. */
7083 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7084 {
7085 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7086 per_cu->s.type_unit_group->primary_symtab = symtab;
7087
7088 if (symtab != NULL)
7089 {
7090 /* Set symtab language to language from DW_AT_language. If the
7091 compilation is from a C file generated by language preprocessors,
7092 do not set the language if it was already deduced by
7093 start_subfile. */
7094 if (!(cu->language == language_c && symtab->language != language_c))
7095 symtab->language = cu->language;
7096 }
7097 }
7098 else
7099 {
7100 augment_type_symtab (objfile,
7101 per_cu->s.type_unit_group->primary_symtab);
7102 symtab = per_cu->s.type_unit_group->primary_symtab;
7103 }
7104
7105 if (dwarf2_per_objfile->using_index)
7106 per_cu->v.quick->symtab = symtab;
7107 else
7108 {
7109 struct partial_symtab *pst = per_cu->v.psymtab;
7110 pst->symtab = symtab;
7111 pst->readin = 1;
7112 }
7113
7114 do_cleanups (back_to);
7115 }
7116
7117 /* Process an imported unit DIE. */
7118
7119 static void
7120 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7121 {
7122 struct attribute *attr;
7123
7124 /* For now we don't handle imported units in type units. */
7125 if (cu->per_cu->is_debug_types)
7126 {
7127 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7128 " supported in type units [in module %s]"),
7129 cu->objfile->name);
7130 }
7131
7132 attr = dwarf2_attr (die, DW_AT_import, cu);
7133 if (attr != NULL)
7134 {
7135 struct dwarf2_per_cu_data *per_cu;
7136 struct symtab *imported_symtab;
7137 sect_offset offset;
7138 int is_dwz;
7139
7140 offset = dwarf2_get_ref_die_offset (attr);
7141 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7142 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7143
7144 /* Queue the unit, if needed. */
7145 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7146 load_full_comp_unit (per_cu, cu->language);
7147
7148 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7149 per_cu);
7150 }
7151 }
7152
7153 /* Process a die and its children. */
7154
7155 static void
7156 process_die (struct die_info *die, struct dwarf2_cu *cu)
7157 {
7158 switch (die->tag)
7159 {
7160 case DW_TAG_padding:
7161 break;
7162 case DW_TAG_compile_unit:
7163 case DW_TAG_partial_unit:
7164 read_file_scope (die, cu);
7165 break;
7166 case DW_TAG_type_unit:
7167 read_type_unit_scope (die, cu);
7168 break;
7169 case DW_TAG_subprogram:
7170 case DW_TAG_inlined_subroutine:
7171 read_func_scope (die, cu);
7172 break;
7173 case DW_TAG_lexical_block:
7174 case DW_TAG_try_block:
7175 case DW_TAG_catch_block:
7176 read_lexical_block_scope (die, cu);
7177 break;
7178 case DW_TAG_GNU_call_site:
7179 read_call_site_scope (die, cu);
7180 break;
7181 case DW_TAG_class_type:
7182 case DW_TAG_interface_type:
7183 case DW_TAG_structure_type:
7184 case DW_TAG_union_type:
7185 process_structure_scope (die, cu);
7186 break;
7187 case DW_TAG_enumeration_type:
7188 process_enumeration_scope (die, cu);
7189 break;
7190
7191 /* These dies have a type, but processing them does not create
7192 a symbol or recurse to process the children. Therefore we can
7193 read them on-demand through read_type_die. */
7194 case DW_TAG_subroutine_type:
7195 case DW_TAG_set_type:
7196 case DW_TAG_array_type:
7197 case DW_TAG_pointer_type:
7198 case DW_TAG_ptr_to_member_type:
7199 case DW_TAG_reference_type:
7200 case DW_TAG_string_type:
7201 break;
7202
7203 case DW_TAG_base_type:
7204 case DW_TAG_subrange_type:
7205 case DW_TAG_typedef:
7206 /* Add a typedef symbol for the type definition, if it has a
7207 DW_AT_name. */
7208 new_symbol (die, read_type_die (die, cu), cu);
7209 break;
7210 case DW_TAG_common_block:
7211 read_common_block (die, cu);
7212 break;
7213 case DW_TAG_common_inclusion:
7214 break;
7215 case DW_TAG_namespace:
7216 processing_has_namespace_info = 1;
7217 read_namespace (die, cu);
7218 break;
7219 case DW_TAG_module:
7220 processing_has_namespace_info = 1;
7221 read_module (die, cu);
7222 break;
7223 case DW_TAG_imported_declaration:
7224 case DW_TAG_imported_module:
7225 processing_has_namespace_info = 1;
7226 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7227 || cu->language != language_fortran))
7228 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7229 dwarf_tag_name (die->tag));
7230 read_import_statement (die, cu);
7231 break;
7232
7233 case DW_TAG_imported_unit:
7234 process_imported_unit_die (die, cu);
7235 break;
7236
7237 default:
7238 new_symbol (die, NULL, cu);
7239 break;
7240 }
7241 }
7242
7243 /* A helper function for dwarf2_compute_name which determines whether DIE
7244 needs to have the name of the scope prepended to the name listed in the
7245 die. */
7246
7247 static int
7248 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7249 {
7250 struct attribute *attr;
7251
7252 switch (die->tag)
7253 {
7254 case DW_TAG_namespace:
7255 case DW_TAG_typedef:
7256 case DW_TAG_class_type:
7257 case DW_TAG_interface_type:
7258 case DW_TAG_structure_type:
7259 case DW_TAG_union_type:
7260 case DW_TAG_enumeration_type:
7261 case DW_TAG_enumerator:
7262 case DW_TAG_subprogram:
7263 case DW_TAG_member:
7264 return 1;
7265
7266 case DW_TAG_variable:
7267 case DW_TAG_constant:
7268 /* We only need to prefix "globally" visible variables. These include
7269 any variable marked with DW_AT_external or any variable that
7270 lives in a namespace. [Variables in anonymous namespaces
7271 require prefixing, but they are not DW_AT_external.] */
7272
7273 if (dwarf2_attr (die, DW_AT_specification, cu))
7274 {
7275 struct dwarf2_cu *spec_cu = cu;
7276
7277 return die_needs_namespace (die_specification (die, &spec_cu),
7278 spec_cu);
7279 }
7280
7281 attr = dwarf2_attr (die, DW_AT_external, cu);
7282 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7283 && die->parent->tag != DW_TAG_module)
7284 return 0;
7285 /* A variable in a lexical block of some kind does not need a
7286 namespace, even though in C++ such variables may be external
7287 and have a mangled name. */
7288 if (die->parent->tag == DW_TAG_lexical_block
7289 || die->parent->tag == DW_TAG_try_block
7290 || die->parent->tag == DW_TAG_catch_block
7291 || die->parent->tag == DW_TAG_subprogram)
7292 return 0;
7293 return 1;
7294
7295 default:
7296 return 0;
7297 }
7298 }
7299
7300 /* Retrieve the last character from a mem_file. */
7301
7302 static void
7303 do_ui_file_peek_last (void *object, const char *buffer, long length)
7304 {
7305 char *last_char_p = (char *) object;
7306
7307 if (length > 0)
7308 *last_char_p = buffer[length - 1];
7309 }
7310
7311 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7312 compute the physname for the object, which include a method's:
7313 - formal parameters (C++/Java),
7314 - receiver type (Go),
7315 - return type (Java).
7316
7317 The term "physname" is a bit confusing.
7318 For C++, for example, it is the demangled name.
7319 For Go, for example, it's the mangled name.
7320
7321 For Ada, return the DIE's linkage name rather than the fully qualified
7322 name. PHYSNAME is ignored..
7323
7324 The result is allocated on the objfile_obstack and canonicalized. */
7325
7326 static const char *
7327 dwarf2_compute_name (const char *name,
7328 struct die_info *die, struct dwarf2_cu *cu,
7329 int physname)
7330 {
7331 struct objfile *objfile = cu->objfile;
7332
7333 if (name == NULL)
7334 name = dwarf2_name (die, cu);
7335
7336 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7337 compute it by typename_concat inside GDB. */
7338 if (cu->language == language_ada
7339 || (cu->language == language_fortran && physname))
7340 {
7341 /* For Ada unit, we prefer the linkage name over the name, as
7342 the former contains the exported name, which the user expects
7343 to be able to reference. Ideally, we want the user to be able
7344 to reference this entity using either natural or linkage name,
7345 but we haven't started looking at this enhancement yet. */
7346 struct attribute *attr;
7347
7348 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7349 if (attr == NULL)
7350 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7351 if (attr && DW_STRING (attr))
7352 return DW_STRING (attr);
7353 }
7354
7355 /* These are the only languages we know how to qualify names in. */
7356 if (name != NULL
7357 && (cu->language == language_cplus || cu->language == language_java
7358 || cu->language == language_fortran))
7359 {
7360 if (die_needs_namespace (die, cu))
7361 {
7362 long length;
7363 const char *prefix;
7364 struct ui_file *buf;
7365
7366 prefix = determine_prefix (die, cu);
7367 buf = mem_fileopen ();
7368 if (*prefix != '\0')
7369 {
7370 char *prefixed_name = typename_concat (NULL, prefix, name,
7371 physname, cu);
7372
7373 fputs_unfiltered (prefixed_name, buf);
7374 xfree (prefixed_name);
7375 }
7376 else
7377 fputs_unfiltered (name, buf);
7378
7379 /* Template parameters may be specified in the DIE's DW_AT_name, or
7380 as children with DW_TAG_template_type_param or
7381 DW_TAG_value_type_param. If the latter, add them to the name
7382 here. If the name already has template parameters, then
7383 skip this step; some versions of GCC emit both, and
7384 it is more efficient to use the pre-computed name.
7385
7386 Something to keep in mind about this process: it is very
7387 unlikely, or in some cases downright impossible, to produce
7388 something that will match the mangled name of a function.
7389 If the definition of the function has the same debug info,
7390 we should be able to match up with it anyway. But fallbacks
7391 using the minimal symbol, for instance to find a method
7392 implemented in a stripped copy of libstdc++, will not work.
7393 If we do not have debug info for the definition, we will have to
7394 match them up some other way.
7395
7396 When we do name matching there is a related problem with function
7397 templates; two instantiated function templates are allowed to
7398 differ only by their return types, which we do not add here. */
7399
7400 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7401 {
7402 struct attribute *attr;
7403 struct die_info *child;
7404 int first = 1;
7405
7406 die->building_fullname = 1;
7407
7408 for (child = die->child; child != NULL; child = child->sibling)
7409 {
7410 struct type *type;
7411 LONGEST value;
7412 gdb_byte *bytes;
7413 struct dwarf2_locexpr_baton *baton;
7414 struct value *v;
7415
7416 if (child->tag != DW_TAG_template_type_param
7417 && child->tag != DW_TAG_template_value_param)
7418 continue;
7419
7420 if (first)
7421 {
7422 fputs_unfiltered ("<", buf);
7423 first = 0;
7424 }
7425 else
7426 fputs_unfiltered (", ", buf);
7427
7428 attr = dwarf2_attr (child, DW_AT_type, cu);
7429 if (attr == NULL)
7430 {
7431 complaint (&symfile_complaints,
7432 _("template parameter missing DW_AT_type"));
7433 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7434 continue;
7435 }
7436 type = die_type (child, cu);
7437
7438 if (child->tag == DW_TAG_template_type_param)
7439 {
7440 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7441 continue;
7442 }
7443
7444 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7445 if (attr == NULL)
7446 {
7447 complaint (&symfile_complaints,
7448 _("template parameter missing "
7449 "DW_AT_const_value"));
7450 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7451 continue;
7452 }
7453
7454 dwarf2_const_value_attr (attr, type, name,
7455 &cu->comp_unit_obstack, cu,
7456 &value, &bytes, &baton);
7457
7458 if (TYPE_NOSIGN (type))
7459 /* GDB prints characters as NUMBER 'CHAR'. If that's
7460 changed, this can use value_print instead. */
7461 c_printchar (value, type, buf);
7462 else
7463 {
7464 struct value_print_options opts;
7465
7466 if (baton != NULL)
7467 v = dwarf2_evaluate_loc_desc (type, NULL,
7468 baton->data,
7469 baton->size,
7470 baton->per_cu);
7471 else if (bytes != NULL)
7472 {
7473 v = allocate_value (type);
7474 memcpy (value_contents_writeable (v), bytes,
7475 TYPE_LENGTH (type));
7476 }
7477 else
7478 v = value_from_longest (type, value);
7479
7480 /* Specify decimal so that we do not depend on
7481 the radix. */
7482 get_formatted_print_options (&opts, 'd');
7483 opts.raw = 1;
7484 value_print (v, buf, &opts);
7485 release_value (v);
7486 value_free (v);
7487 }
7488 }
7489
7490 die->building_fullname = 0;
7491
7492 if (!first)
7493 {
7494 /* Close the argument list, with a space if necessary
7495 (nested templates). */
7496 char last_char = '\0';
7497 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7498 if (last_char == '>')
7499 fputs_unfiltered (" >", buf);
7500 else
7501 fputs_unfiltered (">", buf);
7502 }
7503 }
7504
7505 /* For Java and C++ methods, append formal parameter type
7506 information, if PHYSNAME. */
7507
7508 if (physname && die->tag == DW_TAG_subprogram
7509 && (cu->language == language_cplus
7510 || cu->language == language_java))
7511 {
7512 struct type *type = read_type_die (die, cu);
7513
7514 c_type_print_args (type, buf, 1, cu->language,
7515 &type_print_raw_options);
7516
7517 if (cu->language == language_java)
7518 {
7519 /* For java, we must append the return type to method
7520 names. */
7521 if (die->tag == DW_TAG_subprogram)
7522 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7523 0, 0, &type_print_raw_options);
7524 }
7525 else if (cu->language == language_cplus)
7526 {
7527 /* Assume that an artificial first parameter is
7528 "this", but do not crash if it is not. RealView
7529 marks unnamed (and thus unused) parameters as
7530 artificial; there is no way to differentiate
7531 the two cases. */
7532 if (TYPE_NFIELDS (type) > 0
7533 && TYPE_FIELD_ARTIFICIAL (type, 0)
7534 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7535 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7536 0))))
7537 fputs_unfiltered (" const", buf);
7538 }
7539 }
7540
7541 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7542 &length);
7543 ui_file_delete (buf);
7544
7545 if (cu->language == language_cplus)
7546 {
7547 const char *cname
7548 = dwarf2_canonicalize_name (name, cu,
7549 &objfile->objfile_obstack);
7550
7551 if (cname != NULL)
7552 name = cname;
7553 }
7554 }
7555 }
7556
7557 return name;
7558 }
7559
7560 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7561 If scope qualifiers are appropriate they will be added. The result
7562 will be allocated on the objfile_obstack, or NULL if the DIE does
7563 not have a name. NAME may either be from a previous call to
7564 dwarf2_name or NULL.
7565
7566 The output string will be canonicalized (if C++/Java). */
7567
7568 static const char *
7569 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7570 {
7571 return dwarf2_compute_name (name, die, cu, 0);
7572 }
7573
7574 /* Construct a physname for the given DIE in CU. NAME may either be
7575 from a previous call to dwarf2_name or NULL. The result will be
7576 allocated on the objfile_objstack or NULL if the DIE does not have a
7577 name.
7578
7579 The output string will be canonicalized (if C++/Java). */
7580
7581 static const char *
7582 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7583 {
7584 struct objfile *objfile = cu->objfile;
7585 struct attribute *attr;
7586 const char *retval, *mangled = NULL, *canon = NULL;
7587 struct cleanup *back_to;
7588 int need_copy = 1;
7589
7590 /* In this case dwarf2_compute_name is just a shortcut not building anything
7591 on its own. */
7592 if (!die_needs_namespace (die, cu))
7593 return dwarf2_compute_name (name, die, cu, 1);
7594
7595 back_to = make_cleanup (null_cleanup, NULL);
7596
7597 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7598 if (!attr)
7599 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7600
7601 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7602 has computed. */
7603 if (attr && DW_STRING (attr))
7604 {
7605 char *demangled;
7606
7607 mangled = DW_STRING (attr);
7608
7609 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7610 type. It is easier for GDB users to search for such functions as
7611 `name(params)' than `long name(params)'. In such case the minimal
7612 symbol names do not match the full symbol names but for template
7613 functions there is never a need to look up their definition from their
7614 declaration so the only disadvantage remains the minimal symbol
7615 variant `long name(params)' does not have the proper inferior type.
7616 */
7617
7618 if (cu->language == language_go)
7619 {
7620 /* This is a lie, but we already lie to the caller new_symbol_full.
7621 new_symbol_full assumes we return the mangled name.
7622 This just undoes that lie until things are cleaned up. */
7623 demangled = NULL;
7624 }
7625 else
7626 {
7627 demangled = cplus_demangle (mangled,
7628 (DMGL_PARAMS | DMGL_ANSI
7629 | (cu->language == language_java
7630 ? DMGL_JAVA | DMGL_RET_POSTFIX
7631 : DMGL_RET_DROP)));
7632 }
7633 if (demangled)
7634 {
7635 make_cleanup (xfree, demangled);
7636 canon = demangled;
7637 }
7638 else
7639 {
7640 canon = mangled;
7641 need_copy = 0;
7642 }
7643 }
7644
7645 if (canon == NULL || check_physname)
7646 {
7647 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7648
7649 if (canon != NULL && strcmp (physname, canon) != 0)
7650 {
7651 /* It may not mean a bug in GDB. The compiler could also
7652 compute DW_AT_linkage_name incorrectly. But in such case
7653 GDB would need to be bug-to-bug compatible. */
7654
7655 complaint (&symfile_complaints,
7656 _("Computed physname <%s> does not match demangled <%s> "
7657 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7658 physname, canon, mangled, die->offset.sect_off, objfile->name);
7659
7660 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7661 is available here - over computed PHYSNAME. It is safer
7662 against both buggy GDB and buggy compilers. */
7663
7664 retval = canon;
7665 }
7666 else
7667 {
7668 retval = physname;
7669 need_copy = 0;
7670 }
7671 }
7672 else
7673 retval = canon;
7674
7675 if (need_copy)
7676 retval = obsavestring (retval, strlen (retval),
7677 &objfile->objfile_obstack);
7678
7679 do_cleanups (back_to);
7680 return retval;
7681 }
7682
7683 /* Read the import statement specified by the given die and record it. */
7684
7685 static void
7686 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7687 {
7688 struct objfile *objfile = cu->objfile;
7689 struct attribute *import_attr;
7690 struct die_info *imported_die, *child_die;
7691 struct dwarf2_cu *imported_cu;
7692 const char *imported_name;
7693 const char *imported_name_prefix;
7694 const char *canonical_name;
7695 const char *import_alias;
7696 const char *imported_declaration = NULL;
7697 const char *import_prefix;
7698 VEC (const_char_ptr) *excludes = NULL;
7699 struct cleanup *cleanups;
7700
7701 char *temp;
7702
7703 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7704 if (import_attr == NULL)
7705 {
7706 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7707 dwarf_tag_name (die->tag));
7708 return;
7709 }
7710
7711 imported_cu = cu;
7712 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7713 imported_name = dwarf2_name (imported_die, imported_cu);
7714 if (imported_name == NULL)
7715 {
7716 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7717
7718 The import in the following code:
7719 namespace A
7720 {
7721 typedef int B;
7722 }
7723
7724 int main ()
7725 {
7726 using A::B;
7727 B b;
7728 return b;
7729 }
7730
7731 ...
7732 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7733 <52> DW_AT_decl_file : 1
7734 <53> DW_AT_decl_line : 6
7735 <54> DW_AT_import : <0x75>
7736 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7737 <59> DW_AT_name : B
7738 <5b> DW_AT_decl_file : 1
7739 <5c> DW_AT_decl_line : 2
7740 <5d> DW_AT_type : <0x6e>
7741 ...
7742 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7743 <76> DW_AT_byte_size : 4
7744 <77> DW_AT_encoding : 5 (signed)
7745
7746 imports the wrong die ( 0x75 instead of 0x58 ).
7747 This case will be ignored until the gcc bug is fixed. */
7748 return;
7749 }
7750
7751 /* Figure out the local name after import. */
7752 import_alias = dwarf2_name (die, cu);
7753
7754 /* Figure out where the statement is being imported to. */
7755 import_prefix = determine_prefix (die, cu);
7756
7757 /* Figure out what the scope of the imported die is and prepend it
7758 to the name of the imported die. */
7759 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7760
7761 if (imported_die->tag != DW_TAG_namespace
7762 && imported_die->tag != DW_TAG_module)
7763 {
7764 imported_declaration = imported_name;
7765 canonical_name = imported_name_prefix;
7766 }
7767 else if (strlen (imported_name_prefix) > 0)
7768 {
7769 temp = alloca (strlen (imported_name_prefix)
7770 + 2 + strlen (imported_name) + 1);
7771 strcpy (temp, imported_name_prefix);
7772 strcat (temp, "::");
7773 strcat (temp, imported_name);
7774 canonical_name = temp;
7775 }
7776 else
7777 canonical_name = imported_name;
7778
7779 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7780
7781 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7782 for (child_die = die->child; child_die && child_die->tag;
7783 child_die = sibling_die (child_die))
7784 {
7785 /* DWARF-4: A Fortran use statement with a “rename list” may be
7786 represented by an imported module entry with an import attribute
7787 referring to the module and owned entries corresponding to those
7788 entities that are renamed as part of being imported. */
7789
7790 if (child_die->tag != DW_TAG_imported_declaration)
7791 {
7792 complaint (&symfile_complaints,
7793 _("child DW_TAG_imported_declaration expected "
7794 "- DIE at 0x%x [in module %s]"),
7795 child_die->offset.sect_off, objfile->name);
7796 continue;
7797 }
7798
7799 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7800 if (import_attr == NULL)
7801 {
7802 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7803 dwarf_tag_name (child_die->tag));
7804 continue;
7805 }
7806
7807 imported_cu = cu;
7808 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7809 &imported_cu);
7810 imported_name = dwarf2_name (imported_die, imported_cu);
7811 if (imported_name == NULL)
7812 {
7813 complaint (&symfile_complaints,
7814 _("child DW_TAG_imported_declaration has unknown "
7815 "imported name - DIE at 0x%x [in module %s]"),
7816 child_die->offset.sect_off, objfile->name);
7817 continue;
7818 }
7819
7820 VEC_safe_push (const_char_ptr, excludes, imported_name);
7821
7822 process_die (child_die, cu);
7823 }
7824
7825 cp_add_using_directive (import_prefix,
7826 canonical_name,
7827 import_alias,
7828 imported_declaration,
7829 excludes,
7830 &objfile->objfile_obstack);
7831
7832 do_cleanups (cleanups);
7833 }
7834
7835 /* Cleanup function for handle_DW_AT_stmt_list. */
7836
7837 static void
7838 free_cu_line_header (void *arg)
7839 {
7840 struct dwarf2_cu *cu = arg;
7841
7842 free_line_header (cu->line_header);
7843 cu->line_header = NULL;
7844 }
7845
7846 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7847 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7848 this, it was first present in GCC release 4.3.0. */
7849
7850 static int
7851 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7852 {
7853 if (!cu->checked_producer)
7854 check_producer (cu);
7855
7856 return cu->producer_is_gcc_lt_4_3;
7857 }
7858
7859 static void
7860 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7861 const char **name, const char **comp_dir)
7862 {
7863 struct attribute *attr;
7864
7865 *name = NULL;
7866 *comp_dir = NULL;
7867
7868 /* Find the filename. Do not use dwarf2_name here, since the filename
7869 is not a source language identifier. */
7870 attr = dwarf2_attr (die, DW_AT_name, cu);
7871 if (attr)
7872 {
7873 *name = DW_STRING (attr);
7874 }
7875
7876 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7877 if (attr)
7878 *comp_dir = DW_STRING (attr);
7879 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7880 && IS_ABSOLUTE_PATH (*name))
7881 {
7882 char *d = ldirname (*name);
7883
7884 *comp_dir = d;
7885 if (d != NULL)
7886 make_cleanup (xfree, d);
7887 }
7888 if (*comp_dir != NULL)
7889 {
7890 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7891 directory, get rid of it. */
7892 char *cp = strchr (*comp_dir, ':');
7893
7894 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7895 *comp_dir = cp + 1;
7896 }
7897
7898 if (*name == NULL)
7899 *name = "<unknown>";
7900 }
7901
7902 /* Handle DW_AT_stmt_list for a compilation unit.
7903 DIE is the DW_TAG_compile_unit die for CU.
7904 COMP_DIR is the compilation directory.
7905 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7906
7907 static void
7908 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7909 const char *comp_dir)
7910 {
7911 struct attribute *attr;
7912
7913 gdb_assert (! cu->per_cu->is_debug_types);
7914
7915 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7916 if (attr)
7917 {
7918 unsigned int line_offset = DW_UNSND (attr);
7919 struct line_header *line_header
7920 = dwarf_decode_line_header (line_offset, cu);
7921
7922 if (line_header)
7923 {
7924 cu->line_header = line_header;
7925 make_cleanup (free_cu_line_header, cu);
7926 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7927 }
7928 }
7929 }
7930
7931 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7932
7933 static void
7934 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7935 {
7936 struct objfile *objfile = dwarf2_per_objfile->objfile;
7937 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7938 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7939 CORE_ADDR highpc = ((CORE_ADDR) 0);
7940 struct attribute *attr;
7941 const char *name = NULL;
7942 const char *comp_dir = NULL;
7943 struct die_info *child_die;
7944 bfd *abfd = objfile->obfd;
7945 CORE_ADDR baseaddr;
7946
7947 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7948
7949 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7950
7951 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7952 from finish_block. */
7953 if (lowpc == ((CORE_ADDR) -1))
7954 lowpc = highpc;
7955 lowpc += baseaddr;
7956 highpc += baseaddr;
7957
7958 find_file_and_directory (die, cu, &name, &comp_dir);
7959
7960 prepare_one_comp_unit (cu, die, cu->language);
7961
7962 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7963 standardised yet. As a workaround for the language detection we fall
7964 back to the DW_AT_producer string. */
7965 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7966 cu->language = language_opencl;
7967
7968 /* Similar hack for Go. */
7969 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7970 set_cu_language (DW_LANG_Go, cu);
7971
7972 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7973
7974 /* Decode line number information if present. We do this before
7975 processing child DIEs, so that the line header table is available
7976 for DW_AT_decl_file. */
7977 handle_DW_AT_stmt_list (die, cu, comp_dir);
7978
7979 /* Process all dies in compilation unit. */
7980 if (die->child != NULL)
7981 {
7982 child_die = die->child;
7983 while (child_die && child_die->tag)
7984 {
7985 process_die (child_die, cu);
7986 child_die = sibling_die (child_die);
7987 }
7988 }
7989
7990 /* Decode macro information, if present. Dwarf 2 macro information
7991 refers to information in the line number info statement program
7992 header, so we can only read it if we've read the header
7993 successfully. */
7994 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7995 if (attr && cu->line_header)
7996 {
7997 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7998 complaint (&symfile_complaints,
7999 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8000
8001 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8002 }
8003 else
8004 {
8005 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8006 if (attr && cu->line_header)
8007 {
8008 unsigned int macro_offset = DW_UNSND (attr);
8009
8010 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8011 }
8012 }
8013
8014 do_cleanups (back_to);
8015 }
8016
8017 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8018 Create the set of symtabs used by this TU, or if this TU is sharing
8019 symtabs with another TU and the symtabs have already been created
8020 then restore those symtabs in the line header.
8021 We don't need the pc/line-number mapping for type units. */
8022
8023 static void
8024 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8025 {
8026 struct objfile *objfile = dwarf2_per_objfile->objfile;
8027 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8028 struct type_unit_group *tu_group;
8029 int first_time;
8030 struct line_header *lh;
8031 struct attribute *attr;
8032 unsigned int i, line_offset;
8033
8034 gdb_assert (per_cu->is_debug_types);
8035
8036 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8037
8038 /* If we're using .gdb_index (includes -readnow) then
8039 per_cu->s.type_unit_group may not have been set up yet. */
8040 if (per_cu->s.type_unit_group == NULL)
8041 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
8042 tu_group = per_cu->s.type_unit_group;
8043
8044 /* If we've already processed this stmt_list there's no real need to
8045 do it again, we could fake it and just recreate the part we need
8046 (file name,index -> symtab mapping). If data shows this optimization
8047 is useful we can do it then. */
8048 first_time = tu_group->primary_symtab == NULL;
8049
8050 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8051 debug info. */
8052 lh = NULL;
8053 if (attr != NULL)
8054 {
8055 line_offset = DW_UNSND (attr);
8056 lh = dwarf_decode_line_header (line_offset, cu);
8057 }
8058 if (lh == NULL)
8059 {
8060 if (first_time)
8061 dwarf2_start_symtab (cu, "", NULL, 0);
8062 else
8063 {
8064 gdb_assert (tu_group->symtabs == NULL);
8065 restart_symtab (0);
8066 }
8067 /* Note: The primary symtab will get allocated at the end. */
8068 return;
8069 }
8070
8071 cu->line_header = lh;
8072 make_cleanup (free_cu_line_header, cu);
8073
8074 if (first_time)
8075 {
8076 dwarf2_start_symtab (cu, "", NULL, 0);
8077
8078 tu_group->num_symtabs = lh->num_file_names;
8079 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8080
8081 for (i = 0; i < lh->num_file_names; ++i)
8082 {
8083 char *dir = NULL;
8084 struct file_entry *fe = &lh->file_names[i];
8085
8086 if (fe->dir_index)
8087 dir = lh->include_dirs[fe->dir_index - 1];
8088 dwarf2_start_subfile (fe->name, dir, NULL);
8089
8090 /* Note: We don't have to watch for the main subfile here, type units
8091 don't have DW_AT_name. */
8092
8093 if (current_subfile->symtab == NULL)
8094 {
8095 /* NOTE: start_subfile will recognize when it's been passed
8096 a file it has already seen. So we can't assume there's a
8097 simple mapping from lh->file_names to subfiles,
8098 lh->file_names may contain dups. */
8099 current_subfile->symtab = allocate_symtab (current_subfile->name,
8100 objfile);
8101 }
8102
8103 fe->symtab = current_subfile->symtab;
8104 tu_group->symtabs[i] = fe->symtab;
8105 }
8106 }
8107 else
8108 {
8109 restart_symtab (0);
8110
8111 for (i = 0; i < lh->num_file_names; ++i)
8112 {
8113 struct file_entry *fe = &lh->file_names[i];
8114
8115 fe->symtab = tu_group->symtabs[i];
8116 }
8117 }
8118
8119 /* The main symtab is allocated last. Type units don't have DW_AT_name
8120 so they don't have a "real" (so to speak) symtab anyway.
8121 There is later code that will assign the main symtab to all symbols
8122 that don't have one. We need to handle the case of a symbol with a
8123 missing symtab (DW_AT_decl_file) anyway. */
8124 }
8125
8126 /* Process DW_TAG_type_unit.
8127 For TUs we want to skip the first top level sibling if it's not the
8128 actual type being defined by this TU. In this case the first top
8129 level sibling is there to provide context only. */
8130
8131 static void
8132 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8133 {
8134 struct die_info *child_die;
8135
8136 prepare_one_comp_unit (cu, die, language_minimal);
8137
8138 /* Initialize (or reinitialize) the machinery for building symtabs.
8139 We do this before processing child DIEs, so that the line header table
8140 is available for DW_AT_decl_file. */
8141 setup_type_unit_groups (die, cu);
8142
8143 if (die->child != NULL)
8144 {
8145 child_die = die->child;
8146 while (child_die && child_die->tag)
8147 {
8148 process_die (child_die, cu);
8149 child_die = sibling_die (child_die);
8150 }
8151 }
8152 }
8153 \f
8154 /* DWO/DWP files.
8155
8156 http://gcc.gnu.org/wiki/DebugFission
8157 http://gcc.gnu.org/wiki/DebugFissionDWP
8158
8159 To simplify handling of both DWO files ("object" files with the DWARF info)
8160 and DWP files (a file with the DWOs packaged up into one file), we treat
8161 DWP files as having a collection of virtual DWO files. */
8162
8163 static hashval_t
8164 hash_dwo_file (const void *item)
8165 {
8166 const struct dwo_file *dwo_file = item;
8167
8168 return htab_hash_string (dwo_file->name);
8169 }
8170
8171 static int
8172 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8173 {
8174 const struct dwo_file *lhs = item_lhs;
8175 const struct dwo_file *rhs = item_rhs;
8176
8177 return strcmp (lhs->name, rhs->name) == 0;
8178 }
8179
8180 /* Allocate a hash table for DWO files. */
8181
8182 static htab_t
8183 allocate_dwo_file_hash_table (void)
8184 {
8185 struct objfile *objfile = dwarf2_per_objfile->objfile;
8186
8187 return htab_create_alloc_ex (41,
8188 hash_dwo_file,
8189 eq_dwo_file,
8190 NULL,
8191 &objfile->objfile_obstack,
8192 hashtab_obstack_allocate,
8193 dummy_obstack_deallocate);
8194 }
8195
8196 /* Lookup DWO file DWO_NAME. */
8197
8198 static void **
8199 lookup_dwo_file_slot (const char *dwo_name)
8200 {
8201 struct dwo_file find_entry;
8202 void **slot;
8203
8204 if (dwarf2_per_objfile->dwo_files == NULL)
8205 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8206
8207 memset (&find_entry, 0, sizeof (find_entry));
8208 find_entry.name = dwo_name;
8209 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8210
8211 return slot;
8212 }
8213
8214 static hashval_t
8215 hash_dwo_unit (const void *item)
8216 {
8217 const struct dwo_unit *dwo_unit = item;
8218
8219 /* This drops the top 32 bits of the id, but is ok for a hash. */
8220 return dwo_unit->signature;
8221 }
8222
8223 static int
8224 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8225 {
8226 const struct dwo_unit *lhs = item_lhs;
8227 const struct dwo_unit *rhs = item_rhs;
8228
8229 /* The signature is assumed to be unique within the DWO file.
8230 So while object file CU dwo_id's always have the value zero,
8231 that's OK, assuming each object file DWO file has only one CU,
8232 and that's the rule for now. */
8233 return lhs->signature == rhs->signature;
8234 }
8235
8236 /* Allocate a hash table for DWO CUs,TUs.
8237 There is one of these tables for each of CUs,TUs for each DWO file. */
8238
8239 static htab_t
8240 allocate_dwo_unit_table (struct objfile *objfile)
8241 {
8242 /* Start out with a pretty small number.
8243 Generally DWO files contain only one CU and maybe some TUs. */
8244 return htab_create_alloc_ex (3,
8245 hash_dwo_unit,
8246 eq_dwo_unit,
8247 NULL,
8248 &objfile->objfile_obstack,
8249 hashtab_obstack_allocate,
8250 dummy_obstack_deallocate);
8251 }
8252
8253 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8254
8255 struct create_dwo_info_table_data
8256 {
8257 struct dwo_file *dwo_file;
8258 htab_t cu_htab;
8259 };
8260
8261 /* die_reader_func for create_dwo_debug_info_hash_table. */
8262
8263 static void
8264 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8265 gdb_byte *info_ptr,
8266 struct die_info *comp_unit_die,
8267 int has_children,
8268 void *datap)
8269 {
8270 struct dwarf2_cu *cu = reader->cu;
8271 struct objfile *objfile = dwarf2_per_objfile->objfile;
8272 sect_offset offset = cu->per_cu->offset;
8273 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8274 struct create_dwo_info_table_data *data = datap;
8275 struct dwo_file *dwo_file = data->dwo_file;
8276 htab_t cu_htab = data->cu_htab;
8277 void **slot;
8278 struct attribute *attr;
8279 struct dwo_unit *dwo_unit;
8280
8281 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8282 if (attr == NULL)
8283 {
8284 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8285 " its dwo_id [in module %s]"),
8286 offset.sect_off, dwo_file->name);
8287 return;
8288 }
8289
8290 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8291 dwo_unit->dwo_file = dwo_file;
8292 dwo_unit->signature = DW_UNSND (attr);
8293 dwo_unit->info_or_types_section = section;
8294 dwo_unit->offset = offset;
8295 dwo_unit->length = cu->per_cu->length;
8296
8297 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8298 gdb_assert (slot != NULL);
8299 if (*slot != NULL)
8300 {
8301 const struct dwo_unit *dup_dwo_unit = *slot;
8302
8303 complaint (&symfile_complaints,
8304 _("debug entry at offset 0x%x is duplicate to the entry at"
8305 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8306 offset.sect_off, dup_dwo_unit->offset.sect_off,
8307 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8308 dwo_file->name);
8309 }
8310 else
8311 *slot = dwo_unit;
8312
8313 if (dwarf2_read_debug)
8314 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8315 offset.sect_off,
8316 phex (dwo_unit->signature,
8317 sizeof (dwo_unit->signature)));
8318 }
8319
8320 /* Create a hash table to map DWO IDs to their CU entry in
8321 .debug_info.dwo in DWO_FILE.
8322 Note: This function processes DWO files only, not DWP files. */
8323
8324 static htab_t
8325 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8326 {
8327 struct objfile *objfile = dwarf2_per_objfile->objfile;
8328 struct dwarf2_section_info *section = &dwo_file->sections.info;
8329 bfd *abfd;
8330 htab_t cu_htab;
8331 gdb_byte *info_ptr, *end_ptr;
8332 struct create_dwo_info_table_data create_dwo_info_table_data;
8333
8334 dwarf2_read_section (objfile, section);
8335 info_ptr = section->buffer;
8336
8337 if (info_ptr == NULL)
8338 return NULL;
8339
8340 /* We can't set abfd until now because the section may be empty or
8341 not present, in which case section->asection will be NULL. */
8342 abfd = section->asection->owner;
8343
8344 if (dwarf2_read_debug)
8345 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8346 bfd_get_filename (abfd));
8347
8348 cu_htab = allocate_dwo_unit_table (objfile);
8349
8350 create_dwo_info_table_data.dwo_file = dwo_file;
8351 create_dwo_info_table_data.cu_htab = cu_htab;
8352
8353 end_ptr = info_ptr + section->size;
8354 while (info_ptr < end_ptr)
8355 {
8356 struct dwarf2_per_cu_data per_cu;
8357
8358 memset (&per_cu, 0, sizeof (per_cu));
8359 per_cu.objfile = objfile;
8360 per_cu.is_debug_types = 0;
8361 per_cu.offset.sect_off = info_ptr - section->buffer;
8362 per_cu.info_or_types_section = section;
8363
8364 init_cutu_and_read_dies_no_follow (&per_cu,
8365 &dwo_file->sections.abbrev,
8366 dwo_file,
8367 create_dwo_debug_info_hash_table_reader,
8368 &create_dwo_info_table_data);
8369
8370 info_ptr += per_cu.length;
8371 }
8372
8373 return cu_htab;
8374 }
8375
8376 /* DWP file .debug_{cu,tu}_index section format:
8377 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8378
8379 Both index sections have the same format, and serve to map a 64-bit
8380 signature to a set of section numbers. Each section begins with a header,
8381 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8382 indexes, and a pool of 32-bit section numbers. The index sections will be
8383 aligned at 8-byte boundaries in the file.
8384
8385 The index section header contains two unsigned 32-bit values (using the
8386 byte order of the application binary):
8387
8388 N, the number of compilation units or type units in the index
8389 M, the number of slots in the hash table
8390
8391 (We assume that N and M will not exceed 2^32 - 1.)
8392
8393 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8394
8395 The hash table begins at offset 8 in the section, and consists of an array
8396 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8397 order of the application binary). Unused slots in the hash table are 0.
8398 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8399
8400 The parallel table begins immediately after the hash table
8401 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8402 array of 32-bit indexes (using the byte order of the application binary),
8403 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8404 table contains a 32-bit index into the pool of section numbers. For unused
8405 hash table slots, the corresponding entry in the parallel table will be 0.
8406
8407 Given a 64-bit compilation unit signature or a type signature S, an entry
8408 in the hash table is located as follows:
8409
8410 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8411 the low-order k bits all set to 1.
8412
8413 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8414
8415 3) If the hash table entry at index H matches the signature, use that
8416 entry. If the hash table entry at index H is unused (all zeroes),
8417 terminate the search: the signature is not present in the table.
8418
8419 4) Let H = (H + H') modulo M. Repeat at Step 3.
8420
8421 Because M > N and H' and M are relatively prime, the search is guaranteed
8422 to stop at an unused slot or find the match.
8423
8424 The pool of section numbers begins immediately following the hash table
8425 (at offset 8 + 12 * M from the beginning of the section). The pool of
8426 section numbers consists of an array of 32-bit words (using the byte order
8427 of the application binary). Each item in the array is indexed starting
8428 from 0. The hash table entry provides the index of the first section
8429 number in the set. Additional section numbers in the set follow, and the
8430 set is terminated by a 0 entry (section number 0 is not used in ELF).
8431
8432 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8433 section must be the first entry in the set, and the .debug_abbrev.dwo must
8434 be the second entry. Other members of the set may follow in any order. */
8435
8436 /* Create a hash table to map DWO IDs to their CU/TU entry in
8437 .debug_{info,types}.dwo in DWP_FILE.
8438 Returns NULL if there isn't one.
8439 Note: This function processes DWP files only, not DWO files. */
8440
8441 static struct dwp_hash_table *
8442 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8443 {
8444 struct objfile *objfile = dwarf2_per_objfile->objfile;
8445 bfd *dbfd = dwp_file->dbfd;
8446 char *index_ptr, *index_end;
8447 struct dwarf2_section_info *index;
8448 uint32_t version, nr_units, nr_slots;
8449 struct dwp_hash_table *htab;
8450
8451 if (is_debug_types)
8452 index = &dwp_file->sections.tu_index;
8453 else
8454 index = &dwp_file->sections.cu_index;
8455
8456 if (dwarf2_section_empty_p (index))
8457 return NULL;
8458 dwarf2_read_section (objfile, index);
8459
8460 index_ptr = index->buffer;
8461 index_end = index_ptr + index->size;
8462
8463 version = read_4_bytes (dbfd, index_ptr);
8464 index_ptr += 8; /* Skip the unused word. */
8465 nr_units = read_4_bytes (dbfd, index_ptr);
8466 index_ptr += 4;
8467 nr_slots = read_4_bytes (dbfd, index_ptr);
8468 index_ptr += 4;
8469
8470 if (version != 1)
8471 {
8472 error (_("Dwarf Error: unsupported DWP file version (%u)"
8473 " [in module %s]"),
8474 version, dwp_file->name);
8475 }
8476 if (nr_slots != (nr_slots & -nr_slots))
8477 {
8478 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8479 " is not power of 2 [in module %s]"),
8480 nr_slots, dwp_file->name);
8481 }
8482
8483 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8484 htab->nr_units = nr_units;
8485 htab->nr_slots = nr_slots;
8486 htab->hash_table = index_ptr;
8487 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8488 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8489
8490 return htab;
8491 }
8492
8493 /* Update SECTIONS with the data from SECTP.
8494
8495 This function is like the other "locate" section routines that are
8496 passed to bfd_map_over_sections, but in this context the sections to
8497 read comes from the DWP hash table, not the full ELF section table.
8498
8499 The result is non-zero for success, or zero if an error was found. */
8500
8501 static int
8502 locate_virtual_dwo_sections (asection *sectp,
8503 struct virtual_dwo_sections *sections)
8504 {
8505 const struct dwop_section_names *names = &dwop_section_names;
8506
8507 if (section_is_p (sectp->name, &names->abbrev_dwo))
8508 {
8509 /* There can be only one. */
8510 if (sections->abbrev.asection != NULL)
8511 return 0;
8512 sections->abbrev.asection = sectp;
8513 sections->abbrev.size = bfd_get_section_size (sectp);
8514 }
8515 else if (section_is_p (sectp->name, &names->info_dwo)
8516 || section_is_p (sectp->name, &names->types_dwo))
8517 {
8518 /* There can be only one. */
8519 if (sections->info_or_types.asection != NULL)
8520 return 0;
8521 sections->info_or_types.asection = sectp;
8522 sections->info_or_types.size = bfd_get_section_size (sectp);
8523 }
8524 else if (section_is_p (sectp->name, &names->line_dwo))
8525 {
8526 /* There can be only one. */
8527 if (sections->line.asection != NULL)
8528 return 0;
8529 sections->line.asection = sectp;
8530 sections->line.size = bfd_get_section_size (sectp);
8531 }
8532 else if (section_is_p (sectp->name, &names->loc_dwo))
8533 {
8534 /* There can be only one. */
8535 if (sections->loc.asection != NULL)
8536 return 0;
8537 sections->loc.asection = sectp;
8538 sections->loc.size = bfd_get_section_size (sectp);
8539 }
8540 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8541 {
8542 /* There can be only one. */
8543 if (sections->macinfo.asection != NULL)
8544 return 0;
8545 sections->macinfo.asection = sectp;
8546 sections->macinfo.size = bfd_get_section_size (sectp);
8547 }
8548 else if (section_is_p (sectp->name, &names->macro_dwo))
8549 {
8550 /* There can be only one. */
8551 if (sections->macro.asection != NULL)
8552 return 0;
8553 sections->macro.asection = sectp;
8554 sections->macro.size = bfd_get_section_size (sectp);
8555 }
8556 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8557 {
8558 /* There can be only one. */
8559 if (sections->str_offsets.asection != NULL)
8560 return 0;
8561 sections->str_offsets.asection = sectp;
8562 sections->str_offsets.size = bfd_get_section_size (sectp);
8563 }
8564 else
8565 {
8566 /* No other kind of section is valid. */
8567 return 0;
8568 }
8569
8570 return 1;
8571 }
8572
8573 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8574 HTAB is the hash table from the DWP file.
8575 SECTION_INDEX is the index of the DWO in HTAB. */
8576
8577 static struct dwo_unit *
8578 create_dwo_in_dwp (struct dwp_file *dwp_file,
8579 const struct dwp_hash_table *htab,
8580 uint32_t section_index,
8581 ULONGEST signature, int is_debug_types)
8582 {
8583 struct objfile *objfile = dwarf2_per_objfile->objfile;
8584 bfd *dbfd = dwp_file->dbfd;
8585 const char *kind = is_debug_types ? "TU" : "CU";
8586 struct dwo_file *dwo_file;
8587 struct dwo_unit *dwo_unit;
8588 struct virtual_dwo_sections sections;
8589 void **dwo_file_slot;
8590 char *virtual_dwo_name;
8591 struct dwarf2_section_info *cutu;
8592 struct cleanup *cleanups;
8593 int i;
8594
8595 if (dwarf2_read_debug)
8596 {
8597 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8598 kind,
8599 section_index, phex (signature, sizeof (signature)),
8600 dwp_file->name);
8601 }
8602
8603 /* Fetch the sections of this DWO.
8604 Put a limit on the number of sections we look for so that bad data
8605 doesn't cause us to loop forever. */
8606
8607 #define MAX_NR_DWO_SECTIONS \
8608 (1 /* .debug_info or .debug_types */ \
8609 + 1 /* .debug_abbrev */ \
8610 + 1 /* .debug_line */ \
8611 + 1 /* .debug_loc */ \
8612 + 1 /* .debug_str_offsets */ \
8613 + 1 /* .debug_macro */ \
8614 + 1 /* .debug_macinfo */ \
8615 + 1 /* trailing zero */)
8616
8617 memset (&sections, 0, sizeof (sections));
8618 cleanups = make_cleanup (null_cleanup, 0);
8619
8620 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8621 {
8622 asection *sectp;
8623 uint32_t section_nr =
8624 read_4_bytes (dbfd,
8625 htab->section_pool
8626 + (section_index + i) * sizeof (uint32_t));
8627
8628 if (section_nr == 0)
8629 break;
8630 if (section_nr >= dwp_file->num_sections)
8631 {
8632 error (_("Dwarf Error: bad DWP hash table, section number too large"
8633 " [in module %s]"),
8634 dwp_file->name);
8635 }
8636
8637 sectp = dwp_file->elf_sections[section_nr];
8638 if (! locate_virtual_dwo_sections (sectp, &sections))
8639 {
8640 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8641 " [in module %s]"),
8642 dwp_file->name);
8643 }
8644 }
8645
8646 if (i < 2
8647 || sections.info_or_types.asection == NULL
8648 || sections.abbrev.asection == NULL)
8649 {
8650 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8651 " [in module %s]"),
8652 dwp_file->name);
8653 }
8654 if (i == MAX_NR_DWO_SECTIONS)
8655 {
8656 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8657 " [in module %s]"),
8658 dwp_file->name);
8659 }
8660
8661 /* It's easier for the rest of the code if we fake a struct dwo_file and
8662 have dwo_unit "live" in that. At least for now.
8663
8664 The DWP file can be made up of a random collection of CUs and TUs.
8665 However, for each CU + set of TUs that came from the same original DWO
8666 file, we want to combine them back into a virtual DWO file to save space
8667 (fewer struct dwo_file objects to allocated). Remember that for really
8668 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8669
8670 virtual_dwo_name =
8671 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8672 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8673 sections.line.asection ? sections.line.asection->id : 0,
8674 sections.loc.asection ? sections.loc.asection->id : 0,
8675 (sections.str_offsets.asection
8676 ? sections.str_offsets.asection->id
8677 : 0));
8678 make_cleanup (xfree, virtual_dwo_name);
8679 /* Can we use an existing virtual DWO file? */
8680 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8681 /* Create one if necessary. */
8682 if (*dwo_file_slot == NULL)
8683 {
8684 if (dwarf2_read_debug)
8685 {
8686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8687 virtual_dwo_name);
8688 }
8689 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8690 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8691 virtual_dwo_name,
8692 strlen (virtual_dwo_name));
8693 dwo_file->sections.abbrev = sections.abbrev;
8694 dwo_file->sections.line = sections.line;
8695 dwo_file->sections.loc = sections.loc;
8696 dwo_file->sections.macinfo = sections.macinfo;
8697 dwo_file->sections.macro = sections.macro;
8698 dwo_file->sections.str_offsets = sections.str_offsets;
8699 /* The "str" section is global to the entire DWP file. */
8700 dwo_file->sections.str = dwp_file->sections.str;
8701 /* The info or types section is assigned later to dwo_unit,
8702 there's no need to record it in dwo_file.
8703 Also, we can't simply record type sections in dwo_file because
8704 we record a pointer into the vector in dwo_unit. As we collect more
8705 types we'll grow the vector and eventually have to reallocate space
8706 for it, invalidating all the pointers into the current copy. */
8707 *dwo_file_slot = dwo_file;
8708 }
8709 else
8710 {
8711 if (dwarf2_read_debug)
8712 {
8713 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8714 virtual_dwo_name);
8715 }
8716 dwo_file = *dwo_file_slot;
8717 }
8718 do_cleanups (cleanups);
8719
8720 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8721 dwo_unit->dwo_file = dwo_file;
8722 dwo_unit->signature = signature;
8723 dwo_unit->info_or_types_section =
8724 obstack_alloc (&objfile->objfile_obstack,
8725 sizeof (struct dwarf2_section_info));
8726 *dwo_unit->info_or_types_section = sections.info_or_types;
8727 /* offset, length, type_offset_in_tu are set later. */
8728
8729 return dwo_unit;
8730 }
8731
8732 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8733
8734 static struct dwo_unit *
8735 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8736 const struct dwp_hash_table *htab,
8737 ULONGEST signature, int is_debug_types)
8738 {
8739 bfd *dbfd = dwp_file->dbfd;
8740 uint32_t mask = htab->nr_slots - 1;
8741 uint32_t hash = signature & mask;
8742 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8743 unsigned int i;
8744 void **slot;
8745 struct dwo_unit find_dwo_cu, *dwo_cu;
8746
8747 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8748 find_dwo_cu.signature = signature;
8749 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8750
8751 if (*slot != NULL)
8752 return *slot;
8753
8754 /* Use a for loop so that we don't loop forever on bad debug info. */
8755 for (i = 0; i < htab->nr_slots; ++i)
8756 {
8757 ULONGEST signature_in_table;
8758
8759 signature_in_table =
8760 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8761 if (signature_in_table == signature)
8762 {
8763 uint32_t section_index =
8764 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8765
8766 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8767 signature, is_debug_types);
8768 return *slot;
8769 }
8770 if (signature_in_table == 0)
8771 return NULL;
8772 hash = (hash + hash2) & mask;
8773 }
8774
8775 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8776 " [in module %s]"),
8777 dwp_file->name);
8778 }
8779
8780 /* Subroutine of open_dwop_file to simplify it.
8781 Open the file specified by FILE_NAME and hand it off to BFD for
8782 preliminary analysis. Return a newly initialized bfd *, which
8783 includes a canonicalized copy of FILE_NAME.
8784 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8785 In case of trouble, return NULL.
8786 NOTE: This function is derived from symfile_bfd_open. */
8787
8788 static bfd *
8789 try_open_dwop_file (const char *file_name, int is_dwp)
8790 {
8791 bfd *sym_bfd;
8792 int desc, flags;
8793 char *absolute_name;
8794
8795 flags = OPF_TRY_CWD_FIRST;
8796 if (is_dwp)
8797 flags |= OPF_SEARCH_IN_PATH;
8798 desc = openp (debug_file_directory, flags, file_name,
8799 O_RDONLY | O_BINARY, &absolute_name);
8800 if (desc < 0)
8801 return NULL;
8802
8803 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8804 if (!sym_bfd)
8805 {
8806 xfree (absolute_name);
8807 return NULL;
8808 }
8809 xfree (absolute_name);
8810 bfd_set_cacheable (sym_bfd, 1);
8811
8812 if (!bfd_check_format (sym_bfd, bfd_object))
8813 {
8814 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8815 return NULL;
8816 }
8817
8818 return sym_bfd;
8819 }
8820
8821 /* Try to open DWO/DWP file FILE_NAME.
8822 COMP_DIR is the DW_AT_comp_dir attribute.
8823 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8824 The result is the bfd handle of the file.
8825 If there is a problem finding or opening the file, return NULL.
8826 Upon success, the canonicalized path of the file is stored in the bfd,
8827 same as symfile_bfd_open. */
8828
8829 static bfd *
8830 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8831 {
8832 bfd *abfd;
8833
8834 if (IS_ABSOLUTE_PATH (file_name))
8835 return try_open_dwop_file (file_name, is_dwp);
8836
8837 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8838
8839 if (comp_dir != NULL)
8840 {
8841 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8842
8843 /* NOTE: If comp_dir is a relative path, this will also try the
8844 search path, which seems useful. */
8845 abfd = try_open_dwop_file (path_to_try, is_dwp);
8846 xfree (path_to_try);
8847 if (abfd != NULL)
8848 return abfd;
8849 }
8850
8851 /* That didn't work, try debug-file-directory, which, despite its name,
8852 is a list of paths. */
8853
8854 if (*debug_file_directory == '\0')
8855 return NULL;
8856
8857 return try_open_dwop_file (file_name, is_dwp);
8858 }
8859
8860 /* This function is mapped across the sections and remembers the offset and
8861 size of each of the DWO debugging sections we are interested in. */
8862
8863 static void
8864 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8865 {
8866 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8867 const struct dwop_section_names *names = &dwop_section_names;
8868
8869 if (section_is_p (sectp->name, &names->abbrev_dwo))
8870 {
8871 dwo_sections->abbrev.asection = sectp;
8872 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8873 }
8874 else if (section_is_p (sectp->name, &names->info_dwo))
8875 {
8876 dwo_sections->info.asection = sectp;
8877 dwo_sections->info.size = bfd_get_section_size (sectp);
8878 }
8879 else if (section_is_p (sectp->name, &names->line_dwo))
8880 {
8881 dwo_sections->line.asection = sectp;
8882 dwo_sections->line.size = bfd_get_section_size (sectp);
8883 }
8884 else if (section_is_p (sectp->name, &names->loc_dwo))
8885 {
8886 dwo_sections->loc.asection = sectp;
8887 dwo_sections->loc.size = bfd_get_section_size (sectp);
8888 }
8889 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8890 {
8891 dwo_sections->macinfo.asection = sectp;
8892 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8893 }
8894 else if (section_is_p (sectp->name, &names->macro_dwo))
8895 {
8896 dwo_sections->macro.asection = sectp;
8897 dwo_sections->macro.size = bfd_get_section_size (sectp);
8898 }
8899 else if (section_is_p (sectp->name, &names->str_dwo))
8900 {
8901 dwo_sections->str.asection = sectp;
8902 dwo_sections->str.size = bfd_get_section_size (sectp);
8903 }
8904 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8905 {
8906 dwo_sections->str_offsets.asection = sectp;
8907 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8908 }
8909 else if (section_is_p (sectp->name, &names->types_dwo))
8910 {
8911 struct dwarf2_section_info type_section;
8912
8913 memset (&type_section, 0, sizeof (type_section));
8914 type_section.asection = sectp;
8915 type_section.size = bfd_get_section_size (sectp);
8916 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8917 &type_section);
8918 }
8919 }
8920
8921 /* Initialize the use of the DWO file specified by DWO_NAME.
8922 The result is NULL if DWO_NAME can't be found. */
8923
8924 static struct dwo_file *
8925 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8926 {
8927 struct objfile *objfile = dwarf2_per_objfile->objfile;
8928 struct dwo_file *dwo_file;
8929 bfd *dbfd;
8930 struct cleanup *cleanups;
8931
8932 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8933 if (dbfd == NULL)
8934 {
8935 if (dwarf2_read_debug)
8936 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8937 return NULL;
8938 }
8939 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8940 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8941 dwo_name, strlen (dwo_name));
8942 dwo_file->dbfd = dbfd;
8943
8944 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8945
8946 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8947
8948 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8949
8950 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8951 dwo_file->sections.types);
8952
8953 discard_cleanups (cleanups);
8954
8955 if (dwarf2_read_debug)
8956 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8957
8958 return dwo_file;
8959 }
8960
8961 /* This function is mapped across the sections and remembers the offset and
8962 size of each of the DWP debugging sections we are interested in. */
8963
8964 static void
8965 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8966 {
8967 struct dwp_file *dwp_file = dwp_file_ptr;
8968 const struct dwop_section_names *names = &dwop_section_names;
8969 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8970
8971 /* Record the ELF section number for later lookup: this is what the
8972 .debug_cu_index,.debug_tu_index tables use. */
8973 gdb_assert (elf_section_nr < dwp_file->num_sections);
8974 dwp_file->elf_sections[elf_section_nr] = sectp;
8975
8976 /* Look for specific sections that we need. */
8977 if (section_is_p (sectp->name, &names->str_dwo))
8978 {
8979 dwp_file->sections.str.asection = sectp;
8980 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8981 }
8982 else if (section_is_p (sectp->name, &names->cu_index))
8983 {
8984 dwp_file->sections.cu_index.asection = sectp;
8985 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8986 }
8987 else if (section_is_p (sectp->name, &names->tu_index))
8988 {
8989 dwp_file->sections.tu_index.asection = sectp;
8990 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8991 }
8992 }
8993
8994 /* Hash function for dwp_file loaded CUs/TUs. */
8995
8996 static hashval_t
8997 hash_dwp_loaded_cutus (const void *item)
8998 {
8999 const struct dwo_unit *dwo_unit = item;
9000
9001 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9002 return dwo_unit->signature;
9003 }
9004
9005 /* Equality function for dwp_file loaded CUs/TUs. */
9006
9007 static int
9008 eq_dwp_loaded_cutus (const void *a, const void *b)
9009 {
9010 const struct dwo_unit *dua = a;
9011 const struct dwo_unit *dub = b;
9012
9013 return dua->signature == dub->signature;
9014 }
9015
9016 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9017
9018 static htab_t
9019 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9020 {
9021 return htab_create_alloc_ex (3,
9022 hash_dwp_loaded_cutus,
9023 eq_dwp_loaded_cutus,
9024 NULL,
9025 &objfile->objfile_obstack,
9026 hashtab_obstack_allocate,
9027 dummy_obstack_deallocate);
9028 }
9029
9030 /* Initialize the use of the DWP file for the current objfile.
9031 By convention the name of the DWP file is ${objfile}.dwp.
9032 The result is NULL if it can't be found. */
9033
9034 static struct dwp_file *
9035 open_and_init_dwp_file (const char *comp_dir)
9036 {
9037 struct objfile *objfile = dwarf2_per_objfile->objfile;
9038 struct dwp_file *dwp_file;
9039 char *dwp_name;
9040 bfd *dbfd;
9041 struct cleanup *cleanups;
9042
9043 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9044 cleanups = make_cleanup (xfree, dwp_name);
9045
9046 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9047 if (dbfd == NULL)
9048 {
9049 if (dwarf2_read_debug)
9050 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9051 do_cleanups (cleanups);
9052 return NULL;
9053 }
9054 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9055 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9056 dwp_name, strlen (dwp_name));
9057 dwp_file->dbfd = dbfd;
9058 do_cleanups (cleanups);
9059
9060 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9061
9062 /* +1: section 0 is unused */
9063 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9064 dwp_file->elf_sections =
9065 OBSTACK_CALLOC (&objfile->objfile_obstack,
9066 dwp_file->num_sections, asection *);
9067
9068 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9069
9070 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9071
9072 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9073
9074 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9075
9076 discard_cleanups (cleanups);
9077
9078 if (dwarf2_read_debug)
9079 {
9080 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9081 fprintf_unfiltered (gdb_stdlog,
9082 " %u CUs, %u TUs\n",
9083 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9084 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9085 }
9086
9087 return dwp_file;
9088 }
9089
9090 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9091 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9092 or in the DWP file for the objfile, referenced by THIS_UNIT.
9093 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9094 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9095
9096 This is called, for example, when wanting to read a variable with a
9097 complex location. Therefore we don't want to do file i/o for every call.
9098 Therefore we don't want to look for a DWO file on every call.
9099 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9100 then we check if we've already seen DWO_NAME, and only THEN do we check
9101 for a DWO file.
9102
9103 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9104 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9105
9106 static struct dwo_unit *
9107 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9108 const char *dwo_name, const char *comp_dir,
9109 ULONGEST signature, int is_debug_types)
9110 {
9111 struct objfile *objfile = dwarf2_per_objfile->objfile;
9112 const char *kind = is_debug_types ? "TU" : "CU";
9113 void **dwo_file_slot;
9114 struct dwo_file *dwo_file;
9115 struct dwp_file *dwp_file;
9116
9117 /* Have we already read SIGNATURE from a DWP file? */
9118
9119 if (! dwarf2_per_objfile->dwp_checked)
9120 {
9121 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9122 dwarf2_per_objfile->dwp_checked = 1;
9123 }
9124 dwp_file = dwarf2_per_objfile->dwp_file;
9125
9126 if (dwp_file != NULL)
9127 {
9128 const struct dwp_hash_table *dwp_htab =
9129 is_debug_types ? dwp_file->tus : dwp_file->cus;
9130
9131 if (dwp_htab != NULL)
9132 {
9133 struct dwo_unit *dwo_cutu =
9134 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9135
9136 if (dwo_cutu != NULL)
9137 {
9138 if (dwarf2_read_debug)
9139 {
9140 fprintf_unfiltered (gdb_stdlog,
9141 "Virtual DWO %s %s found: @%s\n",
9142 kind, hex_string (signature),
9143 host_address_to_string (dwo_cutu));
9144 }
9145 return dwo_cutu;
9146 }
9147 }
9148 }
9149
9150 /* Have we already seen DWO_NAME? */
9151
9152 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9153 if (*dwo_file_slot == NULL)
9154 {
9155 /* Read in the file and build a table of the DWOs it contains. */
9156 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9157 }
9158 /* NOTE: This will be NULL if unable to open the file. */
9159 dwo_file = *dwo_file_slot;
9160
9161 if (dwo_file != NULL)
9162 {
9163 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9164
9165 if (htab != NULL)
9166 {
9167 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9168
9169 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9170 find_dwo_cutu.signature = signature;
9171 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9172
9173 if (dwo_cutu != NULL)
9174 {
9175 if (dwarf2_read_debug)
9176 {
9177 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9178 kind, dwo_name, hex_string (signature),
9179 host_address_to_string (dwo_cutu));
9180 }
9181 return dwo_cutu;
9182 }
9183 }
9184 }
9185
9186 /* We didn't find it. This could mean a dwo_id mismatch, or
9187 someone deleted the DWO/DWP file, or the search path isn't set up
9188 correctly to find the file. */
9189
9190 if (dwarf2_read_debug)
9191 {
9192 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9193 kind, dwo_name, hex_string (signature));
9194 }
9195
9196 complaint (&symfile_complaints,
9197 _("Could not find DWO CU referenced by CU at offset 0x%x"
9198 " [in module %s]"),
9199 this_unit->offset.sect_off, objfile->name);
9200 return NULL;
9201 }
9202
9203 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9204 See lookup_dwo_cutu_unit for details. */
9205
9206 static struct dwo_unit *
9207 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9208 const char *dwo_name, const char *comp_dir,
9209 ULONGEST signature)
9210 {
9211 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9212 }
9213
9214 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9215 See lookup_dwo_cutu_unit for details. */
9216
9217 static struct dwo_unit *
9218 lookup_dwo_type_unit (struct signatured_type *this_tu,
9219 const char *dwo_name, const char *comp_dir)
9220 {
9221 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9222 }
9223
9224 /* Free all resources associated with DWO_FILE.
9225 Close the DWO file and munmap the sections.
9226 All memory should be on the objfile obstack. */
9227
9228 static void
9229 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9230 {
9231 int ix;
9232 struct dwarf2_section_info *section;
9233
9234 gdb_assert (dwo_file->dbfd != objfile->obfd);
9235 gdb_bfd_unref (dwo_file->dbfd);
9236
9237 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9238 }
9239
9240 /* Wrapper for free_dwo_file for use in cleanups. */
9241
9242 static void
9243 free_dwo_file_cleanup (void *arg)
9244 {
9245 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9246 struct objfile *objfile = dwarf2_per_objfile->objfile;
9247
9248 free_dwo_file (dwo_file, objfile);
9249 }
9250
9251 /* Traversal function for free_dwo_files. */
9252
9253 static int
9254 free_dwo_file_from_slot (void **slot, void *info)
9255 {
9256 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9257 struct objfile *objfile = (struct objfile *) info;
9258
9259 free_dwo_file (dwo_file, objfile);
9260
9261 return 1;
9262 }
9263
9264 /* Free all resources associated with DWO_FILES. */
9265
9266 static void
9267 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9268 {
9269 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9270 }
9271 \f
9272 /* Read in various DIEs. */
9273
9274 /* qsort helper for inherit_abstract_dies. */
9275
9276 static int
9277 unsigned_int_compar (const void *ap, const void *bp)
9278 {
9279 unsigned int a = *(unsigned int *) ap;
9280 unsigned int b = *(unsigned int *) bp;
9281
9282 return (a > b) - (b > a);
9283 }
9284
9285 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9286 Inherit only the children of the DW_AT_abstract_origin DIE not being
9287 already referenced by DW_AT_abstract_origin from the children of the
9288 current DIE. */
9289
9290 static void
9291 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9292 {
9293 struct die_info *child_die;
9294 unsigned die_children_count;
9295 /* CU offsets which were referenced by children of the current DIE. */
9296 sect_offset *offsets;
9297 sect_offset *offsets_end, *offsetp;
9298 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9299 struct die_info *origin_die;
9300 /* Iterator of the ORIGIN_DIE children. */
9301 struct die_info *origin_child_die;
9302 struct cleanup *cleanups;
9303 struct attribute *attr;
9304 struct dwarf2_cu *origin_cu;
9305 struct pending **origin_previous_list_in_scope;
9306
9307 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9308 if (!attr)
9309 return;
9310
9311 /* Note that following die references may follow to a die in a
9312 different cu. */
9313
9314 origin_cu = cu;
9315 origin_die = follow_die_ref (die, attr, &origin_cu);
9316
9317 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9318 symbols in. */
9319 origin_previous_list_in_scope = origin_cu->list_in_scope;
9320 origin_cu->list_in_scope = cu->list_in_scope;
9321
9322 if (die->tag != origin_die->tag
9323 && !(die->tag == DW_TAG_inlined_subroutine
9324 && origin_die->tag == DW_TAG_subprogram))
9325 complaint (&symfile_complaints,
9326 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9327 die->offset.sect_off, origin_die->offset.sect_off);
9328
9329 child_die = die->child;
9330 die_children_count = 0;
9331 while (child_die && child_die->tag)
9332 {
9333 child_die = sibling_die (child_die);
9334 die_children_count++;
9335 }
9336 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9337 cleanups = make_cleanup (xfree, offsets);
9338
9339 offsets_end = offsets;
9340 child_die = die->child;
9341 while (child_die && child_die->tag)
9342 {
9343 /* For each CHILD_DIE, find the corresponding child of
9344 ORIGIN_DIE. If there is more than one layer of
9345 DW_AT_abstract_origin, follow them all; there shouldn't be,
9346 but GCC versions at least through 4.4 generate this (GCC PR
9347 40573). */
9348 struct die_info *child_origin_die = child_die;
9349 struct dwarf2_cu *child_origin_cu = cu;
9350
9351 while (1)
9352 {
9353 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9354 child_origin_cu);
9355 if (attr == NULL)
9356 break;
9357 child_origin_die = follow_die_ref (child_origin_die, attr,
9358 &child_origin_cu);
9359 }
9360
9361 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9362 counterpart may exist. */
9363 if (child_origin_die != child_die)
9364 {
9365 if (child_die->tag != child_origin_die->tag
9366 && !(child_die->tag == DW_TAG_inlined_subroutine
9367 && child_origin_die->tag == DW_TAG_subprogram))
9368 complaint (&symfile_complaints,
9369 _("Child DIE 0x%x and its abstract origin 0x%x have "
9370 "different tags"), child_die->offset.sect_off,
9371 child_origin_die->offset.sect_off);
9372 if (child_origin_die->parent != origin_die)
9373 complaint (&symfile_complaints,
9374 _("Child DIE 0x%x and its abstract origin 0x%x have "
9375 "different parents"), child_die->offset.sect_off,
9376 child_origin_die->offset.sect_off);
9377 else
9378 *offsets_end++ = child_origin_die->offset;
9379 }
9380 child_die = sibling_die (child_die);
9381 }
9382 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9383 unsigned_int_compar);
9384 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9385 if (offsetp[-1].sect_off == offsetp->sect_off)
9386 complaint (&symfile_complaints,
9387 _("Multiple children of DIE 0x%x refer "
9388 "to DIE 0x%x as their abstract origin"),
9389 die->offset.sect_off, offsetp->sect_off);
9390
9391 offsetp = offsets;
9392 origin_child_die = origin_die->child;
9393 while (origin_child_die && origin_child_die->tag)
9394 {
9395 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9396 while (offsetp < offsets_end
9397 && offsetp->sect_off < origin_child_die->offset.sect_off)
9398 offsetp++;
9399 if (offsetp >= offsets_end
9400 || offsetp->sect_off > origin_child_die->offset.sect_off)
9401 {
9402 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9403 process_die (origin_child_die, origin_cu);
9404 }
9405 origin_child_die = sibling_die (origin_child_die);
9406 }
9407 origin_cu->list_in_scope = origin_previous_list_in_scope;
9408
9409 do_cleanups (cleanups);
9410 }
9411
9412 static void
9413 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9414 {
9415 struct objfile *objfile = cu->objfile;
9416 struct context_stack *new;
9417 CORE_ADDR lowpc;
9418 CORE_ADDR highpc;
9419 struct die_info *child_die;
9420 struct attribute *attr, *call_line, *call_file;
9421 const char *name;
9422 CORE_ADDR baseaddr;
9423 struct block *block;
9424 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9425 VEC (symbolp) *template_args = NULL;
9426 struct template_symbol *templ_func = NULL;
9427
9428 if (inlined_func)
9429 {
9430 /* If we do not have call site information, we can't show the
9431 caller of this inlined function. That's too confusing, so
9432 only use the scope for local variables. */
9433 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9434 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9435 if (call_line == NULL || call_file == NULL)
9436 {
9437 read_lexical_block_scope (die, cu);
9438 return;
9439 }
9440 }
9441
9442 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9443
9444 name = dwarf2_name (die, cu);
9445
9446 /* Ignore functions with missing or empty names. These are actually
9447 illegal according to the DWARF standard. */
9448 if (name == NULL)
9449 {
9450 complaint (&symfile_complaints,
9451 _("missing name for subprogram DIE at %d"),
9452 die->offset.sect_off);
9453 return;
9454 }
9455
9456 /* Ignore functions with missing or invalid low and high pc attributes. */
9457 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9458 {
9459 attr = dwarf2_attr (die, DW_AT_external, cu);
9460 if (!attr || !DW_UNSND (attr))
9461 complaint (&symfile_complaints,
9462 _("cannot get low and high bounds "
9463 "for subprogram DIE at %d"),
9464 die->offset.sect_off);
9465 return;
9466 }
9467
9468 lowpc += baseaddr;
9469 highpc += baseaddr;
9470
9471 /* If we have any template arguments, then we must allocate a
9472 different sort of symbol. */
9473 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9474 {
9475 if (child_die->tag == DW_TAG_template_type_param
9476 || child_die->tag == DW_TAG_template_value_param)
9477 {
9478 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9479 struct template_symbol);
9480 templ_func->base.is_cplus_template_function = 1;
9481 break;
9482 }
9483 }
9484
9485 new = push_context (0, lowpc);
9486 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9487 (struct symbol *) templ_func);
9488
9489 /* If there is a location expression for DW_AT_frame_base, record
9490 it. */
9491 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9492 if (attr)
9493 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9494 expression is being recorded directly in the function's symbol
9495 and not in a separate frame-base object. I guess this hack is
9496 to avoid adding some sort of frame-base adjunct/annex to the
9497 function's symbol :-(. The problem with doing this is that it
9498 results in a function symbol with a location expression that
9499 has nothing to do with the location of the function, ouch! The
9500 relationship should be: a function's symbol has-a frame base; a
9501 frame-base has-a location expression. */
9502 dwarf2_symbol_mark_computed (attr, new->name, cu);
9503
9504 cu->list_in_scope = &local_symbols;
9505
9506 if (die->child != NULL)
9507 {
9508 child_die = die->child;
9509 while (child_die && child_die->tag)
9510 {
9511 if (child_die->tag == DW_TAG_template_type_param
9512 || child_die->tag == DW_TAG_template_value_param)
9513 {
9514 struct symbol *arg = new_symbol (child_die, NULL, cu);
9515
9516 if (arg != NULL)
9517 VEC_safe_push (symbolp, template_args, arg);
9518 }
9519 else
9520 process_die (child_die, cu);
9521 child_die = sibling_die (child_die);
9522 }
9523 }
9524
9525 inherit_abstract_dies (die, cu);
9526
9527 /* If we have a DW_AT_specification, we might need to import using
9528 directives from the context of the specification DIE. See the
9529 comment in determine_prefix. */
9530 if (cu->language == language_cplus
9531 && dwarf2_attr (die, DW_AT_specification, cu))
9532 {
9533 struct dwarf2_cu *spec_cu = cu;
9534 struct die_info *spec_die = die_specification (die, &spec_cu);
9535
9536 while (spec_die)
9537 {
9538 child_die = spec_die->child;
9539 while (child_die && child_die->tag)
9540 {
9541 if (child_die->tag == DW_TAG_imported_module)
9542 process_die (child_die, spec_cu);
9543 child_die = sibling_die (child_die);
9544 }
9545
9546 /* In some cases, GCC generates specification DIEs that
9547 themselves contain DW_AT_specification attributes. */
9548 spec_die = die_specification (spec_die, &spec_cu);
9549 }
9550 }
9551
9552 new = pop_context ();
9553 /* Make a block for the local symbols within. */
9554 block = finish_block (new->name, &local_symbols, new->old_blocks,
9555 lowpc, highpc, objfile);
9556
9557 /* For C++, set the block's scope. */
9558 if (cu->language == language_cplus || cu->language == language_fortran)
9559 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9560 determine_prefix (die, cu),
9561 processing_has_namespace_info);
9562
9563 /* If we have address ranges, record them. */
9564 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9565
9566 /* Attach template arguments to function. */
9567 if (! VEC_empty (symbolp, template_args))
9568 {
9569 gdb_assert (templ_func != NULL);
9570
9571 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9572 templ_func->template_arguments
9573 = obstack_alloc (&objfile->objfile_obstack,
9574 (templ_func->n_template_arguments
9575 * sizeof (struct symbol *)));
9576 memcpy (templ_func->template_arguments,
9577 VEC_address (symbolp, template_args),
9578 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9579 VEC_free (symbolp, template_args);
9580 }
9581
9582 /* In C++, we can have functions nested inside functions (e.g., when
9583 a function declares a class that has methods). This means that
9584 when we finish processing a function scope, we may need to go
9585 back to building a containing block's symbol lists. */
9586 local_symbols = new->locals;
9587 using_directives = new->using_directives;
9588
9589 /* If we've finished processing a top-level function, subsequent
9590 symbols go in the file symbol list. */
9591 if (outermost_context_p ())
9592 cu->list_in_scope = &file_symbols;
9593 }
9594
9595 /* Process all the DIES contained within a lexical block scope. Start
9596 a new scope, process the dies, and then close the scope. */
9597
9598 static void
9599 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9600 {
9601 struct objfile *objfile = cu->objfile;
9602 struct context_stack *new;
9603 CORE_ADDR lowpc, highpc;
9604 struct die_info *child_die;
9605 CORE_ADDR baseaddr;
9606
9607 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9608
9609 /* Ignore blocks with missing or invalid low and high pc attributes. */
9610 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9611 as multiple lexical blocks? Handling children in a sane way would
9612 be nasty. Might be easier to properly extend generic blocks to
9613 describe ranges. */
9614 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9615 return;
9616 lowpc += baseaddr;
9617 highpc += baseaddr;
9618
9619 push_context (0, lowpc);
9620 if (die->child != NULL)
9621 {
9622 child_die = die->child;
9623 while (child_die && child_die->tag)
9624 {
9625 process_die (child_die, cu);
9626 child_die = sibling_die (child_die);
9627 }
9628 }
9629 new = pop_context ();
9630
9631 if (local_symbols != NULL || using_directives != NULL)
9632 {
9633 struct block *block
9634 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9635 highpc, objfile);
9636
9637 /* Note that recording ranges after traversing children, as we
9638 do here, means that recording a parent's ranges entails
9639 walking across all its children's ranges as they appear in
9640 the address map, which is quadratic behavior.
9641
9642 It would be nicer to record the parent's ranges before
9643 traversing its children, simply overriding whatever you find
9644 there. But since we don't even decide whether to create a
9645 block until after we've traversed its children, that's hard
9646 to do. */
9647 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9648 }
9649 local_symbols = new->locals;
9650 using_directives = new->using_directives;
9651 }
9652
9653 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9654
9655 static void
9656 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9657 {
9658 struct objfile *objfile = cu->objfile;
9659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9660 CORE_ADDR pc, baseaddr;
9661 struct attribute *attr;
9662 struct call_site *call_site, call_site_local;
9663 void **slot;
9664 int nparams;
9665 struct die_info *child_die;
9666
9667 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9668
9669 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9670 if (!attr)
9671 {
9672 complaint (&symfile_complaints,
9673 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9674 "DIE 0x%x [in module %s]"),
9675 die->offset.sect_off, objfile->name);
9676 return;
9677 }
9678 pc = DW_ADDR (attr) + baseaddr;
9679
9680 if (cu->call_site_htab == NULL)
9681 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9682 NULL, &objfile->objfile_obstack,
9683 hashtab_obstack_allocate, NULL);
9684 call_site_local.pc = pc;
9685 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9686 if (*slot != NULL)
9687 {
9688 complaint (&symfile_complaints,
9689 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9690 "DIE 0x%x [in module %s]"),
9691 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9692 return;
9693 }
9694
9695 /* Count parameters at the caller. */
9696
9697 nparams = 0;
9698 for (child_die = die->child; child_die && child_die->tag;
9699 child_die = sibling_die (child_die))
9700 {
9701 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9702 {
9703 complaint (&symfile_complaints,
9704 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9705 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9706 child_die->tag, child_die->offset.sect_off, objfile->name);
9707 continue;
9708 }
9709
9710 nparams++;
9711 }
9712
9713 call_site = obstack_alloc (&objfile->objfile_obstack,
9714 (sizeof (*call_site)
9715 + (sizeof (*call_site->parameter)
9716 * (nparams - 1))));
9717 *slot = call_site;
9718 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9719 call_site->pc = pc;
9720
9721 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9722 {
9723 struct die_info *func_die;
9724
9725 /* Skip also over DW_TAG_inlined_subroutine. */
9726 for (func_die = die->parent;
9727 func_die && func_die->tag != DW_TAG_subprogram
9728 && func_die->tag != DW_TAG_subroutine_type;
9729 func_die = func_die->parent);
9730
9731 /* DW_AT_GNU_all_call_sites is a superset
9732 of DW_AT_GNU_all_tail_call_sites. */
9733 if (func_die
9734 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9735 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9736 {
9737 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9738 not complete. But keep CALL_SITE for look ups via call_site_htab,
9739 both the initial caller containing the real return address PC and
9740 the final callee containing the current PC of a chain of tail
9741 calls do not need to have the tail call list complete. But any
9742 function candidate for a virtual tail call frame searched via
9743 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9744 determined unambiguously. */
9745 }
9746 else
9747 {
9748 struct type *func_type = NULL;
9749
9750 if (func_die)
9751 func_type = get_die_type (func_die, cu);
9752 if (func_type != NULL)
9753 {
9754 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9755
9756 /* Enlist this call site to the function. */
9757 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9758 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9759 }
9760 else
9761 complaint (&symfile_complaints,
9762 _("Cannot find function owning DW_TAG_GNU_call_site "
9763 "DIE 0x%x [in module %s]"),
9764 die->offset.sect_off, objfile->name);
9765 }
9766 }
9767
9768 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9769 if (attr == NULL)
9770 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9771 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9772 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9773 /* Keep NULL DWARF_BLOCK. */;
9774 else if (attr_form_is_block (attr))
9775 {
9776 struct dwarf2_locexpr_baton *dlbaton;
9777
9778 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9779 dlbaton->data = DW_BLOCK (attr)->data;
9780 dlbaton->size = DW_BLOCK (attr)->size;
9781 dlbaton->per_cu = cu->per_cu;
9782
9783 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9784 }
9785 else if (is_ref_attr (attr))
9786 {
9787 struct dwarf2_cu *target_cu = cu;
9788 struct die_info *target_die;
9789
9790 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9791 gdb_assert (target_cu->objfile == objfile);
9792 if (die_is_declaration (target_die, target_cu))
9793 {
9794 const char *target_physname;
9795
9796 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9797 if (target_physname == NULL)
9798 complaint (&symfile_complaints,
9799 _("DW_AT_GNU_call_site_target target DIE has invalid "
9800 "physname, for referencing DIE 0x%x [in module %s]"),
9801 die->offset.sect_off, objfile->name);
9802 else
9803 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9804 }
9805 else
9806 {
9807 CORE_ADDR lowpc;
9808
9809 /* DW_AT_entry_pc should be preferred. */
9810 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9811 complaint (&symfile_complaints,
9812 _("DW_AT_GNU_call_site_target target DIE has invalid "
9813 "low pc, for referencing DIE 0x%x [in module %s]"),
9814 die->offset.sect_off, objfile->name);
9815 else
9816 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9817 }
9818 }
9819 else
9820 complaint (&symfile_complaints,
9821 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9822 "block nor reference, for DIE 0x%x [in module %s]"),
9823 die->offset.sect_off, objfile->name);
9824
9825 call_site->per_cu = cu->per_cu;
9826
9827 for (child_die = die->child;
9828 child_die && child_die->tag;
9829 child_die = sibling_die (child_die))
9830 {
9831 struct call_site_parameter *parameter;
9832 struct attribute *loc, *origin;
9833
9834 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9835 {
9836 /* Already printed the complaint above. */
9837 continue;
9838 }
9839
9840 gdb_assert (call_site->parameter_count < nparams);
9841 parameter = &call_site->parameter[call_site->parameter_count];
9842
9843 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9844 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9845 register is contained in DW_AT_GNU_call_site_value. */
9846
9847 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9848 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9849 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9850 {
9851 sect_offset offset;
9852
9853 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9854 offset = dwarf2_get_ref_die_offset (origin);
9855 if (!offset_in_cu_p (&cu->header, offset))
9856 {
9857 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9858 binding can be done only inside one CU. Such referenced DIE
9859 therefore cannot be even moved to DW_TAG_partial_unit. */
9860 complaint (&symfile_complaints,
9861 _("DW_AT_abstract_origin offset is not in CU for "
9862 "DW_TAG_GNU_call_site child DIE 0x%x "
9863 "[in module %s]"),
9864 child_die->offset.sect_off, objfile->name);
9865 continue;
9866 }
9867 parameter->u.param_offset.cu_off = (offset.sect_off
9868 - cu->header.offset.sect_off);
9869 }
9870 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9871 {
9872 complaint (&symfile_complaints,
9873 _("No DW_FORM_block* DW_AT_location for "
9874 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9875 child_die->offset.sect_off, objfile->name);
9876 continue;
9877 }
9878 else
9879 {
9880 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9881 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9882 if (parameter->u.dwarf_reg != -1)
9883 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9884 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9885 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9886 &parameter->u.fb_offset))
9887 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9888 else
9889 {
9890 complaint (&symfile_complaints,
9891 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9892 "for DW_FORM_block* DW_AT_location is supported for "
9893 "DW_TAG_GNU_call_site child DIE 0x%x "
9894 "[in module %s]"),
9895 child_die->offset.sect_off, objfile->name);
9896 continue;
9897 }
9898 }
9899
9900 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9901 if (!attr_form_is_block (attr))
9902 {
9903 complaint (&symfile_complaints,
9904 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9905 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9906 child_die->offset.sect_off, objfile->name);
9907 continue;
9908 }
9909 parameter->value = DW_BLOCK (attr)->data;
9910 parameter->value_size = DW_BLOCK (attr)->size;
9911
9912 /* Parameters are not pre-cleared by memset above. */
9913 parameter->data_value = NULL;
9914 parameter->data_value_size = 0;
9915 call_site->parameter_count++;
9916
9917 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9918 if (attr)
9919 {
9920 if (!attr_form_is_block (attr))
9921 complaint (&symfile_complaints,
9922 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9923 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9924 child_die->offset.sect_off, objfile->name);
9925 else
9926 {
9927 parameter->data_value = DW_BLOCK (attr)->data;
9928 parameter->data_value_size = DW_BLOCK (attr)->size;
9929 }
9930 }
9931 }
9932 }
9933
9934 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9935 Return 1 if the attributes are present and valid, otherwise, return 0.
9936 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9937
9938 static int
9939 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9940 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9941 struct partial_symtab *ranges_pst)
9942 {
9943 struct objfile *objfile = cu->objfile;
9944 struct comp_unit_head *cu_header = &cu->header;
9945 bfd *obfd = objfile->obfd;
9946 unsigned int addr_size = cu_header->addr_size;
9947 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9948 /* Base address selection entry. */
9949 CORE_ADDR base;
9950 int found_base;
9951 unsigned int dummy;
9952 gdb_byte *buffer;
9953 CORE_ADDR marker;
9954 int low_set;
9955 CORE_ADDR low = 0;
9956 CORE_ADDR high = 0;
9957 CORE_ADDR baseaddr;
9958
9959 found_base = cu->base_known;
9960 base = cu->base_address;
9961
9962 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9963 if (offset >= dwarf2_per_objfile->ranges.size)
9964 {
9965 complaint (&symfile_complaints,
9966 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9967 offset);
9968 return 0;
9969 }
9970 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9971
9972 /* Read in the largest possible address. */
9973 marker = read_address (obfd, buffer, cu, &dummy);
9974 if ((marker & mask) == mask)
9975 {
9976 /* If we found the largest possible address, then
9977 read the base address. */
9978 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9979 buffer += 2 * addr_size;
9980 offset += 2 * addr_size;
9981 found_base = 1;
9982 }
9983
9984 low_set = 0;
9985
9986 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9987
9988 while (1)
9989 {
9990 CORE_ADDR range_beginning, range_end;
9991
9992 range_beginning = read_address (obfd, buffer, cu, &dummy);
9993 buffer += addr_size;
9994 range_end = read_address (obfd, buffer, cu, &dummy);
9995 buffer += addr_size;
9996 offset += 2 * addr_size;
9997
9998 /* An end of list marker is a pair of zero addresses. */
9999 if (range_beginning == 0 && range_end == 0)
10000 /* Found the end of list entry. */
10001 break;
10002
10003 /* Each base address selection entry is a pair of 2 values.
10004 The first is the largest possible address, the second is
10005 the base address. Check for a base address here. */
10006 if ((range_beginning & mask) == mask)
10007 {
10008 /* If we found the largest possible address, then
10009 read the base address. */
10010 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10011 found_base = 1;
10012 continue;
10013 }
10014
10015 if (!found_base)
10016 {
10017 /* We have no valid base address for the ranges
10018 data. */
10019 complaint (&symfile_complaints,
10020 _("Invalid .debug_ranges data (no base address)"));
10021 return 0;
10022 }
10023
10024 if (range_beginning > range_end)
10025 {
10026 /* Inverted range entries are invalid. */
10027 complaint (&symfile_complaints,
10028 _("Invalid .debug_ranges data (inverted range)"));
10029 return 0;
10030 }
10031
10032 /* Empty range entries have no effect. */
10033 if (range_beginning == range_end)
10034 continue;
10035
10036 range_beginning += base;
10037 range_end += base;
10038
10039 /* A not-uncommon case of bad debug info.
10040 Don't pollute the addrmap with bad data. */
10041 if (range_beginning + baseaddr == 0
10042 && !dwarf2_per_objfile->has_section_at_zero)
10043 {
10044 complaint (&symfile_complaints,
10045 _(".debug_ranges entry has start address of zero"
10046 " [in module %s]"), objfile->name);
10047 continue;
10048 }
10049
10050 if (ranges_pst != NULL)
10051 addrmap_set_empty (objfile->psymtabs_addrmap,
10052 range_beginning + baseaddr,
10053 range_end - 1 + baseaddr,
10054 ranges_pst);
10055
10056 /* FIXME: This is recording everything as a low-high
10057 segment of consecutive addresses. We should have a
10058 data structure for discontiguous block ranges
10059 instead. */
10060 if (! low_set)
10061 {
10062 low = range_beginning;
10063 high = range_end;
10064 low_set = 1;
10065 }
10066 else
10067 {
10068 if (range_beginning < low)
10069 low = range_beginning;
10070 if (range_end > high)
10071 high = range_end;
10072 }
10073 }
10074
10075 if (! low_set)
10076 /* If the first entry is an end-of-list marker, the range
10077 describes an empty scope, i.e. no instructions. */
10078 return 0;
10079
10080 if (low_return)
10081 *low_return = low;
10082 if (high_return)
10083 *high_return = high;
10084 return 1;
10085 }
10086
10087 /* Get low and high pc attributes from a die. Return 1 if the attributes
10088 are present and valid, otherwise, return 0. Return -1 if the range is
10089 discontinuous, i.e. derived from DW_AT_ranges information. */
10090
10091 static int
10092 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10093 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10094 struct partial_symtab *pst)
10095 {
10096 struct attribute *attr;
10097 struct attribute *attr_high;
10098 CORE_ADDR low = 0;
10099 CORE_ADDR high = 0;
10100 int ret = 0;
10101
10102 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10103 if (attr_high)
10104 {
10105 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10106 if (attr)
10107 {
10108 low = DW_ADDR (attr);
10109 if (attr_high->form == DW_FORM_addr
10110 || attr_high->form == DW_FORM_GNU_addr_index)
10111 high = DW_ADDR (attr_high);
10112 else
10113 high = low + DW_UNSND (attr_high);
10114 }
10115 else
10116 /* Found high w/o low attribute. */
10117 return 0;
10118
10119 /* Found consecutive range of addresses. */
10120 ret = 1;
10121 }
10122 else
10123 {
10124 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10125 if (attr != NULL)
10126 {
10127 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10128 We take advantage of the fact that DW_AT_ranges does not appear
10129 in DW_TAG_compile_unit of DWO files. */
10130 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10131 unsigned int ranges_offset = (DW_UNSND (attr)
10132 + (need_ranges_base
10133 ? cu->ranges_base
10134 : 0));
10135
10136 /* Value of the DW_AT_ranges attribute is the offset in the
10137 .debug_ranges section. */
10138 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10139 return 0;
10140 /* Found discontinuous range of addresses. */
10141 ret = -1;
10142 }
10143 }
10144
10145 /* read_partial_die has also the strict LOW < HIGH requirement. */
10146 if (high <= low)
10147 return 0;
10148
10149 /* When using the GNU linker, .gnu.linkonce. sections are used to
10150 eliminate duplicate copies of functions and vtables and such.
10151 The linker will arbitrarily choose one and discard the others.
10152 The AT_*_pc values for such functions refer to local labels in
10153 these sections. If the section from that file was discarded, the
10154 labels are not in the output, so the relocs get a value of 0.
10155 If this is a discarded function, mark the pc bounds as invalid,
10156 so that GDB will ignore it. */
10157 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10158 return 0;
10159
10160 *lowpc = low;
10161 if (highpc)
10162 *highpc = high;
10163 return ret;
10164 }
10165
10166 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10167 its low and high PC addresses. Do nothing if these addresses could not
10168 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10169 and HIGHPC to the high address if greater than HIGHPC. */
10170
10171 static void
10172 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10174 struct dwarf2_cu *cu)
10175 {
10176 CORE_ADDR low, high;
10177 struct die_info *child = die->child;
10178
10179 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10180 {
10181 *lowpc = min (*lowpc, low);
10182 *highpc = max (*highpc, high);
10183 }
10184
10185 /* If the language does not allow nested subprograms (either inside
10186 subprograms or lexical blocks), we're done. */
10187 if (cu->language != language_ada)
10188 return;
10189
10190 /* Check all the children of the given DIE. If it contains nested
10191 subprograms, then check their pc bounds. Likewise, we need to
10192 check lexical blocks as well, as they may also contain subprogram
10193 definitions. */
10194 while (child && child->tag)
10195 {
10196 if (child->tag == DW_TAG_subprogram
10197 || child->tag == DW_TAG_lexical_block)
10198 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10199 child = sibling_die (child);
10200 }
10201 }
10202
10203 /* Get the low and high pc's represented by the scope DIE, and store
10204 them in *LOWPC and *HIGHPC. If the correct values can't be
10205 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10206
10207 static void
10208 get_scope_pc_bounds (struct die_info *die,
10209 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10210 struct dwarf2_cu *cu)
10211 {
10212 CORE_ADDR best_low = (CORE_ADDR) -1;
10213 CORE_ADDR best_high = (CORE_ADDR) 0;
10214 CORE_ADDR current_low, current_high;
10215
10216 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10217 {
10218 best_low = current_low;
10219 best_high = current_high;
10220 }
10221 else
10222 {
10223 struct die_info *child = die->child;
10224
10225 while (child && child->tag)
10226 {
10227 switch (child->tag) {
10228 case DW_TAG_subprogram:
10229 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10230 break;
10231 case DW_TAG_namespace:
10232 case DW_TAG_module:
10233 /* FIXME: carlton/2004-01-16: Should we do this for
10234 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10235 that current GCC's always emit the DIEs corresponding
10236 to definitions of methods of classes as children of a
10237 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10238 the DIEs giving the declarations, which could be
10239 anywhere). But I don't see any reason why the
10240 standards says that they have to be there. */
10241 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10242
10243 if (current_low != ((CORE_ADDR) -1))
10244 {
10245 best_low = min (best_low, current_low);
10246 best_high = max (best_high, current_high);
10247 }
10248 break;
10249 default:
10250 /* Ignore. */
10251 break;
10252 }
10253
10254 child = sibling_die (child);
10255 }
10256 }
10257
10258 *lowpc = best_low;
10259 *highpc = best_high;
10260 }
10261
10262 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10263 in DIE. */
10264
10265 static void
10266 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10267 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10268 {
10269 struct objfile *objfile = cu->objfile;
10270 struct attribute *attr;
10271 struct attribute *attr_high;
10272
10273 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10274 if (attr_high)
10275 {
10276 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10277 if (attr)
10278 {
10279 CORE_ADDR low = DW_ADDR (attr);
10280 CORE_ADDR high;
10281 if (attr_high->form == DW_FORM_addr
10282 || attr_high->form == DW_FORM_GNU_addr_index)
10283 high = DW_ADDR (attr_high);
10284 else
10285 high = low + DW_UNSND (attr_high);
10286
10287 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10288 }
10289 }
10290
10291 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10292 if (attr)
10293 {
10294 bfd *obfd = objfile->obfd;
10295 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10296 We take advantage of the fact that DW_AT_ranges does not appear
10297 in DW_TAG_compile_unit of DWO files. */
10298 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10299
10300 /* The value of the DW_AT_ranges attribute is the offset of the
10301 address range list in the .debug_ranges section. */
10302 unsigned long offset = (DW_UNSND (attr)
10303 + (need_ranges_base ? cu->ranges_base : 0));
10304 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10305
10306 /* For some target architectures, but not others, the
10307 read_address function sign-extends the addresses it returns.
10308 To recognize base address selection entries, we need a
10309 mask. */
10310 unsigned int addr_size = cu->header.addr_size;
10311 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10312
10313 /* The base address, to which the next pair is relative. Note
10314 that this 'base' is a DWARF concept: most entries in a range
10315 list are relative, to reduce the number of relocs against the
10316 debugging information. This is separate from this function's
10317 'baseaddr' argument, which GDB uses to relocate debugging
10318 information from a shared library based on the address at
10319 which the library was loaded. */
10320 CORE_ADDR base = cu->base_address;
10321 int base_known = cu->base_known;
10322
10323 gdb_assert (dwarf2_per_objfile->ranges.readin);
10324 if (offset >= dwarf2_per_objfile->ranges.size)
10325 {
10326 complaint (&symfile_complaints,
10327 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10328 offset);
10329 return;
10330 }
10331
10332 for (;;)
10333 {
10334 unsigned int bytes_read;
10335 CORE_ADDR start, end;
10336
10337 start = read_address (obfd, buffer, cu, &bytes_read);
10338 buffer += bytes_read;
10339 end = read_address (obfd, buffer, cu, &bytes_read);
10340 buffer += bytes_read;
10341
10342 /* Did we find the end of the range list? */
10343 if (start == 0 && end == 0)
10344 break;
10345
10346 /* Did we find a base address selection entry? */
10347 else if ((start & base_select_mask) == base_select_mask)
10348 {
10349 base = end;
10350 base_known = 1;
10351 }
10352
10353 /* We found an ordinary address range. */
10354 else
10355 {
10356 if (!base_known)
10357 {
10358 complaint (&symfile_complaints,
10359 _("Invalid .debug_ranges data "
10360 "(no base address)"));
10361 return;
10362 }
10363
10364 if (start > end)
10365 {
10366 /* Inverted range entries are invalid. */
10367 complaint (&symfile_complaints,
10368 _("Invalid .debug_ranges data "
10369 "(inverted range)"));
10370 return;
10371 }
10372
10373 /* Empty range entries have no effect. */
10374 if (start == end)
10375 continue;
10376
10377 start += base + baseaddr;
10378 end += base + baseaddr;
10379
10380 /* A not-uncommon case of bad debug info.
10381 Don't pollute the addrmap with bad data. */
10382 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10383 {
10384 complaint (&symfile_complaints,
10385 _(".debug_ranges entry has start address of zero"
10386 " [in module %s]"), objfile->name);
10387 continue;
10388 }
10389
10390 record_block_range (block, start, end - 1);
10391 }
10392 }
10393 }
10394 }
10395
10396 /* Check whether the producer field indicates either of GCC < 4.6, or the
10397 Intel C/C++ compiler, and cache the result in CU. */
10398
10399 static void
10400 check_producer (struct dwarf2_cu *cu)
10401 {
10402 const char *cs;
10403 int major, minor, release;
10404
10405 if (cu->producer == NULL)
10406 {
10407 /* For unknown compilers expect their behavior is DWARF version
10408 compliant.
10409
10410 GCC started to support .debug_types sections by -gdwarf-4 since
10411 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10412 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10413 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10414 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10415 }
10416 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10417 {
10418 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10419
10420 cs = &cu->producer[strlen ("GNU ")];
10421 while (*cs && !isdigit (*cs))
10422 cs++;
10423 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10424 {
10425 /* Not recognized as GCC. */
10426 }
10427 else
10428 {
10429 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10430 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10431 }
10432 }
10433 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10434 cu->producer_is_icc = 1;
10435 else
10436 {
10437 /* For other non-GCC compilers, expect their behavior is DWARF version
10438 compliant. */
10439 }
10440
10441 cu->checked_producer = 1;
10442 }
10443
10444 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10445 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10446 during 4.6.0 experimental. */
10447
10448 static int
10449 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10450 {
10451 if (!cu->checked_producer)
10452 check_producer (cu);
10453
10454 return cu->producer_is_gxx_lt_4_6;
10455 }
10456
10457 /* Return the default accessibility type if it is not overriden by
10458 DW_AT_accessibility. */
10459
10460 static enum dwarf_access_attribute
10461 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10462 {
10463 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10464 {
10465 /* The default DWARF 2 accessibility for members is public, the default
10466 accessibility for inheritance is private. */
10467
10468 if (die->tag != DW_TAG_inheritance)
10469 return DW_ACCESS_public;
10470 else
10471 return DW_ACCESS_private;
10472 }
10473 else
10474 {
10475 /* DWARF 3+ defines the default accessibility a different way. The same
10476 rules apply now for DW_TAG_inheritance as for the members and it only
10477 depends on the container kind. */
10478
10479 if (die->parent->tag == DW_TAG_class_type)
10480 return DW_ACCESS_private;
10481 else
10482 return DW_ACCESS_public;
10483 }
10484 }
10485
10486 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10487 offset. If the attribute was not found return 0, otherwise return
10488 1. If it was found but could not properly be handled, set *OFFSET
10489 to 0. */
10490
10491 static int
10492 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10493 LONGEST *offset)
10494 {
10495 struct attribute *attr;
10496
10497 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10498 if (attr != NULL)
10499 {
10500 *offset = 0;
10501
10502 /* Note that we do not check for a section offset first here.
10503 This is because DW_AT_data_member_location is new in DWARF 4,
10504 so if we see it, we can assume that a constant form is really
10505 a constant and not a section offset. */
10506 if (attr_form_is_constant (attr))
10507 *offset = dwarf2_get_attr_constant_value (attr, 0);
10508 else if (attr_form_is_section_offset (attr))
10509 dwarf2_complex_location_expr_complaint ();
10510 else if (attr_form_is_block (attr))
10511 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10512 else
10513 dwarf2_complex_location_expr_complaint ();
10514
10515 return 1;
10516 }
10517
10518 return 0;
10519 }
10520
10521 /* Add an aggregate field to the field list. */
10522
10523 static void
10524 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10525 struct dwarf2_cu *cu)
10526 {
10527 struct objfile *objfile = cu->objfile;
10528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10529 struct nextfield *new_field;
10530 struct attribute *attr;
10531 struct field *fp;
10532 const char *fieldname = "";
10533
10534 /* Allocate a new field list entry and link it in. */
10535 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10536 make_cleanup (xfree, new_field);
10537 memset (new_field, 0, sizeof (struct nextfield));
10538
10539 if (die->tag == DW_TAG_inheritance)
10540 {
10541 new_field->next = fip->baseclasses;
10542 fip->baseclasses = new_field;
10543 }
10544 else
10545 {
10546 new_field->next = fip->fields;
10547 fip->fields = new_field;
10548 }
10549 fip->nfields++;
10550
10551 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10552 if (attr)
10553 new_field->accessibility = DW_UNSND (attr);
10554 else
10555 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10556 if (new_field->accessibility != DW_ACCESS_public)
10557 fip->non_public_fields = 1;
10558
10559 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10560 if (attr)
10561 new_field->virtuality = DW_UNSND (attr);
10562 else
10563 new_field->virtuality = DW_VIRTUALITY_none;
10564
10565 fp = &new_field->field;
10566
10567 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10568 {
10569 LONGEST offset;
10570
10571 /* Data member other than a C++ static data member. */
10572
10573 /* Get type of field. */
10574 fp->type = die_type (die, cu);
10575
10576 SET_FIELD_BITPOS (*fp, 0);
10577
10578 /* Get bit size of field (zero if none). */
10579 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10580 if (attr)
10581 {
10582 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10583 }
10584 else
10585 {
10586 FIELD_BITSIZE (*fp) = 0;
10587 }
10588
10589 /* Get bit offset of field. */
10590 if (handle_data_member_location (die, cu, &offset))
10591 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10592 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10593 if (attr)
10594 {
10595 if (gdbarch_bits_big_endian (gdbarch))
10596 {
10597 /* For big endian bits, the DW_AT_bit_offset gives the
10598 additional bit offset from the MSB of the containing
10599 anonymous object to the MSB of the field. We don't
10600 have to do anything special since we don't need to
10601 know the size of the anonymous object. */
10602 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10603 }
10604 else
10605 {
10606 /* For little endian bits, compute the bit offset to the
10607 MSB of the anonymous object, subtract off the number of
10608 bits from the MSB of the field to the MSB of the
10609 object, and then subtract off the number of bits of
10610 the field itself. The result is the bit offset of
10611 the LSB of the field. */
10612 int anonymous_size;
10613 int bit_offset = DW_UNSND (attr);
10614
10615 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10616 if (attr)
10617 {
10618 /* The size of the anonymous object containing
10619 the bit field is explicit, so use the
10620 indicated size (in bytes). */
10621 anonymous_size = DW_UNSND (attr);
10622 }
10623 else
10624 {
10625 /* The size of the anonymous object containing
10626 the bit field must be inferred from the type
10627 attribute of the data member containing the
10628 bit field. */
10629 anonymous_size = TYPE_LENGTH (fp->type);
10630 }
10631 SET_FIELD_BITPOS (*fp,
10632 (FIELD_BITPOS (*fp)
10633 + anonymous_size * bits_per_byte
10634 - bit_offset - FIELD_BITSIZE (*fp)));
10635 }
10636 }
10637
10638 /* Get name of field. */
10639 fieldname = dwarf2_name (die, cu);
10640 if (fieldname == NULL)
10641 fieldname = "";
10642
10643 /* The name is already allocated along with this objfile, so we don't
10644 need to duplicate it for the type. */
10645 fp->name = fieldname;
10646
10647 /* Change accessibility for artificial fields (e.g. virtual table
10648 pointer or virtual base class pointer) to private. */
10649 if (dwarf2_attr (die, DW_AT_artificial, cu))
10650 {
10651 FIELD_ARTIFICIAL (*fp) = 1;
10652 new_field->accessibility = DW_ACCESS_private;
10653 fip->non_public_fields = 1;
10654 }
10655 }
10656 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10657 {
10658 /* C++ static member. */
10659
10660 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10661 is a declaration, but all versions of G++ as of this writing
10662 (so through at least 3.2.1) incorrectly generate
10663 DW_TAG_variable tags. */
10664
10665 const char *physname;
10666
10667 /* Get name of field. */
10668 fieldname = dwarf2_name (die, cu);
10669 if (fieldname == NULL)
10670 return;
10671
10672 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10673 if (attr
10674 /* Only create a symbol if this is an external value.
10675 new_symbol checks this and puts the value in the global symbol
10676 table, which we want. If it is not external, new_symbol
10677 will try to put the value in cu->list_in_scope which is wrong. */
10678 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10679 {
10680 /* A static const member, not much different than an enum as far as
10681 we're concerned, except that we can support more types. */
10682 new_symbol (die, NULL, cu);
10683 }
10684
10685 /* Get physical name. */
10686 physname = dwarf2_physname (fieldname, die, cu);
10687
10688 /* The name is already allocated along with this objfile, so we don't
10689 need to duplicate it for the type. */
10690 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10691 FIELD_TYPE (*fp) = die_type (die, cu);
10692 FIELD_NAME (*fp) = fieldname;
10693 }
10694 else if (die->tag == DW_TAG_inheritance)
10695 {
10696 LONGEST offset;
10697
10698 /* C++ base class field. */
10699 if (handle_data_member_location (die, cu, &offset))
10700 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10701 FIELD_BITSIZE (*fp) = 0;
10702 FIELD_TYPE (*fp) = die_type (die, cu);
10703 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10704 fip->nbaseclasses++;
10705 }
10706 }
10707
10708 /* Add a typedef defined in the scope of the FIP's class. */
10709
10710 static void
10711 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10712 struct dwarf2_cu *cu)
10713 {
10714 struct objfile *objfile = cu->objfile;
10715 struct typedef_field_list *new_field;
10716 struct attribute *attr;
10717 struct typedef_field *fp;
10718 char *fieldname = "";
10719
10720 /* Allocate a new field list entry and link it in. */
10721 new_field = xzalloc (sizeof (*new_field));
10722 make_cleanup (xfree, new_field);
10723
10724 gdb_assert (die->tag == DW_TAG_typedef);
10725
10726 fp = &new_field->field;
10727
10728 /* Get name of field. */
10729 fp->name = dwarf2_name (die, cu);
10730 if (fp->name == NULL)
10731 return;
10732
10733 fp->type = read_type_die (die, cu);
10734
10735 new_field->next = fip->typedef_field_list;
10736 fip->typedef_field_list = new_field;
10737 fip->typedef_field_list_count++;
10738 }
10739
10740 /* Create the vector of fields, and attach it to the type. */
10741
10742 static void
10743 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10744 struct dwarf2_cu *cu)
10745 {
10746 int nfields = fip->nfields;
10747
10748 /* Record the field count, allocate space for the array of fields,
10749 and create blank accessibility bitfields if necessary. */
10750 TYPE_NFIELDS (type) = nfields;
10751 TYPE_FIELDS (type) = (struct field *)
10752 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10753 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10754
10755 if (fip->non_public_fields && cu->language != language_ada)
10756 {
10757 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10758
10759 TYPE_FIELD_PRIVATE_BITS (type) =
10760 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10761 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10762
10763 TYPE_FIELD_PROTECTED_BITS (type) =
10764 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10765 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10766
10767 TYPE_FIELD_IGNORE_BITS (type) =
10768 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10769 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10770 }
10771
10772 /* If the type has baseclasses, allocate and clear a bit vector for
10773 TYPE_FIELD_VIRTUAL_BITS. */
10774 if (fip->nbaseclasses && cu->language != language_ada)
10775 {
10776 int num_bytes = B_BYTES (fip->nbaseclasses);
10777 unsigned char *pointer;
10778
10779 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10780 pointer = TYPE_ALLOC (type, num_bytes);
10781 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10782 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10783 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10784 }
10785
10786 /* Copy the saved-up fields into the field vector. Start from the head of
10787 the list, adding to the tail of the field array, so that they end up in
10788 the same order in the array in which they were added to the list. */
10789 while (nfields-- > 0)
10790 {
10791 struct nextfield *fieldp;
10792
10793 if (fip->fields)
10794 {
10795 fieldp = fip->fields;
10796 fip->fields = fieldp->next;
10797 }
10798 else
10799 {
10800 fieldp = fip->baseclasses;
10801 fip->baseclasses = fieldp->next;
10802 }
10803
10804 TYPE_FIELD (type, nfields) = fieldp->field;
10805 switch (fieldp->accessibility)
10806 {
10807 case DW_ACCESS_private:
10808 if (cu->language != language_ada)
10809 SET_TYPE_FIELD_PRIVATE (type, nfields);
10810 break;
10811
10812 case DW_ACCESS_protected:
10813 if (cu->language != language_ada)
10814 SET_TYPE_FIELD_PROTECTED (type, nfields);
10815 break;
10816
10817 case DW_ACCESS_public:
10818 break;
10819
10820 default:
10821 /* Unknown accessibility. Complain and treat it as public. */
10822 {
10823 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10824 fieldp->accessibility);
10825 }
10826 break;
10827 }
10828 if (nfields < fip->nbaseclasses)
10829 {
10830 switch (fieldp->virtuality)
10831 {
10832 case DW_VIRTUALITY_virtual:
10833 case DW_VIRTUALITY_pure_virtual:
10834 if (cu->language == language_ada)
10835 error (_("unexpected virtuality in component of Ada type"));
10836 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10837 break;
10838 }
10839 }
10840 }
10841 }
10842
10843 /* Return true if this member function is a constructor, false
10844 otherwise. */
10845
10846 static int
10847 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10848 {
10849 const char *fieldname;
10850 const char *typename;
10851 int len;
10852
10853 if (die->parent == NULL)
10854 return 0;
10855
10856 if (die->parent->tag != DW_TAG_structure_type
10857 && die->parent->tag != DW_TAG_union_type
10858 && die->parent->tag != DW_TAG_class_type)
10859 return 0;
10860
10861 fieldname = dwarf2_name (die, cu);
10862 typename = dwarf2_name (die->parent, cu);
10863 if (fieldname == NULL || typename == NULL)
10864 return 0;
10865
10866 len = strlen (fieldname);
10867 return (strncmp (fieldname, typename, len) == 0
10868 && (typename[len] == '\0' || typename[len] == '<'));
10869 }
10870
10871 /* Add a member function to the proper fieldlist. */
10872
10873 static void
10874 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10875 struct type *type, struct dwarf2_cu *cu)
10876 {
10877 struct objfile *objfile = cu->objfile;
10878 struct attribute *attr;
10879 struct fnfieldlist *flp;
10880 int i;
10881 struct fn_field *fnp;
10882 const char *fieldname;
10883 struct nextfnfield *new_fnfield;
10884 struct type *this_type;
10885 enum dwarf_access_attribute accessibility;
10886
10887 if (cu->language == language_ada)
10888 error (_("unexpected member function in Ada type"));
10889
10890 /* Get name of member function. */
10891 fieldname = dwarf2_name (die, cu);
10892 if (fieldname == NULL)
10893 return;
10894
10895 /* Look up member function name in fieldlist. */
10896 for (i = 0; i < fip->nfnfields; i++)
10897 {
10898 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10899 break;
10900 }
10901
10902 /* Create new list element if necessary. */
10903 if (i < fip->nfnfields)
10904 flp = &fip->fnfieldlists[i];
10905 else
10906 {
10907 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10908 {
10909 fip->fnfieldlists = (struct fnfieldlist *)
10910 xrealloc (fip->fnfieldlists,
10911 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10912 * sizeof (struct fnfieldlist));
10913 if (fip->nfnfields == 0)
10914 make_cleanup (free_current_contents, &fip->fnfieldlists);
10915 }
10916 flp = &fip->fnfieldlists[fip->nfnfields];
10917 flp->name = fieldname;
10918 flp->length = 0;
10919 flp->head = NULL;
10920 i = fip->nfnfields++;
10921 }
10922
10923 /* Create a new member function field and chain it to the field list
10924 entry. */
10925 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10926 make_cleanup (xfree, new_fnfield);
10927 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10928 new_fnfield->next = flp->head;
10929 flp->head = new_fnfield;
10930 flp->length++;
10931
10932 /* Fill in the member function field info. */
10933 fnp = &new_fnfield->fnfield;
10934
10935 /* Delay processing of the physname until later. */
10936 if (cu->language == language_cplus || cu->language == language_java)
10937 {
10938 add_to_method_list (type, i, flp->length - 1, fieldname,
10939 die, cu);
10940 }
10941 else
10942 {
10943 const char *physname = dwarf2_physname (fieldname, die, cu);
10944 fnp->physname = physname ? physname : "";
10945 }
10946
10947 fnp->type = alloc_type (objfile);
10948 this_type = read_type_die (die, cu);
10949 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10950 {
10951 int nparams = TYPE_NFIELDS (this_type);
10952
10953 /* TYPE is the domain of this method, and THIS_TYPE is the type
10954 of the method itself (TYPE_CODE_METHOD). */
10955 smash_to_method_type (fnp->type, type,
10956 TYPE_TARGET_TYPE (this_type),
10957 TYPE_FIELDS (this_type),
10958 TYPE_NFIELDS (this_type),
10959 TYPE_VARARGS (this_type));
10960
10961 /* Handle static member functions.
10962 Dwarf2 has no clean way to discern C++ static and non-static
10963 member functions. G++ helps GDB by marking the first
10964 parameter for non-static member functions (which is the this
10965 pointer) as artificial. We obtain this information from
10966 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10967 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10968 fnp->voffset = VOFFSET_STATIC;
10969 }
10970 else
10971 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10972 dwarf2_full_name (fieldname, die, cu));
10973
10974 /* Get fcontext from DW_AT_containing_type if present. */
10975 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10976 fnp->fcontext = die_containing_type (die, cu);
10977
10978 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10979 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10980
10981 /* Get accessibility. */
10982 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10983 if (attr)
10984 accessibility = DW_UNSND (attr);
10985 else
10986 accessibility = dwarf2_default_access_attribute (die, cu);
10987 switch (accessibility)
10988 {
10989 case DW_ACCESS_private:
10990 fnp->is_private = 1;
10991 break;
10992 case DW_ACCESS_protected:
10993 fnp->is_protected = 1;
10994 break;
10995 }
10996
10997 /* Check for artificial methods. */
10998 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10999 if (attr && DW_UNSND (attr) != 0)
11000 fnp->is_artificial = 1;
11001
11002 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11003
11004 /* Get index in virtual function table if it is a virtual member
11005 function. For older versions of GCC, this is an offset in the
11006 appropriate virtual table, as specified by DW_AT_containing_type.
11007 For everyone else, it is an expression to be evaluated relative
11008 to the object address. */
11009
11010 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11011 if (attr)
11012 {
11013 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11014 {
11015 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11016 {
11017 /* Old-style GCC. */
11018 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11019 }
11020 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11021 || (DW_BLOCK (attr)->size > 1
11022 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11023 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11024 {
11025 struct dwarf_block blk;
11026 int offset;
11027
11028 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11029 ? 1 : 2);
11030 blk.size = DW_BLOCK (attr)->size - offset;
11031 blk.data = DW_BLOCK (attr)->data + offset;
11032 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11033 if ((fnp->voffset % cu->header.addr_size) != 0)
11034 dwarf2_complex_location_expr_complaint ();
11035 else
11036 fnp->voffset /= cu->header.addr_size;
11037 fnp->voffset += 2;
11038 }
11039 else
11040 dwarf2_complex_location_expr_complaint ();
11041
11042 if (!fnp->fcontext)
11043 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11044 }
11045 else if (attr_form_is_section_offset (attr))
11046 {
11047 dwarf2_complex_location_expr_complaint ();
11048 }
11049 else
11050 {
11051 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11052 fieldname);
11053 }
11054 }
11055 else
11056 {
11057 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11058 if (attr && DW_UNSND (attr))
11059 {
11060 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11061 complaint (&symfile_complaints,
11062 _("Member function \"%s\" (offset %d) is virtual "
11063 "but the vtable offset is not specified"),
11064 fieldname, die->offset.sect_off);
11065 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11066 TYPE_CPLUS_DYNAMIC (type) = 1;
11067 }
11068 }
11069 }
11070
11071 /* Create the vector of member function fields, and attach it to the type. */
11072
11073 static void
11074 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11075 struct dwarf2_cu *cu)
11076 {
11077 struct fnfieldlist *flp;
11078 int i;
11079
11080 if (cu->language == language_ada)
11081 error (_("unexpected member functions in Ada type"));
11082
11083 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11084 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11085 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11086
11087 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11088 {
11089 struct nextfnfield *nfp = flp->head;
11090 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11091 int k;
11092
11093 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11094 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11095 fn_flp->fn_fields = (struct fn_field *)
11096 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11097 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11098 fn_flp->fn_fields[k] = nfp->fnfield;
11099 }
11100
11101 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11102 }
11103
11104 /* Returns non-zero if NAME is the name of a vtable member in CU's
11105 language, zero otherwise. */
11106 static int
11107 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11108 {
11109 static const char vptr[] = "_vptr";
11110 static const char vtable[] = "vtable";
11111
11112 /* Look for the C++ and Java forms of the vtable. */
11113 if ((cu->language == language_java
11114 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11115 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11116 && is_cplus_marker (name[sizeof (vptr) - 1])))
11117 return 1;
11118
11119 return 0;
11120 }
11121
11122 /* GCC outputs unnamed structures that are really pointers to member
11123 functions, with the ABI-specified layout. If TYPE describes
11124 such a structure, smash it into a member function type.
11125
11126 GCC shouldn't do this; it should just output pointer to member DIEs.
11127 This is GCC PR debug/28767. */
11128
11129 static void
11130 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11131 {
11132 struct type *pfn_type, *domain_type, *new_type;
11133
11134 /* Check for a structure with no name and two children. */
11135 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11136 return;
11137
11138 /* Check for __pfn and __delta members. */
11139 if (TYPE_FIELD_NAME (type, 0) == NULL
11140 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11141 || TYPE_FIELD_NAME (type, 1) == NULL
11142 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11143 return;
11144
11145 /* Find the type of the method. */
11146 pfn_type = TYPE_FIELD_TYPE (type, 0);
11147 if (pfn_type == NULL
11148 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11149 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11150 return;
11151
11152 /* Look for the "this" argument. */
11153 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11154 if (TYPE_NFIELDS (pfn_type) == 0
11155 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11156 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11157 return;
11158
11159 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11160 new_type = alloc_type (objfile);
11161 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11162 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11163 TYPE_VARARGS (pfn_type));
11164 smash_to_methodptr_type (type, new_type);
11165 }
11166
11167 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11168 (icc). */
11169
11170 static int
11171 producer_is_icc (struct dwarf2_cu *cu)
11172 {
11173 if (!cu->checked_producer)
11174 check_producer (cu);
11175
11176 return cu->producer_is_icc;
11177 }
11178
11179 /* Called when we find the DIE that starts a structure or union scope
11180 (definition) to create a type for the structure or union. Fill in
11181 the type's name and general properties; the members will not be
11182 processed until process_structure_type.
11183
11184 NOTE: we need to call these functions regardless of whether or not the
11185 DIE has a DW_AT_name attribute, since it might be an anonymous
11186 structure or union. This gets the type entered into our set of
11187 user defined types.
11188
11189 However, if the structure is incomplete (an opaque struct/union)
11190 then suppress creating a symbol table entry for it since gdb only
11191 wants to find the one with the complete definition. Note that if
11192 it is complete, we just call new_symbol, which does it's own
11193 checking about whether the struct/union is anonymous or not (and
11194 suppresses creating a symbol table entry itself). */
11195
11196 static struct type *
11197 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11198 {
11199 struct objfile *objfile = cu->objfile;
11200 struct type *type;
11201 struct attribute *attr;
11202 const char *name;
11203
11204 /* If the definition of this type lives in .debug_types, read that type.
11205 Don't follow DW_AT_specification though, that will take us back up
11206 the chain and we want to go down. */
11207 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11208 if (attr)
11209 {
11210 struct dwarf2_cu *type_cu = cu;
11211 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11212
11213 /* We could just recurse on read_structure_type, but we need to call
11214 get_die_type to ensure only one type for this DIE is created.
11215 This is important, for example, because for c++ classes we need
11216 TYPE_NAME set which is only done by new_symbol. Blech. */
11217 type = read_type_die (type_die, type_cu);
11218
11219 /* TYPE_CU may not be the same as CU.
11220 Ensure TYPE is recorded in CU's type_hash table. */
11221 return set_die_type (die, type, cu);
11222 }
11223
11224 type = alloc_type (objfile);
11225 INIT_CPLUS_SPECIFIC (type);
11226
11227 name = dwarf2_name (die, cu);
11228 if (name != NULL)
11229 {
11230 if (cu->language == language_cplus
11231 || cu->language == language_java)
11232 {
11233 const char *full_name = dwarf2_full_name (name, die, cu);
11234
11235 /* dwarf2_full_name might have already finished building the DIE's
11236 type. If so, there is no need to continue. */
11237 if (get_die_type (die, cu) != NULL)
11238 return get_die_type (die, cu);
11239
11240 TYPE_TAG_NAME (type) = full_name;
11241 if (die->tag == DW_TAG_structure_type
11242 || die->tag == DW_TAG_class_type)
11243 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11244 }
11245 else
11246 {
11247 /* The name is already allocated along with this objfile, so
11248 we don't need to duplicate it for the type. */
11249 TYPE_TAG_NAME (type) = (char *) name;
11250 if (die->tag == DW_TAG_class_type)
11251 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11252 }
11253 }
11254
11255 if (die->tag == DW_TAG_structure_type)
11256 {
11257 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11258 }
11259 else if (die->tag == DW_TAG_union_type)
11260 {
11261 TYPE_CODE (type) = TYPE_CODE_UNION;
11262 }
11263 else
11264 {
11265 TYPE_CODE (type) = TYPE_CODE_CLASS;
11266 }
11267
11268 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11269 TYPE_DECLARED_CLASS (type) = 1;
11270
11271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11272 if (attr)
11273 {
11274 TYPE_LENGTH (type) = DW_UNSND (attr);
11275 }
11276 else
11277 {
11278 TYPE_LENGTH (type) = 0;
11279 }
11280
11281 if (producer_is_icc (cu))
11282 {
11283 /* ICC does not output the required DW_AT_declaration
11284 on incomplete types, but gives them a size of zero. */
11285 }
11286 else
11287 TYPE_STUB_SUPPORTED (type) = 1;
11288
11289 if (die_is_declaration (die, cu))
11290 TYPE_STUB (type) = 1;
11291 else if (attr == NULL && die->child == NULL
11292 && producer_is_realview (cu->producer))
11293 /* RealView does not output the required DW_AT_declaration
11294 on incomplete types. */
11295 TYPE_STUB (type) = 1;
11296
11297 /* We need to add the type field to the die immediately so we don't
11298 infinitely recurse when dealing with pointers to the structure
11299 type within the structure itself. */
11300 set_die_type (die, type, cu);
11301
11302 /* set_die_type should be already done. */
11303 set_descriptive_type (type, die, cu);
11304
11305 return type;
11306 }
11307
11308 /* Finish creating a structure or union type, including filling in
11309 its members and creating a symbol for it. */
11310
11311 static void
11312 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11313 {
11314 struct objfile *objfile = cu->objfile;
11315 struct die_info *child_die = die->child;
11316 struct type *type;
11317
11318 type = get_die_type (die, cu);
11319 if (type == NULL)
11320 type = read_structure_type (die, cu);
11321
11322 if (die->child != NULL && ! die_is_declaration (die, cu))
11323 {
11324 struct field_info fi;
11325 struct die_info *child_die;
11326 VEC (symbolp) *template_args = NULL;
11327 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11328
11329 memset (&fi, 0, sizeof (struct field_info));
11330
11331 child_die = die->child;
11332
11333 while (child_die && child_die->tag)
11334 {
11335 if (child_die->tag == DW_TAG_member
11336 || child_die->tag == DW_TAG_variable)
11337 {
11338 /* NOTE: carlton/2002-11-05: A C++ static data member
11339 should be a DW_TAG_member that is a declaration, but
11340 all versions of G++ as of this writing (so through at
11341 least 3.2.1) incorrectly generate DW_TAG_variable
11342 tags for them instead. */
11343 dwarf2_add_field (&fi, child_die, cu);
11344 }
11345 else if (child_die->tag == DW_TAG_subprogram)
11346 {
11347 /* C++ member function. */
11348 dwarf2_add_member_fn (&fi, child_die, type, cu);
11349 }
11350 else if (child_die->tag == DW_TAG_inheritance)
11351 {
11352 /* C++ base class field. */
11353 dwarf2_add_field (&fi, child_die, cu);
11354 }
11355 else if (child_die->tag == DW_TAG_typedef)
11356 dwarf2_add_typedef (&fi, child_die, cu);
11357 else if (child_die->tag == DW_TAG_template_type_param
11358 || child_die->tag == DW_TAG_template_value_param)
11359 {
11360 struct symbol *arg = new_symbol (child_die, NULL, cu);
11361
11362 if (arg != NULL)
11363 VEC_safe_push (symbolp, template_args, arg);
11364 }
11365
11366 child_die = sibling_die (child_die);
11367 }
11368
11369 /* Attach template arguments to type. */
11370 if (! VEC_empty (symbolp, template_args))
11371 {
11372 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11373 TYPE_N_TEMPLATE_ARGUMENTS (type)
11374 = VEC_length (symbolp, template_args);
11375 TYPE_TEMPLATE_ARGUMENTS (type)
11376 = obstack_alloc (&objfile->objfile_obstack,
11377 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11378 * sizeof (struct symbol *)));
11379 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11380 VEC_address (symbolp, template_args),
11381 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11382 * sizeof (struct symbol *)));
11383 VEC_free (symbolp, template_args);
11384 }
11385
11386 /* Attach fields and member functions to the type. */
11387 if (fi.nfields)
11388 dwarf2_attach_fields_to_type (&fi, type, cu);
11389 if (fi.nfnfields)
11390 {
11391 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11392
11393 /* Get the type which refers to the base class (possibly this
11394 class itself) which contains the vtable pointer for the current
11395 class from the DW_AT_containing_type attribute. This use of
11396 DW_AT_containing_type is a GNU extension. */
11397
11398 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11399 {
11400 struct type *t = die_containing_type (die, cu);
11401
11402 TYPE_VPTR_BASETYPE (type) = t;
11403 if (type == t)
11404 {
11405 int i;
11406
11407 /* Our own class provides vtbl ptr. */
11408 for (i = TYPE_NFIELDS (t) - 1;
11409 i >= TYPE_N_BASECLASSES (t);
11410 --i)
11411 {
11412 const char *fieldname = TYPE_FIELD_NAME (t, i);
11413
11414 if (is_vtable_name (fieldname, cu))
11415 {
11416 TYPE_VPTR_FIELDNO (type) = i;
11417 break;
11418 }
11419 }
11420
11421 /* Complain if virtual function table field not found. */
11422 if (i < TYPE_N_BASECLASSES (t))
11423 complaint (&symfile_complaints,
11424 _("virtual function table pointer "
11425 "not found when defining class '%s'"),
11426 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11427 "");
11428 }
11429 else
11430 {
11431 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11432 }
11433 }
11434 else if (cu->producer
11435 && strncmp (cu->producer,
11436 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11437 {
11438 /* The IBM XLC compiler does not provide direct indication
11439 of the containing type, but the vtable pointer is
11440 always named __vfp. */
11441
11442 int i;
11443
11444 for (i = TYPE_NFIELDS (type) - 1;
11445 i >= TYPE_N_BASECLASSES (type);
11446 --i)
11447 {
11448 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11449 {
11450 TYPE_VPTR_FIELDNO (type) = i;
11451 TYPE_VPTR_BASETYPE (type) = type;
11452 break;
11453 }
11454 }
11455 }
11456 }
11457
11458 /* Copy fi.typedef_field_list linked list elements content into the
11459 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11460 if (fi.typedef_field_list)
11461 {
11462 int i = fi.typedef_field_list_count;
11463
11464 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11465 TYPE_TYPEDEF_FIELD_ARRAY (type)
11466 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11467 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11468
11469 /* Reverse the list order to keep the debug info elements order. */
11470 while (--i >= 0)
11471 {
11472 struct typedef_field *dest, *src;
11473
11474 dest = &TYPE_TYPEDEF_FIELD (type, i);
11475 src = &fi.typedef_field_list->field;
11476 fi.typedef_field_list = fi.typedef_field_list->next;
11477 *dest = *src;
11478 }
11479 }
11480
11481 do_cleanups (back_to);
11482
11483 if (HAVE_CPLUS_STRUCT (type))
11484 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11485 }
11486
11487 quirk_gcc_member_function_pointer (type, objfile);
11488
11489 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11490 snapshots) has been known to create a die giving a declaration
11491 for a class that has, as a child, a die giving a definition for a
11492 nested class. So we have to process our children even if the
11493 current die is a declaration. Normally, of course, a declaration
11494 won't have any children at all. */
11495
11496 while (child_die != NULL && child_die->tag)
11497 {
11498 if (child_die->tag == DW_TAG_member
11499 || child_die->tag == DW_TAG_variable
11500 || child_die->tag == DW_TAG_inheritance
11501 || child_die->tag == DW_TAG_template_value_param
11502 || child_die->tag == DW_TAG_template_type_param)
11503 {
11504 /* Do nothing. */
11505 }
11506 else
11507 process_die (child_die, cu);
11508
11509 child_die = sibling_die (child_die);
11510 }
11511
11512 /* Do not consider external references. According to the DWARF standard,
11513 these DIEs are identified by the fact that they have no byte_size
11514 attribute, and a declaration attribute. */
11515 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11516 || !die_is_declaration (die, cu))
11517 new_symbol (die, type, cu);
11518 }
11519
11520 /* Given a DW_AT_enumeration_type die, set its type. We do not
11521 complete the type's fields yet, or create any symbols. */
11522
11523 static struct type *
11524 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11525 {
11526 struct objfile *objfile = cu->objfile;
11527 struct type *type;
11528 struct attribute *attr;
11529 const char *name;
11530
11531 /* If the definition of this type lives in .debug_types, read that type.
11532 Don't follow DW_AT_specification though, that will take us back up
11533 the chain and we want to go down. */
11534 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11535 if (attr)
11536 {
11537 struct dwarf2_cu *type_cu = cu;
11538 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11539
11540 type = read_type_die (type_die, type_cu);
11541
11542 /* TYPE_CU may not be the same as CU.
11543 Ensure TYPE is recorded in CU's type_hash table. */
11544 return set_die_type (die, type, cu);
11545 }
11546
11547 type = alloc_type (objfile);
11548
11549 TYPE_CODE (type) = TYPE_CODE_ENUM;
11550 name = dwarf2_full_name (NULL, die, cu);
11551 if (name != NULL)
11552 TYPE_TAG_NAME (type) = (char *) name;
11553
11554 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11555 if (attr)
11556 {
11557 TYPE_LENGTH (type) = DW_UNSND (attr);
11558 }
11559 else
11560 {
11561 TYPE_LENGTH (type) = 0;
11562 }
11563
11564 /* The enumeration DIE can be incomplete. In Ada, any type can be
11565 declared as private in the package spec, and then defined only
11566 inside the package body. Such types are known as Taft Amendment
11567 Types. When another package uses such a type, an incomplete DIE
11568 may be generated by the compiler. */
11569 if (die_is_declaration (die, cu))
11570 TYPE_STUB (type) = 1;
11571
11572 return set_die_type (die, type, cu);
11573 }
11574
11575 /* Given a pointer to a die which begins an enumeration, process all
11576 the dies that define the members of the enumeration, and create the
11577 symbol for the enumeration type.
11578
11579 NOTE: We reverse the order of the element list. */
11580
11581 static void
11582 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11583 {
11584 struct type *this_type;
11585
11586 this_type = get_die_type (die, cu);
11587 if (this_type == NULL)
11588 this_type = read_enumeration_type (die, cu);
11589
11590 if (die->child != NULL)
11591 {
11592 struct die_info *child_die;
11593 struct symbol *sym;
11594 struct field *fields = NULL;
11595 int num_fields = 0;
11596 int unsigned_enum = 1;
11597 const char *name;
11598 int flag_enum = 1;
11599 ULONGEST mask = 0;
11600
11601 child_die = die->child;
11602 while (child_die && child_die->tag)
11603 {
11604 if (child_die->tag != DW_TAG_enumerator)
11605 {
11606 process_die (child_die, cu);
11607 }
11608 else
11609 {
11610 name = dwarf2_name (child_die, cu);
11611 if (name)
11612 {
11613 sym = new_symbol (child_die, this_type, cu);
11614 if (SYMBOL_VALUE (sym) < 0)
11615 {
11616 unsigned_enum = 0;
11617 flag_enum = 0;
11618 }
11619 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11620 flag_enum = 0;
11621 else
11622 mask |= SYMBOL_VALUE (sym);
11623
11624 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11625 {
11626 fields = (struct field *)
11627 xrealloc (fields,
11628 (num_fields + DW_FIELD_ALLOC_CHUNK)
11629 * sizeof (struct field));
11630 }
11631
11632 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11633 FIELD_TYPE (fields[num_fields]) = NULL;
11634 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11635 FIELD_BITSIZE (fields[num_fields]) = 0;
11636
11637 num_fields++;
11638 }
11639 }
11640
11641 child_die = sibling_die (child_die);
11642 }
11643
11644 if (num_fields)
11645 {
11646 TYPE_NFIELDS (this_type) = num_fields;
11647 TYPE_FIELDS (this_type) = (struct field *)
11648 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11649 memcpy (TYPE_FIELDS (this_type), fields,
11650 sizeof (struct field) * num_fields);
11651 xfree (fields);
11652 }
11653 if (unsigned_enum)
11654 TYPE_UNSIGNED (this_type) = 1;
11655 if (flag_enum)
11656 TYPE_FLAG_ENUM (this_type) = 1;
11657 }
11658
11659 /* If we are reading an enum from a .debug_types unit, and the enum
11660 is a declaration, and the enum is not the signatured type in the
11661 unit, then we do not want to add a symbol for it. Adding a
11662 symbol would in some cases obscure the true definition of the
11663 enum, giving users an incomplete type when the definition is
11664 actually available. Note that we do not want to do this for all
11665 enums which are just declarations, because C++0x allows forward
11666 enum declarations. */
11667 if (cu->per_cu->is_debug_types
11668 && die_is_declaration (die, cu))
11669 {
11670 struct signatured_type *sig_type;
11671
11672 sig_type
11673 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11674 cu->per_cu->info_or_types_section,
11675 cu->per_cu->offset);
11676 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11677 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11678 return;
11679 }
11680
11681 new_symbol (die, this_type, cu);
11682 }
11683
11684 /* Extract all information from a DW_TAG_array_type DIE and put it in
11685 the DIE's type field. For now, this only handles one dimensional
11686 arrays. */
11687
11688 static struct type *
11689 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11690 {
11691 struct objfile *objfile = cu->objfile;
11692 struct die_info *child_die;
11693 struct type *type;
11694 struct type *element_type, *range_type, *index_type;
11695 struct type **range_types = NULL;
11696 struct attribute *attr;
11697 int ndim = 0;
11698 struct cleanup *back_to;
11699 const char *name;
11700
11701 element_type = die_type (die, cu);
11702
11703 /* The die_type call above may have already set the type for this DIE. */
11704 type = get_die_type (die, cu);
11705 if (type)
11706 return type;
11707
11708 /* Irix 6.2 native cc creates array types without children for
11709 arrays with unspecified length. */
11710 if (die->child == NULL)
11711 {
11712 index_type = objfile_type (objfile)->builtin_int;
11713 range_type = create_range_type (NULL, index_type, 0, -1);
11714 type = create_array_type (NULL, element_type, range_type);
11715 return set_die_type (die, type, cu);
11716 }
11717
11718 back_to = make_cleanup (null_cleanup, NULL);
11719 child_die = die->child;
11720 while (child_die && child_die->tag)
11721 {
11722 if (child_die->tag == DW_TAG_subrange_type)
11723 {
11724 struct type *child_type = read_type_die (child_die, cu);
11725
11726 if (child_type != NULL)
11727 {
11728 /* The range type was succesfully read. Save it for the
11729 array type creation. */
11730 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11731 {
11732 range_types = (struct type **)
11733 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11734 * sizeof (struct type *));
11735 if (ndim == 0)
11736 make_cleanup (free_current_contents, &range_types);
11737 }
11738 range_types[ndim++] = child_type;
11739 }
11740 }
11741 child_die = sibling_die (child_die);
11742 }
11743
11744 /* Dwarf2 dimensions are output from left to right, create the
11745 necessary array types in backwards order. */
11746
11747 type = element_type;
11748
11749 if (read_array_order (die, cu) == DW_ORD_col_major)
11750 {
11751 int i = 0;
11752
11753 while (i < ndim)
11754 type = create_array_type (NULL, type, range_types[i++]);
11755 }
11756 else
11757 {
11758 while (ndim-- > 0)
11759 type = create_array_type (NULL, type, range_types[ndim]);
11760 }
11761
11762 /* Understand Dwarf2 support for vector types (like they occur on
11763 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11764 array type. This is not part of the Dwarf2/3 standard yet, but a
11765 custom vendor extension. The main difference between a regular
11766 array and the vector variant is that vectors are passed by value
11767 to functions. */
11768 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11769 if (attr)
11770 make_vector_type (type);
11771
11772 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11773 implementation may choose to implement triple vectors using this
11774 attribute. */
11775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11776 if (attr)
11777 {
11778 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11779 TYPE_LENGTH (type) = DW_UNSND (attr);
11780 else
11781 complaint (&symfile_complaints,
11782 _("DW_AT_byte_size for array type smaller "
11783 "than the total size of elements"));
11784 }
11785
11786 name = dwarf2_name (die, cu);
11787 if (name)
11788 TYPE_NAME (type) = name;
11789
11790 /* Install the type in the die. */
11791 set_die_type (die, type, cu);
11792
11793 /* set_die_type should be already done. */
11794 set_descriptive_type (type, die, cu);
11795
11796 do_cleanups (back_to);
11797
11798 return type;
11799 }
11800
11801 static enum dwarf_array_dim_ordering
11802 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11803 {
11804 struct attribute *attr;
11805
11806 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11807
11808 if (attr) return DW_SND (attr);
11809
11810 /* GNU F77 is a special case, as at 08/2004 array type info is the
11811 opposite order to the dwarf2 specification, but data is still
11812 laid out as per normal fortran.
11813
11814 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11815 version checking. */
11816
11817 if (cu->language == language_fortran
11818 && cu->producer && strstr (cu->producer, "GNU F77"))
11819 {
11820 return DW_ORD_row_major;
11821 }
11822
11823 switch (cu->language_defn->la_array_ordering)
11824 {
11825 case array_column_major:
11826 return DW_ORD_col_major;
11827 case array_row_major:
11828 default:
11829 return DW_ORD_row_major;
11830 };
11831 }
11832
11833 /* Extract all information from a DW_TAG_set_type DIE and put it in
11834 the DIE's type field. */
11835
11836 static struct type *
11837 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11838 {
11839 struct type *domain_type, *set_type;
11840 struct attribute *attr;
11841
11842 domain_type = die_type (die, cu);
11843
11844 /* The die_type call above may have already set the type for this DIE. */
11845 set_type = get_die_type (die, cu);
11846 if (set_type)
11847 return set_type;
11848
11849 set_type = create_set_type (NULL, domain_type);
11850
11851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11852 if (attr)
11853 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11854
11855 return set_die_type (die, set_type, cu);
11856 }
11857
11858 /* A helper for read_common_block that creates a locexpr baton.
11859 SYM is the symbol which we are marking as computed.
11860 COMMON_DIE is the DIE for the common block.
11861 COMMON_LOC is the location expression attribute for the common
11862 block itself.
11863 MEMBER_LOC is the location expression attribute for the particular
11864 member of the common block that we are processing.
11865 CU is the CU from which the above come. */
11866
11867 static void
11868 mark_common_block_symbol_computed (struct symbol *sym,
11869 struct die_info *common_die,
11870 struct attribute *common_loc,
11871 struct attribute *member_loc,
11872 struct dwarf2_cu *cu)
11873 {
11874 struct objfile *objfile = dwarf2_per_objfile->objfile;
11875 struct dwarf2_locexpr_baton *baton;
11876 gdb_byte *ptr;
11877 unsigned int cu_off;
11878 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11879 LONGEST offset = 0;
11880
11881 gdb_assert (common_loc && member_loc);
11882 gdb_assert (attr_form_is_block (common_loc));
11883 gdb_assert (attr_form_is_block (member_loc)
11884 || attr_form_is_constant (member_loc));
11885
11886 baton = obstack_alloc (&objfile->objfile_obstack,
11887 sizeof (struct dwarf2_locexpr_baton));
11888 baton->per_cu = cu->per_cu;
11889 gdb_assert (baton->per_cu);
11890
11891 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11892
11893 if (attr_form_is_constant (member_loc))
11894 {
11895 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11896 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11897 }
11898 else
11899 baton->size += DW_BLOCK (member_loc)->size;
11900
11901 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11902 baton->data = ptr;
11903
11904 *ptr++ = DW_OP_call4;
11905 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11906 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11907 ptr += 4;
11908
11909 if (attr_form_is_constant (member_loc))
11910 {
11911 *ptr++ = DW_OP_addr;
11912 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11913 ptr += cu->header.addr_size;
11914 }
11915 else
11916 {
11917 /* We have to copy the data here, because DW_OP_call4 will only
11918 use a DW_AT_location attribute. */
11919 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11920 ptr += DW_BLOCK (member_loc)->size;
11921 }
11922
11923 *ptr++ = DW_OP_plus;
11924 gdb_assert (ptr - baton->data == baton->size);
11925
11926 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11927 SYMBOL_LOCATION_BATON (sym) = baton;
11928 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11929 }
11930
11931 /* Create appropriate locally-scoped variables for all the
11932 DW_TAG_common_block entries. Also create a struct common_block
11933 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11934 is used to sepate the common blocks name namespace from regular
11935 variable names. */
11936
11937 static void
11938 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11939 {
11940 struct attribute *attr;
11941
11942 attr = dwarf2_attr (die, DW_AT_location, cu);
11943 if (attr)
11944 {
11945 /* Support the .debug_loc offsets. */
11946 if (attr_form_is_block (attr))
11947 {
11948 /* Ok. */
11949 }
11950 else if (attr_form_is_section_offset (attr))
11951 {
11952 dwarf2_complex_location_expr_complaint ();
11953 attr = NULL;
11954 }
11955 else
11956 {
11957 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11958 "common block member");
11959 attr = NULL;
11960 }
11961 }
11962
11963 if (die->child != NULL)
11964 {
11965 struct objfile *objfile = cu->objfile;
11966 struct die_info *child_die;
11967 size_t n_entries = 0, size;
11968 struct common_block *common_block;
11969 struct symbol *sym;
11970
11971 for (child_die = die->child;
11972 child_die && child_die->tag;
11973 child_die = sibling_die (child_die))
11974 ++n_entries;
11975
11976 size = (sizeof (struct common_block)
11977 + (n_entries - 1) * sizeof (struct symbol *));
11978 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11979 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11980 common_block->n_entries = 0;
11981
11982 for (child_die = die->child;
11983 child_die && child_die->tag;
11984 child_die = sibling_die (child_die))
11985 {
11986 /* Create the symbol in the DW_TAG_common_block block in the current
11987 symbol scope. */
11988 sym = new_symbol (child_die, NULL, cu);
11989 if (sym != NULL)
11990 {
11991 struct attribute *member_loc;
11992
11993 common_block->contents[common_block->n_entries++] = sym;
11994
11995 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11996 cu);
11997 if (member_loc)
11998 {
11999 /* GDB has handled this for a long time, but it is
12000 not specified by DWARF. It seems to have been
12001 emitted by gfortran at least as recently as:
12002 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12003 complaint (&symfile_complaints,
12004 _("Variable in common block has "
12005 "DW_AT_data_member_location "
12006 "- DIE at 0x%x [in module %s]"),
12007 child_die->offset.sect_off, cu->objfile->name);
12008
12009 if (attr_form_is_section_offset (member_loc))
12010 dwarf2_complex_location_expr_complaint ();
12011 else if (attr_form_is_constant (member_loc)
12012 || attr_form_is_block (member_loc))
12013 {
12014 if (attr)
12015 mark_common_block_symbol_computed (sym, die, attr,
12016 member_loc, cu);
12017 }
12018 else
12019 dwarf2_complex_location_expr_complaint ();
12020 }
12021 }
12022 }
12023
12024 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12025 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12026 }
12027 }
12028
12029 /* Create a type for a C++ namespace. */
12030
12031 static struct type *
12032 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12033 {
12034 struct objfile *objfile = cu->objfile;
12035 const char *previous_prefix, *name;
12036 int is_anonymous;
12037 struct type *type;
12038
12039 /* For extensions, reuse the type of the original namespace. */
12040 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12041 {
12042 struct die_info *ext_die;
12043 struct dwarf2_cu *ext_cu = cu;
12044
12045 ext_die = dwarf2_extension (die, &ext_cu);
12046 type = read_type_die (ext_die, ext_cu);
12047
12048 /* EXT_CU may not be the same as CU.
12049 Ensure TYPE is recorded in CU's type_hash table. */
12050 return set_die_type (die, type, cu);
12051 }
12052
12053 name = namespace_name (die, &is_anonymous, cu);
12054
12055 /* Now build the name of the current namespace. */
12056
12057 previous_prefix = determine_prefix (die, cu);
12058 if (previous_prefix[0] != '\0')
12059 name = typename_concat (&objfile->objfile_obstack,
12060 previous_prefix, name, 0, cu);
12061
12062 /* Create the type. */
12063 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12064 objfile);
12065 TYPE_NAME (type) = (char *) name;
12066 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12067
12068 return set_die_type (die, type, cu);
12069 }
12070
12071 /* Read a C++ namespace. */
12072
12073 static void
12074 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12075 {
12076 struct objfile *objfile = cu->objfile;
12077 int is_anonymous;
12078
12079 /* Add a symbol associated to this if we haven't seen the namespace
12080 before. Also, add a using directive if it's an anonymous
12081 namespace. */
12082
12083 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12084 {
12085 struct type *type;
12086
12087 type = read_type_die (die, cu);
12088 new_symbol (die, type, cu);
12089
12090 namespace_name (die, &is_anonymous, cu);
12091 if (is_anonymous)
12092 {
12093 const char *previous_prefix = determine_prefix (die, cu);
12094
12095 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12096 NULL, NULL, &objfile->objfile_obstack);
12097 }
12098 }
12099
12100 if (die->child != NULL)
12101 {
12102 struct die_info *child_die = die->child;
12103
12104 while (child_die && child_die->tag)
12105 {
12106 process_die (child_die, cu);
12107 child_die = sibling_die (child_die);
12108 }
12109 }
12110 }
12111
12112 /* Read a Fortran module as type. This DIE can be only a declaration used for
12113 imported module. Still we need that type as local Fortran "use ... only"
12114 declaration imports depend on the created type in determine_prefix. */
12115
12116 static struct type *
12117 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12118 {
12119 struct objfile *objfile = cu->objfile;
12120 const char *module_name;
12121 struct type *type;
12122
12123 module_name = dwarf2_name (die, cu);
12124 if (!module_name)
12125 complaint (&symfile_complaints,
12126 _("DW_TAG_module has no name, offset 0x%x"),
12127 die->offset.sect_off);
12128 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12129
12130 /* determine_prefix uses TYPE_TAG_NAME. */
12131 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12132
12133 return set_die_type (die, type, cu);
12134 }
12135
12136 /* Read a Fortran module. */
12137
12138 static void
12139 read_module (struct die_info *die, struct dwarf2_cu *cu)
12140 {
12141 struct die_info *child_die = die->child;
12142
12143 while (child_die && child_die->tag)
12144 {
12145 process_die (child_die, cu);
12146 child_die = sibling_die (child_die);
12147 }
12148 }
12149
12150 /* Return the name of the namespace represented by DIE. Set
12151 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12152 namespace. */
12153
12154 static const char *
12155 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12156 {
12157 struct die_info *current_die;
12158 const char *name = NULL;
12159
12160 /* Loop through the extensions until we find a name. */
12161
12162 for (current_die = die;
12163 current_die != NULL;
12164 current_die = dwarf2_extension (die, &cu))
12165 {
12166 name = dwarf2_name (current_die, cu);
12167 if (name != NULL)
12168 break;
12169 }
12170
12171 /* Is it an anonymous namespace? */
12172
12173 *is_anonymous = (name == NULL);
12174 if (*is_anonymous)
12175 name = CP_ANONYMOUS_NAMESPACE_STR;
12176
12177 return name;
12178 }
12179
12180 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12181 the user defined type vector. */
12182
12183 static struct type *
12184 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12185 {
12186 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12187 struct comp_unit_head *cu_header = &cu->header;
12188 struct type *type;
12189 struct attribute *attr_byte_size;
12190 struct attribute *attr_address_class;
12191 int byte_size, addr_class;
12192 struct type *target_type;
12193
12194 target_type = die_type (die, cu);
12195
12196 /* The die_type call above may have already set the type for this DIE. */
12197 type = get_die_type (die, cu);
12198 if (type)
12199 return type;
12200
12201 type = lookup_pointer_type (target_type);
12202
12203 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12204 if (attr_byte_size)
12205 byte_size = DW_UNSND (attr_byte_size);
12206 else
12207 byte_size = cu_header->addr_size;
12208
12209 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12210 if (attr_address_class)
12211 addr_class = DW_UNSND (attr_address_class);
12212 else
12213 addr_class = DW_ADDR_none;
12214
12215 /* If the pointer size or address class is different than the
12216 default, create a type variant marked as such and set the
12217 length accordingly. */
12218 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12219 {
12220 if (gdbarch_address_class_type_flags_p (gdbarch))
12221 {
12222 int type_flags;
12223
12224 type_flags = gdbarch_address_class_type_flags
12225 (gdbarch, byte_size, addr_class);
12226 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12227 == 0);
12228 type = make_type_with_address_space (type, type_flags);
12229 }
12230 else if (TYPE_LENGTH (type) != byte_size)
12231 {
12232 complaint (&symfile_complaints,
12233 _("invalid pointer size %d"), byte_size);
12234 }
12235 else
12236 {
12237 /* Should we also complain about unhandled address classes? */
12238 }
12239 }
12240
12241 TYPE_LENGTH (type) = byte_size;
12242 return set_die_type (die, type, cu);
12243 }
12244
12245 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12246 the user defined type vector. */
12247
12248 static struct type *
12249 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12250 {
12251 struct type *type;
12252 struct type *to_type;
12253 struct type *domain;
12254
12255 to_type = die_type (die, cu);
12256 domain = die_containing_type (die, cu);
12257
12258 /* The calls above may have already set the type for this DIE. */
12259 type = get_die_type (die, cu);
12260 if (type)
12261 return type;
12262
12263 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12264 type = lookup_methodptr_type (to_type);
12265 else
12266 type = lookup_memberptr_type (to_type, domain);
12267
12268 return set_die_type (die, type, cu);
12269 }
12270
12271 /* Extract all information from a DW_TAG_reference_type DIE and add to
12272 the user defined type vector. */
12273
12274 static struct type *
12275 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12276 {
12277 struct comp_unit_head *cu_header = &cu->header;
12278 struct type *type, *target_type;
12279 struct attribute *attr;
12280
12281 target_type = die_type (die, cu);
12282
12283 /* The die_type call above may have already set the type for this DIE. */
12284 type = get_die_type (die, cu);
12285 if (type)
12286 return type;
12287
12288 type = lookup_reference_type (target_type);
12289 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12290 if (attr)
12291 {
12292 TYPE_LENGTH (type) = DW_UNSND (attr);
12293 }
12294 else
12295 {
12296 TYPE_LENGTH (type) = cu_header->addr_size;
12297 }
12298 return set_die_type (die, type, cu);
12299 }
12300
12301 static struct type *
12302 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12303 {
12304 struct type *base_type, *cv_type;
12305
12306 base_type = die_type (die, cu);
12307
12308 /* The die_type call above may have already set the type for this DIE. */
12309 cv_type = get_die_type (die, cu);
12310 if (cv_type)
12311 return cv_type;
12312
12313 /* In case the const qualifier is applied to an array type, the element type
12314 is so qualified, not the array type (section 6.7.3 of C99). */
12315 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12316 {
12317 struct type *el_type, *inner_array;
12318
12319 base_type = copy_type (base_type);
12320 inner_array = base_type;
12321
12322 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12323 {
12324 TYPE_TARGET_TYPE (inner_array) =
12325 copy_type (TYPE_TARGET_TYPE (inner_array));
12326 inner_array = TYPE_TARGET_TYPE (inner_array);
12327 }
12328
12329 el_type = TYPE_TARGET_TYPE (inner_array);
12330 TYPE_TARGET_TYPE (inner_array) =
12331 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12332
12333 return set_die_type (die, base_type, cu);
12334 }
12335
12336 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12337 return set_die_type (die, cv_type, cu);
12338 }
12339
12340 static struct type *
12341 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12342 {
12343 struct type *base_type, *cv_type;
12344
12345 base_type = die_type (die, cu);
12346
12347 /* The die_type call above may have already set the type for this DIE. */
12348 cv_type = get_die_type (die, cu);
12349 if (cv_type)
12350 return cv_type;
12351
12352 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12353 return set_die_type (die, cv_type, cu);
12354 }
12355
12356 /* Handle DW_TAG_restrict_type. */
12357
12358 static struct type *
12359 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12360 {
12361 struct type *base_type, *cv_type;
12362
12363 base_type = die_type (die, cu);
12364
12365 /* The die_type call above may have already set the type for this DIE. */
12366 cv_type = get_die_type (die, cu);
12367 if (cv_type)
12368 return cv_type;
12369
12370 cv_type = make_restrict_type (base_type);
12371 return set_die_type (die, cv_type, cu);
12372 }
12373
12374 /* Extract all information from a DW_TAG_string_type DIE and add to
12375 the user defined type vector. It isn't really a user defined type,
12376 but it behaves like one, with other DIE's using an AT_user_def_type
12377 attribute to reference it. */
12378
12379 static struct type *
12380 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12381 {
12382 struct objfile *objfile = cu->objfile;
12383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12384 struct type *type, *range_type, *index_type, *char_type;
12385 struct attribute *attr;
12386 unsigned int length;
12387
12388 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12389 if (attr)
12390 {
12391 length = DW_UNSND (attr);
12392 }
12393 else
12394 {
12395 /* Check for the DW_AT_byte_size attribute. */
12396 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12397 if (attr)
12398 {
12399 length = DW_UNSND (attr);
12400 }
12401 else
12402 {
12403 length = 1;
12404 }
12405 }
12406
12407 index_type = objfile_type (objfile)->builtin_int;
12408 range_type = create_range_type (NULL, index_type, 1, length);
12409 char_type = language_string_char_type (cu->language_defn, gdbarch);
12410 type = create_string_type (NULL, char_type, range_type);
12411
12412 return set_die_type (die, type, cu);
12413 }
12414
12415 /* Handle DIES due to C code like:
12416
12417 struct foo
12418 {
12419 int (*funcp)(int a, long l);
12420 int b;
12421 };
12422
12423 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12424
12425 static struct type *
12426 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12427 {
12428 struct objfile *objfile = cu->objfile;
12429 struct type *type; /* Type that this function returns. */
12430 struct type *ftype; /* Function that returns above type. */
12431 struct attribute *attr;
12432
12433 type = die_type (die, cu);
12434
12435 /* The die_type call above may have already set the type for this DIE. */
12436 ftype = get_die_type (die, cu);
12437 if (ftype)
12438 return ftype;
12439
12440 ftype = lookup_function_type (type);
12441
12442 /* All functions in C++, Pascal and Java have prototypes. */
12443 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12444 if ((attr && (DW_UNSND (attr) != 0))
12445 || cu->language == language_cplus
12446 || cu->language == language_java
12447 || cu->language == language_pascal)
12448 TYPE_PROTOTYPED (ftype) = 1;
12449 else if (producer_is_realview (cu->producer))
12450 /* RealView does not emit DW_AT_prototyped. We can not
12451 distinguish prototyped and unprototyped functions; default to
12452 prototyped, since that is more common in modern code (and
12453 RealView warns about unprototyped functions). */
12454 TYPE_PROTOTYPED (ftype) = 1;
12455
12456 /* Store the calling convention in the type if it's available in
12457 the subroutine die. Otherwise set the calling convention to
12458 the default value DW_CC_normal. */
12459 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12460 if (attr)
12461 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12462 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12463 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12464 else
12465 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12466
12467 /* We need to add the subroutine type to the die immediately so
12468 we don't infinitely recurse when dealing with parameters
12469 declared as the same subroutine type. */
12470 set_die_type (die, ftype, cu);
12471
12472 if (die->child != NULL)
12473 {
12474 struct type *void_type = objfile_type (objfile)->builtin_void;
12475 struct die_info *child_die;
12476 int nparams, iparams;
12477
12478 /* Count the number of parameters.
12479 FIXME: GDB currently ignores vararg functions, but knows about
12480 vararg member functions. */
12481 nparams = 0;
12482 child_die = die->child;
12483 while (child_die && child_die->tag)
12484 {
12485 if (child_die->tag == DW_TAG_formal_parameter)
12486 nparams++;
12487 else if (child_die->tag == DW_TAG_unspecified_parameters)
12488 TYPE_VARARGS (ftype) = 1;
12489 child_die = sibling_die (child_die);
12490 }
12491
12492 /* Allocate storage for parameters and fill them in. */
12493 TYPE_NFIELDS (ftype) = nparams;
12494 TYPE_FIELDS (ftype) = (struct field *)
12495 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12496
12497 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12498 even if we error out during the parameters reading below. */
12499 for (iparams = 0; iparams < nparams; iparams++)
12500 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12501
12502 iparams = 0;
12503 child_die = die->child;
12504 while (child_die && child_die->tag)
12505 {
12506 if (child_die->tag == DW_TAG_formal_parameter)
12507 {
12508 struct type *arg_type;
12509
12510 /* DWARF version 2 has no clean way to discern C++
12511 static and non-static member functions. G++ helps
12512 GDB by marking the first parameter for non-static
12513 member functions (which is the this pointer) as
12514 artificial. We pass this information to
12515 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12516
12517 DWARF version 3 added DW_AT_object_pointer, which GCC
12518 4.5 does not yet generate. */
12519 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12520 if (attr)
12521 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12522 else
12523 {
12524 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12525
12526 /* GCC/43521: In java, the formal parameter
12527 "this" is sometimes not marked with DW_AT_artificial. */
12528 if (cu->language == language_java)
12529 {
12530 const char *name = dwarf2_name (child_die, cu);
12531
12532 if (name && !strcmp (name, "this"))
12533 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12534 }
12535 }
12536 arg_type = die_type (child_die, cu);
12537
12538 /* RealView does not mark THIS as const, which the testsuite
12539 expects. GCC marks THIS as const in method definitions,
12540 but not in the class specifications (GCC PR 43053). */
12541 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12542 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12543 {
12544 int is_this = 0;
12545 struct dwarf2_cu *arg_cu = cu;
12546 const char *name = dwarf2_name (child_die, cu);
12547
12548 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12549 if (attr)
12550 {
12551 /* If the compiler emits this, use it. */
12552 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12553 is_this = 1;
12554 }
12555 else if (name && strcmp (name, "this") == 0)
12556 /* Function definitions will have the argument names. */
12557 is_this = 1;
12558 else if (name == NULL && iparams == 0)
12559 /* Declarations may not have the names, so like
12560 elsewhere in GDB, assume an artificial first
12561 argument is "this". */
12562 is_this = 1;
12563
12564 if (is_this)
12565 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12566 arg_type, 0);
12567 }
12568
12569 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12570 iparams++;
12571 }
12572 child_die = sibling_die (child_die);
12573 }
12574 }
12575
12576 return ftype;
12577 }
12578
12579 static struct type *
12580 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12581 {
12582 struct objfile *objfile = cu->objfile;
12583 const char *name = NULL;
12584 struct type *this_type, *target_type;
12585
12586 name = dwarf2_full_name (NULL, die, cu);
12587 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12588 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12589 TYPE_NAME (this_type) = (char *) name;
12590 set_die_type (die, this_type, cu);
12591 target_type = die_type (die, cu);
12592 if (target_type != this_type)
12593 TYPE_TARGET_TYPE (this_type) = target_type;
12594 else
12595 {
12596 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12597 spec and cause infinite loops in GDB. */
12598 complaint (&symfile_complaints,
12599 _("Self-referential DW_TAG_typedef "
12600 "- DIE at 0x%x [in module %s]"),
12601 die->offset.sect_off, objfile->name);
12602 TYPE_TARGET_TYPE (this_type) = NULL;
12603 }
12604 return this_type;
12605 }
12606
12607 /* Find a representation of a given base type and install
12608 it in the TYPE field of the die. */
12609
12610 static struct type *
12611 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12612 {
12613 struct objfile *objfile = cu->objfile;
12614 struct type *type;
12615 struct attribute *attr;
12616 int encoding = 0, size = 0;
12617 const char *name;
12618 enum type_code code = TYPE_CODE_INT;
12619 int type_flags = 0;
12620 struct type *target_type = NULL;
12621
12622 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12623 if (attr)
12624 {
12625 encoding = DW_UNSND (attr);
12626 }
12627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12628 if (attr)
12629 {
12630 size = DW_UNSND (attr);
12631 }
12632 name = dwarf2_name (die, cu);
12633 if (!name)
12634 {
12635 complaint (&symfile_complaints,
12636 _("DW_AT_name missing from DW_TAG_base_type"));
12637 }
12638
12639 switch (encoding)
12640 {
12641 case DW_ATE_address:
12642 /* Turn DW_ATE_address into a void * pointer. */
12643 code = TYPE_CODE_PTR;
12644 type_flags |= TYPE_FLAG_UNSIGNED;
12645 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12646 break;
12647 case DW_ATE_boolean:
12648 code = TYPE_CODE_BOOL;
12649 type_flags |= TYPE_FLAG_UNSIGNED;
12650 break;
12651 case DW_ATE_complex_float:
12652 code = TYPE_CODE_COMPLEX;
12653 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12654 break;
12655 case DW_ATE_decimal_float:
12656 code = TYPE_CODE_DECFLOAT;
12657 break;
12658 case DW_ATE_float:
12659 code = TYPE_CODE_FLT;
12660 break;
12661 case DW_ATE_signed:
12662 break;
12663 case DW_ATE_unsigned:
12664 type_flags |= TYPE_FLAG_UNSIGNED;
12665 if (cu->language == language_fortran
12666 && name
12667 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12668 code = TYPE_CODE_CHAR;
12669 break;
12670 case DW_ATE_signed_char:
12671 if (cu->language == language_ada || cu->language == language_m2
12672 || cu->language == language_pascal
12673 || cu->language == language_fortran)
12674 code = TYPE_CODE_CHAR;
12675 break;
12676 case DW_ATE_unsigned_char:
12677 if (cu->language == language_ada || cu->language == language_m2
12678 || cu->language == language_pascal
12679 || cu->language == language_fortran)
12680 code = TYPE_CODE_CHAR;
12681 type_flags |= TYPE_FLAG_UNSIGNED;
12682 break;
12683 case DW_ATE_UTF:
12684 /* We just treat this as an integer and then recognize the
12685 type by name elsewhere. */
12686 break;
12687
12688 default:
12689 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12690 dwarf_type_encoding_name (encoding));
12691 break;
12692 }
12693
12694 type = init_type (code, size, type_flags, NULL, objfile);
12695 TYPE_NAME (type) = name;
12696 TYPE_TARGET_TYPE (type) = target_type;
12697
12698 if (name && strcmp (name, "char") == 0)
12699 TYPE_NOSIGN (type) = 1;
12700
12701 return set_die_type (die, type, cu);
12702 }
12703
12704 /* Read the given DW_AT_subrange DIE. */
12705
12706 static struct type *
12707 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12708 {
12709 struct type *base_type;
12710 struct type *range_type;
12711 struct attribute *attr;
12712 LONGEST low, high;
12713 int low_default_is_valid;
12714 const char *name;
12715 LONGEST negative_mask;
12716
12717 base_type = die_type (die, cu);
12718 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12719 check_typedef (base_type);
12720
12721 /* The die_type call above may have already set the type for this DIE. */
12722 range_type = get_die_type (die, cu);
12723 if (range_type)
12724 return range_type;
12725
12726 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12727 omitting DW_AT_lower_bound. */
12728 switch (cu->language)
12729 {
12730 case language_c:
12731 case language_cplus:
12732 low = 0;
12733 low_default_is_valid = 1;
12734 break;
12735 case language_fortran:
12736 low = 1;
12737 low_default_is_valid = 1;
12738 break;
12739 case language_d:
12740 case language_java:
12741 case language_objc:
12742 low = 0;
12743 low_default_is_valid = (cu->header.version >= 4);
12744 break;
12745 case language_ada:
12746 case language_m2:
12747 case language_pascal:
12748 low = 1;
12749 low_default_is_valid = (cu->header.version >= 4);
12750 break;
12751 default:
12752 low = 0;
12753 low_default_is_valid = 0;
12754 break;
12755 }
12756
12757 /* FIXME: For variable sized arrays either of these could be
12758 a variable rather than a constant value. We'll allow it,
12759 but we don't know how to handle it. */
12760 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12761 if (attr)
12762 low = dwarf2_get_attr_constant_value (attr, low);
12763 else if (!low_default_is_valid)
12764 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12765 "- DIE at 0x%x [in module %s]"),
12766 die->offset.sect_off, cu->objfile->name);
12767
12768 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12769 if (attr)
12770 {
12771 if (attr_form_is_block (attr) || is_ref_attr (attr))
12772 {
12773 /* GCC encodes arrays with unspecified or dynamic length
12774 with a DW_FORM_block1 attribute or a reference attribute.
12775 FIXME: GDB does not yet know how to handle dynamic
12776 arrays properly, treat them as arrays with unspecified
12777 length for now.
12778
12779 FIXME: jimb/2003-09-22: GDB does not really know
12780 how to handle arrays of unspecified length
12781 either; we just represent them as zero-length
12782 arrays. Choose an appropriate upper bound given
12783 the lower bound we've computed above. */
12784 high = low - 1;
12785 }
12786 else
12787 high = dwarf2_get_attr_constant_value (attr, 1);
12788 }
12789 else
12790 {
12791 attr = dwarf2_attr (die, DW_AT_count, cu);
12792 if (attr)
12793 {
12794 int count = dwarf2_get_attr_constant_value (attr, 1);
12795 high = low + count - 1;
12796 }
12797 else
12798 {
12799 /* Unspecified array length. */
12800 high = low - 1;
12801 }
12802 }
12803
12804 /* Dwarf-2 specifications explicitly allows to create subrange types
12805 without specifying a base type.
12806 In that case, the base type must be set to the type of
12807 the lower bound, upper bound or count, in that order, if any of these
12808 three attributes references an object that has a type.
12809 If no base type is found, the Dwarf-2 specifications say that
12810 a signed integer type of size equal to the size of an address should
12811 be used.
12812 For the following C code: `extern char gdb_int [];'
12813 GCC produces an empty range DIE.
12814 FIXME: muller/2010-05-28: Possible references to object for low bound,
12815 high bound or count are not yet handled by this code. */
12816 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12817 {
12818 struct objfile *objfile = cu->objfile;
12819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12820 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12821 struct type *int_type = objfile_type (objfile)->builtin_int;
12822
12823 /* Test "int", "long int", and "long long int" objfile types,
12824 and select the first one having a size above or equal to the
12825 architecture address size. */
12826 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12827 base_type = int_type;
12828 else
12829 {
12830 int_type = objfile_type (objfile)->builtin_long;
12831 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12832 base_type = int_type;
12833 else
12834 {
12835 int_type = objfile_type (objfile)->builtin_long_long;
12836 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12837 base_type = int_type;
12838 }
12839 }
12840 }
12841
12842 negative_mask =
12843 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12844 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12845 low |= negative_mask;
12846 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12847 high |= negative_mask;
12848
12849 range_type = create_range_type (NULL, base_type, low, high);
12850
12851 /* Mark arrays with dynamic length at least as an array of unspecified
12852 length. GDB could check the boundary but before it gets implemented at
12853 least allow accessing the array elements. */
12854 if (attr && attr_form_is_block (attr))
12855 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12856
12857 /* Ada expects an empty array on no boundary attributes. */
12858 if (attr == NULL && cu->language != language_ada)
12859 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12860
12861 name = dwarf2_name (die, cu);
12862 if (name)
12863 TYPE_NAME (range_type) = name;
12864
12865 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12866 if (attr)
12867 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12868
12869 set_die_type (die, range_type, cu);
12870
12871 /* set_die_type should be already done. */
12872 set_descriptive_type (range_type, die, cu);
12873
12874 return range_type;
12875 }
12876
12877 static struct type *
12878 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12879 {
12880 struct type *type;
12881
12882 /* For now, we only support the C meaning of an unspecified type: void. */
12883
12884 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12885 TYPE_NAME (type) = dwarf2_name (die, cu);
12886
12887 return set_die_type (die, type, cu);
12888 }
12889
12890 /* Read a single die and all its descendents. Set the die's sibling
12891 field to NULL; set other fields in the die correctly, and set all
12892 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12893 location of the info_ptr after reading all of those dies. PARENT
12894 is the parent of the die in question. */
12895
12896 static struct die_info *
12897 read_die_and_children (const struct die_reader_specs *reader,
12898 gdb_byte *info_ptr,
12899 gdb_byte **new_info_ptr,
12900 struct die_info *parent)
12901 {
12902 struct die_info *die;
12903 gdb_byte *cur_ptr;
12904 int has_children;
12905
12906 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12907 if (die == NULL)
12908 {
12909 *new_info_ptr = cur_ptr;
12910 return NULL;
12911 }
12912 store_in_ref_table (die, reader->cu);
12913
12914 if (has_children)
12915 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12916 else
12917 {
12918 die->child = NULL;
12919 *new_info_ptr = cur_ptr;
12920 }
12921
12922 die->sibling = NULL;
12923 die->parent = parent;
12924 return die;
12925 }
12926
12927 /* Read a die, all of its descendents, and all of its siblings; set
12928 all of the fields of all of the dies correctly. Arguments are as
12929 in read_die_and_children. */
12930
12931 static struct die_info *
12932 read_die_and_siblings (const struct die_reader_specs *reader,
12933 gdb_byte *info_ptr,
12934 gdb_byte **new_info_ptr,
12935 struct die_info *parent)
12936 {
12937 struct die_info *first_die, *last_sibling;
12938 gdb_byte *cur_ptr;
12939
12940 cur_ptr = info_ptr;
12941 first_die = last_sibling = NULL;
12942
12943 while (1)
12944 {
12945 struct die_info *die
12946 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12947
12948 if (die == NULL)
12949 {
12950 *new_info_ptr = cur_ptr;
12951 return first_die;
12952 }
12953
12954 if (!first_die)
12955 first_die = die;
12956 else
12957 last_sibling->sibling = die;
12958
12959 last_sibling = die;
12960 }
12961 }
12962
12963 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12964 attributes.
12965 The caller is responsible for filling in the extra attributes
12966 and updating (*DIEP)->num_attrs.
12967 Set DIEP to point to a newly allocated die with its information,
12968 except for its child, sibling, and parent fields.
12969 Set HAS_CHILDREN to tell whether the die has children or not. */
12970
12971 static gdb_byte *
12972 read_full_die_1 (const struct die_reader_specs *reader,
12973 struct die_info **diep, gdb_byte *info_ptr,
12974 int *has_children, int num_extra_attrs)
12975 {
12976 unsigned int abbrev_number, bytes_read, i;
12977 sect_offset offset;
12978 struct abbrev_info *abbrev;
12979 struct die_info *die;
12980 struct dwarf2_cu *cu = reader->cu;
12981 bfd *abfd = reader->abfd;
12982
12983 offset.sect_off = info_ptr - reader->buffer;
12984 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12985 info_ptr += bytes_read;
12986 if (!abbrev_number)
12987 {
12988 *diep = NULL;
12989 *has_children = 0;
12990 return info_ptr;
12991 }
12992
12993 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12994 if (!abbrev)
12995 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12996 abbrev_number,
12997 bfd_get_filename (abfd));
12998
12999 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13000 die->offset = offset;
13001 die->tag = abbrev->tag;
13002 die->abbrev = abbrev_number;
13003
13004 /* Make the result usable.
13005 The caller needs to update num_attrs after adding the extra
13006 attributes. */
13007 die->num_attrs = abbrev->num_attrs;
13008
13009 for (i = 0; i < abbrev->num_attrs; ++i)
13010 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13011 info_ptr);
13012
13013 *diep = die;
13014 *has_children = abbrev->has_children;
13015 return info_ptr;
13016 }
13017
13018 /* Read a die and all its attributes.
13019 Set DIEP to point to a newly allocated die with its information,
13020 except for its child, sibling, and parent fields.
13021 Set HAS_CHILDREN to tell whether the die has children or not. */
13022
13023 static gdb_byte *
13024 read_full_die (const struct die_reader_specs *reader,
13025 struct die_info **diep, gdb_byte *info_ptr,
13026 int *has_children)
13027 {
13028 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13029 }
13030 \f
13031 /* Abbreviation tables.
13032
13033 In DWARF version 2, the description of the debugging information is
13034 stored in a separate .debug_abbrev section. Before we read any
13035 dies from a section we read in all abbreviations and install them
13036 in a hash table. */
13037
13038 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13039
13040 static struct abbrev_info *
13041 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13042 {
13043 struct abbrev_info *abbrev;
13044
13045 abbrev = (struct abbrev_info *)
13046 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13047 memset (abbrev, 0, sizeof (struct abbrev_info));
13048 return abbrev;
13049 }
13050
13051 /* Add an abbreviation to the table. */
13052
13053 static void
13054 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13055 unsigned int abbrev_number,
13056 struct abbrev_info *abbrev)
13057 {
13058 unsigned int hash_number;
13059
13060 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13061 abbrev->next = abbrev_table->abbrevs[hash_number];
13062 abbrev_table->abbrevs[hash_number] = abbrev;
13063 }
13064
13065 /* Look up an abbrev in the table.
13066 Returns NULL if the abbrev is not found. */
13067
13068 static struct abbrev_info *
13069 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13070 unsigned int abbrev_number)
13071 {
13072 unsigned int hash_number;
13073 struct abbrev_info *abbrev;
13074
13075 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13076 abbrev = abbrev_table->abbrevs[hash_number];
13077
13078 while (abbrev)
13079 {
13080 if (abbrev->number == abbrev_number)
13081 return abbrev;
13082 abbrev = abbrev->next;
13083 }
13084 return NULL;
13085 }
13086
13087 /* Read in an abbrev table. */
13088
13089 static struct abbrev_table *
13090 abbrev_table_read_table (struct dwarf2_section_info *section,
13091 sect_offset offset)
13092 {
13093 struct objfile *objfile = dwarf2_per_objfile->objfile;
13094 bfd *abfd = section->asection->owner;
13095 struct abbrev_table *abbrev_table;
13096 gdb_byte *abbrev_ptr;
13097 struct abbrev_info *cur_abbrev;
13098 unsigned int abbrev_number, bytes_read, abbrev_name;
13099 unsigned int abbrev_form;
13100 struct attr_abbrev *cur_attrs;
13101 unsigned int allocated_attrs;
13102
13103 abbrev_table = XMALLOC (struct abbrev_table);
13104 abbrev_table->offset = offset;
13105 obstack_init (&abbrev_table->abbrev_obstack);
13106 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13107 (ABBREV_HASH_SIZE
13108 * sizeof (struct abbrev_info *)));
13109 memset (abbrev_table->abbrevs, 0,
13110 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13111
13112 dwarf2_read_section (objfile, section);
13113 abbrev_ptr = section->buffer + offset.sect_off;
13114 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13115 abbrev_ptr += bytes_read;
13116
13117 allocated_attrs = ATTR_ALLOC_CHUNK;
13118 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13119
13120 /* Loop until we reach an abbrev number of 0. */
13121 while (abbrev_number)
13122 {
13123 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13124
13125 /* read in abbrev header */
13126 cur_abbrev->number = abbrev_number;
13127 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13128 abbrev_ptr += bytes_read;
13129 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13130 abbrev_ptr += 1;
13131
13132 /* now read in declarations */
13133 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13134 abbrev_ptr += bytes_read;
13135 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13136 abbrev_ptr += bytes_read;
13137 while (abbrev_name)
13138 {
13139 if (cur_abbrev->num_attrs == allocated_attrs)
13140 {
13141 allocated_attrs += ATTR_ALLOC_CHUNK;
13142 cur_attrs
13143 = xrealloc (cur_attrs, (allocated_attrs
13144 * sizeof (struct attr_abbrev)));
13145 }
13146
13147 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13148 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13149 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13150 abbrev_ptr += bytes_read;
13151 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13152 abbrev_ptr += bytes_read;
13153 }
13154
13155 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13156 (cur_abbrev->num_attrs
13157 * sizeof (struct attr_abbrev)));
13158 memcpy (cur_abbrev->attrs, cur_attrs,
13159 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13160
13161 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13162
13163 /* Get next abbreviation.
13164 Under Irix6 the abbreviations for a compilation unit are not
13165 always properly terminated with an abbrev number of 0.
13166 Exit loop if we encounter an abbreviation which we have
13167 already read (which means we are about to read the abbreviations
13168 for the next compile unit) or if the end of the abbreviation
13169 table is reached. */
13170 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13171 break;
13172 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13173 abbrev_ptr += bytes_read;
13174 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13175 break;
13176 }
13177
13178 xfree (cur_attrs);
13179 return abbrev_table;
13180 }
13181
13182 /* Free the resources held by ABBREV_TABLE. */
13183
13184 static void
13185 abbrev_table_free (struct abbrev_table *abbrev_table)
13186 {
13187 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13188 xfree (abbrev_table);
13189 }
13190
13191 /* Same as abbrev_table_free but as a cleanup.
13192 We pass in a pointer to the pointer to the table so that we can
13193 set the pointer to NULL when we're done. It also simplifies
13194 build_type_unit_groups. */
13195
13196 static void
13197 abbrev_table_free_cleanup (void *table_ptr)
13198 {
13199 struct abbrev_table **abbrev_table_ptr = table_ptr;
13200
13201 if (*abbrev_table_ptr != NULL)
13202 abbrev_table_free (*abbrev_table_ptr);
13203 *abbrev_table_ptr = NULL;
13204 }
13205
13206 /* Read the abbrev table for CU from ABBREV_SECTION. */
13207
13208 static void
13209 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13210 struct dwarf2_section_info *abbrev_section)
13211 {
13212 cu->abbrev_table =
13213 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13214 }
13215
13216 /* Release the memory used by the abbrev table for a compilation unit. */
13217
13218 static void
13219 dwarf2_free_abbrev_table (void *ptr_to_cu)
13220 {
13221 struct dwarf2_cu *cu = ptr_to_cu;
13222
13223 abbrev_table_free (cu->abbrev_table);
13224 /* Set this to NULL so that we SEGV if we try to read it later,
13225 and also because free_comp_unit verifies this is NULL. */
13226 cu->abbrev_table = NULL;
13227 }
13228 \f
13229 /* Returns nonzero if TAG represents a type that we might generate a partial
13230 symbol for. */
13231
13232 static int
13233 is_type_tag_for_partial (int tag)
13234 {
13235 switch (tag)
13236 {
13237 #if 0
13238 /* Some types that would be reasonable to generate partial symbols for,
13239 that we don't at present. */
13240 case DW_TAG_array_type:
13241 case DW_TAG_file_type:
13242 case DW_TAG_ptr_to_member_type:
13243 case DW_TAG_set_type:
13244 case DW_TAG_string_type:
13245 case DW_TAG_subroutine_type:
13246 #endif
13247 case DW_TAG_base_type:
13248 case DW_TAG_class_type:
13249 case DW_TAG_interface_type:
13250 case DW_TAG_enumeration_type:
13251 case DW_TAG_structure_type:
13252 case DW_TAG_subrange_type:
13253 case DW_TAG_typedef:
13254 case DW_TAG_union_type:
13255 return 1;
13256 default:
13257 return 0;
13258 }
13259 }
13260
13261 /* Load all DIEs that are interesting for partial symbols into memory. */
13262
13263 static struct partial_die_info *
13264 load_partial_dies (const struct die_reader_specs *reader,
13265 gdb_byte *info_ptr, int building_psymtab)
13266 {
13267 struct dwarf2_cu *cu = reader->cu;
13268 struct objfile *objfile = cu->objfile;
13269 struct partial_die_info *part_die;
13270 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13271 struct abbrev_info *abbrev;
13272 unsigned int bytes_read;
13273 unsigned int load_all = 0;
13274 int nesting_level = 1;
13275
13276 parent_die = NULL;
13277 last_die = NULL;
13278
13279 gdb_assert (cu->per_cu != NULL);
13280 if (cu->per_cu->load_all_dies)
13281 load_all = 1;
13282
13283 cu->partial_dies
13284 = htab_create_alloc_ex (cu->header.length / 12,
13285 partial_die_hash,
13286 partial_die_eq,
13287 NULL,
13288 &cu->comp_unit_obstack,
13289 hashtab_obstack_allocate,
13290 dummy_obstack_deallocate);
13291
13292 part_die = obstack_alloc (&cu->comp_unit_obstack,
13293 sizeof (struct partial_die_info));
13294
13295 while (1)
13296 {
13297 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13298
13299 /* A NULL abbrev means the end of a series of children. */
13300 if (abbrev == NULL)
13301 {
13302 if (--nesting_level == 0)
13303 {
13304 /* PART_DIE was probably the last thing allocated on the
13305 comp_unit_obstack, so we could call obstack_free
13306 here. We don't do that because the waste is small,
13307 and will be cleaned up when we're done with this
13308 compilation unit. This way, we're also more robust
13309 against other users of the comp_unit_obstack. */
13310 return first_die;
13311 }
13312 info_ptr += bytes_read;
13313 last_die = parent_die;
13314 parent_die = parent_die->die_parent;
13315 continue;
13316 }
13317
13318 /* Check for template arguments. We never save these; if
13319 they're seen, we just mark the parent, and go on our way. */
13320 if (parent_die != NULL
13321 && cu->language == language_cplus
13322 && (abbrev->tag == DW_TAG_template_type_param
13323 || abbrev->tag == DW_TAG_template_value_param))
13324 {
13325 parent_die->has_template_arguments = 1;
13326
13327 if (!load_all)
13328 {
13329 /* We don't need a partial DIE for the template argument. */
13330 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13331 continue;
13332 }
13333 }
13334
13335 /* We only recurse into c++ subprograms looking for template arguments.
13336 Skip their other children. */
13337 if (!load_all
13338 && cu->language == language_cplus
13339 && parent_die != NULL
13340 && parent_die->tag == DW_TAG_subprogram)
13341 {
13342 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13343 continue;
13344 }
13345
13346 /* Check whether this DIE is interesting enough to save. Normally
13347 we would not be interested in members here, but there may be
13348 later variables referencing them via DW_AT_specification (for
13349 static members). */
13350 if (!load_all
13351 && !is_type_tag_for_partial (abbrev->tag)
13352 && abbrev->tag != DW_TAG_constant
13353 && abbrev->tag != DW_TAG_enumerator
13354 && abbrev->tag != DW_TAG_subprogram
13355 && abbrev->tag != DW_TAG_lexical_block
13356 && abbrev->tag != DW_TAG_variable
13357 && abbrev->tag != DW_TAG_namespace
13358 && abbrev->tag != DW_TAG_module
13359 && abbrev->tag != DW_TAG_member
13360 && abbrev->tag != DW_TAG_imported_unit)
13361 {
13362 /* Otherwise we skip to the next sibling, if any. */
13363 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13364 continue;
13365 }
13366
13367 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13368 info_ptr);
13369
13370 /* This two-pass algorithm for processing partial symbols has a
13371 high cost in cache pressure. Thus, handle some simple cases
13372 here which cover the majority of C partial symbols. DIEs
13373 which neither have specification tags in them, nor could have
13374 specification tags elsewhere pointing at them, can simply be
13375 processed and discarded.
13376
13377 This segment is also optional; scan_partial_symbols and
13378 add_partial_symbol will handle these DIEs if we chain
13379 them in normally. When compilers which do not emit large
13380 quantities of duplicate debug information are more common,
13381 this code can probably be removed. */
13382
13383 /* Any complete simple types at the top level (pretty much all
13384 of them, for a language without namespaces), can be processed
13385 directly. */
13386 if (parent_die == NULL
13387 && part_die->has_specification == 0
13388 && part_die->is_declaration == 0
13389 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13390 || part_die->tag == DW_TAG_base_type
13391 || part_die->tag == DW_TAG_subrange_type))
13392 {
13393 if (building_psymtab && part_die->name != NULL)
13394 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13395 VAR_DOMAIN, LOC_TYPEDEF,
13396 &objfile->static_psymbols,
13397 0, (CORE_ADDR) 0, cu->language, objfile);
13398 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13399 continue;
13400 }
13401
13402 /* The exception for DW_TAG_typedef with has_children above is
13403 a workaround of GCC PR debug/47510. In the case of this complaint
13404 type_name_no_tag_or_error will error on such types later.
13405
13406 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13407 it could not find the child DIEs referenced later, this is checked
13408 above. In correct DWARF DW_TAG_typedef should have no children. */
13409
13410 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13411 complaint (&symfile_complaints,
13412 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13413 "- DIE at 0x%x [in module %s]"),
13414 part_die->offset.sect_off, objfile->name);
13415
13416 /* If we're at the second level, and we're an enumerator, and
13417 our parent has no specification (meaning possibly lives in a
13418 namespace elsewhere), then we can add the partial symbol now
13419 instead of queueing it. */
13420 if (part_die->tag == DW_TAG_enumerator
13421 && parent_die != NULL
13422 && parent_die->die_parent == NULL
13423 && parent_die->tag == DW_TAG_enumeration_type
13424 && parent_die->has_specification == 0)
13425 {
13426 if (part_die->name == NULL)
13427 complaint (&symfile_complaints,
13428 _("malformed enumerator DIE ignored"));
13429 else if (building_psymtab)
13430 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13431 VAR_DOMAIN, LOC_CONST,
13432 (cu->language == language_cplus
13433 || cu->language == language_java)
13434 ? &objfile->global_psymbols
13435 : &objfile->static_psymbols,
13436 0, (CORE_ADDR) 0, cu->language, objfile);
13437
13438 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13439 continue;
13440 }
13441
13442 /* We'll save this DIE so link it in. */
13443 part_die->die_parent = parent_die;
13444 part_die->die_sibling = NULL;
13445 part_die->die_child = NULL;
13446
13447 if (last_die && last_die == parent_die)
13448 last_die->die_child = part_die;
13449 else if (last_die)
13450 last_die->die_sibling = part_die;
13451
13452 last_die = part_die;
13453
13454 if (first_die == NULL)
13455 first_die = part_die;
13456
13457 /* Maybe add the DIE to the hash table. Not all DIEs that we
13458 find interesting need to be in the hash table, because we
13459 also have the parent/sibling/child chains; only those that we
13460 might refer to by offset later during partial symbol reading.
13461
13462 For now this means things that might have be the target of a
13463 DW_AT_specification, DW_AT_abstract_origin, or
13464 DW_AT_extension. DW_AT_extension will refer only to
13465 namespaces; DW_AT_abstract_origin refers to functions (and
13466 many things under the function DIE, but we do not recurse
13467 into function DIEs during partial symbol reading) and
13468 possibly variables as well; DW_AT_specification refers to
13469 declarations. Declarations ought to have the DW_AT_declaration
13470 flag. It happens that GCC forgets to put it in sometimes, but
13471 only for functions, not for types.
13472
13473 Adding more things than necessary to the hash table is harmless
13474 except for the performance cost. Adding too few will result in
13475 wasted time in find_partial_die, when we reread the compilation
13476 unit with load_all_dies set. */
13477
13478 if (load_all
13479 || abbrev->tag == DW_TAG_constant
13480 || abbrev->tag == DW_TAG_subprogram
13481 || abbrev->tag == DW_TAG_variable
13482 || abbrev->tag == DW_TAG_namespace
13483 || part_die->is_declaration)
13484 {
13485 void **slot;
13486
13487 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13488 part_die->offset.sect_off, INSERT);
13489 *slot = part_die;
13490 }
13491
13492 part_die = obstack_alloc (&cu->comp_unit_obstack,
13493 sizeof (struct partial_die_info));
13494
13495 /* For some DIEs we want to follow their children (if any). For C
13496 we have no reason to follow the children of structures; for other
13497 languages we have to, so that we can get at method physnames
13498 to infer fully qualified class names, for DW_AT_specification,
13499 and for C++ template arguments. For C++, we also look one level
13500 inside functions to find template arguments (if the name of the
13501 function does not already contain the template arguments).
13502
13503 For Ada, we need to scan the children of subprograms and lexical
13504 blocks as well because Ada allows the definition of nested
13505 entities that could be interesting for the debugger, such as
13506 nested subprograms for instance. */
13507 if (last_die->has_children
13508 && (load_all
13509 || last_die->tag == DW_TAG_namespace
13510 || last_die->tag == DW_TAG_module
13511 || last_die->tag == DW_TAG_enumeration_type
13512 || (cu->language == language_cplus
13513 && last_die->tag == DW_TAG_subprogram
13514 && (last_die->name == NULL
13515 || strchr (last_die->name, '<') == NULL))
13516 || (cu->language != language_c
13517 && (last_die->tag == DW_TAG_class_type
13518 || last_die->tag == DW_TAG_interface_type
13519 || last_die->tag == DW_TAG_structure_type
13520 || last_die->tag == DW_TAG_union_type))
13521 || (cu->language == language_ada
13522 && (last_die->tag == DW_TAG_subprogram
13523 || last_die->tag == DW_TAG_lexical_block))))
13524 {
13525 nesting_level++;
13526 parent_die = last_die;
13527 continue;
13528 }
13529
13530 /* Otherwise we skip to the next sibling, if any. */
13531 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13532
13533 /* Back to the top, do it again. */
13534 }
13535 }
13536
13537 /* Read a minimal amount of information into the minimal die structure. */
13538
13539 static gdb_byte *
13540 read_partial_die (const struct die_reader_specs *reader,
13541 struct partial_die_info *part_die,
13542 struct abbrev_info *abbrev, unsigned int abbrev_len,
13543 gdb_byte *info_ptr)
13544 {
13545 struct dwarf2_cu *cu = reader->cu;
13546 struct objfile *objfile = cu->objfile;
13547 gdb_byte *buffer = reader->buffer;
13548 unsigned int i;
13549 struct attribute attr;
13550 int has_low_pc_attr = 0;
13551 int has_high_pc_attr = 0;
13552 int high_pc_relative = 0;
13553
13554 memset (part_die, 0, sizeof (struct partial_die_info));
13555
13556 part_die->offset.sect_off = info_ptr - buffer;
13557
13558 info_ptr += abbrev_len;
13559
13560 if (abbrev == NULL)
13561 return info_ptr;
13562
13563 part_die->tag = abbrev->tag;
13564 part_die->has_children = abbrev->has_children;
13565
13566 for (i = 0; i < abbrev->num_attrs; ++i)
13567 {
13568 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13569
13570 /* Store the data if it is of an attribute we want to keep in a
13571 partial symbol table. */
13572 switch (attr.name)
13573 {
13574 case DW_AT_name:
13575 switch (part_die->tag)
13576 {
13577 case DW_TAG_compile_unit:
13578 case DW_TAG_partial_unit:
13579 case DW_TAG_type_unit:
13580 /* Compilation units have a DW_AT_name that is a filename, not
13581 a source language identifier. */
13582 case DW_TAG_enumeration_type:
13583 case DW_TAG_enumerator:
13584 /* These tags always have simple identifiers already; no need
13585 to canonicalize them. */
13586 part_die->name = DW_STRING (&attr);
13587 break;
13588 default:
13589 part_die->name
13590 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13591 &objfile->objfile_obstack);
13592 break;
13593 }
13594 break;
13595 case DW_AT_linkage_name:
13596 case DW_AT_MIPS_linkage_name:
13597 /* Note that both forms of linkage name might appear. We
13598 assume they will be the same, and we only store the last
13599 one we see. */
13600 if (cu->language == language_ada)
13601 part_die->name = DW_STRING (&attr);
13602 part_die->linkage_name = DW_STRING (&attr);
13603 break;
13604 case DW_AT_low_pc:
13605 has_low_pc_attr = 1;
13606 part_die->lowpc = DW_ADDR (&attr);
13607 break;
13608 case DW_AT_high_pc:
13609 has_high_pc_attr = 1;
13610 if (attr.form == DW_FORM_addr
13611 || attr.form == DW_FORM_GNU_addr_index)
13612 part_die->highpc = DW_ADDR (&attr);
13613 else
13614 {
13615 high_pc_relative = 1;
13616 part_die->highpc = DW_UNSND (&attr);
13617 }
13618 break;
13619 case DW_AT_location:
13620 /* Support the .debug_loc offsets. */
13621 if (attr_form_is_block (&attr))
13622 {
13623 part_die->d.locdesc = DW_BLOCK (&attr);
13624 }
13625 else if (attr_form_is_section_offset (&attr))
13626 {
13627 dwarf2_complex_location_expr_complaint ();
13628 }
13629 else
13630 {
13631 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13632 "partial symbol information");
13633 }
13634 break;
13635 case DW_AT_external:
13636 part_die->is_external = DW_UNSND (&attr);
13637 break;
13638 case DW_AT_declaration:
13639 part_die->is_declaration = DW_UNSND (&attr);
13640 break;
13641 case DW_AT_type:
13642 part_die->has_type = 1;
13643 break;
13644 case DW_AT_abstract_origin:
13645 case DW_AT_specification:
13646 case DW_AT_extension:
13647 part_die->has_specification = 1;
13648 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13649 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13650 || cu->per_cu->is_dwz);
13651 break;
13652 case DW_AT_sibling:
13653 /* Ignore absolute siblings, they might point outside of
13654 the current compile unit. */
13655 if (attr.form == DW_FORM_ref_addr)
13656 complaint (&symfile_complaints,
13657 _("ignoring absolute DW_AT_sibling"));
13658 else
13659 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13660 break;
13661 case DW_AT_byte_size:
13662 part_die->has_byte_size = 1;
13663 break;
13664 case DW_AT_calling_convention:
13665 /* DWARF doesn't provide a way to identify a program's source-level
13666 entry point. DW_AT_calling_convention attributes are only meant
13667 to describe functions' calling conventions.
13668
13669 However, because it's a necessary piece of information in
13670 Fortran, and because DW_CC_program is the only piece of debugging
13671 information whose definition refers to a 'main program' at all,
13672 several compilers have begun marking Fortran main programs with
13673 DW_CC_program --- even when those functions use the standard
13674 calling conventions.
13675
13676 So until DWARF specifies a way to provide this information and
13677 compilers pick up the new representation, we'll support this
13678 practice. */
13679 if (DW_UNSND (&attr) == DW_CC_program
13680 && cu->language == language_fortran)
13681 {
13682 set_main_name (part_die->name);
13683
13684 /* As this DIE has a static linkage the name would be difficult
13685 to look up later. */
13686 language_of_main = language_fortran;
13687 }
13688 break;
13689 case DW_AT_inline:
13690 if (DW_UNSND (&attr) == DW_INL_inlined
13691 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13692 part_die->may_be_inlined = 1;
13693 break;
13694
13695 case DW_AT_import:
13696 if (part_die->tag == DW_TAG_imported_unit)
13697 {
13698 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13699 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13700 || cu->per_cu->is_dwz);
13701 }
13702 break;
13703
13704 default:
13705 break;
13706 }
13707 }
13708
13709 if (high_pc_relative)
13710 part_die->highpc += part_die->lowpc;
13711
13712 if (has_low_pc_attr && has_high_pc_attr)
13713 {
13714 /* When using the GNU linker, .gnu.linkonce. sections are used to
13715 eliminate duplicate copies of functions and vtables and such.
13716 The linker will arbitrarily choose one and discard the others.
13717 The AT_*_pc values for such functions refer to local labels in
13718 these sections. If the section from that file was discarded, the
13719 labels are not in the output, so the relocs get a value of 0.
13720 If this is a discarded function, mark the pc bounds as invalid,
13721 so that GDB will ignore it. */
13722 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13723 {
13724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13725
13726 complaint (&symfile_complaints,
13727 _("DW_AT_low_pc %s is zero "
13728 "for DIE at 0x%x [in module %s]"),
13729 paddress (gdbarch, part_die->lowpc),
13730 part_die->offset.sect_off, objfile->name);
13731 }
13732 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13733 else if (part_die->lowpc >= part_die->highpc)
13734 {
13735 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13736
13737 complaint (&symfile_complaints,
13738 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13739 "for DIE at 0x%x [in module %s]"),
13740 paddress (gdbarch, part_die->lowpc),
13741 paddress (gdbarch, part_die->highpc),
13742 part_die->offset.sect_off, objfile->name);
13743 }
13744 else
13745 part_die->has_pc_info = 1;
13746 }
13747
13748 return info_ptr;
13749 }
13750
13751 /* Find a cached partial DIE at OFFSET in CU. */
13752
13753 static struct partial_die_info *
13754 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13755 {
13756 struct partial_die_info *lookup_die = NULL;
13757 struct partial_die_info part_die;
13758
13759 part_die.offset = offset;
13760 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13761 offset.sect_off);
13762
13763 return lookup_die;
13764 }
13765
13766 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13767 except in the case of .debug_types DIEs which do not reference
13768 outside their CU (they do however referencing other types via
13769 DW_FORM_ref_sig8). */
13770
13771 static struct partial_die_info *
13772 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13773 {
13774 struct objfile *objfile = cu->objfile;
13775 struct dwarf2_per_cu_data *per_cu = NULL;
13776 struct partial_die_info *pd = NULL;
13777
13778 if (offset_in_dwz == cu->per_cu->is_dwz
13779 && offset_in_cu_p (&cu->header, offset))
13780 {
13781 pd = find_partial_die_in_comp_unit (offset, cu);
13782 if (pd != NULL)
13783 return pd;
13784 /* We missed recording what we needed.
13785 Load all dies and try again. */
13786 per_cu = cu->per_cu;
13787 }
13788 else
13789 {
13790 /* TUs don't reference other CUs/TUs (except via type signatures). */
13791 if (cu->per_cu->is_debug_types)
13792 {
13793 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13794 " external reference to offset 0x%lx [in module %s].\n"),
13795 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13796 bfd_get_filename (objfile->obfd));
13797 }
13798 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13799 objfile);
13800
13801 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13802 load_partial_comp_unit (per_cu);
13803
13804 per_cu->cu->last_used = 0;
13805 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13806 }
13807
13808 /* If we didn't find it, and not all dies have been loaded,
13809 load them all and try again. */
13810
13811 if (pd == NULL && per_cu->load_all_dies == 0)
13812 {
13813 per_cu->load_all_dies = 1;
13814
13815 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13816 THIS_CU->cu may already be in use. So we can't just free it and
13817 replace its DIEs with the ones we read in. Instead, we leave those
13818 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13819 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13820 set. */
13821 load_partial_comp_unit (per_cu);
13822
13823 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13824 }
13825
13826 if (pd == NULL)
13827 internal_error (__FILE__, __LINE__,
13828 _("could not find partial DIE 0x%x "
13829 "in cache [from module %s]\n"),
13830 offset.sect_off, bfd_get_filename (objfile->obfd));
13831 return pd;
13832 }
13833
13834 /* See if we can figure out if the class lives in a namespace. We do
13835 this by looking for a member function; its demangled name will
13836 contain namespace info, if there is any. */
13837
13838 static void
13839 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13840 struct dwarf2_cu *cu)
13841 {
13842 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13843 what template types look like, because the demangler
13844 frequently doesn't give the same name as the debug info. We
13845 could fix this by only using the demangled name to get the
13846 prefix (but see comment in read_structure_type). */
13847
13848 struct partial_die_info *real_pdi;
13849 struct partial_die_info *child_pdi;
13850
13851 /* If this DIE (this DIE's specification, if any) has a parent, then
13852 we should not do this. We'll prepend the parent's fully qualified
13853 name when we create the partial symbol. */
13854
13855 real_pdi = struct_pdi;
13856 while (real_pdi->has_specification)
13857 real_pdi = find_partial_die (real_pdi->spec_offset,
13858 real_pdi->spec_is_dwz, cu);
13859
13860 if (real_pdi->die_parent != NULL)
13861 return;
13862
13863 for (child_pdi = struct_pdi->die_child;
13864 child_pdi != NULL;
13865 child_pdi = child_pdi->die_sibling)
13866 {
13867 if (child_pdi->tag == DW_TAG_subprogram
13868 && child_pdi->linkage_name != NULL)
13869 {
13870 char *actual_class_name
13871 = language_class_name_from_physname (cu->language_defn,
13872 child_pdi->linkage_name);
13873 if (actual_class_name != NULL)
13874 {
13875 struct_pdi->name
13876 = obsavestring (actual_class_name,
13877 strlen (actual_class_name),
13878 &cu->objfile->objfile_obstack);
13879 xfree (actual_class_name);
13880 }
13881 break;
13882 }
13883 }
13884 }
13885
13886 /* Adjust PART_DIE before generating a symbol for it. This function
13887 may set the is_external flag or change the DIE's name. */
13888
13889 static void
13890 fixup_partial_die (struct partial_die_info *part_die,
13891 struct dwarf2_cu *cu)
13892 {
13893 /* Once we've fixed up a die, there's no point in doing so again.
13894 This also avoids a memory leak if we were to call
13895 guess_partial_die_structure_name multiple times. */
13896 if (part_die->fixup_called)
13897 return;
13898
13899 /* If we found a reference attribute and the DIE has no name, try
13900 to find a name in the referred to DIE. */
13901
13902 if (part_die->name == NULL && part_die->has_specification)
13903 {
13904 struct partial_die_info *spec_die;
13905
13906 spec_die = find_partial_die (part_die->spec_offset,
13907 part_die->spec_is_dwz, cu);
13908
13909 fixup_partial_die (spec_die, cu);
13910
13911 if (spec_die->name)
13912 {
13913 part_die->name = spec_die->name;
13914
13915 /* Copy DW_AT_external attribute if it is set. */
13916 if (spec_die->is_external)
13917 part_die->is_external = spec_die->is_external;
13918 }
13919 }
13920
13921 /* Set default names for some unnamed DIEs. */
13922
13923 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13924 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13925
13926 /* If there is no parent die to provide a namespace, and there are
13927 children, see if we can determine the namespace from their linkage
13928 name. */
13929 if (cu->language == language_cplus
13930 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13931 && part_die->die_parent == NULL
13932 && part_die->has_children
13933 && (part_die->tag == DW_TAG_class_type
13934 || part_die->tag == DW_TAG_structure_type
13935 || part_die->tag == DW_TAG_union_type))
13936 guess_partial_die_structure_name (part_die, cu);
13937
13938 /* GCC might emit a nameless struct or union that has a linkage
13939 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13940 if (part_die->name == NULL
13941 && (part_die->tag == DW_TAG_class_type
13942 || part_die->tag == DW_TAG_interface_type
13943 || part_die->tag == DW_TAG_structure_type
13944 || part_die->tag == DW_TAG_union_type)
13945 && part_die->linkage_name != NULL)
13946 {
13947 char *demangled;
13948
13949 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13950 if (demangled)
13951 {
13952 const char *base;
13953
13954 /* Strip any leading namespaces/classes, keep only the base name.
13955 DW_AT_name for named DIEs does not contain the prefixes. */
13956 base = strrchr (demangled, ':');
13957 if (base && base > demangled && base[-1] == ':')
13958 base++;
13959 else
13960 base = demangled;
13961
13962 part_die->name = obsavestring (base, strlen (base),
13963 &cu->objfile->objfile_obstack);
13964 xfree (demangled);
13965 }
13966 }
13967
13968 part_die->fixup_called = 1;
13969 }
13970
13971 /* Read an attribute value described by an attribute form. */
13972
13973 static gdb_byte *
13974 read_attribute_value (const struct die_reader_specs *reader,
13975 struct attribute *attr, unsigned form,
13976 gdb_byte *info_ptr)
13977 {
13978 struct dwarf2_cu *cu = reader->cu;
13979 bfd *abfd = reader->abfd;
13980 struct comp_unit_head *cu_header = &cu->header;
13981 unsigned int bytes_read;
13982 struct dwarf_block *blk;
13983
13984 attr->form = form;
13985 switch (form)
13986 {
13987 case DW_FORM_ref_addr:
13988 if (cu->header.version == 2)
13989 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13990 else
13991 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13992 &cu->header, &bytes_read);
13993 info_ptr += bytes_read;
13994 break;
13995 case DW_FORM_GNU_ref_alt:
13996 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13997 info_ptr += bytes_read;
13998 break;
13999 case DW_FORM_addr:
14000 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14001 info_ptr += bytes_read;
14002 break;
14003 case DW_FORM_block2:
14004 blk = dwarf_alloc_block (cu);
14005 blk->size = read_2_bytes (abfd, info_ptr);
14006 info_ptr += 2;
14007 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14008 info_ptr += blk->size;
14009 DW_BLOCK (attr) = blk;
14010 break;
14011 case DW_FORM_block4:
14012 blk = dwarf_alloc_block (cu);
14013 blk->size = read_4_bytes (abfd, info_ptr);
14014 info_ptr += 4;
14015 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14016 info_ptr += blk->size;
14017 DW_BLOCK (attr) = blk;
14018 break;
14019 case DW_FORM_data2:
14020 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14021 info_ptr += 2;
14022 break;
14023 case DW_FORM_data4:
14024 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14025 info_ptr += 4;
14026 break;
14027 case DW_FORM_data8:
14028 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14029 info_ptr += 8;
14030 break;
14031 case DW_FORM_sec_offset:
14032 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14033 info_ptr += bytes_read;
14034 break;
14035 case DW_FORM_string:
14036 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14037 DW_STRING_IS_CANONICAL (attr) = 0;
14038 info_ptr += bytes_read;
14039 break;
14040 case DW_FORM_strp:
14041 if (!cu->per_cu->is_dwz)
14042 {
14043 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14044 &bytes_read);
14045 DW_STRING_IS_CANONICAL (attr) = 0;
14046 info_ptr += bytes_read;
14047 break;
14048 }
14049 /* FALLTHROUGH */
14050 case DW_FORM_GNU_strp_alt:
14051 {
14052 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14053 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14054 &bytes_read);
14055
14056 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14057 DW_STRING_IS_CANONICAL (attr) = 0;
14058 info_ptr += bytes_read;
14059 }
14060 break;
14061 case DW_FORM_exprloc:
14062 case DW_FORM_block:
14063 blk = dwarf_alloc_block (cu);
14064 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14065 info_ptr += bytes_read;
14066 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14067 info_ptr += blk->size;
14068 DW_BLOCK (attr) = blk;
14069 break;
14070 case DW_FORM_block1:
14071 blk = dwarf_alloc_block (cu);
14072 blk->size = read_1_byte (abfd, info_ptr);
14073 info_ptr += 1;
14074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14075 info_ptr += blk->size;
14076 DW_BLOCK (attr) = blk;
14077 break;
14078 case DW_FORM_data1:
14079 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14080 info_ptr += 1;
14081 break;
14082 case DW_FORM_flag:
14083 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14084 info_ptr += 1;
14085 break;
14086 case DW_FORM_flag_present:
14087 DW_UNSND (attr) = 1;
14088 break;
14089 case DW_FORM_sdata:
14090 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14091 info_ptr += bytes_read;
14092 break;
14093 case DW_FORM_udata:
14094 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14095 info_ptr += bytes_read;
14096 break;
14097 case DW_FORM_ref1:
14098 DW_UNSND (attr) = (cu->header.offset.sect_off
14099 + read_1_byte (abfd, info_ptr));
14100 info_ptr += 1;
14101 break;
14102 case DW_FORM_ref2:
14103 DW_UNSND (attr) = (cu->header.offset.sect_off
14104 + read_2_bytes (abfd, info_ptr));
14105 info_ptr += 2;
14106 break;
14107 case DW_FORM_ref4:
14108 DW_UNSND (attr) = (cu->header.offset.sect_off
14109 + read_4_bytes (abfd, info_ptr));
14110 info_ptr += 4;
14111 break;
14112 case DW_FORM_ref8:
14113 DW_UNSND (attr) = (cu->header.offset.sect_off
14114 + read_8_bytes (abfd, info_ptr));
14115 info_ptr += 8;
14116 break;
14117 case DW_FORM_ref_sig8:
14118 /* Convert the signature to something we can record in DW_UNSND
14119 for later lookup.
14120 NOTE: This is NULL if the type wasn't found. */
14121 DW_SIGNATURED_TYPE (attr) =
14122 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14123 info_ptr += 8;
14124 break;
14125 case DW_FORM_ref_udata:
14126 DW_UNSND (attr) = (cu->header.offset.sect_off
14127 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14128 info_ptr += bytes_read;
14129 break;
14130 case DW_FORM_indirect:
14131 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14132 info_ptr += bytes_read;
14133 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14134 break;
14135 case DW_FORM_GNU_addr_index:
14136 if (reader->dwo_file == NULL)
14137 {
14138 /* For now flag a hard error.
14139 Later we can turn this into a complaint. */
14140 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14141 dwarf_form_name (form),
14142 bfd_get_filename (abfd));
14143 }
14144 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14145 info_ptr += bytes_read;
14146 break;
14147 case DW_FORM_GNU_str_index:
14148 if (reader->dwo_file == NULL)
14149 {
14150 /* For now flag a hard error.
14151 Later we can turn this into a complaint if warranted. */
14152 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14153 dwarf_form_name (form),
14154 bfd_get_filename (abfd));
14155 }
14156 {
14157 ULONGEST str_index =
14158 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14159
14160 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14161 DW_STRING_IS_CANONICAL (attr) = 0;
14162 info_ptr += bytes_read;
14163 }
14164 break;
14165 default:
14166 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14167 dwarf_form_name (form),
14168 bfd_get_filename (abfd));
14169 }
14170
14171 /* Super hack. */
14172 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14173 attr->form = DW_FORM_GNU_ref_alt;
14174
14175 /* We have seen instances where the compiler tried to emit a byte
14176 size attribute of -1 which ended up being encoded as an unsigned
14177 0xffffffff. Although 0xffffffff is technically a valid size value,
14178 an object of this size seems pretty unlikely so we can relatively
14179 safely treat these cases as if the size attribute was invalid and
14180 treat them as zero by default. */
14181 if (attr->name == DW_AT_byte_size
14182 && form == DW_FORM_data4
14183 && DW_UNSND (attr) >= 0xffffffff)
14184 {
14185 complaint
14186 (&symfile_complaints,
14187 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14188 hex_string (DW_UNSND (attr)));
14189 DW_UNSND (attr) = 0;
14190 }
14191
14192 return info_ptr;
14193 }
14194
14195 /* Read an attribute described by an abbreviated attribute. */
14196
14197 static gdb_byte *
14198 read_attribute (const struct die_reader_specs *reader,
14199 struct attribute *attr, struct attr_abbrev *abbrev,
14200 gdb_byte *info_ptr)
14201 {
14202 attr->name = abbrev->name;
14203 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14204 }
14205
14206 /* Read dwarf information from a buffer. */
14207
14208 static unsigned int
14209 read_1_byte (bfd *abfd, const gdb_byte *buf)
14210 {
14211 return bfd_get_8 (abfd, buf);
14212 }
14213
14214 static int
14215 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14216 {
14217 return bfd_get_signed_8 (abfd, buf);
14218 }
14219
14220 static unsigned int
14221 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14222 {
14223 return bfd_get_16 (abfd, buf);
14224 }
14225
14226 static int
14227 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14228 {
14229 return bfd_get_signed_16 (abfd, buf);
14230 }
14231
14232 static unsigned int
14233 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14234 {
14235 return bfd_get_32 (abfd, buf);
14236 }
14237
14238 static int
14239 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14240 {
14241 return bfd_get_signed_32 (abfd, buf);
14242 }
14243
14244 static ULONGEST
14245 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14246 {
14247 return bfd_get_64 (abfd, buf);
14248 }
14249
14250 static CORE_ADDR
14251 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14252 unsigned int *bytes_read)
14253 {
14254 struct comp_unit_head *cu_header = &cu->header;
14255 CORE_ADDR retval = 0;
14256
14257 if (cu_header->signed_addr_p)
14258 {
14259 switch (cu_header->addr_size)
14260 {
14261 case 2:
14262 retval = bfd_get_signed_16 (abfd, buf);
14263 break;
14264 case 4:
14265 retval = bfd_get_signed_32 (abfd, buf);
14266 break;
14267 case 8:
14268 retval = bfd_get_signed_64 (abfd, buf);
14269 break;
14270 default:
14271 internal_error (__FILE__, __LINE__,
14272 _("read_address: bad switch, signed [in module %s]"),
14273 bfd_get_filename (abfd));
14274 }
14275 }
14276 else
14277 {
14278 switch (cu_header->addr_size)
14279 {
14280 case 2:
14281 retval = bfd_get_16 (abfd, buf);
14282 break;
14283 case 4:
14284 retval = bfd_get_32 (abfd, buf);
14285 break;
14286 case 8:
14287 retval = bfd_get_64 (abfd, buf);
14288 break;
14289 default:
14290 internal_error (__FILE__, __LINE__,
14291 _("read_address: bad switch, "
14292 "unsigned [in module %s]"),
14293 bfd_get_filename (abfd));
14294 }
14295 }
14296
14297 *bytes_read = cu_header->addr_size;
14298 return retval;
14299 }
14300
14301 /* Read the initial length from a section. The (draft) DWARF 3
14302 specification allows the initial length to take up either 4 bytes
14303 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14304 bytes describe the length and all offsets will be 8 bytes in length
14305 instead of 4.
14306
14307 An older, non-standard 64-bit format is also handled by this
14308 function. The older format in question stores the initial length
14309 as an 8-byte quantity without an escape value. Lengths greater
14310 than 2^32 aren't very common which means that the initial 4 bytes
14311 is almost always zero. Since a length value of zero doesn't make
14312 sense for the 32-bit format, this initial zero can be considered to
14313 be an escape value which indicates the presence of the older 64-bit
14314 format. As written, the code can't detect (old format) lengths
14315 greater than 4GB. If it becomes necessary to handle lengths
14316 somewhat larger than 4GB, we could allow other small values (such
14317 as the non-sensical values of 1, 2, and 3) to also be used as
14318 escape values indicating the presence of the old format.
14319
14320 The value returned via bytes_read should be used to increment the
14321 relevant pointer after calling read_initial_length().
14322
14323 [ Note: read_initial_length() and read_offset() are based on the
14324 document entitled "DWARF Debugging Information Format", revision
14325 3, draft 8, dated November 19, 2001. This document was obtained
14326 from:
14327
14328 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14329
14330 This document is only a draft and is subject to change. (So beware.)
14331
14332 Details regarding the older, non-standard 64-bit format were
14333 determined empirically by examining 64-bit ELF files produced by
14334 the SGI toolchain on an IRIX 6.5 machine.
14335
14336 - Kevin, July 16, 2002
14337 ] */
14338
14339 static LONGEST
14340 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14341 {
14342 LONGEST length = bfd_get_32 (abfd, buf);
14343
14344 if (length == 0xffffffff)
14345 {
14346 length = bfd_get_64 (abfd, buf + 4);
14347 *bytes_read = 12;
14348 }
14349 else if (length == 0)
14350 {
14351 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14352 length = bfd_get_64 (abfd, buf);
14353 *bytes_read = 8;
14354 }
14355 else
14356 {
14357 *bytes_read = 4;
14358 }
14359
14360 return length;
14361 }
14362
14363 /* Cover function for read_initial_length.
14364 Returns the length of the object at BUF, and stores the size of the
14365 initial length in *BYTES_READ and stores the size that offsets will be in
14366 *OFFSET_SIZE.
14367 If the initial length size is not equivalent to that specified in
14368 CU_HEADER then issue a complaint.
14369 This is useful when reading non-comp-unit headers. */
14370
14371 static LONGEST
14372 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14373 const struct comp_unit_head *cu_header,
14374 unsigned int *bytes_read,
14375 unsigned int *offset_size)
14376 {
14377 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14378
14379 gdb_assert (cu_header->initial_length_size == 4
14380 || cu_header->initial_length_size == 8
14381 || cu_header->initial_length_size == 12);
14382
14383 if (cu_header->initial_length_size != *bytes_read)
14384 complaint (&symfile_complaints,
14385 _("intermixed 32-bit and 64-bit DWARF sections"));
14386
14387 *offset_size = (*bytes_read == 4) ? 4 : 8;
14388 return length;
14389 }
14390
14391 /* Read an offset from the data stream. The size of the offset is
14392 given by cu_header->offset_size. */
14393
14394 static LONGEST
14395 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14396 unsigned int *bytes_read)
14397 {
14398 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14399
14400 *bytes_read = cu_header->offset_size;
14401 return offset;
14402 }
14403
14404 /* Read an offset from the data stream. */
14405
14406 static LONGEST
14407 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14408 {
14409 LONGEST retval = 0;
14410
14411 switch (offset_size)
14412 {
14413 case 4:
14414 retval = bfd_get_32 (abfd, buf);
14415 break;
14416 case 8:
14417 retval = bfd_get_64 (abfd, buf);
14418 break;
14419 default:
14420 internal_error (__FILE__, __LINE__,
14421 _("read_offset_1: bad switch [in module %s]"),
14422 bfd_get_filename (abfd));
14423 }
14424
14425 return retval;
14426 }
14427
14428 static gdb_byte *
14429 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14430 {
14431 /* If the size of a host char is 8 bits, we can return a pointer
14432 to the buffer, otherwise we have to copy the data to a buffer
14433 allocated on the temporary obstack. */
14434 gdb_assert (HOST_CHAR_BIT == 8);
14435 return buf;
14436 }
14437
14438 static char *
14439 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14440 {
14441 /* If the size of a host char is 8 bits, we can return a pointer
14442 to the string, otherwise we have to copy the string to a buffer
14443 allocated on the temporary obstack. */
14444 gdb_assert (HOST_CHAR_BIT == 8);
14445 if (*buf == '\0')
14446 {
14447 *bytes_read_ptr = 1;
14448 return NULL;
14449 }
14450 *bytes_read_ptr = strlen ((char *) buf) + 1;
14451 return (char *) buf;
14452 }
14453
14454 static char *
14455 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14456 {
14457 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14458 if (dwarf2_per_objfile->str.buffer == NULL)
14459 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14460 bfd_get_filename (abfd));
14461 if (str_offset >= dwarf2_per_objfile->str.size)
14462 error (_("DW_FORM_strp pointing outside of "
14463 ".debug_str section [in module %s]"),
14464 bfd_get_filename (abfd));
14465 gdb_assert (HOST_CHAR_BIT == 8);
14466 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14467 return NULL;
14468 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14469 }
14470
14471 /* Read a string at offset STR_OFFSET in the .debug_str section from
14472 the .dwz file DWZ. Throw an error if the offset is too large. If
14473 the string consists of a single NUL byte, return NULL; otherwise
14474 return a pointer to the string. */
14475
14476 static char *
14477 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14478 {
14479 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14480
14481 if (dwz->str.buffer == NULL)
14482 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14483 "section [in module %s]"),
14484 bfd_get_filename (dwz->dwz_bfd));
14485 if (str_offset >= dwz->str.size)
14486 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14487 ".debug_str section [in module %s]"),
14488 bfd_get_filename (dwz->dwz_bfd));
14489 gdb_assert (HOST_CHAR_BIT == 8);
14490 if (dwz->str.buffer[str_offset] == '\0')
14491 return NULL;
14492 return (char *) (dwz->str.buffer + str_offset);
14493 }
14494
14495 static char *
14496 read_indirect_string (bfd *abfd, gdb_byte *buf,
14497 const struct comp_unit_head *cu_header,
14498 unsigned int *bytes_read_ptr)
14499 {
14500 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14501
14502 return read_indirect_string_at_offset (abfd, str_offset);
14503 }
14504
14505 static ULONGEST
14506 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14507 {
14508 ULONGEST result;
14509 unsigned int num_read;
14510 int i, shift;
14511 unsigned char byte;
14512
14513 result = 0;
14514 shift = 0;
14515 num_read = 0;
14516 i = 0;
14517 while (1)
14518 {
14519 byte = bfd_get_8 (abfd, buf);
14520 buf++;
14521 num_read++;
14522 result |= ((ULONGEST) (byte & 127) << shift);
14523 if ((byte & 128) == 0)
14524 {
14525 break;
14526 }
14527 shift += 7;
14528 }
14529 *bytes_read_ptr = num_read;
14530 return result;
14531 }
14532
14533 static LONGEST
14534 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14535 {
14536 LONGEST result;
14537 int i, shift, num_read;
14538 unsigned char byte;
14539
14540 result = 0;
14541 shift = 0;
14542 num_read = 0;
14543 i = 0;
14544 while (1)
14545 {
14546 byte = bfd_get_8 (abfd, buf);
14547 buf++;
14548 num_read++;
14549 result |= ((LONGEST) (byte & 127) << shift);
14550 shift += 7;
14551 if ((byte & 128) == 0)
14552 {
14553 break;
14554 }
14555 }
14556 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14557 result |= -(((LONGEST) 1) << shift);
14558 *bytes_read_ptr = num_read;
14559 return result;
14560 }
14561
14562 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14563 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14564 ADDR_SIZE is the size of addresses from the CU header. */
14565
14566 static CORE_ADDR
14567 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14568 {
14569 struct objfile *objfile = dwarf2_per_objfile->objfile;
14570 bfd *abfd = objfile->obfd;
14571 const gdb_byte *info_ptr;
14572
14573 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14574 if (dwarf2_per_objfile->addr.buffer == NULL)
14575 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14576 objfile->name);
14577 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14578 error (_("DW_FORM_addr_index pointing outside of "
14579 ".debug_addr section [in module %s]"),
14580 objfile->name);
14581 info_ptr = (dwarf2_per_objfile->addr.buffer
14582 + addr_base + addr_index * addr_size);
14583 if (addr_size == 4)
14584 return bfd_get_32 (abfd, info_ptr);
14585 else
14586 return bfd_get_64 (abfd, info_ptr);
14587 }
14588
14589 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14590
14591 static CORE_ADDR
14592 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14593 {
14594 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14595 }
14596
14597 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14598
14599 static CORE_ADDR
14600 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14601 unsigned int *bytes_read)
14602 {
14603 bfd *abfd = cu->objfile->obfd;
14604 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14605
14606 return read_addr_index (cu, addr_index);
14607 }
14608
14609 /* Data structure to pass results from dwarf2_read_addr_index_reader
14610 back to dwarf2_read_addr_index. */
14611
14612 struct dwarf2_read_addr_index_data
14613 {
14614 ULONGEST addr_base;
14615 int addr_size;
14616 };
14617
14618 /* die_reader_func for dwarf2_read_addr_index. */
14619
14620 static void
14621 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14622 gdb_byte *info_ptr,
14623 struct die_info *comp_unit_die,
14624 int has_children,
14625 void *data)
14626 {
14627 struct dwarf2_cu *cu = reader->cu;
14628 struct dwarf2_read_addr_index_data *aidata =
14629 (struct dwarf2_read_addr_index_data *) data;
14630
14631 aidata->addr_base = cu->addr_base;
14632 aidata->addr_size = cu->header.addr_size;
14633 }
14634
14635 /* Given an index in .debug_addr, fetch the value.
14636 NOTE: This can be called during dwarf expression evaluation,
14637 long after the debug information has been read, and thus per_cu->cu
14638 may no longer exist. */
14639
14640 CORE_ADDR
14641 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14642 unsigned int addr_index)
14643 {
14644 struct objfile *objfile = per_cu->objfile;
14645 struct dwarf2_cu *cu = per_cu->cu;
14646 ULONGEST addr_base;
14647 int addr_size;
14648
14649 /* This is intended to be called from outside this file. */
14650 dw2_setup (objfile);
14651
14652 /* We need addr_base and addr_size.
14653 If we don't have PER_CU->cu, we have to get it.
14654 Nasty, but the alternative is storing the needed info in PER_CU,
14655 which at this point doesn't seem justified: it's not clear how frequently
14656 it would get used and it would increase the size of every PER_CU.
14657 Entry points like dwarf2_per_cu_addr_size do a similar thing
14658 so we're not in uncharted territory here.
14659 Alas we need to be a bit more complicated as addr_base is contained
14660 in the DIE.
14661
14662 We don't need to read the entire CU(/TU).
14663 We just need the header and top level die.
14664
14665 IWBN to use the aging mechanism to let us lazily later discard the CU.
14666 For now we skip this optimization. */
14667
14668 if (cu != NULL)
14669 {
14670 addr_base = cu->addr_base;
14671 addr_size = cu->header.addr_size;
14672 }
14673 else
14674 {
14675 struct dwarf2_read_addr_index_data aidata;
14676
14677 /* Note: We can't use init_cutu_and_read_dies_simple here,
14678 we need addr_base. */
14679 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14680 dwarf2_read_addr_index_reader, &aidata);
14681 addr_base = aidata.addr_base;
14682 addr_size = aidata.addr_size;
14683 }
14684
14685 return read_addr_index_1 (addr_index, addr_base, addr_size);
14686 }
14687
14688 /* Given a DW_AT_str_index, fetch the string. */
14689
14690 static char *
14691 read_str_index (const struct die_reader_specs *reader,
14692 struct dwarf2_cu *cu, ULONGEST str_index)
14693 {
14694 struct objfile *objfile = dwarf2_per_objfile->objfile;
14695 const char *dwo_name = objfile->name;
14696 bfd *abfd = objfile->obfd;
14697 struct dwo_sections *sections = &reader->dwo_file->sections;
14698 gdb_byte *info_ptr;
14699 ULONGEST str_offset;
14700
14701 dwarf2_read_section (objfile, &sections->str);
14702 dwarf2_read_section (objfile, &sections->str_offsets);
14703 if (sections->str.buffer == NULL)
14704 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14705 " in CU at offset 0x%lx [in module %s]"),
14706 (long) cu->header.offset.sect_off, dwo_name);
14707 if (sections->str_offsets.buffer == NULL)
14708 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14709 " in CU at offset 0x%lx [in module %s]"),
14710 (long) cu->header.offset.sect_off, dwo_name);
14711 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14712 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14713 " section in CU at offset 0x%lx [in module %s]"),
14714 (long) cu->header.offset.sect_off, dwo_name);
14715 info_ptr = (sections->str_offsets.buffer
14716 + str_index * cu->header.offset_size);
14717 if (cu->header.offset_size == 4)
14718 str_offset = bfd_get_32 (abfd, info_ptr);
14719 else
14720 str_offset = bfd_get_64 (abfd, info_ptr);
14721 if (str_offset >= sections->str.size)
14722 error (_("Offset from DW_FORM_str_index pointing outside of"
14723 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14724 (long) cu->header.offset.sect_off, dwo_name);
14725 return (char *) (sections->str.buffer + str_offset);
14726 }
14727
14728 /* Return the length of an LEB128 number in BUF. */
14729
14730 static int
14731 leb128_size (const gdb_byte *buf)
14732 {
14733 const gdb_byte *begin = buf;
14734 gdb_byte byte;
14735
14736 while (1)
14737 {
14738 byte = *buf++;
14739 if ((byte & 128) == 0)
14740 return buf - begin;
14741 }
14742 }
14743
14744 static void
14745 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14746 {
14747 switch (lang)
14748 {
14749 case DW_LANG_C89:
14750 case DW_LANG_C99:
14751 case DW_LANG_C:
14752 cu->language = language_c;
14753 break;
14754 case DW_LANG_C_plus_plus:
14755 cu->language = language_cplus;
14756 break;
14757 case DW_LANG_D:
14758 cu->language = language_d;
14759 break;
14760 case DW_LANG_Fortran77:
14761 case DW_LANG_Fortran90:
14762 case DW_LANG_Fortran95:
14763 cu->language = language_fortran;
14764 break;
14765 case DW_LANG_Go:
14766 cu->language = language_go;
14767 break;
14768 case DW_LANG_Mips_Assembler:
14769 cu->language = language_asm;
14770 break;
14771 case DW_LANG_Java:
14772 cu->language = language_java;
14773 break;
14774 case DW_LANG_Ada83:
14775 case DW_LANG_Ada95:
14776 cu->language = language_ada;
14777 break;
14778 case DW_LANG_Modula2:
14779 cu->language = language_m2;
14780 break;
14781 case DW_LANG_Pascal83:
14782 cu->language = language_pascal;
14783 break;
14784 case DW_LANG_ObjC:
14785 cu->language = language_objc;
14786 break;
14787 case DW_LANG_Cobol74:
14788 case DW_LANG_Cobol85:
14789 default:
14790 cu->language = language_minimal;
14791 break;
14792 }
14793 cu->language_defn = language_def (cu->language);
14794 }
14795
14796 /* Return the named attribute or NULL if not there. */
14797
14798 static struct attribute *
14799 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14800 {
14801 for (;;)
14802 {
14803 unsigned int i;
14804 struct attribute *spec = NULL;
14805
14806 for (i = 0; i < die->num_attrs; ++i)
14807 {
14808 if (die->attrs[i].name == name)
14809 return &die->attrs[i];
14810 if (die->attrs[i].name == DW_AT_specification
14811 || die->attrs[i].name == DW_AT_abstract_origin)
14812 spec = &die->attrs[i];
14813 }
14814
14815 if (!spec)
14816 break;
14817
14818 die = follow_die_ref (die, spec, &cu);
14819 }
14820
14821 return NULL;
14822 }
14823
14824 /* Return the named attribute or NULL if not there,
14825 but do not follow DW_AT_specification, etc.
14826 This is for use in contexts where we're reading .debug_types dies.
14827 Following DW_AT_specification, DW_AT_abstract_origin will take us
14828 back up the chain, and we want to go down. */
14829
14830 static struct attribute *
14831 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14832 {
14833 unsigned int i;
14834
14835 for (i = 0; i < die->num_attrs; ++i)
14836 if (die->attrs[i].name == name)
14837 return &die->attrs[i];
14838
14839 return NULL;
14840 }
14841
14842 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14843 and holds a non-zero value. This function should only be used for
14844 DW_FORM_flag or DW_FORM_flag_present attributes. */
14845
14846 static int
14847 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14848 {
14849 struct attribute *attr = dwarf2_attr (die, name, cu);
14850
14851 return (attr && DW_UNSND (attr));
14852 }
14853
14854 static int
14855 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14856 {
14857 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14858 which value is non-zero. However, we have to be careful with
14859 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14860 (via dwarf2_flag_true_p) follows this attribute. So we may
14861 end up accidently finding a declaration attribute that belongs
14862 to a different DIE referenced by the specification attribute,
14863 even though the given DIE does not have a declaration attribute. */
14864 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14865 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14866 }
14867
14868 /* Return the die giving the specification for DIE, if there is
14869 one. *SPEC_CU is the CU containing DIE on input, and the CU
14870 containing the return value on output. If there is no
14871 specification, but there is an abstract origin, that is
14872 returned. */
14873
14874 static struct die_info *
14875 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14876 {
14877 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14878 *spec_cu);
14879
14880 if (spec_attr == NULL)
14881 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14882
14883 if (spec_attr == NULL)
14884 return NULL;
14885 else
14886 return follow_die_ref (die, spec_attr, spec_cu);
14887 }
14888
14889 /* Free the line_header structure *LH, and any arrays and strings it
14890 refers to.
14891 NOTE: This is also used as a "cleanup" function. */
14892
14893 static void
14894 free_line_header (struct line_header *lh)
14895 {
14896 if (lh->standard_opcode_lengths)
14897 xfree (lh->standard_opcode_lengths);
14898
14899 /* Remember that all the lh->file_names[i].name pointers are
14900 pointers into debug_line_buffer, and don't need to be freed. */
14901 if (lh->file_names)
14902 xfree (lh->file_names);
14903
14904 /* Similarly for the include directory names. */
14905 if (lh->include_dirs)
14906 xfree (lh->include_dirs);
14907
14908 xfree (lh);
14909 }
14910
14911 /* Add an entry to LH's include directory table. */
14912
14913 static void
14914 add_include_dir (struct line_header *lh, char *include_dir)
14915 {
14916 /* Grow the array if necessary. */
14917 if (lh->include_dirs_size == 0)
14918 {
14919 lh->include_dirs_size = 1; /* for testing */
14920 lh->include_dirs = xmalloc (lh->include_dirs_size
14921 * sizeof (*lh->include_dirs));
14922 }
14923 else if (lh->num_include_dirs >= lh->include_dirs_size)
14924 {
14925 lh->include_dirs_size *= 2;
14926 lh->include_dirs = xrealloc (lh->include_dirs,
14927 (lh->include_dirs_size
14928 * sizeof (*lh->include_dirs)));
14929 }
14930
14931 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14932 }
14933
14934 /* Add an entry to LH's file name table. */
14935
14936 static void
14937 add_file_name (struct line_header *lh,
14938 char *name,
14939 unsigned int dir_index,
14940 unsigned int mod_time,
14941 unsigned int length)
14942 {
14943 struct file_entry *fe;
14944
14945 /* Grow the array if necessary. */
14946 if (lh->file_names_size == 0)
14947 {
14948 lh->file_names_size = 1; /* for testing */
14949 lh->file_names = xmalloc (lh->file_names_size
14950 * sizeof (*lh->file_names));
14951 }
14952 else if (lh->num_file_names >= lh->file_names_size)
14953 {
14954 lh->file_names_size *= 2;
14955 lh->file_names = xrealloc (lh->file_names,
14956 (lh->file_names_size
14957 * sizeof (*lh->file_names)));
14958 }
14959
14960 fe = &lh->file_names[lh->num_file_names++];
14961 fe->name = name;
14962 fe->dir_index = dir_index;
14963 fe->mod_time = mod_time;
14964 fe->length = length;
14965 fe->included_p = 0;
14966 fe->symtab = NULL;
14967 }
14968
14969 /* A convenience function to find the proper .debug_line section for a
14970 CU. */
14971
14972 static struct dwarf2_section_info *
14973 get_debug_line_section (struct dwarf2_cu *cu)
14974 {
14975 struct dwarf2_section_info *section;
14976
14977 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14978 DWO file. */
14979 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14980 section = &cu->dwo_unit->dwo_file->sections.line;
14981 else if (cu->per_cu->is_dwz)
14982 {
14983 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14984
14985 section = &dwz->line;
14986 }
14987 else
14988 section = &dwarf2_per_objfile->line;
14989
14990 return section;
14991 }
14992
14993 /* Read the statement program header starting at OFFSET in
14994 .debug_line, or .debug_line.dwo. Return a pointer
14995 to a struct line_header, allocated using xmalloc.
14996
14997 NOTE: the strings in the include directory and file name tables of
14998 the returned object point into the dwarf line section buffer,
14999 and must not be freed. */
15000
15001 static struct line_header *
15002 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15003 {
15004 struct cleanup *back_to;
15005 struct line_header *lh;
15006 gdb_byte *line_ptr;
15007 unsigned int bytes_read, offset_size;
15008 int i;
15009 char *cur_dir, *cur_file;
15010 struct dwarf2_section_info *section;
15011 bfd *abfd;
15012
15013 section = get_debug_line_section (cu);
15014 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15015 if (section->buffer == NULL)
15016 {
15017 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15018 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15019 else
15020 complaint (&symfile_complaints, _("missing .debug_line section"));
15021 return 0;
15022 }
15023
15024 /* We can't do this until we know the section is non-empty.
15025 Only then do we know we have such a section. */
15026 abfd = section->asection->owner;
15027
15028 /* Make sure that at least there's room for the total_length field.
15029 That could be 12 bytes long, but we're just going to fudge that. */
15030 if (offset + 4 >= section->size)
15031 {
15032 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15033 return 0;
15034 }
15035
15036 lh = xmalloc (sizeof (*lh));
15037 memset (lh, 0, sizeof (*lh));
15038 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15039 (void *) lh);
15040
15041 line_ptr = section->buffer + offset;
15042
15043 /* Read in the header. */
15044 lh->total_length =
15045 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15046 &bytes_read, &offset_size);
15047 line_ptr += bytes_read;
15048 if (line_ptr + lh->total_length > (section->buffer + section->size))
15049 {
15050 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15051 return 0;
15052 }
15053 lh->statement_program_end = line_ptr + lh->total_length;
15054 lh->version = read_2_bytes (abfd, line_ptr);
15055 line_ptr += 2;
15056 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15057 line_ptr += offset_size;
15058 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15059 line_ptr += 1;
15060 if (lh->version >= 4)
15061 {
15062 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15063 line_ptr += 1;
15064 }
15065 else
15066 lh->maximum_ops_per_instruction = 1;
15067
15068 if (lh->maximum_ops_per_instruction == 0)
15069 {
15070 lh->maximum_ops_per_instruction = 1;
15071 complaint (&symfile_complaints,
15072 _("invalid maximum_ops_per_instruction "
15073 "in `.debug_line' section"));
15074 }
15075
15076 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15077 line_ptr += 1;
15078 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15079 line_ptr += 1;
15080 lh->line_range = read_1_byte (abfd, line_ptr);
15081 line_ptr += 1;
15082 lh->opcode_base = read_1_byte (abfd, line_ptr);
15083 line_ptr += 1;
15084 lh->standard_opcode_lengths
15085 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15086
15087 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15088 for (i = 1; i < lh->opcode_base; ++i)
15089 {
15090 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15091 line_ptr += 1;
15092 }
15093
15094 /* Read directory table. */
15095 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15096 {
15097 line_ptr += bytes_read;
15098 add_include_dir (lh, cur_dir);
15099 }
15100 line_ptr += bytes_read;
15101
15102 /* Read file name table. */
15103 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15104 {
15105 unsigned int dir_index, mod_time, length;
15106
15107 line_ptr += bytes_read;
15108 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15109 line_ptr += bytes_read;
15110 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15111 line_ptr += bytes_read;
15112 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15113 line_ptr += bytes_read;
15114
15115 add_file_name (lh, cur_file, dir_index, mod_time, length);
15116 }
15117 line_ptr += bytes_read;
15118 lh->statement_program_start = line_ptr;
15119
15120 if (line_ptr > (section->buffer + section->size))
15121 complaint (&symfile_complaints,
15122 _("line number info header doesn't "
15123 "fit in `.debug_line' section"));
15124
15125 discard_cleanups (back_to);
15126 return lh;
15127 }
15128
15129 /* Subroutine of dwarf_decode_lines to simplify it.
15130 Return the file name of the psymtab for included file FILE_INDEX
15131 in line header LH of PST.
15132 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15133 If space for the result is malloc'd, it will be freed by a cleanup.
15134 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15135
15136 The function creates dangling cleanup registration. */
15137
15138 static char *
15139 psymtab_include_file_name (const struct line_header *lh, int file_index,
15140 const struct partial_symtab *pst,
15141 const char *comp_dir)
15142 {
15143 const struct file_entry fe = lh->file_names [file_index];
15144 char *include_name = fe.name;
15145 char *include_name_to_compare = include_name;
15146 char *dir_name = NULL;
15147 const char *pst_filename;
15148 char *copied_name = NULL;
15149 int file_is_pst;
15150
15151 if (fe.dir_index)
15152 dir_name = lh->include_dirs[fe.dir_index - 1];
15153
15154 if (!IS_ABSOLUTE_PATH (include_name)
15155 && (dir_name != NULL || comp_dir != NULL))
15156 {
15157 /* Avoid creating a duplicate psymtab for PST.
15158 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15159 Before we do the comparison, however, we need to account
15160 for DIR_NAME and COMP_DIR.
15161 First prepend dir_name (if non-NULL). If we still don't
15162 have an absolute path prepend comp_dir (if non-NULL).
15163 However, the directory we record in the include-file's
15164 psymtab does not contain COMP_DIR (to match the
15165 corresponding symtab(s)).
15166
15167 Example:
15168
15169 bash$ cd /tmp
15170 bash$ gcc -g ./hello.c
15171 include_name = "hello.c"
15172 dir_name = "."
15173 DW_AT_comp_dir = comp_dir = "/tmp"
15174 DW_AT_name = "./hello.c" */
15175
15176 if (dir_name != NULL)
15177 {
15178 include_name = concat (dir_name, SLASH_STRING,
15179 include_name, (char *)NULL);
15180 include_name_to_compare = include_name;
15181 make_cleanup (xfree, include_name);
15182 }
15183 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15184 {
15185 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15186 include_name, (char *)NULL);
15187 }
15188 }
15189
15190 pst_filename = pst->filename;
15191 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15192 {
15193 copied_name = concat (pst->dirname, SLASH_STRING,
15194 pst_filename, (char *)NULL);
15195 pst_filename = copied_name;
15196 }
15197
15198 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15199
15200 if (include_name_to_compare != include_name)
15201 xfree (include_name_to_compare);
15202 if (copied_name != NULL)
15203 xfree (copied_name);
15204
15205 if (file_is_pst)
15206 return NULL;
15207 return include_name;
15208 }
15209
15210 /* Ignore this record_line request. */
15211
15212 static void
15213 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15214 {
15215 return;
15216 }
15217
15218 /* Subroutine of dwarf_decode_lines to simplify it.
15219 Process the line number information in LH. */
15220
15221 static void
15222 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15223 struct dwarf2_cu *cu, struct partial_symtab *pst)
15224 {
15225 gdb_byte *line_ptr, *extended_end;
15226 gdb_byte *line_end;
15227 unsigned int bytes_read, extended_len;
15228 unsigned char op_code, extended_op, adj_opcode;
15229 CORE_ADDR baseaddr;
15230 struct objfile *objfile = cu->objfile;
15231 bfd *abfd = objfile->obfd;
15232 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15233 const int decode_for_pst_p = (pst != NULL);
15234 struct subfile *last_subfile = NULL;
15235 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15236 = record_line;
15237
15238 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15239
15240 line_ptr = lh->statement_program_start;
15241 line_end = lh->statement_program_end;
15242
15243 /* Read the statement sequences until there's nothing left. */
15244 while (line_ptr < line_end)
15245 {
15246 /* state machine registers */
15247 CORE_ADDR address = 0;
15248 unsigned int file = 1;
15249 unsigned int line = 1;
15250 unsigned int column = 0;
15251 int is_stmt = lh->default_is_stmt;
15252 int basic_block = 0;
15253 int end_sequence = 0;
15254 CORE_ADDR addr;
15255 unsigned char op_index = 0;
15256
15257 if (!decode_for_pst_p && lh->num_file_names >= file)
15258 {
15259 /* Start a subfile for the current file of the state machine. */
15260 /* lh->include_dirs and lh->file_names are 0-based, but the
15261 directory and file name numbers in the statement program
15262 are 1-based. */
15263 struct file_entry *fe = &lh->file_names[file - 1];
15264 char *dir = NULL;
15265
15266 if (fe->dir_index)
15267 dir = lh->include_dirs[fe->dir_index - 1];
15268
15269 dwarf2_start_subfile (fe->name, dir, comp_dir);
15270 }
15271
15272 /* Decode the table. */
15273 while (!end_sequence)
15274 {
15275 op_code = read_1_byte (abfd, line_ptr);
15276 line_ptr += 1;
15277 if (line_ptr > line_end)
15278 {
15279 dwarf2_debug_line_missing_end_sequence_complaint ();
15280 break;
15281 }
15282
15283 if (op_code >= lh->opcode_base)
15284 {
15285 /* Special operand. */
15286 adj_opcode = op_code - lh->opcode_base;
15287 address += (((op_index + (adj_opcode / lh->line_range))
15288 / lh->maximum_ops_per_instruction)
15289 * lh->minimum_instruction_length);
15290 op_index = ((op_index + (adj_opcode / lh->line_range))
15291 % lh->maximum_ops_per_instruction);
15292 line += lh->line_base + (adj_opcode % lh->line_range);
15293 if (lh->num_file_names < file || file == 0)
15294 dwarf2_debug_line_missing_file_complaint ();
15295 /* For now we ignore lines not starting on an
15296 instruction boundary. */
15297 else if (op_index == 0)
15298 {
15299 lh->file_names[file - 1].included_p = 1;
15300 if (!decode_for_pst_p && is_stmt)
15301 {
15302 if (last_subfile != current_subfile)
15303 {
15304 addr = gdbarch_addr_bits_remove (gdbarch, address);
15305 if (last_subfile)
15306 (*p_record_line) (last_subfile, 0, addr);
15307 last_subfile = current_subfile;
15308 }
15309 /* Append row to matrix using current values. */
15310 addr = gdbarch_addr_bits_remove (gdbarch, address);
15311 (*p_record_line) (current_subfile, line, addr);
15312 }
15313 }
15314 basic_block = 0;
15315 }
15316 else switch (op_code)
15317 {
15318 case DW_LNS_extended_op:
15319 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15320 &bytes_read);
15321 line_ptr += bytes_read;
15322 extended_end = line_ptr + extended_len;
15323 extended_op = read_1_byte (abfd, line_ptr);
15324 line_ptr += 1;
15325 switch (extended_op)
15326 {
15327 case DW_LNE_end_sequence:
15328 p_record_line = record_line;
15329 end_sequence = 1;
15330 break;
15331 case DW_LNE_set_address:
15332 address = read_address (abfd, line_ptr, cu, &bytes_read);
15333
15334 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15335 {
15336 /* This line table is for a function which has been
15337 GCd by the linker. Ignore it. PR gdb/12528 */
15338
15339 long line_offset
15340 = line_ptr - get_debug_line_section (cu)->buffer;
15341
15342 complaint (&symfile_complaints,
15343 _(".debug_line address at offset 0x%lx is 0 "
15344 "[in module %s]"),
15345 line_offset, objfile->name);
15346 p_record_line = noop_record_line;
15347 }
15348
15349 op_index = 0;
15350 line_ptr += bytes_read;
15351 address += baseaddr;
15352 break;
15353 case DW_LNE_define_file:
15354 {
15355 char *cur_file;
15356 unsigned int dir_index, mod_time, length;
15357
15358 cur_file = read_direct_string (abfd, line_ptr,
15359 &bytes_read);
15360 line_ptr += bytes_read;
15361 dir_index =
15362 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15363 line_ptr += bytes_read;
15364 mod_time =
15365 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15366 line_ptr += bytes_read;
15367 length =
15368 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15369 line_ptr += bytes_read;
15370 add_file_name (lh, cur_file, dir_index, mod_time, length);
15371 }
15372 break;
15373 case DW_LNE_set_discriminator:
15374 /* The discriminator is not interesting to the debugger;
15375 just ignore it. */
15376 line_ptr = extended_end;
15377 break;
15378 default:
15379 complaint (&symfile_complaints,
15380 _("mangled .debug_line section"));
15381 return;
15382 }
15383 /* Make sure that we parsed the extended op correctly. If e.g.
15384 we expected a different address size than the producer used,
15385 we may have read the wrong number of bytes. */
15386 if (line_ptr != extended_end)
15387 {
15388 complaint (&symfile_complaints,
15389 _("mangled .debug_line section"));
15390 return;
15391 }
15392 break;
15393 case DW_LNS_copy:
15394 if (lh->num_file_names < file || file == 0)
15395 dwarf2_debug_line_missing_file_complaint ();
15396 else
15397 {
15398 lh->file_names[file - 1].included_p = 1;
15399 if (!decode_for_pst_p && is_stmt)
15400 {
15401 if (last_subfile != current_subfile)
15402 {
15403 addr = gdbarch_addr_bits_remove (gdbarch, address);
15404 if (last_subfile)
15405 (*p_record_line) (last_subfile, 0, addr);
15406 last_subfile = current_subfile;
15407 }
15408 addr = gdbarch_addr_bits_remove (gdbarch, address);
15409 (*p_record_line) (current_subfile, line, addr);
15410 }
15411 }
15412 basic_block = 0;
15413 break;
15414 case DW_LNS_advance_pc:
15415 {
15416 CORE_ADDR adjust
15417 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15418
15419 address += (((op_index + adjust)
15420 / lh->maximum_ops_per_instruction)
15421 * lh->minimum_instruction_length);
15422 op_index = ((op_index + adjust)
15423 % lh->maximum_ops_per_instruction);
15424 line_ptr += bytes_read;
15425 }
15426 break;
15427 case DW_LNS_advance_line:
15428 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15429 line_ptr += bytes_read;
15430 break;
15431 case DW_LNS_set_file:
15432 {
15433 /* The arrays lh->include_dirs and lh->file_names are
15434 0-based, but the directory and file name numbers in
15435 the statement program are 1-based. */
15436 struct file_entry *fe;
15437 char *dir = NULL;
15438
15439 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15440 line_ptr += bytes_read;
15441 if (lh->num_file_names < file || file == 0)
15442 dwarf2_debug_line_missing_file_complaint ();
15443 else
15444 {
15445 fe = &lh->file_names[file - 1];
15446 if (fe->dir_index)
15447 dir = lh->include_dirs[fe->dir_index - 1];
15448 if (!decode_for_pst_p)
15449 {
15450 last_subfile = current_subfile;
15451 dwarf2_start_subfile (fe->name, dir, comp_dir);
15452 }
15453 }
15454 }
15455 break;
15456 case DW_LNS_set_column:
15457 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15458 line_ptr += bytes_read;
15459 break;
15460 case DW_LNS_negate_stmt:
15461 is_stmt = (!is_stmt);
15462 break;
15463 case DW_LNS_set_basic_block:
15464 basic_block = 1;
15465 break;
15466 /* Add to the address register of the state machine the
15467 address increment value corresponding to special opcode
15468 255. I.e., this value is scaled by the minimum
15469 instruction length since special opcode 255 would have
15470 scaled the increment. */
15471 case DW_LNS_const_add_pc:
15472 {
15473 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15474
15475 address += (((op_index + adjust)
15476 / lh->maximum_ops_per_instruction)
15477 * lh->minimum_instruction_length);
15478 op_index = ((op_index + adjust)
15479 % lh->maximum_ops_per_instruction);
15480 }
15481 break;
15482 case DW_LNS_fixed_advance_pc:
15483 address += read_2_bytes (abfd, line_ptr);
15484 op_index = 0;
15485 line_ptr += 2;
15486 break;
15487 default:
15488 {
15489 /* Unknown standard opcode, ignore it. */
15490 int i;
15491
15492 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15493 {
15494 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15495 line_ptr += bytes_read;
15496 }
15497 }
15498 }
15499 }
15500 if (lh->num_file_names < file || file == 0)
15501 dwarf2_debug_line_missing_file_complaint ();
15502 else
15503 {
15504 lh->file_names[file - 1].included_p = 1;
15505 if (!decode_for_pst_p)
15506 {
15507 addr = gdbarch_addr_bits_remove (gdbarch, address);
15508 (*p_record_line) (current_subfile, 0, addr);
15509 }
15510 }
15511 }
15512 }
15513
15514 /* Decode the Line Number Program (LNP) for the given line_header
15515 structure and CU. The actual information extracted and the type
15516 of structures created from the LNP depends on the value of PST.
15517
15518 1. If PST is NULL, then this procedure uses the data from the program
15519 to create all necessary symbol tables, and their linetables.
15520
15521 2. If PST is not NULL, this procedure reads the program to determine
15522 the list of files included by the unit represented by PST, and
15523 builds all the associated partial symbol tables.
15524
15525 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15526 It is used for relative paths in the line table.
15527 NOTE: When processing partial symtabs (pst != NULL),
15528 comp_dir == pst->dirname.
15529
15530 NOTE: It is important that psymtabs have the same file name (via strcmp)
15531 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15532 symtab we don't use it in the name of the psymtabs we create.
15533 E.g. expand_line_sal requires this when finding psymtabs to expand.
15534 A good testcase for this is mb-inline.exp. */
15535
15536 static void
15537 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15538 struct dwarf2_cu *cu, struct partial_symtab *pst,
15539 int want_line_info)
15540 {
15541 struct objfile *objfile = cu->objfile;
15542 const int decode_for_pst_p = (pst != NULL);
15543 struct subfile *first_subfile = current_subfile;
15544
15545 if (want_line_info)
15546 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15547
15548 if (decode_for_pst_p)
15549 {
15550 int file_index;
15551
15552 /* Now that we're done scanning the Line Header Program, we can
15553 create the psymtab of each included file. */
15554 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15555 if (lh->file_names[file_index].included_p == 1)
15556 {
15557 char *include_name =
15558 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15559 if (include_name != NULL)
15560 dwarf2_create_include_psymtab (include_name, pst, objfile);
15561 }
15562 }
15563 else
15564 {
15565 /* Make sure a symtab is created for every file, even files
15566 which contain only variables (i.e. no code with associated
15567 line numbers). */
15568 int i;
15569
15570 for (i = 0; i < lh->num_file_names; i++)
15571 {
15572 char *dir = NULL;
15573 struct file_entry *fe;
15574
15575 fe = &lh->file_names[i];
15576 if (fe->dir_index)
15577 dir = lh->include_dirs[fe->dir_index - 1];
15578 dwarf2_start_subfile (fe->name, dir, comp_dir);
15579
15580 /* Skip the main file; we don't need it, and it must be
15581 allocated last, so that it will show up before the
15582 non-primary symtabs in the objfile's symtab list. */
15583 if (current_subfile == first_subfile)
15584 continue;
15585
15586 if (current_subfile->symtab == NULL)
15587 current_subfile->symtab = allocate_symtab (current_subfile->name,
15588 objfile);
15589 fe->symtab = current_subfile->symtab;
15590 }
15591 }
15592 }
15593
15594 /* Start a subfile for DWARF. FILENAME is the name of the file and
15595 DIRNAME the name of the source directory which contains FILENAME
15596 or NULL if not known. COMP_DIR is the compilation directory for the
15597 linetable's compilation unit or NULL if not known.
15598 This routine tries to keep line numbers from identical absolute and
15599 relative file names in a common subfile.
15600
15601 Using the `list' example from the GDB testsuite, which resides in
15602 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15603 of /srcdir/list0.c yields the following debugging information for list0.c:
15604
15605 DW_AT_name: /srcdir/list0.c
15606 DW_AT_comp_dir: /compdir
15607 files.files[0].name: list0.h
15608 files.files[0].dir: /srcdir
15609 files.files[1].name: list0.c
15610 files.files[1].dir: /srcdir
15611
15612 The line number information for list0.c has to end up in a single
15613 subfile, so that `break /srcdir/list0.c:1' works as expected.
15614 start_subfile will ensure that this happens provided that we pass the
15615 concatenation of files.files[1].dir and files.files[1].name as the
15616 subfile's name. */
15617
15618 static void
15619 dwarf2_start_subfile (char *filename, const char *dirname,
15620 const char *comp_dir)
15621 {
15622 char *fullname;
15623
15624 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15625 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15626 second argument to start_subfile. To be consistent, we do the
15627 same here. In order not to lose the line information directory,
15628 we concatenate it to the filename when it makes sense.
15629 Note that the Dwarf3 standard says (speaking of filenames in line
15630 information): ``The directory index is ignored for file names
15631 that represent full path names''. Thus ignoring dirname in the
15632 `else' branch below isn't an issue. */
15633
15634 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15635 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15636 else
15637 fullname = filename;
15638
15639 start_subfile (fullname, comp_dir);
15640
15641 if (fullname != filename)
15642 xfree (fullname);
15643 }
15644
15645 /* Start a symtab for DWARF.
15646 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15647
15648 static void
15649 dwarf2_start_symtab (struct dwarf2_cu *cu,
15650 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15651 {
15652 start_symtab (name, comp_dir, low_pc);
15653 record_debugformat ("DWARF 2");
15654 record_producer (cu->producer);
15655
15656 /* We assume that we're processing GCC output. */
15657 processing_gcc_compilation = 2;
15658
15659 processing_has_namespace_info = 0;
15660 }
15661
15662 static void
15663 var_decode_location (struct attribute *attr, struct symbol *sym,
15664 struct dwarf2_cu *cu)
15665 {
15666 struct objfile *objfile = cu->objfile;
15667 struct comp_unit_head *cu_header = &cu->header;
15668
15669 /* NOTE drow/2003-01-30: There used to be a comment and some special
15670 code here to turn a symbol with DW_AT_external and a
15671 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15672 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15673 with some versions of binutils) where shared libraries could have
15674 relocations against symbols in their debug information - the
15675 minimal symbol would have the right address, but the debug info
15676 would not. It's no longer necessary, because we will explicitly
15677 apply relocations when we read in the debug information now. */
15678
15679 /* A DW_AT_location attribute with no contents indicates that a
15680 variable has been optimized away. */
15681 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15682 {
15683 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15684 return;
15685 }
15686
15687 /* Handle one degenerate form of location expression specially, to
15688 preserve GDB's previous behavior when section offsets are
15689 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15690 then mark this symbol as LOC_STATIC. */
15691
15692 if (attr_form_is_block (attr)
15693 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15694 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15695 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15696 && (DW_BLOCK (attr)->size
15697 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15698 {
15699 unsigned int dummy;
15700
15701 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15702 SYMBOL_VALUE_ADDRESS (sym) =
15703 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15704 else
15705 SYMBOL_VALUE_ADDRESS (sym) =
15706 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15707 SYMBOL_CLASS (sym) = LOC_STATIC;
15708 fixup_symbol_section (sym, objfile);
15709 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15710 SYMBOL_SECTION (sym));
15711 return;
15712 }
15713
15714 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15715 expression evaluator, and use LOC_COMPUTED only when necessary
15716 (i.e. when the value of a register or memory location is
15717 referenced, or a thread-local block, etc.). Then again, it might
15718 not be worthwhile. I'm assuming that it isn't unless performance
15719 or memory numbers show me otherwise. */
15720
15721 dwarf2_symbol_mark_computed (attr, sym, cu);
15722 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15723
15724 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15725 cu->has_loclist = 1;
15726 }
15727
15728 /* Given a pointer to a DWARF information entry, figure out if we need
15729 to make a symbol table entry for it, and if so, create a new entry
15730 and return a pointer to it.
15731 If TYPE is NULL, determine symbol type from the die, otherwise
15732 used the passed type.
15733 If SPACE is not NULL, use it to hold the new symbol. If it is
15734 NULL, allocate a new symbol on the objfile's obstack. */
15735
15736 static struct symbol *
15737 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15738 struct symbol *space)
15739 {
15740 struct objfile *objfile = cu->objfile;
15741 struct symbol *sym = NULL;
15742 const char *name;
15743 struct attribute *attr = NULL;
15744 struct attribute *attr2 = NULL;
15745 CORE_ADDR baseaddr;
15746 struct pending **list_to_add = NULL;
15747
15748 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15749
15750 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15751
15752 name = dwarf2_name (die, cu);
15753 if (name)
15754 {
15755 const char *linkagename;
15756 int suppress_add = 0;
15757
15758 if (space)
15759 sym = space;
15760 else
15761 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15762 OBJSTAT (objfile, n_syms++);
15763
15764 /* Cache this symbol's name and the name's demangled form (if any). */
15765 SYMBOL_SET_LANGUAGE (sym, cu->language);
15766 linkagename = dwarf2_physname (name, die, cu);
15767 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15768
15769 /* Fortran does not have mangling standard and the mangling does differ
15770 between gfortran, iFort etc. */
15771 if (cu->language == language_fortran
15772 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15773 symbol_set_demangled_name (&(sym->ginfo),
15774 dwarf2_full_name (name, die, cu),
15775 NULL);
15776
15777 /* Default assumptions.
15778 Use the passed type or decode it from the die. */
15779 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15780 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15781 if (type != NULL)
15782 SYMBOL_TYPE (sym) = type;
15783 else
15784 SYMBOL_TYPE (sym) = die_type (die, cu);
15785 attr = dwarf2_attr (die,
15786 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15787 cu);
15788 if (attr)
15789 {
15790 SYMBOL_LINE (sym) = DW_UNSND (attr);
15791 }
15792
15793 attr = dwarf2_attr (die,
15794 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15795 cu);
15796 if (attr)
15797 {
15798 int file_index = DW_UNSND (attr);
15799
15800 if (cu->line_header == NULL
15801 || file_index > cu->line_header->num_file_names)
15802 complaint (&symfile_complaints,
15803 _("file index out of range"));
15804 else if (file_index > 0)
15805 {
15806 struct file_entry *fe;
15807
15808 fe = &cu->line_header->file_names[file_index - 1];
15809 SYMBOL_SYMTAB (sym) = fe->symtab;
15810 }
15811 }
15812
15813 switch (die->tag)
15814 {
15815 case DW_TAG_label:
15816 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15817 if (attr)
15818 {
15819 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15820 }
15821 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15822 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15823 SYMBOL_CLASS (sym) = LOC_LABEL;
15824 add_symbol_to_list (sym, cu->list_in_scope);
15825 break;
15826 case DW_TAG_subprogram:
15827 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15828 finish_block. */
15829 SYMBOL_CLASS (sym) = LOC_BLOCK;
15830 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15831 if ((attr2 && (DW_UNSND (attr2) != 0))
15832 || cu->language == language_ada)
15833 {
15834 /* Subprograms marked external are stored as a global symbol.
15835 Ada subprograms, whether marked external or not, are always
15836 stored as a global symbol, because we want to be able to
15837 access them globally. For instance, we want to be able
15838 to break on a nested subprogram without having to
15839 specify the context. */
15840 list_to_add = &global_symbols;
15841 }
15842 else
15843 {
15844 list_to_add = cu->list_in_scope;
15845 }
15846 break;
15847 case DW_TAG_inlined_subroutine:
15848 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15849 finish_block. */
15850 SYMBOL_CLASS (sym) = LOC_BLOCK;
15851 SYMBOL_INLINED (sym) = 1;
15852 list_to_add = cu->list_in_scope;
15853 break;
15854 case DW_TAG_template_value_param:
15855 suppress_add = 1;
15856 /* Fall through. */
15857 case DW_TAG_constant:
15858 case DW_TAG_variable:
15859 case DW_TAG_member:
15860 /* Compilation with minimal debug info may result in
15861 variables with missing type entries. Change the
15862 misleading `void' type to something sensible. */
15863 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15864 SYMBOL_TYPE (sym)
15865 = objfile_type (objfile)->nodebug_data_symbol;
15866
15867 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15868 /* In the case of DW_TAG_member, we should only be called for
15869 static const members. */
15870 if (die->tag == DW_TAG_member)
15871 {
15872 /* dwarf2_add_field uses die_is_declaration,
15873 so we do the same. */
15874 gdb_assert (die_is_declaration (die, cu));
15875 gdb_assert (attr);
15876 }
15877 if (attr)
15878 {
15879 dwarf2_const_value (attr, sym, cu);
15880 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15881 if (!suppress_add)
15882 {
15883 if (attr2 && (DW_UNSND (attr2) != 0))
15884 list_to_add = &global_symbols;
15885 else
15886 list_to_add = cu->list_in_scope;
15887 }
15888 break;
15889 }
15890 attr = dwarf2_attr (die, DW_AT_location, cu);
15891 if (attr)
15892 {
15893 var_decode_location (attr, sym, cu);
15894 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15895
15896 /* Fortran explicitly imports any global symbols to the local
15897 scope by DW_TAG_common_block. */
15898 if (cu->language == language_fortran && die->parent
15899 && die->parent->tag == DW_TAG_common_block)
15900 attr2 = NULL;
15901
15902 if (SYMBOL_CLASS (sym) == LOC_STATIC
15903 && SYMBOL_VALUE_ADDRESS (sym) == 0
15904 && !dwarf2_per_objfile->has_section_at_zero)
15905 {
15906 /* When a static variable is eliminated by the linker,
15907 the corresponding debug information is not stripped
15908 out, but the variable address is set to null;
15909 do not add such variables into symbol table. */
15910 }
15911 else if (attr2 && (DW_UNSND (attr2) != 0))
15912 {
15913 /* Workaround gfortran PR debug/40040 - it uses
15914 DW_AT_location for variables in -fPIC libraries which may
15915 get overriden by other libraries/executable and get
15916 a different address. Resolve it by the minimal symbol
15917 which may come from inferior's executable using copy
15918 relocation. Make this workaround only for gfortran as for
15919 other compilers GDB cannot guess the minimal symbol
15920 Fortran mangling kind. */
15921 if (cu->language == language_fortran && die->parent
15922 && die->parent->tag == DW_TAG_module
15923 && cu->producer
15924 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15925 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15926
15927 /* A variable with DW_AT_external is never static,
15928 but it may be block-scoped. */
15929 list_to_add = (cu->list_in_scope == &file_symbols
15930 ? &global_symbols : cu->list_in_scope);
15931 }
15932 else
15933 list_to_add = cu->list_in_scope;
15934 }
15935 else
15936 {
15937 /* We do not know the address of this symbol.
15938 If it is an external symbol and we have type information
15939 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15940 The address of the variable will then be determined from
15941 the minimal symbol table whenever the variable is
15942 referenced. */
15943 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15944
15945 /* Fortran explicitly imports any global symbols to the local
15946 scope by DW_TAG_common_block. */
15947 if (cu->language == language_fortran && die->parent
15948 && die->parent->tag == DW_TAG_common_block)
15949 {
15950 /* SYMBOL_CLASS doesn't matter here because
15951 read_common_block is going to reset it. */
15952 if (!suppress_add)
15953 list_to_add = cu->list_in_scope;
15954 }
15955 else if (attr2 && (DW_UNSND (attr2) != 0)
15956 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15957 {
15958 /* A variable with DW_AT_external is never static, but it
15959 may be block-scoped. */
15960 list_to_add = (cu->list_in_scope == &file_symbols
15961 ? &global_symbols : cu->list_in_scope);
15962
15963 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15964 }
15965 else if (!die_is_declaration (die, cu))
15966 {
15967 /* Use the default LOC_OPTIMIZED_OUT class. */
15968 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15969 if (!suppress_add)
15970 list_to_add = cu->list_in_scope;
15971 }
15972 }
15973 break;
15974 case DW_TAG_formal_parameter:
15975 /* If we are inside a function, mark this as an argument. If
15976 not, we might be looking at an argument to an inlined function
15977 when we do not have enough information to show inlined frames;
15978 pretend it's a local variable in that case so that the user can
15979 still see it. */
15980 if (context_stack_depth > 0
15981 && context_stack[context_stack_depth - 1].name != NULL)
15982 SYMBOL_IS_ARGUMENT (sym) = 1;
15983 attr = dwarf2_attr (die, DW_AT_location, cu);
15984 if (attr)
15985 {
15986 var_decode_location (attr, sym, cu);
15987 }
15988 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15989 if (attr)
15990 {
15991 dwarf2_const_value (attr, sym, cu);
15992 }
15993
15994 list_to_add = cu->list_in_scope;
15995 break;
15996 case DW_TAG_unspecified_parameters:
15997 /* From varargs functions; gdb doesn't seem to have any
15998 interest in this information, so just ignore it for now.
15999 (FIXME?) */
16000 break;
16001 case DW_TAG_template_type_param:
16002 suppress_add = 1;
16003 /* Fall through. */
16004 case DW_TAG_class_type:
16005 case DW_TAG_interface_type:
16006 case DW_TAG_structure_type:
16007 case DW_TAG_union_type:
16008 case DW_TAG_set_type:
16009 case DW_TAG_enumeration_type:
16010 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16011 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16012
16013 {
16014 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16015 really ever be static objects: otherwise, if you try
16016 to, say, break of a class's method and you're in a file
16017 which doesn't mention that class, it won't work unless
16018 the check for all static symbols in lookup_symbol_aux
16019 saves you. See the OtherFileClass tests in
16020 gdb.c++/namespace.exp. */
16021
16022 if (!suppress_add)
16023 {
16024 list_to_add = (cu->list_in_scope == &file_symbols
16025 && (cu->language == language_cplus
16026 || cu->language == language_java)
16027 ? &global_symbols : cu->list_in_scope);
16028
16029 /* The semantics of C++ state that "struct foo {
16030 ... }" also defines a typedef for "foo". A Java
16031 class declaration also defines a typedef for the
16032 class. */
16033 if (cu->language == language_cplus
16034 || cu->language == language_java
16035 || cu->language == language_ada)
16036 {
16037 /* The symbol's name is already allocated along
16038 with this objfile, so we don't need to
16039 duplicate it for the type. */
16040 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16041 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16042 }
16043 }
16044 }
16045 break;
16046 case DW_TAG_typedef:
16047 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16048 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16049 list_to_add = cu->list_in_scope;
16050 break;
16051 case DW_TAG_base_type:
16052 case DW_TAG_subrange_type:
16053 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16054 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16055 list_to_add = cu->list_in_scope;
16056 break;
16057 case DW_TAG_enumerator:
16058 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16059 if (attr)
16060 {
16061 dwarf2_const_value (attr, sym, cu);
16062 }
16063 {
16064 /* NOTE: carlton/2003-11-10: See comment above in the
16065 DW_TAG_class_type, etc. block. */
16066
16067 list_to_add = (cu->list_in_scope == &file_symbols
16068 && (cu->language == language_cplus
16069 || cu->language == language_java)
16070 ? &global_symbols : cu->list_in_scope);
16071 }
16072 break;
16073 case DW_TAG_namespace:
16074 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16075 list_to_add = &global_symbols;
16076 break;
16077 case DW_TAG_common_block:
16078 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
16079 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16080 add_symbol_to_list (sym, cu->list_in_scope);
16081 break;
16082 default:
16083 /* Not a tag we recognize. Hopefully we aren't processing
16084 trash data, but since we must specifically ignore things
16085 we don't recognize, there is nothing else we should do at
16086 this point. */
16087 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16088 dwarf_tag_name (die->tag));
16089 break;
16090 }
16091
16092 if (suppress_add)
16093 {
16094 sym->hash_next = objfile->template_symbols;
16095 objfile->template_symbols = sym;
16096 list_to_add = NULL;
16097 }
16098
16099 if (list_to_add != NULL)
16100 add_symbol_to_list (sym, list_to_add);
16101
16102 /* For the benefit of old versions of GCC, check for anonymous
16103 namespaces based on the demangled name. */
16104 if (!processing_has_namespace_info
16105 && cu->language == language_cplus)
16106 cp_scan_for_anonymous_namespaces (sym, objfile);
16107 }
16108 return (sym);
16109 }
16110
16111 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16112
16113 static struct symbol *
16114 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16115 {
16116 return new_symbol_full (die, type, cu, NULL);
16117 }
16118
16119 /* Given an attr with a DW_FORM_dataN value in host byte order,
16120 zero-extend it as appropriate for the symbol's type. The DWARF
16121 standard (v4) is not entirely clear about the meaning of using
16122 DW_FORM_dataN for a constant with a signed type, where the type is
16123 wider than the data. The conclusion of a discussion on the DWARF
16124 list was that this is unspecified. We choose to always zero-extend
16125 because that is the interpretation long in use by GCC. */
16126
16127 static gdb_byte *
16128 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16129 const char *name, struct obstack *obstack,
16130 struct dwarf2_cu *cu, LONGEST *value, int bits)
16131 {
16132 struct objfile *objfile = cu->objfile;
16133 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16134 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16135 LONGEST l = DW_UNSND (attr);
16136
16137 if (bits < sizeof (*value) * 8)
16138 {
16139 l &= ((LONGEST) 1 << bits) - 1;
16140 *value = l;
16141 }
16142 else if (bits == sizeof (*value) * 8)
16143 *value = l;
16144 else
16145 {
16146 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16147 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16148 return bytes;
16149 }
16150
16151 return NULL;
16152 }
16153
16154 /* Read a constant value from an attribute. Either set *VALUE, or if
16155 the value does not fit in *VALUE, set *BYTES - either already
16156 allocated on the objfile obstack, or newly allocated on OBSTACK,
16157 or, set *BATON, if we translated the constant to a location
16158 expression. */
16159
16160 static void
16161 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16162 const char *name, struct obstack *obstack,
16163 struct dwarf2_cu *cu,
16164 LONGEST *value, gdb_byte **bytes,
16165 struct dwarf2_locexpr_baton **baton)
16166 {
16167 struct objfile *objfile = cu->objfile;
16168 struct comp_unit_head *cu_header = &cu->header;
16169 struct dwarf_block *blk;
16170 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16171 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16172
16173 *value = 0;
16174 *bytes = NULL;
16175 *baton = NULL;
16176
16177 switch (attr->form)
16178 {
16179 case DW_FORM_addr:
16180 case DW_FORM_GNU_addr_index:
16181 {
16182 gdb_byte *data;
16183
16184 if (TYPE_LENGTH (type) != cu_header->addr_size)
16185 dwarf2_const_value_length_mismatch_complaint (name,
16186 cu_header->addr_size,
16187 TYPE_LENGTH (type));
16188 /* Symbols of this form are reasonably rare, so we just
16189 piggyback on the existing location code rather than writing
16190 a new implementation of symbol_computed_ops. */
16191 *baton = obstack_alloc (&objfile->objfile_obstack,
16192 sizeof (struct dwarf2_locexpr_baton));
16193 (*baton)->per_cu = cu->per_cu;
16194 gdb_assert ((*baton)->per_cu);
16195
16196 (*baton)->size = 2 + cu_header->addr_size;
16197 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16198 (*baton)->data = data;
16199
16200 data[0] = DW_OP_addr;
16201 store_unsigned_integer (&data[1], cu_header->addr_size,
16202 byte_order, DW_ADDR (attr));
16203 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16204 }
16205 break;
16206 case DW_FORM_string:
16207 case DW_FORM_strp:
16208 case DW_FORM_GNU_str_index:
16209 case DW_FORM_GNU_strp_alt:
16210 /* DW_STRING is already allocated on the objfile obstack, point
16211 directly to it. */
16212 *bytes = (gdb_byte *) DW_STRING (attr);
16213 break;
16214 case DW_FORM_block1:
16215 case DW_FORM_block2:
16216 case DW_FORM_block4:
16217 case DW_FORM_block:
16218 case DW_FORM_exprloc:
16219 blk = DW_BLOCK (attr);
16220 if (TYPE_LENGTH (type) != blk->size)
16221 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16222 TYPE_LENGTH (type));
16223 *bytes = blk->data;
16224 break;
16225
16226 /* The DW_AT_const_value attributes are supposed to carry the
16227 symbol's value "represented as it would be on the target
16228 architecture." By the time we get here, it's already been
16229 converted to host endianness, so we just need to sign- or
16230 zero-extend it as appropriate. */
16231 case DW_FORM_data1:
16232 *bytes = dwarf2_const_value_data (attr, type, name,
16233 obstack, cu, value, 8);
16234 break;
16235 case DW_FORM_data2:
16236 *bytes = dwarf2_const_value_data (attr, type, name,
16237 obstack, cu, value, 16);
16238 break;
16239 case DW_FORM_data4:
16240 *bytes = dwarf2_const_value_data (attr, type, name,
16241 obstack, cu, value, 32);
16242 break;
16243 case DW_FORM_data8:
16244 *bytes = dwarf2_const_value_data (attr, type, name,
16245 obstack, cu, value, 64);
16246 break;
16247
16248 case DW_FORM_sdata:
16249 *value = DW_SND (attr);
16250 break;
16251
16252 case DW_FORM_udata:
16253 *value = DW_UNSND (attr);
16254 break;
16255
16256 default:
16257 complaint (&symfile_complaints,
16258 _("unsupported const value attribute form: '%s'"),
16259 dwarf_form_name (attr->form));
16260 *value = 0;
16261 break;
16262 }
16263 }
16264
16265
16266 /* Copy constant value from an attribute to a symbol. */
16267
16268 static void
16269 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16270 struct dwarf2_cu *cu)
16271 {
16272 struct objfile *objfile = cu->objfile;
16273 struct comp_unit_head *cu_header = &cu->header;
16274 LONGEST value;
16275 gdb_byte *bytes;
16276 struct dwarf2_locexpr_baton *baton;
16277
16278 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16279 SYMBOL_PRINT_NAME (sym),
16280 &objfile->objfile_obstack, cu,
16281 &value, &bytes, &baton);
16282
16283 if (baton != NULL)
16284 {
16285 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16286 SYMBOL_LOCATION_BATON (sym) = baton;
16287 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16288 }
16289 else if (bytes != NULL)
16290 {
16291 SYMBOL_VALUE_BYTES (sym) = bytes;
16292 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16293 }
16294 else
16295 {
16296 SYMBOL_VALUE (sym) = value;
16297 SYMBOL_CLASS (sym) = LOC_CONST;
16298 }
16299 }
16300
16301 /* Return the type of the die in question using its DW_AT_type attribute. */
16302
16303 static struct type *
16304 die_type (struct die_info *die, struct dwarf2_cu *cu)
16305 {
16306 struct attribute *type_attr;
16307
16308 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16309 if (!type_attr)
16310 {
16311 /* A missing DW_AT_type represents a void type. */
16312 return objfile_type (cu->objfile)->builtin_void;
16313 }
16314
16315 return lookup_die_type (die, type_attr, cu);
16316 }
16317
16318 /* True iff CU's producer generates GNAT Ada auxiliary information
16319 that allows to find parallel types through that information instead
16320 of having to do expensive parallel lookups by type name. */
16321
16322 static int
16323 need_gnat_info (struct dwarf2_cu *cu)
16324 {
16325 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16326 of GNAT produces this auxiliary information, without any indication
16327 that it is produced. Part of enhancing the FSF version of GNAT
16328 to produce that information will be to put in place an indicator
16329 that we can use in order to determine whether the descriptive type
16330 info is available or not. One suggestion that has been made is
16331 to use a new attribute, attached to the CU die. For now, assume
16332 that the descriptive type info is not available. */
16333 return 0;
16334 }
16335
16336 /* Return the auxiliary type of the die in question using its
16337 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16338 attribute is not present. */
16339
16340 static struct type *
16341 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16342 {
16343 struct attribute *type_attr;
16344
16345 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16346 if (!type_attr)
16347 return NULL;
16348
16349 return lookup_die_type (die, type_attr, cu);
16350 }
16351
16352 /* If DIE has a descriptive_type attribute, then set the TYPE's
16353 descriptive type accordingly. */
16354
16355 static void
16356 set_descriptive_type (struct type *type, struct die_info *die,
16357 struct dwarf2_cu *cu)
16358 {
16359 struct type *descriptive_type = die_descriptive_type (die, cu);
16360
16361 if (descriptive_type)
16362 {
16363 ALLOCATE_GNAT_AUX_TYPE (type);
16364 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16365 }
16366 }
16367
16368 /* Return the containing type of the die in question using its
16369 DW_AT_containing_type attribute. */
16370
16371 static struct type *
16372 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct attribute *type_attr;
16375
16376 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16377 if (!type_attr)
16378 error (_("Dwarf Error: Problem turning containing type into gdb type "
16379 "[in module %s]"), cu->objfile->name);
16380
16381 return lookup_die_type (die, type_attr, cu);
16382 }
16383
16384 /* Look up the type of DIE in CU using its type attribute ATTR.
16385 If there is no type substitute an error marker. */
16386
16387 static struct type *
16388 lookup_die_type (struct die_info *die, struct attribute *attr,
16389 struct dwarf2_cu *cu)
16390 {
16391 struct objfile *objfile = cu->objfile;
16392 struct type *this_type;
16393
16394 /* First see if we have it cached. */
16395
16396 if (attr->form == DW_FORM_GNU_ref_alt)
16397 {
16398 struct dwarf2_per_cu_data *per_cu;
16399 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16400
16401 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16402 this_type = get_die_type_at_offset (offset, per_cu);
16403 }
16404 else if (is_ref_attr (attr))
16405 {
16406 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16407
16408 this_type = get_die_type_at_offset (offset, cu->per_cu);
16409 }
16410 else if (attr->form == DW_FORM_ref_sig8)
16411 {
16412 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16413
16414 /* sig_type will be NULL if the signatured type is missing from
16415 the debug info. */
16416 if (sig_type == NULL)
16417 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16418 "at 0x%x [in module %s]"),
16419 die->offset.sect_off, objfile->name);
16420
16421 gdb_assert (sig_type->per_cu.is_debug_types);
16422 /* If we haven't filled in type_offset_in_section yet, then we
16423 haven't read the type in yet. */
16424 this_type = NULL;
16425 if (sig_type->type_offset_in_section.sect_off != 0)
16426 {
16427 this_type =
16428 get_die_type_at_offset (sig_type->type_offset_in_section,
16429 &sig_type->per_cu);
16430 }
16431 }
16432 else
16433 {
16434 dump_die_for_error (die);
16435 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16436 dwarf_attr_name (attr->name), objfile->name);
16437 }
16438
16439 /* If not cached we need to read it in. */
16440
16441 if (this_type == NULL)
16442 {
16443 struct die_info *type_die;
16444 struct dwarf2_cu *type_cu = cu;
16445
16446 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16447 /* If we found the type now, it's probably because the type came
16448 from an inter-CU reference and the type's CU got expanded before
16449 ours. */
16450 this_type = get_die_type (type_die, type_cu);
16451 if (this_type == NULL)
16452 this_type = read_type_die_1 (type_die, type_cu);
16453 }
16454
16455 /* If we still don't have a type use an error marker. */
16456
16457 if (this_type == NULL)
16458 {
16459 char *message, *saved;
16460
16461 /* read_type_die already issued a complaint. */
16462 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16463 objfile->name,
16464 cu->header.offset.sect_off,
16465 die->offset.sect_off);
16466 saved = obstack_copy0 (&objfile->objfile_obstack,
16467 message, strlen (message));
16468 xfree (message);
16469
16470 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16471 }
16472
16473 return this_type;
16474 }
16475
16476 /* Return the type in DIE, CU.
16477 Returns NULL for invalid types.
16478
16479 This first does a lookup in the appropriate type_hash table,
16480 and only reads the die in if necessary.
16481
16482 NOTE: This can be called when reading in partial or full symbols. */
16483
16484 static struct type *
16485 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16486 {
16487 struct type *this_type;
16488
16489 this_type = get_die_type (die, cu);
16490 if (this_type)
16491 return this_type;
16492
16493 return read_type_die_1 (die, cu);
16494 }
16495
16496 /* Read the type in DIE, CU.
16497 Returns NULL for invalid types. */
16498
16499 static struct type *
16500 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16501 {
16502 struct type *this_type = NULL;
16503
16504 switch (die->tag)
16505 {
16506 case DW_TAG_class_type:
16507 case DW_TAG_interface_type:
16508 case DW_TAG_structure_type:
16509 case DW_TAG_union_type:
16510 this_type = read_structure_type (die, cu);
16511 break;
16512 case DW_TAG_enumeration_type:
16513 this_type = read_enumeration_type (die, cu);
16514 break;
16515 case DW_TAG_subprogram:
16516 case DW_TAG_subroutine_type:
16517 case DW_TAG_inlined_subroutine:
16518 this_type = read_subroutine_type (die, cu);
16519 break;
16520 case DW_TAG_array_type:
16521 this_type = read_array_type (die, cu);
16522 break;
16523 case DW_TAG_set_type:
16524 this_type = read_set_type (die, cu);
16525 break;
16526 case DW_TAG_pointer_type:
16527 this_type = read_tag_pointer_type (die, cu);
16528 break;
16529 case DW_TAG_ptr_to_member_type:
16530 this_type = read_tag_ptr_to_member_type (die, cu);
16531 break;
16532 case DW_TAG_reference_type:
16533 this_type = read_tag_reference_type (die, cu);
16534 break;
16535 case DW_TAG_const_type:
16536 this_type = read_tag_const_type (die, cu);
16537 break;
16538 case DW_TAG_volatile_type:
16539 this_type = read_tag_volatile_type (die, cu);
16540 break;
16541 case DW_TAG_restrict_type:
16542 this_type = read_tag_restrict_type (die, cu);
16543 break;
16544 case DW_TAG_string_type:
16545 this_type = read_tag_string_type (die, cu);
16546 break;
16547 case DW_TAG_typedef:
16548 this_type = read_typedef (die, cu);
16549 break;
16550 case DW_TAG_subrange_type:
16551 this_type = read_subrange_type (die, cu);
16552 break;
16553 case DW_TAG_base_type:
16554 this_type = read_base_type (die, cu);
16555 break;
16556 case DW_TAG_unspecified_type:
16557 this_type = read_unspecified_type (die, cu);
16558 break;
16559 case DW_TAG_namespace:
16560 this_type = read_namespace_type (die, cu);
16561 break;
16562 case DW_TAG_module:
16563 this_type = read_module_type (die, cu);
16564 break;
16565 default:
16566 complaint (&symfile_complaints,
16567 _("unexpected tag in read_type_die: '%s'"),
16568 dwarf_tag_name (die->tag));
16569 break;
16570 }
16571
16572 return this_type;
16573 }
16574
16575 /* See if we can figure out if the class lives in a namespace. We do
16576 this by looking for a member function; its demangled name will
16577 contain namespace info, if there is any.
16578 Return the computed name or NULL.
16579 Space for the result is allocated on the objfile's obstack.
16580 This is the full-die version of guess_partial_die_structure_name.
16581 In this case we know DIE has no useful parent. */
16582
16583 static char *
16584 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16585 {
16586 struct die_info *spec_die;
16587 struct dwarf2_cu *spec_cu;
16588 struct die_info *child;
16589
16590 spec_cu = cu;
16591 spec_die = die_specification (die, &spec_cu);
16592 if (spec_die != NULL)
16593 {
16594 die = spec_die;
16595 cu = spec_cu;
16596 }
16597
16598 for (child = die->child;
16599 child != NULL;
16600 child = child->sibling)
16601 {
16602 if (child->tag == DW_TAG_subprogram)
16603 {
16604 struct attribute *attr;
16605
16606 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16607 if (attr == NULL)
16608 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16609 if (attr != NULL)
16610 {
16611 char *actual_name
16612 = language_class_name_from_physname (cu->language_defn,
16613 DW_STRING (attr));
16614 char *name = NULL;
16615
16616 if (actual_name != NULL)
16617 {
16618 const char *die_name = dwarf2_name (die, cu);
16619
16620 if (die_name != NULL
16621 && strcmp (die_name, actual_name) != 0)
16622 {
16623 /* Strip off the class name from the full name.
16624 We want the prefix. */
16625 int die_name_len = strlen (die_name);
16626 int actual_name_len = strlen (actual_name);
16627
16628 /* Test for '::' as a sanity check. */
16629 if (actual_name_len > die_name_len + 2
16630 && actual_name[actual_name_len
16631 - die_name_len - 1] == ':')
16632 name =
16633 obsavestring (actual_name,
16634 actual_name_len - die_name_len - 2,
16635 &cu->objfile->objfile_obstack);
16636 }
16637 }
16638 xfree (actual_name);
16639 return name;
16640 }
16641 }
16642 }
16643
16644 return NULL;
16645 }
16646
16647 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16648 prefix part in such case. See
16649 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16650
16651 static char *
16652 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16653 {
16654 struct attribute *attr;
16655 char *base;
16656
16657 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16658 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16659 return NULL;
16660
16661 attr = dwarf2_attr (die, DW_AT_name, cu);
16662 if (attr != NULL && DW_STRING (attr) != NULL)
16663 return NULL;
16664
16665 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16666 if (attr == NULL)
16667 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16668 if (attr == NULL || DW_STRING (attr) == NULL)
16669 return NULL;
16670
16671 /* dwarf2_name had to be already called. */
16672 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16673
16674 /* Strip the base name, keep any leading namespaces/classes. */
16675 base = strrchr (DW_STRING (attr), ':');
16676 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16677 return "";
16678
16679 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16680 &cu->objfile->objfile_obstack);
16681 }
16682
16683 /* Return the name of the namespace/class that DIE is defined within,
16684 or "" if we can't tell. The caller should not xfree the result.
16685
16686 For example, if we're within the method foo() in the following
16687 code:
16688
16689 namespace N {
16690 class C {
16691 void foo () {
16692 }
16693 };
16694 }
16695
16696 then determine_prefix on foo's die will return "N::C". */
16697
16698 static const char *
16699 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16700 {
16701 struct die_info *parent, *spec_die;
16702 struct dwarf2_cu *spec_cu;
16703 struct type *parent_type;
16704 char *retval;
16705
16706 if (cu->language != language_cplus && cu->language != language_java
16707 && cu->language != language_fortran)
16708 return "";
16709
16710 retval = anonymous_struct_prefix (die, cu);
16711 if (retval)
16712 return retval;
16713
16714 /* We have to be careful in the presence of DW_AT_specification.
16715 For example, with GCC 3.4, given the code
16716
16717 namespace N {
16718 void foo() {
16719 // Definition of N::foo.
16720 }
16721 }
16722
16723 then we'll have a tree of DIEs like this:
16724
16725 1: DW_TAG_compile_unit
16726 2: DW_TAG_namespace // N
16727 3: DW_TAG_subprogram // declaration of N::foo
16728 4: DW_TAG_subprogram // definition of N::foo
16729 DW_AT_specification // refers to die #3
16730
16731 Thus, when processing die #4, we have to pretend that we're in
16732 the context of its DW_AT_specification, namely the contex of die
16733 #3. */
16734 spec_cu = cu;
16735 spec_die = die_specification (die, &spec_cu);
16736 if (spec_die == NULL)
16737 parent = die->parent;
16738 else
16739 {
16740 parent = spec_die->parent;
16741 cu = spec_cu;
16742 }
16743
16744 if (parent == NULL)
16745 return "";
16746 else if (parent->building_fullname)
16747 {
16748 const char *name;
16749 const char *parent_name;
16750
16751 /* It has been seen on RealView 2.2 built binaries,
16752 DW_TAG_template_type_param types actually _defined_ as
16753 children of the parent class:
16754
16755 enum E {};
16756 template class <class Enum> Class{};
16757 Class<enum E> class_e;
16758
16759 1: DW_TAG_class_type (Class)
16760 2: DW_TAG_enumeration_type (E)
16761 3: DW_TAG_enumerator (enum1:0)
16762 3: DW_TAG_enumerator (enum2:1)
16763 ...
16764 2: DW_TAG_template_type_param
16765 DW_AT_type DW_FORM_ref_udata (E)
16766
16767 Besides being broken debug info, it can put GDB into an
16768 infinite loop. Consider:
16769
16770 When we're building the full name for Class<E>, we'll start
16771 at Class, and go look over its template type parameters,
16772 finding E. We'll then try to build the full name of E, and
16773 reach here. We're now trying to build the full name of E,
16774 and look over the parent DIE for containing scope. In the
16775 broken case, if we followed the parent DIE of E, we'd again
16776 find Class, and once again go look at its template type
16777 arguments, etc., etc. Simply don't consider such parent die
16778 as source-level parent of this die (it can't be, the language
16779 doesn't allow it), and break the loop here. */
16780 name = dwarf2_name (die, cu);
16781 parent_name = dwarf2_name (parent, cu);
16782 complaint (&symfile_complaints,
16783 _("template param type '%s' defined within parent '%s'"),
16784 name ? name : "<unknown>",
16785 parent_name ? parent_name : "<unknown>");
16786 return "";
16787 }
16788 else
16789 switch (parent->tag)
16790 {
16791 case DW_TAG_namespace:
16792 parent_type = read_type_die (parent, cu);
16793 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16794 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16795 Work around this problem here. */
16796 if (cu->language == language_cplus
16797 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16798 return "";
16799 /* We give a name to even anonymous namespaces. */
16800 return TYPE_TAG_NAME (parent_type);
16801 case DW_TAG_class_type:
16802 case DW_TAG_interface_type:
16803 case DW_TAG_structure_type:
16804 case DW_TAG_union_type:
16805 case DW_TAG_module:
16806 parent_type = read_type_die (parent, cu);
16807 if (TYPE_TAG_NAME (parent_type) != NULL)
16808 return TYPE_TAG_NAME (parent_type);
16809 else
16810 /* An anonymous structure is only allowed non-static data
16811 members; no typedefs, no member functions, et cetera.
16812 So it does not need a prefix. */
16813 return "";
16814 case DW_TAG_compile_unit:
16815 case DW_TAG_partial_unit:
16816 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16817 if (cu->language == language_cplus
16818 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16819 && die->child != NULL
16820 && (die->tag == DW_TAG_class_type
16821 || die->tag == DW_TAG_structure_type
16822 || die->tag == DW_TAG_union_type))
16823 {
16824 char *name = guess_full_die_structure_name (die, cu);
16825 if (name != NULL)
16826 return name;
16827 }
16828 return "";
16829 default:
16830 return determine_prefix (parent, cu);
16831 }
16832 }
16833
16834 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16835 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16836 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16837 an obconcat, otherwise allocate storage for the result. The CU argument is
16838 used to determine the language and hence, the appropriate separator. */
16839
16840 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16841
16842 static char *
16843 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16844 int physname, struct dwarf2_cu *cu)
16845 {
16846 const char *lead = "";
16847 const char *sep;
16848
16849 if (suffix == NULL || suffix[0] == '\0'
16850 || prefix == NULL || prefix[0] == '\0')
16851 sep = "";
16852 else if (cu->language == language_java)
16853 sep = ".";
16854 else if (cu->language == language_fortran && physname)
16855 {
16856 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16857 DW_AT_MIPS_linkage_name is preferred and used instead. */
16858
16859 lead = "__";
16860 sep = "_MOD_";
16861 }
16862 else
16863 sep = "::";
16864
16865 if (prefix == NULL)
16866 prefix = "";
16867 if (suffix == NULL)
16868 suffix = "";
16869
16870 if (obs == NULL)
16871 {
16872 char *retval
16873 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16874
16875 strcpy (retval, lead);
16876 strcat (retval, prefix);
16877 strcat (retval, sep);
16878 strcat (retval, suffix);
16879 return retval;
16880 }
16881 else
16882 {
16883 /* We have an obstack. */
16884 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16885 }
16886 }
16887
16888 /* Return sibling of die, NULL if no sibling. */
16889
16890 static struct die_info *
16891 sibling_die (struct die_info *die)
16892 {
16893 return die->sibling;
16894 }
16895
16896 /* Get name of a die, return NULL if not found. */
16897
16898 static const char *
16899 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16900 struct obstack *obstack)
16901 {
16902 if (name && cu->language == language_cplus)
16903 {
16904 char *canon_name = cp_canonicalize_string (name);
16905
16906 if (canon_name != NULL)
16907 {
16908 if (strcmp (canon_name, name) != 0)
16909 name = obsavestring (canon_name, strlen (canon_name),
16910 obstack);
16911 xfree (canon_name);
16912 }
16913 }
16914
16915 return name;
16916 }
16917
16918 /* Get name of a die, return NULL if not found. */
16919
16920 static const char *
16921 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16922 {
16923 struct attribute *attr;
16924
16925 attr = dwarf2_attr (die, DW_AT_name, cu);
16926 if ((!attr || !DW_STRING (attr))
16927 && die->tag != DW_TAG_class_type
16928 && die->tag != DW_TAG_interface_type
16929 && die->tag != DW_TAG_structure_type
16930 && die->tag != DW_TAG_union_type)
16931 return NULL;
16932
16933 switch (die->tag)
16934 {
16935 case DW_TAG_compile_unit:
16936 case DW_TAG_partial_unit:
16937 /* Compilation units have a DW_AT_name that is a filename, not
16938 a source language identifier. */
16939 case DW_TAG_enumeration_type:
16940 case DW_TAG_enumerator:
16941 /* These tags always have simple identifiers already; no need
16942 to canonicalize them. */
16943 return DW_STRING (attr);
16944
16945 case DW_TAG_subprogram:
16946 /* Java constructors will all be named "<init>", so return
16947 the class name when we see this special case. */
16948 if (cu->language == language_java
16949 && DW_STRING (attr) != NULL
16950 && strcmp (DW_STRING (attr), "<init>") == 0)
16951 {
16952 struct dwarf2_cu *spec_cu = cu;
16953 struct die_info *spec_die;
16954
16955 /* GCJ will output '<init>' for Java constructor names.
16956 For this special case, return the name of the parent class. */
16957
16958 /* GCJ may output suprogram DIEs with AT_specification set.
16959 If so, use the name of the specified DIE. */
16960 spec_die = die_specification (die, &spec_cu);
16961 if (spec_die != NULL)
16962 return dwarf2_name (spec_die, spec_cu);
16963
16964 do
16965 {
16966 die = die->parent;
16967 if (die->tag == DW_TAG_class_type)
16968 return dwarf2_name (die, cu);
16969 }
16970 while (die->tag != DW_TAG_compile_unit
16971 && die->tag != DW_TAG_partial_unit);
16972 }
16973 break;
16974
16975 case DW_TAG_class_type:
16976 case DW_TAG_interface_type:
16977 case DW_TAG_structure_type:
16978 case DW_TAG_union_type:
16979 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16980 structures or unions. These were of the form "._%d" in GCC 4.1,
16981 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16982 and GCC 4.4. We work around this problem by ignoring these. */
16983 if (attr && DW_STRING (attr)
16984 && (strncmp (DW_STRING (attr), "._", 2) == 0
16985 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16986 return NULL;
16987
16988 /* GCC might emit a nameless typedef that has a linkage name. See
16989 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16990 if (!attr || DW_STRING (attr) == NULL)
16991 {
16992 char *demangled = NULL;
16993
16994 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16995 if (attr == NULL)
16996 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16997
16998 if (attr == NULL || DW_STRING (attr) == NULL)
16999 return NULL;
17000
17001 /* Avoid demangling DW_STRING (attr) the second time on a second
17002 call for the same DIE. */
17003 if (!DW_STRING_IS_CANONICAL (attr))
17004 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17005
17006 if (demangled)
17007 {
17008 char *base;
17009
17010 /* FIXME: we already did this for the partial symbol... */
17011 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
17012 &cu->objfile->objfile_obstack);
17013 DW_STRING_IS_CANONICAL (attr) = 1;
17014 xfree (demangled);
17015
17016 /* Strip any leading namespaces/classes, keep only the base name.
17017 DW_AT_name for named DIEs does not contain the prefixes. */
17018 base = strrchr (DW_STRING (attr), ':');
17019 if (base && base > DW_STRING (attr) && base[-1] == ':')
17020 return &base[1];
17021 else
17022 return DW_STRING (attr);
17023 }
17024 }
17025 break;
17026
17027 default:
17028 break;
17029 }
17030
17031 if (!DW_STRING_IS_CANONICAL (attr))
17032 {
17033 DW_STRING (attr)
17034 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17035 &cu->objfile->objfile_obstack);
17036 DW_STRING_IS_CANONICAL (attr) = 1;
17037 }
17038 return DW_STRING (attr);
17039 }
17040
17041 /* Return the die that this die in an extension of, or NULL if there
17042 is none. *EXT_CU is the CU containing DIE on input, and the CU
17043 containing the return value on output. */
17044
17045 static struct die_info *
17046 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17047 {
17048 struct attribute *attr;
17049
17050 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17051 if (attr == NULL)
17052 return NULL;
17053
17054 return follow_die_ref (die, attr, ext_cu);
17055 }
17056
17057 /* Convert a DIE tag into its string name. */
17058
17059 static const char *
17060 dwarf_tag_name (unsigned tag)
17061 {
17062 const char *name = get_DW_TAG_name (tag);
17063
17064 if (name == NULL)
17065 return "DW_TAG_<unknown>";
17066
17067 return name;
17068 }
17069
17070 /* Convert a DWARF attribute code into its string name. */
17071
17072 static const char *
17073 dwarf_attr_name (unsigned attr)
17074 {
17075 const char *name;
17076
17077 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17078 if (attr == DW_AT_MIPS_fde)
17079 return "DW_AT_MIPS_fde";
17080 #else
17081 if (attr == DW_AT_HP_block_index)
17082 return "DW_AT_HP_block_index";
17083 #endif
17084
17085 name = get_DW_AT_name (attr);
17086
17087 if (name == NULL)
17088 return "DW_AT_<unknown>";
17089
17090 return name;
17091 }
17092
17093 /* Convert a DWARF value form code into its string name. */
17094
17095 static const char *
17096 dwarf_form_name (unsigned form)
17097 {
17098 const char *name = get_DW_FORM_name (form);
17099
17100 if (name == NULL)
17101 return "DW_FORM_<unknown>";
17102
17103 return name;
17104 }
17105
17106 static char *
17107 dwarf_bool_name (unsigned mybool)
17108 {
17109 if (mybool)
17110 return "TRUE";
17111 else
17112 return "FALSE";
17113 }
17114
17115 /* Convert a DWARF type code into its string name. */
17116
17117 static const char *
17118 dwarf_type_encoding_name (unsigned enc)
17119 {
17120 const char *name = get_DW_ATE_name (enc);
17121
17122 if (name == NULL)
17123 return "DW_ATE_<unknown>";
17124
17125 return name;
17126 }
17127
17128 static void
17129 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17130 {
17131 unsigned int i;
17132
17133 print_spaces (indent, f);
17134 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17135 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17136
17137 if (die->parent != NULL)
17138 {
17139 print_spaces (indent, f);
17140 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17141 die->parent->offset.sect_off);
17142 }
17143
17144 print_spaces (indent, f);
17145 fprintf_unfiltered (f, " has children: %s\n",
17146 dwarf_bool_name (die->child != NULL));
17147
17148 print_spaces (indent, f);
17149 fprintf_unfiltered (f, " attributes:\n");
17150
17151 for (i = 0; i < die->num_attrs; ++i)
17152 {
17153 print_spaces (indent, f);
17154 fprintf_unfiltered (f, " %s (%s) ",
17155 dwarf_attr_name (die->attrs[i].name),
17156 dwarf_form_name (die->attrs[i].form));
17157
17158 switch (die->attrs[i].form)
17159 {
17160 case DW_FORM_addr:
17161 case DW_FORM_GNU_addr_index:
17162 fprintf_unfiltered (f, "address: ");
17163 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17164 break;
17165 case DW_FORM_block2:
17166 case DW_FORM_block4:
17167 case DW_FORM_block:
17168 case DW_FORM_block1:
17169 fprintf_unfiltered (f, "block: size %s",
17170 pulongest (DW_BLOCK (&die->attrs[i])->size));
17171 break;
17172 case DW_FORM_exprloc:
17173 fprintf_unfiltered (f, "expression: size %s",
17174 pulongest (DW_BLOCK (&die->attrs[i])->size));
17175 break;
17176 case DW_FORM_ref_addr:
17177 fprintf_unfiltered (f, "ref address: ");
17178 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17179 break;
17180 case DW_FORM_GNU_ref_alt:
17181 fprintf_unfiltered (f, "alt ref address: ");
17182 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17183 break;
17184 case DW_FORM_ref1:
17185 case DW_FORM_ref2:
17186 case DW_FORM_ref4:
17187 case DW_FORM_ref8:
17188 case DW_FORM_ref_udata:
17189 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17190 (long) (DW_UNSND (&die->attrs[i])));
17191 break;
17192 case DW_FORM_data1:
17193 case DW_FORM_data2:
17194 case DW_FORM_data4:
17195 case DW_FORM_data8:
17196 case DW_FORM_udata:
17197 case DW_FORM_sdata:
17198 fprintf_unfiltered (f, "constant: %s",
17199 pulongest (DW_UNSND (&die->attrs[i])));
17200 break;
17201 case DW_FORM_sec_offset:
17202 fprintf_unfiltered (f, "section offset: %s",
17203 pulongest (DW_UNSND (&die->attrs[i])));
17204 break;
17205 case DW_FORM_ref_sig8:
17206 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17207 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17208 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17209 else
17210 fprintf_unfiltered (f, "signatured type, offset: unknown");
17211 break;
17212 case DW_FORM_string:
17213 case DW_FORM_strp:
17214 case DW_FORM_GNU_str_index:
17215 case DW_FORM_GNU_strp_alt:
17216 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17217 DW_STRING (&die->attrs[i])
17218 ? DW_STRING (&die->attrs[i]) : "",
17219 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17220 break;
17221 case DW_FORM_flag:
17222 if (DW_UNSND (&die->attrs[i]))
17223 fprintf_unfiltered (f, "flag: TRUE");
17224 else
17225 fprintf_unfiltered (f, "flag: FALSE");
17226 break;
17227 case DW_FORM_flag_present:
17228 fprintf_unfiltered (f, "flag: TRUE");
17229 break;
17230 case DW_FORM_indirect:
17231 /* The reader will have reduced the indirect form to
17232 the "base form" so this form should not occur. */
17233 fprintf_unfiltered (f,
17234 "unexpected attribute form: DW_FORM_indirect");
17235 break;
17236 default:
17237 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17238 die->attrs[i].form);
17239 break;
17240 }
17241 fprintf_unfiltered (f, "\n");
17242 }
17243 }
17244
17245 static void
17246 dump_die_for_error (struct die_info *die)
17247 {
17248 dump_die_shallow (gdb_stderr, 0, die);
17249 }
17250
17251 static void
17252 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17253 {
17254 int indent = level * 4;
17255
17256 gdb_assert (die != NULL);
17257
17258 if (level >= max_level)
17259 return;
17260
17261 dump_die_shallow (f, indent, die);
17262
17263 if (die->child != NULL)
17264 {
17265 print_spaces (indent, f);
17266 fprintf_unfiltered (f, " Children:");
17267 if (level + 1 < max_level)
17268 {
17269 fprintf_unfiltered (f, "\n");
17270 dump_die_1 (f, level + 1, max_level, die->child);
17271 }
17272 else
17273 {
17274 fprintf_unfiltered (f,
17275 " [not printed, max nesting level reached]\n");
17276 }
17277 }
17278
17279 if (die->sibling != NULL && level > 0)
17280 {
17281 dump_die_1 (f, level, max_level, die->sibling);
17282 }
17283 }
17284
17285 /* This is called from the pdie macro in gdbinit.in.
17286 It's not static so gcc will keep a copy callable from gdb. */
17287
17288 void
17289 dump_die (struct die_info *die, int max_level)
17290 {
17291 dump_die_1 (gdb_stdlog, 0, max_level, die);
17292 }
17293
17294 static void
17295 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17296 {
17297 void **slot;
17298
17299 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17300 INSERT);
17301
17302 *slot = die;
17303 }
17304
17305 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17306 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17307
17308 static int
17309 is_ref_attr (struct attribute *attr)
17310 {
17311 switch (attr->form)
17312 {
17313 case DW_FORM_ref_addr:
17314 case DW_FORM_ref1:
17315 case DW_FORM_ref2:
17316 case DW_FORM_ref4:
17317 case DW_FORM_ref8:
17318 case DW_FORM_ref_udata:
17319 case DW_FORM_GNU_ref_alt:
17320 return 1;
17321 default:
17322 return 0;
17323 }
17324 }
17325
17326 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17327 required kind. */
17328
17329 static sect_offset
17330 dwarf2_get_ref_die_offset (struct attribute *attr)
17331 {
17332 sect_offset retval = { DW_UNSND (attr) };
17333
17334 if (is_ref_attr (attr))
17335 return retval;
17336
17337 retval.sect_off = 0;
17338 complaint (&symfile_complaints,
17339 _("unsupported die ref attribute form: '%s'"),
17340 dwarf_form_name (attr->form));
17341 return retval;
17342 }
17343
17344 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17345 * the value held by the attribute is not constant. */
17346
17347 static LONGEST
17348 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17349 {
17350 if (attr->form == DW_FORM_sdata)
17351 return DW_SND (attr);
17352 else if (attr->form == DW_FORM_udata
17353 || attr->form == DW_FORM_data1
17354 || attr->form == DW_FORM_data2
17355 || attr->form == DW_FORM_data4
17356 || attr->form == DW_FORM_data8)
17357 return DW_UNSND (attr);
17358 else
17359 {
17360 complaint (&symfile_complaints,
17361 _("Attribute value is not a constant (%s)"),
17362 dwarf_form_name (attr->form));
17363 return default_value;
17364 }
17365 }
17366
17367 /* Follow reference or signature attribute ATTR of SRC_DIE.
17368 On entry *REF_CU is the CU of SRC_DIE.
17369 On exit *REF_CU is the CU of the result. */
17370
17371 static struct die_info *
17372 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17373 struct dwarf2_cu **ref_cu)
17374 {
17375 struct die_info *die;
17376
17377 if (is_ref_attr (attr))
17378 die = follow_die_ref (src_die, attr, ref_cu);
17379 else if (attr->form == DW_FORM_ref_sig8)
17380 die = follow_die_sig (src_die, attr, ref_cu);
17381 else
17382 {
17383 dump_die_for_error (src_die);
17384 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17385 (*ref_cu)->objfile->name);
17386 }
17387
17388 return die;
17389 }
17390
17391 /* Follow reference OFFSET.
17392 On entry *REF_CU is the CU of the source die referencing OFFSET.
17393 On exit *REF_CU is the CU of the result.
17394 Returns NULL if OFFSET is invalid. */
17395
17396 static struct die_info *
17397 follow_die_offset (sect_offset offset, int offset_in_dwz,
17398 struct dwarf2_cu **ref_cu)
17399 {
17400 struct die_info temp_die;
17401 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17402
17403 gdb_assert (cu->per_cu != NULL);
17404
17405 target_cu = cu;
17406
17407 if (cu->per_cu->is_debug_types)
17408 {
17409 /* .debug_types CUs cannot reference anything outside their CU.
17410 If they need to, they have to reference a signatured type via
17411 DW_FORM_ref_sig8. */
17412 if (! offset_in_cu_p (&cu->header, offset))
17413 return NULL;
17414 }
17415 else if (offset_in_dwz != cu->per_cu->is_dwz
17416 || ! offset_in_cu_p (&cu->header, offset))
17417 {
17418 struct dwarf2_per_cu_data *per_cu;
17419
17420 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17421 cu->objfile);
17422
17423 /* If necessary, add it to the queue and load its DIEs. */
17424 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17425 load_full_comp_unit (per_cu, cu->language);
17426
17427 target_cu = per_cu->cu;
17428 }
17429 else if (cu->dies == NULL)
17430 {
17431 /* We're loading full DIEs during partial symbol reading. */
17432 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17433 load_full_comp_unit (cu->per_cu, language_minimal);
17434 }
17435
17436 *ref_cu = target_cu;
17437 temp_die.offset = offset;
17438 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17439 }
17440
17441 /* Follow reference attribute ATTR of SRC_DIE.
17442 On entry *REF_CU is the CU of SRC_DIE.
17443 On exit *REF_CU is the CU of the result. */
17444
17445 static struct die_info *
17446 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17447 struct dwarf2_cu **ref_cu)
17448 {
17449 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17450 struct dwarf2_cu *cu = *ref_cu;
17451 struct die_info *die;
17452
17453 die = follow_die_offset (offset,
17454 (attr->form == DW_FORM_GNU_ref_alt
17455 || cu->per_cu->is_dwz),
17456 ref_cu);
17457 if (!die)
17458 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17459 "at 0x%x [in module %s]"),
17460 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17461
17462 return die;
17463 }
17464
17465 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17466 Returned value is intended for DW_OP_call*. Returned
17467 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17468
17469 struct dwarf2_locexpr_baton
17470 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17471 struct dwarf2_per_cu_data *per_cu,
17472 CORE_ADDR (*get_frame_pc) (void *baton),
17473 void *baton)
17474 {
17475 struct dwarf2_cu *cu;
17476 struct die_info *die;
17477 struct attribute *attr;
17478 struct dwarf2_locexpr_baton retval;
17479
17480 dw2_setup (per_cu->objfile);
17481
17482 if (per_cu->cu == NULL)
17483 load_cu (per_cu);
17484 cu = per_cu->cu;
17485
17486 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17487 if (!die)
17488 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17489 offset.sect_off, per_cu->objfile->name);
17490
17491 attr = dwarf2_attr (die, DW_AT_location, cu);
17492 if (!attr)
17493 {
17494 /* DWARF: "If there is no such attribute, then there is no effect.".
17495 DATA is ignored if SIZE is 0. */
17496
17497 retval.data = NULL;
17498 retval.size = 0;
17499 }
17500 else if (attr_form_is_section_offset (attr))
17501 {
17502 struct dwarf2_loclist_baton loclist_baton;
17503 CORE_ADDR pc = (*get_frame_pc) (baton);
17504 size_t size;
17505
17506 fill_in_loclist_baton (cu, &loclist_baton, attr);
17507
17508 retval.data = dwarf2_find_location_expression (&loclist_baton,
17509 &size, pc);
17510 retval.size = size;
17511 }
17512 else
17513 {
17514 if (!attr_form_is_block (attr))
17515 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17516 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17517 offset.sect_off, per_cu->objfile->name);
17518
17519 retval.data = DW_BLOCK (attr)->data;
17520 retval.size = DW_BLOCK (attr)->size;
17521 }
17522 retval.per_cu = cu->per_cu;
17523
17524 age_cached_comp_units ();
17525
17526 return retval;
17527 }
17528
17529 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17530 offset. */
17531
17532 struct dwarf2_locexpr_baton
17533 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17534 struct dwarf2_per_cu_data *per_cu,
17535 CORE_ADDR (*get_frame_pc) (void *baton),
17536 void *baton)
17537 {
17538 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17539
17540 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17541 }
17542
17543 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17544 PER_CU. */
17545
17546 struct type *
17547 dwarf2_get_die_type (cu_offset die_offset,
17548 struct dwarf2_per_cu_data *per_cu)
17549 {
17550 sect_offset die_offset_sect;
17551
17552 dw2_setup (per_cu->objfile);
17553
17554 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17555 return get_die_type_at_offset (die_offset_sect, per_cu);
17556 }
17557
17558 /* Follow the signature attribute ATTR in SRC_DIE.
17559 On entry *REF_CU is the CU of SRC_DIE.
17560 On exit *REF_CU is the CU of the result. */
17561
17562 static struct die_info *
17563 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17564 struct dwarf2_cu **ref_cu)
17565 {
17566 struct objfile *objfile = (*ref_cu)->objfile;
17567 struct die_info temp_die;
17568 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17569 struct dwarf2_cu *sig_cu;
17570 struct die_info *die;
17571
17572 /* sig_type will be NULL if the signatured type is missing from
17573 the debug info. */
17574 if (sig_type == NULL)
17575 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17576 "at 0x%x [in module %s]"),
17577 src_die->offset.sect_off, objfile->name);
17578
17579 /* If necessary, add it to the queue and load its DIEs. */
17580
17581 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17582 read_signatured_type (sig_type);
17583
17584 gdb_assert (sig_type->per_cu.cu != NULL);
17585
17586 sig_cu = sig_type->per_cu.cu;
17587 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17588 temp_die.offset = sig_type->type_offset_in_section;
17589 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17590 temp_die.offset.sect_off);
17591 if (die)
17592 {
17593 *ref_cu = sig_cu;
17594 return die;
17595 }
17596
17597 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17598 "from DIE at 0x%x [in module %s]"),
17599 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17600 }
17601
17602 /* Given an offset of a signatured type, return its signatured_type. */
17603
17604 static struct signatured_type *
17605 lookup_signatured_type_at_offset (struct objfile *objfile,
17606 struct dwarf2_section_info *section,
17607 sect_offset offset)
17608 {
17609 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17610 unsigned int length, initial_length_size;
17611 unsigned int sig_offset;
17612 struct signatured_type find_entry, *sig_type;
17613
17614 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17615 sig_offset = (initial_length_size
17616 + 2 /*version*/
17617 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17618 + 1 /*address_size*/);
17619 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17620 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17621
17622 /* This is only used to lookup previously recorded types.
17623 If we didn't find it, it's our bug. */
17624 gdb_assert (sig_type != NULL);
17625 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17626
17627 return sig_type;
17628 }
17629
17630 /* Load the DIEs associated with type unit PER_CU into memory. */
17631
17632 static void
17633 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17634 {
17635 struct signatured_type *sig_type;
17636
17637 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17638 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17639
17640 /* We have the per_cu, but we need the signatured_type.
17641 Fortunately this is an easy translation. */
17642 gdb_assert (per_cu->is_debug_types);
17643 sig_type = (struct signatured_type *) per_cu;
17644
17645 gdb_assert (per_cu->cu == NULL);
17646
17647 read_signatured_type (sig_type);
17648
17649 gdb_assert (per_cu->cu != NULL);
17650 }
17651
17652 /* die_reader_func for read_signatured_type.
17653 This is identical to load_full_comp_unit_reader,
17654 but is kept separate for now. */
17655
17656 static void
17657 read_signatured_type_reader (const struct die_reader_specs *reader,
17658 gdb_byte *info_ptr,
17659 struct die_info *comp_unit_die,
17660 int has_children,
17661 void *data)
17662 {
17663 struct dwarf2_cu *cu = reader->cu;
17664
17665 gdb_assert (cu->die_hash == NULL);
17666 cu->die_hash =
17667 htab_create_alloc_ex (cu->header.length / 12,
17668 die_hash,
17669 die_eq,
17670 NULL,
17671 &cu->comp_unit_obstack,
17672 hashtab_obstack_allocate,
17673 dummy_obstack_deallocate);
17674
17675 if (has_children)
17676 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17677 &info_ptr, comp_unit_die);
17678 cu->dies = comp_unit_die;
17679 /* comp_unit_die is not stored in die_hash, no need. */
17680
17681 /* We try not to read any attributes in this function, because not
17682 all CUs needed for references have been loaded yet, and symbol
17683 table processing isn't initialized. But we have to set the CU language,
17684 or we won't be able to build types correctly.
17685 Similarly, if we do not read the producer, we can not apply
17686 producer-specific interpretation. */
17687 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17688 }
17689
17690 /* Read in a signatured type and build its CU and DIEs.
17691 If the type is a stub for the real type in a DWO file,
17692 read in the real type from the DWO file as well. */
17693
17694 static void
17695 read_signatured_type (struct signatured_type *sig_type)
17696 {
17697 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17698
17699 gdb_assert (per_cu->is_debug_types);
17700 gdb_assert (per_cu->cu == NULL);
17701
17702 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17703 read_signatured_type_reader, NULL);
17704 }
17705
17706 /* Decode simple location descriptions.
17707 Given a pointer to a dwarf block that defines a location, compute
17708 the location and return the value.
17709
17710 NOTE drow/2003-11-18: This function is called in two situations
17711 now: for the address of static or global variables (partial symbols
17712 only) and for offsets into structures which are expected to be
17713 (more or less) constant. The partial symbol case should go away,
17714 and only the constant case should remain. That will let this
17715 function complain more accurately. A few special modes are allowed
17716 without complaint for global variables (for instance, global
17717 register values and thread-local values).
17718
17719 A location description containing no operations indicates that the
17720 object is optimized out. The return value is 0 for that case.
17721 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17722 callers will only want a very basic result and this can become a
17723 complaint.
17724
17725 Note that stack[0] is unused except as a default error return. */
17726
17727 static CORE_ADDR
17728 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17729 {
17730 struct objfile *objfile = cu->objfile;
17731 size_t i;
17732 size_t size = blk->size;
17733 gdb_byte *data = blk->data;
17734 CORE_ADDR stack[64];
17735 int stacki;
17736 unsigned int bytes_read, unsnd;
17737 gdb_byte op;
17738
17739 i = 0;
17740 stacki = 0;
17741 stack[stacki] = 0;
17742 stack[++stacki] = 0;
17743
17744 while (i < size)
17745 {
17746 op = data[i++];
17747 switch (op)
17748 {
17749 case DW_OP_lit0:
17750 case DW_OP_lit1:
17751 case DW_OP_lit2:
17752 case DW_OP_lit3:
17753 case DW_OP_lit4:
17754 case DW_OP_lit5:
17755 case DW_OP_lit6:
17756 case DW_OP_lit7:
17757 case DW_OP_lit8:
17758 case DW_OP_lit9:
17759 case DW_OP_lit10:
17760 case DW_OP_lit11:
17761 case DW_OP_lit12:
17762 case DW_OP_lit13:
17763 case DW_OP_lit14:
17764 case DW_OP_lit15:
17765 case DW_OP_lit16:
17766 case DW_OP_lit17:
17767 case DW_OP_lit18:
17768 case DW_OP_lit19:
17769 case DW_OP_lit20:
17770 case DW_OP_lit21:
17771 case DW_OP_lit22:
17772 case DW_OP_lit23:
17773 case DW_OP_lit24:
17774 case DW_OP_lit25:
17775 case DW_OP_lit26:
17776 case DW_OP_lit27:
17777 case DW_OP_lit28:
17778 case DW_OP_lit29:
17779 case DW_OP_lit30:
17780 case DW_OP_lit31:
17781 stack[++stacki] = op - DW_OP_lit0;
17782 break;
17783
17784 case DW_OP_reg0:
17785 case DW_OP_reg1:
17786 case DW_OP_reg2:
17787 case DW_OP_reg3:
17788 case DW_OP_reg4:
17789 case DW_OP_reg5:
17790 case DW_OP_reg6:
17791 case DW_OP_reg7:
17792 case DW_OP_reg8:
17793 case DW_OP_reg9:
17794 case DW_OP_reg10:
17795 case DW_OP_reg11:
17796 case DW_OP_reg12:
17797 case DW_OP_reg13:
17798 case DW_OP_reg14:
17799 case DW_OP_reg15:
17800 case DW_OP_reg16:
17801 case DW_OP_reg17:
17802 case DW_OP_reg18:
17803 case DW_OP_reg19:
17804 case DW_OP_reg20:
17805 case DW_OP_reg21:
17806 case DW_OP_reg22:
17807 case DW_OP_reg23:
17808 case DW_OP_reg24:
17809 case DW_OP_reg25:
17810 case DW_OP_reg26:
17811 case DW_OP_reg27:
17812 case DW_OP_reg28:
17813 case DW_OP_reg29:
17814 case DW_OP_reg30:
17815 case DW_OP_reg31:
17816 stack[++stacki] = op - DW_OP_reg0;
17817 if (i < size)
17818 dwarf2_complex_location_expr_complaint ();
17819 break;
17820
17821 case DW_OP_regx:
17822 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17823 i += bytes_read;
17824 stack[++stacki] = unsnd;
17825 if (i < size)
17826 dwarf2_complex_location_expr_complaint ();
17827 break;
17828
17829 case DW_OP_addr:
17830 stack[++stacki] = read_address (objfile->obfd, &data[i],
17831 cu, &bytes_read);
17832 i += bytes_read;
17833 break;
17834
17835 case DW_OP_const1u:
17836 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17837 i += 1;
17838 break;
17839
17840 case DW_OP_const1s:
17841 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17842 i += 1;
17843 break;
17844
17845 case DW_OP_const2u:
17846 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17847 i += 2;
17848 break;
17849
17850 case DW_OP_const2s:
17851 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17852 i += 2;
17853 break;
17854
17855 case DW_OP_const4u:
17856 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17857 i += 4;
17858 break;
17859
17860 case DW_OP_const4s:
17861 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17862 i += 4;
17863 break;
17864
17865 case DW_OP_const8u:
17866 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17867 i += 8;
17868 break;
17869
17870 case DW_OP_constu:
17871 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17872 &bytes_read);
17873 i += bytes_read;
17874 break;
17875
17876 case DW_OP_consts:
17877 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17878 i += bytes_read;
17879 break;
17880
17881 case DW_OP_dup:
17882 stack[stacki + 1] = stack[stacki];
17883 stacki++;
17884 break;
17885
17886 case DW_OP_plus:
17887 stack[stacki - 1] += stack[stacki];
17888 stacki--;
17889 break;
17890
17891 case DW_OP_plus_uconst:
17892 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17893 &bytes_read);
17894 i += bytes_read;
17895 break;
17896
17897 case DW_OP_minus:
17898 stack[stacki - 1] -= stack[stacki];
17899 stacki--;
17900 break;
17901
17902 case DW_OP_deref:
17903 /* If we're not the last op, then we definitely can't encode
17904 this using GDB's address_class enum. This is valid for partial
17905 global symbols, although the variable's address will be bogus
17906 in the psymtab. */
17907 if (i < size)
17908 dwarf2_complex_location_expr_complaint ();
17909 break;
17910
17911 case DW_OP_GNU_push_tls_address:
17912 /* The top of the stack has the offset from the beginning
17913 of the thread control block at which the variable is located. */
17914 /* Nothing should follow this operator, so the top of stack would
17915 be returned. */
17916 /* This is valid for partial global symbols, but the variable's
17917 address will be bogus in the psymtab. Make it always at least
17918 non-zero to not look as a variable garbage collected by linker
17919 which have DW_OP_addr 0. */
17920 if (i < size)
17921 dwarf2_complex_location_expr_complaint ();
17922 stack[stacki]++;
17923 break;
17924
17925 case DW_OP_GNU_uninit:
17926 break;
17927
17928 case DW_OP_GNU_addr_index:
17929 case DW_OP_GNU_const_index:
17930 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17931 &bytes_read);
17932 i += bytes_read;
17933 break;
17934
17935 default:
17936 {
17937 const char *name = get_DW_OP_name (op);
17938
17939 if (name)
17940 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17941 name);
17942 else
17943 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17944 op);
17945 }
17946
17947 return (stack[stacki]);
17948 }
17949
17950 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17951 outside of the allocated space. Also enforce minimum>0. */
17952 if (stacki >= ARRAY_SIZE (stack) - 1)
17953 {
17954 complaint (&symfile_complaints,
17955 _("location description stack overflow"));
17956 return 0;
17957 }
17958
17959 if (stacki <= 0)
17960 {
17961 complaint (&symfile_complaints,
17962 _("location description stack underflow"));
17963 return 0;
17964 }
17965 }
17966 return (stack[stacki]);
17967 }
17968
17969 /* memory allocation interface */
17970
17971 static struct dwarf_block *
17972 dwarf_alloc_block (struct dwarf2_cu *cu)
17973 {
17974 struct dwarf_block *blk;
17975
17976 blk = (struct dwarf_block *)
17977 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17978 return (blk);
17979 }
17980
17981 static struct die_info *
17982 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17983 {
17984 struct die_info *die;
17985 size_t size = sizeof (struct die_info);
17986
17987 if (num_attrs > 1)
17988 size += (num_attrs - 1) * sizeof (struct attribute);
17989
17990 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17991 memset (die, 0, sizeof (struct die_info));
17992 return (die);
17993 }
17994
17995 \f
17996 /* Macro support. */
17997
17998 /* Return the full name of file number I in *LH's file name table.
17999 Use COMP_DIR as the name of the current directory of the
18000 compilation. The result is allocated using xmalloc; the caller is
18001 responsible for freeing it. */
18002 static char *
18003 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18004 {
18005 /* Is the file number a valid index into the line header's file name
18006 table? Remember that file numbers start with one, not zero. */
18007 if (1 <= file && file <= lh->num_file_names)
18008 {
18009 struct file_entry *fe = &lh->file_names[file - 1];
18010
18011 if (IS_ABSOLUTE_PATH (fe->name))
18012 return xstrdup (fe->name);
18013 else
18014 {
18015 const char *dir;
18016 int dir_len;
18017 char *full_name;
18018
18019 if (fe->dir_index)
18020 dir = lh->include_dirs[fe->dir_index - 1];
18021 else
18022 dir = comp_dir;
18023
18024 if (dir)
18025 {
18026 dir_len = strlen (dir);
18027 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18028 strcpy (full_name, dir);
18029 full_name[dir_len] = '/';
18030 strcpy (full_name + dir_len + 1, fe->name);
18031 return full_name;
18032 }
18033 else
18034 return xstrdup (fe->name);
18035 }
18036 }
18037 else
18038 {
18039 /* The compiler produced a bogus file number. We can at least
18040 record the macro definitions made in the file, even if we
18041 won't be able to find the file by name. */
18042 char fake_name[80];
18043
18044 xsnprintf (fake_name, sizeof (fake_name),
18045 "<bad macro file number %d>", file);
18046
18047 complaint (&symfile_complaints,
18048 _("bad file number in macro information (%d)"),
18049 file);
18050
18051 return xstrdup (fake_name);
18052 }
18053 }
18054
18055
18056 static struct macro_source_file *
18057 macro_start_file (int file, int line,
18058 struct macro_source_file *current_file,
18059 const char *comp_dir,
18060 struct line_header *lh, struct objfile *objfile)
18061 {
18062 /* The full name of this source file. */
18063 char *full_name = file_full_name (file, lh, comp_dir);
18064
18065 /* We don't create a macro table for this compilation unit
18066 at all until we actually get a filename. */
18067 if (! pending_macros)
18068 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18069 objfile->per_bfd->macro_cache);
18070
18071 if (! current_file)
18072 {
18073 /* If we have no current file, then this must be the start_file
18074 directive for the compilation unit's main source file. */
18075 current_file = macro_set_main (pending_macros, full_name);
18076 macro_define_special (pending_macros);
18077 }
18078 else
18079 current_file = macro_include (current_file, line, full_name);
18080
18081 xfree (full_name);
18082
18083 return current_file;
18084 }
18085
18086
18087 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18088 followed by a null byte. */
18089 static char *
18090 copy_string (const char *buf, int len)
18091 {
18092 char *s = xmalloc (len + 1);
18093
18094 memcpy (s, buf, len);
18095 s[len] = '\0';
18096 return s;
18097 }
18098
18099
18100 static const char *
18101 consume_improper_spaces (const char *p, const char *body)
18102 {
18103 if (*p == ' ')
18104 {
18105 complaint (&symfile_complaints,
18106 _("macro definition contains spaces "
18107 "in formal argument list:\n`%s'"),
18108 body);
18109
18110 while (*p == ' ')
18111 p++;
18112 }
18113
18114 return p;
18115 }
18116
18117
18118 static void
18119 parse_macro_definition (struct macro_source_file *file, int line,
18120 const char *body)
18121 {
18122 const char *p;
18123
18124 /* The body string takes one of two forms. For object-like macro
18125 definitions, it should be:
18126
18127 <macro name> " " <definition>
18128
18129 For function-like macro definitions, it should be:
18130
18131 <macro name> "() " <definition>
18132 or
18133 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18134
18135 Spaces may appear only where explicitly indicated, and in the
18136 <definition>.
18137
18138 The Dwarf 2 spec says that an object-like macro's name is always
18139 followed by a space, but versions of GCC around March 2002 omit
18140 the space when the macro's definition is the empty string.
18141
18142 The Dwarf 2 spec says that there should be no spaces between the
18143 formal arguments in a function-like macro's formal argument list,
18144 but versions of GCC around March 2002 include spaces after the
18145 commas. */
18146
18147
18148 /* Find the extent of the macro name. The macro name is terminated
18149 by either a space or null character (for an object-like macro) or
18150 an opening paren (for a function-like macro). */
18151 for (p = body; *p; p++)
18152 if (*p == ' ' || *p == '(')
18153 break;
18154
18155 if (*p == ' ' || *p == '\0')
18156 {
18157 /* It's an object-like macro. */
18158 int name_len = p - body;
18159 char *name = copy_string (body, name_len);
18160 const char *replacement;
18161
18162 if (*p == ' ')
18163 replacement = body + name_len + 1;
18164 else
18165 {
18166 dwarf2_macro_malformed_definition_complaint (body);
18167 replacement = body + name_len;
18168 }
18169
18170 macro_define_object (file, line, name, replacement);
18171
18172 xfree (name);
18173 }
18174 else if (*p == '(')
18175 {
18176 /* It's a function-like macro. */
18177 char *name = copy_string (body, p - body);
18178 int argc = 0;
18179 int argv_size = 1;
18180 char **argv = xmalloc (argv_size * sizeof (*argv));
18181
18182 p++;
18183
18184 p = consume_improper_spaces (p, body);
18185
18186 /* Parse the formal argument list. */
18187 while (*p && *p != ')')
18188 {
18189 /* Find the extent of the current argument name. */
18190 const char *arg_start = p;
18191
18192 while (*p && *p != ',' && *p != ')' && *p != ' ')
18193 p++;
18194
18195 if (! *p || p == arg_start)
18196 dwarf2_macro_malformed_definition_complaint (body);
18197 else
18198 {
18199 /* Make sure argv has room for the new argument. */
18200 if (argc >= argv_size)
18201 {
18202 argv_size *= 2;
18203 argv = xrealloc (argv, argv_size * sizeof (*argv));
18204 }
18205
18206 argv[argc++] = copy_string (arg_start, p - arg_start);
18207 }
18208
18209 p = consume_improper_spaces (p, body);
18210
18211 /* Consume the comma, if present. */
18212 if (*p == ',')
18213 {
18214 p++;
18215
18216 p = consume_improper_spaces (p, body);
18217 }
18218 }
18219
18220 if (*p == ')')
18221 {
18222 p++;
18223
18224 if (*p == ' ')
18225 /* Perfectly formed definition, no complaints. */
18226 macro_define_function (file, line, name,
18227 argc, (const char **) argv,
18228 p + 1);
18229 else if (*p == '\0')
18230 {
18231 /* Complain, but do define it. */
18232 dwarf2_macro_malformed_definition_complaint (body);
18233 macro_define_function (file, line, name,
18234 argc, (const char **) argv,
18235 p);
18236 }
18237 else
18238 /* Just complain. */
18239 dwarf2_macro_malformed_definition_complaint (body);
18240 }
18241 else
18242 /* Just complain. */
18243 dwarf2_macro_malformed_definition_complaint (body);
18244
18245 xfree (name);
18246 {
18247 int i;
18248
18249 for (i = 0; i < argc; i++)
18250 xfree (argv[i]);
18251 }
18252 xfree (argv);
18253 }
18254 else
18255 dwarf2_macro_malformed_definition_complaint (body);
18256 }
18257
18258 /* Skip some bytes from BYTES according to the form given in FORM.
18259 Returns the new pointer. */
18260
18261 static gdb_byte *
18262 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18263 enum dwarf_form form,
18264 unsigned int offset_size,
18265 struct dwarf2_section_info *section)
18266 {
18267 unsigned int bytes_read;
18268
18269 switch (form)
18270 {
18271 case DW_FORM_data1:
18272 case DW_FORM_flag:
18273 ++bytes;
18274 break;
18275
18276 case DW_FORM_data2:
18277 bytes += 2;
18278 break;
18279
18280 case DW_FORM_data4:
18281 bytes += 4;
18282 break;
18283
18284 case DW_FORM_data8:
18285 bytes += 8;
18286 break;
18287
18288 case DW_FORM_string:
18289 read_direct_string (abfd, bytes, &bytes_read);
18290 bytes += bytes_read;
18291 break;
18292
18293 case DW_FORM_sec_offset:
18294 case DW_FORM_strp:
18295 case DW_FORM_GNU_strp_alt:
18296 bytes += offset_size;
18297 break;
18298
18299 case DW_FORM_block:
18300 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18301 bytes += bytes_read;
18302 break;
18303
18304 case DW_FORM_block1:
18305 bytes += 1 + read_1_byte (abfd, bytes);
18306 break;
18307 case DW_FORM_block2:
18308 bytes += 2 + read_2_bytes (abfd, bytes);
18309 break;
18310 case DW_FORM_block4:
18311 bytes += 4 + read_4_bytes (abfd, bytes);
18312 break;
18313
18314 case DW_FORM_sdata:
18315 case DW_FORM_udata:
18316 case DW_FORM_GNU_addr_index:
18317 case DW_FORM_GNU_str_index:
18318 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18319 if (bytes == NULL)
18320 {
18321 dwarf2_section_buffer_overflow_complaint (section);
18322 return NULL;
18323 }
18324 break;
18325
18326 default:
18327 {
18328 complain:
18329 complaint (&symfile_complaints,
18330 _("invalid form 0x%x in `%s'"),
18331 form,
18332 section->asection->name);
18333 return NULL;
18334 }
18335 }
18336
18337 return bytes;
18338 }
18339
18340 /* A helper for dwarf_decode_macros that handles skipping an unknown
18341 opcode. Returns an updated pointer to the macro data buffer; or,
18342 on error, issues a complaint and returns NULL. */
18343
18344 static gdb_byte *
18345 skip_unknown_opcode (unsigned int opcode,
18346 gdb_byte **opcode_definitions,
18347 gdb_byte *mac_ptr, gdb_byte *mac_end,
18348 bfd *abfd,
18349 unsigned int offset_size,
18350 struct dwarf2_section_info *section)
18351 {
18352 unsigned int bytes_read, i;
18353 unsigned long arg;
18354 gdb_byte *defn;
18355
18356 if (opcode_definitions[opcode] == NULL)
18357 {
18358 complaint (&symfile_complaints,
18359 _("unrecognized DW_MACFINO opcode 0x%x"),
18360 opcode);
18361 return NULL;
18362 }
18363
18364 defn = opcode_definitions[opcode];
18365 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18366 defn += bytes_read;
18367
18368 for (i = 0; i < arg; ++i)
18369 {
18370 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18371 section);
18372 if (mac_ptr == NULL)
18373 {
18374 /* skip_form_bytes already issued the complaint. */
18375 return NULL;
18376 }
18377 }
18378
18379 return mac_ptr;
18380 }
18381
18382 /* A helper function which parses the header of a macro section.
18383 If the macro section is the extended (for now called "GNU") type,
18384 then this updates *OFFSET_SIZE. Returns a pointer to just after
18385 the header, or issues a complaint and returns NULL on error. */
18386
18387 static gdb_byte *
18388 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18389 bfd *abfd,
18390 gdb_byte *mac_ptr,
18391 unsigned int *offset_size,
18392 int section_is_gnu)
18393 {
18394 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18395
18396 if (section_is_gnu)
18397 {
18398 unsigned int version, flags;
18399
18400 version = read_2_bytes (abfd, mac_ptr);
18401 if (version != 4)
18402 {
18403 complaint (&symfile_complaints,
18404 _("unrecognized version `%d' in .debug_macro section"),
18405 version);
18406 return NULL;
18407 }
18408 mac_ptr += 2;
18409
18410 flags = read_1_byte (abfd, mac_ptr);
18411 ++mac_ptr;
18412 *offset_size = (flags & 1) ? 8 : 4;
18413
18414 if ((flags & 2) != 0)
18415 /* We don't need the line table offset. */
18416 mac_ptr += *offset_size;
18417
18418 /* Vendor opcode descriptions. */
18419 if ((flags & 4) != 0)
18420 {
18421 unsigned int i, count;
18422
18423 count = read_1_byte (abfd, mac_ptr);
18424 ++mac_ptr;
18425 for (i = 0; i < count; ++i)
18426 {
18427 unsigned int opcode, bytes_read;
18428 unsigned long arg;
18429
18430 opcode = read_1_byte (abfd, mac_ptr);
18431 ++mac_ptr;
18432 opcode_definitions[opcode] = mac_ptr;
18433 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18434 mac_ptr += bytes_read;
18435 mac_ptr += arg;
18436 }
18437 }
18438 }
18439
18440 return mac_ptr;
18441 }
18442
18443 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18444 including DW_MACRO_GNU_transparent_include. */
18445
18446 static void
18447 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18448 struct macro_source_file *current_file,
18449 struct line_header *lh, const char *comp_dir,
18450 struct dwarf2_section_info *section,
18451 int section_is_gnu, int section_is_dwz,
18452 unsigned int offset_size,
18453 struct objfile *objfile,
18454 htab_t include_hash)
18455 {
18456 enum dwarf_macro_record_type macinfo_type;
18457 int at_commandline;
18458 gdb_byte *opcode_definitions[256];
18459
18460 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18461 &offset_size, section_is_gnu);
18462 if (mac_ptr == NULL)
18463 {
18464 /* We already issued a complaint. */
18465 return;
18466 }
18467
18468 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18469 GDB is still reading the definitions from command line. First
18470 DW_MACINFO_start_file will need to be ignored as it was already executed
18471 to create CURRENT_FILE for the main source holding also the command line
18472 definitions. On first met DW_MACINFO_start_file this flag is reset to
18473 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18474
18475 at_commandline = 1;
18476
18477 do
18478 {
18479 /* Do we at least have room for a macinfo type byte? */
18480 if (mac_ptr >= mac_end)
18481 {
18482 dwarf2_section_buffer_overflow_complaint (section);
18483 break;
18484 }
18485
18486 macinfo_type = read_1_byte (abfd, mac_ptr);
18487 mac_ptr++;
18488
18489 /* Note that we rely on the fact that the corresponding GNU and
18490 DWARF constants are the same. */
18491 switch (macinfo_type)
18492 {
18493 /* A zero macinfo type indicates the end of the macro
18494 information. */
18495 case 0:
18496 break;
18497
18498 case DW_MACRO_GNU_define:
18499 case DW_MACRO_GNU_undef:
18500 case DW_MACRO_GNU_define_indirect:
18501 case DW_MACRO_GNU_undef_indirect:
18502 case DW_MACRO_GNU_define_indirect_alt:
18503 case DW_MACRO_GNU_undef_indirect_alt:
18504 {
18505 unsigned int bytes_read;
18506 int line;
18507 char *body;
18508 int is_define;
18509
18510 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18511 mac_ptr += bytes_read;
18512
18513 if (macinfo_type == DW_MACRO_GNU_define
18514 || macinfo_type == DW_MACRO_GNU_undef)
18515 {
18516 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18517 mac_ptr += bytes_read;
18518 }
18519 else
18520 {
18521 LONGEST str_offset;
18522
18523 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18524 mac_ptr += offset_size;
18525
18526 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18527 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18528 || section_is_dwz)
18529 {
18530 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18531
18532 body = read_indirect_string_from_dwz (dwz, str_offset);
18533 }
18534 else
18535 body = read_indirect_string_at_offset (abfd, str_offset);
18536 }
18537
18538 is_define = (macinfo_type == DW_MACRO_GNU_define
18539 || macinfo_type == DW_MACRO_GNU_define_indirect
18540 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18541 if (! current_file)
18542 {
18543 /* DWARF violation as no main source is present. */
18544 complaint (&symfile_complaints,
18545 _("debug info with no main source gives macro %s "
18546 "on line %d: %s"),
18547 is_define ? _("definition") : _("undefinition"),
18548 line, body);
18549 break;
18550 }
18551 if ((line == 0 && !at_commandline)
18552 || (line != 0 && at_commandline))
18553 complaint (&symfile_complaints,
18554 _("debug info gives %s macro %s with %s line %d: %s"),
18555 at_commandline ? _("command-line") : _("in-file"),
18556 is_define ? _("definition") : _("undefinition"),
18557 line == 0 ? _("zero") : _("non-zero"), line, body);
18558
18559 if (is_define)
18560 parse_macro_definition (current_file, line, body);
18561 else
18562 {
18563 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18564 || macinfo_type == DW_MACRO_GNU_undef_indirect
18565 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18566 macro_undef (current_file, line, body);
18567 }
18568 }
18569 break;
18570
18571 case DW_MACRO_GNU_start_file:
18572 {
18573 unsigned int bytes_read;
18574 int line, file;
18575
18576 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18577 mac_ptr += bytes_read;
18578 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18579 mac_ptr += bytes_read;
18580
18581 if ((line == 0 && !at_commandline)
18582 || (line != 0 && at_commandline))
18583 complaint (&symfile_complaints,
18584 _("debug info gives source %d included "
18585 "from %s at %s line %d"),
18586 file, at_commandline ? _("command-line") : _("file"),
18587 line == 0 ? _("zero") : _("non-zero"), line);
18588
18589 if (at_commandline)
18590 {
18591 /* This DW_MACRO_GNU_start_file was executed in the
18592 pass one. */
18593 at_commandline = 0;
18594 }
18595 else
18596 current_file = macro_start_file (file, line,
18597 current_file, comp_dir,
18598 lh, objfile);
18599 }
18600 break;
18601
18602 case DW_MACRO_GNU_end_file:
18603 if (! current_file)
18604 complaint (&symfile_complaints,
18605 _("macro debug info has an unmatched "
18606 "`close_file' directive"));
18607 else
18608 {
18609 current_file = current_file->included_by;
18610 if (! current_file)
18611 {
18612 enum dwarf_macro_record_type next_type;
18613
18614 /* GCC circa March 2002 doesn't produce the zero
18615 type byte marking the end of the compilation
18616 unit. Complain if it's not there, but exit no
18617 matter what. */
18618
18619 /* Do we at least have room for a macinfo type byte? */
18620 if (mac_ptr >= mac_end)
18621 {
18622 dwarf2_section_buffer_overflow_complaint (section);
18623 return;
18624 }
18625
18626 /* We don't increment mac_ptr here, so this is just
18627 a look-ahead. */
18628 next_type = read_1_byte (abfd, mac_ptr);
18629 if (next_type != 0)
18630 complaint (&symfile_complaints,
18631 _("no terminating 0-type entry for "
18632 "macros in `.debug_macinfo' section"));
18633
18634 return;
18635 }
18636 }
18637 break;
18638
18639 case DW_MACRO_GNU_transparent_include:
18640 case DW_MACRO_GNU_transparent_include_alt:
18641 {
18642 LONGEST offset;
18643 void **slot;
18644 bfd *include_bfd = abfd;
18645 struct dwarf2_section_info *include_section = section;
18646 struct dwarf2_section_info alt_section;
18647 gdb_byte *include_mac_end = mac_end;
18648 int is_dwz = section_is_dwz;
18649 gdb_byte *new_mac_ptr;
18650
18651 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18652 mac_ptr += offset_size;
18653
18654 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18655 {
18656 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18657
18658 dwarf2_read_section (dwarf2_per_objfile->objfile,
18659 &dwz->macro);
18660
18661 include_bfd = dwz->macro.asection->owner;
18662 include_section = &dwz->macro;
18663 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18664 is_dwz = 1;
18665 }
18666
18667 new_mac_ptr = include_section->buffer + offset;
18668 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18669
18670 if (*slot != NULL)
18671 {
18672 /* This has actually happened; see
18673 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18674 complaint (&symfile_complaints,
18675 _("recursive DW_MACRO_GNU_transparent_include in "
18676 ".debug_macro section"));
18677 }
18678 else
18679 {
18680 *slot = new_mac_ptr;
18681
18682 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18683 include_mac_end, current_file,
18684 lh, comp_dir,
18685 section, section_is_gnu, is_dwz,
18686 offset_size, objfile, include_hash);
18687
18688 htab_remove_elt (include_hash, new_mac_ptr);
18689 }
18690 }
18691 break;
18692
18693 case DW_MACINFO_vendor_ext:
18694 if (!section_is_gnu)
18695 {
18696 unsigned int bytes_read;
18697 int constant;
18698
18699 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18700 mac_ptr += bytes_read;
18701 read_direct_string (abfd, mac_ptr, &bytes_read);
18702 mac_ptr += bytes_read;
18703
18704 /* We don't recognize any vendor extensions. */
18705 break;
18706 }
18707 /* FALLTHROUGH */
18708
18709 default:
18710 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18711 mac_ptr, mac_end, abfd, offset_size,
18712 section);
18713 if (mac_ptr == NULL)
18714 return;
18715 break;
18716 }
18717 } while (macinfo_type != 0);
18718 }
18719
18720 static void
18721 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18722 const char *comp_dir, int section_is_gnu)
18723 {
18724 struct objfile *objfile = dwarf2_per_objfile->objfile;
18725 struct line_header *lh = cu->line_header;
18726 bfd *abfd;
18727 gdb_byte *mac_ptr, *mac_end;
18728 struct macro_source_file *current_file = 0;
18729 enum dwarf_macro_record_type macinfo_type;
18730 unsigned int offset_size = cu->header.offset_size;
18731 gdb_byte *opcode_definitions[256];
18732 struct cleanup *cleanup;
18733 htab_t include_hash;
18734 void **slot;
18735 struct dwarf2_section_info *section;
18736 const char *section_name;
18737
18738 if (cu->dwo_unit != NULL)
18739 {
18740 if (section_is_gnu)
18741 {
18742 section = &cu->dwo_unit->dwo_file->sections.macro;
18743 section_name = ".debug_macro.dwo";
18744 }
18745 else
18746 {
18747 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18748 section_name = ".debug_macinfo.dwo";
18749 }
18750 }
18751 else
18752 {
18753 if (section_is_gnu)
18754 {
18755 section = &dwarf2_per_objfile->macro;
18756 section_name = ".debug_macro";
18757 }
18758 else
18759 {
18760 section = &dwarf2_per_objfile->macinfo;
18761 section_name = ".debug_macinfo";
18762 }
18763 }
18764
18765 dwarf2_read_section (objfile, section);
18766 if (section->buffer == NULL)
18767 {
18768 complaint (&symfile_complaints, _("missing %s section"), section_name);
18769 return;
18770 }
18771 abfd = section->asection->owner;
18772
18773 /* First pass: Find the name of the base filename.
18774 This filename is needed in order to process all macros whose definition
18775 (or undefinition) comes from the command line. These macros are defined
18776 before the first DW_MACINFO_start_file entry, and yet still need to be
18777 associated to the base file.
18778
18779 To determine the base file name, we scan the macro definitions until we
18780 reach the first DW_MACINFO_start_file entry. We then initialize
18781 CURRENT_FILE accordingly so that any macro definition found before the
18782 first DW_MACINFO_start_file can still be associated to the base file. */
18783
18784 mac_ptr = section->buffer + offset;
18785 mac_end = section->buffer + section->size;
18786
18787 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18788 &offset_size, section_is_gnu);
18789 if (mac_ptr == NULL)
18790 {
18791 /* We already issued a complaint. */
18792 return;
18793 }
18794
18795 do
18796 {
18797 /* Do we at least have room for a macinfo type byte? */
18798 if (mac_ptr >= mac_end)
18799 {
18800 /* Complaint is printed during the second pass as GDB will probably
18801 stop the first pass earlier upon finding
18802 DW_MACINFO_start_file. */
18803 break;
18804 }
18805
18806 macinfo_type = read_1_byte (abfd, mac_ptr);
18807 mac_ptr++;
18808
18809 /* Note that we rely on the fact that the corresponding GNU and
18810 DWARF constants are the same. */
18811 switch (macinfo_type)
18812 {
18813 /* A zero macinfo type indicates the end of the macro
18814 information. */
18815 case 0:
18816 break;
18817
18818 case DW_MACRO_GNU_define:
18819 case DW_MACRO_GNU_undef:
18820 /* Only skip the data by MAC_PTR. */
18821 {
18822 unsigned int bytes_read;
18823
18824 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18825 mac_ptr += bytes_read;
18826 read_direct_string (abfd, mac_ptr, &bytes_read);
18827 mac_ptr += bytes_read;
18828 }
18829 break;
18830
18831 case DW_MACRO_GNU_start_file:
18832 {
18833 unsigned int bytes_read;
18834 int line, file;
18835
18836 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18837 mac_ptr += bytes_read;
18838 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18839 mac_ptr += bytes_read;
18840
18841 current_file = macro_start_file (file, line, current_file,
18842 comp_dir, lh, objfile);
18843 }
18844 break;
18845
18846 case DW_MACRO_GNU_end_file:
18847 /* No data to skip by MAC_PTR. */
18848 break;
18849
18850 case DW_MACRO_GNU_define_indirect:
18851 case DW_MACRO_GNU_undef_indirect:
18852 case DW_MACRO_GNU_define_indirect_alt:
18853 case DW_MACRO_GNU_undef_indirect_alt:
18854 {
18855 unsigned int bytes_read;
18856
18857 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18858 mac_ptr += bytes_read;
18859 mac_ptr += offset_size;
18860 }
18861 break;
18862
18863 case DW_MACRO_GNU_transparent_include:
18864 case DW_MACRO_GNU_transparent_include_alt:
18865 /* Note that, according to the spec, a transparent include
18866 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18867 skip this opcode. */
18868 mac_ptr += offset_size;
18869 break;
18870
18871 case DW_MACINFO_vendor_ext:
18872 /* Only skip the data by MAC_PTR. */
18873 if (!section_is_gnu)
18874 {
18875 unsigned int bytes_read;
18876
18877 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18878 mac_ptr += bytes_read;
18879 read_direct_string (abfd, mac_ptr, &bytes_read);
18880 mac_ptr += bytes_read;
18881 }
18882 /* FALLTHROUGH */
18883
18884 default:
18885 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18886 mac_ptr, mac_end, abfd, offset_size,
18887 section);
18888 if (mac_ptr == NULL)
18889 return;
18890 break;
18891 }
18892 } while (macinfo_type != 0 && current_file == NULL);
18893
18894 /* Second pass: Process all entries.
18895
18896 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18897 command-line macro definitions/undefinitions. This flag is unset when we
18898 reach the first DW_MACINFO_start_file entry. */
18899
18900 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18901 NULL, xcalloc, xfree);
18902 cleanup = make_cleanup_htab_delete (include_hash);
18903 mac_ptr = section->buffer + offset;
18904 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18905 *slot = mac_ptr;
18906 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18907 current_file, lh, comp_dir, section,
18908 section_is_gnu, 0,
18909 offset_size, objfile, include_hash);
18910 do_cleanups (cleanup);
18911 }
18912
18913 /* Check if the attribute's form is a DW_FORM_block*
18914 if so return true else false. */
18915
18916 static int
18917 attr_form_is_block (struct attribute *attr)
18918 {
18919 return (attr == NULL ? 0 :
18920 attr->form == DW_FORM_block1
18921 || attr->form == DW_FORM_block2
18922 || attr->form == DW_FORM_block4
18923 || attr->form == DW_FORM_block
18924 || attr->form == DW_FORM_exprloc);
18925 }
18926
18927 /* Return non-zero if ATTR's value is a section offset --- classes
18928 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18929 You may use DW_UNSND (attr) to retrieve such offsets.
18930
18931 Section 7.5.4, "Attribute Encodings", explains that no attribute
18932 may have a value that belongs to more than one of these classes; it
18933 would be ambiguous if we did, because we use the same forms for all
18934 of them. */
18935
18936 static int
18937 attr_form_is_section_offset (struct attribute *attr)
18938 {
18939 return (attr->form == DW_FORM_data4
18940 || attr->form == DW_FORM_data8
18941 || attr->form == DW_FORM_sec_offset);
18942 }
18943
18944 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18945 zero otherwise. When this function returns true, you can apply
18946 dwarf2_get_attr_constant_value to it.
18947
18948 However, note that for some attributes you must check
18949 attr_form_is_section_offset before using this test. DW_FORM_data4
18950 and DW_FORM_data8 are members of both the constant class, and of
18951 the classes that contain offsets into other debug sections
18952 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18953 that, if an attribute's can be either a constant or one of the
18954 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18955 taken as section offsets, not constants. */
18956
18957 static int
18958 attr_form_is_constant (struct attribute *attr)
18959 {
18960 switch (attr->form)
18961 {
18962 case DW_FORM_sdata:
18963 case DW_FORM_udata:
18964 case DW_FORM_data1:
18965 case DW_FORM_data2:
18966 case DW_FORM_data4:
18967 case DW_FORM_data8:
18968 return 1;
18969 default:
18970 return 0;
18971 }
18972 }
18973
18974 /* Return the .debug_loc section to use for CU.
18975 For DWO files use .debug_loc.dwo. */
18976
18977 static struct dwarf2_section_info *
18978 cu_debug_loc_section (struct dwarf2_cu *cu)
18979 {
18980 if (cu->dwo_unit)
18981 return &cu->dwo_unit->dwo_file->sections.loc;
18982 return &dwarf2_per_objfile->loc;
18983 }
18984
18985 /* A helper function that fills in a dwarf2_loclist_baton. */
18986
18987 static void
18988 fill_in_loclist_baton (struct dwarf2_cu *cu,
18989 struct dwarf2_loclist_baton *baton,
18990 struct attribute *attr)
18991 {
18992 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18993
18994 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18995
18996 baton->per_cu = cu->per_cu;
18997 gdb_assert (baton->per_cu);
18998 /* We don't know how long the location list is, but make sure we
18999 don't run off the edge of the section. */
19000 baton->size = section->size - DW_UNSND (attr);
19001 baton->data = section->buffer + DW_UNSND (attr);
19002 baton->base_address = cu->base_address;
19003 baton->from_dwo = cu->dwo_unit != NULL;
19004 }
19005
19006 static void
19007 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19008 struct dwarf2_cu *cu)
19009 {
19010 struct objfile *objfile = dwarf2_per_objfile->objfile;
19011 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19012
19013 if (attr_form_is_section_offset (attr)
19014 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19015 the section. If so, fall through to the complaint in the
19016 other branch. */
19017 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19018 {
19019 struct dwarf2_loclist_baton *baton;
19020
19021 baton = obstack_alloc (&objfile->objfile_obstack,
19022 sizeof (struct dwarf2_loclist_baton));
19023
19024 fill_in_loclist_baton (cu, baton, attr);
19025
19026 if (cu->base_known == 0)
19027 complaint (&symfile_complaints,
19028 _("Location list used without "
19029 "specifying the CU base address."));
19030
19031 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
19032 SYMBOL_LOCATION_BATON (sym) = baton;
19033 }
19034 else
19035 {
19036 struct dwarf2_locexpr_baton *baton;
19037
19038 baton = obstack_alloc (&objfile->objfile_obstack,
19039 sizeof (struct dwarf2_locexpr_baton));
19040 baton->per_cu = cu->per_cu;
19041 gdb_assert (baton->per_cu);
19042
19043 if (attr_form_is_block (attr))
19044 {
19045 /* Note that we're just copying the block's data pointer
19046 here, not the actual data. We're still pointing into the
19047 info_buffer for SYM's objfile; right now we never release
19048 that buffer, but when we do clean up properly this may
19049 need to change. */
19050 baton->size = DW_BLOCK (attr)->size;
19051 baton->data = DW_BLOCK (attr)->data;
19052 }
19053 else
19054 {
19055 dwarf2_invalid_attrib_class_complaint ("location description",
19056 SYMBOL_NATURAL_NAME (sym));
19057 baton->size = 0;
19058 }
19059
19060 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
19061 SYMBOL_LOCATION_BATON (sym) = baton;
19062 }
19063 }
19064
19065 /* Return the OBJFILE associated with the compilation unit CU. If CU
19066 came from a separate debuginfo file, then the master objfile is
19067 returned. */
19068
19069 struct objfile *
19070 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19071 {
19072 struct objfile *objfile = per_cu->objfile;
19073
19074 /* Return the master objfile, so that we can report and look up the
19075 correct file containing this variable. */
19076 if (objfile->separate_debug_objfile_backlink)
19077 objfile = objfile->separate_debug_objfile_backlink;
19078
19079 return objfile;
19080 }
19081
19082 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19083 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19084 CU_HEADERP first. */
19085
19086 static const struct comp_unit_head *
19087 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19088 struct dwarf2_per_cu_data *per_cu)
19089 {
19090 gdb_byte *info_ptr;
19091
19092 if (per_cu->cu)
19093 return &per_cu->cu->header;
19094
19095 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19096
19097 memset (cu_headerp, 0, sizeof (*cu_headerp));
19098 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19099
19100 return cu_headerp;
19101 }
19102
19103 /* Return the address size given in the compilation unit header for CU. */
19104
19105 int
19106 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19107 {
19108 struct comp_unit_head cu_header_local;
19109 const struct comp_unit_head *cu_headerp;
19110
19111 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19112
19113 return cu_headerp->addr_size;
19114 }
19115
19116 /* Return the offset size given in the compilation unit header for CU. */
19117
19118 int
19119 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19120 {
19121 struct comp_unit_head cu_header_local;
19122 const struct comp_unit_head *cu_headerp;
19123
19124 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19125
19126 return cu_headerp->offset_size;
19127 }
19128
19129 /* See its dwarf2loc.h declaration. */
19130
19131 int
19132 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19133 {
19134 struct comp_unit_head cu_header_local;
19135 const struct comp_unit_head *cu_headerp;
19136
19137 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19138
19139 if (cu_headerp->version == 2)
19140 return cu_headerp->addr_size;
19141 else
19142 return cu_headerp->offset_size;
19143 }
19144
19145 /* Return the text offset of the CU. The returned offset comes from
19146 this CU's objfile. If this objfile came from a separate debuginfo
19147 file, then the offset may be different from the corresponding
19148 offset in the parent objfile. */
19149
19150 CORE_ADDR
19151 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19152 {
19153 struct objfile *objfile = per_cu->objfile;
19154
19155 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19156 }
19157
19158 /* Locate the .debug_info compilation unit from CU's objfile which contains
19159 the DIE at OFFSET. Raises an error on failure. */
19160
19161 static struct dwarf2_per_cu_data *
19162 dwarf2_find_containing_comp_unit (sect_offset offset,
19163 unsigned int offset_in_dwz,
19164 struct objfile *objfile)
19165 {
19166 struct dwarf2_per_cu_data *this_cu;
19167 int low, high;
19168 const sect_offset *cu_off;
19169
19170 low = 0;
19171 high = dwarf2_per_objfile->n_comp_units - 1;
19172 while (high > low)
19173 {
19174 struct dwarf2_per_cu_data *mid_cu;
19175 int mid = low + (high - low) / 2;
19176
19177 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19178 cu_off = &mid_cu->offset;
19179 if (mid_cu->is_dwz > offset_in_dwz
19180 || (mid_cu->is_dwz == offset_in_dwz
19181 && cu_off->sect_off >= offset.sect_off))
19182 high = mid;
19183 else
19184 low = mid + 1;
19185 }
19186 gdb_assert (low == high);
19187 this_cu = dwarf2_per_objfile->all_comp_units[low];
19188 cu_off = &this_cu->offset;
19189 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19190 {
19191 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19192 error (_("Dwarf Error: could not find partial DIE containing "
19193 "offset 0x%lx [in module %s]"),
19194 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19195
19196 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19197 <= offset.sect_off);
19198 return dwarf2_per_objfile->all_comp_units[low-1];
19199 }
19200 else
19201 {
19202 this_cu = dwarf2_per_objfile->all_comp_units[low];
19203 if (low == dwarf2_per_objfile->n_comp_units - 1
19204 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19205 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19206 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19207 return this_cu;
19208 }
19209 }
19210
19211 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19212
19213 static void
19214 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19215 {
19216 memset (cu, 0, sizeof (*cu));
19217 per_cu->cu = cu;
19218 cu->per_cu = per_cu;
19219 cu->objfile = per_cu->objfile;
19220 obstack_init (&cu->comp_unit_obstack);
19221 }
19222
19223 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19224
19225 static void
19226 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19227 enum language pretend_language)
19228 {
19229 struct attribute *attr;
19230
19231 /* Set the language we're debugging. */
19232 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19233 if (attr)
19234 set_cu_language (DW_UNSND (attr), cu);
19235 else
19236 {
19237 cu->language = pretend_language;
19238 cu->language_defn = language_def (cu->language);
19239 }
19240
19241 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19242 if (attr)
19243 cu->producer = DW_STRING (attr);
19244 }
19245
19246 /* Release one cached compilation unit, CU. We unlink it from the tree
19247 of compilation units, but we don't remove it from the read_in_chain;
19248 the caller is responsible for that.
19249 NOTE: DATA is a void * because this function is also used as a
19250 cleanup routine. */
19251
19252 static void
19253 free_heap_comp_unit (void *data)
19254 {
19255 struct dwarf2_cu *cu = data;
19256
19257 gdb_assert (cu->per_cu != NULL);
19258 cu->per_cu->cu = NULL;
19259 cu->per_cu = NULL;
19260
19261 obstack_free (&cu->comp_unit_obstack, NULL);
19262
19263 xfree (cu);
19264 }
19265
19266 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19267 when we're finished with it. We can't free the pointer itself, but be
19268 sure to unlink it from the cache. Also release any associated storage. */
19269
19270 static void
19271 free_stack_comp_unit (void *data)
19272 {
19273 struct dwarf2_cu *cu = data;
19274
19275 gdb_assert (cu->per_cu != NULL);
19276 cu->per_cu->cu = NULL;
19277 cu->per_cu = NULL;
19278
19279 obstack_free (&cu->comp_unit_obstack, NULL);
19280 cu->partial_dies = NULL;
19281 }
19282
19283 /* Free all cached compilation units. */
19284
19285 static void
19286 free_cached_comp_units (void *data)
19287 {
19288 struct dwarf2_per_cu_data *per_cu, **last_chain;
19289
19290 per_cu = dwarf2_per_objfile->read_in_chain;
19291 last_chain = &dwarf2_per_objfile->read_in_chain;
19292 while (per_cu != NULL)
19293 {
19294 struct dwarf2_per_cu_data *next_cu;
19295
19296 next_cu = per_cu->cu->read_in_chain;
19297
19298 free_heap_comp_unit (per_cu->cu);
19299 *last_chain = next_cu;
19300
19301 per_cu = next_cu;
19302 }
19303 }
19304
19305 /* Increase the age counter on each cached compilation unit, and free
19306 any that are too old. */
19307
19308 static void
19309 age_cached_comp_units (void)
19310 {
19311 struct dwarf2_per_cu_data *per_cu, **last_chain;
19312
19313 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19314 per_cu = dwarf2_per_objfile->read_in_chain;
19315 while (per_cu != NULL)
19316 {
19317 per_cu->cu->last_used ++;
19318 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19319 dwarf2_mark (per_cu->cu);
19320 per_cu = per_cu->cu->read_in_chain;
19321 }
19322
19323 per_cu = dwarf2_per_objfile->read_in_chain;
19324 last_chain = &dwarf2_per_objfile->read_in_chain;
19325 while (per_cu != NULL)
19326 {
19327 struct dwarf2_per_cu_data *next_cu;
19328
19329 next_cu = per_cu->cu->read_in_chain;
19330
19331 if (!per_cu->cu->mark)
19332 {
19333 free_heap_comp_unit (per_cu->cu);
19334 *last_chain = next_cu;
19335 }
19336 else
19337 last_chain = &per_cu->cu->read_in_chain;
19338
19339 per_cu = next_cu;
19340 }
19341 }
19342
19343 /* Remove a single compilation unit from the cache. */
19344
19345 static void
19346 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19347 {
19348 struct dwarf2_per_cu_data *per_cu, **last_chain;
19349
19350 per_cu = dwarf2_per_objfile->read_in_chain;
19351 last_chain = &dwarf2_per_objfile->read_in_chain;
19352 while (per_cu != NULL)
19353 {
19354 struct dwarf2_per_cu_data *next_cu;
19355
19356 next_cu = per_cu->cu->read_in_chain;
19357
19358 if (per_cu == target_per_cu)
19359 {
19360 free_heap_comp_unit (per_cu->cu);
19361 per_cu->cu = NULL;
19362 *last_chain = next_cu;
19363 break;
19364 }
19365 else
19366 last_chain = &per_cu->cu->read_in_chain;
19367
19368 per_cu = next_cu;
19369 }
19370 }
19371
19372 /* Release all extra memory associated with OBJFILE. */
19373
19374 void
19375 dwarf2_free_objfile (struct objfile *objfile)
19376 {
19377 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19378
19379 if (dwarf2_per_objfile == NULL)
19380 return;
19381
19382 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19383 free_cached_comp_units (NULL);
19384
19385 if (dwarf2_per_objfile->quick_file_names_table)
19386 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19387
19388 /* Everything else should be on the objfile obstack. */
19389 }
19390
19391 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19392 We store these in a hash table separate from the DIEs, and preserve them
19393 when the DIEs are flushed out of cache.
19394
19395 The CU "per_cu" pointer is needed because offset alone is not enough to
19396 uniquely identify the type. A file may have multiple .debug_types sections,
19397 or the type may come from a DWO file. We have to use something in
19398 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19399 routine, get_die_type_at_offset, from outside this file, and thus won't
19400 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19401 of the objfile. */
19402
19403 struct dwarf2_per_cu_offset_and_type
19404 {
19405 const struct dwarf2_per_cu_data *per_cu;
19406 sect_offset offset;
19407 struct type *type;
19408 };
19409
19410 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19411
19412 static hashval_t
19413 per_cu_offset_and_type_hash (const void *item)
19414 {
19415 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19416
19417 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19418 }
19419
19420 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19421
19422 static int
19423 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19424 {
19425 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19426 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19427
19428 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19429 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19430 }
19431
19432 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19433 table if necessary. For convenience, return TYPE.
19434
19435 The DIEs reading must have careful ordering to:
19436 * Not cause infite loops trying to read in DIEs as a prerequisite for
19437 reading current DIE.
19438 * Not trying to dereference contents of still incompletely read in types
19439 while reading in other DIEs.
19440 * Enable referencing still incompletely read in types just by a pointer to
19441 the type without accessing its fields.
19442
19443 Therefore caller should follow these rules:
19444 * Try to fetch any prerequisite types we may need to build this DIE type
19445 before building the type and calling set_die_type.
19446 * After building type call set_die_type for current DIE as soon as
19447 possible before fetching more types to complete the current type.
19448 * Make the type as complete as possible before fetching more types. */
19449
19450 static struct type *
19451 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19452 {
19453 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19454 struct objfile *objfile = cu->objfile;
19455
19456 /* For Ada types, make sure that the gnat-specific data is always
19457 initialized (if not already set). There are a few types where
19458 we should not be doing so, because the type-specific area is
19459 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19460 where the type-specific area is used to store the floatformat).
19461 But this is not a problem, because the gnat-specific information
19462 is actually not needed for these types. */
19463 if (need_gnat_info (cu)
19464 && TYPE_CODE (type) != TYPE_CODE_FUNC
19465 && TYPE_CODE (type) != TYPE_CODE_FLT
19466 && !HAVE_GNAT_AUX_INFO (type))
19467 INIT_GNAT_SPECIFIC (type);
19468
19469 if (dwarf2_per_objfile->die_type_hash == NULL)
19470 {
19471 dwarf2_per_objfile->die_type_hash =
19472 htab_create_alloc_ex (127,
19473 per_cu_offset_and_type_hash,
19474 per_cu_offset_and_type_eq,
19475 NULL,
19476 &objfile->objfile_obstack,
19477 hashtab_obstack_allocate,
19478 dummy_obstack_deallocate);
19479 }
19480
19481 ofs.per_cu = cu->per_cu;
19482 ofs.offset = die->offset;
19483 ofs.type = type;
19484 slot = (struct dwarf2_per_cu_offset_and_type **)
19485 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19486 if (*slot)
19487 complaint (&symfile_complaints,
19488 _("A problem internal to GDB: DIE 0x%x has type already set"),
19489 die->offset.sect_off);
19490 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19491 **slot = ofs;
19492 return type;
19493 }
19494
19495 /* Look up the type for the die at OFFSET in the appropriate type_hash
19496 table, or return NULL if the die does not have a saved type. */
19497
19498 static struct type *
19499 get_die_type_at_offset (sect_offset offset,
19500 struct dwarf2_per_cu_data *per_cu)
19501 {
19502 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19503
19504 if (dwarf2_per_objfile->die_type_hash == NULL)
19505 return NULL;
19506
19507 ofs.per_cu = per_cu;
19508 ofs.offset = offset;
19509 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19510 if (slot)
19511 return slot->type;
19512 else
19513 return NULL;
19514 }
19515
19516 /* Look up the type for DIE in the appropriate type_hash table,
19517 or return NULL if DIE does not have a saved type. */
19518
19519 static struct type *
19520 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19521 {
19522 return get_die_type_at_offset (die->offset, cu->per_cu);
19523 }
19524
19525 /* Add a dependence relationship from CU to REF_PER_CU. */
19526
19527 static void
19528 dwarf2_add_dependence (struct dwarf2_cu *cu,
19529 struct dwarf2_per_cu_data *ref_per_cu)
19530 {
19531 void **slot;
19532
19533 if (cu->dependencies == NULL)
19534 cu->dependencies
19535 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19536 NULL, &cu->comp_unit_obstack,
19537 hashtab_obstack_allocate,
19538 dummy_obstack_deallocate);
19539
19540 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19541 if (*slot == NULL)
19542 *slot = ref_per_cu;
19543 }
19544
19545 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19546 Set the mark field in every compilation unit in the
19547 cache that we must keep because we are keeping CU. */
19548
19549 static int
19550 dwarf2_mark_helper (void **slot, void *data)
19551 {
19552 struct dwarf2_per_cu_data *per_cu;
19553
19554 per_cu = (struct dwarf2_per_cu_data *) *slot;
19555
19556 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19557 reading of the chain. As such dependencies remain valid it is not much
19558 useful to track and undo them during QUIT cleanups. */
19559 if (per_cu->cu == NULL)
19560 return 1;
19561
19562 if (per_cu->cu->mark)
19563 return 1;
19564 per_cu->cu->mark = 1;
19565
19566 if (per_cu->cu->dependencies != NULL)
19567 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19568
19569 return 1;
19570 }
19571
19572 /* Set the mark field in CU and in every other compilation unit in the
19573 cache that we must keep because we are keeping CU. */
19574
19575 static void
19576 dwarf2_mark (struct dwarf2_cu *cu)
19577 {
19578 if (cu->mark)
19579 return;
19580 cu->mark = 1;
19581 if (cu->dependencies != NULL)
19582 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19583 }
19584
19585 static void
19586 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19587 {
19588 while (per_cu)
19589 {
19590 per_cu->cu->mark = 0;
19591 per_cu = per_cu->cu->read_in_chain;
19592 }
19593 }
19594
19595 /* Trivial hash function for partial_die_info: the hash value of a DIE
19596 is its offset in .debug_info for this objfile. */
19597
19598 static hashval_t
19599 partial_die_hash (const void *item)
19600 {
19601 const struct partial_die_info *part_die = item;
19602
19603 return part_die->offset.sect_off;
19604 }
19605
19606 /* Trivial comparison function for partial_die_info structures: two DIEs
19607 are equal if they have the same offset. */
19608
19609 static int
19610 partial_die_eq (const void *item_lhs, const void *item_rhs)
19611 {
19612 const struct partial_die_info *part_die_lhs = item_lhs;
19613 const struct partial_die_info *part_die_rhs = item_rhs;
19614
19615 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19616 }
19617
19618 static struct cmd_list_element *set_dwarf2_cmdlist;
19619 static struct cmd_list_element *show_dwarf2_cmdlist;
19620
19621 static void
19622 set_dwarf2_cmd (char *args, int from_tty)
19623 {
19624 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19625 }
19626
19627 static void
19628 show_dwarf2_cmd (char *args, int from_tty)
19629 {
19630 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19631 }
19632
19633 /* Free data associated with OBJFILE, if necessary. */
19634
19635 static void
19636 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19637 {
19638 struct dwarf2_per_objfile *data = d;
19639 int ix;
19640
19641 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19642 VEC_free (dwarf2_per_cu_ptr,
19643 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19644
19645 VEC_free (dwarf2_section_info_def, data->types);
19646
19647 if (data->dwo_files)
19648 free_dwo_files (data->dwo_files, objfile);
19649
19650 if (data->dwz_file && data->dwz_file->dwz_bfd)
19651 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19652 }
19653
19654 \f
19655 /* The "save gdb-index" command. */
19656
19657 /* The contents of the hash table we create when building the string
19658 table. */
19659 struct strtab_entry
19660 {
19661 offset_type offset;
19662 const char *str;
19663 };
19664
19665 /* Hash function for a strtab_entry.
19666
19667 Function is used only during write_hash_table so no index format backward
19668 compatibility is needed. */
19669
19670 static hashval_t
19671 hash_strtab_entry (const void *e)
19672 {
19673 const struct strtab_entry *entry = e;
19674 return mapped_index_string_hash (INT_MAX, entry->str);
19675 }
19676
19677 /* Equality function for a strtab_entry. */
19678
19679 static int
19680 eq_strtab_entry (const void *a, const void *b)
19681 {
19682 const struct strtab_entry *ea = a;
19683 const struct strtab_entry *eb = b;
19684 return !strcmp (ea->str, eb->str);
19685 }
19686
19687 /* Create a strtab_entry hash table. */
19688
19689 static htab_t
19690 create_strtab (void)
19691 {
19692 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19693 xfree, xcalloc, xfree);
19694 }
19695
19696 /* Add a string to the constant pool. Return the string's offset in
19697 host order. */
19698
19699 static offset_type
19700 add_string (htab_t table, struct obstack *cpool, const char *str)
19701 {
19702 void **slot;
19703 struct strtab_entry entry;
19704 struct strtab_entry *result;
19705
19706 entry.str = str;
19707 slot = htab_find_slot (table, &entry, INSERT);
19708 if (*slot)
19709 result = *slot;
19710 else
19711 {
19712 result = XNEW (struct strtab_entry);
19713 result->offset = obstack_object_size (cpool);
19714 result->str = str;
19715 obstack_grow_str0 (cpool, str);
19716 *slot = result;
19717 }
19718 return result->offset;
19719 }
19720
19721 /* An entry in the symbol table. */
19722 struct symtab_index_entry
19723 {
19724 /* The name of the symbol. */
19725 const char *name;
19726 /* The offset of the name in the constant pool. */
19727 offset_type index_offset;
19728 /* A sorted vector of the indices of all the CUs that hold an object
19729 of this name. */
19730 VEC (offset_type) *cu_indices;
19731 };
19732
19733 /* The symbol table. This is a power-of-2-sized hash table. */
19734 struct mapped_symtab
19735 {
19736 offset_type n_elements;
19737 offset_type size;
19738 struct symtab_index_entry **data;
19739 };
19740
19741 /* Hash function for a symtab_index_entry. */
19742
19743 static hashval_t
19744 hash_symtab_entry (const void *e)
19745 {
19746 const struct symtab_index_entry *entry = e;
19747 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19748 sizeof (offset_type) * VEC_length (offset_type,
19749 entry->cu_indices),
19750 0);
19751 }
19752
19753 /* Equality function for a symtab_index_entry. */
19754
19755 static int
19756 eq_symtab_entry (const void *a, const void *b)
19757 {
19758 const struct symtab_index_entry *ea = a;
19759 const struct symtab_index_entry *eb = b;
19760 int len = VEC_length (offset_type, ea->cu_indices);
19761 if (len != VEC_length (offset_type, eb->cu_indices))
19762 return 0;
19763 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19764 VEC_address (offset_type, eb->cu_indices),
19765 sizeof (offset_type) * len);
19766 }
19767
19768 /* Destroy a symtab_index_entry. */
19769
19770 static void
19771 delete_symtab_entry (void *p)
19772 {
19773 struct symtab_index_entry *entry = p;
19774 VEC_free (offset_type, entry->cu_indices);
19775 xfree (entry);
19776 }
19777
19778 /* Create a hash table holding symtab_index_entry objects. */
19779
19780 static htab_t
19781 create_symbol_hash_table (void)
19782 {
19783 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19784 delete_symtab_entry, xcalloc, xfree);
19785 }
19786
19787 /* Create a new mapped symtab object. */
19788
19789 static struct mapped_symtab *
19790 create_mapped_symtab (void)
19791 {
19792 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19793 symtab->n_elements = 0;
19794 symtab->size = 1024;
19795 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19796 return symtab;
19797 }
19798
19799 /* Destroy a mapped_symtab. */
19800
19801 static void
19802 cleanup_mapped_symtab (void *p)
19803 {
19804 struct mapped_symtab *symtab = p;
19805 /* The contents of the array are freed when the other hash table is
19806 destroyed. */
19807 xfree (symtab->data);
19808 xfree (symtab);
19809 }
19810
19811 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19812 the slot.
19813
19814 Function is used only during write_hash_table so no index format backward
19815 compatibility is needed. */
19816
19817 static struct symtab_index_entry **
19818 find_slot (struct mapped_symtab *symtab, const char *name)
19819 {
19820 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19821
19822 index = hash & (symtab->size - 1);
19823 step = ((hash * 17) & (symtab->size - 1)) | 1;
19824
19825 for (;;)
19826 {
19827 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19828 return &symtab->data[index];
19829 index = (index + step) & (symtab->size - 1);
19830 }
19831 }
19832
19833 /* Expand SYMTAB's hash table. */
19834
19835 static void
19836 hash_expand (struct mapped_symtab *symtab)
19837 {
19838 offset_type old_size = symtab->size;
19839 offset_type i;
19840 struct symtab_index_entry **old_entries = symtab->data;
19841
19842 symtab->size *= 2;
19843 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19844
19845 for (i = 0; i < old_size; ++i)
19846 {
19847 if (old_entries[i])
19848 {
19849 struct symtab_index_entry **slot = find_slot (symtab,
19850 old_entries[i]->name);
19851 *slot = old_entries[i];
19852 }
19853 }
19854
19855 xfree (old_entries);
19856 }
19857
19858 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19859 CU_INDEX is the index of the CU in which the symbol appears.
19860 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19861
19862 static void
19863 add_index_entry (struct mapped_symtab *symtab, const char *name,
19864 int is_static, gdb_index_symbol_kind kind,
19865 offset_type cu_index)
19866 {
19867 struct symtab_index_entry **slot;
19868 offset_type cu_index_and_attrs;
19869
19870 ++symtab->n_elements;
19871 if (4 * symtab->n_elements / 3 >= symtab->size)
19872 hash_expand (symtab);
19873
19874 slot = find_slot (symtab, name);
19875 if (!*slot)
19876 {
19877 *slot = XNEW (struct symtab_index_entry);
19878 (*slot)->name = name;
19879 /* index_offset is set later. */
19880 (*slot)->cu_indices = NULL;
19881 }
19882
19883 cu_index_and_attrs = 0;
19884 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19885 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19886 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19887
19888 /* We don't want to record an index value twice as we want to avoid the
19889 duplication.
19890 We process all global symbols and then all static symbols
19891 (which would allow us to avoid the duplication by only having to check
19892 the last entry pushed), but a symbol could have multiple kinds in one CU.
19893 To keep things simple we don't worry about the duplication here and
19894 sort and uniqufy the list after we've processed all symbols. */
19895 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19896 }
19897
19898 /* qsort helper routine for uniquify_cu_indices. */
19899
19900 static int
19901 offset_type_compare (const void *ap, const void *bp)
19902 {
19903 offset_type a = *(offset_type *) ap;
19904 offset_type b = *(offset_type *) bp;
19905
19906 return (a > b) - (b > a);
19907 }
19908
19909 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19910
19911 static void
19912 uniquify_cu_indices (struct mapped_symtab *symtab)
19913 {
19914 int i;
19915
19916 for (i = 0; i < symtab->size; ++i)
19917 {
19918 struct symtab_index_entry *entry = symtab->data[i];
19919
19920 if (entry
19921 && entry->cu_indices != NULL)
19922 {
19923 unsigned int next_to_insert, next_to_check;
19924 offset_type last_value;
19925
19926 qsort (VEC_address (offset_type, entry->cu_indices),
19927 VEC_length (offset_type, entry->cu_indices),
19928 sizeof (offset_type), offset_type_compare);
19929
19930 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19931 next_to_insert = 1;
19932 for (next_to_check = 1;
19933 next_to_check < VEC_length (offset_type, entry->cu_indices);
19934 ++next_to_check)
19935 {
19936 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19937 != last_value)
19938 {
19939 last_value = VEC_index (offset_type, entry->cu_indices,
19940 next_to_check);
19941 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19942 last_value);
19943 ++next_to_insert;
19944 }
19945 }
19946 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19947 }
19948 }
19949 }
19950
19951 /* Add a vector of indices to the constant pool. */
19952
19953 static offset_type
19954 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19955 struct symtab_index_entry *entry)
19956 {
19957 void **slot;
19958
19959 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19960 if (!*slot)
19961 {
19962 offset_type len = VEC_length (offset_type, entry->cu_indices);
19963 offset_type val = MAYBE_SWAP (len);
19964 offset_type iter;
19965 int i;
19966
19967 *slot = entry;
19968 entry->index_offset = obstack_object_size (cpool);
19969
19970 obstack_grow (cpool, &val, sizeof (val));
19971 for (i = 0;
19972 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19973 ++i)
19974 {
19975 val = MAYBE_SWAP (iter);
19976 obstack_grow (cpool, &val, sizeof (val));
19977 }
19978 }
19979 else
19980 {
19981 struct symtab_index_entry *old_entry = *slot;
19982 entry->index_offset = old_entry->index_offset;
19983 entry = old_entry;
19984 }
19985 return entry->index_offset;
19986 }
19987
19988 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19989 constant pool entries going into the obstack CPOOL. */
19990
19991 static void
19992 write_hash_table (struct mapped_symtab *symtab,
19993 struct obstack *output, struct obstack *cpool)
19994 {
19995 offset_type i;
19996 htab_t symbol_hash_table;
19997 htab_t str_table;
19998
19999 symbol_hash_table = create_symbol_hash_table ();
20000 str_table = create_strtab ();
20001
20002 /* We add all the index vectors to the constant pool first, to
20003 ensure alignment is ok. */
20004 for (i = 0; i < symtab->size; ++i)
20005 {
20006 if (symtab->data[i])
20007 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20008 }
20009
20010 /* Now write out the hash table. */
20011 for (i = 0; i < symtab->size; ++i)
20012 {
20013 offset_type str_off, vec_off;
20014
20015 if (symtab->data[i])
20016 {
20017 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20018 vec_off = symtab->data[i]->index_offset;
20019 }
20020 else
20021 {
20022 /* While 0 is a valid constant pool index, it is not valid
20023 to have 0 for both offsets. */
20024 str_off = 0;
20025 vec_off = 0;
20026 }
20027
20028 str_off = MAYBE_SWAP (str_off);
20029 vec_off = MAYBE_SWAP (vec_off);
20030
20031 obstack_grow (output, &str_off, sizeof (str_off));
20032 obstack_grow (output, &vec_off, sizeof (vec_off));
20033 }
20034
20035 htab_delete (str_table);
20036 htab_delete (symbol_hash_table);
20037 }
20038
20039 /* Struct to map psymtab to CU index in the index file. */
20040 struct psymtab_cu_index_map
20041 {
20042 struct partial_symtab *psymtab;
20043 unsigned int cu_index;
20044 };
20045
20046 static hashval_t
20047 hash_psymtab_cu_index (const void *item)
20048 {
20049 const struct psymtab_cu_index_map *map = item;
20050
20051 return htab_hash_pointer (map->psymtab);
20052 }
20053
20054 static int
20055 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20056 {
20057 const struct psymtab_cu_index_map *lhs = item_lhs;
20058 const struct psymtab_cu_index_map *rhs = item_rhs;
20059
20060 return lhs->psymtab == rhs->psymtab;
20061 }
20062
20063 /* Helper struct for building the address table. */
20064 struct addrmap_index_data
20065 {
20066 struct objfile *objfile;
20067 struct obstack *addr_obstack;
20068 htab_t cu_index_htab;
20069
20070 /* Non-zero if the previous_* fields are valid.
20071 We can't write an entry until we see the next entry (since it is only then
20072 that we know the end of the entry). */
20073 int previous_valid;
20074 /* Index of the CU in the table of all CUs in the index file. */
20075 unsigned int previous_cu_index;
20076 /* Start address of the CU. */
20077 CORE_ADDR previous_cu_start;
20078 };
20079
20080 /* Write an address entry to OBSTACK. */
20081
20082 static void
20083 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20084 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20085 {
20086 offset_type cu_index_to_write;
20087 char addr[8];
20088 CORE_ADDR baseaddr;
20089
20090 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20091
20092 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20093 obstack_grow (obstack, addr, 8);
20094 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20095 obstack_grow (obstack, addr, 8);
20096 cu_index_to_write = MAYBE_SWAP (cu_index);
20097 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20098 }
20099
20100 /* Worker function for traversing an addrmap to build the address table. */
20101
20102 static int
20103 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20104 {
20105 struct addrmap_index_data *data = datap;
20106 struct partial_symtab *pst = obj;
20107
20108 if (data->previous_valid)
20109 add_address_entry (data->objfile, data->addr_obstack,
20110 data->previous_cu_start, start_addr,
20111 data->previous_cu_index);
20112
20113 data->previous_cu_start = start_addr;
20114 if (pst != NULL)
20115 {
20116 struct psymtab_cu_index_map find_map, *map;
20117 find_map.psymtab = pst;
20118 map = htab_find (data->cu_index_htab, &find_map);
20119 gdb_assert (map != NULL);
20120 data->previous_cu_index = map->cu_index;
20121 data->previous_valid = 1;
20122 }
20123 else
20124 data->previous_valid = 0;
20125
20126 return 0;
20127 }
20128
20129 /* Write OBJFILE's address map to OBSTACK.
20130 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20131 in the index file. */
20132
20133 static void
20134 write_address_map (struct objfile *objfile, struct obstack *obstack,
20135 htab_t cu_index_htab)
20136 {
20137 struct addrmap_index_data addrmap_index_data;
20138
20139 /* When writing the address table, we have to cope with the fact that
20140 the addrmap iterator only provides the start of a region; we have to
20141 wait until the next invocation to get the start of the next region. */
20142
20143 addrmap_index_data.objfile = objfile;
20144 addrmap_index_data.addr_obstack = obstack;
20145 addrmap_index_data.cu_index_htab = cu_index_htab;
20146 addrmap_index_data.previous_valid = 0;
20147
20148 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20149 &addrmap_index_data);
20150
20151 /* It's highly unlikely the last entry (end address = 0xff...ff)
20152 is valid, but we should still handle it.
20153 The end address is recorded as the start of the next region, but that
20154 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20155 anyway. */
20156 if (addrmap_index_data.previous_valid)
20157 add_address_entry (objfile, obstack,
20158 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20159 addrmap_index_data.previous_cu_index);
20160 }
20161
20162 /* Return the symbol kind of PSYM. */
20163
20164 static gdb_index_symbol_kind
20165 symbol_kind (struct partial_symbol *psym)
20166 {
20167 domain_enum domain = PSYMBOL_DOMAIN (psym);
20168 enum address_class aclass = PSYMBOL_CLASS (psym);
20169
20170 switch (domain)
20171 {
20172 case VAR_DOMAIN:
20173 switch (aclass)
20174 {
20175 case LOC_BLOCK:
20176 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20177 case LOC_TYPEDEF:
20178 return GDB_INDEX_SYMBOL_KIND_TYPE;
20179 case LOC_COMPUTED:
20180 case LOC_CONST_BYTES:
20181 case LOC_OPTIMIZED_OUT:
20182 case LOC_STATIC:
20183 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20184 case LOC_CONST:
20185 /* Note: It's currently impossible to recognize psyms as enum values
20186 short of reading the type info. For now punt. */
20187 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20188 default:
20189 /* There are other LOC_FOO values that one might want to classify
20190 as variables, but dwarf2read.c doesn't currently use them. */
20191 return GDB_INDEX_SYMBOL_KIND_OTHER;
20192 }
20193 case STRUCT_DOMAIN:
20194 return GDB_INDEX_SYMBOL_KIND_TYPE;
20195 default:
20196 return GDB_INDEX_SYMBOL_KIND_OTHER;
20197 }
20198 }
20199
20200 /* Add a list of partial symbols to SYMTAB. */
20201
20202 static void
20203 write_psymbols (struct mapped_symtab *symtab,
20204 htab_t psyms_seen,
20205 struct partial_symbol **psymp,
20206 int count,
20207 offset_type cu_index,
20208 int is_static)
20209 {
20210 for (; count-- > 0; ++psymp)
20211 {
20212 struct partial_symbol *psym = *psymp;
20213 void **slot;
20214
20215 if (SYMBOL_LANGUAGE (psym) == language_ada)
20216 error (_("Ada is not currently supported by the index"));
20217
20218 /* Only add a given psymbol once. */
20219 slot = htab_find_slot (psyms_seen, psym, INSERT);
20220 if (!*slot)
20221 {
20222 gdb_index_symbol_kind kind = symbol_kind (psym);
20223
20224 *slot = psym;
20225 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20226 is_static, kind, cu_index);
20227 }
20228 }
20229 }
20230
20231 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20232 exception if there is an error. */
20233
20234 static void
20235 write_obstack (FILE *file, struct obstack *obstack)
20236 {
20237 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20238 file)
20239 != obstack_object_size (obstack))
20240 error (_("couldn't data write to file"));
20241 }
20242
20243 /* Unlink a file if the argument is not NULL. */
20244
20245 static void
20246 unlink_if_set (void *p)
20247 {
20248 char **filename = p;
20249 if (*filename)
20250 unlink (*filename);
20251 }
20252
20253 /* A helper struct used when iterating over debug_types. */
20254 struct signatured_type_index_data
20255 {
20256 struct objfile *objfile;
20257 struct mapped_symtab *symtab;
20258 struct obstack *types_list;
20259 htab_t psyms_seen;
20260 int cu_index;
20261 };
20262
20263 /* A helper function that writes a single signatured_type to an
20264 obstack. */
20265
20266 static int
20267 write_one_signatured_type (void **slot, void *d)
20268 {
20269 struct signatured_type_index_data *info = d;
20270 struct signatured_type *entry = (struct signatured_type *) *slot;
20271 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20272 struct partial_symtab *psymtab = per_cu->v.psymtab;
20273 gdb_byte val[8];
20274
20275 write_psymbols (info->symtab,
20276 info->psyms_seen,
20277 info->objfile->global_psymbols.list
20278 + psymtab->globals_offset,
20279 psymtab->n_global_syms, info->cu_index,
20280 0);
20281 write_psymbols (info->symtab,
20282 info->psyms_seen,
20283 info->objfile->static_psymbols.list
20284 + psymtab->statics_offset,
20285 psymtab->n_static_syms, info->cu_index,
20286 1);
20287
20288 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20289 entry->per_cu.offset.sect_off);
20290 obstack_grow (info->types_list, val, 8);
20291 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20292 entry->type_offset_in_tu.cu_off);
20293 obstack_grow (info->types_list, val, 8);
20294 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20295 obstack_grow (info->types_list, val, 8);
20296
20297 ++info->cu_index;
20298
20299 return 1;
20300 }
20301
20302 /* Recurse into all "included" dependencies and write their symbols as
20303 if they appeared in this psymtab. */
20304
20305 static void
20306 recursively_write_psymbols (struct objfile *objfile,
20307 struct partial_symtab *psymtab,
20308 struct mapped_symtab *symtab,
20309 htab_t psyms_seen,
20310 offset_type cu_index)
20311 {
20312 int i;
20313
20314 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20315 if (psymtab->dependencies[i]->user != NULL)
20316 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20317 symtab, psyms_seen, cu_index);
20318
20319 write_psymbols (symtab,
20320 psyms_seen,
20321 objfile->global_psymbols.list + psymtab->globals_offset,
20322 psymtab->n_global_syms, cu_index,
20323 0);
20324 write_psymbols (symtab,
20325 psyms_seen,
20326 objfile->static_psymbols.list + psymtab->statics_offset,
20327 psymtab->n_static_syms, cu_index,
20328 1);
20329 }
20330
20331 /* Create an index file for OBJFILE in the directory DIR. */
20332
20333 static void
20334 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20335 {
20336 struct cleanup *cleanup;
20337 char *filename, *cleanup_filename;
20338 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20339 struct obstack cu_list, types_cu_list;
20340 int i;
20341 FILE *out_file;
20342 struct mapped_symtab *symtab;
20343 offset_type val, size_of_contents, total_len;
20344 struct stat st;
20345 htab_t psyms_seen;
20346 htab_t cu_index_htab;
20347 struct psymtab_cu_index_map *psymtab_cu_index_map;
20348
20349 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20350 return;
20351
20352 if (dwarf2_per_objfile->using_index)
20353 error (_("Cannot use an index to create the index"));
20354
20355 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20356 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20357
20358 if (stat (objfile->name, &st) < 0)
20359 perror_with_name (objfile->name);
20360
20361 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20362 INDEX_SUFFIX, (char *) NULL);
20363 cleanup = make_cleanup (xfree, filename);
20364
20365 out_file = fopen (filename, "wb");
20366 if (!out_file)
20367 error (_("Can't open `%s' for writing"), filename);
20368
20369 cleanup_filename = filename;
20370 make_cleanup (unlink_if_set, &cleanup_filename);
20371
20372 symtab = create_mapped_symtab ();
20373 make_cleanup (cleanup_mapped_symtab, symtab);
20374
20375 obstack_init (&addr_obstack);
20376 make_cleanup_obstack_free (&addr_obstack);
20377
20378 obstack_init (&cu_list);
20379 make_cleanup_obstack_free (&cu_list);
20380
20381 obstack_init (&types_cu_list);
20382 make_cleanup_obstack_free (&types_cu_list);
20383
20384 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20385 NULL, xcalloc, xfree);
20386 make_cleanup_htab_delete (psyms_seen);
20387
20388 /* While we're scanning CU's create a table that maps a psymtab pointer
20389 (which is what addrmap records) to its index (which is what is recorded
20390 in the index file). This will later be needed to write the address
20391 table. */
20392 cu_index_htab = htab_create_alloc (100,
20393 hash_psymtab_cu_index,
20394 eq_psymtab_cu_index,
20395 NULL, xcalloc, xfree);
20396 make_cleanup_htab_delete (cu_index_htab);
20397 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20398 xmalloc (sizeof (struct psymtab_cu_index_map)
20399 * dwarf2_per_objfile->n_comp_units);
20400 make_cleanup (xfree, psymtab_cu_index_map);
20401
20402 /* The CU list is already sorted, so we don't need to do additional
20403 work here. Also, the debug_types entries do not appear in
20404 all_comp_units, but only in their own hash table. */
20405 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20406 {
20407 struct dwarf2_per_cu_data *per_cu
20408 = dwarf2_per_objfile->all_comp_units[i];
20409 struct partial_symtab *psymtab = per_cu->v.psymtab;
20410 gdb_byte val[8];
20411 struct psymtab_cu_index_map *map;
20412 void **slot;
20413
20414 if (psymtab->user == NULL)
20415 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20416
20417 map = &psymtab_cu_index_map[i];
20418 map->psymtab = psymtab;
20419 map->cu_index = i;
20420 slot = htab_find_slot (cu_index_htab, map, INSERT);
20421 gdb_assert (slot != NULL);
20422 gdb_assert (*slot == NULL);
20423 *slot = map;
20424
20425 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20426 per_cu->offset.sect_off);
20427 obstack_grow (&cu_list, val, 8);
20428 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20429 obstack_grow (&cu_list, val, 8);
20430 }
20431
20432 /* Dump the address map. */
20433 write_address_map (objfile, &addr_obstack, cu_index_htab);
20434
20435 /* Write out the .debug_type entries, if any. */
20436 if (dwarf2_per_objfile->signatured_types)
20437 {
20438 struct signatured_type_index_data sig_data;
20439
20440 sig_data.objfile = objfile;
20441 sig_data.symtab = symtab;
20442 sig_data.types_list = &types_cu_list;
20443 sig_data.psyms_seen = psyms_seen;
20444 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20445 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20446 write_one_signatured_type, &sig_data);
20447 }
20448
20449 /* Now that we've processed all symbols we can shrink their cu_indices
20450 lists. */
20451 uniquify_cu_indices (symtab);
20452
20453 obstack_init (&constant_pool);
20454 make_cleanup_obstack_free (&constant_pool);
20455 obstack_init (&symtab_obstack);
20456 make_cleanup_obstack_free (&symtab_obstack);
20457 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20458
20459 obstack_init (&contents);
20460 make_cleanup_obstack_free (&contents);
20461 size_of_contents = 6 * sizeof (offset_type);
20462 total_len = size_of_contents;
20463
20464 /* The version number. */
20465 val = MAYBE_SWAP (7);
20466 obstack_grow (&contents, &val, sizeof (val));
20467
20468 /* The offset of the CU list from the start of the file. */
20469 val = MAYBE_SWAP (total_len);
20470 obstack_grow (&contents, &val, sizeof (val));
20471 total_len += obstack_object_size (&cu_list);
20472
20473 /* The offset of the types CU list from the start of the file. */
20474 val = MAYBE_SWAP (total_len);
20475 obstack_grow (&contents, &val, sizeof (val));
20476 total_len += obstack_object_size (&types_cu_list);
20477
20478 /* The offset of the address table from the start of the file. */
20479 val = MAYBE_SWAP (total_len);
20480 obstack_grow (&contents, &val, sizeof (val));
20481 total_len += obstack_object_size (&addr_obstack);
20482
20483 /* The offset of the symbol table from the start of the file. */
20484 val = MAYBE_SWAP (total_len);
20485 obstack_grow (&contents, &val, sizeof (val));
20486 total_len += obstack_object_size (&symtab_obstack);
20487
20488 /* The offset of the constant pool from the start of the file. */
20489 val = MAYBE_SWAP (total_len);
20490 obstack_grow (&contents, &val, sizeof (val));
20491 total_len += obstack_object_size (&constant_pool);
20492
20493 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20494
20495 write_obstack (out_file, &contents);
20496 write_obstack (out_file, &cu_list);
20497 write_obstack (out_file, &types_cu_list);
20498 write_obstack (out_file, &addr_obstack);
20499 write_obstack (out_file, &symtab_obstack);
20500 write_obstack (out_file, &constant_pool);
20501
20502 fclose (out_file);
20503
20504 /* We want to keep the file, so we set cleanup_filename to NULL
20505 here. See unlink_if_set. */
20506 cleanup_filename = NULL;
20507
20508 do_cleanups (cleanup);
20509 }
20510
20511 /* Implementation of the `save gdb-index' command.
20512
20513 Note that the file format used by this command is documented in the
20514 GDB manual. Any changes here must be documented there. */
20515
20516 static void
20517 save_gdb_index_command (char *arg, int from_tty)
20518 {
20519 struct objfile *objfile;
20520
20521 if (!arg || !*arg)
20522 error (_("usage: save gdb-index DIRECTORY"));
20523
20524 ALL_OBJFILES (objfile)
20525 {
20526 struct stat st;
20527
20528 /* If the objfile does not correspond to an actual file, skip it. */
20529 if (stat (objfile->name, &st) < 0)
20530 continue;
20531
20532 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20533 if (dwarf2_per_objfile)
20534 {
20535 volatile struct gdb_exception except;
20536
20537 TRY_CATCH (except, RETURN_MASK_ERROR)
20538 {
20539 write_psymtabs_to_index (objfile, arg);
20540 }
20541 if (except.reason < 0)
20542 exception_fprintf (gdb_stderr, except,
20543 _("Error while writing index for `%s': "),
20544 objfile->name);
20545 }
20546 }
20547 }
20548
20549 \f
20550
20551 int dwarf2_always_disassemble;
20552
20553 static void
20554 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20555 struct cmd_list_element *c, const char *value)
20556 {
20557 fprintf_filtered (file,
20558 _("Whether to always disassemble "
20559 "DWARF expressions is %s.\n"),
20560 value);
20561 }
20562
20563 static void
20564 show_check_physname (struct ui_file *file, int from_tty,
20565 struct cmd_list_element *c, const char *value)
20566 {
20567 fprintf_filtered (file,
20568 _("Whether to check \"physname\" is %s.\n"),
20569 value);
20570 }
20571
20572 void _initialize_dwarf2_read (void);
20573
20574 void
20575 _initialize_dwarf2_read (void)
20576 {
20577 struct cmd_list_element *c;
20578
20579 dwarf2_objfile_data_key
20580 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20581
20582 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20583 Set DWARF 2 specific variables.\n\
20584 Configure DWARF 2 variables such as the cache size"),
20585 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20586 0/*allow-unknown*/, &maintenance_set_cmdlist);
20587
20588 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20589 Show DWARF 2 specific variables\n\
20590 Show DWARF 2 variables such as the cache size"),
20591 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20592 0/*allow-unknown*/, &maintenance_show_cmdlist);
20593
20594 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20595 &dwarf2_max_cache_age, _("\
20596 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20597 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20598 A higher limit means that cached compilation units will be stored\n\
20599 in memory longer, and more total memory will be used. Zero disables\n\
20600 caching, which can slow down startup."),
20601 NULL,
20602 show_dwarf2_max_cache_age,
20603 &set_dwarf2_cmdlist,
20604 &show_dwarf2_cmdlist);
20605
20606 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20607 &dwarf2_always_disassemble, _("\
20608 Set whether `info address' always disassembles DWARF expressions."), _("\
20609 Show whether `info address' always disassembles DWARF expressions."), _("\
20610 When enabled, DWARF expressions are always printed in an assembly-like\n\
20611 syntax. When disabled, expressions will be printed in a more\n\
20612 conversational style, when possible."),
20613 NULL,
20614 show_dwarf2_always_disassemble,
20615 &set_dwarf2_cmdlist,
20616 &show_dwarf2_cmdlist);
20617
20618 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20619 Set debugging of the dwarf2 reader."), _("\
20620 Show debugging of the dwarf2 reader."), _("\
20621 When enabled, debugging messages are printed during dwarf2 reading\n\
20622 and symtab expansion."),
20623 NULL,
20624 NULL,
20625 &setdebuglist, &showdebuglist);
20626
20627 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20628 Set debugging of the dwarf2 DIE reader."), _("\
20629 Show debugging of the dwarf2 DIE reader."), _("\
20630 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20631 The value is the maximum depth to print."),
20632 NULL,
20633 NULL,
20634 &setdebuglist, &showdebuglist);
20635
20636 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20637 Set cross-checking of \"physname\" code against demangler."), _("\
20638 Show cross-checking of \"physname\" code against demangler."), _("\
20639 When enabled, GDB's internal \"physname\" code is checked against\n\
20640 the demangler."),
20641 NULL, show_check_physname,
20642 &setdebuglist, &showdebuglist);
20643
20644 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20645 no_class, &use_deprecated_index_sections, _("\
20646 Set whether to use deprecated gdb_index sections."), _("\
20647 Show whether to use deprecated gdb_index sections."), _("\
20648 When enabled, deprecated .gdb_index sections are used anyway.\n\
20649 Normally they are ignored either because of a missing feature or\n\
20650 performance issue.\n\
20651 Warning: This option must be enabled before gdb reads the file."),
20652 NULL,
20653 NULL,
20654 &setlist, &showlist);
20655
20656 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20657 _("\
20658 Save a gdb-index file.\n\
20659 Usage: save gdb-index DIRECTORY"),
20660 &save_cmdlist);
20661 set_cmd_completer (c, filename_completer);
20662 }