* symfile.h (quick_symbol_functions): Delete member
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70
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 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 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 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 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_psymtab_to_symtab (struct objfile *,
1288 struct partial_symtab *);
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 char *, 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 char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1531 struct obstack *);
1532
1533 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1534
1535 static const char *dwarf2_full_name (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 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 char **name, 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 (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 /* Reader function for dw2_build_type_unit_groups. */
2818
2819 static void
2820 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2821 gdb_byte *info_ptr,
2822 struct die_info *type_unit_die,
2823 int has_children,
2824 void *data)
2825 {
2826 struct dwarf2_cu *cu = reader->cu;
2827 struct attribute *attr;
2828 struct type_unit_group *tu_group;
2829
2830 gdb_assert (data == NULL);
2831
2832 if (! has_children)
2833 return;
2834
2835 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2836 /* Call this for its side-effect of creating the associated
2837 struct type_unit_group if it doesn't already exist. */
2838 tu_group = get_type_unit_group (cu, attr);
2839 }
2840
2841 /* Build dwarf2_per_objfile->type_unit_groups.
2842 This function may be called multiple times. */
2843
2844 static void
2845 dw2_build_type_unit_groups (void)
2846 {
2847 if (dwarf2_per_objfile->type_unit_groups == NULL)
2848 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2849 }
2850
2851 /* die_reader_func for dw2_get_file_names. */
2852
2853 static void
2854 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2855 gdb_byte *info_ptr,
2856 struct die_info *comp_unit_die,
2857 int has_children,
2858 void *data)
2859 {
2860 struct dwarf2_cu *cu = reader->cu;
2861 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2862 struct objfile *objfile = dwarf2_per_objfile->objfile;
2863 struct dwarf2_per_cu_data *lh_cu;
2864 struct line_header *lh;
2865 struct attribute *attr;
2866 int i;
2867 char *name, *comp_dir;
2868 void **slot;
2869 struct quick_file_names *qfn;
2870 unsigned int line_offset;
2871
2872 /* Our callers never want to match partial units -- instead they
2873 will match the enclosing full CU. */
2874 if (comp_unit_die->tag == DW_TAG_partial_unit)
2875 {
2876 this_cu->v.quick->no_file_data = 1;
2877 return;
2878 }
2879
2880 /* If we're reading the line header for TUs, store it in the "per_cu"
2881 for tu_group. */
2882 if (this_cu->is_debug_types)
2883 {
2884 struct type_unit_group *tu_group = data;
2885
2886 gdb_assert (tu_group != NULL);
2887 lh_cu = &tu_group->per_cu;
2888 }
2889 else
2890 lh_cu = this_cu;
2891
2892 lh = NULL;
2893 slot = NULL;
2894 line_offset = 0;
2895
2896 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2897 if (attr)
2898 {
2899 struct quick_file_names find_entry;
2900
2901 line_offset = DW_UNSND (attr);
2902
2903 /* We may have already read in this line header (TU line header sharing).
2904 If we have we're done. */
2905 find_entry.hash.dwo_unit = cu->dwo_unit;
2906 find_entry.hash.line_offset.sect_off = line_offset;
2907 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2908 &find_entry, INSERT);
2909 if (*slot != NULL)
2910 {
2911 lh_cu->v.quick->file_names = *slot;
2912 return;
2913 }
2914
2915 lh = dwarf_decode_line_header (line_offset, cu);
2916 }
2917 if (lh == NULL)
2918 {
2919 lh_cu->v.quick->no_file_data = 1;
2920 return;
2921 }
2922
2923 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2924 qfn->hash.dwo_unit = cu->dwo_unit;
2925 qfn->hash.line_offset.sect_off = line_offset;
2926 gdb_assert (slot != NULL);
2927 *slot = qfn;
2928
2929 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2930
2931 qfn->num_file_names = lh->num_file_names;
2932 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2933 lh->num_file_names * sizeof (char *));
2934 for (i = 0; i < lh->num_file_names; ++i)
2935 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2936 qfn->real_names = NULL;
2937
2938 free_line_header (lh);
2939
2940 lh_cu->v.quick->file_names = qfn;
2941 }
2942
2943 /* A helper for the "quick" functions which attempts to read the line
2944 table for THIS_CU. */
2945
2946 static struct quick_file_names *
2947 dw2_get_file_names (struct objfile *objfile,
2948 struct dwarf2_per_cu_data *this_cu)
2949 {
2950 /* For TUs this should only be called on the parent group. */
2951 if (this_cu->is_debug_types)
2952 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2953
2954 if (this_cu->v.quick->file_names != NULL)
2955 return this_cu->v.quick->file_names;
2956 /* If we know there is no line data, no point in looking again. */
2957 if (this_cu->v.quick->no_file_data)
2958 return NULL;
2959
2960 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2961 in the stub for CUs, there's is no need to lookup the DWO file.
2962 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2963 DWO file. */
2964 if (this_cu->is_debug_types)
2965 {
2966 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2967
2968 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2969 dw2_get_file_names_reader, tu_group);
2970 }
2971 else
2972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2973
2974 if (this_cu->v.quick->no_file_data)
2975 return NULL;
2976 return this_cu->v.quick->file_names;
2977 }
2978
2979 /* A helper for the "quick" functions which computes and caches the
2980 real path for a given file name from the line table. */
2981
2982 static const char *
2983 dw2_get_real_path (struct objfile *objfile,
2984 struct quick_file_names *qfn, int index)
2985 {
2986 if (qfn->real_names == NULL)
2987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2988 qfn->num_file_names, sizeof (char *));
2989
2990 if (qfn->real_names[index] == NULL)
2991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2992
2993 return qfn->real_names[index];
2994 }
2995
2996 static struct symtab *
2997 dw2_find_last_source_symtab (struct objfile *objfile)
2998 {
2999 int index;
3000
3001 dw2_setup (objfile);
3002 index = dwarf2_per_objfile->n_comp_units - 1;
3003 return dw2_instantiate_symtab (dw2_get_cu (index));
3004 }
3005
3006 /* Traversal function for dw2_forget_cached_source_info. */
3007
3008 static int
3009 dw2_free_cached_file_names (void **slot, void *info)
3010 {
3011 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3012
3013 if (file_data->real_names)
3014 {
3015 int i;
3016
3017 for (i = 0; i < file_data->num_file_names; ++i)
3018 {
3019 xfree ((void*) file_data->real_names[i]);
3020 file_data->real_names[i] = NULL;
3021 }
3022 }
3023
3024 return 1;
3025 }
3026
3027 static void
3028 dw2_forget_cached_source_info (struct objfile *objfile)
3029 {
3030 dw2_setup (objfile);
3031
3032 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3033 dw2_free_cached_file_names, NULL);
3034 }
3035
3036 /* Helper function for dw2_map_symtabs_matching_filename that expands
3037 the symtabs and calls the iterator. */
3038
3039 static int
3040 dw2_map_expand_apply (struct objfile *objfile,
3041 struct dwarf2_per_cu_data *per_cu,
3042 const char *name,
3043 const char *full_path, const char *real_path,
3044 int (*callback) (struct symtab *, void *),
3045 void *data)
3046 {
3047 struct symtab *last_made = objfile->symtabs;
3048
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3051 return 0;
3052
3053 /* This may expand more than one symtab, and we want to iterate over
3054 all of them. */
3055 dw2_instantiate_symtab (per_cu);
3056
3057 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3058 objfile->symtabs, last_made);
3059 }
3060
3061 /* Implementation of the map_symtabs_matching_filename method. */
3062
3063 static int
3064 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065 const char *full_path, const char *real_path,
3066 int (*callback) (struct symtab *, void *),
3067 void *data)
3068 {
3069 int i;
3070 const char *name_basename = lbasename (name);
3071 int is_abs = IS_ABSOLUTE_PATH (name);
3072
3073 dw2_setup (objfile);
3074
3075 dw2_build_type_unit_groups ();
3076
3077 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3078 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3079 {
3080 int j;
3081 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3082 struct quick_file_names *file_data;
3083
3084 /* We only need to look at symtabs not already expanded. */
3085 if (per_cu->v.quick->symtab)
3086 continue;
3087
3088 file_data = dw2_get_file_names (objfile, per_cu);
3089 if (file_data == NULL)
3090 continue;
3091
3092 for (j = 0; j < file_data->num_file_names; ++j)
3093 {
3094 const char *this_name = file_data->file_names[j];
3095
3096 if (FILENAME_CMP (name, this_name) == 0
3097 || (!is_abs && compare_filenames_for_search (this_name, name)))
3098 {
3099 if (dw2_map_expand_apply (objfile, per_cu,
3100 name, full_path, real_path,
3101 callback, data))
3102 return 1;
3103 }
3104
3105 /* Before we invoke realpath, which can get expensive when many
3106 files are involved, do a quick comparison of the basenames. */
3107 if (! basenames_may_differ
3108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3109 continue;
3110
3111 if (full_path != NULL)
3112 {
3113 const char *this_real_name = dw2_get_real_path (objfile,
3114 file_data, j);
3115
3116 if (this_real_name != NULL
3117 && (FILENAME_CMP (full_path, this_real_name) == 0
3118 || (!is_abs
3119 && compare_filenames_for_search (this_real_name,
3120 name))))
3121 {
3122 if (dw2_map_expand_apply (objfile, per_cu,
3123 name, full_path, real_path,
3124 callback, data))
3125 return 1;
3126 }
3127 }
3128
3129 if (real_path != NULL)
3130 {
3131 const char *this_real_name = dw2_get_real_path (objfile,
3132 file_data, j);
3133
3134 if (this_real_name != NULL
3135 && (FILENAME_CMP (real_path, this_real_name) == 0
3136 || (!is_abs
3137 && compare_filenames_for_search (this_real_name,
3138 name))))
3139 {
3140 if (dw2_map_expand_apply (objfile, per_cu,
3141 name, full_path, real_path,
3142 callback, data))
3143 return 1;
3144 }
3145 }
3146 }
3147 }
3148
3149 return 0;
3150 }
3151
3152 /* Struct used to manage iterating over all CUs looking for a symbol. */
3153
3154 struct dw2_symtab_iterator
3155 {
3156 /* The internalized form of .gdb_index. */
3157 struct mapped_index *index;
3158 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3159 int want_specific_block;
3160 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3161 Unused if !WANT_SPECIFIC_BLOCK. */
3162 int block_index;
3163 /* The kind of symbol we're looking for. */
3164 domain_enum domain;
3165 /* The list of CUs from the index entry of the symbol,
3166 or NULL if not found. */
3167 offset_type *vec;
3168 /* The next element in VEC to look at. */
3169 int next;
3170 /* The number of elements in VEC, or zero if there is no match. */
3171 int length;
3172 };
3173
3174 /* Initialize the index symtab iterator ITER.
3175 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3176 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3177
3178 static void
3179 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3180 struct mapped_index *index,
3181 int want_specific_block,
3182 int block_index,
3183 domain_enum domain,
3184 const char *name)
3185 {
3186 iter->index = index;
3187 iter->want_specific_block = want_specific_block;
3188 iter->block_index = block_index;
3189 iter->domain = domain;
3190 iter->next = 0;
3191
3192 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3193 iter->length = MAYBE_SWAP (*iter->vec);
3194 else
3195 {
3196 iter->vec = NULL;
3197 iter->length = 0;
3198 }
3199 }
3200
3201 /* Return the next matching CU or NULL if there are no more. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205 {
3206 for ( ; iter->next < iter->length; ++iter->next)
3207 {
3208 offset_type cu_index_and_attrs =
3209 MAYBE_SWAP (iter->vec[iter->next + 1]);
3210 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3211 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3212 int want_static = iter->block_index != GLOBAL_BLOCK;
3213 /* This value is only valid for index versions >= 7. */
3214 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3215 gdb_index_symbol_kind symbol_kind =
3216 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3217 /* Only check the symbol attributes if they're present.
3218 Indices prior to version 7 don't record them,
3219 and indices >= 7 may elide them for certain symbols
3220 (gold does this). */
3221 int attrs_valid =
3222 (iter->index->version >= 7
3223 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3224
3225 /* Skip if already read in. */
3226 if (per_cu->v.quick->symtab)
3227 continue;
3228
3229 if (attrs_valid
3230 && iter->want_specific_block
3231 && want_static != is_static)
3232 continue;
3233
3234 /* Only check the symbol's kind if it has one. */
3235 if (attrs_valid)
3236 {
3237 switch (iter->domain)
3238 {
3239 case VAR_DOMAIN:
3240 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3241 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3242 /* Some types are also in VAR_DOMAIN. */
3243 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3244 continue;
3245 break;
3246 case STRUCT_DOMAIN:
3247 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3248 continue;
3249 break;
3250 case LABEL_DOMAIN:
3251 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3252 continue;
3253 break;
3254 default:
3255 break;
3256 }
3257 }
3258
3259 ++iter->next;
3260 return per_cu;
3261 }
3262
3263 return NULL;
3264 }
3265
3266 static struct symtab *
3267 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3268 const char *name, domain_enum domain)
3269 {
3270 struct symtab *stab_best = NULL;
3271 struct mapped_index *index;
3272
3273 dw2_setup (objfile);
3274
3275 index = dwarf2_per_objfile->index_table;
3276
3277 /* index is NULL if OBJF_READNOW. */
3278 if (index)
3279 {
3280 struct dw2_symtab_iterator iter;
3281 struct dwarf2_per_cu_data *per_cu;
3282
3283 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3284
3285 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3286 {
3287 struct symbol *sym = NULL;
3288 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3289
3290 /* Some caution must be observed with overloaded functions
3291 and methods, since the index will not contain any overload
3292 information (but NAME might contain it). */
3293 if (stab->primary)
3294 {
3295 struct blockvector *bv = BLOCKVECTOR (stab);
3296 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3297
3298 sym = lookup_block_symbol (block, name, domain);
3299 }
3300
3301 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3302 {
3303 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3304 return stab;
3305
3306 stab_best = stab;
3307 }
3308
3309 /* Keep looking through other CUs. */
3310 }
3311 }
3312
3313 return stab_best;
3314 }
3315
3316 static void
3317 dw2_print_stats (struct objfile *objfile)
3318 {
3319 int i, count;
3320
3321 dw2_setup (objfile);
3322 count = 0;
3323 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3324 + dwarf2_per_objfile->n_type_units); ++i)
3325 {
3326 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3327
3328 if (!per_cu->v.quick->symtab)
3329 ++count;
3330 }
3331 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3332 }
3333
3334 static void
3335 dw2_dump (struct objfile *objfile)
3336 {
3337 /* Nothing worth printing. */
3338 }
3339
3340 static void
3341 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3342 struct section_offsets *delta)
3343 {
3344 /* There's nothing to relocate here. */
3345 }
3346
3347 static void
3348 dw2_expand_symtabs_for_function (struct objfile *objfile,
3349 const char *func_name)
3350 {
3351 struct mapped_index *index;
3352
3353 dw2_setup (objfile);
3354
3355 index = dwarf2_per_objfile->index_table;
3356
3357 /* index is NULL if OBJF_READNOW. */
3358 if (index)
3359 {
3360 struct dw2_symtab_iterator iter;
3361 struct dwarf2_per_cu_data *per_cu;
3362
3363 /* Note: It doesn't matter what we pass for block_index here. */
3364 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3365 func_name);
3366
3367 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3368 dw2_instantiate_symtab (per_cu);
3369 }
3370 }
3371
3372 static void
3373 dw2_expand_all_symtabs (struct objfile *objfile)
3374 {
3375 int i;
3376
3377 dw2_setup (objfile);
3378
3379 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3380 + dwarf2_per_objfile->n_type_units); ++i)
3381 {
3382 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3383
3384 dw2_instantiate_symtab (per_cu);
3385 }
3386 }
3387
3388 static void
3389 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3390 const char *filename)
3391 {
3392 int i;
3393
3394 dw2_setup (objfile);
3395
3396 /* We don't need to consider type units here.
3397 This is only called for examining code, e.g. expand_line_sal.
3398 There can be an order of magnitude (or more) more type units
3399 than comp units, and we avoid them if we can. */
3400
3401 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3402 {
3403 int j;
3404 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3405 struct quick_file_names *file_data;
3406
3407 /* We only need to look at symtabs not already expanded. */
3408 if (per_cu->v.quick->symtab)
3409 continue;
3410
3411 file_data = dw2_get_file_names (objfile, per_cu);
3412 if (file_data == NULL)
3413 continue;
3414
3415 for (j = 0; j < file_data->num_file_names; ++j)
3416 {
3417 const char *this_name = file_data->file_names[j];
3418 if (FILENAME_CMP (this_name, filename) == 0)
3419 {
3420 dw2_instantiate_symtab (per_cu);
3421 break;
3422 }
3423 }
3424 }
3425 }
3426
3427 /* A helper function for dw2_find_symbol_file that finds the primary
3428 file name for a given CU. This is a die_reader_func. */
3429
3430 static void
3431 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3432 gdb_byte *info_ptr,
3433 struct die_info *comp_unit_die,
3434 int has_children,
3435 void *data)
3436 {
3437 const char **result_ptr = data;
3438 struct dwarf2_cu *cu = reader->cu;
3439 struct attribute *attr;
3440
3441 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3442 if (attr == NULL)
3443 *result_ptr = NULL;
3444 else
3445 *result_ptr = DW_STRING (attr);
3446 }
3447
3448 static const char *
3449 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3450 {
3451 struct dwarf2_per_cu_data *per_cu;
3452 offset_type *vec;
3453 const char *filename;
3454
3455 dw2_setup (objfile);
3456
3457 /* index_table is NULL if OBJF_READNOW. */
3458 if (!dwarf2_per_objfile->index_table)
3459 {
3460 struct symtab *s;
3461
3462 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3463 {
3464 struct blockvector *bv = BLOCKVECTOR (s);
3465 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3466 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3467
3468 if (sym)
3469 return SYMBOL_SYMTAB (sym)->filename;
3470 }
3471 return NULL;
3472 }
3473
3474 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3475 name, &vec))
3476 return NULL;
3477
3478 /* Note that this just looks at the very first one named NAME -- but
3479 actually we are looking for a function. find_main_filename
3480 should be rewritten so that it doesn't require a custom hook. It
3481 could just use the ordinary symbol tables. */
3482 /* vec[0] is the length, which must always be >0. */
3483 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3484
3485 if (per_cu->v.quick->symtab != NULL)
3486 return per_cu->v.quick->symtab->filename;
3487
3488 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3489 dw2_get_primary_filename_reader, &filename);
3490
3491 return filename;
3492 }
3493
3494 static void
3495 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3496 struct objfile *objfile, int global,
3497 int (*callback) (struct block *,
3498 struct symbol *, void *),
3499 void *data, symbol_compare_ftype *match,
3500 symbol_compare_ftype *ordered_compare)
3501 {
3502 /* Currently unimplemented; used for Ada. The function can be called if the
3503 current language is Ada for a non-Ada objfile using GNU index. As Ada
3504 does not look for non-Ada symbols this function should just return. */
3505 }
3506
3507 static void
3508 dw2_expand_symtabs_matching
3509 (struct objfile *objfile,
3510 int (*file_matcher) (const char *, void *),
3511 int (*name_matcher) (const char *, void *),
3512 enum search_domain kind,
3513 void *data)
3514 {
3515 int i;
3516 offset_type iter;
3517 struct mapped_index *index;
3518
3519 dw2_setup (objfile);
3520
3521 /* index_table is NULL if OBJF_READNOW. */
3522 if (!dwarf2_per_objfile->index_table)
3523 return;
3524 index = dwarf2_per_objfile->index_table;
3525
3526 if (file_matcher != NULL)
3527 {
3528 struct cleanup *cleanup;
3529 htab_t visited_found, visited_not_found;
3530
3531 dw2_build_type_unit_groups ();
3532
3533 visited_found = htab_create_alloc (10,
3534 htab_hash_pointer, htab_eq_pointer,
3535 NULL, xcalloc, xfree);
3536 cleanup = make_cleanup_htab_delete (visited_found);
3537 visited_not_found = htab_create_alloc (10,
3538 htab_hash_pointer, htab_eq_pointer,
3539 NULL, xcalloc, xfree);
3540 make_cleanup_htab_delete (visited_not_found);
3541
3542 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3543 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3544 {
3545 int j;
3546 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3547 struct quick_file_names *file_data;
3548 void **slot;
3549
3550 per_cu->v.quick->mark = 0;
3551
3552 /* We only need to look at symtabs not already expanded. */
3553 if (per_cu->v.quick->symtab)
3554 continue;
3555
3556 file_data = dw2_get_file_names (objfile, per_cu);
3557 if (file_data == NULL)
3558 continue;
3559
3560 if (htab_find (visited_not_found, file_data) != NULL)
3561 continue;
3562 else if (htab_find (visited_found, file_data) != NULL)
3563 {
3564 per_cu->v.quick->mark = 1;
3565 continue;
3566 }
3567
3568 for (j = 0; j < file_data->num_file_names; ++j)
3569 {
3570 if (file_matcher (file_data->file_names[j], data))
3571 {
3572 per_cu->v.quick->mark = 1;
3573 break;
3574 }
3575 }
3576
3577 slot = htab_find_slot (per_cu->v.quick->mark
3578 ? visited_found
3579 : visited_not_found,
3580 file_data, INSERT);
3581 *slot = file_data;
3582 }
3583
3584 do_cleanups (cleanup);
3585 }
3586
3587 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3588 {
3589 offset_type idx = 2 * iter;
3590 const char *name;
3591 offset_type *vec, vec_len, vec_idx;
3592
3593 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3594 continue;
3595
3596 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3597
3598 if (! (*name_matcher) (name, data))
3599 continue;
3600
3601 /* The name was matched, now expand corresponding CUs that were
3602 marked. */
3603 vec = (offset_type *) (index->constant_pool
3604 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3605 vec_len = MAYBE_SWAP (vec[0]);
3606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3607 {
3608 struct dwarf2_per_cu_data *per_cu;
3609 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3610 gdb_index_symbol_kind symbol_kind =
3611 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3612 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3613
3614 /* Don't crash on bad data. */
3615 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3616 + dwarf2_per_objfile->n_type_units))
3617 continue;
3618
3619 /* Only check the symbol's kind if it has one.
3620 Indices prior to version 7 don't record it. */
3621 if (index->version >= 7)
3622 {
3623 switch (kind)
3624 {
3625 case VARIABLES_DOMAIN:
3626 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3627 continue;
3628 break;
3629 case FUNCTIONS_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3631 continue;
3632 break;
3633 case TYPES_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3635 continue;
3636 break;
3637 default:
3638 break;
3639 }
3640 }
3641
3642 per_cu = dw2_get_cu (cu_index);
3643 if (file_matcher == NULL || per_cu->v.quick->mark)
3644 dw2_instantiate_symtab (per_cu);
3645 }
3646 }
3647 }
3648
3649 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3650 symtab. */
3651
3652 static struct symtab *
3653 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3654 {
3655 int i;
3656
3657 if (BLOCKVECTOR (symtab) != NULL
3658 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3659 return symtab;
3660
3661 if (symtab->includes == NULL)
3662 return NULL;
3663
3664 for (i = 0; symtab->includes[i]; ++i)
3665 {
3666 struct symtab *s = symtab->includes[i];
3667
3668 s = recursively_find_pc_sect_symtab (s, pc);
3669 if (s != NULL)
3670 return s;
3671 }
3672
3673 return NULL;
3674 }
3675
3676 static struct symtab *
3677 dw2_find_pc_sect_symtab (struct objfile *objfile,
3678 struct minimal_symbol *msymbol,
3679 CORE_ADDR pc,
3680 struct obj_section *section,
3681 int warn_if_readin)
3682 {
3683 struct dwarf2_per_cu_data *data;
3684 struct symtab *result;
3685
3686 dw2_setup (objfile);
3687
3688 if (!objfile->psymtabs_addrmap)
3689 return NULL;
3690
3691 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3692 if (!data)
3693 return NULL;
3694
3695 if (warn_if_readin && data->v.quick->symtab)
3696 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3697 paddress (get_objfile_arch (objfile), pc));
3698
3699 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3700 gdb_assert (result != NULL);
3701 return result;
3702 }
3703
3704 static void
3705 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3706 void *data, int need_fullname)
3707 {
3708 int i;
3709 struct cleanup *cleanup;
3710 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3711 NULL, xcalloc, xfree);
3712
3713 cleanup = make_cleanup_htab_delete (visited);
3714 dw2_setup (objfile);
3715
3716 dw2_build_type_unit_groups ();
3717
3718 /* We can ignore file names coming from already-expanded CUs. */
3719 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3720 + dwarf2_per_objfile->n_type_units); ++i)
3721 {
3722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3723
3724 if (per_cu->v.quick->symtab)
3725 {
3726 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3727 INSERT);
3728
3729 *slot = per_cu->v.quick->file_names;
3730 }
3731 }
3732
3733 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3734 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3735 {
3736 int j;
3737 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3738 struct quick_file_names *file_data;
3739 void **slot;
3740
3741 /* We only need to look at symtabs not already expanded. */
3742 if (per_cu->v.quick->symtab)
3743 continue;
3744
3745 file_data = dw2_get_file_names (objfile, per_cu);
3746 if (file_data == NULL)
3747 continue;
3748
3749 slot = htab_find_slot (visited, file_data, INSERT);
3750 if (*slot)
3751 {
3752 /* Already visited. */
3753 continue;
3754 }
3755 *slot = file_data;
3756
3757 for (j = 0; j < file_data->num_file_names; ++j)
3758 {
3759 const char *this_real_name;
3760
3761 if (need_fullname)
3762 this_real_name = dw2_get_real_path (objfile, file_data, j);
3763 else
3764 this_real_name = NULL;
3765 (*fun) (file_data->file_names[j], this_real_name, data);
3766 }
3767 }
3768
3769 do_cleanups (cleanup);
3770 }
3771
3772 static int
3773 dw2_has_symbols (struct objfile *objfile)
3774 {
3775 return 1;
3776 }
3777
3778 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3779 {
3780 dw2_has_symbols,
3781 dw2_find_last_source_symtab,
3782 dw2_forget_cached_source_info,
3783 dw2_map_symtabs_matching_filename,
3784 dw2_lookup_symbol,
3785 dw2_print_stats,
3786 dw2_dump,
3787 dw2_relocate,
3788 dw2_expand_symtabs_for_function,
3789 dw2_expand_all_symtabs,
3790 dw2_expand_symtabs_with_filename,
3791 dw2_find_symbol_file,
3792 dw2_map_matching_symbols,
3793 dw2_expand_symtabs_matching,
3794 dw2_find_pc_sect_symtab,
3795 dw2_map_symbol_filenames
3796 };
3797
3798 /* Initialize for reading DWARF for this objfile. Return 0 if this
3799 file will use psymtabs, or 1 if using the GNU index. */
3800
3801 int
3802 dwarf2_initialize_objfile (struct objfile *objfile)
3803 {
3804 /* If we're about to read full symbols, don't bother with the
3805 indices. In this case we also don't care if some other debug
3806 format is making psymtabs, because they are all about to be
3807 expanded anyway. */
3808 if ((objfile->flags & OBJF_READNOW))
3809 {
3810 int i;
3811
3812 dwarf2_per_objfile->using_index = 1;
3813 create_all_comp_units (objfile);
3814 create_all_type_units (objfile);
3815 dwarf2_per_objfile->quick_file_names_table =
3816 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3817
3818 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3819 + dwarf2_per_objfile->n_type_units); ++i)
3820 {
3821 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3822
3823 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3824 struct dwarf2_per_cu_quick_data);
3825 }
3826
3827 /* Return 1 so that gdb sees the "quick" functions. However,
3828 these functions will be no-ops because we will have expanded
3829 all symtabs. */
3830 return 1;
3831 }
3832
3833 if (dwarf2_read_index (objfile))
3834 return 1;
3835
3836 return 0;
3837 }
3838
3839 \f
3840
3841 /* Build a partial symbol table. */
3842
3843 void
3844 dwarf2_build_psymtabs (struct objfile *objfile)
3845 {
3846 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3847 {
3848 init_psymbol_list (objfile, 1024);
3849 }
3850
3851 dwarf2_build_psymtabs_hard (objfile);
3852 }
3853
3854 /* Return the total length of the CU described by HEADER. */
3855
3856 static unsigned int
3857 get_cu_length (const struct comp_unit_head *header)
3858 {
3859 return header->initial_length_size + header->length;
3860 }
3861
3862 /* Return TRUE if OFFSET is within CU_HEADER. */
3863
3864 static inline int
3865 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3866 {
3867 sect_offset bottom = { cu_header->offset.sect_off };
3868 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3869
3870 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3871 }
3872
3873 /* Find the base address of the compilation unit for range lists and
3874 location lists. It will normally be specified by DW_AT_low_pc.
3875 In DWARF-3 draft 4, the base address could be overridden by
3876 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3877 compilation units with discontinuous ranges. */
3878
3879 static void
3880 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3881 {
3882 struct attribute *attr;
3883
3884 cu->base_known = 0;
3885 cu->base_address = 0;
3886
3887 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3888 if (attr)
3889 {
3890 cu->base_address = DW_ADDR (attr);
3891 cu->base_known = 1;
3892 }
3893 else
3894 {
3895 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3896 if (attr)
3897 {
3898 cu->base_address = DW_ADDR (attr);
3899 cu->base_known = 1;
3900 }
3901 }
3902 }
3903
3904 /* Read in the comp unit header information from the debug_info at info_ptr.
3905 NOTE: This leaves members offset, first_die_offset to be filled in
3906 by the caller. */
3907
3908 static gdb_byte *
3909 read_comp_unit_head (struct comp_unit_head *cu_header,
3910 gdb_byte *info_ptr, bfd *abfd)
3911 {
3912 int signed_addr;
3913 unsigned int bytes_read;
3914
3915 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3916 cu_header->initial_length_size = bytes_read;
3917 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3918 info_ptr += bytes_read;
3919 cu_header->version = read_2_bytes (abfd, info_ptr);
3920 info_ptr += 2;
3921 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3922 &bytes_read);
3923 info_ptr += bytes_read;
3924 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3925 info_ptr += 1;
3926 signed_addr = bfd_get_sign_extend_vma (abfd);
3927 if (signed_addr < 0)
3928 internal_error (__FILE__, __LINE__,
3929 _("read_comp_unit_head: dwarf from non elf file"));
3930 cu_header->signed_addr_p = signed_addr;
3931
3932 return info_ptr;
3933 }
3934
3935 /* Helper function that returns the proper abbrev section for
3936 THIS_CU. */
3937
3938 static struct dwarf2_section_info *
3939 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3940 {
3941 struct dwarf2_section_info *abbrev;
3942
3943 if (this_cu->is_dwz)
3944 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3945 else
3946 abbrev = &dwarf2_per_objfile->abbrev;
3947
3948 return abbrev;
3949 }
3950
3951 /* Subroutine of read_and_check_comp_unit_head and
3952 read_and_check_type_unit_head to simplify them.
3953 Perform various error checking on the header. */
3954
3955 static void
3956 error_check_comp_unit_head (struct comp_unit_head *header,
3957 struct dwarf2_section_info *section,
3958 struct dwarf2_section_info *abbrev_section)
3959 {
3960 bfd *abfd = section->asection->owner;
3961 const char *filename = bfd_get_filename (abfd);
3962
3963 if (header->version != 2 && header->version != 3 && header->version != 4)
3964 error (_("Dwarf Error: wrong version in compilation unit header "
3965 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3966 filename);
3967
3968 if (header->abbrev_offset.sect_off
3969 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3970 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3971 "(offset 0x%lx + 6) [in module %s]"),
3972 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3973 filename);
3974
3975 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3976 avoid potential 32-bit overflow. */
3977 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3978 > section->size)
3979 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3980 "(offset 0x%lx + 0) [in module %s]"),
3981 (long) header->length, (long) header->offset.sect_off,
3982 filename);
3983 }
3984
3985 /* Read in a CU/TU header and perform some basic error checking.
3986 The contents of the header are stored in HEADER.
3987 The result is a pointer to the start of the first DIE. */
3988
3989 static gdb_byte *
3990 read_and_check_comp_unit_head (struct comp_unit_head *header,
3991 struct dwarf2_section_info *section,
3992 struct dwarf2_section_info *abbrev_section,
3993 gdb_byte *info_ptr,
3994 int is_debug_types_section)
3995 {
3996 gdb_byte *beg_of_comp_unit = info_ptr;
3997 bfd *abfd = section->asection->owner;
3998
3999 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4000
4001 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4002
4003 /* If we're reading a type unit, skip over the signature and
4004 type_offset fields. */
4005 if (is_debug_types_section)
4006 info_ptr += 8 /*signature*/ + header->offset_size;
4007
4008 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4009
4010 error_check_comp_unit_head (header, section, abbrev_section);
4011
4012 return info_ptr;
4013 }
4014
4015 /* Read in the types comp unit header information from .debug_types entry at
4016 types_ptr. The result is a pointer to one past the end of the header. */
4017
4018 static gdb_byte *
4019 read_and_check_type_unit_head (struct comp_unit_head *header,
4020 struct dwarf2_section_info *section,
4021 struct dwarf2_section_info *abbrev_section,
4022 gdb_byte *info_ptr,
4023 ULONGEST *signature,
4024 cu_offset *type_offset_in_tu)
4025 {
4026 gdb_byte *beg_of_comp_unit = info_ptr;
4027 bfd *abfd = section->asection->owner;
4028
4029 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4030
4031 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4032
4033 /* If we're reading a type unit, skip over the signature and
4034 type_offset fields. */
4035 if (signature != NULL)
4036 *signature = read_8_bytes (abfd, info_ptr);
4037 info_ptr += 8;
4038 if (type_offset_in_tu != NULL)
4039 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4040 header->offset_size);
4041 info_ptr += header->offset_size;
4042
4043 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4044
4045 error_check_comp_unit_head (header, section, abbrev_section);
4046
4047 return info_ptr;
4048 }
4049
4050 /* Fetch the abbreviation table offset from a comp or type unit header. */
4051
4052 static sect_offset
4053 read_abbrev_offset (struct dwarf2_section_info *section,
4054 sect_offset offset)
4055 {
4056 bfd *abfd = section->asection->owner;
4057 gdb_byte *info_ptr;
4058 unsigned int length, initial_length_size, offset_size;
4059 sect_offset abbrev_offset;
4060
4061 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4062 info_ptr = section->buffer + offset.sect_off;
4063 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4064 offset_size = initial_length_size == 4 ? 4 : 8;
4065 info_ptr += initial_length_size + 2 /*version*/;
4066 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4067 return abbrev_offset;
4068 }
4069
4070 /* Allocate a new partial symtab for file named NAME and mark this new
4071 partial symtab as being an include of PST. */
4072
4073 static void
4074 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4075 struct objfile *objfile)
4076 {
4077 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4078
4079 subpst->section_offsets = pst->section_offsets;
4080 subpst->textlow = 0;
4081 subpst->texthigh = 0;
4082
4083 subpst->dependencies = (struct partial_symtab **)
4084 obstack_alloc (&objfile->objfile_obstack,
4085 sizeof (struct partial_symtab *));
4086 subpst->dependencies[0] = pst;
4087 subpst->number_of_dependencies = 1;
4088
4089 subpst->globals_offset = 0;
4090 subpst->n_global_syms = 0;
4091 subpst->statics_offset = 0;
4092 subpst->n_static_syms = 0;
4093 subpst->symtab = NULL;
4094 subpst->read_symtab = pst->read_symtab;
4095 subpst->readin = 0;
4096
4097 /* No private part is necessary for include psymtabs. This property
4098 can be used to differentiate between such include psymtabs and
4099 the regular ones. */
4100 subpst->read_symtab_private = NULL;
4101 }
4102
4103 /* Read the Line Number Program data and extract the list of files
4104 included by the source file represented by PST. Build an include
4105 partial symtab for each of these included files. */
4106
4107 static void
4108 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4109 struct die_info *die,
4110 struct partial_symtab *pst)
4111 {
4112 struct line_header *lh = NULL;
4113 struct attribute *attr;
4114
4115 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4116 if (attr)
4117 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4118 if (lh == NULL)
4119 return; /* No linetable, so no includes. */
4120
4121 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4122 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4123
4124 free_line_header (lh);
4125 }
4126
4127 static hashval_t
4128 hash_signatured_type (const void *item)
4129 {
4130 const struct signatured_type *sig_type = item;
4131
4132 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4133 return sig_type->signature;
4134 }
4135
4136 static int
4137 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4138 {
4139 const struct signatured_type *lhs = item_lhs;
4140 const struct signatured_type *rhs = item_rhs;
4141
4142 return lhs->signature == rhs->signature;
4143 }
4144
4145 /* Allocate a hash table for signatured types. */
4146
4147 static htab_t
4148 allocate_signatured_type_table (struct objfile *objfile)
4149 {
4150 return htab_create_alloc_ex (41,
4151 hash_signatured_type,
4152 eq_signatured_type,
4153 NULL,
4154 &objfile->objfile_obstack,
4155 hashtab_obstack_allocate,
4156 dummy_obstack_deallocate);
4157 }
4158
4159 /* A helper function to add a signatured type CU to a table. */
4160
4161 static int
4162 add_signatured_type_cu_to_table (void **slot, void *datum)
4163 {
4164 struct signatured_type *sigt = *slot;
4165 struct signatured_type ***datap = datum;
4166
4167 **datap = sigt;
4168 ++*datap;
4169
4170 return 1;
4171 }
4172
4173 /* Create the hash table of all entries in the .debug_types section.
4174 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4175 NULL otherwise.
4176 Note: This function processes DWO files only, not DWP files.
4177 The result is a pointer to the hash table or NULL if there are
4178 no types. */
4179
4180 static htab_t
4181 create_debug_types_hash_table (struct dwo_file *dwo_file,
4182 VEC (dwarf2_section_info_def) *types)
4183 {
4184 struct objfile *objfile = dwarf2_per_objfile->objfile;
4185 htab_t types_htab = NULL;
4186 int ix;
4187 struct dwarf2_section_info *section;
4188 struct dwarf2_section_info *abbrev_section;
4189
4190 if (VEC_empty (dwarf2_section_info_def, types))
4191 return NULL;
4192
4193 abbrev_section = (dwo_file != NULL
4194 ? &dwo_file->sections.abbrev
4195 : &dwarf2_per_objfile->abbrev);
4196
4197 if (dwarf2_read_debug)
4198 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4199 dwo_file ? ".dwo" : "",
4200 bfd_get_filename (abbrev_section->asection->owner));
4201
4202 for (ix = 0;
4203 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4204 ++ix)
4205 {
4206 bfd *abfd;
4207 gdb_byte *info_ptr, *end_ptr;
4208 struct dwarf2_section_info *abbrev_section;
4209
4210 dwarf2_read_section (objfile, section);
4211 info_ptr = section->buffer;
4212
4213 if (info_ptr == NULL)
4214 continue;
4215
4216 /* We can't set abfd until now because the section may be empty or
4217 not present, in which case section->asection will be NULL. */
4218 abfd = section->asection->owner;
4219
4220 if (dwo_file)
4221 abbrev_section = &dwo_file->sections.abbrev;
4222 else
4223 abbrev_section = &dwarf2_per_objfile->abbrev;
4224
4225 if (types_htab == NULL)
4226 {
4227 if (dwo_file)
4228 types_htab = allocate_dwo_unit_table (objfile);
4229 else
4230 types_htab = allocate_signatured_type_table (objfile);
4231 }
4232
4233 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4234 because we don't need to read any dies: the signature is in the
4235 header. */
4236
4237 end_ptr = info_ptr + section->size;
4238 while (info_ptr < end_ptr)
4239 {
4240 sect_offset offset;
4241 cu_offset type_offset_in_tu;
4242 ULONGEST signature;
4243 struct signatured_type *sig_type;
4244 struct dwo_unit *dwo_tu;
4245 void **slot;
4246 gdb_byte *ptr = info_ptr;
4247 struct comp_unit_head header;
4248 unsigned int length;
4249
4250 offset.sect_off = ptr - section->buffer;
4251
4252 /* We need to read the type's signature in order to build the hash
4253 table, but we don't need anything else just yet. */
4254
4255 ptr = read_and_check_type_unit_head (&header, section,
4256 abbrev_section, ptr,
4257 &signature, &type_offset_in_tu);
4258
4259 length = get_cu_length (&header);
4260
4261 /* Skip dummy type units. */
4262 if (ptr >= info_ptr + length
4263 || peek_abbrev_code (abfd, ptr) == 0)
4264 {
4265 info_ptr += length;
4266 continue;
4267 }
4268
4269 if (dwo_file)
4270 {
4271 sig_type = NULL;
4272 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4273 struct dwo_unit);
4274 dwo_tu->dwo_file = dwo_file;
4275 dwo_tu->signature = signature;
4276 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4277 dwo_tu->info_or_types_section = section;
4278 dwo_tu->offset = offset;
4279 dwo_tu->length = length;
4280 }
4281 else
4282 {
4283 /* N.B.: type_offset is not usable if this type uses a DWO file.
4284 The real type_offset is in the DWO file. */
4285 dwo_tu = NULL;
4286 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4287 struct signatured_type);
4288 sig_type->signature = signature;
4289 sig_type->type_offset_in_tu = type_offset_in_tu;
4290 sig_type->per_cu.objfile = objfile;
4291 sig_type->per_cu.is_debug_types = 1;
4292 sig_type->per_cu.info_or_types_section = section;
4293 sig_type->per_cu.offset = offset;
4294 sig_type->per_cu.length = length;
4295 }
4296
4297 slot = htab_find_slot (types_htab,
4298 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4299 INSERT);
4300 gdb_assert (slot != NULL);
4301 if (*slot != NULL)
4302 {
4303 sect_offset dup_offset;
4304
4305 if (dwo_file)
4306 {
4307 const struct dwo_unit *dup_tu = *slot;
4308
4309 dup_offset = dup_tu->offset;
4310 }
4311 else
4312 {
4313 const struct signatured_type *dup_tu = *slot;
4314
4315 dup_offset = dup_tu->per_cu.offset;
4316 }
4317
4318 complaint (&symfile_complaints,
4319 _("debug type entry at offset 0x%x is duplicate to the "
4320 "entry at offset 0x%x, signature 0x%s"),
4321 offset.sect_off, dup_offset.sect_off,
4322 phex (signature, sizeof (signature)));
4323 }
4324 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4325
4326 if (dwarf2_read_debug)
4327 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4328 offset.sect_off,
4329 phex (signature, sizeof (signature)));
4330
4331 info_ptr += length;
4332 }
4333 }
4334
4335 return types_htab;
4336 }
4337
4338 /* Create the hash table of all entries in the .debug_types section,
4339 and initialize all_type_units.
4340 The result is zero if there is an error (e.g. missing .debug_types section),
4341 otherwise non-zero. */
4342
4343 static int
4344 create_all_type_units (struct objfile *objfile)
4345 {
4346 htab_t types_htab;
4347 struct signatured_type **iter;
4348
4349 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4350 if (types_htab == NULL)
4351 {
4352 dwarf2_per_objfile->signatured_types = NULL;
4353 return 0;
4354 }
4355
4356 dwarf2_per_objfile->signatured_types = types_htab;
4357
4358 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4359 dwarf2_per_objfile->all_type_units
4360 = obstack_alloc (&objfile->objfile_obstack,
4361 dwarf2_per_objfile->n_type_units
4362 * sizeof (struct signatured_type *));
4363 iter = &dwarf2_per_objfile->all_type_units[0];
4364 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4365 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4366 == dwarf2_per_objfile->n_type_units);
4367
4368 return 1;
4369 }
4370
4371 /* Lookup a signature based type for DW_FORM_ref_sig8.
4372 Returns NULL if signature SIG is not present in the table. */
4373
4374 static struct signatured_type *
4375 lookup_signatured_type (ULONGEST sig)
4376 {
4377 struct signatured_type find_entry, *entry;
4378
4379 if (dwarf2_per_objfile->signatured_types == NULL)
4380 {
4381 complaint (&symfile_complaints,
4382 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4383 return NULL;
4384 }
4385
4386 find_entry.signature = sig;
4387 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4388 return entry;
4389 }
4390 \f
4391 /* Low level DIE reading support. */
4392
4393 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4394
4395 static void
4396 init_cu_die_reader (struct die_reader_specs *reader,
4397 struct dwarf2_cu *cu,
4398 struct dwarf2_section_info *section,
4399 struct dwo_file *dwo_file)
4400 {
4401 gdb_assert (section->readin && section->buffer != NULL);
4402 reader->abfd = section->asection->owner;
4403 reader->cu = cu;
4404 reader->dwo_file = dwo_file;
4405 reader->die_section = section;
4406 reader->buffer = section->buffer;
4407 reader->buffer_end = section->buffer + section->size;
4408 }
4409
4410 /* Initialize a CU (or TU) and read its DIEs.
4411 If the CU defers to a DWO file, read the DWO file as well.
4412
4413 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4414 Otherwise the table specified in the comp unit header is read in and used.
4415 This is an optimization for when we already have the abbrev table.
4416
4417 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4418 Otherwise, a new CU is allocated with xmalloc.
4419
4420 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4421 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4422
4423 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4424 linker) then DIE_READER_FUNC will not get called. */
4425
4426 static void
4427 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4428 struct abbrev_table *abbrev_table,
4429 int use_existing_cu, int keep,
4430 die_reader_func_ftype *die_reader_func,
4431 void *data)
4432 {
4433 struct objfile *objfile = dwarf2_per_objfile->objfile;
4434 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4435 bfd *abfd = section->asection->owner;
4436 struct dwarf2_cu *cu;
4437 gdb_byte *begin_info_ptr, *info_ptr;
4438 struct die_reader_specs reader;
4439 struct die_info *comp_unit_die;
4440 int has_children;
4441 struct attribute *attr;
4442 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4443 struct signatured_type *sig_type = NULL;
4444 struct dwarf2_section_info *abbrev_section;
4445 /* Non-zero if CU currently points to a DWO file and we need to
4446 reread it. When this happens we need to reread the skeleton die
4447 before we can reread the DWO file. */
4448 int rereading_dwo_cu = 0;
4449
4450 if (dwarf2_die_debug)
4451 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4452 this_cu->is_debug_types ? "type" : "comp",
4453 this_cu->offset.sect_off);
4454
4455 if (use_existing_cu)
4456 gdb_assert (keep);
4457
4458 cleanups = make_cleanup (null_cleanup, NULL);
4459
4460 /* This is cheap if the section is already read in. */
4461 dwarf2_read_section (objfile, section);
4462
4463 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4464
4465 abbrev_section = get_abbrev_section_for_cu (this_cu);
4466
4467 if (use_existing_cu && this_cu->cu != NULL)
4468 {
4469 cu = this_cu->cu;
4470
4471 /* If this CU is from a DWO file we need to start over, we need to
4472 refetch the attributes from the skeleton CU.
4473 This could be optimized by retrieving those attributes from when we
4474 were here the first time: the previous comp_unit_die was stored in
4475 comp_unit_obstack. But there's no data yet that we need this
4476 optimization. */
4477 if (cu->dwo_unit != NULL)
4478 rereading_dwo_cu = 1;
4479 }
4480 else
4481 {
4482 /* If !use_existing_cu, this_cu->cu must be NULL. */
4483 gdb_assert (this_cu->cu == NULL);
4484
4485 cu = xmalloc (sizeof (*cu));
4486 init_one_comp_unit (cu, this_cu);
4487
4488 /* If an error occurs while loading, release our storage. */
4489 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4490 }
4491
4492 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4493 {
4494 /* We already have the header, there's no need to read it in again. */
4495 info_ptr += cu->header.first_die_offset.cu_off;
4496 }
4497 else
4498 {
4499 if (this_cu->is_debug_types)
4500 {
4501 ULONGEST signature;
4502 cu_offset type_offset_in_tu;
4503
4504 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4505 abbrev_section, info_ptr,
4506 &signature,
4507 &type_offset_in_tu);
4508
4509 /* Since per_cu is the first member of struct signatured_type,
4510 we can go from a pointer to one to a pointer to the other. */
4511 sig_type = (struct signatured_type *) this_cu;
4512 gdb_assert (sig_type->signature == signature);
4513 gdb_assert (sig_type->type_offset_in_tu.cu_off
4514 == type_offset_in_tu.cu_off);
4515 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4516
4517 /* LENGTH has not been set yet for type units if we're
4518 using .gdb_index. */
4519 this_cu->length = get_cu_length (&cu->header);
4520
4521 /* Establish the type offset that can be used to lookup the type. */
4522 sig_type->type_offset_in_section.sect_off =
4523 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4524 }
4525 else
4526 {
4527 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4528 abbrev_section,
4529 info_ptr, 0);
4530
4531 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4532 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4533 }
4534 }
4535
4536 /* Skip dummy compilation units. */
4537 if (info_ptr >= begin_info_ptr + this_cu->length
4538 || peek_abbrev_code (abfd, info_ptr) == 0)
4539 {
4540 do_cleanups (cleanups);
4541 return;
4542 }
4543
4544 /* If we don't have them yet, read the abbrevs for this compilation unit.
4545 And if we need to read them now, make sure they're freed when we're
4546 done. Note that it's important that if the CU had an abbrev table
4547 on entry we don't free it when we're done: Somewhere up the call stack
4548 it may be in use. */
4549 if (abbrev_table != NULL)
4550 {
4551 gdb_assert (cu->abbrev_table == NULL);
4552 gdb_assert (cu->header.abbrev_offset.sect_off
4553 == abbrev_table->offset.sect_off);
4554 cu->abbrev_table = abbrev_table;
4555 }
4556 else if (cu->abbrev_table == NULL)
4557 {
4558 dwarf2_read_abbrevs (cu, abbrev_section);
4559 make_cleanup (dwarf2_free_abbrev_table, cu);
4560 }
4561 else if (rereading_dwo_cu)
4562 {
4563 dwarf2_free_abbrev_table (cu);
4564 dwarf2_read_abbrevs (cu, abbrev_section);
4565 }
4566
4567 /* Read the top level CU/TU die. */
4568 init_cu_die_reader (&reader, cu, section, NULL);
4569 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4570
4571 /* If we have a DWO stub, process it and then read in the DWO file.
4572 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4573 a DWO CU, that this test will fail. */
4574 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4575 if (attr)
4576 {
4577 char *dwo_name = DW_STRING (attr);
4578 const char *comp_dir_string;
4579 struct dwo_unit *dwo_unit;
4580 ULONGEST signature; /* Or dwo_id. */
4581 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4582 int i,num_extra_attrs;
4583 struct dwarf2_section_info *dwo_abbrev_section;
4584
4585 if (has_children)
4586 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4587 " has children (offset 0x%x) [in module %s]"),
4588 this_cu->offset.sect_off, bfd_get_filename (abfd));
4589
4590 /* These attributes aren't processed until later:
4591 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4592 However, the attribute is found in the stub which we won't have later.
4593 In order to not impose this complication on the rest of the code,
4594 we read them here and copy them to the DWO CU/TU die. */
4595
4596 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4597 DWO file. */
4598 stmt_list = NULL;
4599 if (! this_cu->is_debug_types)
4600 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4601 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4602 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4603 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4604 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4605
4606 /* There should be a DW_AT_addr_base attribute here (if needed).
4607 We need the value before we can process DW_FORM_GNU_addr_index. */
4608 cu->addr_base = 0;
4609 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4610 if (attr)
4611 cu->addr_base = DW_UNSND (attr);
4612
4613 /* There should be a DW_AT_ranges_base attribute here (if needed).
4614 We need the value before we can process DW_AT_ranges. */
4615 cu->ranges_base = 0;
4616 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4617 if (attr)
4618 cu->ranges_base = DW_UNSND (attr);
4619
4620 if (this_cu->is_debug_types)
4621 {
4622 gdb_assert (sig_type != NULL);
4623 signature = sig_type->signature;
4624 }
4625 else
4626 {
4627 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4628 if (! attr)
4629 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4630 dwo_name);
4631 signature = DW_UNSND (attr);
4632 }
4633
4634 /* We may need the comp_dir in order to find the DWO file. */
4635 comp_dir_string = NULL;
4636 if (comp_dir)
4637 comp_dir_string = DW_STRING (comp_dir);
4638
4639 if (this_cu->is_debug_types)
4640 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4641 else
4642 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4643 signature);
4644
4645 if (dwo_unit == NULL)
4646 {
4647 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4648 " with ID %s [in module %s]"),
4649 this_cu->offset.sect_off,
4650 phex (signature, sizeof (signature)),
4651 objfile->name);
4652 }
4653
4654 /* Set up for reading the DWO CU/TU. */
4655 cu->dwo_unit = dwo_unit;
4656 section = dwo_unit->info_or_types_section;
4657 dwarf2_read_section (objfile, section);
4658 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4659 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4660 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4661
4662 if (this_cu->is_debug_types)
4663 {
4664 ULONGEST signature;
4665 cu_offset type_offset_in_tu;
4666
4667 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4668 dwo_abbrev_section,
4669 info_ptr,
4670 &signature,
4671 &type_offset_in_tu);
4672 gdb_assert (sig_type->signature == signature);
4673 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4674 /* For DWOs coming from DWP files, we don't know the CU length
4675 nor the type's offset in the TU until now. */
4676 dwo_unit->length = get_cu_length (&cu->header);
4677 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4678
4679 /* Establish the type offset that can be used to lookup the type.
4680 For DWO files, we don't know it until now. */
4681 sig_type->type_offset_in_section.sect_off =
4682 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4683 }
4684 else
4685 {
4686 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4687 dwo_abbrev_section,
4688 info_ptr, 0);
4689 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4690 /* For DWOs coming from DWP files, we don't know the CU length
4691 until now. */
4692 dwo_unit->length = get_cu_length (&cu->header);
4693 }
4694
4695 /* Discard the original CU's abbrev table, and read the DWO's. */
4696 if (abbrev_table == NULL)
4697 {
4698 dwarf2_free_abbrev_table (cu);
4699 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4700 }
4701 else
4702 {
4703 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4704 make_cleanup (dwarf2_free_abbrev_table, cu);
4705 }
4706
4707 /* Read in the die, but leave space to copy over the attributes
4708 from the stub. This has the benefit of simplifying the rest of
4709 the code - all the real work is done here. */
4710 num_extra_attrs = ((stmt_list != NULL)
4711 + (low_pc != NULL)
4712 + (high_pc != NULL)
4713 + (ranges != NULL)
4714 + (comp_dir != NULL));
4715 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4716 &has_children, num_extra_attrs);
4717
4718 /* Copy over the attributes from the stub to the DWO die. */
4719 i = comp_unit_die->num_attrs;
4720 if (stmt_list != NULL)
4721 comp_unit_die->attrs[i++] = *stmt_list;
4722 if (low_pc != NULL)
4723 comp_unit_die->attrs[i++] = *low_pc;
4724 if (high_pc != NULL)
4725 comp_unit_die->attrs[i++] = *high_pc;
4726 if (ranges != NULL)
4727 comp_unit_die->attrs[i++] = *ranges;
4728 if (comp_dir != NULL)
4729 comp_unit_die->attrs[i++] = *comp_dir;
4730 comp_unit_die->num_attrs += num_extra_attrs;
4731
4732 /* Skip dummy compilation units. */
4733 if (info_ptr >= begin_info_ptr + dwo_unit->length
4734 || peek_abbrev_code (abfd, info_ptr) == 0)
4735 {
4736 do_cleanups (cleanups);
4737 return;
4738 }
4739 }
4740
4741 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4742
4743 if (free_cu_cleanup != NULL)
4744 {
4745 if (keep)
4746 {
4747 /* We've successfully allocated this compilation unit. Let our
4748 caller clean it up when finished with it. */
4749 discard_cleanups (free_cu_cleanup);
4750
4751 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4752 So we have to manually free the abbrev table. */
4753 dwarf2_free_abbrev_table (cu);
4754
4755 /* Link this CU into read_in_chain. */
4756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4757 dwarf2_per_objfile->read_in_chain = this_cu;
4758 }
4759 else
4760 do_cleanups (free_cu_cleanup);
4761 }
4762
4763 do_cleanups (cleanups);
4764 }
4765
4766 /* Read CU/TU THIS_CU in section SECTION,
4767 but do not follow DW_AT_GNU_dwo_name if present.
4768 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4769 to have already done the lookup to find the DWO/DWP file).
4770
4771 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4772 THIS_CU->is_debug_types, but nothing else.
4773
4774 We fill in THIS_CU->length.
4775
4776 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4777 linker) then DIE_READER_FUNC will not get called.
4778
4779 THIS_CU->cu is always freed when done.
4780 This is done in order to not leave THIS_CU->cu in a state where we have
4781 to care whether it refers to the "main" CU or the DWO CU. */
4782
4783 static void
4784 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4785 struct dwarf2_section_info *abbrev_section,
4786 struct dwo_file *dwo_file,
4787 die_reader_func_ftype *die_reader_func,
4788 void *data)
4789 {
4790 struct objfile *objfile = dwarf2_per_objfile->objfile;
4791 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4792 bfd *abfd = section->asection->owner;
4793 struct dwarf2_cu cu;
4794 gdb_byte *begin_info_ptr, *info_ptr;
4795 struct die_reader_specs reader;
4796 struct cleanup *cleanups;
4797 struct die_info *comp_unit_die;
4798 int has_children;
4799
4800 if (dwarf2_die_debug)
4801 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4802 this_cu->is_debug_types ? "type" : "comp",
4803 this_cu->offset.sect_off);
4804
4805 gdb_assert (this_cu->cu == NULL);
4806
4807 /* This is cheap if the section is already read in. */
4808 dwarf2_read_section (objfile, section);
4809
4810 init_one_comp_unit (&cu, this_cu);
4811
4812 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4813
4814 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4815 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4816 abbrev_section, info_ptr,
4817 this_cu->is_debug_types);
4818
4819 this_cu->length = get_cu_length (&cu.header);
4820
4821 /* Skip dummy compilation units. */
4822 if (info_ptr >= begin_info_ptr + this_cu->length
4823 || peek_abbrev_code (abfd, info_ptr) == 0)
4824 {
4825 do_cleanups (cleanups);
4826 return;
4827 }
4828
4829 dwarf2_read_abbrevs (&cu, abbrev_section);
4830 make_cleanup (dwarf2_free_abbrev_table, &cu);
4831
4832 init_cu_die_reader (&reader, &cu, section, dwo_file);
4833 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4834
4835 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4836
4837 do_cleanups (cleanups);
4838 }
4839
4840 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4841 does not lookup the specified DWO file.
4842 This cannot be used to read DWO files.
4843
4844 THIS_CU->cu is always freed when done.
4845 This is done in order to not leave THIS_CU->cu in a state where we have
4846 to care whether it refers to the "main" CU or the DWO CU.
4847 We can revisit this if the data shows there's a performance issue. */
4848
4849 static void
4850 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4851 die_reader_func_ftype *die_reader_func,
4852 void *data)
4853 {
4854 init_cutu_and_read_dies_no_follow (this_cu,
4855 get_abbrev_section_for_cu (this_cu),
4856 NULL,
4857 die_reader_func, data);
4858 }
4859
4860 /* Create a psymtab named NAME and assign it to PER_CU.
4861
4862 The caller must fill in the following details:
4863 dirname, textlow, texthigh. */
4864
4865 static struct partial_symtab *
4866 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4867 {
4868 struct objfile *objfile = per_cu->objfile;
4869 struct partial_symtab *pst;
4870
4871 pst = start_psymtab_common (objfile, objfile->section_offsets,
4872 name, 0,
4873 objfile->global_psymbols.next,
4874 objfile->static_psymbols.next);
4875
4876 pst->psymtabs_addrmap_supported = 1;
4877
4878 /* This is the glue that links PST into GDB's symbol API. */
4879 pst->read_symtab_private = per_cu;
4880 pst->read_symtab = dwarf2_psymtab_to_symtab;
4881 per_cu->v.psymtab = pst;
4882
4883 return pst;
4884 }
4885
4886 /* die_reader_func for process_psymtab_comp_unit. */
4887
4888 static void
4889 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4890 gdb_byte *info_ptr,
4891 struct die_info *comp_unit_die,
4892 int has_children,
4893 void *data)
4894 {
4895 struct dwarf2_cu *cu = reader->cu;
4896 struct objfile *objfile = cu->objfile;
4897 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4898 struct attribute *attr;
4899 CORE_ADDR baseaddr;
4900 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4901 struct partial_symtab *pst;
4902 int has_pc_info;
4903 const char *filename;
4904 int *want_partial_unit_ptr = data;
4905
4906 if (comp_unit_die->tag == DW_TAG_partial_unit
4907 && (want_partial_unit_ptr == NULL
4908 || !*want_partial_unit_ptr))
4909 return;
4910
4911 gdb_assert (! per_cu->is_debug_types);
4912
4913 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4914
4915 cu->list_in_scope = &file_symbols;
4916
4917 /* Allocate a new partial symbol table structure. */
4918 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4919 if (attr == NULL || !DW_STRING (attr))
4920 filename = "";
4921 else
4922 filename = DW_STRING (attr);
4923
4924 pst = create_partial_symtab (per_cu, filename);
4925
4926 /* This must be done before calling dwarf2_build_include_psymtabs. */
4927 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4928 if (attr != NULL)
4929 pst->dirname = DW_STRING (attr);
4930
4931 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4932
4933 dwarf2_find_base_address (comp_unit_die, cu);
4934
4935 /* Possibly set the default values of LOWPC and HIGHPC from
4936 `DW_AT_ranges'. */
4937 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4938 &best_highpc, cu, pst);
4939 if (has_pc_info == 1 && best_lowpc < best_highpc)
4940 /* Store the contiguous range if it is not empty; it can be empty for
4941 CUs with no code. */
4942 addrmap_set_empty (objfile->psymtabs_addrmap,
4943 best_lowpc + baseaddr,
4944 best_highpc + baseaddr - 1, pst);
4945
4946 /* Check if comp unit has_children.
4947 If so, read the rest of the partial symbols from this comp unit.
4948 If not, there's no more debug_info for this comp unit. */
4949 if (has_children)
4950 {
4951 struct partial_die_info *first_die;
4952 CORE_ADDR lowpc, highpc;
4953
4954 lowpc = ((CORE_ADDR) -1);
4955 highpc = ((CORE_ADDR) 0);
4956
4957 first_die = load_partial_dies (reader, info_ptr, 1);
4958
4959 scan_partial_symbols (first_die, &lowpc, &highpc,
4960 ! has_pc_info, cu);
4961
4962 /* If we didn't find a lowpc, set it to highpc to avoid
4963 complaints from `maint check'. */
4964 if (lowpc == ((CORE_ADDR) -1))
4965 lowpc = highpc;
4966
4967 /* If the compilation unit didn't have an explicit address range,
4968 then use the information extracted from its child dies. */
4969 if (! has_pc_info)
4970 {
4971 best_lowpc = lowpc;
4972 best_highpc = highpc;
4973 }
4974 }
4975 pst->textlow = best_lowpc + baseaddr;
4976 pst->texthigh = best_highpc + baseaddr;
4977
4978 pst->n_global_syms = objfile->global_psymbols.next -
4979 (objfile->global_psymbols.list + pst->globals_offset);
4980 pst->n_static_syms = objfile->static_psymbols.next -
4981 (objfile->static_psymbols.list + pst->statics_offset);
4982 sort_pst_symbols (objfile, pst);
4983
4984 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4985 {
4986 int i;
4987 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4988 struct dwarf2_per_cu_data *iter;
4989
4990 /* Fill in 'dependencies' here; we fill in 'users' in a
4991 post-pass. */
4992 pst->number_of_dependencies = len;
4993 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4994 len * sizeof (struct symtab *));
4995 for (i = 0;
4996 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4997 i, iter);
4998 ++i)
4999 pst->dependencies[i] = iter->v.psymtab;
5000
5001 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
5002 }
5003
5004 /* Get the list of files included in the current compilation unit,
5005 and build a psymtab for each of them. */
5006 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5007
5008 if (dwarf2_read_debug)
5009 {
5010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5011
5012 fprintf_unfiltered (gdb_stdlog,
5013 "Psymtab for %s unit @0x%x: %s - %s"
5014 ", %d global, %d static syms\n",
5015 per_cu->is_debug_types ? "type" : "comp",
5016 per_cu->offset.sect_off,
5017 paddress (gdbarch, pst->textlow),
5018 paddress (gdbarch, pst->texthigh),
5019 pst->n_global_syms, pst->n_static_syms);
5020 }
5021 }
5022
5023 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5024 Process compilation unit THIS_CU for a psymtab. */
5025
5026 static void
5027 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5028 int want_partial_unit)
5029 {
5030 /* If this compilation unit was already read in, free the
5031 cached copy in order to read it in again. This is
5032 necessary because we skipped some symbols when we first
5033 read in the compilation unit (see load_partial_dies).
5034 This problem could be avoided, but the benefit is unclear. */
5035 if (this_cu->cu != NULL)
5036 free_one_cached_comp_unit (this_cu);
5037
5038 gdb_assert (! this_cu->is_debug_types);
5039 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5040 process_psymtab_comp_unit_reader,
5041 &want_partial_unit);
5042
5043 /* Age out any secondary CUs. */
5044 age_cached_comp_units ();
5045 }
5046
5047 static hashval_t
5048 hash_type_unit_group (const void *item)
5049 {
5050 const struct type_unit_group *tu_group = item;
5051
5052 return hash_stmt_list_entry (&tu_group->hash);
5053 }
5054
5055 static int
5056 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5057 {
5058 const struct type_unit_group *lhs = item_lhs;
5059 const struct type_unit_group *rhs = item_rhs;
5060
5061 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5062 }
5063
5064 /* Allocate a hash table for type unit groups. */
5065
5066 static htab_t
5067 allocate_type_unit_groups_table (void)
5068 {
5069 return htab_create_alloc_ex (3,
5070 hash_type_unit_group,
5071 eq_type_unit_group,
5072 NULL,
5073 &dwarf2_per_objfile->objfile->objfile_obstack,
5074 hashtab_obstack_allocate,
5075 dummy_obstack_deallocate);
5076 }
5077
5078 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5079 partial symtabs. We combine several TUs per psymtab to not let the size
5080 of any one psymtab grow too big. */
5081 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5082 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5083
5084 /* Helper routine for get_type_unit_group.
5085 Create the type_unit_group object used to hold one or more TUs. */
5086
5087 static struct type_unit_group *
5088 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5089 {
5090 struct objfile *objfile = dwarf2_per_objfile->objfile;
5091 struct dwarf2_per_cu_data *per_cu;
5092 struct type_unit_group *tu_group;
5093
5094 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5095 struct type_unit_group);
5096 per_cu = &tu_group->per_cu;
5097 per_cu->objfile = objfile;
5098 per_cu->is_debug_types = 1;
5099 per_cu->s.type_unit_group = tu_group;
5100
5101 if (dwarf2_per_objfile->using_index)
5102 {
5103 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5104 struct dwarf2_per_cu_quick_data);
5105 tu_group->t.first_tu = cu->per_cu;
5106 }
5107 else
5108 {
5109 unsigned int line_offset = line_offset_struct.sect_off;
5110 struct partial_symtab *pst;
5111 char *name;
5112
5113 /* Give the symtab a useful name for debug purposes. */
5114 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5115 name = xstrprintf ("<type_units_%d>",
5116 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5117 else
5118 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5119
5120 pst = create_partial_symtab (per_cu, name);
5121 pst->anonymous = 1;
5122
5123 xfree (name);
5124 }
5125
5126 tu_group->hash.dwo_unit = cu->dwo_unit;
5127 tu_group->hash.line_offset = line_offset_struct;
5128
5129 return tu_group;
5130 }
5131
5132 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5133 STMT_LIST is a DW_AT_stmt_list attribute. */
5134
5135 static struct type_unit_group *
5136 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5137 {
5138 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5139 struct type_unit_group *tu_group;
5140 void **slot;
5141 unsigned int line_offset;
5142 struct type_unit_group type_unit_group_for_lookup;
5143
5144 if (dwarf2_per_objfile->type_unit_groups == NULL)
5145 {
5146 dwarf2_per_objfile->type_unit_groups =
5147 allocate_type_unit_groups_table ();
5148 }
5149
5150 /* Do we need to create a new group, or can we use an existing one? */
5151
5152 if (stmt_list)
5153 {
5154 line_offset = DW_UNSND (stmt_list);
5155 ++tu_stats->nr_symtab_sharers;
5156 }
5157 else
5158 {
5159 /* Ugh, no stmt_list. Rare, but we have to handle it.
5160 We can do various things here like create one group per TU or
5161 spread them over multiple groups to split up the expansion work.
5162 To avoid worst case scenarios (too many groups or too large groups)
5163 we, umm, group them in bunches. */
5164 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5165 | (tu_stats->nr_stmt_less_type_units
5166 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5167 ++tu_stats->nr_stmt_less_type_units;
5168 }
5169
5170 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5171 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5172 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5173 &type_unit_group_for_lookup, INSERT);
5174 if (*slot != NULL)
5175 {
5176 tu_group = *slot;
5177 gdb_assert (tu_group != NULL);
5178 }
5179 else
5180 {
5181 sect_offset line_offset_struct;
5182
5183 line_offset_struct.sect_off = line_offset;
5184 tu_group = create_type_unit_group (cu, line_offset_struct);
5185 *slot = tu_group;
5186 ++tu_stats->nr_symtabs;
5187 }
5188
5189 return tu_group;
5190 }
5191
5192 /* Struct used to sort TUs by their abbreviation table offset. */
5193
5194 struct tu_abbrev_offset
5195 {
5196 struct signatured_type *sig_type;
5197 sect_offset abbrev_offset;
5198 };
5199
5200 /* Helper routine for build_type_unit_groups, passed to qsort. */
5201
5202 static int
5203 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5204 {
5205 const struct tu_abbrev_offset * const *a = ap;
5206 const struct tu_abbrev_offset * const *b = bp;
5207 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5208 unsigned int boff = (*b)->abbrev_offset.sect_off;
5209
5210 return (aoff > boff) - (aoff < boff);
5211 }
5212
5213 /* A helper function to add a type_unit_group to a table. */
5214
5215 static int
5216 add_type_unit_group_to_table (void **slot, void *datum)
5217 {
5218 struct type_unit_group *tu_group = *slot;
5219 struct type_unit_group ***datap = datum;
5220
5221 **datap = tu_group;
5222 ++*datap;
5223
5224 return 1;
5225 }
5226
5227 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5228 each one passing FUNC,DATA.
5229
5230 The efficiency is because we sort TUs by the abbrev table they use and
5231 only read each abbrev table once. In one program there are 200K TUs
5232 sharing 8K abbrev tables.
5233
5234 The main purpose of this function is to support building the
5235 dwarf2_per_objfile->type_unit_groups table.
5236 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5237 can collapse the search space by grouping them by stmt_list.
5238 The savings can be significant, in the same program from above the 200K TUs
5239 share 8K stmt_list tables.
5240
5241 FUNC is expected to call get_type_unit_group, which will create the
5242 struct type_unit_group if necessary and add it to
5243 dwarf2_per_objfile->type_unit_groups. */
5244
5245 static void
5246 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5247 {
5248 struct objfile *objfile = dwarf2_per_objfile->objfile;
5249 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5250 struct cleanup *cleanups;
5251 struct abbrev_table *abbrev_table;
5252 sect_offset abbrev_offset;
5253 struct tu_abbrev_offset *sorted_by_abbrev;
5254 struct type_unit_group **iter;
5255 int i;
5256
5257 /* It's up to the caller to not call us multiple times. */
5258 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5259
5260 if (dwarf2_per_objfile->n_type_units == 0)
5261 return;
5262
5263 /* TUs typically share abbrev tables, and there can be way more TUs than
5264 abbrev tables. Sort by abbrev table to reduce the number of times we
5265 read each abbrev table in.
5266 Alternatives are to punt or to maintain a cache of abbrev tables.
5267 This is simpler and efficient enough for now.
5268
5269 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5270 symtab to use). Typically TUs with the same abbrev offset have the same
5271 stmt_list value too so in practice this should work well.
5272
5273 The basic algorithm here is:
5274
5275 sort TUs by abbrev table
5276 for each TU with same abbrev table:
5277 read abbrev table if first user
5278 read TU top level DIE
5279 [IWBN if DWO skeletons had DW_AT_stmt_list]
5280 call FUNC */
5281
5282 if (dwarf2_read_debug)
5283 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5284
5285 /* Sort in a separate table to maintain the order of all_type_units
5286 for .gdb_index: TU indices directly index all_type_units. */
5287 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5288 dwarf2_per_objfile->n_type_units);
5289 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5290 {
5291 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5292
5293 sorted_by_abbrev[i].sig_type = sig_type;
5294 sorted_by_abbrev[i].abbrev_offset =
5295 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5296 sig_type->per_cu.offset);
5297 }
5298 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5299 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5300 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5301
5302 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5303 called any number of times, so we don't reset tu_stats here. */
5304
5305 abbrev_offset.sect_off = ~(unsigned) 0;
5306 abbrev_table = NULL;
5307 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5308
5309 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5310 {
5311 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5312
5313 /* Switch to the next abbrev table if necessary. */
5314 if (abbrev_table == NULL
5315 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5316 {
5317 if (abbrev_table != NULL)
5318 {
5319 abbrev_table_free (abbrev_table);
5320 /* Reset to NULL in case abbrev_table_read_table throws
5321 an error: abbrev_table_free_cleanup will get called. */
5322 abbrev_table = NULL;
5323 }
5324 abbrev_offset = tu->abbrev_offset;
5325 abbrev_table =
5326 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5327 abbrev_offset);
5328 ++tu_stats->nr_uniq_abbrev_tables;
5329 }
5330
5331 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5332 func, data);
5333 }
5334
5335 /* Create a vector of pointers to primary type units to make it easy to
5336 iterate over them and CUs. See dw2_get_primary_cu. */
5337 dwarf2_per_objfile->n_type_unit_groups =
5338 htab_elements (dwarf2_per_objfile->type_unit_groups);
5339 dwarf2_per_objfile->all_type_unit_groups =
5340 obstack_alloc (&objfile->objfile_obstack,
5341 dwarf2_per_objfile->n_type_unit_groups
5342 * sizeof (struct type_unit_group *));
5343 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5344 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5345 add_type_unit_group_to_table, &iter);
5346 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5347 == dwarf2_per_objfile->n_type_unit_groups);
5348
5349 do_cleanups (cleanups);
5350
5351 if (dwarf2_read_debug)
5352 {
5353 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5354 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5355 dwarf2_per_objfile->n_type_units);
5356 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5357 tu_stats->nr_uniq_abbrev_tables);
5358 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5359 tu_stats->nr_symtabs);
5360 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5361 tu_stats->nr_symtab_sharers);
5362 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5363 tu_stats->nr_stmt_less_type_units);
5364 }
5365 }
5366
5367 /* Reader function for build_type_psymtabs. */
5368
5369 static void
5370 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5371 gdb_byte *info_ptr,
5372 struct die_info *type_unit_die,
5373 int has_children,
5374 void *data)
5375 {
5376 struct objfile *objfile = dwarf2_per_objfile->objfile;
5377 struct dwarf2_cu *cu = reader->cu;
5378 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5379 struct type_unit_group *tu_group;
5380 struct attribute *attr;
5381 struct partial_die_info *first_die;
5382 CORE_ADDR lowpc, highpc;
5383 struct partial_symtab *pst;
5384
5385 gdb_assert (data == NULL);
5386
5387 if (! has_children)
5388 return;
5389
5390 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5391 tu_group = get_type_unit_group (cu, attr);
5392
5393 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5394
5395 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5396 cu->list_in_scope = &file_symbols;
5397 pst = create_partial_symtab (per_cu, "");
5398 pst->anonymous = 1;
5399
5400 first_die = load_partial_dies (reader, info_ptr, 1);
5401
5402 lowpc = (CORE_ADDR) -1;
5403 highpc = (CORE_ADDR) 0;
5404 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5405
5406 pst->n_global_syms = objfile->global_psymbols.next -
5407 (objfile->global_psymbols.list + pst->globals_offset);
5408 pst->n_static_syms = objfile->static_psymbols.next -
5409 (objfile->static_psymbols.list + pst->statics_offset);
5410 sort_pst_symbols (objfile, pst);
5411 }
5412
5413 /* Traversal function for build_type_psymtabs. */
5414
5415 static int
5416 build_type_psymtab_dependencies (void **slot, void *info)
5417 {
5418 struct objfile *objfile = dwarf2_per_objfile->objfile;
5419 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5420 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5421 struct partial_symtab *pst = per_cu->v.psymtab;
5422 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5423 struct dwarf2_per_cu_data *iter;
5424 int i;
5425
5426 gdb_assert (len > 0);
5427
5428 pst->number_of_dependencies = len;
5429 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5430 len * sizeof (struct psymtab *));
5431 for (i = 0;
5432 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5433 ++i)
5434 {
5435 pst->dependencies[i] = iter->v.psymtab;
5436 iter->s.type_unit_group = tu_group;
5437 }
5438
5439 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5440
5441 return 1;
5442 }
5443
5444 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5445 Build partial symbol tables for the .debug_types comp-units. */
5446
5447 static void
5448 build_type_psymtabs (struct objfile *objfile)
5449 {
5450 if (! create_all_type_units (objfile))
5451 return;
5452
5453 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5454
5455 /* Now that all TUs have been processed we can fill in the dependencies. */
5456 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5457 build_type_psymtab_dependencies, NULL);
5458 }
5459
5460 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5461
5462 static void
5463 psymtabs_addrmap_cleanup (void *o)
5464 {
5465 struct objfile *objfile = o;
5466
5467 objfile->psymtabs_addrmap = NULL;
5468 }
5469
5470 /* Compute the 'user' field for each psymtab in OBJFILE. */
5471
5472 static void
5473 set_partial_user (struct objfile *objfile)
5474 {
5475 int i;
5476
5477 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5478 {
5479 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5480 struct partial_symtab *pst = per_cu->v.psymtab;
5481 int j;
5482
5483 if (pst == NULL)
5484 continue;
5485
5486 for (j = 0; j < pst->number_of_dependencies; ++j)
5487 {
5488 /* Set the 'user' field only if it is not already set. */
5489 if (pst->dependencies[j]->user == NULL)
5490 pst->dependencies[j]->user = pst;
5491 }
5492 }
5493 }
5494
5495 /* Build the partial symbol table by doing a quick pass through the
5496 .debug_info and .debug_abbrev sections. */
5497
5498 static void
5499 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5500 {
5501 struct cleanup *back_to, *addrmap_cleanup;
5502 struct obstack temp_obstack;
5503 int i;
5504
5505 if (dwarf2_read_debug)
5506 {
5507 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5508 objfile->name);
5509 }
5510
5511 dwarf2_per_objfile->reading_partial_symbols = 1;
5512
5513 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5514
5515 /* Any cached compilation units will be linked by the per-objfile
5516 read_in_chain. Make sure to free them when we're done. */
5517 back_to = make_cleanup (free_cached_comp_units, NULL);
5518
5519 build_type_psymtabs (objfile);
5520
5521 create_all_comp_units (objfile);
5522
5523 /* Create a temporary address map on a temporary obstack. We later
5524 copy this to the final obstack. */
5525 obstack_init (&temp_obstack);
5526 make_cleanup_obstack_free (&temp_obstack);
5527 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5528 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5529
5530 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5531 {
5532 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5533
5534 process_psymtab_comp_unit (per_cu, 0);
5535 }
5536
5537 set_partial_user (objfile);
5538
5539 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5540 &objfile->objfile_obstack);
5541 discard_cleanups (addrmap_cleanup);
5542
5543 do_cleanups (back_to);
5544
5545 if (dwarf2_read_debug)
5546 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5547 objfile->name);
5548 }
5549
5550 /* die_reader_func for load_partial_comp_unit. */
5551
5552 static void
5553 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5554 gdb_byte *info_ptr,
5555 struct die_info *comp_unit_die,
5556 int has_children,
5557 void *data)
5558 {
5559 struct dwarf2_cu *cu = reader->cu;
5560
5561 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5562
5563 /* Check if comp unit has_children.
5564 If so, read the rest of the partial symbols from this comp unit.
5565 If not, there's no more debug_info for this comp unit. */
5566 if (has_children)
5567 load_partial_dies (reader, info_ptr, 0);
5568 }
5569
5570 /* Load the partial DIEs for a secondary CU into memory.
5571 This is also used when rereading a primary CU with load_all_dies. */
5572
5573 static void
5574 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5575 {
5576 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5577 load_partial_comp_unit_reader, NULL);
5578 }
5579
5580 static void
5581 read_comp_units_from_section (struct objfile *objfile,
5582 struct dwarf2_section_info *section,
5583 unsigned int is_dwz,
5584 int *n_allocated,
5585 int *n_comp_units,
5586 struct dwarf2_per_cu_data ***all_comp_units)
5587 {
5588 gdb_byte *info_ptr;
5589 bfd *abfd = section->asection->owner;
5590
5591 dwarf2_read_section (objfile, section);
5592
5593 info_ptr = section->buffer;
5594
5595 while (info_ptr < section->buffer + section->size)
5596 {
5597 unsigned int length, initial_length_size;
5598 struct dwarf2_per_cu_data *this_cu;
5599 sect_offset offset;
5600
5601 offset.sect_off = info_ptr - section->buffer;
5602
5603 /* Read just enough information to find out where the next
5604 compilation unit is. */
5605 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5606
5607 /* Save the compilation unit for later lookup. */
5608 this_cu = obstack_alloc (&objfile->objfile_obstack,
5609 sizeof (struct dwarf2_per_cu_data));
5610 memset (this_cu, 0, sizeof (*this_cu));
5611 this_cu->offset = offset;
5612 this_cu->length = length + initial_length_size;
5613 this_cu->is_dwz = is_dwz;
5614 this_cu->objfile = objfile;
5615 this_cu->info_or_types_section = section;
5616
5617 if (*n_comp_units == *n_allocated)
5618 {
5619 *n_allocated *= 2;
5620 *all_comp_units = xrealloc (*all_comp_units,
5621 *n_allocated
5622 * sizeof (struct dwarf2_per_cu_data *));
5623 }
5624 (*all_comp_units)[*n_comp_units] = this_cu;
5625 ++*n_comp_units;
5626
5627 info_ptr = info_ptr + this_cu->length;
5628 }
5629 }
5630
5631 /* Create a list of all compilation units in OBJFILE.
5632 This is only done for -readnow and building partial symtabs. */
5633
5634 static void
5635 create_all_comp_units (struct objfile *objfile)
5636 {
5637 int n_allocated;
5638 int n_comp_units;
5639 struct dwarf2_per_cu_data **all_comp_units;
5640
5641 n_comp_units = 0;
5642 n_allocated = 10;
5643 all_comp_units = xmalloc (n_allocated
5644 * sizeof (struct dwarf2_per_cu_data *));
5645
5646 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5647 &n_allocated, &n_comp_units, &all_comp_units);
5648
5649 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5650 {
5651 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5652
5653 read_comp_units_from_section (objfile, &dwz->info, 1,
5654 &n_allocated, &n_comp_units,
5655 &all_comp_units);
5656 }
5657
5658 dwarf2_per_objfile->all_comp_units
5659 = obstack_alloc (&objfile->objfile_obstack,
5660 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5661 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5662 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5663 xfree (all_comp_units);
5664 dwarf2_per_objfile->n_comp_units = n_comp_units;
5665 }
5666
5667 /* Process all loaded DIEs for compilation unit CU, starting at
5668 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5669 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5670 DW_AT_ranges). If NEED_PC is set, then this function will set
5671 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5672 and record the covered ranges in the addrmap. */
5673
5674 static void
5675 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5676 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5677 {
5678 struct partial_die_info *pdi;
5679
5680 /* Now, march along the PDI's, descending into ones which have
5681 interesting children but skipping the children of the other ones,
5682 until we reach the end of the compilation unit. */
5683
5684 pdi = first_die;
5685
5686 while (pdi != NULL)
5687 {
5688 fixup_partial_die (pdi, cu);
5689
5690 /* Anonymous namespaces or modules have no name but have interesting
5691 children, so we need to look at them. Ditto for anonymous
5692 enums. */
5693
5694 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5695 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5696 || pdi->tag == DW_TAG_imported_unit)
5697 {
5698 switch (pdi->tag)
5699 {
5700 case DW_TAG_subprogram:
5701 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5702 break;
5703 case DW_TAG_constant:
5704 case DW_TAG_variable:
5705 case DW_TAG_typedef:
5706 case DW_TAG_union_type:
5707 if (!pdi->is_declaration)
5708 {
5709 add_partial_symbol (pdi, cu);
5710 }
5711 break;
5712 case DW_TAG_class_type:
5713 case DW_TAG_interface_type:
5714 case DW_TAG_structure_type:
5715 if (!pdi->is_declaration)
5716 {
5717 add_partial_symbol (pdi, cu);
5718 }
5719 break;
5720 case DW_TAG_enumeration_type:
5721 if (!pdi->is_declaration)
5722 add_partial_enumeration (pdi, cu);
5723 break;
5724 case DW_TAG_base_type:
5725 case DW_TAG_subrange_type:
5726 /* File scope base type definitions are added to the partial
5727 symbol table. */
5728 add_partial_symbol (pdi, cu);
5729 break;
5730 case DW_TAG_namespace:
5731 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5732 break;
5733 case DW_TAG_module:
5734 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5735 break;
5736 case DW_TAG_imported_unit:
5737 {
5738 struct dwarf2_per_cu_data *per_cu;
5739
5740 /* For now we don't handle imported units in type units. */
5741 if (cu->per_cu->is_debug_types)
5742 {
5743 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5744 " supported in type units [in module %s]"),
5745 cu->objfile->name);
5746 }
5747
5748 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5749 pdi->is_dwz,
5750 cu->objfile);
5751
5752 /* Go read the partial unit, if needed. */
5753 if (per_cu->v.psymtab == NULL)
5754 process_psymtab_comp_unit (per_cu, 1);
5755
5756 VEC_safe_push (dwarf2_per_cu_ptr,
5757 cu->per_cu->s.imported_symtabs, per_cu);
5758 }
5759 break;
5760 default:
5761 break;
5762 }
5763 }
5764
5765 /* If the die has a sibling, skip to the sibling. */
5766
5767 pdi = pdi->die_sibling;
5768 }
5769 }
5770
5771 /* Functions used to compute the fully scoped name of a partial DIE.
5772
5773 Normally, this is simple. For C++, the parent DIE's fully scoped
5774 name is concatenated with "::" and the partial DIE's name. For
5775 Java, the same thing occurs except that "." is used instead of "::".
5776 Enumerators are an exception; they use the scope of their parent
5777 enumeration type, i.e. the name of the enumeration type is not
5778 prepended to the enumerator.
5779
5780 There are two complexities. One is DW_AT_specification; in this
5781 case "parent" means the parent of the target of the specification,
5782 instead of the direct parent of the DIE. The other is compilers
5783 which do not emit DW_TAG_namespace; in this case we try to guess
5784 the fully qualified name of structure types from their members'
5785 linkage names. This must be done using the DIE's children rather
5786 than the children of any DW_AT_specification target. We only need
5787 to do this for structures at the top level, i.e. if the target of
5788 any DW_AT_specification (if any; otherwise the DIE itself) does not
5789 have a parent. */
5790
5791 /* Compute the scope prefix associated with PDI's parent, in
5792 compilation unit CU. The result will be allocated on CU's
5793 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5794 field. NULL is returned if no prefix is necessary. */
5795 static char *
5796 partial_die_parent_scope (struct partial_die_info *pdi,
5797 struct dwarf2_cu *cu)
5798 {
5799 char *grandparent_scope;
5800 struct partial_die_info *parent, *real_pdi;
5801
5802 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5803 then this means the parent of the specification DIE. */
5804
5805 real_pdi = pdi;
5806 while (real_pdi->has_specification)
5807 real_pdi = find_partial_die (real_pdi->spec_offset,
5808 real_pdi->spec_is_dwz, cu);
5809
5810 parent = real_pdi->die_parent;
5811 if (parent == NULL)
5812 return NULL;
5813
5814 if (parent->scope_set)
5815 return parent->scope;
5816
5817 fixup_partial_die (parent, cu);
5818
5819 grandparent_scope = partial_die_parent_scope (parent, cu);
5820
5821 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5822 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5823 Work around this problem here. */
5824 if (cu->language == language_cplus
5825 && parent->tag == DW_TAG_namespace
5826 && strcmp (parent->name, "::") == 0
5827 && grandparent_scope == NULL)
5828 {
5829 parent->scope = NULL;
5830 parent->scope_set = 1;
5831 return NULL;
5832 }
5833
5834 if (pdi->tag == DW_TAG_enumerator)
5835 /* Enumerators should not get the name of the enumeration as a prefix. */
5836 parent->scope = grandparent_scope;
5837 else if (parent->tag == DW_TAG_namespace
5838 || parent->tag == DW_TAG_module
5839 || parent->tag == DW_TAG_structure_type
5840 || parent->tag == DW_TAG_class_type
5841 || parent->tag == DW_TAG_interface_type
5842 || parent->tag == DW_TAG_union_type
5843 || parent->tag == DW_TAG_enumeration_type)
5844 {
5845 if (grandparent_scope == NULL)
5846 parent->scope = parent->name;
5847 else
5848 parent->scope = typename_concat (&cu->comp_unit_obstack,
5849 grandparent_scope,
5850 parent->name, 0, cu);
5851 }
5852 else
5853 {
5854 /* FIXME drow/2004-04-01: What should we be doing with
5855 function-local names? For partial symbols, we should probably be
5856 ignoring them. */
5857 complaint (&symfile_complaints,
5858 _("unhandled containing DIE tag %d for DIE at %d"),
5859 parent->tag, pdi->offset.sect_off);
5860 parent->scope = grandparent_scope;
5861 }
5862
5863 parent->scope_set = 1;
5864 return parent->scope;
5865 }
5866
5867 /* Return the fully scoped name associated with PDI, from compilation unit
5868 CU. The result will be allocated with malloc. */
5869
5870 static char *
5871 partial_die_full_name (struct partial_die_info *pdi,
5872 struct dwarf2_cu *cu)
5873 {
5874 char *parent_scope;
5875
5876 /* If this is a template instantiation, we can not work out the
5877 template arguments from partial DIEs. So, unfortunately, we have
5878 to go through the full DIEs. At least any work we do building
5879 types here will be reused if full symbols are loaded later. */
5880 if (pdi->has_template_arguments)
5881 {
5882 fixup_partial_die (pdi, cu);
5883
5884 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5885 {
5886 struct die_info *die;
5887 struct attribute attr;
5888 struct dwarf2_cu *ref_cu = cu;
5889
5890 /* DW_FORM_ref_addr is using section offset. */
5891 attr.name = 0;
5892 attr.form = DW_FORM_ref_addr;
5893 attr.u.unsnd = pdi->offset.sect_off;
5894 die = follow_die_ref (NULL, &attr, &ref_cu);
5895
5896 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5897 }
5898 }
5899
5900 parent_scope = partial_die_parent_scope (pdi, cu);
5901 if (parent_scope == NULL)
5902 return NULL;
5903 else
5904 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5905 }
5906
5907 static void
5908 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5909 {
5910 struct objfile *objfile = cu->objfile;
5911 CORE_ADDR addr = 0;
5912 char *actual_name = NULL;
5913 CORE_ADDR baseaddr;
5914 int built_actual_name = 0;
5915
5916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5917
5918 actual_name = partial_die_full_name (pdi, cu);
5919 if (actual_name)
5920 built_actual_name = 1;
5921
5922 if (actual_name == NULL)
5923 actual_name = pdi->name;
5924
5925 switch (pdi->tag)
5926 {
5927 case DW_TAG_subprogram:
5928 if (pdi->is_external || cu->language == language_ada)
5929 {
5930 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5931 of the global scope. But in Ada, we want to be able to access
5932 nested procedures globally. So all Ada subprograms are stored
5933 in the global scope. */
5934 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5935 mst_text, objfile); */
5936 add_psymbol_to_list (actual_name, strlen (actual_name),
5937 built_actual_name,
5938 VAR_DOMAIN, LOC_BLOCK,
5939 &objfile->global_psymbols,
5940 0, pdi->lowpc + baseaddr,
5941 cu->language, objfile);
5942 }
5943 else
5944 {
5945 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5946 mst_file_text, objfile); */
5947 add_psymbol_to_list (actual_name, strlen (actual_name),
5948 built_actual_name,
5949 VAR_DOMAIN, LOC_BLOCK,
5950 &objfile->static_psymbols,
5951 0, pdi->lowpc + baseaddr,
5952 cu->language, objfile);
5953 }
5954 break;
5955 case DW_TAG_constant:
5956 {
5957 struct psymbol_allocation_list *list;
5958
5959 if (pdi->is_external)
5960 list = &objfile->global_psymbols;
5961 else
5962 list = &objfile->static_psymbols;
5963 add_psymbol_to_list (actual_name, strlen (actual_name),
5964 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5965 list, 0, 0, cu->language, objfile);
5966 }
5967 break;
5968 case DW_TAG_variable:
5969 if (pdi->d.locdesc)
5970 addr = decode_locdesc (pdi->d.locdesc, cu);
5971
5972 if (pdi->d.locdesc
5973 && addr == 0
5974 && !dwarf2_per_objfile->has_section_at_zero)
5975 {
5976 /* A global or static variable may also have been stripped
5977 out by the linker if unused, in which case its address
5978 will be nullified; do not add such variables into partial
5979 symbol table then. */
5980 }
5981 else if (pdi->is_external)
5982 {
5983 /* Global Variable.
5984 Don't enter into the minimal symbol tables as there is
5985 a minimal symbol table entry from the ELF symbols already.
5986 Enter into partial symbol table if it has a location
5987 descriptor or a type.
5988 If the location descriptor is missing, new_symbol will create
5989 a LOC_UNRESOLVED symbol, the address of the variable will then
5990 be determined from the minimal symbol table whenever the variable
5991 is referenced.
5992 The address for the partial symbol table entry is not
5993 used by GDB, but it comes in handy for debugging partial symbol
5994 table building. */
5995
5996 if (pdi->d.locdesc || pdi->has_type)
5997 add_psymbol_to_list (actual_name, strlen (actual_name),
5998 built_actual_name,
5999 VAR_DOMAIN, LOC_STATIC,
6000 &objfile->global_psymbols,
6001 0, addr + baseaddr,
6002 cu->language, objfile);
6003 }
6004 else
6005 {
6006 /* Static Variable. Skip symbols without location descriptors. */
6007 if (pdi->d.locdesc == NULL)
6008 {
6009 if (built_actual_name)
6010 xfree (actual_name);
6011 return;
6012 }
6013 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6014 mst_file_data, objfile); */
6015 add_psymbol_to_list (actual_name, strlen (actual_name),
6016 built_actual_name,
6017 VAR_DOMAIN, LOC_STATIC,
6018 &objfile->static_psymbols,
6019 0, addr + baseaddr,
6020 cu->language, objfile);
6021 }
6022 break;
6023 case DW_TAG_typedef:
6024 case DW_TAG_base_type:
6025 case DW_TAG_subrange_type:
6026 add_psymbol_to_list (actual_name, strlen (actual_name),
6027 built_actual_name,
6028 VAR_DOMAIN, LOC_TYPEDEF,
6029 &objfile->static_psymbols,
6030 0, (CORE_ADDR) 0, cu->language, objfile);
6031 break;
6032 case DW_TAG_namespace:
6033 add_psymbol_to_list (actual_name, strlen (actual_name),
6034 built_actual_name,
6035 VAR_DOMAIN, LOC_TYPEDEF,
6036 &objfile->global_psymbols,
6037 0, (CORE_ADDR) 0, cu->language, objfile);
6038 break;
6039 case DW_TAG_class_type:
6040 case DW_TAG_interface_type:
6041 case DW_TAG_structure_type:
6042 case DW_TAG_union_type:
6043 case DW_TAG_enumeration_type:
6044 /* Skip external references. The DWARF standard says in the section
6045 about "Structure, Union, and Class Type Entries": "An incomplete
6046 structure, union or class type is represented by a structure,
6047 union or class entry that does not have a byte size attribute
6048 and that has a DW_AT_declaration attribute." */
6049 if (!pdi->has_byte_size && pdi->is_declaration)
6050 {
6051 if (built_actual_name)
6052 xfree (actual_name);
6053 return;
6054 }
6055
6056 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6057 static vs. global. */
6058 add_psymbol_to_list (actual_name, strlen (actual_name),
6059 built_actual_name,
6060 STRUCT_DOMAIN, LOC_TYPEDEF,
6061 (cu->language == language_cplus
6062 || cu->language == language_java)
6063 ? &objfile->global_psymbols
6064 : &objfile->static_psymbols,
6065 0, (CORE_ADDR) 0, cu->language, objfile);
6066
6067 break;
6068 case DW_TAG_enumerator:
6069 add_psymbol_to_list (actual_name, strlen (actual_name),
6070 built_actual_name,
6071 VAR_DOMAIN, LOC_CONST,
6072 (cu->language == language_cplus
6073 || cu->language == language_java)
6074 ? &objfile->global_psymbols
6075 : &objfile->static_psymbols,
6076 0, (CORE_ADDR) 0, cu->language, objfile);
6077 break;
6078 default:
6079 break;
6080 }
6081
6082 if (built_actual_name)
6083 xfree (actual_name);
6084 }
6085
6086 /* Read a partial die corresponding to a namespace; also, add a symbol
6087 corresponding to that namespace to the symbol table. NAMESPACE is
6088 the name of the enclosing namespace. */
6089
6090 static void
6091 add_partial_namespace (struct partial_die_info *pdi,
6092 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6093 int need_pc, struct dwarf2_cu *cu)
6094 {
6095 /* Add a symbol for the namespace. */
6096
6097 add_partial_symbol (pdi, cu);
6098
6099 /* Now scan partial symbols in that namespace. */
6100
6101 if (pdi->has_children)
6102 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6103 }
6104
6105 /* Read a partial die corresponding to a Fortran module. */
6106
6107 static void
6108 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6109 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6110 {
6111 /* Now scan partial symbols in that module. */
6112
6113 if (pdi->has_children)
6114 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6115 }
6116
6117 /* Read a partial die corresponding to a subprogram and create a partial
6118 symbol for that subprogram. When the CU language allows it, this
6119 routine also defines a partial symbol for each nested subprogram
6120 that this subprogram contains.
6121
6122 DIE my also be a lexical block, in which case we simply search
6123 recursively for suprograms defined inside that lexical block.
6124 Again, this is only performed when the CU language allows this
6125 type of definitions. */
6126
6127 static void
6128 add_partial_subprogram (struct partial_die_info *pdi,
6129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6130 int need_pc, struct dwarf2_cu *cu)
6131 {
6132 if (pdi->tag == DW_TAG_subprogram)
6133 {
6134 if (pdi->has_pc_info)
6135 {
6136 if (pdi->lowpc < *lowpc)
6137 *lowpc = pdi->lowpc;
6138 if (pdi->highpc > *highpc)
6139 *highpc = pdi->highpc;
6140 if (need_pc)
6141 {
6142 CORE_ADDR baseaddr;
6143 struct objfile *objfile = cu->objfile;
6144
6145 baseaddr = ANOFFSET (objfile->section_offsets,
6146 SECT_OFF_TEXT (objfile));
6147 addrmap_set_empty (objfile->psymtabs_addrmap,
6148 pdi->lowpc + baseaddr,
6149 pdi->highpc - 1 + baseaddr,
6150 cu->per_cu->v.psymtab);
6151 }
6152 }
6153
6154 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6155 {
6156 if (!pdi->is_declaration)
6157 /* Ignore subprogram DIEs that do not have a name, they are
6158 illegal. Do not emit a complaint at this point, we will
6159 do so when we convert this psymtab into a symtab. */
6160 if (pdi->name)
6161 add_partial_symbol (pdi, cu);
6162 }
6163 }
6164
6165 if (! pdi->has_children)
6166 return;
6167
6168 if (cu->language == language_ada)
6169 {
6170 pdi = pdi->die_child;
6171 while (pdi != NULL)
6172 {
6173 fixup_partial_die (pdi, cu);
6174 if (pdi->tag == DW_TAG_subprogram
6175 || pdi->tag == DW_TAG_lexical_block)
6176 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6177 pdi = pdi->die_sibling;
6178 }
6179 }
6180 }
6181
6182 /* Read a partial die corresponding to an enumeration type. */
6183
6184 static void
6185 add_partial_enumeration (struct partial_die_info *enum_pdi,
6186 struct dwarf2_cu *cu)
6187 {
6188 struct partial_die_info *pdi;
6189
6190 if (enum_pdi->name != NULL)
6191 add_partial_symbol (enum_pdi, cu);
6192
6193 pdi = enum_pdi->die_child;
6194 while (pdi)
6195 {
6196 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6197 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6198 else
6199 add_partial_symbol (pdi, cu);
6200 pdi = pdi->die_sibling;
6201 }
6202 }
6203
6204 /* Return the initial uleb128 in the die at INFO_PTR. */
6205
6206 static unsigned int
6207 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6208 {
6209 unsigned int bytes_read;
6210
6211 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6212 }
6213
6214 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6215 Return the corresponding abbrev, or NULL if the number is zero (indicating
6216 an empty DIE). In either case *BYTES_READ will be set to the length of
6217 the initial number. */
6218
6219 static struct abbrev_info *
6220 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6221 struct dwarf2_cu *cu)
6222 {
6223 bfd *abfd = cu->objfile->obfd;
6224 unsigned int abbrev_number;
6225 struct abbrev_info *abbrev;
6226
6227 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6228
6229 if (abbrev_number == 0)
6230 return NULL;
6231
6232 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6233 if (!abbrev)
6234 {
6235 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6236 abbrev_number, bfd_get_filename (abfd));
6237 }
6238
6239 return abbrev;
6240 }
6241
6242 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6243 Returns a pointer to the end of a series of DIEs, terminated by an empty
6244 DIE. Any children of the skipped DIEs will also be skipped. */
6245
6246 static gdb_byte *
6247 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6248 {
6249 struct dwarf2_cu *cu = reader->cu;
6250 struct abbrev_info *abbrev;
6251 unsigned int bytes_read;
6252
6253 while (1)
6254 {
6255 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6256 if (abbrev == NULL)
6257 return info_ptr + bytes_read;
6258 else
6259 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6260 }
6261 }
6262
6263 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6264 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6265 abbrev corresponding to that skipped uleb128 should be passed in
6266 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6267 children. */
6268
6269 static gdb_byte *
6270 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6271 struct abbrev_info *abbrev)
6272 {
6273 unsigned int bytes_read;
6274 struct attribute attr;
6275 bfd *abfd = reader->abfd;
6276 struct dwarf2_cu *cu = reader->cu;
6277 gdb_byte *buffer = reader->buffer;
6278 const gdb_byte *buffer_end = reader->buffer_end;
6279 gdb_byte *start_info_ptr = info_ptr;
6280 unsigned int form, i;
6281
6282 for (i = 0; i < abbrev->num_attrs; i++)
6283 {
6284 /* The only abbrev we care about is DW_AT_sibling. */
6285 if (abbrev->attrs[i].name == DW_AT_sibling)
6286 {
6287 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6288 if (attr.form == DW_FORM_ref_addr)
6289 complaint (&symfile_complaints,
6290 _("ignoring absolute DW_AT_sibling"));
6291 else
6292 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6293 }
6294
6295 /* If it isn't DW_AT_sibling, skip this attribute. */
6296 form = abbrev->attrs[i].form;
6297 skip_attribute:
6298 switch (form)
6299 {
6300 case DW_FORM_ref_addr:
6301 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6302 and later it is offset sized. */
6303 if (cu->header.version == 2)
6304 info_ptr += cu->header.addr_size;
6305 else
6306 info_ptr += cu->header.offset_size;
6307 break;
6308 case DW_FORM_GNU_ref_alt:
6309 info_ptr += cu->header.offset_size;
6310 break;
6311 case DW_FORM_addr:
6312 info_ptr += cu->header.addr_size;
6313 break;
6314 case DW_FORM_data1:
6315 case DW_FORM_ref1:
6316 case DW_FORM_flag:
6317 info_ptr += 1;
6318 break;
6319 case DW_FORM_flag_present:
6320 break;
6321 case DW_FORM_data2:
6322 case DW_FORM_ref2:
6323 info_ptr += 2;
6324 break;
6325 case DW_FORM_data4:
6326 case DW_FORM_ref4:
6327 info_ptr += 4;
6328 break;
6329 case DW_FORM_data8:
6330 case DW_FORM_ref8:
6331 case DW_FORM_ref_sig8:
6332 info_ptr += 8;
6333 break;
6334 case DW_FORM_string:
6335 read_direct_string (abfd, info_ptr, &bytes_read);
6336 info_ptr += bytes_read;
6337 break;
6338 case DW_FORM_sec_offset:
6339 case DW_FORM_strp:
6340 case DW_FORM_GNU_strp_alt:
6341 info_ptr += cu->header.offset_size;
6342 break;
6343 case DW_FORM_exprloc:
6344 case DW_FORM_block:
6345 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6346 info_ptr += bytes_read;
6347 break;
6348 case DW_FORM_block1:
6349 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6350 break;
6351 case DW_FORM_block2:
6352 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6353 break;
6354 case DW_FORM_block4:
6355 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6356 break;
6357 case DW_FORM_sdata:
6358 case DW_FORM_udata:
6359 case DW_FORM_ref_udata:
6360 case DW_FORM_GNU_addr_index:
6361 case DW_FORM_GNU_str_index:
6362 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6363 break;
6364 case DW_FORM_indirect:
6365 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6366 info_ptr += bytes_read;
6367 /* We need to continue parsing from here, so just go back to
6368 the top. */
6369 goto skip_attribute;
6370
6371 default:
6372 error (_("Dwarf Error: Cannot handle %s "
6373 "in DWARF reader [in module %s]"),
6374 dwarf_form_name (form),
6375 bfd_get_filename (abfd));
6376 }
6377 }
6378
6379 if (abbrev->has_children)
6380 return skip_children (reader, info_ptr);
6381 else
6382 return info_ptr;
6383 }
6384
6385 /* Locate ORIG_PDI's sibling.
6386 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6387
6388 static gdb_byte *
6389 locate_pdi_sibling (const struct die_reader_specs *reader,
6390 struct partial_die_info *orig_pdi,
6391 gdb_byte *info_ptr)
6392 {
6393 /* Do we know the sibling already? */
6394
6395 if (orig_pdi->sibling)
6396 return orig_pdi->sibling;
6397
6398 /* Are there any children to deal with? */
6399
6400 if (!orig_pdi->has_children)
6401 return info_ptr;
6402
6403 /* Skip the children the long way. */
6404
6405 return skip_children (reader, info_ptr);
6406 }
6407
6408 /* Expand this partial symbol table into a full symbol table. */
6409
6410 static void
6411 dwarf2_psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
6412 {
6413 if (pst != NULL)
6414 {
6415 if (pst->readin)
6416 {
6417 warning (_("bug: psymtab for %s is already read in."),
6418 pst->filename);
6419 }
6420 else
6421 {
6422 if (info_verbose)
6423 {
6424 printf_filtered (_("Reading in symbols for %s..."),
6425 pst->filename);
6426 gdb_flush (gdb_stdout);
6427 }
6428
6429 /* Restore our global data. */
6430 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6431
6432 /* If this psymtab is constructed from a debug-only objfile, the
6433 has_section_at_zero flag will not necessarily be correct. We
6434 can get the correct value for this flag by looking at the data
6435 associated with the (presumably stripped) associated objfile. */
6436 if (objfile->separate_debug_objfile_backlink)
6437 {
6438 struct dwarf2_per_objfile *dpo_backlink
6439 = objfile_data (objfile->separate_debug_objfile_backlink,
6440 dwarf2_objfile_data_key);
6441
6442 dwarf2_per_objfile->has_section_at_zero
6443 = dpo_backlink->has_section_at_zero;
6444 }
6445
6446 dwarf2_per_objfile->reading_partial_symbols = 0;
6447
6448 psymtab_to_symtab_1 (pst);
6449
6450 /* Finish up the debug error message. */
6451 if (info_verbose)
6452 printf_filtered (_("done.\n"));
6453 }
6454 }
6455
6456 process_cu_includes ();
6457 }
6458 \f
6459 /* Reading in full CUs. */
6460
6461 /* Add PER_CU to the queue. */
6462
6463 static void
6464 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6465 enum language pretend_language)
6466 {
6467 struct dwarf2_queue_item *item;
6468
6469 per_cu->queued = 1;
6470 item = xmalloc (sizeof (*item));
6471 item->per_cu = per_cu;
6472 item->pretend_language = pretend_language;
6473 item->next = NULL;
6474
6475 if (dwarf2_queue == NULL)
6476 dwarf2_queue = item;
6477 else
6478 dwarf2_queue_tail->next = item;
6479
6480 dwarf2_queue_tail = item;
6481 }
6482
6483 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6484 unit and add it to our queue.
6485 The result is non-zero if PER_CU was queued, otherwise the result is zero
6486 meaning either PER_CU is already queued or it is already loaded. */
6487
6488 static int
6489 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6490 struct dwarf2_per_cu_data *per_cu,
6491 enum language pretend_language)
6492 {
6493 /* We may arrive here during partial symbol reading, if we need full
6494 DIEs to process an unusual case (e.g. template arguments). Do
6495 not queue PER_CU, just tell our caller to load its DIEs. */
6496 if (dwarf2_per_objfile->reading_partial_symbols)
6497 {
6498 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6499 return 1;
6500 return 0;
6501 }
6502
6503 /* Mark the dependence relation so that we don't flush PER_CU
6504 too early. */
6505 dwarf2_add_dependence (this_cu, per_cu);
6506
6507 /* If it's already on the queue, we have nothing to do. */
6508 if (per_cu->queued)
6509 return 0;
6510
6511 /* If the compilation unit is already loaded, just mark it as
6512 used. */
6513 if (per_cu->cu != NULL)
6514 {
6515 per_cu->cu->last_used = 0;
6516 return 0;
6517 }
6518
6519 /* Add it to the queue. */
6520 queue_comp_unit (per_cu, pretend_language);
6521
6522 return 1;
6523 }
6524
6525 /* Process the queue. */
6526
6527 static void
6528 process_queue (void)
6529 {
6530 struct dwarf2_queue_item *item, *next_item;
6531
6532 if (dwarf2_read_debug)
6533 {
6534 fprintf_unfiltered (gdb_stdlog,
6535 "Expanding one or more symtabs of objfile %s ...\n",
6536 dwarf2_per_objfile->objfile->name);
6537 }
6538
6539 /* The queue starts out with one item, but following a DIE reference
6540 may load a new CU, adding it to the end of the queue. */
6541 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6542 {
6543 if (dwarf2_per_objfile->using_index
6544 ? !item->per_cu->v.quick->symtab
6545 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6546 {
6547 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6548
6549 if (dwarf2_read_debug)
6550 {
6551 fprintf_unfiltered (gdb_stdlog,
6552 "Expanding symtab of %s at offset 0x%x\n",
6553 per_cu->is_debug_types ? "TU" : "CU",
6554 per_cu->offset.sect_off);
6555 }
6556
6557 if (per_cu->is_debug_types)
6558 process_full_type_unit (per_cu, item->pretend_language);
6559 else
6560 process_full_comp_unit (per_cu, item->pretend_language);
6561
6562 if (dwarf2_read_debug)
6563 {
6564 fprintf_unfiltered (gdb_stdlog,
6565 "Done expanding %s at offset 0x%x\n",
6566 per_cu->is_debug_types ? "TU" : "CU",
6567 per_cu->offset.sect_off);
6568 }
6569 }
6570
6571 item->per_cu->queued = 0;
6572 next_item = item->next;
6573 xfree (item);
6574 }
6575
6576 dwarf2_queue_tail = NULL;
6577
6578 if (dwarf2_read_debug)
6579 {
6580 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6581 dwarf2_per_objfile->objfile->name);
6582 }
6583 }
6584
6585 /* Free all allocated queue entries. This function only releases anything if
6586 an error was thrown; if the queue was processed then it would have been
6587 freed as we went along. */
6588
6589 static void
6590 dwarf2_release_queue (void *dummy)
6591 {
6592 struct dwarf2_queue_item *item, *last;
6593
6594 item = dwarf2_queue;
6595 while (item)
6596 {
6597 /* Anything still marked queued is likely to be in an
6598 inconsistent state, so discard it. */
6599 if (item->per_cu->queued)
6600 {
6601 if (item->per_cu->cu != NULL)
6602 free_one_cached_comp_unit (item->per_cu);
6603 item->per_cu->queued = 0;
6604 }
6605
6606 last = item;
6607 item = item->next;
6608 xfree (last);
6609 }
6610
6611 dwarf2_queue = dwarf2_queue_tail = NULL;
6612 }
6613
6614 /* Read in full symbols for PST, and anything it depends on. */
6615
6616 static void
6617 psymtab_to_symtab_1 (struct partial_symtab *pst)
6618 {
6619 struct dwarf2_per_cu_data *per_cu;
6620 int i;
6621
6622 if (pst->readin)
6623 return;
6624
6625 for (i = 0; i < pst->number_of_dependencies; i++)
6626 if (!pst->dependencies[i]->readin
6627 && pst->dependencies[i]->user == NULL)
6628 {
6629 /* Inform about additional files that need to be read in. */
6630 if (info_verbose)
6631 {
6632 /* FIXME: i18n: Need to make this a single string. */
6633 fputs_filtered (" ", gdb_stdout);
6634 wrap_here ("");
6635 fputs_filtered ("and ", gdb_stdout);
6636 wrap_here ("");
6637 printf_filtered ("%s...", pst->dependencies[i]->filename);
6638 wrap_here (""); /* Flush output. */
6639 gdb_flush (gdb_stdout);
6640 }
6641 psymtab_to_symtab_1 (pst->dependencies[i]);
6642 }
6643
6644 per_cu = pst->read_symtab_private;
6645
6646 if (per_cu == NULL)
6647 {
6648 /* It's an include file, no symbols to read for it.
6649 Everything is in the parent symtab. */
6650 pst->readin = 1;
6651 return;
6652 }
6653
6654 dw2_do_instantiate_symtab (per_cu);
6655 }
6656
6657 /* Trivial hash function for die_info: the hash value of a DIE
6658 is its offset in .debug_info for this objfile. */
6659
6660 static hashval_t
6661 die_hash (const void *item)
6662 {
6663 const struct die_info *die = item;
6664
6665 return die->offset.sect_off;
6666 }
6667
6668 /* Trivial comparison function for die_info structures: two DIEs
6669 are equal if they have the same offset. */
6670
6671 static int
6672 die_eq (const void *item_lhs, const void *item_rhs)
6673 {
6674 const struct die_info *die_lhs = item_lhs;
6675 const struct die_info *die_rhs = item_rhs;
6676
6677 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6678 }
6679
6680 /* die_reader_func for load_full_comp_unit.
6681 This is identical to read_signatured_type_reader,
6682 but is kept separate for now. */
6683
6684 static void
6685 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6686 gdb_byte *info_ptr,
6687 struct die_info *comp_unit_die,
6688 int has_children,
6689 void *data)
6690 {
6691 struct dwarf2_cu *cu = reader->cu;
6692 enum language *language_ptr = data;
6693
6694 gdb_assert (cu->die_hash == NULL);
6695 cu->die_hash =
6696 htab_create_alloc_ex (cu->header.length / 12,
6697 die_hash,
6698 die_eq,
6699 NULL,
6700 &cu->comp_unit_obstack,
6701 hashtab_obstack_allocate,
6702 dummy_obstack_deallocate);
6703
6704 if (has_children)
6705 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6706 &info_ptr, comp_unit_die);
6707 cu->dies = comp_unit_die;
6708 /* comp_unit_die is not stored in die_hash, no need. */
6709
6710 /* We try not to read any attributes in this function, because not
6711 all CUs needed for references have been loaded yet, and symbol
6712 table processing isn't initialized. But we have to set the CU language,
6713 or we won't be able to build types correctly.
6714 Similarly, if we do not read the producer, we can not apply
6715 producer-specific interpretation. */
6716 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6717 }
6718
6719 /* Load the DIEs associated with PER_CU into memory. */
6720
6721 static void
6722 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6723 enum language pretend_language)
6724 {
6725 gdb_assert (! this_cu->is_debug_types);
6726
6727 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6728 load_full_comp_unit_reader, &pretend_language);
6729 }
6730
6731 /* Add a DIE to the delayed physname list. */
6732
6733 static void
6734 add_to_method_list (struct type *type, int fnfield_index, int index,
6735 const char *name, struct die_info *die,
6736 struct dwarf2_cu *cu)
6737 {
6738 struct delayed_method_info mi;
6739 mi.type = type;
6740 mi.fnfield_index = fnfield_index;
6741 mi.index = index;
6742 mi.name = name;
6743 mi.die = die;
6744 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6745 }
6746
6747 /* A cleanup for freeing the delayed method list. */
6748
6749 static void
6750 free_delayed_list (void *ptr)
6751 {
6752 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6753 if (cu->method_list != NULL)
6754 {
6755 VEC_free (delayed_method_info, cu->method_list);
6756 cu->method_list = NULL;
6757 }
6758 }
6759
6760 /* Compute the physnames of any methods on the CU's method list.
6761
6762 The computation of method physnames is delayed in order to avoid the
6763 (bad) condition that one of the method's formal parameters is of an as yet
6764 incomplete type. */
6765
6766 static void
6767 compute_delayed_physnames (struct dwarf2_cu *cu)
6768 {
6769 int i;
6770 struct delayed_method_info *mi;
6771 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6772 {
6773 const char *physname;
6774 struct fn_fieldlist *fn_flp
6775 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6776 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6777 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6778 }
6779 }
6780
6781 /* Go objects should be embedded in a DW_TAG_module DIE,
6782 and it's not clear if/how imported objects will appear.
6783 To keep Go support simple until that's worked out,
6784 go back through what we've read and create something usable.
6785 We could do this while processing each DIE, and feels kinda cleaner,
6786 but that way is more invasive.
6787 This is to, for example, allow the user to type "p var" or "b main"
6788 without having to specify the package name, and allow lookups
6789 of module.object to work in contexts that use the expression
6790 parser. */
6791
6792 static void
6793 fixup_go_packaging (struct dwarf2_cu *cu)
6794 {
6795 char *package_name = NULL;
6796 struct pending *list;
6797 int i;
6798
6799 for (list = global_symbols; list != NULL; list = list->next)
6800 {
6801 for (i = 0; i < list->nsyms; ++i)
6802 {
6803 struct symbol *sym = list->symbol[i];
6804
6805 if (SYMBOL_LANGUAGE (sym) == language_go
6806 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6807 {
6808 char *this_package_name = go_symbol_package_name (sym);
6809
6810 if (this_package_name == NULL)
6811 continue;
6812 if (package_name == NULL)
6813 package_name = this_package_name;
6814 else
6815 {
6816 if (strcmp (package_name, this_package_name) != 0)
6817 complaint (&symfile_complaints,
6818 _("Symtab %s has objects from two different Go packages: %s and %s"),
6819 (SYMBOL_SYMTAB (sym)
6820 ? SYMBOL_SYMTAB (sym)->filename
6821 : cu->objfile->name),
6822 this_package_name, package_name);
6823 xfree (this_package_name);
6824 }
6825 }
6826 }
6827 }
6828
6829 if (package_name != NULL)
6830 {
6831 struct objfile *objfile = cu->objfile;
6832 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6833 package_name, objfile);
6834 struct symbol *sym;
6835
6836 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6837
6838 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6839 SYMBOL_SET_LANGUAGE (sym, language_go);
6840 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6841 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6842 e.g., "main" finds the "main" module and not C's main(). */
6843 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6844 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6845 SYMBOL_TYPE (sym) = type;
6846
6847 add_symbol_to_list (sym, &global_symbols);
6848
6849 xfree (package_name);
6850 }
6851 }
6852
6853 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6854
6855 /* Return the symtab for PER_CU. This works properly regardless of
6856 whether we're using the index or psymtabs. */
6857
6858 static struct symtab *
6859 get_symtab (struct dwarf2_per_cu_data *per_cu)
6860 {
6861 return (dwarf2_per_objfile->using_index
6862 ? per_cu->v.quick->symtab
6863 : per_cu->v.psymtab->symtab);
6864 }
6865
6866 /* A helper function for computing the list of all symbol tables
6867 included by PER_CU. */
6868
6869 static void
6870 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6871 htab_t all_children,
6872 struct dwarf2_per_cu_data *per_cu)
6873 {
6874 void **slot;
6875 int ix;
6876 struct dwarf2_per_cu_data *iter;
6877
6878 slot = htab_find_slot (all_children, per_cu, INSERT);
6879 if (*slot != NULL)
6880 {
6881 /* This inclusion and its children have been processed. */
6882 return;
6883 }
6884
6885 *slot = per_cu;
6886 /* Only add a CU if it has a symbol table. */
6887 if (get_symtab (per_cu) != NULL)
6888 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6889
6890 for (ix = 0;
6891 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6892 ++ix)
6893 recursively_compute_inclusions (result, all_children, iter);
6894 }
6895
6896 /* Compute the symtab 'includes' fields for the symtab related to
6897 PER_CU. */
6898
6899 static void
6900 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6901 {
6902 gdb_assert (! per_cu->is_debug_types);
6903
6904 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6905 {
6906 int ix, len;
6907 struct dwarf2_per_cu_data *iter;
6908 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6909 htab_t all_children;
6910 struct symtab *symtab = get_symtab (per_cu);
6911
6912 /* If we don't have a symtab, we can just skip this case. */
6913 if (symtab == NULL)
6914 return;
6915
6916 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6917 NULL, xcalloc, xfree);
6918
6919 for (ix = 0;
6920 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6921 ix, iter);
6922 ++ix)
6923 recursively_compute_inclusions (&result_children, all_children, iter);
6924
6925 /* Now we have a transitive closure of all the included CUs, so
6926 we can convert it to a list of symtabs. */
6927 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6928 symtab->includes
6929 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6930 (len + 1) * sizeof (struct symtab *));
6931 for (ix = 0;
6932 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6933 ++ix)
6934 symtab->includes[ix] = get_symtab (iter);
6935 symtab->includes[len] = NULL;
6936
6937 VEC_free (dwarf2_per_cu_ptr, result_children);
6938 htab_delete (all_children);
6939 }
6940 }
6941
6942 /* Compute the 'includes' field for the symtabs of all the CUs we just
6943 read. */
6944
6945 static void
6946 process_cu_includes (void)
6947 {
6948 int ix;
6949 struct dwarf2_per_cu_data *iter;
6950
6951 for (ix = 0;
6952 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6953 ix, iter);
6954 ++ix)
6955 {
6956 if (! iter->is_debug_types)
6957 compute_symtab_includes (iter);
6958 }
6959
6960 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6961 }
6962
6963 /* Generate full symbol information for PER_CU, whose DIEs have
6964 already been loaded into memory. */
6965
6966 static void
6967 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6968 enum language pretend_language)
6969 {
6970 struct dwarf2_cu *cu = per_cu->cu;
6971 struct objfile *objfile = per_cu->objfile;
6972 CORE_ADDR lowpc, highpc;
6973 struct symtab *symtab;
6974 struct cleanup *back_to, *delayed_list_cleanup;
6975 CORE_ADDR baseaddr;
6976 struct block *static_block;
6977
6978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6979
6980 buildsym_init ();
6981 back_to = make_cleanup (really_free_pendings, NULL);
6982 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6983
6984 cu->list_in_scope = &file_symbols;
6985
6986 cu->language = pretend_language;
6987 cu->language_defn = language_def (cu->language);
6988
6989 /* Do line number decoding in read_file_scope () */
6990 process_die (cu->dies, cu);
6991
6992 /* For now fudge the Go package. */
6993 if (cu->language == language_go)
6994 fixup_go_packaging (cu);
6995
6996 /* Now that we have processed all the DIEs in the CU, all the types
6997 should be complete, and it should now be safe to compute all of the
6998 physnames. */
6999 compute_delayed_physnames (cu);
7000 do_cleanups (delayed_list_cleanup);
7001
7002 /* Some compilers don't define a DW_AT_high_pc attribute for the
7003 compilation unit. If the DW_AT_high_pc is missing, synthesize
7004 it, by scanning the DIE's below the compilation unit. */
7005 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7006
7007 static_block
7008 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7009 per_cu->s.imported_symtabs != NULL);
7010
7011 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7012 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7013 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7014 addrmap to help ensure it has an accurate map of pc values belonging to
7015 this comp unit. */
7016 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7017
7018 symtab = end_symtab_from_static_block (static_block, objfile,
7019 SECT_OFF_TEXT (objfile), 0);
7020
7021 if (symtab != NULL)
7022 {
7023 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7024
7025 /* Set symtab language to language from DW_AT_language. If the
7026 compilation is from a C file generated by language preprocessors, do
7027 not set the language if it was already deduced by start_subfile. */
7028 if (!(cu->language == language_c && symtab->language != language_c))
7029 symtab->language = cu->language;
7030
7031 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7032 produce DW_AT_location with location lists but it can be possibly
7033 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7034 there were bugs in prologue debug info, fixed later in GCC-4.5
7035 by "unwind info for epilogues" patch (which is not directly related).
7036
7037 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7038 needed, it would be wrong due to missing DW_AT_producer there.
7039
7040 Still one can confuse GDB by using non-standard GCC compilation
7041 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7042 */
7043 if (cu->has_loclist && gcc_4_minor >= 5)
7044 symtab->locations_valid = 1;
7045
7046 if (gcc_4_minor >= 5)
7047 symtab->epilogue_unwind_valid = 1;
7048
7049 symtab->call_site_htab = cu->call_site_htab;
7050 }
7051
7052 if (dwarf2_per_objfile->using_index)
7053 per_cu->v.quick->symtab = symtab;
7054 else
7055 {
7056 struct partial_symtab *pst = per_cu->v.psymtab;
7057 pst->symtab = symtab;
7058 pst->readin = 1;
7059 }
7060
7061 /* Push it for inclusion processing later. */
7062 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7063
7064 do_cleanups (back_to);
7065 }
7066
7067 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7068 already been loaded into memory. */
7069
7070 static void
7071 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7072 enum language pretend_language)
7073 {
7074 struct dwarf2_cu *cu = per_cu->cu;
7075 struct objfile *objfile = per_cu->objfile;
7076 struct symtab *symtab;
7077 struct cleanup *back_to, *delayed_list_cleanup;
7078
7079 buildsym_init ();
7080 back_to = make_cleanup (really_free_pendings, NULL);
7081 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7082
7083 cu->list_in_scope = &file_symbols;
7084
7085 cu->language = pretend_language;
7086 cu->language_defn = language_def (cu->language);
7087
7088 /* The symbol tables are set up in read_type_unit_scope. */
7089 process_die (cu->dies, cu);
7090
7091 /* For now fudge the Go package. */
7092 if (cu->language == language_go)
7093 fixup_go_packaging (cu);
7094
7095 /* Now that we have processed all the DIEs in the CU, all the types
7096 should be complete, and it should now be safe to compute all of the
7097 physnames. */
7098 compute_delayed_physnames (cu);
7099 do_cleanups (delayed_list_cleanup);
7100
7101 /* TUs share symbol tables.
7102 If this is the first TU to use this symtab, complete the construction
7103 of it with end_expandable_symtab. Otherwise, complete the addition of
7104 this TU's symbols to the existing symtab. */
7105 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7106 {
7107 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7108 per_cu->s.type_unit_group->primary_symtab = symtab;
7109
7110 if (symtab != NULL)
7111 {
7112 /* Set symtab language to language from DW_AT_language. If the
7113 compilation is from a C file generated by language preprocessors,
7114 do not set the language if it was already deduced by
7115 start_subfile. */
7116 if (!(cu->language == language_c && symtab->language != language_c))
7117 symtab->language = cu->language;
7118 }
7119 }
7120 else
7121 {
7122 augment_type_symtab (objfile,
7123 per_cu->s.type_unit_group->primary_symtab);
7124 symtab = per_cu->s.type_unit_group->primary_symtab;
7125 }
7126
7127 if (dwarf2_per_objfile->using_index)
7128 per_cu->v.quick->symtab = symtab;
7129 else
7130 {
7131 struct partial_symtab *pst = per_cu->v.psymtab;
7132 pst->symtab = symtab;
7133 pst->readin = 1;
7134 }
7135
7136 do_cleanups (back_to);
7137 }
7138
7139 /* Process an imported unit DIE. */
7140
7141 static void
7142 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7143 {
7144 struct attribute *attr;
7145
7146 /* For now we don't handle imported units in type units. */
7147 if (cu->per_cu->is_debug_types)
7148 {
7149 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7150 " supported in type units [in module %s]"),
7151 cu->objfile->name);
7152 }
7153
7154 attr = dwarf2_attr (die, DW_AT_import, cu);
7155 if (attr != NULL)
7156 {
7157 struct dwarf2_per_cu_data *per_cu;
7158 struct symtab *imported_symtab;
7159 sect_offset offset;
7160 int is_dwz;
7161
7162 offset = dwarf2_get_ref_die_offset (attr);
7163 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7164 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7165
7166 /* Queue the unit, if needed. */
7167 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7168 load_full_comp_unit (per_cu, cu->language);
7169
7170 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7171 per_cu);
7172 }
7173 }
7174
7175 /* Process a die and its children. */
7176
7177 static void
7178 process_die (struct die_info *die, struct dwarf2_cu *cu)
7179 {
7180 switch (die->tag)
7181 {
7182 case DW_TAG_padding:
7183 break;
7184 case DW_TAG_compile_unit:
7185 case DW_TAG_partial_unit:
7186 read_file_scope (die, cu);
7187 break;
7188 case DW_TAG_type_unit:
7189 read_type_unit_scope (die, cu);
7190 break;
7191 case DW_TAG_subprogram:
7192 case DW_TAG_inlined_subroutine:
7193 read_func_scope (die, cu);
7194 break;
7195 case DW_TAG_lexical_block:
7196 case DW_TAG_try_block:
7197 case DW_TAG_catch_block:
7198 read_lexical_block_scope (die, cu);
7199 break;
7200 case DW_TAG_GNU_call_site:
7201 read_call_site_scope (die, cu);
7202 break;
7203 case DW_TAG_class_type:
7204 case DW_TAG_interface_type:
7205 case DW_TAG_structure_type:
7206 case DW_TAG_union_type:
7207 process_structure_scope (die, cu);
7208 break;
7209 case DW_TAG_enumeration_type:
7210 process_enumeration_scope (die, cu);
7211 break;
7212
7213 /* These dies have a type, but processing them does not create
7214 a symbol or recurse to process the children. Therefore we can
7215 read them on-demand through read_type_die. */
7216 case DW_TAG_subroutine_type:
7217 case DW_TAG_set_type:
7218 case DW_TAG_array_type:
7219 case DW_TAG_pointer_type:
7220 case DW_TAG_ptr_to_member_type:
7221 case DW_TAG_reference_type:
7222 case DW_TAG_string_type:
7223 break;
7224
7225 case DW_TAG_base_type:
7226 case DW_TAG_subrange_type:
7227 case DW_TAG_typedef:
7228 /* Add a typedef symbol for the type definition, if it has a
7229 DW_AT_name. */
7230 new_symbol (die, read_type_die (die, cu), cu);
7231 break;
7232 case DW_TAG_common_block:
7233 read_common_block (die, cu);
7234 break;
7235 case DW_TAG_common_inclusion:
7236 break;
7237 case DW_TAG_namespace:
7238 processing_has_namespace_info = 1;
7239 read_namespace (die, cu);
7240 break;
7241 case DW_TAG_module:
7242 processing_has_namespace_info = 1;
7243 read_module (die, cu);
7244 break;
7245 case DW_TAG_imported_declaration:
7246 case DW_TAG_imported_module:
7247 processing_has_namespace_info = 1;
7248 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7249 || cu->language != language_fortran))
7250 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7251 dwarf_tag_name (die->tag));
7252 read_import_statement (die, cu);
7253 break;
7254
7255 case DW_TAG_imported_unit:
7256 process_imported_unit_die (die, cu);
7257 break;
7258
7259 default:
7260 new_symbol (die, NULL, cu);
7261 break;
7262 }
7263 }
7264
7265 /* A helper function for dwarf2_compute_name which determines whether DIE
7266 needs to have the name of the scope prepended to the name listed in the
7267 die. */
7268
7269 static int
7270 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7271 {
7272 struct attribute *attr;
7273
7274 switch (die->tag)
7275 {
7276 case DW_TAG_namespace:
7277 case DW_TAG_typedef:
7278 case DW_TAG_class_type:
7279 case DW_TAG_interface_type:
7280 case DW_TAG_structure_type:
7281 case DW_TAG_union_type:
7282 case DW_TAG_enumeration_type:
7283 case DW_TAG_enumerator:
7284 case DW_TAG_subprogram:
7285 case DW_TAG_member:
7286 return 1;
7287
7288 case DW_TAG_variable:
7289 case DW_TAG_constant:
7290 /* We only need to prefix "globally" visible variables. These include
7291 any variable marked with DW_AT_external or any variable that
7292 lives in a namespace. [Variables in anonymous namespaces
7293 require prefixing, but they are not DW_AT_external.] */
7294
7295 if (dwarf2_attr (die, DW_AT_specification, cu))
7296 {
7297 struct dwarf2_cu *spec_cu = cu;
7298
7299 return die_needs_namespace (die_specification (die, &spec_cu),
7300 spec_cu);
7301 }
7302
7303 attr = dwarf2_attr (die, DW_AT_external, cu);
7304 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7305 && die->parent->tag != DW_TAG_module)
7306 return 0;
7307 /* A variable in a lexical block of some kind does not need a
7308 namespace, even though in C++ such variables may be external
7309 and have a mangled name. */
7310 if (die->parent->tag == DW_TAG_lexical_block
7311 || die->parent->tag == DW_TAG_try_block
7312 || die->parent->tag == DW_TAG_catch_block
7313 || die->parent->tag == DW_TAG_subprogram)
7314 return 0;
7315 return 1;
7316
7317 default:
7318 return 0;
7319 }
7320 }
7321
7322 /* Retrieve the last character from a mem_file. */
7323
7324 static void
7325 do_ui_file_peek_last (void *object, const char *buffer, long length)
7326 {
7327 char *last_char_p = (char *) object;
7328
7329 if (length > 0)
7330 *last_char_p = buffer[length - 1];
7331 }
7332
7333 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7334 compute the physname for the object, which include a method's:
7335 - formal parameters (C++/Java),
7336 - receiver type (Go),
7337 - return type (Java).
7338
7339 The term "physname" is a bit confusing.
7340 For C++, for example, it is the demangled name.
7341 For Go, for example, it's the mangled name.
7342
7343 For Ada, return the DIE's linkage name rather than the fully qualified
7344 name. PHYSNAME is ignored..
7345
7346 The result is allocated on the objfile_obstack and canonicalized. */
7347
7348 static const char *
7349 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7350 int physname)
7351 {
7352 struct objfile *objfile = cu->objfile;
7353
7354 if (name == NULL)
7355 name = dwarf2_name (die, cu);
7356
7357 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7358 compute it by typename_concat inside GDB. */
7359 if (cu->language == language_ada
7360 || (cu->language == language_fortran && physname))
7361 {
7362 /* For Ada unit, we prefer the linkage name over the name, as
7363 the former contains the exported name, which the user expects
7364 to be able to reference. Ideally, we want the user to be able
7365 to reference this entity using either natural or linkage name,
7366 but we haven't started looking at this enhancement yet. */
7367 struct attribute *attr;
7368
7369 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7370 if (attr == NULL)
7371 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7372 if (attr && DW_STRING (attr))
7373 return DW_STRING (attr);
7374 }
7375
7376 /* These are the only languages we know how to qualify names in. */
7377 if (name != NULL
7378 && (cu->language == language_cplus || cu->language == language_java
7379 || cu->language == language_fortran))
7380 {
7381 if (die_needs_namespace (die, cu))
7382 {
7383 long length;
7384 const char *prefix;
7385 struct ui_file *buf;
7386
7387 prefix = determine_prefix (die, cu);
7388 buf = mem_fileopen ();
7389 if (*prefix != '\0')
7390 {
7391 char *prefixed_name = typename_concat (NULL, prefix, name,
7392 physname, cu);
7393
7394 fputs_unfiltered (prefixed_name, buf);
7395 xfree (prefixed_name);
7396 }
7397 else
7398 fputs_unfiltered (name, buf);
7399
7400 /* Template parameters may be specified in the DIE's DW_AT_name, or
7401 as children with DW_TAG_template_type_param or
7402 DW_TAG_value_type_param. If the latter, add them to the name
7403 here. If the name already has template parameters, then
7404 skip this step; some versions of GCC emit both, and
7405 it is more efficient to use the pre-computed name.
7406
7407 Something to keep in mind about this process: it is very
7408 unlikely, or in some cases downright impossible, to produce
7409 something that will match the mangled name of a function.
7410 If the definition of the function has the same debug info,
7411 we should be able to match up with it anyway. But fallbacks
7412 using the minimal symbol, for instance to find a method
7413 implemented in a stripped copy of libstdc++, will not work.
7414 If we do not have debug info for the definition, we will have to
7415 match them up some other way.
7416
7417 When we do name matching there is a related problem with function
7418 templates; two instantiated function templates are allowed to
7419 differ only by their return types, which we do not add here. */
7420
7421 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7422 {
7423 struct attribute *attr;
7424 struct die_info *child;
7425 int first = 1;
7426
7427 die->building_fullname = 1;
7428
7429 for (child = die->child; child != NULL; child = child->sibling)
7430 {
7431 struct type *type;
7432 LONGEST value;
7433 gdb_byte *bytes;
7434 struct dwarf2_locexpr_baton *baton;
7435 struct value *v;
7436
7437 if (child->tag != DW_TAG_template_type_param
7438 && child->tag != DW_TAG_template_value_param)
7439 continue;
7440
7441 if (first)
7442 {
7443 fputs_unfiltered ("<", buf);
7444 first = 0;
7445 }
7446 else
7447 fputs_unfiltered (", ", buf);
7448
7449 attr = dwarf2_attr (child, DW_AT_type, cu);
7450 if (attr == NULL)
7451 {
7452 complaint (&symfile_complaints,
7453 _("template parameter missing DW_AT_type"));
7454 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7455 continue;
7456 }
7457 type = die_type (child, cu);
7458
7459 if (child->tag == DW_TAG_template_type_param)
7460 {
7461 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7462 continue;
7463 }
7464
7465 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7466 if (attr == NULL)
7467 {
7468 complaint (&symfile_complaints,
7469 _("template parameter missing "
7470 "DW_AT_const_value"));
7471 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7472 continue;
7473 }
7474
7475 dwarf2_const_value_attr (attr, type, name,
7476 &cu->comp_unit_obstack, cu,
7477 &value, &bytes, &baton);
7478
7479 if (TYPE_NOSIGN (type))
7480 /* GDB prints characters as NUMBER 'CHAR'. If that's
7481 changed, this can use value_print instead. */
7482 c_printchar (value, type, buf);
7483 else
7484 {
7485 struct value_print_options opts;
7486
7487 if (baton != NULL)
7488 v = dwarf2_evaluate_loc_desc (type, NULL,
7489 baton->data,
7490 baton->size,
7491 baton->per_cu);
7492 else if (bytes != NULL)
7493 {
7494 v = allocate_value (type);
7495 memcpy (value_contents_writeable (v), bytes,
7496 TYPE_LENGTH (type));
7497 }
7498 else
7499 v = value_from_longest (type, value);
7500
7501 /* Specify decimal so that we do not depend on
7502 the radix. */
7503 get_formatted_print_options (&opts, 'd');
7504 opts.raw = 1;
7505 value_print (v, buf, &opts);
7506 release_value (v);
7507 value_free (v);
7508 }
7509 }
7510
7511 die->building_fullname = 0;
7512
7513 if (!first)
7514 {
7515 /* Close the argument list, with a space if necessary
7516 (nested templates). */
7517 char last_char = '\0';
7518 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7519 if (last_char == '>')
7520 fputs_unfiltered (" >", buf);
7521 else
7522 fputs_unfiltered (">", buf);
7523 }
7524 }
7525
7526 /* For Java and C++ methods, append formal parameter type
7527 information, if PHYSNAME. */
7528
7529 if (physname && die->tag == DW_TAG_subprogram
7530 && (cu->language == language_cplus
7531 || cu->language == language_java))
7532 {
7533 struct type *type = read_type_die (die, cu);
7534
7535 c_type_print_args (type, buf, 1, cu->language,
7536 &type_print_raw_options);
7537
7538 if (cu->language == language_java)
7539 {
7540 /* For java, we must append the return type to method
7541 names. */
7542 if (die->tag == DW_TAG_subprogram)
7543 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7544 0, 0, &type_print_raw_options);
7545 }
7546 else if (cu->language == language_cplus)
7547 {
7548 /* Assume that an artificial first parameter is
7549 "this", but do not crash if it is not. RealView
7550 marks unnamed (and thus unused) parameters as
7551 artificial; there is no way to differentiate
7552 the two cases. */
7553 if (TYPE_NFIELDS (type) > 0
7554 && TYPE_FIELD_ARTIFICIAL (type, 0)
7555 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7556 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7557 0))))
7558 fputs_unfiltered (" const", buf);
7559 }
7560 }
7561
7562 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7563 &length);
7564 ui_file_delete (buf);
7565
7566 if (cu->language == language_cplus)
7567 {
7568 char *cname
7569 = dwarf2_canonicalize_name (name, cu,
7570 &objfile->objfile_obstack);
7571
7572 if (cname != NULL)
7573 name = cname;
7574 }
7575 }
7576 }
7577
7578 return name;
7579 }
7580
7581 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7582 If scope qualifiers are appropriate they will be added. The result
7583 will be allocated on the objfile_obstack, or NULL if the DIE does
7584 not have a name. NAME may either be from a previous call to
7585 dwarf2_name or NULL.
7586
7587 The output string will be canonicalized (if C++/Java). */
7588
7589 static const char *
7590 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7591 {
7592 return dwarf2_compute_name (name, die, cu, 0);
7593 }
7594
7595 /* Construct a physname for the given DIE in CU. NAME may either be
7596 from a previous call to dwarf2_name or NULL. The result will be
7597 allocated on the objfile_objstack or NULL if the DIE does not have a
7598 name.
7599
7600 The output string will be canonicalized (if C++/Java). */
7601
7602 static const char *
7603 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7604 {
7605 struct objfile *objfile = cu->objfile;
7606 struct attribute *attr;
7607 const char *retval, *mangled = NULL, *canon = NULL;
7608 struct cleanup *back_to;
7609 int need_copy = 1;
7610
7611 /* In this case dwarf2_compute_name is just a shortcut not building anything
7612 on its own. */
7613 if (!die_needs_namespace (die, cu))
7614 return dwarf2_compute_name (name, die, cu, 1);
7615
7616 back_to = make_cleanup (null_cleanup, NULL);
7617
7618 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7619 if (!attr)
7620 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7621
7622 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7623 has computed. */
7624 if (attr && DW_STRING (attr))
7625 {
7626 char *demangled;
7627
7628 mangled = DW_STRING (attr);
7629
7630 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7631 type. It is easier for GDB users to search for such functions as
7632 `name(params)' than `long name(params)'. In such case the minimal
7633 symbol names do not match the full symbol names but for template
7634 functions there is never a need to look up their definition from their
7635 declaration so the only disadvantage remains the minimal symbol
7636 variant `long name(params)' does not have the proper inferior type.
7637 */
7638
7639 if (cu->language == language_go)
7640 {
7641 /* This is a lie, but we already lie to the caller new_symbol_full.
7642 new_symbol_full assumes we return the mangled name.
7643 This just undoes that lie until things are cleaned up. */
7644 demangled = NULL;
7645 }
7646 else
7647 {
7648 demangled = cplus_demangle (mangled,
7649 (DMGL_PARAMS | DMGL_ANSI
7650 | (cu->language == language_java
7651 ? DMGL_JAVA | DMGL_RET_POSTFIX
7652 : DMGL_RET_DROP)));
7653 }
7654 if (demangled)
7655 {
7656 make_cleanup (xfree, demangled);
7657 canon = demangled;
7658 }
7659 else
7660 {
7661 canon = mangled;
7662 need_copy = 0;
7663 }
7664 }
7665
7666 if (canon == NULL || check_physname)
7667 {
7668 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7669
7670 if (canon != NULL && strcmp (physname, canon) != 0)
7671 {
7672 /* It may not mean a bug in GDB. The compiler could also
7673 compute DW_AT_linkage_name incorrectly. But in such case
7674 GDB would need to be bug-to-bug compatible. */
7675
7676 complaint (&symfile_complaints,
7677 _("Computed physname <%s> does not match demangled <%s> "
7678 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7679 physname, canon, mangled, die->offset.sect_off, objfile->name);
7680
7681 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7682 is available here - over computed PHYSNAME. It is safer
7683 against both buggy GDB and buggy compilers. */
7684
7685 retval = canon;
7686 }
7687 else
7688 {
7689 retval = physname;
7690 need_copy = 0;
7691 }
7692 }
7693 else
7694 retval = canon;
7695
7696 if (need_copy)
7697 retval = obsavestring (retval, strlen (retval),
7698 &objfile->objfile_obstack);
7699
7700 do_cleanups (back_to);
7701 return retval;
7702 }
7703
7704 /* Read the import statement specified by the given die and record it. */
7705
7706 static void
7707 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7708 {
7709 struct objfile *objfile = cu->objfile;
7710 struct attribute *import_attr;
7711 struct die_info *imported_die, *child_die;
7712 struct dwarf2_cu *imported_cu;
7713 const char *imported_name;
7714 const char *imported_name_prefix;
7715 const char *canonical_name;
7716 const char *import_alias;
7717 const char *imported_declaration = NULL;
7718 const char *import_prefix;
7719 VEC (const_char_ptr) *excludes = NULL;
7720 struct cleanup *cleanups;
7721
7722 char *temp;
7723
7724 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7725 if (import_attr == NULL)
7726 {
7727 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7728 dwarf_tag_name (die->tag));
7729 return;
7730 }
7731
7732 imported_cu = cu;
7733 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7734 imported_name = dwarf2_name (imported_die, imported_cu);
7735 if (imported_name == NULL)
7736 {
7737 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7738
7739 The import in the following code:
7740 namespace A
7741 {
7742 typedef int B;
7743 }
7744
7745 int main ()
7746 {
7747 using A::B;
7748 B b;
7749 return b;
7750 }
7751
7752 ...
7753 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7754 <52> DW_AT_decl_file : 1
7755 <53> DW_AT_decl_line : 6
7756 <54> DW_AT_import : <0x75>
7757 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7758 <59> DW_AT_name : B
7759 <5b> DW_AT_decl_file : 1
7760 <5c> DW_AT_decl_line : 2
7761 <5d> DW_AT_type : <0x6e>
7762 ...
7763 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7764 <76> DW_AT_byte_size : 4
7765 <77> DW_AT_encoding : 5 (signed)
7766
7767 imports the wrong die ( 0x75 instead of 0x58 ).
7768 This case will be ignored until the gcc bug is fixed. */
7769 return;
7770 }
7771
7772 /* Figure out the local name after import. */
7773 import_alias = dwarf2_name (die, cu);
7774
7775 /* Figure out where the statement is being imported to. */
7776 import_prefix = determine_prefix (die, cu);
7777
7778 /* Figure out what the scope of the imported die is and prepend it
7779 to the name of the imported die. */
7780 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7781
7782 if (imported_die->tag != DW_TAG_namespace
7783 && imported_die->tag != DW_TAG_module)
7784 {
7785 imported_declaration = imported_name;
7786 canonical_name = imported_name_prefix;
7787 }
7788 else if (strlen (imported_name_prefix) > 0)
7789 {
7790 temp = alloca (strlen (imported_name_prefix)
7791 + 2 + strlen (imported_name) + 1);
7792 strcpy (temp, imported_name_prefix);
7793 strcat (temp, "::");
7794 strcat (temp, imported_name);
7795 canonical_name = temp;
7796 }
7797 else
7798 canonical_name = imported_name;
7799
7800 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7801
7802 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7803 for (child_die = die->child; child_die && child_die->tag;
7804 child_die = sibling_die (child_die))
7805 {
7806 /* DWARF-4: A Fortran use statement with a “rename list” may be
7807 represented by an imported module entry with an import attribute
7808 referring to the module and owned entries corresponding to those
7809 entities that are renamed as part of being imported. */
7810
7811 if (child_die->tag != DW_TAG_imported_declaration)
7812 {
7813 complaint (&symfile_complaints,
7814 _("child DW_TAG_imported_declaration expected "
7815 "- DIE at 0x%x [in module %s]"),
7816 child_die->offset.sect_off, objfile->name);
7817 continue;
7818 }
7819
7820 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7821 if (import_attr == NULL)
7822 {
7823 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7824 dwarf_tag_name (child_die->tag));
7825 continue;
7826 }
7827
7828 imported_cu = cu;
7829 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7830 &imported_cu);
7831 imported_name = dwarf2_name (imported_die, imported_cu);
7832 if (imported_name == NULL)
7833 {
7834 complaint (&symfile_complaints,
7835 _("child DW_TAG_imported_declaration has unknown "
7836 "imported name - DIE at 0x%x [in module %s]"),
7837 child_die->offset.sect_off, objfile->name);
7838 continue;
7839 }
7840
7841 VEC_safe_push (const_char_ptr, excludes, imported_name);
7842
7843 process_die (child_die, cu);
7844 }
7845
7846 cp_add_using_directive (import_prefix,
7847 canonical_name,
7848 import_alias,
7849 imported_declaration,
7850 excludes,
7851 &objfile->objfile_obstack);
7852
7853 do_cleanups (cleanups);
7854 }
7855
7856 /* Cleanup function for handle_DW_AT_stmt_list. */
7857
7858 static void
7859 free_cu_line_header (void *arg)
7860 {
7861 struct dwarf2_cu *cu = arg;
7862
7863 free_line_header (cu->line_header);
7864 cu->line_header = NULL;
7865 }
7866
7867 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7868 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7869 this, it was first present in GCC release 4.3.0. */
7870
7871 static int
7872 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7873 {
7874 if (!cu->checked_producer)
7875 check_producer (cu);
7876
7877 return cu->producer_is_gcc_lt_4_3;
7878 }
7879
7880 static void
7881 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7882 char **name, char **comp_dir)
7883 {
7884 struct attribute *attr;
7885
7886 *name = NULL;
7887 *comp_dir = NULL;
7888
7889 /* Find the filename. Do not use dwarf2_name here, since the filename
7890 is not a source language identifier. */
7891 attr = dwarf2_attr (die, DW_AT_name, cu);
7892 if (attr)
7893 {
7894 *name = DW_STRING (attr);
7895 }
7896
7897 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7898 if (attr)
7899 *comp_dir = DW_STRING (attr);
7900 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7901 && IS_ABSOLUTE_PATH (*name))
7902 {
7903 *comp_dir = ldirname (*name);
7904 if (*comp_dir != NULL)
7905 make_cleanup (xfree, *comp_dir);
7906 }
7907 if (*comp_dir != NULL)
7908 {
7909 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7910 directory, get rid of it. */
7911 char *cp = strchr (*comp_dir, ':');
7912
7913 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7914 *comp_dir = cp + 1;
7915 }
7916
7917 if (*name == NULL)
7918 *name = "<unknown>";
7919 }
7920
7921 /* Handle DW_AT_stmt_list for a compilation unit.
7922 DIE is the DW_TAG_compile_unit die for CU.
7923 COMP_DIR is the compilation directory.
7924 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7925
7926 static void
7927 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7928 const char *comp_dir)
7929 {
7930 struct attribute *attr;
7931
7932 gdb_assert (! cu->per_cu->is_debug_types);
7933
7934 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7935 if (attr)
7936 {
7937 unsigned int line_offset = DW_UNSND (attr);
7938 struct line_header *line_header
7939 = dwarf_decode_line_header (line_offset, cu);
7940
7941 if (line_header)
7942 {
7943 cu->line_header = line_header;
7944 make_cleanup (free_cu_line_header, cu);
7945 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7946 }
7947 }
7948 }
7949
7950 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7951
7952 static void
7953 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7954 {
7955 struct objfile *objfile = dwarf2_per_objfile->objfile;
7956 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7957 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7958 CORE_ADDR highpc = ((CORE_ADDR) 0);
7959 struct attribute *attr;
7960 char *name = NULL;
7961 char *comp_dir = NULL;
7962 struct die_info *child_die;
7963 bfd *abfd = objfile->obfd;
7964 CORE_ADDR baseaddr;
7965
7966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7967
7968 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7969
7970 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7971 from finish_block. */
7972 if (lowpc == ((CORE_ADDR) -1))
7973 lowpc = highpc;
7974 lowpc += baseaddr;
7975 highpc += baseaddr;
7976
7977 find_file_and_directory (die, cu, &name, &comp_dir);
7978
7979 prepare_one_comp_unit (cu, die, cu->language);
7980
7981 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7982 standardised yet. As a workaround for the language detection we fall
7983 back to the DW_AT_producer string. */
7984 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7985 cu->language = language_opencl;
7986
7987 /* Similar hack for Go. */
7988 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7989 set_cu_language (DW_LANG_Go, cu);
7990
7991 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7992
7993 /* Decode line number information if present. We do this before
7994 processing child DIEs, so that the line header table is available
7995 for DW_AT_decl_file. */
7996 handle_DW_AT_stmt_list (die, cu, comp_dir);
7997
7998 /* Process all dies in compilation unit. */
7999 if (die->child != NULL)
8000 {
8001 child_die = die->child;
8002 while (child_die && child_die->tag)
8003 {
8004 process_die (child_die, cu);
8005 child_die = sibling_die (child_die);
8006 }
8007 }
8008
8009 /* Decode macro information, if present. Dwarf 2 macro information
8010 refers to information in the line number info statement program
8011 header, so we can only read it if we've read the header
8012 successfully. */
8013 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8014 if (attr && cu->line_header)
8015 {
8016 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8017 complaint (&symfile_complaints,
8018 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8019
8020 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8021 }
8022 else
8023 {
8024 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8025 if (attr && cu->line_header)
8026 {
8027 unsigned int macro_offset = DW_UNSND (attr);
8028
8029 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8030 }
8031 }
8032
8033 do_cleanups (back_to);
8034 }
8035
8036 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8037 Create the set of symtabs used by this TU, or if this TU is sharing
8038 symtabs with another TU and the symtabs have already been created
8039 then restore those symtabs in the line header.
8040 We don't need the pc/line-number mapping for type units. */
8041
8042 static void
8043 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8044 {
8045 struct objfile *objfile = dwarf2_per_objfile->objfile;
8046 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8047 struct type_unit_group *tu_group;
8048 int first_time;
8049 struct line_header *lh;
8050 struct attribute *attr;
8051 unsigned int i, line_offset;
8052
8053 gdb_assert (per_cu->is_debug_types);
8054
8055 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8056
8057 /* If we're using .gdb_index (includes -readnow) then
8058 per_cu->s.type_unit_group may not have been set up yet. */
8059 if (per_cu->s.type_unit_group == NULL)
8060 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
8061 tu_group = per_cu->s.type_unit_group;
8062
8063 /* If we've already processed this stmt_list there's no real need to
8064 do it again, we could fake it and just recreate the part we need
8065 (file name,index -> symtab mapping). If data shows this optimization
8066 is useful we can do it then. */
8067 first_time = tu_group->primary_symtab == NULL;
8068
8069 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8070 debug info. */
8071 lh = NULL;
8072 if (attr != NULL)
8073 {
8074 line_offset = DW_UNSND (attr);
8075 lh = dwarf_decode_line_header (line_offset, cu);
8076 }
8077 if (lh == NULL)
8078 {
8079 if (first_time)
8080 dwarf2_start_symtab (cu, "", NULL, 0);
8081 else
8082 {
8083 gdb_assert (tu_group->symtabs == NULL);
8084 restart_symtab (0);
8085 }
8086 /* Note: The primary symtab will get allocated at the end. */
8087 return;
8088 }
8089
8090 cu->line_header = lh;
8091 make_cleanup (free_cu_line_header, cu);
8092
8093 if (first_time)
8094 {
8095 dwarf2_start_symtab (cu, "", NULL, 0);
8096
8097 tu_group->num_symtabs = lh->num_file_names;
8098 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8099
8100 for (i = 0; i < lh->num_file_names; ++i)
8101 {
8102 char *dir = NULL;
8103 struct file_entry *fe = &lh->file_names[i];
8104
8105 if (fe->dir_index)
8106 dir = lh->include_dirs[fe->dir_index - 1];
8107 dwarf2_start_subfile (fe->name, dir, NULL);
8108
8109 /* Note: We don't have to watch for the main subfile here, type units
8110 don't have DW_AT_name. */
8111
8112 if (current_subfile->symtab == NULL)
8113 {
8114 /* NOTE: start_subfile will recognize when it's been passed
8115 a file it has already seen. So we can't assume there's a
8116 simple mapping from lh->file_names to subfiles,
8117 lh->file_names may contain dups. */
8118 current_subfile->symtab = allocate_symtab (current_subfile->name,
8119 objfile);
8120 }
8121
8122 fe->symtab = current_subfile->symtab;
8123 tu_group->symtabs[i] = fe->symtab;
8124 }
8125 }
8126 else
8127 {
8128 restart_symtab (0);
8129
8130 for (i = 0; i < lh->num_file_names; ++i)
8131 {
8132 struct file_entry *fe = &lh->file_names[i];
8133
8134 fe->symtab = tu_group->symtabs[i];
8135 }
8136 }
8137
8138 /* The main symtab is allocated last. Type units don't have DW_AT_name
8139 so they don't have a "real" (so to speak) symtab anyway.
8140 There is later code that will assign the main symtab to all symbols
8141 that don't have one. We need to handle the case of a symbol with a
8142 missing symtab (DW_AT_decl_file) anyway. */
8143 }
8144
8145 /* Process DW_TAG_type_unit.
8146 For TUs we want to skip the first top level sibling if it's not the
8147 actual type being defined by this TU. In this case the first top
8148 level sibling is there to provide context only. */
8149
8150 static void
8151 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8152 {
8153 struct die_info *child_die;
8154
8155 prepare_one_comp_unit (cu, die, language_minimal);
8156
8157 /* Initialize (or reinitialize) the machinery for building symtabs.
8158 We do this before processing child DIEs, so that the line header table
8159 is available for DW_AT_decl_file. */
8160 setup_type_unit_groups (die, cu);
8161
8162 if (die->child != NULL)
8163 {
8164 child_die = die->child;
8165 while (child_die && child_die->tag)
8166 {
8167 process_die (child_die, cu);
8168 child_die = sibling_die (child_die);
8169 }
8170 }
8171 }
8172 \f
8173 /* DWO/DWP files.
8174
8175 http://gcc.gnu.org/wiki/DebugFission
8176 http://gcc.gnu.org/wiki/DebugFissionDWP
8177
8178 To simplify handling of both DWO files ("object" files with the DWARF info)
8179 and DWP files (a file with the DWOs packaged up into one file), we treat
8180 DWP files as having a collection of virtual DWO files. */
8181
8182 static hashval_t
8183 hash_dwo_file (const void *item)
8184 {
8185 const struct dwo_file *dwo_file = item;
8186
8187 return htab_hash_string (dwo_file->name);
8188 }
8189
8190 static int
8191 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8192 {
8193 const struct dwo_file *lhs = item_lhs;
8194 const struct dwo_file *rhs = item_rhs;
8195
8196 return strcmp (lhs->name, rhs->name) == 0;
8197 }
8198
8199 /* Allocate a hash table for DWO files. */
8200
8201 static htab_t
8202 allocate_dwo_file_hash_table (void)
8203 {
8204 struct objfile *objfile = dwarf2_per_objfile->objfile;
8205
8206 return htab_create_alloc_ex (41,
8207 hash_dwo_file,
8208 eq_dwo_file,
8209 NULL,
8210 &objfile->objfile_obstack,
8211 hashtab_obstack_allocate,
8212 dummy_obstack_deallocate);
8213 }
8214
8215 /* Lookup DWO file DWO_NAME. */
8216
8217 static void **
8218 lookup_dwo_file_slot (const char *dwo_name)
8219 {
8220 struct dwo_file find_entry;
8221 void **slot;
8222
8223 if (dwarf2_per_objfile->dwo_files == NULL)
8224 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8225
8226 memset (&find_entry, 0, sizeof (find_entry));
8227 find_entry.name = dwo_name;
8228 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8229
8230 return slot;
8231 }
8232
8233 static hashval_t
8234 hash_dwo_unit (const void *item)
8235 {
8236 const struct dwo_unit *dwo_unit = item;
8237
8238 /* This drops the top 32 bits of the id, but is ok for a hash. */
8239 return dwo_unit->signature;
8240 }
8241
8242 static int
8243 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8244 {
8245 const struct dwo_unit *lhs = item_lhs;
8246 const struct dwo_unit *rhs = item_rhs;
8247
8248 /* The signature is assumed to be unique within the DWO file.
8249 So while object file CU dwo_id's always have the value zero,
8250 that's OK, assuming each object file DWO file has only one CU,
8251 and that's the rule for now. */
8252 return lhs->signature == rhs->signature;
8253 }
8254
8255 /* Allocate a hash table for DWO CUs,TUs.
8256 There is one of these tables for each of CUs,TUs for each DWO file. */
8257
8258 static htab_t
8259 allocate_dwo_unit_table (struct objfile *objfile)
8260 {
8261 /* Start out with a pretty small number.
8262 Generally DWO files contain only one CU and maybe some TUs. */
8263 return htab_create_alloc_ex (3,
8264 hash_dwo_unit,
8265 eq_dwo_unit,
8266 NULL,
8267 &objfile->objfile_obstack,
8268 hashtab_obstack_allocate,
8269 dummy_obstack_deallocate);
8270 }
8271
8272 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8273
8274 struct create_dwo_info_table_data
8275 {
8276 struct dwo_file *dwo_file;
8277 htab_t cu_htab;
8278 };
8279
8280 /* die_reader_func for create_dwo_debug_info_hash_table. */
8281
8282 static void
8283 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8284 gdb_byte *info_ptr,
8285 struct die_info *comp_unit_die,
8286 int has_children,
8287 void *datap)
8288 {
8289 struct dwarf2_cu *cu = reader->cu;
8290 struct objfile *objfile = dwarf2_per_objfile->objfile;
8291 sect_offset offset = cu->per_cu->offset;
8292 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8293 struct create_dwo_info_table_data *data = datap;
8294 struct dwo_file *dwo_file = data->dwo_file;
8295 htab_t cu_htab = data->cu_htab;
8296 void **slot;
8297 struct attribute *attr;
8298 struct dwo_unit *dwo_unit;
8299
8300 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8301 if (attr == NULL)
8302 {
8303 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8304 " its dwo_id [in module %s]"),
8305 offset.sect_off, dwo_file->name);
8306 return;
8307 }
8308
8309 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8310 dwo_unit->dwo_file = dwo_file;
8311 dwo_unit->signature = DW_UNSND (attr);
8312 dwo_unit->info_or_types_section = section;
8313 dwo_unit->offset = offset;
8314 dwo_unit->length = cu->per_cu->length;
8315
8316 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8317 gdb_assert (slot != NULL);
8318 if (*slot != NULL)
8319 {
8320 const struct dwo_unit *dup_dwo_unit = *slot;
8321
8322 complaint (&symfile_complaints,
8323 _("debug entry at offset 0x%x is duplicate to the entry at"
8324 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8325 offset.sect_off, dup_dwo_unit->offset.sect_off,
8326 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8327 dwo_file->name);
8328 }
8329 else
8330 *slot = dwo_unit;
8331
8332 if (dwarf2_read_debug)
8333 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8334 offset.sect_off,
8335 phex (dwo_unit->signature,
8336 sizeof (dwo_unit->signature)));
8337 }
8338
8339 /* Create a hash table to map DWO IDs to their CU entry in
8340 .debug_info.dwo in DWO_FILE.
8341 Note: This function processes DWO files only, not DWP files. */
8342
8343 static htab_t
8344 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8345 {
8346 struct objfile *objfile = dwarf2_per_objfile->objfile;
8347 struct dwarf2_section_info *section = &dwo_file->sections.info;
8348 bfd *abfd;
8349 htab_t cu_htab;
8350 gdb_byte *info_ptr, *end_ptr;
8351 struct create_dwo_info_table_data create_dwo_info_table_data;
8352
8353 dwarf2_read_section (objfile, section);
8354 info_ptr = section->buffer;
8355
8356 if (info_ptr == NULL)
8357 return NULL;
8358
8359 /* We can't set abfd until now because the section may be empty or
8360 not present, in which case section->asection will be NULL. */
8361 abfd = section->asection->owner;
8362
8363 if (dwarf2_read_debug)
8364 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8365 bfd_get_filename (abfd));
8366
8367 cu_htab = allocate_dwo_unit_table (objfile);
8368
8369 create_dwo_info_table_data.dwo_file = dwo_file;
8370 create_dwo_info_table_data.cu_htab = cu_htab;
8371
8372 end_ptr = info_ptr + section->size;
8373 while (info_ptr < end_ptr)
8374 {
8375 struct dwarf2_per_cu_data per_cu;
8376
8377 memset (&per_cu, 0, sizeof (per_cu));
8378 per_cu.objfile = objfile;
8379 per_cu.is_debug_types = 0;
8380 per_cu.offset.sect_off = info_ptr - section->buffer;
8381 per_cu.info_or_types_section = section;
8382
8383 init_cutu_and_read_dies_no_follow (&per_cu,
8384 &dwo_file->sections.abbrev,
8385 dwo_file,
8386 create_dwo_debug_info_hash_table_reader,
8387 &create_dwo_info_table_data);
8388
8389 info_ptr += per_cu.length;
8390 }
8391
8392 return cu_htab;
8393 }
8394
8395 /* DWP file .debug_{cu,tu}_index section format:
8396 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8397
8398 Both index sections have the same format, and serve to map a 64-bit
8399 signature to a set of section numbers. Each section begins with a header,
8400 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8401 indexes, and a pool of 32-bit section numbers. The index sections will be
8402 aligned at 8-byte boundaries in the file.
8403
8404 The index section header contains two unsigned 32-bit values (using the
8405 byte order of the application binary):
8406
8407 N, the number of compilation units or type units in the index
8408 M, the number of slots in the hash table
8409
8410 (We assume that N and M will not exceed 2^32 - 1.)
8411
8412 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8413
8414 The hash table begins at offset 8 in the section, and consists of an array
8415 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8416 order of the application binary). Unused slots in the hash table are 0.
8417 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8418
8419 The parallel table begins immediately after the hash table
8420 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8421 array of 32-bit indexes (using the byte order of the application binary),
8422 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8423 table contains a 32-bit index into the pool of section numbers. For unused
8424 hash table slots, the corresponding entry in the parallel table will be 0.
8425
8426 Given a 64-bit compilation unit signature or a type signature S, an entry
8427 in the hash table is located as follows:
8428
8429 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8430 the low-order k bits all set to 1.
8431
8432 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8433
8434 3) If the hash table entry at index H matches the signature, use that
8435 entry. If the hash table entry at index H is unused (all zeroes),
8436 terminate the search: the signature is not present in the table.
8437
8438 4) Let H = (H + H') modulo M. Repeat at Step 3.
8439
8440 Because M > N and H' and M are relatively prime, the search is guaranteed
8441 to stop at an unused slot or find the match.
8442
8443 The pool of section numbers begins immediately following the hash table
8444 (at offset 8 + 12 * M from the beginning of the section). The pool of
8445 section numbers consists of an array of 32-bit words (using the byte order
8446 of the application binary). Each item in the array is indexed starting
8447 from 0. The hash table entry provides the index of the first section
8448 number in the set. Additional section numbers in the set follow, and the
8449 set is terminated by a 0 entry (section number 0 is not used in ELF).
8450
8451 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8452 section must be the first entry in the set, and the .debug_abbrev.dwo must
8453 be the second entry. Other members of the set may follow in any order. */
8454
8455 /* Create a hash table to map DWO IDs to their CU/TU entry in
8456 .debug_{info,types}.dwo in DWP_FILE.
8457 Returns NULL if there isn't one.
8458 Note: This function processes DWP files only, not DWO files. */
8459
8460 static struct dwp_hash_table *
8461 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8462 {
8463 struct objfile *objfile = dwarf2_per_objfile->objfile;
8464 bfd *dbfd = dwp_file->dbfd;
8465 char *index_ptr, *index_end;
8466 struct dwarf2_section_info *index;
8467 uint32_t version, nr_units, nr_slots;
8468 struct dwp_hash_table *htab;
8469
8470 if (is_debug_types)
8471 index = &dwp_file->sections.tu_index;
8472 else
8473 index = &dwp_file->sections.cu_index;
8474
8475 if (dwarf2_section_empty_p (index))
8476 return NULL;
8477 dwarf2_read_section (objfile, index);
8478
8479 index_ptr = index->buffer;
8480 index_end = index_ptr + index->size;
8481
8482 version = read_4_bytes (dbfd, index_ptr);
8483 index_ptr += 8; /* Skip the unused word. */
8484 nr_units = read_4_bytes (dbfd, index_ptr);
8485 index_ptr += 4;
8486 nr_slots = read_4_bytes (dbfd, index_ptr);
8487 index_ptr += 4;
8488
8489 if (version != 1)
8490 {
8491 error (_("Dwarf Error: unsupported DWP file version (%u)"
8492 " [in module %s]"),
8493 version, dwp_file->name);
8494 }
8495 if (nr_slots != (nr_slots & -nr_slots))
8496 {
8497 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8498 " is not power of 2 [in module %s]"),
8499 nr_slots, dwp_file->name);
8500 }
8501
8502 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8503 htab->nr_units = nr_units;
8504 htab->nr_slots = nr_slots;
8505 htab->hash_table = index_ptr;
8506 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8507 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8508
8509 return htab;
8510 }
8511
8512 /* Update SECTIONS with the data from SECTP.
8513
8514 This function is like the other "locate" section routines that are
8515 passed to bfd_map_over_sections, but in this context the sections to
8516 read comes from the DWP hash table, not the full ELF section table.
8517
8518 The result is non-zero for success, or zero if an error was found. */
8519
8520 static int
8521 locate_virtual_dwo_sections (asection *sectp,
8522 struct virtual_dwo_sections *sections)
8523 {
8524 const struct dwop_section_names *names = &dwop_section_names;
8525
8526 if (section_is_p (sectp->name, &names->abbrev_dwo))
8527 {
8528 /* There can be only one. */
8529 if (sections->abbrev.asection != NULL)
8530 return 0;
8531 sections->abbrev.asection = sectp;
8532 sections->abbrev.size = bfd_get_section_size (sectp);
8533 }
8534 else if (section_is_p (sectp->name, &names->info_dwo)
8535 || section_is_p (sectp->name, &names->types_dwo))
8536 {
8537 /* There can be only one. */
8538 if (sections->info_or_types.asection != NULL)
8539 return 0;
8540 sections->info_or_types.asection = sectp;
8541 sections->info_or_types.size = bfd_get_section_size (sectp);
8542 }
8543 else if (section_is_p (sectp->name, &names->line_dwo))
8544 {
8545 /* There can be only one. */
8546 if (sections->line.asection != NULL)
8547 return 0;
8548 sections->line.asection = sectp;
8549 sections->line.size = bfd_get_section_size (sectp);
8550 }
8551 else if (section_is_p (sectp->name, &names->loc_dwo))
8552 {
8553 /* There can be only one. */
8554 if (sections->loc.asection != NULL)
8555 return 0;
8556 sections->loc.asection = sectp;
8557 sections->loc.size = bfd_get_section_size (sectp);
8558 }
8559 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8560 {
8561 /* There can be only one. */
8562 if (sections->macinfo.asection != NULL)
8563 return 0;
8564 sections->macinfo.asection = sectp;
8565 sections->macinfo.size = bfd_get_section_size (sectp);
8566 }
8567 else if (section_is_p (sectp->name, &names->macro_dwo))
8568 {
8569 /* There can be only one. */
8570 if (sections->macro.asection != NULL)
8571 return 0;
8572 sections->macro.asection = sectp;
8573 sections->macro.size = bfd_get_section_size (sectp);
8574 }
8575 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8576 {
8577 /* There can be only one. */
8578 if (sections->str_offsets.asection != NULL)
8579 return 0;
8580 sections->str_offsets.asection = sectp;
8581 sections->str_offsets.size = bfd_get_section_size (sectp);
8582 }
8583 else
8584 {
8585 /* No other kind of section is valid. */
8586 return 0;
8587 }
8588
8589 return 1;
8590 }
8591
8592 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8593 HTAB is the hash table from the DWP file.
8594 SECTION_INDEX is the index of the DWO in HTAB. */
8595
8596 static struct dwo_unit *
8597 create_dwo_in_dwp (struct dwp_file *dwp_file,
8598 const struct dwp_hash_table *htab,
8599 uint32_t section_index,
8600 ULONGEST signature, int is_debug_types)
8601 {
8602 struct objfile *objfile = dwarf2_per_objfile->objfile;
8603 bfd *dbfd = dwp_file->dbfd;
8604 const char *kind = is_debug_types ? "TU" : "CU";
8605 struct dwo_file *dwo_file;
8606 struct dwo_unit *dwo_unit;
8607 struct virtual_dwo_sections sections;
8608 void **dwo_file_slot;
8609 char *virtual_dwo_name;
8610 struct dwarf2_section_info *cutu;
8611 struct cleanup *cleanups;
8612 int i;
8613
8614 if (dwarf2_read_debug)
8615 {
8616 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8617 kind,
8618 section_index, phex (signature, sizeof (signature)),
8619 dwp_file->name);
8620 }
8621
8622 /* Fetch the sections of this DWO.
8623 Put a limit on the number of sections we look for so that bad data
8624 doesn't cause us to loop forever. */
8625
8626 #define MAX_NR_DWO_SECTIONS \
8627 (1 /* .debug_info or .debug_types */ \
8628 + 1 /* .debug_abbrev */ \
8629 + 1 /* .debug_line */ \
8630 + 1 /* .debug_loc */ \
8631 + 1 /* .debug_str_offsets */ \
8632 + 1 /* .debug_macro */ \
8633 + 1 /* .debug_macinfo */ \
8634 + 1 /* trailing zero */)
8635
8636 memset (&sections, 0, sizeof (sections));
8637 cleanups = make_cleanup (null_cleanup, 0);
8638
8639 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8640 {
8641 asection *sectp;
8642 uint32_t section_nr =
8643 read_4_bytes (dbfd,
8644 htab->section_pool
8645 + (section_index + i) * sizeof (uint32_t));
8646
8647 if (section_nr == 0)
8648 break;
8649 if (section_nr >= dwp_file->num_sections)
8650 {
8651 error (_("Dwarf Error: bad DWP hash table, section number too large"
8652 " [in module %s]"),
8653 dwp_file->name);
8654 }
8655
8656 sectp = dwp_file->elf_sections[section_nr];
8657 if (! locate_virtual_dwo_sections (sectp, &sections))
8658 {
8659 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8660 " [in module %s]"),
8661 dwp_file->name);
8662 }
8663 }
8664
8665 if (i < 2
8666 || sections.info_or_types.asection == NULL
8667 || sections.abbrev.asection == NULL)
8668 {
8669 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8670 " [in module %s]"),
8671 dwp_file->name);
8672 }
8673 if (i == MAX_NR_DWO_SECTIONS)
8674 {
8675 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8676 " [in module %s]"),
8677 dwp_file->name);
8678 }
8679
8680 /* It's easier for the rest of the code if we fake a struct dwo_file and
8681 have dwo_unit "live" in that. At least for now.
8682
8683 The DWP file can be made up of a random collection of CUs and TUs.
8684 However, for each CU + set of TUs that came from the same original DWO
8685 file, we want to combine them back into a virtual DWO file to save space
8686 (fewer struct dwo_file objects to allocated). Remember that for really
8687 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8688
8689 virtual_dwo_name =
8690 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8691 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8692 sections.line.asection ? sections.line.asection->id : 0,
8693 sections.loc.asection ? sections.loc.asection->id : 0,
8694 (sections.str_offsets.asection
8695 ? sections.str_offsets.asection->id
8696 : 0));
8697 make_cleanup (xfree, virtual_dwo_name);
8698 /* Can we use an existing virtual DWO file? */
8699 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8700 /* Create one if necessary. */
8701 if (*dwo_file_slot == NULL)
8702 {
8703 if (dwarf2_read_debug)
8704 {
8705 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8706 virtual_dwo_name);
8707 }
8708 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8709 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8710 virtual_dwo_name,
8711 strlen (virtual_dwo_name));
8712 dwo_file->sections.abbrev = sections.abbrev;
8713 dwo_file->sections.line = sections.line;
8714 dwo_file->sections.loc = sections.loc;
8715 dwo_file->sections.macinfo = sections.macinfo;
8716 dwo_file->sections.macro = sections.macro;
8717 dwo_file->sections.str_offsets = sections.str_offsets;
8718 /* The "str" section is global to the entire DWP file. */
8719 dwo_file->sections.str = dwp_file->sections.str;
8720 /* The info or types section is assigned later to dwo_unit,
8721 there's no need to record it in dwo_file.
8722 Also, we can't simply record type sections in dwo_file because
8723 we record a pointer into the vector in dwo_unit. As we collect more
8724 types we'll grow the vector and eventually have to reallocate space
8725 for it, invalidating all the pointers into the current copy. */
8726 *dwo_file_slot = dwo_file;
8727 }
8728 else
8729 {
8730 if (dwarf2_read_debug)
8731 {
8732 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8733 virtual_dwo_name);
8734 }
8735 dwo_file = *dwo_file_slot;
8736 }
8737 do_cleanups (cleanups);
8738
8739 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8740 dwo_unit->dwo_file = dwo_file;
8741 dwo_unit->signature = signature;
8742 dwo_unit->info_or_types_section =
8743 obstack_alloc (&objfile->objfile_obstack,
8744 sizeof (struct dwarf2_section_info));
8745 *dwo_unit->info_or_types_section = sections.info_or_types;
8746 /* offset, length, type_offset_in_tu are set later. */
8747
8748 return dwo_unit;
8749 }
8750
8751 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8752
8753 static struct dwo_unit *
8754 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8755 const struct dwp_hash_table *htab,
8756 ULONGEST signature, int is_debug_types)
8757 {
8758 bfd *dbfd = dwp_file->dbfd;
8759 uint32_t mask = htab->nr_slots - 1;
8760 uint32_t hash = signature & mask;
8761 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8762 unsigned int i;
8763 void **slot;
8764 struct dwo_unit find_dwo_cu, *dwo_cu;
8765
8766 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8767 find_dwo_cu.signature = signature;
8768 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8769
8770 if (*slot != NULL)
8771 return *slot;
8772
8773 /* Use a for loop so that we don't loop forever on bad debug info. */
8774 for (i = 0; i < htab->nr_slots; ++i)
8775 {
8776 ULONGEST signature_in_table;
8777
8778 signature_in_table =
8779 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8780 if (signature_in_table == signature)
8781 {
8782 uint32_t section_index =
8783 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8784
8785 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8786 signature, is_debug_types);
8787 return *slot;
8788 }
8789 if (signature_in_table == 0)
8790 return NULL;
8791 hash = (hash + hash2) & mask;
8792 }
8793
8794 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8795 " [in module %s]"),
8796 dwp_file->name);
8797 }
8798
8799 /* Subroutine of open_dwop_file to simplify it.
8800 Open the file specified by FILE_NAME and hand it off to BFD for
8801 preliminary analysis. Return a newly initialized bfd *, which
8802 includes a canonicalized copy of FILE_NAME.
8803 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8804 In case of trouble, return NULL.
8805 NOTE: This function is derived from symfile_bfd_open. */
8806
8807 static bfd *
8808 try_open_dwop_file (const char *file_name, int is_dwp)
8809 {
8810 bfd *sym_bfd;
8811 int desc, flags;
8812 char *absolute_name;
8813
8814 flags = OPF_TRY_CWD_FIRST;
8815 if (is_dwp)
8816 flags |= OPF_SEARCH_IN_PATH;
8817 desc = openp (debug_file_directory, flags, file_name,
8818 O_RDONLY | O_BINARY, &absolute_name);
8819 if (desc < 0)
8820 return NULL;
8821
8822 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8823 if (!sym_bfd)
8824 {
8825 xfree (absolute_name);
8826 return NULL;
8827 }
8828 xfree (absolute_name);
8829 bfd_set_cacheable (sym_bfd, 1);
8830
8831 if (!bfd_check_format (sym_bfd, bfd_object))
8832 {
8833 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8834 return NULL;
8835 }
8836
8837 return sym_bfd;
8838 }
8839
8840 /* Try to open DWO/DWP file FILE_NAME.
8841 COMP_DIR is the DW_AT_comp_dir attribute.
8842 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8843 The result is the bfd handle of the file.
8844 If there is a problem finding or opening the file, return NULL.
8845 Upon success, the canonicalized path of the file is stored in the bfd,
8846 same as symfile_bfd_open. */
8847
8848 static bfd *
8849 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8850 {
8851 bfd *abfd;
8852
8853 if (IS_ABSOLUTE_PATH (file_name))
8854 return try_open_dwop_file (file_name, is_dwp);
8855
8856 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8857
8858 if (comp_dir != NULL)
8859 {
8860 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8861
8862 /* NOTE: If comp_dir is a relative path, this will also try the
8863 search path, which seems useful. */
8864 abfd = try_open_dwop_file (path_to_try, is_dwp);
8865 xfree (path_to_try);
8866 if (abfd != NULL)
8867 return abfd;
8868 }
8869
8870 /* That didn't work, try debug-file-directory, which, despite its name,
8871 is a list of paths. */
8872
8873 if (*debug_file_directory == '\0')
8874 return NULL;
8875
8876 return try_open_dwop_file (file_name, is_dwp);
8877 }
8878
8879 /* This function is mapped across the sections and remembers the offset and
8880 size of each of the DWO debugging sections we are interested in. */
8881
8882 static void
8883 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8884 {
8885 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8886 const struct dwop_section_names *names = &dwop_section_names;
8887
8888 if (section_is_p (sectp->name, &names->abbrev_dwo))
8889 {
8890 dwo_sections->abbrev.asection = sectp;
8891 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8892 }
8893 else if (section_is_p (sectp->name, &names->info_dwo))
8894 {
8895 dwo_sections->info.asection = sectp;
8896 dwo_sections->info.size = bfd_get_section_size (sectp);
8897 }
8898 else if (section_is_p (sectp->name, &names->line_dwo))
8899 {
8900 dwo_sections->line.asection = sectp;
8901 dwo_sections->line.size = bfd_get_section_size (sectp);
8902 }
8903 else if (section_is_p (sectp->name, &names->loc_dwo))
8904 {
8905 dwo_sections->loc.asection = sectp;
8906 dwo_sections->loc.size = bfd_get_section_size (sectp);
8907 }
8908 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8909 {
8910 dwo_sections->macinfo.asection = sectp;
8911 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8912 }
8913 else if (section_is_p (sectp->name, &names->macro_dwo))
8914 {
8915 dwo_sections->macro.asection = sectp;
8916 dwo_sections->macro.size = bfd_get_section_size (sectp);
8917 }
8918 else if (section_is_p (sectp->name, &names->str_dwo))
8919 {
8920 dwo_sections->str.asection = sectp;
8921 dwo_sections->str.size = bfd_get_section_size (sectp);
8922 }
8923 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8924 {
8925 dwo_sections->str_offsets.asection = sectp;
8926 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8927 }
8928 else if (section_is_p (sectp->name, &names->types_dwo))
8929 {
8930 struct dwarf2_section_info type_section;
8931
8932 memset (&type_section, 0, sizeof (type_section));
8933 type_section.asection = sectp;
8934 type_section.size = bfd_get_section_size (sectp);
8935 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8936 &type_section);
8937 }
8938 }
8939
8940 /* Initialize the use of the DWO file specified by DWO_NAME.
8941 The result is NULL if DWO_NAME can't be found. */
8942
8943 static struct dwo_file *
8944 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8945 {
8946 struct objfile *objfile = dwarf2_per_objfile->objfile;
8947 struct dwo_file *dwo_file;
8948 bfd *dbfd;
8949 struct cleanup *cleanups;
8950
8951 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8952 if (dbfd == NULL)
8953 {
8954 if (dwarf2_read_debug)
8955 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8956 return NULL;
8957 }
8958 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8959 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8960 dwo_name, strlen (dwo_name));
8961 dwo_file->dbfd = dbfd;
8962
8963 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8964
8965 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8966
8967 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8968
8969 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8970 dwo_file->sections.types);
8971
8972 discard_cleanups (cleanups);
8973
8974 if (dwarf2_read_debug)
8975 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8976
8977 return dwo_file;
8978 }
8979
8980 /* This function is mapped across the sections and remembers the offset and
8981 size of each of the DWP debugging sections we are interested in. */
8982
8983 static void
8984 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8985 {
8986 struct dwp_file *dwp_file = dwp_file_ptr;
8987 const struct dwop_section_names *names = &dwop_section_names;
8988 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8989
8990 /* Record the ELF section number for later lookup: this is what the
8991 .debug_cu_index,.debug_tu_index tables use. */
8992 gdb_assert (elf_section_nr < dwp_file->num_sections);
8993 dwp_file->elf_sections[elf_section_nr] = sectp;
8994
8995 /* Look for specific sections that we need. */
8996 if (section_is_p (sectp->name, &names->str_dwo))
8997 {
8998 dwp_file->sections.str.asection = sectp;
8999 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9000 }
9001 else if (section_is_p (sectp->name, &names->cu_index))
9002 {
9003 dwp_file->sections.cu_index.asection = sectp;
9004 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9005 }
9006 else if (section_is_p (sectp->name, &names->tu_index))
9007 {
9008 dwp_file->sections.tu_index.asection = sectp;
9009 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9010 }
9011 }
9012
9013 /* Hash function for dwp_file loaded CUs/TUs. */
9014
9015 static hashval_t
9016 hash_dwp_loaded_cutus (const void *item)
9017 {
9018 const struct dwo_unit *dwo_unit = item;
9019
9020 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9021 return dwo_unit->signature;
9022 }
9023
9024 /* Equality function for dwp_file loaded CUs/TUs. */
9025
9026 static int
9027 eq_dwp_loaded_cutus (const void *a, const void *b)
9028 {
9029 const struct dwo_unit *dua = a;
9030 const struct dwo_unit *dub = b;
9031
9032 return dua->signature == dub->signature;
9033 }
9034
9035 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9036
9037 static htab_t
9038 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9039 {
9040 return htab_create_alloc_ex (3,
9041 hash_dwp_loaded_cutus,
9042 eq_dwp_loaded_cutus,
9043 NULL,
9044 &objfile->objfile_obstack,
9045 hashtab_obstack_allocate,
9046 dummy_obstack_deallocate);
9047 }
9048
9049 /* Initialize the use of the DWP file for the current objfile.
9050 By convention the name of the DWP file is ${objfile}.dwp.
9051 The result is NULL if it can't be found. */
9052
9053 static struct dwp_file *
9054 open_and_init_dwp_file (const char *comp_dir)
9055 {
9056 struct objfile *objfile = dwarf2_per_objfile->objfile;
9057 struct dwp_file *dwp_file;
9058 char *dwp_name;
9059 bfd *dbfd;
9060 struct cleanup *cleanups;
9061
9062 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9063 cleanups = make_cleanup (xfree, dwp_name);
9064
9065 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9066 if (dbfd == NULL)
9067 {
9068 if (dwarf2_read_debug)
9069 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9070 do_cleanups (cleanups);
9071 return NULL;
9072 }
9073 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9074 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9075 dwp_name, strlen (dwp_name));
9076 dwp_file->dbfd = dbfd;
9077 do_cleanups (cleanups);
9078
9079 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9080
9081 /* +1: section 0 is unused */
9082 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9083 dwp_file->elf_sections =
9084 OBSTACK_CALLOC (&objfile->objfile_obstack,
9085 dwp_file->num_sections, asection *);
9086
9087 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9088
9089 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9090
9091 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9092
9093 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9094
9095 discard_cleanups (cleanups);
9096
9097 if (dwarf2_read_debug)
9098 {
9099 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9100 fprintf_unfiltered (gdb_stdlog,
9101 " %u CUs, %u TUs\n",
9102 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9103 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9104 }
9105
9106 return dwp_file;
9107 }
9108
9109 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9110 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9111 or in the DWP file for the objfile, referenced by THIS_UNIT.
9112 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9113 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9114
9115 This is called, for example, when wanting to read a variable with a
9116 complex location. Therefore we don't want to do file i/o for every call.
9117 Therefore we don't want to look for a DWO file on every call.
9118 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9119 then we check if we've already seen DWO_NAME, and only THEN do we check
9120 for a DWO file.
9121
9122 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9123 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9124
9125 static struct dwo_unit *
9126 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9127 const char *dwo_name, const char *comp_dir,
9128 ULONGEST signature, int is_debug_types)
9129 {
9130 struct objfile *objfile = dwarf2_per_objfile->objfile;
9131 const char *kind = is_debug_types ? "TU" : "CU";
9132 void **dwo_file_slot;
9133 struct dwo_file *dwo_file;
9134 struct dwp_file *dwp_file;
9135
9136 /* Have we already read SIGNATURE from a DWP file? */
9137
9138 if (! dwarf2_per_objfile->dwp_checked)
9139 {
9140 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9141 dwarf2_per_objfile->dwp_checked = 1;
9142 }
9143 dwp_file = dwarf2_per_objfile->dwp_file;
9144
9145 if (dwp_file != NULL)
9146 {
9147 const struct dwp_hash_table *dwp_htab =
9148 is_debug_types ? dwp_file->tus : dwp_file->cus;
9149
9150 if (dwp_htab != NULL)
9151 {
9152 struct dwo_unit *dwo_cutu =
9153 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9154
9155 if (dwo_cutu != NULL)
9156 {
9157 if (dwarf2_read_debug)
9158 {
9159 fprintf_unfiltered (gdb_stdlog,
9160 "Virtual DWO %s %s found: @%s\n",
9161 kind, hex_string (signature),
9162 host_address_to_string (dwo_cutu));
9163 }
9164 return dwo_cutu;
9165 }
9166 }
9167 }
9168
9169 /* Have we already seen DWO_NAME? */
9170
9171 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9172 if (*dwo_file_slot == NULL)
9173 {
9174 /* Read in the file and build a table of the DWOs it contains. */
9175 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9176 }
9177 /* NOTE: This will be NULL if unable to open the file. */
9178 dwo_file = *dwo_file_slot;
9179
9180 if (dwo_file != NULL)
9181 {
9182 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9183
9184 if (htab != NULL)
9185 {
9186 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9187
9188 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9189 find_dwo_cutu.signature = signature;
9190 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9191
9192 if (dwo_cutu != NULL)
9193 {
9194 if (dwarf2_read_debug)
9195 {
9196 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9197 kind, dwo_name, hex_string (signature),
9198 host_address_to_string (dwo_cutu));
9199 }
9200 return dwo_cutu;
9201 }
9202 }
9203 }
9204
9205 /* We didn't find it. This could mean a dwo_id mismatch, or
9206 someone deleted the DWO/DWP file, or the search path isn't set up
9207 correctly to find the file. */
9208
9209 if (dwarf2_read_debug)
9210 {
9211 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9212 kind, dwo_name, hex_string (signature));
9213 }
9214
9215 complaint (&symfile_complaints,
9216 _("Could not find DWO CU referenced by CU at offset 0x%x"
9217 " [in module %s]"),
9218 this_unit->offset.sect_off, objfile->name);
9219 return NULL;
9220 }
9221
9222 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9223 See lookup_dwo_cutu_unit for details. */
9224
9225 static struct dwo_unit *
9226 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9227 const char *dwo_name, const char *comp_dir,
9228 ULONGEST signature)
9229 {
9230 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9231 }
9232
9233 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9234 See lookup_dwo_cutu_unit for details. */
9235
9236 static struct dwo_unit *
9237 lookup_dwo_type_unit (struct signatured_type *this_tu,
9238 const char *dwo_name, const char *comp_dir)
9239 {
9240 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9241 }
9242
9243 /* Free all resources associated with DWO_FILE.
9244 Close the DWO file and munmap the sections.
9245 All memory should be on the objfile obstack. */
9246
9247 static void
9248 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9249 {
9250 int ix;
9251 struct dwarf2_section_info *section;
9252
9253 gdb_assert (dwo_file->dbfd != objfile->obfd);
9254 gdb_bfd_unref (dwo_file->dbfd);
9255
9256 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9257 }
9258
9259 /* Wrapper for free_dwo_file for use in cleanups. */
9260
9261 static void
9262 free_dwo_file_cleanup (void *arg)
9263 {
9264 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9265 struct objfile *objfile = dwarf2_per_objfile->objfile;
9266
9267 free_dwo_file (dwo_file, objfile);
9268 }
9269
9270 /* Traversal function for free_dwo_files. */
9271
9272 static int
9273 free_dwo_file_from_slot (void **slot, void *info)
9274 {
9275 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9276 struct objfile *objfile = (struct objfile *) info;
9277
9278 free_dwo_file (dwo_file, objfile);
9279
9280 return 1;
9281 }
9282
9283 /* Free all resources associated with DWO_FILES. */
9284
9285 static void
9286 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9287 {
9288 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9289 }
9290 \f
9291 /* Read in various DIEs. */
9292
9293 /* qsort helper for inherit_abstract_dies. */
9294
9295 static int
9296 unsigned_int_compar (const void *ap, const void *bp)
9297 {
9298 unsigned int a = *(unsigned int *) ap;
9299 unsigned int b = *(unsigned int *) bp;
9300
9301 return (a > b) - (b > a);
9302 }
9303
9304 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9305 Inherit only the children of the DW_AT_abstract_origin DIE not being
9306 already referenced by DW_AT_abstract_origin from the children of the
9307 current DIE. */
9308
9309 static void
9310 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9311 {
9312 struct die_info *child_die;
9313 unsigned die_children_count;
9314 /* CU offsets which were referenced by children of the current DIE. */
9315 sect_offset *offsets;
9316 sect_offset *offsets_end, *offsetp;
9317 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9318 struct die_info *origin_die;
9319 /* Iterator of the ORIGIN_DIE children. */
9320 struct die_info *origin_child_die;
9321 struct cleanup *cleanups;
9322 struct attribute *attr;
9323 struct dwarf2_cu *origin_cu;
9324 struct pending **origin_previous_list_in_scope;
9325
9326 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9327 if (!attr)
9328 return;
9329
9330 /* Note that following die references may follow to a die in a
9331 different cu. */
9332
9333 origin_cu = cu;
9334 origin_die = follow_die_ref (die, attr, &origin_cu);
9335
9336 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9337 symbols in. */
9338 origin_previous_list_in_scope = origin_cu->list_in_scope;
9339 origin_cu->list_in_scope = cu->list_in_scope;
9340
9341 if (die->tag != origin_die->tag
9342 && !(die->tag == DW_TAG_inlined_subroutine
9343 && origin_die->tag == DW_TAG_subprogram))
9344 complaint (&symfile_complaints,
9345 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9346 die->offset.sect_off, origin_die->offset.sect_off);
9347
9348 child_die = die->child;
9349 die_children_count = 0;
9350 while (child_die && child_die->tag)
9351 {
9352 child_die = sibling_die (child_die);
9353 die_children_count++;
9354 }
9355 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9356 cleanups = make_cleanup (xfree, offsets);
9357
9358 offsets_end = offsets;
9359 child_die = die->child;
9360 while (child_die && child_die->tag)
9361 {
9362 /* For each CHILD_DIE, find the corresponding child of
9363 ORIGIN_DIE. If there is more than one layer of
9364 DW_AT_abstract_origin, follow them all; there shouldn't be,
9365 but GCC versions at least through 4.4 generate this (GCC PR
9366 40573). */
9367 struct die_info *child_origin_die = child_die;
9368 struct dwarf2_cu *child_origin_cu = cu;
9369
9370 while (1)
9371 {
9372 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9373 child_origin_cu);
9374 if (attr == NULL)
9375 break;
9376 child_origin_die = follow_die_ref (child_origin_die, attr,
9377 &child_origin_cu);
9378 }
9379
9380 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9381 counterpart may exist. */
9382 if (child_origin_die != child_die)
9383 {
9384 if (child_die->tag != child_origin_die->tag
9385 && !(child_die->tag == DW_TAG_inlined_subroutine
9386 && child_origin_die->tag == DW_TAG_subprogram))
9387 complaint (&symfile_complaints,
9388 _("Child DIE 0x%x and its abstract origin 0x%x have "
9389 "different tags"), child_die->offset.sect_off,
9390 child_origin_die->offset.sect_off);
9391 if (child_origin_die->parent != origin_die)
9392 complaint (&symfile_complaints,
9393 _("Child DIE 0x%x and its abstract origin 0x%x have "
9394 "different parents"), child_die->offset.sect_off,
9395 child_origin_die->offset.sect_off);
9396 else
9397 *offsets_end++ = child_origin_die->offset;
9398 }
9399 child_die = sibling_die (child_die);
9400 }
9401 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9402 unsigned_int_compar);
9403 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9404 if (offsetp[-1].sect_off == offsetp->sect_off)
9405 complaint (&symfile_complaints,
9406 _("Multiple children of DIE 0x%x refer "
9407 "to DIE 0x%x as their abstract origin"),
9408 die->offset.sect_off, offsetp->sect_off);
9409
9410 offsetp = offsets;
9411 origin_child_die = origin_die->child;
9412 while (origin_child_die && origin_child_die->tag)
9413 {
9414 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9415 while (offsetp < offsets_end
9416 && offsetp->sect_off < origin_child_die->offset.sect_off)
9417 offsetp++;
9418 if (offsetp >= offsets_end
9419 || offsetp->sect_off > origin_child_die->offset.sect_off)
9420 {
9421 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9422 process_die (origin_child_die, origin_cu);
9423 }
9424 origin_child_die = sibling_die (origin_child_die);
9425 }
9426 origin_cu->list_in_scope = origin_previous_list_in_scope;
9427
9428 do_cleanups (cleanups);
9429 }
9430
9431 static void
9432 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9433 {
9434 struct objfile *objfile = cu->objfile;
9435 struct context_stack *new;
9436 CORE_ADDR lowpc;
9437 CORE_ADDR highpc;
9438 struct die_info *child_die;
9439 struct attribute *attr, *call_line, *call_file;
9440 char *name;
9441 CORE_ADDR baseaddr;
9442 struct block *block;
9443 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9444 VEC (symbolp) *template_args = NULL;
9445 struct template_symbol *templ_func = NULL;
9446
9447 if (inlined_func)
9448 {
9449 /* If we do not have call site information, we can't show the
9450 caller of this inlined function. That's too confusing, so
9451 only use the scope for local variables. */
9452 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9453 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9454 if (call_line == NULL || call_file == NULL)
9455 {
9456 read_lexical_block_scope (die, cu);
9457 return;
9458 }
9459 }
9460
9461 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9462
9463 name = dwarf2_name (die, cu);
9464
9465 /* Ignore functions with missing or empty names. These are actually
9466 illegal according to the DWARF standard. */
9467 if (name == NULL)
9468 {
9469 complaint (&symfile_complaints,
9470 _("missing name for subprogram DIE at %d"),
9471 die->offset.sect_off);
9472 return;
9473 }
9474
9475 /* Ignore functions with missing or invalid low and high pc attributes. */
9476 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9477 {
9478 attr = dwarf2_attr (die, DW_AT_external, cu);
9479 if (!attr || !DW_UNSND (attr))
9480 complaint (&symfile_complaints,
9481 _("cannot get low and high bounds "
9482 "for subprogram DIE at %d"),
9483 die->offset.sect_off);
9484 return;
9485 }
9486
9487 lowpc += baseaddr;
9488 highpc += baseaddr;
9489
9490 /* If we have any template arguments, then we must allocate a
9491 different sort of symbol. */
9492 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9493 {
9494 if (child_die->tag == DW_TAG_template_type_param
9495 || child_die->tag == DW_TAG_template_value_param)
9496 {
9497 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9498 struct template_symbol);
9499 templ_func->base.is_cplus_template_function = 1;
9500 break;
9501 }
9502 }
9503
9504 new = push_context (0, lowpc);
9505 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9506 (struct symbol *) templ_func);
9507
9508 /* If there is a location expression for DW_AT_frame_base, record
9509 it. */
9510 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9511 if (attr)
9512 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9513 expression is being recorded directly in the function's symbol
9514 and not in a separate frame-base object. I guess this hack is
9515 to avoid adding some sort of frame-base adjunct/annex to the
9516 function's symbol :-(. The problem with doing this is that it
9517 results in a function symbol with a location expression that
9518 has nothing to do with the location of the function, ouch! The
9519 relationship should be: a function's symbol has-a frame base; a
9520 frame-base has-a location expression. */
9521 dwarf2_symbol_mark_computed (attr, new->name, cu);
9522
9523 cu->list_in_scope = &local_symbols;
9524
9525 if (die->child != NULL)
9526 {
9527 child_die = die->child;
9528 while (child_die && child_die->tag)
9529 {
9530 if (child_die->tag == DW_TAG_template_type_param
9531 || child_die->tag == DW_TAG_template_value_param)
9532 {
9533 struct symbol *arg = new_symbol (child_die, NULL, cu);
9534
9535 if (arg != NULL)
9536 VEC_safe_push (symbolp, template_args, arg);
9537 }
9538 else
9539 process_die (child_die, cu);
9540 child_die = sibling_die (child_die);
9541 }
9542 }
9543
9544 inherit_abstract_dies (die, cu);
9545
9546 /* If we have a DW_AT_specification, we might need to import using
9547 directives from the context of the specification DIE. See the
9548 comment in determine_prefix. */
9549 if (cu->language == language_cplus
9550 && dwarf2_attr (die, DW_AT_specification, cu))
9551 {
9552 struct dwarf2_cu *spec_cu = cu;
9553 struct die_info *spec_die = die_specification (die, &spec_cu);
9554
9555 while (spec_die)
9556 {
9557 child_die = spec_die->child;
9558 while (child_die && child_die->tag)
9559 {
9560 if (child_die->tag == DW_TAG_imported_module)
9561 process_die (child_die, spec_cu);
9562 child_die = sibling_die (child_die);
9563 }
9564
9565 /* In some cases, GCC generates specification DIEs that
9566 themselves contain DW_AT_specification attributes. */
9567 spec_die = die_specification (spec_die, &spec_cu);
9568 }
9569 }
9570
9571 new = pop_context ();
9572 /* Make a block for the local symbols within. */
9573 block = finish_block (new->name, &local_symbols, new->old_blocks,
9574 lowpc, highpc, objfile);
9575
9576 /* For C++, set the block's scope. */
9577 if (cu->language == language_cplus || cu->language == language_fortran)
9578 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9579 determine_prefix (die, cu),
9580 processing_has_namespace_info);
9581
9582 /* If we have address ranges, record them. */
9583 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9584
9585 /* Attach template arguments to function. */
9586 if (! VEC_empty (symbolp, template_args))
9587 {
9588 gdb_assert (templ_func != NULL);
9589
9590 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9591 templ_func->template_arguments
9592 = obstack_alloc (&objfile->objfile_obstack,
9593 (templ_func->n_template_arguments
9594 * sizeof (struct symbol *)));
9595 memcpy (templ_func->template_arguments,
9596 VEC_address (symbolp, template_args),
9597 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9598 VEC_free (symbolp, template_args);
9599 }
9600
9601 /* In C++, we can have functions nested inside functions (e.g., when
9602 a function declares a class that has methods). This means that
9603 when we finish processing a function scope, we may need to go
9604 back to building a containing block's symbol lists. */
9605 local_symbols = new->locals;
9606 using_directives = new->using_directives;
9607
9608 /* If we've finished processing a top-level function, subsequent
9609 symbols go in the file symbol list. */
9610 if (outermost_context_p ())
9611 cu->list_in_scope = &file_symbols;
9612 }
9613
9614 /* Process all the DIES contained within a lexical block scope. Start
9615 a new scope, process the dies, and then close the scope. */
9616
9617 static void
9618 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9619 {
9620 struct objfile *objfile = cu->objfile;
9621 struct context_stack *new;
9622 CORE_ADDR lowpc, highpc;
9623 struct die_info *child_die;
9624 CORE_ADDR baseaddr;
9625
9626 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9627
9628 /* Ignore blocks with missing or invalid low and high pc attributes. */
9629 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9630 as multiple lexical blocks? Handling children in a sane way would
9631 be nasty. Might be easier to properly extend generic blocks to
9632 describe ranges. */
9633 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9634 return;
9635 lowpc += baseaddr;
9636 highpc += baseaddr;
9637
9638 push_context (0, lowpc);
9639 if (die->child != NULL)
9640 {
9641 child_die = die->child;
9642 while (child_die && child_die->tag)
9643 {
9644 process_die (child_die, cu);
9645 child_die = sibling_die (child_die);
9646 }
9647 }
9648 new = pop_context ();
9649
9650 if (local_symbols != NULL || using_directives != NULL)
9651 {
9652 struct block *block
9653 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9654 highpc, objfile);
9655
9656 /* Note that recording ranges after traversing children, as we
9657 do here, means that recording a parent's ranges entails
9658 walking across all its children's ranges as they appear in
9659 the address map, which is quadratic behavior.
9660
9661 It would be nicer to record the parent's ranges before
9662 traversing its children, simply overriding whatever you find
9663 there. But since we don't even decide whether to create a
9664 block until after we've traversed its children, that's hard
9665 to do. */
9666 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9667 }
9668 local_symbols = new->locals;
9669 using_directives = new->using_directives;
9670 }
9671
9672 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9673
9674 static void
9675 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9676 {
9677 struct objfile *objfile = cu->objfile;
9678 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9679 CORE_ADDR pc, baseaddr;
9680 struct attribute *attr;
9681 struct call_site *call_site, call_site_local;
9682 void **slot;
9683 int nparams;
9684 struct die_info *child_die;
9685
9686 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9687
9688 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9689 if (!attr)
9690 {
9691 complaint (&symfile_complaints,
9692 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9693 "DIE 0x%x [in module %s]"),
9694 die->offset.sect_off, objfile->name);
9695 return;
9696 }
9697 pc = DW_ADDR (attr) + baseaddr;
9698
9699 if (cu->call_site_htab == NULL)
9700 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9701 NULL, &objfile->objfile_obstack,
9702 hashtab_obstack_allocate, NULL);
9703 call_site_local.pc = pc;
9704 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9705 if (*slot != NULL)
9706 {
9707 complaint (&symfile_complaints,
9708 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9709 "DIE 0x%x [in module %s]"),
9710 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9711 return;
9712 }
9713
9714 /* Count parameters at the caller. */
9715
9716 nparams = 0;
9717 for (child_die = die->child; child_die && child_die->tag;
9718 child_die = sibling_die (child_die))
9719 {
9720 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9721 {
9722 complaint (&symfile_complaints,
9723 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9724 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9725 child_die->tag, child_die->offset.sect_off, objfile->name);
9726 continue;
9727 }
9728
9729 nparams++;
9730 }
9731
9732 call_site = obstack_alloc (&objfile->objfile_obstack,
9733 (sizeof (*call_site)
9734 + (sizeof (*call_site->parameter)
9735 * (nparams - 1))));
9736 *slot = call_site;
9737 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9738 call_site->pc = pc;
9739
9740 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9741 {
9742 struct die_info *func_die;
9743
9744 /* Skip also over DW_TAG_inlined_subroutine. */
9745 for (func_die = die->parent;
9746 func_die && func_die->tag != DW_TAG_subprogram
9747 && func_die->tag != DW_TAG_subroutine_type;
9748 func_die = func_die->parent);
9749
9750 /* DW_AT_GNU_all_call_sites is a superset
9751 of DW_AT_GNU_all_tail_call_sites. */
9752 if (func_die
9753 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9754 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9755 {
9756 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9757 not complete. But keep CALL_SITE for look ups via call_site_htab,
9758 both the initial caller containing the real return address PC and
9759 the final callee containing the current PC of a chain of tail
9760 calls do not need to have the tail call list complete. But any
9761 function candidate for a virtual tail call frame searched via
9762 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9763 determined unambiguously. */
9764 }
9765 else
9766 {
9767 struct type *func_type = NULL;
9768
9769 if (func_die)
9770 func_type = get_die_type (func_die, cu);
9771 if (func_type != NULL)
9772 {
9773 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9774
9775 /* Enlist this call site to the function. */
9776 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9777 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9778 }
9779 else
9780 complaint (&symfile_complaints,
9781 _("Cannot find function owning DW_TAG_GNU_call_site "
9782 "DIE 0x%x [in module %s]"),
9783 die->offset.sect_off, objfile->name);
9784 }
9785 }
9786
9787 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9788 if (attr == NULL)
9789 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9790 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9791 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9792 /* Keep NULL DWARF_BLOCK. */;
9793 else if (attr_form_is_block (attr))
9794 {
9795 struct dwarf2_locexpr_baton *dlbaton;
9796
9797 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9798 dlbaton->data = DW_BLOCK (attr)->data;
9799 dlbaton->size = DW_BLOCK (attr)->size;
9800 dlbaton->per_cu = cu->per_cu;
9801
9802 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9803 }
9804 else if (is_ref_attr (attr))
9805 {
9806 struct dwarf2_cu *target_cu = cu;
9807 struct die_info *target_die;
9808
9809 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9810 gdb_assert (target_cu->objfile == objfile);
9811 if (die_is_declaration (target_die, target_cu))
9812 {
9813 const char *target_physname;
9814
9815 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9816 if (target_physname == NULL)
9817 complaint (&symfile_complaints,
9818 _("DW_AT_GNU_call_site_target target DIE has invalid "
9819 "physname, for referencing DIE 0x%x [in module %s]"),
9820 die->offset.sect_off, objfile->name);
9821 else
9822 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9823 }
9824 else
9825 {
9826 CORE_ADDR lowpc;
9827
9828 /* DW_AT_entry_pc should be preferred. */
9829 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9830 complaint (&symfile_complaints,
9831 _("DW_AT_GNU_call_site_target target DIE has invalid "
9832 "low pc, for referencing DIE 0x%x [in module %s]"),
9833 die->offset.sect_off, objfile->name);
9834 else
9835 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9836 }
9837 }
9838 else
9839 complaint (&symfile_complaints,
9840 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9841 "block nor reference, for DIE 0x%x [in module %s]"),
9842 die->offset.sect_off, objfile->name);
9843
9844 call_site->per_cu = cu->per_cu;
9845
9846 for (child_die = die->child;
9847 child_die && child_die->tag;
9848 child_die = sibling_die (child_die))
9849 {
9850 struct call_site_parameter *parameter;
9851 struct attribute *loc, *origin;
9852
9853 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9854 {
9855 /* Already printed the complaint above. */
9856 continue;
9857 }
9858
9859 gdb_assert (call_site->parameter_count < nparams);
9860 parameter = &call_site->parameter[call_site->parameter_count];
9861
9862 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9863 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9864 register is contained in DW_AT_GNU_call_site_value. */
9865
9866 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9867 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9868 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9869 {
9870 sect_offset offset;
9871
9872 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9873 offset = dwarf2_get_ref_die_offset (origin);
9874 if (!offset_in_cu_p (&cu->header, offset))
9875 {
9876 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9877 binding can be done only inside one CU. Such referenced DIE
9878 therefore cannot be even moved to DW_TAG_partial_unit. */
9879 complaint (&symfile_complaints,
9880 _("DW_AT_abstract_origin offset is not in CU for "
9881 "DW_TAG_GNU_call_site child DIE 0x%x "
9882 "[in module %s]"),
9883 child_die->offset.sect_off, objfile->name);
9884 continue;
9885 }
9886 parameter->u.param_offset.cu_off = (offset.sect_off
9887 - cu->header.offset.sect_off);
9888 }
9889 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9890 {
9891 complaint (&symfile_complaints,
9892 _("No DW_FORM_block* DW_AT_location for "
9893 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9894 child_die->offset.sect_off, objfile->name);
9895 continue;
9896 }
9897 else
9898 {
9899 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9900 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9901 if (parameter->u.dwarf_reg != -1)
9902 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9903 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9904 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9905 &parameter->u.fb_offset))
9906 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9907 else
9908 {
9909 complaint (&symfile_complaints,
9910 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9911 "for DW_FORM_block* DW_AT_location is supported for "
9912 "DW_TAG_GNU_call_site child DIE 0x%x "
9913 "[in module %s]"),
9914 child_die->offset.sect_off, objfile->name);
9915 continue;
9916 }
9917 }
9918
9919 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9920 if (!attr_form_is_block (attr))
9921 {
9922 complaint (&symfile_complaints,
9923 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9924 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9925 child_die->offset.sect_off, objfile->name);
9926 continue;
9927 }
9928 parameter->value = DW_BLOCK (attr)->data;
9929 parameter->value_size = DW_BLOCK (attr)->size;
9930
9931 /* Parameters are not pre-cleared by memset above. */
9932 parameter->data_value = NULL;
9933 parameter->data_value_size = 0;
9934 call_site->parameter_count++;
9935
9936 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9937 if (attr)
9938 {
9939 if (!attr_form_is_block (attr))
9940 complaint (&symfile_complaints,
9941 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9942 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9943 child_die->offset.sect_off, objfile->name);
9944 else
9945 {
9946 parameter->data_value = DW_BLOCK (attr)->data;
9947 parameter->data_value_size = DW_BLOCK (attr)->size;
9948 }
9949 }
9950 }
9951 }
9952
9953 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9954 Return 1 if the attributes are present and valid, otherwise, return 0.
9955 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9956
9957 static int
9958 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9959 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9960 struct partial_symtab *ranges_pst)
9961 {
9962 struct objfile *objfile = cu->objfile;
9963 struct comp_unit_head *cu_header = &cu->header;
9964 bfd *obfd = objfile->obfd;
9965 unsigned int addr_size = cu_header->addr_size;
9966 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9967 /* Base address selection entry. */
9968 CORE_ADDR base;
9969 int found_base;
9970 unsigned int dummy;
9971 gdb_byte *buffer;
9972 CORE_ADDR marker;
9973 int low_set;
9974 CORE_ADDR low = 0;
9975 CORE_ADDR high = 0;
9976 CORE_ADDR baseaddr;
9977
9978 found_base = cu->base_known;
9979 base = cu->base_address;
9980
9981 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9982 if (offset >= dwarf2_per_objfile->ranges.size)
9983 {
9984 complaint (&symfile_complaints,
9985 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9986 offset);
9987 return 0;
9988 }
9989 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9990
9991 /* Read in the largest possible address. */
9992 marker = read_address (obfd, buffer, cu, &dummy);
9993 if ((marker & mask) == mask)
9994 {
9995 /* If we found the largest possible address, then
9996 read the base address. */
9997 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9998 buffer += 2 * addr_size;
9999 offset += 2 * addr_size;
10000 found_base = 1;
10001 }
10002
10003 low_set = 0;
10004
10005 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10006
10007 while (1)
10008 {
10009 CORE_ADDR range_beginning, range_end;
10010
10011 range_beginning = read_address (obfd, buffer, cu, &dummy);
10012 buffer += addr_size;
10013 range_end = read_address (obfd, buffer, cu, &dummy);
10014 buffer += addr_size;
10015 offset += 2 * addr_size;
10016
10017 /* An end of list marker is a pair of zero addresses. */
10018 if (range_beginning == 0 && range_end == 0)
10019 /* Found the end of list entry. */
10020 break;
10021
10022 /* Each base address selection entry is a pair of 2 values.
10023 The first is the largest possible address, the second is
10024 the base address. Check for a base address here. */
10025 if ((range_beginning & mask) == mask)
10026 {
10027 /* If we found the largest possible address, then
10028 read the base address. */
10029 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10030 found_base = 1;
10031 continue;
10032 }
10033
10034 if (!found_base)
10035 {
10036 /* We have no valid base address for the ranges
10037 data. */
10038 complaint (&symfile_complaints,
10039 _("Invalid .debug_ranges data (no base address)"));
10040 return 0;
10041 }
10042
10043 if (range_beginning > range_end)
10044 {
10045 /* Inverted range entries are invalid. */
10046 complaint (&symfile_complaints,
10047 _("Invalid .debug_ranges data (inverted range)"));
10048 return 0;
10049 }
10050
10051 /* Empty range entries have no effect. */
10052 if (range_beginning == range_end)
10053 continue;
10054
10055 range_beginning += base;
10056 range_end += base;
10057
10058 /* A not-uncommon case of bad debug info.
10059 Don't pollute the addrmap with bad data. */
10060 if (range_beginning + baseaddr == 0
10061 && !dwarf2_per_objfile->has_section_at_zero)
10062 {
10063 complaint (&symfile_complaints,
10064 _(".debug_ranges entry has start address of zero"
10065 " [in module %s]"), objfile->name);
10066 continue;
10067 }
10068
10069 if (ranges_pst != NULL)
10070 addrmap_set_empty (objfile->psymtabs_addrmap,
10071 range_beginning + baseaddr,
10072 range_end - 1 + baseaddr,
10073 ranges_pst);
10074
10075 /* FIXME: This is recording everything as a low-high
10076 segment of consecutive addresses. We should have a
10077 data structure for discontiguous block ranges
10078 instead. */
10079 if (! low_set)
10080 {
10081 low = range_beginning;
10082 high = range_end;
10083 low_set = 1;
10084 }
10085 else
10086 {
10087 if (range_beginning < low)
10088 low = range_beginning;
10089 if (range_end > high)
10090 high = range_end;
10091 }
10092 }
10093
10094 if (! low_set)
10095 /* If the first entry is an end-of-list marker, the range
10096 describes an empty scope, i.e. no instructions. */
10097 return 0;
10098
10099 if (low_return)
10100 *low_return = low;
10101 if (high_return)
10102 *high_return = high;
10103 return 1;
10104 }
10105
10106 /* Get low and high pc attributes from a die. Return 1 if the attributes
10107 are present and valid, otherwise, return 0. Return -1 if the range is
10108 discontinuous, i.e. derived from DW_AT_ranges information. */
10109
10110 static int
10111 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10112 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10113 struct partial_symtab *pst)
10114 {
10115 struct attribute *attr;
10116 struct attribute *attr_high;
10117 CORE_ADDR low = 0;
10118 CORE_ADDR high = 0;
10119 int ret = 0;
10120
10121 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10122 if (attr_high)
10123 {
10124 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10125 if (attr)
10126 {
10127 low = DW_ADDR (attr);
10128 if (attr_high->form == DW_FORM_addr
10129 || attr_high->form == DW_FORM_GNU_addr_index)
10130 high = DW_ADDR (attr_high);
10131 else
10132 high = low + DW_UNSND (attr_high);
10133 }
10134 else
10135 /* Found high w/o low attribute. */
10136 return 0;
10137
10138 /* Found consecutive range of addresses. */
10139 ret = 1;
10140 }
10141 else
10142 {
10143 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10144 if (attr != NULL)
10145 {
10146 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10147 We take advantage of the fact that DW_AT_ranges does not appear
10148 in DW_TAG_compile_unit of DWO files. */
10149 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10150 unsigned int ranges_offset = (DW_UNSND (attr)
10151 + (need_ranges_base
10152 ? cu->ranges_base
10153 : 0));
10154
10155 /* Value of the DW_AT_ranges attribute is the offset in the
10156 .debug_ranges section. */
10157 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10158 return 0;
10159 /* Found discontinuous range of addresses. */
10160 ret = -1;
10161 }
10162 }
10163
10164 /* read_partial_die has also the strict LOW < HIGH requirement. */
10165 if (high <= low)
10166 return 0;
10167
10168 /* When using the GNU linker, .gnu.linkonce. sections are used to
10169 eliminate duplicate copies of functions and vtables and such.
10170 The linker will arbitrarily choose one and discard the others.
10171 The AT_*_pc values for such functions refer to local labels in
10172 these sections. If the section from that file was discarded, the
10173 labels are not in the output, so the relocs get a value of 0.
10174 If this is a discarded function, mark the pc bounds as invalid,
10175 so that GDB will ignore it. */
10176 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10177 return 0;
10178
10179 *lowpc = low;
10180 if (highpc)
10181 *highpc = high;
10182 return ret;
10183 }
10184
10185 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10186 its low and high PC addresses. Do nothing if these addresses could not
10187 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10188 and HIGHPC to the high address if greater than HIGHPC. */
10189
10190 static void
10191 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10192 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10193 struct dwarf2_cu *cu)
10194 {
10195 CORE_ADDR low, high;
10196 struct die_info *child = die->child;
10197
10198 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10199 {
10200 *lowpc = min (*lowpc, low);
10201 *highpc = max (*highpc, high);
10202 }
10203
10204 /* If the language does not allow nested subprograms (either inside
10205 subprograms or lexical blocks), we're done. */
10206 if (cu->language != language_ada)
10207 return;
10208
10209 /* Check all the children of the given DIE. If it contains nested
10210 subprograms, then check their pc bounds. Likewise, we need to
10211 check lexical blocks as well, as they may also contain subprogram
10212 definitions. */
10213 while (child && child->tag)
10214 {
10215 if (child->tag == DW_TAG_subprogram
10216 || child->tag == DW_TAG_lexical_block)
10217 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10218 child = sibling_die (child);
10219 }
10220 }
10221
10222 /* Get the low and high pc's represented by the scope DIE, and store
10223 them in *LOWPC and *HIGHPC. If the correct values can't be
10224 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10225
10226 static void
10227 get_scope_pc_bounds (struct die_info *die,
10228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10229 struct dwarf2_cu *cu)
10230 {
10231 CORE_ADDR best_low = (CORE_ADDR) -1;
10232 CORE_ADDR best_high = (CORE_ADDR) 0;
10233 CORE_ADDR current_low, current_high;
10234
10235 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10236 {
10237 best_low = current_low;
10238 best_high = current_high;
10239 }
10240 else
10241 {
10242 struct die_info *child = die->child;
10243
10244 while (child && child->tag)
10245 {
10246 switch (child->tag) {
10247 case DW_TAG_subprogram:
10248 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10249 break;
10250 case DW_TAG_namespace:
10251 case DW_TAG_module:
10252 /* FIXME: carlton/2004-01-16: Should we do this for
10253 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10254 that current GCC's always emit the DIEs corresponding
10255 to definitions of methods of classes as children of a
10256 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10257 the DIEs giving the declarations, which could be
10258 anywhere). But I don't see any reason why the
10259 standards says that they have to be there. */
10260 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10261
10262 if (current_low != ((CORE_ADDR) -1))
10263 {
10264 best_low = min (best_low, current_low);
10265 best_high = max (best_high, current_high);
10266 }
10267 break;
10268 default:
10269 /* Ignore. */
10270 break;
10271 }
10272
10273 child = sibling_die (child);
10274 }
10275 }
10276
10277 *lowpc = best_low;
10278 *highpc = best_high;
10279 }
10280
10281 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10282 in DIE. */
10283
10284 static void
10285 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10286 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10287 {
10288 struct objfile *objfile = cu->objfile;
10289 struct attribute *attr;
10290 struct attribute *attr_high;
10291
10292 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10293 if (attr_high)
10294 {
10295 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10296 if (attr)
10297 {
10298 CORE_ADDR low = DW_ADDR (attr);
10299 CORE_ADDR high;
10300 if (attr_high->form == DW_FORM_addr
10301 || attr_high->form == DW_FORM_GNU_addr_index)
10302 high = DW_ADDR (attr_high);
10303 else
10304 high = low + DW_UNSND (attr_high);
10305
10306 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10307 }
10308 }
10309
10310 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10311 if (attr)
10312 {
10313 bfd *obfd = objfile->obfd;
10314 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10315 We take advantage of the fact that DW_AT_ranges does not appear
10316 in DW_TAG_compile_unit of DWO files. */
10317 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10318
10319 /* The value of the DW_AT_ranges attribute is the offset of the
10320 address range list in the .debug_ranges section. */
10321 unsigned long offset = (DW_UNSND (attr)
10322 + (need_ranges_base ? cu->ranges_base : 0));
10323 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10324
10325 /* For some target architectures, but not others, the
10326 read_address function sign-extends the addresses it returns.
10327 To recognize base address selection entries, we need a
10328 mask. */
10329 unsigned int addr_size = cu->header.addr_size;
10330 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10331
10332 /* The base address, to which the next pair is relative. Note
10333 that this 'base' is a DWARF concept: most entries in a range
10334 list are relative, to reduce the number of relocs against the
10335 debugging information. This is separate from this function's
10336 'baseaddr' argument, which GDB uses to relocate debugging
10337 information from a shared library based on the address at
10338 which the library was loaded. */
10339 CORE_ADDR base = cu->base_address;
10340 int base_known = cu->base_known;
10341
10342 gdb_assert (dwarf2_per_objfile->ranges.readin);
10343 if (offset >= dwarf2_per_objfile->ranges.size)
10344 {
10345 complaint (&symfile_complaints,
10346 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10347 offset);
10348 return;
10349 }
10350
10351 for (;;)
10352 {
10353 unsigned int bytes_read;
10354 CORE_ADDR start, end;
10355
10356 start = read_address (obfd, buffer, cu, &bytes_read);
10357 buffer += bytes_read;
10358 end = read_address (obfd, buffer, cu, &bytes_read);
10359 buffer += bytes_read;
10360
10361 /* Did we find the end of the range list? */
10362 if (start == 0 && end == 0)
10363 break;
10364
10365 /* Did we find a base address selection entry? */
10366 else if ((start & base_select_mask) == base_select_mask)
10367 {
10368 base = end;
10369 base_known = 1;
10370 }
10371
10372 /* We found an ordinary address range. */
10373 else
10374 {
10375 if (!base_known)
10376 {
10377 complaint (&symfile_complaints,
10378 _("Invalid .debug_ranges data "
10379 "(no base address)"));
10380 return;
10381 }
10382
10383 if (start > end)
10384 {
10385 /* Inverted range entries are invalid. */
10386 complaint (&symfile_complaints,
10387 _("Invalid .debug_ranges data "
10388 "(inverted range)"));
10389 return;
10390 }
10391
10392 /* Empty range entries have no effect. */
10393 if (start == end)
10394 continue;
10395
10396 start += base + baseaddr;
10397 end += base + baseaddr;
10398
10399 /* A not-uncommon case of bad debug info.
10400 Don't pollute the addrmap with bad data. */
10401 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10402 {
10403 complaint (&symfile_complaints,
10404 _(".debug_ranges entry has start address of zero"
10405 " [in module %s]"), objfile->name);
10406 continue;
10407 }
10408
10409 record_block_range (block, start, end - 1);
10410 }
10411 }
10412 }
10413 }
10414
10415 /* Check whether the producer field indicates either of GCC < 4.6, or the
10416 Intel C/C++ compiler, and cache the result in CU. */
10417
10418 static void
10419 check_producer (struct dwarf2_cu *cu)
10420 {
10421 const char *cs;
10422 int major, minor, release;
10423
10424 if (cu->producer == NULL)
10425 {
10426 /* For unknown compilers expect their behavior is DWARF version
10427 compliant.
10428
10429 GCC started to support .debug_types sections by -gdwarf-4 since
10430 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10431 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10432 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10433 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10434 }
10435 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10436 {
10437 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10438
10439 cs = &cu->producer[strlen ("GNU ")];
10440 while (*cs && !isdigit (*cs))
10441 cs++;
10442 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10443 {
10444 /* Not recognized as GCC. */
10445 }
10446 else
10447 {
10448 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10449 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10450 }
10451 }
10452 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10453 cu->producer_is_icc = 1;
10454 else
10455 {
10456 /* For other non-GCC compilers, expect their behavior is DWARF version
10457 compliant. */
10458 }
10459
10460 cu->checked_producer = 1;
10461 }
10462
10463 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10464 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10465 during 4.6.0 experimental. */
10466
10467 static int
10468 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10469 {
10470 if (!cu->checked_producer)
10471 check_producer (cu);
10472
10473 return cu->producer_is_gxx_lt_4_6;
10474 }
10475
10476 /* Return the default accessibility type if it is not overriden by
10477 DW_AT_accessibility. */
10478
10479 static enum dwarf_access_attribute
10480 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10481 {
10482 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10483 {
10484 /* The default DWARF 2 accessibility for members is public, the default
10485 accessibility for inheritance is private. */
10486
10487 if (die->tag != DW_TAG_inheritance)
10488 return DW_ACCESS_public;
10489 else
10490 return DW_ACCESS_private;
10491 }
10492 else
10493 {
10494 /* DWARF 3+ defines the default accessibility a different way. The same
10495 rules apply now for DW_TAG_inheritance as for the members and it only
10496 depends on the container kind. */
10497
10498 if (die->parent->tag == DW_TAG_class_type)
10499 return DW_ACCESS_private;
10500 else
10501 return DW_ACCESS_public;
10502 }
10503 }
10504
10505 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10506 offset. If the attribute was not found return 0, otherwise return
10507 1. If it was found but could not properly be handled, set *OFFSET
10508 to 0. */
10509
10510 static int
10511 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10512 LONGEST *offset)
10513 {
10514 struct attribute *attr;
10515
10516 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10517 if (attr != NULL)
10518 {
10519 *offset = 0;
10520
10521 /* Note that we do not check for a section offset first here.
10522 This is because DW_AT_data_member_location is new in DWARF 4,
10523 so if we see it, we can assume that a constant form is really
10524 a constant and not a section offset. */
10525 if (attr_form_is_constant (attr))
10526 *offset = dwarf2_get_attr_constant_value (attr, 0);
10527 else if (attr_form_is_section_offset (attr))
10528 dwarf2_complex_location_expr_complaint ();
10529 else if (attr_form_is_block (attr))
10530 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10531 else
10532 dwarf2_complex_location_expr_complaint ();
10533
10534 return 1;
10535 }
10536
10537 return 0;
10538 }
10539
10540 /* Add an aggregate field to the field list. */
10541
10542 static void
10543 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10544 struct dwarf2_cu *cu)
10545 {
10546 struct objfile *objfile = cu->objfile;
10547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10548 struct nextfield *new_field;
10549 struct attribute *attr;
10550 struct field *fp;
10551 char *fieldname = "";
10552
10553 /* Allocate a new field list entry and link it in. */
10554 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10555 make_cleanup (xfree, new_field);
10556 memset (new_field, 0, sizeof (struct nextfield));
10557
10558 if (die->tag == DW_TAG_inheritance)
10559 {
10560 new_field->next = fip->baseclasses;
10561 fip->baseclasses = new_field;
10562 }
10563 else
10564 {
10565 new_field->next = fip->fields;
10566 fip->fields = new_field;
10567 }
10568 fip->nfields++;
10569
10570 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10571 if (attr)
10572 new_field->accessibility = DW_UNSND (attr);
10573 else
10574 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10575 if (new_field->accessibility != DW_ACCESS_public)
10576 fip->non_public_fields = 1;
10577
10578 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10579 if (attr)
10580 new_field->virtuality = DW_UNSND (attr);
10581 else
10582 new_field->virtuality = DW_VIRTUALITY_none;
10583
10584 fp = &new_field->field;
10585
10586 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10587 {
10588 LONGEST offset;
10589
10590 /* Data member other than a C++ static data member. */
10591
10592 /* Get type of field. */
10593 fp->type = die_type (die, cu);
10594
10595 SET_FIELD_BITPOS (*fp, 0);
10596
10597 /* Get bit size of field (zero if none). */
10598 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10599 if (attr)
10600 {
10601 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10602 }
10603 else
10604 {
10605 FIELD_BITSIZE (*fp) = 0;
10606 }
10607
10608 /* Get bit offset of field. */
10609 if (handle_data_member_location (die, cu, &offset))
10610 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10611 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10612 if (attr)
10613 {
10614 if (gdbarch_bits_big_endian (gdbarch))
10615 {
10616 /* For big endian bits, the DW_AT_bit_offset gives the
10617 additional bit offset from the MSB of the containing
10618 anonymous object to the MSB of the field. We don't
10619 have to do anything special since we don't need to
10620 know the size of the anonymous object. */
10621 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10622 }
10623 else
10624 {
10625 /* For little endian bits, compute the bit offset to the
10626 MSB of the anonymous object, subtract off the number of
10627 bits from the MSB of the field to the MSB of the
10628 object, and then subtract off the number of bits of
10629 the field itself. The result is the bit offset of
10630 the LSB of the field. */
10631 int anonymous_size;
10632 int bit_offset = DW_UNSND (attr);
10633
10634 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10635 if (attr)
10636 {
10637 /* The size of the anonymous object containing
10638 the bit field is explicit, so use the
10639 indicated size (in bytes). */
10640 anonymous_size = DW_UNSND (attr);
10641 }
10642 else
10643 {
10644 /* The size of the anonymous object containing
10645 the bit field must be inferred from the type
10646 attribute of the data member containing the
10647 bit field. */
10648 anonymous_size = TYPE_LENGTH (fp->type);
10649 }
10650 SET_FIELD_BITPOS (*fp,
10651 (FIELD_BITPOS (*fp)
10652 + anonymous_size * bits_per_byte
10653 - bit_offset - FIELD_BITSIZE (*fp)));
10654 }
10655 }
10656
10657 /* Get name of field. */
10658 fieldname = dwarf2_name (die, cu);
10659 if (fieldname == NULL)
10660 fieldname = "";
10661
10662 /* The name is already allocated along with this objfile, so we don't
10663 need to duplicate it for the type. */
10664 fp->name = fieldname;
10665
10666 /* Change accessibility for artificial fields (e.g. virtual table
10667 pointer or virtual base class pointer) to private. */
10668 if (dwarf2_attr (die, DW_AT_artificial, cu))
10669 {
10670 FIELD_ARTIFICIAL (*fp) = 1;
10671 new_field->accessibility = DW_ACCESS_private;
10672 fip->non_public_fields = 1;
10673 }
10674 }
10675 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10676 {
10677 /* C++ static member. */
10678
10679 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10680 is a declaration, but all versions of G++ as of this writing
10681 (so through at least 3.2.1) incorrectly generate
10682 DW_TAG_variable tags. */
10683
10684 const char *physname;
10685
10686 /* Get name of field. */
10687 fieldname = dwarf2_name (die, cu);
10688 if (fieldname == NULL)
10689 return;
10690
10691 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10692 if (attr
10693 /* Only create a symbol if this is an external value.
10694 new_symbol checks this and puts the value in the global symbol
10695 table, which we want. If it is not external, new_symbol
10696 will try to put the value in cu->list_in_scope which is wrong. */
10697 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10698 {
10699 /* A static const member, not much different than an enum as far as
10700 we're concerned, except that we can support more types. */
10701 new_symbol (die, NULL, cu);
10702 }
10703
10704 /* Get physical name. */
10705 physname = dwarf2_physname (fieldname, die, cu);
10706
10707 /* The name is already allocated along with this objfile, so we don't
10708 need to duplicate it for the type. */
10709 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10710 FIELD_TYPE (*fp) = die_type (die, cu);
10711 FIELD_NAME (*fp) = fieldname;
10712 }
10713 else if (die->tag == DW_TAG_inheritance)
10714 {
10715 LONGEST offset;
10716
10717 /* C++ base class field. */
10718 if (handle_data_member_location (die, cu, &offset))
10719 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10720 FIELD_BITSIZE (*fp) = 0;
10721 FIELD_TYPE (*fp) = die_type (die, cu);
10722 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10723 fip->nbaseclasses++;
10724 }
10725 }
10726
10727 /* Add a typedef defined in the scope of the FIP's class. */
10728
10729 static void
10730 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10731 struct dwarf2_cu *cu)
10732 {
10733 struct objfile *objfile = cu->objfile;
10734 struct typedef_field_list *new_field;
10735 struct attribute *attr;
10736 struct typedef_field *fp;
10737 char *fieldname = "";
10738
10739 /* Allocate a new field list entry and link it in. */
10740 new_field = xzalloc (sizeof (*new_field));
10741 make_cleanup (xfree, new_field);
10742
10743 gdb_assert (die->tag == DW_TAG_typedef);
10744
10745 fp = &new_field->field;
10746
10747 /* Get name of field. */
10748 fp->name = dwarf2_name (die, cu);
10749 if (fp->name == NULL)
10750 return;
10751
10752 fp->type = read_type_die (die, cu);
10753
10754 new_field->next = fip->typedef_field_list;
10755 fip->typedef_field_list = new_field;
10756 fip->typedef_field_list_count++;
10757 }
10758
10759 /* Create the vector of fields, and attach it to the type. */
10760
10761 static void
10762 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10763 struct dwarf2_cu *cu)
10764 {
10765 int nfields = fip->nfields;
10766
10767 /* Record the field count, allocate space for the array of fields,
10768 and create blank accessibility bitfields if necessary. */
10769 TYPE_NFIELDS (type) = nfields;
10770 TYPE_FIELDS (type) = (struct field *)
10771 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10772 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10773
10774 if (fip->non_public_fields && cu->language != language_ada)
10775 {
10776 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10777
10778 TYPE_FIELD_PRIVATE_BITS (type) =
10779 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10780 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10781
10782 TYPE_FIELD_PROTECTED_BITS (type) =
10783 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10784 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10785
10786 TYPE_FIELD_IGNORE_BITS (type) =
10787 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10788 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10789 }
10790
10791 /* If the type has baseclasses, allocate and clear a bit vector for
10792 TYPE_FIELD_VIRTUAL_BITS. */
10793 if (fip->nbaseclasses && cu->language != language_ada)
10794 {
10795 int num_bytes = B_BYTES (fip->nbaseclasses);
10796 unsigned char *pointer;
10797
10798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10799 pointer = TYPE_ALLOC (type, num_bytes);
10800 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10801 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10802 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10803 }
10804
10805 /* Copy the saved-up fields into the field vector. Start from the head of
10806 the list, adding to the tail of the field array, so that they end up in
10807 the same order in the array in which they were added to the list. */
10808 while (nfields-- > 0)
10809 {
10810 struct nextfield *fieldp;
10811
10812 if (fip->fields)
10813 {
10814 fieldp = fip->fields;
10815 fip->fields = fieldp->next;
10816 }
10817 else
10818 {
10819 fieldp = fip->baseclasses;
10820 fip->baseclasses = fieldp->next;
10821 }
10822
10823 TYPE_FIELD (type, nfields) = fieldp->field;
10824 switch (fieldp->accessibility)
10825 {
10826 case DW_ACCESS_private:
10827 if (cu->language != language_ada)
10828 SET_TYPE_FIELD_PRIVATE (type, nfields);
10829 break;
10830
10831 case DW_ACCESS_protected:
10832 if (cu->language != language_ada)
10833 SET_TYPE_FIELD_PROTECTED (type, nfields);
10834 break;
10835
10836 case DW_ACCESS_public:
10837 break;
10838
10839 default:
10840 /* Unknown accessibility. Complain and treat it as public. */
10841 {
10842 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10843 fieldp->accessibility);
10844 }
10845 break;
10846 }
10847 if (nfields < fip->nbaseclasses)
10848 {
10849 switch (fieldp->virtuality)
10850 {
10851 case DW_VIRTUALITY_virtual:
10852 case DW_VIRTUALITY_pure_virtual:
10853 if (cu->language == language_ada)
10854 error (_("unexpected virtuality in component of Ada type"));
10855 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10856 break;
10857 }
10858 }
10859 }
10860 }
10861
10862 /* Return true if this member function is a constructor, false
10863 otherwise. */
10864
10865 static int
10866 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10867 {
10868 const char *fieldname;
10869 const char *typename;
10870 int len;
10871
10872 if (die->parent == NULL)
10873 return 0;
10874
10875 if (die->parent->tag != DW_TAG_structure_type
10876 && die->parent->tag != DW_TAG_union_type
10877 && die->parent->tag != DW_TAG_class_type)
10878 return 0;
10879
10880 fieldname = dwarf2_name (die, cu);
10881 typename = dwarf2_name (die->parent, cu);
10882 if (fieldname == NULL || typename == NULL)
10883 return 0;
10884
10885 len = strlen (fieldname);
10886 return (strncmp (fieldname, typename, len) == 0
10887 && (typename[len] == '\0' || typename[len] == '<'));
10888 }
10889
10890 /* Add a member function to the proper fieldlist. */
10891
10892 static void
10893 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10894 struct type *type, struct dwarf2_cu *cu)
10895 {
10896 struct objfile *objfile = cu->objfile;
10897 struct attribute *attr;
10898 struct fnfieldlist *flp;
10899 int i;
10900 struct fn_field *fnp;
10901 char *fieldname;
10902 struct nextfnfield *new_fnfield;
10903 struct type *this_type;
10904 enum dwarf_access_attribute accessibility;
10905
10906 if (cu->language == language_ada)
10907 error (_("unexpected member function in Ada type"));
10908
10909 /* Get name of member function. */
10910 fieldname = dwarf2_name (die, cu);
10911 if (fieldname == NULL)
10912 return;
10913
10914 /* Look up member function name in fieldlist. */
10915 for (i = 0; i < fip->nfnfields; i++)
10916 {
10917 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10918 break;
10919 }
10920
10921 /* Create new list element if necessary. */
10922 if (i < fip->nfnfields)
10923 flp = &fip->fnfieldlists[i];
10924 else
10925 {
10926 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10927 {
10928 fip->fnfieldlists = (struct fnfieldlist *)
10929 xrealloc (fip->fnfieldlists,
10930 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10931 * sizeof (struct fnfieldlist));
10932 if (fip->nfnfields == 0)
10933 make_cleanup (free_current_contents, &fip->fnfieldlists);
10934 }
10935 flp = &fip->fnfieldlists[fip->nfnfields];
10936 flp->name = fieldname;
10937 flp->length = 0;
10938 flp->head = NULL;
10939 i = fip->nfnfields++;
10940 }
10941
10942 /* Create a new member function field and chain it to the field list
10943 entry. */
10944 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10945 make_cleanup (xfree, new_fnfield);
10946 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10947 new_fnfield->next = flp->head;
10948 flp->head = new_fnfield;
10949 flp->length++;
10950
10951 /* Fill in the member function field info. */
10952 fnp = &new_fnfield->fnfield;
10953
10954 /* Delay processing of the physname until later. */
10955 if (cu->language == language_cplus || cu->language == language_java)
10956 {
10957 add_to_method_list (type, i, flp->length - 1, fieldname,
10958 die, cu);
10959 }
10960 else
10961 {
10962 const char *physname = dwarf2_physname (fieldname, die, cu);
10963 fnp->physname = physname ? physname : "";
10964 }
10965
10966 fnp->type = alloc_type (objfile);
10967 this_type = read_type_die (die, cu);
10968 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10969 {
10970 int nparams = TYPE_NFIELDS (this_type);
10971
10972 /* TYPE is the domain of this method, and THIS_TYPE is the type
10973 of the method itself (TYPE_CODE_METHOD). */
10974 smash_to_method_type (fnp->type, type,
10975 TYPE_TARGET_TYPE (this_type),
10976 TYPE_FIELDS (this_type),
10977 TYPE_NFIELDS (this_type),
10978 TYPE_VARARGS (this_type));
10979
10980 /* Handle static member functions.
10981 Dwarf2 has no clean way to discern C++ static and non-static
10982 member functions. G++ helps GDB by marking the first
10983 parameter for non-static member functions (which is the this
10984 pointer) as artificial. We obtain this information from
10985 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10986 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10987 fnp->voffset = VOFFSET_STATIC;
10988 }
10989 else
10990 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10991 dwarf2_full_name (fieldname, die, cu));
10992
10993 /* Get fcontext from DW_AT_containing_type if present. */
10994 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10995 fnp->fcontext = die_containing_type (die, cu);
10996
10997 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10998 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10999
11000 /* Get accessibility. */
11001 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11002 if (attr)
11003 accessibility = DW_UNSND (attr);
11004 else
11005 accessibility = dwarf2_default_access_attribute (die, cu);
11006 switch (accessibility)
11007 {
11008 case DW_ACCESS_private:
11009 fnp->is_private = 1;
11010 break;
11011 case DW_ACCESS_protected:
11012 fnp->is_protected = 1;
11013 break;
11014 }
11015
11016 /* Check for artificial methods. */
11017 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11018 if (attr && DW_UNSND (attr) != 0)
11019 fnp->is_artificial = 1;
11020
11021 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11022
11023 /* Get index in virtual function table if it is a virtual member
11024 function. For older versions of GCC, this is an offset in the
11025 appropriate virtual table, as specified by DW_AT_containing_type.
11026 For everyone else, it is an expression to be evaluated relative
11027 to the object address. */
11028
11029 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11030 if (attr)
11031 {
11032 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11033 {
11034 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11035 {
11036 /* Old-style GCC. */
11037 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11038 }
11039 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11040 || (DW_BLOCK (attr)->size > 1
11041 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11042 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11043 {
11044 struct dwarf_block blk;
11045 int offset;
11046
11047 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11048 ? 1 : 2);
11049 blk.size = DW_BLOCK (attr)->size - offset;
11050 blk.data = DW_BLOCK (attr)->data + offset;
11051 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11052 if ((fnp->voffset % cu->header.addr_size) != 0)
11053 dwarf2_complex_location_expr_complaint ();
11054 else
11055 fnp->voffset /= cu->header.addr_size;
11056 fnp->voffset += 2;
11057 }
11058 else
11059 dwarf2_complex_location_expr_complaint ();
11060
11061 if (!fnp->fcontext)
11062 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11063 }
11064 else if (attr_form_is_section_offset (attr))
11065 {
11066 dwarf2_complex_location_expr_complaint ();
11067 }
11068 else
11069 {
11070 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11071 fieldname);
11072 }
11073 }
11074 else
11075 {
11076 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11077 if (attr && DW_UNSND (attr))
11078 {
11079 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11080 complaint (&symfile_complaints,
11081 _("Member function \"%s\" (offset %d) is virtual "
11082 "but the vtable offset is not specified"),
11083 fieldname, die->offset.sect_off);
11084 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11085 TYPE_CPLUS_DYNAMIC (type) = 1;
11086 }
11087 }
11088 }
11089
11090 /* Create the vector of member function fields, and attach it to the type. */
11091
11092 static void
11093 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11094 struct dwarf2_cu *cu)
11095 {
11096 struct fnfieldlist *flp;
11097 int i;
11098
11099 if (cu->language == language_ada)
11100 error (_("unexpected member functions in Ada type"));
11101
11102 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11103 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11104 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11105
11106 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11107 {
11108 struct nextfnfield *nfp = flp->head;
11109 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11110 int k;
11111
11112 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11113 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11114 fn_flp->fn_fields = (struct fn_field *)
11115 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11116 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11117 fn_flp->fn_fields[k] = nfp->fnfield;
11118 }
11119
11120 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11121 }
11122
11123 /* Returns non-zero if NAME is the name of a vtable member in CU's
11124 language, zero otherwise. */
11125 static int
11126 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11127 {
11128 static const char vptr[] = "_vptr";
11129 static const char vtable[] = "vtable";
11130
11131 /* Look for the C++ and Java forms of the vtable. */
11132 if ((cu->language == language_java
11133 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11134 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11135 && is_cplus_marker (name[sizeof (vptr) - 1])))
11136 return 1;
11137
11138 return 0;
11139 }
11140
11141 /* GCC outputs unnamed structures that are really pointers to member
11142 functions, with the ABI-specified layout. If TYPE describes
11143 such a structure, smash it into a member function type.
11144
11145 GCC shouldn't do this; it should just output pointer to member DIEs.
11146 This is GCC PR debug/28767. */
11147
11148 static void
11149 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11150 {
11151 struct type *pfn_type, *domain_type, *new_type;
11152
11153 /* Check for a structure with no name and two children. */
11154 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11155 return;
11156
11157 /* Check for __pfn and __delta members. */
11158 if (TYPE_FIELD_NAME (type, 0) == NULL
11159 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11160 || TYPE_FIELD_NAME (type, 1) == NULL
11161 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11162 return;
11163
11164 /* Find the type of the method. */
11165 pfn_type = TYPE_FIELD_TYPE (type, 0);
11166 if (pfn_type == NULL
11167 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11168 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11169 return;
11170
11171 /* Look for the "this" argument. */
11172 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11173 if (TYPE_NFIELDS (pfn_type) == 0
11174 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11175 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11176 return;
11177
11178 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11179 new_type = alloc_type (objfile);
11180 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11181 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11182 TYPE_VARARGS (pfn_type));
11183 smash_to_methodptr_type (type, new_type);
11184 }
11185
11186 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11187 (icc). */
11188
11189 static int
11190 producer_is_icc (struct dwarf2_cu *cu)
11191 {
11192 if (!cu->checked_producer)
11193 check_producer (cu);
11194
11195 return cu->producer_is_icc;
11196 }
11197
11198 /* Called when we find the DIE that starts a structure or union scope
11199 (definition) to create a type for the structure or union. Fill in
11200 the type's name and general properties; the members will not be
11201 processed until process_structure_type.
11202
11203 NOTE: we need to call these functions regardless of whether or not the
11204 DIE has a DW_AT_name attribute, since it might be an anonymous
11205 structure or union. This gets the type entered into our set of
11206 user defined types.
11207
11208 However, if the structure is incomplete (an opaque struct/union)
11209 then suppress creating a symbol table entry for it since gdb only
11210 wants to find the one with the complete definition. Note that if
11211 it is complete, we just call new_symbol, which does it's own
11212 checking about whether the struct/union is anonymous or not (and
11213 suppresses creating a symbol table entry itself). */
11214
11215 static struct type *
11216 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11217 {
11218 struct objfile *objfile = cu->objfile;
11219 struct type *type;
11220 struct attribute *attr;
11221 char *name;
11222
11223 /* If the definition of this type lives in .debug_types, read that type.
11224 Don't follow DW_AT_specification though, that will take us back up
11225 the chain and we want to go down. */
11226 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11227 if (attr)
11228 {
11229 struct dwarf2_cu *type_cu = cu;
11230 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11231
11232 /* We could just recurse on read_structure_type, but we need to call
11233 get_die_type to ensure only one type for this DIE is created.
11234 This is important, for example, because for c++ classes we need
11235 TYPE_NAME set which is only done by new_symbol. Blech. */
11236 type = read_type_die (type_die, type_cu);
11237
11238 /* TYPE_CU may not be the same as CU.
11239 Ensure TYPE is recorded in CU's type_hash table. */
11240 return set_die_type (die, type, cu);
11241 }
11242
11243 type = alloc_type (objfile);
11244 INIT_CPLUS_SPECIFIC (type);
11245
11246 name = dwarf2_name (die, cu);
11247 if (name != NULL)
11248 {
11249 if (cu->language == language_cplus
11250 || cu->language == language_java)
11251 {
11252 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11253
11254 /* dwarf2_full_name might have already finished building the DIE's
11255 type. If so, there is no need to continue. */
11256 if (get_die_type (die, cu) != NULL)
11257 return get_die_type (die, cu);
11258
11259 TYPE_TAG_NAME (type) = full_name;
11260 if (die->tag == DW_TAG_structure_type
11261 || die->tag == DW_TAG_class_type)
11262 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11263 }
11264 else
11265 {
11266 /* The name is already allocated along with this objfile, so
11267 we don't need to duplicate it for the type. */
11268 TYPE_TAG_NAME (type) = (char *) name;
11269 if (die->tag == DW_TAG_class_type)
11270 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11271 }
11272 }
11273
11274 if (die->tag == DW_TAG_structure_type)
11275 {
11276 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11277 }
11278 else if (die->tag == DW_TAG_union_type)
11279 {
11280 TYPE_CODE (type) = TYPE_CODE_UNION;
11281 }
11282 else
11283 {
11284 TYPE_CODE (type) = TYPE_CODE_CLASS;
11285 }
11286
11287 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11288 TYPE_DECLARED_CLASS (type) = 1;
11289
11290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11291 if (attr)
11292 {
11293 TYPE_LENGTH (type) = DW_UNSND (attr);
11294 }
11295 else
11296 {
11297 TYPE_LENGTH (type) = 0;
11298 }
11299
11300 if (producer_is_icc (cu))
11301 {
11302 /* ICC does not output the required DW_AT_declaration
11303 on incomplete types, but gives them a size of zero. */
11304 }
11305 else
11306 TYPE_STUB_SUPPORTED (type) = 1;
11307
11308 if (die_is_declaration (die, cu))
11309 TYPE_STUB (type) = 1;
11310 else if (attr == NULL && die->child == NULL
11311 && producer_is_realview (cu->producer))
11312 /* RealView does not output the required DW_AT_declaration
11313 on incomplete types. */
11314 TYPE_STUB (type) = 1;
11315
11316 /* We need to add the type field to the die immediately so we don't
11317 infinitely recurse when dealing with pointers to the structure
11318 type within the structure itself. */
11319 set_die_type (die, type, cu);
11320
11321 /* set_die_type should be already done. */
11322 set_descriptive_type (type, die, cu);
11323
11324 return type;
11325 }
11326
11327 /* Finish creating a structure or union type, including filling in
11328 its members and creating a symbol for it. */
11329
11330 static void
11331 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11332 {
11333 struct objfile *objfile = cu->objfile;
11334 struct die_info *child_die = die->child;
11335 struct type *type;
11336
11337 type = get_die_type (die, cu);
11338 if (type == NULL)
11339 type = read_structure_type (die, cu);
11340
11341 if (die->child != NULL && ! die_is_declaration (die, cu))
11342 {
11343 struct field_info fi;
11344 struct die_info *child_die;
11345 VEC (symbolp) *template_args = NULL;
11346 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11347
11348 memset (&fi, 0, sizeof (struct field_info));
11349
11350 child_die = die->child;
11351
11352 while (child_die && child_die->tag)
11353 {
11354 if (child_die->tag == DW_TAG_member
11355 || child_die->tag == DW_TAG_variable)
11356 {
11357 /* NOTE: carlton/2002-11-05: A C++ static data member
11358 should be a DW_TAG_member that is a declaration, but
11359 all versions of G++ as of this writing (so through at
11360 least 3.2.1) incorrectly generate DW_TAG_variable
11361 tags for them instead. */
11362 dwarf2_add_field (&fi, child_die, cu);
11363 }
11364 else if (child_die->tag == DW_TAG_subprogram)
11365 {
11366 /* C++ member function. */
11367 dwarf2_add_member_fn (&fi, child_die, type, cu);
11368 }
11369 else if (child_die->tag == DW_TAG_inheritance)
11370 {
11371 /* C++ base class field. */
11372 dwarf2_add_field (&fi, child_die, cu);
11373 }
11374 else if (child_die->tag == DW_TAG_typedef)
11375 dwarf2_add_typedef (&fi, child_die, cu);
11376 else if (child_die->tag == DW_TAG_template_type_param
11377 || child_die->tag == DW_TAG_template_value_param)
11378 {
11379 struct symbol *arg = new_symbol (child_die, NULL, cu);
11380
11381 if (arg != NULL)
11382 VEC_safe_push (symbolp, template_args, arg);
11383 }
11384
11385 child_die = sibling_die (child_die);
11386 }
11387
11388 /* Attach template arguments to type. */
11389 if (! VEC_empty (symbolp, template_args))
11390 {
11391 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11392 TYPE_N_TEMPLATE_ARGUMENTS (type)
11393 = VEC_length (symbolp, template_args);
11394 TYPE_TEMPLATE_ARGUMENTS (type)
11395 = obstack_alloc (&objfile->objfile_obstack,
11396 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11397 * sizeof (struct symbol *)));
11398 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11399 VEC_address (symbolp, template_args),
11400 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11401 * sizeof (struct symbol *)));
11402 VEC_free (symbolp, template_args);
11403 }
11404
11405 /* Attach fields and member functions to the type. */
11406 if (fi.nfields)
11407 dwarf2_attach_fields_to_type (&fi, type, cu);
11408 if (fi.nfnfields)
11409 {
11410 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11411
11412 /* Get the type which refers to the base class (possibly this
11413 class itself) which contains the vtable pointer for the current
11414 class from the DW_AT_containing_type attribute. This use of
11415 DW_AT_containing_type is a GNU extension. */
11416
11417 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11418 {
11419 struct type *t = die_containing_type (die, cu);
11420
11421 TYPE_VPTR_BASETYPE (type) = t;
11422 if (type == t)
11423 {
11424 int i;
11425
11426 /* Our own class provides vtbl ptr. */
11427 for (i = TYPE_NFIELDS (t) - 1;
11428 i >= TYPE_N_BASECLASSES (t);
11429 --i)
11430 {
11431 const char *fieldname = TYPE_FIELD_NAME (t, i);
11432
11433 if (is_vtable_name (fieldname, cu))
11434 {
11435 TYPE_VPTR_FIELDNO (type) = i;
11436 break;
11437 }
11438 }
11439
11440 /* Complain if virtual function table field not found. */
11441 if (i < TYPE_N_BASECLASSES (t))
11442 complaint (&symfile_complaints,
11443 _("virtual function table pointer "
11444 "not found when defining class '%s'"),
11445 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11446 "");
11447 }
11448 else
11449 {
11450 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11451 }
11452 }
11453 else if (cu->producer
11454 && strncmp (cu->producer,
11455 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11456 {
11457 /* The IBM XLC compiler does not provide direct indication
11458 of the containing type, but the vtable pointer is
11459 always named __vfp. */
11460
11461 int i;
11462
11463 for (i = TYPE_NFIELDS (type) - 1;
11464 i >= TYPE_N_BASECLASSES (type);
11465 --i)
11466 {
11467 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11468 {
11469 TYPE_VPTR_FIELDNO (type) = i;
11470 TYPE_VPTR_BASETYPE (type) = type;
11471 break;
11472 }
11473 }
11474 }
11475 }
11476
11477 /* Copy fi.typedef_field_list linked list elements content into the
11478 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11479 if (fi.typedef_field_list)
11480 {
11481 int i = fi.typedef_field_list_count;
11482
11483 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11484 TYPE_TYPEDEF_FIELD_ARRAY (type)
11485 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11486 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11487
11488 /* Reverse the list order to keep the debug info elements order. */
11489 while (--i >= 0)
11490 {
11491 struct typedef_field *dest, *src;
11492
11493 dest = &TYPE_TYPEDEF_FIELD (type, i);
11494 src = &fi.typedef_field_list->field;
11495 fi.typedef_field_list = fi.typedef_field_list->next;
11496 *dest = *src;
11497 }
11498 }
11499
11500 do_cleanups (back_to);
11501
11502 if (HAVE_CPLUS_STRUCT (type))
11503 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11504 }
11505
11506 quirk_gcc_member_function_pointer (type, objfile);
11507
11508 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11509 snapshots) has been known to create a die giving a declaration
11510 for a class that has, as a child, a die giving a definition for a
11511 nested class. So we have to process our children even if the
11512 current die is a declaration. Normally, of course, a declaration
11513 won't have any children at all. */
11514
11515 while (child_die != NULL && child_die->tag)
11516 {
11517 if (child_die->tag == DW_TAG_member
11518 || child_die->tag == DW_TAG_variable
11519 || child_die->tag == DW_TAG_inheritance
11520 || child_die->tag == DW_TAG_template_value_param
11521 || child_die->tag == DW_TAG_template_type_param)
11522 {
11523 /* Do nothing. */
11524 }
11525 else
11526 process_die (child_die, cu);
11527
11528 child_die = sibling_die (child_die);
11529 }
11530
11531 /* Do not consider external references. According to the DWARF standard,
11532 these DIEs are identified by the fact that they have no byte_size
11533 attribute, and a declaration attribute. */
11534 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11535 || !die_is_declaration (die, cu))
11536 new_symbol (die, type, cu);
11537 }
11538
11539 /* Given a DW_AT_enumeration_type die, set its type. We do not
11540 complete the type's fields yet, or create any symbols. */
11541
11542 static struct type *
11543 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11544 {
11545 struct objfile *objfile = cu->objfile;
11546 struct type *type;
11547 struct attribute *attr;
11548 const char *name;
11549
11550 /* If the definition of this type lives in .debug_types, read that type.
11551 Don't follow DW_AT_specification though, that will take us back up
11552 the chain and we want to go down. */
11553 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11554 if (attr)
11555 {
11556 struct dwarf2_cu *type_cu = cu;
11557 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11558
11559 type = read_type_die (type_die, type_cu);
11560
11561 /* TYPE_CU may not be the same as CU.
11562 Ensure TYPE is recorded in CU's type_hash table. */
11563 return set_die_type (die, type, cu);
11564 }
11565
11566 type = alloc_type (objfile);
11567
11568 TYPE_CODE (type) = TYPE_CODE_ENUM;
11569 name = dwarf2_full_name (NULL, die, cu);
11570 if (name != NULL)
11571 TYPE_TAG_NAME (type) = (char *) name;
11572
11573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11574 if (attr)
11575 {
11576 TYPE_LENGTH (type) = DW_UNSND (attr);
11577 }
11578 else
11579 {
11580 TYPE_LENGTH (type) = 0;
11581 }
11582
11583 /* The enumeration DIE can be incomplete. In Ada, any type can be
11584 declared as private in the package spec, and then defined only
11585 inside the package body. Such types are known as Taft Amendment
11586 Types. When another package uses such a type, an incomplete DIE
11587 may be generated by the compiler. */
11588 if (die_is_declaration (die, cu))
11589 TYPE_STUB (type) = 1;
11590
11591 return set_die_type (die, type, cu);
11592 }
11593
11594 /* Given a pointer to a die which begins an enumeration, process all
11595 the dies that define the members of the enumeration, and create the
11596 symbol for the enumeration type.
11597
11598 NOTE: We reverse the order of the element list. */
11599
11600 static void
11601 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11602 {
11603 struct type *this_type;
11604
11605 this_type = get_die_type (die, cu);
11606 if (this_type == NULL)
11607 this_type = read_enumeration_type (die, cu);
11608
11609 if (die->child != NULL)
11610 {
11611 struct die_info *child_die;
11612 struct symbol *sym;
11613 struct field *fields = NULL;
11614 int num_fields = 0;
11615 int unsigned_enum = 1;
11616 char *name;
11617 int flag_enum = 1;
11618 ULONGEST mask = 0;
11619
11620 child_die = die->child;
11621 while (child_die && child_die->tag)
11622 {
11623 if (child_die->tag != DW_TAG_enumerator)
11624 {
11625 process_die (child_die, cu);
11626 }
11627 else
11628 {
11629 name = dwarf2_name (child_die, cu);
11630 if (name)
11631 {
11632 sym = new_symbol (child_die, this_type, cu);
11633 if (SYMBOL_VALUE (sym) < 0)
11634 {
11635 unsigned_enum = 0;
11636 flag_enum = 0;
11637 }
11638 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11639 flag_enum = 0;
11640 else
11641 mask |= SYMBOL_VALUE (sym);
11642
11643 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11644 {
11645 fields = (struct field *)
11646 xrealloc (fields,
11647 (num_fields + DW_FIELD_ALLOC_CHUNK)
11648 * sizeof (struct field));
11649 }
11650
11651 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11652 FIELD_TYPE (fields[num_fields]) = NULL;
11653 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11654 FIELD_BITSIZE (fields[num_fields]) = 0;
11655
11656 num_fields++;
11657 }
11658 }
11659
11660 child_die = sibling_die (child_die);
11661 }
11662
11663 if (num_fields)
11664 {
11665 TYPE_NFIELDS (this_type) = num_fields;
11666 TYPE_FIELDS (this_type) = (struct field *)
11667 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11668 memcpy (TYPE_FIELDS (this_type), fields,
11669 sizeof (struct field) * num_fields);
11670 xfree (fields);
11671 }
11672 if (unsigned_enum)
11673 TYPE_UNSIGNED (this_type) = 1;
11674 if (flag_enum)
11675 TYPE_FLAG_ENUM (this_type) = 1;
11676 }
11677
11678 /* If we are reading an enum from a .debug_types unit, and the enum
11679 is a declaration, and the enum is not the signatured type in the
11680 unit, then we do not want to add a symbol for it. Adding a
11681 symbol would in some cases obscure the true definition of the
11682 enum, giving users an incomplete type when the definition is
11683 actually available. Note that we do not want to do this for all
11684 enums which are just declarations, because C++0x allows forward
11685 enum declarations. */
11686 if (cu->per_cu->is_debug_types
11687 && die_is_declaration (die, cu))
11688 {
11689 struct signatured_type *sig_type;
11690
11691 sig_type
11692 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11693 cu->per_cu->info_or_types_section,
11694 cu->per_cu->offset);
11695 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11696 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11697 return;
11698 }
11699
11700 new_symbol (die, this_type, cu);
11701 }
11702
11703 /* Extract all information from a DW_TAG_array_type DIE and put it in
11704 the DIE's type field. For now, this only handles one dimensional
11705 arrays. */
11706
11707 static struct type *
11708 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11709 {
11710 struct objfile *objfile = cu->objfile;
11711 struct die_info *child_die;
11712 struct type *type;
11713 struct type *element_type, *range_type, *index_type;
11714 struct type **range_types = NULL;
11715 struct attribute *attr;
11716 int ndim = 0;
11717 struct cleanup *back_to;
11718 char *name;
11719
11720 element_type = die_type (die, cu);
11721
11722 /* The die_type call above may have already set the type for this DIE. */
11723 type = get_die_type (die, cu);
11724 if (type)
11725 return type;
11726
11727 /* Irix 6.2 native cc creates array types without children for
11728 arrays with unspecified length. */
11729 if (die->child == NULL)
11730 {
11731 index_type = objfile_type (objfile)->builtin_int;
11732 range_type = create_range_type (NULL, index_type, 0, -1);
11733 type = create_array_type (NULL, element_type, range_type);
11734 return set_die_type (die, type, cu);
11735 }
11736
11737 back_to = make_cleanup (null_cleanup, NULL);
11738 child_die = die->child;
11739 while (child_die && child_die->tag)
11740 {
11741 if (child_die->tag == DW_TAG_subrange_type)
11742 {
11743 struct type *child_type = read_type_die (child_die, cu);
11744
11745 if (child_type != NULL)
11746 {
11747 /* The range type was succesfully read. Save it for the
11748 array type creation. */
11749 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11750 {
11751 range_types = (struct type **)
11752 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11753 * sizeof (struct type *));
11754 if (ndim == 0)
11755 make_cleanup (free_current_contents, &range_types);
11756 }
11757 range_types[ndim++] = child_type;
11758 }
11759 }
11760 child_die = sibling_die (child_die);
11761 }
11762
11763 /* Dwarf2 dimensions are output from left to right, create the
11764 necessary array types in backwards order. */
11765
11766 type = element_type;
11767
11768 if (read_array_order (die, cu) == DW_ORD_col_major)
11769 {
11770 int i = 0;
11771
11772 while (i < ndim)
11773 type = create_array_type (NULL, type, range_types[i++]);
11774 }
11775 else
11776 {
11777 while (ndim-- > 0)
11778 type = create_array_type (NULL, type, range_types[ndim]);
11779 }
11780
11781 /* Understand Dwarf2 support for vector types (like they occur on
11782 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11783 array type. This is not part of the Dwarf2/3 standard yet, but a
11784 custom vendor extension. The main difference between a regular
11785 array and the vector variant is that vectors are passed by value
11786 to functions. */
11787 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11788 if (attr)
11789 make_vector_type (type);
11790
11791 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11792 implementation may choose to implement triple vectors using this
11793 attribute. */
11794 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11795 if (attr)
11796 {
11797 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11798 TYPE_LENGTH (type) = DW_UNSND (attr);
11799 else
11800 complaint (&symfile_complaints,
11801 _("DW_AT_byte_size for array type smaller "
11802 "than the total size of elements"));
11803 }
11804
11805 name = dwarf2_name (die, cu);
11806 if (name)
11807 TYPE_NAME (type) = name;
11808
11809 /* Install the type in the die. */
11810 set_die_type (die, type, cu);
11811
11812 /* set_die_type should be already done. */
11813 set_descriptive_type (type, die, cu);
11814
11815 do_cleanups (back_to);
11816
11817 return type;
11818 }
11819
11820 static enum dwarf_array_dim_ordering
11821 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11822 {
11823 struct attribute *attr;
11824
11825 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11826
11827 if (attr) return DW_SND (attr);
11828
11829 /* GNU F77 is a special case, as at 08/2004 array type info is the
11830 opposite order to the dwarf2 specification, but data is still
11831 laid out as per normal fortran.
11832
11833 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11834 version checking. */
11835
11836 if (cu->language == language_fortran
11837 && cu->producer && strstr (cu->producer, "GNU F77"))
11838 {
11839 return DW_ORD_row_major;
11840 }
11841
11842 switch (cu->language_defn->la_array_ordering)
11843 {
11844 case array_column_major:
11845 return DW_ORD_col_major;
11846 case array_row_major:
11847 default:
11848 return DW_ORD_row_major;
11849 };
11850 }
11851
11852 /* Extract all information from a DW_TAG_set_type DIE and put it in
11853 the DIE's type field. */
11854
11855 static struct type *
11856 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11857 {
11858 struct type *domain_type, *set_type;
11859 struct attribute *attr;
11860
11861 domain_type = die_type (die, cu);
11862
11863 /* The die_type call above may have already set the type for this DIE. */
11864 set_type = get_die_type (die, cu);
11865 if (set_type)
11866 return set_type;
11867
11868 set_type = create_set_type (NULL, domain_type);
11869
11870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11871 if (attr)
11872 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11873
11874 return set_die_type (die, set_type, cu);
11875 }
11876
11877 /* A helper for read_common_block that creates a locexpr baton.
11878 SYM is the symbol which we are marking as computed.
11879 COMMON_DIE is the DIE for the common block.
11880 COMMON_LOC is the location expression attribute for the common
11881 block itself.
11882 MEMBER_LOC is the location expression attribute for the particular
11883 member of the common block that we are processing.
11884 CU is the CU from which the above come. */
11885
11886 static void
11887 mark_common_block_symbol_computed (struct symbol *sym,
11888 struct die_info *common_die,
11889 struct attribute *common_loc,
11890 struct attribute *member_loc,
11891 struct dwarf2_cu *cu)
11892 {
11893 struct objfile *objfile = dwarf2_per_objfile->objfile;
11894 struct dwarf2_locexpr_baton *baton;
11895 gdb_byte *ptr;
11896 unsigned int cu_off;
11897 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11898 LONGEST offset = 0;
11899
11900 gdb_assert (common_loc && member_loc);
11901 gdb_assert (attr_form_is_block (common_loc));
11902 gdb_assert (attr_form_is_block (member_loc)
11903 || attr_form_is_constant (member_loc));
11904
11905 baton = obstack_alloc (&objfile->objfile_obstack,
11906 sizeof (struct dwarf2_locexpr_baton));
11907 baton->per_cu = cu->per_cu;
11908 gdb_assert (baton->per_cu);
11909
11910 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11911
11912 if (attr_form_is_constant (member_loc))
11913 {
11914 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11915 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11916 }
11917 else
11918 baton->size += DW_BLOCK (member_loc)->size;
11919
11920 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11921 baton->data = ptr;
11922
11923 *ptr++ = DW_OP_call4;
11924 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11925 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11926 ptr += 4;
11927
11928 if (attr_form_is_constant (member_loc))
11929 {
11930 *ptr++ = DW_OP_addr;
11931 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11932 ptr += cu->header.addr_size;
11933 }
11934 else
11935 {
11936 /* We have to copy the data here, because DW_OP_call4 will only
11937 use a DW_AT_location attribute. */
11938 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11939 ptr += DW_BLOCK (member_loc)->size;
11940 }
11941
11942 *ptr++ = DW_OP_plus;
11943 gdb_assert (ptr - baton->data == baton->size);
11944
11945 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11946 SYMBOL_LOCATION_BATON (sym) = baton;
11947 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11948 }
11949
11950 /* Create appropriate locally-scoped variables for all the
11951 DW_TAG_common_block entries. Also create a struct common_block
11952 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11953 is used to sepate the common blocks name namespace from regular
11954 variable names. */
11955
11956 static void
11957 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11958 {
11959 struct attribute *attr;
11960
11961 attr = dwarf2_attr (die, DW_AT_location, cu);
11962 if (attr)
11963 {
11964 /* Support the .debug_loc offsets. */
11965 if (attr_form_is_block (attr))
11966 {
11967 /* Ok. */
11968 }
11969 else if (attr_form_is_section_offset (attr))
11970 {
11971 dwarf2_complex_location_expr_complaint ();
11972 attr = NULL;
11973 }
11974 else
11975 {
11976 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11977 "common block member");
11978 attr = NULL;
11979 }
11980 }
11981
11982 if (die->child != NULL)
11983 {
11984 struct objfile *objfile = cu->objfile;
11985 struct die_info *child_die;
11986 size_t n_entries = 0, size;
11987 struct common_block *common_block;
11988 struct symbol *sym;
11989
11990 for (child_die = die->child;
11991 child_die && child_die->tag;
11992 child_die = sibling_die (child_die))
11993 ++n_entries;
11994
11995 size = (sizeof (struct common_block)
11996 + (n_entries - 1) * sizeof (struct symbol *));
11997 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11998 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11999 common_block->n_entries = 0;
12000
12001 for (child_die = die->child;
12002 child_die && child_die->tag;
12003 child_die = sibling_die (child_die))
12004 {
12005 /* Create the symbol in the DW_TAG_common_block block in the current
12006 symbol scope. */
12007 sym = new_symbol (child_die, NULL, cu);
12008 if (sym != NULL)
12009 {
12010 struct attribute *member_loc;
12011
12012 common_block->contents[common_block->n_entries++] = sym;
12013
12014 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12015 cu);
12016 if (member_loc)
12017 {
12018 /* GDB has handled this for a long time, but it is
12019 not specified by DWARF. It seems to have been
12020 emitted by gfortran at least as recently as:
12021 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12022 complaint (&symfile_complaints,
12023 _("Variable in common block has "
12024 "DW_AT_data_member_location "
12025 "- DIE at 0x%x [in module %s]"),
12026 child_die->offset.sect_off, cu->objfile->name);
12027
12028 if (attr_form_is_section_offset (member_loc))
12029 dwarf2_complex_location_expr_complaint ();
12030 else if (attr_form_is_constant (member_loc)
12031 || attr_form_is_block (member_loc))
12032 {
12033 if (attr)
12034 mark_common_block_symbol_computed (sym, die, attr,
12035 member_loc, cu);
12036 }
12037 else
12038 dwarf2_complex_location_expr_complaint ();
12039 }
12040 }
12041 }
12042
12043 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12044 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12045 }
12046 }
12047
12048 /* Create a type for a C++ namespace. */
12049
12050 static struct type *
12051 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12052 {
12053 struct objfile *objfile = cu->objfile;
12054 const char *previous_prefix, *name;
12055 int is_anonymous;
12056 struct type *type;
12057
12058 /* For extensions, reuse the type of the original namespace. */
12059 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12060 {
12061 struct die_info *ext_die;
12062 struct dwarf2_cu *ext_cu = cu;
12063
12064 ext_die = dwarf2_extension (die, &ext_cu);
12065 type = read_type_die (ext_die, ext_cu);
12066
12067 /* EXT_CU may not be the same as CU.
12068 Ensure TYPE is recorded in CU's type_hash table. */
12069 return set_die_type (die, type, cu);
12070 }
12071
12072 name = namespace_name (die, &is_anonymous, cu);
12073
12074 /* Now build the name of the current namespace. */
12075
12076 previous_prefix = determine_prefix (die, cu);
12077 if (previous_prefix[0] != '\0')
12078 name = typename_concat (&objfile->objfile_obstack,
12079 previous_prefix, name, 0, cu);
12080
12081 /* Create the type. */
12082 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12083 objfile);
12084 TYPE_NAME (type) = (char *) name;
12085 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12086
12087 return set_die_type (die, type, cu);
12088 }
12089
12090 /* Read a C++ namespace. */
12091
12092 static void
12093 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12094 {
12095 struct objfile *objfile = cu->objfile;
12096 int is_anonymous;
12097
12098 /* Add a symbol associated to this if we haven't seen the namespace
12099 before. Also, add a using directive if it's an anonymous
12100 namespace. */
12101
12102 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12103 {
12104 struct type *type;
12105
12106 type = read_type_die (die, cu);
12107 new_symbol (die, type, cu);
12108
12109 namespace_name (die, &is_anonymous, cu);
12110 if (is_anonymous)
12111 {
12112 const char *previous_prefix = determine_prefix (die, cu);
12113
12114 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12115 NULL, NULL, &objfile->objfile_obstack);
12116 }
12117 }
12118
12119 if (die->child != NULL)
12120 {
12121 struct die_info *child_die = die->child;
12122
12123 while (child_die && child_die->tag)
12124 {
12125 process_die (child_die, cu);
12126 child_die = sibling_die (child_die);
12127 }
12128 }
12129 }
12130
12131 /* Read a Fortran module as type. This DIE can be only a declaration used for
12132 imported module. Still we need that type as local Fortran "use ... only"
12133 declaration imports depend on the created type in determine_prefix. */
12134
12135 static struct type *
12136 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12137 {
12138 struct objfile *objfile = cu->objfile;
12139 char *module_name;
12140 struct type *type;
12141
12142 module_name = dwarf2_name (die, cu);
12143 if (!module_name)
12144 complaint (&symfile_complaints,
12145 _("DW_TAG_module has no name, offset 0x%x"),
12146 die->offset.sect_off);
12147 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12148
12149 /* determine_prefix uses TYPE_TAG_NAME. */
12150 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12151
12152 return set_die_type (die, type, cu);
12153 }
12154
12155 /* Read a Fortran module. */
12156
12157 static void
12158 read_module (struct die_info *die, struct dwarf2_cu *cu)
12159 {
12160 struct die_info *child_die = die->child;
12161
12162 while (child_die && child_die->tag)
12163 {
12164 process_die (child_die, cu);
12165 child_die = sibling_die (child_die);
12166 }
12167 }
12168
12169 /* Return the name of the namespace represented by DIE. Set
12170 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12171 namespace. */
12172
12173 static const char *
12174 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12175 {
12176 struct die_info *current_die;
12177 const char *name = NULL;
12178
12179 /* Loop through the extensions until we find a name. */
12180
12181 for (current_die = die;
12182 current_die != NULL;
12183 current_die = dwarf2_extension (die, &cu))
12184 {
12185 name = dwarf2_name (current_die, cu);
12186 if (name != NULL)
12187 break;
12188 }
12189
12190 /* Is it an anonymous namespace? */
12191
12192 *is_anonymous = (name == NULL);
12193 if (*is_anonymous)
12194 name = CP_ANONYMOUS_NAMESPACE_STR;
12195
12196 return name;
12197 }
12198
12199 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12200 the user defined type vector. */
12201
12202 static struct type *
12203 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12204 {
12205 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12206 struct comp_unit_head *cu_header = &cu->header;
12207 struct type *type;
12208 struct attribute *attr_byte_size;
12209 struct attribute *attr_address_class;
12210 int byte_size, addr_class;
12211 struct type *target_type;
12212
12213 target_type = die_type (die, cu);
12214
12215 /* The die_type call above may have already set the type for this DIE. */
12216 type = get_die_type (die, cu);
12217 if (type)
12218 return type;
12219
12220 type = lookup_pointer_type (target_type);
12221
12222 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12223 if (attr_byte_size)
12224 byte_size = DW_UNSND (attr_byte_size);
12225 else
12226 byte_size = cu_header->addr_size;
12227
12228 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12229 if (attr_address_class)
12230 addr_class = DW_UNSND (attr_address_class);
12231 else
12232 addr_class = DW_ADDR_none;
12233
12234 /* If the pointer size or address class is different than the
12235 default, create a type variant marked as such and set the
12236 length accordingly. */
12237 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12238 {
12239 if (gdbarch_address_class_type_flags_p (gdbarch))
12240 {
12241 int type_flags;
12242
12243 type_flags = gdbarch_address_class_type_flags
12244 (gdbarch, byte_size, addr_class);
12245 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12246 == 0);
12247 type = make_type_with_address_space (type, type_flags);
12248 }
12249 else if (TYPE_LENGTH (type) != byte_size)
12250 {
12251 complaint (&symfile_complaints,
12252 _("invalid pointer size %d"), byte_size);
12253 }
12254 else
12255 {
12256 /* Should we also complain about unhandled address classes? */
12257 }
12258 }
12259
12260 TYPE_LENGTH (type) = byte_size;
12261 return set_die_type (die, type, cu);
12262 }
12263
12264 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12265 the user defined type vector. */
12266
12267 static struct type *
12268 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12269 {
12270 struct type *type;
12271 struct type *to_type;
12272 struct type *domain;
12273
12274 to_type = die_type (die, cu);
12275 domain = die_containing_type (die, cu);
12276
12277 /* The calls above may have already set the type for this DIE. */
12278 type = get_die_type (die, cu);
12279 if (type)
12280 return type;
12281
12282 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12283 type = lookup_methodptr_type (to_type);
12284 else
12285 type = lookup_memberptr_type (to_type, domain);
12286
12287 return set_die_type (die, type, cu);
12288 }
12289
12290 /* Extract all information from a DW_TAG_reference_type DIE and add to
12291 the user defined type vector. */
12292
12293 static struct type *
12294 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12295 {
12296 struct comp_unit_head *cu_header = &cu->header;
12297 struct type *type, *target_type;
12298 struct attribute *attr;
12299
12300 target_type = die_type (die, cu);
12301
12302 /* The die_type call above may have already set the type for this DIE. */
12303 type = get_die_type (die, cu);
12304 if (type)
12305 return type;
12306
12307 type = lookup_reference_type (target_type);
12308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12309 if (attr)
12310 {
12311 TYPE_LENGTH (type) = DW_UNSND (attr);
12312 }
12313 else
12314 {
12315 TYPE_LENGTH (type) = cu_header->addr_size;
12316 }
12317 return set_die_type (die, type, cu);
12318 }
12319
12320 static struct type *
12321 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12322 {
12323 struct type *base_type, *cv_type;
12324
12325 base_type = die_type (die, cu);
12326
12327 /* The die_type call above may have already set the type for this DIE. */
12328 cv_type = get_die_type (die, cu);
12329 if (cv_type)
12330 return cv_type;
12331
12332 /* In case the const qualifier is applied to an array type, the element type
12333 is so qualified, not the array type (section 6.7.3 of C99). */
12334 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12335 {
12336 struct type *el_type, *inner_array;
12337
12338 base_type = copy_type (base_type);
12339 inner_array = base_type;
12340
12341 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12342 {
12343 TYPE_TARGET_TYPE (inner_array) =
12344 copy_type (TYPE_TARGET_TYPE (inner_array));
12345 inner_array = TYPE_TARGET_TYPE (inner_array);
12346 }
12347
12348 el_type = TYPE_TARGET_TYPE (inner_array);
12349 TYPE_TARGET_TYPE (inner_array) =
12350 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12351
12352 return set_die_type (die, base_type, cu);
12353 }
12354
12355 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12356 return set_die_type (die, cv_type, cu);
12357 }
12358
12359 static struct type *
12360 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12361 {
12362 struct type *base_type, *cv_type;
12363
12364 base_type = die_type (die, cu);
12365
12366 /* The die_type call above may have already set the type for this DIE. */
12367 cv_type = get_die_type (die, cu);
12368 if (cv_type)
12369 return cv_type;
12370
12371 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12372 return set_die_type (die, cv_type, cu);
12373 }
12374
12375 /* Extract all information from a DW_TAG_string_type DIE and add to
12376 the user defined type vector. It isn't really a user defined type,
12377 but it behaves like one, with other DIE's using an AT_user_def_type
12378 attribute to reference it. */
12379
12380 static struct type *
12381 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12382 {
12383 struct objfile *objfile = cu->objfile;
12384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12385 struct type *type, *range_type, *index_type, *char_type;
12386 struct attribute *attr;
12387 unsigned int length;
12388
12389 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12390 if (attr)
12391 {
12392 length = DW_UNSND (attr);
12393 }
12394 else
12395 {
12396 /* Check for the DW_AT_byte_size attribute. */
12397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12398 if (attr)
12399 {
12400 length = DW_UNSND (attr);
12401 }
12402 else
12403 {
12404 length = 1;
12405 }
12406 }
12407
12408 index_type = objfile_type (objfile)->builtin_int;
12409 range_type = create_range_type (NULL, index_type, 1, length);
12410 char_type = language_string_char_type (cu->language_defn, gdbarch);
12411 type = create_string_type (NULL, char_type, range_type);
12412
12413 return set_die_type (die, type, cu);
12414 }
12415
12416 /* Handle DIES due to C code like:
12417
12418 struct foo
12419 {
12420 int (*funcp)(int a, long l);
12421 int b;
12422 };
12423
12424 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12425
12426 static struct type *
12427 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12428 {
12429 struct objfile *objfile = cu->objfile;
12430 struct type *type; /* Type that this function returns. */
12431 struct type *ftype; /* Function that returns above type. */
12432 struct attribute *attr;
12433
12434 type = die_type (die, cu);
12435
12436 /* The die_type call above may have already set the type for this DIE. */
12437 ftype = get_die_type (die, cu);
12438 if (ftype)
12439 return ftype;
12440
12441 ftype = lookup_function_type (type);
12442
12443 /* All functions in C++, Pascal and Java have prototypes. */
12444 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12445 if ((attr && (DW_UNSND (attr) != 0))
12446 || cu->language == language_cplus
12447 || cu->language == language_java
12448 || cu->language == language_pascal)
12449 TYPE_PROTOTYPED (ftype) = 1;
12450 else if (producer_is_realview (cu->producer))
12451 /* RealView does not emit DW_AT_prototyped. We can not
12452 distinguish prototyped and unprototyped functions; default to
12453 prototyped, since that is more common in modern code (and
12454 RealView warns about unprototyped functions). */
12455 TYPE_PROTOTYPED (ftype) = 1;
12456
12457 /* Store the calling convention in the type if it's available in
12458 the subroutine die. Otherwise set the calling convention to
12459 the default value DW_CC_normal. */
12460 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12461 if (attr)
12462 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12463 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12464 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12465 else
12466 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12467
12468 /* We need to add the subroutine type to the die immediately so
12469 we don't infinitely recurse when dealing with parameters
12470 declared as the same subroutine type. */
12471 set_die_type (die, ftype, cu);
12472
12473 if (die->child != NULL)
12474 {
12475 struct type *void_type = objfile_type (objfile)->builtin_void;
12476 struct die_info *child_die;
12477 int nparams, iparams;
12478
12479 /* Count the number of parameters.
12480 FIXME: GDB currently ignores vararg functions, but knows about
12481 vararg member functions. */
12482 nparams = 0;
12483 child_die = die->child;
12484 while (child_die && child_die->tag)
12485 {
12486 if (child_die->tag == DW_TAG_formal_parameter)
12487 nparams++;
12488 else if (child_die->tag == DW_TAG_unspecified_parameters)
12489 TYPE_VARARGS (ftype) = 1;
12490 child_die = sibling_die (child_die);
12491 }
12492
12493 /* Allocate storage for parameters and fill them in. */
12494 TYPE_NFIELDS (ftype) = nparams;
12495 TYPE_FIELDS (ftype) = (struct field *)
12496 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12497
12498 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12499 even if we error out during the parameters reading below. */
12500 for (iparams = 0; iparams < nparams; iparams++)
12501 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12502
12503 iparams = 0;
12504 child_die = die->child;
12505 while (child_die && child_die->tag)
12506 {
12507 if (child_die->tag == DW_TAG_formal_parameter)
12508 {
12509 struct type *arg_type;
12510
12511 /* DWARF version 2 has no clean way to discern C++
12512 static and non-static member functions. G++ helps
12513 GDB by marking the first parameter for non-static
12514 member functions (which is the this pointer) as
12515 artificial. We pass this information to
12516 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12517
12518 DWARF version 3 added DW_AT_object_pointer, which GCC
12519 4.5 does not yet generate. */
12520 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12521 if (attr)
12522 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12523 else
12524 {
12525 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12526
12527 /* GCC/43521: In java, the formal parameter
12528 "this" is sometimes not marked with DW_AT_artificial. */
12529 if (cu->language == language_java)
12530 {
12531 const char *name = dwarf2_name (child_die, cu);
12532
12533 if (name && !strcmp (name, "this"))
12534 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12535 }
12536 }
12537 arg_type = die_type (child_die, cu);
12538
12539 /* RealView does not mark THIS as const, which the testsuite
12540 expects. GCC marks THIS as const in method definitions,
12541 but not in the class specifications (GCC PR 43053). */
12542 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12543 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12544 {
12545 int is_this = 0;
12546 struct dwarf2_cu *arg_cu = cu;
12547 const char *name = dwarf2_name (child_die, cu);
12548
12549 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12550 if (attr)
12551 {
12552 /* If the compiler emits this, use it. */
12553 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12554 is_this = 1;
12555 }
12556 else if (name && strcmp (name, "this") == 0)
12557 /* Function definitions will have the argument names. */
12558 is_this = 1;
12559 else if (name == NULL && iparams == 0)
12560 /* Declarations may not have the names, so like
12561 elsewhere in GDB, assume an artificial first
12562 argument is "this". */
12563 is_this = 1;
12564
12565 if (is_this)
12566 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12567 arg_type, 0);
12568 }
12569
12570 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12571 iparams++;
12572 }
12573 child_die = sibling_die (child_die);
12574 }
12575 }
12576
12577 return ftype;
12578 }
12579
12580 static struct type *
12581 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12582 {
12583 struct objfile *objfile = cu->objfile;
12584 const char *name = NULL;
12585 struct type *this_type, *target_type;
12586
12587 name = dwarf2_full_name (NULL, die, cu);
12588 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12589 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12590 TYPE_NAME (this_type) = (char *) name;
12591 set_die_type (die, this_type, cu);
12592 target_type = die_type (die, cu);
12593 if (target_type != this_type)
12594 TYPE_TARGET_TYPE (this_type) = target_type;
12595 else
12596 {
12597 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12598 spec and cause infinite loops in GDB. */
12599 complaint (&symfile_complaints,
12600 _("Self-referential DW_TAG_typedef "
12601 "- DIE at 0x%x [in module %s]"),
12602 die->offset.sect_off, objfile->name);
12603 TYPE_TARGET_TYPE (this_type) = NULL;
12604 }
12605 return this_type;
12606 }
12607
12608 /* Find a representation of a given base type and install
12609 it in the TYPE field of the die. */
12610
12611 static struct type *
12612 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12613 {
12614 struct objfile *objfile = cu->objfile;
12615 struct type *type;
12616 struct attribute *attr;
12617 int encoding = 0, size = 0;
12618 char *name;
12619 enum type_code code = TYPE_CODE_INT;
12620 int type_flags = 0;
12621 struct type *target_type = NULL;
12622
12623 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12624 if (attr)
12625 {
12626 encoding = DW_UNSND (attr);
12627 }
12628 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12629 if (attr)
12630 {
12631 size = DW_UNSND (attr);
12632 }
12633 name = dwarf2_name (die, cu);
12634 if (!name)
12635 {
12636 complaint (&symfile_complaints,
12637 _("DW_AT_name missing from DW_TAG_base_type"));
12638 }
12639
12640 switch (encoding)
12641 {
12642 case DW_ATE_address:
12643 /* Turn DW_ATE_address into a void * pointer. */
12644 code = TYPE_CODE_PTR;
12645 type_flags |= TYPE_FLAG_UNSIGNED;
12646 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12647 break;
12648 case DW_ATE_boolean:
12649 code = TYPE_CODE_BOOL;
12650 type_flags |= TYPE_FLAG_UNSIGNED;
12651 break;
12652 case DW_ATE_complex_float:
12653 code = TYPE_CODE_COMPLEX;
12654 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12655 break;
12656 case DW_ATE_decimal_float:
12657 code = TYPE_CODE_DECFLOAT;
12658 break;
12659 case DW_ATE_float:
12660 code = TYPE_CODE_FLT;
12661 break;
12662 case DW_ATE_signed:
12663 break;
12664 case DW_ATE_unsigned:
12665 type_flags |= TYPE_FLAG_UNSIGNED;
12666 if (cu->language == language_fortran
12667 && name
12668 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12669 code = TYPE_CODE_CHAR;
12670 break;
12671 case DW_ATE_signed_char:
12672 if (cu->language == language_ada || cu->language == language_m2
12673 || cu->language == language_pascal
12674 || cu->language == language_fortran)
12675 code = TYPE_CODE_CHAR;
12676 break;
12677 case DW_ATE_unsigned_char:
12678 if (cu->language == language_ada || cu->language == language_m2
12679 || cu->language == language_pascal
12680 || cu->language == language_fortran)
12681 code = TYPE_CODE_CHAR;
12682 type_flags |= TYPE_FLAG_UNSIGNED;
12683 break;
12684 case DW_ATE_UTF:
12685 /* We just treat this as an integer and then recognize the
12686 type by name elsewhere. */
12687 break;
12688
12689 default:
12690 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12691 dwarf_type_encoding_name (encoding));
12692 break;
12693 }
12694
12695 type = init_type (code, size, type_flags, NULL, objfile);
12696 TYPE_NAME (type) = name;
12697 TYPE_TARGET_TYPE (type) = target_type;
12698
12699 if (name && strcmp (name, "char") == 0)
12700 TYPE_NOSIGN (type) = 1;
12701
12702 return set_die_type (die, type, cu);
12703 }
12704
12705 /* Read the given DW_AT_subrange DIE. */
12706
12707 static struct type *
12708 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12709 {
12710 struct type *base_type;
12711 struct type *range_type;
12712 struct attribute *attr;
12713 LONGEST low, high;
12714 int low_default_is_valid;
12715 char *name;
12716 LONGEST negative_mask;
12717
12718 base_type = die_type (die, cu);
12719 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12720 check_typedef (base_type);
12721
12722 /* The die_type call above may have already set the type for this DIE. */
12723 range_type = get_die_type (die, cu);
12724 if (range_type)
12725 return range_type;
12726
12727 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12728 omitting DW_AT_lower_bound. */
12729 switch (cu->language)
12730 {
12731 case language_c:
12732 case language_cplus:
12733 low = 0;
12734 low_default_is_valid = 1;
12735 break;
12736 case language_fortran:
12737 low = 1;
12738 low_default_is_valid = 1;
12739 break;
12740 case language_d:
12741 case language_java:
12742 case language_objc:
12743 low = 0;
12744 low_default_is_valid = (cu->header.version >= 4);
12745 break;
12746 case language_ada:
12747 case language_m2:
12748 case language_pascal:
12749 low = 1;
12750 low_default_is_valid = (cu->header.version >= 4);
12751 break;
12752 default:
12753 low = 0;
12754 low_default_is_valid = 0;
12755 break;
12756 }
12757
12758 /* FIXME: For variable sized arrays either of these could be
12759 a variable rather than a constant value. We'll allow it,
12760 but we don't know how to handle it. */
12761 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12762 if (attr)
12763 low = dwarf2_get_attr_constant_value (attr, low);
12764 else if (!low_default_is_valid)
12765 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12766 "- DIE at 0x%x [in module %s]"),
12767 die->offset.sect_off, cu->objfile->name);
12768
12769 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12770 if (attr)
12771 {
12772 if (attr_form_is_block (attr) || is_ref_attr (attr))
12773 {
12774 /* GCC encodes arrays with unspecified or dynamic length
12775 with a DW_FORM_block1 attribute or a reference attribute.
12776 FIXME: GDB does not yet know how to handle dynamic
12777 arrays properly, treat them as arrays with unspecified
12778 length for now.
12779
12780 FIXME: jimb/2003-09-22: GDB does not really know
12781 how to handle arrays of unspecified length
12782 either; we just represent them as zero-length
12783 arrays. Choose an appropriate upper bound given
12784 the lower bound we've computed above. */
12785 high = low - 1;
12786 }
12787 else
12788 high = dwarf2_get_attr_constant_value (attr, 1);
12789 }
12790 else
12791 {
12792 attr = dwarf2_attr (die, DW_AT_count, cu);
12793 if (attr)
12794 {
12795 int count = dwarf2_get_attr_constant_value (attr, 1);
12796 high = low + count - 1;
12797 }
12798 else
12799 {
12800 /* Unspecified array length. */
12801 high = low - 1;
12802 }
12803 }
12804
12805 /* Dwarf-2 specifications explicitly allows to create subrange types
12806 without specifying a base type.
12807 In that case, the base type must be set to the type of
12808 the lower bound, upper bound or count, in that order, if any of these
12809 three attributes references an object that has a type.
12810 If no base type is found, the Dwarf-2 specifications say that
12811 a signed integer type of size equal to the size of an address should
12812 be used.
12813 For the following C code: `extern char gdb_int [];'
12814 GCC produces an empty range DIE.
12815 FIXME: muller/2010-05-28: Possible references to object for low bound,
12816 high bound or count are not yet handled by this code. */
12817 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12818 {
12819 struct objfile *objfile = cu->objfile;
12820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12821 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12822 struct type *int_type = objfile_type (objfile)->builtin_int;
12823
12824 /* Test "int", "long int", and "long long int" objfile types,
12825 and select the first one having a size above or equal to the
12826 architecture address size. */
12827 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12828 base_type = int_type;
12829 else
12830 {
12831 int_type = objfile_type (objfile)->builtin_long;
12832 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12833 base_type = int_type;
12834 else
12835 {
12836 int_type = objfile_type (objfile)->builtin_long_long;
12837 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12838 base_type = int_type;
12839 }
12840 }
12841 }
12842
12843 negative_mask =
12844 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12845 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12846 low |= negative_mask;
12847 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12848 high |= negative_mask;
12849
12850 range_type = create_range_type (NULL, base_type, low, high);
12851
12852 /* Mark arrays with dynamic length at least as an array of unspecified
12853 length. GDB could check the boundary but before it gets implemented at
12854 least allow accessing the array elements. */
12855 if (attr && attr_form_is_block (attr))
12856 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12857
12858 /* Ada expects an empty array on no boundary attributes. */
12859 if (attr == NULL && cu->language != language_ada)
12860 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12861
12862 name = dwarf2_name (die, cu);
12863 if (name)
12864 TYPE_NAME (range_type) = name;
12865
12866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12867 if (attr)
12868 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12869
12870 set_die_type (die, range_type, cu);
12871
12872 /* set_die_type should be already done. */
12873 set_descriptive_type (range_type, die, cu);
12874
12875 return range_type;
12876 }
12877
12878 static struct type *
12879 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12880 {
12881 struct type *type;
12882
12883 /* For now, we only support the C meaning of an unspecified type: void. */
12884
12885 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12886 TYPE_NAME (type) = dwarf2_name (die, cu);
12887
12888 return set_die_type (die, type, cu);
12889 }
12890
12891 /* Read a single die and all its descendents. Set the die's sibling
12892 field to NULL; set other fields in the die correctly, and set all
12893 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12894 location of the info_ptr after reading all of those dies. PARENT
12895 is the parent of the die in question. */
12896
12897 static struct die_info *
12898 read_die_and_children (const struct die_reader_specs *reader,
12899 gdb_byte *info_ptr,
12900 gdb_byte **new_info_ptr,
12901 struct die_info *parent)
12902 {
12903 struct die_info *die;
12904 gdb_byte *cur_ptr;
12905 int has_children;
12906
12907 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12908 if (die == NULL)
12909 {
12910 *new_info_ptr = cur_ptr;
12911 return NULL;
12912 }
12913 store_in_ref_table (die, reader->cu);
12914
12915 if (has_children)
12916 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12917 else
12918 {
12919 die->child = NULL;
12920 *new_info_ptr = cur_ptr;
12921 }
12922
12923 die->sibling = NULL;
12924 die->parent = parent;
12925 return die;
12926 }
12927
12928 /* Read a die, all of its descendents, and all of its siblings; set
12929 all of the fields of all of the dies correctly. Arguments are as
12930 in read_die_and_children. */
12931
12932 static struct die_info *
12933 read_die_and_siblings (const struct die_reader_specs *reader,
12934 gdb_byte *info_ptr,
12935 gdb_byte **new_info_ptr,
12936 struct die_info *parent)
12937 {
12938 struct die_info *first_die, *last_sibling;
12939 gdb_byte *cur_ptr;
12940
12941 cur_ptr = info_ptr;
12942 first_die = last_sibling = NULL;
12943
12944 while (1)
12945 {
12946 struct die_info *die
12947 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12948
12949 if (die == NULL)
12950 {
12951 *new_info_ptr = cur_ptr;
12952 return first_die;
12953 }
12954
12955 if (!first_die)
12956 first_die = die;
12957 else
12958 last_sibling->sibling = die;
12959
12960 last_sibling = die;
12961 }
12962 }
12963
12964 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12965 attributes.
12966 The caller is responsible for filling in the extra attributes
12967 and updating (*DIEP)->num_attrs.
12968 Set DIEP to point to a newly allocated die with its information,
12969 except for its child, sibling, and parent fields.
12970 Set HAS_CHILDREN to tell whether the die has children or not. */
12971
12972 static gdb_byte *
12973 read_full_die_1 (const struct die_reader_specs *reader,
12974 struct die_info **diep, gdb_byte *info_ptr,
12975 int *has_children, int num_extra_attrs)
12976 {
12977 unsigned int abbrev_number, bytes_read, i;
12978 sect_offset offset;
12979 struct abbrev_info *abbrev;
12980 struct die_info *die;
12981 struct dwarf2_cu *cu = reader->cu;
12982 bfd *abfd = reader->abfd;
12983
12984 offset.sect_off = info_ptr - reader->buffer;
12985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12986 info_ptr += bytes_read;
12987 if (!abbrev_number)
12988 {
12989 *diep = NULL;
12990 *has_children = 0;
12991 return info_ptr;
12992 }
12993
12994 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12995 if (!abbrev)
12996 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12997 abbrev_number,
12998 bfd_get_filename (abfd));
12999
13000 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13001 die->offset = offset;
13002 die->tag = abbrev->tag;
13003 die->abbrev = abbrev_number;
13004
13005 /* Make the result usable.
13006 The caller needs to update num_attrs after adding the extra
13007 attributes. */
13008 die->num_attrs = abbrev->num_attrs;
13009
13010 for (i = 0; i < abbrev->num_attrs; ++i)
13011 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13012 info_ptr);
13013
13014 *diep = die;
13015 *has_children = abbrev->has_children;
13016 return info_ptr;
13017 }
13018
13019 /* Read a die and all its attributes.
13020 Set DIEP to point to a newly allocated die with its information,
13021 except for its child, sibling, and parent fields.
13022 Set HAS_CHILDREN to tell whether the die has children or not. */
13023
13024 static gdb_byte *
13025 read_full_die (const struct die_reader_specs *reader,
13026 struct die_info **diep, gdb_byte *info_ptr,
13027 int *has_children)
13028 {
13029 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13030 }
13031 \f
13032 /* Abbreviation tables.
13033
13034 In DWARF version 2, the description of the debugging information is
13035 stored in a separate .debug_abbrev section. Before we read any
13036 dies from a section we read in all abbreviations and install them
13037 in a hash table. */
13038
13039 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13040
13041 static struct abbrev_info *
13042 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13043 {
13044 struct abbrev_info *abbrev;
13045
13046 abbrev = (struct abbrev_info *)
13047 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13048 memset (abbrev, 0, sizeof (struct abbrev_info));
13049 return abbrev;
13050 }
13051
13052 /* Add an abbreviation to the table. */
13053
13054 static void
13055 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13056 unsigned int abbrev_number,
13057 struct abbrev_info *abbrev)
13058 {
13059 unsigned int hash_number;
13060
13061 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13062 abbrev->next = abbrev_table->abbrevs[hash_number];
13063 abbrev_table->abbrevs[hash_number] = abbrev;
13064 }
13065
13066 /* Look up an abbrev in the table.
13067 Returns NULL if the abbrev is not found. */
13068
13069 static struct abbrev_info *
13070 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13071 unsigned int abbrev_number)
13072 {
13073 unsigned int hash_number;
13074 struct abbrev_info *abbrev;
13075
13076 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13077 abbrev = abbrev_table->abbrevs[hash_number];
13078
13079 while (abbrev)
13080 {
13081 if (abbrev->number == abbrev_number)
13082 return abbrev;
13083 abbrev = abbrev->next;
13084 }
13085 return NULL;
13086 }
13087
13088 /* Read in an abbrev table. */
13089
13090 static struct abbrev_table *
13091 abbrev_table_read_table (struct dwarf2_section_info *section,
13092 sect_offset offset)
13093 {
13094 struct objfile *objfile = dwarf2_per_objfile->objfile;
13095 bfd *abfd = section->asection->owner;
13096 struct abbrev_table *abbrev_table;
13097 gdb_byte *abbrev_ptr;
13098 struct abbrev_info *cur_abbrev;
13099 unsigned int abbrev_number, bytes_read, abbrev_name;
13100 unsigned int abbrev_form;
13101 struct attr_abbrev *cur_attrs;
13102 unsigned int allocated_attrs;
13103
13104 abbrev_table = XMALLOC (struct abbrev_table);
13105 abbrev_table->offset = offset;
13106 obstack_init (&abbrev_table->abbrev_obstack);
13107 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13108 (ABBREV_HASH_SIZE
13109 * sizeof (struct abbrev_info *)));
13110 memset (abbrev_table->abbrevs, 0,
13111 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13112
13113 dwarf2_read_section (objfile, section);
13114 abbrev_ptr = section->buffer + offset.sect_off;
13115 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13116 abbrev_ptr += bytes_read;
13117
13118 allocated_attrs = ATTR_ALLOC_CHUNK;
13119 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13120
13121 /* Loop until we reach an abbrev number of 0. */
13122 while (abbrev_number)
13123 {
13124 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13125
13126 /* read in abbrev header */
13127 cur_abbrev->number = abbrev_number;
13128 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13129 abbrev_ptr += bytes_read;
13130 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13131 abbrev_ptr += 1;
13132
13133 /* now read in declarations */
13134 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13135 abbrev_ptr += bytes_read;
13136 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13137 abbrev_ptr += bytes_read;
13138 while (abbrev_name)
13139 {
13140 if (cur_abbrev->num_attrs == allocated_attrs)
13141 {
13142 allocated_attrs += ATTR_ALLOC_CHUNK;
13143 cur_attrs
13144 = xrealloc (cur_attrs, (allocated_attrs
13145 * sizeof (struct attr_abbrev)));
13146 }
13147
13148 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13149 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13150 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13151 abbrev_ptr += bytes_read;
13152 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13153 abbrev_ptr += bytes_read;
13154 }
13155
13156 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13157 (cur_abbrev->num_attrs
13158 * sizeof (struct attr_abbrev)));
13159 memcpy (cur_abbrev->attrs, cur_attrs,
13160 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13161
13162 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13163
13164 /* Get next abbreviation.
13165 Under Irix6 the abbreviations for a compilation unit are not
13166 always properly terminated with an abbrev number of 0.
13167 Exit loop if we encounter an abbreviation which we have
13168 already read (which means we are about to read the abbreviations
13169 for the next compile unit) or if the end of the abbreviation
13170 table is reached. */
13171 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13172 break;
13173 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13174 abbrev_ptr += bytes_read;
13175 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13176 break;
13177 }
13178
13179 xfree (cur_attrs);
13180 return abbrev_table;
13181 }
13182
13183 /* Free the resources held by ABBREV_TABLE. */
13184
13185 static void
13186 abbrev_table_free (struct abbrev_table *abbrev_table)
13187 {
13188 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13189 xfree (abbrev_table);
13190 }
13191
13192 /* Same as abbrev_table_free but as a cleanup.
13193 We pass in a pointer to the pointer to the table so that we can
13194 set the pointer to NULL when we're done. It also simplifies
13195 build_type_unit_groups. */
13196
13197 static void
13198 abbrev_table_free_cleanup (void *table_ptr)
13199 {
13200 struct abbrev_table **abbrev_table_ptr = table_ptr;
13201
13202 if (*abbrev_table_ptr != NULL)
13203 abbrev_table_free (*abbrev_table_ptr);
13204 *abbrev_table_ptr = NULL;
13205 }
13206
13207 /* Read the abbrev table for CU from ABBREV_SECTION. */
13208
13209 static void
13210 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13211 struct dwarf2_section_info *abbrev_section)
13212 {
13213 cu->abbrev_table =
13214 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13215 }
13216
13217 /* Release the memory used by the abbrev table for a compilation unit. */
13218
13219 static void
13220 dwarf2_free_abbrev_table (void *ptr_to_cu)
13221 {
13222 struct dwarf2_cu *cu = ptr_to_cu;
13223
13224 abbrev_table_free (cu->abbrev_table);
13225 /* Set this to NULL so that we SEGV if we try to read it later,
13226 and also because free_comp_unit verifies this is NULL. */
13227 cu->abbrev_table = NULL;
13228 }
13229 \f
13230 /* Returns nonzero if TAG represents a type that we might generate a partial
13231 symbol for. */
13232
13233 static int
13234 is_type_tag_for_partial (int tag)
13235 {
13236 switch (tag)
13237 {
13238 #if 0
13239 /* Some types that would be reasonable to generate partial symbols for,
13240 that we don't at present. */
13241 case DW_TAG_array_type:
13242 case DW_TAG_file_type:
13243 case DW_TAG_ptr_to_member_type:
13244 case DW_TAG_set_type:
13245 case DW_TAG_string_type:
13246 case DW_TAG_subroutine_type:
13247 #endif
13248 case DW_TAG_base_type:
13249 case DW_TAG_class_type:
13250 case DW_TAG_interface_type:
13251 case DW_TAG_enumeration_type:
13252 case DW_TAG_structure_type:
13253 case DW_TAG_subrange_type:
13254 case DW_TAG_typedef:
13255 case DW_TAG_union_type:
13256 return 1;
13257 default:
13258 return 0;
13259 }
13260 }
13261
13262 /* Load all DIEs that are interesting for partial symbols into memory. */
13263
13264 static struct partial_die_info *
13265 load_partial_dies (const struct die_reader_specs *reader,
13266 gdb_byte *info_ptr, int building_psymtab)
13267 {
13268 struct dwarf2_cu *cu = reader->cu;
13269 struct objfile *objfile = cu->objfile;
13270 struct partial_die_info *part_die;
13271 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13272 struct abbrev_info *abbrev;
13273 unsigned int bytes_read;
13274 unsigned int load_all = 0;
13275 int nesting_level = 1;
13276
13277 parent_die = NULL;
13278 last_die = NULL;
13279
13280 gdb_assert (cu->per_cu != NULL);
13281 if (cu->per_cu->load_all_dies)
13282 load_all = 1;
13283
13284 cu->partial_dies
13285 = htab_create_alloc_ex (cu->header.length / 12,
13286 partial_die_hash,
13287 partial_die_eq,
13288 NULL,
13289 &cu->comp_unit_obstack,
13290 hashtab_obstack_allocate,
13291 dummy_obstack_deallocate);
13292
13293 part_die = obstack_alloc (&cu->comp_unit_obstack,
13294 sizeof (struct partial_die_info));
13295
13296 while (1)
13297 {
13298 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13299
13300 /* A NULL abbrev means the end of a series of children. */
13301 if (abbrev == NULL)
13302 {
13303 if (--nesting_level == 0)
13304 {
13305 /* PART_DIE was probably the last thing allocated on the
13306 comp_unit_obstack, so we could call obstack_free
13307 here. We don't do that because the waste is small,
13308 and will be cleaned up when we're done with this
13309 compilation unit. This way, we're also more robust
13310 against other users of the comp_unit_obstack. */
13311 return first_die;
13312 }
13313 info_ptr += bytes_read;
13314 last_die = parent_die;
13315 parent_die = parent_die->die_parent;
13316 continue;
13317 }
13318
13319 /* Check for template arguments. We never save these; if
13320 they're seen, we just mark the parent, and go on our way. */
13321 if (parent_die != NULL
13322 && cu->language == language_cplus
13323 && (abbrev->tag == DW_TAG_template_type_param
13324 || abbrev->tag == DW_TAG_template_value_param))
13325 {
13326 parent_die->has_template_arguments = 1;
13327
13328 if (!load_all)
13329 {
13330 /* We don't need a partial DIE for the template argument. */
13331 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13332 continue;
13333 }
13334 }
13335
13336 /* We only recurse into c++ subprograms looking for template arguments.
13337 Skip their other children. */
13338 if (!load_all
13339 && cu->language == language_cplus
13340 && parent_die != NULL
13341 && parent_die->tag == DW_TAG_subprogram)
13342 {
13343 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13344 continue;
13345 }
13346
13347 /* Check whether this DIE is interesting enough to save. Normally
13348 we would not be interested in members here, but there may be
13349 later variables referencing them via DW_AT_specification (for
13350 static members). */
13351 if (!load_all
13352 && !is_type_tag_for_partial (abbrev->tag)
13353 && abbrev->tag != DW_TAG_constant
13354 && abbrev->tag != DW_TAG_enumerator
13355 && abbrev->tag != DW_TAG_subprogram
13356 && abbrev->tag != DW_TAG_lexical_block
13357 && abbrev->tag != DW_TAG_variable
13358 && abbrev->tag != DW_TAG_namespace
13359 && abbrev->tag != DW_TAG_module
13360 && abbrev->tag != DW_TAG_member
13361 && abbrev->tag != DW_TAG_imported_unit)
13362 {
13363 /* Otherwise we skip to the next sibling, if any. */
13364 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13365 continue;
13366 }
13367
13368 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13369 info_ptr);
13370
13371 /* This two-pass algorithm for processing partial symbols has a
13372 high cost in cache pressure. Thus, handle some simple cases
13373 here which cover the majority of C partial symbols. DIEs
13374 which neither have specification tags in them, nor could have
13375 specification tags elsewhere pointing at them, can simply be
13376 processed and discarded.
13377
13378 This segment is also optional; scan_partial_symbols and
13379 add_partial_symbol will handle these DIEs if we chain
13380 them in normally. When compilers which do not emit large
13381 quantities of duplicate debug information are more common,
13382 this code can probably be removed. */
13383
13384 /* Any complete simple types at the top level (pretty much all
13385 of them, for a language without namespaces), can be processed
13386 directly. */
13387 if (parent_die == NULL
13388 && part_die->has_specification == 0
13389 && part_die->is_declaration == 0
13390 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13391 || part_die->tag == DW_TAG_base_type
13392 || part_die->tag == DW_TAG_subrange_type))
13393 {
13394 if (building_psymtab && part_die->name != NULL)
13395 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13396 VAR_DOMAIN, LOC_TYPEDEF,
13397 &objfile->static_psymbols,
13398 0, (CORE_ADDR) 0, cu->language, objfile);
13399 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13400 continue;
13401 }
13402
13403 /* The exception for DW_TAG_typedef with has_children above is
13404 a workaround of GCC PR debug/47510. In the case of this complaint
13405 type_name_no_tag_or_error will error on such types later.
13406
13407 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13408 it could not find the child DIEs referenced later, this is checked
13409 above. In correct DWARF DW_TAG_typedef should have no children. */
13410
13411 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13412 complaint (&symfile_complaints,
13413 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13414 "- DIE at 0x%x [in module %s]"),
13415 part_die->offset.sect_off, objfile->name);
13416
13417 /* If we're at the second level, and we're an enumerator, and
13418 our parent has no specification (meaning possibly lives in a
13419 namespace elsewhere), then we can add the partial symbol now
13420 instead of queueing it. */
13421 if (part_die->tag == DW_TAG_enumerator
13422 && parent_die != NULL
13423 && parent_die->die_parent == NULL
13424 && parent_die->tag == DW_TAG_enumeration_type
13425 && parent_die->has_specification == 0)
13426 {
13427 if (part_die->name == NULL)
13428 complaint (&symfile_complaints,
13429 _("malformed enumerator DIE ignored"));
13430 else if (building_psymtab)
13431 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13432 VAR_DOMAIN, LOC_CONST,
13433 (cu->language == language_cplus
13434 || cu->language == language_java)
13435 ? &objfile->global_psymbols
13436 : &objfile->static_psymbols,
13437 0, (CORE_ADDR) 0, cu->language, objfile);
13438
13439 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13440 continue;
13441 }
13442
13443 /* We'll save this DIE so link it in. */
13444 part_die->die_parent = parent_die;
13445 part_die->die_sibling = NULL;
13446 part_die->die_child = NULL;
13447
13448 if (last_die && last_die == parent_die)
13449 last_die->die_child = part_die;
13450 else if (last_die)
13451 last_die->die_sibling = part_die;
13452
13453 last_die = part_die;
13454
13455 if (first_die == NULL)
13456 first_die = part_die;
13457
13458 /* Maybe add the DIE to the hash table. Not all DIEs that we
13459 find interesting need to be in the hash table, because we
13460 also have the parent/sibling/child chains; only those that we
13461 might refer to by offset later during partial symbol reading.
13462
13463 For now this means things that might have be the target of a
13464 DW_AT_specification, DW_AT_abstract_origin, or
13465 DW_AT_extension. DW_AT_extension will refer only to
13466 namespaces; DW_AT_abstract_origin refers to functions (and
13467 many things under the function DIE, but we do not recurse
13468 into function DIEs during partial symbol reading) and
13469 possibly variables as well; DW_AT_specification refers to
13470 declarations. Declarations ought to have the DW_AT_declaration
13471 flag. It happens that GCC forgets to put it in sometimes, but
13472 only for functions, not for types.
13473
13474 Adding more things than necessary to the hash table is harmless
13475 except for the performance cost. Adding too few will result in
13476 wasted time in find_partial_die, when we reread the compilation
13477 unit with load_all_dies set. */
13478
13479 if (load_all
13480 || abbrev->tag == DW_TAG_constant
13481 || abbrev->tag == DW_TAG_subprogram
13482 || abbrev->tag == DW_TAG_variable
13483 || abbrev->tag == DW_TAG_namespace
13484 || part_die->is_declaration)
13485 {
13486 void **slot;
13487
13488 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13489 part_die->offset.sect_off, INSERT);
13490 *slot = part_die;
13491 }
13492
13493 part_die = obstack_alloc (&cu->comp_unit_obstack,
13494 sizeof (struct partial_die_info));
13495
13496 /* For some DIEs we want to follow their children (if any). For C
13497 we have no reason to follow the children of structures; for other
13498 languages we have to, so that we can get at method physnames
13499 to infer fully qualified class names, for DW_AT_specification,
13500 and for C++ template arguments. For C++, we also look one level
13501 inside functions to find template arguments (if the name of the
13502 function does not already contain the template arguments).
13503
13504 For Ada, we need to scan the children of subprograms and lexical
13505 blocks as well because Ada allows the definition of nested
13506 entities that could be interesting for the debugger, such as
13507 nested subprograms for instance. */
13508 if (last_die->has_children
13509 && (load_all
13510 || last_die->tag == DW_TAG_namespace
13511 || last_die->tag == DW_TAG_module
13512 || last_die->tag == DW_TAG_enumeration_type
13513 || (cu->language == language_cplus
13514 && last_die->tag == DW_TAG_subprogram
13515 && (last_die->name == NULL
13516 || strchr (last_die->name, '<') == NULL))
13517 || (cu->language != language_c
13518 && (last_die->tag == DW_TAG_class_type
13519 || last_die->tag == DW_TAG_interface_type
13520 || last_die->tag == DW_TAG_structure_type
13521 || last_die->tag == DW_TAG_union_type))
13522 || (cu->language == language_ada
13523 && (last_die->tag == DW_TAG_subprogram
13524 || last_die->tag == DW_TAG_lexical_block))))
13525 {
13526 nesting_level++;
13527 parent_die = last_die;
13528 continue;
13529 }
13530
13531 /* Otherwise we skip to the next sibling, if any. */
13532 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13533
13534 /* Back to the top, do it again. */
13535 }
13536 }
13537
13538 /* Read a minimal amount of information into the minimal die structure. */
13539
13540 static gdb_byte *
13541 read_partial_die (const struct die_reader_specs *reader,
13542 struct partial_die_info *part_die,
13543 struct abbrev_info *abbrev, unsigned int abbrev_len,
13544 gdb_byte *info_ptr)
13545 {
13546 struct dwarf2_cu *cu = reader->cu;
13547 struct objfile *objfile = cu->objfile;
13548 gdb_byte *buffer = reader->buffer;
13549 unsigned int i;
13550 struct attribute attr;
13551 int has_low_pc_attr = 0;
13552 int has_high_pc_attr = 0;
13553 int high_pc_relative = 0;
13554
13555 memset (part_die, 0, sizeof (struct partial_die_info));
13556
13557 part_die->offset.sect_off = info_ptr - buffer;
13558
13559 info_ptr += abbrev_len;
13560
13561 if (abbrev == NULL)
13562 return info_ptr;
13563
13564 part_die->tag = abbrev->tag;
13565 part_die->has_children = abbrev->has_children;
13566
13567 for (i = 0; i < abbrev->num_attrs; ++i)
13568 {
13569 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13570
13571 /* Store the data if it is of an attribute we want to keep in a
13572 partial symbol table. */
13573 switch (attr.name)
13574 {
13575 case DW_AT_name:
13576 switch (part_die->tag)
13577 {
13578 case DW_TAG_compile_unit:
13579 case DW_TAG_partial_unit:
13580 case DW_TAG_type_unit:
13581 /* Compilation units have a DW_AT_name that is a filename, not
13582 a source language identifier. */
13583 case DW_TAG_enumeration_type:
13584 case DW_TAG_enumerator:
13585 /* These tags always have simple identifiers already; no need
13586 to canonicalize them. */
13587 part_die->name = DW_STRING (&attr);
13588 break;
13589 default:
13590 part_die->name
13591 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13592 &objfile->objfile_obstack);
13593 break;
13594 }
13595 break;
13596 case DW_AT_linkage_name:
13597 case DW_AT_MIPS_linkage_name:
13598 /* Note that both forms of linkage name might appear. We
13599 assume they will be the same, and we only store the last
13600 one we see. */
13601 if (cu->language == language_ada)
13602 part_die->name = DW_STRING (&attr);
13603 part_die->linkage_name = DW_STRING (&attr);
13604 break;
13605 case DW_AT_low_pc:
13606 has_low_pc_attr = 1;
13607 part_die->lowpc = DW_ADDR (&attr);
13608 break;
13609 case DW_AT_high_pc:
13610 has_high_pc_attr = 1;
13611 if (attr.form == DW_FORM_addr
13612 || attr.form == DW_FORM_GNU_addr_index)
13613 part_die->highpc = DW_ADDR (&attr);
13614 else
13615 {
13616 high_pc_relative = 1;
13617 part_die->highpc = DW_UNSND (&attr);
13618 }
13619 break;
13620 case DW_AT_location:
13621 /* Support the .debug_loc offsets. */
13622 if (attr_form_is_block (&attr))
13623 {
13624 part_die->d.locdesc = DW_BLOCK (&attr);
13625 }
13626 else if (attr_form_is_section_offset (&attr))
13627 {
13628 dwarf2_complex_location_expr_complaint ();
13629 }
13630 else
13631 {
13632 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13633 "partial symbol information");
13634 }
13635 break;
13636 case DW_AT_external:
13637 part_die->is_external = DW_UNSND (&attr);
13638 break;
13639 case DW_AT_declaration:
13640 part_die->is_declaration = DW_UNSND (&attr);
13641 break;
13642 case DW_AT_type:
13643 part_die->has_type = 1;
13644 break;
13645 case DW_AT_abstract_origin:
13646 case DW_AT_specification:
13647 case DW_AT_extension:
13648 part_die->has_specification = 1;
13649 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13650 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13651 || cu->per_cu->is_dwz);
13652 break;
13653 case DW_AT_sibling:
13654 /* Ignore absolute siblings, they might point outside of
13655 the current compile unit. */
13656 if (attr.form == DW_FORM_ref_addr)
13657 complaint (&symfile_complaints,
13658 _("ignoring absolute DW_AT_sibling"));
13659 else
13660 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13661 break;
13662 case DW_AT_byte_size:
13663 part_die->has_byte_size = 1;
13664 break;
13665 case DW_AT_calling_convention:
13666 /* DWARF doesn't provide a way to identify a program's source-level
13667 entry point. DW_AT_calling_convention attributes are only meant
13668 to describe functions' calling conventions.
13669
13670 However, because it's a necessary piece of information in
13671 Fortran, and because DW_CC_program is the only piece of debugging
13672 information whose definition refers to a 'main program' at all,
13673 several compilers have begun marking Fortran main programs with
13674 DW_CC_program --- even when those functions use the standard
13675 calling conventions.
13676
13677 So until DWARF specifies a way to provide this information and
13678 compilers pick up the new representation, we'll support this
13679 practice. */
13680 if (DW_UNSND (&attr) == DW_CC_program
13681 && cu->language == language_fortran)
13682 {
13683 set_main_name (part_die->name);
13684
13685 /* As this DIE has a static linkage the name would be difficult
13686 to look up later. */
13687 language_of_main = language_fortran;
13688 }
13689 break;
13690 case DW_AT_inline:
13691 if (DW_UNSND (&attr) == DW_INL_inlined
13692 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13693 part_die->may_be_inlined = 1;
13694 break;
13695
13696 case DW_AT_import:
13697 if (part_die->tag == DW_TAG_imported_unit)
13698 {
13699 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13700 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13701 || cu->per_cu->is_dwz);
13702 }
13703 break;
13704
13705 default:
13706 break;
13707 }
13708 }
13709
13710 if (high_pc_relative)
13711 part_die->highpc += part_die->lowpc;
13712
13713 if (has_low_pc_attr && has_high_pc_attr)
13714 {
13715 /* When using the GNU linker, .gnu.linkonce. sections are used to
13716 eliminate duplicate copies of functions and vtables and such.
13717 The linker will arbitrarily choose one and discard the others.
13718 The AT_*_pc values for such functions refer to local labels in
13719 these sections. If the section from that file was discarded, the
13720 labels are not in the output, so the relocs get a value of 0.
13721 If this is a discarded function, mark the pc bounds as invalid,
13722 so that GDB will ignore it. */
13723 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13724 {
13725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13726
13727 complaint (&symfile_complaints,
13728 _("DW_AT_low_pc %s is zero "
13729 "for DIE at 0x%x [in module %s]"),
13730 paddress (gdbarch, part_die->lowpc),
13731 part_die->offset.sect_off, objfile->name);
13732 }
13733 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13734 else if (part_die->lowpc >= part_die->highpc)
13735 {
13736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13737
13738 complaint (&symfile_complaints,
13739 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13740 "for DIE at 0x%x [in module %s]"),
13741 paddress (gdbarch, part_die->lowpc),
13742 paddress (gdbarch, part_die->highpc),
13743 part_die->offset.sect_off, objfile->name);
13744 }
13745 else
13746 part_die->has_pc_info = 1;
13747 }
13748
13749 return info_ptr;
13750 }
13751
13752 /* Find a cached partial DIE at OFFSET in CU. */
13753
13754 static struct partial_die_info *
13755 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13756 {
13757 struct partial_die_info *lookup_die = NULL;
13758 struct partial_die_info part_die;
13759
13760 part_die.offset = offset;
13761 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13762 offset.sect_off);
13763
13764 return lookup_die;
13765 }
13766
13767 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13768 except in the case of .debug_types DIEs which do not reference
13769 outside their CU (they do however referencing other types via
13770 DW_FORM_ref_sig8). */
13771
13772 static struct partial_die_info *
13773 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13774 {
13775 struct objfile *objfile = cu->objfile;
13776 struct dwarf2_per_cu_data *per_cu = NULL;
13777 struct partial_die_info *pd = NULL;
13778
13779 if (offset_in_dwz == cu->per_cu->is_dwz
13780 && offset_in_cu_p (&cu->header, offset))
13781 {
13782 pd = find_partial_die_in_comp_unit (offset, cu);
13783 if (pd != NULL)
13784 return pd;
13785 /* We missed recording what we needed.
13786 Load all dies and try again. */
13787 per_cu = cu->per_cu;
13788 }
13789 else
13790 {
13791 /* TUs don't reference other CUs/TUs (except via type signatures). */
13792 if (cu->per_cu->is_debug_types)
13793 {
13794 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13795 " external reference to offset 0x%lx [in module %s].\n"),
13796 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13797 bfd_get_filename (objfile->obfd));
13798 }
13799 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13800 objfile);
13801
13802 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13803 load_partial_comp_unit (per_cu);
13804
13805 per_cu->cu->last_used = 0;
13806 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13807 }
13808
13809 /* If we didn't find it, and not all dies have been loaded,
13810 load them all and try again. */
13811
13812 if (pd == NULL && per_cu->load_all_dies == 0)
13813 {
13814 per_cu->load_all_dies = 1;
13815
13816 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13817 THIS_CU->cu may already be in use. So we can't just free it and
13818 replace its DIEs with the ones we read in. Instead, we leave those
13819 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13820 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13821 set. */
13822 load_partial_comp_unit (per_cu);
13823
13824 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13825 }
13826
13827 if (pd == NULL)
13828 internal_error (__FILE__, __LINE__,
13829 _("could not find partial DIE 0x%x "
13830 "in cache [from module %s]\n"),
13831 offset.sect_off, bfd_get_filename (objfile->obfd));
13832 return pd;
13833 }
13834
13835 /* See if we can figure out if the class lives in a namespace. We do
13836 this by looking for a member function; its demangled name will
13837 contain namespace info, if there is any. */
13838
13839 static void
13840 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13841 struct dwarf2_cu *cu)
13842 {
13843 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13844 what template types look like, because the demangler
13845 frequently doesn't give the same name as the debug info. We
13846 could fix this by only using the demangled name to get the
13847 prefix (but see comment in read_structure_type). */
13848
13849 struct partial_die_info *real_pdi;
13850 struct partial_die_info *child_pdi;
13851
13852 /* If this DIE (this DIE's specification, if any) has a parent, then
13853 we should not do this. We'll prepend the parent's fully qualified
13854 name when we create the partial symbol. */
13855
13856 real_pdi = struct_pdi;
13857 while (real_pdi->has_specification)
13858 real_pdi = find_partial_die (real_pdi->spec_offset,
13859 real_pdi->spec_is_dwz, cu);
13860
13861 if (real_pdi->die_parent != NULL)
13862 return;
13863
13864 for (child_pdi = struct_pdi->die_child;
13865 child_pdi != NULL;
13866 child_pdi = child_pdi->die_sibling)
13867 {
13868 if (child_pdi->tag == DW_TAG_subprogram
13869 && child_pdi->linkage_name != NULL)
13870 {
13871 char *actual_class_name
13872 = language_class_name_from_physname (cu->language_defn,
13873 child_pdi->linkage_name);
13874 if (actual_class_name != NULL)
13875 {
13876 struct_pdi->name
13877 = obsavestring (actual_class_name,
13878 strlen (actual_class_name),
13879 &cu->objfile->objfile_obstack);
13880 xfree (actual_class_name);
13881 }
13882 break;
13883 }
13884 }
13885 }
13886
13887 /* Adjust PART_DIE before generating a symbol for it. This function
13888 may set the is_external flag or change the DIE's name. */
13889
13890 static void
13891 fixup_partial_die (struct partial_die_info *part_die,
13892 struct dwarf2_cu *cu)
13893 {
13894 /* Once we've fixed up a die, there's no point in doing so again.
13895 This also avoids a memory leak if we were to call
13896 guess_partial_die_structure_name multiple times. */
13897 if (part_die->fixup_called)
13898 return;
13899
13900 /* If we found a reference attribute and the DIE has no name, try
13901 to find a name in the referred to DIE. */
13902
13903 if (part_die->name == NULL && part_die->has_specification)
13904 {
13905 struct partial_die_info *spec_die;
13906
13907 spec_die = find_partial_die (part_die->spec_offset,
13908 part_die->spec_is_dwz, cu);
13909
13910 fixup_partial_die (spec_die, cu);
13911
13912 if (spec_die->name)
13913 {
13914 part_die->name = spec_die->name;
13915
13916 /* Copy DW_AT_external attribute if it is set. */
13917 if (spec_die->is_external)
13918 part_die->is_external = spec_die->is_external;
13919 }
13920 }
13921
13922 /* Set default names for some unnamed DIEs. */
13923
13924 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13925 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13926
13927 /* If there is no parent die to provide a namespace, and there are
13928 children, see if we can determine the namespace from their linkage
13929 name. */
13930 if (cu->language == language_cplus
13931 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13932 && part_die->die_parent == NULL
13933 && part_die->has_children
13934 && (part_die->tag == DW_TAG_class_type
13935 || part_die->tag == DW_TAG_structure_type
13936 || part_die->tag == DW_TAG_union_type))
13937 guess_partial_die_structure_name (part_die, cu);
13938
13939 /* GCC might emit a nameless struct or union that has a linkage
13940 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13941 if (part_die->name == NULL
13942 && (part_die->tag == DW_TAG_class_type
13943 || part_die->tag == DW_TAG_interface_type
13944 || part_die->tag == DW_TAG_structure_type
13945 || part_die->tag == DW_TAG_union_type)
13946 && part_die->linkage_name != NULL)
13947 {
13948 char *demangled;
13949
13950 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13951 if (demangled)
13952 {
13953 const char *base;
13954
13955 /* Strip any leading namespaces/classes, keep only the base name.
13956 DW_AT_name for named DIEs does not contain the prefixes. */
13957 base = strrchr (demangled, ':');
13958 if (base && base > demangled && base[-1] == ':')
13959 base++;
13960 else
13961 base = demangled;
13962
13963 part_die->name = obsavestring (base, strlen (base),
13964 &cu->objfile->objfile_obstack);
13965 xfree (demangled);
13966 }
13967 }
13968
13969 part_die->fixup_called = 1;
13970 }
13971
13972 /* Read an attribute value described by an attribute form. */
13973
13974 static gdb_byte *
13975 read_attribute_value (const struct die_reader_specs *reader,
13976 struct attribute *attr, unsigned form,
13977 gdb_byte *info_ptr)
13978 {
13979 struct dwarf2_cu *cu = reader->cu;
13980 bfd *abfd = reader->abfd;
13981 struct comp_unit_head *cu_header = &cu->header;
13982 unsigned int bytes_read;
13983 struct dwarf_block *blk;
13984
13985 attr->form = form;
13986 switch (form)
13987 {
13988 case DW_FORM_ref_addr:
13989 if (cu->header.version == 2)
13990 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13991 else
13992 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13993 &cu->header, &bytes_read);
13994 info_ptr += bytes_read;
13995 break;
13996 case DW_FORM_GNU_ref_alt:
13997 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13998 info_ptr += bytes_read;
13999 break;
14000 case DW_FORM_addr:
14001 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14002 info_ptr += bytes_read;
14003 break;
14004 case DW_FORM_block2:
14005 blk = dwarf_alloc_block (cu);
14006 blk->size = read_2_bytes (abfd, info_ptr);
14007 info_ptr += 2;
14008 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14009 info_ptr += blk->size;
14010 DW_BLOCK (attr) = blk;
14011 break;
14012 case DW_FORM_block4:
14013 blk = dwarf_alloc_block (cu);
14014 blk->size = read_4_bytes (abfd, info_ptr);
14015 info_ptr += 4;
14016 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14017 info_ptr += blk->size;
14018 DW_BLOCK (attr) = blk;
14019 break;
14020 case DW_FORM_data2:
14021 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14022 info_ptr += 2;
14023 break;
14024 case DW_FORM_data4:
14025 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14026 info_ptr += 4;
14027 break;
14028 case DW_FORM_data8:
14029 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14030 info_ptr += 8;
14031 break;
14032 case DW_FORM_sec_offset:
14033 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14034 info_ptr += bytes_read;
14035 break;
14036 case DW_FORM_string:
14037 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14038 DW_STRING_IS_CANONICAL (attr) = 0;
14039 info_ptr += bytes_read;
14040 break;
14041 case DW_FORM_strp:
14042 if (!cu->per_cu->is_dwz)
14043 {
14044 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14045 &bytes_read);
14046 DW_STRING_IS_CANONICAL (attr) = 0;
14047 info_ptr += bytes_read;
14048 break;
14049 }
14050 /* FALLTHROUGH */
14051 case DW_FORM_GNU_strp_alt:
14052 {
14053 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14054 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14055 &bytes_read);
14056
14057 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14058 DW_STRING_IS_CANONICAL (attr) = 0;
14059 info_ptr += bytes_read;
14060 }
14061 break;
14062 case DW_FORM_exprloc:
14063 case DW_FORM_block:
14064 blk = dwarf_alloc_block (cu);
14065 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14066 info_ptr += bytes_read;
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14070 break;
14071 case DW_FORM_block1:
14072 blk = dwarf_alloc_block (cu);
14073 blk->size = read_1_byte (abfd, info_ptr);
14074 info_ptr += 1;
14075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14076 info_ptr += blk->size;
14077 DW_BLOCK (attr) = blk;
14078 break;
14079 case DW_FORM_data1:
14080 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14081 info_ptr += 1;
14082 break;
14083 case DW_FORM_flag:
14084 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14085 info_ptr += 1;
14086 break;
14087 case DW_FORM_flag_present:
14088 DW_UNSND (attr) = 1;
14089 break;
14090 case DW_FORM_sdata:
14091 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14092 info_ptr += bytes_read;
14093 break;
14094 case DW_FORM_udata:
14095 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14096 info_ptr += bytes_read;
14097 break;
14098 case DW_FORM_ref1:
14099 DW_UNSND (attr) = (cu->header.offset.sect_off
14100 + read_1_byte (abfd, info_ptr));
14101 info_ptr += 1;
14102 break;
14103 case DW_FORM_ref2:
14104 DW_UNSND (attr) = (cu->header.offset.sect_off
14105 + read_2_bytes (abfd, info_ptr));
14106 info_ptr += 2;
14107 break;
14108 case DW_FORM_ref4:
14109 DW_UNSND (attr) = (cu->header.offset.sect_off
14110 + read_4_bytes (abfd, info_ptr));
14111 info_ptr += 4;
14112 break;
14113 case DW_FORM_ref8:
14114 DW_UNSND (attr) = (cu->header.offset.sect_off
14115 + read_8_bytes (abfd, info_ptr));
14116 info_ptr += 8;
14117 break;
14118 case DW_FORM_ref_sig8:
14119 /* Convert the signature to something we can record in DW_UNSND
14120 for later lookup.
14121 NOTE: This is NULL if the type wasn't found. */
14122 DW_SIGNATURED_TYPE (attr) =
14123 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14124 info_ptr += 8;
14125 break;
14126 case DW_FORM_ref_udata:
14127 DW_UNSND (attr) = (cu->header.offset.sect_off
14128 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14129 info_ptr += bytes_read;
14130 break;
14131 case DW_FORM_indirect:
14132 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14133 info_ptr += bytes_read;
14134 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14135 break;
14136 case DW_FORM_GNU_addr_index:
14137 if (reader->dwo_file == NULL)
14138 {
14139 /* For now flag a hard error.
14140 Later we can turn this into a complaint. */
14141 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14142 dwarf_form_name (form),
14143 bfd_get_filename (abfd));
14144 }
14145 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14146 info_ptr += bytes_read;
14147 break;
14148 case DW_FORM_GNU_str_index:
14149 if (reader->dwo_file == NULL)
14150 {
14151 /* For now flag a hard error.
14152 Later we can turn this into a complaint if warranted. */
14153 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14154 dwarf_form_name (form),
14155 bfd_get_filename (abfd));
14156 }
14157 {
14158 ULONGEST str_index =
14159 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14160
14161 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14162 DW_STRING_IS_CANONICAL (attr) = 0;
14163 info_ptr += bytes_read;
14164 }
14165 break;
14166 default:
14167 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14168 dwarf_form_name (form),
14169 bfd_get_filename (abfd));
14170 }
14171
14172 /* Super hack. */
14173 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14174 attr->form = DW_FORM_GNU_ref_alt;
14175
14176 /* We have seen instances where the compiler tried to emit a byte
14177 size attribute of -1 which ended up being encoded as an unsigned
14178 0xffffffff. Although 0xffffffff is technically a valid size value,
14179 an object of this size seems pretty unlikely so we can relatively
14180 safely treat these cases as if the size attribute was invalid and
14181 treat them as zero by default. */
14182 if (attr->name == DW_AT_byte_size
14183 && form == DW_FORM_data4
14184 && DW_UNSND (attr) >= 0xffffffff)
14185 {
14186 complaint
14187 (&symfile_complaints,
14188 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14189 hex_string (DW_UNSND (attr)));
14190 DW_UNSND (attr) = 0;
14191 }
14192
14193 return info_ptr;
14194 }
14195
14196 /* Read an attribute described by an abbreviated attribute. */
14197
14198 static gdb_byte *
14199 read_attribute (const struct die_reader_specs *reader,
14200 struct attribute *attr, struct attr_abbrev *abbrev,
14201 gdb_byte *info_ptr)
14202 {
14203 attr->name = abbrev->name;
14204 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14205 }
14206
14207 /* Read dwarf information from a buffer. */
14208
14209 static unsigned int
14210 read_1_byte (bfd *abfd, const gdb_byte *buf)
14211 {
14212 return bfd_get_8 (abfd, buf);
14213 }
14214
14215 static int
14216 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14217 {
14218 return bfd_get_signed_8 (abfd, buf);
14219 }
14220
14221 static unsigned int
14222 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14223 {
14224 return bfd_get_16 (abfd, buf);
14225 }
14226
14227 static int
14228 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14229 {
14230 return bfd_get_signed_16 (abfd, buf);
14231 }
14232
14233 static unsigned int
14234 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14235 {
14236 return bfd_get_32 (abfd, buf);
14237 }
14238
14239 static int
14240 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14241 {
14242 return bfd_get_signed_32 (abfd, buf);
14243 }
14244
14245 static ULONGEST
14246 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14247 {
14248 return bfd_get_64 (abfd, buf);
14249 }
14250
14251 static CORE_ADDR
14252 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14253 unsigned int *bytes_read)
14254 {
14255 struct comp_unit_head *cu_header = &cu->header;
14256 CORE_ADDR retval = 0;
14257
14258 if (cu_header->signed_addr_p)
14259 {
14260 switch (cu_header->addr_size)
14261 {
14262 case 2:
14263 retval = bfd_get_signed_16 (abfd, buf);
14264 break;
14265 case 4:
14266 retval = bfd_get_signed_32 (abfd, buf);
14267 break;
14268 case 8:
14269 retval = bfd_get_signed_64 (abfd, buf);
14270 break;
14271 default:
14272 internal_error (__FILE__, __LINE__,
14273 _("read_address: bad switch, signed [in module %s]"),
14274 bfd_get_filename (abfd));
14275 }
14276 }
14277 else
14278 {
14279 switch (cu_header->addr_size)
14280 {
14281 case 2:
14282 retval = bfd_get_16 (abfd, buf);
14283 break;
14284 case 4:
14285 retval = bfd_get_32 (abfd, buf);
14286 break;
14287 case 8:
14288 retval = bfd_get_64 (abfd, buf);
14289 break;
14290 default:
14291 internal_error (__FILE__, __LINE__,
14292 _("read_address: bad switch, "
14293 "unsigned [in module %s]"),
14294 bfd_get_filename (abfd));
14295 }
14296 }
14297
14298 *bytes_read = cu_header->addr_size;
14299 return retval;
14300 }
14301
14302 /* Read the initial length from a section. The (draft) DWARF 3
14303 specification allows the initial length to take up either 4 bytes
14304 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14305 bytes describe the length and all offsets will be 8 bytes in length
14306 instead of 4.
14307
14308 An older, non-standard 64-bit format is also handled by this
14309 function. The older format in question stores the initial length
14310 as an 8-byte quantity without an escape value. Lengths greater
14311 than 2^32 aren't very common which means that the initial 4 bytes
14312 is almost always zero. Since a length value of zero doesn't make
14313 sense for the 32-bit format, this initial zero can be considered to
14314 be an escape value which indicates the presence of the older 64-bit
14315 format. As written, the code can't detect (old format) lengths
14316 greater than 4GB. If it becomes necessary to handle lengths
14317 somewhat larger than 4GB, we could allow other small values (such
14318 as the non-sensical values of 1, 2, and 3) to also be used as
14319 escape values indicating the presence of the old format.
14320
14321 The value returned via bytes_read should be used to increment the
14322 relevant pointer after calling read_initial_length().
14323
14324 [ Note: read_initial_length() and read_offset() are based on the
14325 document entitled "DWARF Debugging Information Format", revision
14326 3, draft 8, dated November 19, 2001. This document was obtained
14327 from:
14328
14329 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14330
14331 This document is only a draft and is subject to change. (So beware.)
14332
14333 Details regarding the older, non-standard 64-bit format were
14334 determined empirically by examining 64-bit ELF files produced by
14335 the SGI toolchain on an IRIX 6.5 machine.
14336
14337 - Kevin, July 16, 2002
14338 ] */
14339
14340 static LONGEST
14341 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14342 {
14343 LONGEST length = bfd_get_32 (abfd, buf);
14344
14345 if (length == 0xffffffff)
14346 {
14347 length = bfd_get_64 (abfd, buf + 4);
14348 *bytes_read = 12;
14349 }
14350 else if (length == 0)
14351 {
14352 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14353 length = bfd_get_64 (abfd, buf);
14354 *bytes_read = 8;
14355 }
14356 else
14357 {
14358 *bytes_read = 4;
14359 }
14360
14361 return length;
14362 }
14363
14364 /* Cover function for read_initial_length.
14365 Returns the length of the object at BUF, and stores the size of the
14366 initial length in *BYTES_READ and stores the size that offsets will be in
14367 *OFFSET_SIZE.
14368 If the initial length size is not equivalent to that specified in
14369 CU_HEADER then issue a complaint.
14370 This is useful when reading non-comp-unit headers. */
14371
14372 static LONGEST
14373 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14374 const struct comp_unit_head *cu_header,
14375 unsigned int *bytes_read,
14376 unsigned int *offset_size)
14377 {
14378 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14379
14380 gdb_assert (cu_header->initial_length_size == 4
14381 || cu_header->initial_length_size == 8
14382 || cu_header->initial_length_size == 12);
14383
14384 if (cu_header->initial_length_size != *bytes_read)
14385 complaint (&symfile_complaints,
14386 _("intermixed 32-bit and 64-bit DWARF sections"));
14387
14388 *offset_size = (*bytes_read == 4) ? 4 : 8;
14389 return length;
14390 }
14391
14392 /* Read an offset from the data stream. The size of the offset is
14393 given by cu_header->offset_size. */
14394
14395 static LONGEST
14396 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14397 unsigned int *bytes_read)
14398 {
14399 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14400
14401 *bytes_read = cu_header->offset_size;
14402 return offset;
14403 }
14404
14405 /* Read an offset from the data stream. */
14406
14407 static LONGEST
14408 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14409 {
14410 LONGEST retval = 0;
14411
14412 switch (offset_size)
14413 {
14414 case 4:
14415 retval = bfd_get_32 (abfd, buf);
14416 break;
14417 case 8:
14418 retval = bfd_get_64 (abfd, buf);
14419 break;
14420 default:
14421 internal_error (__FILE__, __LINE__,
14422 _("read_offset_1: bad switch [in module %s]"),
14423 bfd_get_filename (abfd));
14424 }
14425
14426 return retval;
14427 }
14428
14429 static gdb_byte *
14430 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14431 {
14432 /* If the size of a host char is 8 bits, we can return a pointer
14433 to the buffer, otherwise we have to copy the data to a buffer
14434 allocated on the temporary obstack. */
14435 gdb_assert (HOST_CHAR_BIT == 8);
14436 return buf;
14437 }
14438
14439 static char *
14440 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14441 {
14442 /* If the size of a host char is 8 bits, we can return a pointer
14443 to the string, otherwise we have to copy the string to a buffer
14444 allocated on the temporary obstack. */
14445 gdb_assert (HOST_CHAR_BIT == 8);
14446 if (*buf == '\0')
14447 {
14448 *bytes_read_ptr = 1;
14449 return NULL;
14450 }
14451 *bytes_read_ptr = strlen ((char *) buf) + 1;
14452 return (char *) buf;
14453 }
14454
14455 static char *
14456 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14457 {
14458 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14459 if (dwarf2_per_objfile->str.buffer == NULL)
14460 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14461 bfd_get_filename (abfd));
14462 if (str_offset >= dwarf2_per_objfile->str.size)
14463 error (_("DW_FORM_strp pointing outside of "
14464 ".debug_str section [in module %s]"),
14465 bfd_get_filename (abfd));
14466 gdb_assert (HOST_CHAR_BIT == 8);
14467 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14468 return NULL;
14469 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14470 }
14471
14472 /* Read a string at offset STR_OFFSET in the .debug_str section from
14473 the .dwz file DWZ. Throw an error if the offset is too large. If
14474 the string consists of a single NUL byte, return NULL; otherwise
14475 return a pointer to the string. */
14476
14477 static char *
14478 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14479 {
14480 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14481
14482 if (dwz->str.buffer == NULL)
14483 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14484 "section [in module %s]"),
14485 bfd_get_filename (dwz->dwz_bfd));
14486 if (str_offset >= dwz->str.size)
14487 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14488 ".debug_str section [in module %s]"),
14489 bfd_get_filename (dwz->dwz_bfd));
14490 gdb_assert (HOST_CHAR_BIT == 8);
14491 if (dwz->str.buffer[str_offset] == '\0')
14492 return NULL;
14493 return (char *) (dwz->str.buffer + str_offset);
14494 }
14495
14496 static char *
14497 read_indirect_string (bfd *abfd, gdb_byte *buf,
14498 const struct comp_unit_head *cu_header,
14499 unsigned int *bytes_read_ptr)
14500 {
14501 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14502
14503 return read_indirect_string_at_offset (abfd, str_offset);
14504 }
14505
14506 static ULONGEST
14507 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14508 {
14509 ULONGEST result;
14510 unsigned int num_read;
14511 int i, shift;
14512 unsigned char byte;
14513
14514 result = 0;
14515 shift = 0;
14516 num_read = 0;
14517 i = 0;
14518 while (1)
14519 {
14520 byte = bfd_get_8 (abfd, buf);
14521 buf++;
14522 num_read++;
14523 result |= ((ULONGEST) (byte & 127) << shift);
14524 if ((byte & 128) == 0)
14525 {
14526 break;
14527 }
14528 shift += 7;
14529 }
14530 *bytes_read_ptr = num_read;
14531 return result;
14532 }
14533
14534 static LONGEST
14535 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14536 {
14537 LONGEST result;
14538 int i, shift, num_read;
14539 unsigned char byte;
14540
14541 result = 0;
14542 shift = 0;
14543 num_read = 0;
14544 i = 0;
14545 while (1)
14546 {
14547 byte = bfd_get_8 (abfd, buf);
14548 buf++;
14549 num_read++;
14550 result |= ((LONGEST) (byte & 127) << shift);
14551 shift += 7;
14552 if ((byte & 128) == 0)
14553 {
14554 break;
14555 }
14556 }
14557 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14558 result |= -(((LONGEST) 1) << shift);
14559 *bytes_read_ptr = num_read;
14560 return result;
14561 }
14562
14563 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14564 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14565 ADDR_SIZE is the size of addresses from the CU header. */
14566
14567 static CORE_ADDR
14568 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14569 {
14570 struct objfile *objfile = dwarf2_per_objfile->objfile;
14571 bfd *abfd = objfile->obfd;
14572 const gdb_byte *info_ptr;
14573
14574 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14575 if (dwarf2_per_objfile->addr.buffer == NULL)
14576 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14577 objfile->name);
14578 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14579 error (_("DW_FORM_addr_index pointing outside of "
14580 ".debug_addr section [in module %s]"),
14581 objfile->name);
14582 info_ptr = (dwarf2_per_objfile->addr.buffer
14583 + addr_base + addr_index * addr_size);
14584 if (addr_size == 4)
14585 return bfd_get_32 (abfd, info_ptr);
14586 else
14587 return bfd_get_64 (abfd, info_ptr);
14588 }
14589
14590 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14591
14592 static CORE_ADDR
14593 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14594 {
14595 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14596 }
14597
14598 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14599
14600 static CORE_ADDR
14601 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14602 unsigned int *bytes_read)
14603 {
14604 bfd *abfd = cu->objfile->obfd;
14605 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14606
14607 return read_addr_index (cu, addr_index);
14608 }
14609
14610 /* Data structure to pass results from dwarf2_read_addr_index_reader
14611 back to dwarf2_read_addr_index. */
14612
14613 struct dwarf2_read_addr_index_data
14614 {
14615 ULONGEST addr_base;
14616 int addr_size;
14617 };
14618
14619 /* die_reader_func for dwarf2_read_addr_index. */
14620
14621 static void
14622 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14623 gdb_byte *info_ptr,
14624 struct die_info *comp_unit_die,
14625 int has_children,
14626 void *data)
14627 {
14628 struct dwarf2_cu *cu = reader->cu;
14629 struct dwarf2_read_addr_index_data *aidata =
14630 (struct dwarf2_read_addr_index_data *) data;
14631
14632 aidata->addr_base = cu->addr_base;
14633 aidata->addr_size = cu->header.addr_size;
14634 }
14635
14636 /* Given an index in .debug_addr, fetch the value.
14637 NOTE: This can be called during dwarf expression evaluation,
14638 long after the debug information has been read, and thus per_cu->cu
14639 may no longer exist. */
14640
14641 CORE_ADDR
14642 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14643 unsigned int addr_index)
14644 {
14645 struct objfile *objfile = per_cu->objfile;
14646 struct dwarf2_cu *cu = per_cu->cu;
14647 ULONGEST addr_base;
14648 int addr_size;
14649
14650 /* This is intended to be called from outside this file. */
14651 dw2_setup (objfile);
14652
14653 /* We need addr_base and addr_size.
14654 If we don't have PER_CU->cu, we have to get it.
14655 Nasty, but the alternative is storing the needed info in PER_CU,
14656 which at this point doesn't seem justified: it's not clear how frequently
14657 it would get used and it would increase the size of every PER_CU.
14658 Entry points like dwarf2_per_cu_addr_size do a similar thing
14659 so we're not in uncharted territory here.
14660 Alas we need to be a bit more complicated as addr_base is contained
14661 in the DIE.
14662
14663 We don't need to read the entire CU(/TU).
14664 We just need the header and top level die.
14665
14666 IWBN to use the aging mechanism to let us lazily later discard the CU.
14667 For now we skip this optimization. */
14668
14669 if (cu != NULL)
14670 {
14671 addr_base = cu->addr_base;
14672 addr_size = cu->header.addr_size;
14673 }
14674 else
14675 {
14676 struct dwarf2_read_addr_index_data aidata;
14677
14678 /* Note: We can't use init_cutu_and_read_dies_simple here,
14679 we need addr_base. */
14680 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14681 dwarf2_read_addr_index_reader, &aidata);
14682 addr_base = aidata.addr_base;
14683 addr_size = aidata.addr_size;
14684 }
14685
14686 return read_addr_index_1 (addr_index, addr_base, addr_size);
14687 }
14688
14689 /* Given a DW_AT_str_index, fetch the string. */
14690
14691 static char *
14692 read_str_index (const struct die_reader_specs *reader,
14693 struct dwarf2_cu *cu, ULONGEST str_index)
14694 {
14695 struct objfile *objfile = dwarf2_per_objfile->objfile;
14696 const char *dwo_name = objfile->name;
14697 bfd *abfd = objfile->obfd;
14698 struct dwo_sections *sections = &reader->dwo_file->sections;
14699 gdb_byte *info_ptr;
14700 ULONGEST str_offset;
14701
14702 dwarf2_read_section (objfile, &sections->str);
14703 dwarf2_read_section (objfile, &sections->str_offsets);
14704 if (sections->str.buffer == NULL)
14705 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14706 " in CU at offset 0x%lx [in module %s]"),
14707 (long) cu->header.offset.sect_off, dwo_name);
14708 if (sections->str_offsets.buffer == NULL)
14709 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14710 " in CU at offset 0x%lx [in module %s]"),
14711 (long) cu->header.offset.sect_off, dwo_name);
14712 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14713 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14714 " section in CU at offset 0x%lx [in module %s]"),
14715 (long) cu->header.offset.sect_off, dwo_name);
14716 info_ptr = (sections->str_offsets.buffer
14717 + str_index * cu->header.offset_size);
14718 if (cu->header.offset_size == 4)
14719 str_offset = bfd_get_32 (abfd, info_ptr);
14720 else
14721 str_offset = bfd_get_64 (abfd, info_ptr);
14722 if (str_offset >= sections->str.size)
14723 error (_("Offset from DW_FORM_str_index pointing outside of"
14724 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14725 (long) cu->header.offset.sect_off, dwo_name);
14726 return (char *) (sections->str.buffer + str_offset);
14727 }
14728
14729 /* Return the length of an LEB128 number in BUF. */
14730
14731 static int
14732 leb128_size (const gdb_byte *buf)
14733 {
14734 const gdb_byte *begin = buf;
14735 gdb_byte byte;
14736
14737 while (1)
14738 {
14739 byte = *buf++;
14740 if ((byte & 128) == 0)
14741 return buf - begin;
14742 }
14743 }
14744
14745 static void
14746 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14747 {
14748 switch (lang)
14749 {
14750 case DW_LANG_C89:
14751 case DW_LANG_C99:
14752 case DW_LANG_C:
14753 cu->language = language_c;
14754 break;
14755 case DW_LANG_C_plus_plus:
14756 cu->language = language_cplus;
14757 break;
14758 case DW_LANG_D:
14759 cu->language = language_d;
14760 break;
14761 case DW_LANG_Fortran77:
14762 case DW_LANG_Fortran90:
14763 case DW_LANG_Fortran95:
14764 cu->language = language_fortran;
14765 break;
14766 case DW_LANG_Go:
14767 cu->language = language_go;
14768 break;
14769 case DW_LANG_Mips_Assembler:
14770 cu->language = language_asm;
14771 break;
14772 case DW_LANG_Java:
14773 cu->language = language_java;
14774 break;
14775 case DW_LANG_Ada83:
14776 case DW_LANG_Ada95:
14777 cu->language = language_ada;
14778 break;
14779 case DW_LANG_Modula2:
14780 cu->language = language_m2;
14781 break;
14782 case DW_LANG_Pascal83:
14783 cu->language = language_pascal;
14784 break;
14785 case DW_LANG_ObjC:
14786 cu->language = language_objc;
14787 break;
14788 case DW_LANG_Cobol74:
14789 case DW_LANG_Cobol85:
14790 default:
14791 cu->language = language_minimal;
14792 break;
14793 }
14794 cu->language_defn = language_def (cu->language);
14795 }
14796
14797 /* Return the named attribute or NULL if not there. */
14798
14799 static struct attribute *
14800 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14801 {
14802 for (;;)
14803 {
14804 unsigned int i;
14805 struct attribute *spec = NULL;
14806
14807 for (i = 0; i < die->num_attrs; ++i)
14808 {
14809 if (die->attrs[i].name == name)
14810 return &die->attrs[i];
14811 if (die->attrs[i].name == DW_AT_specification
14812 || die->attrs[i].name == DW_AT_abstract_origin)
14813 spec = &die->attrs[i];
14814 }
14815
14816 if (!spec)
14817 break;
14818
14819 die = follow_die_ref (die, spec, &cu);
14820 }
14821
14822 return NULL;
14823 }
14824
14825 /* Return the named attribute or NULL if not there,
14826 but do not follow DW_AT_specification, etc.
14827 This is for use in contexts where we're reading .debug_types dies.
14828 Following DW_AT_specification, DW_AT_abstract_origin will take us
14829 back up the chain, and we want to go down. */
14830
14831 static struct attribute *
14832 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14833 {
14834 unsigned int i;
14835
14836 for (i = 0; i < die->num_attrs; ++i)
14837 if (die->attrs[i].name == name)
14838 return &die->attrs[i];
14839
14840 return NULL;
14841 }
14842
14843 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14844 and holds a non-zero value. This function should only be used for
14845 DW_FORM_flag or DW_FORM_flag_present attributes. */
14846
14847 static int
14848 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14849 {
14850 struct attribute *attr = dwarf2_attr (die, name, cu);
14851
14852 return (attr && DW_UNSND (attr));
14853 }
14854
14855 static int
14856 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14857 {
14858 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14859 which value is non-zero. However, we have to be careful with
14860 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14861 (via dwarf2_flag_true_p) follows this attribute. So we may
14862 end up accidently finding a declaration attribute that belongs
14863 to a different DIE referenced by the specification attribute,
14864 even though the given DIE does not have a declaration attribute. */
14865 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14866 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14867 }
14868
14869 /* Return the die giving the specification for DIE, if there is
14870 one. *SPEC_CU is the CU containing DIE on input, and the CU
14871 containing the return value on output. If there is no
14872 specification, but there is an abstract origin, that is
14873 returned. */
14874
14875 static struct die_info *
14876 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14877 {
14878 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14879 *spec_cu);
14880
14881 if (spec_attr == NULL)
14882 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14883
14884 if (spec_attr == NULL)
14885 return NULL;
14886 else
14887 return follow_die_ref (die, spec_attr, spec_cu);
14888 }
14889
14890 /* Free the line_header structure *LH, and any arrays and strings it
14891 refers to.
14892 NOTE: This is also used as a "cleanup" function. */
14893
14894 static void
14895 free_line_header (struct line_header *lh)
14896 {
14897 if (lh->standard_opcode_lengths)
14898 xfree (lh->standard_opcode_lengths);
14899
14900 /* Remember that all the lh->file_names[i].name pointers are
14901 pointers into debug_line_buffer, and don't need to be freed. */
14902 if (lh->file_names)
14903 xfree (lh->file_names);
14904
14905 /* Similarly for the include directory names. */
14906 if (lh->include_dirs)
14907 xfree (lh->include_dirs);
14908
14909 xfree (lh);
14910 }
14911
14912 /* Add an entry to LH's include directory table. */
14913
14914 static void
14915 add_include_dir (struct line_header *lh, char *include_dir)
14916 {
14917 /* Grow the array if necessary. */
14918 if (lh->include_dirs_size == 0)
14919 {
14920 lh->include_dirs_size = 1; /* for testing */
14921 lh->include_dirs = xmalloc (lh->include_dirs_size
14922 * sizeof (*lh->include_dirs));
14923 }
14924 else if (lh->num_include_dirs >= lh->include_dirs_size)
14925 {
14926 lh->include_dirs_size *= 2;
14927 lh->include_dirs = xrealloc (lh->include_dirs,
14928 (lh->include_dirs_size
14929 * sizeof (*lh->include_dirs)));
14930 }
14931
14932 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14933 }
14934
14935 /* Add an entry to LH's file name table. */
14936
14937 static void
14938 add_file_name (struct line_header *lh,
14939 char *name,
14940 unsigned int dir_index,
14941 unsigned int mod_time,
14942 unsigned int length)
14943 {
14944 struct file_entry *fe;
14945
14946 /* Grow the array if necessary. */
14947 if (lh->file_names_size == 0)
14948 {
14949 lh->file_names_size = 1; /* for testing */
14950 lh->file_names = xmalloc (lh->file_names_size
14951 * sizeof (*lh->file_names));
14952 }
14953 else if (lh->num_file_names >= lh->file_names_size)
14954 {
14955 lh->file_names_size *= 2;
14956 lh->file_names = xrealloc (lh->file_names,
14957 (lh->file_names_size
14958 * sizeof (*lh->file_names)));
14959 }
14960
14961 fe = &lh->file_names[lh->num_file_names++];
14962 fe->name = name;
14963 fe->dir_index = dir_index;
14964 fe->mod_time = mod_time;
14965 fe->length = length;
14966 fe->included_p = 0;
14967 fe->symtab = NULL;
14968 }
14969
14970 /* A convenience function to find the proper .debug_line section for a
14971 CU. */
14972
14973 static struct dwarf2_section_info *
14974 get_debug_line_section (struct dwarf2_cu *cu)
14975 {
14976 struct dwarf2_section_info *section;
14977
14978 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14979 DWO file. */
14980 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14981 section = &cu->dwo_unit->dwo_file->sections.line;
14982 else if (cu->per_cu->is_dwz)
14983 {
14984 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14985
14986 section = &dwz->line;
14987 }
14988 else
14989 section = &dwarf2_per_objfile->line;
14990
14991 return section;
14992 }
14993
14994 /* Read the statement program header starting at OFFSET in
14995 .debug_line, or .debug_line.dwo. Return a pointer
14996 to a struct line_header, allocated using xmalloc.
14997
14998 NOTE: the strings in the include directory and file name tables of
14999 the returned object point into the dwarf line section buffer,
15000 and must not be freed. */
15001
15002 static struct line_header *
15003 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15004 {
15005 struct cleanup *back_to;
15006 struct line_header *lh;
15007 gdb_byte *line_ptr;
15008 unsigned int bytes_read, offset_size;
15009 int i;
15010 char *cur_dir, *cur_file;
15011 struct dwarf2_section_info *section;
15012 bfd *abfd;
15013
15014 section = get_debug_line_section (cu);
15015 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15016 if (section->buffer == NULL)
15017 {
15018 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15019 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15020 else
15021 complaint (&symfile_complaints, _("missing .debug_line section"));
15022 return 0;
15023 }
15024
15025 /* We can't do this until we know the section is non-empty.
15026 Only then do we know we have such a section. */
15027 abfd = section->asection->owner;
15028
15029 /* Make sure that at least there's room for the total_length field.
15030 That could be 12 bytes long, but we're just going to fudge that. */
15031 if (offset + 4 >= section->size)
15032 {
15033 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15034 return 0;
15035 }
15036
15037 lh = xmalloc (sizeof (*lh));
15038 memset (lh, 0, sizeof (*lh));
15039 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15040 (void *) lh);
15041
15042 line_ptr = section->buffer + offset;
15043
15044 /* Read in the header. */
15045 lh->total_length =
15046 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15047 &bytes_read, &offset_size);
15048 line_ptr += bytes_read;
15049 if (line_ptr + lh->total_length > (section->buffer + section->size))
15050 {
15051 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15052 return 0;
15053 }
15054 lh->statement_program_end = line_ptr + lh->total_length;
15055 lh->version = read_2_bytes (abfd, line_ptr);
15056 line_ptr += 2;
15057 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15058 line_ptr += offset_size;
15059 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15060 line_ptr += 1;
15061 if (lh->version >= 4)
15062 {
15063 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15064 line_ptr += 1;
15065 }
15066 else
15067 lh->maximum_ops_per_instruction = 1;
15068
15069 if (lh->maximum_ops_per_instruction == 0)
15070 {
15071 lh->maximum_ops_per_instruction = 1;
15072 complaint (&symfile_complaints,
15073 _("invalid maximum_ops_per_instruction "
15074 "in `.debug_line' section"));
15075 }
15076
15077 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15078 line_ptr += 1;
15079 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15080 line_ptr += 1;
15081 lh->line_range = read_1_byte (abfd, line_ptr);
15082 line_ptr += 1;
15083 lh->opcode_base = read_1_byte (abfd, line_ptr);
15084 line_ptr += 1;
15085 lh->standard_opcode_lengths
15086 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15087
15088 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15089 for (i = 1; i < lh->opcode_base; ++i)
15090 {
15091 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15092 line_ptr += 1;
15093 }
15094
15095 /* Read directory table. */
15096 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15097 {
15098 line_ptr += bytes_read;
15099 add_include_dir (lh, cur_dir);
15100 }
15101 line_ptr += bytes_read;
15102
15103 /* Read file name table. */
15104 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15105 {
15106 unsigned int dir_index, mod_time, length;
15107
15108 line_ptr += bytes_read;
15109 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15110 line_ptr += bytes_read;
15111 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15112 line_ptr += bytes_read;
15113 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15114 line_ptr += bytes_read;
15115
15116 add_file_name (lh, cur_file, dir_index, mod_time, length);
15117 }
15118 line_ptr += bytes_read;
15119 lh->statement_program_start = line_ptr;
15120
15121 if (line_ptr > (section->buffer + section->size))
15122 complaint (&symfile_complaints,
15123 _("line number info header doesn't "
15124 "fit in `.debug_line' section"));
15125
15126 discard_cleanups (back_to);
15127 return lh;
15128 }
15129
15130 /* Subroutine of dwarf_decode_lines to simplify it.
15131 Return the file name of the psymtab for included file FILE_INDEX
15132 in line header LH of PST.
15133 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15134 If space for the result is malloc'd, it will be freed by a cleanup.
15135 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15136
15137 static char *
15138 psymtab_include_file_name (const struct line_header *lh, int file_index,
15139 const struct partial_symtab *pst,
15140 const char *comp_dir)
15141 {
15142 const struct file_entry fe = lh->file_names [file_index];
15143 char *include_name = fe.name;
15144 char *include_name_to_compare = include_name;
15145 char *dir_name = NULL;
15146 const char *pst_filename;
15147 char *copied_name = NULL;
15148 int file_is_pst;
15149
15150 if (fe.dir_index)
15151 dir_name = lh->include_dirs[fe.dir_index - 1];
15152
15153 if (!IS_ABSOLUTE_PATH (include_name)
15154 && (dir_name != NULL || comp_dir != NULL))
15155 {
15156 /* Avoid creating a duplicate psymtab for PST.
15157 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15158 Before we do the comparison, however, we need to account
15159 for DIR_NAME and COMP_DIR.
15160 First prepend dir_name (if non-NULL). If we still don't
15161 have an absolute path prepend comp_dir (if non-NULL).
15162 However, the directory we record in the include-file's
15163 psymtab does not contain COMP_DIR (to match the
15164 corresponding symtab(s)).
15165
15166 Example:
15167
15168 bash$ cd /tmp
15169 bash$ gcc -g ./hello.c
15170 include_name = "hello.c"
15171 dir_name = "."
15172 DW_AT_comp_dir = comp_dir = "/tmp"
15173 DW_AT_name = "./hello.c" */
15174
15175 if (dir_name != NULL)
15176 {
15177 include_name = concat (dir_name, SLASH_STRING,
15178 include_name, (char *)NULL);
15179 include_name_to_compare = include_name;
15180 make_cleanup (xfree, include_name);
15181 }
15182 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15183 {
15184 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15185 include_name, (char *)NULL);
15186 }
15187 }
15188
15189 pst_filename = pst->filename;
15190 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15191 {
15192 copied_name = concat (pst->dirname, SLASH_STRING,
15193 pst_filename, (char *)NULL);
15194 pst_filename = copied_name;
15195 }
15196
15197 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15198
15199 if (include_name_to_compare != include_name)
15200 xfree (include_name_to_compare);
15201 if (copied_name != NULL)
15202 xfree (copied_name);
15203
15204 if (file_is_pst)
15205 return NULL;
15206 return include_name;
15207 }
15208
15209 /* Ignore this record_line request. */
15210
15211 static void
15212 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15213 {
15214 return;
15215 }
15216
15217 /* Subroutine of dwarf_decode_lines to simplify it.
15218 Process the line number information in LH. */
15219
15220 static void
15221 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15222 struct dwarf2_cu *cu, struct partial_symtab *pst)
15223 {
15224 gdb_byte *line_ptr, *extended_end;
15225 gdb_byte *line_end;
15226 unsigned int bytes_read, extended_len;
15227 unsigned char op_code, extended_op, adj_opcode;
15228 CORE_ADDR baseaddr;
15229 struct objfile *objfile = cu->objfile;
15230 bfd *abfd = objfile->obfd;
15231 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15232 const int decode_for_pst_p = (pst != NULL);
15233 struct subfile *last_subfile = NULL;
15234 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15235 = record_line;
15236
15237 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15238
15239 line_ptr = lh->statement_program_start;
15240 line_end = lh->statement_program_end;
15241
15242 /* Read the statement sequences until there's nothing left. */
15243 while (line_ptr < line_end)
15244 {
15245 /* state machine registers */
15246 CORE_ADDR address = 0;
15247 unsigned int file = 1;
15248 unsigned int line = 1;
15249 unsigned int column = 0;
15250 int is_stmt = lh->default_is_stmt;
15251 int basic_block = 0;
15252 int end_sequence = 0;
15253 CORE_ADDR addr;
15254 unsigned char op_index = 0;
15255
15256 if (!decode_for_pst_p && lh->num_file_names >= file)
15257 {
15258 /* Start a subfile for the current file of the state machine. */
15259 /* lh->include_dirs and lh->file_names are 0-based, but the
15260 directory and file name numbers in the statement program
15261 are 1-based. */
15262 struct file_entry *fe = &lh->file_names[file - 1];
15263 char *dir = NULL;
15264
15265 if (fe->dir_index)
15266 dir = lh->include_dirs[fe->dir_index - 1];
15267
15268 dwarf2_start_subfile (fe->name, dir, comp_dir);
15269 }
15270
15271 /* Decode the table. */
15272 while (!end_sequence)
15273 {
15274 op_code = read_1_byte (abfd, line_ptr);
15275 line_ptr += 1;
15276 if (line_ptr > line_end)
15277 {
15278 dwarf2_debug_line_missing_end_sequence_complaint ();
15279 break;
15280 }
15281
15282 if (op_code >= lh->opcode_base)
15283 {
15284 /* Special operand. */
15285 adj_opcode = op_code - lh->opcode_base;
15286 address += (((op_index + (adj_opcode / lh->line_range))
15287 / lh->maximum_ops_per_instruction)
15288 * lh->minimum_instruction_length);
15289 op_index = ((op_index + (adj_opcode / lh->line_range))
15290 % lh->maximum_ops_per_instruction);
15291 line += lh->line_base + (adj_opcode % lh->line_range);
15292 if (lh->num_file_names < file || file == 0)
15293 dwarf2_debug_line_missing_file_complaint ();
15294 /* For now we ignore lines not starting on an
15295 instruction boundary. */
15296 else if (op_index == 0)
15297 {
15298 lh->file_names[file - 1].included_p = 1;
15299 if (!decode_for_pst_p && is_stmt)
15300 {
15301 if (last_subfile != current_subfile)
15302 {
15303 addr = gdbarch_addr_bits_remove (gdbarch, address);
15304 if (last_subfile)
15305 (*p_record_line) (last_subfile, 0, addr);
15306 last_subfile = current_subfile;
15307 }
15308 /* Append row to matrix using current values. */
15309 addr = gdbarch_addr_bits_remove (gdbarch, address);
15310 (*p_record_line) (current_subfile, line, addr);
15311 }
15312 }
15313 basic_block = 0;
15314 }
15315 else switch (op_code)
15316 {
15317 case DW_LNS_extended_op:
15318 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15319 &bytes_read);
15320 line_ptr += bytes_read;
15321 extended_end = line_ptr + extended_len;
15322 extended_op = read_1_byte (abfd, line_ptr);
15323 line_ptr += 1;
15324 switch (extended_op)
15325 {
15326 case DW_LNE_end_sequence:
15327 p_record_line = record_line;
15328 end_sequence = 1;
15329 break;
15330 case DW_LNE_set_address:
15331 address = read_address (abfd, line_ptr, cu, &bytes_read);
15332
15333 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15334 {
15335 /* This line table is for a function which has been
15336 GCd by the linker. Ignore it. PR gdb/12528 */
15337
15338 long line_offset
15339 = line_ptr - get_debug_line_section (cu)->buffer;
15340
15341 complaint (&symfile_complaints,
15342 _(".debug_line address at offset 0x%lx is 0 "
15343 "[in module %s]"),
15344 line_offset, objfile->name);
15345 p_record_line = noop_record_line;
15346 }
15347
15348 op_index = 0;
15349 line_ptr += bytes_read;
15350 address += baseaddr;
15351 break;
15352 case DW_LNE_define_file:
15353 {
15354 char *cur_file;
15355 unsigned int dir_index, mod_time, length;
15356
15357 cur_file = read_direct_string (abfd, line_ptr,
15358 &bytes_read);
15359 line_ptr += bytes_read;
15360 dir_index =
15361 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15362 line_ptr += bytes_read;
15363 mod_time =
15364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15365 line_ptr += bytes_read;
15366 length =
15367 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15368 line_ptr += bytes_read;
15369 add_file_name (lh, cur_file, dir_index, mod_time, length);
15370 }
15371 break;
15372 case DW_LNE_set_discriminator:
15373 /* The discriminator is not interesting to the debugger;
15374 just ignore it. */
15375 line_ptr = extended_end;
15376 break;
15377 default:
15378 complaint (&symfile_complaints,
15379 _("mangled .debug_line section"));
15380 return;
15381 }
15382 /* Make sure that we parsed the extended op correctly. If e.g.
15383 we expected a different address size than the producer used,
15384 we may have read the wrong number of bytes. */
15385 if (line_ptr != extended_end)
15386 {
15387 complaint (&symfile_complaints,
15388 _("mangled .debug_line section"));
15389 return;
15390 }
15391 break;
15392 case DW_LNS_copy:
15393 if (lh->num_file_names < file || file == 0)
15394 dwarf2_debug_line_missing_file_complaint ();
15395 else
15396 {
15397 lh->file_names[file - 1].included_p = 1;
15398 if (!decode_for_pst_p && is_stmt)
15399 {
15400 if (last_subfile != current_subfile)
15401 {
15402 addr = gdbarch_addr_bits_remove (gdbarch, address);
15403 if (last_subfile)
15404 (*p_record_line) (last_subfile, 0, addr);
15405 last_subfile = current_subfile;
15406 }
15407 addr = gdbarch_addr_bits_remove (gdbarch, address);
15408 (*p_record_line) (current_subfile, line, addr);
15409 }
15410 }
15411 basic_block = 0;
15412 break;
15413 case DW_LNS_advance_pc:
15414 {
15415 CORE_ADDR adjust
15416 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15417
15418 address += (((op_index + adjust)
15419 / lh->maximum_ops_per_instruction)
15420 * lh->minimum_instruction_length);
15421 op_index = ((op_index + adjust)
15422 % lh->maximum_ops_per_instruction);
15423 line_ptr += bytes_read;
15424 }
15425 break;
15426 case DW_LNS_advance_line:
15427 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15428 line_ptr += bytes_read;
15429 break;
15430 case DW_LNS_set_file:
15431 {
15432 /* The arrays lh->include_dirs and lh->file_names are
15433 0-based, but the directory and file name numbers in
15434 the statement program are 1-based. */
15435 struct file_entry *fe;
15436 char *dir = NULL;
15437
15438 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15439 line_ptr += bytes_read;
15440 if (lh->num_file_names < file || file == 0)
15441 dwarf2_debug_line_missing_file_complaint ();
15442 else
15443 {
15444 fe = &lh->file_names[file - 1];
15445 if (fe->dir_index)
15446 dir = lh->include_dirs[fe->dir_index - 1];
15447 if (!decode_for_pst_p)
15448 {
15449 last_subfile = current_subfile;
15450 dwarf2_start_subfile (fe->name, dir, comp_dir);
15451 }
15452 }
15453 }
15454 break;
15455 case DW_LNS_set_column:
15456 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15457 line_ptr += bytes_read;
15458 break;
15459 case DW_LNS_negate_stmt:
15460 is_stmt = (!is_stmt);
15461 break;
15462 case DW_LNS_set_basic_block:
15463 basic_block = 1;
15464 break;
15465 /* Add to the address register of the state machine the
15466 address increment value corresponding to special opcode
15467 255. I.e., this value is scaled by the minimum
15468 instruction length since special opcode 255 would have
15469 scaled the increment. */
15470 case DW_LNS_const_add_pc:
15471 {
15472 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15473
15474 address += (((op_index + adjust)
15475 / lh->maximum_ops_per_instruction)
15476 * lh->minimum_instruction_length);
15477 op_index = ((op_index + adjust)
15478 % lh->maximum_ops_per_instruction);
15479 }
15480 break;
15481 case DW_LNS_fixed_advance_pc:
15482 address += read_2_bytes (abfd, line_ptr);
15483 op_index = 0;
15484 line_ptr += 2;
15485 break;
15486 default:
15487 {
15488 /* Unknown standard opcode, ignore it. */
15489 int i;
15490
15491 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15492 {
15493 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15494 line_ptr += bytes_read;
15495 }
15496 }
15497 }
15498 }
15499 if (lh->num_file_names < file || file == 0)
15500 dwarf2_debug_line_missing_file_complaint ();
15501 else
15502 {
15503 lh->file_names[file - 1].included_p = 1;
15504 if (!decode_for_pst_p)
15505 {
15506 addr = gdbarch_addr_bits_remove (gdbarch, address);
15507 (*p_record_line) (current_subfile, 0, addr);
15508 }
15509 }
15510 }
15511 }
15512
15513 /* Decode the Line Number Program (LNP) for the given line_header
15514 structure and CU. The actual information extracted and the type
15515 of structures created from the LNP depends on the value of PST.
15516
15517 1. If PST is NULL, then this procedure uses the data from the program
15518 to create all necessary symbol tables, and their linetables.
15519
15520 2. If PST is not NULL, this procedure reads the program to determine
15521 the list of files included by the unit represented by PST, and
15522 builds all the associated partial symbol tables.
15523
15524 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15525 It is used for relative paths in the line table.
15526 NOTE: When processing partial symtabs (pst != NULL),
15527 comp_dir == pst->dirname.
15528
15529 NOTE: It is important that psymtabs have the same file name (via strcmp)
15530 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15531 symtab we don't use it in the name of the psymtabs we create.
15532 E.g. expand_line_sal requires this when finding psymtabs to expand.
15533 A good testcase for this is mb-inline.exp. */
15534
15535 static void
15536 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15537 struct dwarf2_cu *cu, struct partial_symtab *pst,
15538 int want_line_info)
15539 {
15540 struct objfile *objfile = cu->objfile;
15541 const int decode_for_pst_p = (pst != NULL);
15542 struct subfile *first_subfile = current_subfile;
15543
15544 if (want_line_info)
15545 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15546
15547 if (decode_for_pst_p)
15548 {
15549 int file_index;
15550
15551 /* Now that we're done scanning the Line Header Program, we can
15552 create the psymtab of each included file. */
15553 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15554 if (lh->file_names[file_index].included_p == 1)
15555 {
15556 char *include_name =
15557 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15558 if (include_name != NULL)
15559 dwarf2_create_include_psymtab (include_name, pst, objfile);
15560 }
15561 }
15562 else
15563 {
15564 /* Make sure a symtab is created for every file, even files
15565 which contain only variables (i.e. no code with associated
15566 line numbers). */
15567 int i;
15568
15569 for (i = 0; i < lh->num_file_names; i++)
15570 {
15571 char *dir = NULL;
15572 struct file_entry *fe;
15573
15574 fe = &lh->file_names[i];
15575 if (fe->dir_index)
15576 dir = lh->include_dirs[fe->dir_index - 1];
15577 dwarf2_start_subfile (fe->name, dir, comp_dir);
15578
15579 /* Skip the main file; we don't need it, and it must be
15580 allocated last, so that it will show up before the
15581 non-primary symtabs in the objfile's symtab list. */
15582 if (current_subfile == first_subfile)
15583 continue;
15584
15585 if (current_subfile->symtab == NULL)
15586 current_subfile->symtab = allocate_symtab (current_subfile->name,
15587 objfile);
15588 fe->symtab = current_subfile->symtab;
15589 }
15590 }
15591 }
15592
15593 /* Start a subfile for DWARF. FILENAME is the name of the file and
15594 DIRNAME the name of the source directory which contains FILENAME
15595 or NULL if not known. COMP_DIR is the compilation directory for the
15596 linetable's compilation unit or NULL if not known.
15597 This routine tries to keep line numbers from identical absolute and
15598 relative file names in a common subfile.
15599
15600 Using the `list' example from the GDB testsuite, which resides in
15601 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15602 of /srcdir/list0.c yields the following debugging information for list0.c:
15603
15604 DW_AT_name: /srcdir/list0.c
15605 DW_AT_comp_dir: /compdir
15606 files.files[0].name: list0.h
15607 files.files[0].dir: /srcdir
15608 files.files[1].name: list0.c
15609 files.files[1].dir: /srcdir
15610
15611 The line number information for list0.c has to end up in a single
15612 subfile, so that `break /srcdir/list0.c:1' works as expected.
15613 start_subfile will ensure that this happens provided that we pass the
15614 concatenation of files.files[1].dir and files.files[1].name as the
15615 subfile's name. */
15616
15617 static void
15618 dwarf2_start_subfile (char *filename, const char *dirname,
15619 const char *comp_dir)
15620 {
15621 char *fullname;
15622
15623 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15624 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15625 second argument to start_subfile. To be consistent, we do the
15626 same here. In order not to lose the line information directory,
15627 we concatenate it to the filename when it makes sense.
15628 Note that the Dwarf3 standard says (speaking of filenames in line
15629 information): ``The directory index is ignored for file names
15630 that represent full path names''. Thus ignoring dirname in the
15631 `else' branch below isn't an issue. */
15632
15633 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15634 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15635 else
15636 fullname = filename;
15637
15638 start_subfile (fullname, comp_dir);
15639
15640 if (fullname != filename)
15641 xfree (fullname);
15642 }
15643
15644 /* Start a symtab for DWARF.
15645 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15646
15647 static void
15648 dwarf2_start_symtab (struct dwarf2_cu *cu,
15649 char *name, char *comp_dir, CORE_ADDR low_pc)
15650 {
15651 start_symtab (name, comp_dir, low_pc);
15652 record_debugformat ("DWARF 2");
15653 record_producer (cu->producer);
15654
15655 /* We assume that we're processing GCC output. */
15656 processing_gcc_compilation = 2;
15657
15658 processing_has_namespace_info = 0;
15659 }
15660
15661 static void
15662 var_decode_location (struct attribute *attr, struct symbol *sym,
15663 struct dwarf2_cu *cu)
15664 {
15665 struct objfile *objfile = cu->objfile;
15666 struct comp_unit_head *cu_header = &cu->header;
15667
15668 /* NOTE drow/2003-01-30: There used to be a comment and some special
15669 code here to turn a symbol with DW_AT_external and a
15670 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15671 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15672 with some versions of binutils) where shared libraries could have
15673 relocations against symbols in their debug information - the
15674 minimal symbol would have the right address, but the debug info
15675 would not. It's no longer necessary, because we will explicitly
15676 apply relocations when we read in the debug information now. */
15677
15678 /* A DW_AT_location attribute with no contents indicates that a
15679 variable has been optimized away. */
15680 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15681 {
15682 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15683 return;
15684 }
15685
15686 /* Handle one degenerate form of location expression specially, to
15687 preserve GDB's previous behavior when section offsets are
15688 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15689 then mark this symbol as LOC_STATIC. */
15690
15691 if (attr_form_is_block (attr)
15692 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15693 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15694 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15695 && (DW_BLOCK (attr)->size
15696 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15697 {
15698 unsigned int dummy;
15699
15700 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15701 SYMBOL_VALUE_ADDRESS (sym) =
15702 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15703 else
15704 SYMBOL_VALUE_ADDRESS (sym) =
15705 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15706 SYMBOL_CLASS (sym) = LOC_STATIC;
15707 fixup_symbol_section (sym, objfile);
15708 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15709 SYMBOL_SECTION (sym));
15710 return;
15711 }
15712
15713 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15714 expression evaluator, and use LOC_COMPUTED only when necessary
15715 (i.e. when the value of a register or memory location is
15716 referenced, or a thread-local block, etc.). Then again, it might
15717 not be worthwhile. I'm assuming that it isn't unless performance
15718 or memory numbers show me otherwise. */
15719
15720 dwarf2_symbol_mark_computed (attr, sym, cu);
15721 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15722
15723 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15724 cu->has_loclist = 1;
15725 }
15726
15727 /* Given a pointer to a DWARF information entry, figure out if we need
15728 to make a symbol table entry for it, and if so, create a new entry
15729 and return a pointer to it.
15730 If TYPE is NULL, determine symbol type from the die, otherwise
15731 used the passed type.
15732 If SPACE is not NULL, use it to hold the new symbol. If it is
15733 NULL, allocate a new symbol on the objfile's obstack. */
15734
15735 static struct symbol *
15736 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15737 struct symbol *space)
15738 {
15739 struct objfile *objfile = cu->objfile;
15740 struct symbol *sym = NULL;
15741 char *name;
15742 struct attribute *attr = NULL;
15743 struct attribute *attr2 = NULL;
15744 CORE_ADDR baseaddr;
15745 struct pending **list_to_add = NULL;
15746
15747 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15748
15749 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15750
15751 name = dwarf2_name (die, cu);
15752 if (name)
15753 {
15754 const char *linkagename;
15755 int suppress_add = 0;
15756
15757 if (space)
15758 sym = space;
15759 else
15760 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15761 OBJSTAT (objfile, n_syms++);
15762
15763 /* Cache this symbol's name and the name's demangled form (if any). */
15764 SYMBOL_SET_LANGUAGE (sym, cu->language);
15765 linkagename = dwarf2_physname (name, die, cu);
15766 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15767
15768 /* Fortran does not have mangling standard and the mangling does differ
15769 between gfortran, iFort etc. */
15770 if (cu->language == language_fortran
15771 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15772 symbol_set_demangled_name (&(sym->ginfo),
15773 (char *) dwarf2_full_name (name, die, cu),
15774 NULL);
15775
15776 /* Default assumptions.
15777 Use the passed type or decode it from the die. */
15778 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15779 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15780 if (type != NULL)
15781 SYMBOL_TYPE (sym) = type;
15782 else
15783 SYMBOL_TYPE (sym) = die_type (die, cu);
15784 attr = dwarf2_attr (die,
15785 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15786 cu);
15787 if (attr)
15788 {
15789 SYMBOL_LINE (sym) = DW_UNSND (attr);
15790 }
15791
15792 attr = dwarf2_attr (die,
15793 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15794 cu);
15795 if (attr)
15796 {
15797 int file_index = DW_UNSND (attr);
15798
15799 if (cu->line_header == NULL
15800 || file_index > cu->line_header->num_file_names)
15801 complaint (&symfile_complaints,
15802 _("file index out of range"));
15803 else if (file_index > 0)
15804 {
15805 struct file_entry *fe;
15806
15807 fe = &cu->line_header->file_names[file_index - 1];
15808 SYMBOL_SYMTAB (sym) = fe->symtab;
15809 }
15810 }
15811
15812 switch (die->tag)
15813 {
15814 case DW_TAG_label:
15815 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15816 if (attr)
15817 {
15818 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15819 }
15820 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15821 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15822 SYMBOL_CLASS (sym) = LOC_LABEL;
15823 add_symbol_to_list (sym, cu->list_in_scope);
15824 break;
15825 case DW_TAG_subprogram:
15826 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15827 finish_block. */
15828 SYMBOL_CLASS (sym) = LOC_BLOCK;
15829 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15830 if ((attr2 && (DW_UNSND (attr2) != 0))
15831 || cu->language == language_ada)
15832 {
15833 /* Subprograms marked external are stored as a global symbol.
15834 Ada subprograms, whether marked external or not, are always
15835 stored as a global symbol, because we want to be able to
15836 access them globally. For instance, we want to be able
15837 to break on a nested subprogram without having to
15838 specify the context. */
15839 list_to_add = &global_symbols;
15840 }
15841 else
15842 {
15843 list_to_add = cu->list_in_scope;
15844 }
15845 break;
15846 case DW_TAG_inlined_subroutine:
15847 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15848 finish_block. */
15849 SYMBOL_CLASS (sym) = LOC_BLOCK;
15850 SYMBOL_INLINED (sym) = 1;
15851 list_to_add = cu->list_in_scope;
15852 break;
15853 case DW_TAG_template_value_param:
15854 suppress_add = 1;
15855 /* Fall through. */
15856 case DW_TAG_constant:
15857 case DW_TAG_variable:
15858 case DW_TAG_member:
15859 /* Compilation with minimal debug info may result in
15860 variables with missing type entries. Change the
15861 misleading `void' type to something sensible. */
15862 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15863 SYMBOL_TYPE (sym)
15864 = objfile_type (objfile)->nodebug_data_symbol;
15865
15866 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15867 /* In the case of DW_TAG_member, we should only be called for
15868 static const members. */
15869 if (die->tag == DW_TAG_member)
15870 {
15871 /* dwarf2_add_field uses die_is_declaration,
15872 so we do the same. */
15873 gdb_assert (die_is_declaration (die, cu));
15874 gdb_assert (attr);
15875 }
15876 if (attr)
15877 {
15878 dwarf2_const_value (attr, sym, cu);
15879 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15880 if (!suppress_add)
15881 {
15882 if (attr2 && (DW_UNSND (attr2) != 0))
15883 list_to_add = &global_symbols;
15884 else
15885 list_to_add = cu->list_in_scope;
15886 }
15887 break;
15888 }
15889 attr = dwarf2_attr (die, DW_AT_location, cu);
15890 if (attr)
15891 {
15892 var_decode_location (attr, sym, cu);
15893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15894
15895 /* Fortran explicitly imports any global symbols to the local
15896 scope by DW_TAG_common_block. */
15897 if (cu->language == language_fortran && die->parent
15898 && die->parent->tag == DW_TAG_common_block)
15899 attr2 = NULL;
15900
15901 if (SYMBOL_CLASS (sym) == LOC_STATIC
15902 && SYMBOL_VALUE_ADDRESS (sym) == 0
15903 && !dwarf2_per_objfile->has_section_at_zero)
15904 {
15905 /* When a static variable is eliminated by the linker,
15906 the corresponding debug information is not stripped
15907 out, but the variable address is set to null;
15908 do not add such variables into symbol table. */
15909 }
15910 else if (attr2 && (DW_UNSND (attr2) != 0))
15911 {
15912 /* Workaround gfortran PR debug/40040 - it uses
15913 DW_AT_location for variables in -fPIC libraries which may
15914 get overriden by other libraries/executable and get
15915 a different address. Resolve it by the minimal symbol
15916 which may come from inferior's executable using copy
15917 relocation. Make this workaround only for gfortran as for
15918 other compilers GDB cannot guess the minimal symbol
15919 Fortran mangling kind. */
15920 if (cu->language == language_fortran && die->parent
15921 && die->parent->tag == DW_TAG_module
15922 && cu->producer
15923 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15924 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15925
15926 /* A variable with DW_AT_external is never static,
15927 but it may be block-scoped. */
15928 list_to_add = (cu->list_in_scope == &file_symbols
15929 ? &global_symbols : cu->list_in_scope);
15930 }
15931 else
15932 list_to_add = cu->list_in_scope;
15933 }
15934 else
15935 {
15936 /* We do not know the address of this symbol.
15937 If it is an external symbol and we have type information
15938 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15939 The address of the variable will then be determined from
15940 the minimal symbol table whenever the variable is
15941 referenced. */
15942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15943
15944 /* Fortran explicitly imports any global symbols to the local
15945 scope by DW_TAG_common_block. */
15946 if (cu->language == language_fortran && die->parent
15947 && die->parent->tag == DW_TAG_common_block)
15948 {
15949 /* SYMBOL_CLASS doesn't matter here because
15950 read_common_block is going to reset it. */
15951 if (!suppress_add)
15952 list_to_add = cu->list_in_scope;
15953 }
15954 else if (attr2 && (DW_UNSND (attr2) != 0)
15955 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15956 {
15957 /* A variable with DW_AT_external is never static, but it
15958 may be block-scoped. */
15959 list_to_add = (cu->list_in_scope == &file_symbols
15960 ? &global_symbols : cu->list_in_scope);
15961
15962 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15963 }
15964 else if (!die_is_declaration (die, cu))
15965 {
15966 /* Use the default LOC_OPTIMIZED_OUT class. */
15967 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15968 if (!suppress_add)
15969 list_to_add = cu->list_in_scope;
15970 }
15971 }
15972 break;
15973 case DW_TAG_formal_parameter:
15974 /* If we are inside a function, mark this as an argument. If
15975 not, we might be looking at an argument to an inlined function
15976 when we do not have enough information to show inlined frames;
15977 pretend it's a local variable in that case so that the user can
15978 still see it. */
15979 if (context_stack_depth > 0
15980 && context_stack[context_stack_depth - 1].name != NULL)
15981 SYMBOL_IS_ARGUMENT (sym) = 1;
15982 attr = dwarf2_attr (die, DW_AT_location, cu);
15983 if (attr)
15984 {
15985 var_decode_location (attr, sym, cu);
15986 }
15987 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15988 if (attr)
15989 {
15990 dwarf2_const_value (attr, sym, cu);
15991 }
15992
15993 list_to_add = cu->list_in_scope;
15994 break;
15995 case DW_TAG_unspecified_parameters:
15996 /* From varargs functions; gdb doesn't seem to have any
15997 interest in this information, so just ignore it for now.
15998 (FIXME?) */
15999 break;
16000 case DW_TAG_template_type_param:
16001 suppress_add = 1;
16002 /* Fall through. */
16003 case DW_TAG_class_type:
16004 case DW_TAG_interface_type:
16005 case DW_TAG_structure_type:
16006 case DW_TAG_union_type:
16007 case DW_TAG_set_type:
16008 case DW_TAG_enumeration_type:
16009 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16010 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16011
16012 {
16013 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16014 really ever be static objects: otherwise, if you try
16015 to, say, break of a class's method and you're in a file
16016 which doesn't mention that class, it won't work unless
16017 the check for all static symbols in lookup_symbol_aux
16018 saves you. See the OtherFileClass tests in
16019 gdb.c++/namespace.exp. */
16020
16021 if (!suppress_add)
16022 {
16023 list_to_add = (cu->list_in_scope == &file_symbols
16024 && (cu->language == language_cplus
16025 || cu->language == language_java)
16026 ? &global_symbols : cu->list_in_scope);
16027
16028 /* The semantics of C++ state that "struct foo {
16029 ... }" also defines a typedef for "foo". A Java
16030 class declaration also defines a typedef for the
16031 class. */
16032 if (cu->language == language_cplus
16033 || cu->language == language_java
16034 || cu->language == language_ada)
16035 {
16036 /* The symbol's name is already allocated along
16037 with this objfile, so we don't need to
16038 duplicate it for the type. */
16039 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16040 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16041 }
16042 }
16043 }
16044 break;
16045 case DW_TAG_typedef:
16046 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16047 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16048 list_to_add = cu->list_in_scope;
16049 break;
16050 case DW_TAG_base_type:
16051 case DW_TAG_subrange_type:
16052 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16053 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16054 list_to_add = cu->list_in_scope;
16055 break;
16056 case DW_TAG_enumerator:
16057 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16058 if (attr)
16059 {
16060 dwarf2_const_value (attr, sym, cu);
16061 }
16062 {
16063 /* NOTE: carlton/2003-11-10: See comment above in the
16064 DW_TAG_class_type, etc. block. */
16065
16066 list_to_add = (cu->list_in_scope == &file_symbols
16067 && (cu->language == language_cplus
16068 || cu->language == language_java)
16069 ? &global_symbols : cu->list_in_scope);
16070 }
16071 break;
16072 case DW_TAG_namespace:
16073 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16074 list_to_add = &global_symbols;
16075 break;
16076 case DW_TAG_common_block:
16077 SYMBOL_CLASS (sym) = LOC_STATIC;
16078 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16079 add_symbol_to_list (sym, cu->list_in_scope);
16080 break;
16081 default:
16082 /* Not a tag we recognize. Hopefully we aren't processing
16083 trash data, but since we must specifically ignore things
16084 we don't recognize, there is nothing else we should do at
16085 this point. */
16086 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16087 dwarf_tag_name (die->tag));
16088 break;
16089 }
16090
16091 if (suppress_add)
16092 {
16093 sym->hash_next = objfile->template_symbols;
16094 objfile->template_symbols = sym;
16095 list_to_add = NULL;
16096 }
16097
16098 if (list_to_add != NULL)
16099 add_symbol_to_list (sym, list_to_add);
16100
16101 /* For the benefit of old versions of GCC, check for anonymous
16102 namespaces based on the demangled name. */
16103 if (!processing_has_namespace_info
16104 && cu->language == language_cplus)
16105 cp_scan_for_anonymous_namespaces (sym, objfile);
16106 }
16107 return (sym);
16108 }
16109
16110 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16111
16112 static struct symbol *
16113 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16114 {
16115 return new_symbol_full (die, type, cu, NULL);
16116 }
16117
16118 /* Given an attr with a DW_FORM_dataN value in host byte order,
16119 zero-extend it as appropriate for the symbol's type. The DWARF
16120 standard (v4) is not entirely clear about the meaning of using
16121 DW_FORM_dataN for a constant with a signed type, where the type is
16122 wider than the data. The conclusion of a discussion on the DWARF
16123 list was that this is unspecified. We choose to always zero-extend
16124 because that is the interpretation long in use by GCC. */
16125
16126 static gdb_byte *
16127 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16128 const char *name, struct obstack *obstack,
16129 struct dwarf2_cu *cu, LONGEST *value, int bits)
16130 {
16131 struct objfile *objfile = cu->objfile;
16132 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16133 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16134 LONGEST l = DW_UNSND (attr);
16135
16136 if (bits < sizeof (*value) * 8)
16137 {
16138 l &= ((LONGEST) 1 << bits) - 1;
16139 *value = l;
16140 }
16141 else if (bits == sizeof (*value) * 8)
16142 *value = l;
16143 else
16144 {
16145 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16146 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16147 return bytes;
16148 }
16149
16150 return NULL;
16151 }
16152
16153 /* Read a constant value from an attribute. Either set *VALUE, or if
16154 the value does not fit in *VALUE, set *BYTES - either already
16155 allocated on the objfile obstack, or newly allocated on OBSTACK,
16156 or, set *BATON, if we translated the constant to a location
16157 expression. */
16158
16159 static void
16160 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16161 const char *name, struct obstack *obstack,
16162 struct dwarf2_cu *cu,
16163 LONGEST *value, gdb_byte **bytes,
16164 struct dwarf2_locexpr_baton **baton)
16165 {
16166 struct objfile *objfile = cu->objfile;
16167 struct comp_unit_head *cu_header = &cu->header;
16168 struct dwarf_block *blk;
16169 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16170 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16171
16172 *value = 0;
16173 *bytes = NULL;
16174 *baton = NULL;
16175
16176 switch (attr->form)
16177 {
16178 case DW_FORM_addr:
16179 case DW_FORM_GNU_addr_index:
16180 {
16181 gdb_byte *data;
16182
16183 if (TYPE_LENGTH (type) != cu_header->addr_size)
16184 dwarf2_const_value_length_mismatch_complaint (name,
16185 cu_header->addr_size,
16186 TYPE_LENGTH (type));
16187 /* Symbols of this form are reasonably rare, so we just
16188 piggyback on the existing location code rather than writing
16189 a new implementation of symbol_computed_ops. */
16190 *baton = obstack_alloc (&objfile->objfile_obstack,
16191 sizeof (struct dwarf2_locexpr_baton));
16192 (*baton)->per_cu = cu->per_cu;
16193 gdb_assert ((*baton)->per_cu);
16194
16195 (*baton)->size = 2 + cu_header->addr_size;
16196 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16197 (*baton)->data = data;
16198
16199 data[0] = DW_OP_addr;
16200 store_unsigned_integer (&data[1], cu_header->addr_size,
16201 byte_order, DW_ADDR (attr));
16202 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16203 }
16204 break;
16205 case DW_FORM_string:
16206 case DW_FORM_strp:
16207 case DW_FORM_GNU_str_index:
16208 case DW_FORM_GNU_strp_alt:
16209 /* DW_STRING is already allocated on the objfile obstack, point
16210 directly to it. */
16211 *bytes = (gdb_byte *) DW_STRING (attr);
16212 break;
16213 case DW_FORM_block1:
16214 case DW_FORM_block2:
16215 case DW_FORM_block4:
16216 case DW_FORM_block:
16217 case DW_FORM_exprloc:
16218 blk = DW_BLOCK (attr);
16219 if (TYPE_LENGTH (type) != blk->size)
16220 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16221 TYPE_LENGTH (type));
16222 *bytes = blk->data;
16223 break;
16224
16225 /* The DW_AT_const_value attributes are supposed to carry the
16226 symbol's value "represented as it would be on the target
16227 architecture." By the time we get here, it's already been
16228 converted to host endianness, so we just need to sign- or
16229 zero-extend it as appropriate. */
16230 case DW_FORM_data1:
16231 *bytes = dwarf2_const_value_data (attr, type, name,
16232 obstack, cu, value, 8);
16233 break;
16234 case DW_FORM_data2:
16235 *bytes = dwarf2_const_value_data (attr, type, name,
16236 obstack, cu, value, 16);
16237 break;
16238 case DW_FORM_data4:
16239 *bytes = dwarf2_const_value_data (attr, type, name,
16240 obstack, cu, value, 32);
16241 break;
16242 case DW_FORM_data8:
16243 *bytes = dwarf2_const_value_data (attr, type, name,
16244 obstack, cu, value, 64);
16245 break;
16246
16247 case DW_FORM_sdata:
16248 *value = DW_SND (attr);
16249 break;
16250
16251 case DW_FORM_udata:
16252 *value = DW_UNSND (attr);
16253 break;
16254
16255 default:
16256 complaint (&symfile_complaints,
16257 _("unsupported const value attribute form: '%s'"),
16258 dwarf_form_name (attr->form));
16259 *value = 0;
16260 break;
16261 }
16262 }
16263
16264
16265 /* Copy constant value from an attribute to a symbol. */
16266
16267 static void
16268 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16269 struct dwarf2_cu *cu)
16270 {
16271 struct objfile *objfile = cu->objfile;
16272 struct comp_unit_head *cu_header = &cu->header;
16273 LONGEST value;
16274 gdb_byte *bytes;
16275 struct dwarf2_locexpr_baton *baton;
16276
16277 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16278 SYMBOL_PRINT_NAME (sym),
16279 &objfile->objfile_obstack, cu,
16280 &value, &bytes, &baton);
16281
16282 if (baton != NULL)
16283 {
16284 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16285 SYMBOL_LOCATION_BATON (sym) = baton;
16286 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16287 }
16288 else if (bytes != NULL)
16289 {
16290 SYMBOL_VALUE_BYTES (sym) = bytes;
16291 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16292 }
16293 else
16294 {
16295 SYMBOL_VALUE (sym) = value;
16296 SYMBOL_CLASS (sym) = LOC_CONST;
16297 }
16298 }
16299
16300 /* Return the type of the die in question using its DW_AT_type attribute. */
16301
16302 static struct type *
16303 die_type (struct die_info *die, struct dwarf2_cu *cu)
16304 {
16305 struct attribute *type_attr;
16306
16307 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16308 if (!type_attr)
16309 {
16310 /* A missing DW_AT_type represents a void type. */
16311 return objfile_type (cu->objfile)->builtin_void;
16312 }
16313
16314 return lookup_die_type (die, type_attr, cu);
16315 }
16316
16317 /* True iff CU's producer generates GNAT Ada auxiliary information
16318 that allows to find parallel types through that information instead
16319 of having to do expensive parallel lookups by type name. */
16320
16321 static int
16322 need_gnat_info (struct dwarf2_cu *cu)
16323 {
16324 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16325 of GNAT produces this auxiliary information, without any indication
16326 that it is produced. Part of enhancing the FSF version of GNAT
16327 to produce that information will be to put in place an indicator
16328 that we can use in order to determine whether the descriptive type
16329 info is available or not. One suggestion that has been made is
16330 to use a new attribute, attached to the CU die. For now, assume
16331 that the descriptive type info is not available. */
16332 return 0;
16333 }
16334
16335 /* Return the auxiliary type of the die in question using its
16336 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16337 attribute is not present. */
16338
16339 static struct type *
16340 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16341 {
16342 struct attribute *type_attr;
16343
16344 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16345 if (!type_attr)
16346 return NULL;
16347
16348 return lookup_die_type (die, type_attr, cu);
16349 }
16350
16351 /* If DIE has a descriptive_type attribute, then set the TYPE's
16352 descriptive type accordingly. */
16353
16354 static void
16355 set_descriptive_type (struct type *type, struct die_info *die,
16356 struct dwarf2_cu *cu)
16357 {
16358 struct type *descriptive_type = die_descriptive_type (die, cu);
16359
16360 if (descriptive_type)
16361 {
16362 ALLOCATE_GNAT_AUX_TYPE (type);
16363 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16364 }
16365 }
16366
16367 /* Return the containing type of the die in question using its
16368 DW_AT_containing_type attribute. */
16369
16370 static struct type *
16371 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16372 {
16373 struct attribute *type_attr;
16374
16375 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16376 if (!type_attr)
16377 error (_("Dwarf Error: Problem turning containing type into gdb type "
16378 "[in module %s]"), cu->objfile->name);
16379
16380 return lookup_die_type (die, type_attr, cu);
16381 }
16382
16383 /* Look up the type of DIE in CU using its type attribute ATTR.
16384 If there is no type substitute an error marker. */
16385
16386 static struct type *
16387 lookup_die_type (struct die_info *die, struct attribute *attr,
16388 struct dwarf2_cu *cu)
16389 {
16390 struct objfile *objfile = cu->objfile;
16391 struct type *this_type;
16392
16393 /* First see if we have it cached. */
16394
16395 if (attr->form == DW_FORM_GNU_ref_alt)
16396 {
16397 struct dwarf2_per_cu_data *per_cu;
16398 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16399
16400 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16401 this_type = get_die_type_at_offset (offset, per_cu);
16402 }
16403 else if (is_ref_attr (attr))
16404 {
16405 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16406
16407 this_type = get_die_type_at_offset (offset, cu->per_cu);
16408 }
16409 else if (attr->form == DW_FORM_ref_sig8)
16410 {
16411 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16412
16413 /* sig_type will be NULL if the signatured type is missing from
16414 the debug info. */
16415 if (sig_type == NULL)
16416 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16417 "at 0x%x [in module %s]"),
16418 die->offset.sect_off, objfile->name);
16419
16420 gdb_assert (sig_type->per_cu.is_debug_types);
16421 /* If we haven't filled in type_offset_in_section yet, then we
16422 haven't read the type in yet. */
16423 this_type = NULL;
16424 if (sig_type->type_offset_in_section.sect_off != 0)
16425 {
16426 this_type =
16427 get_die_type_at_offset (sig_type->type_offset_in_section,
16428 &sig_type->per_cu);
16429 }
16430 }
16431 else
16432 {
16433 dump_die_for_error (die);
16434 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16435 dwarf_attr_name (attr->name), objfile->name);
16436 }
16437
16438 /* If not cached we need to read it in. */
16439
16440 if (this_type == NULL)
16441 {
16442 struct die_info *type_die;
16443 struct dwarf2_cu *type_cu = cu;
16444
16445 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16446 /* If we found the type now, it's probably because the type came
16447 from an inter-CU reference and the type's CU got expanded before
16448 ours. */
16449 this_type = get_die_type (type_die, type_cu);
16450 if (this_type == NULL)
16451 this_type = read_type_die_1 (type_die, type_cu);
16452 }
16453
16454 /* If we still don't have a type use an error marker. */
16455
16456 if (this_type == NULL)
16457 {
16458 char *message, *saved;
16459
16460 /* read_type_die already issued a complaint. */
16461 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16462 objfile->name,
16463 cu->header.offset.sect_off,
16464 die->offset.sect_off);
16465 saved = obstack_copy0 (&objfile->objfile_obstack,
16466 message, strlen (message));
16467 xfree (message);
16468
16469 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16470 }
16471
16472 return this_type;
16473 }
16474
16475 /* Return the type in DIE, CU.
16476 Returns NULL for invalid types.
16477
16478 This first does a lookup in the appropriate type_hash table,
16479 and only reads the die in if necessary.
16480
16481 NOTE: This can be called when reading in partial or full symbols. */
16482
16483 static struct type *
16484 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16485 {
16486 struct type *this_type;
16487
16488 this_type = get_die_type (die, cu);
16489 if (this_type)
16490 return this_type;
16491
16492 return read_type_die_1 (die, cu);
16493 }
16494
16495 /* Read the type in DIE, CU.
16496 Returns NULL for invalid types. */
16497
16498 static struct type *
16499 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16500 {
16501 struct type *this_type = NULL;
16502
16503 switch (die->tag)
16504 {
16505 case DW_TAG_class_type:
16506 case DW_TAG_interface_type:
16507 case DW_TAG_structure_type:
16508 case DW_TAG_union_type:
16509 this_type = read_structure_type (die, cu);
16510 break;
16511 case DW_TAG_enumeration_type:
16512 this_type = read_enumeration_type (die, cu);
16513 break;
16514 case DW_TAG_subprogram:
16515 case DW_TAG_subroutine_type:
16516 case DW_TAG_inlined_subroutine:
16517 this_type = read_subroutine_type (die, cu);
16518 break;
16519 case DW_TAG_array_type:
16520 this_type = read_array_type (die, cu);
16521 break;
16522 case DW_TAG_set_type:
16523 this_type = read_set_type (die, cu);
16524 break;
16525 case DW_TAG_pointer_type:
16526 this_type = read_tag_pointer_type (die, cu);
16527 break;
16528 case DW_TAG_ptr_to_member_type:
16529 this_type = read_tag_ptr_to_member_type (die, cu);
16530 break;
16531 case DW_TAG_reference_type:
16532 this_type = read_tag_reference_type (die, cu);
16533 break;
16534 case DW_TAG_const_type:
16535 this_type = read_tag_const_type (die, cu);
16536 break;
16537 case DW_TAG_volatile_type:
16538 this_type = read_tag_volatile_type (die, cu);
16539 break;
16540 case DW_TAG_string_type:
16541 this_type = read_tag_string_type (die, cu);
16542 break;
16543 case DW_TAG_typedef:
16544 this_type = read_typedef (die, cu);
16545 break;
16546 case DW_TAG_subrange_type:
16547 this_type = read_subrange_type (die, cu);
16548 break;
16549 case DW_TAG_base_type:
16550 this_type = read_base_type (die, cu);
16551 break;
16552 case DW_TAG_unspecified_type:
16553 this_type = read_unspecified_type (die, cu);
16554 break;
16555 case DW_TAG_namespace:
16556 this_type = read_namespace_type (die, cu);
16557 break;
16558 case DW_TAG_module:
16559 this_type = read_module_type (die, cu);
16560 break;
16561 default:
16562 complaint (&symfile_complaints,
16563 _("unexpected tag in read_type_die: '%s'"),
16564 dwarf_tag_name (die->tag));
16565 break;
16566 }
16567
16568 return this_type;
16569 }
16570
16571 /* See if we can figure out if the class lives in a namespace. We do
16572 this by looking for a member function; its demangled name will
16573 contain namespace info, if there is any.
16574 Return the computed name or NULL.
16575 Space for the result is allocated on the objfile's obstack.
16576 This is the full-die version of guess_partial_die_structure_name.
16577 In this case we know DIE has no useful parent. */
16578
16579 static char *
16580 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16581 {
16582 struct die_info *spec_die;
16583 struct dwarf2_cu *spec_cu;
16584 struct die_info *child;
16585
16586 spec_cu = cu;
16587 spec_die = die_specification (die, &spec_cu);
16588 if (spec_die != NULL)
16589 {
16590 die = spec_die;
16591 cu = spec_cu;
16592 }
16593
16594 for (child = die->child;
16595 child != NULL;
16596 child = child->sibling)
16597 {
16598 if (child->tag == DW_TAG_subprogram)
16599 {
16600 struct attribute *attr;
16601
16602 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16603 if (attr == NULL)
16604 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16605 if (attr != NULL)
16606 {
16607 char *actual_name
16608 = language_class_name_from_physname (cu->language_defn,
16609 DW_STRING (attr));
16610 char *name = NULL;
16611
16612 if (actual_name != NULL)
16613 {
16614 char *die_name = dwarf2_name (die, cu);
16615
16616 if (die_name != NULL
16617 && strcmp (die_name, actual_name) != 0)
16618 {
16619 /* Strip off the class name from the full name.
16620 We want the prefix. */
16621 int die_name_len = strlen (die_name);
16622 int actual_name_len = strlen (actual_name);
16623
16624 /* Test for '::' as a sanity check. */
16625 if (actual_name_len > die_name_len + 2
16626 && actual_name[actual_name_len
16627 - die_name_len - 1] == ':')
16628 name =
16629 obsavestring (actual_name,
16630 actual_name_len - die_name_len - 2,
16631 &cu->objfile->objfile_obstack);
16632 }
16633 }
16634 xfree (actual_name);
16635 return name;
16636 }
16637 }
16638 }
16639
16640 return NULL;
16641 }
16642
16643 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16644 prefix part in such case. See
16645 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16646
16647 static char *
16648 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16649 {
16650 struct attribute *attr;
16651 char *base;
16652
16653 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16654 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16655 return NULL;
16656
16657 attr = dwarf2_attr (die, DW_AT_name, cu);
16658 if (attr != NULL && DW_STRING (attr) != NULL)
16659 return NULL;
16660
16661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16662 if (attr == NULL)
16663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16664 if (attr == NULL || DW_STRING (attr) == NULL)
16665 return NULL;
16666
16667 /* dwarf2_name had to be already called. */
16668 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16669
16670 /* Strip the base name, keep any leading namespaces/classes. */
16671 base = strrchr (DW_STRING (attr), ':');
16672 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16673 return "";
16674
16675 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16676 &cu->objfile->objfile_obstack);
16677 }
16678
16679 /* Return the name of the namespace/class that DIE is defined within,
16680 or "" if we can't tell. The caller should not xfree the result.
16681
16682 For example, if we're within the method foo() in the following
16683 code:
16684
16685 namespace N {
16686 class C {
16687 void foo () {
16688 }
16689 };
16690 }
16691
16692 then determine_prefix on foo's die will return "N::C". */
16693
16694 static const char *
16695 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16696 {
16697 struct die_info *parent, *spec_die;
16698 struct dwarf2_cu *spec_cu;
16699 struct type *parent_type;
16700 char *retval;
16701
16702 if (cu->language != language_cplus && cu->language != language_java
16703 && cu->language != language_fortran)
16704 return "";
16705
16706 retval = anonymous_struct_prefix (die, cu);
16707 if (retval)
16708 return retval;
16709
16710 /* We have to be careful in the presence of DW_AT_specification.
16711 For example, with GCC 3.4, given the code
16712
16713 namespace N {
16714 void foo() {
16715 // Definition of N::foo.
16716 }
16717 }
16718
16719 then we'll have a tree of DIEs like this:
16720
16721 1: DW_TAG_compile_unit
16722 2: DW_TAG_namespace // N
16723 3: DW_TAG_subprogram // declaration of N::foo
16724 4: DW_TAG_subprogram // definition of N::foo
16725 DW_AT_specification // refers to die #3
16726
16727 Thus, when processing die #4, we have to pretend that we're in
16728 the context of its DW_AT_specification, namely the contex of die
16729 #3. */
16730 spec_cu = cu;
16731 spec_die = die_specification (die, &spec_cu);
16732 if (spec_die == NULL)
16733 parent = die->parent;
16734 else
16735 {
16736 parent = spec_die->parent;
16737 cu = spec_cu;
16738 }
16739
16740 if (parent == NULL)
16741 return "";
16742 else if (parent->building_fullname)
16743 {
16744 const char *name;
16745 const char *parent_name;
16746
16747 /* It has been seen on RealView 2.2 built binaries,
16748 DW_TAG_template_type_param types actually _defined_ as
16749 children of the parent class:
16750
16751 enum E {};
16752 template class <class Enum> Class{};
16753 Class<enum E> class_e;
16754
16755 1: DW_TAG_class_type (Class)
16756 2: DW_TAG_enumeration_type (E)
16757 3: DW_TAG_enumerator (enum1:0)
16758 3: DW_TAG_enumerator (enum2:1)
16759 ...
16760 2: DW_TAG_template_type_param
16761 DW_AT_type DW_FORM_ref_udata (E)
16762
16763 Besides being broken debug info, it can put GDB into an
16764 infinite loop. Consider:
16765
16766 When we're building the full name for Class<E>, we'll start
16767 at Class, and go look over its template type parameters,
16768 finding E. We'll then try to build the full name of E, and
16769 reach here. We're now trying to build the full name of E,
16770 and look over the parent DIE for containing scope. In the
16771 broken case, if we followed the parent DIE of E, we'd again
16772 find Class, and once again go look at its template type
16773 arguments, etc., etc. Simply don't consider such parent die
16774 as source-level parent of this die (it can't be, the language
16775 doesn't allow it), and break the loop here. */
16776 name = dwarf2_name (die, cu);
16777 parent_name = dwarf2_name (parent, cu);
16778 complaint (&symfile_complaints,
16779 _("template param type '%s' defined within parent '%s'"),
16780 name ? name : "<unknown>",
16781 parent_name ? parent_name : "<unknown>");
16782 return "";
16783 }
16784 else
16785 switch (parent->tag)
16786 {
16787 case DW_TAG_namespace:
16788 parent_type = read_type_die (parent, cu);
16789 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16790 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16791 Work around this problem here. */
16792 if (cu->language == language_cplus
16793 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16794 return "";
16795 /* We give a name to even anonymous namespaces. */
16796 return TYPE_TAG_NAME (parent_type);
16797 case DW_TAG_class_type:
16798 case DW_TAG_interface_type:
16799 case DW_TAG_structure_type:
16800 case DW_TAG_union_type:
16801 case DW_TAG_module:
16802 parent_type = read_type_die (parent, cu);
16803 if (TYPE_TAG_NAME (parent_type) != NULL)
16804 return TYPE_TAG_NAME (parent_type);
16805 else
16806 /* An anonymous structure is only allowed non-static data
16807 members; no typedefs, no member functions, et cetera.
16808 So it does not need a prefix. */
16809 return "";
16810 case DW_TAG_compile_unit:
16811 case DW_TAG_partial_unit:
16812 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16813 if (cu->language == language_cplus
16814 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16815 && die->child != NULL
16816 && (die->tag == DW_TAG_class_type
16817 || die->tag == DW_TAG_structure_type
16818 || die->tag == DW_TAG_union_type))
16819 {
16820 char *name = guess_full_die_structure_name (die, cu);
16821 if (name != NULL)
16822 return name;
16823 }
16824 return "";
16825 default:
16826 return determine_prefix (parent, cu);
16827 }
16828 }
16829
16830 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16831 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16832 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16833 an obconcat, otherwise allocate storage for the result. The CU argument is
16834 used to determine the language and hence, the appropriate separator. */
16835
16836 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16837
16838 static char *
16839 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16840 int physname, struct dwarf2_cu *cu)
16841 {
16842 const char *lead = "";
16843 const char *sep;
16844
16845 if (suffix == NULL || suffix[0] == '\0'
16846 || prefix == NULL || prefix[0] == '\0')
16847 sep = "";
16848 else if (cu->language == language_java)
16849 sep = ".";
16850 else if (cu->language == language_fortran && physname)
16851 {
16852 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16853 DW_AT_MIPS_linkage_name is preferred and used instead. */
16854
16855 lead = "__";
16856 sep = "_MOD_";
16857 }
16858 else
16859 sep = "::";
16860
16861 if (prefix == NULL)
16862 prefix = "";
16863 if (suffix == NULL)
16864 suffix = "";
16865
16866 if (obs == NULL)
16867 {
16868 char *retval
16869 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16870
16871 strcpy (retval, lead);
16872 strcat (retval, prefix);
16873 strcat (retval, sep);
16874 strcat (retval, suffix);
16875 return retval;
16876 }
16877 else
16878 {
16879 /* We have an obstack. */
16880 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16881 }
16882 }
16883
16884 /* Return sibling of die, NULL if no sibling. */
16885
16886 static struct die_info *
16887 sibling_die (struct die_info *die)
16888 {
16889 return die->sibling;
16890 }
16891
16892 /* Get name of a die, return NULL if not found. */
16893
16894 static char *
16895 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16896 struct obstack *obstack)
16897 {
16898 if (name && cu->language == language_cplus)
16899 {
16900 char *canon_name = cp_canonicalize_string (name);
16901
16902 if (canon_name != NULL)
16903 {
16904 if (strcmp (canon_name, name) != 0)
16905 name = obsavestring (canon_name, strlen (canon_name),
16906 obstack);
16907 xfree (canon_name);
16908 }
16909 }
16910
16911 return name;
16912 }
16913
16914 /* Get name of a die, return NULL if not found. */
16915
16916 static char *
16917 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16918 {
16919 struct attribute *attr;
16920
16921 attr = dwarf2_attr (die, DW_AT_name, cu);
16922 if ((!attr || !DW_STRING (attr))
16923 && die->tag != DW_TAG_class_type
16924 && die->tag != DW_TAG_interface_type
16925 && die->tag != DW_TAG_structure_type
16926 && die->tag != DW_TAG_union_type)
16927 return NULL;
16928
16929 switch (die->tag)
16930 {
16931 case DW_TAG_compile_unit:
16932 case DW_TAG_partial_unit:
16933 /* Compilation units have a DW_AT_name that is a filename, not
16934 a source language identifier. */
16935 case DW_TAG_enumeration_type:
16936 case DW_TAG_enumerator:
16937 /* These tags always have simple identifiers already; no need
16938 to canonicalize them. */
16939 return DW_STRING (attr);
16940
16941 case DW_TAG_subprogram:
16942 /* Java constructors will all be named "<init>", so return
16943 the class name when we see this special case. */
16944 if (cu->language == language_java
16945 && DW_STRING (attr) != NULL
16946 && strcmp (DW_STRING (attr), "<init>") == 0)
16947 {
16948 struct dwarf2_cu *spec_cu = cu;
16949 struct die_info *spec_die;
16950
16951 /* GCJ will output '<init>' for Java constructor names.
16952 For this special case, return the name of the parent class. */
16953
16954 /* GCJ may output suprogram DIEs with AT_specification set.
16955 If so, use the name of the specified DIE. */
16956 spec_die = die_specification (die, &spec_cu);
16957 if (spec_die != NULL)
16958 return dwarf2_name (spec_die, spec_cu);
16959
16960 do
16961 {
16962 die = die->parent;
16963 if (die->tag == DW_TAG_class_type)
16964 return dwarf2_name (die, cu);
16965 }
16966 while (die->tag != DW_TAG_compile_unit
16967 && die->tag != DW_TAG_partial_unit);
16968 }
16969 break;
16970
16971 case DW_TAG_class_type:
16972 case DW_TAG_interface_type:
16973 case DW_TAG_structure_type:
16974 case DW_TAG_union_type:
16975 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16976 structures or unions. These were of the form "._%d" in GCC 4.1,
16977 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16978 and GCC 4.4. We work around this problem by ignoring these. */
16979 if (attr && DW_STRING (attr)
16980 && (strncmp (DW_STRING (attr), "._", 2) == 0
16981 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16982 return NULL;
16983
16984 /* GCC might emit a nameless typedef that has a linkage name. See
16985 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16986 if (!attr || DW_STRING (attr) == NULL)
16987 {
16988 char *demangled = NULL;
16989
16990 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16991 if (attr == NULL)
16992 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16993
16994 if (attr == NULL || DW_STRING (attr) == NULL)
16995 return NULL;
16996
16997 /* Avoid demangling DW_STRING (attr) the second time on a second
16998 call for the same DIE. */
16999 if (!DW_STRING_IS_CANONICAL (attr))
17000 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17001
17002 if (demangled)
17003 {
17004 char *base;
17005
17006 /* FIXME: we already did this for the partial symbol... */
17007 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
17008 &cu->objfile->objfile_obstack);
17009 DW_STRING_IS_CANONICAL (attr) = 1;
17010 xfree (demangled);
17011
17012 /* Strip any leading namespaces/classes, keep only the base name.
17013 DW_AT_name for named DIEs does not contain the prefixes. */
17014 base = strrchr (DW_STRING (attr), ':');
17015 if (base && base > DW_STRING (attr) && base[-1] == ':')
17016 return &base[1];
17017 else
17018 return DW_STRING (attr);
17019 }
17020 }
17021 break;
17022
17023 default:
17024 break;
17025 }
17026
17027 if (!DW_STRING_IS_CANONICAL (attr))
17028 {
17029 DW_STRING (attr)
17030 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17031 &cu->objfile->objfile_obstack);
17032 DW_STRING_IS_CANONICAL (attr) = 1;
17033 }
17034 return DW_STRING (attr);
17035 }
17036
17037 /* Return the die that this die in an extension of, or NULL if there
17038 is none. *EXT_CU is the CU containing DIE on input, and the CU
17039 containing the return value on output. */
17040
17041 static struct die_info *
17042 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17043 {
17044 struct attribute *attr;
17045
17046 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17047 if (attr == NULL)
17048 return NULL;
17049
17050 return follow_die_ref (die, attr, ext_cu);
17051 }
17052
17053 /* Convert a DIE tag into its string name. */
17054
17055 static const char *
17056 dwarf_tag_name (unsigned tag)
17057 {
17058 const char *name = get_DW_TAG_name (tag);
17059
17060 if (name == NULL)
17061 return "DW_TAG_<unknown>";
17062
17063 return name;
17064 }
17065
17066 /* Convert a DWARF attribute code into its string name. */
17067
17068 static const char *
17069 dwarf_attr_name (unsigned attr)
17070 {
17071 const char *name;
17072
17073 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17074 if (attr == DW_AT_MIPS_fde)
17075 return "DW_AT_MIPS_fde";
17076 #else
17077 if (attr == DW_AT_HP_block_index)
17078 return "DW_AT_HP_block_index";
17079 #endif
17080
17081 name = get_DW_AT_name (attr);
17082
17083 if (name == NULL)
17084 return "DW_AT_<unknown>";
17085
17086 return name;
17087 }
17088
17089 /* Convert a DWARF value form code into its string name. */
17090
17091 static const char *
17092 dwarf_form_name (unsigned form)
17093 {
17094 const char *name = get_DW_FORM_name (form);
17095
17096 if (name == NULL)
17097 return "DW_FORM_<unknown>";
17098
17099 return name;
17100 }
17101
17102 static char *
17103 dwarf_bool_name (unsigned mybool)
17104 {
17105 if (mybool)
17106 return "TRUE";
17107 else
17108 return "FALSE";
17109 }
17110
17111 /* Convert a DWARF type code into its string name. */
17112
17113 static const char *
17114 dwarf_type_encoding_name (unsigned enc)
17115 {
17116 const char *name = get_DW_ATE_name (enc);
17117
17118 if (name == NULL)
17119 return "DW_ATE_<unknown>";
17120
17121 return name;
17122 }
17123
17124 static void
17125 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17126 {
17127 unsigned int i;
17128
17129 print_spaces (indent, f);
17130 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17131 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17132
17133 if (die->parent != NULL)
17134 {
17135 print_spaces (indent, f);
17136 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17137 die->parent->offset.sect_off);
17138 }
17139
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, " has children: %s\n",
17142 dwarf_bool_name (die->child != NULL));
17143
17144 print_spaces (indent, f);
17145 fprintf_unfiltered (f, " attributes:\n");
17146
17147 for (i = 0; i < die->num_attrs; ++i)
17148 {
17149 print_spaces (indent, f);
17150 fprintf_unfiltered (f, " %s (%s) ",
17151 dwarf_attr_name (die->attrs[i].name),
17152 dwarf_form_name (die->attrs[i].form));
17153
17154 switch (die->attrs[i].form)
17155 {
17156 case DW_FORM_addr:
17157 case DW_FORM_GNU_addr_index:
17158 fprintf_unfiltered (f, "address: ");
17159 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17160 break;
17161 case DW_FORM_block2:
17162 case DW_FORM_block4:
17163 case DW_FORM_block:
17164 case DW_FORM_block1:
17165 fprintf_unfiltered (f, "block: size %s",
17166 pulongest (DW_BLOCK (&die->attrs[i])->size));
17167 break;
17168 case DW_FORM_exprloc:
17169 fprintf_unfiltered (f, "expression: size %s",
17170 pulongest (DW_BLOCK (&die->attrs[i])->size));
17171 break;
17172 case DW_FORM_ref_addr:
17173 fprintf_unfiltered (f, "ref address: ");
17174 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17175 break;
17176 case DW_FORM_GNU_ref_alt:
17177 fprintf_unfiltered (f, "alt ref address: ");
17178 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17179 break;
17180 case DW_FORM_ref1:
17181 case DW_FORM_ref2:
17182 case DW_FORM_ref4:
17183 case DW_FORM_ref8:
17184 case DW_FORM_ref_udata:
17185 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17186 (long) (DW_UNSND (&die->attrs[i])));
17187 break;
17188 case DW_FORM_data1:
17189 case DW_FORM_data2:
17190 case DW_FORM_data4:
17191 case DW_FORM_data8:
17192 case DW_FORM_udata:
17193 case DW_FORM_sdata:
17194 fprintf_unfiltered (f, "constant: %s",
17195 pulongest (DW_UNSND (&die->attrs[i])));
17196 break;
17197 case DW_FORM_sec_offset:
17198 fprintf_unfiltered (f, "section offset: %s",
17199 pulongest (DW_UNSND (&die->attrs[i])));
17200 break;
17201 case DW_FORM_ref_sig8:
17202 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17203 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17204 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17205 else
17206 fprintf_unfiltered (f, "signatured type, offset: unknown");
17207 break;
17208 case DW_FORM_string:
17209 case DW_FORM_strp:
17210 case DW_FORM_GNU_str_index:
17211 case DW_FORM_GNU_strp_alt:
17212 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17213 DW_STRING (&die->attrs[i])
17214 ? DW_STRING (&die->attrs[i]) : "",
17215 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17216 break;
17217 case DW_FORM_flag:
17218 if (DW_UNSND (&die->attrs[i]))
17219 fprintf_unfiltered (f, "flag: TRUE");
17220 else
17221 fprintf_unfiltered (f, "flag: FALSE");
17222 break;
17223 case DW_FORM_flag_present:
17224 fprintf_unfiltered (f, "flag: TRUE");
17225 break;
17226 case DW_FORM_indirect:
17227 /* The reader will have reduced the indirect form to
17228 the "base form" so this form should not occur. */
17229 fprintf_unfiltered (f,
17230 "unexpected attribute form: DW_FORM_indirect");
17231 break;
17232 default:
17233 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17234 die->attrs[i].form);
17235 break;
17236 }
17237 fprintf_unfiltered (f, "\n");
17238 }
17239 }
17240
17241 static void
17242 dump_die_for_error (struct die_info *die)
17243 {
17244 dump_die_shallow (gdb_stderr, 0, die);
17245 }
17246
17247 static void
17248 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17249 {
17250 int indent = level * 4;
17251
17252 gdb_assert (die != NULL);
17253
17254 if (level >= max_level)
17255 return;
17256
17257 dump_die_shallow (f, indent, die);
17258
17259 if (die->child != NULL)
17260 {
17261 print_spaces (indent, f);
17262 fprintf_unfiltered (f, " Children:");
17263 if (level + 1 < max_level)
17264 {
17265 fprintf_unfiltered (f, "\n");
17266 dump_die_1 (f, level + 1, max_level, die->child);
17267 }
17268 else
17269 {
17270 fprintf_unfiltered (f,
17271 " [not printed, max nesting level reached]\n");
17272 }
17273 }
17274
17275 if (die->sibling != NULL && level > 0)
17276 {
17277 dump_die_1 (f, level, max_level, die->sibling);
17278 }
17279 }
17280
17281 /* This is called from the pdie macro in gdbinit.in.
17282 It's not static so gcc will keep a copy callable from gdb. */
17283
17284 void
17285 dump_die (struct die_info *die, int max_level)
17286 {
17287 dump_die_1 (gdb_stdlog, 0, max_level, die);
17288 }
17289
17290 static void
17291 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17292 {
17293 void **slot;
17294
17295 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17296 INSERT);
17297
17298 *slot = die;
17299 }
17300
17301 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17302 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17303
17304 static int
17305 is_ref_attr (struct attribute *attr)
17306 {
17307 switch (attr->form)
17308 {
17309 case DW_FORM_ref_addr:
17310 case DW_FORM_ref1:
17311 case DW_FORM_ref2:
17312 case DW_FORM_ref4:
17313 case DW_FORM_ref8:
17314 case DW_FORM_ref_udata:
17315 case DW_FORM_GNU_ref_alt:
17316 return 1;
17317 default:
17318 return 0;
17319 }
17320 }
17321
17322 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17323 required kind. */
17324
17325 static sect_offset
17326 dwarf2_get_ref_die_offset (struct attribute *attr)
17327 {
17328 sect_offset retval = { DW_UNSND (attr) };
17329
17330 if (is_ref_attr (attr))
17331 return retval;
17332
17333 retval.sect_off = 0;
17334 complaint (&symfile_complaints,
17335 _("unsupported die ref attribute form: '%s'"),
17336 dwarf_form_name (attr->form));
17337 return retval;
17338 }
17339
17340 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17341 * the value held by the attribute is not constant. */
17342
17343 static LONGEST
17344 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17345 {
17346 if (attr->form == DW_FORM_sdata)
17347 return DW_SND (attr);
17348 else if (attr->form == DW_FORM_udata
17349 || attr->form == DW_FORM_data1
17350 || attr->form == DW_FORM_data2
17351 || attr->form == DW_FORM_data4
17352 || attr->form == DW_FORM_data8)
17353 return DW_UNSND (attr);
17354 else
17355 {
17356 complaint (&symfile_complaints,
17357 _("Attribute value is not a constant (%s)"),
17358 dwarf_form_name (attr->form));
17359 return default_value;
17360 }
17361 }
17362
17363 /* Follow reference or signature attribute ATTR of SRC_DIE.
17364 On entry *REF_CU is the CU of SRC_DIE.
17365 On exit *REF_CU is the CU of the result. */
17366
17367 static struct die_info *
17368 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17369 struct dwarf2_cu **ref_cu)
17370 {
17371 struct die_info *die;
17372
17373 if (is_ref_attr (attr))
17374 die = follow_die_ref (src_die, attr, ref_cu);
17375 else if (attr->form == DW_FORM_ref_sig8)
17376 die = follow_die_sig (src_die, attr, ref_cu);
17377 else
17378 {
17379 dump_die_for_error (src_die);
17380 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17381 (*ref_cu)->objfile->name);
17382 }
17383
17384 return die;
17385 }
17386
17387 /* Follow reference OFFSET.
17388 On entry *REF_CU is the CU of the source die referencing OFFSET.
17389 On exit *REF_CU is the CU of the result.
17390 Returns NULL if OFFSET is invalid. */
17391
17392 static struct die_info *
17393 follow_die_offset (sect_offset offset, int offset_in_dwz,
17394 struct dwarf2_cu **ref_cu)
17395 {
17396 struct die_info temp_die;
17397 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17398
17399 gdb_assert (cu->per_cu != NULL);
17400
17401 target_cu = cu;
17402
17403 if (cu->per_cu->is_debug_types)
17404 {
17405 /* .debug_types CUs cannot reference anything outside their CU.
17406 If they need to, they have to reference a signatured type via
17407 DW_FORM_ref_sig8. */
17408 if (! offset_in_cu_p (&cu->header, offset))
17409 return NULL;
17410 }
17411 else if (offset_in_dwz != cu->per_cu->is_dwz
17412 || ! offset_in_cu_p (&cu->header, offset))
17413 {
17414 struct dwarf2_per_cu_data *per_cu;
17415
17416 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17417 cu->objfile);
17418
17419 /* If necessary, add it to the queue and load its DIEs. */
17420 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17421 load_full_comp_unit (per_cu, cu->language);
17422
17423 target_cu = per_cu->cu;
17424 }
17425 else if (cu->dies == NULL)
17426 {
17427 /* We're loading full DIEs during partial symbol reading. */
17428 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17429 load_full_comp_unit (cu->per_cu, language_minimal);
17430 }
17431
17432 *ref_cu = target_cu;
17433 temp_die.offset = offset;
17434 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17435 }
17436
17437 /* Follow reference attribute ATTR of SRC_DIE.
17438 On entry *REF_CU is the CU of SRC_DIE.
17439 On exit *REF_CU is the CU of the result. */
17440
17441 static struct die_info *
17442 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17443 struct dwarf2_cu **ref_cu)
17444 {
17445 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17446 struct dwarf2_cu *cu = *ref_cu;
17447 struct die_info *die;
17448
17449 die = follow_die_offset (offset,
17450 (attr->form == DW_FORM_GNU_ref_alt
17451 || cu->per_cu->is_dwz),
17452 ref_cu);
17453 if (!die)
17454 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17455 "at 0x%x [in module %s]"),
17456 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17457
17458 return die;
17459 }
17460
17461 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17462 Returned value is intended for DW_OP_call*. Returned
17463 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17464
17465 struct dwarf2_locexpr_baton
17466 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17467 struct dwarf2_per_cu_data *per_cu,
17468 CORE_ADDR (*get_frame_pc) (void *baton),
17469 void *baton)
17470 {
17471 struct dwarf2_cu *cu;
17472 struct die_info *die;
17473 struct attribute *attr;
17474 struct dwarf2_locexpr_baton retval;
17475
17476 dw2_setup (per_cu->objfile);
17477
17478 if (per_cu->cu == NULL)
17479 load_cu (per_cu);
17480 cu = per_cu->cu;
17481
17482 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17483 if (!die)
17484 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17485 offset.sect_off, per_cu->objfile->name);
17486
17487 attr = dwarf2_attr (die, DW_AT_location, cu);
17488 if (!attr)
17489 {
17490 /* DWARF: "If there is no such attribute, then there is no effect.".
17491 DATA is ignored if SIZE is 0. */
17492
17493 retval.data = NULL;
17494 retval.size = 0;
17495 }
17496 else if (attr_form_is_section_offset (attr))
17497 {
17498 struct dwarf2_loclist_baton loclist_baton;
17499 CORE_ADDR pc = (*get_frame_pc) (baton);
17500 size_t size;
17501
17502 fill_in_loclist_baton (cu, &loclist_baton, attr);
17503
17504 retval.data = dwarf2_find_location_expression (&loclist_baton,
17505 &size, pc);
17506 retval.size = size;
17507 }
17508 else
17509 {
17510 if (!attr_form_is_block (attr))
17511 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17512 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17513 offset.sect_off, per_cu->objfile->name);
17514
17515 retval.data = DW_BLOCK (attr)->data;
17516 retval.size = DW_BLOCK (attr)->size;
17517 }
17518 retval.per_cu = cu->per_cu;
17519
17520 age_cached_comp_units ();
17521
17522 return retval;
17523 }
17524
17525 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17526 offset. */
17527
17528 struct dwarf2_locexpr_baton
17529 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17530 struct dwarf2_per_cu_data *per_cu,
17531 CORE_ADDR (*get_frame_pc) (void *baton),
17532 void *baton)
17533 {
17534 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17535
17536 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17537 }
17538
17539 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17540 PER_CU. */
17541
17542 struct type *
17543 dwarf2_get_die_type (cu_offset die_offset,
17544 struct dwarf2_per_cu_data *per_cu)
17545 {
17546 sect_offset die_offset_sect;
17547
17548 dw2_setup (per_cu->objfile);
17549
17550 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17551 return get_die_type_at_offset (die_offset_sect, per_cu);
17552 }
17553
17554 /* Follow the signature attribute ATTR in SRC_DIE.
17555 On entry *REF_CU is the CU of SRC_DIE.
17556 On exit *REF_CU is the CU of the result. */
17557
17558 static struct die_info *
17559 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17560 struct dwarf2_cu **ref_cu)
17561 {
17562 struct objfile *objfile = (*ref_cu)->objfile;
17563 struct die_info temp_die;
17564 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17565 struct dwarf2_cu *sig_cu;
17566 struct die_info *die;
17567
17568 /* sig_type will be NULL if the signatured type is missing from
17569 the debug info. */
17570 if (sig_type == NULL)
17571 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17572 "at 0x%x [in module %s]"),
17573 src_die->offset.sect_off, objfile->name);
17574
17575 /* If necessary, add it to the queue and load its DIEs. */
17576
17577 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17578 read_signatured_type (sig_type);
17579
17580 gdb_assert (sig_type->per_cu.cu != NULL);
17581
17582 sig_cu = sig_type->per_cu.cu;
17583 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17584 temp_die.offset = sig_type->type_offset_in_section;
17585 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17586 temp_die.offset.sect_off);
17587 if (die)
17588 {
17589 *ref_cu = sig_cu;
17590 return die;
17591 }
17592
17593 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17594 "from DIE at 0x%x [in module %s]"),
17595 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17596 }
17597
17598 /* Given an offset of a signatured type, return its signatured_type. */
17599
17600 static struct signatured_type *
17601 lookup_signatured_type_at_offset (struct objfile *objfile,
17602 struct dwarf2_section_info *section,
17603 sect_offset offset)
17604 {
17605 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17606 unsigned int length, initial_length_size;
17607 unsigned int sig_offset;
17608 struct signatured_type find_entry, *sig_type;
17609
17610 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17611 sig_offset = (initial_length_size
17612 + 2 /*version*/
17613 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17614 + 1 /*address_size*/);
17615 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17616 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17617
17618 /* This is only used to lookup previously recorded types.
17619 If we didn't find it, it's our bug. */
17620 gdb_assert (sig_type != NULL);
17621 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17622
17623 return sig_type;
17624 }
17625
17626 /* Load the DIEs associated with type unit PER_CU into memory. */
17627
17628 static void
17629 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17630 {
17631 struct signatured_type *sig_type;
17632
17633 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17634 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17635
17636 /* We have the per_cu, but we need the signatured_type.
17637 Fortunately this is an easy translation. */
17638 gdb_assert (per_cu->is_debug_types);
17639 sig_type = (struct signatured_type *) per_cu;
17640
17641 gdb_assert (per_cu->cu == NULL);
17642
17643 read_signatured_type (sig_type);
17644
17645 gdb_assert (per_cu->cu != NULL);
17646 }
17647
17648 /* die_reader_func for read_signatured_type.
17649 This is identical to load_full_comp_unit_reader,
17650 but is kept separate for now. */
17651
17652 static void
17653 read_signatured_type_reader (const struct die_reader_specs *reader,
17654 gdb_byte *info_ptr,
17655 struct die_info *comp_unit_die,
17656 int has_children,
17657 void *data)
17658 {
17659 struct dwarf2_cu *cu = reader->cu;
17660
17661 gdb_assert (cu->die_hash == NULL);
17662 cu->die_hash =
17663 htab_create_alloc_ex (cu->header.length / 12,
17664 die_hash,
17665 die_eq,
17666 NULL,
17667 &cu->comp_unit_obstack,
17668 hashtab_obstack_allocate,
17669 dummy_obstack_deallocate);
17670
17671 if (has_children)
17672 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17673 &info_ptr, comp_unit_die);
17674 cu->dies = comp_unit_die;
17675 /* comp_unit_die is not stored in die_hash, no need. */
17676
17677 /* We try not to read any attributes in this function, because not
17678 all CUs needed for references have been loaded yet, and symbol
17679 table processing isn't initialized. But we have to set the CU language,
17680 or we won't be able to build types correctly.
17681 Similarly, if we do not read the producer, we can not apply
17682 producer-specific interpretation. */
17683 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17684 }
17685
17686 /* Read in a signatured type and build its CU and DIEs.
17687 If the type is a stub for the real type in a DWO file,
17688 read in the real type from the DWO file as well. */
17689
17690 static void
17691 read_signatured_type (struct signatured_type *sig_type)
17692 {
17693 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17694
17695 gdb_assert (per_cu->is_debug_types);
17696 gdb_assert (per_cu->cu == NULL);
17697
17698 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17699 read_signatured_type_reader, NULL);
17700 }
17701
17702 /* Decode simple location descriptions.
17703 Given a pointer to a dwarf block that defines a location, compute
17704 the location and return the value.
17705
17706 NOTE drow/2003-11-18: This function is called in two situations
17707 now: for the address of static or global variables (partial symbols
17708 only) and for offsets into structures which are expected to be
17709 (more or less) constant. The partial symbol case should go away,
17710 and only the constant case should remain. That will let this
17711 function complain more accurately. A few special modes are allowed
17712 without complaint for global variables (for instance, global
17713 register values and thread-local values).
17714
17715 A location description containing no operations indicates that the
17716 object is optimized out. The return value is 0 for that case.
17717 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17718 callers will only want a very basic result and this can become a
17719 complaint.
17720
17721 Note that stack[0] is unused except as a default error return. */
17722
17723 static CORE_ADDR
17724 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17725 {
17726 struct objfile *objfile = cu->objfile;
17727 size_t i;
17728 size_t size = blk->size;
17729 gdb_byte *data = blk->data;
17730 CORE_ADDR stack[64];
17731 int stacki;
17732 unsigned int bytes_read, unsnd;
17733 gdb_byte op;
17734
17735 i = 0;
17736 stacki = 0;
17737 stack[stacki] = 0;
17738 stack[++stacki] = 0;
17739
17740 while (i < size)
17741 {
17742 op = data[i++];
17743 switch (op)
17744 {
17745 case DW_OP_lit0:
17746 case DW_OP_lit1:
17747 case DW_OP_lit2:
17748 case DW_OP_lit3:
17749 case DW_OP_lit4:
17750 case DW_OP_lit5:
17751 case DW_OP_lit6:
17752 case DW_OP_lit7:
17753 case DW_OP_lit8:
17754 case DW_OP_lit9:
17755 case DW_OP_lit10:
17756 case DW_OP_lit11:
17757 case DW_OP_lit12:
17758 case DW_OP_lit13:
17759 case DW_OP_lit14:
17760 case DW_OP_lit15:
17761 case DW_OP_lit16:
17762 case DW_OP_lit17:
17763 case DW_OP_lit18:
17764 case DW_OP_lit19:
17765 case DW_OP_lit20:
17766 case DW_OP_lit21:
17767 case DW_OP_lit22:
17768 case DW_OP_lit23:
17769 case DW_OP_lit24:
17770 case DW_OP_lit25:
17771 case DW_OP_lit26:
17772 case DW_OP_lit27:
17773 case DW_OP_lit28:
17774 case DW_OP_lit29:
17775 case DW_OP_lit30:
17776 case DW_OP_lit31:
17777 stack[++stacki] = op - DW_OP_lit0;
17778 break;
17779
17780 case DW_OP_reg0:
17781 case DW_OP_reg1:
17782 case DW_OP_reg2:
17783 case DW_OP_reg3:
17784 case DW_OP_reg4:
17785 case DW_OP_reg5:
17786 case DW_OP_reg6:
17787 case DW_OP_reg7:
17788 case DW_OP_reg8:
17789 case DW_OP_reg9:
17790 case DW_OP_reg10:
17791 case DW_OP_reg11:
17792 case DW_OP_reg12:
17793 case DW_OP_reg13:
17794 case DW_OP_reg14:
17795 case DW_OP_reg15:
17796 case DW_OP_reg16:
17797 case DW_OP_reg17:
17798 case DW_OP_reg18:
17799 case DW_OP_reg19:
17800 case DW_OP_reg20:
17801 case DW_OP_reg21:
17802 case DW_OP_reg22:
17803 case DW_OP_reg23:
17804 case DW_OP_reg24:
17805 case DW_OP_reg25:
17806 case DW_OP_reg26:
17807 case DW_OP_reg27:
17808 case DW_OP_reg28:
17809 case DW_OP_reg29:
17810 case DW_OP_reg30:
17811 case DW_OP_reg31:
17812 stack[++stacki] = op - DW_OP_reg0;
17813 if (i < size)
17814 dwarf2_complex_location_expr_complaint ();
17815 break;
17816
17817 case DW_OP_regx:
17818 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17819 i += bytes_read;
17820 stack[++stacki] = unsnd;
17821 if (i < size)
17822 dwarf2_complex_location_expr_complaint ();
17823 break;
17824
17825 case DW_OP_addr:
17826 stack[++stacki] = read_address (objfile->obfd, &data[i],
17827 cu, &bytes_read);
17828 i += bytes_read;
17829 break;
17830
17831 case DW_OP_const1u:
17832 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17833 i += 1;
17834 break;
17835
17836 case DW_OP_const1s:
17837 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17838 i += 1;
17839 break;
17840
17841 case DW_OP_const2u:
17842 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17843 i += 2;
17844 break;
17845
17846 case DW_OP_const2s:
17847 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17848 i += 2;
17849 break;
17850
17851 case DW_OP_const4u:
17852 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17853 i += 4;
17854 break;
17855
17856 case DW_OP_const4s:
17857 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17858 i += 4;
17859 break;
17860
17861 case DW_OP_const8u:
17862 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17863 i += 8;
17864 break;
17865
17866 case DW_OP_constu:
17867 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17868 &bytes_read);
17869 i += bytes_read;
17870 break;
17871
17872 case DW_OP_consts:
17873 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17874 i += bytes_read;
17875 break;
17876
17877 case DW_OP_dup:
17878 stack[stacki + 1] = stack[stacki];
17879 stacki++;
17880 break;
17881
17882 case DW_OP_plus:
17883 stack[stacki - 1] += stack[stacki];
17884 stacki--;
17885 break;
17886
17887 case DW_OP_plus_uconst:
17888 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17889 &bytes_read);
17890 i += bytes_read;
17891 break;
17892
17893 case DW_OP_minus:
17894 stack[stacki - 1] -= stack[stacki];
17895 stacki--;
17896 break;
17897
17898 case DW_OP_deref:
17899 /* If we're not the last op, then we definitely can't encode
17900 this using GDB's address_class enum. This is valid for partial
17901 global symbols, although the variable's address will be bogus
17902 in the psymtab. */
17903 if (i < size)
17904 dwarf2_complex_location_expr_complaint ();
17905 break;
17906
17907 case DW_OP_GNU_push_tls_address:
17908 /* The top of the stack has the offset from the beginning
17909 of the thread control block at which the variable is located. */
17910 /* Nothing should follow this operator, so the top of stack would
17911 be returned. */
17912 /* This is valid for partial global symbols, but the variable's
17913 address will be bogus in the psymtab. Make it always at least
17914 non-zero to not look as a variable garbage collected by linker
17915 which have DW_OP_addr 0. */
17916 if (i < size)
17917 dwarf2_complex_location_expr_complaint ();
17918 stack[stacki]++;
17919 break;
17920
17921 case DW_OP_GNU_uninit:
17922 break;
17923
17924 case DW_OP_GNU_addr_index:
17925 case DW_OP_GNU_const_index:
17926 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17927 &bytes_read);
17928 i += bytes_read;
17929 break;
17930
17931 default:
17932 {
17933 const char *name = get_DW_OP_name (op);
17934
17935 if (name)
17936 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17937 name);
17938 else
17939 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17940 op);
17941 }
17942
17943 return (stack[stacki]);
17944 }
17945
17946 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17947 outside of the allocated space. Also enforce minimum>0. */
17948 if (stacki >= ARRAY_SIZE (stack) - 1)
17949 {
17950 complaint (&symfile_complaints,
17951 _("location description stack overflow"));
17952 return 0;
17953 }
17954
17955 if (stacki <= 0)
17956 {
17957 complaint (&symfile_complaints,
17958 _("location description stack underflow"));
17959 return 0;
17960 }
17961 }
17962 return (stack[stacki]);
17963 }
17964
17965 /* memory allocation interface */
17966
17967 static struct dwarf_block *
17968 dwarf_alloc_block (struct dwarf2_cu *cu)
17969 {
17970 struct dwarf_block *blk;
17971
17972 blk = (struct dwarf_block *)
17973 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17974 return (blk);
17975 }
17976
17977 static struct die_info *
17978 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17979 {
17980 struct die_info *die;
17981 size_t size = sizeof (struct die_info);
17982
17983 if (num_attrs > 1)
17984 size += (num_attrs - 1) * sizeof (struct attribute);
17985
17986 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17987 memset (die, 0, sizeof (struct die_info));
17988 return (die);
17989 }
17990
17991 \f
17992 /* Macro support. */
17993
17994 /* Return the full name of file number I in *LH's file name table.
17995 Use COMP_DIR as the name of the current directory of the
17996 compilation. The result is allocated using xmalloc; the caller is
17997 responsible for freeing it. */
17998 static char *
17999 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18000 {
18001 /* Is the file number a valid index into the line header's file name
18002 table? Remember that file numbers start with one, not zero. */
18003 if (1 <= file && file <= lh->num_file_names)
18004 {
18005 struct file_entry *fe = &lh->file_names[file - 1];
18006
18007 if (IS_ABSOLUTE_PATH (fe->name))
18008 return xstrdup (fe->name);
18009 else
18010 {
18011 const char *dir;
18012 int dir_len;
18013 char *full_name;
18014
18015 if (fe->dir_index)
18016 dir = lh->include_dirs[fe->dir_index - 1];
18017 else
18018 dir = comp_dir;
18019
18020 if (dir)
18021 {
18022 dir_len = strlen (dir);
18023 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18024 strcpy (full_name, dir);
18025 full_name[dir_len] = '/';
18026 strcpy (full_name + dir_len + 1, fe->name);
18027 return full_name;
18028 }
18029 else
18030 return xstrdup (fe->name);
18031 }
18032 }
18033 else
18034 {
18035 /* The compiler produced a bogus file number. We can at least
18036 record the macro definitions made in the file, even if we
18037 won't be able to find the file by name. */
18038 char fake_name[80];
18039
18040 xsnprintf (fake_name, sizeof (fake_name),
18041 "<bad macro file number %d>", file);
18042
18043 complaint (&symfile_complaints,
18044 _("bad file number in macro information (%d)"),
18045 file);
18046
18047 return xstrdup (fake_name);
18048 }
18049 }
18050
18051
18052 static struct macro_source_file *
18053 macro_start_file (int file, int line,
18054 struct macro_source_file *current_file,
18055 const char *comp_dir,
18056 struct line_header *lh, struct objfile *objfile)
18057 {
18058 /* The full name of this source file. */
18059 char *full_name = file_full_name (file, lh, comp_dir);
18060
18061 /* We don't create a macro table for this compilation unit
18062 at all until we actually get a filename. */
18063 if (! pending_macros)
18064 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18065 objfile->per_bfd->macro_cache);
18066
18067 if (! current_file)
18068 {
18069 /* If we have no current file, then this must be the start_file
18070 directive for the compilation unit's main source file. */
18071 current_file = macro_set_main (pending_macros, full_name);
18072 macro_define_special (pending_macros);
18073 }
18074 else
18075 current_file = macro_include (current_file, line, full_name);
18076
18077 xfree (full_name);
18078
18079 return current_file;
18080 }
18081
18082
18083 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18084 followed by a null byte. */
18085 static char *
18086 copy_string (const char *buf, int len)
18087 {
18088 char *s = xmalloc (len + 1);
18089
18090 memcpy (s, buf, len);
18091 s[len] = '\0';
18092 return s;
18093 }
18094
18095
18096 static const char *
18097 consume_improper_spaces (const char *p, const char *body)
18098 {
18099 if (*p == ' ')
18100 {
18101 complaint (&symfile_complaints,
18102 _("macro definition contains spaces "
18103 "in formal argument list:\n`%s'"),
18104 body);
18105
18106 while (*p == ' ')
18107 p++;
18108 }
18109
18110 return p;
18111 }
18112
18113
18114 static void
18115 parse_macro_definition (struct macro_source_file *file, int line,
18116 const char *body)
18117 {
18118 const char *p;
18119
18120 /* The body string takes one of two forms. For object-like macro
18121 definitions, it should be:
18122
18123 <macro name> " " <definition>
18124
18125 For function-like macro definitions, it should be:
18126
18127 <macro name> "() " <definition>
18128 or
18129 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18130
18131 Spaces may appear only where explicitly indicated, and in the
18132 <definition>.
18133
18134 The Dwarf 2 spec says that an object-like macro's name is always
18135 followed by a space, but versions of GCC around March 2002 omit
18136 the space when the macro's definition is the empty string.
18137
18138 The Dwarf 2 spec says that there should be no spaces between the
18139 formal arguments in a function-like macro's formal argument list,
18140 but versions of GCC around March 2002 include spaces after the
18141 commas. */
18142
18143
18144 /* Find the extent of the macro name. The macro name is terminated
18145 by either a space or null character (for an object-like macro) or
18146 an opening paren (for a function-like macro). */
18147 for (p = body; *p; p++)
18148 if (*p == ' ' || *p == '(')
18149 break;
18150
18151 if (*p == ' ' || *p == '\0')
18152 {
18153 /* It's an object-like macro. */
18154 int name_len = p - body;
18155 char *name = copy_string (body, name_len);
18156 const char *replacement;
18157
18158 if (*p == ' ')
18159 replacement = body + name_len + 1;
18160 else
18161 {
18162 dwarf2_macro_malformed_definition_complaint (body);
18163 replacement = body + name_len;
18164 }
18165
18166 macro_define_object (file, line, name, replacement);
18167
18168 xfree (name);
18169 }
18170 else if (*p == '(')
18171 {
18172 /* It's a function-like macro. */
18173 char *name = copy_string (body, p - body);
18174 int argc = 0;
18175 int argv_size = 1;
18176 char **argv = xmalloc (argv_size * sizeof (*argv));
18177
18178 p++;
18179
18180 p = consume_improper_spaces (p, body);
18181
18182 /* Parse the formal argument list. */
18183 while (*p && *p != ')')
18184 {
18185 /* Find the extent of the current argument name. */
18186 const char *arg_start = p;
18187
18188 while (*p && *p != ',' && *p != ')' && *p != ' ')
18189 p++;
18190
18191 if (! *p || p == arg_start)
18192 dwarf2_macro_malformed_definition_complaint (body);
18193 else
18194 {
18195 /* Make sure argv has room for the new argument. */
18196 if (argc >= argv_size)
18197 {
18198 argv_size *= 2;
18199 argv = xrealloc (argv, argv_size * sizeof (*argv));
18200 }
18201
18202 argv[argc++] = copy_string (arg_start, p - arg_start);
18203 }
18204
18205 p = consume_improper_spaces (p, body);
18206
18207 /* Consume the comma, if present. */
18208 if (*p == ',')
18209 {
18210 p++;
18211
18212 p = consume_improper_spaces (p, body);
18213 }
18214 }
18215
18216 if (*p == ')')
18217 {
18218 p++;
18219
18220 if (*p == ' ')
18221 /* Perfectly formed definition, no complaints. */
18222 macro_define_function (file, line, name,
18223 argc, (const char **) argv,
18224 p + 1);
18225 else if (*p == '\0')
18226 {
18227 /* Complain, but do define it. */
18228 dwarf2_macro_malformed_definition_complaint (body);
18229 macro_define_function (file, line, name,
18230 argc, (const char **) argv,
18231 p);
18232 }
18233 else
18234 /* Just complain. */
18235 dwarf2_macro_malformed_definition_complaint (body);
18236 }
18237 else
18238 /* Just complain. */
18239 dwarf2_macro_malformed_definition_complaint (body);
18240
18241 xfree (name);
18242 {
18243 int i;
18244
18245 for (i = 0; i < argc; i++)
18246 xfree (argv[i]);
18247 }
18248 xfree (argv);
18249 }
18250 else
18251 dwarf2_macro_malformed_definition_complaint (body);
18252 }
18253
18254 /* Skip some bytes from BYTES according to the form given in FORM.
18255 Returns the new pointer. */
18256
18257 static gdb_byte *
18258 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18259 enum dwarf_form form,
18260 unsigned int offset_size,
18261 struct dwarf2_section_info *section)
18262 {
18263 unsigned int bytes_read;
18264
18265 switch (form)
18266 {
18267 case DW_FORM_data1:
18268 case DW_FORM_flag:
18269 ++bytes;
18270 break;
18271
18272 case DW_FORM_data2:
18273 bytes += 2;
18274 break;
18275
18276 case DW_FORM_data4:
18277 bytes += 4;
18278 break;
18279
18280 case DW_FORM_data8:
18281 bytes += 8;
18282 break;
18283
18284 case DW_FORM_string:
18285 read_direct_string (abfd, bytes, &bytes_read);
18286 bytes += bytes_read;
18287 break;
18288
18289 case DW_FORM_sec_offset:
18290 case DW_FORM_strp:
18291 case DW_FORM_GNU_strp_alt:
18292 bytes += offset_size;
18293 break;
18294
18295 case DW_FORM_block:
18296 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18297 bytes += bytes_read;
18298 break;
18299
18300 case DW_FORM_block1:
18301 bytes += 1 + read_1_byte (abfd, bytes);
18302 break;
18303 case DW_FORM_block2:
18304 bytes += 2 + read_2_bytes (abfd, bytes);
18305 break;
18306 case DW_FORM_block4:
18307 bytes += 4 + read_4_bytes (abfd, bytes);
18308 break;
18309
18310 case DW_FORM_sdata:
18311 case DW_FORM_udata:
18312 case DW_FORM_GNU_addr_index:
18313 case DW_FORM_GNU_str_index:
18314 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18315 if (bytes == NULL)
18316 {
18317 dwarf2_section_buffer_overflow_complaint (section);
18318 return NULL;
18319 }
18320 break;
18321
18322 default:
18323 {
18324 complain:
18325 complaint (&symfile_complaints,
18326 _("invalid form 0x%x in `%s'"),
18327 form,
18328 section->asection->name);
18329 return NULL;
18330 }
18331 }
18332
18333 return bytes;
18334 }
18335
18336 /* A helper for dwarf_decode_macros that handles skipping an unknown
18337 opcode. Returns an updated pointer to the macro data buffer; or,
18338 on error, issues a complaint and returns NULL. */
18339
18340 static gdb_byte *
18341 skip_unknown_opcode (unsigned int opcode,
18342 gdb_byte **opcode_definitions,
18343 gdb_byte *mac_ptr, gdb_byte *mac_end,
18344 bfd *abfd,
18345 unsigned int offset_size,
18346 struct dwarf2_section_info *section)
18347 {
18348 unsigned int bytes_read, i;
18349 unsigned long arg;
18350 gdb_byte *defn;
18351
18352 if (opcode_definitions[opcode] == NULL)
18353 {
18354 complaint (&symfile_complaints,
18355 _("unrecognized DW_MACFINO opcode 0x%x"),
18356 opcode);
18357 return NULL;
18358 }
18359
18360 defn = opcode_definitions[opcode];
18361 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18362 defn += bytes_read;
18363
18364 for (i = 0; i < arg; ++i)
18365 {
18366 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18367 section);
18368 if (mac_ptr == NULL)
18369 {
18370 /* skip_form_bytes already issued the complaint. */
18371 return NULL;
18372 }
18373 }
18374
18375 return mac_ptr;
18376 }
18377
18378 /* A helper function which parses the header of a macro section.
18379 If the macro section is the extended (for now called "GNU") type,
18380 then this updates *OFFSET_SIZE. Returns a pointer to just after
18381 the header, or issues a complaint and returns NULL on error. */
18382
18383 static gdb_byte *
18384 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18385 bfd *abfd,
18386 gdb_byte *mac_ptr,
18387 unsigned int *offset_size,
18388 int section_is_gnu)
18389 {
18390 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18391
18392 if (section_is_gnu)
18393 {
18394 unsigned int version, flags;
18395
18396 version = read_2_bytes (abfd, mac_ptr);
18397 if (version != 4)
18398 {
18399 complaint (&symfile_complaints,
18400 _("unrecognized version `%d' in .debug_macro section"),
18401 version);
18402 return NULL;
18403 }
18404 mac_ptr += 2;
18405
18406 flags = read_1_byte (abfd, mac_ptr);
18407 ++mac_ptr;
18408 *offset_size = (flags & 1) ? 8 : 4;
18409
18410 if ((flags & 2) != 0)
18411 /* We don't need the line table offset. */
18412 mac_ptr += *offset_size;
18413
18414 /* Vendor opcode descriptions. */
18415 if ((flags & 4) != 0)
18416 {
18417 unsigned int i, count;
18418
18419 count = read_1_byte (abfd, mac_ptr);
18420 ++mac_ptr;
18421 for (i = 0; i < count; ++i)
18422 {
18423 unsigned int opcode, bytes_read;
18424 unsigned long arg;
18425
18426 opcode = read_1_byte (abfd, mac_ptr);
18427 ++mac_ptr;
18428 opcode_definitions[opcode] = mac_ptr;
18429 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18430 mac_ptr += bytes_read;
18431 mac_ptr += arg;
18432 }
18433 }
18434 }
18435
18436 return mac_ptr;
18437 }
18438
18439 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18440 including DW_MACRO_GNU_transparent_include. */
18441
18442 static void
18443 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18444 struct macro_source_file *current_file,
18445 struct line_header *lh, char *comp_dir,
18446 struct dwarf2_section_info *section,
18447 int section_is_gnu, int section_is_dwz,
18448 unsigned int offset_size,
18449 struct objfile *objfile,
18450 htab_t include_hash)
18451 {
18452 enum dwarf_macro_record_type macinfo_type;
18453 int at_commandline;
18454 gdb_byte *opcode_definitions[256];
18455
18456 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18457 &offset_size, section_is_gnu);
18458 if (mac_ptr == NULL)
18459 {
18460 /* We already issued a complaint. */
18461 return;
18462 }
18463
18464 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18465 GDB is still reading the definitions from command line. First
18466 DW_MACINFO_start_file will need to be ignored as it was already executed
18467 to create CURRENT_FILE for the main source holding also the command line
18468 definitions. On first met DW_MACINFO_start_file this flag is reset to
18469 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18470
18471 at_commandline = 1;
18472
18473 do
18474 {
18475 /* Do we at least have room for a macinfo type byte? */
18476 if (mac_ptr >= mac_end)
18477 {
18478 dwarf2_section_buffer_overflow_complaint (section);
18479 break;
18480 }
18481
18482 macinfo_type = read_1_byte (abfd, mac_ptr);
18483 mac_ptr++;
18484
18485 /* Note that we rely on the fact that the corresponding GNU and
18486 DWARF constants are the same. */
18487 switch (macinfo_type)
18488 {
18489 /* A zero macinfo type indicates the end of the macro
18490 information. */
18491 case 0:
18492 break;
18493
18494 case DW_MACRO_GNU_define:
18495 case DW_MACRO_GNU_undef:
18496 case DW_MACRO_GNU_define_indirect:
18497 case DW_MACRO_GNU_undef_indirect:
18498 case DW_MACRO_GNU_define_indirect_alt:
18499 case DW_MACRO_GNU_undef_indirect_alt:
18500 {
18501 unsigned int bytes_read;
18502 int line;
18503 char *body;
18504 int is_define;
18505
18506 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18507 mac_ptr += bytes_read;
18508
18509 if (macinfo_type == DW_MACRO_GNU_define
18510 || macinfo_type == DW_MACRO_GNU_undef)
18511 {
18512 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18513 mac_ptr += bytes_read;
18514 }
18515 else
18516 {
18517 LONGEST str_offset;
18518
18519 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18520 mac_ptr += offset_size;
18521
18522 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18523 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18524 || section_is_dwz)
18525 {
18526 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18527
18528 body = read_indirect_string_from_dwz (dwz, str_offset);
18529 }
18530 else
18531 body = read_indirect_string_at_offset (abfd, str_offset);
18532 }
18533
18534 is_define = (macinfo_type == DW_MACRO_GNU_define
18535 || macinfo_type == DW_MACRO_GNU_define_indirect
18536 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18537 if (! current_file)
18538 {
18539 /* DWARF violation as no main source is present. */
18540 complaint (&symfile_complaints,
18541 _("debug info with no main source gives macro %s "
18542 "on line %d: %s"),
18543 is_define ? _("definition") : _("undefinition"),
18544 line, body);
18545 break;
18546 }
18547 if ((line == 0 && !at_commandline)
18548 || (line != 0 && at_commandline))
18549 complaint (&symfile_complaints,
18550 _("debug info gives %s macro %s with %s line %d: %s"),
18551 at_commandline ? _("command-line") : _("in-file"),
18552 is_define ? _("definition") : _("undefinition"),
18553 line == 0 ? _("zero") : _("non-zero"), line, body);
18554
18555 if (is_define)
18556 parse_macro_definition (current_file, line, body);
18557 else
18558 {
18559 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18560 || macinfo_type == DW_MACRO_GNU_undef_indirect
18561 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18562 macro_undef (current_file, line, body);
18563 }
18564 }
18565 break;
18566
18567 case DW_MACRO_GNU_start_file:
18568 {
18569 unsigned int bytes_read;
18570 int line, file;
18571
18572 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18573 mac_ptr += bytes_read;
18574 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18575 mac_ptr += bytes_read;
18576
18577 if ((line == 0 && !at_commandline)
18578 || (line != 0 && at_commandline))
18579 complaint (&symfile_complaints,
18580 _("debug info gives source %d included "
18581 "from %s at %s line %d"),
18582 file, at_commandline ? _("command-line") : _("file"),
18583 line == 0 ? _("zero") : _("non-zero"), line);
18584
18585 if (at_commandline)
18586 {
18587 /* This DW_MACRO_GNU_start_file was executed in the
18588 pass one. */
18589 at_commandline = 0;
18590 }
18591 else
18592 current_file = macro_start_file (file, line,
18593 current_file, comp_dir,
18594 lh, objfile);
18595 }
18596 break;
18597
18598 case DW_MACRO_GNU_end_file:
18599 if (! current_file)
18600 complaint (&symfile_complaints,
18601 _("macro debug info has an unmatched "
18602 "`close_file' directive"));
18603 else
18604 {
18605 current_file = current_file->included_by;
18606 if (! current_file)
18607 {
18608 enum dwarf_macro_record_type next_type;
18609
18610 /* GCC circa March 2002 doesn't produce the zero
18611 type byte marking the end of the compilation
18612 unit. Complain if it's not there, but exit no
18613 matter what. */
18614
18615 /* Do we at least have room for a macinfo type byte? */
18616 if (mac_ptr >= mac_end)
18617 {
18618 dwarf2_section_buffer_overflow_complaint (section);
18619 return;
18620 }
18621
18622 /* We don't increment mac_ptr here, so this is just
18623 a look-ahead. */
18624 next_type = read_1_byte (abfd, mac_ptr);
18625 if (next_type != 0)
18626 complaint (&symfile_complaints,
18627 _("no terminating 0-type entry for "
18628 "macros in `.debug_macinfo' section"));
18629
18630 return;
18631 }
18632 }
18633 break;
18634
18635 case DW_MACRO_GNU_transparent_include:
18636 case DW_MACRO_GNU_transparent_include_alt:
18637 {
18638 LONGEST offset;
18639 void **slot;
18640 bfd *include_bfd = abfd;
18641 struct dwarf2_section_info *include_section = section;
18642 struct dwarf2_section_info alt_section;
18643 gdb_byte *include_mac_end = mac_end;
18644 int is_dwz = section_is_dwz;
18645 gdb_byte *new_mac_ptr;
18646
18647 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18648 mac_ptr += offset_size;
18649
18650 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18651 {
18652 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18653
18654 dwarf2_read_section (dwarf2_per_objfile->objfile,
18655 &dwz->macro);
18656
18657 include_bfd = dwz->macro.asection->owner;
18658 include_section = &dwz->macro;
18659 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18660 is_dwz = 1;
18661 }
18662
18663 new_mac_ptr = include_section->buffer + offset;
18664 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18665
18666 if (*slot != NULL)
18667 {
18668 /* This has actually happened; see
18669 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18670 complaint (&symfile_complaints,
18671 _("recursive DW_MACRO_GNU_transparent_include in "
18672 ".debug_macro section"));
18673 }
18674 else
18675 {
18676 *slot = new_mac_ptr;
18677
18678 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18679 include_mac_end, current_file,
18680 lh, comp_dir,
18681 section, section_is_gnu, is_dwz,
18682 offset_size, objfile, include_hash);
18683
18684 htab_remove_elt (include_hash, new_mac_ptr);
18685 }
18686 }
18687 break;
18688
18689 case DW_MACINFO_vendor_ext:
18690 if (!section_is_gnu)
18691 {
18692 unsigned int bytes_read;
18693 int constant;
18694
18695 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18696 mac_ptr += bytes_read;
18697 read_direct_string (abfd, mac_ptr, &bytes_read);
18698 mac_ptr += bytes_read;
18699
18700 /* We don't recognize any vendor extensions. */
18701 break;
18702 }
18703 /* FALLTHROUGH */
18704
18705 default:
18706 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18707 mac_ptr, mac_end, abfd, offset_size,
18708 section);
18709 if (mac_ptr == NULL)
18710 return;
18711 break;
18712 }
18713 } while (macinfo_type != 0);
18714 }
18715
18716 static void
18717 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18718 char *comp_dir, int section_is_gnu)
18719 {
18720 struct objfile *objfile = dwarf2_per_objfile->objfile;
18721 struct line_header *lh = cu->line_header;
18722 bfd *abfd;
18723 gdb_byte *mac_ptr, *mac_end;
18724 struct macro_source_file *current_file = 0;
18725 enum dwarf_macro_record_type macinfo_type;
18726 unsigned int offset_size = cu->header.offset_size;
18727 gdb_byte *opcode_definitions[256];
18728 struct cleanup *cleanup;
18729 htab_t include_hash;
18730 void **slot;
18731 struct dwarf2_section_info *section;
18732 const char *section_name;
18733
18734 if (cu->dwo_unit != NULL)
18735 {
18736 if (section_is_gnu)
18737 {
18738 section = &cu->dwo_unit->dwo_file->sections.macro;
18739 section_name = ".debug_macro.dwo";
18740 }
18741 else
18742 {
18743 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18744 section_name = ".debug_macinfo.dwo";
18745 }
18746 }
18747 else
18748 {
18749 if (section_is_gnu)
18750 {
18751 section = &dwarf2_per_objfile->macro;
18752 section_name = ".debug_macro";
18753 }
18754 else
18755 {
18756 section = &dwarf2_per_objfile->macinfo;
18757 section_name = ".debug_macinfo";
18758 }
18759 }
18760
18761 dwarf2_read_section (objfile, section);
18762 if (section->buffer == NULL)
18763 {
18764 complaint (&symfile_complaints, _("missing %s section"), section_name);
18765 return;
18766 }
18767 abfd = section->asection->owner;
18768
18769 /* First pass: Find the name of the base filename.
18770 This filename is needed in order to process all macros whose definition
18771 (or undefinition) comes from the command line. These macros are defined
18772 before the first DW_MACINFO_start_file entry, and yet still need to be
18773 associated to the base file.
18774
18775 To determine the base file name, we scan the macro definitions until we
18776 reach the first DW_MACINFO_start_file entry. We then initialize
18777 CURRENT_FILE accordingly so that any macro definition found before the
18778 first DW_MACINFO_start_file can still be associated to the base file. */
18779
18780 mac_ptr = section->buffer + offset;
18781 mac_end = section->buffer + section->size;
18782
18783 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18784 &offset_size, section_is_gnu);
18785 if (mac_ptr == NULL)
18786 {
18787 /* We already issued a complaint. */
18788 return;
18789 }
18790
18791 do
18792 {
18793 /* Do we at least have room for a macinfo type byte? */
18794 if (mac_ptr >= mac_end)
18795 {
18796 /* Complaint is printed during the second pass as GDB will probably
18797 stop the first pass earlier upon finding
18798 DW_MACINFO_start_file. */
18799 break;
18800 }
18801
18802 macinfo_type = read_1_byte (abfd, mac_ptr);
18803 mac_ptr++;
18804
18805 /* Note that we rely on the fact that the corresponding GNU and
18806 DWARF constants are the same. */
18807 switch (macinfo_type)
18808 {
18809 /* A zero macinfo type indicates the end of the macro
18810 information. */
18811 case 0:
18812 break;
18813
18814 case DW_MACRO_GNU_define:
18815 case DW_MACRO_GNU_undef:
18816 /* Only skip the data by MAC_PTR. */
18817 {
18818 unsigned int bytes_read;
18819
18820 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18821 mac_ptr += bytes_read;
18822 read_direct_string (abfd, mac_ptr, &bytes_read);
18823 mac_ptr += bytes_read;
18824 }
18825 break;
18826
18827 case DW_MACRO_GNU_start_file:
18828 {
18829 unsigned int bytes_read;
18830 int line, file;
18831
18832 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18833 mac_ptr += bytes_read;
18834 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18835 mac_ptr += bytes_read;
18836
18837 current_file = macro_start_file (file, line, current_file,
18838 comp_dir, lh, objfile);
18839 }
18840 break;
18841
18842 case DW_MACRO_GNU_end_file:
18843 /* No data to skip by MAC_PTR. */
18844 break;
18845
18846 case DW_MACRO_GNU_define_indirect:
18847 case DW_MACRO_GNU_undef_indirect:
18848 case DW_MACRO_GNU_define_indirect_alt:
18849 case DW_MACRO_GNU_undef_indirect_alt:
18850 {
18851 unsigned int bytes_read;
18852
18853 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18854 mac_ptr += bytes_read;
18855 mac_ptr += offset_size;
18856 }
18857 break;
18858
18859 case DW_MACRO_GNU_transparent_include:
18860 case DW_MACRO_GNU_transparent_include_alt:
18861 /* Note that, according to the spec, a transparent include
18862 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18863 skip this opcode. */
18864 mac_ptr += offset_size;
18865 break;
18866
18867 case DW_MACINFO_vendor_ext:
18868 /* Only skip the data by MAC_PTR. */
18869 if (!section_is_gnu)
18870 {
18871 unsigned int bytes_read;
18872
18873 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18874 mac_ptr += bytes_read;
18875 read_direct_string (abfd, mac_ptr, &bytes_read);
18876 mac_ptr += bytes_read;
18877 }
18878 /* FALLTHROUGH */
18879
18880 default:
18881 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18882 mac_ptr, mac_end, abfd, offset_size,
18883 section);
18884 if (mac_ptr == NULL)
18885 return;
18886 break;
18887 }
18888 } while (macinfo_type != 0 && current_file == NULL);
18889
18890 /* Second pass: Process all entries.
18891
18892 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18893 command-line macro definitions/undefinitions. This flag is unset when we
18894 reach the first DW_MACINFO_start_file entry. */
18895
18896 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18897 NULL, xcalloc, xfree);
18898 cleanup = make_cleanup_htab_delete (include_hash);
18899 mac_ptr = section->buffer + offset;
18900 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18901 *slot = mac_ptr;
18902 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18903 current_file, lh, comp_dir, section,
18904 section_is_gnu, 0,
18905 offset_size, objfile, include_hash);
18906 do_cleanups (cleanup);
18907 }
18908
18909 /* Check if the attribute's form is a DW_FORM_block*
18910 if so return true else false. */
18911
18912 static int
18913 attr_form_is_block (struct attribute *attr)
18914 {
18915 return (attr == NULL ? 0 :
18916 attr->form == DW_FORM_block1
18917 || attr->form == DW_FORM_block2
18918 || attr->form == DW_FORM_block4
18919 || attr->form == DW_FORM_block
18920 || attr->form == DW_FORM_exprloc);
18921 }
18922
18923 /* Return non-zero if ATTR's value is a section offset --- classes
18924 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18925 You may use DW_UNSND (attr) to retrieve such offsets.
18926
18927 Section 7.5.4, "Attribute Encodings", explains that no attribute
18928 may have a value that belongs to more than one of these classes; it
18929 would be ambiguous if we did, because we use the same forms for all
18930 of them. */
18931
18932 static int
18933 attr_form_is_section_offset (struct attribute *attr)
18934 {
18935 return (attr->form == DW_FORM_data4
18936 || attr->form == DW_FORM_data8
18937 || attr->form == DW_FORM_sec_offset);
18938 }
18939
18940 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18941 zero otherwise. When this function returns true, you can apply
18942 dwarf2_get_attr_constant_value to it.
18943
18944 However, note that for some attributes you must check
18945 attr_form_is_section_offset before using this test. DW_FORM_data4
18946 and DW_FORM_data8 are members of both the constant class, and of
18947 the classes that contain offsets into other debug sections
18948 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18949 that, if an attribute's can be either a constant or one of the
18950 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18951 taken as section offsets, not constants. */
18952
18953 static int
18954 attr_form_is_constant (struct attribute *attr)
18955 {
18956 switch (attr->form)
18957 {
18958 case DW_FORM_sdata:
18959 case DW_FORM_udata:
18960 case DW_FORM_data1:
18961 case DW_FORM_data2:
18962 case DW_FORM_data4:
18963 case DW_FORM_data8:
18964 return 1;
18965 default:
18966 return 0;
18967 }
18968 }
18969
18970 /* Return the .debug_loc section to use for CU.
18971 For DWO files use .debug_loc.dwo. */
18972
18973 static struct dwarf2_section_info *
18974 cu_debug_loc_section (struct dwarf2_cu *cu)
18975 {
18976 if (cu->dwo_unit)
18977 return &cu->dwo_unit->dwo_file->sections.loc;
18978 return &dwarf2_per_objfile->loc;
18979 }
18980
18981 /* A helper function that fills in a dwarf2_loclist_baton. */
18982
18983 static void
18984 fill_in_loclist_baton (struct dwarf2_cu *cu,
18985 struct dwarf2_loclist_baton *baton,
18986 struct attribute *attr)
18987 {
18988 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18989
18990 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18991
18992 baton->per_cu = cu->per_cu;
18993 gdb_assert (baton->per_cu);
18994 /* We don't know how long the location list is, but make sure we
18995 don't run off the edge of the section. */
18996 baton->size = section->size - DW_UNSND (attr);
18997 baton->data = section->buffer + DW_UNSND (attr);
18998 baton->base_address = cu->base_address;
18999 baton->from_dwo = cu->dwo_unit != NULL;
19000 }
19001
19002 static void
19003 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19004 struct dwarf2_cu *cu)
19005 {
19006 struct objfile *objfile = dwarf2_per_objfile->objfile;
19007 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19008
19009 if (attr_form_is_section_offset (attr)
19010 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19011 the section. If so, fall through to the complaint in the
19012 other branch. */
19013 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19014 {
19015 struct dwarf2_loclist_baton *baton;
19016
19017 baton = obstack_alloc (&objfile->objfile_obstack,
19018 sizeof (struct dwarf2_loclist_baton));
19019
19020 fill_in_loclist_baton (cu, baton, attr);
19021
19022 if (cu->base_known == 0)
19023 complaint (&symfile_complaints,
19024 _("Location list used without "
19025 "specifying the CU base address."));
19026
19027 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
19028 SYMBOL_LOCATION_BATON (sym) = baton;
19029 }
19030 else
19031 {
19032 struct dwarf2_locexpr_baton *baton;
19033
19034 baton = obstack_alloc (&objfile->objfile_obstack,
19035 sizeof (struct dwarf2_locexpr_baton));
19036 baton->per_cu = cu->per_cu;
19037 gdb_assert (baton->per_cu);
19038
19039 if (attr_form_is_block (attr))
19040 {
19041 /* Note that we're just copying the block's data pointer
19042 here, not the actual data. We're still pointing into the
19043 info_buffer for SYM's objfile; right now we never release
19044 that buffer, but when we do clean up properly this may
19045 need to change. */
19046 baton->size = DW_BLOCK (attr)->size;
19047 baton->data = DW_BLOCK (attr)->data;
19048 }
19049 else
19050 {
19051 dwarf2_invalid_attrib_class_complaint ("location description",
19052 SYMBOL_NATURAL_NAME (sym));
19053 baton->size = 0;
19054 }
19055
19056 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
19057 SYMBOL_LOCATION_BATON (sym) = baton;
19058 }
19059 }
19060
19061 /* Return the OBJFILE associated with the compilation unit CU. If CU
19062 came from a separate debuginfo file, then the master objfile is
19063 returned. */
19064
19065 struct objfile *
19066 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19067 {
19068 struct objfile *objfile = per_cu->objfile;
19069
19070 /* Return the master objfile, so that we can report and look up the
19071 correct file containing this variable. */
19072 if (objfile->separate_debug_objfile_backlink)
19073 objfile = objfile->separate_debug_objfile_backlink;
19074
19075 return objfile;
19076 }
19077
19078 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19079 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19080 CU_HEADERP first. */
19081
19082 static const struct comp_unit_head *
19083 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19084 struct dwarf2_per_cu_data *per_cu)
19085 {
19086 gdb_byte *info_ptr;
19087
19088 if (per_cu->cu)
19089 return &per_cu->cu->header;
19090
19091 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19092
19093 memset (cu_headerp, 0, sizeof (*cu_headerp));
19094 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19095
19096 return cu_headerp;
19097 }
19098
19099 /* Return the address size given in the compilation unit header for CU. */
19100
19101 int
19102 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19103 {
19104 struct comp_unit_head cu_header_local;
19105 const struct comp_unit_head *cu_headerp;
19106
19107 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19108
19109 return cu_headerp->addr_size;
19110 }
19111
19112 /* Return the offset size given in the compilation unit header for CU. */
19113
19114 int
19115 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19116 {
19117 struct comp_unit_head cu_header_local;
19118 const struct comp_unit_head *cu_headerp;
19119
19120 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19121
19122 return cu_headerp->offset_size;
19123 }
19124
19125 /* See its dwarf2loc.h declaration. */
19126
19127 int
19128 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19129 {
19130 struct comp_unit_head cu_header_local;
19131 const struct comp_unit_head *cu_headerp;
19132
19133 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19134
19135 if (cu_headerp->version == 2)
19136 return cu_headerp->addr_size;
19137 else
19138 return cu_headerp->offset_size;
19139 }
19140
19141 /* Return the text offset of the CU. The returned offset comes from
19142 this CU's objfile. If this objfile came from a separate debuginfo
19143 file, then the offset may be different from the corresponding
19144 offset in the parent objfile. */
19145
19146 CORE_ADDR
19147 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19148 {
19149 struct objfile *objfile = per_cu->objfile;
19150
19151 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19152 }
19153
19154 /* Locate the .debug_info compilation unit from CU's objfile which contains
19155 the DIE at OFFSET. Raises an error on failure. */
19156
19157 static struct dwarf2_per_cu_data *
19158 dwarf2_find_containing_comp_unit (sect_offset offset,
19159 unsigned int offset_in_dwz,
19160 struct objfile *objfile)
19161 {
19162 struct dwarf2_per_cu_data *this_cu;
19163 int low, high;
19164 const sect_offset *cu_off;
19165
19166 low = 0;
19167 high = dwarf2_per_objfile->n_comp_units - 1;
19168 while (high > low)
19169 {
19170 struct dwarf2_per_cu_data *mid_cu;
19171 int mid = low + (high - low) / 2;
19172
19173 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19174 cu_off = &mid_cu->offset;
19175 if (mid_cu->is_dwz > offset_in_dwz
19176 || (mid_cu->is_dwz == offset_in_dwz
19177 && cu_off->sect_off >= offset.sect_off))
19178 high = mid;
19179 else
19180 low = mid + 1;
19181 }
19182 gdb_assert (low == high);
19183 this_cu = dwarf2_per_objfile->all_comp_units[low];
19184 cu_off = &this_cu->offset;
19185 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19186 {
19187 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19188 error (_("Dwarf Error: could not find partial DIE containing "
19189 "offset 0x%lx [in module %s]"),
19190 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19191
19192 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19193 <= offset.sect_off);
19194 return dwarf2_per_objfile->all_comp_units[low-1];
19195 }
19196 else
19197 {
19198 this_cu = dwarf2_per_objfile->all_comp_units[low];
19199 if (low == dwarf2_per_objfile->n_comp_units - 1
19200 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19201 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19202 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19203 return this_cu;
19204 }
19205 }
19206
19207 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19208
19209 static void
19210 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19211 {
19212 memset (cu, 0, sizeof (*cu));
19213 per_cu->cu = cu;
19214 cu->per_cu = per_cu;
19215 cu->objfile = per_cu->objfile;
19216 obstack_init (&cu->comp_unit_obstack);
19217 }
19218
19219 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19220
19221 static void
19222 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19223 enum language pretend_language)
19224 {
19225 struct attribute *attr;
19226
19227 /* Set the language we're debugging. */
19228 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19229 if (attr)
19230 set_cu_language (DW_UNSND (attr), cu);
19231 else
19232 {
19233 cu->language = pretend_language;
19234 cu->language_defn = language_def (cu->language);
19235 }
19236
19237 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19238 if (attr)
19239 cu->producer = DW_STRING (attr);
19240 }
19241
19242 /* Release one cached compilation unit, CU. We unlink it from the tree
19243 of compilation units, but we don't remove it from the read_in_chain;
19244 the caller is responsible for that.
19245 NOTE: DATA is a void * because this function is also used as a
19246 cleanup routine. */
19247
19248 static void
19249 free_heap_comp_unit (void *data)
19250 {
19251 struct dwarf2_cu *cu = data;
19252
19253 gdb_assert (cu->per_cu != NULL);
19254 cu->per_cu->cu = NULL;
19255 cu->per_cu = NULL;
19256
19257 obstack_free (&cu->comp_unit_obstack, NULL);
19258
19259 xfree (cu);
19260 }
19261
19262 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19263 when we're finished with it. We can't free the pointer itself, but be
19264 sure to unlink it from the cache. Also release any associated storage. */
19265
19266 static void
19267 free_stack_comp_unit (void *data)
19268 {
19269 struct dwarf2_cu *cu = data;
19270
19271 gdb_assert (cu->per_cu != NULL);
19272 cu->per_cu->cu = NULL;
19273 cu->per_cu = NULL;
19274
19275 obstack_free (&cu->comp_unit_obstack, NULL);
19276 cu->partial_dies = NULL;
19277 }
19278
19279 /* Free all cached compilation units. */
19280
19281 static void
19282 free_cached_comp_units (void *data)
19283 {
19284 struct dwarf2_per_cu_data *per_cu, **last_chain;
19285
19286 per_cu = dwarf2_per_objfile->read_in_chain;
19287 last_chain = &dwarf2_per_objfile->read_in_chain;
19288 while (per_cu != NULL)
19289 {
19290 struct dwarf2_per_cu_data *next_cu;
19291
19292 next_cu = per_cu->cu->read_in_chain;
19293
19294 free_heap_comp_unit (per_cu->cu);
19295 *last_chain = next_cu;
19296
19297 per_cu = next_cu;
19298 }
19299 }
19300
19301 /* Increase the age counter on each cached compilation unit, and free
19302 any that are too old. */
19303
19304 static void
19305 age_cached_comp_units (void)
19306 {
19307 struct dwarf2_per_cu_data *per_cu, **last_chain;
19308
19309 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19310 per_cu = dwarf2_per_objfile->read_in_chain;
19311 while (per_cu != NULL)
19312 {
19313 per_cu->cu->last_used ++;
19314 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19315 dwarf2_mark (per_cu->cu);
19316 per_cu = per_cu->cu->read_in_chain;
19317 }
19318
19319 per_cu = dwarf2_per_objfile->read_in_chain;
19320 last_chain = &dwarf2_per_objfile->read_in_chain;
19321 while (per_cu != NULL)
19322 {
19323 struct dwarf2_per_cu_data *next_cu;
19324
19325 next_cu = per_cu->cu->read_in_chain;
19326
19327 if (!per_cu->cu->mark)
19328 {
19329 free_heap_comp_unit (per_cu->cu);
19330 *last_chain = next_cu;
19331 }
19332 else
19333 last_chain = &per_cu->cu->read_in_chain;
19334
19335 per_cu = next_cu;
19336 }
19337 }
19338
19339 /* Remove a single compilation unit from the cache. */
19340
19341 static void
19342 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19343 {
19344 struct dwarf2_per_cu_data *per_cu, **last_chain;
19345
19346 per_cu = dwarf2_per_objfile->read_in_chain;
19347 last_chain = &dwarf2_per_objfile->read_in_chain;
19348 while (per_cu != NULL)
19349 {
19350 struct dwarf2_per_cu_data *next_cu;
19351
19352 next_cu = per_cu->cu->read_in_chain;
19353
19354 if (per_cu == target_per_cu)
19355 {
19356 free_heap_comp_unit (per_cu->cu);
19357 per_cu->cu = NULL;
19358 *last_chain = next_cu;
19359 break;
19360 }
19361 else
19362 last_chain = &per_cu->cu->read_in_chain;
19363
19364 per_cu = next_cu;
19365 }
19366 }
19367
19368 /* Release all extra memory associated with OBJFILE. */
19369
19370 void
19371 dwarf2_free_objfile (struct objfile *objfile)
19372 {
19373 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19374
19375 if (dwarf2_per_objfile == NULL)
19376 return;
19377
19378 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19379 free_cached_comp_units (NULL);
19380
19381 if (dwarf2_per_objfile->quick_file_names_table)
19382 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19383
19384 /* Everything else should be on the objfile obstack. */
19385 }
19386
19387 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19388 We store these in a hash table separate from the DIEs, and preserve them
19389 when the DIEs are flushed out of cache.
19390
19391 The CU "per_cu" pointer is needed because offset alone is not enough to
19392 uniquely identify the type. A file may have multiple .debug_types sections,
19393 or the type may come from a DWO file. We have to use something in
19394 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19395 routine, get_die_type_at_offset, from outside this file, and thus won't
19396 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19397 of the objfile. */
19398
19399 struct dwarf2_per_cu_offset_and_type
19400 {
19401 const struct dwarf2_per_cu_data *per_cu;
19402 sect_offset offset;
19403 struct type *type;
19404 };
19405
19406 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19407
19408 static hashval_t
19409 per_cu_offset_and_type_hash (const void *item)
19410 {
19411 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19412
19413 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19414 }
19415
19416 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19417
19418 static int
19419 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19420 {
19421 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19422 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19423
19424 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19425 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19426 }
19427
19428 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19429 table if necessary. For convenience, return TYPE.
19430
19431 The DIEs reading must have careful ordering to:
19432 * Not cause infite loops trying to read in DIEs as a prerequisite for
19433 reading current DIE.
19434 * Not trying to dereference contents of still incompletely read in types
19435 while reading in other DIEs.
19436 * Enable referencing still incompletely read in types just by a pointer to
19437 the type without accessing its fields.
19438
19439 Therefore caller should follow these rules:
19440 * Try to fetch any prerequisite types we may need to build this DIE type
19441 before building the type and calling set_die_type.
19442 * After building type call set_die_type for current DIE as soon as
19443 possible before fetching more types to complete the current type.
19444 * Make the type as complete as possible before fetching more types. */
19445
19446 static struct type *
19447 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19448 {
19449 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19450 struct objfile *objfile = cu->objfile;
19451
19452 /* For Ada types, make sure that the gnat-specific data is always
19453 initialized (if not already set). There are a few types where
19454 we should not be doing so, because the type-specific area is
19455 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19456 where the type-specific area is used to store the floatformat).
19457 But this is not a problem, because the gnat-specific information
19458 is actually not needed for these types. */
19459 if (need_gnat_info (cu)
19460 && TYPE_CODE (type) != TYPE_CODE_FUNC
19461 && TYPE_CODE (type) != TYPE_CODE_FLT
19462 && !HAVE_GNAT_AUX_INFO (type))
19463 INIT_GNAT_SPECIFIC (type);
19464
19465 if (dwarf2_per_objfile->die_type_hash == NULL)
19466 {
19467 dwarf2_per_objfile->die_type_hash =
19468 htab_create_alloc_ex (127,
19469 per_cu_offset_and_type_hash,
19470 per_cu_offset_and_type_eq,
19471 NULL,
19472 &objfile->objfile_obstack,
19473 hashtab_obstack_allocate,
19474 dummy_obstack_deallocate);
19475 }
19476
19477 ofs.per_cu = cu->per_cu;
19478 ofs.offset = die->offset;
19479 ofs.type = type;
19480 slot = (struct dwarf2_per_cu_offset_and_type **)
19481 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19482 if (*slot)
19483 complaint (&symfile_complaints,
19484 _("A problem internal to GDB: DIE 0x%x has type already set"),
19485 die->offset.sect_off);
19486 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19487 **slot = ofs;
19488 return type;
19489 }
19490
19491 /* Look up the type for the die at OFFSET in the appropriate type_hash
19492 table, or return NULL if the die does not have a saved type. */
19493
19494 static struct type *
19495 get_die_type_at_offset (sect_offset offset,
19496 struct dwarf2_per_cu_data *per_cu)
19497 {
19498 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19499
19500 if (dwarf2_per_objfile->die_type_hash == NULL)
19501 return NULL;
19502
19503 ofs.per_cu = per_cu;
19504 ofs.offset = offset;
19505 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19506 if (slot)
19507 return slot->type;
19508 else
19509 return NULL;
19510 }
19511
19512 /* Look up the type for DIE in the appropriate type_hash table,
19513 or return NULL if DIE does not have a saved type. */
19514
19515 static struct type *
19516 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19517 {
19518 return get_die_type_at_offset (die->offset, cu->per_cu);
19519 }
19520
19521 /* Add a dependence relationship from CU to REF_PER_CU. */
19522
19523 static void
19524 dwarf2_add_dependence (struct dwarf2_cu *cu,
19525 struct dwarf2_per_cu_data *ref_per_cu)
19526 {
19527 void **slot;
19528
19529 if (cu->dependencies == NULL)
19530 cu->dependencies
19531 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19532 NULL, &cu->comp_unit_obstack,
19533 hashtab_obstack_allocate,
19534 dummy_obstack_deallocate);
19535
19536 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19537 if (*slot == NULL)
19538 *slot = ref_per_cu;
19539 }
19540
19541 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19542 Set the mark field in every compilation unit in the
19543 cache that we must keep because we are keeping CU. */
19544
19545 static int
19546 dwarf2_mark_helper (void **slot, void *data)
19547 {
19548 struct dwarf2_per_cu_data *per_cu;
19549
19550 per_cu = (struct dwarf2_per_cu_data *) *slot;
19551
19552 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19553 reading of the chain. As such dependencies remain valid it is not much
19554 useful to track and undo them during QUIT cleanups. */
19555 if (per_cu->cu == NULL)
19556 return 1;
19557
19558 if (per_cu->cu->mark)
19559 return 1;
19560 per_cu->cu->mark = 1;
19561
19562 if (per_cu->cu->dependencies != NULL)
19563 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19564
19565 return 1;
19566 }
19567
19568 /* Set the mark field in CU and in every other compilation unit in the
19569 cache that we must keep because we are keeping CU. */
19570
19571 static void
19572 dwarf2_mark (struct dwarf2_cu *cu)
19573 {
19574 if (cu->mark)
19575 return;
19576 cu->mark = 1;
19577 if (cu->dependencies != NULL)
19578 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19579 }
19580
19581 static void
19582 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19583 {
19584 while (per_cu)
19585 {
19586 per_cu->cu->mark = 0;
19587 per_cu = per_cu->cu->read_in_chain;
19588 }
19589 }
19590
19591 /* Trivial hash function for partial_die_info: the hash value of a DIE
19592 is its offset in .debug_info for this objfile. */
19593
19594 static hashval_t
19595 partial_die_hash (const void *item)
19596 {
19597 const struct partial_die_info *part_die = item;
19598
19599 return part_die->offset.sect_off;
19600 }
19601
19602 /* Trivial comparison function for partial_die_info structures: two DIEs
19603 are equal if they have the same offset. */
19604
19605 static int
19606 partial_die_eq (const void *item_lhs, const void *item_rhs)
19607 {
19608 const struct partial_die_info *part_die_lhs = item_lhs;
19609 const struct partial_die_info *part_die_rhs = item_rhs;
19610
19611 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19612 }
19613
19614 static struct cmd_list_element *set_dwarf2_cmdlist;
19615 static struct cmd_list_element *show_dwarf2_cmdlist;
19616
19617 static void
19618 set_dwarf2_cmd (char *args, int from_tty)
19619 {
19620 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19621 }
19622
19623 static void
19624 show_dwarf2_cmd (char *args, int from_tty)
19625 {
19626 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19627 }
19628
19629 /* Free data associated with OBJFILE, if necessary. */
19630
19631 static void
19632 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19633 {
19634 struct dwarf2_per_objfile *data = d;
19635 int ix;
19636
19637 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19638 VEC_free (dwarf2_per_cu_ptr,
19639 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19640
19641 VEC_free (dwarf2_section_info_def, data->types);
19642
19643 if (data->dwo_files)
19644 free_dwo_files (data->dwo_files, objfile);
19645
19646 if (data->dwz_file && data->dwz_file->dwz_bfd)
19647 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19648 }
19649
19650 \f
19651 /* The "save gdb-index" command. */
19652
19653 /* The contents of the hash table we create when building the string
19654 table. */
19655 struct strtab_entry
19656 {
19657 offset_type offset;
19658 const char *str;
19659 };
19660
19661 /* Hash function for a strtab_entry.
19662
19663 Function is used only during write_hash_table so no index format backward
19664 compatibility is needed. */
19665
19666 static hashval_t
19667 hash_strtab_entry (const void *e)
19668 {
19669 const struct strtab_entry *entry = e;
19670 return mapped_index_string_hash (INT_MAX, entry->str);
19671 }
19672
19673 /* Equality function for a strtab_entry. */
19674
19675 static int
19676 eq_strtab_entry (const void *a, const void *b)
19677 {
19678 const struct strtab_entry *ea = a;
19679 const struct strtab_entry *eb = b;
19680 return !strcmp (ea->str, eb->str);
19681 }
19682
19683 /* Create a strtab_entry hash table. */
19684
19685 static htab_t
19686 create_strtab (void)
19687 {
19688 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19689 xfree, xcalloc, xfree);
19690 }
19691
19692 /* Add a string to the constant pool. Return the string's offset in
19693 host order. */
19694
19695 static offset_type
19696 add_string (htab_t table, struct obstack *cpool, const char *str)
19697 {
19698 void **slot;
19699 struct strtab_entry entry;
19700 struct strtab_entry *result;
19701
19702 entry.str = str;
19703 slot = htab_find_slot (table, &entry, INSERT);
19704 if (*slot)
19705 result = *slot;
19706 else
19707 {
19708 result = XNEW (struct strtab_entry);
19709 result->offset = obstack_object_size (cpool);
19710 result->str = str;
19711 obstack_grow_str0 (cpool, str);
19712 *slot = result;
19713 }
19714 return result->offset;
19715 }
19716
19717 /* An entry in the symbol table. */
19718 struct symtab_index_entry
19719 {
19720 /* The name of the symbol. */
19721 const char *name;
19722 /* The offset of the name in the constant pool. */
19723 offset_type index_offset;
19724 /* A sorted vector of the indices of all the CUs that hold an object
19725 of this name. */
19726 VEC (offset_type) *cu_indices;
19727 };
19728
19729 /* The symbol table. This is a power-of-2-sized hash table. */
19730 struct mapped_symtab
19731 {
19732 offset_type n_elements;
19733 offset_type size;
19734 struct symtab_index_entry **data;
19735 };
19736
19737 /* Hash function for a symtab_index_entry. */
19738
19739 static hashval_t
19740 hash_symtab_entry (const void *e)
19741 {
19742 const struct symtab_index_entry *entry = e;
19743 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19744 sizeof (offset_type) * VEC_length (offset_type,
19745 entry->cu_indices),
19746 0);
19747 }
19748
19749 /* Equality function for a symtab_index_entry. */
19750
19751 static int
19752 eq_symtab_entry (const void *a, const void *b)
19753 {
19754 const struct symtab_index_entry *ea = a;
19755 const struct symtab_index_entry *eb = b;
19756 int len = VEC_length (offset_type, ea->cu_indices);
19757 if (len != VEC_length (offset_type, eb->cu_indices))
19758 return 0;
19759 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19760 VEC_address (offset_type, eb->cu_indices),
19761 sizeof (offset_type) * len);
19762 }
19763
19764 /* Destroy a symtab_index_entry. */
19765
19766 static void
19767 delete_symtab_entry (void *p)
19768 {
19769 struct symtab_index_entry *entry = p;
19770 VEC_free (offset_type, entry->cu_indices);
19771 xfree (entry);
19772 }
19773
19774 /* Create a hash table holding symtab_index_entry objects. */
19775
19776 static htab_t
19777 create_symbol_hash_table (void)
19778 {
19779 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19780 delete_symtab_entry, xcalloc, xfree);
19781 }
19782
19783 /* Create a new mapped symtab object. */
19784
19785 static struct mapped_symtab *
19786 create_mapped_symtab (void)
19787 {
19788 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19789 symtab->n_elements = 0;
19790 symtab->size = 1024;
19791 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19792 return symtab;
19793 }
19794
19795 /* Destroy a mapped_symtab. */
19796
19797 static void
19798 cleanup_mapped_symtab (void *p)
19799 {
19800 struct mapped_symtab *symtab = p;
19801 /* The contents of the array are freed when the other hash table is
19802 destroyed. */
19803 xfree (symtab->data);
19804 xfree (symtab);
19805 }
19806
19807 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19808 the slot.
19809
19810 Function is used only during write_hash_table so no index format backward
19811 compatibility is needed. */
19812
19813 static struct symtab_index_entry **
19814 find_slot (struct mapped_symtab *symtab, const char *name)
19815 {
19816 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19817
19818 index = hash & (symtab->size - 1);
19819 step = ((hash * 17) & (symtab->size - 1)) | 1;
19820
19821 for (;;)
19822 {
19823 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19824 return &symtab->data[index];
19825 index = (index + step) & (symtab->size - 1);
19826 }
19827 }
19828
19829 /* Expand SYMTAB's hash table. */
19830
19831 static void
19832 hash_expand (struct mapped_symtab *symtab)
19833 {
19834 offset_type old_size = symtab->size;
19835 offset_type i;
19836 struct symtab_index_entry **old_entries = symtab->data;
19837
19838 symtab->size *= 2;
19839 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19840
19841 for (i = 0; i < old_size; ++i)
19842 {
19843 if (old_entries[i])
19844 {
19845 struct symtab_index_entry **slot = find_slot (symtab,
19846 old_entries[i]->name);
19847 *slot = old_entries[i];
19848 }
19849 }
19850
19851 xfree (old_entries);
19852 }
19853
19854 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19855 CU_INDEX is the index of the CU in which the symbol appears.
19856 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19857
19858 static void
19859 add_index_entry (struct mapped_symtab *symtab, const char *name,
19860 int is_static, gdb_index_symbol_kind kind,
19861 offset_type cu_index)
19862 {
19863 struct symtab_index_entry **slot;
19864 offset_type cu_index_and_attrs;
19865
19866 ++symtab->n_elements;
19867 if (4 * symtab->n_elements / 3 >= symtab->size)
19868 hash_expand (symtab);
19869
19870 slot = find_slot (symtab, name);
19871 if (!*slot)
19872 {
19873 *slot = XNEW (struct symtab_index_entry);
19874 (*slot)->name = name;
19875 /* index_offset is set later. */
19876 (*slot)->cu_indices = NULL;
19877 }
19878
19879 cu_index_and_attrs = 0;
19880 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19881 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19882 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19883
19884 /* We don't want to record an index value twice as we want to avoid the
19885 duplication.
19886 We process all global symbols and then all static symbols
19887 (which would allow us to avoid the duplication by only having to check
19888 the last entry pushed), but a symbol could have multiple kinds in one CU.
19889 To keep things simple we don't worry about the duplication here and
19890 sort and uniqufy the list after we've processed all symbols. */
19891 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19892 }
19893
19894 /* qsort helper routine for uniquify_cu_indices. */
19895
19896 static int
19897 offset_type_compare (const void *ap, const void *bp)
19898 {
19899 offset_type a = *(offset_type *) ap;
19900 offset_type b = *(offset_type *) bp;
19901
19902 return (a > b) - (b > a);
19903 }
19904
19905 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19906
19907 static void
19908 uniquify_cu_indices (struct mapped_symtab *symtab)
19909 {
19910 int i;
19911
19912 for (i = 0; i < symtab->size; ++i)
19913 {
19914 struct symtab_index_entry *entry = symtab->data[i];
19915
19916 if (entry
19917 && entry->cu_indices != NULL)
19918 {
19919 unsigned int next_to_insert, next_to_check;
19920 offset_type last_value;
19921
19922 qsort (VEC_address (offset_type, entry->cu_indices),
19923 VEC_length (offset_type, entry->cu_indices),
19924 sizeof (offset_type), offset_type_compare);
19925
19926 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19927 next_to_insert = 1;
19928 for (next_to_check = 1;
19929 next_to_check < VEC_length (offset_type, entry->cu_indices);
19930 ++next_to_check)
19931 {
19932 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19933 != last_value)
19934 {
19935 last_value = VEC_index (offset_type, entry->cu_indices,
19936 next_to_check);
19937 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19938 last_value);
19939 ++next_to_insert;
19940 }
19941 }
19942 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19943 }
19944 }
19945 }
19946
19947 /* Add a vector of indices to the constant pool. */
19948
19949 static offset_type
19950 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19951 struct symtab_index_entry *entry)
19952 {
19953 void **slot;
19954
19955 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19956 if (!*slot)
19957 {
19958 offset_type len = VEC_length (offset_type, entry->cu_indices);
19959 offset_type val = MAYBE_SWAP (len);
19960 offset_type iter;
19961 int i;
19962
19963 *slot = entry;
19964 entry->index_offset = obstack_object_size (cpool);
19965
19966 obstack_grow (cpool, &val, sizeof (val));
19967 for (i = 0;
19968 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19969 ++i)
19970 {
19971 val = MAYBE_SWAP (iter);
19972 obstack_grow (cpool, &val, sizeof (val));
19973 }
19974 }
19975 else
19976 {
19977 struct symtab_index_entry *old_entry = *slot;
19978 entry->index_offset = old_entry->index_offset;
19979 entry = old_entry;
19980 }
19981 return entry->index_offset;
19982 }
19983
19984 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19985 constant pool entries going into the obstack CPOOL. */
19986
19987 static void
19988 write_hash_table (struct mapped_symtab *symtab,
19989 struct obstack *output, struct obstack *cpool)
19990 {
19991 offset_type i;
19992 htab_t symbol_hash_table;
19993 htab_t str_table;
19994
19995 symbol_hash_table = create_symbol_hash_table ();
19996 str_table = create_strtab ();
19997
19998 /* We add all the index vectors to the constant pool first, to
19999 ensure alignment is ok. */
20000 for (i = 0; i < symtab->size; ++i)
20001 {
20002 if (symtab->data[i])
20003 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20004 }
20005
20006 /* Now write out the hash table. */
20007 for (i = 0; i < symtab->size; ++i)
20008 {
20009 offset_type str_off, vec_off;
20010
20011 if (symtab->data[i])
20012 {
20013 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20014 vec_off = symtab->data[i]->index_offset;
20015 }
20016 else
20017 {
20018 /* While 0 is a valid constant pool index, it is not valid
20019 to have 0 for both offsets. */
20020 str_off = 0;
20021 vec_off = 0;
20022 }
20023
20024 str_off = MAYBE_SWAP (str_off);
20025 vec_off = MAYBE_SWAP (vec_off);
20026
20027 obstack_grow (output, &str_off, sizeof (str_off));
20028 obstack_grow (output, &vec_off, sizeof (vec_off));
20029 }
20030
20031 htab_delete (str_table);
20032 htab_delete (symbol_hash_table);
20033 }
20034
20035 /* Struct to map psymtab to CU index in the index file. */
20036 struct psymtab_cu_index_map
20037 {
20038 struct partial_symtab *psymtab;
20039 unsigned int cu_index;
20040 };
20041
20042 static hashval_t
20043 hash_psymtab_cu_index (const void *item)
20044 {
20045 const struct psymtab_cu_index_map *map = item;
20046
20047 return htab_hash_pointer (map->psymtab);
20048 }
20049
20050 static int
20051 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20052 {
20053 const struct psymtab_cu_index_map *lhs = item_lhs;
20054 const struct psymtab_cu_index_map *rhs = item_rhs;
20055
20056 return lhs->psymtab == rhs->psymtab;
20057 }
20058
20059 /* Helper struct for building the address table. */
20060 struct addrmap_index_data
20061 {
20062 struct objfile *objfile;
20063 struct obstack *addr_obstack;
20064 htab_t cu_index_htab;
20065
20066 /* Non-zero if the previous_* fields are valid.
20067 We can't write an entry until we see the next entry (since it is only then
20068 that we know the end of the entry). */
20069 int previous_valid;
20070 /* Index of the CU in the table of all CUs in the index file. */
20071 unsigned int previous_cu_index;
20072 /* Start address of the CU. */
20073 CORE_ADDR previous_cu_start;
20074 };
20075
20076 /* Write an address entry to OBSTACK. */
20077
20078 static void
20079 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20080 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20081 {
20082 offset_type cu_index_to_write;
20083 char addr[8];
20084 CORE_ADDR baseaddr;
20085
20086 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20087
20088 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20089 obstack_grow (obstack, addr, 8);
20090 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20091 obstack_grow (obstack, addr, 8);
20092 cu_index_to_write = MAYBE_SWAP (cu_index);
20093 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20094 }
20095
20096 /* Worker function for traversing an addrmap to build the address table. */
20097
20098 static int
20099 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20100 {
20101 struct addrmap_index_data *data = datap;
20102 struct partial_symtab *pst = obj;
20103
20104 if (data->previous_valid)
20105 add_address_entry (data->objfile, data->addr_obstack,
20106 data->previous_cu_start, start_addr,
20107 data->previous_cu_index);
20108
20109 data->previous_cu_start = start_addr;
20110 if (pst != NULL)
20111 {
20112 struct psymtab_cu_index_map find_map, *map;
20113 find_map.psymtab = pst;
20114 map = htab_find (data->cu_index_htab, &find_map);
20115 gdb_assert (map != NULL);
20116 data->previous_cu_index = map->cu_index;
20117 data->previous_valid = 1;
20118 }
20119 else
20120 data->previous_valid = 0;
20121
20122 return 0;
20123 }
20124
20125 /* Write OBJFILE's address map to OBSTACK.
20126 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20127 in the index file. */
20128
20129 static void
20130 write_address_map (struct objfile *objfile, struct obstack *obstack,
20131 htab_t cu_index_htab)
20132 {
20133 struct addrmap_index_data addrmap_index_data;
20134
20135 /* When writing the address table, we have to cope with the fact that
20136 the addrmap iterator only provides the start of a region; we have to
20137 wait until the next invocation to get the start of the next region. */
20138
20139 addrmap_index_data.objfile = objfile;
20140 addrmap_index_data.addr_obstack = obstack;
20141 addrmap_index_data.cu_index_htab = cu_index_htab;
20142 addrmap_index_data.previous_valid = 0;
20143
20144 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20145 &addrmap_index_data);
20146
20147 /* It's highly unlikely the last entry (end address = 0xff...ff)
20148 is valid, but we should still handle it.
20149 The end address is recorded as the start of the next region, but that
20150 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20151 anyway. */
20152 if (addrmap_index_data.previous_valid)
20153 add_address_entry (objfile, obstack,
20154 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20155 addrmap_index_data.previous_cu_index);
20156 }
20157
20158 /* Return the symbol kind of PSYM. */
20159
20160 static gdb_index_symbol_kind
20161 symbol_kind (struct partial_symbol *psym)
20162 {
20163 domain_enum domain = PSYMBOL_DOMAIN (psym);
20164 enum address_class aclass = PSYMBOL_CLASS (psym);
20165
20166 switch (domain)
20167 {
20168 case VAR_DOMAIN:
20169 switch (aclass)
20170 {
20171 case LOC_BLOCK:
20172 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20173 case LOC_TYPEDEF:
20174 return GDB_INDEX_SYMBOL_KIND_TYPE;
20175 case LOC_COMPUTED:
20176 case LOC_CONST_BYTES:
20177 case LOC_OPTIMIZED_OUT:
20178 case LOC_STATIC:
20179 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20180 case LOC_CONST:
20181 /* Note: It's currently impossible to recognize psyms as enum values
20182 short of reading the type info. For now punt. */
20183 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20184 default:
20185 /* There are other LOC_FOO values that one might want to classify
20186 as variables, but dwarf2read.c doesn't currently use them. */
20187 return GDB_INDEX_SYMBOL_KIND_OTHER;
20188 }
20189 case STRUCT_DOMAIN:
20190 return GDB_INDEX_SYMBOL_KIND_TYPE;
20191 default:
20192 return GDB_INDEX_SYMBOL_KIND_OTHER;
20193 }
20194 }
20195
20196 /* Add a list of partial symbols to SYMTAB. */
20197
20198 static void
20199 write_psymbols (struct mapped_symtab *symtab,
20200 htab_t psyms_seen,
20201 struct partial_symbol **psymp,
20202 int count,
20203 offset_type cu_index,
20204 int is_static)
20205 {
20206 for (; count-- > 0; ++psymp)
20207 {
20208 struct partial_symbol *psym = *psymp;
20209 void **slot;
20210
20211 if (SYMBOL_LANGUAGE (psym) == language_ada)
20212 error (_("Ada is not currently supported by the index"));
20213
20214 /* Only add a given psymbol once. */
20215 slot = htab_find_slot (psyms_seen, psym, INSERT);
20216 if (!*slot)
20217 {
20218 gdb_index_symbol_kind kind = symbol_kind (psym);
20219
20220 *slot = psym;
20221 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20222 is_static, kind, cu_index);
20223 }
20224 }
20225 }
20226
20227 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20228 exception if there is an error. */
20229
20230 static void
20231 write_obstack (FILE *file, struct obstack *obstack)
20232 {
20233 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20234 file)
20235 != obstack_object_size (obstack))
20236 error (_("couldn't data write to file"));
20237 }
20238
20239 /* Unlink a file if the argument is not NULL. */
20240
20241 static void
20242 unlink_if_set (void *p)
20243 {
20244 char **filename = p;
20245 if (*filename)
20246 unlink (*filename);
20247 }
20248
20249 /* A helper struct used when iterating over debug_types. */
20250 struct signatured_type_index_data
20251 {
20252 struct objfile *objfile;
20253 struct mapped_symtab *symtab;
20254 struct obstack *types_list;
20255 htab_t psyms_seen;
20256 int cu_index;
20257 };
20258
20259 /* A helper function that writes a single signatured_type to an
20260 obstack. */
20261
20262 static int
20263 write_one_signatured_type (void **slot, void *d)
20264 {
20265 struct signatured_type_index_data *info = d;
20266 struct signatured_type *entry = (struct signatured_type *) *slot;
20267 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20268 struct partial_symtab *psymtab = per_cu->v.psymtab;
20269 gdb_byte val[8];
20270
20271 write_psymbols (info->symtab,
20272 info->psyms_seen,
20273 info->objfile->global_psymbols.list
20274 + psymtab->globals_offset,
20275 psymtab->n_global_syms, info->cu_index,
20276 0);
20277 write_psymbols (info->symtab,
20278 info->psyms_seen,
20279 info->objfile->static_psymbols.list
20280 + psymtab->statics_offset,
20281 psymtab->n_static_syms, info->cu_index,
20282 1);
20283
20284 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20285 entry->per_cu.offset.sect_off);
20286 obstack_grow (info->types_list, val, 8);
20287 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20288 entry->type_offset_in_tu.cu_off);
20289 obstack_grow (info->types_list, val, 8);
20290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20291 obstack_grow (info->types_list, val, 8);
20292
20293 ++info->cu_index;
20294
20295 return 1;
20296 }
20297
20298 /* Recurse into all "included" dependencies and write their symbols as
20299 if they appeared in this psymtab. */
20300
20301 static void
20302 recursively_write_psymbols (struct objfile *objfile,
20303 struct partial_symtab *psymtab,
20304 struct mapped_symtab *symtab,
20305 htab_t psyms_seen,
20306 offset_type cu_index)
20307 {
20308 int i;
20309
20310 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20311 if (psymtab->dependencies[i]->user != NULL)
20312 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20313 symtab, psyms_seen, cu_index);
20314
20315 write_psymbols (symtab,
20316 psyms_seen,
20317 objfile->global_psymbols.list + psymtab->globals_offset,
20318 psymtab->n_global_syms, cu_index,
20319 0);
20320 write_psymbols (symtab,
20321 psyms_seen,
20322 objfile->static_psymbols.list + psymtab->statics_offset,
20323 psymtab->n_static_syms, cu_index,
20324 1);
20325 }
20326
20327 /* Create an index file for OBJFILE in the directory DIR. */
20328
20329 static void
20330 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20331 {
20332 struct cleanup *cleanup;
20333 char *filename, *cleanup_filename;
20334 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20335 struct obstack cu_list, types_cu_list;
20336 int i;
20337 FILE *out_file;
20338 struct mapped_symtab *symtab;
20339 offset_type val, size_of_contents, total_len;
20340 struct stat st;
20341 htab_t psyms_seen;
20342 htab_t cu_index_htab;
20343 struct psymtab_cu_index_map *psymtab_cu_index_map;
20344
20345 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20346 return;
20347
20348 if (dwarf2_per_objfile->using_index)
20349 error (_("Cannot use an index to create the index"));
20350
20351 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20352 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20353
20354 if (stat (objfile->name, &st) < 0)
20355 perror_with_name (objfile->name);
20356
20357 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20358 INDEX_SUFFIX, (char *) NULL);
20359 cleanup = make_cleanup (xfree, filename);
20360
20361 out_file = fopen (filename, "wb");
20362 if (!out_file)
20363 error (_("Can't open `%s' for writing"), filename);
20364
20365 cleanup_filename = filename;
20366 make_cleanup (unlink_if_set, &cleanup_filename);
20367
20368 symtab = create_mapped_symtab ();
20369 make_cleanup (cleanup_mapped_symtab, symtab);
20370
20371 obstack_init (&addr_obstack);
20372 make_cleanup_obstack_free (&addr_obstack);
20373
20374 obstack_init (&cu_list);
20375 make_cleanup_obstack_free (&cu_list);
20376
20377 obstack_init (&types_cu_list);
20378 make_cleanup_obstack_free (&types_cu_list);
20379
20380 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20381 NULL, xcalloc, xfree);
20382 make_cleanup_htab_delete (psyms_seen);
20383
20384 /* While we're scanning CU's create a table that maps a psymtab pointer
20385 (which is what addrmap records) to its index (which is what is recorded
20386 in the index file). This will later be needed to write the address
20387 table. */
20388 cu_index_htab = htab_create_alloc (100,
20389 hash_psymtab_cu_index,
20390 eq_psymtab_cu_index,
20391 NULL, xcalloc, xfree);
20392 make_cleanup_htab_delete (cu_index_htab);
20393 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20394 xmalloc (sizeof (struct psymtab_cu_index_map)
20395 * dwarf2_per_objfile->n_comp_units);
20396 make_cleanup (xfree, psymtab_cu_index_map);
20397
20398 /* The CU list is already sorted, so we don't need to do additional
20399 work here. Also, the debug_types entries do not appear in
20400 all_comp_units, but only in their own hash table. */
20401 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20402 {
20403 struct dwarf2_per_cu_data *per_cu
20404 = dwarf2_per_objfile->all_comp_units[i];
20405 struct partial_symtab *psymtab = per_cu->v.psymtab;
20406 gdb_byte val[8];
20407 struct psymtab_cu_index_map *map;
20408 void **slot;
20409
20410 if (psymtab->user == NULL)
20411 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20412
20413 map = &psymtab_cu_index_map[i];
20414 map->psymtab = psymtab;
20415 map->cu_index = i;
20416 slot = htab_find_slot (cu_index_htab, map, INSERT);
20417 gdb_assert (slot != NULL);
20418 gdb_assert (*slot == NULL);
20419 *slot = map;
20420
20421 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20422 per_cu->offset.sect_off);
20423 obstack_grow (&cu_list, val, 8);
20424 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20425 obstack_grow (&cu_list, val, 8);
20426 }
20427
20428 /* Dump the address map. */
20429 write_address_map (objfile, &addr_obstack, cu_index_htab);
20430
20431 /* Write out the .debug_type entries, if any. */
20432 if (dwarf2_per_objfile->signatured_types)
20433 {
20434 struct signatured_type_index_data sig_data;
20435
20436 sig_data.objfile = objfile;
20437 sig_data.symtab = symtab;
20438 sig_data.types_list = &types_cu_list;
20439 sig_data.psyms_seen = psyms_seen;
20440 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20441 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20442 write_one_signatured_type, &sig_data);
20443 }
20444
20445 /* Now that we've processed all symbols we can shrink their cu_indices
20446 lists. */
20447 uniquify_cu_indices (symtab);
20448
20449 obstack_init (&constant_pool);
20450 make_cleanup_obstack_free (&constant_pool);
20451 obstack_init (&symtab_obstack);
20452 make_cleanup_obstack_free (&symtab_obstack);
20453 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20454
20455 obstack_init (&contents);
20456 make_cleanup_obstack_free (&contents);
20457 size_of_contents = 6 * sizeof (offset_type);
20458 total_len = size_of_contents;
20459
20460 /* The version number. */
20461 val = MAYBE_SWAP (7);
20462 obstack_grow (&contents, &val, sizeof (val));
20463
20464 /* The offset of the CU list from the start of the file. */
20465 val = MAYBE_SWAP (total_len);
20466 obstack_grow (&contents, &val, sizeof (val));
20467 total_len += obstack_object_size (&cu_list);
20468
20469 /* The offset of the types CU list from the start of the file. */
20470 val = MAYBE_SWAP (total_len);
20471 obstack_grow (&contents, &val, sizeof (val));
20472 total_len += obstack_object_size (&types_cu_list);
20473
20474 /* The offset of the address table from the start of the file. */
20475 val = MAYBE_SWAP (total_len);
20476 obstack_grow (&contents, &val, sizeof (val));
20477 total_len += obstack_object_size (&addr_obstack);
20478
20479 /* The offset of the symbol table from the start of the file. */
20480 val = MAYBE_SWAP (total_len);
20481 obstack_grow (&contents, &val, sizeof (val));
20482 total_len += obstack_object_size (&symtab_obstack);
20483
20484 /* The offset of the constant pool from the start of the file. */
20485 val = MAYBE_SWAP (total_len);
20486 obstack_grow (&contents, &val, sizeof (val));
20487 total_len += obstack_object_size (&constant_pool);
20488
20489 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20490
20491 write_obstack (out_file, &contents);
20492 write_obstack (out_file, &cu_list);
20493 write_obstack (out_file, &types_cu_list);
20494 write_obstack (out_file, &addr_obstack);
20495 write_obstack (out_file, &symtab_obstack);
20496 write_obstack (out_file, &constant_pool);
20497
20498 fclose (out_file);
20499
20500 /* We want to keep the file, so we set cleanup_filename to NULL
20501 here. See unlink_if_set. */
20502 cleanup_filename = NULL;
20503
20504 do_cleanups (cleanup);
20505 }
20506
20507 /* Implementation of the `save gdb-index' command.
20508
20509 Note that the file format used by this command is documented in the
20510 GDB manual. Any changes here must be documented there. */
20511
20512 static void
20513 save_gdb_index_command (char *arg, int from_tty)
20514 {
20515 struct objfile *objfile;
20516
20517 if (!arg || !*arg)
20518 error (_("usage: save gdb-index DIRECTORY"));
20519
20520 ALL_OBJFILES (objfile)
20521 {
20522 struct stat st;
20523
20524 /* If the objfile does not correspond to an actual file, skip it. */
20525 if (stat (objfile->name, &st) < 0)
20526 continue;
20527
20528 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20529 if (dwarf2_per_objfile)
20530 {
20531 volatile struct gdb_exception except;
20532
20533 TRY_CATCH (except, RETURN_MASK_ERROR)
20534 {
20535 write_psymtabs_to_index (objfile, arg);
20536 }
20537 if (except.reason < 0)
20538 exception_fprintf (gdb_stderr, except,
20539 _("Error while writing index for `%s': "),
20540 objfile->name);
20541 }
20542 }
20543 }
20544
20545 \f
20546
20547 int dwarf2_always_disassemble;
20548
20549 static void
20550 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20551 struct cmd_list_element *c, const char *value)
20552 {
20553 fprintf_filtered (file,
20554 _("Whether to always disassemble "
20555 "DWARF expressions is %s.\n"),
20556 value);
20557 }
20558
20559 static void
20560 show_check_physname (struct ui_file *file, int from_tty,
20561 struct cmd_list_element *c, const char *value)
20562 {
20563 fprintf_filtered (file,
20564 _("Whether to check \"physname\" is %s.\n"),
20565 value);
20566 }
20567
20568 void _initialize_dwarf2_read (void);
20569
20570 void
20571 _initialize_dwarf2_read (void)
20572 {
20573 struct cmd_list_element *c;
20574
20575 dwarf2_objfile_data_key
20576 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20577
20578 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20579 Set DWARF 2 specific variables.\n\
20580 Configure DWARF 2 variables such as the cache size"),
20581 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20582 0/*allow-unknown*/, &maintenance_set_cmdlist);
20583
20584 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20585 Show DWARF 2 specific variables\n\
20586 Show DWARF 2 variables such as the cache size"),
20587 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20588 0/*allow-unknown*/, &maintenance_show_cmdlist);
20589
20590 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20591 &dwarf2_max_cache_age, _("\
20592 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20593 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20594 A higher limit means that cached compilation units will be stored\n\
20595 in memory longer, and more total memory will be used. Zero disables\n\
20596 caching, which can slow down startup."),
20597 NULL,
20598 show_dwarf2_max_cache_age,
20599 &set_dwarf2_cmdlist,
20600 &show_dwarf2_cmdlist);
20601
20602 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20603 &dwarf2_always_disassemble, _("\
20604 Set whether `info address' always disassembles DWARF expressions."), _("\
20605 Show whether `info address' always disassembles DWARF expressions."), _("\
20606 When enabled, DWARF expressions are always printed in an assembly-like\n\
20607 syntax. When disabled, expressions will be printed in a more\n\
20608 conversational style, when possible."),
20609 NULL,
20610 show_dwarf2_always_disassemble,
20611 &set_dwarf2_cmdlist,
20612 &show_dwarf2_cmdlist);
20613
20614 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20615 Set debugging of the dwarf2 reader."), _("\
20616 Show debugging of the dwarf2 reader."), _("\
20617 When enabled, debugging messages are printed during dwarf2 reading\n\
20618 and symtab expansion."),
20619 NULL,
20620 NULL,
20621 &setdebuglist, &showdebuglist);
20622
20623 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20624 Set debugging of the dwarf2 DIE reader."), _("\
20625 Show debugging of the dwarf2 DIE reader."), _("\
20626 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20627 The value is the maximum depth to print."),
20628 NULL,
20629 NULL,
20630 &setdebuglist, &showdebuglist);
20631
20632 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20633 Set cross-checking of \"physname\" code against demangler."), _("\
20634 Show cross-checking of \"physname\" code against demangler."), _("\
20635 When enabled, GDB's internal \"physname\" code is checked against\n\
20636 the demangler."),
20637 NULL, show_check_physname,
20638 &setdebuglist, &showdebuglist);
20639
20640 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20641 no_class, &use_deprecated_index_sections, _("\
20642 Set whether to use deprecated gdb_index sections."), _("\
20643 Show whether to use deprecated gdb_index sections."), _("\
20644 When enabled, deprecated .gdb_index sections are used anyway.\n\
20645 Normally they are ignored either because of a missing feature or\n\
20646 performance issue.\n\
20647 Warning: This option must be enabled before gdb reads the file."),
20648 NULL,
20649 NULL,
20650 &setlist, &showlist);
20651
20652 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20653 _("\
20654 Save a gdb-index file.\n\
20655 Usage: save gdb-index DIRECTORY"),
20656 &save_cmdlist);
20657 set_cmd_completer (c, filename_completer);
20658 }
This page took 0.534693 seconds and 4 git commands to generate.